use std::collections::BTreeMap;
use rig_core::message::{
AssistantContent, CallId, ImageMediaType, Issuer, Message, Reasoning, ToolName,
ToolResultContent, UserContent,
};
use crate::{
claude::{
command, shown,
wire::{
AssistantError, Block, ClaudeMessageId, ClaudeToolUseId, Ending, LimitStatus,
RateLimit, ResultBlock, ResultContent, Usage,
},
},
error::{Code, Failure},
};
const MAX_SAFE_JSON_INTEGER: u64 = (1 << 53) - 1;
const ISSUER: Issuer = Issuer::from_static("claude-agent");
pub fn tool_name(name: &str) -> &str {
name.strip_prefix(&command::tool_name("")).unwrap_or(name)
}
pub fn assistant(id: &ClaudeMessageId, blocks: &[Block]) -> Option<Message> {
let content: Vec<AssistantContent> = blocks
.iter()
.filter_map(|block| match block {
Block::Text { text } => Some(AssistantContent::text(text.clone())),
Block::Thinking {
thinking,
signature,
} => Some(AssistantContent::Reasoning(
Reasoning::new_with_signature(
thinking,
Some(signature.clone()).filter(|signature| !signature.is_empty()),
)
.sealed(ISSUER),
)),
Block::ToolUse { id, name, input } => Some(AssistantContent::tool_call(
id.as_str(),
ToolName::new(tool_name(name)).ok()?,
input.clone(),
)),
Block::Other => None,
})
.collect();
(!content.is_empty()).then(|| Message::Assistant {
id: Some(id.to_string()),
content,
})
}
pub fn tool_result(
id: &ClaudeToolUseId,
name: &str,
content: ResultContent,
) -> Option<UserContent> {
let content = match content {
ResultContent::Text(text) => vec![ToolResultContent::text(text)],
ResultContent::Blocks(blocks) => blocks
.into_iter()
.filter_map(|block| match block {
ResultBlock::Text { text } => Some(ToolResultContent::text(text)),
ResultBlock::Image { source } => Some(ToolResultContent::image_base64(
source.data,
Some(match source.media_type.as_str() {
"image/png" => ImageMediaType::PNG,
"image/jpeg" => ImageMediaType::JPEG,
"image/webp" => ImageMediaType::WEBP,
"image/gif" => ImageMediaType::GIF,
_ => return None,
}),
None,
)),
ResultBlock::Other => None,
})
.collect(),
};
Some(UserContent::tool_result(
CallId::from_wire(id.as_str()),
ToolName::new(tool_name(name)).ok()?,
content,
))
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Counters {
input: Option<u64>,
output: Option<u64>,
cache_read: Option<u64>,
cache_write: Option<u64>,
cache_write_1h: Option<u64>,
reasoning: Option<u64>,
}
impl Counters {
pub fn merge(&mut self, usage: &Usage) {
let highest = |kept: &mut Option<u64>, reported: Option<u64>| {
*kept = (*kept).max(reported);
};
highest(&mut self.input, usage.input_tokens);
highest(&mut self.output, usage.output_tokens);
highest(&mut self.cache_read, usage.cache_read_input_tokens);
highest(&mut self.cache_write, usage.cache_creation_input_tokens);
highest(
&mut self.cache_write_1h,
usage
.cache_creation
.and_then(|cache| cache.ephemeral_1h_input_tokens),
);
highest(
&mut self.reasoning,
usage
.output_tokens_details
.and_then(|details| details.thinking_tokens),
);
}
fn input_total(&self) -> Option<u64> {
self.input.map(|uncached| {
uncached
.saturating_add(self.cache_read.unwrap_or(0))
.saturating_add(self.cache_write.unwrap_or(0))
})
}
pub fn total(&self) -> u64 {
self.input_total()
.unwrap_or(0)
.saturating_add(self.output.unwrap_or(0))
}
pub fn usage(&self) -> BTreeMap<String, u64> {
[
("inputTokens", self.input_total()),
("outputTokens", self.output),
("totalTokens", self.input.map(|_| self.total())),
("cacheReadTokens", self.cache_read),
("cacheWriteTokens", self.cache_write),
("cacheWrite1hTokens", self.cache_write_1h),
("reasoningTokens", self.reasoning),
]
.into_iter()
.filter_map(|(name, value)| {
value
.filter(|value| *value <= MAX_SAFE_JSON_INTEGER)
.map(|value| (name.to_owned(), value))
})
.collect()
}
}
pub fn assistant_failure(error: AssistantError, text: &str, limit: Option<&RateLimit>) -> Failure {
match error {
AssistantError::AuthenticationFailed => Failure::new(
Code::Authentication,
"Claude Code is not signed in, or its login expired; run `claude auth login`.",
),
AssistantError::BillingError => Failure::new(
Code::Quota,
"Claude Code reports a billing problem; check the Claude plan of the account `claude auth status` shows.",
),
AssistantError::RateLimit => limit.map_or_else(
|| Failure::new(Code::Quota, "Claude Code subscription limit reached."),
limit_failure,
),
AssistantError::ServerError => Failure::new(
Code::Transient,
format!("Claude Code's service failed: {}", shown(text)),
),
AssistantError::InvalidRequest | AssistantError::Unknown => Failure::new(
Code::ProviderError,
format!("Claude Code could not answer: {}", shown(text)),
),
}
}
pub fn limit_failure(limit: &RateLimit) -> Failure {
let window = match limit.rate_limit_type.as_deref() {
Some("five_hour") => "the 5-hour window".to_owned(),
Some("seven_day") => "the weekly window".to_owned(),
Some(other) => format!("the {} window", other.replace('_', " ")),
None => "its window".to_owned(),
};
let reset = limit
.resets_at
.and_then(|at| {
at.format(&time::format_description::well_known::Rfc3339)
.ok()
})
.map_or_else(String::new, |at| format!("; it resets at {at}"));
Failure::new(
Code::Quota,
format!("Claude Code subscription limit reached in {window}{reset}."),
)
}
pub fn reached(limit: &RateLimit) -> bool {
limit.status == LimitStatus::Rejected
}
pub fn finish_failure(ending: &Ending, errors: &[String]) -> Failure {
let detail = shown(&errors.join(" "));
let subtype = match ending {
Ending::NoStructuredOutput => {
return Failure::new(
Code::InvalidResult,
format!("Claude Code gave no answer matching the verdict schema. {detail}"),
);
}
Ending::Other(subtype) => subtype,
};
Failure::new(
Code::ProviderError,
format!("Claude Code ended the turn with {subtype}. {detail}")
.trim_end()
.to_owned(),
)
}
#[cfg(test)]
mod tests;