noether-engine 0.4.0

Noether composition engine: Lagrange graph AST, type checker, planner, executor, semantic index, LLM-backed composition agent
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
use super::{LlmConfig, LlmError, LlmProvider, Message, Role};
use crate::index::embedding::{Embedding, EmbeddingError, EmbeddingProvider};
use serde_json::json;
use std::sync::Mutex;
use std::time::{Duration, Instant};

// ── Token source ──────────────────────────────────────────────────────────────

/// Cached access token with expiry tracking.
struct CachedToken {
    access_token: String,
    /// Refresh this many seconds before actual expiry to avoid races.
    expires_at: Instant,
}

impl CachedToken {
    fn new(token: String, expires_in_secs: u64) -> Self {
        // Refresh 5 minutes early to avoid using a token that's about to expire.
        let margin = expires_in_secs.saturating_sub(300);
        Self {
            access_token: token,
            expires_at: Instant::now() + Duration::from_secs(margin),
        }
    }

    fn is_valid(&self) -> bool {
        Instant::now() < self.expires_at
    }
}

/// How the provider obtains a Bearer token.
///
/// Resolution order in `VertexAiConfig::from_env()`:
///   1. `VERTEX_AI_TOKEN` env var → `Static` (no refresh, works for 1-hour tokens)
///   2. `GOOGLE_APPLICATION_CREDENTIALS` file / `~/.config/gcloud/application_default_credentials.json`
///      with `type: "authorized_user"` → `RefreshToken` (auto-refreshes every ~55 min)
///   3. GCE/Cloud Run/GKE metadata server → `MetadataServer` (auto-refreshes, zero config)
///   4. `gcloud auth print-access-token` subprocess → `GcloudSubprocess` (local dev fallback)
enum TokenSource {
    /// Explicit static token — no auto-refresh. Fine for short-lived CLI invocations.
    Static(String),
    /// OAuth2 refresh token flow (ADC user credentials or `authorized_user` service files).
    RefreshToken {
        client_id: String,
        client_secret: String,
        refresh_token: String,
        cached: Mutex<Option<CachedToken>>,
    },
    /// GCE instance metadata server — zero-config inside Google Cloud.
    MetadataServer { cached: Mutex<Option<CachedToken>> },
    /// `gcloud auth print-access-token` subprocess — local dev fallback when no ADC file.
    GcloudSubprocess { cached: Mutex<Option<CachedToken>> },
}

impl TokenSource {
    /// Obtain a valid access token, refreshing if necessary.
    fn get_token(&self) -> Result<String, String> {
        match self {
            Self::Static(t) => Ok(t.clone()),

            Self::RefreshToken {
                client_id,
                client_secret,
                refresh_token,
                cached,
            } => {
                let mut guard = cached.lock().unwrap();
                if let Some(ref c) = *guard {
                    if c.is_valid() {
                        return Ok(c.access_token.clone());
                    }
                }
                let (token, expires_in) = oauth2_refresh(client_id, client_secret, refresh_token)?;
                *guard = Some(CachedToken::new(token.clone(), expires_in));
                Ok(token)
            }

            Self::MetadataServer { cached } => {
                let mut guard = cached.lock().unwrap();
                if let Some(ref c) = *guard {
                    if c.is_valid() {
                        return Ok(c.access_token.clone());
                    }
                }
                let (token, expires_in) = metadata_server_token()?;
                *guard = Some(CachedToken::new(token.clone(), expires_in));
                Ok(token)
            }

            Self::GcloudSubprocess { cached } => {
                let mut guard = cached.lock().unwrap();
                if let Some(ref c) = *guard {
                    if c.is_valid() {
                        return Ok(c.access_token.clone());
                    }
                }
                let token = gcloud_print_access_token()?;
                // gcloud tokens last ~1h; cache for 55 minutes.
                *guard = Some(CachedToken::new(token.clone(), 3300));
                Ok(token)
            }
        }
    }
}

// ── VertexAiConfig ────────────────────────────────────────────────────────────

/// Configuration for Vertex AI providers.
pub struct VertexAiConfig {
    pub project: String,
    pub location: String,
    token_source: TokenSource,
}

