llm-agent-runtime 1.74.0

Unified Tokio agent runtime -- orchestration, memory, knowledge graph, and ReAct loop in one crate
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//! # Module: Providers
//!
//! ## Responsibility
//! Provides built-in LLM inference integrations behind the `LlmProvider` trait.
//! Optional feature flags gate each provider:
//! - `anthropic` — Anthropic Messages API
//! - `openai`    — OpenAI Chat Completions API (and compatible endpoints)
//!
//! ## Guarantees
//! - `LlmProvider` is an async, object-safe trait
//! - Both providers are `Send + Sync` and usable behind `Arc<dyn LlmProvider>`
//! - Non-panicking: all operations return `Result`
//!
//! ## Feature Gate
//! This module is only compiled when the `providers` feature (or a sub-feature) is enabled.

use crate::error::AgentRuntimeError;
use async_trait::async_trait;

// ── CompletionOptions ─────────────────────────────────────────────────────────

/// Options for a completion request, passed to [`LlmProvider::complete_with_options`].
///
/// Allows callers to supply per-request parameters (max tokens, temperature,
/// timeout) in addition to the model name, without changing the base
/// [`LlmProvider::complete`] signature.
#[derive(Debug)]
pub struct CompletionOptions<'a> {
    /// Model identifier (e.g. `"claude-sonnet-4-6"`, `"gpt-4o"`).
    pub model: &'a str,
    /// Maximum number of output tokens. Overrides provider defaults when set.
    pub max_tokens: Option<usize>,
    /// Sampling temperature in `[0.0, 2.0]`. Higher = more random.
    pub temperature: Option<f32>,
    /// Per-request wall-clock timeout.
    pub timeout: Option<std::time::Duration>,
    /// Stop sequences: the model will stop generating when it produces any of
    /// these strings.  An empty slice means no stop sequences.
    pub stop_sequences: Vec<String>,
}

impl<'a> CompletionOptions<'a> {
    /// Create options with just a model name and all other fields unset.
    pub fn new(model: &'a str) -> Self {
        Self {
            model,
            max_tokens: None,
            temperature: None,
            timeout: None,
            stop_sequences: vec![],
        }
    }

    /// Set the maximum output tokens.
    pub fn with_max_tokens(mut self, n: usize) -> Self {
        self.max_tokens = Some(n);
        self
    }

    /// Set the sampling temperature.
    pub fn with_temperature(mut self, t: f32) -> Self {
        self.temperature = Some(t);
        self
    }

    /// Set the per-request timeout.
    pub fn with_timeout(mut self, d: std::time::Duration) -> Self {
        self.timeout = Some(d);
        self
    }

    /// Set stop sequences for this request.
    pub fn with_stop_sequences(mut self, sequences: Vec<String>) -> Self {
        self.stop_sequences = sequences;
        self
    }

    /// Set the per-request timeout from a number of seconds.
    pub fn with_timeout_secs(self, secs: u64) -> Self {
        self.with_timeout(std::time::Duration::from_secs(secs))
    }

    /// Set the per-request timeout from a number of milliseconds.
    pub fn with_timeout_ms(self, ms: u64) -> Self {
        self.with_timeout(std::time::Duration::from_millis(ms))
    }

    /// Return `true` if at least one stop sequence has been configured.
    pub fn has_stop_sequences(&self) -> bool {
        !self.stop_sequences.is_empty()
    }

    /// Return the number of stop sequences configured.
    pub fn stop_sequence_count(&self) -> usize {
        self.stop_sequences.len()
    }
}

// ── LlmProvider ───────────────────────────────────────────────────────────────

/// Abstraction over an LLM inference endpoint.
///
/// Implement this trait to integrate any model API with `AgentRuntime`.
/// Built-in implementations are provided for Anthropic and OpenAI
/// when the corresponding feature flags are enabled.
#[async_trait]
pub trait LlmProvider: Send + Sync {
    /// Send a prompt to the model and return the completion text.
    ///
    /// # Arguments
    /// * `prompt` — the full prompt / context string
    /// * `model`  — model identifier (e.g. `"claude-sonnet-4-6"`, `"gpt-4o"`)
    async fn complete(&self, prompt: &str, model: &str) -> Result<String, AgentRuntimeError>;

