agent-sdk-provider 0.1.0-alpha.3

Optional provider adapter helpers layered over agent-sdk-core ProviderAdapter contracts.
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
use std::{fmt, sync::Arc};

use agent_sdk_core::{
    AgentError, ProviderAdapter, ProviderCapabilities, ProviderMessageRole,
    ProviderProjectionPolicy, ProviderRequest, ProviderResponse, ProviderStopReason,
    ProviderToolCall, ProviderUsage, RetryClassification, ToolCallId,
    tool_records::CanonicalToolName,
};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use crate::{
    ProviderApiKey, ProviderToolArgumentSink,
    error::{provider_failure, unsupported_response},
    http::{CurlJsonHttpTransport, JsonHttpRequest, JsonHttpTransport},
};

#[derive(Clone, Debug, Eq, PartialEq)]
/// Configuration for the live Anthropic Messages adapter.
pub struct AnthropicMessagesConfig {
    /// Stable provider ref exposed through `ProviderCapabilities`.
    pub provider_ref: String,
    /// Anthropic model id.
    pub model: String,
    /// Absolute Messages API endpoint.
    pub endpoint_url: String,
    /// Anthropic API version header value.
    pub api_version: String,
    /// Maximum output tokens for one request.
    pub max_tokens: u32,
    /// Maximum input tokens advertised by this route.
    pub max_input_tokens: Option<u32>,
}

impl AnthropicMessagesConfig {
    /// Creates a config for Anthropic's hosted Messages API.
    pub fn new(model: impl Into<String>) -> Self {
        Self {
            provider_ref: "provider.anthropic.messages".to_string(),
            model: model.into(),
            endpoint_url: "https://api.anthropic.com/v1/messages".to_string(),
            api_version: "2023-06-01".to_string(),
            max_tokens: 1024,
            max_input_tokens: None,
        }
    }

    /// Sets the stable provider ref used in SDK capability metadata.
    pub fn provider_ref(mut self, provider_ref: impl Into<String>) -> Self {
        self.provider_ref = provider_ref.into();
        self
    }

    /// Sets a custom endpoint URL.
    pub fn endpoint_url(mut self, endpoint_url: impl Into<String>) -> Self {
        self.endpoint_url = endpoint_url.into();
        self
    }

    /// Sets the Anthropic API version header.
    pub fn api_version(mut self, api_version: impl Into<String>) -> Self {
        self.api_version = api_version.into();
        self
    }

    /// Sets the maximum output token budget.
    pub fn max_tokens(mut self, max_tokens: u32) -> Self {
        self.max_tokens = max_tokens;
        self
    }

    /// Sets the maximum input token limit advertised for this route.
    pub fn max_input_tokens(mut self, max_input_tokens: u32) -> Self {
        self.max_input_tokens = Some(max_input_tokens);
        self
    }
}

#[derive(Clone)]
/// Live Anthropic Messages API adapter.
pub struct AnthropicMessagesAdapter {
    config: AnthropicMessagesConfig,
    api_key: ProviderApiKey,
    http: Arc<dyn JsonHttpTransport>,
    argument_sink: Option<Arc<dyn ProviderToolArgumentSink>>,
}

impl AnthropicMessagesAdapter {
    /// Creates a live adapter using `ANTHROPIC_API_KEY`.
    pub fn from_env(model: impl Into<String>) -> Result<Self, AgentError> {
        Self::new(
            AnthropicMessagesConfig::new(model),
            ProviderApiKey::from_env("ANTHROPIC_API_KEY")?,
        )
    }

    /// Creates a live adapter with a host-resolved API key.
    pub fn new(
        config: AnthropicMessagesConfig,
        api_key: ProviderApiKey,
    ) -> Result<Self, AgentError> {
        Self::with_transport(config, api_key, Arc::new(CurlJsonHttpTransport::new()))
    }

    /// Creates an adapter with an injected JSON transport.
    pub fn with_transport(
        config: AnthropicMessagesConfig,
        api_key: ProviderApiKey,
        http: Arc<dyn JsonHttpTransport>,
    ) -> Result<Self, AgentError> {
        Ok(Self {
            config,
            api_key,
            http,
            argument_sink: None,
        })
    }

    /// Adds an optional host-owned sink for raw tool-call arguments.
    pub fn with_argument_sink(mut self, sink: Arc<dyn ProviderToolArgumentSink>) -> Self {
        self.argument_sink = Some(sink);
        self
    }

