Files
pipelines/internal/pipeline/chat_prompt_queue.go
T
alan 1ea1e5b73c Schedule chat prompts: send_at holds a queued prompt until its time
A chat_prompt with a future send_at (epoch ms) is always queued, durable in
chat_prompt_queue (new send_at column, added to older databases on Open),
listed with send_at in chat_queue frames, and delivered by the room's runner
once the time passes, waking on a timer. The queue stays FIFO: prompts queued
after a scheduled one wait behind it. A past or zero send_at behaves as before;
chat_unqueue cancels as for any queued prompt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 00:51:06 +00:00

190 lines
6.5 KiB
Go

package pipeline
import (
"context"
"database/sql"
"errors"
"time"
)
// ErrChatQueueFull refuses a prompt that would exceed a session's queue bound.
var ErrChatQueueFull = errors.New("chat prompt queue is full")
// ChatQueuedPrompt is one durable prompt waiting for its session's harness to
// finish the turn in flight. Files and Parts are the server-prepared attachment
// references (JSON) the prompt carries; the bytes stay in the attachment store.
type ChatQueuedPrompt struct {
ID int64
SessionID string
OperationID string
MessageID string
NativeID string
Text string
Client string
FilesJSON string
PartsJSON string
CreatedAt time.Time
// SendAt is when a scheduled prompt becomes deliverable; zero means as
// soon as the session is free.
SendAt time.Time
}
// initChatPromptQueue creates the additive queue table. A prompt belongs to
// its chat session, not to a connection: rows are written when a prompt is
// queued behind a running turn and deleted once it is delivered or removed.
// (session_id, operation_id) is unique so a client retry never enqueues twice.
func (s *Store) initChatPromptQueue() error {
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_prompt_queue (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
operation_id TEXT NOT NULL,
message_id TEXT NOT NULL,
native_message_id TEXT NOT NULL DEFAULT '',
text TEXT NOT NULL,
client TEXT NOT NULL DEFAULT '',
files_json TEXT NOT NULL DEFAULT '[]',
parts_json TEXT NOT NULL DEFAULT '[]',
created_at INTEGER NOT NULL,
send_at INTEGER NOT NULL DEFAULT 0,
UNIQUE(session_id, operation_id));
CREATE INDEX IF NOT EXISTS chat_prompt_queue_session ON chat_prompt_queue(session_id, id);`)
if err != nil {
return err
}
// send_at (epoch ms, 0 = unscheduled) arrived after the table; an older
// database gains it with every existing row unscheduled.
return s.addColumnIfMissing("chat_prompt_queue", "send_at", "INTEGER NOT NULL DEFAULT 0")
}
// EnqueueChatPrompt appends prompt to its session's queue. When the session
// already holds the same operation id the existing row is returned with
// created false and nothing is written. A new row that would exceed limit is
// refused with ErrChatQueueFull.
func (s *Store) EnqueueChatPrompt(ctx context.Context, prompt ChatQueuedPrompt, limit int) (ChatQueuedPrompt, bool, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return ChatQueuedPrompt{}, false, err
}
defer tx.Rollback()
existing, err := scanChatQueuedPrompt(tx.QueryRowContext(ctx, chatQueueSelect+` WHERE session_id = ? AND operation_id = ?`, prompt.SessionID, prompt.OperationID))
if err == nil {
return existing, false, nil
}
if !errors.Is(err, sql.ErrNoRows) {
return ChatQueuedPrompt{}, false, err
}
var count int
if err := tx.QueryRowContext(ctx, `SELECT COUNT(*) FROM chat_prompt_queue WHERE session_id = ?`, prompt.SessionID).Scan(&count); err != nil {
return ChatQueuedPrompt{}, false, err
}
if limit > 0 && count >= limit {
return ChatQueuedPrompt{}, false, ErrChatQueueFull
}
if prompt.CreatedAt.IsZero() {
prompt.CreatedAt = time.Now()
}
if prompt.FilesJSON == "" {
prompt.FilesJSON = "[]"
}
if prompt.PartsJSON == "" {
prompt.PartsJSON = "[]"
}
result, err := tx.ExecContext(ctx, `INSERT INTO chat_prompt_queue
(session_id, operation_id, message_id, native_message_id, text, client, files_json, parts_json, created_at, send_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
prompt.SessionID, prompt.OperationID, prompt.MessageID, prompt.NativeID, prompt.Text, prompt.Client,
prompt.FilesJSON, prompt.PartsJSON, prompt.CreatedAt.UnixMilli(), unixMilliOrZero(prompt.SendAt))
if err != nil {
return ChatQueuedPrompt{}, false, err
}
if prompt.ID, err = result.LastInsertId(); err != nil {
return ChatQueuedPrompt{}, false, err
}
if err := tx.Commit(); err != nil {
return ChatQueuedPrompt{}, false, err
}
return prompt, true, nil
}
// DeleteChatQueuedPrompt removes one queued prompt and reports whether a row
// was removed.
func (s *Store) DeleteChatQueuedPrompt(ctx context.Context, sessionID, operationID string) (bool, error) {
result, err := s.db.ExecContext(ctx, `DELETE FROM chat_prompt_queue WHERE session_id = ? AND operation_id = ?`, sessionID, operationID)
if err != nil {
return false, err
}
n, err := result.RowsAffected()
return n > 0, err
}
// ClearChatQueuedPrompts drops every queued prompt of one session.
func (s *Store) ClearChatQueuedPrompts(ctx context.Context, sessionID string) error {
_, err := s.db.ExecContext(ctx, `DELETE FROM chat_prompt_queue WHERE session_id = ?`, sessionID)
return err
}
// ChatQueuedPrompts lists one session's queued prompts in delivery order.
func (s *Store) ChatQueuedPrompts(ctx context.Context, sessionID string) ([]ChatQueuedPrompt, error) {
rows, err := s.db.QueryContext(ctx, chatQueueSelect+` WHERE session_id = ? ORDER BY id`, sessionID)
if err != nil {
return nil, err
}
defer rows.Close()
var prompts []ChatQueuedPrompt
for rows.Next() {
prompt, err := scanChatQueuedPrompt(rows)
if err != nil {
return nil, err
}
prompts = append(prompts, prompt)
}
return prompts, rows.Err()
}
// ChatQueuedSessions lists the sessions holding queued prompts, oldest first,
// so a restarted server can resume their delivery.
func (s *Store) ChatQueuedSessions(ctx context.Context) ([]string, error) {
rows, err := s.db.QueryContext(ctx, `SELECT session_id FROM chat_prompt_queue GROUP BY session_id ORDER BY MIN(id)`)
if err != nil {
return nil, err
}
defer rows.Close()
var sessions []string
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
return nil, err
}
sessions = append(sessions, id)
}
return sessions, rows.Err()
}
const chatQueueSelect = `SELECT id, session_id, operation_id, message_id, native_message_id, text, client, files_json, parts_json, created_at, send_at FROM chat_prompt_queue`
type chatQueueScanner interface {
Scan(...any) error
}
func scanChatQueuedPrompt(row chatQueueScanner) (ChatQueuedPrompt, error) {
var prompt ChatQueuedPrompt
var created, sendAt int64
err := row.Scan(&prompt.ID, &prompt.SessionID, &prompt.OperationID, &prompt.MessageID, &prompt.NativeID,
&prompt.Text, &prompt.Client, &prompt.FilesJSON, &prompt.PartsJSON, &created, &sendAt)
if err != nil {
return ChatQueuedPrompt{}, err
}
prompt.CreatedAt = time.UnixMilli(created)
if sendAt > 0 {
prompt.SendAt = time.UnixMilli(sendAt)
}
return prompt, nil
}
func unixMilliOrZero(t time.Time) int64 {
if t.IsZero() {
return 0
}
return t.UnixMilli()
}