Skip to main content

objectiveai_cli/command/tasks/
schedule.rs

1//! `agents tasks schedule` — bare-naked handler. Captures the
2//! caller's current `AgentArguments` from env vars and persists
3//! the schedule row in `tasks.sqlite`.
4
5use objectiveai_sdk::cli::command::AgentArguments;
6use objectiveai_sdk::cli::command::tasks::schedule::{Request, Response};
7
8use crate::context::Context;
9use crate::db;
10use crate::error::Error;
11
12pub async fn execute(ctx: &Context, request: Request) -> Result<Response, Error> {
13    // Snapshot the caller's identity from `ctx.config` — the CLI
14    // bin reads OBJECTIVEAI_* env vars into Config at startup, so
15    // this captures the same identity without re-touching the env.
16    // The runner that fires this schedule later will re-export each
17    // `Some(_)` field as the matching env var.
18    let agent_arguments = AgentArguments {
19        agent_instance_hierarchy: Some(ctx.config.agent_instance_hierarchy.clone()),
20        agent_id: ctx.config.agent_id.clone(),
21        agent_full_id: ctx.config.agent_full_id.clone(),
22        agent_remote: ctx.config.agent_remote.clone(),
23        response_id: ctx.config.response_id.clone(),
24        response_ids: ctx.config.response_ids.clone(),
25        mcp_session_id: ctx.config.mcp_session_id.clone(),
26    };
27
28    let name = request.name.clone();
29    let (_db_id, version) = db::tasks::insert_schedule(
30        ctx.db_client().await?,
31        &request.name,
32        &request.command,
33        &request.description,
34        &ctx.config.agent_instance_hierarchy,
35        request.interval_seconds,
36        &agent_arguments,
37        ctx.plugin.as_ref(),
38        request.overwrite,
39    )
40    .await?
41    .ok_or_else(|| Error::ScheduleAlreadyExists {
42        name: name.clone(),
43        agent_instance_hierarchy: ctx.config.agent_instance_hierarchy.clone(),
44    })?;
45
46    Ok(Response {
47        name,
48        agent_instance_hierarchy: ctx.config.agent_instance_hierarchy.clone(),
49        version: version as u64,
50    })
51}
52
53pub mod request_schema {
54    use objectiveai_sdk::cli::command::tasks::schedule as sdk;
55    use objectiveai_sdk::cli::command::tasks::schedule::request_schema::{
56        Request, Response,
57    };
58
59    use crate::context::Context;
60    use crate::error::Error;
61
62    pub async fn execute(_ctx: &Context, _request: Request) -> Result<Response, Error> {
63        Ok(objectiveai_sdk::cli::command::ResponseSchema(
64            schemars::schema_for!(sdk::Request),
65        ))
66    }
67}
68
69pub mod response_schema {
70    use objectiveai_sdk::cli::command::tasks::schedule as sdk;
71    use objectiveai_sdk::cli::command::tasks::schedule::response_schema::{
72        Request, Response,
73    };
74
75    use crate::context::Context;
76    use crate::error::Error;
77
78    pub async fn execute(_ctx: &Context, _request: Request) -> Result<Response, Error> {
79        Ok(objectiveai_sdk::cli::command::ResponseSchema(
80            schemars::schema_for!(sdk::Response),
81        ))
82    }
83}