    /// Send a prompt with additional per-request options.
    ///
    /// The default implementation ignores `options.max_tokens`,
    /// `options.temperature`, and `options.timeout` and delegates to
    /// `complete(prompt, options.model)`.  Override this method to honour those
    /// fields in your provider implementation.
    async fn complete_with_options(
        &self,
        prompt: &str,
        options: CompletionOptions<'_>,
    ) -> Result<String, AgentRuntimeError> {
        self.complete(prompt, options.model).await
    }

    /// Stream the completion token-by-token.
    ///
    /// Returns a `Receiver` that yields string chunks as they arrive.
    /// The channel closes when the stream is complete or an error occurs.
    ///
    /// # Default implementation
    ///
    /// The default wraps `complete` into a single-chunk stream using a channel
    /// with a buffer of 64 slots.  Custom providers that support true token
    /// streaming should override this method; the 64-slot buffer is sized so
    /// that fast producers do not block waiting for a slow consumer to drain
    /// the first chunk.
    ///
    /// # Note for implementors
    ///
    /// If you override this method, choose a channel capacity that balances
    /// memory use against throughput for your expected token rate.  A capacity
    /// of 1 will cause the producer to block after each token; a capacity of
    /// 0 is unbounded and may exhaust memory on a slow consumer.
    async fn stream_complete(
        &self,
        prompt: &str,
        model: &str,
    ) -> Result<tokio::sync::mpsc::Receiver<Result<String, AgentRuntimeError>>, AgentRuntimeError>
    {
        let result = self.complete(prompt, model).await;
        let (tx, rx) = tokio::sync::mpsc::channel(64);
        // Ignore send error — receiver may already be dropped
        let _ = tx.send(result).await;
        Ok(rx)
    }
}

// ── AnthropicProvider ─────────────────────────────────────────────────────────

#[cfg(feature = "anthropic")]
/// Built-in provider for the Anthropic Messages API.
///
/// Requires the `anthropic` feature flag.
///
/// # Example
/// ```no_run
/// use llm_agent_runtime::providers::AnthropicProvider;
/// let provider = AnthropicProvider::new("sk-ant-...");
/// ```
pub struct AnthropicProvider {
    api_key: String,
    /// Messages API endpoint. Overridable via [`with_base_url`](AnthropicProvider::with_base_url)
    /// to point at a mock server or proxy during testing.
    api_url: String,
    client: reqwest::Client,
    /// Semaphore bounding concurrent SSE background tasks.
    stream_semaphore: std::sync::Arc<tokio::sync::Semaphore>,
    /// Maximum output tokens for `stream_complete`. Falls back to `MAX_TOKENS`
    /// when `None`.
    stream_max_tokens: Option<u32>,
}

#[cfg(feature = "anthropic")]
impl AnthropicProvider {
    const DEFAULT_API_URL: &'static str = "https://api.anthropic.com/v1/messages";
    const API_VERSION: &'static str = "2023-06-01";
    const MAX_TOKENS: u32 = 1024;
    /// Default maximum number of concurrent streaming tasks.
    const DEFAULT_STREAM_CONCURRENCY: usize = 32;

