rpi-ai 0.1.9

Unified multi-provider LLM types + streaming, Rust port of pi-ai
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
//! Mirrors `packages/ai/src/api/anthropic-messages.ts` + `providers/anthropic.ts`
//! — the Anthropic Messages-API provider: request building, SSE decoding,
//! event mapping, retry, and cost. The crate-level [`Provider`] impl lives here.
//!
//! Only compiled when the `providers` feature is on (reqwest/bytes/eventsource-
//! stream are optional deps). The smoke test behind the `smoke` feature drives
//! a live request against `ANTHROPIC_API_KEY`.
//!
//! v1 scope is API-key auth only; OAuth/Copilot identity headers and the
//! Claude-Code system-prompt stealth prefix are TODO (see plan §5.16). The
//! `is_oauth` flag flows through `build_params` so enabling them later is a
//! localized change, not a re-plumb of the stream path.

pub mod build_params;
pub mod cache_control;
pub mod cost;
pub mod json_parse;
pub mod mapper;
pub mod models;
pub mod retry;
pub mod sse;

use crate::error::AiError;
use crate::event_stream::{
    create_assistant_message_event_stream, AssistantMessageEventStream,
    AssistantMessageEventStreamProducer,
};
use crate::model::{Model, StreamingProtocolCompat};
use crate::provider::{CacheRetention, Provider, SimpleStreamOptions};
use crate::types::{AssistantMessageEvent, Content, Context, DoneReason, StopReason, Usage};
use async_trait::async_trait;
use std::sync::Arc;

use crate::providers::anthropic::build_params::build_params;
use crate::providers::anthropic::cost::calculate_cost;
use crate::providers::anthropic::mapper::{emit_terminal_error, run_mapper, MapperState};
use crate::providers::anthropic::models::anthropic_models;
use crate::providers::anthropic::retry::retry_provider_request;
use crate::providers::anthropic::sse::SseEventStream;

/// The fixed Anthropic Messages-API version header. Mirrors the SDK default
/// (`2023-06-01`); the TS SDK attaches this implicitly, the Rust port sets it
/// explicitly since it speaks raw `reqwest`.
const ANTHROPIC_VERSION: &str = "2023-06-01";

/// The Anthropic Messages-API provider. Mirrors the `Anthropic`-SDK-backed
/// surface in `api/anthropic-messages.ts` (`stream` + `streamSimple`) +
/// `providers/anthropic.ts`.
///
/// v1 is API-key auth only: the resolved key is sent as `x-api-key`. OAuth
/// (`sk-ant-oat*` bearer tokens, Claude Code identity headers) and Copilot
/// (`github-copilot` provider, bearer + dynamic headers) paths are TODO.
/// Construct via [`AnthropicProvider::new`] or [`AnthropicProvider::from_env`].
pub struct AnthropicProvider {
    /// Default API key, used when `SimpleStreamOptions::api_key` is `None`.
    /// `None` here means "defer to opts + env at call time".
    api_key: Option<String>,
    http: reqwest::Client,
    models: Vec<Model>,
}

impl AnthropicProvider {
    /// Build a provider with an explicit default API key + custom
    /// `reqwest::Client` (useful for proxies / custom TLS). Serves the
    /// hand-curated catalog from [`models::anthropic_models`].
    pub fn new(api_key: Option<String>, http: reqwest::Client) -> Self {
        Self {
            api_key,
            http,
            models: anthropic_models(),
        }
    }

    /// Build with a custom model catalog (e.g. a generated models.dev snapshot).
    pub fn with_models(api_key: Option<String>, http: reqwest::Client, models: Vec<Model>) -> Self {
        Self {
            api_key,
            http,
            models,
        }
    }

    /// Convenience: build with a default `reqwest::Client`, deferring API-key
    /// resolution to call time (opts.api_key → provider default →
    /// `ANTHROPIC_API_KEY` env). Mirrors how the smoke test constructs a
    /// provider without a pre-resolved key.
    pub fn from_env() -> Self {
        let http = reqwest::Client::new();
        Self::new(None, http)
    }

    /// Default API key carried on the provider (`opts.api_key` still wins).
    pub fn api_key(&self) -> Option<&str> {
        self.api_key.as_deref()
    }
}

