Skip to main content

trusty_code/provider/
adapter.rs

1//! Model-adapter factory — maps a model slug to a concrete [`Provider`].
2//!
3//! Why: The agent loop holds a model slug and needs the matching backend
4//! behaviour (tool-choice mapping, usage extraction). A single factory keeps the
5//! slug→provider decision in one place instead of scattering `if slug.starts_with`
6//! branches across the loop (#1021).
7//! What: [`provider_for`] returns an `Arc<dyn Provider>` — [`BedrockProvider`]
8//! for `bedrock/*` slugs and [`OpenRouterProvider`] for everything else.
9//! Test: `adapter::tests::*`.
10
11use std::sync::Arc;
12
13use super::bedrock::BedrockProvider;
14use super::openrouter::OpenRouterProvider;
15use super::traits::Provider;
16
17/// Slug prefix that routes to the Bedrock backend.
18const BEDROCK_PREFIX: &str = "bedrock/";
19
20/// Select the provider implementation for a model slug.
21///
22/// Why: The loop must stay backend-agnostic; this factory is the only place that
23/// knows how to map a slug to a backend, so adding a backend touches one site.
24/// What: Returns [`BedrockProvider`] for slugs beginning with `bedrock/`, and
25/// the default [`OpenRouterProvider`] (carrying the slug for its
26/// `supports_native_tools` decision) for everything else.
27/// Test: `adapter::tests::provider_for_routes_*`.
28pub fn provider_for(slug: &str) -> Arc<dyn Provider> {
29    if slug.starts_with(BEDROCK_PREFIX) {
30        Arc::new(BedrockProvider::new())
31    } else {
32        Arc::new(OpenRouterProvider::new(slug))
33    }
34}
35
36// ── Tests ─────────────────────────────────────────────────────────────────────
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    /// `bedrock/*` slugs route to the Bedrock provider.
43    ///
44    /// Why: Per-agent routing must direct Bedrock-hosted models to the Bedrock
45    /// backend, not OpenRouter.
46    /// What: Resolve a `bedrock/...` slug, assert `name() == "bedrock"`.
47    /// Test: this test.
48    #[test]
49    fn provider_for_routes_bedrock() {
50        let p = provider_for("bedrock/us.anthropic.claude-sonnet-4-5");
51        assert_eq!(p.name(), "bedrock");
52    }
53
54    /// Non-Bedrock slugs route to OpenRouter (the default).
55    ///
56    /// Why: Qwen/DeepSeek/Gemma/OpenAI/Anthropic-via-OR all go through
57    /// OpenRouter; verify the default branch for several families.
58    /// What: Resolve each slug, assert `name() == "openrouter"`.
59    /// Test: this test.
60    #[test]
61    fn provider_for_routes_openrouter_default() {
62        for slug in [
63            "qwen/qwen-2.5-coder-32b-instruct",
64            "deepseek/deepseek-chat",
65            "google/gemma-2-27b-it",
66            "openai/gpt-4o-mini",
67            "anthropic/claude-sonnet-4-5",
68        ] {
69            assert_eq!(
70                provider_for(slug).name(),
71                "openrouter",
72                "slug {slug} should route to OpenRouter"
73            );
74        }
75    }
76
77    /// The OpenRouter default carries the slug through to `supports_native_tools`.
78    ///
79    /// Why: The factory must build the OpenRouter provider with the slug so its
80    /// native-tool decision is correct, not a blank default.
81    /// What: Resolve a Claude slug and a Qwen slug; assert their native-tool
82    /// flags differ as expected.
83    /// Test: this test.
84    #[test]
85    fn provider_for_preserves_slug_for_native_tool_decision() {
86        assert!(provider_for("anthropic/claude-sonnet-4-5").supports_native_tools());
87        assert!(!provider_for("qwen/qwen-2.5-coder-32b-instruct").supports_native_tools());
88    }
89}