Skip to main content

ag_ui/event/
reasoning.rs

1//! Model reasoning: `REASONING_*`, plus the deprecated `THINKING_*` events
2//! they replaced.
3
4use serde::{Deserialize, Serialize};
5
6use crate::event::BaseEvent;
7use crate::ids::MessageId;
8
9/// Opens a reasoning block. Reasoning messages inside it are bracketed by
10/// `REASONING_MESSAGE_START` / `REASONING_MESSAGE_END`, and the block closes
11/// with `REASONING_END`.
12#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
13#[serde(rename_all = "camelCase")]
14#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
15#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
16pub struct ReasoningStartEvent {
17    /// Timestamp and raw provider event.
18    #[serde(flatten)]
19    pub base: BaseEvent,
20    /// The message this reasoning belongs to.
21    pub message_id: MessageId,
22}
23
24impl ReasoningStartEvent {
25    /// Opens a reasoning block for `message_id`.
26    pub fn new(message_id: impl Into<MessageId>) -> Self {
27        Self {
28            base: BaseEvent::default(),
29            message_id: message_id.into(),
30        }
31    }
32}
33
34/// The single role a reasoning message may declare.
35#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
36#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
37#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
38pub enum ReasoningRole {
39    /// `"reasoning"`.
40    #[default]
41    #[serde(rename = "reasoning")]
42    Reasoning,
43}
44
45/// Opens a reasoning message.
46#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
47#[serde(rename_all = "camelCase")]
48#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
49#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
50pub struct ReasoningMessageStartEvent {
51    /// Timestamp and raw provider event.
52    #[serde(flatten)]
53    pub base: BaseEvent,
54    /// Id of the reasoning message being opened.
55    pub message_id: MessageId,
56    /// Always [`ReasoningRole::Reasoning`]. Required, unlike the optional role
57    /// on `TEXT_MESSAGE_START`.
58    pub role: ReasoningRole,
59}
60
61impl ReasoningMessageStartEvent {
62    /// Opens a reasoning message.
63    pub fn new(message_id: impl Into<MessageId>) -> Self {
64        Self {
65            base: BaseEvent::default(),
66            message_id: message_id.into(),
67            role: ReasoningRole::Reasoning,
68        }
69    }
70}
71
72/// Appends a chunk of reasoning text.
73#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
74#[serde(rename_all = "camelCase")]
75#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
76#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
77pub struct ReasoningMessageContentEvent {
78    /// Timestamp and raw provider event.
79    #[serde(flatten)]
80    pub base: BaseEvent,
81    /// The reasoning message being appended to.
82    pub message_id: MessageId,
83    /// The text to append.
84    pub delta: String,
85}
86
87impl ReasoningMessageContentEvent {
88    /// Appends `delta` to the reasoning message.
89    pub fn new(message_id: impl Into<MessageId>, delta: impl Into<String>) -> Self {
90        Self {
91            base: BaseEvent::default(),
92            message_id: message_id.into(),
93            delta: delta.into(),
94        }
95    }
96}
97
98/// Closes a reasoning message.
99#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
100#[serde(rename_all = "camelCase")]
101#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
102#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
103pub struct ReasoningMessageEndEvent {
104    /// Timestamp and raw provider event.
105    #[serde(flatten)]
106    pub base: BaseEvent,
107    /// The reasoning message being closed.
108    pub message_id: MessageId,
109}
110
111impl ReasoningMessageEndEvent {
112    /// Closes the reasoning message.
113    pub fn new(message_id: impl Into<MessageId>) -> Self {
114        Self {
115            base: BaseEvent::default(),
116            message_id: message_id.into(),
117        }
118    }
119}
120
121/// A self-contained reasoning update: start, content and end folded into one.
122#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
123#[serde(rename_all = "camelCase")]
124#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
125#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
126pub struct ReasoningMessageChunkEvent {
127    /// Timestamp and raw provider event.
128    #[serde(flatten)]
129    pub base: BaseEvent,
130    /// The reasoning message this chunk belongs to.
131    #[serde(default, skip_serializing_if = "Option::is_none")]
132    pub message_id: Option<MessageId>,
133    /// The text to append.
134    #[serde(default, skip_serializing_if = "Option::is_none")]
135    pub delta: Option<String>,
136}
137
138impl ReasoningMessageChunkEvent {
139    /// Builds a reasoning chunk.
140    pub fn new(message_id: Option<MessageId>, delta: Option<String>) -> Self {
141        Self {
142            base: BaseEvent::default(),
143            message_id,
144            delta,
145        }
146    }
147}
148
149/// Closes a reasoning block.
150#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
151#[serde(rename_all = "camelCase")]
152#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
153#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
154pub struct ReasoningEndEvent {
155    /// Timestamp and raw provider event.
156    #[serde(flatten)]
157    pub base: BaseEvent,
158    /// The message whose reasoning block is closing.
159    pub message_id: MessageId,
160}
161
162impl ReasoningEndEvent {
163    /// Closes the reasoning block for `message_id`.
164    pub fn new(message_id: impl Into<MessageId>) -> Self {
165        Self {
166            base: BaseEvent::default(),
167            message_id: message_id.into(),
168        }
169    }
170}
171
172/// What an encrypted reasoning signature belongs to.
173#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
174#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
175#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
176pub enum ReasoningEncryptedValueSubtype {
177    /// The signature covers a tool call.
178    #[serde(rename = "tool-call")]
179    ToolCall,
180    /// The signature covers a message.
181    #[default]
182    #[serde(rename = "message")]
183    Message,
184}
185
186impl ReasoningEncryptedValueSubtype {
187    /// The subtype string as it appears on the wire.
188    pub const fn as_str(&self) -> &'static str {
189        match self {
190            Self::ToolCall => "tool-call",
191            Self::Message => "message",
192        }
193    }
194}
195
196/// Carries a provider's opaque reasoning signature.
197///
198/// Under zero-data-retention the provider returns no readable reasoning, only a
199/// blob that must be replayed on the next request for the model to continue
200/// coherently.
201#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
202#[serde(rename_all = "camelCase")]
203#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
204#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
205pub struct ReasoningEncryptedValueEvent {
206    /// Timestamp and raw provider event.
207    #[serde(flatten)]
208    pub base: BaseEvent,
209    /// Whether `entity_id` names a tool call or a message.
210    pub subtype: ReasoningEncryptedValueSubtype,
211    /// The tool call or message the blob belongs to.
212    pub entity_id: String,
213    /// The opaque blob.
214    pub encrypted_value: String,
215}
216
217impl ReasoningEncryptedValueEvent {
218    /// Attaches an encrypted reasoning blob to an entity.
219    pub fn new(
220        subtype: ReasoningEncryptedValueSubtype,
221        entity_id: impl Into<String>,
222        encrypted_value: impl Into<String>,
223    ) -> Self {
224        Self {
225            base: BaseEvent::default(),
226            subtype,
227            entity_id: entity_id.into(),
228            encrypted_value: encrypted_value.into(),
229        }
230    }
231}
232
233// The five THINKING_* payloads below are deprecated, but `#[deprecated]` is
234// suppressed when the `utoipa` feature is on: utoipa 5.5's derive emits a
235// `.deprecated()` call on the `AllOf` builder it uses for `#[serde(flatten)]`
236// structs and tagged-enum variants, and that builder has no such method, so the
237// crate would not compile. The deprecation stays unconditional on the
238// `Event::thinking_*` constructors, which utoipa never sees.
239
240/// Opens a thinking block.
241#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
242#[serde(rename_all = "camelCase")]
243#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
244#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
245#[cfg_attr(
246    not(feature = "utoipa"),
247    deprecated(note = "use the REASONING_* events: ReasoningStartEvent")
248)]
249pub struct ThinkingStartEvent {
250    /// Timestamp and raw provider event.
251    #[serde(flatten)]
252    pub base: BaseEvent,
253    /// Heading to show above the block.
254    #[serde(default, skip_serializing_if = "Option::is_none")]
255    pub title: Option<String>,
256}
257
258impl ThinkingStartEvent {
259    /// Opens a thinking block with an optional title.
260    pub fn new(title: Option<String>) -> Self {
261        Self {
262            base: BaseEvent::default(),
263            title,
264        }
265    }
266}
267
268/// Closes a thinking block.
269#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
270#[serde(rename_all = "camelCase")]
271#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
272#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
273#[cfg_attr(
274    not(feature = "utoipa"),
275    deprecated(note = "use the REASONING_* events: ReasoningEndEvent")
276)]
277pub struct ThinkingEndEvent {
278    /// Timestamp and raw provider event.
279    #[serde(flatten)]
280    pub base: BaseEvent,
281}
282
283/// Opens a thinking text message.
284#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
285#[serde(rename_all = "camelCase")]
286#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
287#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
288#[cfg_attr(
289    not(feature = "utoipa"),
290    deprecated(note = "use the REASONING_* events: ReasoningMessageStartEvent")
291)]
292pub struct ThinkingTextMessageStartEvent {
293    /// Timestamp and raw provider event.
294    #[serde(flatten)]
295    pub base: BaseEvent,
296}
297
298/// Appends a chunk of thinking text.
299///
300/// Unlike its replacement it carries no message id — a thinking block could
301/// only ever have one message in flight, which is why the event was replaced.
302#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
303#[serde(rename_all = "camelCase")]
304#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
305#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
306#[cfg_attr(
307    not(feature = "utoipa"),
308    deprecated(note = "use the REASONING_* events: ReasoningMessageContentEvent")
309)]
310pub struct ThinkingTextMessageContentEvent {
311    /// Timestamp and raw provider event.
312    #[serde(flatten)]
313    pub base: BaseEvent,
314    /// The text to append.
315    pub delta: String,
316}
317
318impl ThinkingTextMessageContentEvent {
319    /// Appends `delta` to the open thinking message.
320    pub fn new(delta: impl Into<String>) -> Self {
321        Self {
322            base: BaseEvent::default(),
323            delta: delta.into(),
324        }
325    }
326}
327
328/// Closes a thinking text message.
329#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
330#[serde(rename_all = "camelCase")]
331#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
332#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
333#[cfg_attr(
334    not(feature = "utoipa"),
335    deprecated(note = "use the REASONING_* events: ReasoningMessageEndEvent")
336)]
337pub struct ThinkingTextMessageEndEvent {
338    /// Timestamp and raw provider event.
339    #[serde(flatten)]
340    pub base: BaseEvent,
341}