#[async_trait]
impl Provider for AnthropicProvider {
    fn id(&self) -> &str {
        "anthropic"
    }

    fn models(&self) -> &[Model] {
        &self.models
    }

    async fn stream_simple(
        &self,
        model: &Model,
        ctx: &Context,
        opts: &SimpleStreamOptions,
    ) -> AssistantMessageEventStream {
        // `stream_simple` returns synchronously: create the stream, spawn the
        // producer task, hand back the consumer. Failures surface as `Error`
        // events on the stream, never as panics (plan §5.11). Mirrors the TS
        // `stream` IIFE pattern.
        let (mut prod, stream) = create_assistant_message_event_stream();

        let http = self.http.clone();
        let provider_key = self.api_key.clone();
        let model = model.clone();
        let ctx = Arc::new(ctx.clone());
        let opts = opts.clone();

        tokio::spawn(async move {
            run_anthropic_stream(&mut prod, http, provider_key, &model, &ctx, &opts).await;
            // `prod` drops here, closing the mpsc sender so the consumer's
            // `next()` returns `None` after the terminal event.
        });

        stream
    }
}

// ----------------------------------------------------------------------------
// Producer task — mirrors the TS `stream` async IIFE (anthropic-messages.ts
// :487-774) + `streamSimple` (:801-841).
// ----------------------------------------------------------------------------

