Skip to main content

objectiveai_sdk/cli/command/channels/logs/subscribe/
mod.rs

1//! `channels logs subscribe` — the live cousin of `list --pending`.
2//! Returns IMMEDIATELY if unread entries already exist; otherwise
3//! blocks until a new matching entry arrives or the channel closes,
4//! then returns. Yields envelope [`ChannelLogEntry`] items (advancing
5//! the role's watermark), or the terminal `"channel_closed"` when the
6//! channel is closed with nothing pending. Authenticate with a channel
7//! secret (`S_pub` or `S_owner`).
8
9use crate::cli::command::CommandRequest;
10
11pub use super::list::ChannelLogEntry;
12
13#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
14#[schemars(rename = "cli.command.channels.logs.subscribe.Request")]
15pub struct Request {
16    pub path_type: Path,
17    pub channel_id: String,
18    pub secret: String,
19    /// Skip entries with `id <= after_id`.
20    #[serde(default, skip_serializing_if = "Option::is_none")]
21    #[schemars(extend("omitempty" = true))]
22    pub after_id: Option<i64>,
23    /// Cap on entries returned per wake.
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    #[schemars(extend("omitempty" = true))]
26    pub limit: Option<i64>,
27    #[serde(flatten)]
28    pub base: crate::cli::command::RequestBase,
29}
30
31#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
32#[schemars(rename = "cli.command.channels.logs.subscribe.Path")]
33pub enum Path {
34    #[serde(rename = "channels/logs/subscribe")]
35    ChannelsLogsSubscribe,
36}
37
38impl CommandRequest for Request {
39    fn request_base(&self) -> &crate::cli::command::RequestBase {
40        &self.base
41    }
42
43    fn request_base_mut(&mut self) -> Option<&mut crate::cli::command::RequestBase> {
44        Some(&mut self.base)
45    }
46}
47
48/// Subscribe's wire shape. Either a real envelope entry (the EXACT
49/// same shape `list` emits) OR the literal string `"channel_closed"`.
50/// `#[serde(untagged)]` so the `Item` arm passes through transparently.
51#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
52#[serde(untagged)]
53#[schemars(rename = "cli.command.channels.logs.subscribe.ResponseItem")]
54pub enum ResponseItem {
55    #[schemars(title = "Item")]
56    Item(ChannelLogEntry),
57    #[schemars(title = "ChannelClosed")]
58    ChannelClosed(ChannelClosedTag),
59}
60
61/// Single-variant enum whose lone variant serializes as the literal
62/// string `"channel_closed"`.
63#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
64#[schemars(rename = "cli.command.channels.logs.subscribe.ChannelClosedTag")]
65pub enum ChannelClosedTag {
66    #[serde(rename = "channel_closed")]
67    ChannelClosed,
68}
69
70#[derive(clap::Args)]
71#[command(group(clap::ArgGroup::new("channel_required").required(true).args(["id"])))]
72#[command(group(clap::ArgGroup::new("secret_required").required(true).args(["secret"])))]
73pub struct Args {
74    /// The channel id.
75    #[arg(long)]
76    pub id: Option<String>,
77    /// A channel secret (`S_pub` or `S_owner`).
78    #[arg(long)]
79    pub secret: Option<String>,
80    /// Skip entries with id <= after_id.
81    #[arg(long)]
82    pub after_id: Option<i64>,
83    /// Cap on entries returned per wake.
84    #[arg(long)]
85    pub limit: Option<i64>,
86    #[command(flatten)]
87    pub base: crate::cli::command::RequestBaseArgs,
88}
89
90#[derive(clap::Args)]
91#[command(args_conflicts_with_subcommands = true)]
92pub struct Command {
93    #[command(flatten)]
94    pub args: Args,
95    #[command(subcommand)]
96    pub schema: Option<Schema>,
97}
98
99#[derive(clap::Subcommand)]
100pub enum Schema {
101    /// Emit the JSON Schema for this leaf's `Request` type and exit.
102    RequestSchema(request_schema::Args),
103    /// Emit the JSON Schema for this leaf's `Response` type and exit.
104    ResponseSchema(response_schema::Args),
105}
106
107impl TryFrom<Args> for Request {
108    type Error = crate::cli::command::FromArgsError;
109    fn try_from(args: Args) -> Result<Self, Self::Error> {
110        let channel_id = args.id.ok_or_else(|| {
111            crate::cli::command::FromArgsError::path_parse("id", "--id is required".to_string())
112        })?;
113        let secret = args.secret.ok_or_else(|| {
114            crate::cli::command::FromArgsError::path_parse(
115                "secret",
116                "--secret is required".to_string(),
117            )
118        })?;
119        Ok(Self {
120            path_type: Path::ChannelsLogsSubscribe,
121            channel_id,
122            secret,
123            after_id: args.after_id,
124            limit: args.limit,
125            base: args.base.into(),
126        })
127    }
128}
129
130#[cfg(feature = "mcp")]
131impl crate::cli::command::CommandResponse for ResponseItem {
132    fn into_mcp(self) -> crate::cli::command::McpResponseItem {
133        crate::cli::command::McpResponseItem::JSONL(serde_json::to_value(self).unwrap())
134    }
135}
136
137#[cfg(feature = "cli-executor")]
138pub async fn execute<E: crate::cli::command::CommandExecutor>(
139    executor: &E,
140    mut request: Request,
141    identity: Option<&crate::identity::Identity>,
142) -> Result<E::Stream<ResponseItem>, E::Error> {
143    request.base.clear_transform();
144    executor.execute(request, identity).await
145}
146
147#[cfg(feature = "cli-executor")]
148pub async fn execute_transform<E: crate::cli::command::CommandExecutor>(
149    executor: &E,
150    mut request: Request,
151    transform: crate::cli::command::Transform,
152    identity: Option<&crate::identity::Identity>,
153) -> Result<E::Stream<serde_json::Value>, E::Error> {
154    request.base.set_transform(transform);
155    executor.execute(request, identity).await
156}
157
158pub mod request_schema;
159
160pub mod response_schema;
161
162/// See [`crate::daemon::command_listener`].
163#[cfg(all(feature = "cli", feature = "daemon"))]
164pub struct ListenerExecution {
165    pub request: Request,
166    pub identity: crate::identity::Identity,
167    pub response: crate::daemon::command_listener::ResponseItemStream<ResponseItem>,
168}