1use anyhow::{Context, Result};
11use serde_json::Value as JsonValue;
12use std::fmt::Write;
13
14use super::common::{
15 NormalizeNonText, REASONING_EFFORT_HIGH, REASONING_EFFORT_MAX, RESPONSE_FORMAT_TEMPLATE,
16 TOOL_CALLS_BLOCK_NAME, TOOLS_TEMPLATE, drop_thinking_messages, encode_arguments_to_dsml,
17 find_last_user_index, merge_tool_messages, normalize_message_contents, reasoning_text,
18 render_tools, sort_tool_results_by_call_order, task_token, to_json,
19};
20pub use super::common::{ReasoningEffort, ThinkingMode, tokens};
21
22#[derive(Clone, Copy)]
23pub(super) enum Encoding {
24 V4(Option<ReasoningEffort>),
25 V41 {
26 effort: u8,
27 add_generation_prompt: bool,
28 },
29}
30
31impl Encoding {
32 fn is_v41(self) -> bool {
33 matches!(self, Self::V41 { .. })
34 }
35
36 fn tag(self, v4: &'static str, v41: &'static str) -> &'static str {
37 if self.is_v41() { v41 } else { v4 }
38 }
39
40 fn reasoning_prefix(self) -> String {
41 match self {
42 Self::V4(Some(ReasoningEffort::High)) => REASONING_EFFORT_HIGH.to_string(),
43 Self::V4(Some(ReasoningEffort::Max)) => REASONING_EFFORT_MAX.to_string(),
44 Self::V4(None) => String::new(),
45 Self::V41 { effort, .. } => format!(
46 "Reasoning Effort: {effort} (range 1-100, the higher the value, the more thorough the reasoning)\n\n"
47 ),
48 }
49 }
50
51 fn render_tools(self, tools: &[JsonValue]) -> String {
52 let template = if self.is_v41() {
53 TOOLS_TEMPLATE
54 .replace("{dsml_token}tool_calls", "{dsml_token} calls")
55 .replace("{dsml_token}invoke", "{dsml_token} invoke")
56 .replace("{dsml_token}parameter", "{dsml_token} parameter")
57 } else {
58 TOOLS_TEMPLATE.to_string()
59 };
60 render_tools(&template, tools)
61 }
62}
63
64fn render_message(
66 prompt: &mut String,
67 index: usize,
68 messages: &[JsonValue],
69 thinking_mode: ThinkingMode,
70 drop_thinking: bool,
71 encoding: Encoding,
72 last_user_idx: Option<usize>,
73) -> Result<()> {
74 let msg = &messages[index];
75
76 let role = msg
77 .get("role")
78 .and_then(|r| r.as_str())
79 .context("Missing 'role' field")?;
80
81 if encoding.is_v41()
82 && (role == "system" || (index == 0 && thinking_mode == ThinkingMode::Thinking))
83 {
84 prompt.push_str("<|System|>");
85 }
86 if index == 0 && thinking_mode == ThinkingMode::Thinking {
87 prompt.push_str(&encoding.reasoning_prefix());
88 }
89
90 match role {
91 "system" => {
92 let content = msg.get("content").and_then(|c| c.as_str()).unwrap_or("");
93 prompt.push_str(content);
94 if let Some(tools) = msg
96 .get("tools")
97 .and_then(|t| t.as_array())
98 .filter(|t| !t.is_empty())
99 {
100 prompt.push_str("\n\n");
101 prompt.push_str(&encoding.render_tools(tools));
102 }
103 if let Some(response_format) = msg.get("response_format") {
104 prompt.push_str("\n\n");
105 prompt.push_str(
106 &RESPONSE_FORMAT_TEMPLATE.replace("{schema}", &to_json(response_format)),
107 );
108 }
109 }
110
111 "developer" => {
112 let content = msg
113 .get("content")
114 .and_then(|c| c.as_str())
115 .filter(|s| !s.is_empty())
116 .context("Developer role requires content")?;
117
118 prompt.push_str(tokens::USER_START);
119 prompt.push_str(content);
120
121 if let Some(tools) = msg
122 .get("tools")
123 .and_then(|t| t.as_array())
124 .filter(|t| !t.is_empty())
125 {
126 prompt.push_str("\n\n");
127 prompt.push_str(&encoding.render_tools(tools));
128 }
129 if let Some(response_format) = msg.get("response_format") {
130 prompt.push_str("\n\n");
131 prompt.push_str(
132 &RESPONSE_FORMAT_TEMPLATE.replace("{schema}", &to_json(response_format)),
133 );
134 }
135 }
136
137 "user" => {
138 prompt.push_str(tokens::USER_START);
139 if let Some(blocks) = msg.get("content_blocks").and_then(|b| b.as_array()) {
140 for (block_idx, block) in blocks.iter().enumerate() {
141 if block_idx > 0 {
142 prompt.push_str("\n\n");
143 }
144 let block_type = block.get("type").and_then(|v| v.as_str()).unwrap_or("");
145 match block_type {
146 "text" => {
147 let text = block.get("text").and_then(|v| v.as_str()).unwrap_or("");
148 prompt.push_str(text);
149 }
150 "tool_result" => {
151 prompt.push_str("<tool_result>");
152 render_tool_result_content(
153 prompt,
154 block.get("content").unwrap_or(&JsonValue::Null),
155 )?;
156 prompt.push_str("</tool_result>");
157 }
158 other => {
159 write!(prompt, "[Unsupported {}]", other)?;
160 }
161 }
162 }
163 }
164 }
165
166 "latest_reminder" => {
167 let content = msg.get("content").and_then(|c| c.as_str()).unwrap_or("");
168 prompt.push_str(tokens::LATEST_REMINDER);
169 prompt.push_str(content);
170 }
171
172 "tool" => {
173 anyhow::bail!(
174 "deepseek_v4 merges tool messages into user; preprocess with merge_tool_messages()"
175 );
176 }
177
178 "assistant" => {
179 let content = msg.get("content").and_then(|c| c.as_str()).unwrap_or("");
180 let reasoning = reasoning_text(msg.get("reasoning_content"));
181 let wo_eos = msg.get("wo_eos").and_then(|v| v.as_bool()).unwrap_or(false);
182
183 let prev_has_task = index > 0
184 && messages[index - 1]
185 .get("task")
186 .map(|v| !v.is_null())
187 .unwrap_or(false);
188
189 if thinking_mode == ThinkingMode::Thinking && !prev_has_task {
190 let render_thinking = !drop_thinking || last_user_idx.is_none_or(|u| index > u);
191 if render_thinking {
192 prompt.push_str(&reasoning);
193 prompt.push_str(tokens::THINKING_END);
194 }
195 }
196
197 prompt.push_str(content);
198
199 if let Some(tool_calls) = msg.get("tool_calls").and_then(|t| t.as_array())
200 && !tool_calls.is_empty()
201 {
202 prompt.push_str("\n\n");
203 writeln!(
204 prompt,
205 "<{}{}>",
206 tokens::DSML_TOKEN,
207 encoding.tag(TOOL_CALLS_BLOCK_NAME, " calls")
208 )?;
209
210 for (call_idx, tc) in tool_calls.iter().enumerate() {
211 if call_idx > 0 {
212 prompt.push('\n');
213 }
214 let fn_obj = tc.get("function").unwrap_or(tc);
217 let name = fn_obj
218 .get("name")
219 .and_then(|n| n.as_str())
220 .context("Missing tool call name")?;
221 let arguments = if encoding.is_v41() {
222 super::v41::encode_arguments(fn_obj)?
223 } else {
224 encode_arguments_to_dsml(fn_obj)?
225 };
226 write!(
227 prompt,
228 "<{}{} name=\"{}\">\n{}\n</{}{}>",
229 tokens::DSML_TOKEN,
230 encoding.tag("invoke", " invoke"),
231 name,
232 arguments,
233 tokens::DSML_TOKEN,
234 encoding.tag("invoke", " invoke")
235 )?;
236 }
237 write!(
238 prompt,
239 "\n</{}{}>",
240 tokens::DSML_TOKEN,
241 encoding.tag(TOOL_CALLS_BLOCK_NAME, " calls")
242 )?;
243 }
244
245 if !wo_eos {
246 prompt.push_str(tokens::EOS);
247 }
248 }
249
250 other => anyhow::bail!("Unknown role: {}", other),
251 }
252
253 if index + 1 == messages.len()
255 && matches!(
256 encoding,
257 Encoding::V41 {
258 add_generation_prompt: false,
259 ..
260 }
261 )
262 {
263 return Ok(());
264 }
265
266 if index + 1 < messages.len() {
268 let next_role = messages[index + 1].get("role").and_then(|r| r.as_str());
269 if !matches!(next_role, Some("assistant") | Some("latest_reminder")) {
270 return Ok(());
271 }
272 }
273
274 let task = msg.get("task").and_then(|v| v.as_str());
276 if let Some(task) = task {
277 let sp = task_token(task).with_context(|| format!("Invalid task: '{}'", task))?;
278 if task != "action" {
279 prompt.push_str(sp);
280 } else {
281 prompt.push_str(tokens::ASSISTANT_START);
282 prompt.push_str(if thinking_mode != ThinkingMode::Thinking {
283 tokens::THINKING_END
284 } else {
285 tokens::THINKING_START
286 });
287 prompt.push_str(sp);
288 }
289 } else if matches!(role, "user" | "developer")
290 || (encoding.is_v41() && role == "system" && index > 0)
291 {
292 prompt.push_str(tokens::ASSISTANT_START);
293 let seed_thinking = thinking_mode == ThinkingMode::Thinking
294 && (!drop_thinking || last_user_idx.is_none_or(|u| index >= u));
295 prompt.push_str(if seed_thinking {
296 tokens::THINKING_START
297 } else {
298 tokens::THINKING_END
299 });
300 }
301
302 Ok(())
303}
304
305fn render_tool_result_content(prompt: &mut String, content: &JsonValue) -> Result<()> {
307 match content {
308 JsonValue::String(s) => prompt.push_str(s),
309 JsonValue::Array(items) => {
310 for (index, item) in items.iter().enumerate() {
311 if index > 0 {
312 prompt.push_str("\n\n");
313 }
314 let item_type = item.get("type").and_then(|v| v.as_str()).unwrap_or("");
315 if item_type == "text" {
316 prompt.push_str(item.get("text").and_then(|v| v.as_str()).unwrap_or(""));
317 } else {
318 write!(prompt, "[Unsupported {}]", item_type)?;
319 }
320 }
321 }
322 JsonValue::Null => {}
323 _ => prompt.push_str(&to_json(content)),
324 }
325 Ok(())
326}
327
328pub fn encode_messages(
332 messages: &[JsonValue],
333 thinking_mode: ThinkingMode,
334 add_bos_token: bool,
335) -> Result<String> {
336 encode_messages_with_options(messages, thinking_mode, add_bos_token, true, None)
337}
338
339pub fn encode_messages_with_options(
348 messages: &[JsonValue],
349 thinking_mode: ThinkingMode,
350 add_bos_token: bool,
351 drop_thinking: bool,
352 reasoning_effort: Option<ReasoningEffort>,
353) -> Result<String> {
354 encode_owned_messages(
355 messages.to_vec(),
356 thinking_mode,
357 add_bos_token,
358 drop_thinking,
359 Encoding::V4(reasoning_effort),
360 )
361}
362
363pub(super) fn encode_owned_messages(
364 messages: Vec<JsonValue>,
365 thinking_mode: ThinkingMode,
366 add_bos_token: bool,
367 drop_thinking: bool,
368 encoding: Encoding,
369) -> Result<String> {
370 let merged = merge_tool_messages(messages);
371 let mut full = if encoding.is_v41() {
373 merged
374 } else {
375 sort_tool_results_by_call_order(merged)
376 };
377
378 let mut prompt = String::new();
379 if add_bos_token {
380 prompt.push_str(tokens::BOS);
381 }
382
383 let has_tools = full.iter().any(|m| {
385 m.get("tools")
386 .map(|v| match v {
387 JsonValue::Array(a) => !a.is_empty(),
388 JsonValue::Null => false,
389 _ => true,
390 })
391 .unwrap_or(false)
392 });
393 let effective_drop_thinking = drop_thinking && !has_tools;
394
395 if thinking_mode == ThinkingMode::Thinking && effective_drop_thinking {
396 full = if encoding.is_v41() {
397 super::v41::drop_thinking_messages(full)
398 } else {
399 drop_thinking_messages(full)
400 };
401 }
402
403 let last_user_idx = if encoding.is_v41() {
404 super::v41::find_last_user_index(&full)
405 } else {
406 find_last_user_index(&full)
407 };
408 for idx in 0..full.len() {
409 render_message(
410 &mut prompt,
411 idx,
412 &full,
413 thinking_mode,
414 effective_drop_thinking,
415 encoding,
416 last_user_idx,
417 )?;
418 }
419
420 Ok(prompt)
421}
422
423#[derive(Debug)]
425pub struct DeepSeekV4Formatter {
426 thinking_mode: ThinkingMode,
427}
428
429impl DeepSeekV4Formatter {
430 pub fn new(thinking_mode: ThinkingMode) -> Self {
431 Self { thinking_mode }
432 }
433
434 pub fn new_thinking() -> Self {
436 Self::new(ThinkingMode::Thinking)
437 }
438
439 pub fn new_chat() -> Self {
441 Self::new(ThinkingMode::Chat)
442 }
443
444 fn resolve_reasoning_effort(v: Option<&JsonValue>) -> (bool, Option<ReasoningEffort>) {
445 match v.and_then(JsonValue::as_str) {
446 Some("none") => (true, None),
447 Some("max") => (false, Some(ReasoningEffort::Max)),
448 Some("high") | Some("medium") | Some("xhigh") => (false, Some(ReasoningEffort::High)),
449 Some("low") | Some("minimal") => (false, None),
450 None if v.is_none() => (false, Some(ReasoningEffort::High)),
451 _ => {
452 tracing::warn!(
453 value = ?v,
454 "reasoning_effort must be one of \"none\", \"minimal\", \"low\", \"medium\", \"high\", \"xhigh\", \"max\"; ignoring and using API default (high)"
455 );
456 (false, Some(ReasoningEffort::High))
457 }
458 }
459 }
460
461 fn resolve_drop_thinking(
462 args: Option<&std::collections::HashMap<String, serde_json::Value>>,
463 ) -> bool {
464 let Some(args) = args else { return true };
465 let Some(v) = args.get("drop_thinking") else {
466 return true;
467 };
468 if let Some(b) = v.as_bool() {
469 return b;
470 }
471 tracing::warn!(
472 value = ?v,
473 "chat_template_args.drop_thinking must be a bool; ignoring and using default (true)"
474 );
475 true
476 }
477}
478
479impl crate::OAIPromptFormatter for DeepSeekV4Formatter {
480 fn supports_add_generation_prompt(&self) -> bool {
481 true
482 }
483
484 fn render(&self, req: &dyn crate::OAIChatLikeRequest) -> Result<String> {
485 let args = req.chat_template_args();
486 let effort_value = req
487 .reasoning_effort()
488 .map(|value| serde_json::to_value(value).context("serialize reasoning_effort"))
489 .transpose()?
490 .or_else(|| args.and_then(|args| args.get("reasoning_effort").cloned()));
491 let (disable_thinking, reasoning_effort) =
492 Self::resolve_reasoning_effort(effort_value.as_ref());
493 let mut thinking_mode = super::common::resolve_thinking_mode(args, self.thinking_mode);
494 if disable_thinking {
495 thinking_mode = ThinkingMode::Chat;
496 }
497 let drop_thinking = Self::resolve_drop_thinking(args);
498
499 let messages_json = crate::messages_to_json(req)?;
500 crate::reject_unsupported_partial_assistant(&messages_json)?;
501 crate::reject_unsupported_message_tools(&messages_json, &["developer"])?;
502
503 let JsonValue::Array(mut messages_array) = messages_json else {
504 anyhow::bail!("Messages is not an array");
505 };
506
507 normalize_message_contents(&mut messages_array, NormalizeNonText::LeaveUntouched);
508
509 super::common::inject_tools_and_response_format(&mut messages_array, req)?;
510
511 encode_owned_messages(
512 messages_array,
513 thinking_mode,
514 true,
515 drop_thinking,
516 Encoding::V4(reasoning_effort),
517 )
518 }
519}
520
521#[cfg(test)]
522mod tests {
523 use super::*;
524 use serde_json::json;
525
526 #[test]
527 fn test_simple_conversation() {
528 let messages = json!([
529 {"role": "system", "content": "You are a helpful assistant."},
530 {"role": "user", "content": "Hello"},
531 {"role": "assistant", "reasoning_content": "greet", "content": "Hi!"},
532 {"role": "user", "content": "What is 2+2?"}
533 ]);
534 let out =
535 encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true).unwrap();
536 assert!(out.starts_with(tokens::BOS));
537 assert!(out.ends_with(&format!(
538 "{}{}",
539 tokens::ASSISTANT_START,
540 tokens::THINKING_START
541 )));
542 assert!(!out.contains("greet"));
544 }
545
546 #[test]
547 fn test_reasoning_effort_prefixes() {
548 let messages = json!([
549 {"role": "system", "content": "hi"},
550 {"role": "user", "content": "hello"}
551 ]);
552
553 let high = encode_messages_with_options(
554 messages.as_array().unwrap(),
555 ThinkingMode::Thinking,
556 true,
557 true,
558 Some(ReasoningEffort::High),
559 )
560 .unwrap();
561 let max = encode_messages_with_options(
562 messages.as_array().unwrap(),
563 ThinkingMode::Thinking,
564 true,
565 true,
566 Some(ReasoningEffort::Max),
567 )
568 .unwrap();
569 let low = encode_messages_with_options(
570 messages.as_array().unwrap(),
571 ThinkingMode::Thinking,
572 true,
573 true,
574 None,
575 )
576 .unwrap();
577
578 assert_eq!(
579 high,
580 concat!(
581 "<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum with no shortcuts permitted.\n",
582 "You 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.\n",
583 "Explicitly 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",
584 "hi<|User|>hello<|Assistant|><think>"
585 )
586 );
587 assert_eq!(
588 max,
589 concat!(
590 "<|begin▁of▁sentence|>Reasoning Effort: Beyond maximum — exhaustive, relentless, and uncompromising.\n",
591 "You 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.\n",
592 "Do 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",
593 "hi<|User|>hello<|Assistant|><think>"
594 )
595 );
596 assert_eq!(
597 low,
598 "<|begin▁of▁sentence|>hi<|User|>hello<|Assistant|><think>"
599 );
600 }
601
602 #[test]
603 fn test_content_blocks_with_tool_result() {
604 let messages = json!([
610 {"role": "user", "content": "call tool"},
611 {"role": "assistant", "content": "", "tool_calls": [{
612 "id": "c1", "type": "function",
613 "function": {"name": "f", "arguments": "{}"}
614 }]},
615 {"role": "tool", "tool_call_id": "c1", "content": "RESULT"},
616 {"role": "user", "content": "thanks"}
617 ]);
618 let out = encode_messages(messages.as_array().unwrap(), ThinkingMode::Chat, true).unwrap();
619 assert!(
620 out.contains("<tool_result>RESULT</tool_result>\n\nthanks"),
621 "expected tool_result block followed by 'thanks' in the merged user turn, got:\n{}",
622 out
623 );
624 }
625
626 #[test]
627 fn test_empty_tool_lists_and_merged_user_task_match_reference() {
628 let call = |id: &str| json!({"id": id, "type": "function", "function": {"name": "search", "arguments": "{}"}});
629 let invoke = "<|DSML|invoke name=\"search\">\n\n</|DSML|invoke>";
630 let calls = format!(
631 "<|Assistant|></think>\n\n<|DSML|tool_calls>\n{invoke}\n{invoke}\n</|DSML|tool_calls><|end▁of▁sentence|>"
632 );
633 for (messages, expected) in [
635 (
636 json!([{"role": "system", "content": "S", "tools": []}, {"role": "user", "content": "Hi"}]),
637 "<|begin▁of▁sentence|>S<|User|>Hi<|Assistant|></think>".to_owned(),
638 ),
639 (
640 json!([
641 {"role": "user", "content": "q"},
642 {"role": "assistant", "content": "", "tool_calls": [call("a"), call("b")]},
643 {"role": "assistant", "content": "x", "tool_calls": []},
644 {"role": "tool", "tool_call_id": "b", "content": "B"},
645 {"role": "tool", "tool_call_id": "a", "content": "A"}
646 ]),
647 format!(
648 "<|begin▁of▁sentence|><|User|>q{calls}x<|end▁of▁sentence|><|User|><tool_result>A</tool_result>\n\n<tool_result>B</tool_result><|Assistant|></think>"
649 ),
650 ),
651 (
652 json!([
653 {"role": "user", "content": "q"},
654 {"role": "assistant", "content": "", "tool_calls": [call("a"), call("b")]},
655 {"role": "tool", "tool_call_id": "b", "content": "B"},
656 {"role": "user", "content": "Search", "task": "action"},
657 {"role": "user", "content": "More"},
658 {"role": "tool", "tool_call_id": "a", "content": "A"},
659 {"role": "assistant", "content": "OK"}
660 ]),
661 format!(
662 "<|begin▁of▁sentence|><|User|>q{calls}<|User|><tool_result>A</tool_result>\n\nSearch\n\nMore\n\n<tool_result>B</tool_result><|Assistant|></think>OK<|end▁of▁sentence|>"
663 ),
664 ),
665 ] {
666 let out =
667 encode_messages(messages.as_array().unwrap(), ThinkingMode::Chat, true).unwrap();
668 assert_eq!(out, expected);
669 }
670 }
671
672 #[test]
673 fn test_drop_thinking_auto_disable_when_tools_present() {
674 let messages = json!([
675 {"role": "system", "content": "s", "tools": [{
676 "type": "function",
677 "function": {"name": "f", "description": "", "parameters": {"type": "object", "properties": {}}}
678 }]},
679 {"role": "user", "content": "hi"},
680 {"role": "assistant", "reasoning_content": "PRIOR_REASONING", "content": "reply"},
681 {"role": "user", "content": "again"}
682 ]);
683 let out =
684 encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true).unwrap();
685 assert!(out.contains("PRIOR_REASONING"));
687 }
688
689 #[test]
700 fn test_assistant_reasoning_preserved_when_no_user_in_history() {
701 let messages = json!([
702 {"role": "system", "content": "sys"},
703 {"role": "assistant", "content": "hello", "reasoning_content": "REASONING_BLOCK"}
704 ]);
705 let out =
706 encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true).unwrap();
707 assert_eq!(
708 out, "<|begin▁of▁sentence|>sysREASONING_BLOCK</think>hello<|end▁of▁sentence|>",
709 "Output must match Python reference byte-for-byte when no user/developer in history"
710 );
711 }
712
713 #[test]
721 fn test_to_json_preserves_spacing_past_escaped_backslash() {
722 let v = json!({"path": "\\", "count": 5});
723 let got = to_json(&v);
724 assert_eq!(
725 got, r#"{"path": "\\", "count": 5}"#,
726 "to_json must match Python's json.dumps formatting past an escaped backslash"
727 );
728 }
729
730 #[test]
731 fn test_resolve_drop_thinking_warns_on_malformed_value() {
732 use std::collections::HashMap;
733 let mut args = HashMap::new();
735 args.insert(
736 "drop_thinking".to_string(),
737 serde_json::Value::String("false".to_string()),
738 );
739 assert!(DeepSeekV4Formatter::resolve_drop_thinking(Some(&args)));
740 let malformed = serde_json::Value::String("HIGH".to_string());
742 assert_eq!(
743 DeepSeekV4Formatter::resolve_reasoning_effort(Some(&malformed)),
744 (false, Some(ReasoningEffort::High))
745 );
746 }
747
748 #[test]
749 fn test_resolve_thinking_mode_honors_enable_thinking() {
750 use std::collections::HashMap;
751 let mut args = HashMap::new();
752 args.insert(
753 "enable_thinking".to_string(),
754 serde_json::Value::Bool(false),
755 );
756 assert_eq!(
757 super::super::common::resolve_thinking_mode(Some(&args), ThinkingMode::Thinking),
758 ThinkingMode::Chat
759 );
760 args.insert("enable_thinking".to_string(), serde_json::Value::Bool(true));
761 assert_eq!(
762 super::super::common::resolve_thinking_mode(Some(&args), ThinkingMode::Thinking),
763 ThinkingMode::Thinking
764 );
765 }
766
767 struct MockRequest {
768 messages: JsonValue,
769 typed: Option<Vec<dynamo_protocols::types::ChatCompletionRequestMessage>>,
770 chat_template_args: Option<std::collections::HashMap<String, JsonValue>>,
771 reasoning_effort: Option<JsonValue>,
772 tools: Option<JsonValue>,
773 tool_choice: Option<JsonValue>,
774 response_format: Option<JsonValue>,
775 }
776
777 impl MockRequest {
778 fn new(messages: JsonValue) -> Self {
779 Self {
780 messages,
781 typed: None,
782 chat_template_args: None,
783 reasoning_effort: None,
784 tools: None,
785 tool_choice: None,
786 response_format: None,
787 }
788 }
789
790 fn with_chat_template_args(
791 mut self,
792 args: std::collections::HashMap<String, JsonValue>,
793 ) -> Self {
794 self.chat_template_args = Some(args);
795 self
796 }
797
798 fn with_reasoning_effort(mut self, reasoning_effort: JsonValue) -> Self {
799 self.reasoning_effort = Some(reasoning_effort);
800 self
801 }
802
803 fn with_tools(mut self, tools: JsonValue) -> Self {
804 self.tools = Some(tools);
805 self
806 }
807
808 fn with_tool_choice(mut self, tool_choice: JsonValue) -> Self {
809 self.tool_choice = Some(tool_choice);
810 self
811 }
812
813 fn with_response_format(mut self, response_format: JsonValue) -> Self {
814 self.response_format = Some(response_format);
815 self
816 }
817 }
818
819 impl crate::OAIChatLikeRequest for MockRequest {
820 fn model(&self) -> String {
821 "deepseek-v4".to_string()
822 }
823
824 fn messages(&self) -> minijinja::value::Value {
825 assert!(
826 self.typed.is_none(),
827 "typed requests must skip MiniJinja conversion"
828 );
829 minijinja::value::Value::from_serialize(&self.messages)
830 }
831
832 fn typed_messages(
833 &self,
834 ) -> Option<&[dynamo_protocols::types::ChatCompletionRequestMessage]> {
835 self.typed.as_deref()
836 }
837
838 fn should_add_generation_prompt(&self) -> bool {
839 true
840 }
841
842 fn chat_template_args(
843 &self,
844 ) -> Option<&std::collections::HashMap<String, serde_json::Value>> {
845 self.chat_template_args.as_ref()
846 }
847
848 fn reasoning_effort(&self) -> Option<minijinja::value::Value> {
849 self.reasoning_effort
850 .as_ref()
851 .map(minijinja::value::Value::from_serialize)
852 }
853
854 fn tools(&self) -> Option<minijinja::value::Value> {
855 self.tools
856 .as_ref()
857 .map(minijinja::value::Value::from_serialize)
858 }
859
860 fn tool_choice(&self) -> Option<minijinja::value::Value> {
861 self.tool_choice
862 .as_ref()
863 .map(minijinja::value::Value::from_serialize)
864 }
865
866 fn response_format(&self) -> Option<minijinja::value::Value> {
867 self.response_format
868 .as_ref()
869 .map(minijinja::value::Value::from_serialize)
870 }
871 }
872
873 #[test]
874 fn typed_messages_match_value_messages() {
875 use crate::OAIPromptFormatter;
876 let messages = json!([
877 {"role": "system", "content": "Use tools. 中文 🦀"},
878 {"role": "user", "content": [{"type": "text", "text": "weather?"}]},
879 {"role": "assistant", "content": null, "reasoning_content": "check",
880 "tool_calls": [{"id": "call_1", "type": "function",
881 "function": {"name": "weather", "arguments": "{\"city\":\"東京\"}"}}]},
882 {"role": "tool", "tool_call_id": "call_1", "content": "sunny"},
883 {"role": "user", "content": "explain"}
884 ]);
885 let mut typed = MockRequest::new(messages.clone());
886 typed.typed = Some(serde_json::from_value(messages.clone()).unwrap());
887 let value = MockRequest::new(messages);
888 for formatter in [
889 DeepSeekV4Formatter::new_thinking(),
890 DeepSeekV4Formatter::new_chat(),
891 ] {
892 assert_eq!(
893 formatter.render(&typed).unwrap(),
894 formatter.render(&value).unwrap()
895 );
896 }
897 }
898
899 fn weather_tool() -> JsonValue {
900 json!([{
901 "type": "function",
902 "function": {
903 "name": "get_current_weather",
904 "description": "Get the current weather in a given location",
905 "parameters": {
906 "type": "object",
907 "properties": {"location": {"type": "string"}},
908 "required": ["location"]
909 }
910 }
911 }])
912 }
913
914 #[test]
915 fn test_formatter_rejects_unsupported_partial_assistant() {
916 use crate::OAIPromptFormatter;
917
918 let request = MockRequest::new(json!([
919 {"role": "user", "content": "Continue"},
920 {"role": "assistant", "content": "prefix", "partial": true}
921 ]));
922 let error = DeepSeekV4Formatter::new_thinking()
923 .render(&request)
924 .unwrap_err();
925
926 assert!(matches!(
927 error.downcast_ref::<crate::PromptRenderError>(),
928 Some(crate::PromptRenderError::InvalidRequest(message))
929 if message.contains("`partial: true` is not supported")
930 ));
931 }
932
933 #[test]
934 fn test_formatter_rejects_system_tools_before_injection() {
935 use crate::OAIPromptFormatter;
936
937 let request = MockRequest::new(json!([
938 {"role": "system", "tools": [
939 {"type": "function", "function": {"name": "dynamic_tool"}}
940 ]},
941 {"role": "user", "content": "Use a tool"}
942 ]))
943 .with_tools(weather_tool());
944 let error = DeepSeekV4Formatter::new_thinking()
945 .render(&request)
946 .unwrap_err();
947
948 assert!(matches!(
949 error.downcast_ref::<crate::PromptRenderError>(),
950 Some(crate::PromptRenderError::InvalidRequest(message))
951 if message.contains("message-level `tools`") && message.contains("system")
952 ));
953 }
954
955 #[test]
956 fn test_formatter_preserves_developer_tools_with_top_level_tools() {
957 use crate::OAIPromptFormatter;
958
959 let request = MockRequest::new(json!([
960 {"role": "developer", "content": "Use a tool", "tools": [
961 {"type": "function", "function": {"name": "developer_tool"}}
962 ]}
963 ]))
964 .with_tools(weather_tool());
965 let rendered = DeepSeekV4Formatter::new_thinking()
966 .render(&request)
967 .unwrap();
968
969 assert!(rendered.contains("developer_tool"));
970 assert!(rendered.contains("get_current_weather"));
971 }
972
973 #[test]
974 fn test_render_tool_choice_none_strips_tools_keeps_response_format() {
975 use crate::OAIPromptFormatter;
976
977 let req = MockRequest::new(json!([
978 {"role": "system", "content": "sys"},
979 {"role": "user", "content": "weather in Boston?"}
980 ]))
981 .with_tools(weather_tool())
982 .with_tool_choice(json!("none"))
983 .with_response_format(json!({"type": "json_object"}));
984
985 let formatter = DeepSeekV4Formatter::new_chat();
986 let out = formatter.render(&req).unwrap();
987
988 assert!(
989 !out.contains("## Tools"),
990 "tool_choice=none must strip the tools block, got: {out}"
991 );
992 assert!(
993 !out.contains("get_current_weather"),
994 "tool schema leaked into prompt despite tool_choice=none: {out}"
995 );
996 assert!(
997 out.contains("## Response Format"),
998 "response_format must survive tool_choice=none: {out}"
999 );
1000 }
1001
1002 #[test]
1003 fn test_render_tool_choice_auto_keeps_tools() {
1004 use crate::OAIPromptFormatter;
1005
1006 let req = MockRequest::new(json!([
1007 {"role": "system", "content": "sys"},
1008 {"role": "user", "content": "weather in Boston?"}
1009 ]))
1010 .with_tools(weather_tool())
1011 .with_tool_choice(json!("auto"));
1012
1013 let formatter = DeepSeekV4Formatter::new_chat();
1014 let out = formatter.render(&req).unwrap();
1015
1016 assert!(out.contains("## Tools"));
1017 assert!(out.contains("get_current_weather"));
1018 }
1019
1020 #[test]
1021 fn test_render_absent_tool_choice_keeps_tools() {
1022 use crate::OAIPromptFormatter;
1023
1024 let req = MockRequest::new(json!([
1025 {"role": "system", "content": "sys"},
1026 {"role": "user", "content": "weather in Boston?"}
1027 ]))
1028 .with_tools(weather_tool());
1029
1030 let formatter = DeepSeekV4Formatter::new_chat();
1031 let out = formatter.render(&req).unwrap();
1032
1033 assert!(out.contains("## Tools"));
1034 assert!(out.contains("get_current_weather"));
1035 }
1036
1037 #[test]
1038 fn test_resolve_reasoning_effort_accepts_full_range() {
1039 let effort = |v: &str| {
1040 let value = json!(v);
1041 DeepSeekV4Formatter::resolve_reasoning_effort(Some(&value))
1042 };
1043
1044 assert_eq!(effort("max"), (false, Some(ReasoningEffort::Max)));
1045 assert_eq!(effort("xhigh"), (false, Some(ReasoningEffort::High)));
1046 assert_eq!(effort("high"), (false, Some(ReasoningEffort::High)));
1047 assert_eq!(effort("minimal"), (false, None));
1048 assert_eq!(effort("low"), (false, None));
1049 assert_eq!(effort("medium"), (false, Some(ReasoningEffort::High)));
1050 assert_eq!(effort("none"), (true, None));
1051 assert_eq!(effort("bogus"), (false, Some(ReasoningEffort::High)));
1052 assert_eq!(
1053 DeepSeekV4Formatter::resolve_reasoning_effort(None),
1054 (false, Some(ReasoningEffort::High))
1055 );
1056 }
1057
1058 #[test]
1059 fn test_render_leaves_null_assistant_tool_content_empty() {
1060 use crate::OAIPromptFormatter;
1061
1062 let req = MockRequest::new(json!([
1063 {"role": "user", "content": "call tool"},
1064 {"role": "assistant", "content": null, "tool_calls": [{
1065 "id": "c1", "type": "function",
1066 "function": {"name": "f", "arguments": "{}"}
1067 }]}
1068 ]));
1069
1070 let formatter = DeepSeekV4Formatter::new_chat();
1071 let out = formatter.render(&req).unwrap();
1072
1073 assert!(out.contains(&format!(
1074 "<{}{}>",
1075 tokens::DSML_TOKEN,
1076 TOOL_CALLS_BLOCK_NAME
1077 )));
1078 assert!(!out.contains("null"));
1079 }
1080
1081 #[test]
1082 fn test_render_wires_reasoning_effort_from_chat_template_args() {
1083 use crate::OAIPromptFormatter;
1084 use std::collections::HashMap;
1085
1086 for (effort, expected) in [
1087 ("high", REASONING_EFFORT_HIGH),
1088 ("max", REASONING_EFFORT_MAX),
1089 ] {
1090 let mut args = HashMap::new();
1091 args.insert("reasoning_effort".to_string(), json!(effort));
1092
1093 let req = MockRequest::new(json!([
1094 {"role": "system", "content": "sys"},
1095 {"role": "user", "content": "hi"}
1096 ]))
1097 .with_chat_template_args(args);
1098
1099 let formatter = DeepSeekV4Formatter::new_thinking();
1100 let out = formatter.render(&req).unwrap();
1101
1102 assert!(out.starts_with(tokens::BOS));
1103 assert!(
1104 out[tokens::BOS.len()..].starts_with(expected),
1105 "{effort} preamble should appear after BOS, got:\n{out}"
1106 );
1107 }
1108 }
1109
1110 #[test]
1111 fn test_render_wires_top_level_reasoning_effort_and_none_disables_thinking() {
1112 use crate::OAIPromptFormatter;
1113
1114 let formatter = DeepSeekV4Formatter::new_thinking();
1115 for (effort, expected_prefix) in [
1116 ("high", "Reasoning Effort: Absolute maximum"),
1117 ("max", "Reasoning Effort: Beyond maximum"),
1118 ] {
1119 let req: dynamo_protocols::types::CreateChatCompletionRequest =
1120 serde_json::from_value(json!({
1121 "model": "deepseek-v4",
1122 "messages": [{"role": "user", "content": "hi"}],
1123 "reasoning_effort": effort
1124 }))
1125 .unwrap();
1126 let out = formatter.render(&req).unwrap();
1127
1128 assert!(
1129 out[tokens::BOS.len()..].starts_with(expected_prefix),
1130 "top-level {effort} did not select its prefix: {out}"
1131 );
1132 assert!(out.ends_with(tokens::THINKING_START));
1133 }
1134
1135 let req: dynamo_protocols::types::CreateChatCompletionRequest =
1136 serde_json::from_value(json!({
1137 "model": "deepseek-v4",
1138 "messages": [{"role": "user", "content": "hi"}],
1139 "reasoning_effort": "none"
1140 }))
1141 .unwrap();
1142 let out = formatter.render(&req).unwrap();
1143
1144 assert_eq!(
1145 out,
1146 "<|begin▁of▁sentence|><|User|>hi<|Assistant|></think>"
1147 );
1148 }
1149
1150 #[test]
1151 fn test_top_level_reasoning_effort_precedes_template_argument() {
1152 use crate::OAIPromptFormatter;
1153 use std::collections::HashMap;
1154
1155 let mut args = HashMap::new();
1156 args.insert("reasoning_effort".to_string(), json!("max"));
1157 let req = MockRequest::new(json!([{"role": "user", "content": "hi"}]))
1158 .with_chat_template_args(args)
1159 .with_reasoning_effort(json!("low"));
1160
1161 let out = DeepSeekV4Formatter::new_thinking().render(&req).unwrap();
1162
1163 assert_eq!(
1164 out,
1165 "<|begin▁of▁sentence|><|User|>hi<|Assistant|><think>"
1166 );
1167 }
1168
1169 #[test]
1170 fn test_render_drop_thinking_override_from_chat_template_args() {
1171 use crate::OAIPromptFormatter;
1172 use std::collections::HashMap;
1173
1174 let messages = json!([
1175 {"role": "user", "content": "first"},
1176 {"role": "assistant", "reasoning_content": "PRIOR", "content": "reply"},
1177 {"role": "user", "content": "again"}
1178 ]);
1179
1180 let req_default = MockRequest::new(messages.clone());
1182 let formatter = DeepSeekV4Formatter::new_thinking();
1183 let out_default = formatter.render(&req_default).unwrap();
1184 assert!(
1185 !out_default.contains("PRIOR"),
1186 "default drop_thinking=true should strip prior reasoning, got:\n{}",
1187 out_default
1188 );
1189
1190 let mut args = HashMap::new();
1192 args.insert("drop_thinking".to_string(), json!(false));
1193 let req_keep = MockRequest::new(messages).with_chat_template_args(args);
1194 let out_keep = formatter.render(&req_keep).unwrap();
1195 assert!(
1196 out_keep.contains("PRIOR"),
1197 "drop_thinking=false override should preserve prior reasoning, got:\n{}",
1198 out_keep
1199 );
1200 }
1201
1202 #[test]
1208 fn test_developer_only_conversation_renders_developer_content() {
1209 let messages = json!([
1210 {"role": "system", "content": "sys"},
1211 {"role": "developer", "content": "x"},
1212 {"role": "assistant", "reasoning_content": "R", "content": "ok"}
1213 ]);
1214 let out =
1215 encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true).unwrap();
1216 assert!(
1217 out.contains("x"),
1218 "developer content should appear in output, got:\n{}",
1219 out
1220 );
1221 }
1222
1223 #[test]
1224 fn test_developer_as_last_user_index_controls_reasoning_cutoff() {
1225 let messages = json!([
1230 {"role": "user", "content": "a"},
1231 {"role": "assistant", "reasoning_content": "FIRST", "content": "r1"},
1232 {"role": "developer", "content": "y"},
1233 {"role": "assistant", "reasoning_content": "SECOND", "content": "r2"}
1234 ]);
1235 let out =
1236 encode_messages(messages.as_array().unwrap(), ThinkingMode::Thinking, true).unwrap();
1237 assert!(
1238 !out.contains("FIRST"),
1239 "reasoning before last user/developer (idx 1 < 2) should be stripped, got:\n{}",
1240 out
1241 );
1242 assert!(
1243 out.contains("SECOND"),
1244 "reasoning at/after last user/developer (idx 3 > 2) should survive, got:\n{}",
1245 out
1246 );
1247 }
1248}