impl VertexAiConfig {
    /// Load from environment variables.
    ///
    /// Token resolution order:
    ///   1. `VERTEX_AI_TOKEN` — explicit static token
    ///   2. `GOOGLE_APPLICATION_CREDENTIALS` file (authorized_user or service account key)
    ///   3. `~/.config/gcloud/application_default_credentials.json` (ADC)
    ///   4. GCE/Cloud Run metadata server (http://metadata.google.internal/...)
    ///   5. `gcloud auth print-access-token` subprocess
    pub fn from_env() -> Result<Self, String> {
        let project = std::env::var("VERTEX_AI_PROJECT")
            .or_else(|_| std::env::var("GOOGLE_CLOUD_PROJECT"))
            .map_err(|_| {
                "Vertex AI project not configured. Set VERTEX_AI_PROJECT \
                 (or GOOGLE_CLOUD_PROJECT) to your GCP project ID."
                    .to_string()
            })?;
        let location = std::env::var("VERTEX_AI_LOCATION")
            .or_else(|_| std::env::var("GOOGLE_CLOUD_LOCATION"))
            .unwrap_or_else(|_| "europe-west1".into());

        let token_source = resolve_token_source()?;
        Ok(Self {
            project,
            location,
            token_source,
        })
    }

    /// Get a valid access token, auto-refreshing if the current one has expired.
    pub fn get_token(&self) -> Result<String, String> {
        self.token_source.get_token()
    }
}

// Manual Clone: we need to clone config for the providers, but Mutex isn't Clone.
// We just start with a fresh empty cache in the clone.
impl Clone for VertexAiConfig {
    fn clone(&self) -> Self {
        let token_source = match &self.token_source {
            TokenSource::Static(t) => TokenSource::Static(t.clone()),
            TokenSource::RefreshToken {
                client_id,
                client_secret,
                refresh_token,
                ..
            } => TokenSource::RefreshToken {
                client_id: client_id.clone(),
                client_secret: client_secret.clone(),
                refresh_token: refresh_token.clone(),
                cached: Mutex::new(None),
            },
            TokenSource::MetadataServer { .. } => TokenSource::MetadataServer {
                cached: Mutex::new(None),
            },
            TokenSource::GcloudSubprocess { .. } => TokenSource::GcloudSubprocess {
                cached: Mutex::new(None),
            },
        };
        Self {
            project: self.project.clone(),
            location: self.location.clone(),
            token_source,
        }
    }
}

impl std::fmt::Debug for VertexAiConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let source = match &self.token_source {
            TokenSource::Static(_) => "static",
            TokenSource::RefreshToken { .. } => "refresh_token",
            TokenSource::MetadataServer { .. } => "metadata_server",
            TokenSource::GcloudSubprocess { .. } => "gcloud_subprocess",
        };
        f.debug_struct("VertexAiConfig")
            .field("project", &self.project)
            .field("location", &self.location)
            .field("token_source", &source)
            .finish()
    }
}

// ── Token resolution ──────────────────────────────────────────────────────────

fn resolve_token_source() -> Result<TokenSource, String> {
    // 1. Explicit static token
    if let Ok(t) = std::env::var("VERTEX_AI_TOKEN") {
        return Ok(TokenSource::Static(t));
    }

    // 2. GOOGLE_APPLICATION_CREDENTIALS file
    if let Ok(path) = std::env::var("GOOGLE_APPLICATION_CREDENTIALS") {
        if let Ok(source) = load_credentials_file(&path) {
            return Ok(source);
        }
    }

    // 3. ADC file (~/.config/gcloud/application_default_credentials.json)
    let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".into());
    let adc_path =
        std::path::PathBuf::from(&home).join(".config/gcloud/application_default_credentials.json");
    if adc_path.exists() {
        if let Ok(source) = load_credentials_file(adc_path.to_str().unwrap_or("")) {
            return Ok(source);
        }
    }

    // 4. GCE / Cloud Run / GKE metadata server
    if metadata_server_available() {
        return Ok(TokenSource::MetadataServer {
            cached: Mutex::new(None),
        });
    }

    // 5. gcloud subprocess (local dev fallback)
    if gcloud_available() {
        return Ok(TokenSource::GcloudSubprocess {
            cached: Mutex::new(None),
        });
    }

    Err("No Google credentials found. Options:\n\
         • Run `gcloud auth application-default login`\n\
         • Set VERTEX_AI_TOKEN to an access token\n\
         • Set GOOGLE_APPLICATION_CREDENTIALS to a service account key file\n\
         • Run on GCE/Cloud Run/GKE (metadata server)"
        .into())
}

