adk_model/anthropic/models.rs
1//! Model discovery API for Anthropic.
2//!
3//! Provides [`ModelInfo`] and the `list_models` / `get_model` methods on
4//! [`AnthropicClient`], wrapping the `GET /v1/models` and
5//! `GET /v1/models/{model_id}` endpoints.
6
7use super::client::{AnthropicClient, convert_anthropic_error};
8use adk_core::AdkError;
9use serde::{Deserialize, Serialize};
10
11/// Information about an Anthropic model.
12///
13/// Contains the model identifier, human-readable display name, and creation
14/// timestamp as returned by the Anthropic Models API.
15///
16/// # Example
17///
18/// ```rust,ignore
19/// use adk_model::anthropic::{AnthropicClient, AnthropicConfig};
20///
21/// let client = AnthropicClient::new(AnthropicConfig::new("sk-ant-xxx", "claude-sonnet-4-5-20250929"))?;
22/// let models = client.list_models().await?;
23/// for model in &models {
24/// println!("{}: {} (created {})", model.id, model.display_name, model.created_at);
25/// }
26/// ```
27#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
28pub struct ModelInfo {
29 /// Unique model identifier (e.g., "claude-sonnet-4-5-20250929").
30 pub id: String,
31 /// Human-readable name for the model.
32 pub display_name: String,
33 /// RFC 3339 datetime string of when the model was created.
34 pub created_at: String,
35}
36
37impl From<adk_anthropic::ModelInfo> for ModelInfo {
38 fn from(m: adk_anthropic::ModelInfo) -> Self {
39 // adk_anthropic::ModelInfo.created_at is a time::OffsetDateTime serialized
40 // with RFC 3339 via serde. We round-trip through serde_json to get the
41 // RFC 3339 string without importing the `time` crate directly.
42 let created_at = serde_json::to_value(&m)
43 .ok()
44 .and_then(|v| v.get("created_at")?.as_str().map(String::from))
45 .unwrap_or_default();
46
47 Self { id: m.id, display_name: m.display_name, created_at }
48 }
49}
50
51impl AnthropicClient {
52 /// List available Claude models.
53 ///
54 /// Calls `GET /v1/models` and returns all model descriptors from the
55 /// first page. For paginated access, use `list_models_paginated`.
56 ///
57 /// # Errors
58 ///
59 /// Returns `AdkError::Model` with structured error context if the
60 /// API returns an error.
61 pub async fn list_models(&self) -> Result<Vec<ModelInfo>, AdkError> {
62 let response = self.client.list_models(None).await.map_err(convert_anthropic_error)?;
63
64 Ok(response.data.into_iter().map(ModelInfo::from).collect())
65 }
66
67 /// Get details for a specific model.
68 ///
69 /// Calls `GET /v1/models/{model_id}` and returns the model descriptor.
70 ///
71 /// # Errors
72 ///
73 /// Returns `AdkError::Model` with structured error context if the
74 /// API returns an error (e.g., model not found).
75 pub async fn get_model(&self, model_id: &str) -> Result<ModelInfo, AdkError> {
76 let info = self.client.get_model(model_id).await.map_err(convert_anthropic_error)?;
77
78 Ok(ModelInfo::from(info))
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85
86 #[test]
87 fn test_model_info_from_adk_anthropic() {
88 // Build an adk_anthropic::ModelInfo via serde round-trip to avoid
89 // importing the `time` crate directly.
90 let json = serde_json::json!({
91 "id": "claude-sonnet-4-5-20250929",
92 "created_at": "2025-09-29T00:00:00Z",
93 "display_name": "Claude Sonnet 4.5",
94 "type": "model"
95 });
96 let model: adk_anthropic::ModelInfo = serde_json::from_value(json).unwrap();
97
98 let info = ModelInfo::from(model);
99 assert_eq!(info.id, "claude-sonnet-4-5-20250929");
100 assert_eq!(info.display_name, "Claude Sonnet 4.5");
101 assert_eq!(info.created_at, "2025-09-29T00:00:00Z");
102 }
103
104 #[test]
105 fn test_model_info_serialization_roundtrip() {
106 let info = ModelInfo {
107 id: "claude-3-opus-20240229".to_string(),
108 display_name: "Claude 3 Opus".to_string(),
109 created_at: "2024-02-29T00:00:00Z".to_string(),
110 };
111
112 let json = serde_json::to_string(&info).unwrap();
113 let deserialized: ModelInfo = serde_json::from_str(&json).unwrap();
114 assert_eq!(info, deserialized);
115 }
116}