aether_core/testing/
agent_event_builder.rs1use crate::events::{AgentEvent, StreamState, ToolEvent};
2use crate::mcp::tool_bridge::encode_structured;
3use serde::Serialize;
4
5pub fn agent_event(message_id: &str) -> AgentEventBuilder {
6 AgentEventBuilder::new(message_id)
7}
8
9pub struct AgentEventBuilder {
10 message_id: String,
11 chunks: Vec<AgentEvent>,
12 full_text: String,
13}
14
15impl AgentEventBuilder {
16 pub fn new(message_id: &str) -> Self {
17 Self { message_id: message_id.to_string(), chunks: Vec::new(), full_text: String::new() }
18 }
19
20 pub fn text(mut self, chunks: &[&str]) -> Self {
21 for chunk in chunks {
22 self.chunks.push(AgentEvent::text(&self.message_id, chunk, StreamState::Partial));
23 self.full_text.push_str(chunk);
24 }
25 self
26 }
27
28 pub fn tool_call<T: Serialize, U: Serialize>(
29 mut self,
30 tool_call_id: &str,
31 name: &str,
32 request: &T,
33 result: &U,
34 ) -> Self {
35 let request_json = serde_json::to_string(request).expect("Failed to serialize request");
36 let result_value = serde_json::to_value(result).expect("Failed to serialize result");
37
38 self.push_tool_call(tool_call_id, name, &request_json);
39
40 self.chunks.push(AgentEvent::tool_result(tool_call_id, name, &request_json, &encode_structured(&result_value)));
41
42 self
43 }
44
45 pub fn tool_call_with_error<T: Serialize>(
46 mut self,
47 tool_call_id: &str,
48 name: &str,
49 request: &T,
50 error_message: &str,
51 ) -> Self {
52 let request_json = serde_json::to_string(request).expect("Failed to serialize request");
53
54 let error_result = format!("Tool execution error: {error_message}");
55
56 self.push_tool_call(tool_call_id, name, &request_json);
57
58 self.chunks.push(AgentEvent::tool_error(tool_call_id, name, &request_json, &error_result));
59
60 self
61 }
62
63 pub fn build(mut self) -> Vec<AgentEvent> {
64 self.chunks.push(AgentEvent::text(&self.message_id, &self.full_text, StreamState::Complete));
65
66 self.chunks
67 }
68
69 fn push_tool_call(&mut self, tool_call_id: &str, name: &str, arguments: &str) {
70 self.chunks.extend([
71 AgentEvent::Tool(ToolEvent::InputStarted { id: tool_call_id.to_string(), name: name.to_string() }),
72 AgentEvent::Tool(ToolEvent::InputDelta { id: tool_call_id.to_string(), chunk: arguments.to_string() }),
73 AgentEvent::tool_call(tool_call_id, name, arguments),
74 ]);
75 }
76}