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