rdsh 0.12.0

Rust port/shim for dsh: fast launcher, token saver, zero behavior breakage via passthrough
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
//! DSH configuration and JSON catalog sharing, without starting a DSH runtime.
use anyhow::{bail, Result};
use serde_json::{json, Value};
use std::{
    collections::HashSet,
    path::{Path, PathBuf},
    time::Duration,
};

const LIMIT: usize = 8 * 1024 * 1024;
#[derive(Default)]
pub struct Registry {
    pub routes: Vec<Route>,
    pub diagnostics: Vec<String>,
}
pub struct Route {
    pub id: String,
    pub name: String,
    pub models: Vec<Model>,
    pub key: Option<String>,
}
pub struct Model {
    pub id: String,
    pub name: String,
    pub api: String,
    pub base: String,
    pub max_tokens: u64,
    pub token_field: String,
    pub unavailable: Option<String>,
}
fn read(path: &Path) -> Result<String> {
    use std::io::Read;
    let file = std::fs::File::open(path)?;
    let mut bytes = vec![];
    file.take(LIMIT as u64 + 1).read_to_end(&mut bytes)?;
    if bytes.len() > LIMIT {
        bail!("NATIVE_CONFIG_SIZE_LIMIT");
    }
    String::from_utf8(bytes).map_err(|_| anyhow::anyhow!("NATIVE_CONFIG_ENCODING"))
}
fn scalar<'a>(v: &'a Value, key: &str) -> Option<&'a str> {
    v.get(key).and_then(Value::as_str).filter(|s| !s.is_empty())
}
fn safe_id(id: &str) -> bool {
    !id.is_empty()
        && id.len() <= 200
        && id
            .bytes()
            .all(|c| c.is_ascii_alphanumeric() || b"-_.".contains(&c))
}
fn endpoint(base: &str, api: &str, model: &str) -> Result<reqwest::Url> {
    let mut url =
        reqwest::Url::parse(base).map_err(|_| anyhow::anyhow!("NATIVE_ENDPOINT_INVALID"))?;
    if !["http", "https"].contains(&url.scheme())
        || url.host_str().is_none()
        || !url.username().is_empty()
        || url.password().is_some()
        || url.query().is_some()
        || url.fragment().is_some()
    {
        bail!("NATIVE_ENDPOINT_INVALID");
    }
    let google_suffix;
    let suffix = match api {
        "openai-completions" => "chat/completions",
        "openai-responses" => "responses",
        "anthropic-messages" => "messages",
        "google-generative-ai" => {
            let model = model.strip_prefix("models/").unwrap_or(model);
            if !safe_id(model) {
                bail!("NATIVE_MODEL_ID_INVALID");
            }
            google_suffix = format!("models/{model}:generateContent");
            &google_suffix
        }
        _ => bail!("NATIVE_PROTOCOL_UNSUPPORTED"),
    };
    let path = url.path().trim_end_matches('/');
    let path = if api == "anthropic-messages" && !path.ends_with("/v1") {
        format!("{path}/v1/{suffix}")
    } else {
        format!("{path}/{suffix}")
    };
    url.set_path(&path);
    Ok(url)
}
fn catalog_dir() -> Option<PathBuf> {
    // Locate data beside the installed launcher. Never evaluate its JavaScript.
    let entry = PathBuf::from(crate::passthrough::find_original_dsh()?)
        .canonicalize()
        .ok()?;
    for ancestor in entry.ancestors().skip(1).take(5) {
        let data = ancestor.join("node_modules/@earendil-works/pi-ai/dist/providers/data");
        if data.is_dir() {
            return Some(data);
        }
    }
    None
}
impl Registry {
    pub fn load(profile: &str, catalog: Option<PathBuf>) -> Result<Self> {
        if !safe_id(profile) {
            bail!("NATIVE_PROFILE_NAME_INVALID");
        }
        let root = PathBuf::from(crate::inspect::dsh_home())
            .join("profiles")
            .join(profile);
        Self::load_from(&root, catalog.or_else(catalog_dir).as_deref(), |env| {
            std::env::var(env)
                .ok()
                .filter(|v| !v.is_empty())
                .or_else(|| crate::models::credential_ref(env))
        })
    }
    fn load_from(
        root: &Path,
        catalog: Option<&Path>,
        key: impl Fn(&str) -> Option<String>,
    ) -> Result<Self> {
        if !root.is_dir() {
            bail!("NATIVE_PROFILE_NOT_FOUND");
        }
        let mut routes = None;
        for name in ["cordis.yml", "cordis.patch.yml"] {
            let path = root.join(name);
            if path.exists() {
                // Do not surface parser errors containing configuration/secret values.
                let text =
                    read(&path).map_err(|_| anyhow::anyhow!("NATIVE_PROFILE_READ_FAILED"))?;
                if let Some(found) = crate::providers::native::read_routes(&text)
                    .map_err(|_| anyhow::anyhow!("NATIVE_PROFILE_LAYOUT_UNSUPPORTED"))?
                {
                    routes = Some(found);
                }
            }
        }
        let Some(routes) = routes else {
            return Ok(Self {
                diagnostics: vec![
                    "選択プロファイルに明示的なllm-pi-ai providers設定がありません".into(),
                ],
                ..Self::default()
            });
        };
        let mut registry = Self::default();
        for (id, config) in routes {
            if !safe_id(&id) {
                registry
                    .diagnostics
                    .push("未対応のプロバイダーIDを除外しました".into());
                continue;
            }
            let catalog_models: Vec<Value> = catalog
                .and_then(|p| read(&p.join(format!("{id}.json"))).ok())
                .and_then(|s| serde_json::from_str::<Value>(&s).ok())
                .and_then(|v| v.as_object().cloned())
                .map(|groups| {
                    // pi-ai flattenModelCatalog uses Object.assign: later APIs win.
                    let mut models = serde_json::Map::new();
                    for group in groups.into_values().filter_map(|v| v.as_object().cloned()) {
                        models.extend(group);
                    }
                    models.into_values().collect()
                })
                .unwrap_or_default();
            let models = config
                .get("models")
                .and_then(Value::as_array)
                .cloned()
                .unwrap_or_else(|| catalog_models.clone());
            let key_name = scalar(&config, "apiKeyEnv");
            let auth = key_name.and_then(&key);
            let mut seen = HashSet::new();
            let mut resolved = vec![];
            for model in models {
                let Some(mid) = scalar(&model, "id") else {
                    continue;
                };
                if !seen.insert(mid.to_owned()) {
                    continue;
                }
                let base_model = catalog_models.iter().find(|v| scalar(v, "id") == Some(mid));
                let api = scalar(&config, "api")
                    .or_else(|| scalar(&model, "api"))
                    .or_else(|| base_model.and_then(|v| scalar(v, "api")))
                    .unwrap_or("");
                let base = scalar(&config, "baseURL")
                    .or_else(|| scalar(&model, "baseURL"))
                    .or_else(|| scalar(&model, "baseUrl"))
                    .or_else(|| base_model.and_then(|v| scalar(v, "baseUrl")))
                    .unwrap_or("");
                let unavailable = if config["unsupported"] == true || model["unsupported"] == true {
                    Some("このルートの追加設定にはnative未対応の項目があります".into())
                } else if endpoint(base, api, mid).is_err() {
                    Some("未対応APIまたは不正・未指定の接続先です(OAuth専用APIは未対応)".into())
                } else if key_name.is_none() || auth.is_none() {
                    Some("apiKeyEnvと既存の環境変数またはcredential refが必要です".into())
                } else {
                    None
                };
                let name = scalar(&model, "name")
                    .or_else(|| base_model.and_then(|v| scalar(v, "name")))
                    .unwrap_or(mid)
                    .to_owned();
                let max_tokens = model
                    .get("maxTokens")
                    .and_then(|v| v.as_u64().or_else(|| v.as_str()?.parse().ok()))
                    .or_else(|| base_model.and_then(|v| v["maxTokens"].as_u64()))
                    .unwrap_or(4096)
                    .clamp(1, 8192);
                let token_field = base_model
                    .and_then(|v| v["compat"]["maxTokensField"].as_str())
                    .filter(|s| ["max_tokens", "max_completion_tokens"].contains(s))
                    .unwrap_or(
                        if reqwest::Url::parse(base)
                            .ok()
                            .and_then(|u| u.host_str().map(str::to_owned))
                            .as_deref()
                            == Some("api.openai.com")
                        {
                            "max_completion_tokens"
                        } else {
                            "max_tokens"
                        },
                    )
                    .to_owned();
                resolved.push(Model {
                    id: mid.into(),
                    name,
                    api: api.into(),
                    base: base.into(),
                    max_tokens,
                    token_field,
                    unavailable,
                });
            }
            if resolved.is_empty() {
                registry.diagnostics.push(format!(
                    "{id}: 明示モデルまたはインストール済みJSONカタログがありません"
                ));
            }
            registry.routes.push(Route {
                id: format!("dsh:{id}"),
                name: scalar(&config, "displayName").unwrap_or(&id).into(),
                models: resolved,
                key: auth,
            });
        }
        Ok(registry)
    }
    pub fn public(&self) -> Vec<Value> {
        self.routes.iter().map(|r| json!({"id":r.id,"name":format!("DSH登録 · {}",r.name),"models":r.models.iter().map(|m| json!({"id":m.id,"name":m.name,"unavailable":m.unavailable})).collect::<Vec<_>>()})).collect()
    }
    pub fn allowed(&self, provider: &str, model: &str) -> bool {
        self.find(provider, model)
            .is_some_and(|(_, m)| m.unavailable.is_none())
    }
    fn find(&self, provider: &str, model: &str) -> Option<(&Route, &Model)> {
        let route = self.routes.iter().find(|r| r.id == provider)?;
        Some((route, route.models.iter().find(|m| m.id == model)?))
    }
    pub async fn generate(&self, provider: &str, model: &str, prompt: &str) -> Result<String> {
        let (route, model) = self
            .find(provider, model)
            .filter(|(_, m)| m.unavailable.is_none())
            .ok_or_else(|| anyhow::anyhow!("NATIVE_MODEL_UNAVAILABLE"))?;
        let url = endpoint(&model.base, &model.api, &model.id)?;
        let mut body = match model.api.as_str() {
            "openai-completions" => {
                json!({"model":model.id,"messages":[{"role":"user","content":prompt}],"stream":false})
            }
            "openai-responses" => {
                json!({"model":model.id,"input":prompt,"stream":false,"store":false,"max_output_tokens":model.max_tokens})
            }
            "anthropic-messages" => {
                json!({"model":model.id,"max_tokens":model.max_tokens,"messages":[{"role":"user","content":prompt}],"stream":false})
            }
            "google-generative-ai" => {
                json!({"contents":[{"role":"user","parts":[{"text":prompt}]}],"generationConfig":{"maxOutputTokens":model.max_tokens}})
            }
            _ => bail!("NATIVE_PROTOCOL_UNSUPPORTED"),
        };
        if model.api == "openai-completions" {
            body[&model.token_field] = json!(model.max_tokens);
        }
        let client = reqwest::Client::builder()
            .redirect(reqwest::redirect::Policy::none())
            .timeout(Duration::from_secs(180))
            .build()
            .map_err(|_| anyhow::anyhow!("NATIVE_HTTP_SETUP_FAILED"))?;
        let mut request = client.post(url).header("content-type", "application/json");
        if let Some(key) = &route.key {
            request = if model.api == "google-generative-ai" {
                request.header("x-goog-api-key", key)
            } else if model.api == "anthropic-messages" {
                request
                    .header("x-api-key", key)
                    .header("anthropic-version", "2023-06-01")
            } else {
                request.bearer_auth(key)
            };
        }
        let mut response = request
            .body(serde_json::to_vec(&body)?)
            .send()
            .await
            .map_err(|_| anyhow::anyhow!("NATIVE_HTTP_REQUEST_FAILED"))?;
        if !response.status().is_success() {
            bail!("NATIVE_HTTP_STATUS_{}", response.status().as_u16());
        }
        let mut bytes = vec![];
        while let Some(chunk) = response
            .chunk()
            .await
            .map_err(|_| anyhow::anyhow!("NATIVE_HTTP_READ_FAILED"))?
        {
            if bytes.len() + chunk.len() > LIMIT {
                bail!("NATIVE_HTTP_OUTPUT_LIMIT");
            }
            bytes.extend_from_slice(&chunk);
        }
        let value: Value = serde_json::from_slice(&bytes)
            .map_err(|_| anyhow::anyhow!("NATIVE_HTTP_INVALID_JSON"))?;
        let text =
            match model.api.as_str() {
                "openai-completions" => value["choices"][0]["message"]["content"]
                    .as_str()
                    .map(str::to_owned),
                "openai-responses" => value["output"].as_array().map(|items| {
                    items
                        .iter()
                        .filter(|v| v["type"] == "message")
                        .filter_map(|v| v["content"].as_array())
                        .flatten()
                        .filter(|v| v["type"] == "output_text")
                        .filter_map(|v| v["text"].as_str())
                        .collect::<String>()
                }),
                "anthropic-messages" => value["content"].as_array().map(|items| {
                    items
                        .iter()
                        .filter(|v| v["type"] == "text")
                        .filter_map(|v| v["text"].as_str())
                        .collect::<String>()
                }),
                "google-generative-ai" => value["candidates"][0]["content"]["parts"]
                    .as_array()
                    .map(|parts| {
                        parts
                            .iter()
                            .filter(|v| v["thought"] != true)
                            .filter_map(|v| v["text"].as_str())
                            .collect::<String>()
                    }),
                _ => None,
            }
            .filter(|s| !s.is_empty())
            .ok_or_else(|| anyhow::anyhow!("NATIVE_HTTP_NO_TEXT"))?;
        Ok(text)
    }
}

