Chat tools: argument validation, replace, file snapshots, server-fetched images, show_run, show_diff head/paths, schedule_followup

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 19:44:47 +00:00
parent c441400f36
commit d204ce3187
27 changed files with 1988 additions and 79 deletions
+65 -1
View File
@@ -102,6 +102,70 @@ func (s *Store) AppendChatShownItem(ctx context.Context, sessionID string, item
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
}
if err := applyChatShownBudget(ctx, tx, sessionID); err != nil {
return err
}
return tx.Commit()
}
// ReplaceChatShownItem stores item over the chat's existing item with the
// same id (found false when the chat has none), then applies the body budget.
func (s *Store) ReplaceChatShownItem(ctx context.Context, sessionID string, item protocol.ChatShownItem) (bool, error) {
payload, err := json.Marshal(item)
if err != nil {
return false, err
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return false, err
}
defer tx.Rollback()
result, err := tx.ExecContext(ctx, `UPDATE chat_shown_items SET payload=? WHERE session_id=? AND id=?`, string(payload), sessionID, item.ID)
if err != nil {
return false, err
}
if n, _ := result.RowsAffected(); n == 0 {
return false, nil
}
if err := applyChatShownBudget(ctx, tx, sessionID); err != nil {
return false, err
}
return true, tx.Commit()
}
// ChatShownItem returns one of a chat's retained items by id.
func (s *Store) ChatShownItem(ctx context.Context, sessionID, id string) (protocol.ChatShownItem, bool, error) {
var payload string
err := s.db.QueryRowContext(ctx, `SELECT payload FROM chat_shown_items WHERE session_id=? AND id=?`, sessionID, id).Scan(&payload)
if errors.Is(err, sql.ErrNoRows) {
return protocol.ChatShownItem{}, false, nil
}
if err != nil {
return protocol.ChatShownItem{}, false, err
}
var item protocol.ChatShownItem
if err := json.Unmarshal([]byte(payload), &item); err != nil {
return protocol.ChatShownItem{}, false, err
}
return item, true, nil
}
// RunSummaryByID returns one run's summary (found false for an unknown run).
func (s *Store) RunSummaryByID(ctx context.Context, id int64) (protocol.RunSummary, bool, error) {
var summary protocol.RunSummary
err := scanRun(s.db.QueryRowContext(ctx, runSelect+" WHERE r.id=?", id), &summary)
if errors.Is(err, sql.ErrNoRows) {
return protocol.RunSummary{}, false, nil
}
if err != nil {
return protocol.RunSummary{}, false, err
}
return summary, true, nil
}
// applyChatShownBudget keeps HTML and diff bodies only for the newest
// documents within protocol.ChatShownHTMLBudget.
func applyChatShownBudget(ctx context.Context, tx *sql.Tx, sessionID string) error {
items, err := chatShownItems(ctx, tx, sessionID)
if err != nil {
return err
@@ -138,7 +202,7 @@ SELECT id FROM chat_shown_items WHERE session_id=? ORDER BY created_at DESC, row
return err
}
}
return tx.Commit()
return nil
}
// ChatShownItems returns a chat's retained items, oldest first.