feat(telegram): carry inbound media through the chat turn
Telegram dropped every incoming photo, document, video and audio message: the default handler required message text, and nothing on the turn path knew media existed. The receiving path now closes the gap end to end. - telegram: extract the attachment from the update (largest photo, plus document, video, video-note, audio and voice kinds), download its bytes on the turn lane through getFile + the Bot API file link with a 20 MB gate, submit the turn with the attachment, and answer unsupported content kinds (sticker, location, contact, poll) with a clear notice instead of silence - messaging: Inbound carries turn-scoped transport media and MediaAttachment a Data field for in-process bytes; Message gains Media attachment metadata which stores persist (Postgres jsonb via migration 0020, memory store) with Data stripped at the persist boundary, and pb.Message carries the metadata on the gateway wire - gateway: TurnModelRequest threads the turn's media to the model seam - archied: the chat turn model merges the attachments into the final user message as ai-sdk image/file part entries, so vision-capable models see the file for the turn it arrived in; stored history keeps the placeholder note plus caption Session log: messages carry what was sent as data (the attachment metadata), not just prose. Transcript rendering of attachments and a re-look capability are tracked as archie-core-jaba.
This commit is contained in:
22 files changed
+1355
-172
No files matched your search
@@ -0,0 +1,9 @@
|
||||
-- +goose Up
|
||||
-- Attachment metadata on stored chat messages (messaging.Message.Media):
|
||||
-- type, platform attachment id, name, mime, size. The attachment bytes are
|
||||
-- deliberately not kept -- they are turn-scoped -- so this is a JSON
|
||||
-- metadata list, not a blob column.
|
||||
ALTER TABLE messages ADD COLUMN media jsonb NOT NULL DEFAULT '[]';
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE messages DROP COLUMN media;
|
||||
@@ -110,8 +110,8 @@ func (q *Queries) FinalizeTurn(ctx context.Context, arg FinalizeTurnParams) (int
|
||||
}
|
||||
|
||||
const insertMessageAt = `-- name: InsertMessageAt :exec
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, ts)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, media, ts)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
ON CONFLICT (session_id, message_id) DO NOTHING
|
||||
`
|
||||
|
||||
@@ -123,6 +123,7 @@ type InsertMessageAtParams struct {
|
||||
SenderID string
|
||||
Role string
|
||||
Text string
|
||||
Media []byte
|
||||
Ts int64
|
||||
}
|
||||
|
||||
@@ -136,17 +137,18 @@ func (q *Queries) InsertMessageAt(ctx context.Context, arg InsertMessageAtParams
|
||||
arg.SenderID,
|
||||
arg.Role,
|
||||
arg.Text,
|
||||
arg.Media,
|
||||
arg.Ts,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const insertMessageClamped = `-- name: InsertMessageClamped :exec
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, ts)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, (
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, media, ts)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, (
|
||||
SELECT CASE
|
||||
WHEN MAX(m.ts) IS NOT NULL AND MAX(m.ts) >= $8 THEN MAX(m.ts) + 1
|
||||
ELSE $8
|
||||
WHEN MAX(m.ts) IS NOT NULL AND MAX(m.ts) >= $9 THEN MAX(m.ts) + 1
|
||||
ELSE $9
|
||||
END
|
||||
FROM messages m WHERE m.session_id = $2
|
||||
))
|
||||
@@ -161,6 +163,7 @@ type InsertMessageClampedParams struct {
|
||||
SenderID string
|
||||
Role string
|
||||
Text string
|
||||
Media []byte
|
||||
Ts int64
|
||||
}
|
||||
|
||||
@@ -177,6 +180,7 @@ func (q *Queries) InsertMessageClamped(ctx context.Context, arg InsertMessageCla
|
||||
arg.SenderID,
|
||||
arg.Role,
|
||||
arg.Text,
|
||||
arg.Media,
|
||||
arg.Ts,
|
||||
)
|
||||
return err
|
||||
@@ -365,10 +369,10 @@ func (q *Queries) NextReplyAfterTs(ctx context.Context, arg NextReplyAfterTsPara
|
||||
}
|
||||
|
||||
const recentMessages = `-- name: RecentMessages :many
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts,
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts, m.media::text AS media,
|
||||
COALESCE(s.channel_id, '') AS channel_id, COALESCE(s.thread_id, '') AS thread_id
|
||||
FROM (
|
||||
SELECT msg.message_id, msg.source_id, msg.sender, msg.sender_id, msg.role, msg.text, msg.ts, msg.session_id
|
||||
SELECT msg.message_id, msg.source_id, msg.sender, msg.sender_id, msg.role, msg.text, msg.ts, msg.media::text AS media, msg.session_id
|
||||
FROM messages msg WHERE msg.session_id = $1 ORDER BY msg.ts DESC LIMIT $2
|
||||
) m
|
||||
LEFT JOIN sessions s ON s.session_id = m.session_id
|
||||
@@ -388,6 +392,7 @@ type RecentMessagesRow struct {
|
||||
Role string
|
||||
Text string
|
||||
Ts int64
|
||||
Media string
|
||||
ChannelID string
|
||||
ThreadID string
|
||||
}
|
||||
@@ -409,6 +414,7 @@ func (q *Queries) RecentMessages(ctx context.Context, arg RecentMessagesParams)
|
||||
&i.Role,
|
||||
&i.Text,
|
||||
&i.Ts,
|
||||
&i.Media,
|
||||
&i.ChannelID,
|
||||
&i.ThreadID,
|
||||
); err != nil {
|
||||
@@ -633,7 +639,7 @@ func (q *Queries) SearchMessagesCount(ctx context.Context, arg SearchMessagesCou
|
||||
}
|
||||
|
||||
const searchMessagesPage = `-- name: SearchMessagesPage :many
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts,
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts, m.media::text AS media,
|
||||
COALESCE(s.channel_id, '') AS channel_id, COALESCE(s.thread_id, '') AS thread_id
|
||||
FROM messages m
|
||||
LEFT JOIN sessions s ON s.session_id = m.session_id
|
||||
@@ -657,6 +663,7 @@ type SearchMessagesPageRow struct {
|
||||
Role string
|
||||
Text string
|
||||
Ts int64
|
||||
Media string
|
||||
ChannelID string
|
||||
ThreadID string
|
||||
}
|
||||
@@ -683,6 +690,7 @@ func (q *Queries) SearchMessagesPage(ctx context.Context, arg SearchMessagesPage
|
||||
&i.Role,
|
||||
&i.Text,
|
||||
&i.Ts,
|
||||
&i.Media,
|
||||
&i.ChannelID,
|
||||
&i.ThreadID,
|
||||
); err != nil {
|
||||
|
||||
@@ -240,6 +240,7 @@ type Message struct {
|
||||
Text string
|
||||
Ts int64
|
||||
Search interface{}
|
||||
Media []byte
|
||||
}
|
||||
|
||||
type Org struct {
|
||||
|
||||
@@ -61,30 +61,30 @@ SELECT pg_advisory_xact_lock(hashtextextended('messages:' || $1::text, 0));
|
||||
-- SQL, not in Go: the CASE assigns MAX(ts)+1 when the caller's timestamp is
|
||||
-- not ahead of the session's newest, else the caller's timestamp. A Go-side
|
||||
-- read-then-write would race two concurrent appends to the same session.
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, ts)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, (
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, media, ts)
|
||||
VALUES (@message_id, @session_id, @source_id, @sender, @sender_id, @role, @text, @media, (
|
||||
SELECT CASE
|
||||
WHEN MAX(m.ts) IS NOT NULL AND MAX(m.ts) >= $8 THEN MAX(m.ts) + 1
|
||||
ELSE $8
|
||||
WHEN MAX(m.ts) IS NOT NULL AND MAX(m.ts) >= @ts THEN MAX(m.ts) + 1
|
||||
ELSE @ts
|
||||
END
|
||||
FROM messages m WHERE m.session_id = $2
|
||||
FROM messages m WHERE m.session_id = @session_id
|
||||
))
|
||||
ON CONFLICT (session_id, message_id) DO NOTHING;
|
||||
|
||||
-- name: InsertMessageAt :exec
|
||||
-- Rewriting history places records deliberately, so no clamp applies.
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, ts)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
INSERT INTO messages (message_id, session_id, source_id, sender, sender_id, role, text, media, ts)
|
||||
VALUES (@message_id, @session_id, @source_id, @sender, @sender_id, @role, @text, @media, @ts)
|
||||
ON CONFLICT (session_id, message_id) DO NOTHING;
|
||||
|
||||
-- name: MessageID :one
|
||||
SELECT message_id FROM messages WHERE session_id = $1 AND message_id = $2;
|
||||
|
||||
-- name: RecentMessages :many
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts,
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts, m.media::text AS media,
|
||||
COALESCE(s.channel_id, '') AS channel_id, COALESCE(s.thread_id, '') AS thread_id
|
||||
FROM (
|
||||
SELECT msg.message_id, msg.source_id, msg.sender, msg.sender_id, msg.role, msg.text, msg.ts, msg.session_id
|
||||
SELECT msg.message_id, msg.source_id, msg.sender, msg.sender_id, msg.role, msg.text, msg.ts, msg.media::text AS media, msg.session_id
|
||||
FROM messages msg WHERE msg.session_id = $1 ORDER BY msg.ts DESC LIMIT $2
|
||||
) m
|
||||
LEFT JOIN sessions s ON s.session_id = m.session_id
|
||||
@@ -106,7 +106,7 @@ SELECT COUNT(*) FROM messages
|
||||
WHERE session_id = $1 AND search @@ plainto_tsquery('simple', $2);
|
||||
|
||||
-- name: SearchMessagesPage :many
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts,
|
||||
SELECT m.message_id, m.source_id, m.sender, m.sender_id, m.role, m.text, m.ts, m.media::text AS media,
|
||||
COALESCE(s.channel_id, '') AS channel_id, COALESCE(s.thread_id, '') AS thread_id
|
||||
FROM messages m
|
||||
LEFT JOIN sessions s ON s.session_id = m.session_id
|
||||
|
||||
Reference in new issue
Block a user