Skip to main content

synaptic_callbacks/
stdout.rs

1use async_trait::async_trait;
2use synaptic_core::{CallbackHandler, RunEvent, SynapticError};
3
4/// A callback handler that prints events to stdout.
5///
6/// When `verbose` is true, additional detail is printed for each event.
7pub 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        }
73        Ok(())
74    }
75}