160 lines
5.1 KiB
Go
160 lines
5.1 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// The chat tool tables: the MCP endpoint tokens a harness registers (each
|
|
// names the scope it serves: "session:<id>", "pending:" until a Codex thread
|
|
// id is bound, or "instance:<harness>") and the items a chat's model showed.
|
|
func (s *Store) initChatTools() error {
|
|
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_tool_tokens (
|
|
token TEXT PRIMARY KEY,
|
|
scope TEXT NOT NULL,
|
|
created_at INTEGER NOT NULL);
|
|
CREATE INDEX IF NOT EXISTS chat_tool_tokens_scope ON chat_tool_tokens(scope);
|
|
CREATE TABLE IF NOT EXISTS chat_shown_items (
|
|
id TEXT PRIMARY KEY,
|
|
session_id TEXT NOT NULL,
|
|
created_at INTEGER NOT NULL,
|
|
payload TEXT NOT NULL);
|
|
CREATE INDEX IF NOT EXISTS chat_shown_items_session ON chat_shown_items(session_id, created_at);`)
|
|
return err
|
|
}
|
|
|
|
// ChatToolToken returns the token issued for scope, issuing one when none is.
|
|
func (s *Store) ChatToolToken(ctx context.Context, scope string) (string, error) {
|
|
var token string
|
|
err := s.db.QueryRowContext(ctx, `SELECT token FROM chat_tool_tokens WHERE scope=? ORDER BY created_at, token LIMIT 1`, scope).Scan(&token)
|
|
if err == nil {
|
|
return token, nil
|
|
}
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return "", err
|
|
}
|
|
return s.IssueChatToolToken(ctx, scope)
|
|
}
|
|
|
|
// IssueChatToolToken records a new random token for scope.
|
|
func (s *Store) IssueChatToolToken(ctx context.Context, scope string) (string, error) {
|
|
var secret [32]byte
|
|
if _, err := rand.Read(secret[:]); err != nil {
|
|
return "", err
|
|
}
|
|
token := hex.EncodeToString(secret[:])
|
|
if _, err := s.db.ExecContext(ctx, `INSERT INTO chat_tool_tokens(token,scope,created_at) VALUES(?,?,?)`, token, scope, time.Now().UnixMilli()); err != nil {
|
|
return "", err
|
|
}
|
|
return token, nil
|
|
}
|
|
|
|
// ChatToolScope returns the scope a token serves (found false for an unknown
|
|
// token).
|
|
func (s *Store) ChatToolScope(ctx context.Context, token string) (string, bool, error) {
|
|
var scope string
|
|
err := s.db.QueryRowContext(ctx, `SELECT scope FROM chat_tool_tokens WHERE token=?`, token).Scan(&scope)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", false, nil
|
|
}
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
return scope, true, nil
|
|
}
|
|
|
|
// BindChatToolToken moves a token to scope (a pending Codex token to its thread).
|
|
func (s *Store) BindChatToolToken(ctx context.Context, token, scope string) error {
|
|
result, err := s.db.ExecContext(ctx, `UPDATE chat_tool_tokens SET scope=? WHERE token=?`, scope, token)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n, _ := result.RowsAffected(); n == 0 {
|
|
return errors.New("unknown chat tool token")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// AppendChatShownItem records one shown item and applies the retention bounds:
|
|
// at most protocol.ChatShownLimit items per chat, and HTML bodies only for the
|
|
// newest documents within protocol.ChatShownHTMLBudget.
|
|
func (s *Store) AppendChatShownItem(ctx context.Context, sessionID string, item protocol.ChatShownItem) error {
|
|
payload, err := json.Marshal(item)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if _, err := tx.ExecContext(ctx, `INSERT INTO chat_shown_items(id,session_id,created_at,payload) VALUES(?,?,?,?)`, item.ID, sessionID, item.CreatedAt, string(payload)); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `DELETE FROM chat_shown_items WHERE session_id=? AND id NOT IN (
|
|
SELECT id FROM chat_shown_items WHERE session_id=? ORDER BY created_at DESC, rowid DESC LIMIT ?)`, sessionID, sessionID, protocol.ChatShownLimit); err != nil {
|
|
return err
|
|
}
|
|
items, err := chatShownItems(ctx, tx, sessionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
budget := protocol.ChatShownHTMLBudget
|
|
for i := len(items) - 1; i >= 0; i-- {
|
|
current := items[i]
|
|
if current.Kind != protocol.ChatShownHTML || current.Expired {
|
|
continue
|
|
}
|
|
if len(current.HTML) <= budget {
|
|
budget -= len(current.HTML)
|
|
continue
|
|
}
|
|
budget = 0
|
|
current.HTML, current.Expired = "", true
|
|
data, err := json.Marshal(current)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `UPDATE chat_shown_items SET payload=? WHERE id=?`, string(data), current.ID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// ChatShownItems returns a chat's retained items, oldest first.
|
|
func (s *Store) ChatShownItems(ctx context.Context, sessionID string) ([]protocol.ChatShownItem, error) {
|
|
return chatShownItems(ctx, s.db, sessionID)
|
|
}
|
|
|
|
type chatShownQueryer interface {
|
|
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
|
|
}
|
|
|
|
func chatShownItems(ctx context.Context, db chatShownQueryer, sessionID string) ([]protocol.ChatShownItem, error) {
|
|
rows, err := db.QueryContext(ctx, `SELECT payload FROM chat_shown_items WHERE session_id=? ORDER BY created_at, rowid`, sessionID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []protocol.ChatShownItem
|
|
for rows.Next() {
|
|
var payload string
|
|
if err := rows.Scan(&payload); err != nil {
|
|
return nil, err
|
|
}
|
|
var item protocol.ChatShownItem
|
|
if json.Unmarshal([]byte(payload), &item) == nil {
|
|
items = append(items, item)
|
|
}
|
|
}
|
|
return items, rows.Err()
|
|
}
|