use super::client::{AnthropicClient, convert_anthropic_error};
use adk_core::AdkError;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ModelInfo {
pub id: String,
pub display_name: String,
pub created_at: String,
}
impl From<adk_anthropic::ModelInfo> for ModelInfo {
fn from(m: adk_anthropic::ModelInfo) -> Self {
let created_at = serde_json::to_value(&m)
.ok()
.and_then(|v| v.get("created_at")?.as_str().map(String::from))
.unwrap_or_default();
Self { id: m.id, display_name: m.display_name, created_at }
}
}
impl AnthropicClient {
pub async fn list_models(&self) -> Result<Vec<ModelInfo>, AdkError> {
let response = self.client.list_models(None).await.map_err(convert_anthropic_error)?;
Ok(response.data.into_iter().map(ModelInfo::from).collect())
}
pub async fn get_model(&self, model_id: &str) -> Result<ModelInfo, AdkError> {
let info = self.client.get_model(model_id).await.map_err(convert_anthropic_error)?;
Ok(ModelInfo::from(info))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_model_info_from_adk_anthropic() {
let json = serde_json::json!({
"id": "claude-sonnet-4-5-20250929",
"created_at": "2025-09-29T00:00:00Z",
"display_name": "Claude Sonnet 4.5",
"type": "model"
});
let model: adk_anthropic::ModelInfo = serde_json::from_value(json).unwrap();
let info = ModelInfo::from(model);
assert_eq!(info.id, "claude-sonnet-4-5-20250929");
assert_eq!(info.display_name, "Claude Sonnet 4.5");
assert_eq!(info.created_at, "2025-09-29T00:00:00Z");
}
#[test]
fn test_model_info_serialization_roundtrip() {
let info = ModelInfo {
id: "claude-3-opus-20240229".to_string(),
display_name: "Claude 3 Opus".to_string(),
created_at: "2024-02-29T00:00:00Z".to_string(),
};
let json = serde_json::to_string(&info).unwrap();
let deserialized: ModelInfo = serde_json::from_str(&json).unwrap();
assert_eq!(info, deserialized);
}
}