Skip to main content

call_coding_clis/output/
render.rs

1use super::model::{Event, Transcript};
2
3pub fn render_transcript(transcript: &Transcript, show_thinking: bool, tty: bool) -> String {
4    crate::json_output::render_parsed(
5        &parsed_output_from_transcript(transcript),
6        show_thinking,
7        tty,
8    )
9}
10
11fn parsed_output_from_transcript(transcript: &Transcript) -> crate::json_output::ParsedJsonOutput {
12    crate::json_output::ParsedJsonOutput {
13        schema_name: String::new(),
14        events: transcript.events.iter().map(event_to_json_event).collect(),
15        final_text: transcript.final_text.clone(),
16        session_id: transcript.session_id.clone().unwrap_or_default(),
17        error: transcript.error.clone().unwrap_or_default(),
18        usage: transcript.usage.counts.clone(),
19        cost_usd: transcript.usage.cost_usd,
20        duration_ms: transcript.usage.duration_ms,
21        unknown_json_lines: transcript.unknown_json_lines.clone(),
22    }
23}
24
25fn event_to_json_event(event: &Event) -> crate::json_output::JsonEvent {
26    match event {
27        Event::Text(text) => crate::json_output::JsonEvent {
28            event_type: "text".into(),
29            text: text.clone(),
30            thinking: String::new(),
31            tool_call: None,
32            tool_result: None,
33        },
34        Event::Thinking(thinking) => crate::json_output::JsonEvent {
35            event_type: "thinking".into(),
36            text: String::new(),
37            thinking: thinking.clone(),
38            tool_call: None,
39            tool_result: None,
40        },
41        Event::ToolCall(tool_call) => crate::json_output::JsonEvent {
42            event_type: "tool_use".into(),
43            text: String::new(),
44            thinking: String::new(),
45            tool_call: Some(crate::json_output::ToolCall {
46                id: tool_call.id.clone(),
47                name: tool_call.name.clone(),
48                arguments: tool_call.arguments.clone(),
49            }),
50            tool_result: None,
51        },
52        Event::ToolResult(tool_result) => crate::json_output::JsonEvent {
53            event_type: "tool_result".into(),
54            text: String::new(),
55            thinking: String::new(),
56            tool_call: None,
57            tool_result: Some(crate::json_output::ToolResult {
58                tool_call_id: tool_result.tool_call_id.clone(),
59                content: tool_result.content.clone(),
60                is_error: tool_result.is_error,
61            }),
62        },
63        Event::Error(error) => crate::json_output::JsonEvent {
64            event_type: "error".into(),
65            text: error.clone(),
66            thinking: String::new(),
67            tool_call: None,
68            tool_result: None,
69        },
70        Event::RawUnknownJson(raw) => crate::json_output::JsonEvent {
71            event_type: "raw_unknown_json".into(),
72            text: raw.clone(),
73            thinking: String::new(),
74            tool_call: None,
75            tool_result: None,
76        },
77    }
78}