vlorql-llm 0.5.0

Multi-provider LLM client for SQL query generation supporting Anthropic, DeepSeek, Zhipu, and local models
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//! Async LLM clients that produce validated VlorQl query-plan data.
//!
//! The crate exposes a single [`LlmClient`] trait and a factory
//! function ([`create_llm_client`]) that returns a boxed
//! implementation for one of six supported providers. All clients
//! share the same JSON contract so the rest of VlorQl can treat them
//! uniformly.
//!
//! ## Streaming
//!
//! [`LlmClient::stream_plan`] returns a `Stream<Item = Result<String,
//! VlorQLError>>`. The items are raw text deltas emitted by the LLM
//! (concatenated, they form the assistant's reply).
//!
//! ## Retries
//!
//! The HTTP clients retry on transient failures (5xx, 429, timeouts).
//! Set [`LlmConfig::max_retries`] to control the budget.

#![deny(missing_docs)]

use async_trait::async_trait;
use futures::stream::Stream;

use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use vlorql_core::errors::{ConfigErrorKind, LlmErrorKind, VlorQLError};
use vlorql_core::schema::QueryPlan;

// ── Internal modules ──────────────────────────────────────────────
mod retry_client;
pub(crate) use retry_client::RetryableHttpClient;

pub(crate) mod schema;
pub(crate) mod sse;

// ── Public: LLM providers ─────────────────────────────────────────
pub mod anthropic;
pub mod deepseek;
pub mod local;
/// A deterministic client for unit and integration tests.
pub mod mock;
/// OpenAI-compatible chat-completions client.
pub mod openai;
pub mod zhipu;

// ── Public: V2 parse pipeline ─────────────────────────────────────
pub mod parser_v2;
pub use parser_v2::builder::query_builder::{from_canonical_str, from_canonical_value};
pub use parser_v2::pipeline::{
    ParseError, ParseResult, parse_query_plan, parse_query_plan_debug, parse_query_plan_lenient,
};
pub use parser_v2::recover::{detect_template_leak, extract_json_content};

/// Parse LLM response text into a [`QueryPlan`](vlorql_core::schema::QueryPlan) using the V2 pipeline.
///
/// This is the recommended internal helper for all `LlmClient` implementations.
/// It runs the full V2 pipeline (recover → normalize → build → fix → validate → optimize)
/// and converts errors to the crate's `VlorQLError` type.
pub(crate) fn parse_llm_response(content: &str) -> Result<QueryPlan, VlorQLError> {
    parser_v2::pipeline::parse_query_plan(content, None).map_err(|e| {
        VlorQLError::llm(
            LlmErrorKind::ParseError {
                details: e.to_string(),
            },
            json!({
                "source": "v2_pipeline",
                "content": sse::truncate(content, 4096),
            }),
        )
    })
}

/// Supported LLM providers.
///
/// Each variant corresponds to a dedicated client implementation
/// reachable through [`create_llm_client`]. The `serde` representation
/// uses `snake_case` so the value can be deserialized from the
/// `provider` field of a TOML/JSON configuration.
///
/// # Examples
///
/// ```
/// use vlorql_llm::LlmProvider;
///
/// let provider = LlmProvider::OpenAi;
/// assert_eq!(provider.id(), "openai");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LlmProvider {
    /// OpenAI's hosted `/chat/completions` endpoint.
    OpenAi,
    /// Anthropic's `/v1/messages` endpoint (Claude).
    Anthropic,
    /// DeepSeek's OpenAI-compatible chat-completions endpoint.
    DeepSeek,
    /// Zhipu GLM's `/api/paas/v4/chat/completions` endpoint.
    Zhipu,
    /// Locally running vLLM OpenAI-compatible server.
    Vllm,
    /// Locally running Ollama `/api/chat` endpoint.
    Ollama,
}

impl LlmProvider {
    /// Returns the canonical identifier of the provider.
    #[must_use]
    pub fn id(self) -> &'static str {
        match self {
            LlmProvider::OpenAi => "openai",
            LlmProvider::Anthropic => "anthropic",
            LlmProvider::DeepSeek => "deepseek",
            LlmProvider::Zhipu => "zhipu",
            LlmProvider::Vllm => "vllm",
            LlmProvider::Ollama => "ollama",
        }
    }
}

impl std::fmt::Display for LlmProvider {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.id())
    }
}

