Skip to main content

gproxy_protocol/wire/openai/responses/
stream.rs

1//! HTTP Responses SSE JSON events, Responses.md:125798-136308.
2//! AnnotationAdded.annotation is explicitly `unknown` (136212), hence Value.
3use super::input::present_optional;
4use super::response::{GenerateContentResponseBody, ResponseOutputItem};
5use crate::Rest;
6use serde::{Deserialize, Serialize};
7
8/// Typed payloads for the HTTP Responses SSE stream. SSE framing and `[DONE]`
9/// handling remain transport concerns; this enum only represents JSON data.
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11#[serde(tag = "type")]
12#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
13#[derive(gproxy_protocol_macros::DeclaredFields)]
14#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
15pub enum StreamEvent {
16    /// Undocumented upstream heartbeat; converted to Claude ping or otherwise ignored.
17    #[serde(rename = "keepalive")]
18    Keepalive,
19    /// Out-of-band response metadata emitted by the Codex Responses backend.
20    #[serde(rename = "response.metadata")]
21    Metadata(ResponseMetadata),
22    #[serde(rename = "response.created")]
23    Created(ResponseCreated),
24    #[serde(rename = "response.queued")]
25    Queued(ResponseQueued),
26    #[serde(rename = "response.in_progress")]
27    InProgress(ResponseInProgress),
28    #[serde(rename = "response.completed")]
29    Completed(ResponseCompleted),
30    #[serde(rename = "response.failed")]
31    Failed(ResponseFailed),
32    #[serde(rename = "response.incomplete")]
33    Incomplete(ResponseIncomplete),
34    #[serde(rename = "response.output_item.added")]
35    OutputItemAdded(OutputItemEvent),
36    #[serde(rename = "response.output_item.done")]
37    OutputItemDone(OutputItemEvent),
38    #[serde(rename = "response.content_part.added")]
39    ContentPartAdded(ContentPartEvent),
40    #[serde(rename = "response.content_part.done")]
41    ContentPartDone(ContentPartEvent),
42    #[serde(rename = "response.output_text.delta")]
43    OutputTextDelta(OutputTextDelta),
44    #[serde(rename = "response.output_text.done")]
45    OutputTextDone(OutputTextDone),
46    #[serde(rename = "response.output_text.annotation.added")]
47    OutputTextAnnotationAdded(OutputTextAnnotationAdded),
48    #[serde(rename = "response.refusal.delta")]
49    RefusalDelta(RefusalDelta),
50    #[serde(rename = "response.refusal.done")]
51    RefusalDone(RefusalDone),
52    #[serde(rename = "response.reasoning_text.delta")]
53    ReasoningTextDelta(ReasoningTextDelta),
54    #[serde(rename = "response.reasoning_text.done")]
55    ReasoningTextDone(ReasoningTextDone),
56    #[serde(rename = "response.reasoning_summary_part.added")]
57    ReasoningSummaryPartAdded(ReasoningSummaryPartAddedEvent),
58    #[serde(rename = "response.reasoning_summary_part.done")]
59    ReasoningSummaryPartDone(ReasoningSummaryPartDoneEvent),
60    #[serde(rename = "response.reasoning_summary_text.delta")]
61    ReasoningSummaryTextDelta(ReasoningSummaryTextDelta),
62    #[serde(rename = "response.reasoning_summary_text.done")]
63    ReasoningSummaryTextDone(ReasoningSummaryTextDone),
64    #[serde(rename = "response.function_call_arguments.delta")]
65    FunctionCallArgumentsDelta(FunctionCallArgumentsDelta),
66    #[serde(rename = "response.function_call_arguments.done")]
67    FunctionCallArgumentsDone(FunctionCallArgumentsDone),
68    #[serde(rename = "response.custom_tool_call_input.delta")]
69    CustomToolInputDelta(CustomToolInputDelta),
70    #[serde(rename = "response.custom_tool_call_input.done")]
71    CustomToolInputDone(CustomToolInputDone),
72    #[serde(rename = "response.code_interpreter_call_code.delta")]
73    CodeInterpreterCodeDelta(CodeInterpreterCodeDelta),
74    #[serde(rename = "response.code_interpreter_call_code.done")]
75    CodeInterpreterCodeDone(CodeInterpreterCodeDone),
76    #[serde(rename = "response.audio.delta")]
77    AudioDelta(AudioDelta),
78    #[serde(rename = "response.audio.done")]
79    AudioDone(AudioDone),
80    #[serde(rename = "response.audio.transcript.delta")]
81    AudioTranscriptDelta(AudioTranscriptDelta),
82    #[serde(rename = "response.audio.transcript.done")]
83    AudioTranscriptDone(AudioTranscriptDone),
84    #[serde(rename = "response.image_generation_call.partial_image")]
85    ImagePartial(ImagePartial),
86    #[serde(rename = "response.image_generation_call.in_progress")]
87    ImageCall(ImageCallEvent),
88    #[serde(rename = "response.image_generation_call.generating")]
89    ImageGenerating(ImageCallEvent),
90    #[serde(rename = "response.image_generation_call.completed")]
91    ImageCompleted(ImageCallEvent),
92    #[serde(rename = "response.code_interpreter_call.in_progress")]
93    CodeInterpreterInProgress(CodeInterpreterEvent),
94    #[serde(rename = "response.code_interpreter_call.interpreting")]
95    CodeInterpreterInterpreting(CodeInterpreterEvent),
96    #[serde(rename = "response.code_interpreter_call.completed")]
97    CodeInterpreterCompleted(CodeInterpreterEvent),
98    #[serde(rename = "response.file_search_call.in_progress")]
99    FileSearchInProgress(ToolCallEvent),
100    #[serde(rename = "response.file_search_call.searching")]
101    FileSearchSearching(ToolCallEvent),
102    #[serde(rename = "response.file_search_call.completed")]
103    FileSearchCompleted(ToolCallEvent),
104    #[serde(rename = "response.web_search_call.in_progress")]
105    WebSearchInProgress(ToolCallEvent),
106    #[serde(rename = "response.web_search_call.searching")]
107    WebSearchSearching(ToolCallEvent),
108    #[serde(rename = "response.web_search_call.completed")]
109    WebSearchCompleted(ToolCallEvent),
110    #[serde(rename = "response.mcp_call_arguments.delta")]
111    McpArgumentsDelta(McpArgumentsDelta),
112    #[serde(rename = "response.mcp_call_arguments.done")]
113    McpArgumentsDone(McpArgumentsDone),
114    #[serde(rename = "response.mcp_call.in_progress")]
115    McpInProgress(McpCallEvent),
116    #[serde(rename = "response.mcp_call.completed")]
117    McpCompleted(McpCallEvent),
118    #[serde(rename = "response.mcp_call.failed")]
119    McpFailed(McpCallEvent),
120    #[serde(rename = "response.mcp_list_tools.in_progress")]
121    McpListToolsInProgress(McpListToolsEvent),
122    #[serde(rename = "response.mcp_list_tools.completed")]
123    McpListToolsCompleted(McpListToolsEvent),
124    #[serde(rename = "response.mcp_list_tools.failed")]
125    McpListToolsFailed(McpListToolsEvent),
126    #[serde(rename = "error")]
127    Error(ResponseStreamError),
128    /// A future event type is ignored by converters. Known event payloads remain strict.
129    /// Native passthrough retains the original bytes rather than this marker.
130    #[serde(other, rename = "unknown")]
131    Unknown,
132}
133/// Codex turn headers and opaque moderation/verification or buffering metadata.
134/// These are stream notifications, distinct from user-supplied Response.metadata.
135#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
136#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
137#[derive(gproxy_protocol_macros::DeclaredFields)]
138#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
139pub struct ResponseMetadata {
140    #[serde(default, skip_serializing_if = "Option::is_none")]
141    pub sequence_number: Option<i64>,
142    #[serde(default, skip_serializing_if = "Option::is_none")]
143    pub response_id: Option<String>,
144    #[serde(
145        default,
146        deserialize_with = "present_optional",
147        skip_serializing_if = "Option::is_none"
148    )]
149    pub metadata: Option<serde_json::Value>,
150    #[serde(
151        default,
152        deserialize_with = "present_optional",
153        skip_serializing_if = "Option::is_none"
154    )]
155    pub headers: Option<serde_json::Value>,
156    #[serde(
157        default,
158        deserialize_with = "present_optional",
159        skip_serializing_if = "Option::is_none"
160    )]
161    pub safety_buffering: Option<serde_json::Value>,
162    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
163    pub rest: Rest,
164}
165
166macro_rules! event_struct { ($name:ident { $($field:ident : $ty:ty),* $(,)? }) => { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)] #[serde(rename_all="snake_case")] #[cfg_attr(not(feature="exhaustive"), non_exhaustive)] #[derive(gproxy_protocol_macros::DeclaredFields)]
167#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
168pub struct $name { pub sequence_number: i64, $(pub $field: $ty,)* #[serde(default, flatten, skip_serializing_if="serde_json::Map::is_empty")] pub rest: Rest } }; }
169macro_rules! agent_event_struct { ($name:ident { $($field:ident : $ty:ty),* $(,)? }) => { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)] #[serde(rename_all="snake_case")] #[cfg_attr(not(feature="exhaustive"), non_exhaustive)] #[derive(gproxy_protocol_macros::DeclaredFields)]
170#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
171pub struct $name { #[serde(default, deserialize_with = "super::input::present_nullable", skip_serializing_if = "Option::is_none")] pub agent: Option<Option<super::multi_agent::Agent>>, pub sequence_number: i64, $(pub $field: $ty,)* #[serde(default, flatten, skip_serializing_if="serde_json::Map::is_empty")] pub rest: Rest } }; }
172event_struct!(ResponseCreated {
173    response: GenerateContentResponseBody
174});
175event_struct!(ResponseQueued {
176    response: GenerateContentResponseBody
177});
178event_struct!(ResponseInProgress {
179    response: GenerateContentResponseBody
180});
181event_struct!(ResponseCompleted {
182    response: GenerateContentResponseBody
183});
184event_struct!(ResponseFailed {
185    response: GenerateContentResponseBody
186});
187event_struct!(ResponseIncomplete {
188    response: GenerateContentResponseBody
189});
190agent_event_struct!(OutputItemEvent {
191    output_index: i64,
192    item: ResponseOutputItem
193});
194agent_event_struct!(ContentPartEvent {
195    item_id: String,
196    output_index: i64,
197    content_index: i64,
198    part: OutputContentPart
199});
200agent_event_struct!(OutputTextDelta { item_id: String, output_index: i64, content_index: i64, delta: String, logprobs: Vec<StreamLogprob> });
201agent_event_struct!(OutputTextDone { item_id: String, output_index: i64, content_index: i64, text: String, logprobs: Vec<StreamLogprob> });
202agent_event_struct!(OutputTextAnnotationAdded {
203    item_id: String,
204    output_index: i64,
205    content_index: i64,
206    annotation_index: i64,
207    annotation: serde_json::Value
208});
209agent_event_struct!(RefusalDelta {
210    item_id: String,
211    output_index: i64,
212    content_index: i64,
213    delta: String
214});
215agent_event_struct!(RefusalDone {
216    item_id: String,
217    output_index: i64,
218    content_index: i64,
219    refusal: String
220});
221agent_event_struct!(ReasoningTextDelta {
222    item_id: String,
223    output_index: i64,
224    content_index: i64,
225    delta: String
226});
227agent_event_struct!(ReasoningTextDone {
228    item_id: String,
229    output_index: i64,
230    content_index: i64,
231    text: String
232});
233agent_event_struct!(ReasoningSummaryPartAddedEvent {
234    item_id: String,
235    output_index: i64,
236    summary_index: i64,
237    part: ReasoningSummaryPart
238});
239#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
240#[serde(rename_all = "snake_case")]
241#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
242#[derive(gproxy_protocol_macros::DeclaredFields)]
243#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
244pub struct ReasoningSummaryPartDoneEvent {
245    #[serde(
246        default,
247        deserialize_with = "super::input::present_nullable",
248        skip_serializing_if = "Option::is_none"
249    )]
250    pub agent: Option<Option<super::multi_agent::Agent>>,
251    pub sequence_number: i64,
252    pub item_id: String,
253    pub output_index: i64,
254    pub summary_index: i64,
255    pub part: ReasoningSummaryPart,
256    #[serde(
257        default,
258        deserialize_with = "present_optional",
259        skip_serializing_if = "Option::is_none"
260    )]
261    pub status: Option<SummaryPartStatus>,
262    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
263    pub rest: Rest,
264}
265#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
266#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
267#[derive(gproxy_protocol_macros::DeclaredFields)]
268#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
269pub enum SummaryPartStatus {
270    #[serde(rename = "incomplete")]
271    Incomplete,
272}
273agent_event_struct!(ReasoningSummaryTextDelta {
274    item_id: String,
275    output_index: i64,
276    summary_index: i64,
277    delta: String
278});
279agent_event_struct!(ReasoningSummaryTextDone {
280    item_id: String,
281    output_index: i64,
282    summary_index: i64,
283    text: String
284});
285agent_event_struct!(FunctionCallArgumentsDelta {
286    item_id: String,
287    output_index: i64,
288    delta: String
289});
290agent_event_struct!(FunctionCallArgumentsDone {
291    item_id: String,
292    output_index: i64,
293    name: String,
294    arguments: String
295});
296agent_event_struct!(CustomToolInputDelta {
297    item_id: String,
298    output_index: i64,
299    delta: String
300});
301agent_event_struct!(CustomToolInputDone {
302    item_id: String,
303    output_index: i64,
304    input: String
305});
306agent_event_struct!(CodeInterpreterCodeDelta {
307    item_id: String,
308    output_index: i64,
309    delta: String
310});
311agent_event_struct!(CodeInterpreterCodeDone {
312    item_id: String,
313    output_index: i64,
314    code: String
315});
316agent_event_struct!(AudioDelta { delta: String });
317agent_event_struct!(AudioDone {});
318agent_event_struct!(AudioTranscriptDelta { delta: String });
319agent_event_struct!(AudioTranscriptDone {});
320agent_event_struct!(ImagePartial {
321    item_id: String,
322    output_index: i64,
323    partial_image_index: i64,
324    partial_image_b64: String
325});
326agent_event_struct!(ImageCall {
327    item_id: String,
328    output_index: i64
329});
330pub type ImageCallEvent = ImageCall;
331agent_event_struct!(CodeInterpreterEvent {
332    item_id: String,
333    output_index: i64
334});
335agent_event_struct!(ToolCallEvent {
336    item_id: String,
337    output_index: i64
338});
339agent_event_struct!(McpArgumentsDelta {
340    item_id: String,
341    output_index: i64,
342    delta: String
343});
344agent_event_struct!(McpArgumentsDone {
345    item_id: String,
346    output_index: i64,
347    arguments: String
348});
349agent_event_struct!(McpCallEvent {
350    item_id: String,
351    output_index: i64
352});
353agent_event_struct!(McpListToolsEvent {
354    item_id: String,
355    output_index: i64
356});
357pub type ResponseStream = crate::WireResponse<crate::connection::ByteStream>;
358
359#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
360#[serde(untagged)]
361#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
362#[derive(gproxy_protocol_macros::DeclaredFields)]
363#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
364pub enum OutputContentPart {
365    Text(super::input::ResponseOutputText),
366    Refusal(super::input::ResponseOutputRefusal),
367    Reasoning(ReasoningText),
368}
369#[derive(
370    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
371)]
372#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
373#[derive(gproxy_protocol_macros::DeclaredFields)]
374#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
375pub struct ReasoningText {
376    pub text: String,
377    #[serde(rename = "type")]
378    pub type_: ReasoningTextType,
379    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
380    pub rest: Rest,
381}
382#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
383#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
384#[derive(gproxy_protocol_macros::DeclaredFields)]
385#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
386pub enum ReasoningTextType {
387    #[serde(rename = "reasoning_text")]
388    ReasoningText,
389}
390#[derive(
391    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
392)]
393#[serde(rename_all = "snake_case")]
394#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
395#[derive(gproxy_protocol_macros::DeclaredFields)]
396#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
397pub struct StreamLogprob {
398    pub token: String,
399    pub logprob: serde_json::Number,
400    #[serde(
401        default,
402        deserialize_with = "present_optional",
403        skip_serializing_if = "Option::is_none"
404    )]
405    pub top_logprobs: Option<Vec<StreamTopLogprob>>,
406    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
407    pub rest: Rest,
408}
409#[derive(
410    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
411)]
412#[serde(rename_all = "snake_case")]
413#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
414#[derive(gproxy_protocol_macros::DeclaredFields)]
415#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
416pub struct StreamTopLogprob {
417    #[serde(
418        default,
419        deserialize_with = "present_optional",
420        skip_serializing_if = "Option::is_none"
421    )]
422    pub token: Option<String>,
423    #[serde(
424        default,
425        deserialize_with = "present_optional",
426        skip_serializing_if = "Option::is_none"
427    )]
428    pub logprob: Option<serde_json::Number>,
429    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
430    pub rest: Rest,
431}
432#[derive(
433    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
434)]
435#[serde(rename_all = "snake_case")]
436#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
437#[derive(gproxy_protocol_macros::DeclaredFields)]
438#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
439pub struct ReasoningSummaryPart {
440    pub text: String,
441    #[serde(rename = "type")]
442    pub type_: ReasoningSummaryPartType,
443    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
444    pub rest: Rest,
445}
446#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
447#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
448#[derive(gproxy_protocol_macros::DeclaredFields)]
449#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
450pub enum ReasoningSummaryPartType {
451    #[serde(rename = "summary_text")]
452    SummaryText,
453}
454
455/// HTTP Responses uses flat errors; the Codex backend also emits nested errors.
456#[derive(Debug, Clone, PartialEq, Eq, Serialize, gproxy_protocol_macros::DeclaredFields)]
457#[serde(untagged)]
458#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
459#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
460pub enum ResponseStreamError {
461    Flat(ResponseErrorEvent),
462    Nested(ResponseNestedErrorEvent),
463}
464
465impl<'de> Deserialize<'de> for ResponseStreamError {
466    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
467        let value = serde_json::Value::deserialize(deserializer)?;
468        // A malformed nested error must not fall back to flat fields and lose
469        // the upstream failure as an unknown extension.
470        if value.get("error").is_some() {
471            serde_json::from_value(value).map(Self::Nested)
472        } else {
473            serde_json::from_value(value).map(Self::Flat)
474        }
475        .map_err(serde::de::Error::custom)
476    }
477}
478
479impl From<ResponseErrorEvent> for ResponseStreamError {
480    fn from(event: ResponseErrorEvent) -> Self {
481        Self::Flat(event)
482    }
483}
484
485impl ResponseStreamError {
486    pub fn sequence_number(&self) -> i64 {
487        match self {
488            Self::Flat(event) => event.sequence_number,
489            Self::Nested(event) => event.sequence_number,
490        }
491    }
492}
493
494#[derive(
495    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::DeclaredFields,
496)]
497#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
498#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
499pub struct ResponseNestedErrorEvent {
500    pub sequence_number: i64,
501    pub error: super::websocket::ErrorDetail,
502    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
503    pub rest: Rest,
504}
505#[derive(
506    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
507)]
508#[serde(rename_all = "snake_case")]
509#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
510#[derive(gproxy_protocol_macros::DeclaredFields)]
511#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
512pub struct ResponseErrorEvent {
513    #[wire(required)]
514    #[cfg_attr(feature = "schema", schemars(extend("x-gproxy-required" = true)))]
515    #[serde(deserialize_with = "super::input::required_nullable")]
516    pub code: Option<String>,
517    pub message: String,
518    #[wire(required)]
519    #[cfg_attr(feature = "schema", schemars(extend("x-gproxy-required" = true)))]
520    #[serde(deserialize_with = "super::input::required_nullable")]
521    pub param: Option<String>,
522    pub sequence_number: i64,
523    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
524    pub rest: Rest,
525}
526
527impl StreamEvent {
528    pub fn agent(&self) -> Option<&super::multi_agent::Agent> {
529        match self {
530            Self::OutputItemAdded(v) => v.agent.as_ref().and_then(Option::as_ref),
531            Self::OutputItemDone(v) => v.agent.as_ref().and_then(Option::as_ref),
532            Self::ContentPartAdded(v) => v.agent.as_ref().and_then(Option::as_ref),
533            Self::ContentPartDone(v) => v.agent.as_ref().and_then(Option::as_ref),
534            Self::OutputTextDelta(v) => v.agent.as_ref().and_then(Option::as_ref),
535            Self::OutputTextDone(v) => v.agent.as_ref().and_then(Option::as_ref),
536            Self::OutputTextAnnotationAdded(v) => v.agent.as_ref().and_then(Option::as_ref),
537            Self::RefusalDelta(v) => v.agent.as_ref().and_then(Option::as_ref),
538            Self::RefusalDone(v) => v.agent.as_ref().and_then(Option::as_ref),
539            Self::ReasoningTextDelta(v) => v.agent.as_ref().and_then(Option::as_ref),
540            Self::ReasoningTextDone(v) => v.agent.as_ref().and_then(Option::as_ref),
541            Self::ReasoningSummaryPartAdded(v) => v.agent.as_ref().and_then(Option::as_ref),
542            Self::ReasoningSummaryPartDone(v) => v.agent.as_ref().and_then(Option::as_ref),
543            Self::ReasoningSummaryTextDelta(v) => v.agent.as_ref().and_then(Option::as_ref),
544            Self::ReasoningSummaryTextDone(v) => v.agent.as_ref().and_then(Option::as_ref),
545            Self::FunctionCallArgumentsDelta(v) => v.agent.as_ref().and_then(Option::as_ref),
546            Self::FunctionCallArgumentsDone(v) => v.agent.as_ref().and_then(Option::as_ref),
547            Self::CustomToolInputDelta(v) => v.agent.as_ref().and_then(Option::as_ref),
548            Self::CustomToolInputDone(v) => v.agent.as_ref().and_then(Option::as_ref),
549            Self::CodeInterpreterCodeDelta(v) => v.agent.as_ref().and_then(Option::as_ref),
550            Self::CodeInterpreterCodeDone(v) => v.agent.as_ref().and_then(Option::as_ref),
551            Self::AudioDelta(v) => v.agent.as_ref().and_then(Option::as_ref),
552            Self::AudioDone(v) => v.agent.as_ref().and_then(Option::as_ref),
553            Self::AudioTranscriptDelta(v) => v.agent.as_ref().and_then(Option::as_ref),
554            Self::AudioTranscriptDone(v) => v.agent.as_ref().and_then(Option::as_ref),
555            Self::ImagePartial(v) => v.agent.as_ref().and_then(Option::as_ref),
556            Self::ImageCall(v) => v.agent.as_ref().and_then(Option::as_ref),
557            Self::ImageGenerating(v) => v.agent.as_ref().and_then(Option::as_ref),
558            Self::ImageCompleted(v) => v.agent.as_ref().and_then(Option::as_ref),
559            Self::CodeInterpreterInProgress(v) => v.agent.as_ref().and_then(Option::as_ref),
560            Self::CodeInterpreterInterpreting(v) => v.agent.as_ref().and_then(Option::as_ref),
561            Self::CodeInterpreterCompleted(v) => v.agent.as_ref().and_then(Option::as_ref),
562            Self::FileSearchInProgress(v) => v.agent.as_ref().and_then(Option::as_ref),
563            Self::FileSearchSearching(v) => v.agent.as_ref().and_then(Option::as_ref),
564            Self::FileSearchCompleted(v) => v.agent.as_ref().and_then(Option::as_ref),
565            Self::WebSearchInProgress(v) => v.agent.as_ref().and_then(Option::as_ref),
566            Self::WebSearchSearching(v) => v.agent.as_ref().and_then(Option::as_ref),
567            Self::WebSearchCompleted(v) => v.agent.as_ref().and_then(Option::as_ref),
568            Self::McpArgumentsDelta(v) => v.agent.as_ref().and_then(Option::as_ref),
569            Self::McpArgumentsDone(v) => v.agent.as_ref().and_then(Option::as_ref),
570            Self::McpInProgress(v) => v.agent.as_ref().and_then(Option::as_ref),
571            Self::McpCompleted(v) => v.agent.as_ref().and_then(Option::as_ref),
572            Self::McpFailed(v) => v.agent.as_ref().and_then(Option::as_ref),
573            Self::McpListToolsInProgress(v) => v.agent.as_ref().and_then(Option::as_ref),
574            Self::McpListToolsCompleted(v) => v.agent.as_ref().and_then(Option::as_ref),
575            Self::McpListToolsFailed(v) => v.agent.as_ref().and_then(Option::as_ref),
576            _ => None,
577        }
578    }
579    pub(crate) fn set_agent(&mut self, agent: Option<super::multi_agent::Agent>) {
580        match self {
581            Self::OutputItemAdded(v) => v.agent = agent.map(Some),
582            Self::OutputItemDone(v) => v.agent = agent.map(Some),
583            Self::ContentPartAdded(v) => v.agent = agent.map(Some),
584            Self::ContentPartDone(v) => v.agent = agent.map(Some),
585            Self::OutputTextDelta(v) => v.agent = agent.map(Some),
586            Self::OutputTextDone(v) => v.agent = agent.map(Some),
587            Self::OutputTextAnnotationAdded(v) => v.agent = agent.map(Some),
588            Self::RefusalDelta(v) => v.agent = agent.map(Some),
589            Self::RefusalDone(v) => v.agent = agent.map(Some),
590            Self::ReasoningTextDelta(v) => v.agent = agent.map(Some),
591            Self::ReasoningTextDone(v) => v.agent = agent.map(Some),
592            Self::ReasoningSummaryPartAdded(v) => v.agent = agent.map(Some),
593            Self::ReasoningSummaryPartDone(v) => v.agent = agent.map(Some),
594            Self::ReasoningSummaryTextDelta(v) => v.agent = agent.map(Some),
595            Self::ReasoningSummaryTextDone(v) => v.agent = agent.map(Some),
596            Self::FunctionCallArgumentsDelta(v) => v.agent = agent.map(Some),
597            Self::FunctionCallArgumentsDone(v) => v.agent = agent.map(Some),
598            Self::CustomToolInputDelta(v) => v.agent = agent.map(Some),
599            Self::CustomToolInputDone(v) => v.agent = agent.map(Some),
600            Self::CodeInterpreterCodeDelta(v) => v.agent = agent.map(Some),
601            Self::CodeInterpreterCodeDone(v) => v.agent = agent.map(Some),
602            Self::AudioDelta(v) => v.agent = agent.map(Some),
603            Self::AudioDone(v) => v.agent = agent.map(Some),
604            Self::AudioTranscriptDelta(v) => v.agent = agent.map(Some),
605            Self::AudioTranscriptDone(v) => v.agent = agent.map(Some),
606            Self::ImagePartial(v) => v.agent = agent.map(Some),
607            Self::ImageCall(v) => v.agent = agent.map(Some),
608            Self::ImageGenerating(v) => v.agent = agent.map(Some),
609            Self::ImageCompleted(v) => v.agent = agent.map(Some),
610            Self::CodeInterpreterInProgress(v) => v.agent = agent.map(Some),
611            Self::CodeInterpreterInterpreting(v) => v.agent = agent.map(Some),
612            Self::CodeInterpreterCompleted(v) => v.agent = agent.map(Some),
613            Self::FileSearchInProgress(v) => v.agent = agent.map(Some),
614            Self::FileSearchSearching(v) => v.agent = agent.map(Some),
615            Self::FileSearchCompleted(v) => v.agent = agent.map(Some),
616            Self::WebSearchInProgress(v) => v.agent = agent.map(Some),
617            Self::WebSearchSearching(v) => v.agent = agent.map(Some),
618            Self::WebSearchCompleted(v) => v.agent = agent.map(Some),
619            Self::McpArgumentsDelta(v) => v.agent = agent.map(Some),
620            Self::McpArgumentsDone(v) => v.agent = agent.map(Some),
621            Self::McpInProgress(v) => v.agent = agent.map(Some),
622            Self::McpCompleted(v) => v.agent = agent.map(Some),
623            Self::McpFailed(v) => v.agent = agent.map(Some),
624            Self::McpListToolsInProgress(v) => v.agent = agent.map(Some),
625            Self::McpListToolsCompleted(v) => v.agent = agent.map(Some),
626            Self::McpListToolsFailed(v) => v.agent = agent.map(Some),
627            _ => {}
628        }
629    }
630}
631
632impl StreamEvent {
633    pub(crate) fn output_index(&self) -> Option<i64> {
634        match self {
635            Self::OutputItemAdded(v) => Some(v.output_index),
636            Self::OutputItemDone(v) => Some(v.output_index),
637            Self::ContentPartAdded(v) => Some(v.output_index),
638            Self::ContentPartDone(v) => Some(v.output_index),
639            Self::OutputTextDelta(v) => Some(v.output_index),
640            Self::OutputTextDone(v) => Some(v.output_index),
641            Self::OutputTextAnnotationAdded(v) => Some(v.output_index),
642            Self::RefusalDelta(v) => Some(v.output_index),
643            Self::RefusalDone(v) => Some(v.output_index),
644            Self::ReasoningTextDelta(v) => Some(v.output_index),
645            Self::ReasoningTextDone(v) => Some(v.output_index),
646            Self::ReasoningSummaryPartAdded(v) => Some(v.output_index),
647            Self::ReasoningSummaryPartDone(v) => Some(v.output_index),
648            Self::ReasoningSummaryTextDelta(v) => Some(v.output_index),
649            Self::ReasoningSummaryTextDone(v) => Some(v.output_index),
650            Self::FunctionCallArgumentsDelta(v) => Some(v.output_index),
651            Self::FunctionCallArgumentsDone(v) => Some(v.output_index),
652            Self::CustomToolInputDelta(v) => Some(v.output_index),
653            Self::CustomToolInputDone(v) => Some(v.output_index),
654            Self::CodeInterpreterCodeDelta(v) => Some(v.output_index),
655            Self::CodeInterpreterCodeDone(v) => Some(v.output_index),
656            Self::ImagePartial(v) => Some(v.output_index),
657            Self::ImageCall(v) | Self::ImageGenerating(v) | Self::ImageCompleted(v) => {
658                Some(v.output_index)
659            }
660            Self::CodeInterpreterInProgress(v) => Some(v.output_index),
661            Self::CodeInterpreterInterpreting(v) => Some(v.output_index),
662            Self::CodeInterpreterCompleted(v) => Some(v.output_index),
663            Self::FileSearchInProgress(v) => Some(v.output_index),
664            Self::FileSearchSearching(v) => Some(v.output_index),
665            Self::FileSearchCompleted(v) => Some(v.output_index),
666            Self::WebSearchInProgress(v) => Some(v.output_index),
667            Self::WebSearchSearching(v) => Some(v.output_index),
668            Self::WebSearchCompleted(v) => Some(v.output_index),
669            Self::McpArgumentsDelta(v) => Some(v.output_index),
670            Self::McpArgumentsDone(v) => Some(v.output_index),
671            Self::McpInProgress(v) => Some(v.output_index),
672            Self::McpCompleted(v) => Some(v.output_index),
673            Self::McpFailed(v) => Some(v.output_index),
674            Self::McpListToolsInProgress(v) => Some(v.output_index),
675            Self::McpListToolsCompleted(v) => Some(v.output_index),
676            Self::McpListToolsFailed(v) => Some(v.output_index),
677            _ => None,
678        }
679    }
680}