1pub mod json_stream;
2pub mod null;
3pub mod terminal;
4
5pub use json_stream::JsonStreamRenderer;
6pub use null::NullRenderer;
7pub use terminal::TerminalRenderer;
8
9use std::io::{self, Write};
10
11use agent_base::{AgentResult, RuntimeEvent};
12
13pub trait EventRenderer: Send {
18 fn render(&mut self, event: RuntimeEvent) -> AgentResult<()>;
20
21 fn finish_turn(&mut self) -> AgentResult<()>;
23
24 fn finish_session(&mut self) -> AgentResult<()> {
26 Ok(())
27 }
28}
29
30#[derive(Clone, Debug)]
32pub enum OutputFormat {
33 Terminal {
35 show_thinking: bool,
36 show_tool_args: bool,
37 color: bool,
38 },
39 Json,
41 Quiet,
43}
44
45pub fn create_renderer(format: &OutputFormat, writer: Option<Box<dyn Write + Send>>) -> Box<dyn EventRenderer> {
50 match format {
51 OutputFormat::Terminal { show_thinking, show_tool_args, color } => {
52 let w = writer.unwrap_or_else(|| Box::new(io::stdout()));
53 Box::new(TerminalRenderer::new(*show_thinking, *show_tool_args, *color, w))
54 }
55 OutputFormat::Json => {
56 let w = writer.unwrap_or_else(|| Box::new(io::stdout()));
57 Box::new(JsonStreamRenderer::new(w))
58 }
59 OutputFormat::Quiet => Box::new(NullRenderer),
60 }
61}
62
63pub fn create_stdout_renderer(format: &OutputFormat) -> Box<dyn EventRenderer> {
65 create_renderer(format, None)
66}