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
89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// RouteStream must degrade to Route when no streaming responder is set, so
|
|
// adapters can always call it without first checking for support.
|
|
func TestRouteStreamFallsBackWhenNoStreamResponder(t *testing.T) {
|
|
r := NewRouter(nil, func(context.Context, Message) (string, error) {
|
|
return "blocking reply", nil
|
|
}, "telegram")
|
|
|
|
var deltas []string
|
|
got, err := r.RouteStream(context.Background(), Message{Text: "hello"}, DeltaFunc(func(d string) {
|
|
deltas = append(deltas, d)
|
|
}))
|
|
if err != nil {
|
|
t.Fatalf("RouteStream: %v", err)
|
|
}
|
|
if got != "blocking reply" {
|
|
t.Errorf("reply = %q, want %q", got, "blocking reply")
|
|
}
|
|
if len(deltas) != 0 {
|
|
t.Errorf("expected no deltas without a stream responder, got %v", deltas)
|
|
}
|
|
}
|
|
|
|
func TestRouteStreamStreamsFreeText(t *testing.T) {
|
|
r := NewRouter(nil, nil, "telegram")
|
|
r.LLMStream = func(_ context.Context, _ Message, stream TurnStream) (string, error) {
|
|
for _, d := range []string{"a", "b", "c"} {
|
|
stream.Delta(d)
|
|
}
|
|
return "abc", nil
|
|
}
|
|
|
|
var sb strings.Builder
|
|
got, err := r.RouteStream(context.Background(), Message{Text: "hello"}, DeltaFunc(func(d string) {
|
|
sb.WriteString(d)
|
|
}))
|
|
if err != nil {
|
|
t.Fatalf("RouteStream: %v", err)
|
|
}
|
|
if got != "abc" || sb.String() != "abc" {
|
|
t.Errorf("reply=%q deltas=%q, want both %q", got, sb.String(), "abc")
|
|
}
|
|
}
|
|
|
|
// Local commands answer from local state in one step, so they must not be
|
|
// sent to the streaming responder.
|
|
func TestRouteStreamDoesNotStreamLocalCommands(t *testing.T) {
|
|
r := NewRouter(nil, nil, "telegram")
|
|
streamed := false
|
|
r.LLMStream = func(context.Context, Message, TurnStream) (string, error) {
|
|
streamed = true
|
|
return "", nil
|
|
}
|
|
|
|
if _, err := r.RouteStream(context.Background(), Message{Text: "/status"}, DeltaFunc(func(string) {})); err != nil {
|
|
t.Fatalf("RouteStream: %v", err)
|
|
}
|
|
if streamed {
|
|
t.Error("/status must not be routed to the streaming responder")
|
|
}
|
|
}
|
|
|
|
func TestRouteStreamRejectsUnknownCommandLocally(t *testing.T) {
|
|
r := NewRouter(nil, nil, "telegram")
|
|
streamed := false
|
|
r.LLMStream = func(context.Context, Message, TurnStream) (string, error) {
|
|
streamed = true
|
|
return "ok", nil
|
|
}
|
|
|
|
reply, err := r.RouteStream(context.Background(), Message{Text: "/commands"}, DeltaFunc(func(string) {}))
|
|
if err != nil {
|
|
t.Fatalf("RouteStream: %v", err)
|
|
}
|
|
if streamed {
|
|
t.Error("unknown /commands must not reach the streaming responder")
|
|
}
|
|
if !strings.Contains(reply, "/help") {
|
|
t.Fatalf("reply = %q, want /help guidance", reply)
|
|
}
|
|
}
|