Running the opt-in live suite (CM_LIVE_LLM=1 + ANTHROPIC_API_KEY) against
the real API immediately surfaced a launch blocker: Anthropic (and
OpenAI) restrict tool names to ^[a-zA-Z0-9_-]{1,128}$ — our ENTIRE
registry uses dotted names (clock.now, email.send, shell.exec, ...).
The scripted provider never enforced the pattern, so every real-model
deployment would have 400'd on the first tool call.
- Fix at the provider boundary, where it belongs: wire_tool_name /
internal_tool_name codec (dots <-> __) applied in BOTH HTTP providers
at all three sites (tools list, assistant tool_use echo, inbound
tool_use decode). Internal naming (DB step rows, scenarios, UI traces)
unchanged. Offline unit test round-trips every registry name through
the wire pattern
- New live tests, all passing against api.anthropic.com (Haiku 4.5):
- provider tool ROUND TRIP: real ToolUse arrives, ToolResult ships
back exactly as a checkpoint would reassemble it, model completes,
real usage events on the wire
- full runtime loop: real model calls clock.now, run completes, REAL
token usage metered, credits decremented
- the #1-risk validation: a real model's email.send intercepted ->
suspended -> approved -> checkpoint RESUMED against the live API ->
completed -> outbox exactly 1 (checkpoint/resume fidelity end to end)
- Stray TC_OPENAI_COMPAT_* envs renamed to CM_OPENAI_COMPAT_*
No credentials stored anywhere; the key was passed via env only.
163 Rust tests (+5 live, key-gated).
Co-Authored-By: Claude Fable 5 <[email protected]>
190 lines
7.4 KiB
Rust
190 lines
7.4 KiB
Rust
//! Anthropic Messages API provider (cloud target).
|
|
|
|
use async_stream::try_stream;
|
|
use eventsource_stream::Eventsource;
|
|
use futures::StreamExt;
|
|
use serde_json::{json, Value};
|
|
|
|
use crate::provider::{
|
|
ChatRequest, ChatRole, ContentPart, EventStream, LlmError, LlmEvent, LlmProvider, StopReason,
|
|
};
|
|
|
|
pub struct AnthropicProvider {
|
|
client: reqwest::Client,
|
|
base_url: String,
|
|
api_key: String,
|
|
}
|
|
|
|
impl AnthropicProvider {
|
|
pub fn new(api_key: String) -> AnthropicProvider {
|
|
AnthropicProvider::with_base_url(api_key, "https://api.anthropic.com".into())
|
|
}
|
|
|
|
/// Base URL override exists for self-hosted proxies and tests.
|
|
pub fn with_base_url(api_key: String, base_url: String) -> AnthropicProvider {
|
|
AnthropicProvider {
|
|
client: reqwest::Client::new(),
|
|
base_url,
|
|
api_key,
|
|
}
|
|
}
|
|
|
|
fn wire_messages(request: &ChatRequest) -> Vec<Value> {
|
|
request
|
|
.messages
|
|
.iter()
|
|
.map(|message| {
|
|
let role = match message.role {
|
|
ChatRole::User => "user",
|
|
ChatRole::Assistant => "assistant",
|
|
};
|
|
let content: Vec<Value> = message
|
|
.parts
|
|
.iter()
|
|
.map(|part| match part {
|
|
ContentPart::Text { text } => json!({"type": "text", "text": text}),
|
|
ContentPart::ToolUse { id, name, input } => {
|
|
json!({"type": "tool_use", "id": id,
|
|
"name": crate::wire_tool_name(name), "input": input})
|
|
}
|
|
ContentPart::ToolResult {
|
|
tool_use_id,
|
|
content,
|
|
} => json!({
|
|
"type": "tool_result",
|
|
"tool_use_id": tool_use_id,
|
|
"content": content.to_string(),
|
|
}),
|
|
})
|
|
.collect();
|
|
json!({"role": role, "content": content})
|
|
})
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
fn stop_reason(wire: &str) -> StopReason {
|
|
match wire {
|
|
"tool_use" => StopReason::ToolUse,
|
|
"max_tokens" => StopReason::MaxTokens,
|
|
_ => StopReason::EndTurn,
|
|
}
|
|
}
|
|
|
|
#[async_trait::async_trait]
|
|
impl LlmProvider for AnthropicProvider {
|
|
async fn stream(&self, request: ChatRequest) -> Result<EventStream, LlmError> {
|
|
let tools: Vec<Value> = request
|
|
.tools
|
|
.iter()
|
|
.map(|t| {
|
|
json!({
|
|
"name": crate::wire_tool_name(&t.name),
|
|
"description": t.description,
|
|
"input_schema": t.input_schema,
|
|
})
|
|
})
|
|
.collect();
|
|
let body = json!({
|
|
"model": request.model,
|
|
"max_tokens": request.max_tokens,
|
|
"system": request.system,
|
|
"messages": AnthropicProvider::wire_messages(&request),
|
|
"tools": tools,
|
|
"stream": true,
|
|
});
|
|
|
|
let response = self
|
|
.client
|
|
.post(format!("{}/v1/messages", self.base_url))
|
|
.header("x-api-key", &self.api_key)
|
|
.header("anthropic-version", "2023-06-01")
|
|
.json(&body)
|
|
.send()
|
|
.await
|
|
.map_err(|e| LlmError::Transport(e.to_string()))?;
|
|
if !response.status().is_success() {
|
|
let status = response.status();
|
|
let detail = response.text().await.unwrap_or_default();
|
|
return Err(LlmError::Api(format!("{status}: {detail}")));
|
|
}
|
|
|
|
let mut sse = response.bytes_stream().eventsource();
|
|
let stream = try_stream! {
|
|
// tool_use input arrives as accumulated partial JSON between
|
|
// content_block_start and content_block_stop.
|
|
let mut pending_tool: Option<(String, String, String)> = None; // id, name, json
|
|
let mut input_tokens: u32 = 0;
|
|
while let Some(event) = sse.next().await {
|
|
let event = event.map_err(|e| LlmError::Transport(e.to_string()))?;
|
|
let data: Value = serde_json::from_str(&event.data)
|
|
.map_err(|e| LlmError::Wire(format!("{e}: {}", event.data)))?;
|
|
match data["type"].as_str().unwrap_or_default() {
|
|
"content_block_start" => {
|
|
let block = &data["content_block"];
|
|
if block["type"] == "tool_use" {
|
|
pending_tool = Some((
|
|
block["id"].as_str().unwrap_or_default().to_owned(),
|
|
crate::internal_tool_name(
|
|
block["name"].as_str().unwrap_or_default(),
|
|
),
|
|
String::new(),
|
|
));
|
|
}
|
|
}
|
|
"content_block_delta" => {
|
|
let delta = &data["delta"];
|
|
match delta["type"].as_str().unwrap_or_default() {
|
|
"text_delta" => {
|
|
if let Some(text) = delta["text"].as_str() {
|
|
yield LlmEvent::TextDelta(text.to_owned());
|
|
}
|
|
}
|
|
"input_json_delta" => {
|
|
if let Some((_, _, buf)) = pending_tool.as_mut() {
|
|
buf.push_str(delta["partial_json"].as_str().unwrap_or_default());
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
"content_block_stop" => {
|
|
if let Some((id, name, buf)) = pending_tool.take() {
|
|
let input: Value = if buf.is_empty() {
|
|
json!({})
|
|
} else {
|
|
serde_json::from_str(&buf)
|
|
.map_err(|e| LlmError::Wire(format!("tool input: {e}")))?
|
|
};
|
|
yield LlmEvent::ToolUse { id, name, input };
|
|
}
|
|
}
|
|
"message_start" => {
|
|
input_tokens =
|
|
data["message"]["usage"]["input_tokens"].as_u64().unwrap_or(0) as u32;
|
|
}
|
|
"message_delta" => {
|
|
if let Some(out) = data["usage"]["output_tokens"].as_u64() {
|
|
yield LlmEvent::Usage {
|
|
input_tokens,
|
|
output_tokens: out as u32,
|
|
};
|
|
}
|
|
if let Some(reason) = data["delta"]["stop_reason"].as_str() {
|
|
yield LlmEvent::Stop(stop_reason(reason));
|
|
}
|
|
}
|
|
"error" => {
|
|
Err(LlmError::Api(data["error"]["message"]
|
|
.as_str()
|
|
.unwrap_or("unknown")
|
|
.to_owned()))?;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
};
|
|
Ok(Box::pin(stream))
|
|
}
|
|
}
|