Skip to main content

gproxy_protocol/openai/generate_content/responses/stream/
mod.rs

1mod events;
2mod payloads;
3
4use serde::{Deserialize, Serialize, de};
5use serde_json::Value;
6
7use crate::openai::common::{ResponseStreamEventType, ResponseStreamEventTypeKnown, Rest};
8
9pub use events::*;
10pub use payloads::*;
11
12#[derive(Debug, Clone, PartialEq)]
13#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
14pub enum ResponseStreamEvent {
15    Known(Box<KnownResponseStreamEvent>),
16    Unknown(UnknownResponseStreamEvent),
17}
18
19impl ResponseStreamEvent {
20    pub fn event_name(&self) -> Option<&str> {
21        match self {
22            Self::Known(event) => Some(event.event_name()),
23            Self::Unknown(event) => event.type_.as_ref().map(ResponseStreamEventType::as_str),
24        }
25    }
26}
27
28impl Serialize for ResponseStreamEvent {
29    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
30        match self {
31            Self::Known(event) => event.serialize(serializer),
32            Self::Unknown(event) => event.serialize(serializer),
33        }
34    }
35}
36
37impl<'de> Deserialize<'de> for ResponseStreamEvent {
38    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
39        let value = Value::deserialize(deserializer)?;
40        let Some(type_name) = value.get("type").and_then(Value::as_str) else {
41            return serde_json::from_value(value)
42                .map(Self::Unknown)
43                .map_err(de::Error::custom);
44        };
45        let event_type =
46            serde_json::from_value::<ResponseStreamEventType>(Value::String(type_name.to_owned()))
47                .map_err(de::Error::custom)?;
48        match event_type {
49            ResponseStreamEventType::Known(_) => serde_json::from_value(value)
50                .map(Box::new)
51                .map(Self::Known)
52                .map_err(de::Error::custom),
53            ResponseStreamEventType::Unknown(_) => serde_json::from_value(value)
54                .map(Self::Unknown)
55                .map_err(de::Error::custom),
56        }
57    }
58}
59
60#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
61#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
62pub struct UnknownResponseStreamEvent {
63    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
64    pub type_: Option<ResponseStreamEventType>,
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub sequence_number: Option<u64>,
67    #[serde(default, flatten)]
68    pub rest: Rest,
69}
70
71impl KnownResponseStreamEvent {
72    pub fn event_type(&self) -> ResponseStreamEventTypeKnown {
73        use ResponseStreamEventTypeKnown as T;
74        match self {
75            Self::ResponseCreated(_) => T::ResponseCreated,
76            Self::ResponseInProgress(_) => T::ResponseInProgress,
77            Self::ResponseCompleted(_) => T::ResponseCompleted,
78            Self::ResponseFailed(_) => T::ResponseFailed,
79            Self::ResponseIncomplete(_) => T::ResponseIncomplete,
80            Self::ResponseQueued(_) => T::ResponseQueued,
81            Self::ResponseInjectCreated(_) => T::ResponseInjectCreated,
82            Self::ResponseInjectFailed(_) => T::ResponseInjectFailed,
83            Self::ResponseSteerAccepted(_) => T::ResponseSteerAccepted,
84            Self::ResponseSteerPending(_) => T::ResponseSteerPending,
85            Self::ResponseSteerFailed(_) => T::ResponseSteerFailed,
86            Self::ResponseOutputItemAdded(_) => T::ResponseOutputItemAdded,
87            Self::ResponseOutputItemDone(_) => T::ResponseOutputItemDone,
88            Self::ResponseContentPartAdded(_) => T::ResponseContentPartAdded,
89            Self::ResponseContentPartDone(_) => T::ResponseContentPartDone,
90            Self::ResponseOutputTextDelta(_) => T::ResponseOutputTextDelta,
91            Self::ResponseOutputTextDone(_) => T::ResponseOutputTextDone,
92            Self::ResponseOutputTextAnnotationAdded(_) => T::ResponseOutputTextAnnotationAdded,
93            Self::ResponseFunctionCallArgumentsDelta(_) => T::ResponseFunctionCallArgumentsDelta,
94            Self::ResponseFunctionCallArgumentsDone(_) => T::ResponseFunctionCallArgumentsDone,
95            Self::ResponseCustomToolCallInputDelta(_) => T::ResponseCustomToolCallInputDelta,
96            Self::ResponseCustomToolCallInputDone(_) => T::ResponseCustomToolCallInputDone,
97            Self::ResponseRefusalDelta(_) => T::ResponseRefusalDelta,
98            Self::ResponseRefusalDone(_) => T::ResponseRefusalDone,
99            Self::ResponseReasoningSummaryPartAdded(_) => T::ResponseReasoningSummaryPartAdded,
100            Self::ResponseReasoningSummaryPartDone(_) => T::ResponseReasoningSummaryPartDone,
101            Self::ResponseReasoningSummaryTextDelta(_) => T::ResponseReasoningSummaryTextDelta,
102            Self::ResponseReasoningSummaryTextDone(_) => T::ResponseReasoningSummaryTextDone,
103            Self::ResponseReasoningTextDelta(_) => T::ResponseReasoningTextDelta,
104            Self::ResponseReasoningTextDone(_) => T::ResponseReasoningTextDone,
105            Self::ResponseAudioDelta(_) => T::ResponseAudioDelta,
106            Self::ResponseAudioDone(_) => T::ResponseAudioDone,
107            Self::ResponseAudioTranscriptDelta(_) => T::ResponseAudioTranscriptDelta,
108            Self::ResponseAudioTranscriptDone(_) => T::ResponseAudioTranscriptDone,
109            Self::ResponseImageGenerationCallCompleted(_) => {
110                T::ResponseImageGenerationCallCompleted
111            }
112            Self::ResponseImageGenerationCallGenerating(_) => {
113                T::ResponseImageGenerationCallGenerating
114            }
115            Self::ResponseImageGenerationCallInProgress(_) => {
116                T::ResponseImageGenerationCallInProgress
117            }
118            Self::ResponseImageGenerationCallPartialImage(_) => {
119                T::ResponseImageGenerationCallPartialImage
120            }
121            Self::ResponseFileSearchCallInProgress(_) => T::ResponseFileSearchCallInProgress,
122            Self::ResponseFileSearchCallSearching(_) => T::ResponseFileSearchCallSearching,
123            Self::ResponseFileSearchCallCompleted(_) => T::ResponseFileSearchCallCompleted,
124            Self::ResponseWebSearchCallInProgress(_) => T::ResponseWebSearchCallInProgress,
125            Self::ResponseWebSearchCallSearching(_) => T::ResponseWebSearchCallSearching,
126            Self::ResponseWebSearchCallCompleted(_) => T::ResponseWebSearchCallCompleted,
127            Self::ResponseCodeInterpreterCallInProgress(_) => {
128                T::ResponseCodeInterpreterCallInProgress
129            }
130            Self::ResponseCodeInterpreterCallInterpreting(_) => {
131                T::ResponseCodeInterpreterCallInterpreting
132            }
133            Self::ResponseCodeInterpreterCallCompleted(_) => {
134                T::ResponseCodeInterpreterCallCompleted
135            }
136            Self::ResponseCodeInterpreterCallCodeDelta(_) => {
137                T::ResponseCodeInterpreterCallCodeDelta
138            }
139            Self::ResponseCodeInterpreterCallCodeDone(_) => T::ResponseCodeInterpreterCallCodeDone,
140            Self::ResponseMcpCallArgumentsDelta(_) => T::ResponseMcpCallArgumentsDelta,
141            Self::ResponseMcpCallArgumentsDone(_) => T::ResponseMcpCallArgumentsDone,
142            Self::ResponseMcpCallInProgress(_) => T::ResponseMcpCallInProgress,
143            Self::ResponseMcpCallCompleted(_) => T::ResponseMcpCallCompleted,
144            Self::ResponseMcpCallFailed(_) => T::ResponseMcpCallFailed,
145            Self::ResponseMcpListToolsInProgress(_) => T::ResponseMcpListToolsInProgress,
146            Self::ResponseMcpListToolsCompleted(_) => T::ResponseMcpListToolsCompleted,
147            Self::ResponseMcpListToolsFailed(_) => T::ResponseMcpListToolsFailed,
148            Self::Error(_) => T::Error,
149        }
150    }
151
152    pub fn event_name(&self) -> &'static str {
153        self.event_type().as_str()
154    }
155}