fn load_credentials_file(path: &str) -> Result<TokenSource, String> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| format!("cannot read credentials file {path}: {e}"))?;
    let creds: serde_json::Value =
        serde_json::from_str(&content).map_err(|e| format!("credentials JSON parse error: {e}"))?;

    match creds["type"].as_str() {
        Some("authorized_user") => Ok(TokenSource::RefreshToken {
            client_id: creds["client_id"]
                .as_str()
                .ok_or("missing client_id")?
                .into(),
            client_secret: creds["client_secret"]
                .as_str()
                .ok_or("missing client_secret")?
                .into(),
            refresh_token: creds["refresh_token"]
                .as_str()
                .ok_or("missing refresh_token")?
                .into(),
            cached: Mutex::new(None),
        }),
        Some("service_account") => {
            // Service accounts on non-GCE machines need JWT → token exchange.
            // We delegate to `gcloud auth print-access-token` which handles this
            // transparently when GOOGLE_APPLICATION_CREDENTIALS is set.
            Ok(TokenSource::GcloudSubprocess {
                cached: Mutex::new(None),
            })
        }
        other => Err(format!(
            "unsupported credentials type: {:?}",
            other.unwrap_or("missing")
        )),
    }
}

/// Exchange a refresh token for an access token via the Google OAuth2 endpoint.
/// Returns `(access_token, expires_in_seconds)`.
fn oauth2_refresh(
    client_id: &str,
    client_secret: &str,
    refresh_token: &str,
) -> Result<(String, u64), String> {
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(15))
        .connect_timeout(std::time::Duration::from_secs(10))
        .build()
        .unwrap_or_else(|_| reqwest::blocking::Client::new());
    let resp = client
        .post("https://oauth2.googleapis.com/token")
        .form(&[
            ("client_id", client_id),
            ("client_secret", client_secret),
            ("refresh_token", refresh_token),
            ("grant_type", "refresh_token"),
        ])
        .send()
        .map_err(|e| format!("token refresh HTTP error: {e}"))?;

    let status = resp.status();
    let body: serde_json::Value = resp
        .json()
        .map_err(|e| format!("token refresh parse error: {e}"))?;

    if !status.is_success() {
        return Err(format!(
            "token refresh failed (HTTP {status}): {}",
            body.get("error_description")
                .or(body.get("error"))
                .and_then(|v| v.as_str())
                .unwrap_or("unknown error")
        ));
    }

    let token = body["access_token"]
        .as_str()
        .ok_or("token refresh response has no access_token")?
        .to_string();
    let expires_in = body["expires_in"].as_u64().unwrap_or(3600);
    Ok((token, expires_in))
}

/// Fetch a token from the GCE instance metadata server.
/// Returns `(access_token, expires_in_seconds)`.
fn metadata_server_token() -> Result<(String, u64), String> {
    let client = reqwest::blocking::Client::builder()
        .timeout(Duration::from_secs(5))
        .build()
        .unwrap();
    let resp = client
        .get("http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token")
        .header("Metadata-Flavor", "Google")
        .send()
        .map_err(|e| format!("metadata server request failed: {e}"))?;

    if !resp.status().is_success() {
        return Err(format!("metadata server returned HTTP {}", resp.status()));
    }

    let body: serde_json::Value = resp
        .json()
        .map_err(|e| format!("metadata server parse error: {e}"))?;
    let token = body["access_token"]
        .as_str()
        .ok_or("metadata server response has no access_token")?
        .to_string();
    let expires_in = body["expires_in"].as_u64().unwrap_or(3600);
    Ok((token, expires_in))
}

fn metadata_server_available() -> bool {
    let client = reqwest::blocking::Client::builder()
        .timeout(Duration::from_millis(500))
        .build()
        .unwrap_or_else(|_| reqwest::blocking::Client::new());
    client
        .get("http://metadata.google.internal/")
        .header("Metadata-Flavor", "Google")
        .send()
        .is_ok()
}

fn gcloud_available() -> bool {
    std::process::Command::new("gcloud")
        .arg("version")
        .output()
        .is_ok()
}

fn gcloud_print_access_token() -> Result<String, String> {
    let out = std::process::Command::new("gcloud")
        .args(["auth", "print-access-token"])
        .output()
        .map_err(|e| format!("gcloud subprocess failed: {e}"))?;

    if !out.status.success() {
        let stderr = String::from_utf8_lossy(&out.stderr);
        return Err(format!(
            "gcloud auth print-access-token failed: {stderr}. \
             Run `gcloud auth application-default login` to authenticate."
        ));
    }

    Ok(std::str::from_utf8(&out.stdout)
        .map_err(|e| format!("gcloud output encoding error: {e}"))?
        .trim()
        .to_string())
}

