feat(telegram): send local files as uploads, not fetchable URLs

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.
This commit is contained in:
sam committed 2026-08-25 01:43:15 +10:00
1 parent bb1ab9682f
commit 0b2398f026
13 files changed
+839 -34

No files matched your search

+15
View File
@@ -54,6 +54,7 @@ import (
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
memorytoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/memory"
"github.com/samcharles93/archie-core/internal/tools/sendfile"
"github.com/samcharles93/archie-core/internal/tools/webfetch"
"github.com/samcharles93/archie-core/internal/webhookguard"
"github.com/samcharles93/archie-core/internal/webui"
@@ -889,6 +890,20 @@ func (b *boot) registerStandaloneTools() {
log.Info("web fetch disabled")
}
// send_file. Rooted at the same workspace as the file tools and gated
// by the same confinement, because it hands a host file to an outbound
// message: a send that could reach paths the read tool refuses would
// be a way around whatever confinement is configured.
if entry := sendfile.Tool(cfg.Chat.Workspace); entry != nil {
if err := b.toolReg.Register(*entry); err != nil {
log.Warn("send_file registration failed", "err", err)
} else {
log.Info("file sending enabled", "workspace", cfg.Chat.Workspace)
}
} else {
log.Info("file sending disabled (chat.workspace is unset)")
}
b.registerMinimaxTool(cfg, log)
}
+36 -2
View File
@@ -23,7 +23,9 @@ func (s *recordingStream) ToolCall(event gateway.ToolCallEvent) {
}
func (s *recordingStream) Media(event gateway.MediaEvent) {
s.events = append(s.events, "media:"+event.ToolName+":"+event.Attachment.Type)
// 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 {
@@ -106,9 +108,41 @@ func TestDrainChatStream(t *testing.T) {
},
wantEvents: []string{
`tool:generate_video::{"is_multimodal":true,"urls":[{"type":"video","url":"https://x/v"}]}`,
"media:generate_video:video",
"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
+14 -2
View File
@@ -387,9 +387,21 @@ func drainChatStream(parts <-chan core.StreamPart, turn gateway.TurnStream) stri
Err: part.ToolResult.Error,
})
for _, ref := range multimodalMediaRefs(part.ToolResult.Output) {
// Path and URL are alternatives, not a pair: the channel
// uploads one and fetches the other. A ref carrying
// neither names nothing deliverable, so it is dropped
// rather than sent as an empty attachment.
if ref.URL == "" && ref.Path == "" {
continue
}
turn.Media(gateway.MediaEvent{
ToolName: name,
Attachment: gateway.MediaAttachment{Type: ref.Type, URL: ref.URL},
ToolName: name,
Attachment: gateway.MediaAttachment{
Type: ref.Type,
URL: ref.URL,
Path: ref.Path,
FileName: ref.FileName,
},
})
}
}