126 lines
3.9 KiB
Go
126 lines
3.9 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
const chatOpenWorkspaceLimit = 64
|
|
|
|
func (s *Store) initChatOpenWorkspace() error {
|
|
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_open_workspaces (
|
|
scope TEXT PRIMARY KEY, entries_json TEXT NOT NULL, revision INTEGER NOT NULL DEFAULT 0,
|
|
updated_at INTEGER NOT NULL DEFAULT 0
|
|
);`)
|
|
return err
|
|
}
|
|
|
|
func validateOpenWorkspace(entries []protocol.ChatOpenWorkspaceEntry) error {
|
|
if len(entries) > chatOpenWorkspaceLimit {
|
|
return fmt.Errorf("too many open chats")
|
|
}
|
|
seen := make(map[string]struct{}, len(entries))
|
|
for _, entry := range entries {
|
|
id := strings.TrimSpace(entry.SessionID)
|
|
if id == "" || len(id) > 200 {
|
|
return fmt.Errorf("invalid open chat session id")
|
|
}
|
|
if _, ok := seen[id]; ok {
|
|
return fmt.Errorf("duplicate open chat session id %q", id)
|
|
}
|
|
seen[id] = struct{}{}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func decodeOpenWorkspace(raw string) ([]protocol.ChatOpenWorkspaceEntry, error) {
|
|
var entries []protocol.ChatOpenWorkspaceEntry
|
|
if err := json.Unmarshal([]byte(raw), &entries); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validateOpenWorkspace(entries); err != nil {
|
|
return nil, err
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
func (s *Store) ChatOpenWorkspace(ctx context.Context, scope string) (protocol.ChatOpenWorkspaceSnapshot, error) {
|
|
var raw string
|
|
var snapshot protocol.ChatOpenWorkspaceSnapshot
|
|
err := s.db.QueryRowContext(ctx, `SELECT entries_json,revision FROM chat_open_workspaces WHERE scope=?`, scope).Scan(&raw, &snapshot.Revision)
|
|
if err == sql.ErrNoRows {
|
|
return snapshot, nil
|
|
}
|
|
if err != nil {
|
|
return snapshot, err
|
|
}
|
|
snapshot.Entries, err = decodeOpenWorkspace(raw)
|
|
return snapshot, err
|
|
}
|
|
|
|
// SaveChatOpenWorkspace atomically applies a full snapshot. Seed never replaces
|
|
// a workspace that another device has already created.
|
|
func (s *Store) SaveChatOpenWorkspace(ctx context.Context, scope string, entries []protocol.ChatOpenWorkspaceEntry, seed bool) (protocol.ChatOpenWorkspaceSnapshot, error) {
|
|
if strings.TrimSpace(scope) == "" {
|
|
return protocol.ChatOpenWorkspaceSnapshot{}, fmt.Errorf("workspace scope is required")
|
|
}
|
|
if err := validateOpenWorkspace(entries); err != nil {
|
|
return protocol.ChatOpenWorkspaceSnapshot{}, err
|
|
}
|
|
raw, err := json.Marshal(entries)
|
|
if err != nil {
|
|
return protocol.ChatOpenWorkspaceSnapshot{}, err
|
|
}
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return protocol.ChatOpenWorkspaceSnapshot{}, err
|
|
}
|
|
defer tx.Rollback()
|
|
var old string
|
|
var revision int64
|
|
err = tx.QueryRowContext(ctx, `SELECT entries_json,revision FROM chat_open_workspaces WHERE scope=?`, scope).Scan(&old, &revision)
|
|
if err == sql.ErrNoRows {
|
|
revision = 1
|
|
_, err = tx.ExecContext(ctx, `INSERT INTO chat_open_workspaces(scope,entries_json,revision,updated_at) VALUES(?,?,?,?)`, scope, string(raw), revision, time.Now().UnixMilli())
|
|
} else if err == nil {
|
|
if seed {
|
|
current, decodeErr := decodeOpenWorkspace(old)
|
|
if decodeErr != nil {
|
|
return protocol.ChatOpenWorkspaceSnapshot{}, decodeErr
|
|
}
|
|
if len(current) != 0 {
|
|
entries = current
|
|
raw, _ = json.Marshal(entries)
|
|
} else {
|
|
revision++
|
|
}
|
|
} else {
|
|
revision++
|
|
}
|
|
if len(entries) == 0 && seed && old != "" {
|
|
// An explicitly empty seed is still a valid seed; only a non-empty
|
|
// existing list wins the race above.
|
|
var current []protocol.ChatOpenWorkspaceEntry
|
|
current, _ = decodeOpenWorkspace(old)
|
|
if len(current) != 0 {
|
|
entries = current
|
|
raw, _ = json.Marshal(entries)
|
|
}
|
|
}
|
|
_, err = tx.ExecContext(ctx, `UPDATE chat_open_workspaces SET entries_json=?,revision=?,updated_at=? WHERE scope=?`, string(raw), revision, time.Now().UnixMilli(), scope)
|
|
}
|
|
if err != nil {
|
|
return protocol.ChatOpenWorkspaceSnapshot{}, err
|
|
}
|
|
if err = tx.Commit(); err != nil {
|
|
return protocol.ChatOpenWorkspaceSnapshot{}, err
|
|
}
|
|
return protocol.ChatOpenWorkspaceSnapshot{Revision: revision, Entries: entries}, nil
|
|
}
|