spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / hooks / turns / turn-diffs-of.ts
29 lines · 866 BRaw
1import type { SessionMessage } from 'claude-code'
2
3import TurnOf from './turn-of'
4import type Types from './types'
5
6/**
7 * The transcript's turns that edited files, newest first, each numbered by
8 * its place among all prompts, read over `$.session.messages()`.
9 *
10 * Recomputed whole from the rows given, so a rewind that shrinks the
11 * transcript simply yields fewer turns.
12 *
13 * @param messages the transcript rows, oldest first
14 * @returns the turns with edits, newest first
15 */
16export function turnDiffsOf(
17 messages: readonly SessionMessage[],
18): Types.TurnDiff[] {
19 const starts = messages.flatMap((message, at) =>
20 TurnOf.isPromptRow(message) ? [at] : [],
21 )
22
23 return starts
24 .map((start, ordinal) =>
25 TurnOf.turnOf(messages.slice(start, starts[ordinal + 1]), ordinal + 1),
26 )
27 .filter(turn => turn.files.length > 0)
28 .toReversed()
29}