Skip to main content

zai_rs/agent/
response.rs

1//! Agent API response types
2
3use serde::{Deserialize, Serialize};
4
5use super::request::AgentConfig;
6
7/// Response from creating a new agent
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct AgentCreateResponse {
10    /// The created agent ID
11    pub id: String,
12
13    /// Agent name
14    pub name: String,
15
16    /// Agent description
17    pub description: Option<String>,
18
19    /// Creation timestamp
20    pub created_at: Option<u64>,
21
22    /// Agent model
23    pub model: Option<String>,
24}
25
26/// Detailed agent information
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct AgentDetails {
29    /// Agent ID
30    pub id: String,
31
32    /// Agent name
33    pub name: String,
34
35    /// Agent description
36    pub description: Option<String>,
37
38    /// System prompt
39    pub system_prompt: Option<String>,
40
41    /// Model used
42    pub model: Option<String>,
43
44    /// Available tools
45    pub tools: Option<Vec<serde_json::Value>>,
46
47    /// Agent configuration
48    pub config: Option<AgentConfig>,
49
50    /// Creation timestamp
51    pub created_at: Option<u64>,
52
53    /// Last update timestamp
54    pub updated_at: Option<u64>,
55}
56
57/// Response from updating an agent
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct AgentUpdateResponse {
60    /// Agent ID
61    pub id: String,
62
63    /// Success status
64    pub success: bool,
65
66    /// Update timestamp
67    pub updated_at: Option<u64>,
68}
69
70/// Response from deleting an agent
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct AgentDeleteResponse {
73    /// Deleted agent ID
74    pub id: String,
75
76    /// Success status
77    pub success: bool,
78
79    /// Deletion timestamp
80    pub deleted_at: Option<u64>,
81}
82
83/// Response from agent chat
84#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct AgentChatResponse {
86    /// Conversation ID
87    pub conversation_id: Option<String>,
88
89    /// Session ID
90    pub session_id: Option<String>,
91
92    /// Agent response
93    pub response: AgentMessage,
94
95    /// Tool calls made by the agent
96    pub tool_calls: Option<Vec<AgentToolCall>>,
97
98    /// Usage statistics
99    pub usage: Option<AgentUsage>,
100}
101
102/// Agent message
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct AgentMessage {
105    /// Message role (assistant/user)
106    pub role: String,
107
108    /// Message content
109    pub content: String,
110
111    /// Reasoning content (for thinking mode)
112    pub reasoning_content: Option<String>,
113
114    /// Timestamp
115    pub timestamp: Option<u64>,
116}
117
118/// Tool call made by agent
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct AgentToolCall {
121    /// Tool call ID
122    pub id: String,
123
124    /// Tool name
125    pub name: String,
126
127    /// Tool arguments (JSON string)
128    pub arguments: String,
129
130    /// Tool result
131    pub result: Option<serde_json::Value>,
132}
133
134/// Token usage statistics
135#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct AgentUsage {
137    /// Input tokens
138    pub prompt_tokens: Option<u32>,
139
140    /// Output tokens
141    pub completion_tokens: Option<u32>,
142
143    /// Total tokens
144    pub total_tokens: Option<u32>,
145}
146
147/// Conversation history
148#[derive(Debug, Clone, Serialize, Deserialize)]
149pub struct ConversationHistory {
150    /// Conversation ID
151    pub conversation_id: String,
152
153    /// Messages in the conversation
154    pub messages: Vec<AgentMessage>,
155
156    /// Total number of messages
157    pub total_count: Option<u32>,
158
159    /// Whether there are more messages
160    pub has_more: Option<bool>,
161}
162
163#[cfg(test)]
164mod tests {
165    use super::*;
166
167    #[test]
168    fn test_agent_create_response_serde_roundtrip() {
169        let resp = AgentCreateResponse {
170            id: "agent_123".to_string(),
171            name: "TestAgent".to_string(),
172            description: Some("A test agent".to_string()),
173            created_at: Some(1700000000),
174            model: Some("glm-4.5".to_string()),
175        };
176        let json = serde_json::to_string(&resp).unwrap();
177        let parsed: AgentCreateResponse = serde_json::from_str(&json).unwrap();
178        assert_eq!(parsed.id, "agent_123");
179        assert_eq!(parsed.name, "TestAgent");
180        assert_eq!(parsed.description, Some("A test agent".to_string()));
181    }
182
183    #[test]
184    fn test_agent_details_serde_roundtrip() {
185        let details = AgentDetails {
186            id: "agent_456".to_string(),
187            name: "DetailAgent".to_string(),
188            description: None,
189            system_prompt: Some("Be helpful".to_string()),
190            model: Some("glm-4.5-flash".to_string()),
191            tools: None,
192            config: None,
193            created_at: Some(1700000000),
194            updated_at: Some(1700000100),
195        };
196        let json = serde_json::to_string(&details).unwrap();
197        let parsed: AgentDetails = serde_json::from_str(&json).unwrap();
198        assert_eq!(parsed.id, "agent_456");
199        assert!(parsed.description.is_none());
200    }
201
202    #[test]
203    fn test_agent_chat_response_serde_roundtrip() {
204        let resp = AgentChatResponse {
205            conversation_id: Some("conv_789".to_string()),
206            session_id: Some("sess_012".to_string()),
207            response: AgentMessage {
208                role: "assistant".to_string(),
209                content: "Hello!".to_string(),
210                reasoning_content: None,
211                timestamp: Some(1700000000),
212            },
213            tool_calls: None,
214            usage: Some(AgentUsage {
215                prompt_tokens: Some(10),
216                completion_tokens: Some(5),
217                total_tokens: Some(15),
218            }),
219        };
220        let json = serde_json::to_string(&resp).unwrap();
221        let parsed: AgentChatResponse = serde_json::from_str(&json).unwrap();
222        assert_eq!(parsed.response.content, "Hello!");
223        assert_eq!(parsed.usage.unwrap().total_tokens, Some(15));
224    }
225
226    #[test]
227    fn test_agent_update_response_serde_roundtrip() {
228        let resp = AgentUpdateResponse {
229            id: "agent_123".to_string(),
230            success: true,
231            updated_at: Some(1700000200),
232        };
233        let json = serde_json::to_string(&resp).unwrap();
234        let parsed: AgentUpdateResponse = serde_json::from_str(&json).unwrap();
235        assert!(parsed.success);
236    }
237
238    #[test]
239    fn test_agent_delete_response_serde_roundtrip() {
240        let resp = AgentDeleteResponse {
241            id: "agent_123".to_string(),
242            success: true,
243            deleted_at: Some(1700000300),
244        };
245        let json = serde_json::to_string(&resp).unwrap();
246        let parsed: AgentDeleteResponse = serde_json::from_str(&json).unwrap();
247        assert!(parsed.success);
248    }
249
250    #[test]
251    fn test_conversation_history_serde_roundtrip() {
252        let history = ConversationHistory {
253            conversation_id: "conv_abc".to_string(),
254            messages: vec![AgentMessage {
255                role: "user".to_string(),
256                content: "Hi".to_string(),
257                reasoning_content: None,
258                timestamp: None,
259            }],
260            total_count: Some(1),
261            has_more: Some(false),
262        };
263        let json = serde_json::to_string(&history).unwrap();
264        let parsed: ConversationHistory = serde_json::from_str(&json).unwrap();
265        assert_eq!(parsed.messages.len(), 1);
266        assert_eq!(parsed.messages[0].role, "user");
267    }
268}