0696ab849a
Snapshot the chat directory with internal/filediff when a turn starts and compare it when the turn ends; store changed turns (newest 100 per chat), push chat_turn_diffs summaries, attach them to chat_messages replies and serve one turn's patches through chat_turn_diff.
98 lines
3.5 KiB
Go
98 lines
3.5 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// initChatTurnDiffs creates the additive per-turn change table. One row is a
|
|
// chat turn that changed files: its chip summary and the bounded filediff
|
|
// capture (at most filediff's retained patch budget). A session keeps its
|
|
// newest protocol.ChatTurnDiffLimit rows; older ones are pruned on insert.
|
|
func (s *Store) initChatTurnDiffs() error {
|
|
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_turn_diffs (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
session_id TEXT NOT NULL,
|
|
turn_id TEXT NOT NULL,
|
|
summary_json TEXT NOT NULL,
|
|
changes_json TEXT NOT NULL,
|
|
UNIQUE(session_id, turn_id));
|
|
CREATE INDEX IF NOT EXISTS chat_turn_diffs_session ON chat_turn_diffs(session_id, id);`)
|
|
return err
|
|
}
|
|
|
|
// RecordChatTurnDiff stores one turn's summary and changes and prunes the
|
|
// session to its newest protocol.ChatTurnDiffLimit turns.
|
|
func (s *Store) RecordChatTurnDiff(ctx context.Context, sessionID string, summary protocol.ChatTurnDiffSummary, changes protocol.StageFileChanges) error {
|
|
summaryJSON, err := json.Marshal(summary)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
changesJSON, err := json.Marshal(changes)
|
|
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 OR REPLACE INTO chat_turn_diffs(session_id,turn_id,summary_json,changes_json) VALUES(?,?,?,?)`,
|
|
sessionID, summary.TurnID, string(summaryJSON), string(changesJSON)); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `DELETE FROM chat_turn_diffs WHERE session_id = ? AND id NOT IN (
|
|
SELECT id FROM chat_turn_diffs WHERE session_id = ? ORDER BY id DESC LIMIT ?)`, sessionID, sessionID, protocol.ChatTurnDiffLimit); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// ChatTurnDiffs returns the session's recorded summaries, oldest first.
|
|
func (s *Store) ChatTurnDiffs(ctx context.Context, sessionID string) ([]protocol.ChatTurnDiffSummary, error) {
|
|
rows, err := s.db.QueryContext(ctx, `SELECT summary_json FROM chat_turn_diffs WHERE session_id = ? ORDER BY id`, sessionID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
summaries := []protocol.ChatTurnDiffSummary{}
|
|
for rows.Next() {
|
|
var raw string
|
|
if err := rows.Scan(&raw); err != nil {
|
|
return nil, err
|
|
}
|
|
var summary protocol.ChatTurnDiffSummary
|
|
if err := json.Unmarshal([]byte(raw), &summary); err != nil {
|
|
continue // an unreadable row is not reported as a turn
|
|
}
|
|
summaries = append(summaries, summary)
|
|
}
|
|
return summaries, rows.Err()
|
|
}
|
|
|
|
// ChatTurnDiff returns one turn's summary and changes; found is false when the
|
|
// session holds no such turn (never recorded, or pruned).
|
|
func (s *Store) ChatTurnDiff(ctx context.Context, sessionID, turnID string) (protocol.ChatTurnDiffSummary, protocol.StageFileChanges, bool, error) {
|
|
var summaryJSON, changesJSON string
|
|
var summary protocol.ChatTurnDiffSummary
|
|
var changes protocol.StageFileChanges
|
|
err := s.db.QueryRowContext(ctx, `SELECT summary_json, changes_json FROM chat_turn_diffs WHERE session_id = ? AND turn_id = ?`, sessionID, turnID).Scan(&summaryJSON, &changesJSON)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return summary, changes, false, nil
|
|
}
|
|
if err != nil {
|
|
return summary, changes, false, err
|
|
}
|
|
if err := json.Unmarshal([]byte(summaryJSON), &summary); err != nil {
|
|
return summary, changes, false, err
|
|
}
|
|
if err := json.Unmarshal([]byte(changesJSON), &changes); err != nil {
|
|
return summary, changes, false, err
|
|
}
|
|
return summary, changes, true, nil
|
|
}
|