Skip to content

Commit 664e80b

Browse files
committed
Improve output of tool call
Signed-off-by: David Gageot <david.gageot@docker.com>
1 parent 7b1f2e6 commit 664e80b

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

pkg/tui/components/tool/tool.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tool
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"log/slog"
67
"strings"
@@ -125,24 +126,35 @@ func (mv *toolModel) View() string {
125126
// Add tool result content if available (for completed tools with content)
126127
var resultContent string
127128
if (msg.ToolStatus == types.ToolStatusCompleted || msg.ToolStatus == types.ToolStatusError) && msg.Content != "" {
129+
var content string
130+
var m map[string]any
131+
if err := json.Unmarshal([]byte(msg.Content), &m); err != nil {
132+
content = msg.Content
133+
} else if buf, err := json.MarshalIndent(m, "", " "); err != nil {
134+
content = msg.Content
135+
} else {
136+
content = string(buf)
137+
}
138+
128139
// Calculate available width for content (accounting for padding)
129140
padding := styles.ToolCallResult.Padding().GetHorizontalPadding()
130141
availableWidth := max(mv.width-2-padding, 10) // Minimum readable width
131142

132143
// Wrap long lines to fit the component width
133-
lines := wrapLines(msg.Content, availableWidth)
144+
lines := wrapLines(content, availableWidth)
134145

135146
// Take only first 10 lines after wrapping
147+
header := "output"
136148
if len(lines) > 10 {
137149
lines = lines[:10]
138-
// Add indicator that content was truncated
139-
lines = append(lines, wrapLines("... (output truncated)", availableWidth)...)
150+
header = "output (truncated)"
151+
lines = append(lines, wrapLines("...", availableWidth)...)
140152
}
141153

142-
// Join the lines back and apply muted style
154+
// Join the lines back
143155
trimmedContent := strings.Join(lines, "\n")
144156
if trimmedContent != "" {
145-
resultContent = "\n" + styles.ToolCallResult.Render(styles.ToolCallResultKey.Render("\n-> output:")+"\n"+trimmedContent)
157+
resultContent = "\n" + styles.ToolCallResult.Render(styles.ToolCallResultKey.Render("\n-> "+header+":")+"\n"+trimmedContent)
146158
}
147159
}
148160

0 commit comments

Comments
 (0)