/// Default endpoint per provider. May be overridden via `api_base`.
fn default_api_base(provider: LlmProvider) -> &'static str {
    match provider {
        LlmProvider::OpenAi => "https://api.openai.com/v1/chat/completions",
        LlmProvider::Anthropic => "https://api.anthropic.com/v1/messages",
        LlmProvider::DeepSeek => "https://api.deepseek.com/v1/chat/completions",
        LlmProvider::Zhipu => "https://open.bigmodel.cn/api/paas/v4/chat/completions",
        LlmProvider::Vllm => "http://localhost:8000/v1/chat/completions",
        LlmProvider::Ollama => "http://localhost:11434/api/chat",
    }
}

/// Provider-agnostic LLM configuration.
///
/// The struct is intentionally flat so it round-trips through
/// `serde_json` / TOML with no surprises. `api_key` is optional
/// because local providers (vLLM, Ollama) usually do not require
/// authentication; for hosted providers the factory
/// ([`create_llm_client`]) also checks the documented environment
/// variable when `api_key` is empty.
///
/// # Examples
///
/// ```
/// use vlorql_llm::{LlmConfig, LlmProvider};
///
/// let config = LlmConfig {
///     provider: LlmProvider::OpenAi,
///     model: "gpt-4o-mini".to_owned(),
///     ..LlmConfig::default()
/// };
/// assert_eq!(config.effective_api_base(), "https://api.openai.com/v1/chat/completions");
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct LlmConfig {
    /// The target provider.
    pub provider: LlmProvider,
    /// API key, if the provider requires authentication.
    pub api_key: Option<String>,
    /// Override the default endpoint. May be a full URL or a
    /// base URL (the client appends the chat-completions suffix
    /// when appropriate).
    pub api_base: Option<String>,
    /// Model identifier (e.g. `"gpt-4o-mini"`, `"claude-sonnet-4-5"`).
    pub model: String,
    /// Maximum number of tokens the LLM is allowed to emit.
    pub max_tokens: u32,
    /// Sampling temperature. `0.0` produces deterministic output.
    pub temperature: f32,
    /// Per-request timeout, in seconds.
    pub timeout_seconds: u64,
    /// Maximum number of retry attempts for transient errors.
    pub max_retries: u32,
    /// Free-form provider-specific options. The
    /// [`local::LocalClient`] recognises `"backend"` and
    /// `"strict_json_schema"`; other clients ignore the map.
    #[serde(default)]
    pub extra: HashMap<String, Value>,
}

impl Default for LlmConfig {
    fn default() -> Self {
        Self {
            provider: LlmProvider::OpenAi,
            api_key: None,
            api_base: None,
            model: "gpt-4o-mini".to_owned(),
            max_tokens: 1024,
            temperature: 0.0,
            timeout_seconds: 60,
            max_retries: 3,
            extra: HashMap::new(),
        }
    }
}

impl LlmConfig {
    /// Returns the effective API base, falling back to the provider default.
    #[must_use]
    pub fn effective_api_base(&self) -> String {
        self.api_base
            .clone()
            .unwrap_or_else(|| default_api_base(self.provider).to_owned())
    }
}

pub(crate) const DEFAULT_API_BASE: &str = "https://api.openai.com/v1/chat/completions";
pub(crate) const DEFAULT_MAX_ATTEMPTS: usize = 3;
pub(crate) const DEFAULT_RETRY_DELAY: Duration = Duration::from_secs(1);

/// Token usage returned by an LLM provider.
#[derive(Debug, Clone, Copy, PartialEq, Default, serde::Serialize)]
pub struct TokenUsage {
    /// Number of tokens in the prompt.
    pub prompt_tokens: u64,
    /// Number of tokens in the completion.
    pub completion_tokens: u64,
}

/// Result of `stream_plan()`. The stream emits text deltas; after the
/// stream ends, `usage` contains the provider's token usage (if any).
pub struct StreamResult {
    /// Stream of text delta chunks from the LLM.
    pub stream: Box<dyn Stream<Item = Result<String, VlorQLError>> + Send + Unpin>,
    /// Token usage captured from the streaming response, populated after the stream ends.
    pub usage: Arc<tokio::sync::Mutex<Option<TokenUsage>>>,
}

