Skip to main content

mojentic/llm/
reasoning.rs

1//! Explicit model capabilities, separate from provider wire conventions.
2use crate::error::{MojenticError, Result};
3use crate::llm::gateway::ReasoningEffort;
4use serde::{Deserialize, Serialize};
5
6/// A caller-selected model contract; never inferred from a model-name substring.
7/// Provider defaults remain unknown when no effort is requested.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct ReasoningProfile {
10    /// Stable label for provenance and diagnostics.
11    pub name: String,
12    /// Exact accepted controls; high and xhigh are not aliases.
13    pub supported_efforts: Vec<ReasoningEffort>,
14    /// Whether the model consumes native assistant reasoning in history.
15    pub supports_reasoning_history: bool,
16}
17
18impl ReasoningProfile {
19    /// Qwen3.8 thinking controls and native reasoning-history support.
20    /// No default effort is imposed: set one explicitly for comparable runs.
21    pub fn qwen3_8() -> Self {
22        Self {
23            name: "qwen3.8".into(),
24            supported_efforts: vec![
25                ReasoningEffort::Disabled,
26                ReasoningEffort::Low,
27                ReasoningEffort::Medium,
28                ReasoningEffort::XHigh,
29            ],
30            supports_reasoning_history: true,
31        }
32    }
33
34    /// Reject unsupported controls before HTTP instead of silently translating them.
35    pub fn validate(&self, effort: Option<ReasoningEffort>, preserve_history: bool) -> Result<()> {
36        if let Some(effort) = effort {
37            if !self.supported_efforts.contains(&effort) {
38                return Err(MojenticError::ConfigError(format!(
39                    "reasoning effort {effort:?} is unsupported by profile {}; supported: {:?}",
40                    self.name, self.supported_efforts
41                )));
42            }
43        }
44        if preserve_history && !self.supports_reasoning_history {
45            return Err(MojenticError::ConfigError(format!(
46                "profile {} does not support native reasoning history",
47                self.name
48            )));
49        }
50        Ok(())
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57    #[test]
58    fn qwen_accepts_exact_controls_without_inventing_a_default() {
59        let profile = ReasoningProfile::qwen3_8();
60        for effort in [
61            None,
62            Some(ReasoningEffort::Disabled),
63            Some(ReasoningEffort::Low),
64            Some(ReasoningEffort::Medium),
65            Some(ReasoningEffort::XHigh),
66        ] {
67            assert!(profile.validate(effort, true).is_ok());
68        }
69        assert!(profile.validate(Some(ReasoningEffort::High), false).is_err());
70        assert!(profile.validate(Some(ReasoningEffort::Enabled), false).is_err());
71    }
72    #[test]
73    fn boolean_model_profile_rejects_named_effort_and_native_history() {
74        let profile = ReasoningProfile {
75            name: "boolean-only".into(),
76            supported_efforts: vec![ReasoningEffort::Disabled, ReasoningEffort::Enabled],
77            supports_reasoning_history: false,
78        };
79        assert!(profile.validate(Some(ReasoningEffort::Enabled), false).is_ok());
80        assert!(profile.validate(Some(ReasoningEffort::Low), false).is_err());
81        assert!(profile.validate(None, true).is_err());
82    }
83}