Skip to content

World's first open-source enterprise world model.

Keep DeepSeek reasoning through tool calls and conversation replay (#1241)

Maya Kid7b0ccc44fbea
diff --git a/crates/utopia-llm/src/lib.rs b/crates/utopia-llm/src/lib.rs
index bc6cf02b..c046e93a 100644
--- a/crates/utopia-llm/src/lib.rs
+++ b/crates/utopia-llm/src/lib.rs
@@ -32,6 +32,8 @@ pub struct AssistantTurn {
/// Preserve the provider value; absent is not an implicit `stop`.
pub finish_reason: Option<String>,
pub content: Option<String>,
+ /// Provider reasoning, kept apart from visible text and replayed for tool continuation.
+ pub reasoning_content: Option<String>,
pub tool_calls: Vec<ToolCall>,
}
@@ -39,6 +41,9 @@ impl AssistantTurn {
/// 还原为 OpenAI 协议的 assistant 消息(回灌对话历史用)。
pub fn to_message(&self) -> serde_json::Value {
let mut msg = json!({ "role": "assistant", "content": self.content });
+ if let Some(reasoning) = &self.reasoning_content {
+ msg["reasoning_content"] = json!(reasoning);
+ }
if !self.tool_calls.is_empty() {
msg["tool_calls"] = json!(self
.tool_calls
@@ -885,6 +890,7 @@ impl LlmClient {
.unwrap_or_default();
Ok(AssistantTurn {
content,
+ reasoning_content: msg["reasoning_content"].as_str().map(String::from),
tool_calls,
finish_reason: body["choices"][0]["finish_reason"]
.as_str()
@@ -926,6 +932,7 @@ impl LlmClient {
let stream = async_stream::try_stream! {
let mut buf = SseBuffer::default();
let mut content = String::new();
+ let mut reasoning_content: Option<String> = None;
let mut calls: Vec<ToolCall> = Vec::new();
let mut finish_reason = None;
let mut done = false;
@@ -950,6 +957,9 @@ impl LlmClient {
done = true;
}
let delta = &v["choices"][0]["delta"];
+ if let Some(text) = delta["reasoning_content"].as_str() {
+ reasoning_content.get_or_insert_default().push_str(text);
+ }
if let Some(text) = delta["content"].as_str() {
if !text.is_empty() {
content.push_str(text);
@@ -992,7 +1002,7 @@ impl LlmClient {
}
calls.retain(|c| !c.name.is_empty());
let content = if content.is_empty() { None } else { Some(content) };
- yield ToolStreamItem::Turn(AssistantTurn { content, tool_calls: calls, finish_reason });
+ yield ToolStreamItem::Turn(AssistantTurn { content, reasoning_content, tool_calls: calls, finish_reason });
};
Ok(stream)
}
@@ -2063,6 +2073,66 @@ mod tests {
}
}
+ #[tokio::test]
+ async fn tool_turns_preserve_reasoning_for_continuation_without_showing_it() {
+ for reasoning in [Some("先查找🦀"), Some(""), None] {
+ let body = json!({"choices":[{"message":{
+ "content":"Searching now",
+ "reasoning_content":reasoning,
+ "tool_calls":[{"id":"call_1","type":"function","function":{"name":"search","arguments":"{}"}}]
+ },"finish_reason":"tool_calls"}]}).to_string();
+ let (addr, server) = an_http_response("200 OK", "application/json", &body).await;
+ let turn = client_at(addr)
+ .chat_tools_with(&[], None, None)
+ .await
+ .unwrap();
+ server.await.unwrap();
+ let message = turn.to_message();
+ assert_eq!(turn.content.as_deref(), Some("Searching now"));
+ assert_eq!(message["content"], "Searching now");
+ assert_eq!(
+ message.get("reasoning_content"),
+ reasoning.map(|s| json!(s)).as_ref()
+ );
+ assert_eq!(message["tool_calls"][0]["id"], "call_1");
+ }
+ }
+
+ #[tokio::test]
+ async fn streamed_tool_turns_join_reasoning_across_frames_and_utf8_chunks() {
+ use futures_util::TryStreamExt;
+ let frames = [
+ json!({"choices":[{"delta":{"reasoning_content":"先查找"}}]}),
+ json!({"choices":[{"delta":{"reasoning_content":"🦀"}}]}),
+ json!({"choices":[{"delta":{
+ "content":"Searching now",
+ "tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"search","arguments":"{}"}}]
+ },"finish_reason":"tool_calls"}]}),
+ ];
+ let sse = frames
+ .iter()
+ .map(|frame| format!("data: {frame}\n\n"))
+ .collect::<String>()
+ + "data: [DONE]\n\n";
+ let (addr, server) = bytewise_sse(&sse).await;
+ let items: Vec<ToolStreamItem> = client_at(addr)
+ .chat_tools_stream_with(&[], None, None)
+ .await
+ .unwrap()
+ .try_collect()
+ .await
+ .unwrap();
+ server.await.unwrap();
+ let [ToolStreamItem::Delta(delta), ToolStreamItem::Turn(turn)] = items.as_slice() else {
+ panic!("expected only the visible content delta and completed turn: {items:?}");
+ };
+ assert_eq!(delta, "Searching now");
+ assert_eq!(turn.content.as_deref(), Some("Searching now"));
+ let message = turn.to_message();
+ assert_eq!(message["reasoning_content"], "先查找🦀");
+ assert_eq!(message["tool_calls"][0]["id"], "call_1");
+ }
+
#[tokio::test]
async fn either_finish_signal_completes_raw_and_tool_streams() {
use futures_util::TryStreamExt;
diff --git a/crates/utopia-server/src/api/agent.rs b/crates/utopia-server/src/api/agent.rs
index 1bca29ae..bef57f54 100644
--- a/crates/utopia-server/src/api/agent.rs
+++ b/crates/utopia-server/src/api/agent.rs
@@ -566,7 +566,16 @@ impl AgentHook for Policy {
/// 最后那一轮的工具往返放在它的结论之前——顺序就是真实顺序,模型读起来
/// 是「我问了、我查了、我答了」。tool 消息的工具名在协议里没有,从前面那条
/// 带 tool_calls 的 assistant 消息里按 id 找回来
+#[cfg(test)]
pub fn history_messages(turns: &[(String, String)], last_tool_exchange: &[Value]) -> Vec<Message> {
+ history_messages_with_reasoning(turns, last_tool_exchange, &[])
+}
+
+pub fn history_messages_with_reasoning(
+ turns: &[(String, String)],
+ last_tool_exchange: &[Value],
+ reasoning: &[Option<String>],
+) -> Vec<Message> {
let last_assistant = turns.iter().rposition(|(role, _)| role == "assistant");
let mut out = Vec::new();
for (i, (role, content)) in turns.iter().enumerate() {
@@ -574,7 +583,17 @@ pub fn history_messages(turns: &[(String, String)], last_tool_exchange: &[Value]
out.extend(exchange_messages(last_tool_exchange));
}
match role.as_str() {
- "assistant" => out.push(Message::assistant(content.clone())),
+ "assistant" => {
+ let mut parts = Vec::new();
+ if let Some(Some(text)) = reasoning.get(i) {
+ parts.push(AssistantContent::reasoning(text));
+ }
+ parts.push(AssistantContent::text(content));
+ out.push(Message::Assistant {
+ id: None,
+ content: parts,
+ });
+ }
_ => out.push(Message::user(content.clone())),
}
}
@@ -588,6 +607,9 @@ fn exchange_messages(exchange: &[Value]) -> Vec<Message> {
match m["role"].as_str() {
Some("assistant") => {
let mut content = Vec::new();
+ if let Some(reasoning) = m["reasoning_content"].as_str() {
+ content.push(AssistantContent::reasoning(reasoning));
+ }
if let Some(text) = m["content"].as_str().filter(|t| !t.is_empty()) {
content.push(AssistantContent::text(text));
}
diff --git a/crates/utopia-server/src/api/chat.rs b/crates/utopia-server/src/api/chat.rs
index ccd6a0a1..47b6a344 100644
--- a/crates/utopia-server/src/api/chat.rs
+++ b/crates/utopia-server/src/api/chat.rs
@@ -912,8 +912,12 @@ pub async fn chat(
.filter(|(i, _)| Some(*i) != current)
.map(|(_, turn)| turn.clone())
.collect();
- let context = super::chat_context::Context::new(
- prior.clone(), history.last_tool_exchange.clone(), client.history_char_budget(),
+ let prior_reasoning: Vec<_> = history.reasoning.iter().enumerate()
+ .filter(|(i, _)| Some(*i) != current)
+ .map(|(_, text)| text.clone()).collect();
+ let context = super::chat_context::Context::with_reasoning(
+ prior.clone(), history.last_tool_exchange.clone(),
+ prior_reasoning.clone(), client.history_char_budget(),
);
let rig_agent = AgentBuilder::new(RigModel::with_context(client.clone(), context.clone()))
.preamble(&system_prompt)
@@ -924,7 +928,9 @@ pub async fn chat(
.build();
let mut runner = rig_agent
.runner(Message::user(query.clone()))
- .history(agent::history_messages(&prior, &history.last_tool_exchange));
+ .history(agent::history_messages_with_reasoning(
+ &prior, &history.last_tool_exchange, &prior_reasoning,
+ ));
// 贴在历史之后、当前问题之前——位置就是服从性,跟抽取里 known_block
// 紧挨正文是同一条理由(角色与位置由 `rig_model::wire` 定)
if let Some(block) = agent::known_entities_block(&history.entities, KNOWN_ENTITY_LIMIT) {
@@ -943,6 +949,7 @@ pub async fn chat(
let mut exchange_acc: Vec<serde_json::Value> = Vec::new();
// 当前模型回合里说的话与发出的调用;回合的结果一到,攒成一条 assistant 消息
let mut turn_text = String::new();
+ let mut turn_reasoning: Option<String> = None;
let mut turn_calls: Vec<serde_json::Value> = Vec::new();
// 这一回合的正文发出去了多少;后面的扣着,因为那可能是写成正文的工具调用(#937)
let mut turn_published = 0usize;
@@ -953,6 +960,15 @@ pub async fn chat(
while let Some(item) = run.next().await {
match item {
+ Ok(MultiTurnStreamItem::StreamAssistantItem(StreamedAssistantContent::Reasoning {
+ reasoning, ..
+ })) => {
+ for block in reasoning.content {
+ if let rig_core::message::ReasoningContent::Text { text, .. } = block {
+ turn_reasoning.get_or_insert_default().push_str(&text);
+ }
+ }
+ }
Ok(MultiTurnStreamItem::StreamAssistantItem(StreamedAssistantContent::Text(t))) => {
// Tool-round narration stays live, line by line; a line that is or
// may become a tool call written as text is held (#937). Withhold the
@@ -1008,7 +1024,7 @@ pub async fn chat(
answer_acc.push_str("\n\n");
yield ProducerEvent::Progress(delta_event("\n\n"));
}
- exchange_acc.push(json!({
+ let mut recorded = json!({
"role": "assistant",
"content": if turn_text.is_empty() {
serde_json::Value::Null
@@ -1016,7 +1032,11 @@ pub async fn chat(
serde_json::Value::String(turn_text.clone())
},
"tool_calls": std::mem::take(&mut turn_calls),
- }));
+ });
+ if let Some(reasoning) = turn_reasoning.take() {
+ recorded["reasoning_content"] = json!(reasoning);
+ }
+ exchange_acc.push(recorded);
turn_text.clear();
turn_published = 0;
turn_holding = false;
@@ -1086,6 +1106,7 @@ pub async fn chat(
yield ProducerEvent::Progress(delta_event("\n\n"));
}
turn_text.clear();
+ turn_reasoning = None;
turn_calls.clear();
turn_published = 0;
turn_holding = false;
@@ -1136,7 +1157,12 @@ pub async fn chat(
exchange: &exchange_acc, sources: &sources, resolved: &resolved,
};
match finalization::answer_with_context(&client, input, &context).await {
- Ok(answer) => { turn_text = answer; turn_calls.clear(); finished = true; }
+ Ok(answer) => {
+ turn_text = answer.content.unwrap_or_default();
+ turn_reasoning = answer.reasoning_content;
+ turn_calls.clear();
+ finished = true;
+ }
Err(e) => {
let message = format!("Model could not produce a final answer: {e}");
let code = model_failure_code(&e)
@@ -1192,6 +1218,7 @@ pub async fn chat(
.unwrap_or_default();
sources = carried_sources(&answer_acc, previous, &history.last_sources);
}
+ exchange_acc.extend(terminal_reasoning(turn_reasoning));
yield ProducerEvent::Outcome(Ok(Answer {
content: answer_acc,
record: utopia_store::conversations::TurnRecord {
@@ -1245,6 +1272,15 @@ fn describe(err: &StreamingError) -> (Failure, bool) {
}
}
+// DeepSeek also needs a completed answer's reasoning on later tool requests.
+// Keep it in the internal exchange metadata, apart from the published answer.
+fn terminal_reasoning(reasoning: Option<String>) -> Vec<serde_json::Value> {
+ reasoning
+ .into_iter()
+ .map(|text| json!({"role":"assistant", "reasoning_content":text}))
+ .collect()
+}
+
/// 降级路径:端点不支持工具调用时的一次性 RAG 注入。
/// 检索一次、把来源塞进系统提示、流式作答、落库
fn legacy_rag(
@@ -1275,6 +1311,7 @@ fn legacy_rag(
serde_json::to_string(&legacy_sources).unwrap_or_else(|_| "[]".into()),
));
let mut answer_acc = String::new();
+ let mut reasoning = None;
let deltas = loop {
let (turns, _) = context.snapshot();
let mut lmsgs = vec![json!({ "role": "system", "content": legacy_system_prompt(&chunks) })];
@@ -1282,7 +1319,7 @@ fn legacy_rag(
lmsgs.push(json!({ "role": role, "content": content }));
}
lmsgs.push(json!({"role":"user", "content":query}));
- match client.chat_stream_raw(&lmsgs).await {
+ match client.chat_tools_stream_with(&lmsgs, None, None).await {
Err(e) if context.recover(&client, &e) => continue,
result => break result,
}
@@ -1292,7 +1329,8 @@ fn legacy_rag(
let mut deltas = std::pin::pin!(deltas);
while let Some(item) = deltas.next().await {
match item {
- Ok(text) => { answer_acc.push_str(&text); yield ProducerEvent::Progress(delta_event(&text)); }
+ Ok(utopia_llm::ToolStreamItem::Delta(text)) => { answer_acc.push_str(&text); yield ProducerEvent::Progress(delta_event(&text)); }
+ Ok(utopia_llm::ToolStreamItem::Turn(turn)) => reasoning = turn.reasoning_content,
Err(e) => { yield ProducerEvent::Outcome(Err(Failure::model(&e, e.to_string()))); return; }
}
}
@@ -1304,6 +1342,7 @@ fn legacy_rag(
content: answer_acc,
record: utopia_store::conversations::TurnRecord {
sources: serde_json::Value::Array(legacy_sources),
+ tool_exchange: json!(terminal_reasoning(reasoning)),
..utopia_store::conversations::TurnRecord::empty()
},
final_text: None,
diff --git a/crates/utopia-server/src/api/chat_context.rs b/crates/utopia-server/src/api/chat_context.rs
index aa119dd7..3b9816e5 100644
--- a/crates/utopia-server/src/api/chat_context.rs
+++ b/crates/utopia-server/src/api/chat_context.rs
@@ -18,6 +18,8 @@ pub(super) struct Context(Arc<Mutex<Window>>);
struct Window {
/// 落库的历史,最旧在前;`start` 之前的不再发
turns: Turns,
+ /// 每条终答自己的协议推理,按原始下标随正文一起保留或裁掉。
+ reasoning: Vec<Option<String>>,
/// 上一轮的工具往返:带 tool_calls 的助手消息和配套的 tool 结果,协议 JSON
exchange: Vec<Value>,
/// 最后一条助手消息在 `turns` 里的下标,往返插在它前面
@@ -36,7 +38,10 @@ impl Window {
fn size(&self) -> usize {
self.turns[self.start..]
.iter()
- .map(|(_, s)| s.chars().count())
+ .zip(&self.reasoning[self.start..])
+ .map(|((_, s), reasoning)| {
+ s.chars().count() + reasoning.as_deref().map_or(0, |s| s.chars().count())
+ })
.sum::<usize>()
+ if self.keeps_exchange() {
self.exchange
@@ -75,19 +80,40 @@ impl Window {
}
/// 落库的历史和往返展开成协议消息,和 rig 的 `wire` 走同一条路,数出来的条数才对得上
+#[cfg(test)]
fn protocol(turns: &[(String, String)], exchange: &[Value]) -> Vec<Value> {
+ protocol_with_reasoning(turns, exchange, &[])
+}
+
+fn protocol_with_reasoning(
+ turns: &[(String, String)],
+ exchange: &[Value],
+ reasoning: &[Option<String>],
+) -> Vec<Value> {
let mut out = Vec::new();
- for m in agent::history_messages(turns, exchange) {
+ for m in agent::history_messages_with_reasoning(turns, exchange, reasoning) {
rig_model::push_message(&mut out, &m);
}
out
}
impl Context {
+ #[cfg(test)]
pub fn new(turns: Turns, exchange: Vec<Value>, budget: usize) -> Self {
+ Self::with_reasoning(turns, exchange, vec![], budget)
+ }
+
+ pub fn with_reasoning(
+ turns: Turns,
+ exchange: Vec<Value>,
+ mut reasoning: Vec<Option<String>>,
+ budget: usize,
+ ) -> Self {
+ reasoning.resize(turns.len(), None);
let last_assistant = turns.iter().rposition(|(role, _)| role == "assistant");
let mut window = Window {
turns,
+ reasoning,
exchange,
last_assistant,
start: 0,
@@ -117,7 +143,7 @@ impl Context {
/// 问题前面。上一轮的往返在最后那条回答之前,丢它是从中间抠掉,不是掐头
pub fn apply(&self, messages: &[Value]) -> Vec<Value> {
let w = self.0.lock().unwrap();
- let full = protocol(&w.turns, &w.exchange);
+ let full = protocol_with_reasoning(&w.turns, &w.exchange, &w.reasoning);
// rig 的列表开头应当就是这份完整历史,后面至少还有本轮的问题。少了——rig 没把历史
// 全交回来,或者给的不是这一轮的列表——按条数替换会把本轮的问题一起吞掉,还不出声。
// 这时原样发:请求超长顶多再挨一次 400,也好过一个没有问题的请求(#973 评审的后续)
@@ -132,9 +158,10 @@ impl Context {
}
return messages.to_vec();
}
- let kept = protocol(
+ let kept = protocol_with_reasoning(
&w.turns[w.start..],
if w.keeps_exchange() { &w.exchange } else { &[] },
+ &w.reasoning[w.start..],
);
let mut out = Vec::with_capacity(messages.len());
let mut replaced = 0;
@@ -300,6 +327,47 @@ mod tests {
assert_eq!(context.apply(&messages), messages, "nothing to remove");
}
+ #[test]
+ fn reasoning_stays_with_its_answer_when_exchanges_and_older_turns_are_dropped() {
+ let turns: Turns = vec![
+ ("user".into(), "question".into()),
+ ("assistant".into(), "same answer".into()),
+ ("user".into(), "question".into()),
+ ("assistant".into(), "same answer".into()),
+ ];
+ let reasoning = vec![
+ None,
+ Some("old thought".into()),
+ None,
+ Some("界😀new thought".into()),
+ ];
+ let exchange = heavy_exchange();
+ let context = Context::with_reasoning(turns.clone(), exchange.clone(), reasoning, 100);
+ assert!(
+ context.snapshot().1.is_empty(),
+ "tool evidence is dropped first"
+ );
+ let mut messages = protocol(&turns, &exchange);
+ messages.push(json!({"role":"user","content":"current"}));
+ let applied = context.apply(&messages);
+ assert_eq!(applied[1]["reasoning_content"], "old thought");
+ assert_eq!(applied[3]["reasoning_content"], "界😀new thought");
+ assert!(applied[0].get("reasoning_content").is_none());
+
+ let budget = "questionsame answer界😀new thought".chars().count();
+ context.0.lock().unwrap().trim(budget);
+ assert_eq!(context.snapshot().0, turns[2..]);
+ let applied = context.apply(&messages);
+ assert_eq!(applied.len(), 3);
+ assert_eq!(applied[1]["reasoning_content"], "界😀new thought");
+ assert_eq!(applied[2]["content"], "current");
+ context.0.lock().unwrap().trim(budget - 1);
+ assert!(
+ context.snapshot().0.is_empty(),
+ "reasoning counts toward the budget"
+ );
+ }
+
#[test]
fn a_refusal_halves_the_history_once_and_only_when_something_can_go() {
let client = LlmClient::new("http://unused", None, "m");
diff --git a/crates/utopia-server/src/api/chat_empty_reply_tests.rs b/crates/utopia-server/src/api/chat_empty_reply_tests.rs
index e1af7c10..b1baf0ba 100644
--- a/crates/utopia-server/src/api/chat_empty_reply_tests.rs
+++ b/crates/utopia-server/src/api/chat_empty_reply_tests.rs
@@ -42,6 +42,8 @@ pub(super) enum Reply {
HttpError(u16, &'static str),
/// 调一个工具:(名字, 参数 JSON)
Tool(&'static str, &'static str),
+ ThoughtTool(&'static str, &'static str, &'static str),
+ ThoughtText(&'static str, &'static str),
}
/// 按脚本回话的假模型。`replies[i]` 是第 i+1 次请求的回复,脚本读完之后一律回空。
@@ -74,10 +76,11 @@ impl Respond for Scripted {
};
let frame = match self.replies.get(n - 1).copied().unwrap_or(Reply::Empty) {
Reply::HttpError(status, body) => {
- return ResponseTemplate::new(status).set_body_string(body)
+ return ResponseTemplate::new(status).set_body_string(body);
}
Reply::Http(status) => {
- return ResponseTemplate::new(status).set_body_string("Evidence gathering complete")
+ return ResponseTemplate::new(status)
+ .set_body_string("Evidence gathering complete");
}
Reply::Finished(text, reason) => Some(
serde_json::json!({ "choices": [{ "delta": { "content": text }, "finish_reason": reason }] }),
@@ -138,6 +141,15 @@ impl Respond for Scripted {
"function": { "name": name, "arguments": args }
}]
} }] })),
+ Reply::ThoughtTool(name, args, reasoning) => {
+ Some(serde_json::json!({"choices":[{"delta":{
+ "reasoning_content":reasoning,
+ "tool_calls":[{"index":0,"id":format!("call_{n}"),"function":{"name":name,"arguments":args}}]
+ }}]}))
+ }
+ Reply::ThoughtText(text, reasoning) => Some(serde_json::json!({"choices":[{"delta":{
+ "content":text,"reasoning_content":reasoning
+ }}]})),
};
let mut sse = String::new();
if let Some(frame) = frame {
diff --git a/crates/utopia-server/src/api/chat_finalization.rs b/crates/utopia-server/src/api/chat_finalization.rs
index d56e080f..d94d866e 100644
--- a/crates/utopia-server/src/api/chat_finalization.rs
+++ b/crates/utopia-server/src/api/chat_finalization.rs
@@ -5,7 +5,7 @@ use futures_util::StreamExt;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::time::Duration;
-use utopia_llm::{LlmClient, ToolStreamItem};
+use utopia_llm::{AssistantTurn, LlmClient, ToolStreamItem};
const ANSWER_DEADLINE: Duration = Duration::from_secs(120);
const MAX_CONTEXT_BYTES: usize = 1024 * 1024;
@@ -89,7 +89,7 @@ fn messages(input: &AnswerContext<'_>) -> Vec<Value> {
}
enum Candidate {
- Accepted(String),
+ Accepted(AssistantTurn),
Repairable(&'static str),
}
@@ -126,13 +126,13 @@ async fn answer_once(
anyhow::bail!("Model did not finish its final answer: {reason}")
}
}
- let text = turn.content.unwrap_or_default();
+ let text = turn.content.as_deref().unwrap_or_default();
if let Some(reason) =
- finalization_error(&text, !turn.tool_calls.is_empty(), question)
+ finalization_error(text, !turn.tool_calls.is_empty(), question)
{
return Ok(Candidate::Repairable(reason));
}
- return Ok(Candidate::Accepted(text));
+ return Ok(Candidate::Accepted(turn));
}
}
}
@@ -143,7 +143,7 @@ pub(super) async fn answer_with_context(
client: &LlmClient,
input: AnswerContext<'_>,
context: &super::super::chat_context::Context,
-) -> anyhow::Result<String> {
+) -> anyhow::Result<AssistantTurn> {
answer_with_deadline(client, input, ANSWER_DEADLINE, Some(context)).await
}
@@ -152,7 +152,7 @@ async fn answer_with_deadline(
input: AnswerContext<'_>,
deadline: Duration,
context: Option<&super::super::chat_context::Context>,
-) -> anyhow::Result<String> {
+) -> anyhow::Result<AssistantTurn> {
let mut messages = messages(&input);
// A total deadline covers BOTH attempts, not a fresh allowance per retry.
tokio::time::timeout(deadline, async {
@@ -201,7 +201,7 @@ mod tests {
ResponseTemplate::new(400).set_body_json(json!({"error":{"code":"context_length_exceeded","message":"Prompt is too long"}}))
} else {
ResponseTemplate::new(200).insert_header("content-type", "text/event-stream")
- .set_body_string("data: {\"choices\":[{\"delta\":{\"content\":\"Answer from evidence.\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n")
+ .set_body_string("data: {\"choices\":[{\"delta\":{\"content\":\"Answer from evidence.\",\"reasoning_content\":\"private final reasoning\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n")
}
}).mount(&server).await;
let history = vec![
@@ -224,9 +224,11 @@ mod tests {
resolved: &[],
};
let client = LlmClient::new(&server.uri(), None, "test");
+ let answer = answer_with_context(&client, input, &context).await.unwrap();
+ assert_eq!(answer.content.as_deref(), Some("Answer from evidence."));
assert_eq!(
- answer_with_context(&client, input, &context).await.unwrap(),
- "Answer from evidence."
+ answer.reasoning_content.as_deref(),
+ Some("private final reasoning")
);
let seen = seen.lock().unwrap();
assert_eq!(seen.len(), 2);
diff --git a/crates/utopia-server/src/api/chat_history_tests.rs b/crates/utopia-server/src/api/chat_history_tests.rs
index a0be4e26..01f2e6c6 100644
--- a/crates/utopia-server/src/api/chat_history_tests.rs
+++ b/crates/utopia-server/src/api/chat_history_tests.rs
@@ -91,6 +91,59 @@ async fn the_question_reaches_the_model_once() -> anyhow::Result<()> {
f.cleanup().await
}
+#[tokio::test]
+async fn repeated_answers_keep_their_own_reasoning_when_history_is_replayed() -> anyhow::Result<()>
+{
+ const ANSWER: &str = "The same public answer.";
+ let tool_thoughts = ["private-tool-1", "private-tool-2", "private-tool-3"];
+ let answer_thoughts = ["private-answer-1", "private-answer-2", "private-answer-3"];
+ let replies = tool_thoughts
+ .into_iter()
+ .zip(answer_thoughts)
+ .flat_map(|(tool, answer)| {
+ [
+ Reply::ThoughtTool("no_evidence_needed", r#"{"reason":"greeting"}"#, tool),
+ Reply::ThoughtText(ANSWER, answer),
+ ]
+ })
+ .collect();
+ let Some(f) = fixture(Scripted::new(replies)).await? else {
+ return Ok(());
+ };
+ let id = utopia_store::conversations::create(&f.pool, f.kb, f.user.id, "Reasoning").await?;
+ for question in ["first greeting", "second greeting", "third greeting"] {
+ let sse = ask_in(&f, id, question).await?;
+ assert!(sse.contains("event: done") && sse.contains(ANSWER), "{sse}");
+ assert!(
+ !sse.contains("private-"),
+ "reasoning must not enter SSE: {sse}"
+ );
+ assert_eq!(f.stored_answer().await?.as_deref(), Some(ANSWER));
+ }
+ let requests = f.requests();
+ assert_eq!(
+ requests.len(),
+ 6,
+ "each question has a tool turn then an answer"
+ );
+ let messages = requests[5]["messages"].as_array().expect("messages");
+ let earlier_answers: Vec<_> = messages
+ .iter()
+ .filter(|m| m["role"] == "assistant" && m["content"] == ANSWER)
+ .collect();
+ assert_eq!(earlier_answers.len(), 2, "{messages:?}");
+ for (message, reasoning) in earlier_answers.iter().zip(answer_thoughts) {
+ assert_eq!(message["reasoning_content"], reasoning, "{message}");
+ }
+ let latest_tool = messages
+ .iter()
+ .rev()
+ .find(|m| m["role"] == "assistant" && m["tool_calls"].is_array())
+ .expect("the third tool turn is echoed for continuation");
+ assert_eq!(latest_tool["reasoning_content"], tool_thoughts[2]);
+ f.cleanup().await
+}
+
#[tokio::test]
async fn earlier_entities_sit_right_before_the_question() -> anyhow::Result<()> {
let Some(f) = fixture(Scripted::new(vec![
diff --git a/crates/utopia-server/src/api/rig_model.rs b/crates/utopia-server/src/api/rig_model.rs
index 6477b3f0..cef34b7c 100644
--- a/crates/utopia-server/src/api/rig_model.rs
+++ b/crates/utopia-server/src/api/rig_model.rs
@@ -10,9 +10,13 @@ use futures_util::{stream, Stream, StreamExt};
use rig_core::completion::{
CompletionError, CompletionModel, CompletionRequest, CompletionResponse, Usage,
};
-use rig_core::message::{AssistantContent, Message, ToolChoice, ToolResultContent, UserContent};
+use rig_core::message::{
+ AssistantContent, Message, Reasoning, ReasoningContent, ToolChoice, ToolResultContent,
+ UserContent,
+};
use rig_core::streaming::{
- RawStreamingChoice, RawStreamingToolCall, StreamFinal, StreamingCompletionResponse,
+ MintKind, RawStreamingChoice, RawStreamingToolCall, StreamFinal, StreamPartId,
+ StreamingCompletionResponse,
};
use serde_json::{json, Value};
use utopia_llm::{AssistantTurn, LlmClient, ToolStreamItem};
@@ -95,25 +99,32 @@ impl CompletionModel for RigModel {
let w = wire(&request);
let source = self.open_stream(&w).await?;
// `LlmClient` 的流已经把工具调用按 index 归并好、放在流末的 `Turn` 里;
- // rig 只要每个调用一条 `ToolCall`,再一条 `FinalResponse` 收尾
+ // 推理块也要交给 rig 保留,下一次工具续接时随 assistant 消息回传
let inner = source.flat_map(|item| {
let items: Vec<Result<RawStreamingChoice, CompletionError>> = match item {
Ok(ToolStreamItem::Delta(text)) => vec![Ok(RawStreamingChoice::Message(text))],
Ok(ToolStreamItem::Turn(turn)) => {
- let mut v: Vec<_> = turn
- .tool_calls
- .iter()
- .map(|c| {
- Ok(RawStreamingChoice::ToolCall(
- RawStreamingToolCall::new(
- call_id(&c.id),
- c.name.clone(),
- args_value(&c.arguments),
- )
- .with_additional_params(c.extra_content.clone()),
- ))
- })
- .collect();
+ let mut v = Vec::new();
+ if let Some(reasoning) = turn.reasoning_content {
+ v.push(Ok(RawStreamingChoice::Reasoning {
+ id: StreamPartId::minted(MintKind::Reasoning, 0),
+ provider_id: None,
+ content: ReasoningContent::Text {
+ text: reasoning,
+ signature: None,
+ },
+ }));
+ }
+ v.extend(turn.tool_calls.iter().map(|c| {
+ Ok(RawStreamingChoice::ToolCall(
+ RawStreamingToolCall::new(
+ call_id(&c.id),
+ c.name.clone(),
+ args_value(&c.arguments),
+ )
+ .with_additional_params(c.extra_content.clone()),
+ ))
+ }));
v.push(Ok(RawStreamingChoice::FinalResponse(
StreamFinal::new(PROVIDER, Usage::new()).with_optional_finish_reason(
turn.finish_reason.as_deref().map(finish_reason),
@@ -299,10 +310,18 @@ pub(super) fn push_message(out: &mut Vec<Value>, m: &Message) {
}
Message::Assistant { content, .. } => {
let mut text = String::new();
+ let mut reasoning: Option<String> = None;
let mut calls = Vec::new();
for c in content {
match c {
AssistantContent::Text(t) => text.push_str(&t.text),
+ AssistantContent::Reasoning(r) => {
+ for block in &r.content {
+ if let ReasoningContent::Text { text, .. } = block {
+ reasoning.get_or_insert_default().push_str(text);
+ }
+ }
+ }
// 端点挂在调用上的 `extra_content`(Gemini 3 的 thought_signature)
// 跟着回去:同一回合里缺了它,端点拒收整个请求
AssistantContent::ToolCall(tc) => {
@@ -319,7 +338,7 @@ pub(super) fn push_message(out: &mut Vec<Value>, m: &Message) {
}
calls.push(call);
}
- // 推理块与图片不回灌
+ // 图片不回灌
_ => {}
}
}
@@ -330,6 +349,9 @@ pub(super) fn push_message(out: &mut Vec<Value>, m: &Message) {
if !calls.is_empty() {
msg["tool_calls"] = Value::Array(calls);
}
+ if let Some(reasoning) = reasoning {
+ msg["reasoning_content"] = Value::String(reasoning);
+ }
out.push(msg);
}
}
@@ -377,6 +399,9 @@ pub(crate) fn call_id(id: &str) -> String {
fn choice_of(turn: &AssistantTurn) -> Vec<AssistantContent> {
let mut out = Vec::new();
+ if let Some(reasoning) = &turn.reasoning_content {
+ out.push(AssistantContent::Reasoning(Reasoning::new(reasoning)));
+ }
if let Some(text) = turn.content.as_deref().filter(|t| !t.is_empty()) {
out.push(AssistantContent::text(text));
}
@@ -394,7 +419,9 @@ fn choice_of(turn: &AssistantTurn) -> Vec<AssistantContent> {
#[cfg(test)]
mod tests {
use super::*;
+ use rig_agent::{tool::DynamicTool, tool::ToolOutput, AgentBuilder};
use rig_core::completion::{Document, ToolDefinition};
+ use wiremock::{matchers::method, matchers::path, Mock, MockServer, ResponseTemplate};
fn req(history: Vec<Message>) -> CompletionRequest {
CompletionRequest {
@@ -518,6 +545,7 @@ mod tests {
let turn = AssistantTurn {
finish_reason: None,
content: Some("hm".into()),
+ reasoning_content: None,
tool_calls: vec![utopia_llm::ToolCall {
id: "c9".into(),
name: "find_entities".into(),
@@ -546,6 +574,7 @@ mod tests {
let turn = AssistantTurn {
finish_reason: Some("stop".into()),
content: None,
+ reasoning_content: None,
tool_calls: vec![utopia_llm::ToolCall {
id: "call_170866".into(),
name: "search_kb".into(),
@@ -591,5 +620,124 @@ mod tests {
assert!(w.messages[2]["tool_calls"][0]
.get("extra_content")
.is_none());
+ assert!(w.messages[2].get("reasoning_content").is_none());
+ }
+
+ #[test]
+ fn reasoning_text_blocks_join_verbatim_without_entering_visible_content() {
+ let mut messages = Vec::new();
+ push_message(
+ &mut messages,
+ &Message::Assistant {
+ id: None,
+ content: vec![
+ AssistantContent::Reasoning(Reasoning::multi(vec![
+ " plan\n".into(),
+ "next step ".into(),
+ ])),
+ AssistantContent::Reasoning(Reasoning::encrypted("opaque")),
+ AssistantContent::text("answer"),
+ ],
+ },
+ );
+ assert_eq!(messages[0]["reasoning_content"], " plan\nnext step ");
+ assert_eq!(messages[0]["content"], "answer");
+ push_message(&mut messages, &Message::assistant("ordinary"));
+ assert!(messages[1].get("reasoning_content").is_none());
+ }
+
+ /// Exercise the actual runner: after executing the tool it must send the
+ /// provider's reasoning back on the next HTTP request, including an empty string.
+ #[tokio::test]
+ async fn reasoning_survives_unary_and_streamed_tool_continuations() {
+ for streaming in [false, true] {
+ for reasoning in [" Need evidence.\n先查工具 ", ""] {
+ let server = MockServer::start().await;
+ let reasoning = reasoning.to_string();
+ let expected = reasoning.clone();
+ Mock::given(method("POST"))
+ .and(path("/chat/completions"))
+ .respond_with(move |request: &wiremock::Request| {
+ let body: Value = serde_json::from_slice(&request.body).unwrap();
+ let messages = body["messages"].as_array().unwrap();
+ let continued = messages.iter().any(|m| m["role"] == "tool");
+ let mut message = if continued {
+ let assistant = messages
+ .iter()
+ .find(|m| m.get("tool_calls").is_some())
+ .unwrap();
+ if assistant["reasoning_content"] != expected {
+ return ResponseTemplate::new(400).set_body_json(json!({
+ "error": {"message": "reasoning_content was not preserved"}
+ }));
+ }
+ json!({"role": "assistant", "content": "done"})
+ } else {
+ json!({
+ "role": "assistant",
+ "content": "looking",
+ "reasoning_content": expected,
+ "tool_calls": [{
+ "id": "c1", "type": "function",
+ "function": {"name": "search_chunks", "arguments": "{}"}
+ }]
+ })
+ };
+ let finish = if continued { "stop" } else { "tool_calls" };
+ if streaming {
+ if let Some(calls) = message["tool_calls"].as_array_mut() {
+ calls[0]["index"] = json!(0);
+ }
+ let delta = json!({"choices": [{"delta": message}]});
+ let end = json!({"choices": [{"delta": {}, "finish_reason": finish}]});
+ ResponseTemplate::new(200)
+ .insert_header("content-type", "text/event-stream")
+ .set_body_string(format!(
+ "data: {delta}\n\ndata: {end}\n\ndata: [DONE]\n\n"
+ ))
+ } else {
+ ResponseTemplate::new(200).set_body_json(json!({
+ "choices": [{"message": message, "finish_reason": finish}]
+ }))
+ }
+ })
+ .expect(2)
+ .mount(&server)
+ .await;
+ let model = RigModel::with_context(
+ LlmClient::new(&server.uri(), None, "deepseek-reasoner"),
+ super::super::chat_context::Context::new(vec![], vec![], usize::MAX),
+ );
+ let agent = AgentBuilder::new(model)
+ .dynamic_tool(DynamicTool::new(
+ "search_chunks",
+ "search",
+ json!({"type": "object", "properties": {}}),
+ |_, _| Box::pin(async { Ok(ToolOutput::text("found")) }),
+ ))
+ .build();
+ let runner = agent.runner("question").max_turns(3);
+ if streaming {
+ let mut output = runner.stream().await;
+ while let Some(item) = output.next().await {
+ item.unwrap();
+ }
+ } else {
+ runner.run().await.unwrap();
+ }
+ let requests = server.received_requests().await.unwrap();
+ assert_eq!(requests.len(), 2, "one tool turn and one final answer");
+ let continuation: Value = serde_json::from_slice(&requests[1].body).unwrap();
+ let assistant = continuation["messages"]
+ .as_array()
+ .unwrap()
+ .iter()
+ .find(|m| m.get("tool_calls").is_some())
+ .unwrap();
+ assert_eq!(assistant["reasoning_content"], reasoning);
+ assert_eq!(assistant["content"], "looking");
+ server.verify().await;
+ }
+ }
}
}
diff --git a/crates/utopia-store/src/conversations.rs b/crates/utopia-store/src/conversations.rs
index adc1fabb..f08abbc7 100644
--- a/crates/utopia-store/src/conversations.rs
+++ b/crates/utopia-store/src/conversations.rs
@@ -171,12 +171,15 @@ pub async fn retry_target(
/// 「我已经查过了」这件事也一起省掉了:跨轮之后模型只看得见自己写的散文,
/// 于是接着说「翻译」时重查一遍,还落到了另一批同名实体上。
///
-/// 折中是**只回放最近一轮**:需要的是「我刚做过什么」,不是二十轮的输出。
+/// 工具往返**只回放最近一轮**:需要的是「我刚做过什么」,不是二十轮的输出。
+/// 协议要求的终答推理则随各条正文回放,不属于可以单独裁掉的工具证据。
pub struct History {
/// Identities parallel to turns, so callers can exclude their own appended question.
pub turn_ids: Vec<Uuid>,
/// `(role, content)`,按时间序
pub turns: Vec<(String, String)>,
+ /// 终答的协议推理,和 `turns` 一一对应;不混入用户看到的正文。
+ pub reasoning: Vec<Option<String>>,
/// 这场对话里已经认下的实体(去重)
pub entities: Vec<serde_json::Value>,
/// **最近一轮助手做过什么**:带 `tool_calls` 的助手消息与配套的 tool 结果。
@@ -213,6 +216,17 @@ pub async fn recent_context(pool: &PgPool, conversation_id: Uuid, n: i64) -> App
.fetch_all(pool)
.await?;
rows.reverse();
+ Ok(history_from_rows(rows))
+}
+
+fn reasoning_metadata(message: &serde_json::Value) -> bool {
+ message["role"] == "assistant"
+ && message.get("tool_calls").is_none()
+ && message.get("content").is_none()
+ && message.get("reasoning_content").is_some()
+}
+
+fn history_from_rows(rows: Vec<ContextRow>) -> History {
// 实体按 id 去重、保持首次出现的顺序:同一个实体在几轮里反复出现是常态,
// 每轮各列一遍只是把同一件事说三遍
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
@@ -227,14 +241,30 @@ pub async fn recent_context(pool: &PgPool, conversation_id: Uuid, n: i64) -> App
}
// 最后一条助手消息的那一段。**倒着找**——最后一条通常是刚落库的用户消息
let last_assistant = rows.iter().rev().find(|row| row.role == "assistant");
- let last_tool_exchange = last_assistant
+ let mut last_tool_exchange = last_assistant
.and_then(|row| row.tool_exchange.as_array().cloned())
.unwrap_or_default();
+ last_tool_exchange.retain(|m| !reasoning_metadata(m));
let last_sources = last_assistant
.and_then(|row| row.sources.as_array().cloned())
.unwrap_or_default();
- Ok(History {
+ History {
turn_ids: rows.iter().map(|row| row.id).collect(),
+ reasoning: rows
+ .iter()
+ .map(|row| {
+ if row.role != "assistant" {
+ return None;
+ }
+ row.tool_exchange
+ .as_array()?
+ .iter()
+ .rev()
+ .find(|m| reasoning_metadata(m))?["reasoning_content"]
+ .as_str()
+ .map(str::to_owned)
+ })
+ .collect(),
turns: rows
.into_iter()
.map(|mut row| {
@@ -250,7 +280,7 @@ pub async fn recent_context(pool: &PgPool, conversation_id: Uuid, n: i64) -> App
entities,
last_tool_exchange,
last_sources,
- })
+ }
}
/// 一轮除了正文之外留下的东西。
@@ -268,7 +298,8 @@ pub struct TurnRecord {
/// 这一轮认下的实体(id / 名字 / 类型)。下一轮回放,让模型接着走而不是重搜
pub resolved: serde_json::Value,
/// 这一轮调了什么、拿回什么(已截断的那一份)。下一轮回放最近的一段——
- /// 没有它,模型跨轮之后就不知道自己查过,于是重查
+ /// 没有它,模型跨轮之后就不知道自己查过,于是重查。
+ /// 末尾可附仅含 `role` 和 `reasoning_content` 的终答协议 metadata。
pub tool_exchange: serde_json::Value,
}
@@ -336,3 +367,57 @@ pub async fn delete(
}
Ok(())
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::json;
+
+ #[test]
+ fn history_separates_terminal_reasoning_from_tool_exchanges() {
+ let row = |role: &str, tool_exchange| ContextRow {
+ id: Uuid::now_v7(),
+ role: role.into(),
+ content: "same text".into(),
+ resolved: json!([]),
+ tool_exchange,
+ sources: json!([]),
+ stopped: false,
+ };
+ let call = json!({"role":"assistant","reasoning_content":"tool thought",
+ "tool_calls":[{"id":"c"}]} );
+ let result = json!({"role":"tool","tool_call_id":"c","content":"ok"});
+ let history = history_from_rows(vec![
+ row("user", json!([])),
+ row("assistant", json!([])),
+ row(
+ "assistant",
+ json!([{"role":"assistant","reasoning_content":"older thought"}]),
+ ),
+ row(
+ "assistant",
+ json!([{"role":"assistant","reasoning_content":7}]),
+ ),
+ row(
+ "assistant",
+ json!([call.clone(), result.clone(),
+ {"role":"assistant","reasoning_content":""}]),
+ ),
+ row("user", json!([])),
+ ]);
+ assert_eq!(
+ history.reasoning,
+ [
+ None,
+ None,
+ Some("older thought".into()),
+ None,
+ Some(String::new()),
+ None
+ ]
+ );
+ assert_eq!(history.reasoning.len(), history.turns.len());
+ assert_eq!(history.turn_ids.len(), history.turns.len());
+ assert_eq!(history.last_tool_exchange, [call, result]);
+ }
+}