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 } // 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, UNIQUE(session_id, operation_id)); CREATE INDEX IF NOT EXISTS chat_prompt_queue_session ON chat_prompt_queue(session_id, id);`) return err } // 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) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, prompt.SessionID, prompt.OperationID, prompt.MessageID, prompt.NativeID, prompt.Text, prompt.Client, prompt.FilesJSON, prompt.PartsJSON, prompt.CreatedAt.UnixMilli()) 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 FROM chat_prompt_queue` type chatQueueScanner interface { Scan(...any) error } func scanChatQueuedPrompt(row chatQueueScanner) (ChatQueuedPrompt, error) { var prompt ChatQueuedPrompt var created int64 err := row.Scan(&prompt.ID, &prompt.SessionID, &prompt.OperationID, &prompt.MessageID, &prompt.NativeID, &prompt.Text, &prompt.Client, &prompt.FilesJSON, &prompt.PartsJSON, &created) if err != nil { return ChatQueuedPrompt{}, err } prompt.CreatedAt = time.UnixMilli(created) return prompt, nil }