The agent could produce a file -- a transcript, a log dump, a screenshot --
and had no way to hand it over. Asked to send one it offered a "download"
link; nothing arrived and it had no signal that anything had failed. The
defect was not that it lied: success and silent non-delivery were
indistinguishable to it.
Three places made a local file undeliverable, and each is fixed here:
- liveReply.Media skipped any attachment with an empty URL, so a local
file was dropped before it ever reached a sender.
- telegramMediaSender built every attachment as models.InputFileString,
which tells Telegram to FETCH the string as a URL. A host path is not
fetchable, so the send did nothing while reporting success. It now
chooses by what the attachment carries: URL -> fetch, Path -> upload
via models.InputFileUpload over an open file.
- Nothing let the agent ask for a send at all. New send_file tool
(internal/tools/sendfile) validates the file and returns a
MultimodalResult naming it; the turn's channel delivers it.
Meaning changes, called out because the names did not:
- MediaAttachment gains Path, the alternative to URL. Exactly one is
meaningful for an outbound send.
- tools.MultimodalResult.URLs used to mean remote-hosted media only; it
now means media this result wants delivered, which may be local. JSON
name unchanged so existing producers keep working.
- errNoURL is now errNoSource: having neither source, not having no URL,
is what makes an attachment undeliverable.
Failures are visible at every layer, which is the actual fix. send_file
errors on an absent, irregular, unreadable, oversized or confinement-
refused path, so the model sees it and can report it. The sender checks
Telegram's ceilings (10 MB photos, 50 MB otherwise) before the request and
reports the real size rather than leaving a 400 nobody reads. A delivery
that still fails now appends "could not send <name>" to the reply instead
of an empty link, which read as a delivered file.
Exposing a public URL for local files was explicitly rejected: it trades a
missing feature for a hosting and access-control problem.
send_file resolves paths through builtin.ResolveReadable -- the read tool's
own policy, exported rather than reimplemented -- so sending cannot reach
what reading cannot, whatever [chat] unrestricted_filesystem is set to.
240 lines
8.4 KiB
Go
240 lines
8.4 KiB
Go
package main
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
if text != "ab" {
|
|
t.Fatalf("text = %q, want %q", text, "ab")
|
|
}
|
|
}
|