Skip to main content

graphrt_llm/
openai.rs

1//! OpenAI-compatible chat completions provider.
2//!
3//! Works with any endpoint implementing `POST {base_url}/chat/completions` with
4//! streaming: OpenAI, Azure OpenAI (set [`AuthStyle::Header`] with `api-key`
5//! and an `api-version` query parameter), Ollama, vLLM, LM Studio, Groq,
6//! OpenRouter, DeepSeek, Together, Mistral and others.
7
8use std::collections::HashMap;
9use std::fmt;
10use std::sync::Arc;
11use std::time::Duration;
12
13use futures::future::BoxFuture;
14use graphrt_core::event::Usage;
15use serde_json::{Map, Value as Json, json};
16
17use crate::credentials::{Credential, CredentialSource};
18use crate::error::LlmError;
19use crate::http::{Decoded, SseDecoder, build_client, send_sse, truncated};
20use crate::sse::SseEvent;
21use crate::types::{
22    ChatDelta, ChatModel, ChatRequest, ChatStream, ContentPart, FinishReason, Message, ResponseFormat, Role,
23    ToolChoice, check_extra,
24};
25
26/// How the API key is sent.
27#[derive(Clone, Debug, PartialEq, Eq)]
28#[non_exhaustive]
29pub enum AuthStyle {
30    /// `Authorization: Bearer <key>`.
31    Bearer,
32    /// A custom header, e.g. `api-key` for Azure OpenAI.
33    Header(String),
34}
35
36/// Configuration of an OpenAI-compatible endpoint.
37#[derive(Clone)]
38pub struct OpenAiConfig {
39    /// Base URL without the `/chat/completions` suffix, e.g. `https://api.openai.com/v1`.
40    pub base_url: String,
41    pub api_key: Option<String>,
42    pub auth: AuthStyle,
43    /// Extra headers sent with every request.
44    pub headers: Vec<(String, String)>,
45    /// Extra query parameters (e.g. `api-version`).
46    pub query: Vec<(String, String)>,
47    /// Ask for a final usage chunk (`stream_options.include_usage`). Disable for
48    /// endpoints that reject the field.
49    pub include_usage: bool,
50    /// Send `max_completion_tokens` instead of `max_tokens` (required by some newer models).
51    pub use_max_completion_tokens: bool,
52    pub connect_timeout: Duration,
53    /// Per-request credential source. When set it replaces `api_key`; its headers are
54    /// appended and its `base_url`, if any, overrides `base_url`. A 401/403 response
55    /// invalidates the credential and the request is retried once.
56    pub credentials: Option<Arc<dyn CredentialSource>>,
57    /// Provider name passed to the credential source (default `openai`).
58    pub credential_name: String,
59}
60
61impl fmt::Debug for OpenAiConfig {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        f.debug_struct("OpenAiConfig")
64            .field("base_url", &self.base_url)
65            .field("api_key", &self.api_key.as_ref().map(|_| "<redacted>"))
66            .field("auth", &self.auth)
67            .field("headers", &self.headers.iter().map(|(k, _)| k).collect::<Vec<_>>())
68            .field("include_usage", &self.include_usage)
69            .field("credentials", &self.credentials.as_ref().map(|_| "<source>"))
70            .field("credential_name", &self.credential_name)
71            .finish_non_exhaustive()
72    }
73}
74
75impl Default for OpenAiConfig {
76    fn default() -> Self {
77        Self {
78            base_url: "https://api.openai.com/v1".to_owned(),
79            api_key: None,
80            auth: AuthStyle::Bearer,
81            headers: Vec::new(),
82            query: Vec::new(),
83            include_usage: true,
84            use_max_completion_tokens: false,
85            connect_timeout: Duration::from_secs(10),
86            credentials: None,
87            credential_name: "openai".to_owned(),
88        }
89    }
90}
91
92impl OpenAiConfig {
93    pub fn new(base_url: impl Into<String>, api_key: Option<String>) -> Self {
94        Self { base_url: base_url.into(), api_key, ..Self::default() }
95    }
96
97    /// Ask `source` for a credential before every request (see [`crate::credentials`]).
98    pub fn with_credentials(mut self, source: Arc<dyn CredentialSource>) -> Self {
99        self.credentials = Some(source);
100        self
101    }
102
103    /// Provider name passed to the credential source.
104    pub fn credential_name(mut self, name: impl Into<String>) -> Self {
105        self.credential_name = name.into();
106        self
107    }
108}
109
110/// An OpenAI-compatible chat model endpoint.
111#[derive(Clone)]
112pub struct OpenAiModel {
113    config: OpenAiConfig,
114    client: reqwest::Client,
115}
116
117impl fmt::Debug for OpenAiModel {
118    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119        f.debug_struct("OpenAiModel").field("config", &self.config).finish_non_exhaustive()
120    }
121}
122
123impl OpenAiModel {
124    pub fn new(config: OpenAiConfig) -> Result<Self, LlmError> {
125        let client = build_client(config.connect_timeout)?;
126        Ok(Self { config, client })
127    }
128
129    fn endpoint(&self, base_url: &str, with_query: bool) -> String {
130        let mut url = format!("{}/chat/completions", base_url.trim_end_matches('/'));
131        let query: &[(String, String)] = if with_query { &self.config.query } else { &[] };
132        for (i, (k, v)) in query.iter().enumerate() {
133            url.push(if i == 0 { '?' } else { '&' });
134            url.push_str(&percent_encode(k));
135            url.push('=');
136            url.push_str(&percent_encode(v));
137        }
138        url
139    }
140}
141
142/// Percent-encode a query component (RFC 3986 unreserved characters pass through).
143fn percent_encode(s: &str) -> String {
144    let mut out = String::with_capacity(s.len());
145    for b in s.bytes() {
146        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') {
147            out.push(char::from(b));
148        } else {
149            out.push_str(&format!("%{b:02X}"));
150        }
151    }
152    out
153}
154
155impl OpenAiModel {
156    fn request(&self, body: &Json, credential: Option<&Credential>) -> Result<reqwest::RequestBuilder, LlmError> {
157        let endpoint =
158            crate::http::resolve_endpoint(&self.config.base_url, credential.and_then(|c| c.base_url.as_deref()))?;
159        let mut builder = self.client.post(self.endpoint(&endpoint.base_url, endpoint.send_static)).json(body);
160        let key = credential.map(|c| c.secret.as_str()).or(self.config.api_key.as_deref());
161        if let Some(key) = key {
162            builder = match &self.config.auth {
163                AuthStyle::Bearer => builder.bearer_auth(key),
164                AuthStyle::Header(name) => builder.header(name.as_str(), key),
165            };
166        }
167        // Operator headers (gateway keys, org ids) never follow a credential override
168        // to another origin.
169        if endpoint.send_static {
170            for (k, v) in &self.config.headers {
171                builder = builder.header(k.as_str(), v.as_str());
172            }
173        }
174        if let Some(credential) = credential {
175            for (k, v) in &credential.headers {
176                builder = builder.header(k.as_str(), v.as_str());
177            }
178        }
179        Ok(builder)
180    }
181}
182
183impl ChatModel for OpenAiModel {
184    fn chat(&self, request: ChatRequest) -> BoxFuture<'_, Result<ChatStream, LlmError>> {
185        Box::pin(async move {
186            let body = build_body(&request, &self.config)?;
187            let name = self.config.credential_name.as_str();
188            let mut retried = false;
189            loop {
190                let credential = match &self.config.credentials {
191                    Some(source) => Some(source.credential(name).await?),
192                    None => None,
193                };
194                let result = send_sse(self.request(&body, credential.as_ref())?, OpenAiDecoder::default()).await;
195                match (result, &self.config.credentials) {
196                    // The credential was rejected: drop it and retry once with a fresh one.
197                    (Err(LlmError::Auth(_)), Some(source)) if !retried => {
198                        source.invalidate(name);
199                        retried = true;
200                    }
201                    (result, _) => return result,
202                }
203            }
204        })
205    }
206}
207
208fn content_json(parts: &[ContentPart]) -> Json {
209    if let [ContentPart::Text { text }] = parts {
210        return Json::String(text.clone());
211    }
212    let items: Vec<Json> = parts
213        .iter()
214        .map(|p| match p {
215            ContentPart::Text { text } => json!({"type": "text", "text": text}),
216            ContentPart::Image { url, detail } => {
217                let mut image = Map::new();
218                image.insert("url".into(), Json::String(url.clone()));
219                if let Some(d) = detail {
220                    image.insert("detail".into(), Json::String(d.clone()));
221                }
222                json!({"type": "image_url", "image_url": image})
223            }
224        })
225        .collect();
226    Json::Array(items)
227}
228
229fn message_json(m: &Message) -> Result<Json, LlmError> {
230    Ok(match m.role {
231        Role::System => json!({"role": "system", "content": content_json(&m.content)}),
232        Role::User => json!({"role": "user", "content": content_json(&m.content)}),
233        Role::Tool => {
234            let id = m
235                .tool_call_id
236                .as_deref()
237                .filter(|id| !id.is_empty())
238                .ok_or_else(|| LlmError::Config("tool message without tool_call_id".into()))?;
239            json!({"role": "tool", "tool_call_id": id, "content": m.text()})
240        }
241        Role::Assistant => {
242            let mut obj = Map::new();
243            obj.insert("role".into(), json!("assistant"));
244            let text = m.text();
245            obj.insert("content".into(), if text.is_empty() { Json::Null } else { Json::String(text) });
246            if !m.tool_calls.is_empty() {
247                let calls: Vec<Json> = m
248                    .tool_calls
249                    .iter()
250                    .map(|c| json!({"id": c.id, "type": "function", "function": {"name": c.name, "arguments": c.arguments}}))
251                    .collect();
252                obj.insert("tool_calls".into(), Json::Array(calls));
253            }
254            Json::Object(obj)
255        }
256    })
257}
258
259/// Build the JSON request body.
260pub(crate) fn build_body(req: &ChatRequest, config: &OpenAiConfig) -> Result<Json, LlmError> {
261    check_extra(req)?;
262    // Only the first choice is decoded; asking for more would silently discard them.
263    if let Some(n) = req.extra.get("n") {
264        if n.as_u64() != Some(1) {
265            return Err(LlmError::Config(format!("`n` must be 1 (got {n}); only one choice is supported")));
266        }
267    }
268    let mut body = Map::new();
269    body.insert("model".into(), json!(req.model));
270    let messages = req.messages.iter().map(message_json).collect::<Result<Vec<_>, _>>()?;
271    body.insert("messages".into(), Json::Array(messages));
272    body.insert("stream".into(), json!(true));
273    if config.include_usage {
274        body.insert("stream_options".into(), json!({"include_usage": true}));
275    }
276    if !req.tools.is_empty() {
277        let tools: Vec<Json> = req
278            .tools
279            .iter()
280            .map(|t| json!({"type": "function", "function": {"name": t.name, "description": t.description, "parameters": t.parameters}}))
281            .collect();
282        body.insert("tools".into(), Json::Array(tools));
283    }
284    if let Some(choice) = &req.tool_choice {
285        let v = match choice {
286            ToolChoice::Auto => json!("auto"),
287            ToolChoice::None => json!("none"),
288            ToolChoice::Required => json!("required"),
289            ToolChoice::Tool(name) => json!({"type": "function", "function": {"name": name}}),
290        };
291        body.insert("tool_choice".into(), v);
292    }
293    if let Some(t) = req.temperature {
294        body.insert("temperature".into(), json!(t));
295    }
296    if let Some(p) = req.top_p {
297        body.insert("top_p".into(), json!(p));
298    }
299    if let Some(max) = req.max_tokens {
300        let key = if config.use_max_completion_tokens { "max_completion_tokens" } else { "max_tokens" };
301        body.insert(key.into(), json!(max));
302    }
303    if !req.stop.is_empty() {
304        body.insert("stop".into(), json!(req.stop));
305    }
306    match &req.response_format {
307        None | Some(ResponseFormat::Text) => {}
308        Some(ResponseFormat::JsonObject) => {
309            body.insert("response_format".into(), json!({"type": "json_object"}));
310        }
311        Some(ResponseFormat::JsonSchema { name, schema, strict }) => {
312            body.insert(
313                "response_format".into(),
314                json!({"type": "json_schema", "json_schema": {"name": name, "schema": schema, "strict": strict}}),
315            );
316        }
317    }
318    for (k, v) in &req.extra {
319        body.insert(k.clone(), v.clone());
320    }
321    Ok(Json::Object(body))
322}
323
324/// Decodes `chat.completion.chunk` events.
325///
326/// Tool-call fragments are keyed by their `index`. Some compatible servers omit
327/// `index`; then a fragment with a new `id` starts a new call and a fragment
328/// without `id` continues the most recent one.
329#[derive(Debug, Default)]
330pub(crate) struct OpenAiDecoder {
331    /// Call index assigned to each call id seen so far.
332    ids: HashMap<String, usize>,
333    /// Index of the most recent call fragment.
334    last: Option<usize>,
335    /// Smallest index not used yet.
336    next: usize,
337    /// A `finish_reason` was received (some servers end without `[DONE]`).
338    saw_finish: bool,
339}
340
341impl OpenAiDecoder {
342    fn call_index(&mut self, explicit: Option<usize>, id: Option<&str>) -> usize {
343        let index = match (explicit, id) {
344            (Some(i), _) => i,
345            (None, Some(id)) => match self.ids.get(id) {
346                Some(&i) => i,
347                None => self.next,
348            },
349            (None, None) => self.last.unwrap_or(self.next),
350        };
351        if let Some(id) = id {
352            self.ids.entry(id.to_owned()).or_insert(index);
353        }
354        self.last = Some(index);
355        self.next = self.next.max(index.saturating_add(1));
356        index
357    }
358}
359
360/// Classify an error object embedded in the stream.
361fn stream_error(err: &Json) -> LlmError {
362    let message = err.get("message").and_then(Json::as_str).map(str::to_owned).unwrap_or_else(|| err.to_string());
363    let kind = err.get("type").and_then(Json::as_str).unwrap_or_default();
364    let code = match err.get("code") {
365        Some(Json::String(c)) => c.clone(),
366        Some(Json::Number(n)) => n.to_string(),
367        _ => String::new(),
368    };
369    let tag = format!("{kind} {code}").to_ascii_lowercase();
370    if tag.contains("rate_limit") || code == "429" {
371        LlmError::RateLimit { message, retry_after_ms: None }
372    } else if tag.contains("context_length") {
373        LlmError::ContextLength(message)
374    } else if tag.contains("auth") || tag.contains("permission") || code == "401" || code == "403" {
375        LlmError::Auth(message)
376    } else if tag.contains("invalid_request") || code == "400" {
377        LlmError::BadRequest(message)
378    } else {
379        LlmError::Server { status: 0, message }
380    }
381}
382
383fn as_u64(v: Option<&Json>) -> u64 {
384    v.and_then(Json::as_u64).unwrap_or(0)
385}
386
387fn finish_reason(s: &str) -> FinishReason {
388    match s {
389        "stop" => FinishReason::Stop,
390        "length" => FinishReason::Length,
391        "tool_calls" | "function_call" => FinishReason::ToolCalls,
392        "content_filter" => FinishReason::ContentFilter,
393        other => FinishReason::Other(other.to_owned()),
394    }
395}
396
397impl SseDecoder for OpenAiDecoder {
398    fn decode(&mut self, event: SseEvent) -> Result<Decoded, LlmError> {
399        let data = event.data.trim();
400        if data == "[DONE]" {
401            if !self.saw_finish {
402                return Err(truncated());
403            }
404            return Ok(Decoded::done(Vec::new()));
405        }
406        if data.is_empty() {
407            return Ok(Decoded::default());
408        }
409        let chunk: Json = serde_json::from_str(data).map_err(|e| LlmError::Decode(format!("{e}: {data}")))?;
410        if let Some(err) = chunk.get("error").filter(|e| !e.is_null()) {
411            return Err(stream_error(err));
412        }
413        let mut deltas = Vec::new();
414        if let Some(choice) = chunk.get("choices").and_then(Json::as_array).and_then(|c| c.first()) {
415            if let Some(delta) = choice.get("delta") {
416                // Reasoning is exposed as `reasoning_content` (DeepSeek, vLLM) or `reasoning`
417                // (OpenRouter, Ollama). Some servers send both with the same text: use the first.
418                let reasoning = ["reasoning_content", "reasoning"]
419                    .iter()
420                    .find_map(|key| delta.get(*key).and_then(Json::as_str).filter(|s| !s.is_empty()));
421                if let Some(r) = reasoning {
422                    deltas.push(ChatDelta::Reasoning(r.to_owned()));
423                }
424                if let Some(text) = delta.get("content").and_then(Json::as_str).filter(|s| !s.is_empty()) {
425                    deltas.push(ChatDelta::Text(text.to_owned()));
426                }
427                if let Some(calls) = delta.get("tool_calls").and_then(Json::as_array) {
428                    for call in calls {
429                        let explicit = call.get("index").and_then(Json::as_u64).and_then(|i| usize::try_from(i).ok());
430                        let id = call.get("id").and_then(Json::as_str).filter(|s| !s.is_empty());
431                        let index = self.call_index(explicit, id);
432                        let function = call.get("function");
433                        let arguments = match function.and_then(|f| f.get("arguments")) {
434                            None | Some(Json::Null) => String::new(),
435                            Some(Json::String(s)) => s.clone(),
436                            // Some servers send the arguments as a JSON object instead of a string.
437                            Some(other) => other.to_string(),
438                        };
439                        deltas.push(ChatDelta::ToolCall {
440                            index,
441                            id: id.map(str::to_owned),
442                            name: function.and_then(|f| f.get("name")).and_then(Json::as_str).map(str::to_owned),
443                            arguments,
444                        });
445                    }
446                }
447            }
448            if let Some(reason) = choice.get("finish_reason").and_then(Json::as_str) {
449                self.saw_finish = true;
450                deltas.push(ChatDelta::Finish(finish_reason(reason)));
451            }
452        }
453        if let Some(usage) = chunk.get("usage").filter(|u| u.is_object()) {
454            deltas.push(ChatDelta::Usage(Usage {
455                prompt_tokens: as_u64(usage.get("prompt_tokens")),
456                completion_tokens: as_u64(usage.get("completion_tokens")),
457                total_tokens: as_u64(usage.get("total_tokens")),
458            }));
459        }
460        Ok(Decoded::deltas(deltas))
461    }
462
463    /// Servers that omit `[DONE]` are accepted once a `finish_reason` arrived.
464    fn end_of_stream(&mut self) -> Result<Vec<ChatDelta>, LlmError> {
465        if self.saw_finish { Ok(Vec::new()) } else { Err(truncated()) }
466    }
467}
468
469#[cfg(test)]
470mod tests {
471    use super::*;
472    use crate::sse::SseParser;
473    use crate::types::ToolCall;
474    use graphrt_core::tool::ToolSpec;
475
476    #[test]
477    fn body_shape() {
478        let mut assistant = Message::assistant("");
479        assistant.tool_calls = vec![ToolCall { id: "c1".into(), name: "search".into(), arguments: "{}".into() }];
480        let mut req = ChatRequest::new(
481            "gpt-4o-mini",
482            vec![
483                Message::system("sys"),
484                Message {
485                    role: Role::User,
486                    content: vec![
487                        ContentPart::Text { text: "look".into() },
488                        ContentPart::Image { url: "https://x/y.png".into(), detail: Some("high".into()) },
489                    ],
490                    tool_calls: vec![],
491                    tool_call_id: None,
492                    provider_state: None,
493                },
494                assistant,
495                Message::tool("c1", "result"),
496            ],
497        );
498        req.tools =
499            vec![ToolSpec { name: "search".into(), description: "d".into(), parameters: json!({"type": "object"}) }];
500        req.tool_choice = Some(ToolChoice::Tool("search".into()));
501        req.max_tokens = Some(10);
502        req.response_format =
503            Some(ResponseFormat::JsonSchema { name: "out".into(), schema: json!({"type": "object"}), strict: true });
504        req.extra.insert("seed".into(), json!(7));
505        let body = build_body(&req, &OpenAiConfig::default()).unwrap();
506        assert_eq!(body["stream"], true);
507        assert_eq!(body["stream_options"]["include_usage"], true);
508        assert_eq!(body["messages"][0]["content"], "sys");
509        assert_eq!(body["messages"][1]["content"][1]["image_url"]["detail"], "high");
510        assert_eq!(body["messages"][2]["content"], Json::Null);
511        assert_eq!(body["messages"][2]["tool_calls"][0]["function"]["name"], "search");
512        assert_eq!(body["messages"][3]["tool_call_id"], "c1");
513        assert_eq!(body["tools"][0]["function"]["parameters"]["type"], "object");
514        assert_eq!(body["tool_choice"]["function"]["name"], "search");
515        assert_eq!(body["max_tokens"], 10);
516        assert_eq!(body["response_format"]["json_schema"]["strict"], true);
517        assert_eq!(body["seed"], 7);
518
519        let cfg = OpenAiConfig { use_max_completion_tokens: true, include_usage: false, ..OpenAiConfig::default() };
520        let body = build_body(&req, &cfg).unwrap();
521        assert_eq!(body["max_completion_tokens"], 10);
522        assert!(body.get("stream_options").is_none());
523    }
524
525    #[test]
526    fn body_rejects_reserved_extra_n_and_missing_tool_id() {
527        let req = ChatRequest::new("m", vec![Message::user("x")]).with_extra("stream", json!(false));
528        assert!(matches!(build_body(&req, &OpenAiConfig::default()), Err(LlmError::Config(_))));
529        let req = ChatRequest::new("m", vec![Message::user("x")]).with_extra("n", json!(2));
530        assert!(matches!(build_body(&req, &OpenAiConfig::default()), Err(LlmError::Config(_))));
531        let mut tool = Message::tool("x", "r");
532        tool.tool_call_id = None;
533        let req = ChatRequest::new("m", vec![tool]);
534        assert!(matches!(build_body(&req, &OpenAiConfig::default()), Err(LlmError::Config(_))));
535    }
536
537    fn decode_all(fixture: &str) -> (Vec<ChatDelta>, Result<bool, LlmError>) {
538        let mut parser = SseParser::new();
539        let mut decoder = OpenAiDecoder::default();
540        let mut deltas = Vec::new();
541        for ev in parser.push(fixture.as_bytes()).unwrap() {
542            match decoder.decode(ev) {
543                Ok(d) => {
544                    deltas.extend(d.deltas);
545                    if d.done {
546                        return (deltas, Ok(true));
547                    }
548                }
549                Err(e) => return (deltas, Err(e)),
550            }
551        }
552        match decoder.end_of_stream() {
553            Ok(d) => {
554                deltas.extend(d);
555                (deltas, Ok(false))
556            }
557            Err(e) => (deltas, Err(e)),
558        }
559    }
560
561    #[test]
562    fn truncated_stream_is_a_retryable_error() {
563        let (_, end) = decode_all("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hel\"}}]}\n\n");
564        let err = end.unwrap_err();
565        assert!(matches!(err, LlmError::Network(_)));
566        assert!(err.is_retryable());
567        // `[DONE]` without any finish_reason is also incomplete.
568        let (_, end) =
569            decode_all("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"x\"}}]}\n\ndata: [DONE]\n\n");
570        assert!(end.is_err());
571        // A finish_reason without `[DONE]` is accepted.
572        let (deltas, end) = decode_all(
573            "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"x\"},\"finish_reason\":\"stop\"}]}\n\n",
574        );
575        assert!(!end.unwrap());
576        assert_eq!(deltas.last(), Some(&ChatDelta::Finish(FinishReason::Stop)));
577    }
578
579    #[tokio::test]
580    async fn tool_calls_without_index_and_object_arguments() {
581        let fixture = concat!(
582            "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"id\":\"a\",\"function\":{\"name\":\"one\",\"arguments\":{\"x\":1}}}]}}]}\n\n",
583            "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"id\":\"b\",\"function\":{\"name\":\"two\",\"arguments\":\"{\\\"y\\\":\"}}]}}]}\n\n",
584            "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"function\":{\"arguments\":\"2}\"}}]}}]}\n\n",
585            "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\n",
586            "data: [DONE]\n\n",
587        );
588        let (deltas, end) = decode_all(fixture);
589        assert!(end.unwrap());
590        let r =
591            crate::types::collect(Box::pin(futures::stream::iter(deltas.into_iter().map(Ok))), |_| {}).await.unwrap();
592        assert_eq!(r.tool_calls.len(), 2, "{:?}", r.tool_calls);
593        assert_eq!(r.tool_calls[0].arguments_json().unwrap()["x"], 1);
594        assert_eq!(r.tool_calls[1].name, "two");
595        assert_eq!(r.tool_calls[1].arguments_json().unwrap()["y"], 2);
596    }
597
598    #[test]
599    fn duplicate_reasoning_fields_are_not_doubled() {
600        let (deltas, _) = decode_all(
601            "data: {\"choices\":[{\"delta\":{\"reasoning_content\":\"r\",\"reasoning\":\"r\"},\"finish_reason\":\"stop\"}]}\n\n",
602        );
603        assert_eq!(deltas.iter().filter(|d| matches!(d, ChatDelta::Reasoning(_))).count(), 1);
604    }
605
606    #[test]
607    fn decode_recorded_stream() {
608        let fixture = concat!(
609            "data: {\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"reasoning_content\":\"think\"}}]}\n\n",
610            "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hel\"}}]}\n\n",
611            "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"}}]}\n\n",
612            "data: {\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"type\":\"function\",\"function\":{\"name\":\"get\",\"arguments\":\"\"}}]}}]}\n\n",
613            "data: {\"choices\":[{\"index\":0,\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"{\\\"a\\\":1}\"}}]}}]}\n\n",
614            "data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\n",
615            "data: {\"choices\":[],\"usage\":{\"prompt_tokens\":5,\"completion_tokens\":2,\"total_tokens\":7}}\n\n",
616            "data: [DONE]\n\n",
617        );
618        let (deltas, done) = decode_all(fixture);
619        assert!(done.unwrap());
620        assert_eq!(deltas[0], ChatDelta::Reasoning("think".into()));
621        assert_eq!(deltas[1], ChatDelta::Text("Hel".into()));
622        assert!(matches!(&deltas[3], ChatDelta::ToolCall { index: 0, name: Some(n), .. } if n == "get"));
623        assert!(matches!(&deltas[4], ChatDelta::ToolCall { arguments, .. } if arguments == "{\"a\":1}"));
624        assert_eq!(deltas[5], ChatDelta::Finish(FinishReason::ToolCalls));
625        assert_eq!(deltas[6], ChatDelta::Usage(Usage { prompt_tokens: 5, completion_tokens: 2, total_tokens: 7 }));
626    }
627
628    #[test]
629    fn stream_error_object() {
630        let mut d = OpenAiDecoder::default();
631        let ev = SseEvent { data: "{\"error\":{\"message\":\"boom\"}}".into(), ..SseEvent::default() };
632        assert!(matches!(d.decode(ev), Err(LlmError::Server { .. })));
633        let ev = SseEvent {
634            data: "{\"error\":{\"message\":\"slow\",\"type\":\"rate_limit_exceeded\"}}".into(),
635            ..SseEvent::default()
636        };
637        assert!(matches!(d.decode(ev), Err(LlmError::RateLimit { .. })));
638        let ev = SseEvent {
639            data: "{\"error\":{\"message\":\"long\",\"code\":\"context_length_exceeded\"}}".into(),
640            ..SseEvent::default()
641        };
642        assert!(matches!(d.decode(ev), Err(LlmError::ContextLength(_))));
643    }
644
645    #[test]
646    fn debug_redacts_key() {
647        let cfg = OpenAiConfig::new("https://x", Some("sk-secret".into()));
648        assert!(!format!("{cfg:?}").contains("sk-secret"));
649    }
650}