/// The producer task body. Mirrors the TS `stream` async IIFE: build the empty
/// `output`, resolve auth (`assertRequestAuth`), build params, POST with retry
/// (`retryProviderRequest`), then drive the SSE mapper to the terminal event.
/// Every failure path pushes an `Error` terminal so the consumer's `result()`
/// always resolves.
///
/// The TS collapses `streamSimple` → `stream` → `buildParams` with an
/// intermediate `AnthropicOptions`; the Rust port folds `streamSimple` into
/// this function and lets `build_params` read `SimpleStreamOptions` directly
/// (the adaptive-vs-budget thinking decision lives there), so there is no
/// intermediate options shape.
async fn run_anthropic_stream(
    prod: &mut AssistantMessageEventStreamProducer,
    http: reqwest::Client,
    provider_key: Option<String>,
    model: &Model,
    ctx: &Context,
    opts: &SimpleStreamOptions,
) {
    let mut state = MapperState::new(
        model.api.clone(),
        model.provider.clone(),
        model.id.clone(),
        now_ms(),
    );

    // ---- resolve auth (assertRequestAuth) ----
    let resolved_key = resolve_api_key(&provider_key, opts);
    // TS `assertRequestAuth(model.provider, options?.apiKey, options?.headers)`
    // checks `options.headers` alone — but upstream the model-resolution layer
    // has *already merged* `model.headers` into `options.headers` (models.ts
    // `getApiKeyAndHeaders` → `auth.headers = merge(..., providerOrModel.headers)`).
    // The Rust port keeps auth headers on `model.headers` and merges them later
    // in `assemble_headers`, so here the check must consider BOTH sources to
    // reproduce the TS net effect. This is what lets a Bearer header folded onto
    // `model.headers` (the `ANTHROPIC_AUTH_TOKEN` / `models.json authHeader:true`
    // paths in rpi-cli's `provider::resolve`) authenticate without an `x-api-key`.
    let header_owned_auth = has_header_auth(&opts.headers) || has_header_auth(&model.headers);
    let api_key_for_header = match (resolved_key, header_owned_auth) {
        (Some(k), _) => Some(k),
        (None, true) => None, // headers carry auth; do not send x-api-key.
        (None, false) => {
            // Mirrors `assertRequestAuth`'s `throw new Error("No API key for…")`.
            let msg = format!("No API key for provider: {}", model.provider);
            emit_terminal_error(prod, &mut state, msg, false);
            return;
        }
    };

    // ---- build params (is_oauth = false in v1) ----
    let built = build_params(model, ctx, false, opts);
    let mut body = match serde_json::to_value(&built.request) {
        Ok(v) => v,
        Err(e) => {
            emit_terminal_error(
                prod,
                &mut state,
                format!("failed to serialize request body: {e}"),
                false,
            );
            return;
        }
    };
    let non_stream = std::env::var("RPI_ANTHROPIC_NON_STREAM").ok().as_deref() == Some("1");
    if non_stream {
        body["stream"] = serde_json::Value::Bool(false);
        strip_cache_control(&mut body);
        simplify_non_stream_request(&mut body);
    }

    // ---- assemble headers (createClient, API-key arm) ----
    let headers = assemble_headers(
        model,
        opts,
        built.beta_header.as_deref(),
        api_key_for_header.as_deref(),
    );

    let url = format!("{}/v1/messages", model.base_url.trim_end_matches('/'));
    let timeout = opts.timeout;
    let signal = opts.signal.clone();

    // ---- POST with retry (retryProviderRequest) ----
    // The TS hands the SDK `.create({...params, stream:true}, {signal, timeout,
    // maxRetries:0}).asResponse()` to retryProviderRequest; the Rust port
    // reproduces that as a raw `reqwest` POST that returns a `Response` on
    // 2xx and an `AiError::Http { status }` on non-2xx (so the retry predicate
    // can classify 408/409/429/>=500). Cancellation is honored via a `select!`
    // around the send — the in-flight reqwest future is dropped on abort.
    let response_result = retry_provider_request(
        move || {
            let http = http.clone();
            let body = body.clone();
            let url = url.clone();
            let headers = headers.clone();
            let signal = signal.clone();
            async move {
                let mut req = http.post(&url);
                if let Some(t) = timeout {
                    req = req.timeout(t);
                }
                for (k, v) in &headers {
                    req = req.header(k.as_str(), v.as_str());
                }
                let send_fut = req.json(&body).send();
                let resp = tokio::select! {
                    r = send_fut => r.map_err(|e| AiError::Http {
                        status: None,
                        message: format!("http transport error: {e}"),
                    })?,
                    _ = signal.cancelled() => return Err(AiError::Abort {
                        message: "Request aborted".to_string(),
                    }),
                };
                let status = resp.status();
                if !status.is_success() {
                    let code = status.as_u16();
                    // Read the error body for a diagnostic message; the retry
                    // predicate only needs the status code.
                    let text = resp.text().await.unwrap_or_default();
                    return Err(AiError::Http {
                        status: Some(code),
                        message: text,
                    });
                }
                Ok(resp)
            }
        },
        opts.max_retries,
        opts.max_retry_delay,
        &opts.signal,
    )
    .await;

    let response = match response_result {
        Ok(r) => r,
        Err(err) => {
            let aborted = err.is_abort();
            eprintln!("anthropic request failed: {err}");
            emit_terminal_error(prod, &mut state, err.to_string(), aborted);
            return;
        }
    };

    if non_stream {
        let response_body = match response.text().await {
            Ok(body) => body,
            Err(error) => {
                emit_terminal_error(
                    prod,
                    &mut state,
                    format!("failed to read non-stream response: {error}"),
                    false,
                );
                return;
            }
        };
        let value: serde_json::Value = match serde_json::from_str(&response_body) {
            Ok(value) => value,
            Err(error) => {
                emit_terminal_error(
                    prod,
                    &mut state,
                    format!("failed to parse non-stream response: {error}"),
                    false,
                );
                return;
            }
        };
        let text = value
            .get("content")
            .and_then(serde_json::Value::as_array)
            .map(|blocks| {
                blocks
                    .iter()
                    .filter(|block| {
                        block.get("type").and_then(serde_json::Value::as_str) == Some("text")
                    })
                    .filter_map(|block| block.get("text").and_then(serde_json::Value::as_str))
                    .collect::<String>()
            })
            .unwrap_or_default();
        if text.trim().is_empty() {
            emit_terminal_error(
                prod,
                &mut state,
                "non-stream response contained no text content".into(),
                false,
            );
            return;
        }
        state.output.content.push(Content::text(text.clone()));
        state.output.response_id = value
            .get("id")
            .and_then(serde_json::Value::as_str)
            .map(str::to_string);
        state.output.response_model = value
            .get("model")
            .and_then(serde_json::Value::as_str)
            .map(str::to_string);
        if let Some(usage) = value.get("usage") {
            state.output.usage.input = usage
                .get("input_tokens")
                .and_then(serde_json::Value::as_i64)
                .unwrap_or(0);
            state.output.usage.output = usage
                .get("output_tokens")
                .and_then(serde_json::Value::as_i64)
                .unwrap_or(0);
            state.output.usage.total_tokens = state.output.usage.input + state.output.usage.output;
        }
        state.output.stop_reason = StopReason::Stop;
        let partial = std::sync::Arc::new(state.output.clone());
        prod.push(AssistantMessageEvent::Start {
            partial: partial.clone(),
        });
        prod.push(AssistantMessageEvent::TextStart {
            content_index: 0,
            partial: partial.clone(),
        });
        prod.push(AssistantMessageEvent::TextDelta {
            content_index: 0,
            delta: text.clone(),
            partial: partial.clone(),
        });
        prod.push(AssistantMessageEvent::TextEnd {
            content_index: 0,
            content: text,
            partial,
        });
        prod.push(AssistantMessageEvent::Done {
            reason: DoneReason::Stop,
            message: state.output,
        });
        return;
    }

    // ---- push start, drive SSE → mapper → terminal ----
    // The TS pushes `start` here right before the event loop; the Rust mapper
    // defers `start` to the first event (`ensure_started`) so a pre-stream
    // failure never delivers a partial `start` with no terminal. `run_mapper`
    // applies the model-aware `cost_fn` on the final usage before the terminal
    // event (mirrors the inline `calculateCost(model, output.usage)` calls).
    let mut sse = SseEventStream::new(response, opts.signal.clone());
    let cost_model = model.cost.clone();
    let cost_fn = move |usage: &Usage| calculate_cost(&cost_model, usage);
    run_mapper(&mut sse, prod, &mut state, cost_fn).await;
}

