ag-ui 1.0.0

Rust SDK for AG-UI 1.0 with protocol types, SSE, a server runtime for hosting agents, and a client runtime for consuming them.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Model reasoning: `REASONING_*`, plus the deprecated `THINKING_*` events
//! they replaced.

use serde::{Deserialize, Serialize};

use crate::event::BaseEvent;
use crate::ids::{MessageId, SubagentRunId};

/// Opens a reasoning block. Reasoning messages inside it are bracketed by
/// `REASONING_MESSAGE_START` / `REASONING_MESSAGE_END`, and the block closes
/// with `REASONING_END`.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ReasoningStartEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
    /// The message this reasoning belongs to.
    pub message_id: MessageId,
    /// The subagent that produced this event; absent means the parent agent.
    /// A JSON `null` is rejected — see [`crate::event::subagent`].
    #[serde(
        default,
        deserialize_with = "crate::serde_util::reject_null",
        skip_serializing_if = "Option::is_none"
    )]
    pub subagent_run_id: Option<SubagentRunId>,
}

impl ReasoningStartEvent {
    /// Opens a reasoning block for `message_id`.
    pub fn new(message_id: impl Into<MessageId>) -> Self {
        Self {
            base: BaseEvent::default(),
            message_id: message_id.into(),
            subagent_run_id: None,
        }
    }
}

/// The single role a reasoning message may declare.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum ReasoningRole {
    /// `"reasoning"`.
    #[default]
    #[serde(rename = "reasoning")]
    Reasoning,
}

/// Opens a reasoning message.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ReasoningMessageStartEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
    /// Id of the reasoning message being opened.
    pub message_id: MessageId,
    /// Always [`ReasoningRole::Reasoning`]. Required, unlike the optional role
    /// on `TEXT_MESSAGE_START`.
    pub role: ReasoningRole,
    /// The subagent that produced this event; absent means the parent agent.
    /// A JSON `null` is rejected — see [`crate::event::subagent`].
    #[serde(
        default,
        deserialize_with = "crate::serde_util::reject_null",
        skip_serializing_if = "Option::is_none"
    )]
    pub subagent_run_id: Option<SubagentRunId>,
}

impl ReasoningMessageStartEvent {
    /// Opens a reasoning message.
    pub fn new(message_id: impl Into<MessageId>) -> Self {
        Self {
            base: BaseEvent::default(),
            message_id: message_id.into(),
            role: ReasoningRole::Reasoning,
            subagent_run_id: None,
        }
    }
}

/// Appends a chunk of reasoning text.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ReasoningMessageContentEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
    /// The reasoning message being appended to.
    pub message_id: MessageId,
    /// The text to append.
    pub delta: String,
    /// The subagent that produced this event; absent means the parent agent.
    /// A JSON `null` is rejected — see [`crate::event::subagent`].
    #[serde(
        default,
        deserialize_with = "crate::serde_util::reject_null",
        skip_serializing_if = "Option::is_none"
    )]
    pub subagent_run_id: Option<SubagentRunId>,
}

impl ReasoningMessageContentEvent {
    /// Appends `delta` to the reasoning message.
    pub fn new(message_id: impl Into<MessageId>, delta: impl Into<String>) -> Self {
        Self {
            base: BaseEvent::default(),
            message_id: message_id.into(),
            delta: delta.into(),
            subagent_run_id: None,
        }
    }
}

/// Closes a reasoning message.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ReasoningMessageEndEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
    /// The reasoning message being closed.
    pub message_id: MessageId,
    /// The subagent that produced this event; absent means the parent agent.
    /// A JSON `null` is rejected — see [`crate::event::subagent`].
    #[serde(
        default,
        deserialize_with = "crate::serde_util::reject_null",
        skip_serializing_if = "Option::is_none"
    )]
    pub subagent_run_id: Option<SubagentRunId>,
}

impl ReasoningMessageEndEvent {
    /// Closes the reasoning message.
    pub fn new(message_id: impl Into<MessageId>) -> Self {
        Self {
            base: BaseEvent::default(),
            message_id: message_id.into(),
            subagent_run_id: None,
        }
    }
}

/// A self-contained reasoning update: start, content and end folded into one.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ReasoningMessageChunkEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
    /// The reasoning message this chunk belongs to.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub message_id: Option<MessageId>,
    /// The text to append.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub delta: Option<String>,
    /// The subagent that produced this event; absent means the parent agent.
    /// A JSON `null` is rejected — see [`crate::event::subagent`]. Under
    /// concurrency a chunk that omits its `message_id` is resolved within the
    /// sending subagent's own stream, so attribute every chunk when several
    /// subagents stream at once.
    #[serde(
        default,
        deserialize_with = "crate::serde_util::reject_null",
        skip_serializing_if = "Option::is_none"
    )]
    pub subagent_run_id: Option<SubagentRunId>,
}

impl ReasoningMessageChunkEvent {
    /// Builds a reasoning chunk.
    pub fn new(message_id: Option<MessageId>, delta: Option<String>) -> Self {
        Self {
            base: BaseEvent::default(),
            message_id,
            delta,
            subagent_run_id: None,
        }
    }
}

