fix(chat): harden inline tool call streaming

Telegram streaming callbacks now enqueue cancellable rendering rather than performing network I/O while draining the authoritative model stream. Final edit failures fall back to normal split delivery.

Completed tool events now correlate inputs by call ID and expose only bounded, conservative parameter shapes. Tool-call visibility is snapshotted per reply so reloads cannot change an in-flight turn.
This commit is contained in:
sam committed 2026-08-14 04:52:48 +10:00
1 parent 3fd672c999
commit 0bbd6e75de
12 files changed
+537 -62

No files matched your search

+47 -3
View File
@@ -1,6 +1,7 @@
package main
import (
"strings"
"testing"
"github.com/samcharles93/ai-sdk/core"
@@ -18,7 +19,7 @@ func (s *recordingStream) Delta(text string) {
}
func (s *recordingStream) ToolCall(event gateway.ToolCallEvent) {
s.events = append(s.events, "tool:"+event.Name+":"+event.Summary())
s.events = append(s.events, "tool:"+event.Name+":"+event.Parameters+":"+event.Summary())
}
func streamOf(parts ...core.StreamPart) <-chan core.StreamPart {
@@ -55,14 +56,14 @@ func TestDrainChatStream(t *testing.T) {
{Type: core.StreamPartTextDelta, TextDelta: " done"},
},
wantText: "checking done",
wantEvents: []string{"text:checking", "tool:shell:exit 0", "text: done"},
wantEvents: []string{"text:checking", `tool:shell:{"cmd":"[string]"}:exit 0`, "text: done"},
},
{
name: "a failed tool is reported as a failure, not silence",
parts: []core.StreamPart{
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolName: "shell", Error: "exit status 2"}},
},
wantEvents: []string{"tool:shell:failed: exit status 2"},
wantEvents: []string{"tool:shell::failed: exit status 2"},
},
{
// The tool call part carries no outcome yet: reporting it as
@@ -107,6 +108,49 @@ func TestDrainChatStream(t *testing.T) {
}
}
func TestDrainChatStreamCorrelatesBoundsAndRedactsToolParameters(t *testing.T) {
secret := strings.Repeat("s", 200)
sink := &recordingStream{}
drainChatStream(streamOf(
core.StreamPart{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{
ToolCallID: "call-2",
ToolName: "shell",
Input: `{"cmd":"curl -H 'Authorization: Bearer ` + secret + `'","nested":{"api_token":"` + secret + `"}}`,
}},
core.StreamPart{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{
ToolCallID: "call-1",
ToolName: "read",
Input: `{"path":"/tmp/one"}`,
}},
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
ToolCallID: "call-1",
ToolName: "read",
Output: "one",
}},
core.StreamPart{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
ToolCallID: "call-2",
ToolName: "shell",
Output: "ok",
}},
), sink)
if len(sink.events) != 2 {
t.Fatalf("events = %v, want two completed calls", sink.events)
}
if sink.events[0] != `tool:read:{"path":"[string]"}:one` {
t.Fatalf("first event = %q, want parameters correlated by call ID", sink.events[0])
}
if strings.Contains(sink.events[1], secret) {
t.Fatalf("second event exposed secret parameters: %q", sink.events[1])
}
if !strings.Contains(sink.events[1], `"api_token":"[redacted]"`) {
t.Fatalf("second event = %q, want redacted token", sink.events[1])
}
if len([]rune(sink.events[1])) > 180 {
t.Fatalf("second event is %d runes, want bounded parameters", len([]rune(sink.events[1])))
}
}
// The stream must be drained to close even when nobody is rendering it:
// ai-sdk writes to FullStream synchronously, so an abandoned stream stalls
// the generating goroutine.
+28 -6
View File
@@ -86,7 +86,7 @@ func setupTelegramGateway(ctx context.Context, s telegramSetup) (start func(), o
configureTelegramUpdates(tg, s)
}
tg.Dangerous = s.Dangerous
tg.ShowToolCalls = cfg.Chat.Telegram.ShowToolCalls
tg.SetShowToolCalls(cfg.Chat.Telegram.ShowToolCalls)
tg.ReleaseAnnouncements = &releaseannounce.Announcer{
StatePath: releaseAnnouncementStatePath(cfg.WorkDir, cfg.BotUser),
Components: []releaseannounce.Component{
@@ -156,10 +156,10 @@ func makeTelegramReload(s telegramSetup) func(*telegram.Gateway) error {
}
g.Token = token
g.AllowedUserIDs = newCfg.Chat.Telegram.AllowedUserIDs
g.ShowToolCalls = newCfg.Chat.Telegram.ShowToolCalls
g.SetShowToolCalls(newCfg.Chat.Telegram.ShowToolCalls)
s.Log.Info("chat gateway config reloaded",
"allowed_user_ids", len(g.AllowedUserIDs),
"show_tool_calls", g.ShowToolCalls)
"show_tool_calls", g.ShowToolCalls())
return nil
}
}
@@ -266,7 +266,13 @@ func sendChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, o
// 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 {
type pendingToolCall struct {
name string
parameters string
}
var sb strings.Builder
pending := make(map[string]pendingToolCall)
for part := range parts {
switch part.Type {
case core.StreamPartTextDelta:
@@ -277,14 +283,30 @@ func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream) stri
if turn != nil {
turn.Delta(part.TextDelta)
}
case core.StreamPartToolCall:
if part.ToolCall == nil || part.ToolCall.ToolCallID == "" {
continue
}
pending[part.ToolCall.ToolCallID] = pendingToolCall{
name: part.ToolCall.ToolName,
parameters: gateway.SummarizeToolParameters(part.ToolCall.Input),
}
case core.StreamPartToolResult:
if part.ToolResult == nil || turn == nil {
continue
}
call := pending[part.ToolResult.ToolCallID]
delete(pending, part.ToolResult.ToolCallID)
name := part.ToolResult.ToolName
if name == "" {
name = call.name
}
turn.ToolCall(gateway.ToolCallEvent{
Name: part.ToolResult.ToolName,
Output: part.ToolResult.Output,
Err: part.ToolResult.Error,
ID: part.ToolResult.ToolCallID,
Name: name,
Parameters: call.parameters,
Output: part.ToolResult.Output,
Err: part.ToolResult.Error,
})
}
}