fn strip_cache_control(value: &mut serde_json::Value) {
    match value {
        serde_json::Value::Object(map) => {
            map.remove("cache_control");
            for child in map.values_mut() {
                strip_cache_control(child);
            }
        }
        serde_json::Value::Array(items) => {
            for child in items {
                strip_cache_control(child);
            }
        }
        _ => {}
    }
}

fn simplify_non_stream_request(value: &mut serde_json::Value) {
    let text_from_blocks = |blocks: &serde_json::Value| {
        blocks
            .as_array()
            .map(|items| {
                items
                    .iter()
                    .filter_map(|item| item.get("text").and_then(serde_json::Value::as_str))
                    .collect::<String>()
            })
            .unwrap_or_default()
    };
    if let Some(messages) = value
        .get_mut("messages")
        .and_then(serde_json::Value::as_array_mut)
    {
        for message in messages {
            if let Some(content) = message.get_mut("content") {
                if content.is_array() {
                    *content = serde_json::Value::String(text_from_blocks(content));
                }
            }
        }
    }
    if let Some(system) = value.get_mut("system") {
        if system.is_array() {
            *system = serde_json::Value::String(text_from_blocks(system));
        }
    }
}

// ----------------------------------------------------------------------------
// Header + auth helpers — mirrors createClient (API-key arm) + assertRequestAuth.
// ----------------------------------------------------------------------------

/// Resolve the API key: `opts.api_key` wins, then the provider default, then
/// `ANTHROPIC_API_KEY` from the environment. Mirrors the TS fallback chain
/// (`options?.apiKey` → SDK's `apiKey: null` reads the env var internally).
fn resolve_api_key(provider_key: &Option<String>, opts: &SimpleStreamOptions) -> Option<String> {
    opts.api_key
        .clone()
        .or_else(|| provider_key.clone())
        .or_else(|| {
            std::env::var("ANTHROPIC_API_KEY")
                .ok()
                .filter(|s| !s.is_empty())
        })
}

/// True when `headers` carries an `authorization`, `x-api-key`, or
/// `cf-aig-authorization` header — the auth-owned-header cases
/// `assertRequestAuth` accepts without a resolved API key. Mirrors `hasHeader`
/// over those three names (case-insensitive).
fn has_header_auth(headers: &Option<std::collections::BTreeMap<String, String>>) -> bool {
    let Some(h) = headers else {
        return false;
    };
    const NAMES: &[&str] = &["authorization", "x-api-key", "cf-aig-authorization"];
    h.keys()
        .any(|k| NAMES.contains(&k.to_ascii_lowercase().as_str()))
}

