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
90 lines
3.1 KiB
Go
90 lines
3.1 KiB
Go
package gateway
|
|
|
|
import "strings"
|
|
|
|
// toolSummaryMaxRunes bounds a rendered tool summary. Tool output is
|
|
// unbounded -- a grep can return thousands of lines -- and the summary is
|
|
// one line in a chat transcript, so it is cut rather than allowed to bury the
|
|
// answer it precedes.
|
|
const toolSummaryMaxRunes = 80
|
|
|
|
// ToolCallEvent reports one completed tool invocation within a turn. It
|
|
// carries the raw outcome rather than rendered text: presentation (emoji,
|
|
// layout, whether to show it at all) belongs to the channel adapter.
|
|
type ToolCallEvent struct {
|
|
// Name is the tool the model invoked.
|
|
Name string
|
|
// Output is what the tool returned, verbatim.
|
|
Output string
|
|
// Err is non-empty when the tool failed, in which case Output is
|
|
// unreliable and usually empty.
|
|
Err string
|
|
}
|
|
|
|
// Summary reduces the outcome to a single short line suitable for an inline
|
|
// status entry. It never returns an empty string: a tool that returned
|
|
// nothing still ran, and a blank summary would read as a rendering fault.
|
|
func (e ToolCallEvent) Summary() string {
|
|
if line := firstNonEmptyLine(e.Err); line != "" {
|
|
return "failed: " + truncateRunes(line, toolSummaryMaxRunes)
|
|
}
|
|
if line := firstNonEmptyLine(e.Output); line != "" {
|
|
return truncateRunes(line, toolSummaryMaxRunes)
|
|
}
|
|
return "done"
|
|
}
|
|
|
|
// TurnStream receives a turn's output as it is produced.
|
|
//
|
|
// It replaces the plain delta callback so a channel can render tool activity
|
|
// in the same ordered pass as the text: both arrive on the generating
|
|
// goroutine, in the order the model produced them. Implementations must not
|
|
// block -- a slow renderer stalls generation -- and must be safe to call
|
|
// from that goroutine only.
|
|
//
|
|
// Callers that render text alone use DeltaFunc rather than implementing this.
|
|
type TurnStream interface {
|
|
// Delta appends the next fragment of assistant text.
|
|
Delta(text string)
|
|
// ToolCall reports a tool invocation that has finished executing.
|
|
ToolCall(event ToolCallEvent)
|
|
}
|
|
|
|
// DeltaFunc adapts a plain text-delta callback to TurnStream for callers that
|
|
// render text only. Tool events are discarded. A nil DeltaFunc is inert, so
|
|
// DeltaFunc(nil) is a usable "stream nothing" sink.
|
|
type DeltaFunc func(string)
|
|
|
|
// Delta forwards the fragment to the wrapped callback.
|
|
func (f DeltaFunc) Delta(text string) {
|
|
if f == nil {
|
|
return
|
|
}
|
|
f(text)
|
|
}
|
|
|
|
// ToolCall discards the event: a text-only caller has nowhere to put it.
|
|
func (f DeltaFunc) ToolCall(ToolCallEvent) {}
|
|
|
|
// firstNonEmptyLine returns the first line of s that holds something other
|
|
// than whitespace, trimmed. It returns "" when there is no such line.
|
|
func firstNonEmptyLine(s string) string {
|
|
for line := range strings.SplitSeq(s, "\n") {
|
|
if trimmed := strings.TrimSpace(line); trimmed != "" {
|
|
return trimmed
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// truncateRunes shortens s to at most maxRunes runes, marking the cut with an
|
|
// ellipsis. Counting runes rather than bytes keeps multi-byte text from being
|
|
// cut mid-character.
|
|
func truncateRunes(s string, maxRunes int) string {
|
|
runes := []rune(s)
|
|
if len(runes) <= maxRunes {
|
|
return s
|
|
}
|
|
return string(runes[:maxRunes]) + "…"
|
|
}
|