    fn wire_request(&self, request: &ProviderRequest) -> Value {
        let mut system = Vec::new();
        let mut messages = Vec::new();
        for message in &request.messages {
            match message.role {
                ProviderMessageRole::System | ProviderMessageRole::Developer => {
                    system.push(message.content.clone());
                }
                ProviderMessageRole::Assistant => {
                    messages.push(json!({"role": "assistant", "content": message.content}));
                }
                ProviderMessageRole::Tool => {
                    messages.push(json!({
                        "role": "user",
                        "content": format!("Tool result:\n{}", message.content),
                    }));
                }
                ProviderMessageRole::Context | ProviderMessageRole::User => {
                    messages.push(json!({"role": "user", "content": message.content}));
                }
            }
        }

        let mut body = json!({
            "model": self.config.model.clone(),
            "max_tokens": self.config.max_tokens,
            "messages": messages,
        });
        if !system.is_empty() {
            body["system"] = Value::String(system.join("\n\n"));
        }
        if let Some(output_config) = anthropic_output_config(request) {
            body["output_config"] = output_config;
        }
        body
    }

    fn map_response(
        &self,
        response: AnthropicMessagesResponse,
    ) -> Result<ProviderResponse, AgentError> {
        let tool_calls = self.tool_calls_from_response(&response)?;
        let usage = response.usage.clone().map(ProviderUsage::from);
        if !tool_calls.is_empty() {
            let mut mapped = ProviderResponse::tool_use(tool_calls);
            mapped.usage = usage;
            return Ok(mapped);
        }

        Ok(ProviderResponse {
            schema_version: ProviderResponse::SCHEMA_VERSION,
            output_text: response.output_text(),
            stop_reason: response.stop_reason(),
            tool_calls: Vec::new(),
            usage,
        })
    }

    fn tool_calls_from_response(
        &self,
        response: &AnthropicMessagesResponse,
    ) -> Result<Vec<ProviderToolCall>, AgentError> {
        let mut calls = Vec::new();
        for item in &response.content {
            if item.kind != "tool_use" {
                continue;
            }
            let call_id = item.id.as_deref().ok_or_else(|| {
                unsupported_response("Anthropic Messages", "tool_use block missing id")
            })?;
            let name = item.name.as_deref().ok_or_else(|| {
                unsupported_response("Anthropic Messages", "tool_use block missing name")
            })?;
            let canonical_tool_name = CanonicalToolName::new(name);
            let mut call = ProviderToolCall::new(
                ToolCallId::new(call_id),
                canonical_tool_name.clone(),
                format!("provider requested tool {name} with arguments stored as content refs"),
            );
            if let (Some(sink), Some(input)) = (self.argument_sink.as_ref(), item.input.as_ref()) {
                let raw_arguments = serde_json::to_string(input).map_err(|error| {
                    provider_failure(
                        RetryClassification::RepairNeeded,
                        format!("Anthropic tool input could not be serialized: {error}"),
                    )
                })?;
                if let Some(args_ref) = sink.store_tool_arguments(
                    &self.config.provider_ref,
                    call_id,
                    &canonical_tool_name,
                    &raw_arguments,
                )? {
                    call = call.with_args_ref(args_ref);
                }
            }
            calls.push(call);
        }
        Ok(calls)
    }
}

impl ProviderAdapter for AnthropicMessagesAdapter {
    fn capabilities(&self) -> ProviderCapabilities {
        let mut capabilities = ProviderCapabilities::text_only(self.config.provider_ref.clone());
        capabilities.supports_usage = true;
        capabilities.max_input_tokens = self.config.max_input_tokens;
        capabilities
    }

    fn project_request(
        &self,
        projection: &agent_sdk_core::ContextProjection,
        policy: &ProviderProjectionPolicy,
    ) -> Result<ProviderRequest, AgentError> {
        agent_sdk_core::projection::project_context_projection(projection, policy)
    }

    fn complete(&self, request: &ProviderRequest) -> Result<ProviderResponse, AgentError> {
        let http_request =
            JsonHttpRequest::new(self.config.endpoint_url.clone(), self.wire_request(request))
                .header("x-api-key", self.api_key.expose_secret())
                .header("anthropic-version", self.config.api_version.clone())
                .header("Content-Type", "application/json");
        let response = self.http.post_json(http_request)?;
        let message = serde_json::from_value::<AnthropicMessagesResponse>(response.body)
            .map_err(|error| unsupported_response("Anthropic Messages", error.to_string()))?;
        self.map_response(message)
    }
}