#[cfg(test)]
pub(super) mod tests {
    use super::*;
    use std::io::{Read, Write};
    struct Scratch(PathBuf);
    impl Scratch {
        fn new() -> Self {
            let p = std::env::temp_dir().join(format!(
                "rdsh-shared-models-{}",
                crate::local_http::random_token().unwrap()
            ));
            std::fs::create_dir(&p).unwrap();
            Self(p)
        }
        fn write(&self, name: &str, text: &str) {
            std::fs::write(self.0.join(name), text).unwrap();
        }
    }
    impl Drop for Scratch {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }
    fn patch(api: &str, base: &str) -> String {
        format!("- id: llm-pi-ai\n  config:\n    providers:\n      test:\n        api: {api}\n        baseURL: '{base}'\n        apiKeyEnv: TEST_API_KEY\n        models:\n          - id: test-model\n            name: Test Model\n")
    }
    pub fn mock(
        replies: Vec<(u16, Value)>,
    ) -> (String, std::thread::JoinHandle<Vec<(String, Value)>>) {
        let listener = std::net::TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)).unwrap();
        listener.set_nonblocking(true).unwrap();
        let url = format!(
            "http://127.0.0.1:{}/v1",
            listener.local_addr().unwrap().port()
        );
        let thread = std::thread::spawn(move || {
            let mut requests = vec![];
            for (code, reply) in replies {
                let start = std::time::Instant::now();
                // A connection that dies before its headers arrive is not the
                // request this reply belongs to. Wait for the next one instead of
                // spending the reply on a probe the client already abandoned.
                let (mut stream, mut bytes, end) = loop {
                    let accepted = loop {
                        match listener.accept() {
                            Ok(s) => break s,
                            Err(e)
                                if e.kind() == std::io::ErrorKind::WouldBlock
                                    && start.elapsed() < Duration::from_secs(10) =>
                            {
                                std::thread::sleep(Duration::from_millis(5))
                            }
                            Err(e) => panic!("mock accept: {e}"),
                        }
                    };
                    let (mut stream, _) = accepted;
                    // The listener is non-blocking so accept() can poll, and on
                    // Windows the accepted socket inherits that flag: read() then
                    // returns WSAEWOULDBLOCK instead of waiting out the timeout.
                    stream.set_nonblocking(false).unwrap();
                    stream
                        .set_read_timeout(Some(Duration::from_secs(5)))
                        .unwrap();
                    let mut bytes = Vec::new();
                    let mut last_error = None;
                    loop {
                        let mut b = [0; 4096];
                        match stream.read(&mut b) {
                            Ok(0) => break,
                            Ok(n) => bytes.extend_from_slice(&b[..n]),
                            Err(e) => {
                                eprintln!("mock: read stopped: {e} ({} bytes)", bytes.len());
                                last_error = Some(e);
                                break;
                            }
                        }
                        if bytes.windows(4).any(|w| w == b"\r\n\r\n") {
                            break;
                        }
                    }
                    match bytes.windows(4).position(|b| b == b"\r\n\r\n") {
                        Some(end) => break (stream, bytes, end),
                        None => {
                            eprintln!("mock: request without headers: {last_error:?}");
                            assert!(
                                start.elapsed() < Duration::from_secs(10),
                                "mock: no complete request arrived"
                            );
                        }
                    }
                };
                let headers = String::from_utf8_lossy(&bytes[..end]);
                let length: usize = headers
                    .lines()
                    .find_map(|l| {
                        l.to_lowercase()
                            .strip_prefix("content-length:")
                            .map(|s| s.trim().parse().unwrap())
                    })
                    .expect("mock request needs a content-length");
                let body_start = end + 4;
                let body_end = body_start + length;
                // Headers and body can arrive in separate segments; a short body
                // here used to panic the mock thread and leave the client waiting.
                while bytes.len() < body_end {
                    let mut b = [0; 4096];
                    match stream.read(&mut b) {
                        Ok(0) => break,
                        Ok(n) => bytes.extend_from_slice(&b[..n]),
                        Err(e) => {
                            eprintln!("mock: body read stopped: {e} ({} bytes)", bytes.len());
                            break;
                        }
                    }
                }
                assert!(
                    bytes.len() >= body_end,
                    "mock: short body ({} of {length} bytes)",
                    bytes.len().saturating_sub(body_start)
                );
                requests.push((
                    String::from_utf8(bytes[..body_start].to_vec()).unwrap(),
                    serde_json::from_slice(&bytes[body_start..body_end]).unwrap(),
                ));
                let body = serde_json::to_vec(&reply).unwrap();
                // The client can be gone already (it reset the request); a closed
                // connection is not a reason to fail the test.
                let head = format!("HTTP/1.1 {code} Test\r\nContent-Length: {}\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n", body.len());
                let _ = stream.write_all(head.as_bytes());
                let _ = stream.write_all(&body);
            }
            requests
        });
        (url, thread)
    }
    pub fn registry(api: &str, base: &str) -> Registry {
        let root = Scratch::new();
        root.write("cordis.patch.yml", &patch(api, base));
        Registry::load_from(&root.0, None, |_| Some("synthetic-api-secret".into())).unwrap()
    }
    #[test]
    fn catalog_defaults_explicit_replacement_and_credentials_stay_private() {
        let root = Scratch::new();
        root.write("cordis.patch.yml","- id: llm-pi-ai\n  config:\n    providers:\n      test:\n        apiKeyEnv: TEST_API_KEY\n");
        root.write("test.json",&json!({"old-api":{"test-model":{"id":"test-model","api":"bad","baseUrl":"https://bad.invalid"}},"openai-completions":{"test-model":{"id":"test-model","name":"Catalog Name","api":"openai-completions","baseUrl":"https://example.invalid/v1"}}}).to_string());
        let r = Registry::load_from(&root.0, Some(&root.0), |name| {
            assert_eq!(name, "TEST_API_KEY");
            Some("synthetic-api-secret".into())
        })
        .unwrap();
        assert!(r.allowed("dsh:test", "test-model"));
        assert_eq!(r.routes[0].models[0].name, "Catalog Name");
        let public = serde_json::to_string(&r.public()).unwrap();
        assert!(!public.contains("synthetic-api-secret"));
        assert!(!public.contains("baseUrl"));
        root.write(
            "cordis.patch.yml",
            &patch("openai-completions", "https://other.invalid/v1")
                .replace("test-model", "new-model"),
        );
        let r = Registry::load_from(&root.0, Some(&root.0), |_| None).unwrap();
        assert_eq!(r.routes[0].models.len(), 1);
        assert_eq!(r.routes[0].models[0].id, "new-model");
        assert!(!r.allowed("dsh:test", "new-model"));
    }
    #[test]
    fn patch_replaces_config_and_unsupported_settings_fail_closed() {
        let root = Scratch::new();
        root.write(
            "cordis.yml",
            &patch("openai-completions", "https://base.invalid/v1"),
        );
        root.write(
            "cordis.patch.yml",
            &patch("openai-codex-responses", "https://patch.invalid/v1"),
        );
        let r = Registry::load_from(&root.0, None, |_| Some("key".into())).unwrap();
        assert!(!r.allowed("dsh:test", "test-model"));
        assert_eq!(r.routes[0].models[0].base, "https://patch.invalid/v1");
        root.write(
            "cordis.patch.yml",
            &patch("openai-completions", "https://patch.invalid/v1")
                .replace("        api:", "        reasoning: high\n        api:"),
        );
        let r = Registry::load_from(&root.0, None, |_| Some("key".into())).unwrap();
        assert!(!r.allowed("dsh:test", "test-model"));
        root.write(
            "cordis.patch.yml",
            &patch("openai-completions", "https://patch.invalid/v1")
                .replace("        api:", "        api: bad\n        api:"),
        );
        assert!(Registry::load_from(&root.0, None, |_| None).is_err());
    }
    #[tokio::test]
    async fn direct_http_protocols_and_error_redaction() {
        let _env_lock = crate::rustxng::tests::env_lock();
        let turn = r#"{"version":1,"text":"OK","toolCalls":[]}"#;
        for (api, path, response) in [
            (
                "openai-completions",
                "/v1/chat/completions",
                json!({"choices":[{"message":{"content":turn}}]}),
            ),
            (
                "openai-responses",
                "/v1/responses",
                json!({"output":[{"type":"message","content":[{"type":"output_text","text":turn}]}]}),
            ),
            (
                "anthropic-messages",
                "/v1/messages",
                json!({"content":[{"type":"text","text":turn}]}),
            ),
        ] {
            let (url, server) = mock(vec![(200, response)]);
            let r = registry(api, &url);
            assert_eq!(
                r.generate("dsh:test", "test-model", "USER INPUT")
                    .await
                    .unwrap(),
                turn
            );
            let requests = server.join().unwrap();
            let (headers, body) = &requests[0];
            assert!(headers.starts_with(&format!("POST {path} HTTP/1.1")));
            assert_eq!(body["model"], "test-model");
            if api == "openai-responses" {
                assert_eq!(body["input"], "USER INPUT");
                assert_eq!(body["max_output_tokens"], 4096);
                assert_eq!(body["store"], false);
            } else {
                assert_eq!(body["messages"][0]["content"], "USER INPUT");
                assert_eq!(body["max_tokens"], 4096);
            }

            assert!(!body.to_string().contains("synthetic-api-secret"));
            if api == "anthropic-messages" {
                assert!(headers.contains("x-api-key: synthetic-api-secret"));
                assert!(headers.contains("anthropic-version: 2023-06-01"));
            } else {
                assert!(headers.contains("authorization: Bearer synthetic-api-secret"));
            }
        }
        for code in [302, 401, 429] {
            let (url, server) = mock(vec![(
                code,
                json!({"error":"synthetic-api-secret private body"}),
            )]);
            let r = registry("openai-completions", &url);
            assert_eq!(
                r.generate("dsh:test", "test-model", "input")
                    .await
                    .unwrap_err()
                    .to_string(),
                format!("NATIVE_HTTP_STATUS_{code}")
            );
            server.join().unwrap();
        }
    }
    #[test]
    fn endpoint_rejects_credentials_queries_and_unknown_protocols() {
        for base in [
            "https://user:secret@example.invalid/v1",
            "file:///tmp/key",
            "https://example.invalid/v1?key=secret",
            "https://example.invalid/v1#fragment",
        ] {
            assert!(endpoint(base, "openai-completions", "test-model").is_err());
        }
        assert!(endpoint(
            "https://example.invalid",
            "openai-codex-responses",
            "test-model"
        )
        .is_err());
    }
    #[tokio::test]
    async fn google_direct_transport_keeps_key_in_header_and_filters_thought_parts() {
        let _env_lock = crate::rustxng::tests::env_lock();
        let turn = r#"{"version":1,"text":"Google OK","toolCalls":[]}"#;
        let (url, server) = mock(vec![(
            200,
            json!({"candidates":[{"content":{"parts":[{"thought":true,"text":"private reasoning"},{"text":turn}]}}]}),
        )]);
        let r = registry("google-generative-ai", &url);
        assert_eq!(
            r.generate("dsh:test", "test-model", "USER INPUT")
                .await
                .unwrap(),
            turn
        );
        let requests = server.join().unwrap();
        assert!(requests[0]
            .0
            .starts_with("POST /v1/models/test-model:generateContent HTTP/1.1"));
        assert!(requests[0]
            .0
            .contains("x-goog-api-key: synthetic-api-secret"));
        assert_eq!(
            requests[0].1["contents"][0]["parts"][0]["text"],
            "USER INPUT"
        );
        assert_eq!(requests[0].1["generationConfig"]["maxOutputTokens"], 4096);
        assert!(!requests[0].1.to_string().contains("synthetic-api-secret"));
        assert!(endpoint(&url, "google-generative-ai", "../bad").is_err());
    }
}