Skip to main content

objectiveai_cli/command/agents/
enqueue.rs

1//! `agents enqueue` — fire-and-forget into the queue.
2//!
3//! Bypasses the lock race and spawn child entirely (that's `agents
4//! message`); writes one row into `message_queue` against the
5//! target. With `--key`, `db::message_queue::enqueue_with_content`
6//! replaces any prior active row scoped to the same (target, key)
7//! pair before insert, so re-issuing with the same key reliably
8//! replaces the prior payload. Refs have no queue identity — error.
9//! Tags need NOT exist yet: the row is parked against the tag NAME
10//! whether or not the tag is registered, and the queue's two-rule read
11//! predicate resolves it once the tag binds to an agent that reads
12//! (park-now, deliver-on-bind). A tag that never binds simply leaves
13//! the row unconsumed.
14
15use objectiveai_sdk::cli::command::agents::enqueue::{Request, Response};
16use objectiveai_sdk::cli::command::agents::selector::AgentSelector;
17
18use crate::context::Context;
19use crate::error::Error;
20
21pub async fn execute(ctx: &Context, request: Request) -> Result<Response, Error> {
22    let content = super::message::resolve_message(ctx, request.message).await?;
23    let (hier, tag) = match request.agent {
24        AgentSelector::Instance {
25            parent_agent_instance_hierarchy,
26            agent_instance,
27        } => {
28            let parent = parent_agent_instance_hierarchy
29                .as_deref()
30                .unwrap_or(&ctx.config.agent_instance_hierarchy);
31            (Some(format!("{parent}/{agent_instance}")), None)
32        }
33        // No existence check: park the row against the tag NAME whether
34        // or not the tag is registered yet. The queue's read predicate
35        // resolves it once the tag binds to a reading agent (park-now,
36        // deliver-on-bind).
37        AgentSelector::Tag { agent_tag } => (None, Some(agent_tag)),
38        AgentSelector::Ref { .. } => return Err(Error::EnqueueRefTarget),
39    };
40    let id = crate::db::message_queue::enqueue_with_content(
41        ctx.db_client().await?,
42        hier.clone(),
43        tag.clone(),
44        &ctx.config.agent_instance_hierarchy,
45        request.key,
46        content,
47    )
48    .await?;
49    Ok(Response {
50        id,
51        agent_instance_hierarchy: hier,
52        agent_tag: tag,
53    })
54}
55
56pub mod request_schema {
57    use objectiveai_sdk::cli::command::agents::enqueue as sdk;
58    use objectiveai_sdk::cli::command::agents::enqueue::request_schema::{Request, Response};
59
60    use crate::context::Context;
61    use crate::error::Error;
62
63    pub async fn execute(_ctx: &Context, _request: Request) -> Result<Response, Error> {
64        Ok(objectiveai_sdk::cli::command::ResponseSchema(schemars::schema_for!(sdk::Request)))
65    }
66}
67
68pub mod response_schema {
69    use objectiveai_sdk::cli::command::agents::enqueue as sdk;
70    use objectiveai_sdk::cli::command::agents::enqueue::response_schema::{Request, Response};
71
72    use crate::context::Context;
73    use crate::error::Error;
74
75    pub async fn execute(_ctx: &Context, _request: Request) -> Result<Response, Error> {
76        Ok(objectiveai_sdk::cli::command::ResponseSchema(schemars::schema_for!(sdk::Response)))
77    }
78}