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
88 changes: 81 additions & 7 deletions crates/tw-dialect/src/chat/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,15 @@ fn user_parts(content: &Value, dropped: &mut Dropped) -> Vec<Part> {

fn assistant_parts(m: &Value, dropped: &mut Dropped) -> Vec<Part> {
let mut parts = Vec::new();
// DeepSeek 等实现的推理字段,不在 OpenAI 的定义里
if let Some(t) = str_of(m, "reasoning_content").filter(|t| !t.is_empty()) {
// DeepSeek 等实现的推理字段,不在 OpenAI 的定义里。DeepSeek 叫 reasoning_content,
// OpenRouter、vLLM 叫 reasoning
if let Some(t) = str_of(m, "reasoning_content")
.or_else(|| str_of(m, "reasoning"))
.filter(|t| !t.is_empty())
{
parts.push(Part::Thinking(Thinking {
text: t.to_string(),
signature: None,
signature: Some(Signature::new(Vendor::Chat, "")),
}));
}
match m.get("content") {
Expand Down Expand Up @@ -308,7 +312,10 @@ pub fn encode_request(r: &Request, t: &Target, dropped: &mut Dropped) -> Value {
if !r.system.is_empty() {
messages.push(json!({ "role": "system", "content": r.system.join("\n\n") }));
}
for m in &r.messages {
// 连着的几条助手消息并成一条:Responses 客户端一项一条消息,推理、文字、工具调用
// 各在一条里,而 DeepSeek 要 reasoning_content 和 tool_calls 写在同一条助手消息上。
// 用户那一侧不并:对话中途折成用户消息的系统提示要留在原位(见 `fold_system_turns`)
for m in &merge_assistant_turns(&r.messages) {
match m.role {
Role::User | Role::System => user_messages(m, dropped, &mut messages),
Role::Assistant => {
Expand Down Expand Up @@ -498,8 +505,23 @@ fn user_messages(m: &Message, dropped: &mut Dropped, out: &mut Vec<Value>) {
out.push(json!({ "role": "user", "content": content }));
}

/// 连着的助手消息并成一条,别的原样
fn merge_assistant_turns(messages: &[Message]) -> Vec<Message> {
let mut out: Vec<Message> = Vec::with_capacity(messages.len());
for m in messages {
match out.last_mut() {
Some(last) if last.role == Role::Assistant && m.role == Role::Assistant => {
last.parts.extend(m.parts.iter().cloned())
}
_ => out.push(m.clone()),
}
}
out
}

fn assistant_message(m: &Message, dropped: &mut Dropped) -> Option<Value> {
let mut texts = Vec::new();
let mut reasoning = Vec::new();
let mut calls = Vec::new();
for p in &m.parts {
match p {
Expand All @@ -509,7 +531,15 @@ fn assistant_message(m: &Message, dropped: &mut Dropped) -> Option<Value> {
"type": "function",
"function": { "name": c.name, "arguments": c.input.to_json_text() },
})),
Part::Thinking(_) => dropped.feature(Feature::ReasoningHistory),
// 只写回 Chat 上游自己给的推理:DeepSeek 在工具调用的那几轮要它原样带回;
// 别家签发的写过去没有意义,而且 OpenAI 自己的接口不认 reasoning_content
Part::Thinking(th) => match &th.signature {
Some(s) if s.vendor == Vendor::Chat && !th.text.is_empty() => {
reasoning.push(th.text.as_str())
}
Some(s) if s.vendor == Vendor::Chat => {}
_ => dropped.feature(Feature::ReasoningHistory),
},
_ => {}
}
}
Expand All @@ -520,6 +550,9 @@ fn assistant_message(m: &Message, dropped: &mut Dropped) -> Option<Value> {
"role": "assistant",
"content": if texts.is_empty() { Value::Null } else { json!(texts.join("\n")) },
});
if !reasoning.is_empty() {
msg["reasoning_content"] = json!(reasoning.join("\n\n"));
}
if !calls.is_empty() {
msg["tool_calls"] = Value::Array(calls);
}
Expand Down Expand Up @@ -614,13 +647,54 @@ mod tests {
m[3],
json!({"role": "tool", "tool_call_id": "call_1", "content": "放大了"})
);
// 兼容实现:max_tokens;推理内容不回传
// 兼容实现:max_tokens;Chat 上游给的推理内容原样写回
assert_eq!(v["max_tokens"], 2000);
assert_eq!(dropped, ["messages.content.thinking"]);
assert_eq!(m[2]["reasoning_content"], "看图");
assert!(dropped.is_empty(), "{dropped:?}");
assert_eq!(v["stream_options"]["include_usage"], true);
assert_eq!(v["response_format"]["json_schema"]["strict"], true);
}

#[test]
fn reasoning_signed_by_another_vendor_is_not_written_back() {
let r = Request {
model: "gpt-5".into(),
messages: vec![Message {
role: Role::Assistant,
parts: vec![
Part::Thinking(Thinking {
text: "想".into(),
signature: Some(Signature::new(Vendor::Anthropic, "sig")),
}),
Part::Thinking(Thinking {
text: "想".into(),
signature: None,
}),
Part::Text("答".into()),
],
}],
..Default::default()
};
let (v, dropped) = encode(&r, Dialect::Anthropic, true);
assert_eq!(v["messages"][0]["content"], "答");
assert!(v["messages"][0].get("reasoning_content").is_none());
assert_eq!(dropped, ["messages.content.thinking"]);
}

#[test]
fn reasoning_is_read_too() {
let (r, _, _) = decode(
r#"{"model": "m", "messages": [{"role": "assistant", "content": "答", "reasoning": "想"}]}"#,
);
assert_eq!(
r.messages[0].parts[0],
Part::Thinking(Thinking {
text: "想".into(),
signature: Some(Signature::new(Vendor::Chat, "")),
})
);
}

#[test]
fn the_official_endpoint_gets_max_completion_tokens() {
let (r, _, _) = decode(CLIENT);
Expand Down
16 changes: 13 additions & 3 deletions crates/tw-dialect/src/chat/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ pub fn decode_response(v: &Value) -> Response {
.unwrap_or(&Value::Null);
let msg = choice.get("message").unwrap_or(&Value::Null);
let mut blocks = Vec::new();
if let Some(t) = str_of(msg, "reasoning_content").filter(|t| !t.is_empty()) {
// DeepSeek 叫 reasoning_content,OpenRouter、vLLM 叫 reasoning
if let Some(t) = str_of(msg, "reasoning_content")
.or_else(|| str_of(msg, "reasoning"))
.filter(|t| !t.is_empty())
{
blocks.push(Block::Thinking(Thinking {
text: t.to_string(),
signature: None,
signature: Some(Signature::new(Vendor::Chat, "")),
}));
}
for key in ["content", "refusal"] {
Expand Down Expand Up @@ -212,7 +216,13 @@ mod tests {
"tool_calls": [{"id": "call_1", "type": "function", "function": {"name": "f", "arguments": ""}}]},
"finish_reason": "tool_calls"}]
}));
assert!(matches!(&r.blocks[0], Block::Thinking(t) if t.text == "推理"));
assert_eq!(
r.blocks[0],
Block::Thinking(Thinking {
text: "推理".into(),
signature: Some(Signature::new(Vendor::Chat, "")),
})
);
assert!(
matches!(&r.blocks[2], Block::ToolCall(c) if c.input == ToolInput::Json(json!({})))
);
Expand Down
14 changes: 13 additions & 1 deletion crates/tw-dialect/src/chat/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,14 @@ impl Parser {
}

fn close(&mut self, out: &mut Vec<Event>) {
if let Some((index, _)) = self.open.take() {
if let Some((index, o)) = self.open.take() {
// 推理块结束时打上 Chat 的厂商:下一轮认得出该原样写回 reasoning_content
if o == Open::Thinking {
out.push(Event::Delta {
index,
delta: Delta::Signature(Signature::new(Vendor::Chat, "")),
});
}
out.push(Event::BlockStop { index });
}
}
Expand Down Expand Up @@ -367,6 +374,11 @@ mod tests {
.filter(|e| matches!(e, Event::BlockStop { .. }))
.count();
assert_eq!(stops, 4);
// 推理块带着 Chat 的厂商结束:客户端带回来时认得出该写回 reasoning_content
assert!(ev.contains(&Event::Delta {
index: 0,
delta: Delta::Signature(Signature::new(Vendor::Chat, "")),
}));
assert!(ev.contains(&Event::Stop(StopReason::ToolUse)));
assert!(ev.contains(&Event::Usage(Usage {
input: 40,
Expand Down
3 changes: 2 additions & 1 deletion crates/tw-dialect/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,8 @@ mod tests {
{"type": "thinking", "thinking": "x", "signature": "tw1.o.rs_1:enc"},
{"type": "text", "text": "answer"}
]},
{"role": "assistant", "content": [{"type": "thinking", "thinking": "y", "signature": "tw1.n."}]}
{"role": "assistant", "content": [{"type": "thinking", "thinking": "y", "signature": "tw1.n."}]},
{"role": "assistant", "content": [{"type": "thinking", "thinking": "z", "signature": "tw1.ch."}]}
]})
.to_string();
let out: Value =
Expand Down
9 changes: 8 additions & 1 deletion crates/tw-dialect/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ pub enum Vendor {
Anthropic,
OpenAi,
Google,
/// Chat 格式的上游(DeepSeek 等)的 `reasoning_content`。它们不签名,`value` 为空;
/// 打上厂商只为了下一轮认得出这段推理该原样写回 `reasoning_content` —— DeepSeek
/// 在工具调用的那几轮要求带回来,而 OpenAI 自己的 Chat 接口不认这个字段
Chat,
}

/// 转换写出去的签名的前缀。**直通时看到它就知道这段推理不是那个上游签发的**
Expand All @@ -448,6 +452,8 @@ impl Signature {
(Vendor::Anthropic, true) => "ar",
(Vendor::OpenAi, _) => "o",
(Vendor::Google, _) => "g",
// `c` 是压缩项的(见 [`crate::compaction::CARRIED_PREFIX`])
(Vendor::Chat, _) => "ch",
}
}

Expand All @@ -471,7 +477,8 @@ impl Signature {
"ar" => (Vendor::Anthropic, true),
"o" => (Vendor::OpenAi, false),
"g" => (Vendor::Google, false),
// `n`:没有签名的推理(比如 DeepSeek 的 reasoning_content)
"ch" => (Vendor::Chat, false),
// `n`:没有签名的推理
_ => return None,
};
Some(Signature {
Expand Down
Loading
Loading