fix(messaging): render a tool call as one line with its own icon
A tool call rendered as a label line plus a ```text fence. Telegram draws a fenced block as a full code widget (language header bar, copy button, panel), and the block parser inserts a spacer paragraph before every block, so one call cost four rendered elements. Three calls filled the screen to show three word counts. Every toolPreview path already summarises to a single line, so the fence was never wrapping more than one. Drop it and put the preview on the header line. Tool lines now join with a hard break rather than a bare newline: a soft break would space-join them into one run-on paragraph, and a blank line would put the spacer back. Three calls are now three consecutive blocks. Two hardcoded values go with it. The status word is printed only when the outcome is not success, since every line otherwise read "done" and distinguished nothing. The icon is no longer a literal in the renderer: each builtin declares its own on tools.Tool (not on Schema, which is the model's contract), the provider carries it into ToolEntry, and the turn snapshots the registry's icons alongside its tool set. A tool that declares none renders without one rather than borrowing a generic wrench. Carries a dependency upgrade (go.mod, go.sum, tools/go.mod, tools/go.sum).
This commit is contained in:
22 files changed
+340
-49
No files matched your search
@@ -243,15 +243,15 @@ func chatRepoEnv(cfg config.Config, identity string) []gateway.RepoEnv {
|
||||
// and records at its own adapter (curatorLLMRunner). Anything that reaches the
|
||||
// runtime without recording leaves /status reporting a stale last-known
|
||||
// outcome, which is a /status that lies about the provider.
|
||||
func sendChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, options core.GenerateOptions, turn gateway.TurnStream, outcomes *providerOutcomeRecorder) (string, error) {
|
||||
text, err := runChatTurn(ctx, llm, chatModel, options, turn)
|
||||
func sendChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, options core.GenerateOptions, turn gateway.TurnStream, outcomes *providerOutcomeRecorder, icons map[string]string) (string, error) {
|
||||
text, err := runChatTurn(ctx, llm, chatModel, options, turn, icons)
|
||||
// record tolerates a nil recorder: a setup built without one (tests, a
|
||||
// deployment whose health surface is unwired) still makes its calls.
|
||||
outcomes.record(chatModel, err)
|
||||
return text, err
|
||||
}
|
||||
|
||||
func runChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, options core.GenerateOptions, turn gateway.TurnStream) (string, error) {
|
||||
func runChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, options core.GenerateOptions, turn gateway.TurnStream, icons map[string]string) (string, error) {
|
||||
if turn == nil {
|
||||
result, err := llm.Chat(ctx, chatModel, options)
|
||||
if err != nil {
|
||||
@@ -263,7 +263,7 @@ func runChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, op
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("llm chat stream: %w", err)
|
||||
}
|
||||
text := drainChatStream(stream.FullStream, turn)
|
||||
text := drainChatStream(stream.FullStream, turn, icons)
|
||||
if _, err := stream.FinishReason(); err != nil {
|
||||
return "", fmt.Errorf("llm chat stream: %w", err)
|
||||
}
|
||||
@@ -280,7 +280,7 @@ func runChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, op
|
||||
// A tool is reported on its result, never on its call: the call part carries
|
||||
// no outcome yet, so reporting both would show every tool twice, once with
|
||||
// nothing to say.
|
||||
func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream) string {
|
||||
func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream, icons map[string]string) string {
|
||||
type pendingToolCall struct {
|
||||
name string
|
||||
parameters string
|
||||
@@ -319,6 +319,7 @@ func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream) stri
|
||||
turn.ToolCall(gateway.ToolCallEvent{
|
||||
ID: part.ToolResult.ToolCallID,
|
||||
Name: name,
|
||||
Emoji: icons[name],
|
||||
Parameters: call.parameters,
|
||||
Output: part.ToolResult.Output,
|
||||
Err: part.ToolResult.Error,
|
||||
@@ -401,8 +402,10 @@ func (g *chatTitleGenerator) GenerateTitle(ctx context.Context, sessionID, first
|
||||
// caller. Sharing the process-wide recorder let a title that merely timed
|
||||
// out overwrite the chat-model health line with "failed", reporting a
|
||||
// broken provider on the strength of a call no user was waiting for.
|
||||
// No tool set and no stream, so no tool call can be reported: this turn
|
||||
// needs no icons.
|
||||
text, err := sendChatTurn(ctx, g.llm, g.chatModels.ActiveModel(),
|
||||
core.GenerateOptions{Messages: messages, MaxSteps: 1}, nil, nil)
|
||||
core.GenerateOptions{Messages: messages, MaxSteps: 1}, nil, nil, nil)
|
||||
if err != nil {
|
||||
if g.log != nil {
|
||||
g.log.Error("session title generation failed", "session", sessionID, "err", err)
|
||||
|
||||
@@ -163,7 +163,7 @@ func TestDrainChatStream(t *testing.T) {
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
sink := &recordingStream{}
|
||||
text := drainChatStream(streamOf(tc.parts...), sink)
|
||||
text := drainChatStream(streamOf(tc.parts...), sink, nil)
|
||||
|
||||
if text != tc.wantText {
|
||||
t.Fatalf("text = %q, want %q", text, tc.wantText)
|
||||
@@ -204,7 +204,7 @@ func TestDrainChatStreamCorrelatesBoundsAndRedactsToolParameters(t *testing.T) {
|
||||
ToolName: "shell",
|
||||
Output: "ok",
|
||||
}},
|
||||
), sink)
|
||||
), sink, nil)
|
||||
|
||||
if len(sink.events) != 2 {
|
||||
t.Fatalf("events = %v, want two completed calls", sink.events)
|
||||
@@ -231,9 +231,43 @@ func TestDrainChatStreamWithoutASinkStillCollectsText(t *testing.T) {
|
||||
core.StreamPart{Type: core.StreamPartTextDelta, TextDelta: "a"},
|
||||
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolName: "shell", Output: "ok"}},
|
||||
core.StreamPart{Type: core.StreamPartTextDelta, TextDelta: "b"},
|
||||
), nil)
|
||||
), nil, nil)
|
||||
|
||||
if text != "ab" {
|
||||
t.Fatalf("text = %q, want %q", text, "ab")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDrainChatStreamCarriesTheToolsRegisteredIcon pins the seam between the
|
||||
// tool registry and what a chat surface renders. The icon is looked up by
|
||||
// tool name at report time, so a tool that registers one gets it and a tool
|
||||
// that does not is reported without one rather than with a generic stand-in.
|
||||
func TestDrainChatStreamCarriesTheToolsRegisteredIcon(t *testing.T) {
|
||||
sink := &iconRecordingStream{}
|
||||
drainChatStream(streamOf(
|
||||
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolName: "shell", Output: "ok"}},
|
||||
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolName: "mcp.github.search", Output: "ok"}},
|
||||
), sink, map[string]string{"shell": "💻"})
|
||||
|
||||
want := []string{"shell:💻", "mcp.github.search:"}
|
||||
if len(sink.icons) != len(want) {
|
||||
t.Fatalf("icons = %v, want %v", sink.icons, want)
|
||||
}
|
||||
for i, w := range want {
|
||||
if sink.icons[i] != w {
|
||||
t.Errorf("icons[%d] = %q, want %q", i, sink.icons[i], w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type iconRecordingStream struct {
|
||||
icons []string
|
||||
}
|
||||
|
||||
func (s *iconRecordingStream) Delta(string) {}
|
||||
|
||||
func (s *iconRecordingStream) ToolCall(event gateway.ToolCallEvent) {
|
||||
s.icons = append(s.icons, event.Name+":"+event.Emoji)
|
||||
}
|
||||
|
||||
func (s *iconRecordingStream) Media(gateway.MediaEvent) {}
|
||||
@@ -60,6 +60,7 @@ func (m *chatTurnModel) Prepare(
|
||||
toolInfo: toolSummaries(options.Tools),
|
||||
toolTokens: gateway.EstimateTokens(string(toolSchema)),
|
||||
outcomes: m.outcomes,
|
||||
icons: toolIcons(m.registry),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -70,6 +71,26 @@ type preparedChatTurnModel struct {
|
||||
toolInfo []gateway.ToolSummary
|
||||
toolTokens int
|
||||
outcomes *providerOutcomeRecorder
|
||||
// icons maps tool name to the icon that tool registered, for chat
|
||||
// surfaces to render. A nil map yields "" for every lookup, which is
|
||||
// the no-icon rendering.
|
||||
icons map[string]string
|
||||
}
|
||||
|
||||
// toolIcons snapshots the icon each registered tool declares. It is taken at
|
||||
// prepare time alongside the tool set itself, so a turn renders the icons of
|
||||
// the tools it was actually given.
|
||||
func toolIcons(registry *tools.Registry) map[string]string {
|
||||
if registry == nil {
|
||||
return nil
|
||||
}
|
||||
icons := make(map[string]string)
|
||||
for _, entry := range registry.All() {
|
||||
if entry.Emoji != "" {
|
||||
icons[entry.Name] = entry.Emoji
|
||||
}
|
||||
}
|
||||
return icons
|
||||
}
|
||||
|
||||
func (m *preparedChatTurnModel) ToolSummaries() []gateway.ToolSummary {
|
||||
@@ -100,5 +121,5 @@ func (m *preparedChatTurnModel) Generate(
|
||||
// This is one provider response's output allowance, not a turn-
|
||||
// continuation budget. Tool loops remain free to continue.
|
||||
options.MaxTokens = request.MaxOutputTokens
|
||||
return sendChatTurn(ctx, m.llm, m.model, options, stream, m.outcomes)
|
||||
return sendChatTurn(ctx, m.llm, m.model, options, stream, m.outcomes, m.icons)
|
||||
}
|
||||
@@ -163,7 +163,7 @@ func TestSendChatTurnRecordsEachCallOutcome(t *testing.T) {
|
||||
_, err := sendChatTurn(t.Context(), llm, "openai/gpt-5.6", core.GenerateOptions{
|
||||
Messages: []chat.Message{{Role: chat.RoleUser, Content: "hi"}},
|
||||
MaxSteps: 1,
|
||||
}, nil, recorder)
|
||||
}, nil, recorder, nil)
|
||||
|
||||
if tc.wantErr == "" && err != nil {
|
||||
t.Fatalf("sendChatTurn = %v, want success (request path %q)", err, requestedPath)
|
||||
|
||||
Reference in new issue
Block a user