Skip to main content

paladin_llm/
lib.rs

1//! # paladin-llm
2//!
3//! LLM provider adapters for the Paladin framework.
4//!
5//! This crate provides concrete adapter implementations for multiple LLM providers,
6//! all implementing the [`paladin_ports::output::llm_port::LlmPort`] trait defined
7//! in `paladin-ports`.
8//!
9//! ## Supported Providers
10//!
11//! | Feature flag | Provider | Types |
12//! |---|---|---|
13//! | `openai` (default) | OpenAI | [`openai::OpenAIAdapter`], [`openai::OpenAIConfig`] |
14//! | `anthropic` | Anthropic | `anthropic::AnthropicAdapter`, `anthropic::AnthropicConfig` |
15//! | `deepseek` | DeepSeek | `deepseek::DeepSeekAdapter`, `deepseek::DeepSeekConfig` |
16//! | `kimi` | Kimi (Moonshot AI) | `kimi::KimiAdapter`, `kimi::KimiConfig` |
17//! | `qwen` | Qwen (Alibaba DashScope) | `qwen::QwenAdapter`, `qwen::QwenConfig` |
18//! | `grok` | Grok (xAI) | `grok::GrokAdapter`, `grok::GrokConfig` |
19//! | `ollama` | Ollama (self-hosted, keyless) | `ollama::OllamaAdapter`, `ollama::OllamaConfig` |
20//! | `openai-compatible` | Any OpenAI-compatible endpoint (operator-configured) | `openai_compatible::OpenAiCompatibleAdapter`, `openai_compatible::OpenAiCompatibleConfig` |
21//! | `gemini` | Google Gemini (text-only, bespoke protocol) | `gemini::GeminiAdapter`, `gemini::GeminiConfig` |
22//! | `mock` (default) | Testing | [`mock::MockLlmAdapter`], [`mock::MultiStepMockLlmPort`] |
23//! | *(none — always compiled)* | Model fallback chain over any of the above | [`fallback::FallbackLlmAdapter`] |
24//! | `openai-embeddings` | OpenAI Embeddings | `openai::OpenAIEmbeddingAdapter` |
25//! | `vision` | Vision (multimodal) | Extends OpenAI and Anthropic adapters |
26//!
27//! ## Architecture
28//!
29//! Follows the Hexagonal Architecture pattern — this crate is a pure adapter
30//! layer. It depends only on `paladin-core` (domain types) and `paladin-ports`
31//! (port trait contracts). It has no dependency on the root `paladin` crate.
32//!
33//! ## Quick Start
34//!
35//! ```rust,no_run
36//! # #[cfg(feature = "openai")]
37//! # {
38//! use paladin_llm::openai::{OpenAIAdapter, OpenAIConfig};
39//! use paladin_llm::provider_factory::LlmProviderFactory;
40//!
41//! // From environment variables
42//! let factory = LlmProviderFactory::new();
43//! let provider = factory.create("openai").expect("OPENAI_API_KEY must be set");
44//! # }
45//! ```
46
47#![deny(unsafe_code)]
48#![warn(missing_docs)]
49
50/// Rate pacing (PACE-02, D-01/D-07/D-08): [`cadence::CadenceLlmAdapter`] decorates an
51/// `Arc<dyn LlmPort>` with a per-(provider, model) gate that a provider 429 opens, over the shared
52/// [`paladin_ports::output::cadence_port::CadencePort`]. Not feature-gated -- it needs no provider
53/// adapter itself, mirroring [`pricing`] and [`fallback`].
54pub mod cadence;
55/// Shared configuration types for LLM providers and request behavior.
56#[allow(missing_docs)]
57pub mod config;
58/// Shared LLM adapter conformance suite (RT-06, D-31): one fixed case list,
59/// instantiated once per adapter via [`llm_conformance_suite!`], so every
60/// provider is measured against the same bar rather than its own
61/// hand-written tests. `#[cfg(test)]`-only, gated on any feature that pulls
62/// in `reqwest` (its own in-file `TrivialAdapter` needs an HTTP client) --
63/// `reqwest`'s implicit same-named feature is suppressed here because
64/// `openai`/etc already reference it via `dep:reqwest` (Cargo's namespaced-
65/// features rule), so this lists every feature that declares that edge.
66#[cfg(all(
67    test,
68    any(
69        feature = "openai",
70        feature = "anthropic",
71        feature = "deepseek",
72        feature = "kimi",
73        feature = "qwen",
74        feature = "grok",
75        feature = "ollama",
76        feature = "openai-compatible",
77        feature = "gemini"
78    )
79))]
80mod conformance;
81/// Error types returned by provider adapters.
82#[allow(missing_docs)]
83pub mod error;
84/// Model fallback (FT-FR-16/17): [`fallback::FallbackLlmAdapter`] composes an
85/// ordered chain of plain `LlmPort`s and hops on Transient/Unknown errors
86/// only. Not feature-gated (ADR-0046) — it needs no provider adapter itself.
87pub mod fallback;
88/// The one shared HTTP-status-to-`LlmError` mapping every provider adapter
89/// routes its non-2xx branch through (FT-FR-01, D-03) — redacts before it
90/// bounds, and emits a typed `ProviderError { status }` for every status
91/// without a dedicated variant. Not feature-gated.
92pub mod http_status;
93/// LLM-backed content analysis service orchestration.
94#[allow(missing_docs)]
95pub mod llm_analysis_service;
96/// Treasurer per-call pricing (D-08/D-09, ADR-0052): [`pricing::PricingLlmAdapter`] decorates
97/// an `Arc<dyn LlmPort>` with cost from an operator [`paladin_core::platform::container::cost::PriceTable`].
98/// Not feature-gated — it needs no provider adapter itself, mirroring [`fallback`].
99pub mod pricing;
100/// Factory for selecting provider adapters from runtime configuration.
101#[allow(missing_docs)]
102pub mod provider_factory;
103// Documented by its own inner `//!` block (a `pub mod` carries one doc form, never both).
104pub mod rate_limit_headers;
105// Documented by its own inner `//!` block (a `pub mod` carries one doc form, never both).
106pub mod reasoning;
107/// Credential redaction shared by every provider adapter (not feature-gated
108/// — reused by the shared compatible core and by bespoke adapters alike).
109#[allow(missing_docs)]
110pub mod redaction;
111/// The per-model profile registry (ADR-0061, D-05..D-07): seeded entry, config
112/// [`registry::ModelOverride`], adapter-wide fallback, and the visible unknown-model fallback.
113/// Not feature-gated -- every adapter resolves its profile through it.
114pub mod registry;
115/// The one wire-time sampling filter every request builder calls (ADR-0061): a set parameter is
116/// sent only when the model's profile admits it, every omission is logged once and recorded under
117/// [`sampling::PARAMS_OMITTED_METADATA_KEY`]. Not feature-gated.
118pub mod sampling;
119/// Application-layer services composing this crate's ports (provider capabilities,
120/// [`paladin_ports::output::token_counter_port::TokenCounterPort`]) rather than
121/// implementing a provider adapter themselves. Houses [`services::commissary`], the
122/// v0.10.0-native prompt-budgeting service (fail-loud pre-flight guard + bounded,
123/// priority-ordered allocator with explicit truncation markers).
124#[allow(missing_docs)]
125pub mod services;
126/// The check every usage mapper's output passes through (G-50-5a): the vendor's own total and
127/// the containment [`paladin_core::platform::container::token_usage::TokenUsage`] promises are
128/// compared with the mapped usage, and any finding is logged once and recorded under
129/// [`usage_reconcile::USAGE_UNRECONCILED_METADATA_KEY`]. Not feature-gated.
130pub mod usage_reconcile;
131// The one user-turn text helper every provider adapter's request builder calls (PARITY-08,
132// handoff B-4). Crate-private; documented by its own inner `//!` block; compiled only when a
133// provider adapter is.
134#[cfg(any(
135    feature = "openai",
136    feature = "anthropic",
137    feature = "deepseek",
138    feature = "kimi",
139    feature = "qwen",
140    feature = "grok",
141    feature = "ollama",
142    feature = "gemini",
143    feature = "openai-compatible"
144))]
145mod turn;
146// The one rule deciding what a completion's text and finish reason mean: refusal, content filter,
147// function call, empty on any stop (PARITY-05, D-09/D-11a). Crate-private; documented by its own
148// inner `//!` block; compiled only when an adapter that calls it is.
149#[cfg(any(
150    feature = "openai",
151    feature = "anthropic",
152    feature = "gemini",
153    feature = "deepseek",
154    feature = "kimi",
155    feature = "qwen",
156    feature = "grok",
157    feature = "ollama",
158    feature = "openai-compatible"
159))]
160mod response_guards;
161// Reassembles complete SSE lines from arbitrarily split network reads so a frame that straddles
162// two reads is not dropped (CR-02). Crate-private; compiled with the adapters that stream.
163#[cfg(any(
164    feature = "openai",
165    feature = "anthropic",
166    feature = "gemini",
167    feature = "deepseek",
168    feature = "kimi",
169    feature = "qwen",
170    feature = "grok",
171    feature = "ollama",
172    feature = "openai-compatible"
173))]
174mod sse;
175// The shared saturating exponential back-off for the retrying adapters (WR-02). Crate-private.
176#[cfg(any(
177    feature = "openai",
178    feature = "deepseek",
179    feature = "kimi",
180    feature = "qwen",
181    feature = "grok",
182    feature = "ollama",
183    feature = "openai-compatible"
184))]
185mod backoff;
186/// The shared context-window precedence resolver (PRIM-04, D-01/D-02): one
187/// pure function, [`window::resolve_context_window`], that every consumer of
188/// [`paladin_ports::output::llm_port::ProviderCapabilities`] calls through
189/// instead of writing its own config-table/capability/fallback walk. Not
190/// feature-gated — both [`services::commissary`] and the facade's
191/// `HistoryTrimmer` need it unconditionally.
192pub mod window;
193
194// Mock-endpoint plumbing shared by every adapter's wire tests; not gated on any adapter feature.
195#[cfg(test)]
196mod test_support;
197
198#[cfg(any(
199    feature = "kimi",
200    feature = "qwen",
201    feature = "grok",
202    feature = "ollama",
203    feature = "openai-compatible"
204))]
205/// Shared OpenAI-compatible protocol engine (D-05) that thin provider
206/// presets sit on. Widen this `any(...)` list as later presets land.
207#[allow(missing_docs)]
208pub mod compat;
209
210#[cfg(feature = "openai")]
211/// OpenAI provider adapter and related configuration.
212#[allow(missing_docs)]
213pub mod openai;
214
215#[cfg(feature = "anthropic")]
216/// Anthropic provider adapter and related configuration.
217#[allow(missing_docs)]
218pub mod anthropic;
219
220#[cfg(feature = "deepseek")]
221/// DeepSeek provider adapter and related configuration.
222#[allow(missing_docs)]
223pub mod deepseek;
224
225#[cfg(feature = "mock")]
226/// Mock provider adapters for tests and deterministic workflows.
227#[allow(missing_docs)]
228pub mod mock;
229
230#[cfg(feature = "kimi")]
231/// Kimi (Moonshot AI) provider adapter and related configuration.
232#[allow(missing_docs)]
233pub mod kimi;
234
235#[cfg(feature = "qwen")]
236/// Qwen (Alibaba DashScope) provider adapter and related configuration.
237#[allow(missing_docs)]
238pub mod qwen;
239
240#[cfg(feature = "grok")]
241/// Grok (xAI) provider adapter and related configuration.
242#[allow(missing_docs)]
243pub mod grok;
244
245#[cfg(feature = "ollama")]
246/// Ollama (self-hosted, keyless) provider adapter and related configuration.
247#[allow(missing_docs)]
248pub mod ollama;
249
250#[cfg(feature = "openai-compatible")]
251/// Generic operator-configured OpenAI-compatible provider adapter and
252/// related configuration (D-03) — points at any OpenAI-compatible endpoint
253/// with no new code.
254#[allow(missing_docs)]
255pub mod openai_compatible;
256
257#[cfg(feature = "gemini")]
258/// Google Gemini (text-only) provider adapter and related configuration
259/// (D-08) — implements [`paladin_ports::output::llm_port::LlmPort`]
260/// directly against Gemini's own bespoke `generateContent` protocol; does
261/// not sit on [`compat::CompatEngine`].
262#[allow(missing_docs)]
263pub mod gemini;
264
265/// Cross-adapter capability invariants (WEB-03, ADR-0004).
266///
267/// These tests need all three shipped adapters in scope simultaneously, which none of
268/// the per-adapter `#[cfg(test)]` modules can see on their own (each only compiles
269/// under its own feature flag) — so they live here, gated on all three features being
270/// enabled together (as they are for `cargo test --workspace`, since the root
271/// `paladin-ai` package requests `openai`, `anthropic` and `deepseek` together).
272#[cfg(all(test, feature = "openai", feature = "anthropic", feature = "deepseek"))]
273mod capability_invariants {
274    use crate::anthropic::{AnthropicAdapter, AnthropicConfig};
275    use crate::deepseek::{DeepSeekAdapter, DeepSeekConfig};
276    use crate::openai::{OpenAIAdapter, OpenAIConfig};
277    use paladin_ports::output::llm_port::LlmPort;
278
279    /// WEB-03's own success criterion 3: a test asserting the correspondence between
280    /// the declared tool-calling capability and whether a tool-calling request path
281    /// actually exists. `LlmRequest`'s complete field set is `id`, `model`, `prompt`,
282    /// `attachments`, `stream`, `metadata` — no field through which a tool definition
283    /// could travel — so the request surface never supports tool calling today, and
284    /// every shipped adapter's declared capability must match that fact.
285    ///
286    /// Extended for D-12: no shipped adapter's `generate()` ever returns a populated
287    /// `function_call` on its `LlmResponse` — every occurrence of a populated one in
288    /// the workspace is in a test double under `tests/` — so `supports_function_calling`
289    /// is pinned here too, by the same test, so the two flags cannot drift apart
290    /// independently again.
291    #[test]
292    fn test_capabilities_tool_calling_matches_request_surface() {
293        // `LlmRequest` has no tools field today, so no adapter's request path can
294        // carry a tool call. This is the single source of truth the correspondence
295        // below is checked against.
296        const REQUEST_SURFACE_SUPPORTS_TOOL_CALLING: bool = false;
297        // No shipped adapter's `generate()` ever returns `Some(FunctionCall)` on the
298        // `LlmResponse` it builds. This is the single source of truth the second
299        // correspondence below is checked against (D-12).
300        const RESPONSE_SURFACE_SUPPORTS_FUNCTION_CALLING: bool = false;
301
302        let openai = OpenAIAdapter::new(OpenAIConfig::new(
303            "test-key".to_string(),
304            crate::openai::adapter::OPENAI_DEFAULT_MODEL.to_string(),
305        ))
306        .unwrap();
307        let anthropic = AnthropicAdapter::new(AnthropicConfig::new(
308            "sk-ant-test123".to_string(),
309            "claude-3-5-sonnet-20241022".to_string(),
310        ))
311        .unwrap();
312        let deepseek = DeepSeekAdapter::new(DeepSeekConfig::new(
313            "test-key".to_string(),
314            "deepseek-chat".to_string(),
315        ))
316        .unwrap();
317
318        for (name, declared_tool_calling, declared_function_calling) in [
319            (
320                "openai",
321                openai.get_capabilities().supports_tool_calling,
322                openai.get_capabilities().supports_function_calling,
323            ),
324            (
325                "anthropic",
326                anthropic.get_capabilities().supports_tool_calling,
327                anthropic.get_capabilities().supports_function_calling,
328            ),
329            (
330                "deepseek",
331                deepseek.get_capabilities().supports_tool_calling,
332                deepseek.get_capabilities().supports_function_calling,
333            ),
334        ] {
335            assert_eq!(
336                declared_tool_calling, REQUEST_SURFACE_SUPPORTS_TOOL_CALLING,
337                "{name}'s declared supports_tool_calling ({declared_tool_calling}) must match whether a \
338                 tool-calling request path exists on LlmRequest ({REQUEST_SURFACE_SUPPORTS_TOOL_CALLING})"
339            );
340            assert_eq!(
341                declared_function_calling, RESPONSE_SURFACE_SUPPORTS_FUNCTION_CALLING,
342                "{name}'s declared supports_function_calling ({declared_function_calling}) must match \
343                 whether generate() ever returns a populated function_call \
344                 ({RESPONSE_SURFACE_SUPPORTS_FUNCTION_CALLING})"
345            );
346        }
347    }
348
349    /// The assumption-delta invariant test: every shipped adapter must declare a
350    /// `Some((min, max))` temperature range, never silently fall back to `None` (the
351    /// framework's `[0.0, 1.0]` default). Catches a future adapter reintroducing the
352    /// singular global-clamp assumption the moment it lands.
353    ///
354    /// This module constructs the three adapters that need no feature beyond their own; the
355    /// six compat-era adapters (Kimi, Qwen, Grok, Gemini, Ollama, OpenAI-compatible) are
356    /// covered by `every_new_provider_declares_a_temperature_range` in
357    /// `capability_invariants_new_providers` below.
358    #[test]
359    fn test_every_adapter_declares_a_temperature_range() {
360        let openai = OpenAIAdapter::new(OpenAIConfig::new(
361            "test-key".to_string(),
362            crate::openai::adapter::OPENAI_DEFAULT_MODEL.to_string(),
363        ))
364        .unwrap();
365        let anthropic = AnthropicAdapter::new(AnthropicConfig::new(
366            "sk-ant-test123".to_string(),
367            "claude-3-5-sonnet-20241022".to_string(),
368        ))
369        .unwrap();
370        let deepseek = DeepSeekAdapter::new(DeepSeekConfig::new(
371            "test-key".to_string(),
372            "deepseek-chat".to_string(),
373        ))
374        .unwrap();
375
376        assert!(
377            openai.get_capabilities().temperature_range.is_some(),
378            "openai must declare a temperature_range"
379        );
380        assert!(
381            anthropic.get_capabilities().temperature_range.is_some(),
382            "anthropic must declare a temperature_range"
383        );
384        assert!(
385            deepseek.get_capabilities().temperature_range.is_some(),
386            "deepseek must declare a temperature_range"
387        );
388    }
389}
390
391/// Cross-adapter capability invariants for the six providers this phase adds
392/// (PROV-02, RESEARCH.md Pitfall 4, Open Question 3).
393///
394/// A **sibling** module to [`capability_invariants`] above, not a widening of its
395/// `cfg` gate: widening the existing module's gate would make the shipped three's
396/// invariant stop compiling unless every one of `kimi`/`qwen`/`grok`/`ollama`/`gemini`/
397/// `openai-compatible` were also enabled, silently disabling a test that runs today
398/// under `cargo test --workspace`. This module carries its own two source-of-truth
399/// constants rather than importing the sibling module's private ones, for the same
400/// reason: the two modules must be independently compilable under their own feature
401/// sets.
402#[cfg(all(
403    test,
404    feature = "kimi",
405    feature = "qwen",
406    feature = "grok",
407    feature = "ollama",
408    feature = "gemini",
409    feature = "openai-compatible"
410))]
411mod capability_invariants_new_providers {
412    use crate::gemini::adapter::GEMINI_DEFAULT_MODEL;
413    use crate::gemini::{GeminiAdapter, GeminiConfig};
414    use crate::grok::adapter::GROK_DEFAULT_MODEL;
415    use crate::grok::{GrokAdapter, GrokConfig};
416    use crate::kimi::adapter::KIMI_DEFAULT_MODEL;
417    use crate::kimi::{KimiAdapter, KimiConfig};
418    use crate::ollama::adapter::{OLLAMA_DEFAULT_BASE_URL, OLLAMA_DEFAULT_MODEL};
419    use crate::ollama::{OllamaAdapter, OllamaConfig};
420    use crate::openai_compatible::{
421        OpenAiCompatibleAdapter, OpenAiCompatibleCapabilitiesConfig, OpenAiCompatibleConfig,
422    };
423    use crate::qwen::adapter::QWEN_DEFAULT_MODEL;
424    use crate::qwen::{QwenAdapter, QwenConfig};
425    use paladin_ports::output::llm_port::LlmPort;
426
427    /// The temperature-range invariant for the six compat-era adapters, the counterpart of
428    /// `capability_invariants::test_every_adapter_declares_a_temperature_range` (which
429    /// constructs OpenAI, Anthropic and DeepSeek): every shipped adapter declares
430    /// `Some((min, max))`, never `None`.
431    ///
432    /// [`OpenAiCompatibleAdapter`] is the one adapter whose range is the operator's to declare
433    /// (D-04): with an empty declaration it reports `None` by design, so this test constructs it
434    /// the way an operator who declares a range does.
435    #[test]
436    fn every_new_provider_declares_a_temperature_range() {
437        let kimi = KimiAdapter::new(KimiConfig::new(
438            "test-key".to_string(),
439            KIMI_DEFAULT_MODEL.to_string(),
440        ))
441        .unwrap();
442        let qwen = QwenAdapter::new(QwenConfig::new(
443            "test-key".to_string(),
444            QWEN_DEFAULT_MODEL.to_string(),
445        ))
446        .unwrap();
447        let grok = GrokAdapter::new(GrokConfig::new(
448            "test-key".to_string(),
449            GROK_DEFAULT_MODEL.to_string(),
450        ))
451        .unwrap();
452        let ollama = OllamaAdapter::new(OllamaConfig::new(
453            OLLAMA_DEFAULT_BASE_URL.to_string(),
454            OLLAMA_DEFAULT_MODEL.to_string(),
455        ))
456        .unwrap();
457        let gemini = GeminiAdapter::new(GeminiConfig::new(
458            "test-key".to_string(),
459            GEMINI_DEFAULT_MODEL.to_string(),
460        ))
461        .unwrap();
462        let openai_compatible = OpenAiCompatibleAdapter::new(OpenAiCompatibleConfig::new(
463            "test-key".to_string(),
464            "http://localhost:8080".to_string(),
465            "test-model".to_string(),
466            OpenAiCompatibleCapabilitiesConfig {
467                supports_streaming: true,
468                supports_tool_calling: false,
469                supports_function_calling: false,
470                supports_vision: false,
471                supports_embeddings: false,
472                supports_system_messages: false,
473                max_context_tokens: None,
474                temperature_range: Some((0.0, 2.0)),
475            },
476        ))
477        .unwrap();
478
479        for (name, range) in [
480            ("kimi", kimi.get_capabilities().temperature_range),
481            ("qwen", qwen.get_capabilities().temperature_range),
482            ("grok", grok.get_capabilities().temperature_range),
483            ("ollama", ollama.get_capabilities().temperature_range),
484            ("gemini", gemini.get_capabilities().temperature_range),
485            (
486                "openai-compatible (declared range)",
487                openai_compatible.get_capabilities().temperature_range,
488            ),
489        ] {
490            assert!(range.is_some(), "{name} must declare a temperature_range");
491        }
492    }
493
494    /// This phase's own restatement of the sibling module's success criterion 3: a
495    /// test asserting the correspondence between the declared tool-calling
496    /// capability and whether a tool-calling request path actually exists.
497    /// `LlmRequest`'s complete field set is `id`, `model`, `prompt`, `attachments`,
498    /// `stream`, `metadata` — no field through which a tool definition could
499    /// travel — so the request surface never supports tool calling today, and
500    /// every one of the six adapters this phase adds must declare that fact
501    /// truthfully.
502    ///
503    /// The same reasoning covers `supports_function_calling`: no adapter added by
504    /// this phase ever returns a populated `function_call` on the `LlmResponse` it
505    /// builds — every occurrence of a populated one in the workspace is in a test
506    /// double under `tests/` — so both flags are pinned here together, by the same
507    /// test, so they cannot drift apart independently (D-12).
508    ///
509    /// [`OpenAiCompatibleAdapter`] is constructed from an **empty** capability
510    /// declaration (every field its pessimistic default) rather than a populated
511    /// one, so this test covers the exact configuration an operator gets when they
512    /// declare nothing (D-04) — the path most likely to regress if a future change
513    /// to the generic provider's defaulting logic reintroduces an over-reporting
514    /// capability flag (RESEARCH.md Pitfall 4/5).
515    #[test]
516    fn test_new_adapter_capabilities_match_request_surface() {
517        // `LlmRequest` has no tools field today, so no adapter's request path can
518        // carry a tool call. This is the single source of truth the correspondence
519        // below is checked against.
520        const REQUEST_SURFACE_SUPPORTS_TOOL_CALLING: bool = false;
521        // No adapter this phase adds ever returns `Some(FunctionCall)` on the
522        // `LlmResponse` it builds. This is the single source of truth the second
523        // correspondence below is checked against (D-12).
524        const RESPONSE_SURFACE_SUPPORTS_FUNCTION_CALLING: bool = false;
525
526        // A throwaway test key and a mockito-free placeholder base URL — none of
527        // these adapters make a request in this test, so the URL need not resolve.
528        let kimi = KimiAdapter::new(KimiConfig::new(
529            "test-key".to_string(),
530            KIMI_DEFAULT_MODEL.to_string(),
531        ))
532        .unwrap();
533        let qwen = QwenAdapter::new(QwenConfig::new(
534            "test-key".to_string(),
535            QWEN_DEFAULT_MODEL.to_string(),
536        ))
537        .unwrap();
538        let grok = GrokAdapter::new(GrokConfig::new(
539            "test-key".to_string(),
540            GROK_DEFAULT_MODEL.to_string(),
541        ))
542        .unwrap();
543        let ollama = OllamaAdapter::new(OllamaConfig::new(
544            OLLAMA_DEFAULT_BASE_URL.to_string(),
545            OLLAMA_DEFAULT_MODEL.to_string(),
546        ))
547        .unwrap();
548        let gemini = GeminiAdapter::new(GeminiConfig::new(
549            "test-key".to_string(),
550            GEMINI_DEFAULT_MODEL.to_string(),
551        ))
552        .unwrap();
553        // The pessimistic-default path (D-04): every field its own conservative
554        // default, exactly what an operator gets when they declare nothing.
555        let empty_capabilities = OpenAiCompatibleCapabilitiesConfig {
556            supports_streaming: true,
557            supports_tool_calling: false,
558            supports_function_calling: false,
559            supports_vision: false,
560            supports_embeddings: false,
561            supports_system_messages: false,
562            max_context_tokens: None,
563            temperature_range: None,
564        };
565        let openai_compatible = OpenAiCompatibleAdapter::new(OpenAiCompatibleConfig::new(
566            "test-key".to_string(),
567            "http://localhost:8080".to_string(),
568            "test-model".to_string(),
569            empty_capabilities,
570        ))
571        .unwrap();
572
573        for (name, declared_tool_calling, declared_function_calling) in [
574            (
575                "kimi",
576                kimi.get_capabilities().supports_tool_calling,
577                kimi.get_capabilities().supports_function_calling,
578            ),
579            (
580                "qwen",
581                qwen.get_capabilities().supports_tool_calling,
582                qwen.get_capabilities().supports_function_calling,
583            ),
584            (
585                "grok",
586                grok.get_capabilities().supports_tool_calling,
587                grok.get_capabilities().supports_function_calling,
588            ),
589            (
590                "ollama",
591                ollama.get_capabilities().supports_tool_calling,
592                ollama.get_capabilities().supports_function_calling,
593            ),
594            (
595                "gemini",
596                gemini.get_capabilities().supports_tool_calling,
597                gemini.get_capabilities().supports_function_calling,
598            ),
599            (
600                "openai-compatible (empty declaration)",
601                openai_compatible.get_capabilities().supports_tool_calling,
602                openai_compatible
603                    .get_capabilities()
604                    .supports_function_calling,
605            ),
606        ] {
607            assert_eq!(
608                declared_tool_calling, REQUEST_SURFACE_SUPPORTS_TOOL_CALLING,
609                "{name}'s declared supports_tool_calling ({declared_tool_calling}) must match whether a \
610                 tool-calling request path exists on LlmRequest ({REQUEST_SURFACE_SUPPORTS_TOOL_CALLING})"
611            );
612            assert_eq!(
613                declared_function_calling, RESPONSE_SURFACE_SUPPORTS_FUNCTION_CALLING,
614                "{name}'s declared supports_function_calling ({declared_function_calling}) must match \
615                 whether generate() ever returns a populated function_call \
616                 ({RESPONSE_SURFACE_SUPPORTS_FUNCTION_CALLING})"
617            );
618        }
619    }
620}