Skip to main content

rig/providers/openrouter/
streaming.rs

1use std::collections::HashMap;
2
3use async_stream::stream;
4use futures::StreamExt;
5use http::Request;
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8use tracing::info_span;
9use tracing_futures::Instrument;
10
11use crate::completion::{CompletionError, CompletionRequest, GetTokenUsage};
12use crate::http_client::HttpClientExt;
13use crate::http_client::sse::{Event, GenericEventSource};
14use crate::json_utils;
15use crate::providers::openrouter::{
16    OpenRouterRequestParams, OpenrouterCompletionRequest, ReasoningDetails,
17};
18use crate::streaming;
19
20#[derive(Clone, Serialize, Deserialize, Debug)]
21pub struct StreamingCompletionResponse {
22    pub usage: Usage,
23}
24
25impl GetTokenUsage for StreamingCompletionResponse {
26    fn token_usage(&self) -> Option<crate::completion::Usage> {
27        let mut usage = crate::completion::Usage::new();
28
29        usage.input_tokens = self.usage.prompt_tokens as u64;
30        usage.output_tokens = self.usage.completion_tokens as u64;
31        usage.total_tokens = self.usage.total_tokens as u64;
32
33        Some(usage)
34    }
35}
36
37#[derive(Deserialize, Debug, PartialEq)]
38#[serde(rename_all = "snake_case")]
39pub enum FinishReason {
40    ToolCalls,
41    Stop,
42    Error,
43    ContentFilter,
44    Length,
45    #[serde(untagged)]
46    Other(String),
47}
48
49#[derive(Deserialize, Debug)]
50#[allow(dead_code)]
51struct StreamingChoice {
52    pub finish_reason: Option<FinishReason>,
53    pub native_finish_reason: Option<String>,
54    pub logprobs: Option<Value>,
55    pub index: usize,
56    pub delta: StreamingDelta,
57}
58
59#[derive(Deserialize, Debug)]
60struct StreamingFunction {
61    pub name: Option<String>,
62    pub arguments: Option<String>,
63}
64
65#[derive(Deserialize, Debug)]
66#[allow(dead_code)]
67struct StreamingToolCall {
68    pub index: usize,
69    pub id: Option<String>,
70    pub r#type: Option<String>,
71    pub function: StreamingFunction,
72}
73
74#[derive(Serialize, Deserialize, Debug, Clone, Default)]
75pub struct Usage {
76    pub prompt_tokens: u32,
77    pub completion_tokens: u32,
78    pub total_tokens: u32,
79}
80
81#[derive(Deserialize, Debug)]
82#[allow(dead_code)]
83struct ErrorResponse {
84    pub code: i32,
85    pub message: String,
86}
87
88#[derive(Deserialize, Debug)]
89#[allow(dead_code)]
90struct StreamingDelta {
91    pub role: Option<String>,
92    pub content: Option<String>,
93    #[serde(default, deserialize_with = "json_utils::null_or_vec")]
94    pub tool_calls: Vec<StreamingToolCall>,
95    pub reasoning: Option<String>,
96    #[serde(default, deserialize_with = "json_utils::null_or_vec")]
97    pub reasoning_details: Vec<ReasoningDetails>,
98}
99
100#[derive(Deserialize, Debug)]
101#[allow(dead_code)]
102struct StreamingCompletionChunk {
103    id: String,
104    model: String,
105    choices: Vec<StreamingChoice>,
106    usage: Option<Usage>,
107    error: Option<ErrorResponse>,
108}
109
110impl<T> super::CompletionModel<T>
111where
112    T: HttpClientExt + Clone + std::fmt::Debug + Default + 'static,
113{
114    pub(crate) async fn stream(
115        &self,
116        completion_request: CompletionRequest,
117    ) -> Result<streaming::StreamingCompletionResponse<StreamingCompletionResponse>, CompletionError>
118    {
119        let request_model = completion_request
120            .model
121            .clone()
122            .unwrap_or_else(|| self.model.clone());
123        let preamble = completion_request.preamble.clone();
124        let mut request = OpenrouterCompletionRequest::try_from(OpenRouterRequestParams {
125            model: request_model.as_ref(),
126            request: completion_request,
127            strict_tools: self.strict_tools,
128        })?;
129
130        let params = json_utils::merge(
131            request.additional_params.unwrap_or(serde_json::json!({})),
132            serde_json::json!({"stream": true }),
133        );
134
135        request.additional_params = Some(params);
136
137        let body = serde_json::to_vec(&request)?;
138
139        let req = self
140            .client
141            .post("/chat/completions")?
142            .body(body)
143            .map_err(|x| CompletionError::HttpError(x.into()))?;
144
145        let span = if tracing::Span::current().is_disabled() {
146            info_span!(
147                target: "rig::completions",
148                "chat_streaming",
149                gen_ai.operation.name = "chat_streaming",
150                gen_ai.provider.name = "openrouter",
151                gen_ai.request.model = &request_model,
152                gen_ai.system_instructions = preamble,
153                gen_ai.response.id = tracing::field::Empty,
154                gen_ai.response.model = tracing::field::Empty,
155                gen_ai.usage.output_tokens = tracing::field::Empty,
156                gen_ai.usage.input_tokens = tracing::field::Empty,
157                gen_ai.usage.cached_tokens = tracing::field::Empty,
158            )
159        } else {
160            tracing::Span::current()
161        };
162
163        tracing::Instrument::instrument(
164            send_compatible_streaming_request(self.client.clone(), req),
165            span,
166        )
167        .await
168    }
169}
170
171pub async fn send_compatible_streaming_request<T>(
172    http_client: T,
173    req: Request<Vec<u8>>,
174) -> Result<streaming::StreamingCompletionResponse<StreamingCompletionResponse>, CompletionError>
175where
176    T: HttpClientExt + Clone + 'static,
177{
178    let span = tracing::Span::current();
179    // Build the request with proper headers for SSE
180    let mut event_source = GenericEventSource::new(http_client, req);
181
182    let stream = stream! {
183        // Accumulate tool calls by index while streaming
184        let mut tool_calls: HashMap<usize, streaming::RawStreamingToolCall> = HashMap::new();
185        let mut final_usage = None;
186
187        while let Some(event_result) = event_source.next().await {
188            match event_result {
189                Ok(Event::Open) => {
190                    tracing::trace!("SSE connection opened");
191                    continue;
192                }
193
194                Ok(Event::Message(message)) => {
195                    if message.data.trim().is_empty() || message.data == "[DONE]" {
196                        continue;
197                    }
198
199                    let data = match serde_json::from_str::<StreamingCompletionChunk>(&message.data) {
200                        Ok(data) => data,
201                        Err(error) => {
202                            tracing::error!(?error, message = message.data, "Failed to parse SSE message");
203                            continue;
204                        }
205                    };
206
207                    // Expect at least one choice
208                     let Some(choice) = data.choices.first() else {
209                        tracing::debug!("There is no choice");
210                        continue;
211                    };
212                    let delta = &choice.delta;
213
214                    if !delta.tool_calls.is_empty() {
215                        for tool_call in &delta.tool_calls {
216                            let index = tool_call.index;
217
218                            // Get or create tool call entry
219                            let existing_tool_call = tool_calls.entry(index).or_insert_with(streaming::RawStreamingToolCall::empty);
220
221                            // Update fields if present
222                            if let Some(id) = &tool_call.id && !id.is_empty() {
223                                    existing_tool_call.id = id.clone();
224                            }
225
226                            if let Some(name) = &tool_call.function.name && !name.is_empty() {
227                                    existing_tool_call.name = name.clone();
228                                    yield Ok(streaming::RawStreamingChoice::ToolCallDelta {
229                                        id: existing_tool_call.id.clone(),
230                                        internal_call_id: existing_tool_call.internal_call_id.clone(),
231                                        content: streaming::ToolCallDeltaContent::Name(name.clone()),
232                                    });
233                            }
234
235                                // Convert current arguments to string if needed
236                            if let Some(chunk) = &tool_call.function.arguments && !chunk.is_empty() {
237                                let current_args = match &existing_tool_call.arguments {
238                                    serde_json::Value::Null => String::new(),
239                                    serde_json::Value::String(s) => s.clone(),
240                                    v => v.to_string(),
241                                };
242
243                                // Concatenate the new chunk
244                                let combined = format!("{current_args}{chunk}");
245
246                                // Try to parse as JSON if it looks complete
247                                if combined.trim_start().starts_with('{') && combined.trim_end().ends_with('}') {
248                                    match serde_json::from_str(&combined) {
249                                        Ok(parsed) => existing_tool_call.arguments = parsed,
250                                        Err(_) => existing_tool_call.arguments = serde_json::Value::String(combined),
251                                    }
252                                } else {
253                                    existing_tool_call.arguments = serde_json::Value::String(combined);
254                                }
255
256                                // Emit the delta so UI can show progress
257                                yield Ok(streaming::RawStreamingChoice::ToolCallDelta {
258                                    id: existing_tool_call.id.clone(),
259                                    internal_call_id: existing_tool_call.internal_call_id.clone(),
260                                    content: streaming::ToolCallDeltaContent::Delta(chunk.clone()),
261                                });
262                            }
263                        }
264
265                        // Update the signature and the additional params of the tool call if present
266                        for reasoning_detail in &delta.reasoning_details {
267                            if let ReasoningDetails::Encrypted { id, data, .. } = reasoning_detail
268                                && let Some(id) = id
269                                && let Some(tool_call) = tool_calls.values_mut().find(|tool_call| tool_call.id.eq(id))
270                                && let Ok(additional_params) = serde_json::to_value(reasoning_detail) {
271                                tool_call.signature = Some(data.clone());
272                                tool_call.additional_params = Some(additional_params);
273                            }
274                        }
275                    }
276
277                    // Streamed reasoning content
278                    if let Some(reasoning) = &delta.reasoning && !reasoning.is_empty() {
279                        yield Ok(streaming::RawStreamingChoice::ReasoningDelta {
280                            reasoning: reasoning.clone(),
281                            id: None,
282                        });
283                    }
284
285                    // Streamed text content
286                    if let Some(content) = &delta.content && !content.is_empty() {
287                        yield Ok(streaming::RawStreamingChoice::Message(content.clone()));
288                    }
289
290                    // Usage updates
291                    if let Some(usage) = data.usage {
292                        final_usage = Some(usage);
293                    }
294
295                    // Finish reason
296                    if let Some(finish_reason) = &choice.finish_reason && *finish_reason == FinishReason::ToolCalls {
297                        for (_idx, tool_call) in tool_calls.into_iter() {
298                            yield Ok(streaming::RawStreamingChoice::ToolCall(tool_call));
299                        }
300                        tool_calls = HashMap::new();
301                    }
302                }
303                Err(crate::http_client::Error::StreamEnded) => {
304                    break;
305                }
306                Err(error) => {
307                    tracing::error!(?error, "SSE error");
308                    yield Err(CompletionError::ProviderError(error.to_string()));
309                    break;
310                }
311            }
312        }
313
314        // Ensure event source is closed when stream ends
315        event_source.close();
316
317        // Flush any accumulated tool calls (that weren't emitted as ToolCall earlier)
318        for (_idx, tool_call) in tool_calls.into_iter() {
319            yield Ok(streaming::RawStreamingChoice::ToolCall(tool_call));
320        }
321
322        // Final response with usage
323        yield Ok(streaming::RawStreamingChoice::FinalResponse(StreamingCompletionResponse {
324            usage: final_usage.unwrap_or_default(),
325        }));
326    }.instrument(span);
327
328    Ok(streaming::StreamingCompletionResponse::stream(Box::pin(
329        stream,
330    )))
331}
332
333#[cfg(test)]
334mod tests {
335    use super::*;
336    use serde_json::json;
337
338    #[test]
339    fn test_streaming_completion_response_deserialization() {
340        let json = json!({
341            "id": "gen-abc123",
342            "choices": [{
343                "index": 0,
344                "delta": {
345                    "role": "assistant",
346                    "content": "Hello"
347                }
348            }],
349            "created": 1234567890u64,
350            "model": "gpt-3.5-turbo",
351            "object": "chat.completion.chunk"
352        });
353
354        let response: StreamingCompletionChunk = serde_json::from_value(json).unwrap();
355        assert_eq!(response.id, "gen-abc123");
356        assert_eq!(response.model, "gpt-3.5-turbo");
357        assert_eq!(response.choices.len(), 1);
358    }
359
360    #[test]
361    fn test_delta_with_content() {
362        let json = json!({
363            "role": "assistant",
364            "content": "Hello, world!"
365        });
366
367        let delta: StreamingDelta = serde_json::from_value(json).unwrap();
368        assert_eq!(delta.role, Some("assistant".to_string()));
369        assert_eq!(delta.content, Some("Hello, world!".to_string()));
370    }
371
372    #[test]
373    fn test_delta_with_tool_call() {
374        let json = json!({
375            "role": "assistant",
376            "tool_calls": [{
377                "index": 0,
378                "id": "call_abc",
379                "type": "function",
380                "function": {
381                    "name": "get_weather",
382                    "arguments": "{\"location\":"
383                }
384            }]
385        });
386
387        let delta: StreamingDelta = serde_json::from_value(json).unwrap();
388        assert_eq!(delta.tool_calls.len(), 1);
389        assert_eq!(delta.tool_calls[0].index, 0);
390        assert_eq!(delta.tool_calls[0].id, Some("call_abc".to_string()));
391    }
392
393    #[test]
394    fn test_tool_call_with_partial_arguments() {
395        let json = json!({
396            "index": 0,
397            "id": null,
398            "type": null,
399            "function": {
400                "name": null,
401                "arguments": "Paris"
402            }
403        });
404
405        let tool_call: StreamingToolCall = serde_json::from_value(json).unwrap();
406        assert_eq!(tool_call.index, 0);
407        assert!(tool_call.id.is_none());
408        assert_eq!(tool_call.function.arguments, Some("Paris".to_string()));
409    }
410
411    #[test]
412    fn test_streaming_with_usage() {
413        let json = json!({
414            "id": "gen-xyz",
415            "choices": [{
416                "index": 0,
417                "delta": {
418                    "content": null
419                }
420            }],
421            "created": 1234567890u64,
422            "model": "gpt-4",
423            "object": "chat.completion.chunk",
424            "usage": {
425                "prompt_tokens": 100,
426                "completion_tokens": 50,
427                "total_tokens": 150
428            }
429        });
430
431        let response: StreamingCompletionChunk = serde_json::from_value(json).unwrap();
432        assert!(response.usage.is_some());
433        let usage = response.usage.unwrap();
434        assert_eq!(usage.prompt_tokens, 100);
435        assert_eq!(usage.completion_tokens, 50);
436        assert_eq!(usage.total_tokens, 150);
437    }
438
439    #[test]
440    fn test_multiple_tool_call_deltas() {
441        // Simulates the sequence of deltas for a tool call with arguments
442        let start_json = json!({
443            "id": "gen-1",
444            "choices": [{
445                "index": 0,
446                "delta": {
447                    "tool_calls": [{
448                        "index": 0,
449                        "id": "call_123",
450                        "type": "function",
451                        "function": {
452                            "name": "search",
453                            "arguments": ""
454                        }
455                    }]
456                }
457            }],
458            "created": 1234567890u64,
459            "model": "gpt-4",
460            "object": "chat.completion.chunk"
461        });
462
463        let delta1_json = json!({
464            "id": "gen-2",
465            "choices": [{
466                "index": 0,
467                "delta": {
468                    "tool_calls": [{
469                        "index": 0,
470                        "function": {
471                            "arguments": "{\"query\":"
472                        }
473                    }]
474                }
475            }],
476            "created": 1234567890u64,
477            "model": "gpt-4",
478            "object": "chat.completion.chunk"
479        });
480
481        let delta2_json = json!({
482            "id": "gen-3",
483            "choices": [{
484                "index": 0,
485                "delta": {
486                    "tool_calls": [{
487                        "index": 0,
488                        "function": {
489                            "arguments": "\"Rust programming\"}"
490                        }
491                    }]
492                }
493            }],
494            "created": 1234567890u64,
495            "model": "gpt-4",
496            "object": "chat.completion.chunk"
497        });
498
499        // Verify all chunks deserialize
500        let start: StreamingCompletionChunk = serde_json::from_value(start_json).unwrap();
501        assert_eq!(
502            start.choices[0].delta.tool_calls[0].id,
503            Some("call_123".to_string())
504        );
505
506        let delta1: StreamingCompletionChunk = serde_json::from_value(delta1_json).unwrap();
507        assert_eq!(
508            delta1.choices[0].delta.tool_calls[0].function.arguments,
509            Some("{\"query\":".to_string())
510        );
511
512        let delta2: StreamingCompletionChunk = serde_json::from_value(delta2_json).unwrap();
513        assert_eq!(
514            delta2.choices[0].delta.tool_calls[0].function.arguments,
515            Some("\"Rust programming\"}".to_string())
516        );
517    }
518
519    #[test]
520    fn test_response_with_error() {
521        let json = json!({
522            "id": "cmpl-abc123",
523            "object": "chat.completion.chunk",
524            "created": 1234567890,
525            "model": "gpt-3.5-turbo",
526            "provider": "openai",
527            "error": { "code": 500, "message": "Provider disconnected" },
528            "choices": [
529                { "index": 0, "delta": { "content": "" }, "finish_reason": "error" }
530            ]
531        });
532
533        let response: StreamingCompletionChunk = serde_json::from_value(json).unwrap();
534        assert!(response.error.is_some());
535        let error = response.error.as_ref().unwrap();
536        assert_eq!(error.code, 500);
537        assert_eq!(error.message, "Provider disconnected");
538    }
539}