fn anthropic_output_config(request: &ProviderRequest) -> Option<Value> {
    let hint = request.structured_output_hint.as_ref()?;
    let schema = hint.redacted_schema.clone()?;
    Some(json!({
        "format": {
            "type": "json_schema",
            "schema": schema,
        }
    }))
}

#[derive(Clone, Deserialize, Eq, PartialEq, Serialize)]
/// Minimal Anthropic Messages response shape used by the adapter.
pub struct AnthropicMessagesResponse {
    /// Provider response id.
    pub id: Option<String>,
    /// Content blocks returned by Claude.
    #[serde(default)]
    pub content: Vec<AnthropicContentBlock>,
    /// Provider stop reason.
    pub stop_reason: Option<String>,
    /// Provider usage accounting.
    pub usage: Option<AnthropicUsage>,
}

impl AnthropicMessagesResponse {
    /// Creates a text response fixture.
    pub fn text(text: impl Into<String>) -> Self {
        Self {
            id: Some("msg_test".to_string()),
            content: vec![AnthropicContentBlock::text(text)],
            stop_reason: Some("end_turn".to_string()),
            usage: None,
        }
    }

    /// Creates a tool-use response fixture.
    pub fn tool_use(id: impl Into<String>, name: impl Into<String>, input: Value) -> Self {
        Self {
            id: Some("msg_tool".to_string()),
            content: vec![AnthropicContentBlock::tool_use(id, name, input)],
            stop_reason: Some("tool_use".to_string()),
            usage: None,
        }
    }

    fn output_text(&self) -> String {
        self.content
            .iter()
            .filter(|item| item.kind == "text")
            .filter_map(|item| item.text.as_deref())
            .collect::<Vec<_>>()
            .join("")
    }

    fn stop_reason(&self) -> ProviderStopReason {
        match self.stop_reason.as_deref().unwrap_or("end_turn") {
            "end_turn" => ProviderStopReason::EndTurn,
            "max_tokens" => ProviderStopReason::MaxTokens,
            "tool_use" => ProviderStopReason::ToolUse,
            "stop_sequence" => ProviderStopReason::EndTurn,
            _ => ProviderStopReason::Unknown,
        }
    }
}

impl fmt::Debug for AnthropicMessagesResponse {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("AnthropicMessagesResponse")
            .field("id", &self.id)
            .field("content_count", &self.content.len())
            .field("content", &self.content)
            .field("stop_reason", &self.stop_reason)
            .field("usage", &self.usage)
            .finish()
    }
}

#[derive(Clone, Deserialize, Eq, PartialEq, Serialize)]
/// Anthropic content block.
pub struct AnthropicContentBlock {
    #[serde(rename = "type")]
    /// Content block type.
    pub kind: String,
    /// Text content for text blocks.
    pub text: Option<String>,
    /// Tool-use id.
    pub id: Option<String>,
    /// Tool name.
    pub name: Option<String>,
    /// Tool input arguments.
    pub input: Option<Value>,
}

impl AnthropicContentBlock {
    /// Creates a text block.
    pub fn text(text: impl Into<String>) -> Self {
        Self {
            kind: "text".to_string(),
            text: Some(text.into()),
            id: None,
            name: None,
            input: None,
        }
    }

    /// Creates a tool-use block.
    pub fn tool_use(id: impl Into<String>, name: impl Into<String>, input: Value) -> Self {
        Self {
            kind: "tool_use".to_string(),
            text: None,
            id: Some(id.into()),
            name: Some(name.into()),
            input: Some(input),
        }
    }
}

impl fmt::Debug for AnthropicContentBlock {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("AnthropicContentBlock")
            .field("kind", &self.kind)
            .field(
                "text_chars",
                &self.text.as_ref().map(|value| value.chars().count()),
            )
            .field("id", &self.id)
            .field("name", &self.name)
            .field("input", &"<redacted>")
            .field("input_present", &self.input.is_some())
            .finish()
    }
}

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
/// Anthropic usage accounting.
pub struct AnthropicUsage {
    /// Provider input tokens.
    pub input_tokens: Option<u32>,
    /// Provider output tokens.
    pub output_tokens: Option<u32>,
}

impl From<AnthropicUsage> for ProviderUsage {
    fn from(value: AnthropicUsage) -> Self {
        let total_tokens = match (value.input_tokens, value.output_tokens) {
            (Some(input), Some(output)) => Some(input + output),
            _ => None,
        };
        Self {
            input_tokens: value.input_tokens,
            output_tokens: value.output_tokens,
            total_tokens,
        }
    }
}