Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ categories = ["network-programming", "web-programming::http-server"]

# 二进制走 CalVer,crate 走 SemVer —— 两者是两回事,卖给不同的人。
# 这里是 crate 的版本。
version = "0.91.0"
version = "0.92.0"

[workspace.dependencies]
# ── 内部 crate ───────────────────────────────────────────
Expand Down
1 change: 1 addition & 0 deletions crates/tw-api/msg-codes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ gw.content.refused
gw.content.refused_invisible_message
gw.content.refused_invisible_tool_result
gw.convert.failed
gw.convert.reasoning_history_incompatible
gw.convert.tool_unsendable
gw.convert.tools_unsendable
gw.count_tokens.bedrock_upstream
Expand Down
10 changes: 9 additions & 1 deletion crates/tw-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub use aliases::{Alias, Aliases};
pub use credential::{CredentialError, Header, Headers, Secret, SecretResolveError, auth_header};
pub use init::{generate_control_key, generate_initial, generate_key};
pub use limits::{KeyLimit, LimitMeasure, LimitPer, UsageLimit};
pub use model_specs::{ModelSpec, ResolvedSpec, Sourced, SpecSource};
pub use model_specs::{ModelSpec, ReasoningHistory, ResolvedSpec, Sourced, SpecSource};
pub use plugins::Plugin;
pub use proxy::{DIRECT, OnProxyFail, Proxy, ProxyKind, SYSTEM};
pub use validate::ValidationError;
Expand Down Expand Up @@ -192,6 +192,8 @@ impl Default for Provider {
aws: None,
protocol: None,
forward_client_identity: false,
reasoning_history: ReasoningHistory::Auto,
reasoning_history_group: None,
proxy: default_proxy(),
on_proxy_fail: OnProxyFail::default(),
models: Vec::new(),
Expand Down Expand Up @@ -776,6 +778,12 @@ pub fn parse_duration_secs(s: &str) -> Option<u64> {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Provider {
/// Request-side history format; model_specs can override it per model.
#[serde(default, skip_serializing_if = "is_default")]
pub reasoning_history: ReasoningHistory,
/// Explicitly share signed Chat history between compatible upstream identities.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reasoning_history_group: Option<String>,
pub name: String,
pub base_url: String,
/// API 密钥,按接口协议放进它认的请求头。可以写 `${ENV}`。**不做 keychain。**
Expand Down
15 changes: 15 additions & 0 deletions crates/tw-config/src/model_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@ use serde::{Deserialize, Serialize};

use crate::{Config, Provider};

/// Request-side reasoning-history capability. Independent of the API protocol.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ReasoningHistory {
#[default]
Auto,
ReasoningContent,
Reasoning,
ContentBlocks,
Disabled,
}

/// `providers[].model_specs` 的一项。每项都可选,但至少写一项(校验管)。
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ModelSpec {
/// Override this model's request-side reasoning-history format.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reasoning_history: Option<ReasoningHistory>,
/// 一次最多输入多少 token,也就是上下文窗口
#[serde(default, skip_serializing_if = "Option::is_none")]
pub context_window: Option<u32>,
Expand Down
30 changes: 30 additions & 0 deletions crates/tw-config/tests/manual/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const fn row(name: &'static str, kind: Kind, def: Def, doc: T2) -> Row {
fn protocols() -> Vec<&'static str> {
super::fields::<Protocol>()
}
fn reasoning_histories() -> Vec<&'static str> {
super::fields::<ReasoningHistory>()
}
fn billings() -> Vec<&'static str> {
super::fields::<Billing>()
}
Expand Down Expand Up @@ -650,6 +653,24 @@ pub fn sections() -> Vec<Section> {
"上游的接口格式。不写:官方地址按 `base_url` 识别(Bedrock 的推理地址是 `bedrock`),其余按 `anthropic` 处理。",
),
),
row(
"reasoning_history",
Kind::Enum(reasoning_histories),
Def::Is("auto"),
t(
"Request-side Chat reasoning history: auto uses verified endpoint defaults, otherwise keeps native content blocks for the same source and the existing reasoning_content field convention. Select reasoning-content, reasoning, content-blocks, or disabled for a compatible gateway. Model-specific settings take precedence. Signed history cannot cross an incompatible source or format.",
"Chat 请求的思考历史格式:auto 使用已验证的端点默认值,其余同来源保留原生内容块和既有的 reasoning_content 字段惯例。兼容网关可显式选择 reasoning-content、reasoning、content-blocks 或 disabled。模型级设置优先。带签名的历史不发送到来源或格式不兼容的上游。",
),
),
row(
"reasoning_history_group",
Kind::Str,
Def::Unset,
t(
"Explicit compatibility group for signed Chat history. Unset: isolated by upstream, model and configured credentials. Only share a group when the backends accept each other's signatures; blank is treated as unset.",
"带签名的 Chat 历史的显式兼容组。不写时按上游、模型及配置凭据隔离;只有确认后端相互接受签名时才共享组。空白视为未设置。",
),
),
row(
"forward_client_identity",
Kind::Bool,
Expand Down Expand Up @@ -783,6 +804,15 @@ pub fn sections() -> Vec<Section> {
// 每项都可选,至少写一项由校验管
ty: checked!(ModelSpec, "{}"),
rows: vec![
row(
"reasoning_history",
Kind::Enum(reasoning_histories),
Def::Unset,
t(
"Override this model's request-side reasoning-history format. Unset: use the upstream setting. This is independent of whether the model can reason.",
"覆盖这个模型的请求侧思考历史格式。不写时遵从上游设置;与模型会不会推理是两项独立能力。",
),
),
row(
"context_window",
Kind::Int,
Expand Down
14 changes: 12 additions & 2 deletions crates/tw-control/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,27 @@ async fn set_model_spec(
Json(req): Json<tw_api::ModelSpecSave>,
) -> Result<Json<tw_api::ConfigWritten>, Fail> {
let model = req.model.trim();
let spec = tw_config::ModelSpec {
let mut spec = tw_config::ModelSpec {
context_window: req.context_window,
max_output_tokens: req.max_output_tokens,
reasoning: req.reasoning,
image_input: req.image_input,
..Default::default()
};
let spec = (!spec.is_empty()).then_some(spec);
let version = s
.cfg
.transform(req.base_version.as_deref(), Origin::Ui, |text, cfg| {
if !cfg.providers.iter().any(|p| p.name == req.provider) {
return Err(not_found("upstream", &req.provider));
}
// The UI edits the four public specifications, not the advanced history override.
spec.reasoning_history = cfg
.providers
.iter()
.find(|p| p.name == req.provider)
.and_then(|p| p.model_specs.get(model))
.and_then(|s| s.reasoning_history);
let spec = (!spec.is_empty()).then_some(spec);
tw_config::check_model_spec(&req.provider, model, spec.as_ref())
.map_err(|e| invalid(e.msg()))?;
Ok(edit::set_model_spec(
Expand Down Expand Up @@ -543,6 +551,8 @@ fn to_provider(
aws,
protocol: input.protocol.map(Into::into),
forward_client_identity: input.forward_client_identity,
reasoning_history: existing.map(|e| e.reasoning_history).unwrap_or_default(),
reasoning_history_group: existing.and_then(|e| e.reasoning_history_group.clone()),
models: input
.models
.iter()
Expand Down
Loading
Loading