tool call vis improvement

This commit is contained in:
Luxferre
2026-09-11 15:30:32 +03:00
parent 69571704a1
commit 9ee04ddf32
+40 -16
View File
@@ -1136,24 +1136,49 @@ func parseStream(ctx context.Context, r io.Reader) (Message, Usage, error) {
var order []int var order []int
var toolCallsShown bool var toolCallsShown bool
// Live, in-place display of tool calls as they stream in, so the user sees // Progressive, per-token display of tool calls as they stream in, so the
// the invocation text being shaped instead of a blank pause. Only used when // user watches the invocation being shaped token by token (like reasoning
// stdout is a terminal; piped/redirected output is left untouched. // text) instead of a blank pause. Only used when stdout is a terminal;
// piped/redirected output is left untouched. Each fragment is appended
// directly to the terminal; the closing ")" is emitted only when the call is
// finalized, so the visible text is always a true prefix of the final form.
tty := isTerminal(int(os.Stdout.Fd())) tty := isTerminal(int(os.Stdout.Fd()))
liveIdx := -1 liveIdx := -1
liveActive := false livePrinted := ""
showLive := func() { liveTarget := func(t *ToolCall, closed bool) string {
s := "[tool call: " + t.Function.Name
if closed || t.Function.Arguments != "" {
s += "(" + filterText(t.Function.Arguments)
if closed { s += ")]" }
}
return s
}
finishLive := func(closed bool) {
if liveIdx < 0 { return }
target := liveTarget(tcs[liveIdx], closed)
if len(target) > len(livePrinted) {
fmt.Print(c(target[len(livePrinted):], 33))
}
if closed { fmt.Print("\n") }
livePrinted = ""
liveIdx = -1
}
stepLive := func() {
if !tty || len(order) == 0 { return } if !tty || len(order) == 0 { return }
cur := order[len(order)-1] cur := order[len(order)-1]
txt := fmt.Sprintf("[tool call: %s(%s)]", tcs[cur].Function.Name, filterText(tcs[cur].Function.Arguments)) if liveIdx != -1 && liveIdx != cur {
if liveActive && liveIdx != cur { // A different tool call is now being shaped: finalize the previous one.
// A new tool call started; finalize the previous live line. finishLive(true)
fmt.Print("\n")
liveActive = false
} }
fmt.Print("\r\033[K" + c(txt, 33)) if liveIdx == -1 {
liveActive = true
liveIdx = cur liveIdx = cur
livePrinted = ""
}
target := liveTarget(tcs[cur], false)
if len(target) > len(livePrinted) {
fmt.Print(c(target[len(livePrinted):], 33))
livePrinted = target
}
toolCallsShown = true toolCallsShown = true
} }
@@ -1247,7 +1272,7 @@ func parseStream(ctx context.Context, r io.Reader) (Message, Usage, error) {
if tc.Function.Name != "" { t.Function.Name += tc.Function.Name } if tc.Function.Name != "" { t.Function.Name += tc.Function.Name }
if tc.Function.Arguments != "" { t.Function.Arguments += tc.Function.Arguments } if tc.Function.Arguments != "" { t.Function.Arguments += tc.Function.Arguments }
} }
showLive() stepLive()
} }
} }
if err := ctx.Err(); err != nil { return Message{}, lastUsage, err } if err := ctx.Err(); err != nil { return Message{}, lastUsage, err }
@@ -1260,10 +1285,9 @@ func parseStream(ctx context.Context, r io.Reader) (Message, Usage, error) {
fmt.Println(renderMDLine(lineBuf, &mdSt)) fmt.Println(renderMDLine(lineBuf, &mdSt))
} }
} }
if liveActive { if liveIdx >= 0 {
// Finalize the in-progress tool-call line so following output starts fresh. // Finalize the in-progress tool-call line so following output starts fresh.
fmt.Print("\n") finishLive(true)
liveActive = false
} }
if inReasoning { if inReasoning {
fmt.Print("\n" + c("--- reasoning end ---", 36) + "\n") fmt.Print("\n" + c("--- reasoning end ---", 36) + "\n")