/// Closes a reasoning block.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ReasoningEndEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
    /// The message whose reasoning block is closing.
    pub message_id: MessageId,
    /// The subagent that produced this event; absent means the parent agent.
    /// A JSON `null` is rejected — see [`crate::event::subagent`].
    #[serde(
        default,
        deserialize_with = "crate::serde_util::reject_null",
        skip_serializing_if = "Option::is_none"
    )]
    pub subagent_run_id: Option<SubagentRunId>,
}

impl ReasoningEndEvent {
    /// Closes the reasoning block for `message_id`.
    pub fn new(message_id: impl Into<MessageId>) -> Self {
        Self {
            base: BaseEvent::default(),
            message_id: message_id.into(),
            subagent_run_id: None,
        }
    }
}

/// What an encrypted reasoning signature belongs to.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum ReasoningEncryptedValueSubtype {
    /// The signature covers a tool call.
    #[serde(rename = "tool-call")]
    ToolCall,
    /// The signature covers a message.
    #[default]
    #[serde(rename = "message")]
    Message,
}

impl ReasoningEncryptedValueSubtype {
    /// The subtype string as it appears on the wire.
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::ToolCall => "tool-call",
            Self::Message => "message",
        }
    }
}

/// Carries a provider's opaque reasoning signature.
///
/// Under zero-data-retention the provider returns no readable reasoning, only a
/// blob that must be replayed on the next request for the model to continue
/// coherently.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ReasoningEncryptedValueEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
    /// Whether `entity_id` names a tool call or a message.
    pub subtype: ReasoningEncryptedValueSubtype,
    /// The tool call or message the blob belongs to.
    pub entity_id: String,
    /// The opaque blob.
    pub encrypted_value: String,
    /// The subagent that produced this event; absent means the parent agent.
    /// A JSON `null` is rejected — see [`crate::event::subagent`].
    #[serde(
        default,
        deserialize_with = "crate::serde_util::reject_null",
        skip_serializing_if = "Option::is_none"
    )]
    pub subagent_run_id: Option<SubagentRunId>,
}

impl ReasoningEncryptedValueEvent {
    /// Attaches an encrypted reasoning blob to an entity.
    pub fn new(
        subtype: ReasoningEncryptedValueSubtype,
        entity_id: impl Into<String>,
        encrypted_value: impl Into<String>,
    ) -> Self {
        Self {
            base: BaseEvent::default(),
            subtype,
            entity_id: entity_id.into(),
            encrypted_value: encrypted_value.into(),
            subagent_run_id: None,
        }
    }
}

// The five THINKING_* payloads below are deprecated, but `#[deprecated]` is
// suppressed when the `utoipa` feature is on: utoipa 5.5's derive emits a
// `.deprecated()` call on the `AllOf` builder it uses for `#[serde(flatten)]`
// structs and tagged-enum variants, and that builder has no such method, so the
// crate would not compile. The deprecation stays unconditional on the
// `Event::thinking_*` constructors, which utoipa never sees.
//
// None of them carries `subagentRunId`: the family predates subagents and is
// excluded from the attribution table upstream, so accepting the field here
// would let TypeScript-shaped payloads through that Python and .NET reject.

/// Opens a thinking block.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(
    not(feature = "utoipa"),
    deprecated(note = "use the REASONING_* events: ReasoningStartEvent")
)]
pub struct ThinkingStartEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
    /// Heading to show above the block.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
}

impl ThinkingStartEvent {
    /// Opens a thinking block with an optional title.
    pub fn new(title: Option<String>) -> Self {
        Self {
            base: BaseEvent::default(),
            title,
        }
    }
}

/// Closes a thinking block.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(
    not(feature = "utoipa"),
    deprecated(note = "use the REASONING_* events: ReasoningEndEvent")
)]
pub struct ThinkingEndEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
}

/// Opens a thinking text message.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(
    not(feature = "utoipa"),
    deprecated(note = "use the REASONING_* events: ReasoningMessageStartEvent")
)]
pub struct ThinkingTextMessageStartEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
}

/// Appends a chunk of thinking text.
///
/// Unlike its replacement it carries no message id — a thinking block could
/// only ever have one message in flight, which is why the event was replaced.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(
    not(feature = "utoipa"),
    deprecated(note = "use the REASONING_* events: ReasoningMessageContentEvent")
)]
pub struct ThinkingTextMessageContentEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
    /// The text to append.
    pub delta: String,
}

impl ThinkingTextMessageContentEvent {
    /// Appends `delta` to the open thinking message.
    pub fn new(delta: impl Into<String>) -> Self {
        Self {
            base: BaseEvent::default(),
            delta: delta.into(),
        }
    }
}

/// Closes a thinking text message.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(
    not(feature = "utoipa"),
    deprecated(note = "use the REASONING_* events: ReasoningMessageEndEvent")
)]
pub struct ThinkingTextMessageEndEvent {
    /// Timestamp and raw provider event.
    #[serde(flatten)]
    pub base: BaseEvent,
}