|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/GrayCodeAI/reflect/internal/engine" |
| 8 | + "github.com/GrayCodeAI/reflect/internal/ui/icons" |
| 9 | +) |
| 10 | + |
| 11 | +// displayBlock is a projection over the transcript, never a replacement for |
| 12 | +// it. Rendering and keyboard targeting use the same boundaries and indices. |
| 13 | +// Prompts, warnings, answers, and user messages always keep their own blocks. |
| 14 | +type displayBlock struct { |
| 15 | + start, end int |
| 16 | + activity bool |
| 17 | +} |
| 18 | + |
| 19 | +func isToolMessage(msg displayMsg) bool { |
| 20 | + return msg.role == "tool_use" || msg.role == "tool_result" |
| 21 | +} |
| 22 | + |
| 23 | +func nextDisplayBlock(messages []displayMsg, start, end int) displayBlock { |
| 24 | + block := displayBlock{start: start, end: start + 1, activity: isToolMessage(messages[start])} |
| 25 | + if block.activity { |
| 26 | + for block.end < end && isToolMessage(messages[block.end]) { |
| 27 | + block.end++ |
| 28 | + } |
| 29 | + } |
| 30 | + return block |
| 31 | +} |
| 32 | + |
| 33 | +type activityCall struct { |
| 34 | + name string |
| 35 | + state engine.ToolState |
| 36 | +} |
| 37 | + |
| 38 | +// activityCalls joins results by call ID, keeping launch order even when calls |
| 39 | +// finish out of order. ID-less legacy messages pair with the latest pending |
| 40 | +// call of the same name. They never collapse distinct identified calls. |
| 41 | +func activityCalls(messages []displayMsg, block displayBlock) []activityCall { |
| 42 | + var calls []activityCall |
| 43 | + byID := make(map[string]int) |
| 44 | + pending := make([]bool, 0) |
| 45 | + for _, msg := range messages[block.start:block.end] { |
| 46 | + index, found := byID[msg.toolID] |
| 47 | + found = found && msg.toolID != "" |
| 48 | + if !found && msg.role == "tool_result" && msg.toolID == "" { |
| 49 | + for i := len(calls) - 1; i >= 0; i-- { |
| 50 | + if pending[i] && (msg.toolName == "" || calls[i].name == msg.toolName) { |
| 51 | + index, found = i, true |
| 52 | + break |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + if !found { |
| 57 | + name := msg.toolName |
| 58 | + if name == "" && msg.role == "tool_use" { |
| 59 | + name = msg.content |
| 60 | + } |
| 61 | + index = len(calls) |
| 62 | + calls = append(calls, activityCall{name: name, state: msg.toolState}) |
| 63 | + pending = append(pending, msg.role == "tool_use") |
| 64 | + if msg.toolID != "" { |
| 65 | + byID[msg.toolID] = index |
| 66 | + } |
| 67 | + } |
| 68 | + if msg.role == "tool_result" { |
| 69 | + calls[index].state = msg.toolState |
| 70 | + if calls[index].state == "" { |
| 71 | + calls[index].state = engine.ToolStateCompleted |
| 72 | + } |
| 73 | + pending[index] = false |
| 74 | + } |
| 75 | + } |
| 76 | + return calls |
| 77 | +} |
| 78 | + |
| 79 | +func activityCategory(name string) int { |
| 80 | + switch strings.ToLower(name) { |
| 81 | + case "read", "read_file": |
| 82 | + return 0 |
| 83 | + case "grep", "glob", "ls", "code_search", "codesearch", "search": |
| 84 | + return 1 |
| 85 | + case "bash", "powershell", "shell": |
| 86 | + return 2 |
| 87 | + case "write", "edit", "multiedit", "notebookedit", "structurededit", "write_file", "edit_file", "apply_patch": |
| 88 | + return 3 |
| 89 | + default: |
| 90 | + return 4 |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func renderActivityGroup(messages []displayMsg, block displayBlock, width int, expanded map[int]bool) string { |
| 95 | + calls := activityCalls(messages, block) |
| 96 | + var counts [5]int |
| 97 | + var running, failed, cancelled, incomplete int |
| 98 | + for _, call := range calls { |
| 99 | + counts[activityCategory(call.name)]++ |
| 100 | + switch call.state { |
| 101 | + case engine.ToolStateCompleted: |
| 102 | + case engine.ToolStateFailed, engine.ToolStateTimedOut: |
| 103 | + failed++ |
| 104 | + case engine.ToolStateCancelled: |
| 105 | + cancelled++ |
| 106 | + case toolStateIncomplete: |
| 107 | + incomplete++ |
| 108 | + default: |
| 109 | + running++ |
| 110 | + } |
| 111 | + } |
| 112 | + marker, style := icons.Check(), statusDimStyle |
| 113 | + var parts []string |
| 114 | + if failed > 0 { |
| 115 | + marker, style = "!", errorStyle |
| 116 | + parts = append(parts, fmt.Sprintf("%d failed", failed)) |
| 117 | + } |
| 118 | + if cancelled > 0 { |
| 119 | + if failed == 0 { |
| 120 | + marker = "×" |
| 121 | + } |
| 122 | + parts = append(parts, fmt.Sprintf("%d cancelled", cancelled)) |
| 123 | + } |
| 124 | + if running > 0 { |
| 125 | + if failed == 0 { |
| 126 | + marker, style = "·", toolStyle |
| 127 | + } |
| 128 | + parts = append(parts, fmt.Sprintf("%d running", running)) |
| 129 | + } |
| 130 | + if incomplete > 0 { |
| 131 | + if failed == 0 && running == 0 { |
| 132 | + marker, style = "?", warnStyle |
| 133 | + } |
| 134 | + parts = append(parts, fmt.Sprintf("%d incomplete", incomplete)) |
| 135 | + } |
| 136 | + labels := [5][2]string{{"Read %d file", "Read %d files"}, {"%d search", "%d searches"}, {"Ran %d command", "Ran %d commands"}, {"%d edit", "%d edits"}, {"%d tool call", "%d tool calls"}} |
| 137 | + for category, count := range counts { |
| 138 | + if count > 0 { |
| 139 | + plural := 0 |
| 140 | + if count != 1 { |
| 141 | + plural = 1 |
| 142 | + } |
| 143 | + parts = append(parts, fmt.Sprintf(labels[category][plural], count)) |
| 144 | + } |
| 145 | + } |
| 146 | + hint := "Tab, Enter: details" |
| 147 | + if expanded[block.start] { |
| 148 | + hint = "Enter: collapse" |
| 149 | + } |
| 150 | + heading := marker + " " + strings.Join(parts, " · ") + " · " + hint |
| 151 | + var b strings.Builder |
| 152 | + b.WriteString(style.Render(clipFooterLine(heading, max(1, width)))) |
| 153 | + b.WriteByte('\n') |
| 154 | + if expanded[block.start] { |
| 155 | + // Keep individual results collapsed until the whole group is opened. |
| 156 | + // Opening reveals the existing bounded detail view, including commands. |
| 157 | + details := make(map[int]bool, block.end-block.start) |
| 158 | + for i := block.start; i < block.end; i++ { |
| 159 | + details[i] = true |
| 160 | + } |
| 161 | + for i := block.start; i < block.end; i++ { |
| 162 | + b.WriteString(renderDisplayMessage(messages[i], i, messages, width, details)) |
| 163 | + } |
| 164 | + } else if failed > 0 { |
| 165 | + // A short diagnostic remains visible without opening routine output. |
| 166 | + for i := block.end - 1; i >= block.start; i-- { |
| 167 | + msg := messages[i] |
| 168 | + if msg.role == "tool_result" && (msg.toolState == engine.ToolStateFailed || msg.toolState == engine.ToolStateTimedOut) { |
| 169 | + lines := strings.Split(strings.TrimSpace(sanitizeDisplay(msg.content)), "\n") |
| 170 | + if tail := singleLineActivity(lines[len(lines)-1]); tail != "" { |
| 171 | + b.WriteString(errorStyle.Render(clipFooterLine(" "+tail, max(1, width)))) |
| 172 | + b.WriteByte('\n') |
| 173 | + } |
| 174 | + break |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + b.WriteByte('\n') |
| 179 | + return b.String() |
| 180 | +} |
| 181 | + |
| 182 | +func renderDisplayBlock(messages []displayMsg, block displayBlock, width int, expanded map[int]bool) string { |
| 183 | + if block.activity { |
| 184 | + return renderActivityGroup(messages, block, width, expanded) |
| 185 | + } |
| 186 | + return renderDisplayMessage(messages[block.start], block.start, messages, width, expanded) |
| 187 | +} |
0 commit comments