synaptic_callbacks/
stdout.rs1use async_trait::async_trait;
2use synaptic_core::{CallbackHandler, RunEvent, SynapticError};
3
4pub struct StdOutCallbackHandler {
8 verbose: bool,
9}
10
11impl StdOutCallbackHandler {
12 pub fn new() -> Self {
13 Self { verbose: false }
14 }
15
16 pub fn verbose() -> Self {
17 Self { verbose: true }
18 }
19}
20
21impl Default for StdOutCallbackHandler {
22 fn default() -> Self {
23 Self::new()
24 }
25}
26
27#[async_trait]
28impl CallbackHandler for StdOutCallbackHandler {
29 async fn on_event(&self, event: RunEvent) -> Result<(), SynapticError> {
30 match event {
31 RunEvent::RunStarted { run_id, session_id } => {
32 if self.verbose {
33 println!("[RunStarted] run_id={run_id} session_id={session_id}");
34 } else {
35 println!("[RunStarted] run_id={run_id}");
36 }
37 }
38 RunEvent::RunStep { run_id, step } => {
39 if self.verbose {
40 println!("[RunStep] run_id={run_id} step={step}");
41 } else {
42 println!("[RunStep] step={step}");
43 }
44 }
45 RunEvent::LlmCalled {
46 run_id,
47 message_count,
48 } => {
49 if self.verbose {
50 println!("[LlmCalled] run_id={run_id} message_count={message_count}");
51 } else {
52 println!("[LlmCalled] message_count={message_count}");
53 }
54 }
55 RunEvent::ToolCalled { run_id, tool_name } => {
56 if self.verbose {
57 println!("[ToolCalled] run_id={run_id} tool_name={tool_name}");
58 } else {
59 println!("[ToolCalled] tool_name={tool_name}");
60 }
61 }
62 RunEvent::RunFinished { run_id, output } => {
63 if self.verbose {
64 println!("[RunFinished] run_id={run_id} output={output}");
65 } else {
66 println!("[RunFinished] run_id={run_id}");
67 }
68 }
69 RunEvent::RunFailed { run_id, error } => {
70 println!("[RunFailed] run_id={run_id} error={error}");
71 }
72 RunEvent::BeforeToolCall {
73 run_id,
74 tool_name,
75 arguments,
76 } => {
77 if self.verbose {
78 println!("[BeforeToolCall] run_id={run_id} tool={tool_name} args={arguments}");
79 }
80 }
81 RunEvent::AfterToolCall {
82 run_id,
83 tool_name,
84 result,
85 } => {
86 if self.verbose {
87 let truncated = if result.len() > 100 {
88 format!("{}...", &result[..100])
89 } else {
90 result
91 };
92 println!("[AfterToolCall] run_id={run_id} tool={tool_name} result={truncated}");
93 }
94 }
95 RunEvent::BeforeMessage {
96 run_id,
97 message_count,
98 } => {
99 if self.verbose {
100 println!("[BeforeMessage] run_id={run_id} messages={message_count}");
101 }
102 }
103 RunEvent::AfterMessage {
104 run_id,
105 response_length,
106 } => {
107 if self.verbose {
108 println!("[AfterMessage] run_id={run_id} response_len={response_length}");
109 }
110 }
111 }
112 Ok(())
113 }
114}