1use std::borrow::Cow;
7
8use anyhow::{Context, Result};
9use serde_json::Value as JsonValue;
10
11use crate::python::PyJsonFormatter;
12
13pub mod tokens {
15 pub const BOS: &str = "<|begin▁of▁sentence|>";
16 pub const EOS: &str = "<|end▁of▁sentence|>";
17 pub const THINKING_START: &str = "<think>";
18 pub const THINKING_END: &str = "</think>";
19 pub const DSML_TOKEN: &str = "|DSML|";
20 pub const USER_START: &str = "<|User|>";
21 pub const ASSISTANT_START: &str = "<|Assistant|>";
22 pub const LATEST_REMINDER: &str = "<|latest_reminder|>";
23
24 pub const TASK_ACTION: &str = "<|action|>";
26 pub const TASK_QUERY: &str = "<|query|>";
27 pub const TASK_AUTHORITY: &str = "<|authority|>";
28 pub const TASK_DOMAIN: &str = "<|domain|>";
29 pub const TASK_TITLE: &str = "<|title|>";
30 pub const TASK_READ_URL: &str = "<|read_url|>";
31}
32
33pub(crate) const TOOL_CALLS_BLOCK_NAME: &str = "tool_calls";
34
35pub(crate) const RESPONSE_FORMAT_TEMPLATE: &str =
36 "## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n{schema}";
37
38pub(crate) const TOOLS_TEMPLATE: &str = r#"## Tools
39
40You have access to a set of tools to help answer the user's question. You can invoke tools by writing a "<{dsml_token}tool_calls>" block like the following:
41
42<{dsml_token}tool_calls>
43<{dsml_token}invoke name="$TOOL_NAME">
44<{dsml_token}parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE</{dsml_token}parameter>
45...
46</{dsml_token}invoke>
47<{dsml_token}invoke name="$TOOL_NAME2">
48...
49</{dsml_token}invoke>
50</{dsml_token}tool_calls>
51
52String parameters should be specified as is and set `string="true"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string="false"`.
53
54If thinking_mode is enabled (triggered by {thinking_start_token}), you MUST output your complete reasoning inside {thinking_start_token}...{thinking_end_token} BEFORE any tool calls or final response.
55
56Otherwise, output directly after {thinking_end_token} with tool calls or final response.
57
58### Available Tool Schemas
59
60{tool_schemas}
61
62You MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.
63"#;
64
65pub(crate) const TOOLS_SYSTEM_TEMPLATE: &str = r#"## Tools
67
68You have access to a set of tools you can use to answer the user's question.
69You can invoke functions by writing a "<{dsml_token}function_calls>" block like the following as part of your reply to the user:
70<{dsml_token}function_calls>
71<{dsml_token}invoke name="$FUNCTION_NAME">
72<{dsml_token}parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE</{dsml_token}parameter>
73...
74</{dsml_token}invoke>
75<{dsml_token}invoke name="$FUNCTION_NAME2">
76...
77</{dsml_token}invoke>
78</{dsml_token}function_calls>
79
80String and scalar parameters should be specified as is without any escaping or quotes, while lists and objects should use JSON format. The "string" attribute should be set to "true" for string type parameters and "false" for other types (numbers, booleans, arrays, objects).
81
82If the thinking_mode is enabled, then after function results you should strongly consider outputting a thinking block. Here is an example:
83
84<{dsml_token}function_calls>
85...
86</{dsml_token}function_calls>
87
88<function_results>
89...
90</function_results>
91
92{thinking_start_token}...thinking about results{thinking_end_token}
93
94Here are the functions available in JSONSchema format:
95<functions>
96{tool_schemas}
97</functions>
98"#;
99
100pub(crate) const REASONING_EFFORT_HIGH: &str = "Reasoning Effort: Absolute maximum with no shortcuts permitted.\nYou MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.\nExplicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.\n\n";
101
102pub(crate) const REASONING_EFFORT_MAX: &str = "Reasoning Effort: Beyond maximum — exhaustive, relentless, and uncompromising.\nYou MUST reason with the utmost depth and rigor, leaving absolutely nothing to chance: exhaustively decompose the problem into its most fundamental components, trace every causal chain to its root, and resolve the underlying cause rather than any surface symptom.\nDo not stop reasoning until you have independently verified the solution from multiple angles and are certain that no assumption remains unchecked and no error remains undiscovered.\n\n";
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub enum ThinkingMode {
107 Chat,
108 Thinking,
109}
110
111impl ThinkingMode {
112 pub fn as_str(&self) -> &'static str {
113 match self {
114 ThinkingMode::Chat => "chat",
115 ThinkingMode::Thinking => "thinking",
116 }
117 }
118}
119
120#[derive(Debug, Clone, Copy, PartialEq, Eq)]
122pub enum ReasoningEffort {
123 Max,
124 High,
125}
126
127#[derive(Debug, Clone, Copy, PartialEq, Eq)]
128pub(crate) enum NormalizeNonText {
129 SerializeJson,
130 LeaveUntouched,
131}
132
133pub(crate) fn to_json(value: &JsonValue) -> String {
137 use serde::Serialize;
138
139 let mut buf = Vec::with_capacity(64);
141 let mut ser = serde_json::Serializer::with_formatter(&mut buf, PyJsonFormatter);
142 value
143 .serialize(&mut ser)
144 .expect("JsonValue serialization to Vec<u8> is infallible");
145 String::from_utf8(buf).expect("serde_json output is always valid UTF-8")
146}
147
148pub(crate) fn render_tools(template: &str, tools: &[JsonValue]) -> String {
149 let tools_json: Vec<String> = tools
150 .iter()
151 .filter_map(|tool| tool.get("function"))
152 .map(to_json)
153 .collect();
154
155 template
158 .replace("{dsml_token}", tokens::DSML_TOKEN)
159 .replace("{thinking_start_token}", tokens::THINKING_START)
160 .replace("{thinking_end_token}", tokens::THINKING_END)
161 .replace("{tool_schemas}", &tools_json.join("\n"))
162}
163
164pub(crate) fn find_last_user_index(messages: &[JsonValue]) -> Option<usize> {
165 messages
166 .iter()
167 .enumerate()
168 .rev()
169 .find(|(_, msg)| {
170 msg.get("role")
171 .and_then(|r| r.as_str())
172 .map(|r| r == "user" || r == "developer")
173 .unwrap_or(false)
174 })
175 .map(|(idx, _)| idx)
176}
177
178pub(crate) fn extract_visible_text(content: &JsonValue) -> String {
179 match content {
180 JsonValue::String(text) => text.clone(),
181 JsonValue::Array(items) => items
182 .iter()
183 .filter_map(|item| {
184 if let Some(text) = item.as_str() {
185 return Some(text.to_string());
186 }
187 let item_type = item.get("type").and_then(|v| v.as_str());
188 if item_type == Some("text") {
189 return item
190 .get("text")
191 .and_then(|v| v.as_str())
192 .map(|text| text.to_string());
193 }
194 tracing::warn!(
195 chunk_type = item_type.unwrap_or("unknown"),
196 "DeepSeek formatter dropped non-text content chunk while normalizing message content",
197 );
198 None
199 })
200 .collect::<String>(),
201 _ => to_json(content),
202 }
203}
204
205pub(crate) fn normalize_message_contents(messages: &mut [JsonValue], non_text: NormalizeNonText) {
206 for msg in messages {
207 let Some(content) = msg.get("content") else {
208 continue;
209 };
210 if content.is_string() {
212 continue;
213 }
214 if !content.is_array() && non_text == NormalizeNonText::LeaveUntouched {
215 continue;
216 }
217 let normalized = extract_visible_text(content);
218 if let Some(obj) = msg.as_object_mut() {
219 obj.insert("content".to_string(), JsonValue::String(normalized));
220 }
221 }
222}
223
224pub(crate) fn encode_arguments_to_dsml(tool_call: &JsonValue) -> Result<String> {
225 let arguments_str = tool_call
226 .get("arguments")
227 .and_then(|a| a.as_str())
228 .context("Missing or invalid 'arguments' field")?;
229
230 let arguments: JsonValue = match serde_json::from_str(arguments_str) {
232 Ok(v) => v,
233 Err(_) => serde_json::json!({ "arguments": arguments_str }),
234 };
235
236 let arguments_obj = arguments
237 .as_object()
238 .context("Arguments must be a JSON object")?;
239
240 let mut params = Vec::new();
241 for (key, value) in arguments_obj {
242 let value_str = if let Some(vs) = value.as_str() {
243 vs.to_string()
244 } else {
245 to_json(value)
246 };
247 params.push(format!(
248 "<{}parameter name=\"{}\" string=\"{}\">{}</{}parameter>",
249 tokens::DSML_TOKEN,
250 key,
251 if value.is_string() { "true" } else { "false" },
252 value_str,
253 tokens::DSML_TOKEN
254 ));
255 }
256
257 Ok(params.join("\n"))
258}
259
260pub(crate) fn task_token(task: &str) -> Option<&'static str> {
261 match task {
262 "action" => Some(tokens::TASK_ACTION),
263 "query" => Some(tokens::TASK_QUERY),
264 "authority" => Some(tokens::TASK_AUTHORITY),
265 "domain" => Some(tokens::TASK_DOMAIN),
266 "title" => Some(tokens::TASK_TITLE),
267 "read_url" => Some(tokens::TASK_READ_URL),
268 _ => None,
269 }
270}
271
272const USER_FIELDS_TO_PRESERVE: [&str; 3] = ["task", "wo_eos", "mask"];
273
274fn preserve_user_fields(target: &mut JsonValue, source: &JsonValue) {
275 if let Some(obj) = target.as_object_mut() {
276 for key in USER_FIELDS_TO_PRESERVE {
277 if let Some(v) = source.get(key) {
278 obj.insert(key.to_string(), v.clone());
279 }
280 }
281 }
282}
283
284pub(crate) fn merge_tool_messages(messages: Vec<JsonValue>) -> Vec<JsonValue> {
289 let mut merged: Vec<JsonValue> = Vec::with_capacity(messages.len());
290
291 for mut msg in messages {
292 let role = msg.get("role").and_then(|r| r.as_str()).unwrap_or("");
293
294 if role == "tool" {
295 let obj = msg
296 .as_object_mut()
297 .expect("a message with a role is an object");
298 let tool_block = JsonValue::Object(serde_json::Map::from_iter([
299 ("type".into(), JsonValue::String("tool_result".into())),
300 (
301 "tool_use_id".into(),
302 obj.remove("tool_call_id")
303 .unwrap_or_else(|| JsonValue::String(String::new())),
304 ),
305 (
306 "content".into(),
307 obj.remove("content")
308 .unwrap_or_else(|| JsonValue::String(String::new())),
309 ),
310 ]));
311
312 let can_merge = merged
313 .last()
314 .map(|m| {
315 m.get("role").and_then(|r| r.as_str()) == Some("user")
316 && m.get("content_blocks").is_some()
317 })
318 .unwrap_or(false);
319
320 if can_merge {
321 let last = merged.last_mut().unwrap();
322 if let Some(blocks) = last
323 .as_object_mut()
324 .and_then(|o| o.get_mut("content_blocks"))
325 .and_then(|v| v.as_array_mut())
326 {
327 blocks.push(tool_block);
328 }
329 } else {
330 merged.push(JsonValue::Object(serde_json::Map::from_iter([
331 ("role".into(), JsonValue::String("user".into())),
332 ("content_blocks".into(), JsonValue::Array(vec![tool_block])),
333 ])));
334 }
335 } else if role == "user" {
336 let text = msg
337 .as_object_mut()
338 .expect("a message with a role is an object")
339 .remove("content")
340 .filter(JsonValue::is_string)
341 .unwrap_or_else(|| JsonValue::String(String::new()));
342 let text_block = JsonValue::Object(serde_json::Map::from_iter([
343 ("type".into(), JsonValue::String("text".into())),
344 ("text".into(), text),
345 ]));
346
347 let can_merge = merged
348 .last()
349 .map(|m| {
350 m.get("role").and_then(|r| r.as_str()) == Some("user")
351 && m.get("content_blocks").is_some()
352 && m.get("task").map(|v| v.is_null()).unwrap_or(true)
353 })
354 .unwrap_or(false);
355
356 if can_merge {
357 if let Some(blocks) = merged
359 .last_mut()
360 .and_then(|last| last.get_mut("content_blocks"))
361 .and_then(|v| v.as_array_mut())
362 {
363 blocks.push(text_block);
364 }
365 } else {
366 let mut new_msg = JsonValue::Object(serde_json::Map::from_iter([
369 ("role".into(), JsonValue::String("user".into())),
370 ("content_blocks".into(), JsonValue::Array(vec![text_block])),
371 ]));
372 preserve_user_fields(&mut new_msg, &msg);
373 merged.push(new_msg);
374 }
375 } else {
376 merged.push(msg);
377 }
378 }
379
380 merged
381}
382
383pub(crate) fn sort_tool_results_by_call_order(mut messages: Vec<JsonValue>) -> Vec<JsonValue> {
386 use std::collections::HashMap;
387 let mut last_order: HashMap<String, usize> = HashMap::new();
388
389 for msg in &mut messages {
390 let role = msg.get("role").and_then(|r| r.as_str()).unwrap_or("");
391 if role == "assistant" {
392 if let Some(tcs) = msg
394 .get("tool_calls")
395 .and_then(|t| t.as_array())
396 .filter(|t| !t.is_empty())
397 {
398 last_order.clear();
399 for (idx, tc) in tcs.iter().enumerate() {
400 let id = tc
401 .get("id")
402 .and_then(|v| v.as_str())
403 .or_else(|| {
404 tc.get("function")
405 .and_then(|f| f.get("id"))
406 .and_then(|v| v.as_str())
407 })
408 .unwrap_or("");
409 if !id.is_empty() {
410 last_order.insert(id.to_string(), idx);
411 }
412 }
413 }
414 } else if role == "user" && !last_order.is_empty() {
415 let Some(blocks) = msg
416 .as_object_mut()
417 .and_then(|o| o.get_mut("content_blocks"))
418 .and_then(|v| v.as_array_mut())
419 else {
420 continue;
421 };
422
423 let tool_positions: Vec<usize> = blocks
425 .iter()
426 .enumerate()
427 .filter(|(_, b)| b.get("type").and_then(|v| v.as_str()) == Some("tool_result"))
428 .map(|(i, _)| i)
429 .collect();
430
431 if tool_positions.len() > 1 {
432 let start = *tool_positions
433 .first()
434 .expect("tool_positions has length > 1");
435 let end = *tool_positions
436 .last()
437 .expect("tool_positions has length > 1");
438 let is_contiguous = end - start + 1 == tool_positions.len();
439
440 if is_contiguous {
441 blocks[start..=end].sort_by_key(|b| {
443 let id = b.get("tool_use_id").and_then(|v| v.as_str()).unwrap_or("");
444 *last_order.get(id).unwrap_or(&0)
445 });
446 } else {
447 let mut tool_blocks: Vec<JsonValue> = tool_positions
449 .iter()
450 .map(|&i| std::mem::take(&mut blocks[i]))
451 .collect();
452
453 tool_blocks.sort_by_key(|b| {
454 let id = b.get("tool_use_id").and_then(|v| v.as_str()).unwrap_or("");
455 *last_order.get(id).unwrap_or(&0)
456 });
457
458 for (sorted_idx, &pos) in tool_positions.iter().enumerate() {
459 blocks[pos] = std::mem::take(&mut tool_blocks[sorted_idx]);
460 }
461 }
462 }
463 }
464 }
465
466 messages
467}
468
469pub(crate) fn reasoning_text(reasoning: Option<&JsonValue>) -> Cow<'_, str> {
470 match reasoning {
471 Some(JsonValue::String(text)) => Cow::Borrowed(text),
472 Some(JsonValue::Array(segments)) => Cow::Owned(
473 segments
474 .iter()
475 .filter_map(JsonValue::as_str)
476 .filter(|text| !text.is_empty())
477 .collect::<Vec<_>>()
478 .join("\n"),
479 ),
480 _ => Cow::Borrowed(""),
481 }
482}
483
484pub(crate) fn drop_thinking_messages(messages: Vec<JsonValue>) -> Vec<JsonValue> {
486 let last_user_idx = find_last_user_index(&messages);
487 let mut out = Vec::with_capacity(messages.len());
488 const KEEP: &[&str] = &[
489 "user",
490 "system",
491 "tool",
492 "latest_reminder",
493 "direct_search_results",
494 ];
495
496 for (idx, mut msg) in messages.into_iter().enumerate() {
497 let role = msg.get("role").and_then(|r| r.as_str()).unwrap_or("");
498 if KEEP.contains(&role) || last_user_idx.is_none_or(|u| idx >= u) {
499 out.push(msg);
500 } else if role == "assistant" {
501 if let Some(obj) = msg.as_object_mut() {
502 obj.remove("reasoning_content");
503 }
504 out.push(msg);
505 }
506 }
508 out
509}
510
511pub(crate) fn resolve_thinking_mode(
512 args: Option<&std::collections::HashMap<String, serde_json::Value>>,
513 default_mode: ThinkingMode,
514) -> ThinkingMode {
515 if let Some(enabled) = crate::thinking_bool_from_args(args) {
516 return if enabled {
517 ThinkingMode::Thinking
518 } else {
519 ThinkingMode::Chat
520 };
521 }
522 if let Some(args) = args
523 && let Some(mode) = args.get("thinking_mode").and_then(|v| v.as_str())
524 {
525 match mode {
526 "chat" => return ThinkingMode::Chat,
527 "thinking" => return ThinkingMode::Thinking,
528 _ => {}
529 }
530 }
531 default_mode
532}
533
534pub(crate) fn inject_tools_and_response_format(
535 messages_array: &mut Vec<JsonValue>,
536 req: &dyn crate::OAIChatLikeRequest,
537) -> Result<()> {
538 let tools_json = req
539 .tools()
540 .map(|t| crate::python::template_json(&t))
541 .transpose()
542 .context("Failed to convert tools to JSON")?;
543
544 let tools_json = match req.tool_choice() {
550 Some(ref tc) if tc.as_str() == Some("none") => None,
551 _ => tools_json,
552 };
553
554 let response_format_json = req
555 .response_format()
556 .map(|rf| crate::python::template_json(&rf))
557 .transpose()
558 .context("Failed to convert response_format to JSON")?;
559
560 if tools_json.is_some() || response_format_json.is_some() {
561 let system_idx = messages_array
562 .iter()
563 .position(|msg| msg.get("role").and_then(|r| r.as_str()) == Some("system"));
564
565 if let Some(idx) = system_idx {
566 if let Some(msg) = messages_array.get_mut(idx)
567 && let Some(obj) = msg.as_object_mut()
568 {
569 if let Some(tools) = tools_json {
570 obj.insert("tools".to_string(), tools);
571 }
572 if let Some(rf) = response_format_json {
573 obj.insert("response_format".to_string(), rf);
574 }
575 }
576 } else {
577 let mut system_msg = serde_json::json!({
578 "role": "system",
579 "content": ""
580 });
581 if let Some(obj) = system_msg.as_object_mut() {
582 if let Some(tools) = tools_json {
583 obj.insert("tools".to_string(), tools);
584 }
585 if let Some(rf) = response_format_json {
586 obj.insert("response_format".to_string(), rf);
587 }
588 }
589 messages_array.insert(0, system_msg);
590 }
591 }
592 Ok(())
593}
594
595#[cfg(test)]
596mod tests {
597
598 #[test]
599 fn to_json_writes_python_floats() {
600 let value = serde_json::json!({"a": 0.000001, "b": [1e16, 3]});
601 assert_eq!(to_json(&value), r#"{"a": 1e-06, "b": [1e+16, 3]}"#);
602 }
603
604 use super::*;
605 use serde_json::json;
606
607 #[test]
608 fn merged_users_drop_their_metadata_like_the_reference() {
609 let merged = merge_tool_messages(vec![
612 json!({"role": "tool", "tool_call_id": "c1", "content": "result"}),
613 json!({"role": "user", "content": "one", "wo_eos": true, "mask": [1, 0]}),
614 json!({"role": "user", "content": "two", "task": "action"}),
615 json!({"role": "assistant", "content": "a"}),
616 json!({"role": "user", "content": "three", "task": "action"}),
617 json!({"role": "user", "content": "four", "wo_eos": true}),
618 ]);
619 assert_eq!(
620 merged,
621 [
622 json!({"role": "user", "content_blocks": [
623 {"type": "tool_result", "tool_use_id": "c1", "content": "result"},
624 {"type": "text", "text": "one"},
625 {"type": "text", "text": "two"}
626 ]}),
627 json!({"role": "assistant", "content": "a"}),
628 json!({"role": "user", "content_blocks": [{"type": "text", "text": "three"}], "task": "action"}),
629 json!({"role": "user", "content_blocks": [{"type": "text", "text": "four"}], "wo_eos": true}),
630 ]
631 );
632 }
633
634 #[test]
635 fn test_extract_visible_text_from_content_array() {
636 let content = json!([
637 {"type": "text", "text": "who "},
638 {"type": "text", "text": "are "},
639 {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}},
640 {"type": "text", "text": "you?"}
641 ]);
642 assert_eq!(extract_visible_text(&content), "who are you?");
643 }
644
645 #[test]
646 fn test_render_tools_preserves_placeholder_text_inside_tool_schema() {
647 let tools = json!([{
648 "type": "function",
649 "function": {
650 "name": "placeholder_tool",
651 "description": "literal {dsml_token} {thinking_start_token} {thinking_end_token}",
652 "parameters": {"type": "object", "properties": {}}
653 }
654 }]);
655 let rendered = render_tools(
656 "static {dsml_token} {thinking_start_token} {thinking_end_token}\n{tool_schemas}",
657 tools.as_array().unwrap(),
658 );
659
660 assert!(rendered.starts_with(&format!(
661 "static {} {} {}\n",
662 tokens::DSML_TOKEN,
663 tokens::THINKING_START,
664 tokens::THINKING_END
665 )));
666 assert!(
667 rendered.contains("literal {dsml_token} {thinking_start_token} {thinking_end_token}")
668 );
669 }
670}