/// Vertex AI LLM provider for Gemini models.
/// Uses the global endpoint: https://aiplatform.googleapis.com/v1/...
pub struct VertexAiLlmProvider {
    config: VertexAiConfig,
    client: reqwest::blocking::Client,
}

impl VertexAiLlmProvider {
    pub fn new(config: VertexAiConfig) -> Self {
        let client = reqwest::blocking::Client::builder()
            .timeout(std::time::Duration::from_secs(120))
            .connect_timeout(std::time::Duration::from_secs(15))
            .build()
            .expect("failed to build reqwest client");
        Self { config, client }
    }

    fn base_url(&self) -> String {
        if self.config.location == "global" {
            "https://aiplatform.googleapis.com/v1".into()
        } else {
            format!(
                "https://{}-aiplatform.googleapis.com/v1",
                self.config.location
            )
        }
    }
}

impl LlmProvider for VertexAiLlmProvider {
    fn complete(&self, messages: &[Message], config: &LlmConfig) -> Result<String, LlmError> {
        let url = format!(
            "{base}/projects/{project}/locations/{location}/publishers/google/models/{model}:generateContent",
            base = self.base_url(),
            project = self.config.project,
            location = self.config.location,
            model = config.model,
        );

        // Convert messages to Gemini format
        let system_instruction: Option<String> = messages
            .iter()
            .find(|m| matches!(m.role, Role::System))
            .map(|m| m.content.clone());

        let contents: Vec<serde_json::Value> = messages
            .iter()
            .filter(|m| !matches!(m.role, Role::System))
            .map(|m| {
                let role = match m.role {
                    Role::User => "user",
                    Role::Assistant => "model",
                    Role::System => unreachable!(),
                };
                json!({
                    "role": role,
                    "parts": [{"text": m.content}]
                })
            })
            .collect();

        let mut body = json!({
            "contents": contents,
            "generationConfig": {
                "maxOutputTokens": config.max_tokens,
                "temperature": config.temperature,
            }
        });

        if let Some(sys) = system_instruction {
            body["systemInstruction"] = json!({
                "parts": [{"text": sys}]
            });
        }

        let token = self
            .config
            .get_token()
            .map_err(|e| LlmError::Provider(format!("auth error: {e}")))?;

        let response = self
            .client
            .post(&url)
            .bearer_auth(&token)
            .json(&body)
            .send()
            .map_err(|e| LlmError::Http(e.to_string()))?;

        let status = response.status();
        let text = response.text().map_err(|e| LlmError::Http(e.to_string()))?;

        if !status.is_success() {
            return Err(LlmError::Provider(format!("HTTP {status}: {text}")));
        }

        let json: serde_json::Value =
            serde_json::from_str(&text).map_err(|e| LlmError::Parse(e.to_string()))?;

        // Extract text from Gemini response
        json["candidates"][0]["content"]["parts"][0]["text"]
            .as_str()
            .map(|s| s.to_string())
            .ok_or_else(|| LlmError::Parse(format!("unexpected response format: {json}")))
    }
}

/// Vertex AI embedding provider.
/// Uses the global endpoint by default.
pub struct VertexAiEmbeddingProvider {
    config: VertexAiConfig,
    model: String,
    dimensions: usize,
    client: reqwest::blocking::Client,
}

impl VertexAiEmbeddingProvider {
    pub fn new(config: VertexAiConfig, model: Option<String>, dimensions: Option<usize>) -> Self {
        let client = reqwest::blocking::Client::builder()
            .timeout(std::time::Duration::from_secs(30))
            .connect_timeout(std::time::Duration::from_secs(15))
            .build()
            .expect("failed to build reqwest client");
        Self {
            config,
            model: model.unwrap_or_else(|| "text-embedding-005".into()),
            dimensions: dimensions.unwrap_or(256),
            client,
        }
    }

    fn base_url(&self) -> String {
        if self.config.location == "global" {
            "https://aiplatform.googleapis.com/v1".into()
        } else {
            format!(
                "https://{}-aiplatform.googleapis.com/v1",
                self.config.location
            )
        }
    }
}

impl EmbeddingProvider for VertexAiEmbeddingProvider {
    fn dimensions(&self) -> usize {
        self.dimensions
    }

