Files
archie/internal/gateway/stream_test.go
T
sam 0d73f2fa5a feat(chat): show tool calls inline and stream replies into one message
Chat returned only the final answer, so a wrong tool choice, a
hallucinated action, or a footgun like `systemctl stop` against
`restart` was invisible from the conversation. Only text deltas were
threaded through the turn, and tool calls executed inside Generate with
nowhere to report.

Replace the delta callback with gateway.TurnStream, which carries text
fragments and completed tool calls on one ordered path from the model
loop to the channel. The sequence is the point: a tool line that
overtakes the sentence it interrupted describes a turn that never
happened. Text-only callers use gateway.DeltaFunc and are unaffected.

A tool is reported on its result, never on its call  --  the call part
carries no outcome yet, so reporting both would list every tool twice,
once with nothing to say.

Telegram renders it behind [chat.telegram] show_tool_calls, off by
default: this is debugging visibility, and it narrates every internal
step to provide it. The dashboard always shows it, being the operator's
own observability surface; its tool lines are live-only and are not
persisted with the conversation.

The Telegram live reply changes mechanism, and with it what a frame
MEANS. It used to stream through sendMessageDraft: drafts are ephemeral
30-second previews, so a tool call longer than that outlived the
preview, and successive frames were observed concatenated with their
cursors intact. It now sends one real message and edits it. An update
used to be "the next preview of the answer"; it is now "the whole
answer as it currently stands, replacing what is there". The cursor is
appended at render time and never stored, so exactly one exists and
always at the end, and a frame too long for one message is cut to its
tail rather than rejected outright.

A stopped or failed turn now edits the cursor away instead of leaving
it blinking on a message nothing will finish. That cleanup runs on a
live context because turnCtx is already dead, and it runs on both
failure branches: ai-sdk raises an aborted stream as ErrAborted with
ctx.Err() formatted in rather than wrapped, so errors.Is cannot be
trusted to tell a /stop from a fault.

Closes #467
2026-08-14 02:36:12 +10:00

98 lines
2.7 KiB
Go

package gateway
import (
"strings"
"testing"
)
func TestToolCallEventSummary(t *testing.T) {
tests := []struct {
name string
event ToolCallEvent
want string
}{
{
name: "single line output is the summary",
event: ToolCallEvent{Name: "memory_edit", Output: "added entry"},
want: "added entry",
},
{
name: "only the first non-empty line is kept",
event: ToolCallEvent{Name: "read", Output: "\n\nfirst line\nsecond line"},
want: "first line",
},
{
name: "surrounding whitespace is trimmed",
event: ToolCallEvent{Name: "shell", Output: " exit 0 \n"},
want: "exit 0",
},
{
name: "an error replaces the output",
event: ToolCallEvent{Name: "shell", Output: "partial", Err: "exit status 2"},
want: "failed: exit status 2",
},
{
name: "a multi-line error is reduced the same way",
event: ToolCallEvent{Name: "shell", Err: "exit status 2\nstack trace"},
want: "failed: exit status 2",
},
{
name: "no output at all still reads as a completed call",
event: ToolCallEvent{Name: "write"},
want: "done",
},
{
name: "whitespace-only output reads as a completed call",
event: ToolCallEvent{Name: "write", Output: " \n "},
want: "done",
},
{
name: "a long line is truncated with an ellipsis",
event: ToolCallEvent{Name: "grep", Output: strings.Repeat("x", toolSummaryMaxRunes+10)},
want: strings.Repeat("x", toolSummaryMaxRunes) + "…",
},
{
name: "truncation counts runes, not bytes",
event: ToolCallEvent{Name: "grep", Output: strings.Repeat("é", toolSummaryMaxRunes+10)},
want: strings.Repeat("é", toolSummaryMaxRunes) + "…",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := tc.event.Summary(); got != tc.want {
t.Fatalf("Summary() = %q, want %q", got, tc.want)
}
})
}
}
// A caller that only renders text must be able to keep passing a plain
// callback, and must never be handed a tool event it cannot use.
func TestDeltaFuncIsTextOnly(t *testing.T) {
var got []string
stream := DeltaFunc(func(delta string) { got = append(got, delta) })
stream.Delta("one")
stream.ToolCall(ToolCallEvent{Name: "shell", Output: "exit 0"})
stream.Delta("two")
want := []string{"one", "two"}
if len(got) != len(want) {
t.Fatalf("deltas = %v, want %v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("deltas = %v, want %v", got, want)
}
}
}
// RouteStream and TurnRunner accept a nil sink to mean "not streaming", so a
// nil DeltaFunc must be inert rather than a panic waiting for the first token.
func TestDeltaFuncNilIsInert(t *testing.T) {
stream := DeltaFunc(nil)
stream.Delta("anything")
stream.ToolCall(ToolCallEvent{Name: "shell"})
}