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, [`FireworksProvider`] for `fireworks/*`,
9//! [`TogetherProvider`] for `together/*`, and [`OpenRouterProvider`] for
10//! everything else.
11//! Test: `adapter::tests::*`.
12
13use std::sync::Arc;
14
15use super::atlascloud::AtlasCloudProvider;
16use super::bedrock::BedrockProvider;
17use super::fireworks::FireworksProvider;
18use super::openrouter::OpenRouterProvider;
19use super::together::TogetherProvider;
20use super::traits::Provider;
21
22/// Slug prefix that routes to the Bedrock backend.
23const BEDROCK_PREFIX: &str = "bedrock/";
24
25/// Slug prefix that routes to the Fireworks backend (#2406).
26const FIREWORKS_PREFIX: &str = "fireworks/";
27
28/// Slug prefix that routes to the Together.ai backend (#2494).
29const TOGETHER_PREFIX: &str = "together/";
30
31/// Slug prefix that routes to the AtlasCloud backend (#2536).
32const ATLASCLOUD_PREFIX: &str = "atlascloud/";
33
34/// Select the provider implementation for a model slug.
35///
36/// Why: The loop must stay backend-agnostic; this factory is the only place that
37/// knows how to map a slug to a backend, so adding a backend touches one site.
38/// What: Returns [`BedrockProvider`] for slugs beginning with `bedrock/`,
39/// [`FireworksProvider`] for slugs beginning with `fireworks/` (#2406),
40/// [`TogetherProvider`] for slugs beginning with `together/` (#2494),
41/// [`AtlasCloudProvider`] for slugs beginning with `atlascloud/` (#2536), and the
42/// default [`OpenRouterProvider`] (carrying the slug for its
43/// `supports_native_tools` decision) for everything else. The `together/` and
44/// `atlascloud/` checks sit alongside `fireworks/` — after `bedrock/` — so no
45/// `together/*` or `atlascloud/*` slug can fall through to the OpenRouter default.
46/// Test: `adapter::tests::provider_for_routes_*`.
47pub fn provider_for(slug: &str) -> Arc<dyn Provider> {
48    if slug.starts_with(BEDROCK_PREFIX) {
49        Arc::new(BedrockProvider::new())
50    } else if slug.starts_with(FIREWORKS_PREFIX) {
51        Arc::new(FireworksProvider::new())
52    } else if slug.starts_with(TOGETHER_PREFIX) {
53        Arc::new(TogetherProvider::new())
54    } else if slug.starts_with(ATLASCLOUD_PREFIX) {
55        Arc::new(AtlasCloudProvider::new())
56    } else {
57        Arc::new(OpenRouterProvider::new(slug))
58    }
59}
60
61// ── Tests ─────────────────────────────────────────────────────────────────────
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    /// `bedrock/*` slugs route to the Bedrock provider.
68    ///
69    /// Why: Per-agent routing must direct Bedrock-hosted models to the Bedrock
70    /// backend, not OpenRouter.
71    /// What: Resolve a `bedrock/...` slug, assert `name() == "bedrock"`.
72    /// Test: this test.
73    #[test]
74    fn provider_for_routes_bedrock() {
75        let p = provider_for("bedrock/us.anthropic.claude-sonnet-4-5");
76        assert_eq!(p.name(), "bedrock");
77    }
78
79    /// `fireworks/*` slugs route to the Fireworks provider (#2406).
80    ///
81    /// Why: per-agent routing must direct Fireworks-hosted models to the
82    /// Fireworks normalisation profile, not the OpenRouter default.
83    /// What: Resolve a `fireworks/...` slug, assert `name() == "fireworks"`.
84    /// Test: this test.
85    #[test]
86    fn provider_for_routes_fireworks() {
87        let p = provider_for("fireworks/accounts/fireworks/models/llama-v3p1-70b-instruct");
88        assert_eq!(p.name(), "fireworks");
89    }
90
91    /// `together/*` slugs route to the Together provider (#2494).
92    ///
93    /// Why: per-agent routing must direct Together-hosted models to the Together
94    /// normalisation profile, not the OpenRouter default.
95    /// What: Resolve a `together/...` slug, assert `name() == "together"`.
96    /// Test: this test.
97    #[test]
98    fn provider_for_routes_together() {
99        let p = provider_for("together/meta-llama/Llama-3.3-70B-Instruct-Turbo");
100        assert_eq!(p.name(), "together");
101    }
102
103    /// `atlascloud/*` slugs route to the AtlasCloud provider (#2536), including
104    /// the nested `atlascloud/openai/gpt-5.6-sol` form whose model id is itself
105    /// `vendor/model`-shaped.
106    ///
107    /// Why: per-agent routing must direct AtlasCloud-hosted models to the
108    /// AtlasCloud normalisation profile, not the OpenRouter default — and the
109    /// nested `openai/`-shaped model id must NOT re-home the slug to OpenRouter.
110    /// What: Resolve both the nested and a bare `atlascloud/...` slug, assert
111    /// `name() == "atlascloud"`.
112    /// Test: this test.
113    #[test]
114    fn provider_for_routes_atlascloud() {
115        assert_eq!(
116            provider_for("atlascloud/openai/gpt-5.6-sol").name(),
117            "atlascloud"
118        );
119        assert_eq!(provider_for("atlascloud/deepseek-v3").name(), "atlascloud");
120    }
121
122    /// Non-Bedrock, non-Fireworks, non-Together, non-AtlasCloud slugs route to
123    /// OpenRouter (the default).
124    ///
125    /// Why: Qwen/DeepSeek/Gemma/OpenAI/Anthropic-via-OR all go through
126    /// OpenRouter; verify the default branch for several families.
127    /// What: Resolve each slug, assert `name() == "openrouter"`.
128    /// Test: this test.
129    #[test]
130    fn provider_for_routes_openrouter_default() {
131        for slug in [
132            "qwen/qwen-2.5-coder-32b-instruct",
133            "deepseek/deepseek-chat",
134            "google/gemma-2-27b-it",
135            "openai/gpt-4o-mini",
136            "anthropic/claude-sonnet-4-5",
137        ] {
138            assert_eq!(
139                provider_for(slug).name(),
140                "openrouter",
141                "slug {slug} should route to OpenRouter"
142            );
143        }
144    }
145
146    /// The OpenRouter default carries the slug through to `supports_native_tools`.
147    ///
148    /// Why: The factory must build the OpenRouter provider with the slug so its
149    /// native-tool decision is correct, not a blank default.
150    /// What: Resolve a Claude slug and a Qwen slug; assert their native-tool
151    /// flags differ as expected.
152    /// Test: this test.
153    #[test]
154    fn provider_for_preserves_slug_for_native_tool_decision() {
155        assert!(provider_for("anthropic/claude-sonnet-4-5").supports_native_tools());
156        assert!(!provider_for("qwen/qwen-2.5-coder-32b-instruct").supports_native_tools());
157    }
158}