    fn embed(&self, text: &str) -> Result<Embedding, EmbeddingError> {
        let url = format!(
            "{base}/projects/{project}/locations/{location}/publishers/google/models/{model}:predict",
            base = self.base_url(),
            project = self.config.project,
            location = self.config.location,
            model = self.model,
        );

        let body = json!({
            "instances": [{"content": text}],
            "parameters": {"outputDimensionality": self.dimensions}
        });

        let token = self
            .config
            .get_token()
            .map_err(|e| EmbeddingError::Provider(format!("auth error: {e}")))?;

        let response = self
            .client
            .post(&url)
            .bearer_auth(&token)
            .json(&body)
            .send()
            .map_err(|e| EmbeddingError::Provider(e.to_string()))?;

        let status = response.status();
        let text = response
            .text()
            .map_err(|e| EmbeddingError::Provider(e.to_string()))?;

        if !status.is_success() {
            return Err(EmbeddingError::Provider(format!("HTTP {status}: {text}")));
        }

        let json: serde_json::Value =
            serde_json::from_str(&text).map_err(|e| EmbeddingError::Provider(e.to_string()))?;

        let values = json["predictions"][0]["embeddings"]["values"]
            .as_array()
            .ok_or_else(|| EmbeddingError::Provider("unexpected response format".into()))?;

        values
            .iter()
            .map(|v| {
                v.as_f64()
                    .map(|f| f as f32)
                    .ok_or_else(|| EmbeddingError::Provider("non-numeric embedding value".into()))
            })
            .collect()
    }
}

// ── Mistral on Vertex AI ────────────────────────────────────────────────────

/// Vertex AI LLM provider for Mistral models (mistral-small-2503, mistral-medium-3, codestral-2).
///
/// Mistral uses the OpenAI-compatible `rawPredict` endpoint and is only available in
/// `us-central1` and `europe-west4` (not `global`). Models must be enabled from the
/// Model Garden console before use.
///
/// Model name detection: model names containing "mistral" or "codestral" route here.
pub struct MistralLlmProvider {
    config: VertexAiConfig,
    /// Resolved region: defaults to us-central1 if config.location is "global".
    region: String,
    client: reqwest::blocking::Client,
}

impl MistralLlmProvider {
    pub fn new(config: VertexAiConfig) -> Self {
        // Mistral doesn't support "global" — fall back to europe-west4 (enabled by default).
        // us-central1 also works if explicitly set and the model is enabled there.
        let region = if config.location == "global" || config.location.is_empty() {
            "europe-west4".into()
        } else {
            config.location.clone()
        };
        let client = reqwest::blocking::Client::builder()
            .timeout(std::time::Duration::from_secs(120))
            .connect_timeout(std::time::Duration::from_secs(15))
            .build()
            .expect("failed to build reqwest client");
        Self {
            config,
            region,
            client,
        }
    }
}

impl LlmProvider for MistralLlmProvider {
    fn complete(&self, messages: &[Message], config: &LlmConfig) -> Result<String, LlmError> {
        let url = format!(
            "https://{region}-aiplatform.googleapis.com/v1/projects/{project}/locations/{region}/publishers/mistralai/models/{model}:rawPredict",
            region = self.region,
            project = self.config.project,
            model = config.model,
        );

        // OpenAI-compatible message format
        let msgs: Vec<serde_json::Value> = messages
            .iter()
            .map(|m| {
                let role = match m.role {
                    Role::System => "system",
                    Role::User => "user",
                    Role::Assistant => "assistant",
                };
                json!({"role": role, "content": m.content})
            })
            .collect();

        let body = json!({
            "model": config.model,
            "messages": msgs,
            "max_tokens": config.max_tokens,
            "temperature": config.temperature,
            "stream": false,
        });

        let token = self
            .config
            .get_token()
            .map_err(|e| LlmError::Provider(format!("auth error: {e}")))?;

        let response = self
            .client
            .post(&url)
            .bearer_auth(&token)
            .json(&body)
            .send()
            .map_err(|e| LlmError::Http(e.to_string()))?;

        let status = response.status();
        let text = response.text().map_err(|e| LlmError::Http(e.to_string()))?;

        if !status.is_success() {
            return Err(LlmError::Provider(format!("HTTP {status}: {text}")));
        }

        let json: serde_json::Value =
            serde_json::from_str(&text).map_err(|e| LlmError::Parse(e.to_string()))?;

        // OpenAI-compatible response: choices[0].message.content
        json["choices"][0]["message"]["content"]
            .as_str()
            .map(|s| s.to_string())
            .ok_or_else(|| LlmError::Parse(format!("unexpected Mistral response: {json}")))
    }
}