openai-protocol 1.7.0

OpenAI-compatible API protocol definitions and types
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
412
413
414
415
416
417
418
419
420
421
422
//! Builder for ChatCompletionStreamResponse
//!
//! Provides an ergonomic fluent API for constructing streaming chat completion responses.

use std::borrow::Cow;

use crate::{
    chat::*,
    common::{FunctionCallDelta, ToolCallDelta, Usage},
};

/// Builder for ChatCompletionStreamResponse
///
/// Provides a fluent interface for constructing streaming chat completion chunks with sensible defaults.
#[must_use = "Builder does nothing until .build() is called"]
#[derive(Clone, Debug)]
pub struct ChatCompletionStreamResponseBuilder {
    id: String,
    object: String,
    created: u64,
    model: String,
    choices: Vec<ChatStreamChoice>,
    usage: Option<Usage>,
    system_fingerprint: Option<String>,
}

impl ChatCompletionStreamResponseBuilder {
    /// Create a new builder with required fields
    ///
    /// # Arguments
    /// - `id`: Completion ID (e.g., "chatcmpl_abc123")
    /// - `model`: Model name used for generation
    pub fn new(id: impl Into<String>, model: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            object: "chat.completion.chunk".to_string(),
            created: chrono::Utc::now().timestamp() as u64,
            model: model.into(),
            choices: Vec::new(),
            usage: None,
            system_fingerprint: None,
        }
    }

    /// Copy common fields from a ChatCompletionRequest
    ///
    /// This populates the model field from the request.
    pub fn copy_from_request(mut self, request: &ChatCompletionRequest) -> Self {
        self.model.clone_from(&request.model);
        self
    }

    /// Set the object type (default: "chat.completion.chunk")
    pub fn object(mut self, object: impl Into<String>) -> Self {
        self.object = object.into();
        self
    }

    /// Set the creation timestamp (default: current time)
    pub fn created(mut self, timestamp: u64) -> Self {
        self.created = timestamp;
        self
    }

    /// Set the choices
    pub fn choices(mut self, choices: Vec<ChatStreamChoice>) -> Self {
        self.choices = choices;
        self
    }

    /// Add a single choice (delta)
    pub fn add_choice(mut self, choice: ChatStreamChoice) -> Self {
        self.choices.push(choice);
        self
    }

    /// Set usage information (typically sent in final chunk)
    pub fn usage(mut self, usage: Usage) -> Self {
        self.usage = Some(usage);
        self
    }

    /// Set system fingerprint if provided (handles Option)
    pub fn maybe_system_fingerprint(mut self, fingerprint: Option<impl Into<String>>) -> Self {
        if let Some(fp) = fingerprint {
            self.system_fingerprint = Some(fp.into());
        }
        self
    }

    /// Set usage if provided (handles Option)
    pub fn maybe_usage(mut self, usage: Option<Usage>) -> Self {
        if let Some(u) = usage {
            self.usage = Some(u);
        }
        self
    }

    /// Add a choice delta that sets `role` and `content`
    pub fn add_choice_content(
        mut self,
        index: u32,
        role: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        self.choices.push(ChatStreamChoice {
            index,
            delta: ChatMessageDelta {
                role: Some(role.into()),
                content: Some(content.into()),
                tool_calls: None,
                reasoning_content: None,
            },
            logprobs: None,
            finish_reason: None,
            matched_stop: None,
        });
        self
    }

    /// Add a choice delta that sets `role`, `content`, and `logprobs`
    pub fn add_choice_content_with_logprobs(
        mut self,
        index: u32,
        role: impl Into<String>,
        content: impl Into<String>,
        logprobs: Option<crate::common::ChatLogProbs>,
    ) -> Self {
        self.choices.push(ChatStreamChoice {
            index,
            delta: ChatMessageDelta {
                role: Some(role.into()),
                content: Some(content.into()),
                tool_calls: None,
                reasoning_content: None,
            },
            logprobs,
            finish_reason: None,
            matched_stop: None,
        });
        self
    }

    /// Add a choice delta that only sets `role`
    pub fn add_choice_role(mut self, index: u32, role: impl Into<String>) -> Self {
        self.choices.push(ChatStreamChoice {
            index,
            delta: ChatMessageDelta {
                role: Some(role.into()),
                content: None,
                tool_calls: None,
                reasoning_content: None,
            },
            logprobs: None,
            finish_reason: None,
            matched_stop: None,
        });
        self
    }

    /// Add a choice delta that appends a tool-call *arguments delta*
    /// Uses `Cow` so you can pass `&str` or `String` without extra clones
    pub fn add_choice_tool_args(
        mut self,
        index: u32,
        args_delta: impl Into<Cow<'static, str>>,
    ) -> Self {
        self.choices.push(ChatStreamChoice {
            index,
            delta: ChatMessageDelta {
                role: Some("assistant".to_string()),
                content: None,
                tool_calls: Some(vec![ToolCallDelta {
                    index: 0,
                    id: None,
                    tool_type: None,
                    function: Some(FunctionCallDelta {
                        name: None,
                        arguments: Some(args_delta.into().into_owned()),
                    }),
                }]),
                reasoning_content: None,
            },
            logprobs: None,
            finish_reason: None,
            matched_stop: None,
        });
        self
    }

    /// Add a choice delta that sets reasoning content (for models that stream reasoning)
    pub fn add_choice_reasoning(mut self, index: u32, reasoning: impl Into<String>) -> Self {
        self.choices.push(ChatStreamChoice {
            index,
            delta: ChatMessageDelta {
                role: Some("assistant".to_string()),
                content: None,
                tool_calls: None,
                reasoning_content: Some(reasoning.into()),
            },
            logprobs: None,
            finish_reason: None,
            matched_stop: None,
        });
        self
    }

    /// Add a choice delta for tool call with function name and ID
    pub fn add_choice_tool_name(
        mut self,
        index: u32,
        tool_call_id: impl Into<String>,
        function_name: impl Into<String>,
    ) -> Self {
        self.choices.push(ChatStreamChoice {
            index,
            delta: ChatMessageDelta {
                role: Some("assistant".to_string()),
                content: None,
                tool_calls: Some(vec![ToolCallDelta {
                    index: 0,
                    id: Some(tool_call_id.into()),
                    tool_type: Some("function".to_string()),
                    function: Some(FunctionCallDelta {
                        name: Some(function_name.into()),
                        arguments: None,
                    }),
                }]),
                reasoning_content: None,
            },
            logprobs: None,
            finish_reason: None,
            matched_stop: None,
        });
        self
    }

    /// Add a choice delta with a pre-constructed ToolCallDelta
    /// Useful when you already have a ToolCallDelta object to emit
    pub fn add_choice_tool_call_delta(
        mut self,
        index: u32,
        tool_call_delta: ToolCallDelta,
    ) -> Self {
        self.choices.push(ChatStreamChoice {
            index,
            delta: ChatMessageDelta {
                role: Some("assistant".to_string()),
                content: None,
                tool_calls: Some(vec![tool_call_delta]),
                reasoning_content: None,
            },
            logprobs: None,
            finish_reason: None,
            matched_stop: None,
        });
        self
    }

    /// Add a choice with finish_reason (final chunk)
    /// This is used for the last chunk in a stream to signal completion
    pub fn add_choice_finish_reason(
        mut self,
        index: u32,
        finish_reason: impl Into<String>,
        matched_stop: Option<serde_json::Value>,
    ) -> Self {
        self.choices.push(ChatStreamChoice {
            index,
            delta: ChatMessageDelta {
                role: None,
                content: None,
                tool_calls: None,
                reasoning_content: None,
            },
            logprobs: None,
            finish_reason: Some(finish_reason.into()),
            matched_stop,
        });
        self
    }

    /// Build the ChatCompletionStreamResponse
    pub fn build(self) -> ChatCompletionStreamResponse {
        ChatCompletionStreamResponse {
            id: self.id,
            object: self.object,
            created: self.created,
            model: self.model,
            system_fingerprint: self.system_fingerprint,
            choices: self.choices,
            usage: self.usage,
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_build_minimal() {
        let chunk = ChatCompletionStreamResponseBuilder::new("chatcmpl_123", "gpt-4").build();

        assert_eq!(chunk.id, "chatcmpl_123");
        assert_eq!(chunk.model, "gpt-4");
        assert_eq!(chunk.object, "chat.completion.chunk");
        assert!(chunk.choices.is_empty());
        assert!(chunk.usage.is_none());
    }

    #[test]
    fn test_with_content_delta() {
        let chunk = ChatCompletionStreamResponseBuilder::new("chatcmpl_456", "gpt-4")
            .add_choice_content(0, "assistant", "Hello")
            .build();

        assert_eq!(chunk.choices.len(), 1);
        assert_eq!(chunk.choices[0].index, 0);
        assert_eq!(chunk.choices[0].delta.content.as_ref().unwrap(), "Hello");
        assert_eq!(chunk.choices[0].delta.role.as_ref().unwrap(), "assistant");
        assert!(chunk.choices[0].finish_reason.is_none());
    }

    #[test]
    fn test_with_role_delta() {
        let chunk = ChatCompletionStreamResponseBuilder::new("chatcmpl_789", "gpt-4")
            .add_choice_role(0, "assistant")
            .build();

        assert_eq!(chunk.choices.len(), 1);
        assert_eq!(chunk.choices[0].delta.role.as_ref().unwrap(), "assistant");
        assert!(chunk.choices[0].delta.content.is_none());
    }

    #[test]
    fn test_with_finish_reason() {
        let chunk = ChatCompletionStreamResponseBuilder::new("chatcmpl_101", "gpt-4")
            .add_choice_finish_reason(0, "stop", None)
            .build();

        assert_eq!(chunk.choices.len(), 1);
        assert_eq!(chunk.choices[0].finish_reason.as_ref().unwrap(), "stop");
        assert!(chunk.choices[0].delta.content.is_none());
        assert!(chunk.choices[0].delta.role.is_none());
    }

    #[test]
    fn test_multiple_deltas() {
        let chunk = ChatCompletionStreamResponseBuilder::new("chatcmpl_202", "gpt-4")
            .add_choice_role(0, "assistant")
            .add_choice_content(0, "assistant", "Hello")
            .add_choice_content(0, "assistant", " world")
            .add_choice_finish_reason(0, "stop", None)
            .build();

        assert_eq!(chunk.choices.len(), 4); // role + 2 content + finish
    }

    #[test]
    fn test_with_usage() {
        let usage = Usage {
            prompt_tokens: 10,
            completion_tokens: 20,
            total_tokens: 30,
            prompt_tokens_details: None,
            completion_tokens_details: None,
        };

        let chunk = ChatCompletionStreamResponseBuilder::new("chatcmpl_303", "gpt-4")
            .add_choice_finish_reason(0, "stop", None)
            .usage(usage)
            .build();

        assert!(chunk.usage.is_some());
        assert_eq!(chunk.usage.as_ref().unwrap().total_tokens, 30);
    }

    #[test]
    fn test_copy_from_request() {
        let request = ChatCompletionRequest {
            messages: vec![],
            model: "gpt-3.5-turbo".to_string(),
            ..Default::default()
        };

        let chunk = ChatCompletionStreamResponseBuilder::new("chatcmpl_404", "gpt-4")
            .copy_from_request(&request)
            .add_choice_content(0, "assistant", "test")
            .build();

        assert_eq!(chunk.model, "gpt-3.5-turbo"); // Copied from request
    }

    #[test]
    fn test_add_choice_explicit() {
        let choice = ChatStreamChoice {
            index: 0,
            delta: ChatMessageDelta {
                role: Some("assistant".to_string()),
                content: Some("Hello".to_string()),
                tool_calls: None,
                reasoning_content: None,
            },
            logprobs: None,
            finish_reason: None,
            matched_stop: None,
        };

        let chunk = ChatCompletionStreamResponseBuilder::new("chatcmpl_505", "gpt-4")
            .add_choice(choice)
            .build();

        assert_eq!(chunk.choices.len(), 1);
        assert_eq!(chunk.choices[0].delta.role.as_ref().unwrap(), "assistant");
        assert_eq!(chunk.choices[0].delta.content.as_ref().unwrap(), "Hello");
    }
}