Skip to main content

objectiveai_sdk/agent/mock/
agent.rs

1//! Mock Agent types and validation logic.
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use twox_hash::XxHash3_128;
6
7/// The base configuration for a Mock Agent (without computed ID).
8#[derive(
9    Clone,
10    Debug,
11    Default,
12    PartialEq,
13    Serialize,
14    Deserialize,
15    JsonSchema,
16    arbitrary::Arbitrary,
17)]
18#[schemars(rename = "agent.mock.AgentBase")]
19pub struct AgentBase {
20    /// The upstream provider marker.
21    pub upstream: super::Upstream,
22
23    /// The output mode for vector completions. Ignored for agent completions.
24    pub output_mode: super::OutputMode,
25
26    /// Number of top log probabilities to return (2-20).
27    ///
28    /// **Vector completions only.** Ignored for agent completions.
29    #[serde(skip_serializing_if = "Option::is_none")]
30    #[schemars(extend("omitempty" = true))]
31    #[arbitrary(with = crate::arbitrary_util::arbitrary_option_u64)]
32    pub top_logprobs: Option<u64>,
33
34    /// If true, the mock client will return an error instead of a response.
35    #[serde(default, skip_serializing_if = "Option::is_none")]
36    #[schemars(extend("omitempty" = true))]
37    pub error: Option<bool>,
38
39    /// Probability (0-100) that the mock returns an error mid-stream.
40    /// Requires `error` to be `Some(true)`.
41    #[serde(default, skip_serializing_if = "Option::is_none")]
42    #[schemars(extend("omitempty" = true))]
43    pub error_probability: Option<u8>,
44
45    /// MCP servers the agent can connect to.
46    #[serde(skip_serializing_if = "Option::is_none")]
47    #[schemars(extend("omitempty" = true))]
48    pub mcp_servers: Option<super::super::McpServers>,
49
50    /// Client-side ObjectiveAI MCP surface the calling client is
51    /// expected to expose locally back to the API (objectiveai
52    /// built-in, plus specific plugins / tools by owner+name+version).
53    #[serde(skip_serializing_if = "Option::is_none")]
54    #[schemars(extend("omitempty" = true))]
55    pub client_objectiveai_mcp: Option<super::super::ClientObjectiveaiMcp>,
56
57    /// Deterministic-script override. When `Some`, the mock agent
58    /// emits each [`super::Call`] as its own assistant turn —
59    /// `tool_calls` first, then `content` — in array order. Each
60    /// subsequent turn inspects the continuation to count how many
61    /// `Call`s have already been satisfied (assistant message with
62    /// exactly that `Call`'s `tool_calls` (by name+arguments) and
63    /// `content`); the next un-matched `Call` is what that turn
64    /// emits. Once every `Call` has been satisfied in the
65    /// continuation, the mock falls through to its normal
66    /// dispatcher. Pure addition — agents without `calls` are
67    /// unaffected.
68    #[serde(default, skip_serializing_if = "Option::is_none")]
69    #[schemars(extend("omitempty" = true))]
70    pub calls: Option<Vec<super::Call>>,
71}
72
73impl AgentBase {
74    /// Normalizes the configuration for deterministic ID computation.
75    pub fn prepare(&mut self) {
76        self.top_logprobs = match self.top_logprobs {
77            Some(0) | Some(1) => None,
78            other => other,
79        };
80        if self.error == Some(true) && self.error_probability == Some(0) {
81            self.error = None;
82            self.error_probability = None;
83        }
84        if self.error == Some(false) {
85            self.error = None;
86        }
87        self.mcp_servers = match self.mcp_servers.take() {
88            Some(mcp_servers) => {
89                super::super::mcp::mcp_servers::prepare(mcp_servers)
90            }
91            None => None,
92        };
93        self.client_objectiveai_mcp = match self.client_objectiveai_mcp.take() {
94            Some(cm) => super::super::client_objectiveai_mcp::prepare(cm),
95            None => None,
96        };
97    }
98
99    /// Validates the configuration.
100    pub fn validate(&self) -> Result<(), String> {
101        if let Some(top_logprobs) = self.top_logprobs
102            && top_logprobs > 20
103        {
104            return Err("`top_logprobs` must be at most 20".to_string());
105        }
106        if let Some(mcp_servers) = &self.mcp_servers {
107            super::super::mcp::mcp_servers::validate(mcp_servers)?;
108        }
109        if let Some(cm) = &self.client_objectiveai_mcp {
110            super::super::client_objectiveai_mcp::validate(cm)?;
111        }
112        if let Some(p) = self.error_probability {
113            if p > 100 {
114                return Err(
115                    "`error_probability` must be at most 100".to_string()
116                );
117            }
118            if self.error != Some(true) {
119                return Err("`error_probability` requires `error` to be true"
120                    .to_string());
121            }
122        }
123        Ok(())
124    }
125
126    /// Returns the messages as-is.
127    pub fn merged_messages(
128        &self,
129        messages: Vec<super::super::completions::message::Message>,
130    ) -> Vec<super::super::completions::message::Message> {
131        messages
132    }
133
134    /// Computes the deterministic content-addressed ID.
135    pub fn id(&self) -> String {
136        let mut hasher = XxHash3_128::with_seed(0);
137        hasher.write(serde_json::to_string(self).unwrap().as_bytes());
138        format!("{:0>22}", base62::encode(hasher.finish_128()))
139    }
140
141    pub const fn model() -> &'static str {
142        "mock"
143    }
144}
145
146/// A validated Mock Agent with its computed content-addressed ID.
147#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
148#[schemars(rename = "agent.mock.Agent")]
149pub struct Agent {
150    /// The deterministic content-addressed ID (22-character base62 string).
151    pub id: String,
152    /// The normalized configuration.
153    #[serde(flatten)]
154    pub base: AgentBase,
155}
156
157impl TryFrom<AgentBase> for Agent {
158    type Error = String;
159    fn try_from(mut base: AgentBase) -> Result<Self, Self::Error> {
160        base.prepare();
161        base.validate()?;
162        let id = base.id();
163        Ok(Agent { id, base })
164    }
165}