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).
274 lines
9.7 KiB
Go
274 lines
9.7 KiB
Go
package archied
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/ai-sdk/core"
|
|
|
|
"github.com/samcharles93/archie-core/internal/gateway"
|
|
)
|
|
|
|
// recordingStream captures the turn's reported progress in arrival order.
|
|
type recordingStream struct {
|
|
events []string
|
|
}
|
|
|
|
func (s *recordingStream) Delta(text string) {
|
|
s.events = append(s.events, "text:"+text)
|
|
}
|
|
|
|
func (s *recordingStream) ToolCall(event gateway.ToolCallEvent) {
|
|
s.events = append(s.events, "tool:"+event.Name+":"+event.Parameters+":"+event.Summary())
|
|
}
|
|
|
|
func (s *recordingStream) Media(event gateway.MediaEvent) {
|
|
// Path is recorded alongside the type: a local attachment that lost
|
|
// its path on the way through would still look like a media event.
|
|
s.events = append(s.events, "media:"+event.ToolName+":"+event.Attachment.Type+":"+event.Attachment.Path)
|
|
}
|
|
|
|
func streamOf(parts ...core.StreamPart) <-chan core.StreamPart {
|
|
ch := make(chan core.StreamPart, len(parts))
|
|
for _, part := range parts {
|
|
ch <- part
|
|
}
|
|
close(ch)
|
|
return ch
|
|
}
|
|
|
|
func TestDrainChatStream(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
parts []core.StreamPart
|
|
wantText string
|
|
wantEvents []string
|
|
}{
|
|
{
|
|
name: "text only",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartTextDelta, TextDelta: "hello "},
|
|
{Type: core.StreamPartTextDelta, TextDelta: "world"},
|
|
},
|
|
wantText: "hello world",
|
|
wantEvents: []string{"text:hello ", "text:world"},
|
|
},
|
|
{
|
|
name: "a tool result is reported between the text around it",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartTextDelta, TextDelta: "checking"},
|
|
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "shell", Input: `{"cmd":"true"}`}},
|
|
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{ToolCallID: "1", ToolName: "shell", Output: "exit 0"}},
|
|
{Type: core.StreamPartTextDelta, TextDelta: " done"},
|
|
},
|
|
wantText: "checking 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"},
|
|
},
|
|
{
|
|
// The tool call part carries no outcome yet: reporting it as
|
|
// well would show every tool twice.
|
|
name: "the tool call part alone reports nothing",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolName: "shell"}},
|
|
},
|
|
wantEvents: nil,
|
|
},
|
|
{
|
|
name: "parts with nothing to render are skipped",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartStartStep},
|
|
{Type: core.StreamPartTextDelta, TextDelta: ""},
|
|
{Type: core.StreamPartReasoningDelta, ReasoningDelta: "thinking"},
|
|
{Type: core.StreamPartToolResult},
|
|
{Type: core.StreamPartFinishStep},
|
|
{Type: core.StreamPartFinish},
|
|
},
|
|
wantEvents: nil,
|
|
},
|
|
{
|
|
// A tool's output carrying a tools.MultimodalResult envelope
|
|
// must report both the ordinary tool-call event (so the turn
|
|
// narrates what ran) and a Media event per URL, so the
|
|
// channel has something to actually deliver.
|
|
name: "a tool result carrying MultimodalResult URLs reports Media",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "generate_video"}},
|
|
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
|
ToolCallID: "1",
|
|
ToolName: "generate_video",
|
|
Output: `{"is_multimodal":true,"urls":[{"type":"video","url":"https://x/v"}]}`,
|
|
}},
|
|
},
|
|
wantEvents: []string{
|
|
`tool:generate_video::{"is_multimodal":true,"urls":[{"type":"video","url":"https://x/v"}]}`,
|
|
"media:generate_video:video:",
|
|
},
|
|
},
|
|
{
|
|
// A local file has no URL and must not acquire one: the
|
|
// channel uploads it. Losing Path here is exactly the
|
|
// silent non-delivery send_file exists to end.
|
|
name: "a tool result carrying a local path reports Media with that path",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "send_file"}},
|
|
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
|
ToolCallID: "1",
|
|
ToolName: "send_file",
|
|
Output: `{"is_multimodal":true,"urls":[{"type":"document","path":"/tmp/a.md"}]}`,
|
|
}},
|
|
},
|
|
wantEvents: []string{
|
|
"tool:send_file::" + `{"is_multimodal":true,"urls":[{"type":"document","path":"/tmp/a.md"}]}`,
|
|
"media:send_file:document:/tmp/a.md",
|
|
},
|
|
},
|
|
{
|
|
// A ref naming neither a URL nor a path is not deliverable,
|
|
// and an empty attachment would render as a delivered file.
|
|
name: "a media ref with no source reports no Media event",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "odd_tool"}},
|
|
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
|
ToolCallID: "1",
|
|
ToolName: "odd_tool",
|
|
Output: `{"is_multimodal":true,"urls":[{"type":"document"}]}`,
|
|
}},
|
|
},
|
|
wantEvents: []string{"tool:odd_tool::" + `{"is_multimodal":true,"urls":[{"type":"document"}]}`},
|
|
},
|
|
{
|
|
// An ordinary JSON tool result that happens to be a JSON
|
|
// object but isn't a MultimodalResult (no is_multimodal, or
|
|
// false) must not be misread as carrying media.
|
|
name: "a plain JSON tool result reports no Media event",
|
|
parts: []core.StreamPart{
|
|
{Type: core.StreamPartToolCall, ToolCall: &core.ToolCall{ToolCallID: "1", ToolName: "read_file"}},
|
|
{Type: core.StreamPartToolResult, ToolResult: &core.ToolResult{
|
|
ToolCallID: "1",
|
|
ToolName: "read_file",
|
|
Output: `{"path":"a.txt","content":"hello"}`,
|
|
}},
|
|
},
|
|
wantEvents: []string{`tool:read_file::{"path":"a.txt","content":"hello"}`},
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
sink := &recordingStream{}
|
|
text := drainChatStream(streamOf(tc.parts...), sink, nil)
|
|
|
|
if text != tc.wantText {
|
|
t.Fatalf("text = %q, want %q", text, tc.wantText)
|
|
}
|
|
if len(sink.events) != len(tc.wantEvents) {
|
|
t.Fatalf("events = %v, want %v", sink.events, tc.wantEvents)
|
|
}
|
|
for i := range tc.wantEvents {
|
|
if sink.events[i] != tc.wantEvents[i] {
|
|
t.Fatalf("events = %v, want %v", sink.events, tc.wantEvents)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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, nil)
|
|
|
|
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.
|
|
func TestDrainChatStreamWithoutASinkStillCollectsText(t *testing.T) {
|
|
text := drainChatStream(streamOf(
|
|
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)
|
|
|
|
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) {}
|