Skip to main content

ag_ui/event/
factories.rs

1//! Ergonomic constructors for every event type.
2//!
3//! These mirror the `create*Event` helpers in the TypeScript SDK: each takes
4//! the fields the schema requires and leaves the optional ones unset. Anything
5//! optional is set afterwards, either on the payload struct or through
6//! [`Event::with_timestamp`] and [`Event::with_raw_event`].
7//!
8//! ```
9//! # use ag_ui::{Event, RunOutcome, Interrupt};
10//! let paused = Event::run_finished_interrupt(
11//!     "thread-1",
12//!     "run-1",
13//!     vec![Interrupt::new("i-1", "tool_approval")],
14//! )
15//! .with_timestamp(1_700_000_000_000);
16//! ```
17
18use serde_json::Value;
19
20use crate::JsonObject;
21use crate::event::Event;
22use crate::event::activity::{ActivityDeltaEvent, ActivitySnapshotEvent};
23use crate::event::lifecycle::{
24    RunErrorEvent, RunFinishedEvent, RunStartedEvent, StepFinishedEvent, StepStartedEvent,
25};
26use crate::event::reasoning::{
27    ReasoningEncryptedValueEvent, ReasoningEncryptedValueSubtype, ReasoningEndEvent,
28    ReasoningMessageChunkEvent, ReasoningMessageContentEvent, ReasoningMessageEndEvent,
29    ReasoningMessageStartEvent, ReasoningStartEvent, ThinkingEndEvent, ThinkingStartEvent,
30    ThinkingTextMessageContentEvent, ThinkingTextMessageEndEvent, ThinkingTextMessageStartEvent,
31};
32use crate::event::special::{CustomEvent, RawEvent};
33use crate::event::state::{MessagesSnapshotEvent, StateDeltaEvent, StateSnapshotEvent};
34use crate::event::text::{
35    TextMessageChunkEvent, TextMessageContentEvent, TextMessageEndEvent, TextMessageRole,
36    TextMessageStartEvent,
37};
38use crate::event::tool::{
39    ToolCallArgsEvent, ToolCallChunkEvent, ToolCallEndEvent, ToolCallResultEvent,
40    ToolCallStartEvent,
41};
42use crate::ids::{MessageId, RunId, StepName, ThreadId, ToolCallId};
43use crate::message::Message;
44use crate::outcome::{Interrupt, RunOutcome};
45use crate::patch::PatchOperation;
46
47impl Event {
48    /// `TEXT_MESSAGE_START` — opens a text message.
49    pub fn text_message_start(message_id: impl Into<MessageId>, role: TextMessageRole) -> Self {
50        TextMessageStartEvent::new(message_id, role).into()
51    }
52
53    /// `TEXT_MESSAGE_CONTENT` — appends text to an open message.
54    pub fn text_message_content(
55        message_id: impl Into<MessageId>,
56        delta: impl Into<String>,
57    ) -> Self {
58        TextMessageContentEvent::new(message_id, delta).into()
59    }
60
61    /// `TEXT_MESSAGE_END` — closes a text message.
62    pub fn text_message_end(message_id: impl Into<MessageId>) -> Self {
63        TextMessageEndEvent::new(message_id).into()
64    }
65
66    /// `TEXT_MESSAGE_CHUNK` — a whole text update in one event.
67    pub fn text_message_chunk(message_id: Option<MessageId>, delta: Option<String>) -> Self {
68        TextMessageChunkEvent::new(message_id, delta).into()
69    }
70
71    /// `TOOL_CALL_START` — opens a tool call.
72    pub fn tool_call_start(
73        tool_call_id: impl Into<ToolCallId>,
74        tool_call_name: impl Into<String>,
75    ) -> Self {
76        ToolCallStartEvent::new(tool_call_id, tool_call_name).into()
77    }
78
79    /// `TOOL_CALL_ARGS` — appends argument JSON to an open call.
80    pub fn tool_call_args(tool_call_id: impl Into<ToolCallId>, delta: impl Into<String>) -> Self {
81        ToolCallArgsEvent::new(tool_call_id, delta).into()
82    }
83
84    /// `TOOL_CALL_END` — closes a tool call.
85    pub fn tool_call_end(tool_call_id: impl Into<ToolCallId>) -> Self {
86        ToolCallEndEvent::new(tool_call_id).into()
87    }
88
89    /// `TOOL_CALL_CHUNK` — a whole tool call in one event.
90    pub fn tool_call_chunk(
91        tool_call_id: Option<ToolCallId>,
92        tool_call_name: Option<String>,
93        delta: Option<String>,
94    ) -> Self {
95        ToolCallChunkEvent::new(tool_call_id, tool_call_name, delta).into()
96    }
97
98    /// `TOOL_CALL_RESULT` — reports what a tool returned.
99    pub fn tool_call_result(
100        message_id: impl Into<MessageId>,
101        tool_call_id: impl Into<ToolCallId>,
102        content: impl Into<String>,
103    ) -> Self {
104        ToolCallResultEvent::new(message_id, tool_call_id, content).into()
105    }
106
107    /// `THINKING_START` — opens a thinking block.
108    #[deprecated(note = "use Event::reasoning_start")]
109    pub fn thinking_start(title: Option<String>) -> Self {
110        ThinkingStartEvent::new(title).into()
111    }
112
113    /// `THINKING_END` — closes a thinking block.
114    #[deprecated(note = "use Event::reasoning_end")]
115    pub fn thinking_end() -> Self {
116        ThinkingEndEvent::default().into()
117    }
118
119    /// `THINKING_TEXT_MESSAGE_START` — opens a thinking message.
120    #[deprecated(note = "use Event::reasoning_message_start")]
121    pub fn thinking_text_message_start() -> Self {
122        ThinkingTextMessageStartEvent::default().into()
123    }
124
125    /// `THINKING_TEXT_MESSAGE_CONTENT` — appends thinking text.
126    #[deprecated(note = "use Event::reasoning_message_content")]
127    pub fn thinking_text_message_content(delta: impl Into<String>) -> Self {
128        ThinkingTextMessageContentEvent::new(delta).into()
129    }
130
131    /// `THINKING_TEXT_MESSAGE_END` — closes a thinking message.
132    #[deprecated(note = "use Event::reasoning_message_end")]
133    pub fn thinking_text_message_end() -> Self {
134        ThinkingTextMessageEndEvent::default().into()
135    }
136
137    /// `STATE_SNAPSHOT` — replaces the shared state.
138    pub fn state_snapshot(snapshot: impl Into<Value>) -> Self {
139        StateSnapshotEvent::new(snapshot).into()
140    }
141
142    /// `STATE_DELTA` — patches the shared state.
143    pub fn state_delta(delta: impl Into<Vec<PatchOperation>>) -> Self {
144        StateDeltaEvent::new(delta).into()
145    }
146
147    /// `MESSAGES_SNAPSHOT` — replaces the message history.
148    pub fn messages_snapshot(messages: impl Into<Vec<Message>>) -> Self {
149        MessagesSnapshotEvent::new(messages).into()
150    }
151
152    /// `ACTIVITY_SNAPSHOT` — publishes an activity payload.
153    pub fn activity_snapshot(
154        message_id: impl Into<MessageId>,
155        activity_type: impl Into<String>,
156        content: JsonObject,
157    ) -> Self {
158        ActivitySnapshotEvent::new(message_id, activity_type, content).into()
159    }
160
161    /// `ACTIVITY_DELTA` — patches an activity payload.
162    pub fn activity_delta(
163        message_id: impl Into<MessageId>,
164        activity_type: impl Into<String>,
165        patch: impl Into<Vec<PatchOperation>>,
166    ) -> Self {
167        ActivityDeltaEvent::new(message_id, activity_type, patch).into()
168    }
169
170    /// `RAW` — forwards a provider event verbatim.
171    pub fn raw(event: impl Into<Value>) -> Self {
172        RawEvent::new(event).into()
173    }
174
175    /// `CUSTOM` — an application-defined event.
176    pub fn custom(name: impl Into<String>, value: impl Into<Value>) -> Self {
177        CustomEvent::new(name, value).into()
178    }
179
180    /// `RUN_STARTED` — starts a run.
181    pub fn run_started(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
182        RunStartedEvent::new(thread_id, run_id).into()
183    }
184
185    /// `RUN_FINISHED` without an outcome — the legacy shape, which consumers
186    /// read as success.
187    pub fn run_finished(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
188        RunFinishedEvent::new(thread_id, run_id).into()
189    }
190
191    /// `RUN_FINISHED` with an explicit success outcome.
192    pub fn run_finished_success(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
193        RunFinishedEvent::new(thread_id, run_id)
194            .with_outcome(RunOutcome::Success)
195            .into()
196    }
197
198    /// `RUN_FINISHED` with an interrupt outcome — the run is paused until the
199    /// client answers.
200    pub fn run_finished_interrupt(
201        thread_id: impl Into<ThreadId>,
202        run_id: impl Into<RunId>,
203        interrupts: impl Into<Vec<Interrupt>>,
204    ) -> Self {
205        RunFinishedEvent::new(thread_id, run_id)
206            .with_outcome(RunOutcome::interrupt(interrupts))
207            .into()
208    }
209
210    /// `RUN_ERROR` — fails the run.
211    pub fn run_error(message: impl Into<String>) -> Self {
212        RunErrorEvent::new(message).into()
213    }
214
215    /// `STEP_STARTED` — opens a named step.
216    pub fn step_started(step_name: impl Into<StepName>) -> Self {
217        StepStartedEvent::new(step_name).into()
218    }
219
220    /// `STEP_FINISHED` — closes a named step.
221    pub fn step_finished(step_name: impl Into<StepName>) -> Self {
222        StepFinishedEvent::new(step_name).into()
223    }
224
225    /// `REASONING_START` — opens a reasoning block.
226    pub fn reasoning_start(message_id: impl Into<MessageId>) -> Self {
227        ReasoningStartEvent::new(message_id).into()
228    }
229
230    /// `REASONING_MESSAGE_START` — opens a reasoning message.
231    pub fn reasoning_message_start(message_id: impl Into<MessageId>) -> Self {
232        ReasoningMessageStartEvent::new(message_id).into()
233    }
234
235    /// `REASONING_MESSAGE_CONTENT` — appends reasoning text.
236    pub fn reasoning_message_content(
237        message_id: impl Into<MessageId>,
238        delta: impl Into<String>,
239    ) -> Self {
240        ReasoningMessageContentEvent::new(message_id, delta).into()
241    }
242
243    /// `REASONING_MESSAGE_END` — closes a reasoning message.
244    pub fn reasoning_message_end(message_id: impl Into<MessageId>) -> Self {
245        ReasoningMessageEndEvent::new(message_id).into()
246    }
247
248    /// `REASONING_MESSAGE_CHUNK` — a whole reasoning update in one event.
249    pub fn reasoning_message_chunk(message_id: Option<MessageId>, delta: Option<String>) -> Self {
250        ReasoningMessageChunkEvent::new(message_id, delta).into()
251    }
252
253    /// `REASONING_END` — closes a reasoning block.
254    pub fn reasoning_end(message_id: impl Into<MessageId>) -> Self {
255        ReasoningEndEvent::new(message_id).into()
256    }
257
258    /// `REASONING_ENCRYPTED_VALUE` — carries an opaque reasoning blob.
259    pub fn reasoning_encrypted_value(
260        subtype: ReasoningEncryptedValueSubtype,
261        entity_id: impl Into<String>,
262        encrypted_value: impl Into<String>,
263    ) -> Self {
264        ReasoningEncryptedValueEvent::new(subtype, entity_id, encrypted_value).into()
265    }
266}