Files
archie/internal/gateway/messageevent.go
T
sam e3b1a859a8 feat(gateway): flip channel boundary onto messaging.Message
Replaced gateway.Message as the currency at the channel-facing boundary
with the canonical internal/domain/messaging.Message. Channel adapters
now build the record directly (RoleUser, ConversationID, Sender) and the
Router hands it back unchanged, so nothing converts between two shapes in
process. gateway.Message is deleted; the dashboard route that had no home
in the record lives in a new Inbound envelope (Message + transport-only
Page), which reaches the system prompt and is never persisted.

Role is now set at construction instead of derived at the store boundary,
and the assistant reply is a direct messaging.Message (RoleAssistant,
Sender=botUser). The gRPC proto is unchanged: pb.Message still carries no
role, and both sides keep deriving it from the owning session's bot
identity via gateway.RoleForSender (ToStoredMessage/FromStoredMessage are
deleted).

Adds tests that the old shape could not express: Page reaches the system
prompt but not the persisted record, and a channel-built Inbound round-trips
with the Role the channel set.

Refs: archie-core-9g5e
2026-09-09 01:05:36 +10:00

140 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
// carries the platform detail an adapter needs beyond the persisted
// record, and keeps the Text() and FromID() helpers for callers that only
// want a sender and a body.
//
// 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, under the name callers that only
// need a sender use.
func (e MessageEvent) FromID() string { return e.SenderID }