1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
//! Clean node rendering
//!
//! Simple, maintainable rendering functions for each node type.
use crate::analyzer::TreeNode;
use crate::parser::models::{ContentBlock, NodeType};
use crate::tui::code_render;
use crate::tui::theme::{colors, icons};
use ratatui::{
style::{Color, Modifier, Style},
text::{Line, Span},
};
use std::collections::HashMap;
/// Context passed to all renderers carrying session-level data.
pub struct RenderContext<'a> {
/// tool_use_id → tool_name (for matching ToolResult with its ToolUse)
pub tool_correlation: &'a HashMap<String, String>,
/// tool_use_id → brief result summary (✓ 42 lines / ✗ error msg)
pub tool_result_map: &'a HashMap<String, String>,
}
/// Helper to render regular tool parameters
fn render_parameters(lines: &mut Vec<Line<'static>>, input: &serde_json::Value) {
lines.push(Line::from(Span::styled(
"Parameters:",
Style::default().fg(Color::Cyan),
)));
if let Some(obj) = input.as_object() {
for (key, value) in obj.iter() {
lines.push(Line::from(vec![
Span::styled(" ", Style::default()),
Span::styled(format!("{}: ", key), Style::default().fg(Color::Yellow)),
]));
// Render value line-by-line so multiline strings (old_string, new_string, etc.) display fully
let value_str = if let Some(s) = value.as_str() {
s.to_string()
} else {
format!("{}", value)
};
for line in value_str.lines() {
lines.push(Line::from(vec![
Span::styled(" ", Style::default()),
Span::raw(line.to_string()),
]));
}
}
}
}
/// Render a node's content for the details panel
pub fn render_node_content(node: &TreeNode, ctx: &RenderContext) -> Vec<Line<'static>> {
match node.node.node_type {
NodeType::User => render_user(node, ctx),
// All assistant nodes (including those with thinking + tool_use merged blocks)
// go through render_assistant which handles every block type in order.
NodeType::Assistant => render_assistant(node, ctx),
NodeType::Progress => render_progress(node),
NodeType::FileHistorySnapshot => render_file_snapshot(node),
NodeType::System => render_system(node),
_ => render_unknown(node),
}
}
/// Render user message
fn render_user(node: &TreeNode, ctx: &RenderContext) -> Vec<Line<'static>> {
let mut lines = vec![];
// Check if this is a tool result using typed blocks
let is_tool_result = node
.node
.message
.as_ref()
.map(|m| {
m.content_blocks()
.iter()
.any(|b| matches!(b, ContentBlock::ToolResult { .. }))
})
.unwrap_or(false);
if is_tool_result {
if let Some(ref msg) = node.node.message {
for block in msg.content_blocks() {
if let ContentBlock::ToolResult {
tool_use_id,
content,
is_error,
} = block
{
// Look up matched tool name from correlation map
let tool_name = ctx
.tool_correlation
.get(tool_use_id.as_str())
.map(|s| s.as_str())
.unwrap_or("Unknown");
// Header: "Tool Result ← ToolName [id]"
lines.push(Line::from(vec![
Span::styled(
format!("{} Tool Result ← ", icons::TOOL_RESULT),
Style::default()
.fg(colors::TOOL_RESULT)
.add_modifier(Modifier::BOLD),
),
Span::styled(
tool_name.to_string(),
Style::default()
.fg(Color::Yellow)
.add_modifier(Modifier::BOLD),
),
Span::styled(
format!(" [{}]", tool_use_id),
Style::default().fg(Color::DarkGray),
),
]));
lines.push(Line::from(""));
// Show error status
let err = is_error.unwrap_or(false);
lines.push(Line::from(vec![
Span::styled("Status: ", Style::default().fg(Color::Cyan)),
Span::styled(
if err { "Error" } else { "Success" },
Style::default().fg(if err { Color::Red } else { Color::Green }),
),
]));
lines.push(Line::from(""));
// Show content — prefer clean toolUseResult over block content
let (result_str, file_path) = if let Some(ref tool_use_result) =
node.node.tool_use_result
{
if let Ok(result) = serde_json::from_value::<
crate::parser::models::ToolResult,
>(tool_use_result.clone())
{
if let Some(ref file) = result.file {
(file.content.clone(), file.file_path.clone())
} else {
(result.content.clone(), None)
}
} else {
// Fallback: extract string from block content
let s = content.as_ref().and_then(|c| c.as_str()).map(String::from);
(s, None)
}
} else {
let s = content.as_ref().and_then(|c| c.as_str()).map(String::from);
(s, None)
};
if let Some(result_content) = result_str {
if !result_content.is_empty() {
lines.push(Line::from(Span::styled(
"Output:",
Style::default().fg(Color::Cyan),
)));
let language = file_path
.as_ref()
.and_then(|path| code_render::detect_language(path));
let highlighted_lines =
code_render::highlight_code(&result_content, language);
lines.extend(highlighted_lines);
} else {
lines.push(Line::from(Span::styled(
"(no output)",
Style::default().fg(Color::DarkGray),
)));
}
} else {
lines.push(Line::from(Span::styled(
"(no output)",
Style::default().fg(Color::DarkGray),
)));
}
}
}
}
} else {
// Regular user message with icon
lines.push(Line::from(Span::styled(
format!("{} User Message", icons::USER),
Style::default()
.fg(colors::USER_MSG)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
// Extract text from message using typed helper
if let Some(ref msg) = node.node.message {
let text = msg.text_content();
if !text.is_empty() {
for line in text.lines() {
lines.push(Line::from(line.to_string()));
}
} else {
lines.push(Line::from(Span::styled(
"(empty message)",
Style::default().fg(Color::DarkGray),
)));
}
}
}
lines
}
/// Render assistant message (with separate thinking and text)
fn render_assistant(node: &TreeNode, ctx: &RenderContext) -> Vec<Line<'static>> {
let mut lines = vec![];
if let Some(ref msg) = node.node.message {
let blocks = msg.content_blocks();
if !blocks.is_empty() {
let mut has_thinking = false;
let mut has_text = false;
let mut has_tools = false;
// First pass: render thinking blocks (always expanded)
for block in blocks {
if let ContentBlock::Thinking { thinking, .. } = block {
if !has_thinking {
lines.push(Line::from(Span::styled(
" Extended Thinking",
Style::default()
.fg(Color::Blue)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
has_thinking = true;
}
for line in thinking.lines() {
lines.push(Line::from(line.to_string()));
}
}
}
if has_thinking {
lines.push(Line::from(""));
}
// Second pass: render text and tool_use blocks
for block in blocks {
match block {
ContentBlock::Text { text } => {
if !has_text {
lines.push(Line::from(Span::styled(
" Response",
Style::default()
.fg(Color::Green)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
has_text = true;
}
for line in text.lines() {
lines.push(Line::from(line.to_string()));
}
}
ContentBlock::ToolUse { id, name, input } => {
if !has_tools {
if has_text {
lines.push(Line::from(""));
}
lines.push(Line::from(Span::styled(
" Tool Calls",
Style::default()
.fg(Color::Yellow)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
has_tools = true;
}
lines.push(Line::from(vec![Span::styled(
format!("{} Tool: {}", icons::TOOL_USE, name),
Style::default()
.fg(colors::TOOL_USE)
.add_modifier(Modifier::BOLD),
)]));
lines.push(Line::from(vec![
Span::styled("ID: ", Style::default().fg(Color::Cyan)),
Span::styled(id.clone(), Style::default().fg(Color::DarkGray)),
]));
lines.push(Line::from(""));
if name == "Edit" {
if let Some(rendered) =
code_render::render_edit_result(&input.to_string())
{
lines.extend(rendered);
} else {
render_parameters(&mut lines, input);
}
} else {
render_parameters(&mut lines, input);
}
// Brief result summary
if let Some(summary) = ctx.tool_result_map.get(id.as_str()) {
lines.push(Line::from(""));
let is_err = summary.starts_with('✗');
lines.push(Line::from(vec![
Span::styled("Result: ", Style::default().fg(Color::DarkGray)),
Span::styled(
summary.clone(),
Style::default().fg(if is_err {
Color::Red
} else {
Color::Green
}),
),
]));
}
lines.push(Line::from(""));
}
_ => {}
}
}
} else {
// Fallback for legacy string content
let text = msg.text_content();
if !text.is_empty() {
lines.push(Line::from(Span::styled(
" Response",
Style::default()
.fg(Color::Green)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
for line in text.lines() {
lines.push(Line::from(line.to_string()));
}
}
}
}
if lines.is_empty() {
lines.push(Line::from(Span::styled(
"Assistant (no content)",
Style::default().fg(Color::DarkGray),
)));
}
lines
}
/// Render tool use (also handles assistant messages with tool_use content)
#[allow(dead_code)]
fn render_tool_use(node: &TreeNode) -> Vec<Line<'static>> {
let mut lines = vec![];
// Check if this is an assistant message with tool_use content blocks
if node.node.node_type == NodeType::Assistant {
if let Some(ref msg) = node.node.message {
for block in msg.content_blocks() {
if let ContentBlock::ToolUse { id, name, input } = block {
lines.push(Line::from(Span::styled(
format!("{} Tool: {}", icons::TOOL_USE, name),
Style::default()
.fg(colors::TOOL_USE)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
// Show tool ID
lines.push(Line::from(vec![
Span::styled("ID: ", Style::default().fg(Color::Cyan)),
Span::styled(id.clone(), Style::default().fg(Color::DarkGray)),
]));
// Show input parameters with smart rendering for Edit tool
lines.push(Line::from(""));
if name == "Edit" {
if let Some(rendered) = code_render::render_edit_result(&input.to_string())
{
lines.extend(rendered);
} else {
render_parameters(&mut lines, input);
}
} else if name == "Read" {
if let Some(file_path) = input.get("file_path").and_then(|v| v.as_str()) {
lines.push(Line::from(vec![
Span::styled(
" Reading: ",
Style::default()
.fg(Color::Cyan)
.add_modifier(Modifier::BOLD),
),
Span::styled(
file_path.to_string(),
Style::default().fg(Color::Yellow),
),
]));
}
} else {
render_parameters(&mut lines, input);
}
}
}
}
} else if let Some(ref tool_use) = node.node.tool_use {
// Legacy tool_use node
lines.push(Line::from(Span::styled(
format!(" Tool: {}", tool_use.name),
Style::default()
.fg(Color::Yellow)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
// Show input parameters
if let Some(obj) = tool_use.input.as_object() {
lines.push(Line::from(Span::styled(
"Parameters:",
Style::default().fg(Color::Cyan),
)));
for (key, value) in obj.iter() {
let value_str = if let Some(s) = value.as_str() {
s.to_string()
} else {
format!("{}", value)
};
lines.push(Line::from(vec![
Span::styled(" ", Style::default()),
Span::styled(format!("{}: ", key), Style::default().fg(Color::Yellow)),
Span::raw(value_str),
]));
}
}
}
lines
}
/// Render tool result
#[allow(dead_code)]
fn render_tool_result(node: &TreeNode) -> Vec<Line<'static>> {
let mut lines = vec![];
// Try to parse toolUseResult from JSON value (if it's an object, not error string)
let tool_use_result: Option<crate::parser::models::ToolResult> = node
.node
.tool_use_result
.as_ref()
.and_then(|v| serde_json::from_value(v.clone()).ok());
// Prefer toolUseResult (clean content) over tool_result (has line numbers)
let result = tool_use_result.as_ref().or(node.node.tool_result.as_ref());
if let Some(result) = result {
let is_error = result.is_error.unwrap_or(false);
if is_error {
lines.push(Line::from(Span::styled(
" Error",
Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
if let Some(ref error) = result.error {
for line in error.lines() {
lines.push(Line::from(Span::styled(
line.to_string(),
Style::default().fg(Color::Red),
)));
}
}
} else {
lines.push(Line::from(Span::styled(
" Success",
Style::default()
.fg(Color::Green)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
// Prefer file.content (clean) over content (may have line numbers)
let (content_str, file_path) = if let Some(ref file) = result.file {
(file.content.as_ref(), file.file_path.as_ref())
} else {
(result.content.as_ref(), None)
};
if let Some(content) = content_str {
// Detect language from file path if available
let language = file_path.and_then(|path| code_render::detect_language(path));
// Try smart rendering for Edit tool results
if let Some(rendered) = code_render::render_edit_result(content) {
lines.extend(rendered);
} else {
let highlighted_lines = code_render::highlight_code(content, language);
lines.extend(highlighted_lines);
}
}
}
}
lines
}
/// Render thinking block
#[allow(dead_code)]
fn render_thinking(node: &TreeNode) -> Vec<Line<'static>> {
let mut lines = vec![];
lines.push(Line::from(Span::styled(
format!("{} Thinking", icons::THINKING),
Style::default()
.fg(colors::THINKING)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
if let Some(ref thinking) = node.node.thinking {
for line in thinking.lines() {
lines.push(Line::from(line.to_string()));
}
}
lines
}
/// Render progress update
fn render_progress(node: &TreeNode) -> Vec<Line<'static>> {
let mut lines = vec![];
lines.push(Line::from(Span::styled(
format!("{} Progress", icons::PROGRESS),
Style::default()
.fg(colors::PROGRESS)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
// Check for progress subtype in data.type
if let Some(data) = node.node.extra.as_ref().and_then(|e| e.get("data")) {
if let Some(progress_type) = data.get("type").and_then(|t| t.as_str()) {
lines.push(Line::from(vec![
Span::styled("Type: ", Style::default().fg(Color::Cyan)),
Span::raw(progress_type.to_string()),
]));
// Show type-specific fields
match progress_type {
"bash_progress" => {
if let Some(elapsed) = data.get("elapsedTimeSeconds").and_then(|e| e.as_f64()) {
lines.push(Line::from(vec![
Span::styled("Elapsed: ", Style::default().fg(Color::Cyan)),
Span::raw(format!("{:.1}s", elapsed)),
]));
}
if let Some(output) = data.get("fullOutput").and_then(|o| o.as_str()) {
lines.push(Line::from(""));
for line in output.lines() {
lines.push(Line::from(line.to_string()));
}
}
}
"agent_progress" => {
if let Some(agent_id) = data.get("agentId").and_then(|a| a.as_str()) {
lines.push(Line::from(vec![
Span::styled("Agent ID: ", Style::default().fg(Color::Cyan)),
Span::raw(agent_id.to_string()),
]));
}
if let Some(prompt) = data.get("prompt").and_then(|p| p.as_str()) {
lines.push(Line::from(""));
lines.push(Line::from(Span::styled(
"Task:",
Style::default().fg(Color::Cyan),
)));
for line in prompt.lines() {
lines.push(Line::from(line.to_string()));
}
}
}
_ => {}
}
}
}
lines
}
/// Render file snapshot
fn render_file_snapshot(node: &TreeNode) -> Vec<Line<'static>> {
let mut lines = vec![];
lines.push(Line::from(Span::styled(
" File Snapshot",
Style::default()
.fg(Color::Magenta)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
if let Some(snapshot) = node.node.extra.as_ref().and_then(|e| e.get("snapshot")) {
if let Some(tracked_files) = snapshot.get("trackedFileBackups") {
if let Some(files_obj) = tracked_files.as_object() {
lines.push(Line::from(format!("{} tracked files:", files_obj.len())));
lines.push(Line::from(""));
for (file_path, _) in files_obj.iter() {
lines.push(Line::from(vec![
Span::styled(" • ", Style::default().fg(Color::Cyan)),
Span::raw(file_path.clone()),
]));
}
if files_obj.len() > 15 {
lines.push(Line::from(Span::styled(
format!(" ... and {} more", files_obj.len() - 15),
Style::default().fg(Color::DarkGray),
)));
}
}
}
}
lines
}
/// Render system node
fn render_system(node: &TreeNode) -> Vec<Line<'static>> {
let mut lines = vec![];
lines.push(Line::from(Span::styled(
" System",
Style::default()
.fg(Color::Gray)
.add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(""));
if let Some(subtype) = node
.node
.extra
.as_ref()
.and_then(|e| e.get("subtype"))
.and_then(|s| s.as_str())
{
lines.push(Line::from(vec![
Span::styled("Type: ", Style::default().fg(Color::Cyan)),
Span::raw(subtype.to_string()),
]));
if subtype == "turn_duration" {
if let Some(duration_ms) = node
.node
.extra
.as_ref()
.and_then(|e| e.get("durationMs"))
.and_then(|d| d.as_i64())
{
lines.push(Line::from(vec![
Span::styled("Duration: ", Style::default().fg(Color::Cyan)),
Span::raw(format!("{:.2}s", duration_ms as f64 / 1000.0)),
]));
}
}
}
lines
}
/// Render unknown node type
fn render_unknown(node: &TreeNode) -> Vec<Line<'static>> {
vec![Line::from(Span::styled(
format!("Unknown: {}", node.node.node_type),
Style::default().fg(Color::DarkGray),
))]
}