1use super::{
4 Backend, ModelId,
5 location::Location,
6 validation::{self, positive_integer, present, script, text, timeout},
7};
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(rename_all = "lowercase")]
13pub enum Reasoning {
14 None,
15 Minimal,
16 Low,
17 Medium,
18 High,
19 Xhigh,
20 Max,
21}
22impl Reasoning {
23 pub fn as_str(self) -> &'static str {
24 match self {
25 Self::None => "none",
26 Self::Minimal => "minimal",
27 Self::Low => "low",
28 Self::Medium => "medium",
29 Self::High => "high",
30 Self::Xhigh => "xhigh",
31 Self::Max => "max",
32 }
33 }
34}
35impl std::fmt::Display for Reasoning {
36 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 formatter.write_str(self.as_str())
38 }
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(tag = "kind", rename_all = "lowercase", deny_unknown_fields)]
43pub enum Profile {
44 Agent {
45 backend: Backend,
46 model: ModelId,
47 #[serde(default, deserialize_with = "present")]
48 reasoning: Option<Reasoning>,
49 #[serde(
50 rename(serialize = "timeoutMs", deserialize = "timeout_ms"),
51 default,
52 deserialize_with = "timeout",
53 serialize_with = "validation::milliseconds::serialize"
54 )]
55 timeout_ms: Option<std::time::Duration>,
56 #[serde(
57 rename(serialize = "maxToolCalls", deserialize = "max_tool_calls"),
58 default,
59 deserialize_with = "positive_integer"
60 )]
61 max_tool_calls: Option<u64>,
62 #[serde(
63 rename(serialize = "maxTokens", deserialize = "max_tokens"),
64 default,
65 deserialize_with = "positive_integer"
66 )]
67 max_tokens: Option<u64>,
68 },
69 Human {},
70 Runtime {
71 #[serde(serialize_with = "crate::platform::serialize_program")]
72 command: String,
73 args: Vec<crate::scope::Argument>,
74 #[serde(
75 rename(serialize = "timeoutMs", deserialize = "timeout_ms"),
76 default,
77 deserialize_with = "timeout",
78 serialize_with = "validation::milliseconds::serialize"
79 )]
80 timeout_ms: Option<std::time::Duration>,
81 },
82}
83
84#[derive(Debug, Clone, Default, Deserialize)]
86#[serde(rename_all = "snake_case", deny_unknown_fields)]
87pub(super) struct AgentProfileOverrides {
88 #[serde(default, deserialize_with = "present")]
89 pub backend: Option<Backend>,
90 #[serde(default, deserialize_with = "present")]
91 pub model: Option<ModelId>,
92 #[serde(default, deserialize_with = "present")]
93 pub reasoning: Option<Reasoning>,
94 #[serde(default, deserialize_with = "timeout")]
95 pub timeout_ms: Option<std::time::Duration>,
96 #[serde(default, deserialize_with = "positive_integer")]
97 pub max_tool_calls: Option<u64>,
98 #[serde(default, deserialize_with = "positive_integer")]
99 pub max_tokens: Option<u64>,
100}
101
102impl Profile {
103 pub(super) fn apply_overrides(&self, overrides: AgentProfileOverrides) -> Result<Self, String> {
104 let Self::Agent {
105 backend,
106 model,
107 reasoning,
108 timeout_ms,
109 max_tool_calls,
110 max_tokens,
111 } = self
112 else {
113 return Err("Profile aliases support only agent profiles (kind = \"agent\").".into());
114 };
115 Ok(Self::Agent {
116 backend: overrides.backend.unwrap_or_else(|| backend.clone()),
117 model: overrides.model.unwrap_or_else(|| model.clone()),
118 reasoning: overrides.reasoning.or(*reasoning),
119 timeout_ms: overrides.timeout_ms.or(*timeout_ms),
120 max_tool_calls: overrides.max_tool_calls.or(*max_tool_calls),
121 max_tokens: overrides.max_tokens.or(*max_tokens),
122 })
123 }
124
125 pub(super) fn validate(&self, location: &Location<'_, '_>) -> Result<(), String> {
126 match self {
127 Self::Agent {
128 backend,
129 model,
130 reasoning,
131 ..
132 } => {
133 location
134 .child("model")
135 .check(text(model, "Agent profile model"))?;
136 if let Some(provider) = backend.builtin() {
137 location.child("backend").check(provider.validate_eval())?;
138 }
139 if let (Some(reasoning), Some(provider)) = (reasoning, backend.builtin()) {
142 location.child("reasoning").check(
143 crate::llm::Api::from(provider).validate_reasoning(reasoning.as_str()),
144 )?;
145 }
146 Ok(())
147 }
148 Self::Human {} => Ok(()),
149 Self::Runtime { command, args, .. } => location.check(script(command, args)),
150 }
151 }
152
153 pub fn kind(&self) -> ProfileKind {
154 match self {
155 Self::Agent { .. } => ProfileKind::Agent,
156 Self::Human {} => ProfileKind::Human,
157 Self::Runtime { .. } => ProfileKind::Runtime,
158 }
159 }
160}
161
162#[derive(
164 Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, clap::ValueEnum,
165)]
166#[serde(rename_all = "lowercase")]
167pub enum ProfileKind {
168 Runtime,
170 Agent,
172 Human,
174}
175
176impl ProfileKind {
177 pub fn name(self) -> &'static str {
178 match self {
179 Self::Runtime => "runtime",
180 Self::Agent => "agent",
181 Self::Human => "human",
182 }
183 }
184}