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    /// Messages prepended to the user's prompt.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    #[schemars(extend("omitempty" = true))]
49    pub prefix_messages:
50        Option<Vec<super::super::completions::message::Message>>,
51
52    /// Messages inserted after the leading chain of system/developer messages.
53    #[serde(skip_serializing_if = "Option::is_none")]
54    #[schemars(extend("omitempty" = true))]
55    pub post_system_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}
151
152impl AgentBase {
153    /// Normalizes the configuration for deterministic ID computation.
154    pub fn prepare(&mut self) {
155        self.synthetic_reasoning = match self.synthetic_reasoning {
156            Some(false) => None,
157            other => other,
158        };
159        self.top_logprobs = match self.top_logprobs {
160            Some(0) | Some(1) => None,
161            other => other,
162        };
163        self.prefix_messages = match self.prefix_messages.take() {
164            Some(prefix_messages) if prefix_messages.is_empty() => None,
165            Some(mut prefix_messages) => {
166                super::super::completions::message::prompt::prepare(
167                    &mut prefix_messages,
168                );
169                if prefix_messages.is_empty() {
170                    None
171                } else {
172                    Some(prefix_messages)
173                }
174            }
175            None => None,
176        };
177        self.post_system_prefix_messages = match self
178            .post_system_prefix_messages
179            .take()
180        {
181            Some(msgs) if msgs.is_empty() => None,
182            Some(mut msgs) => {
183                super::super::completions::message::prompt::prepare(&mut msgs);
184                if msgs.is_empty() { None } else { Some(msgs) }
185            }
186            None => None,
187        };
188        self.suffix_messages = match self.suffix_messages.take() {
189            Some(suffix_messages) if suffix_messages.is_empty() => None,
190            Some(mut suffix_messages) => {
191                super::super::completions::message::prompt::prepare(
192                    &mut suffix_messages,
193                );
194                if suffix_messages.is_empty() {
195                    None
196                } else {
197                    Some(suffix_messages)
198                }
199            }
200            None => None,
201        };
202        self.mcp_servers = match self.mcp_servers.take() {
203            Some(mcp_servers) => {
204                super::super::mcp::mcp_servers::prepare(mcp_servers)
205            }
206            None => None,
207        };
208        self.client_objectiveai_mcp = match self.client_objectiveai_mcp.take() {
209            Some(cm) => super::super::client_objectiveai_mcp::prepare(cm),
210            None => None,
211        };
212        self.frequency_penalty = match self.frequency_penalty {
213            Some(frequency_penalty) if frequency_penalty == 0.0 => None,
214            other => other,
215        };
216        self.logit_bias = match self.logit_bias.take() {
217            Some(logit_bias) if logit_bias.is_empty() => None,
218            Some(mut logit_bias) => {
219                logit_bias.retain(|_, &mut weight| weight != 0);
220                logit_bias.sort_unstable_keys();
221                Some(logit_bias)
222            }
223            None => None,
224        };
225        self.max_completion_tokens = match self.max_completion_tokens {
226            Some(0) => None,
227            other => other,
228        };
229        self.presence_penalty = match self.presence_penalty {
230            Some(presence_penalty) if presence_penalty == 0.0 => None,
231            other => other,
232        };
233        self.stop = match self.stop.take() {
234            Some(stop) => stop.prepare(),
235            None => None,
236        };
237        self.temperature = match self.temperature {
238            Some(temperature) if temperature == 1.0 => None,
239            other => other,
240        };
241        self.top_p = match self.top_p {
242            Some(top_p) if top_p == 1.0 => None,
243            other => other,
244        };
245        self.max_tokens = match self.max_tokens {
246            Some(0) => None,
247            other => other,
248        };
249        self.min_p = match self.min_p {
250            Some(min_p) if min_p == 0.0 => None,
251            other => other,
252        };
253        self.provider = match self.provider.take() {
254            Some(provider) => provider.prepare(),
255            None => None,
256        };
257        self.reasoning = match self.reasoning.take() {
258            Some(reasoning) => reasoning.prepare(),
259            None => None,
260        };
261        self.repetition_penalty = match self.repetition_penalty {
262            Some(repetition_penalty) if repetition_penalty == 1.0 => None,
263            other => other,
264        };
265        self.top_a = match self.top_a {
266            Some(top_a) if top_a == 0.0 => None,
267            other => other,
268        };
269        self.top_k = match self.top_k {
270            Some(0) => None,
271            other => other,
272        };
273        self.verbosity = match self.verbosity.take() {
274            Some(verbosity) => verbosity.prepare(),
275            None => None,
276        };
277    }
278
279    /// Validates the configuration.
280    pub fn validate(&self) -> Result<(), String> {
281        fn validate_f64(
282            name: &str,
283            value: Option<f64>,
284            min: f64,
285            max: f64,
286        ) -> Result<(), String> {
287            if let Some(v) = value {
288                if !v.is_finite() {
289                    return Err(format!("`{}` must be a finite number", name));
290                }
291                if v < min || v > max {
292                    return Err(format!(
293                        "`{}` must be between {} and {}",
294                        name, min, max
295                    ));
296                }
297            }
298            Ok(())
299        }
300        fn validate_u64(
301            name: &str,
302            value: Option<u64>,
303            min: u64,
304            max: u64,
305        ) -> Result<(), String> {
306            if let Some(v) = value {
307                if v < min || v > max {
308                    return Err(format!(
309                        "`{}` must be between {} and {}",
310                        name, min, max
311                    ));
312                }
313            }
314            Ok(())
315        }
316        if self.model.is_empty() {
317            return Err("`model` string cannot be empty".to_string());
318        }
319        if self.synthetic_reasoning.is_some()
320            && let super::OutputMode::Instruction = self.output_mode
321        {
322            return Err(
323                "`synthetic_reasoning` cannot be true when `output_mode` is \"instruction\""
324                    .to_string(),
325            );
326        }
327        if let Some(top_logprobs) = self.top_logprobs
328            && top_logprobs > 20
329        {
330            return Err("`top_logprobs` must be at most 20".to_string());
331        }
332        if let Some(mcp_servers) = &self.mcp_servers {
333            super::super::mcp::mcp_servers::validate(mcp_servers)?;
334        }
335        if let Some(cm) = &self.client_objectiveai_mcp {
336            super::super::client_objectiveai_mcp::validate(cm)?;
337        }
338        validate_f64("frequency_penalty", self.frequency_penalty, -2.0, 2.0)?;
339        if let Some(logit_bias) = &self.logit_bias {
340            for (token, weight) in logit_bias {
341                if token.is_empty() {
342                    return Err("`logit_bias` keys cannot be empty".to_string());
343                } else if !token.chars().all(|c| c.is_ascii_digit()) {
344                    return Err(
345                        "`logit_bias` keys must be stringified token IDs"
346                            .to_string(),
347                    );
348                } else if token.chars().next().unwrap() == '0'
349                    && token.len() > 1
350                {
351                    return Err("`logit_bias` keys cannot have leading zeros"
352                        .to_string());
353                } else if *weight < -100 || *weight > 100 {
354                    return Err(
355                        "`logit_bias` values must be between -100 and 100"
356                            .to_string(),
357                    );
358                }
359            }
360        }
361        validate_u64(
362            "max_completion_tokens",
363            self.max_completion_tokens,
364            0,
365            i32::MAX as u64,
366        )?;
367        validate_f64("presence_penalty", self.presence_penalty, -2.0, 2.0)?;
368        if let Some(stop) = &self.stop {
369            stop.validate()?;
370        }
371        validate_f64("temperature", self.temperature, 0.0, 2.0)?;
372        validate_f64("top_p", self.top_p, 0.0, 1.0)?;
373        validate_u64("max_tokens", self.max_tokens, 0, i32::MAX as u64)?;
374        validate_f64("min_p", self.min_p, 0.0, 1.0)?;
375        if let Some(provider) = &self.provider {
376            provider.validate()?;
377        }
378        if let Some(reasoning) = &self.reasoning {
379            reasoning.validate()?;
380        }
381        validate_f64("repetition_penalty", self.repetition_penalty, 0.0, 2.0)?;
382        validate_f64("top_a", self.top_a, 0.0, 1.0)?;
383        validate_u64("top_k", self.top_k, 0, i32::MAX as u64)?;
384        if let Some(verbosity) = &self.verbosity {
385            verbosity.validate()?;
386        }
387        Ok(())
388    }
389
390    /// Returns prefix messages, then the provided messages, then suffix messages.
391    pub fn merged_messages(
392        &self,
393        messages: Vec<super::super::completions::message::Message>,
394    ) -> Vec<super::super::completions::message::Message> {
395        use super::super::completions::message::Message;
396        let prefix_len = self.prefix_messages.as_ref().map_or(0, |m| m.len());
397        let post_sys_len = self
398            .post_system_prefix_messages
399            .as_ref()
400            .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 = Vec::with_capacity(
403            prefix_len + post_sys_len + messages.len() + suffix_len,
404        );
405        if let Some(prefix) = &self.prefix_messages {
406            merged.extend(prefix.iter().cloned());
407        }
408        let mut post_sys_inserted = self.post_system_prefix_messages.is_none();
409        for msg in messages {
410            if !post_sys_inserted {
411                if !matches!(msg, Message::System(_) | Message::Developer(_)) {
412                    merged.extend(
413                        self.post_system_prefix_messages
414                            .as_ref()
415                            .unwrap()
416                            .iter()
417                            .cloned(),
418                    );
419                    post_sys_inserted = true;
420                }
421            }
422            merged.push(msg);
423        }
424        if !post_sys_inserted {
425            merged.extend(
426                self.post_system_prefix_messages
427                    .as_ref()
428                    .unwrap()
429                    .iter()
430                    .cloned(),
431            );
432        }
433        if let Some(suffix) = &self.suffix_messages {
434            merged.extend(suffix.iter().cloned());
435        }
436        merged
437    }
438
439    /// Computes the deterministic content-addressed ID.
440    pub fn id(&self) -> String {
441        let mut hasher = XxHash3_128::with_seed(0);
442        hasher.write(serde_json::to_string(self).unwrap().as_bytes());
443        format!("{:0>22}", base62::encode(hasher.finish_128()))
444    }
445}
446
447/// A validated OpenRouter Agent with its computed content-addressed ID.
448#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
449#[schemars(rename = "agent.openrouter.Agent")]
450pub struct Agent {
451    /// The deterministic content-addressed ID (22-character base62 string).
452    pub id: String,
453    /// The normalized configuration.
454    #[serde(flatten)]
455    pub base: AgentBase,
456}
457
458impl TryFrom<AgentBase> for Agent {
459    type Error = String;
460    fn try_from(mut base: AgentBase) -> Result<Self, Self::Error> {
461        base.prepare();
462        base.validate()?;
463        let id = base.id();
464        Ok(Agent { id, base })
465    }
466}