    /// Create a new Anthropic provider with the given API key.
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            api_url: Self::DEFAULT_API_URL.to_owned(),
            client: reqwest::Client::new(),
            stream_semaphore: std::sync::Arc::new(tokio::sync::Semaphore::new(
                Self::DEFAULT_STREAM_CONCURRENCY,
            )),
            stream_max_tokens: None,
        }
    }

    /// Create a provider pointing to a custom API endpoint.
    ///
    /// Useful for testing against a mock server or routing through a proxy.
    pub fn with_base_url(api_key: impl Into<String>, api_url: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            api_url: api_url.into(),
            client: reqwest::Client::new(),
            stream_semaphore: std::sync::Arc::new(tokio::sync::Semaphore::new(
                Self::DEFAULT_STREAM_CONCURRENCY,
            )),
            stream_max_tokens: None,
        }
    }

    /// Create a provider with a custom limit on concurrent streaming tasks.
    ///
    /// Useful when many agents share a single provider and you want to cap
    /// the number of simultaneous SSE parse tasks.
    pub fn with_max_concurrent_streams(api_key: impl Into<String>, max: usize) -> Self {
        Self {
            api_key: api_key.into(),
            api_url: Self::DEFAULT_API_URL.to_owned(),
            client: reqwest::Client::new(),
            stream_semaphore: std::sync::Arc::new(tokio::sync::Semaphore::new(max)),
            stream_max_tokens: None,
        }
    }

    /// Set the maximum output tokens used by [`stream_complete`].
    ///
    /// By default `stream_complete` uses the hard-coded `MAX_TOKENS` constant
    /// (1024).  Call this method to override that value, e.g. when a task
    /// requires longer streamed responses.
    ///
    /// [`stream_complete`]: AnthropicProvider::stream_complete
    pub fn with_stream_max_tokens(mut self, max_tokens: u32) -> Self {
        self.stream_max_tokens = Some(max_tokens);
        self
    }
}

#[cfg(feature = "anthropic")]
#[async_trait]
impl LlmProvider for AnthropicProvider {
    async fn complete(&self, prompt: &str, model: &str) -> Result<String, AgentRuntimeError> {
        self.complete_with_options(prompt, CompletionOptions::new(model))
            .await
    }

    /// Complete with per-request options (max_tokens, temperature).
    ///
    /// Falls back to `Self::MAX_TOKENS` when `options.max_tokens` is not set.
    #[tracing::instrument(skip(self, prompt, options), fields(model = options.model, provider = "anthropic"))]
    async fn complete_with_options(
        &self,
        prompt: &str,
        options: CompletionOptions<'_>,
    ) -> Result<String, AgentRuntimeError> {
        let max_tokens = options
            .max_tokens
            .unwrap_or(Self::MAX_TOKENS as usize) as u32;

        let mut body = serde_json::json!({
            "model": options.model,
            "max_tokens": max_tokens,
            "messages": [{ "role": "user", "content": prompt }]
        });
        if let Some(t) = options.temperature {
            body["temperature"] = serde_json::json!(t);
        }

        let mut req = self
            .client
            .post(&self.api_url)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", Self::API_VERSION)
            .header("content-type", "application/json")
            .json(&body);
        if let Some(timeout) = options.timeout {
            req = req.timeout(timeout);
        }
        let response = req
            .send()
            .await
            .map_err(|e| AgentRuntimeError::Provider(format!("Anthropic request failed: {e}")))?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await.unwrap_or_default();
            return Err(AgentRuntimeError::Provider(format!(
                "Anthropic API error {status}: {text}"
            )));
        }

        let json: serde_json::Value = response
            .json()
            .await
            .map_err(|e| AgentRuntimeError::Provider(format!("Anthropic parse failed: {e}")))?;

        let text = json["content"]
            .as_array()
            .and_then(|arr| arr.first())
            .and_then(|block| block["text"].as_str())
            .ok_or_else(|| {
                AgentRuntimeError::Provider("Anthropic response missing content[0].text".into())
            })?;