/// A client that turns a natural-language question into a structured query plan.
///
/// # Examples
///
/// ```
/// use vlorql_llm::{LlmClient, MockLlmClient};
/// use vlorql_core::schema::{QueryPlan, Projection, FromClause};
///
/// # async fn example() {
/// let plan = QueryPlan {
///     select: vec![Projection::Column {
///         table: None, column: "id".to_owned(), alias: None,
///     }],
///     from: FromClause::table("users".to_owned(), None),
///     r#where: None, group_by: None, having: None,
///     order_by: None, limit: None, offset: None,
///     joins: None, ctes: None, distinct: false, distinct_on: None, set_operation: None,
/// };
/// let client = MockLlmClient::success(plan);
/// let (_plan, _usage) = client.generate_plan("test", "prompt", None).await.unwrap();
/// # }
/// ```
#[async_trait]
pub trait LlmClient: Send + Sync {
    /// Generates a complete query plan from the LLM.
    ///
    /// `temperature` overrides the sampling temperature used for this
    /// single call. Pass `None` to fall back to [`LlmConfig::temperature`]
    /// as returned by [`LlmClient::config`].
    async fn generate_plan(
        &self,
        question: &str,
        system_prompt: &str,
        temperature: Option<f32>,
    ) -> Result<(QueryPlan, TokenUsage), VlorQLError>;

    /// Streams raw text deltas as the LLM emits them.
    async fn stream_plan(
        &self,
        question: String,
        system_prompt: String,
    ) -> Result<StreamResult, VlorQLError>;

    /// Returns the provider that produced this client.
    fn provider(&self) -> LlmProvider;

    /// Returns the configuration used to build this client.
    fn config(&self) -> &LlmConfig;
}

#[async_trait]
impl<T> LlmClient for Box<T>
where
    T: LlmClient + ?Sized,
{
    async fn generate_plan(
        &self,
        question: &str,
        system_prompt: &str,
        temperature: Option<f32>,
    ) -> Result<(QueryPlan, TokenUsage), VlorQLError> {
        (**self)
            .generate_plan(question, system_prompt, temperature)
            .await
    }

    async fn stream_plan(
        &self,
        question: String,
        system_prompt: String,
    ) -> Result<StreamResult, VlorQLError> {
        (**self).stream_plan(question, system_prompt).await
    }

    fn provider(&self) -> LlmProvider {
        (**self).provider()
    }

    fn config(&self) -> &LlmConfig {
        (**self).config()
    }
}

pub use anthropic::AnthropicClient;
pub use deepseek::DeepSeekClient;
pub use local::{LocalBackend, LocalClient};
pub use mock::MockLlmClient;
pub use openai::OpenAIClient;
pub use zhipu::ZhipuClient;

