Skip to main content

gproxy_protocol/wire/claude/
stream.rs

1//! Claude Messages SSE event data; transport framing is deliberately separate.
2//! Sources: Streaming Messages.md; Beta types.md RawMessageStreamEvent and deltas.
3//! Optional/nullability omitted by the rendered Beta table is corroborated by
4//! anthropic-sdk-python types/beta/{beta_message,beta_raw_message_delta_event,
5//! beta_message_delta_usage,beta_compaction_content_block_delta}.py.
6use super::generate_content as response;
7use crate::Rest;
8use serde::{Deserialize, Serialize};
9
10pub type StreamResponse = crate::WireResponse<crate::connection::ByteStream>;
11pub type StreamUsage = response::Usage;
12pub type StreamStopReason = response::StopReason;
13pub type StreamMessageType = response::GenerateContentResponseBodyType;
14fn present_nullable<'de, D, T>(d: D) -> Result<Option<Option<T>>, D::Error>
15where
16    D: serde::Deserializer<'de>,
17    T: Deserialize<'de>,
18{
19    Option::<T>::deserialize(d).map(Some)
20}
21
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23#[serde(tag = "type")]
24#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
25#[derive(gproxy_protocol_macros::DeclaredFields)]
26#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
27pub enum StreamEvent {
28    #[serde(rename = "message_start")]
29    MessageStart(Box<MessageStartEvent>),
30    #[serde(rename = "content_block_start")]
31    ContentBlockStart(ContentBlockStartEvent),
32    #[serde(rename = "content_block_delta")]
33    ContentBlockDelta(ContentBlockDeltaEvent),
34    #[serde(rename = "content_block_stop")]
35    ContentBlockStop(ContentBlockStopEvent),
36    #[serde(rename = "message_delta")]
37    MessageDelta(Box<MessageDeltaEvent>),
38    #[serde(rename = "message_stop")]
39    MessageStop(MessageStopEvent),
40    #[serde(rename = "ping")]
41    Ping(PingEvent),
42    #[serde(rename = "error")]
43    Error(ErrorEvent),
44}
45
46#[derive(
47    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
48)]
49#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
50#[derive(gproxy_protocol_macros::DeclaredFields)]
51#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
52pub struct MessageStartEvent {
53    pub message: StreamMessage,
54    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
55    pub rest: Rest,
56}
57
58#[derive(
59    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
60)]
61#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
62#[derive(gproxy_protocol_macros::DeclaredFields)]
63#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
64pub struct StreamMessage {
65    #[serde(
66        default,
67        deserialize_with = "present_nullable",
68        skip_serializing_if = "Option::is_none"
69    )]
70    pub input_transformations: Option<Option<Vec<response::InputTransformation>>>,
71
72    #[serde(rename = "type")]
73    pub type_: StreamMessageType,
74    pub id: String,
75    #[serde(
76        default,
77        deserialize_with = "present_nullable",
78        skip_serializing_if = "Option::is_none"
79    )]
80    pub container: Option<Option<response::ResponseContainer>>,
81    pub content: Vec<response::ResponseContentBlock>,
82    #[serde(
83        default,
84        deserialize_with = "present_nullable",
85        skip_serializing_if = "Option::is_none"
86    )]
87    pub context_management: Option<Option<response::ContextManagementResponse>>,
88    #[serde(
89        default,
90        deserialize_with = "present_nullable",
91        skip_serializing_if = "Option::is_none"
92    )]
93    pub diagnostics: Option<Option<response::DiagnosticsResponse>>,
94    pub model: String,
95    pub role: response::ResponseRole,
96    #[serde(
97        default,
98        deserialize_with = "present_nullable",
99        skip_serializing_if = "Option::is_none"
100    )]
101    pub stop_details: Option<Option<response::RefusalStopDetails>>,
102    #[serde(
103        default,
104        deserialize_with = "present_nullable",
105        skip_serializing_if = "Option::is_none"
106    )]
107    pub stop_reason: Option<Option<StreamStopReason>>,
108    #[serde(
109        default,
110        deserialize_with = "present_nullable",
111        skip_serializing_if = "Option::is_none"
112    )]
113    pub stop_sequence: Option<Option<String>>,
114    pub usage: StreamUsage,
115    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
116    pub rest: Rest,
117}
118
119#[derive(
120    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
121)]
122#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
123#[derive(gproxy_protocol_macros::DeclaredFields)]
124#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
125pub struct ContentBlockStartEvent {
126    pub index: i64,
127    pub content_block: response::ResponseContentBlock,
128    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
129    pub rest: Rest,
130}
131
132#[derive(
133    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
134)]
135#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
136#[derive(gproxy_protocol_macros::DeclaredFields)]
137#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
138pub struct ContentBlockDeltaEvent {
139    pub index: i64,
140    pub delta: ContentBlockDelta,
141    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
142    pub rest: Rest,
143}
144
145#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
146#[serde(tag = "type")]
147#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
148#[derive(gproxy_protocol_macros::DeclaredFields)]
149#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
150pub enum ContentBlockDelta {
151    #[serde(rename = "text_delta")]
152    Text(TextDelta),
153    #[serde(rename = "input_json_delta")]
154    InputJson(InputJsonDelta),
155    #[serde(rename = "thinking_delta")]
156    Thinking(ThinkingDelta),
157    #[serde(rename = "signature_delta")]
158    Signature(SignatureDelta),
159    #[serde(rename = "citations_delta")]
160    Citations(CitationsDelta),
161    #[serde(rename = "compaction_delta")]
162    Compaction(CompactionDelta),
163}
164
165#[derive(
166    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
167)]
168#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
169#[derive(gproxy_protocol_macros::DeclaredFields)]
170#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
171pub struct TextDelta {
172    pub text: String,
173    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
174    pub rest: Rest,
175}
176
177#[derive(
178    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
179)]
180#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
181#[derive(gproxy_protocol_macros::DeclaredFields)]
182#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
183pub struct InputJsonDelta {
184    pub partial_json: String,
185    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
186    pub rest: Rest,
187}
188
189#[derive(
190    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
191)]
192#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
193#[derive(gproxy_protocol_macros::DeclaredFields)]
194#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
195pub struct ThinkingDelta {
196    pub thinking: String,
197    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
198    pub rest: Rest,
199}
200
201#[derive(
202    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
203)]
204#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
205#[derive(gproxy_protocol_macros::DeclaredFields)]
206#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
207pub struct SignatureDelta {
208    pub signature: String,
209    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
210    pub rest: Rest,
211}
212
213#[derive(
214    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
215)]
216#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
217#[derive(gproxy_protocol_macros::DeclaredFields)]
218#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
219pub struct CitationsDelta {
220    pub citation: response::ResponseTextCitation,
221    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
222    pub rest: Rest,
223}
224
225#[derive(
226    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
227)]
228#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
229#[derive(gproxy_protocol_macros::DeclaredFields)]
230#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
231pub struct CompactionDelta {
232    #[serde(
233        default,
234        deserialize_with = "present_nullable",
235        skip_serializing_if = "Option::is_none"
236    )]
237    pub content: Option<Option<String>>,
238    #[serde(
239        default,
240        deserialize_with = "present_nullable",
241        skip_serializing_if = "Option::is_none"
242    )]
243    pub encrypted_content: Option<Option<String>>,
244    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
245    pub rest: Rest,
246}
247
248#[derive(
249    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
250)]
251#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
252#[derive(gproxy_protocol_macros::DeclaredFields)]
253#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
254pub struct ContentBlockStopEvent {
255    pub index: i64,
256    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
257    pub rest: Rest,
258}
259
260#[derive(
261    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
262)]
263#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
264#[derive(gproxy_protocol_macros::DeclaredFields)]
265#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
266pub struct MessageDeltaEvent {
267    #[serde(
268        default,
269        deserialize_with = "present_nullable",
270        skip_serializing_if = "Option::is_none"
271    )]
272    pub input_transformations: Option<Option<Vec<response::InputTransformation>>>,
273
274    #[serde(
275        default,
276        deserialize_with = "present_nullable",
277        skip_serializing_if = "Option::is_none"
278    )]
279    pub context_management: Option<Option<response::ContextManagementResponse>>,
280    pub delta: MessageDelta,
281    pub usage: MessageDeltaUsage,
282    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
283    pub rest: Rest,
284}
285
286#[derive(
287    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
288)]
289#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
290#[derive(gproxy_protocol_macros::DeclaredFields)]
291#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
292pub struct MessageDelta {
293    #[serde(
294        default,
295        deserialize_with = "present_nullable",
296        skip_serializing_if = "Option::is_none"
297    )]
298    pub container: Option<Option<response::ResponseContainer>>,
299    #[serde(
300        default,
301        deserialize_with = "present_nullable",
302        skip_serializing_if = "Option::is_none"
303    )]
304    pub stop_details: Option<Option<response::RefusalStopDetails>>,
305    #[serde(
306        default,
307        deserialize_with = "present_nullable",
308        skip_serializing_if = "Option::is_none"
309    )]
310    pub stop_reason: Option<Option<StreamStopReason>>,
311    #[serde(
312        default,
313        deserialize_with = "present_nullable",
314        skip_serializing_if = "Option::is_none"
315    )]
316    pub stop_sequence: Option<Option<String>>,
317    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
318    pub rest: Rest,
319}
320
321#[derive(
322    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
323)]
324#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
325#[derive(gproxy_protocol_macros::DeclaredFields)]
326#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
327pub struct MessageDeltaUsage {
328    #[serde(
329        default,
330        deserialize_with = "present_nullable",
331        skip_serializing_if = "Option::is_none"
332    )]
333    pub cache_creation_input_tokens: Option<Option<i64>>,
334    #[serde(
335        default,
336        deserialize_with = "present_nullable",
337        skip_serializing_if = "Option::is_none"
338    )]
339    pub cache_read_input_tokens: Option<Option<i64>>,
340    #[serde(
341        default,
342        deserialize_with = "present_nullable",
343        skip_serializing_if = "Option::is_none"
344    )]
345    pub fallback_credit: Option<Option<response::FallbackCreditUsage>>,
346    #[serde(
347        default,
348        deserialize_with = "present_nullable",
349        skip_serializing_if = "Option::is_none"
350    )]
351    pub input_tokens: Option<Option<i64>>,
352    #[serde(
353        default,
354        deserialize_with = "present_nullable",
355        skip_serializing_if = "Option::is_none"
356    )]
357    pub iterations: Option<Option<Vec<response::IterationUsage>>>,
358    pub output_tokens: i64,
359    #[serde(
360        default,
361        deserialize_with = "present_nullable",
362        skip_serializing_if = "Option::is_none"
363    )]
364    pub output_tokens_details: Option<Option<response::OutputTokensDetails>>,
365    #[serde(
366        default,
367        deserialize_with = "present_nullable",
368        skip_serializing_if = "Option::is_none"
369    )]
370    pub server_tool_use: Option<Option<response::ServerToolUsage>>,
371    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
372    pub rest: Rest,
373}
374
375#[derive(
376    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
377)]
378#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
379#[derive(gproxy_protocol_macros::DeclaredFields)]
380#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
381pub struct MessageStopEvent {
382    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
383    pub rest: Rest,
384}
385
386#[derive(
387    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
388)]
389#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
390#[derive(gproxy_protocol_macros::DeclaredFields)]
391#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
392pub struct PingEvent {
393    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
394    pub rest: Rest,
395}
396
397#[derive(
398    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
399)]
400#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
401#[derive(gproxy_protocol_macros::DeclaredFields)]
402#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
403pub struct ErrorEvent {
404    pub error: StreamError,
405    #[serde(
406        default,
407        deserialize_with = "present_nullable",
408        skip_serializing_if = "Option::is_none"
409    )]
410    pub request_id: Option<Option<String>>,
411    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
412    pub rest: Rest,
413}
414
415#[derive(
416    Debug, Clone, PartialEq, Eq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder,
417)]
418#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
419#[derive(gproxy_protocol_macros::DeclaredFields)]
420#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
421pub struct StreamError {
422    #[serde(rename = "type")]
423    pub type_: StreamErrorType,
424    pub message: String,
425    #[serde(default, flatten, skip_serializing_if = "serde_json::Map::is_empty")]
426    pub rest: Rest,
427}
428
429#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
430#[serde(rename_all = "snake_case")]
431#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
432#[derive(gproxy_protocol_macros::DeclaredFields)]
433#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
434pub enum StreamErrorType {
435    ApiError,
436    AuthenticationError,
437    BillingError,
438    InvalidRequestError,
439    NotFoundError,
440    OverloadedError,
441    PermissionError,
442    RateLimitError,
443    TimeoutError,
444    ConflictError,
445    RequestTooLarge,
446}