Skip to main content

objectiveai_sdk/cli/command/viewer/config/
mod.rs

1pub mod address;
2pub mod get;
3pub mod secret;
4pub mod signature;
5
6#[derive(clap::Subcommand)]
7pub enum Command {
8    Get(get::Command),
9    Address {
10        #[command(subcommand)]
11        command: address::Command,
12    },
13    Secret {
14        #[command(subcommand)]
15        command: secret::Command,
16    },
17    Signature {
18        #[command(subcommand)]
19        command: signature::Command,
20    },
21}
22
23#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
24#[serde(untagged)]
25#[schemars(rename = "cli.command.viewer.config.Request")]
26pub enum Request {
27    #[schemars(title = "Get")]
28    Get(get::Request),
29    #[schemars(title = "GetRequestSchema")]
30    GetRequestSchema(get::request_schema::Request),
31    #[schemars(title = "GetResponseSchema")]
32    GetResponseSchema(get::response_schema::Request),
33    #[schemars(title = "Address")]
34    Address(address::Request),
35    #[schemars(title = "Secret")]
36    Secret(secret::Request),
37    #[schemars(title = "Signature")]
38    Signature(signature::Request),
39}
40
41// Exempt from json-schema coverage: tier aggregate (see the root
42// `ResponseItem` in command.rs - TS7056).
43#[objectiveai_sdk_macros::json_schema_ignore]
44#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
45#[schemars(rename = "cli.command.viewer.config.Response")]
46#[serde(untagged)]
47pub enum Response {
48    #[schemars(title = "Get")]
49    Get(get::Response),
50    #[schemars(title = "GetRequestSchema")]
51    GetRequestSchema(get::request_schema::Response),
52    #[schemars(title = "GetResponseSchema")]
53    GetResponseSchema(get::response_schema::Response),
54    #[schemars(title = "Address")]
55    Address(address::Response),
56    #[schemars(title = "Secret")]
57    Secret(secret::Response),
58    #[schemars(title = "Signature")]
59    Signature(signature::Response),
60}
61
62#[cfg(feature = "mcp")]
63impl crate::cli::command::CommandResponse for Response {
64    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
65        match self {
66            Response::Get(v) => v.into_mcp(),
67            Response::GetRequestSchema(v) => v.into_mcp(),
68            Response::GetResponseSchema(v) => v.into_mcp(),
69            Response::Address(v) => v.into_mcp(),
70            Response::Secret(v) => v.into_mcp(),
71            Response::Signature(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::Get(cmd) => match cmd.schema {
81                None => Ok(Request::Get(get::Request::try_from(cmd.args)?)),
82                Some(get::Schema::RequestSchema(args)) =>
83                    Ok(Request::GetRequestSchema(get::request_schema::Request::try_from(args)?)),
84                Some(get::Schema::ResponseSchema(args)) =>
85                    Ok(Request::GetResponseSchema(get::response_schema::Request::try_from(args)?)),
86            },
87            Command::Address { command } =>
88                Ok(Request::Address(address::Request::try_from(command)?)),
89            Command::Secret { command } =>
90                Ok(Request::Secret(secret::Request::try_from(command)?)),
91            Command::Signature { command } =>
92                Ok(Request::Signature(signature::Request::try_from(command)?)),
93        }
94    }
95}
96
97impl crate::cli::command::CommandRequest for Request {
98    fn request_base(&self) -> &crate::cli::command::RequestBase {
99        match self {
100            Request::Get(inner) => inner.request_base(),
101            Request::GetRequestSchema(inner) => inner.request_base(),
102            Request::GetResponseSchema(inner) => inner.request_base(),
103            Request::Address(inner) => inner.request_base(),
104            Request::Secret(inner) => inner.request_base(),
105            Request::Signature(inner) => inner.request_base(),
106        }
107    }
108
109    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
110        match self {
111            Request::Get(inner) => inner.request_base_mut(),
112            Request::GetRequestSchema(inner) => inner.request_base_mut(),
113            Request::GetResponseSchema(inner) => inner.request_base_mut(),
114            Request::Address(inner) => inner.request_base_mut(),
115            Request::Secret(inner) => inner.request_base_mut(),
116            Request::Signature(inner) => inner.request_base_mut(),
117        }
118    }
119}
120
121#[cfg(feature = "cli-executor")]
122pub async fn execute<E: crate::cli::command::CommandExecutor>(
123    executor: &E,
124    request: Request,
125
126        agent_arguments: Option<&crate::cli::command::AgentArguments>,
127    ) -> Result<
128    std::pin::Pin<Box<dyn futures::Stream<Item = Result<Response, E::Error>> + Send>>,
129    E::Error,
130> {
131    use futures::StreamExt;
132    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<Response, E::Error>> + Send>> =
133        match request {
134            Request::Get(req) => {
135                let value = get::execute(executor, req, agent_arguments).await?;
136                Box::pin(crate::cli::command::StreamOnce::new(Ok(
137                    Response::Get(value),
138                )))
139            }
140            Request::GetRequestSchema(req) => {
141                let value = get::request_schema::execute(executor, req, agent_arguments).await?;
142                Box::pin(crate::cli::command::StreamOnce::new(Ok(
143                    Response::GetRequestSchema(value),
144                )))
145            }
146            Request::GetResponseSchema(req) => {
147                let value = get::response_schema::execute(executor, req, agent_arguments).await?;
148                Box::pin(crate::cli::command::StreamOnce::new(Ok(
149                    Response::GetResponseSchema(value),
150                )))
151            }
152            Request::Address(req) => {
153                let inner = address::execute(executor, req, agent_arguments).await?;
154                Box::pin(inner.map(|r| r.map(Response::Address)))
155            }
156            Request::Secret(req) => {
157                let inner = secret::execute(executor, req, agent_arguments).await?;
158                Box::pin(inner.map(|r| r.map(Response::Secret)))
159            }
160            Request::Signature(req) => {
161                let inner = signature::execute(executor, req, agent_arguments).await?;
162                Box::pin(inner.map(|r| r.map(Response::Signature)))
163            }
164        };
165    Ok(stream)
166}
167
168#[cfg(feature = "cli-executor")]
169pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
170    executor: &E,
171    request: Request,
172    transform: crate::cli::command::Transform,
173
174        agent_arguments: Option<&crate::cli::command::AgentArguments>,
175    ) -> Result<
176    std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>>,
177    E::Error,
178> {
179    let stream: std::pin::Pin<Box<dyn futures::Stream<Item = Result<serde_json::Value, E::Error>> + Send>> =
180        match request {
181            Request::Get(req) => {
182                let value = get::execute_transform(executor, req, transform, agent_arguments).await?;
183                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
184            }
185            Request::GetRequestSchema(req) => {
186                let value = get::request_schema::execute_transform(executor, req, transform, agent_arguments).await?;
187                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
188            }
189            Request::GetResponseSchema(req) => {
190                let value = get::response_schema::execute_transform(executor, req, transform, agent_arguments).await?;
191                Box::pin(crate::cli::command::StreamOnce::new(Ok(value)))
192            }
193            Request::Address(req) => {
194                let inner = address::execute_transform(executor, req, transform, agent_arguments).await?;
195                Box::pin(inner)
196            }
197            Request::Secret(req) => {
198                let inner = secret::execute_transform(executor, req, transform, agent_arguments).await?;
199                Box::pin(inner)
200            }
201            Request::Signature(req) => {
202                let inner = signature::execute_transform(executor, req, transform, agent_arguments).await?;
203                Box::pin(inner)
204            }
205        };
206    Ok(stream)
207}