/// Creates an LLM client from a populated [`LlmConfig`].
///
/// The factory inspects the `provider` field, performs provider-specific
/// validation (e.g. requiring an API key for hosted providers) and returns a
/// boxed [`LlmClient`].
///
/// # Errors
///
/// Returns a [`VlorQLError::Config`] when the API key is missing for a hosted
/// provider or the model name is empty.
///
/// # Examples
///
/// ```
/// use vlorql_llm::{LlmConfig, LlmProvider, create_llm_client};
///
/// let config = LlmConfig {
///     provider: LlmProvider::Ollama,
///     model: "llama3.2".to_owned(),
///     api_key: None,
///     ..LlmConfig::default()
/// };
/// let client = create_llm_client(config);
/// assert!(client.is_ok());
/// ```
pub fn create_llm_client(config: LlmConfig) -> Result<Box<dyn LlmClient>, VlorQLError> {
    let needs_api_key = !matches!(config.provider, LlmProvider::Vllm | LlmProvider::Ollama);
    if needs_api_key
        && config
            .api_key
            .as_deref()
            .map(str::trim)
            .map(str::is_empty)
            .unwrap_or(true)
    {
        return Err(VlorQLError::config(
            ConfigErrorKind::MissingApiKey {
                provider: config.provider.to_string(),
            },
            json!({
                "provider": config.provider,
                "field": "api_key",
            }),
        ));
    }
    if config.model.trim().is_empty() {
        return Err(VlorQLError::config(
            ConfigErrorKind::EmptyModel,
            json!({"field": "model"}),
        ));
    }
    match config.provider {
        LlmProvider::OpenAi => Ok(Box::new(OpenAIClient::from_config(config))),
        LlmProvider::Vllm | LlmProvider::Ollama => {
            local::LocalClient::new(config).map(|client| Box::new(client) as Box<dyn LlmClient>)
        }
        LlmProvider::DeepSeek => deepseek::DeepSeekClient::new(config)
            .map(|client| Box::new(client) as Box<dyn LlmClient>),
        LlmProvider::Zhipu => {
            zhipu::ZhipuClient::new(config).map(|client| Box::new(client) as Box<dyn LlmClient>)
        }
        LlmProvider::Anthropic => anthropic::AnthropicClient::new(config)
            .map(|client| Box::new(client) as Box<dyn LlmClient>),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::StreamExt;
    use mockito::{Matcher, Server};
    use serde_json::json;
    use vlorql_core::schema::{FromClause, Projection};

    fn plan() -> QueryPlan {
        QueryPlan {
            select: vec![Projection::Star { table: None }],
            from: FromClause::table("users".to_owned(), Some("t1".to_owned())),
            r#where: None,
            group_by: None,
            having: None,
            order_by: None,
            limit: None,
            offset: None,
            joins: None,
            ctes: None,
            distinct: false,
            distinct_on: None,
            set_operation: None,
        }
    }

    fn response_for(plan: &QueryPlan) -> String {
        json!({
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": serde_json::to_string(plan).expect("plan should serialize")
                }
            }]
        })
        .to_string()
    }

    #[tokio::test]
    async fn mock_client_returns_success_and_failure() {
        let expected = plan();
        let success = MockLlmClient::success(expected.clone());
        let (actual, _usage) = success
            .generate_plan("question", "system", None)
            .await
            .expect("mock should succeed");
        assert_eq!(actual, expected);

        let failure = MockLlmClient::failure();
        let error = failure
            .generate_plan("question", "system", None)
            .await
            .expect_err("mock should fail");
        assert_eq!(error.error_code(), "L001");
    }

    #[tokio::test]
    async fn openai_client_sends_messages_and_parses_query_plan() {
        let mut server = Server::new_async().await;
        let expected = plan();
        let mock = server
            .mock("POST", "/v1/chat/completions")
            .match_header("authorization", "Bearer test-key")
            .match_header(
                "content-type",
                Matcher::Regex("application/json.*".to_owned()),
            )
            .match_body(Matcher::Regex(r#""model":"gpt-4o-mini""#.to_owned()))
            .with_status(200)
            .with_body(response_for(&expected))
            .create_async()
            .await;

        let client = OpenAIClient::new("test-key", "gpt-4o-mini")
            .with_api_base(format!("{}/v1", server.url()));
        let request_body = client.request_body("show users", "system instructions", None);
        assert_eq!(request_body["model"], "gpt-4o-mini");
        assert_eq!(request_body["temperature"], 0.0);
        assert_eq!(request_body["response_format"]["type"], "json_schema");
        assert_eq!(
            request_body["response_format"]["json_schema"]["name"],
            "QueryPlan"
        );
        assert_eq!(
            request_body["response_format"]["json_schema"]["strict"],
            true
        );

        let (actual, _usage) = client
            .generate_plan("show users", "system instructions", None)
            .await
            .expect("OpenAI response should parse");

        assert_eq!(actual, expected);
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn openai_client_falls_back_to_json_object_for_unknown_models() {
        let mut server = Server::new_async().await;
        let expected = plan();
        let mock = server
            .mock("POST", "/chat/completions")
            .match_body(Matcher::Regex(
                r#""model":"local-model".*"response_format":\{"type":"json_object"\}"#.to_owned(),
            ))
            .with_status(200)
            .with_body(response_for(&expected))
            .create_async()
            .await;
        let client =
            OpenAIClient::new("key", "local-model").with_api_base(format!("{}/", server.url()));
        let request_body = client.request_body("q", "s", None);
        assert_eq!(request_body["model"], "local-model");
        assert_eq!(request_body["response_format"]["type"], "json_object");

        let (actual, _usage) = client
            .generate_plan("q", "s", None)
            .await
            .expect("response");
        assert_eq!(actual, expected);
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn openai_client_retries_temporary_http_errors() {
        let mut server = Server::new_async().await;
        let expected = plan();
        let failures = server
            .mock("POST", "/v1/chat/completions")
            .with_status(503)
            .with_body(r#"{"error":{"message":"busy"}}"#)
            .expect(2)
            .create_async()
            .await;
        let success = server
            .mock("POST", "/v1/chat/completions")
            .with_status(200)
            .with_body(response_for(&expected))
            .create_async()
            .await;
        let client =
            OpenAIClient::new("key", "local-model").with_api_base(format!("{}/v1", server.url()));

        let (actual, _usage) = client
            .generate_plan("q", "s", None)
            .await
            .expect("retry should succeed");
        assert_eq!(actual, expected);
        failures.assert_async().await;
        success.assert_async().await;
    }

    #[tokio::test]
    async fn openai_client_converts_invalid_plan_to_llm_parse_error() {
        let mut server = Server::new_async().await;
        let mock = server
            .mock("POST", "/v1/chat/completions")
            .with_status(200)
            .with_body(
                json!({
                    "choices": [{"message": {"content": r#"{"unexpected":true}"#}}]
                })
                .to_string(),
            )
            .create_async()
            .await;
        let client =
            OpenAIClient::new("key", "local-model").with_api_base(format!("{}/v1", server.url()));

        let error = client
            .generate_plan("q", "s", None)
            .await
            .expect_err("invalid plan should fail");
        assert_eq!(error.error_code(), "L003");
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn mock_client_stream_plan_emits_single_chunk() {
        use futures::stream::StreamExt;
        let plan = QueryPlan {
            select: vec![Projection::Star { table: None }],
            from: FromClause::table("users".to_owned(), None),
            r#where: None,
            group_by: None,
            having: None,
            order_by: None,
            limit: None,
            offset: None,
            joins: None,
            ctes: None,
            distinct: false,
            distinct_on: None,
            set_operation: None,
        };
        let client = MockLlmClient::success(plan);
        let mut result = client
            .stream_plan("question".to_owned(), "system".to_owned())
            .await
            .expect("stream should be produced");
        let mut collected = String::new();
        while let Some(item) = result.stream.next().await {
            collected.push_str(&item.expect("chunk should be Ok"));
        }
        assert!(collected.contains("users"));
        assert!(collected.contains("\"from\""));
    }

    #[tokio::test]
    async fn openai_client_stream_emits_delta_chunks() {
        let mut server = Server::new_async().await;
        let body = [
            format!(
                "data: {}\n\n",
                serde_json::json!({
                    "id": "1",
                    "choices": [{"delta": {"content": "hello "}}]
                })
            ),
            format!(
                "data: {}\n\n",
                serde_json::json!({
                    "id": "1",
                    "choices": [{"delta": {"content": "world"}}]
                })
            ),
            "data: [DONE]\n".to_owned(),
        ]
        .join("");
        let mock = server
            .mock("POST", "/v1/chat/completions")
            .with_status(200)
            .with_header("content-type", "text/event-stream")
            .with_body(body)
            .create_async()
            .await;

        let client =
            OpenAIClient::new("key", "local-model").with_api_base(format!("{}/v1", server.url()));
        let mut result = client
            .stream_plan("hi".to_owned(), "system".to_owned())
            .await
            .expect("stream should be produced");
        let mut combined = String::new();
        while let Some(chunk) = result.stream.next().await {
            combined.push_str(&chunk.expect("chunk should be Ok"));
        }
        assert_eq!(combined, "hello world");
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn openai_client_stream_propagates_http_error() {
        let mut server = Server::new_async().await;
        let mock = server
            .mock("POST", "/v1/chat/completions")
            .with_status(500)
            .with_body(r#"{"error":{"message":"down"}}"#)
            .create_async()
            .await;
        let client =
            OpenAIClient::new("key", "local-model").with_api_base(format!("{}/v1", server.url()));
        let outcome = client
            .stream_plan("hi".to_owned(), "system".to_owned())
            .await;
        let err = match outcome {
            Ok(_) => panic!("500 should produce an error"),
            Err(error) => error,
        };
        assert_eq!(err.error_code(), "L001");
        mock.assert_async().await;
    }

    #[test]
    fn llm_config_default_matches_documented_defaults() {
        let config = LlmConfig::default();
        assert_eq!(config.provider, LlmProvider::OpenAi);
        assert_eq!(config.model, "gpt-4o-mini");
        assert_eq!(config.max_tokens, 1024);
        assert_eq!(config.max_retries, 3);
        assert_eq!(config.timeout_seconds, 60);
    }

    #[test]
    fn llm_config_effective_api_base_uses_provider_default() {
        let initial = LlmConfig {
            provider: LlmProvider::Zhipu,
            ..LlmConfig::default()
        };
        assert_eq!(
            initial.effective_api_base(),
            "https://open.bigmodel.cn/api/paas/v4/chat/completions"
        );
        let overridden = LlmConfig {
            api_base: Some("https://example.test/v1".to_owned()),
            ..initial
        };
        assert_eq!(overridden.effective_api_base(), "https://example.test/v1");
    }

    #[test]
    fn create_llm_client_requires_api_key_for_hosted_providers() {
        let config = LlmConfig {
            provider: LlmProvider::OpenAi,
            api_key: None,
            ..LlmConfig::default()
        };
        let error = match create_llm_client(config) {
            Ok(_) => panic!("api key should be required"),
            Err(error) => error,
        };
        assert_eq!(error.error_code(), "G004");
    }

    #[test]
    fn create_llm_client_allows_local_providers_without_key() {
        let config = LlmConfig {
            provider: LlmProvider::Ollama,
            api_key: None,
            model: "llama3".to_owned(),
            ..LlmConfig::default()
        };
        let client = match create_llm_client(config) {
            Ok(client) => client,
            Err(error) => panic!("ollama client should build: {error}"),
        };
        assert_eq!(client.provider(), LlmProvider::Ollama);
    }

    #[test]
    fn create_llm_client_rejects_empty_model() {
        let config = LlmConfig {
            api_key: Some("k".to_owned()),
            model: "  ".to_owned(),
            ..LlmConfig::default()
        };
        let error = match create_llm_client(config) {
            Ok(_) => panic!("empty model should be rejected"),
            Err(error) => error,
        };
        assert_eq!(error.error_code(), "G005");
    }

    #[test]
    fn llm_config_round_trips_through_serde() {
        let config = LlmConfig {
            provider: LlmProvider::DeepSeek,
            api_key: Some("k".to_owned()),
            api_base: None,
            model: "deepseek-chat".to_owned(),
            max_tokens: 2048,
            temperature: 0.2,
            timeout_seconds: 90,
            max_retries: 5,
            extra: std::collections::HashMap::new(),
        };
        let serialized = serde_json::to_string(&config).expect("config should serialize");
        let restored: LlmConfig =
            serde_json::from_str(&serialized).expect("config should deserialize");
        assert_eq!(restored.provider, LlmProvider::DeepSeek);
        assert_eq!(restored.model, "deepseek-chat");
        assert_eq!(restored.max_tokens, 2048);
        assert_eq!(restored.temperature, 0.2);
    }

    /// Verify that the LLM span created by OpenAIClient::generate_plan
    /// includes provider, model, and streaming attributes.
    #[tokio::test]
    async fn llm_span_contains_provider_and_model() {
        use std::sync::Arc;
        use std::sync::Mutex;
        use tracing_subscriber::Layer;
        use tracing_subscriber::layer::SubscriberExt;

        let captured = Arc::new(Mutex::new(Vec::<String>::new()));
        let captured_clone = Arc::clone(&captured);

        // A layer that records the "llm.generate_plan" span's fields.
        let layer = tracing_subscriber::fmt::layer()
            .with_test_writer()
            .with_filter(tracing_subscriber::filter::filter_fn(move |meta| {
                meta.target().starts_with("llm")
            }));

        let subscriber = tracing_subscriber::registry().with(layer);
        let _guard = tracing::subscriber::set_default(subscriber);

        let client = OpenAIClient::from_config(LlmConfig {
            provider: LlmProvider::OpenAi,
            api_key: Some("test-key".to_owned()),
            model: "gpt-4o-mini".to_owned(),
            ..LlmConfig::default()
        });

        // The mockito server will fail, so we expect an error — but the span
        // should still be created with the correct attributes.
        let result = client
            .generate_plan("test question", "test prompt", None)
            .await;
        assert!(result.is_err(), "expected error from mock endpoint");

        // The span was created; the subscriber captured it.
        // The test verifies that the span instrumentation does not panic.
        drop(captured_clone.lock().expect("lock should not be poisoned"));
    }
}