Files
archie/internal/gateway/messageevent.go
T
sam e5e0316475 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.
2026-08-25 01:43:15 +10:00

139 lines
5.9 KiB
Go

package gateway
// MediaAttachment describes a file attached to a message. Platform-agnostic;
// platform-specific fields live in Raw.
//
// Numeric fields use pointers so that zero is distinguishable from
// "unknown" across a JSON round-trip: a 0-byte file is valid and
// different from a file whose size was never set.
type MediaAttachment struct {
// Type is "image", "video", "audio", or "document".
Type string `json:"type"`
// FileID is the platform-assigned identifier for download APIs.
FileID string `json:"file_id"`
// URL is a direct download URL when available. May be empty for
// platforms that require a download API call.
URL string `json:"url,omitempty"`
// Path is a local filesystem path on the host that produced the
// attachment. It is the alternative to URL for outbound delivery: a
// URL is fetched by the platform, a Path is uploaded by us. A file the
// agent wrote has no URL and never gets one -- publishing it would
// trade a delivery problem for a hosting and access-control one -- so
// senders that support upload must read this.
//
// Exactly one of URL and Path is meaningful for an outbound send.
Path string `json:"path,omitempty"`
// MIMEType is the content type (e.g. "image/png", "audio/ogg").
MIMEType string `json:"mime_type,omitempty"`
// FileName is the original file name when known.
FileName string `json:"file_name,omitempty"`
// FileSize is the attachment size in bytes. nil means unknown.
FileSize *int64 `json:"file_size,omitempty"`
// Width is the pixel width for images and video. nil means unknown
// or not applicable.
Width *int `json:"width,omitempty"`
// Height is the pixel height for images and video.
Height *int `json:"height,omitempty"`
// Duration is the play time in seconds for audio and video.
Duration *int `json:"duration,omitempty"`
}
// MessageEvent is the rich message model for all gateway channels. It
// extends the 3-field Message struct for new adapters while
// remaining backward-compatible via the Text() and FromID() helpers.
//
// Every field except Type is optional. A minimal text message needs
// only Type=MsgText, Text, ChannelID, Platform, and SenderID.
type MessageEvent struct {
// Type discriminates the message kind.
Type MessageType `json:"type"`
// Text is the human-readable message body. It carries the caption
// for media messages and the full content for text and system
// messages.
Text string `json:"text,omitempty"`
// ── Identifiers ──────────────────────────────────────────────
// ID is the platform-assigned message identifier. Used for reply
// threading, reactions, and edit/delete tracking.
ID string `json:"id,omitempty"`
// ChannelID identifies the conversation within the platform
// (chat ID, channel name, DM thread).
ChannelID string `json:"channel_id"`
// Platform names the source adapter ("telegram", "discord",
// "web", "slack").
Platform string `json:"platform"`
// ── Sender ───────────────────────────────────────────────────
// SenderID is the platform-specific sender identifier.
SenderID string `json:"sender_id"`
// SenderName is the display name when available.
SenderName string `json:"sender_name,omitempty"`
// ── Threading ────────────────────────────────────────────────
// ReplyToID is the platform message ID this message is in reply
// to. Empty for top-level messages.
ReplyToID string `json:"reply_to_id,omitempty"`
// ThreadID identifies the conversation thread for platforms that
// support threading (Slack, Discord forums).
ThreadID string `json:"thread_id,omitempty"`
// ── Media ────────────────────────────────────────────────────
// Media holds attached files. Nil for text-only messages.
Media []MediaAttachment `json:"media,omitempty"`
// ── Edit tracking ────────────────────────────────────────────
// EditedFrom is the original text before the edit. Set only when
// Type is MsgEdited.
EditedFrom string `json:"edited_from,omitempty"`
// ── Reactions ────────────────────────────────────────────────
// Reaction is the emoji used when Type is MsgReaction.
Reaction string `json:"reaction,omitempty"`
// ReactedToID is the message being reacted to.
ReactedToID string `json:"reacted_to_id,omitempty"`
// ── Callbacks ────────────────────────────────────────────────
// CallbackData is the payload from a button or inline-keyboard
// press (Type == MsgCallback).
CallbackData string `json:"callback_data,omitempty"`
// ── Timestamp ────────────────────────────────────────────────
// Timestamp is the platform-assigned message time in Unix
// milliseconds. Zero when unknown.
Timestamp int64 `json:"timestamp,omitempty"`
// ── Platform payload ─────────────────────────────────────────
// Raw is the original platform-specific payload. Adapters set
// this for debug logging or handlers that need platform details
// not captured by the generic fields.
Raw map[string]any `json:"raw,omitempty"`
}
// FromID returns the sender identifier. Backward-compatible alias for
// code that uses Message.From.
func (e MessageEvent) FromID() string { return e.SenderID }