Records every file edit of a turn and replays them with /replay

hooks/register.tsx 40 lines1import type { Register } from 'claude-code'
2
3const EDIT_TOOLS = new Set(['Edit', 'MultiEdit', 'Write', 'NotebookEdit'])
4
5export const register: Register = on => {
6 let edits: string[] = []
7
8 on('session.start', async ($, e, next) => {
9 await $.command.register({
10 name: 'replay',
11 description: 'Replay the file edits of the last turn, in order',
12 })
13
14 return next(e)
15 })
16
17 on('prompt.submit', ($, e, next) => {
18 if (e.command === undefined) {
19 edits = []
20 }
21
22 return next(e)
23 })
24
25 on('tool.call', async ($, e, next) => {
26 const path = typeof e.file_path === 'string' ? e.file_path : undefined
27
28 if (path !== undefined && EDIT_TOOLS.has(e.tool)) {
29 edits.push(`${edits.length + 1}. ${e.tool} ${path}`)
30 $.ui.status(`${edits.length} edits across ${new Set(edits.map(x => x.split(' ')[2])).size} files`)
31 }
32
33 return next(e)
34 })
35
36 on('command.run', { command: 'replay' }, () => ({
37 text: edits.length === 0 ? 'No edits in the last turn.' : edits.join('\n'),
38 }))
39}
40