Skip to main content

objectiveai_sdk/agent/openrouter/
agent.rs

1//! OpenRouter Agent types and validation logic.
2
3use indexmap::IndexMap;
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6use twox_hash::XxHash3_128;
7
8/// The base configuration for an OpenRouter Agent (without computed ID).
9#[derive(
10    Clone,
11    Debug,
12    Default,
13    PartialEq,
14    Serialize,
15    Deserialize,
16    JsonSchema,
17    arbitrary::Arbitrary,
18)]
19#[schemars(rename = "agent.openrouter.AgentBase")]
20pub struct AgentBase {
21    /// The upstream provider marker.
22    pub upstream: super::Upstream,
23
24    /// The upstream language model identifier (e.g., `"gpt-4"`, `"claude-3-opus"`).
25    pub model: String,
26
27    /// The output mode for vector completions. Ignored for agent completions.
28    #[serde(default)]
29    pub output_mode: super::OutputMode,
30
31    /// Enable synthetic reasoning for non-reasoning LLMs.
32    ///
33    /// **Vector completions only.** Ignored for agent completions.
34    #[serde(skip_serializing_if = "Option::is_none")]
35    #[schemars(extend("omitempty" = true))]
36    pub synthetic_reasoning: Option<bool>,
37
38    /// Number of top log probabilities to return (2-20).
39    ///
40    /// **Vector completions only.** Ignored for agent completions.
41    #[serde(skip_serializing_if = "Option::is_none")]
42    #[schemars(extend("omitempty" = true))]
43    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_u64)]
44    pub top_logprobs: Option<u64>,
45
46    /// The agent's system prompt: a single role+content entry, rendered as the
47    /// leading message of every request.
48    #[serde(skip_serializing_if = "Option::is_none")]
49    #[schemars(extend("omitempty" = true))]
50    pub system_prompt: Option<super::system_prompt::SystemPrompt>,
51
52    /// Messages prepended to the user's prompt.
53    #[serde(skip_serializing_if = "Option::is_none")]
54    #[schemars(extend("omitempty" = true))]
55    pub prefix_messages:
56        Option<Vec<super::super::completions::message::Message>>,
57
58    /// Messages appended after the user's prompt.
59    #[serde(skip_serializing_if = "Option::is_none")]
60    #[schemars(extend("omitempty" = true))]
61    pub suffix_messages:
62        Option<Vec<super::super::completions::message::Message>>,
63
64    /// MCP servers the agent can connect to.
65    #[serde(skip_serializing_if = "Option::is_none")]
66    #[schemars(extend("omitempty" = true))]
67    pub mcp_servers: Option<super::super::McpServers>,
68
69    /// Client-side ObjectiveAI MCP surface the calling client is
70    /// expected to expose locally back to the API (objectiveai
71    /// built-in, plus specific plugins / tools by owner+name+version).
72    #[serde(skip_serializing_if = "Option::is_none")]
73    #[schemars(extend("omitempty" = true))]
74    pub client_objectiveai_mcp: Option<super::super::ClientObjectiveaiMcp>,
75
76    // --- OpenAI-compatible parameters ---
77    /// Penalizes tokens based on their frequency in the output so far (-2.0 to 2.0).
78    #[serde(skip_serializing_if = "Option::is_none")]
79    #[schemars(extend("omitempty" = true))]
80    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_f64)]
81    pub frequency_penalty: Option<f64>,
82    /// Token ID to bias mapping (-100 to 100). Positive values increase likelihood.
83    #[serde(skip_serializing_if = "Option::is_none")]
84    #[schemars(extend("omitempty" = true))]
85    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_indexmap_string_i64)]
86    pub logit_bias: Option<IndexMap<String, i64>>,
87    /// Maximum tokens in the completion.
88    #[serde(skip_serializing_if = "Option::is_none")]
89    #[schemars(extend("omitempty" = true))]
90    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_u64)]
91    pub max_completion_tokens: Option<u64>,
92    /// Penalizes tokens based on their presence in the output so far (-2.0 to 2.0).
93    #[serde(skip_serializing_if = "Option::is_none")]
94    #[schemars(extend("omitempty" = true))]
95    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_f64)]
96    pub presence_penalty: Option<f64>,
97    /// Stop sequences that halt generation.
98    #[serde(skip_serializing_if = "Option::is_none")]
99    #[schemars(extend("omitempty" = true))]
100    pub stop: Option<super::Stop>,
101    /// Sampling temperature (0.0 to 2.0). Higher = more random.
102    #[serde(skip_serializing_if = "Option::is_none")]
103    #[schemars(extend("omitempty" = true))]
104    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_f64)]
105    pub temperature: Option<f64>,
106    /// Nucleus sampling probability (0.0 to 1.0).
107    #[serde(skip_serializing_if = "Option::is_none")]
108    #[schemars(extend("omitempty" = true))]
109    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_f64)]
110    pub top_p: Option<f64>,
111
112    // --- OpenRouter-specific parameters ---
113    /// Maximum tokens (OpenRouter variant of max_completion_tokens).
114    #[serde(skip_serializing_if = "Option::is_none")]
115    #[schemars(extend("omitempty" = true))]
116    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_u64)]
117    pub max_tokens: Option<u64>,
118    /// Minimum probability threshold for sampling (0.0 to 1.0).
119    #[serde(skip_serializing_if = "Option::is_none")]
120    #[schemars(extend("omitempty" = true))]
121    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_f64)]
122    pub min_p: Option<f64>,
123    /// Provider routing preferences.
124    #[serde(skip_serializing_if = "Option::is_none")]
125    #[schemars(extend("omitempty" = true))]
126    pub provider: Option<super::Provider>,
127    /// Reasoning/thinking configuration for supported models.
128    #[serde(skip_serializing_if = "Option::is_none")]
129    #[schemars(extend("omitempty" = true))]
130    pub reasoning: Option<super::Reasoning>,
131    /// Repetition penalty (0.0 to 2.0). Values > 1.0 penalize repetition.
132    #[serde(skip_serializing_if = "Option::is_none")]
133    #[schemars(extend("omitempty" = true))]
134    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_f64)]
135    pub repetition_penalty: Option<f64>,
136    /// Top-a sampling parameter (0.0 to 1.0).
137    #[serde(skip_serializing_if = "Option::is_none")]
138    #[schemars(extend("omitempty" = true))]
139    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_f64)]
140    pub top_a: Option<f64>,
141    /// Top-k sampling: only consider the k most likely tokens.
142    #[serde(skip_serializing_if = "Option::is_none")]
143    #[schemars(extend("omitempty" = true))]
144    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_u64)]
145    pub top_k: Option<u64>,
146    /// Output verbosity hint for supported models.
147    #[serde(skip_serializing_if = "Option::is_none")]
148    #[schemars(extend("omitempty" = true))]
149    pub verbosity: Option<super::Verbosity>,
150    /// Context compression engine for long contexts. When set, the
151    /// upstream client emits the matching `plugins` entry on the
152    /// outgoing OpenRouter chat-completions request.
153    #[serde(skip_serializing_if = "Option::is_none")]
154    #[schemars(extend("omitempty" = true))]
155    pub context_compression: Option<super::ContextCompression>,
156}
157
158impl AgentBase {
159    /// Normalizes the configuration for deterministic ID computation.
160    pub fn prepare(&mut self) {
161        self.synthetic_reasoning = match self.synthetic_reasoning {
162            Some(false) => None,
163            other => other,
164        };
165        self.top_logprobs = match self.top_logprobs {
166            Some(0) | Some(1) => None,
167            other => other,
168        };
169        self.prefix_messages = match self.prefix_messages.take() {
170            Some(prefix_messages) if prefix_messages.is_empty() => None,
171            Some(mut prefix_messages) => {
172                super::super::completions::message::prompt::prepare(
173                    &mut prefix_messages,
174                );
175                if prefix_messages.is_empty() {
176                    None
177                } else {
178                    Some(prefix_messages)
179                }
180            }
181            None => None,
182        };
183        self.suffix_messages = match self.suffix_messages.take() {
184            Some(suffix_messages) if suffix_messages.is_empty() => None,
185            Some(mut suffix_messages) => {
186                super::super::completions::message::prompt::prepare(
187                    &mut suffix_messages,
188                );
189                if suffix_messages.is_empty() {
190                    None
191                } else {
192                    Some(suffix_messages)
193                }
194            }
195            None => None,
196        };
197        self.mcp_servers = match self.mcp_servers.take() {
198            Some(mcp_servers) => {
199                super::super::mcp::mcp_servers::prepare(mcp_servers)
200            }
201            None => None,
202        };
203        self.client_objectiveai_mcp = match self.client_objectiveai_mcp.take() {
204            Some(cm) => super::super::client_objectiveai_mcp::prepare(cm),
205            None => None,
206        };
207        self.frequency_penalty = match self.frequency_penalty {
208            Some(frequency_penalty) if frequency_penalty == 0.0 => None,
209            other => other,
210        };
211        self.logit_bias = match self.logit_bias.take() {
212            Some(logit_bias) if logit_bias.is_empty() => None,
213            Some(mut logit_bias) => {
214                logit_bias.retain(|_, &mut weight| weight != 0);
215                logit_bias.sort_unstable_keys();
216                Some(logit_bias)
217            }
218            None => None,
219        };
220        self.max_completion_tokens = match self.max_completion_tokens {
221            Some(0) => None,
222            other => other,
223        };
224        self.presence_penalty = match self.presence_penalty {
225            Some(presence_penalty) if presence_penalty == 0.0 => None,
226            other => other,
227        };
228        self.stop = match self.stop.take() {
229            Some(stop) => stop.prepare(),
230            None => None,
231        };
232        self.temperature = match self.temperature {
233            Some(temperature) if temperature == 1.0 => None,
234            other => other,
235        };
236        self.top_p = match self.top_p {
237            Some(top_p) if top_p == 1.0 => None,
238            other => other,
239        };
240        self.max_tokens = match self.max_tokens {
241            Some(0) => None,
242            other => other,
243        };
244        self.min_p = match self.min_p {
245            Some(min_p) if min_p == 0.0 => None,
246            other => other,
247        };
248        self.provider = match self.provider.take() {
249            Some(provider) => provider.prepare(),
250            None => None,
251        };
252        self.reasoning = match self.reasoning.take() {
253            Some(reasoning) => reasoning.prepare(),
254            None => None,
255        };
256        self.repetition_penalty = match self.repetition_penalty {
257            Some(repetition_penalty) if repetition_penalty == 1.0 => None,
258            other => other,
259        };
260        self.top_a = match self.top_a {
261            Some(top_a) if top_a == 0.0 => None,
262            other => other,
263        };
264        self.top_k = match self.top_k {
265            Some(0) => None,
266            other => other,
267        };
268        self.verbosity = match self.verbosity.take() {
269            Some(verbosity) => verbosity.prepare(),
270            None => None,
271        };
272        self.context_compression = match self.context_compression.take() {
273            Some(cc) => cc.prepare(),
274            None => None,
275        };
276    }
277
278    /// Validates the configuration.
279    pub fn validate(&self) -> Result<(), String> {
280        fn validate_f64(
281            name: &str,
282            value: Option<f64>,
283            min: f64,
284            max: f64,
285        ) -> Result<(), String> {
286            if let Some(v) = value {
287                if !v.is_finite() {
288                    return Err(format!("`{}` must be a finite number", name));
289                }
290                if v < min || v > max {
291                    return Err(format!(
292                        "`{}` must be between {} and {}",
293                        name, min, max
294                    ));
295                }
296            }
297            Ok(())
298        }
299        fn validate_u64(
300            name: &str,
301            value: Option<u64>,
302            min: u64,
303            max: u64,
304        ) -> Result<(), String> {
305            if let Some(v) = value {
306                if v < min || v > max {
307                    return Err(format!(
308                        "`{}` must be between {} and {}",
309                        name, min, max
310                    ));
311                }
312            }
313            Ok(())
314        }
315        if self.model.is_empty() {
316            return Err("`model` string cannot be empty".to_string());
317        }
318        if self.synthetic_reasoning.is_some()
319            && let super::OutputMode::Instruction = self.output_mode
320        {
321            return Err(
322                "`synthetic_reasoning` cannot be true when `output_mode` is \"instruction\""
323                    .to_string(),
324            );
325        }
326        if let Some(top_logprobs) = self.top_logprobs
327            && top_logprobs > 20
328        {
329            return Err("`top_logprobs` must be at most 20".to_string());
330        }
331        if let Some(mcp_servers) = &self.mcp_servers {
332            super::super::mcp::mcp_servers::validate(mcp_servers)?;
333        }
334        if let Some(cm) = &self.client_objectiveai_mcp {
335            super::super::client_objectiveai_mcp::validate(cm)?;
336        }
337        validate_f64("frequency_penalty", self.frequency_penalty, -2.0, 2.0)?;
338        if let Some(logit_bias) = &self.logit_bias {
339            for (token, weight) in logit_bias {
340                if token.is_empty() {
341                    return Err("`logit_bias` keys cannot be empty".to_string());
342                } else if !token.chars().all(|c| c.is_ascii_digit()) {
343                    return Err(
344                        "`logit_bias` keys must be stringified token IDs"
345                            .to_string(),
346                    );
347                } else if token.chars().next().unwrap() == '0'
348                    && token.len() > 1
349                {
350                    return Err("`logit_bias` keys cannot have leading zeros"
351                        .to_string());
352                } else if *weight < -100 || *weight > 100 {
353                    return Err(
354                        "`logit_bias` values must be between -100 and 100"
355                            .to_string(),
356                    );
357                }
358            }
359        }
360        validate_u64(
361            "max_completion_tokens",
362            self.max_completion_tokens,
363            0,
364            i32::MAX as u64,
365        )?;
366        validate_f64("presence_penalty", self.presence_penalty, -2.0, 2.0)?;
367        if let Some(stop) = &self.stop {
368            stop.validate()?;
369        }
370        validate_f64("temperature", self.temperature, 0.0, 2.0)?;
371        validate_f64("top_p", self.top_p, 0.0, 1.0)?;
372        validate_u64("max_tokens", self.max_tokens, 0, i32::MAX as u64)?;
373        validate_f64("min_p", self.min_p, 0.0, 1.0)?;
374        if let Some(provider) = &self.provider {
375            provider.validate()?;
376        }
377        if let Some(reasoning) = &self.reasoning {
378            reasoning.validate()?;
379        }
380        validate_f64("repetition_penalty", self.repetition_penalty, 0.0, 2.0)?;
381        validate_f64("top_a", self.top_a, 0.0, 1.0)?;
382        validate_u64("top_k", self.top_k, 0, i32::MAX as u64)?;
383        if let Some(verbosity) = &self.verbosity {
384            verbosity.validate()?;
385        }
386        if let Some(cc) = &self.context_compression {
387            cc.validate()?;
388        }
389        if let Some(system_prompt) = &self.system_prompt {
390            system_prompt.validate()?;
391        }
392        Ok(())
393    }
394
395    /// Returns prefix messages, then the provided messages, then suffix messages.
396    pub fn merged_messages(
397        &self,
398        messages: Vec<super::super::completions::message::Message>,
399    ) -> Vec<super::super::completions::message::Message> {
400        let prefix_len = self.prefix_messages.as_ref().map_or(0, |m| m.len());
401        let suffix_len = self.suffix_messages.as_ref().map_or(0, |m| m.len());
402        let mut merged =
403            Vec::with_capacity(prefix_len + messages.len() + suffix_len);
404        if let Some(prefix) = &self.prefix_messages {
405            merged.extend(prefix.iter().cloned());
406        }
407        merged.extend(messages);
408        if let Some(suffix) = &self.suffix_messages {
409            merged.extend(suffix.iter().cloned());
410        }
411        merged
412    }
413
414    /// Computes the deterministic content-addressed ID.
415    pub fn id(&self) -> String {
416        let mut hasher = XxHash3_128::with_seed(0);
417        hasher.write(serde_json::to_string(self).unwrap().as_bytes());
418        format!("{:0>22}", base62::encode(hasher.finish_128()))
419    }
420}
421
422/// A validated OpenRouter Agent with its computed content-addressed ID.
423#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
424#[schemars(rename = "agent.openrouter.Agent")]
425pub struct Agent {
426    /// The deterministic content-addressed ID (22-character base62 string).
427    pub id: String,
428    /// The normalized configuration.
429    #[serde(flatten)]
430    pub base: AgentBase,
431}
432
433impl TryFrom<AgentBase> for Agent {
434    type Error = String;
435    fn try_from(mut base: AgentBase) -> Result<Self, Self::Error> {
436        base.prepare();
437        base.validate()?;
438        let id = base.id();
439        Ok(Agent { id, base })
440    }
441}