objectiveai_sdk/agent/script/
agent.rs1use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use twox_hash::XxHash3_128;
6
7#[derive(
9 Clone,
10 Debug,
11 PartialEq,
12 Serialize,
13 Deserialize,
14 JsonSchema,
15 arbitrary::Arbitrary,
16)]
17#[schemars(rename = "agent.script.AgentBase")]
18pub struct AgentBase {
19 pub upstream: super::Upstream,
21
22 pub output_mode: super::OutputMode,
24
25 #[serde(flatten)]
29 #[schemars(schema_with = "crate::flatten_schema::<super::Script>")]
30 pub script: super::Script,
31
32 #[serde(skip_serializing_if = "Option::is_none")]
34 #[schemars(extend("omitempty" = true))]
35 pub mcp_servers: Option<super::super::McpServers>,
36
37 #[serde(skip_serializing_if = "Option::is_none")]
42 #[schemars(extend("omitempty" = true))]
43 pub laboratories: Option<super::super::Laboratories>,
44
45 #[serde(skip_serializing_if = "Option::is_none")]
49 #[schemars(extend("omitempty" = true))]
50 pub client_objectiveai_mcp: Option<super::super::ClientObjectiveaiMcp>,
51}
52
53impl AgentBase {
54 pub fn prepare(&mut self) {
58 self.mcp_servers = match self.mcp_servers.take() {
59 Some(mcp_servers) => {
60 super::super::mcp::mcp_servers::prepare(mcp_servers)
61 }
62 None => None,
63 };
64 self.laboratories = match self.laboratories.take() {
65 Some(laboratories) => {
66 super::super::laboratory::laboratories::prepare(laboratories)
67 }
68 None => None,
69 };
70 self.client_objectiveai_mcp = match self.client_objectiveai_mcp.take() {
71 Some(cm) => super::super::client_objectiveai_mcp::prepare(cm),
72 None => None,
73 };
74 }
75
76 pub fn validate(&self) -> Result<(), String> {
78 self.script.validate()?;
79 if let Some(mcp_servers) = &self.mcp_servers {
80 super::super::mcp::mcp_servers::validate(mcp_servers)?;
81 }
82 if let Some(laboratories) = &self.laboratories {
83 super::super::laboratory::laboratories::validate(laboratories)?;
84 }
85 if let Some(cm) = &self.client_objectiveai_mcp {
86 super::super::client_objectiveai_mcp::validate(cm)?;
87 }
88 Ok(())
89 }
90
91 pub fn merged_messages(
93 &self,
94 messages: Vec<super::super::completions::message::Message>,
95 ) -> Vec<super::super::completions::message::Message> {
96 messages
97 }
98
99 pub fn id(&self) -> String {
101 let mut hasher = XxHash3_128::with_seed(0);
102 hasher.write(serde_json::to_string(self).unwrap().as_bytes());
103 format!("{:0>22}", base62::encode(hasher.finish_128()))
104 }
105
106 pub const fn model() -> &'static str {
107 "script"
108 }
109}
110
111#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
113#[schemars(rename = "agent.script.Agent")]
114pub struct Agent {
115 pub id: String,
117 #[serde(flatten)]
119 pub base: AgentBase,
120}
121
122impl TryFrom<AgentBase> for Agent {
123 type Error = String;
124 fn try_from(mut base: AgentBase) -> Result<Self, Self::Error> {
125 base.prepare();
126 base.validate()?;
127 let id = base.id();
128 Ok(Agent { id, base })
129 }
130}