/// Assemble the HTTP header set for the POST. Mirrors the API-key arm of TS
/// `createClient` (anthropic-messages.ts:914-936): `accept`,
/// `anthropic-dangerous-direct-browser-access`, `anthropic-version`,
/// `anthropic-beta` (when present), `x-session-affinity` (when enabled), then
/// `model.headers` and `opts.headers` merged (later wins). `x-api-key` is
/// applied LAST so caller headers cannot clobber the resolved key (the SDK
/// attaches it out-of-band from `defaultHeaders`).
fn assemble_headers(
    model: &Model,
    opts: &SimpleStreamOptions,
    beta_header: Option<&str>,
    api_key: Option<&str>,
) -> Vec<(String, String)> {
    let mut headers: Vec<(String, String)> = Vec::new();
    headers.push(("accept".into(), "application/json".into()));
    if std::env::var("RPI_ANTHROPIC_NON_STREAM").ok().as_deref() != Some("1") {
        headers.push((
            "anthropic-dangerous-direct-browser-access".into(),
            "true".into(),
        ));
    }
    headers.push(("anthropic-version".into(), ANTHROPIC_VERSION.into()));
    if let Some(beta) = beta_header {
        headers.push(("anthropic-beta".into(), beta.into()));
    }

    // Session affinity: TS `cacheSessionId = cacheRetention === "none" ?
    // undefined : options?.sessionId`, then gated on the model's
    // `sendSessionAffinityHeaders` compat flag (default false).
    if !matches!(opts.cache_retention, CacheRetention::None) {
        if let Some(session_id) = &opts.session_id {
            let send = model
                .compat
                .as_ref()
                .and_then(|c| match c {
                    StreamingProtocolCompat::AnthropicMessages(a) => Some(a),
                    _ => None,
                })
                .map(|a| a.send_session_affinity_headers.unwrap_or(false))
                .unwrap_or(false);
            if send {
                headers.push(("x-session-affinity".into(), session_id.clone()));
            }
        }
    }

    // model.headers then opts.headers (later wins). Mirrors `mergeHeaders`.
    if let Some(model_headers) = &model.headers {
        for (k, v) in model_headers {
            merge_header(&mut headers, k.clone(), v.clone());
        }
    }
    if let Some(opt_headers) = &opts.headers {
        for (k, v) in opt_headers {
            merge_header(&mut headers, k.clone(), v.clone());
        }
    }

    // x-api-key applied last so it is not overridable by model/opts headers
    // (matches the SDK attaching it out-of-band). When a key is resolved it is
    // authoritative: drop any `x-api-key` a caller injected via `headers` so
    // only the resolved key is sent. Header-owned-auth callers pass
    // `api_key = None` here and rely on their own `x-api-key` / `authorization`
    // header surviving the merges above.
    if let Some(key) = api_key {
        headers.retain(|(k, _)| k.to_ascii_lowercase() != "x-api-key");
        headers.push(("x-api-key".into(), key.into()));
    }

    headers
}

/// Insert-or-replace a header by lowercase name. Mirrors TS `mergeHeaders` /
/// `Object.assign` last-wins semantics.
fn merge_header(headers: &mut Vec<(String, String)>, name: String, value: String) {
    let lname = name.to_ascii_lowercase();
    if let Some(slot) = headers
        .iter_mut()
        .find(|(k, _)| k.to_ascii_lowercase() == lname)
    {
        slot.1 = value;
    } else {
        headers.push((name, value));
    }
}

