返回 CodeWhale
reasoning_content_replayed_after_tool_call.rs
根目录 / crates / tui / tests / integration / reasoning_content_replayed_after_tool_call.rs
1 use futures_util::StreamExt;
2
3 use crate::llm_client::LlmClient;
4 use crate::llm_client::mock::{MockLlmClient, canned};
5 use codewhale_models::Role;
6 use codewhale_models::{ContentBlock, Message, MessageRequest};
7
8 fn user_message(text: &str) -> Message {
9 Message {
10 role: Role::User,
11 content: vec![ContentBlock::Text {
12 text: text.to_string(),
13 cache_control: None,
14 }],
15 }
16 }
17
18 fn assistant_thinking_tool_call(
19 thinking: &str,
20 id: &str,
21 name: &str,
22 input: serde_json::Value,
23 ) -> Message {
24 Message {
25 role: Role::Assistant,
26 content: vec![
27 ContentBlock::Thinking {
28 thinking: thinking.to_string(),
29 signature: None,
30 state: None,
31 redacted_data: None,
32 },
33 ContentBlock::ToolUse {
34 execution_id: None,
35 id: id.to_string(),
36 name: name.to_string(),
37 input,
38 caller: None,
39 thought_signature: None,
40 },
41 ],
42 }
43 }
44
45 fn tool_result_message(tool_use_id: &str, content: &str) -> Message {
46 Message {
47 role: Role::User,
48 content: vec![ContentBlock::ToolResult {
49 execution_id: None,
50 tool_use_id: tool_use_id.to_string(),
51 content: content.to_string(),
52 is_error: None,
53 content_blocks: None,
54 }],
55 }
56 }
57
58 fn make_request(messages: Vec<Message>) -> MessageRequest {
59 MessageRequest {
60 model: "deepseek-v4-pro".to_string(),
61 messages,
62 max_tokens: 4096,
63 system: None,
64 tools: None,
65 tool_choice: None,
66 metadata: None,
67 thinking: None,
68 reasoning_effort: Some("high".to_string()),
69 stream: Some(true),
70 temperature: None,
71 top_p: None,
72 safety_check: None,
73 title: None,
74 }
75 }
76
77 #[tokio::test]
78 async fn reasoning_content_is_replayed_after_thinking_tool_call() {
79 let mock = MockLlmClient::new(vec![]);
80
81 mock.push_turn(vec![
82 canned::message_start("r1"),
83 canned::thinking_delta(0, "I should inspect /tmp before answering."),
84 canned::tool_use_block_start(1, "call_a", "list_dir"),
85 canned::tool_input_delta(1, r#"{"path":"/tmp"}"#),
86 canned::block_stop(1),
87 canned::message_delta("tool_use", None),
88 canned::message_stop(),
89 ]);
90
91 mock.push_factory(|request| {
92 let assistant = request
93 .messages
94 .iter()
95 .rev()
96 .find(|message| message.role == "assistant")
97 .expect("follow-up request must include the prior assistant tool-call turn");
98
99 assert!(
100 assistant
101 .content
102 .iter()
103 .any(|block| matches!(block, ContentBlock::Thinking { .. })),
104 "DeepSeek V4 follow-up requests must replay reasoning_content on the assistant tool-call turn"
105 );
106
107 canned::simple_text_turn("I see the /tmp entries.")
108 });
109
110 let mut first = mock
111 .create_message_stream(make_request(vec![user_message("list /tmp")]))
112 .await
113 .expect("first stream opens");
114 while first.next().await.is_some() {}
115
116 let mut second = mock
117 .create_message_stream(make_request(vec![
118 user_message("list /tmp"),
119 assistant_thinking_tool_call(
120 "I should inspect /tmp before answering.",
121 "call_a",
122 "list_dir",
123 serde_json::json!({ "path": "/tmp" }),
124 ),
125 tool_result_message("call_a", "/tmp/file1\n/tmp/file2"),
126 ]))
127 .await
128 .expect("second stream opens");
129 while second.next().await.is_some() {}
130 }
131
131 lines RUST