        Ok(text.to_owned())
    }

    /// Stream the completion token-by-token using `chunk()` for true streaming.
    ///
    /// Uses `response.chunk()` to consume the SSE body incrementally so tokens
    /// are emitted as they arrive rather than after the entire response has been
    /// buffered, fixing the original `bytes().await` implementation.
    async fn stream_complete(
        &self,
        prompt: &str,
        model: &str,
    ) -> Result<tokio::sync::mpsc::Receiver<Result<String, AgentRuntimeError>>, AgentRuntimeError>
    {
        let max_tokens = self.stream_max_tokens.unwrap_or(Self::MAX_TOKENS);
        let body = serde_json::json!({
            "model": model,
            "max_tokens": max_tokens,
            "stream": true,
            "messages": [{ "role": "user", "content": prompt }]
        });

        let response = self
            .client
            .post(&self.api_url)
            .header("x-api-key", &self.api_key)
            .header("anthropic-version", Self::API_VERSION)
            .header("content-type", "application/json")
            .json(&body)
            .send()
            .await
            .map_err(|e| {
                AgentRuntimeError::Provider(format!("Anthropic stream request failed: {e}"))
            })?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await.unwrap_or_default();
            return Err(AgentRuntimeError::Provider(format!(
                "Anthropic stream API error {status}: {text}"
            )));
        }

        let (tx, rx) = tokio::sync::mpsc::channel::<Result<String, AgentRuntimeError>>(32);

        let permit = std::sync::Arc::clone(&self.stream_semaphore)
            .acquire_owned()
            .await
            .map_err(|_| {
                AgentRuntimeError::Provider("Anthropic stream semaphore closed".into())
            })?;

        // Parse SSE incrementally using chunk() so tokens are emitted as they
        // arrive rather than after the full response body has been buffered.
        tokio::spawn(async move {
            let _permit = permit;
            let mut response = response;
            let mut buffer = String::new();
            loop {
                match response.chunk().await {
                    Ok(Some(chunk)) => {
                        match String::from_utf8(chunk.to_vec()) {
                            Ok(s) => buffer.push_str(&s),
                            Err(e) => {
                                let _ = tx
                                    .send(Err(AgentRuntimeError::Provider(format!(
                                        "Anthropic stream: invalid UTF-8 in chunk: {e}"
                                    ))))
                                    .await;
                                return;
                            }
                        }
                        // Drain complete SSE lines from the buffer.
                        while let Some(newline) = buffer.find('\n') {
                            let line = buffer[..newline].trim().to_owned();
                            buffer = buffer[newline + 1..].to_owned();
                            if let Some(data) = line.strip_prefix("data: ") {
                                if data == "[DONE]" {
                                    return;
                                }
                                if let Ok(json) =
                                    serde_json::from_str::<serde_json::Value>(data)
                                {
                                    if let Some(delta) = json["delta"]["text"].as_str() {
                                        if tx.send(Ok(delta.to_owned())).await.is_err() {
                                            return;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Ok(None) => break,
                    Err(e) => {
                        let _ = tx
                            .send(Err(AgentRuntimeError::Provider(format!(
                                "Anthropic stream chunk error: {e}"
                            ))))
                            .await;
                        return;
                    }
                }
            }
        });

        Ok(rx)
    }
}

// ── OpenAiProvider ────────────────────────────────────────────────────────────

#[cfg(feature = "openai")]
/// Built-in provider for the OpenAI Chat Completions API.
///
/// Also compatible with Azure OpenAI and any OpenAI-compatible endpoint.
/// Requires the `openai` feature flag.
///
/// # Example
/// ```no_run
/// use llm_agent_runtime::providers::OpenAiProvider;
/// let provider = OpenAiProvider::new("sk-...");
/// // For Azure or custom endpoints:
/// let custom = OpenAiProvider::with_base_url("sk-...", "https://my-endpoint/v1");
/// ```
pub struct OpenAiProvider {
    api_key: String,
    base_url: String,
    client: reqwest::Client,
    /// Semaphore bounding concurrent SSE background tasks (item 10).
    stream_semaphore: std::sync::Arc<tokio::sync::Semaphore>,
}

#[cfg(feature = "openai")]
impl OpenAiProvider {
    const DEFAULT_BASE_URL: &'static str = "https://api.openai.com/v1";
    /// Default maximum number of concurrent streaming tasks.
    const DEFAULT_STREAM_CONCURRENCY: usize = 32;

    /// Create a new OpenAI provider with the default base URL.
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            base_url: Self::DEFAULT_BASE_URL.to_owned(),
            client: reqwest::Client::new(),
            stream_semaphore: std::sync::Arc::new(tokio::sync::Semaphore::new(
                Self::DEFAULT_STREAM_CONCURRENCY,
            )),
        }
    }

    /// Create a provider pointing to a custom base URL (e.g. Azure, local models).
    pub fn with_base_url(api_key: impl Into<String>, base_url: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            base_url: base_url.into(),
            client: reqwest::Client::new(),
            stream_semaphore: std::sync::Arc::new(tokio::sync::Semaphore::new(
                Self::DEFAULT_STREAM_CONCURRENCY,
            )),
        }
    }

    /// Create a provider with a custom limit on concurrent streaming tasks.
    pub fn with_max_concurrent_streams(
        api_key: impl Into<String>,
        base_url: impl Into<String>,
        max: usize,
    ) -> Self {
        Self {
            api_key: api_key.into(),
            base_url: base_url.into(),
            client: reqwest::Client::new(),
            stream_semaphore: std::sync::Arc::new(tokio::sync::Semaphore::new(max)),
        }
    }
}

#[cfg(feature = "openai")]
#[async_trait]
impl LlmProvider for OpenAiProvider {
    #[tracing::instrument(skip(self, prompt), fields(model, provider = "openai"))]
    async fn complete(&self, prompt: &str, model: &str) -> Result<String, AgentRuntimeError> {
        self.complete_with_options(prompt, CompletionOptions::new(model))
            .await
    }

    /// Complete with per-request options (max_tokens, temperature, timeout).
    ///
    /// Honours `options.max_tokens` and `options.temperature` when set, falling
    /// back to the API default when unset.
    #[tracing::instrument(skip(self, prompt, options), fields(model = options.model, provider = "openai"))]
    async fn complete_with_options(
        &self,
        prompt: &str,
        options: CompletionOptions<'_>,
    ) -> Result<String, AgentRuntimeError> {
        let url = format!("{}/chat/completions", self.base_url);
        let mut body = serde_json::json!({
            "model": options.model,
            "messages": [{ "role": "user", "content": prompt }]
        });
        if let Some(max_tokens) = options.max_tokens {
            body["max_tokens"] = serde_json::json!(max_tokens);
        }
        if let Some(temp) = options.temperature {
            body["temperature"] = serde_json::json!(temp);
        }

        let mut req = self
            .client
            .post(&url)
            .bearer_auth(&self.api_key)
            .header("content-type", "application/json")
            .json(&body);
        if let Some(timeout) = options.timeout {
            req = req.timeout(timeout);
        }
        let response = req
            .send()
            .await
            .map_err(|e| AgentRuntimeError::Provider(format!("OpenAI request failed: {e}")))?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await.unwrap_or_default();
            return Err(AgentRuntimeError::Provider(format!(
                "OpenAI API error {status}: {text}"
            )));
        }

        let json: serde_json::Value = response
            .json()
            .await
            .map_err(|e| AgentRuntimeError::Provider(format!("OpenAI parse failed: {e}")))?;

        let text = json["choices"]
            .as_array()
            .and_then(|arr| arr.first())
            .and_then(|choice| choice["message"]["content"].as_str())
            .ok_or_else(|| {
                AgentRuntimeError::Provider(
                    "OpenAI response missing choices[0].message.content".into(),
                )
            })?;

        Ok(text.to_owned())
    }

    async fn stream_complete(
        &self,
        prompt: &str,
        model: &str,
    ) -> Result<tokio::sync::mpsc::Receiver<Result<String, AgentRuntimeError>>, AgentRuntimeError>
    {
        let url = format!("{}/chat/completions", self.base_url);
        let body = serde_json::json!({
            "model": model,
            "stream": true,
            "messages": [{ "role": "user", "content": prompt }]
        });

        let mut response = self
            .client
            .post(&url)
            .bearer_auth(&self.api_key)
            .header("content-type", "application/json")
            .json(&body)
            .send()
            .await
            .map_err(|e| {
                AgentRuntimeError::Provider(format!("OpenAI stream request failed: {e}"))
            })?;

        if !response.status().is_success() {
            let status = response.status();
            let text = response.text().await.unwrap_or_default();
            return Err(AgentRuntimeError::Provider(format!(
                "OpenAI stream API error {status}: {text}"
            )));
        }

        let (tx, rx) = tokio::sync::mpsc::channel::<Result<String, AgentRuntimeError>>(32);

        // Item 10 — bound concurrent tasks via semaphore.
        let permit = std::sync::Arc::clone(&self.stream_semaphore)
            .acquire_owned()
            .await
            .map_err(|_| {
                AgentRuntimeError::Provider("OpenAI stream semaphore closed".into())
            })?;

        tokio::spawn(async move {
            let _permit = permit;
            // Incremental chunk-by-chunk reading — avoids buffering the full
            // response body before emitting any tokens.
            let mut buffer = String::new();
            loop {
                match response.chunk().await {
                    Ok(Some(chunk)) => {
                        let text = match std::str::from_utf8(&chunk) {
                            Ok(t) => t,
                            Err(e) => {
                                let _ = tx
                                    .send(Err(AgentRuntimeError::Provider(format!(
                                        "OpenAI stream chunk is not valid UTF-8: {e}"
                                    ))))
                                    .await;
                                return;
                            }
                        };
                        buffer.push_str(text);
                        // Drain complete SSE lines from the buffer.
                        while let Some(newline) = buffer.find('\n') {
                            let line: String = buffer.drain(..=newline).collect();
                            let line = line.trim_end_matches(['\r', '\n']);
                            if let Some(data) = line.strip_prefix("data: ") {
                                if data == "[DONE]" {
                                    return;
                                }
                                if let Ok(json) =
                                    serde_json::from_str::<serde_json::Value>(data)
                                {
                                    if let Some(content) = json["choices"]
                                        .as_array()
                                        .and_then(|c| c.first())
                                        .and_then(|c| c["delta"]["content"].as_str())
                                    {
                                        if tx.send(Ok(content.to_owned())).await.is_err() {
                                            return;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Ok(None) => break,
                    Err(e) => {
                        let _ = tx
                            .send(Err(AgentRuntimeError::Provider(format!(
                                "OpenAI stream read failed: {e}"
                            ))))
                            .await;
                        return;
                    }
                }
            }
        });

        Ok(rx)
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    /// A stub provider for testing.
    struct StubProvider {
        response: String,
    }

    #[async_trait]
    impl LlmProvider for StubProvider {
        async fn complete(&self, _prompt: &str, _model: &str) -> Result<String, AgentRuntimeError> {
            Ok(self.response.clone())
        }
        // Uses default stream_complete implementation which wraps complete().
    }

    #[tokio::test]
    async fn test_stub_provider_returns_configured_response() {
        let p = StubProvider {
            response: "hello".into(),
        };
        let result = p.complete("prompt", "stub-model").await.unwrap();
        assert_eq!(result, "hello");
    }

    #[tokio::test]
    async fn test_llm_provider_is_object_safe() {
        let p: Arc<dyn LlmProvider> = Arc::new(StubProvider {
            response: "ok".into(),
        });
        let result = p.complete("test", "model").await.unwrap();
        assert_eq!(result, "ok");
    }

    #[tokio::test]
    async fn test_stub_provider_ignores_model_parameter() {
        let p = StubProvider {
            response: "42".into(),
        };
        let r1 = p.complete("q", "model-a").await.unwrap();
        let r2 = p.complete("q", "model-b").await.unwrap();
        assert_eq!(r1, r2);
    }

    #[tokio::test]
    async fn test_stub_provider_stream_returns_single_chunk() {
        let p = StubProvider {
            response: "hello world".into(),
        };
        let mut rx = p.stream_complete("prompt", "model").await.unwrap();
        let mut collected = String::new();
        while let Some(chunk) = rx.recv().await {
            collected.push_str(&chunk.unwrap());
        }
        assert_eq!(collected, "hello world");
    }

    #[tokio::test]
    async fn test_stream_receiver_closes_after_completion() {
        let p = StubProvider {
            response: "done".into(),
        };
        let mut rx = p.stream_complete("prompt", "model").await.unwrap();
        // Drain all chunks
        while let Some(_chunk) = rx.recv().await {}
        // Channel should now be closed — next recv returns None
        assert!(rx.recv().await.is_none());
    }
}