/// Wall-clock ms-since-epoch for the assistant message timestamp. Mirrors TS
/// `Date.now()`. The provider is the one `pi-ai` component that touches real
/// time: the message timestamp is persisted into the harness JSONL log, so a
/// monotonic counter (as faux uses) would be wrong here.
fn now_ms() -> i64 {
    use std::time::{SystemTime, UNIX_EPOCH};
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as i64)
        .unwrap_or(0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::providers::anthropic::models::claude_haiku_4_5;
    use crate::types::{Api, InputModality, ModelCost};

    /// Build a minimal model for header/auth tests (no network).
    fn test_model() -> Model {
        let mut m = Model::new(
            "claude-haiku-4-5",
            "Claude Haiku 4.5",
            Api::AnthropicMessages,
            "anthropic",
            "https://api.anthropic.com",
        );
        m.input = vec![InputModality::Text];
        m.context_window = 200_000;
        m.max_tokens = 8_192;
        m.cost = ModelCost::default();
        m.compat = Some(crate::model::StreamingProtocolCompat::AnthropicMessages(
            crate::model::AnthropicMessagesCompat::default(),
        ));
        m
    }

    #[test]
    fn assemble_headers_includes_required_defaults() {
        let model = test_model();
        let opts = SimpleStreamOptions::default();
        let headers = assemble_headers(&model, &opts, None, Some("sk-test"));
        let names: Vec<&str> = headers.iter().map(|(k, _)| k.as_str()).collect();
        assert!(names.contains(&"accept"));
        assert!(names.contains(&"anthropic-version"));
        assert!(names.contains(&"anthropic-dangerous-direct-browser-access"));
        assert!(names.contains(&"x-api-key"));
        // No beta header when none requested.
        assert!(!names.contains(&"anthropic-beta"));
        let version = headers
            .iter()
            .find(|(k, _)| k == "anthropic-version")
            .map(|(_, v)| v.as_str())
            .unwrap();
        assert_eq!(version, ANTHROPIC_VERSION);
    }

    #[test]
    fn assemble_headers_applies_beta_when_present() {
        let model = test_model();
        let opts = SimpleStreamOptions::default();
        let headers = assemble_headers(
            &model,
            &opts,
            Some("fine-grained-tool-streaming-2025-05-14"),
            Some("k"),
        );
        let beta = headers
            .iter()
            .find(|(k, _)| k == "anthropic-beta")
            .map(|(_, v)| v.as_str())
            .unwrap();
        assert_eq!(beta, "fine-grained-tool-streaming-2025-05-14");
    }

    #[test]
    fn assemble_headers_x_api_key_not_overridable_by_opts() {
        let model = test_model();
        let mut opts = SimpleStreamOptions::default();
        let mut extra = std::collections::BTreeMap::new();
        // Caller tries to override the resolved key via opts.headers — should
        // be ignored (x-api-key applied last wins), matching SDK semantics.
        extra.insert("x-api-key".to_string(), "SK-ATTACK".to_string());
        opts.headers = Some(extra);
        let headers = assemble_headers(&model, &opts, None, Some("sk-resolved"));
        let key = headers
            .iter()
            .find(|(k, _)| k == "x-api-key")
            .map(|(_, v)| v.as_str())
            .unwrap();
        assert_eq!(key, "sk-resolved");
    }

    #[test]
    fn assemble_headers_session_affinity_gated_on_compat_and_retention() {
        // Haiku 4.5 uses the catalog model which has send_session_affinity off
        // (default false) → no x-session-affinity even with a session id.
        let mut model = claude_haiku_4_5();
        let _ = &mut model; // catalog model, compat default has no affinity flag
        let mut opts = SimpleStreamOptions::default();
        opts.session_id = Some("sess-1".into());
        opts.cache_retention = CacheRetention::Short;
        let headers = assemble_headers(&model, &opts, None, Some("k"));
        assert!(
            !headers.iter().any(|(k, _)| k == "x-session-affinity"),
            "default catalog should not send session affinity"
        );

        // Opt in via compat flag → header emitted.
        let mut model = test_model();
        let mut compat = crate::model::AnthropicMessagesCompat::default();
        compat.send_session_affinity_headers = Some(true);
        model.compat = Some(StreamingProtocolCompat::AnthropicMessages(compat));
        let headers = assemble_headers(&model, &opts, None, Some("k"));
        assert_eq!(
            headers
                .iter()
                .find(|(k, _)| k == "x-session-affinity")
                .map(|(_, v)| v.as_str()),
            Some("sess-1")
        );

        // CacheRetention::None suppresses it even when compat is on.
        let mut opts2 = opts.clone();
        opts2.cache_retention = CacheRetention::None;
        let headers = assemble_headers(&model, &opts2, None, Some("k"));
        assert!(
            !headers.iter().any(|(k, _)| k == "x-session-affinity"),
            "None retention suppresses session affinity"
        );
    }

    #[test]
    fn assemble_headers_model_and_opts_merge_last_wins() {
        let mut model = test_model();
        let mut mh = std::collections::BTreeMap::new();
        mh.insert("x-custom".to_string(), "from-model".to_string());
        model.headers = Some(mh);
        let mut opts = SimpleStreamOptions::default();
        let mut oh = std::collections::BTreeMap::new();
        oh.insert("x-custom".to_string(), "from-opts".to_string());
        oh.insert("x-extra".to_string(), "extra".to_string());
        opts.headers = Some(oh);
        let headers = assemble_headers(&model, &opts, None, None);
        assert_eq!(
            headers
                .iter()
                .find(|(k, _)| k == "x-custom")
                .map(|(_, v)| v.as_str()),
            Some("from-opts"),
            "opts.headers wins over model.headers"
        );
        assert_eq!(
            headers
                .iter()
                .find(|(k, _)| k == "x-extra")
                .map(|(_, v)| v.as_str()),
            Some("extra")
        );
    }

    #[test]
    fn has_header_auth_detects_owned_auth_headers() {
        let mut h = std::collections::BTreeMap::new();
        h.insert("X-API-Key".to_string(), "k".to_string());
        assert!(has_header_auth(&Some(h.clone())));
        let mut h2 = std::collections::BTreeMap::new();
        h2.insert("authorization".to_string(), "Bearer t".to_string());
        assert!(has_header_auth(&Some(h2)));
        assert!(!has_header_auth(&None));
        let mut h3 = std::collections::BTreeMap::new();
        h3.insert("x-other".to_string(), "v".to_string());
        assert!(!has_header_auth(&Some(h3)));
    }

    #[test]
    fn resolve_api_key_prefers_opts_then_provider_then_env() {
        let opts = SimpleStreamOptions::default().with_api_key("opts-key");
        assert_eq!(
            resolve_api_key(&Some("provider-key".into()), &opts),
            Some("opts-key".into())
        );

        let opts = SimpleStreamOptions::default();
        assert_eq!(
            resolve_api_key(&Some("provider-key".into()), &opts),
            Some("provider-key".into())
        );
    }

    #[test]
    fn provider_exposes_catalog_models() {
        let p = AnthropicProvider::from_env();
        assert_eq!(p.id(), "anthropic");
        assert!(!p.models().is_empty());
        assert!(p.models().iter().any(|m| m.id == "claude-haiku-4-5"));
        assert!(p.api_key().is_none(), "from_env does not pre-read the key");
    }

    /// Regression for the Bearer-on-`model.headers` path (the
    /// `ANTHROPIC_AUTH_TOKEN` / `models.json authHeader:true` routes in
    /// rpi-cli's `provider::resolve`): when the auth header lives on
    /// `model.headers` and no key is set, the header-owned-auth check must pass
    /// so the request is NOT rejected as "No API key for provider". This mirrors
    /// the net effect of upstream `assertRequestAuth`, which runs *after* the
    /// model-resolution layer has merged `model.headers` into `options.headers`.
    #[test]
    fn header_auth_on_model_headers_counts_as_owned() {
        let mut model = test_model();
        let mut mh = std::collections::BTreeMap::new();
        mh.insert("authorization".to_string(), "Bearer tok".to_string());
        model.headers = Some(mh);

        // No opts.headers, no key — the only auth is on the model. Must satisfy.
        let owned = has_header_auth(&SimpleStreamOptions::default().headers)
            || has_header_auth(&model.headers);
        assert!(
            owned,
            "a Bearer header on model.headers must satisfy header-owned auth"
        );

        // A model carrying only non-auth headers must NOT satisfy it.
        let mut model2 = test_model();
        let mut mh2 = std::collections::BTreeMap::new();
        mh2.insert("x-custom".to_string(), "v".to_string());
        model2.headers = Some(mh2);
        let owned2 = has_header_auth(&SimpleStreamOptions::default().headers)
            || has_header_auth(&model2.headers);
        assert!(
            !owned2,
            "non-auth headers on model.headers must not satisfy header-owned auth"
        );
    }
}