Skip to main content

trusty_code/provider/
atlascloud.rs

1//! AtlasCloud provider — request-shaping normalisation for `atlascloud/*` slugs
2//! (#2536, epic #2400).
3//!
4//! Why: enabling AtlasCloud in tcode (#2536) is not only a transport concern —
5//! `AgentLoop::build_request` consults `crate::provider::provider_for` to decide
6//! whether to request OpenRouter's detailed-usage directive, whether to attach
7//! prompt-cache breakpoints, and whether to send a native `tools` array. Routing
8//! an `atlascloud/*` slug to the default `OpenRouterProvider` would (wrongly)
9//! request `usage:{include:true}` — a directive AtlasCloud doesn't understand —
10//! so AtlasCloud needs its own normalisation profile, mirroring the Together
11//! precedent (#2494) and the shared capability registry's AtlasCloud seed
12//! (`detailed_usage_accounting = false`, no explicit `cache_control`
13//! breakpoints, native OpenAI-style function calling).
14//! What: [`AtlasCloudProvider`] implements [`Provider`] with the OpenAI-dialect
15//! `tool_choice` mapping (identical to OpenRouter/Fireworks/Together), the
16//! standard usage extraction, native tool support, NO explicit prompt-cache
17//! breakpoints, and NO detailed-usage request. The transport
18//! (`crate::llm::client::OpenAiCompatClient`) is what actually reaches
19//! `api.atlascloud.ai`; this type only shapes the request.
20//! Test: `atlascloud::tests::*`.
21
22use serde_json::{Value, json};
23
24use super::traits::{Provider, ToolChoice};
25use crate::llm::ChatResponse;
26use crate::perf::TokenUsage;
27
28/// Provider for AtlasCloud-hosted models (`atlascloud/*` slugs), served behind the
29/// OpenAI-compatible `/chat/completions` schema.
30///
31/// Why: gives `build_request` an AtlasCloud-correct normalisation profile so the
32/// wire payload matches what AtlasCloud accepts (no OpenRouter-only directives,
33/// no `cache_control` breakpoints).
34/// What: zero-sized marker type implementing [`Provider`].
35/// Test: `atlascloud::tests::*`.
36#[derive(Debug, Clone, Default)]
37pub struct AtlasCloudProvider;
38
39impl AtlasCloudProvider {
40    /// Construct an `AtlasCloudProvider`.
41    ///
42    /// Why: gives the factory a uniform constructor call site.
43    /// What: returns the zero-sized marker.
44    /// Test: `atlascloud::tests::name_is_atlascloud`.
45    pub fn new() -> Self {
46        Self
47    }
48}
49
50impl Provider for AtlasCloudProvider {
51    fn name(&self) -> &str {
52        "atlascloud"
53    }
54
55    /// Translate a neutral [`ToolChoice`] into OpenAI-dialect wire JSON.
56    ///
57    /// Why: AtlasCloud uses the OpenAI `tool_choice` spelling (string policies +
58    /// `{type:function, function:{name}}` object), identical to OpenRouter,
59    /// Fireworks, and Together.
60    /// What: the OpenAI mapping.
61    /// Test: `atlascloud::tests::map_tool_choice_scalars`.
62    fn map_tool_choice(&self, choice: ToolChoice) -> Value {
63        match choice {
64            ToolChoice::None => json!("none"),
65            ToolChoice::Auto => json!("auto"),
66            ToolChoice::Required => json!("required"),
67            ToolChoice::Function(name) => json!({
68                "type": "function",
69                "function": { "name": name },
70            }),
71        }
72    }
73
74    /// Extract canonical token usage from an AtlasCloud response.
75    ///
76    /// Why: AtlasCloud returns the standard OpenAI `usage` block, already
77    /// normalised into `ChatResponse.usage` by the shared adapter's parse.
78    /// What: delegates to `ChatResponse::token_usage` (identical to the other
79    /// OpenAI-dialect providers).
80    /// Test: `atlascloud::tests::extract_usage_maps_fields`.
81    fn extract_usage(&self, response: &ChatResponse) -> TokenUsage {
82        response.clone().token_usage()
83    }
84
85    fn supports_native_tools(&self) -> bool {
86        // AtlasCloud's tool-capable models accept the native OpenAI `tools`
87        // array; mirrors the shared registry's AtlasCloud `native_tool_calling =
88        // true`.
89        true
90    }
91
92    fn supports_prompt_caching(&self) -> bool {
93        // AtlasCloud caching is provider-side/implicit: the caller cannot place
94        // Anthropic-style `cache_control` breakpoints, so tcode must never mark
95        // an AtlasCloud request with one (matching the Together precedent — the
96        // wire body carries no `cache_control` markers).
97        false
98    }
99
100    fn wants_detailed_usage(&self) -> bool {
101        // `usage:{include:true}` is an OpenRouter-only directive; AtlasCloud would
102        // not understand it. Mirrors the shared registry's AtlasCloud
103        // `detailed_usage_accounting = false`.
104        false
105    }
106}
107
108// ── Tests ─────────────────────────────────────────────────────────────────────
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113
114    /// `name()` returns `"atlascloud"`.
115    ///
116    /// Why: routing and telemetry distinguish AtlasCloud by name; the transport's
117    /// `provider_for_slug` and `dispatch`'s bedrock check both key off it.
118    /// What: assert the name.
119    /// Test: this test.
120    #[test]
121    fn name_is_atlascloud() {
122        assert_eq!(AtlasCloudProvider::new().name(), "atlascloud");
123    }
124
125    /// AtlasCloud reports native-tool support.
126    ///
127    /// Why: `build_request` sends the native `tools` array for tool-capable
128    /// AtlasCloud models.
129    /// What: assert `supports_native_tools()` is `true`.
130    /// Test: this test.
131    #[test]
132    fn atlascloud_supports_native_tools() {
133        assert!(AtlasCloudProvider::new().supports_native_tools());
134    }
135
136    /// AtlasCloud does NOT request OpenRouter's detailed usage accounting.
137    ///
138    /// Why: this is the exact gate that would otherwise attach the
139    /// OpenRouter-only `usage:{include:true}` directive to an AtlasCloud request —
140    /// the core reason an AtlasCloud-specific provider is needed.
141    /// What: assert `wants_detailed_usage()` is `false`.
142    /// Test: this test.
143    #[test]
144    fn atlascloud_does_not_want_detailed_usage() {
145        assert!(!AtlasCloudProvider::new().wants_detailed_usage());
146    }
147
148    /// AtlasCloud does NOT place explicit prompt-cache breakpoints.
149    ///
150    /// Why: `build_request` must never mark an AtlasCloud request with an
151    /// Anthropic-style `cache_control` breakpoint — AtlasCloud caching is
152    /// provider-side and rejects caller-placed markers.
153    /// What: assert `supports_prompt_caching()` is `false`.
154    /// Test: this test.
155    #[test]
156    fn atlascloud_does_not_support_prompt_caching() {
157        assert!(!AtlasCloudProvider::new().supports_prompt_caching());
158    }
159
160    /// `map_tool_choice` maps the scalar policies to the OpenAI wire strings.
161    ///
162    /// Why: AtlasCloud expects the OpenAI spelling; a wrong shape breaks
163    /// tool routing.
164    /// What: map each scalar variant, assert the JSON value.
165    /// Test: this test.
166    #[test]
167    fn map_tool_choice_scalars() {
168        let p = AtlasCloudProvider::new();
169        assert_eq!(p.map_tool_choice(ToolChoice::None), json!("none"));
170        assert_eq!(p.map_tool_choice(ToolChoice::Auto), json!("auto"));
171        assert_eq!(p.map_tool_choice(ToolChoice::Required), json!("required"));
172        assert_eq!(
173            p.map_tool_choice(ToolChoice::Function("search".into())),
174            json!({"type": "function", "function": {"name": "search"}})
175        );
176    }
177
178    /// `extract_usage` maps a normalised usage block into `TokenUsage`.
179    ///
180    /// Why: pins that AtlasCloud performs no extra (inconsistent) usage transform.
181    /// What: deserialise a response fixture, extract usage, assert fields.
182    /// Test: this test.
183    #[test]
184    fn extract_usage_maps_fields() {
185        let fixture = r#"{
186          "id": "ac-1",
187          "choices": [{"message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}],
188          "usage": {"prompt_tokens": 40, "completion_tokens": 8, "total_tokens": 48}
189        }"#;
190        let resp: ChatResponse = serde_json::from_str(fixture).expect("deserialise");
191        let usage = AtlasCloudProvider::new().extract_usage(&resp);
192        assert_eq!(usage.prompt_tokens, 40);
193        assert_eq!(usage.completion_tokens, 8);
194    }
195}