Skip to main content

objectiveai_sdk/cli/command/
command_executor.rs

1use futures::Stream;
2
3use crate::cli::command::CommandRequest;
4use crate::cli::command::CommandResponse;
5
6pub mod binary;
7pub mod plugin;
8pub mod websocket;
9
10pub use binary::BinaryExecutor;
11pub use websocket::WebSocketExecutor;
12
13/// Run a [`CommandRequest`] against some backend (subprocess of the cli
14/// binary, in-process router, mock, …) and surface its output as a
15/// stream of typed items.
16///
17/// `T` is left to the caller: pick a concrete leaf response type
18/// (`agents::spawn::Response`, `functions::execute::standard::ResponseItem`,
19/// …) or a more general `serde_json::Value` for opaque consumption.
20///
21/// Every call accepts an optional [`AgentArguments`] bag controlling
22/// per-call identity. When `Some`, subprocess-spawning executors stamp
23/// each `Some(v)` field on the child env and `env_remove` each `None`
24/// — atomic per-call override. When `None`, the child inherits parent
25/// env unchanged. In-process executors (e.g. the plugin executor)
26/// accept the parameter for trait-signature symmetry and ignore it.
27pub trait CommandExecutor {
28    type Error: Send + 'static;
29    type Stream<T>: Stream<Item = Result<T, Self::Error>> + Send + 'static
30    where
31        T: Send + 'static;
32
33    fn execute<R, T>(
34        &self,
35        request: R,
36        agent_arguments: Option<&super::AgentArguments>,
37    ) -> impl Future<Output = Result<Self::Stream<T>, Self::Error>> + Send
38    where
39        R: CommandRequest + Send + serde::Serialize,
40        T: CommandResponse + serde::Serialize + serde::de::DeserializeOwned + Send + 'static;
41
42    /// Convenience for unary commands: run the request and resolve the
43    /// first item from the stream. Implementations should error with
44    /// their own "empty stream" variant if the stream closes without
45    /// producing an item.
46    fn execute_one<R, T>(
47        &self,
48        request: R,
49        agent_arguments: Option<&super::AgentArguments>,
50    ) -> impl Future<Output = Result<T, Self::Error>> + Send
51    where
52        R: CommandRequest + Send + serde::Serialize,
53        T: CommandResponse + serde::Serialize + serde::de::DeserializeOwned + Send + 'static;
54}