Skip to main content

ag_ui/event/
activity.rs

1//! Structured progress updates: `ACTIVITY_SNAPSHOT`, `ACTIVITY_DELTA`.
2//!
3//! Activities are how an agent reports what it is *doing* — searching, reading
4//! files, waiting on an API — in a shape the client renders itself, rather than
5//! as prose in the reply.
6
7use serde::{Deserialize, Serialize};
8
9use crate::JsonObject;
10use crate::event::BaseEvent;
11use crate::ids::MessageId;
12use crate::patch::PatchOperation;
13
14/// Publishes the full content of an activity.
15#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
18#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
19pub struct ActivitySnapshotEvent {
20    /// Timestamp and raw provider event.
21    #[serde(flatten)]
22    pub base: BaseEvent,
23    /// The activity message being written.
24    pub message_id: MessageId,
25    /// Client-defined activity discriminator, for example `"web_search"`.
26    pub activity_type: String,
27    /// The activity payload.
28    #[cfg_attr(
29        feature = "schemars",
30        schemars(with = "std::collections::BTreeMap<String, serde_json::Value>")
31    )]
32    #[cfg_attr(feature = "utoipa", schema(value_type = Object))]
33    pub content: JsonObject,
34    /// Whether this replaces the existing content (the default) or merges into
35    /// it.
36    #[serde(default = "default_replace")]
37    pub replace: bool,
38}
39
40/// The upstream schema defaults `replace` to `true`, so an omitted field means
41/// "replace" rather than "merge".
42const fn default_replace() -> bool {
43    true
44}
45
46impl Default for ActivitySnapshotEvent {
47    fn default() -> Self {
48        Self {
49            base: BaseEvent::default(),
50            message_id: MessageId::default(),
51            activity_type: String::new(),
52            content: JsonObject::new(),
53            replace: default_replace(),
54        }
55    }
56}
57
58impl ActivitySnapshotEvent {
59    /// Publishes an activity payload, replacing any previous content.
60    pub fn new(
61        message_id: impl Into<MessageId>,
62        activity_type: impl Into<String>,
63        content: JsonObject,
64    ) -> Self {
65        Self {
66            base: BaseEvent::default(),
67            message_id: message_id.into(),
68            activity_type: activity_type.into(),
69            content,
70            replace: true,
71        }
72    }
73}
74
75/// Mutates an activity's content with a JSON Patch document.
76#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
77#[serde(rename_all = "camelCase")]
78#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
79#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
80pub struct ActivityDeltaEvent {
81    /// Timestamp and raw provider event.
82    #[serde(flatten)]
83    pub base: BaseEvent,
84    /// The activity message being patched.
85    pub message_id: MessageId,
86    /// Client-defined activity discriminator.
87    pub activity_type: String,
88    /// RFC 6902 operations, applied in order to the activity content.
89    pub patch: Vec<PatchOperation>,
90}
91
92impl ActivityDeltaEvent {
93    /// Patches an activity's content.
94    pub fn new(
95        message_id: impl Into<MessageId>,
96        activity_type: impl Into<String>,
97        patch: impl Into<Vec<PatchOperation>>,
98    ) -> Self {
99        Self {
100            base: BaseEvent::default(),
101            message_id: message_id.into(),
102            activity_type: activity_type.into(),
103            patch: patch.into(),
104        }
105    }
106}