objectiveai_sdk/cli/command/agents/tools/
mod.rs1use crate::cli::command::CommandRequest;
11
12pub mod call;
13pub mod list;
14
15#[derive(clap::Subcommand)]
16pub enum Command {
17 Call(call::Command),
19 List(list::Command),
21}
22
23#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
24#[serde(untagged)]
25#[schemars(rename = "cli.command.agents.tools.Request")]
26pub enum Request {
27 #[schemars(title = "Call")]
28 Call(call::Request),
29 #[schemars(title = "CallRequestSchema")]
30 CallRequestSchema(call::request_schema::Request),
31 #[schemars(title = "CallResponseSchema")]
32 CallResponseSchema(call::response_schema::Request),
33 #[schemars(title = "List")]
34 List(list::Request),
35 #[schemars(title = "ListRequestSchema")]
36 ListRequestSchema(list::request_schema::Request),
37 #[schemars(title = "ListResponseSchema")]
38 ListResponseSchema(list::response_schema::Request),
39}
40
41#[objectiveai_sdk_macros::json_schema_ignore]
44#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
45#[schemars(rename = "cli.command.agents.tools.ResponseItem")]
46#[serde(untagged)]
47pub enum ResponseItem {
48 #[schemars(title = "Call")]
49 Call(call::Response),
50 #[schemars(title = "CallRequestSchema")]
51 CallRequestSchema(call::request_schema::Response),
52 #[schemars(title = "CallResponseSchema")]
53 CallResponseSchema(call::response_schema::Response),
54 #[schemars(title = "List")]
55 List(list::Response),
56 #[schemars(title = "ListRequestSchema")]
57 ListRequestSchema(list::request_schema::Response),
58 #[schemars(title = "ListResponseSchema")]
59 ListResponseSchema(list::response_schema::Response),
60}
61
62#[cfg(feature = "mcp")]
63impl crate::cli::command::CommandResponse for ResponseItem {
64 fn into_mcp(self) -> crate::cli::command::McpResponseItem {
65 match self {
66 ResponseItem::Call(v) => v.into_mcp(),
67 ResponseItem::CallRequestSchema(v) => v.into_mcp(),
68 ResponseItem::CallResponseSchema(v) => v.into_mcp(),
69 ResponseItem::List(v) => v.into_mcp(),
70 ResponseItem::ListRequestSchema(v) => v.into_mcp(),
71 ResponseItem::ListResponseSchema(v) => v.into_mcp(),
72 }
73 }
74}
75
76impl TryFrom<Command> for Request {
77 type Error = crate::cli::command::FromArgsError;
78 fn try_from(command: Command) -> Result<Self, Self::Error> {
79 match command {
80 Command::Call(cmd) => match cmd.schema {
81 None => Ok(Request::Call(call::Request::try_from(cmd.args)?)),
82 Some(call::Schema::RequestSchema(args)) => Ok(
83 Request::CallRequestSchema(call::request_schema::Request::try_from(args)?),
84 ),
85 Some(call::Schema::ResponseSchema(args)) => Ok(
86 Request::CallResponseSchema(call::response_schema::Request::try_from(args)?),
87 ),
88 },
89 Command::List(cmd) => match cmd.schema {
90 None => Ok(Request::List(list::Request::try_from(cmd.args)?)),
91 Some(list::Schema::RequestSchema(args)) => Ok(
92 Request::ListRequestSchema(list::request_schema::Request::try_from(args)?),
93 ),
94 Some(list::Schema::ResponseSchema(args)) => Ok(
95 Request::ListResponseSchema(list::response_schema::Request::try_from(args)?),
96 ),
97 },
98 }
99 }
100}
101
102impl CommandRequest for Request {
103 fn request_base(&self) -> &crate::cli::command::RequestBase {
104 match self {
105 Request::Call(inner) => inner.request_base(),
106 Request::CallRequestSchema(inner) => inner.request_base(),
107 Request::CallResponseSchema(inner) => inner.request_base(),
108 Request::List(inner) => inner.request_base(),
109 Request::ListRequestSchema(inner) => inner.request_base(),
110 Request::ListResponseSchema(inner) => inner.request_base(),
111 }
112 }
113
114 fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
115 match self {
116 Request::Call(inner) => inner.request_base_mut(),
117 Request::CallRequestSchema(inner) => inner.request_base_mut(),
118 Request::CallResponseSchema(inner) => inner.request_base_mut(),
119 Request::List(inner) => inner.request_base_mut(),
120 Request::ListRequestSchema(inner) => inner.request_base_mut(),
121 Request::ListResponseSchema(inner) => inner.request_base_mut(),
122 }
123 }
124}
125
126#[cfg(feature = "cli-executor")]
127pub async fn execute<E: crate::cli::command::CommandExecutor>(
128 executor: &E,
129 request: Request,
130 agent_arguments: Option<&crate::cli::command::AgentArguments>,
131) -> Result<
132 std::pin::Pin<Box<dyn futures::Stream<Item = Result<ResponseItem, E::Error>> + Send>>,
133 E::Error,
134> {
135 let stream: std::pin::Pin<
136 Box<dyn futures::Stream<Item = Result<ResponseItem, E::Error>> + Send>,
137 > = match request {
138 Request::Call(req) => {
139 let value = call::execute(executor, req, agent_arguments).await?;
140 Box::pin(crate::cli::command::StreamOnce::new(Ok(
141 ResponseItem::Call(value),
142 )))
143 }
144 Request::CallRequestSchema(req) => {
145 let value =
146 call::request_schema::execute(executor, req, agent_arguments).await?;
147 Box::pin(crate::cli::command::StreamOnce::new(Ok(
148 ResponseItem::CallRequestSchema(value),
149 )))
150 }
151 Request::CallResponseSchema(req) => {
152 let value =
153 call::response_schema::execute(executor, req, agent_arguments).await?;
154 Box::pin(crate::cli::command::StreamOnce::new(Ok(
155 ResponseItem::CallResponseSchema(value),
156 )))
157 }
158 Request::List(req) => {
159 let value = list::execute(executor, req, agent_arguments).await?;
160 Box::pin(crate::cli::command::StreamOnce::new(Ok(
161 ResponseItem::List(value),
162 )))
163 }
164 Request::ListRequestSchema(req) => {
165 let value =
166 list::request_schema::execute(executor, req, agent_arguments).await?;
167 Box::pin(crate::cli::command::StreamOnce::new(Ok(
168 ResponseItem::ListRequestSchema(value),
169 )))
170 }
171 Request::ListResponseSchema(req) => {
172 let value =
173 list::response_schema::execute(executor, req, agent_arguments).await?;
174 Box::pin(crate::cli::command::StreamOnce::new(Ok(
175 ResponseItem::ListResponseSchema(value),
176 )))
177 }
178 };
179 Ok(stream)
180}
181
182#[cfg(feature = "cli-executor")]
183pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
184 executor: &E,
185 request: Request,
186 transform: crate::cli::command::Transform,
187 agent_arguments: Option<&crate::cli::command::AgentArguments>,
188) -> Result<
189 std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>>,
190 E::Error,
191> {
192 let stream: std::pin::Pin<
193 Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>,
194 > = match request {
195 Request::Call(req) => {
196 let value = call::execute_transform(executor, req, transform, agent_arguments).await?;
197 Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
198 }
199 Request::CallRequestSchema(req) => {
200 let value =
201 call::request_schema::execute_transform(executor, req, transform, agent_arguments).await?;
202 Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
203 }
204 Request::CallResponseSchema(req) => {
205 let value =
206 call::response_schema::execute_transform(executor, req, transform, agent_arguments).await?;
207 Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
208 }
209 Request::List(req) => {
210 let value = list::execute_transform(executor, req, transform, agent_arguments).await?;
211 Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
212 }
213 Request::ListRequestSchema(req) => {
214 let value =
215 list::request_schema::execute_transform(executor, req, transform, agent_arguments).await?;
216 Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
217 }
218 Request::ListResponseSchema(req) => {
219 let value =
220 list::response_schema::execute_transform(executor, req, transform, agent_arguments).await?;
221 Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
222 }
223 };
224 Ok(stream)
225}