Skip to main content

objectiveai_sdk/cli/command/laboratories/attach/
mod.rs

1//! `laboratories attach` — attach a laboratory id to an agent
2//! target (a tag, or an instance hierarchy via PAIH + `--agent-instance`).
3//! The attachment is keyed on the tag (for a tag target) or on the AIH
4//! (for an instance target); see the CLI handler for the lock + insert.
5
6use crate::cli::command::CommandRequest;
7use crate::cli::command::agents::selector::{AgentSelector, AgentSelectorArgs};
8
9#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
10#[schemars(rename = "cli.command.laboratories.attach.Request")]
11pub struct Request {
12    pub path_type: Path,
13    pub selector: AgentSelector,
14    pub laboratory_id: String,
15    /// The EXACT machine id whose laboratory host serves the
16    /// laboratory. Provided together with `machine_state` or not at
17    /// all; neither ⇒ the current machine + the daemon's own state.
18    #[serde(default, skip_serializing_if = "Option::is_none")]
19    #[schemars(extend("omitempty" = true))]
20    pub machine: Option<String>,
21    /// The state (on `machine`) whose laboratory host serves the
22    /// laboratory. Paired with `machine` — both or neither.
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    #[schemars(extend("omitempty" = true))]
25    pub machine_state: Option<String>,
26    #[serde(flatten)]
27    pub base: crate::cli::command::RequestBase,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
31#[schemars(rename = "cli.command.laboratories.attach.Path")]
32pub enum Path {
33    #[serde(rename = "laboratories/attach")]
34    LaboratoriesAttach,
35}
36
37impl CommandRequest for Request {
38    fn request_base(&self) -> &crate::cli::command::RequestBase {
39        &self.base
40    }
41
42    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
43        Some(&mut self.base)
44    }
45}
46
47/// Confirmation — attach succeeded; echoes the laboratory id that was
48/// attached.
49#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
50#[schemars(rename = "cli.command.laboratories.attach.Response")]
51pub struct Response {
52    /// The laboratory id that was attached to the target.
53    pub laboratory_id: String,
54    /// The machine id of the laboratory host the attachment row
55    /// records (the provided pair, or the auto-filled local one).
56    #[serde(default, skip_serializing_if = "Option::is_none")]
57    #[schemars(extend("omitempty" = true))]
58    pub machine: Option<String>,
59    /// The state the attachment row records, paired with `machine`.
60    #[serde(default, skip_serializing_if = "Option::is_none")]
61    #[schemars(extend("omitempty" = true))]
62    pub machine_state: Option<String>,
63}
64
65#[derive(clap::Args)]
66#[command(group(clap::ArgGroup::new("laboratory_id_required").required(true).args(["laboratory_id"])))]
67pub struct Args {
68    #[command(flatten)]
69    pub selector: AgentSelectorArgs,
70    /// Laboratory id to attach to the target agent.
71    #[arg(long)]
72    pub laboratory_id: Option<String>,
73    /// The EXACT machine id whose host serves the laboratory.
74    /// Requires `--machine-state`; neither ⇒ the current machine +
75    /// the daemon's own state.
76    #[arg(long, requires = "machine_state")]
77    pub machine: Option<String>,
78    /// The state (on `--machine`) whose host serves the laboratory.
79    /// Requires `--machine` — both or neither.
80    #[arg(long, requires = "machine")]
81    pub machine_state: Option<String>,
82    #[command(flatten)]
83    pub base: crate::cli::command::RequestBaseArgs,
84}
85
86#[derive(clap::Args)]
87#[command(args_conflicts_with_subcommands = true)]
88pub struct Command {
89    #[command(flatten)]
90    pub args: Args,
91    #[command(subcommand)]
92    pub schema: Option<Schema>,
93}
94
95#[derive(clap::Subcommand)]
96pub enum Schema {
97    /// Emit the JSON Schema for this leaf's `Request` type and exit.
98    RequestSchema(request_schema::Args),
99    /// Emit the JSON Schema for this leaf's `Response` type and exit.
100    ResponseSchema(response_schema::Args),
101}
102
103impl TryFrom<Args> for Request {
104    type Error = crate::cli::command::FromArgsError;
105    fn try_from(args: Args) -> Result<Self, Self::Error> {
106        let selector = AgentSelector::try_from(args.selector)?;
107        let laboratory_id = args.laboratory_id.ok_or_else(|| {
108            crate::cli::command::FromArgsError::path_parse(
109                "laboratory_id",
110                "--laboratory-id is required".to_string(),
111            )
112        })?;
113        // Both-or-neither, re-validated beyond clap's mutual
114        // `requires` (which only runs for argv-built requests).
115        if args.machine.is_some() != args.machine_state.is_some() {
116            return Err(crate::cli::command::FromArgsError::path_parse(
117                "machine",
118                "--machine and --machine-state must be provided together".to_string(),
119            ));
120        }
121        Ok(Self {
122            path_type: Path::LaboratoriesAttach,
123            selector,
124            laboratory_id,
125            machine: args.machine,
126            machine_state: args.machine_state,
127            base: args.base.into(),
128        })
129    }
130}
131
132#[cfg(feature = "cli-executor")]
133pub async fn execute<E: crate::cli::command::CommandExecutor>(
134    executor: &E,
135    mut request: Request,
136    agent_arguments: Option<&crate::cli::command::AgentArguments>,
137) -> Result<Response, E::Error> {
138    request.base.clear_transform();
139    executor.execute_one(request, agent_arguments).await
140}
141
142#[cfg(feature = "cli-executor")]
143pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
144    executor: &E,
145    mut request: Request,
146    transform: crate::cli::command::Transform,
147    agent_arguments: Option<&crate::cli::command::AgentArguments>,
148) -> Result<serde_json::Value, E::Error> {
149    request.base.set_transform(transform);
150    executor.execute_one(request, agent_arguments).await
151}
152
153#[cfg(feature = "mcp")]
154impl crate::cli::command::CommandResponse for Response {
155    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
156        crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
157    }
158}
159
160pub mod request_schema;
161
162pub mod response_schema;
163
164/// One `/listen` broadcast run of `laboratories attach`: the actual
165/// [`Request`], the producer's
166/// [`AgentArguments`](crate::cli::command::AgentArguments), and the
167/// unary response future. See [`crate::cli::broadcast_listener`].
168#[cfg(feature = "cli-listener")]
169pub struct ListenerExecution {
170    pub request: Request,
171    pub agent_arguments: crate::cli::command::AgentArguments,
172    pub response: crate::cli::broadcast_listener::UnaryResponse<Response>,
173}