use axum::http::StatusCode;
use axum::response::{IntoResponse, Json, Response};
use crate::tool_parser;
use super::types::*;
pub(super) fn anthropic_error(status: StatusCode, error_type: &str, message: String) -> Response {
let body = serde_json::json!({
"type": "error",
"error": {
"type": error_type,
"message": message,
}
});
(status, Json(body)).into_response()
}
impl From<&AnthropicTool> for tool_parser::ToolDefinition {
fn from(t: &AnthropicTool) -> Self {
tool_parser::ToolDefinition {
tool_type: "function".to_string(),
function: tool_parser::FunctionDefinition {
name: t.name.clone(),
description: t.description.clone(),
parameters: Some(t.input_schema.clone()),
},
}
}
}
impl From<&AnthropicToolChoice> for tool_parser::ToolChoice {
fn from(tc: &AnthropicToolChoice) -> Self {
match tc.choice_type.as_str() {
"any" => tool_parser::ToolChoice::Mode("required".to_string()),
"auto" => tool_parser::ToolChoice::Mode("auto".to_string()),
"none" => tool_parser::ToolChoice::Mode("none".to_string()),
"tool" => {
if let Some(ref name) = tc.name {
tool_parser::ToolChoice::Specific {
function: tool_parser::ToolChoiceFunction { name: name.clone() },
}
} else {
tool_parser::ToolChoice::Mode("auto".to_string())
}
}
_ => tool_parser::ToolChoice::Mode("auto".to_string()),
}
}
}
pub(super) fn convert_stop_reason(finish_reason: &str) -> &'static str {
match finish_reason {
"stop" => "end_turn",
"tool_calls" => "tool_use",
"length" => "max_tokens",
"content_filter" => "refusal",
crate::ir::FINISH_REASON_TIMEOUT => "max_tokens",
_ => "end_turn",
}
}