claude-code-types 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +59 -79
  3. package/index.d.ts +733 -535
  4. package/package.json +32 -30
package/index.d.ts CHANGED
@@ -1,535 +1,733 @@
1
- /**
2
- * Type definitions for Claude Code chat history JSONL files.
3
- *
4
- * Claude Code stores conversation history at:
5
- * ~/.claude/projects/<project-slug>/<session-id>.jsonl
6
- *
7
- * Each line is a JSON object conforming to the `TranscriptEntry` union type.
8
- *
9
- * Usage:
10
- * import type { TranscriptEntry } from 'claude-code-types';
11
- * const entry: TranscriptEntry = JSON.parse(line);
12
- */
13
-
14
- // ---------------------------------------------------------------------------
15
- // Top-level entry union
16
- // ---------------------------------------------------------------------------
17
-
18
- export type TranscriptEntry =
19
- | UserEntry
20
- | AssistantEntry
21
- | SystemEntry
22
- | FileHistorySnapshotEntry
23
- | PrLinkEntry
24
- | ProgressEntry
25
- | QueueOperationEntry
26
- | SavedHookContextEntry
27
- | SummaryEntry;
28
-
29
- // ---------------------------------------------------------------------------
30
- // Shared base fields (present on most entries that represent conversation turns)
31
- // ---------------------------------------------------------------------------
32
-
33
- interface EntryBase {
34
- uuid: string;
35
- parentUuid: string | null;
36
- isSidechain: boolean;
37
- sessionId: string;
38
- timestamp: string;
39
- cwd: string;
40
- version: string;
41
- gitBranch?: string;
42
- slug?: string;
43
- teamName?: string;
44
- userType: 'external';
45
- }
46
-
47
- // ---------------------------------------------------------------------------
48
- // User entry
49
- // ---------------------------------------------------------------------------
50
-
51
- export interface UserEntry extends EntryBase {
52
- type: 'user';
53
- message: UserMessage;
54
- isMeta?: boolean;
55
- isCompactSummary?: boolean;
56
- isVisibleInTranscriptOnly?: boolean;
57
- imagePasteIds?: string[];
58
- permissionMode?: PermissionMode;
59
- planContent?: string;
60
- thinkingMetadata?: ThinkingMetadata;
61
- todos?: Todo[];
62
- /** Present when this user message is a tool result being delivered back */
63
- toolUseResult?: unknown;
64
- /** UUID of the assistant message whose tool_use triggered this result */
65
- sourceToolAssistantUUID?: string;
66
- /** ID of the tool_use content block this result corresponds to */
67
- sourceToolUseID?: string;
68
- }
69
-
70
- export interface UserMessage {
71
- role: 'user';
72
- content: string | UserContentBlock[];
73
- }
74
-
75
- export type UserContentBlock =
76
- | TextBlock
77
- | ImageBlock
78
- | DocumentBlock
79
- | ToolResultBlock;
80
-
81
- // ---------------------------------------------------------------------------
82
- // Assistant entry
83
- // ---------------------------------------------------------------------------
84
-
85
- export interface AssistantEntry extends EntryBase {
86
- type: 'assistant';
87
- message: AssistantMessage;
88
- requestId?: string;
89
- /** Present when the API returned an error instead of a response */
90
- apiError?: unknown;
91
- error?: string;
92
- isApiErrorMessage?: boolean;
93
- }
94
-
95
- export interface AssistantMessage {
96
- role: 'assistant';
97
- id?: string;
98
- type?: 'message';
99
- model?: Model;
100
- content: AssistantContentBlock[];
101
- stop_reason: StopReason | null;
102
- stop_sequence?: string | null;
103
- usage?: Usage;
104
- container?: unknown;
105
- context_management?: unknown;
106
- }
107
-
108
- export type AssistantContentBlock =
109
- | TextBlock
110
- | ThinkingBlock
111
- | RedactedThinkingBlock
112
- | ToolUseBlock
113
- | ServerToolUseBlock
114
- | WebSearchToolResultBlock;
115
-
116
- // ---------------------------------------------------------------------------
117
- // System entry (multiple subtypes)
118
- // ---------------------------------------------------------------------------
119
-
120
- export interface SystemEntry extends Partial<EntryBase> {
121
- type: 'system';
122
- subtype: SystemSubtype;
123
- isMeta?: boolean;
124
- content?: string;
125
- level?: string;
126
- /** turn_duration */
127
- durationMs?: number;
128
- /** api_error */
129
- error?: string;
130
- cause?: string;
131
- retryAttempt?: number;
132
- retryInMs?: number;
133
- maxRetries?: number;
134
- /** compact_boundary */
135
- compactMetadata?: CompactMetadata;
136
- /** microcompact_boundary */
137
- microcompactMetadata?: MicrocompactMetadata;
138
- /** stop_hook_summary */
139
- hookCount?: number;
140
- hookErrors?: unknown[];
141
- hookInfos?: unknown[];
142
- hasOutput?: boolean;
143
- stopReason?: string;
144
- preventedContinuation?: boolean;
145
- toolUseID?: string;
146
- logicalParentUuid?: string;
147
- }
148
-
149
- export type SystemSubtype =
150
- | 'api_error'
151
- | 'compact_boundary'
152
- | 'informational'
153
- | 'local_command'
154
- | 'microcompact_boundary'
155
- | 'stop_hook_summary'
156
- | 'turn_duration';
157
-
158
- // ---------------------------------------------------------------------------
159
- // File history snapshot (undo/restore tracking)
160
- // ---------------------------------------------------------------------------
161
-
162
- export interface FileHistorySnapshotEntry {
163
- type: 'file-history-snapshot';
164
- messageId: string;
165
- isSnapshotUpdate: boolean;
166
- snapshot: FileHistorySnapshot;
167
- }
168
-
169
- export interface FileHistorySnapshot {
170
- messageId: string;
171
- timestamp: string;
172
- trackedFileBackups: Record<string, FileBackup>;
173
- }
174
-
175
- export interface FileBackup {
176
- backupFileName: string;
177
- version: number;
178
- backupTime: string;
179
- }
180
-
181
- // ---------------------------------------------------------------------------
182
- // PR link
183
- // ---------------------------------------------------------------------------
184
-
185
- export interface PrLinkEntry {
186
- type: 'pr-link';
187
- sessionId: string;
188
- timestamp: string;
189
- prNumber: number;
190
- prUrl: string;
191
- prRepository: string;
192
- }
193
-
194
- // ---------------------------------------------------------------------------
195
- // Progress (streaming updates for subagent / Task tool)
196
- // ---------------------------------------------------------------------------
197
-
198
- export interface ProgressEntry extends Partial<EntryBase> {
199
- type: 'progress';
200
- data: ProgressData;
201
- parentToolUseID?: string;
202
- toolUseID?: string;
203
- }
204
-
205
- export interface ProgressData {
206
- message: {
207
- type: 'user' | 'assistant';
208
- message: UserMessage | AssistantMessage;
209
- uuid?: string;
210
- timestamp?: string;
211
- };
212
- }
213
-
214
- // ---------------------------------------------------------------------------
215
- // Queue operation (messages typed while agent is busy)
216
- // ---------------------------------------------------------------------------
217
-
218
- export interface QueueOperationEntry {
219
- type: 'queue-operation';
220
- operation: 'enqueue' | 'dequeue';
221
- sessionId: string;
222
- timestamp: string;
223
- content: string;
224
- }
225
-
226
- // ---------------------------------------------------------------------------
227
- // Saved hook context
228
- // ---------------------------------------------------------------------------
229
-
230
- export interface SavedHookContextEntry extends Partial<EntryBase> {
231
- type: 'saved_hook_context';
232
- content: string[];
233
- hookEvent?: string;
234
- hookName?: string;
235
- toolUseID?: string;
236
- }
237
-
238
- // ---------------------------------------------------------------------------
239
- // Summary
240
- // ---------------------------------------------------------------------------
241
-
242
- export interface SummaryEntry {
243
- type: 'summary';
244
- summary: string;
245
- leafUuid: string;
246
- }
247
-
248
- // ---------------------------------------------------------------------------
249
- // Content blocks
250
- // ---------------------------------------------------------------------------
251
-
252
- export interface TextBlock {
253
- type: 'text';
254
- text: string;
255
- citations?: TextCitation[] | null;
256
- }
257
-
258
- export interface ThinkingBlock {
259
- type: 'thinking';
260
- thinking: string;
261
- signature: string;
262
- }
263
-
264
- export interface RedactedThinkingBlock {
265
- type: 'redacted_thinking';
266
- data: string;
267
- }
268
-
269
- export interface ToolUseBlock {
270
- type: 'tool_use';
271
- id: string;
272
- name: BuiltinToolName | (string & {});
273
- input: Record<string, unknown>;
274
- /** Present in progress/streaming entries */
275
- caller?: { type: string };
276
- }
277
-
278
- export interface ToolResultBlock {
279
- type: 'tool_result';
280
- tool_use_id: string;
281
- content?: string | TextBlock[];
282
- is_error?: boolean;
283
- }
284
-
285
- export interface ImageBlock {
286
- type: 'image';
287
- source: Base64ImageSource | UrlImageSource;
288
- }
289
-
290
- export interface DocumentBlock {
291
- type: 'document';
292
- source: Base64DocumentSource | PlainTextSource | UrlDocumentSource;
293
- title?: string | null;
294
- context?: string | null;
295
- }
296
-
297
- export interface ServerToolUseBlock {
298
- type: 'server_tool_use';
299
- id: string;
300
- name: 'web_search';
301
- input: Record<string, unknown>;
302
- }
303
-
304
- export interface WebSearchToolResultBlock {
305
- type: 'web_search_tool_result';
306
- tool_use_id: string;
307
- content: WebSearchResultError | WebSearchResultItem[];
308
- }
309
-
310
- // ---------------------------------------------------------------------------
311
- // Media sources
312
- // ---------------------------------------------------------------------------
313
-
314
- export interface Base64ImageSource {
315
- type: 'base64';
316
- media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';
317
- data: string;
318
- }
319
-
320
- export interface UrlImageSource {
321
- type: 'url';
322
- url: string;
323
- }
324
-
325
- export interface Base64DocumentSource {
326
- type: 'base64';
327
- media_type: 'application/pdf';
328
- data: string;
329
- }
330
-
331
- export interface PlainTextSource {
332
- type: 'text';
333
- media_type: 'text/plain';
334
- data: string;
335
- }
336
-
337
- export interface UrlDocumentSource {
338
- type: 'url';
339
- url: string;
340
- }
341
-
342
- // ---------------------------------------------------------------------------
343
- // Citations
344
- // ---------------------------------------------------------------------------
345
-
346
- export type TextCitation =
347
- | CitationCharLocation
348
- | CitationPageLocation
349
- | CitationContentBlockLocation
350
- | CitationWebSearchResultLocation;
351
-
352
- export interface CitationCharLocation {
353
- type: 'char_location';
354
- cited_text: string;
355
- document_index: number;
356
- document_title: string | null;
357
- start_char_index: number;
358
- end_char_index: number;
359
- file_id: string | null;
360
- }
361
-
362
- export interface CitationPageLocation {
363
- type: 'page_location';
364
- cited_text: string;
365
- document_index: number;
366
- document_title: string | null;
367
- start_page_number: number;
368
- end_page_number: number;
369
- file_id: string | null;
370
- }
371
-
372
- export interface CitationContentBlockLocation {
373
- type: 'content_block_location';
374
- cited_text: string;
375
- document_index: number;
376
- document_title: string | null;
377
- start_block_index: number;
378
- end_block_index: number;
379
- file_id: string | null;
380
- }
381
-
382
- export interface CitationWebSearchResultLocation {
383
- type: 'web_search_result_location';
384
- cited_text: string;
385
- encrypted_index: string;
386
- title: string | null;
387
- url: string;
388
- }
389
-
390
- // ---------------------------------------------------------------------------
391
- // Web search results
392
- // ---------------------------------------------------------------------------
393
-
394
- export interface WebSearchResultError {
395
- type: 'web_search_error';
396
- error_code: string;
397
- message: string;
398
- }
399
-
400
- export interface WebSearchResultItem {
401
- type: 'web_search_result';
402
- url: string;
403
- title: string;
404
- encrypted_content: string;
405
- page_age?: string | null;
406
- }
407
-
408
- // ---------------------------------------------------------------------------
409
- // Usage & metadata
410
- // ---------------------------------------------------------------------------
411
-
412
- export interface Usage {
413
- input_tokens: number;
414
- output_tokens: number;
415
- cache_creation_input_tokens?: number | null;
416
- cache_read_input_tokens?: number | null;
417
- cache_creation?: CacheCreation | null;
418
- server_tool_use?: ServerToolUsage | null;
419
- service_tier?: 'standard' | 'priority' | 'batch' | null;
420
- inference_geo?: string | null;
421
- }
422
-
423
- export interface CacheCreation {
424
- ephemeral_5m_input_tokens: number;
425
- ephemeral_1h_input_tokens: number;
426
- }
427
-
428
- export interface ServerToolUsage {
429
- web_search_requests: number;
430
- }
431
-
432
- export interface CompactMetadata {
433
- trigger: 'auto' | 'manual';
434
- preTokens: number;
435
- }
436
-
437
- export interface MicrocompactMetadata {
438
- trigger: 'auto' | 'manual';
439
- preTokens: number;
440
- tokensSaved: number;
441
- compactedToolIds: string[];
442
- }
443
-
444
- export interface ThinkingMetadata {
445
- maxThinkingTokens: number;
446
- }
447
-
448
- export interface Todo {
449
- content: string;
450
- status: string;
451
- activeForm?: string;
452
- }
453
-
454
- // ---------------------------------------------------------------------------
455
- // Enums / unions
456
- // ---------------------------------------------------------------------------
457
-
458
- export type Model =
459
- | 'claude-opus-4-6'
460
- | 'claude-opus-4-5-20251101'
461
- | 'claude-sonnet-4-5-20250929'
462
- | 'claude-haiku-4-5-20251001'
463
- | '<synthetic>'
464
- | (string & {});
465
-
466
- export type StopReason =
467
- | 'end_turn'
468
- | 'max_tokens'
469
- | 'stop_sequence'
470
- | 'tool_use'
471
- | 'pause_turn'
472
- | 'refusal';
473
-
474
- export type PermissionMode =
475
- | 'default'
476
- | 'plan'
477
- | 'acceptEdits'
478
- | 'dontAsk'
479
- | 'bypassPermissions';
480
-
481
- /** Built-in Claude Code tools (non-exhaustive — MCP tools use `mcp__<server>__<tool>` pattern) */
482
- export type BuiltinToolName =
483
- | 'AskUserQuestion'
484
- | 'Bash'
485
- | 'Edit'
486
- | 'EnterPlanMode'
487
- | 'ExitPlanMode'
488
- | 'Glob'
489
- | 'Grep'
490
- | 'KillShell'
491
- | 'ListMcpResourcesTool'
492
- | 'NotebookEdit'
493
- | 'Read'
494
- | 'ReadMcpResourceTool'
495
- | 'SendMessage'
496
- | 'Skill'
497
- | 'Task'
498
- | 'TaskCreate'
499
- | 'TaskGet'
500
- | 'TaskList'
501
- | 'TaskOutput'
502
- | 'TaskStop'
503
- | 'TaskUpdate'
504
- | 'TodoWrite'
505
- | 'WebFetch'
506
- | 'WebSearch'
507
- | 'Write';
508
-
509
- // ---------------------------------------------------------------------------
510
- // Parser helper type
511
- // ---------------------------------------------------------------------------
512
-
513
- /**
514
- * Parse a single JSONL line into a typed TranscriptEntry.
515
- *
516
- * @example
517
- * ```ts
518
- * import type { TranscriptEntry } from 'claude-code-types';
519
- * import { readFileSync } from 'fs';
520
- *
521
- * const entries: TranscriptEntry[] = readFileSync(path, 'utf-8')
522
- * .split('\n')
523
- * .filter(Boolean)
524
- * .map(line => JSON.parse(line) as TranscriptEntry);
525
- *
526
- * for (const entry of entries) {
527
- * switch (entry.type) {
528
- * case 'user': console.log(entry.message.content); break;
529
- * case 'assistant': console.log(entry.message.model); break;
530
- * case 'system': console.log(entry.subtype); break;
531
- * }
532
- * }
533
- * ```
534
- */
535
- export type ParseLine = (line: string) => TranscriptEntry | null;
1
+ /**
2
+ * Type definitions for Claude Code chat history JSONL files.
3
+ *
4
+ * Claude Code stores conversation history at:
5
+ * `~/.claude/projects/<project-slug>/<session-id>.jsonl`
6
+ *
7
+ * Each line is a JSON object conforming to the {@link TranscriptEntry} union type.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import type { TranscriptEntry } from 'claude-code-types';
12
+ * import { readFileSync } from 'fs';
13
+ *
14
+ * const entries: TranscriptEntry[] = readFileSync(path, 'utf-8')
15
+ * .split('\n')
16
+ * .filter(Boolean)
17
+ * .map(line => JSON.parse(line) as TranscriptEntry);
18
+ *
19
+ * for (const entry of entries) {
20
+ * switch (entry.type) {
21
+ * case 'user': console.log(entry.message.content); break;
22
+ * case 'assistant': console.log(entry.message.model); break;
23
+ * case 'system': console.log(entry.subtype); break;
24
+ * }
25
+ * }
26
+ * ```
27
+ *
28
+ * @packageDocumentation
29
+ */
30
+
31
+ // ---------------------------------------------------------------------------
32
+ // Top-level entry union
33
+ // ---------------------------------------------------------------------------
34
+
35
+ /** Discriminated union of all JSONL line types. Switch on `entry.type` to narrow. */
36
+ export type TranscriptEntry =
37
+ | UserEntry
38
+ | AssistantEntry
39
+ | SystemEntry
40
+ | FileHistorySnapshotEntry
41
+ | PrLinkEntry
42
+ | ProgressEntry
43
+ | QueueOperationEntry
44
+ | SavedHookContextEntry
45
+ | SummaryEntry;
46
+
47
+ // ---------------------------------------------------------------------------
48
+ // Shared base fields (present on most entries that represent conversation turns)
49
+ // ---------------------------------------------------------------------------
50
+
51
+ interface EntryBase {
52
+ /** Unique identifier for this entry. */
53
+ uuid: string;
54
+ /** UUID of the parent entry in the conversation tree, or `null` for root. */
55
+ parentUuid: string | null;
56
+ /** Whether this entry is on a side-chain (branched conversation path). */
57
+ isSidechain: boolean;
58
+ sessionId: string;
59
+ /** ISO 8601 timestamp. */
60
+ timestamp: string;
61
+ /** Working directory at the time this entry was created. */
62
+ cwd: string;
63
+ /** Claude Code version string (e.g. `"1.0.33"`). */
64
+ version: string;
65
+ gitBranch?: string;
66
+ /** Project slug derived from the working directory. */
67
+ slug?: string;
68
+ teamName?: string;
69
+ userType: 'external';
70
+ }
71
+
72
+ // ---------------------------------------------------------------------------
73
+ // User entry
74
+ // ---------------------------------------------------------------------------
75
+
76
+ /** Human message or tool result delivered back to the model. */
77
+ export interface UserEntry extends EntryBase {
78
+ type: 'user';
79
+ message: UserMessage;
80
+ isMeta?: boolean;
81
+ /** When `true`, this message is a compaction summary replacing earlier history. */
82
+ isCompactSummary?: boolean;
83
+ /** When `true`, this message is shown in the transcript UI but not sent to the API. */
84
+ isVisibleInTranscriptOnly?: boolean;
85
+ imagePasteIds?: string[];
86
+ permissionMode?: PermissionMode;
87
+ planContent?: string;
88
+ thinkingMetadata?: ThinkingMetadata;
89
+ todos?: Todo[];
90
+ /** Present when this user message is a tool result being delivered back. */
91
+ toolUseResult?: unknown;
92
+ /** UUID of the assistant message whose tool_use triggered this result. */
93
+ sourceToolAssistantUUID?: string;
94
+ /** ID of the tool_use content block this result corresponds to. */
95
+ sourceToolUseID?: string;
96
+ }
97
+
98
+ /** The `message` payload inside a {@link UserEntry}. */
99
+ export interface UserMessage {
100
+ role: 'user';
101
+ content: string | UserContentBlock[];
102
+ }
103
+
104
+ /** Content blocks that can appear in a user message. */
105
+ export type UserContentBlock =
106
+ | TextBlock
107
+ | ImageBlock
108
+ | DocumentBlock
109
+ | ToolResultBlock;
110
+
111
+ // ---------------------------------------------------------------------------
112
+ // Assistant entry
113
+ // ---------------------------------------------------------------------------
114
+
115
+ /** Model response, including text, thinking, and tool calls. */
116
+ export interface AssistantEntry extends EntryBase {
117
+ type: 'assistant';
118
+ message: AssistantMessage;
119
+ /** Anthropic API request ID for this response. */
120
+ requestId?: string;
121
+ /** Present when the API returned an error instead of a response. */
122
+ apiError?: unknown;
123
+ error?: string;
124
+ isApiErrorMessage?: boolean;
125
+ }
126
+
127
+ /**
128
+ * The `message` payload inside an {@link AssistantEntry}.
129
+ *
130
+ * Mirrors the Anthropic Messages API response shape. Note that `stop_reason`
131
+ * is `null` in streaming `message_start` events and only populated in
132
+ * `message_delta`. Stored entries may retain the `null` from partial snapshots.
133
+ */
134
+ export interface AssistantMessage {
135
+ role: 'assistant';
136
+ /** Anthropic message ID (e.g. `"msg_01XFDUDYJgAACzvnptvVoYEL"`). Format may change over time. */
137
+ id?: string;
138
+ type?: 'message';
139
+ /** Model that generated this response. See {@link Model}. */
140
+ model?: Model;
141
+ content: AssistantContentBlock[];
142
+ /** `null` during streaming until the final `message_delta` event. */
143
+ stop_reason: StopReason | null;
144
+ stop_sequence?: string | null;
145
+ usage?: Usage;
146
+ /**
147
+ * Present when the code execution tool (beta) was used. Contains `id` and
148
+ * `expires_at` for container reuse. Typed as `unknown` because the schema
149
+ * is still in beta.
150
+ */
151
+ container?: unknown;
152
+ /**
153
+ * Present when context editing strategies were applied (beta).
154
+ * Contains `applied_edits` describing which tool uses or thinking turns
155
+ * were cleared. Typed as `unknown` because the schema is still in beta.
156
+ */
157
+ context_management?: unknown;
158
+ }
159
+
160
+ /** Content blocks that can appear in an assistant message. */
161
+ export type AssistantContentBlock =
162
+ | TextBlock
163
+ | ThinkingBlock
164
+ | RedactedThinkingBlock
165
+ | ToolUseBlock
166
+ | ServerToolUseBlock
167
+ | WebSearchToolResultBlock;
168
+
169
+ // ---------------------------------------------------------------------------
170
+ // System entry (multiple subtypes)
171
+ // ---------------------------------------------------------------------------
172
+
173
+ /**
174
+ * Internal system events. Uses `Partial<EntryBase>` — not all base fields
175
+ * are guaranteed present. Switch on `subtype` to determine which optional
176
+ * fields are relevant:
177
+ *
178
+ * - `api_error`: `error`, `cause`, `retryAttempt`, `retryInMs`, `maxRetries`
179
+ * - `compact_boundary`: `compactMetadata`
180
+ * - `microcompact_boundary`: `microcompactMetadata`
181
+ * - `turn_duration`: `durationMs`
182
+ * - `stop_hook_summary`: `hookCount`, `hookErrors`, `hookInfos`, `hasOutput`, `stopReason`, `preventedContinuation`
183
+ */
184
+ export interface SystemEntry extends Partial<EntryBase> {
185
+ type: 'system';
186
+ subtype: SystemSubtype;
187
+ isMeta?: boolean;
188
+ content?: string;
189
+ level?: string;
190
+ /** Milliseconds the turn took (subtype `turn_duration`). */
191
+ durationMs?: number;
192
+ /** Error message (subtype `api_error`). */
193
+ error?: string;
194
+ cause?: string;
195
+ retryAttempt?: number;
196
+ retryInMs?: number;
197
+ maxRetries?: number;
198
+ /** Subtype `compact_boundary`. */
199
+ compactMetadata?: CompactMetadata;
200
+ /** Subtype `microcompact_boundary`. */
201
+ microcompactMetadata?: MicrocompactMetadata;
202
+ /** Subtype `stop_hook_summary`. */
203
+ hookCount?: number;
204
+ hookErrors?: unknown[];
205
+ hookInfos?: unknown[];
206
+ hasOutput?: boolean;
207
+ stopReason?: string;
208
+ preventedContinuation?: boolean;
209
+ toolUseID?: string;
210
+ logicalParentUuid?: string;
211
+ }
212
+
213
+ /** Discriminator values for {@link SystemEntry.subtype}. */
214
+ export type SystemSubtype =
215
+ | 'api_error'
216
+ | 'compact_boundary'
217
+ | 'informational'
218
+ | 'local_command'
219
+ | 'microcompact_boundary'
220
+ | 'stop_hook_summary'
221
+ | 'turn_duration';
222
+
223
+ // ---------------------------------------------------------------------------
224
+ // File history snapshot (undo/restore tracking)
225
+ // ---------------------------------------------------------------------------
226
+
227
+ /**
228
+ * Tracks file backups for undo/restore. The `messageId` may collide with the
229
+ * immediately following message's `uuid`.
230
+ */
231
+ export interface FileHistorySnapshotEntry {
232
+ type: 'file-history-snapshot';
233
+ messageId: string;
234
+ /** `false` for initial snapshot, `true` for incremental updates. */
235
+ isSnapshotUpdate: boolean;
236
+ snapshot: FileHistorySnapshot;
237
+ }
238
+
239
+ /** Snapshot of all tracked file backups at a point in time. */
240
+ export interface FileHistorySnapshot {
241
+ messageId: string;
242
+ timestamp: string;
243
+ /** Map of original file path to backup info. */
244
+ trackedFileBackups: Record<string, FileBackup>;
245
+ }
246
+
247
+ /** Backup metadata for a single tracked file. */
248
+ export interface FileBackup {
249
+ backupFileName: string;
250
+ version: number;
251
+ backupTime: string;
252
+ }
253
+
254
+ // ---------------------------------------------------------------------------
255
+ // PR link
256
+ // ---------------------------------------------------------------------------
257
+
258
+ /** Records a pull request created or linked during the session. */
259
+ export interface PrLinkEntry {
260
+ type: 'pr-link';
261
+ sessionId: string;
262
+ timestamp: string;
263
+ prNumber: number;
264
+ prUrl: string;
265
+ prRepository: string;
266
+ }
267
+
268
+ // ---------------------------------------------------------------------------
269
+ // Progress (streaming updates for subagent / Task tool)
270
+ // ---------------------------------------------------------------------------
271
+
272
+ /**
273
+ * Streaming updates from a subagent / Task tool invocation.
274
+ *
275
+ * **Caveat:** Progress entries can be very large (multi-MB) because they may
276
+ * include full `normalizedMessages` snapshots. The `parentToolUseID` may
277
+ * reference UUIDs that don't exist elsewhere in the file.
278
+ */
279
+ export interface ProgressEntry extends Partial<EntryBase> {
280
+ type: 'progress';
281
+ data: ProgressData;
282
+ parentToolUseID?: string;
283
+ toolUseID?: string;
284
+ }
285
+
286
+ /** Payload inside a {@link ProgressEntry}. */
287
+ export interface ProgressData {
288
+ message: {
289
+ type: 'user' | 'assistant';
290
+ message: UserMessage | AssistantMessage;
291
+ uuid?: string;
292
+ timestamp?: string;
293
+ };
294
+ }
295
+
296
+ // ---------------------------------------------------------------------------
297
+ // Queue operation (messages typed while agent is busy)
298
+ // ---------------------------------------------------------------------------
299
+
300
+ /** Messages queued by the user while the agent is processing a turn. */
301
+ export interface QueueOperationEntry {
302
+ type: 'queue-operation';
303
+ operation: 'enqueue' | 'dequeue';
304
+ sessionId: string;
305
+ timestamp: string;
306
+ content: string;
307
+ }
308
+
309
+ // ---------------------------------------------------------------------------
310
+ // Saved hook context
311
+ // ---------------------------------------------------------------------------
312
+
313
+ /** Persisted context from a hook execution. */
314
+ export interface SavedHookContextEntry extends Partial<EntryBase> {
315
+ type: 'saved_hook_context';
316
+ content: string[];
317
+ hookEvent?: string;
318
+ hookName?: string;
319
+ toolUseID?: string;
320
+ }
321
+
322
+ // ---------------------------------------------------------------------------
323
+ // Summary
324
+ // ---------------------------------------------------------------------------
325
+
326
+ /** Conversation summary used for context compaction. */
327
+ export interface SummaryEntry {
328
+ type: 'summary';
329
+ summary: string;
330
+ /** UUID of the leaf message this summary covers up to. */
331
+ leafUuid: string;
332
+ }
333
+
334
+ // ---------------------------------------------------------------------------
335
+ // Content blocks
336
+ // ---------------------------------------------------------------------------
337
+
338
+ /** Plain text content block. Used in both user and assistant messages. */
339
+ export interface TextBlock {
340
+ type: 'text';
341
+ text: string;
342
+ /**
343
+ * Present only when the request included documents with `citations: { enabled: true }`.
344
+ * `cited_text` does not count toward output or input tokens.
345
+ */
346
+ citations?: TextCitation[] | null;
347
+ }
348
+
349
+ /**
350
+ * Extended thinking content block.
351
+ *
352
+ * The `signature` field contains a cryptographic signature used to verify
353
+ * authenticity. **Modifying a thinking block will cause the API to reject it.**
354
+ *
355
+ * During tool use cycles, thinking blocks must be returned with the
356
+ * corresponding tool results. They can only be dropped after the full
357
+ * tool use cycle completes.
358
+ */
359
+ export interface ThinkingBlock {
360
+ type: 'thinking';
361
+ thinking: string;
362
+ /** Cryptographic signature — do not modify. */
363
+ signature: string;
364
+ }
365
+
366
+ /**
367
+ * Redacted thinking block (content hidden by safety classifiers).
368
+ * The `data` field is encrypted and must be passed back to the API as-is.
369
+ */
370
+ export interface RedactedThinkingBlock {
371
+ type: 'redacted_thinking';
372
+ data: string;
373
+ }
374
+
375
+ /**
376
+ * Tool invocation by the model.
377
+ *
378
+ * For Claude Code built-in tools, `name` will be a {@link BuiltinToolName}.
379
+ * MCP tools use the pattern `mcp__<server>__<tool>`.
380
+ */
381
+ export interface ToolUseBlock {
382
+ type: 'tool_use';
383
+ /** Prefixed with `toolu_` (e.g. `"toolu_01A09q90qw..."`). */
384
+ id: string;
385
+ name: BuiltinToolName | (string & {});
386
+ input: Record<string, unknown>;
387
+ /** Present in progress/streaming entries only — not part of the Anthropic API. */
388
+ caller?: { type: string };
389
+ }
390
+
391
+ /** Tool result delivered in a user message. */
392
+ export interface ToolResultBlock {
393
+ type: 'tool_result';
394
+ tool_use_id: string;
395
+ content?: string | TextBlock[];
396
+ is_error?: boolean;
397
+ }
398
+
399
+ /** Image content block in a user message. */
400
+ export interface ImageBlock {
401
+ type: 'image';
402
+ source: Base64ImageSource | UrlImageSource;
403
+ }
404
+
405
+ /** Document content block in a user message (PDF, plain text, or URL). */
406
+ export interface DocumentBlock {
407
+ type: 'document';
408
+ source: Base64DocumentSource | PlainTextSource | UrlDocumentSource;
409
+ title?: string | null;
410
+ context?: string | null;
411
+ }
412
+
413
+ /**
414
+ * Server-side tool invocation (executed by the Anthropic API, not locally).
415
+ * Currently limited to web search.
416
+ */
417
+ export interface ServerToolUseBlock {
418
+ type: 'server_tool_use';
419
+ /** Prefixed with `srvtoolu_` (e.g. `"srvtoolu_01B3C4D5..."`). */
420
+ id: string;
421
+ name: 'web_search';
422
+ input: Record<string, unknown>;
423
+ }
424
+
425
+ /** Result from a server-side web search tool invocation. */
426
+ export interface WebSearchToolResultBlock {
427
+ type: 'web_search_tool_result';
428
+ tool_use_id: string;
429
+ content: WebSearchResultError | WebSearchResultItem[];
430
+ }
431
+
432
+ // ---------------------------------------------------------------------------
433
+ // Media sources
434
+ // ---------------------------------------------------------------------------
435
+
436
+ /** Base64-encoded image source. */
437
+ export interface Base64ImageSource {
438
+ type: 'base64';
439
+ media_type: 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp';
440
+ data: string;
441
+ }
442
+
443
+ /** URL-referenced image source. */
444
+ export interface UrlImageSource {
445
+ type: 'url';
446
+ url: string;
447
+ }
448
+
449
+ /** Base64-encoded PDF document source. */
450
+ export interface Base64DocumentSource {
451
+ type: 'base64';
452
+ media_type: 'application/pdf';
453
+ data: string;
454
+ }
455
+
456
+ /** Plain text document source. */
457
+ export interface PlainTextSource {
458
+ type: 'text';
459
+ media_type: 'text/plain';
460
+ data: string;
461
+ }
462
+
463
+ /** URL-referenced document source. */
464
+ export interface UrlDocumentSource {
465
+ type: 'url';
466
+ url: string;
467
+ }
468
+
469
+ // ---------------------------------------------------------------------------
470
+ // Citations
471
+ // ---------------------------------------------------------------------------
472
+
473
+ /** Discriminated union of all citation location types. */
474
+ export type TextCitation =
475
+ | CitationCharLocation
476
+ | CitationPageLocation
477
+ | CitationContentBlockLocation
478
+ | CitationWebSearchResultLocation
479
+ | CitationSearchResultLocation;
480
+
481
+ /**
482
+ * Citation from a plain text document.
483
+ * Character indices are 0-based; `end_char_index` is exclusive.
484
+ */
485
+ export interface CitationCharLocation {
486
+ type: 'char_location';
487
+ cited_text: string;
488
+ document_index: number;
489
+ document_title: string | null;
490
+ start_char_index: number;
491
+ /** Exclusive upper bound. */
492
+ end_char_index: number;
493
+ /** Non-null only when the document was provided via the Files API. */
494
+ file_id: string | null;
495
+ }
496
+
497
+ /**
498
+ * Citation from a PDF document.
499
+ * Page numbers are 1-based; `end_page_number` is exclusive.
500
+ */
501
+ export interface CitationPageLocation {
502
+ type: 'page_location';
503
+ cited_text: string;
504
+ document_index: number;
505
+ document_title: string | null;
506
+ start_page_number: number;
507
+ /** Exclusive upper bound. */
508
+ end_page_number: number;
509
+ /** Non-null only when the document was provided via the Files API. */
510
+ file_id: string | null;
511
+ }
512
+
513
+ /**
514
+ * Citation from a custom content document.
515
+ * Block indices are 0-based; `end_block_index` is exclusive.
516
+ */
517
+ export interface CitationContentBlockLocation {
518
+ type: 'content_block_location';
519
+ cited_text: string;
520
+ document_index: number;
521
+ document_title: string | null;
522
+ start_block_index: number;
523
+ /** Exclusive upper bound. */
524
+ end_block_index: number;
525
+ /** Non-null only when the document was provided via the Files API. */
526
+ file_id: string | null;
527
+ }
528
+
529
+ /** Citation from a web search result. */
530
+ export interface CitationWebSearchResultLocation {
531
+ type: 'web_search_result_location';
532
+ cited_text: string;
533
+ encrypted_index: string;
534
+ title: string | null;
535
+ url: string;
536
+ }
537
+
538
+ /**
539
+ * Citation from a `search_result` content block (RAG applications).
540
+ * Block indices are 0-based; `end_block_index` is exclusive.
541
+ */
542
+ export interface CitationSearchResultLocation {
543
+ type: 'search_result_location';
544
+ cited_text: string;
545
+ source: string;
546
+ title: string | null;
547
+ search_result_index: number;
548
+ start_block_index: number;
549
+ /** Exclusive upper bound. */
550
+ end_block_index: number;
551
+ }
552
+
553
+ // ---------------------------------------------------------------------------
554
+ // Web search results
555
+ // ---------------------------------------------------------------------------
556
+
557
+ /** Error returned by the server-side web search tool. */
558
+ export interface WebSearchResultError {
559
+ type: 'web_search_error';
560
+ error_code: string;
561
+ message: string;
562
+ }
563
+
564
+ /** A single web search result item. */
565
+ export interface WebSearchResultItem {
566
+ type: 'web_search_result';
567
+ url: string;
568
+ title: string;
569
+ encrypted_content: string;
570
+ page_age?: string | null;
571
+ }
572
+
573
+ // ---------------------------------------------------------------------------
574
+ // Usage & metadata
575
+ // ---------------------------------------------------------------------------
576
+
577
+ /**
578
+ * Token usage for a single API response.
579
+ *
580
+ * **Total billable input tokens** = `input_tokens + cache_creation_input_tokens + cache_read_input_tokens`.
581
+ *
582
+ * **Caveat:** `cache_read_input_tokens` can be inflated when server tools
583
+ * (e.g. web search) are used, because the API accumulates cache reads from
584
+ * multiple internal calls.
585
+ */
586
+ export interface Usage {
587
+ input_tokens: number;
588
+ output_tokens: number;
589
+ /** Tokens written to cache. `0` when prompt caching is not configured. */
590
+ cache_creation_input_tokens?: number | null;
591
+ /** Tokens read from cache. `0` when prompt caching is not configured. */
592
+ cache_read_input_tokens?: number | null;
593
+ /** Breakdown of cache creation by TTL. */
594
+ cache_creation?: CacheCreation | null;
595
+ /** Present only when web search was used. */
596
+ server_tool_use?: ServerToolUsage | null;
597
+ service_tier?: 'standard' | 'priority' | 'batch' | null;
598
+ /** Geographic region where inference ran (e.g. `"us-west-2"`). */
599
+ inference_geo?: string | null;
600
+ }
601
+
602
+ /** Cache creation breakdown by TTL tier. */
603
+ export interface CacheCreation {
604
+ ephemeral_5m_input_tokens: number;
605
+ ephemeral_1h_input_tokens: number;
606
+ }
607
+
608
+ /** Server-side tool usage counters. */
609
+ export interface ServerToolUsage {
610
+ web_search_requests: number;
611
+ }
612
+
613
+ /** Metadata emitted with `compact_boundary` system entries. */
614
+ export interface CompactMetadata {
615
+ trigger: 'auto' | 'manual';
616
+ /** Token count before compaction. */
617
+ preTokens: number;
618
+ }
619
+
620
+ /** Metadata emitted with `microcompact_boundary` system entries. */
621
+ export interface MicrocompactMetadata {
622
+ trigger: 'auto' | 'manual';
623
+ /** Token count before compaction. */
624
+ preTokens: number;
625
+ tokensSaved: number;
626
+ compactedToolIds: string[];
627
+ }
628
+
629
+ /** Configuration for extended thinking budget. */
630
+ export interface ThinkingMetadata {
631
+ maxThinkingTokens: number;
632
+ }
633
+
634
+ /** A todo item tracked in the Claude Code task list. */
635
+ export interface Todo {
636
+ content: string;
637
+ status: string;
638
+ /** Present continuous form shown in the spinner (e.g. `"Running tests"`). */
639
+ activeForm?: string;
640
+ }
641
+
642
+ // ---------------------------------------------------------------------------
643
+ // Enums / unions
644
+ // ---------------------------------------------------------------------------
645
+
646
+ /**
647
+ * Anthropic model identifiers. This list is non-exhaustive — Anthropic
648
+ * regularly adds new model IDs. Some models have aliases that resolve to the
649
+ * same underlying model (e.g. `"claude-sonnet-4-0"` and `"claude-sonnet-4-20250514"`).
650
+ *
651
+ * The `"<synthetic>"` value is specific to Claude Code for locally-generated messages.
652
+ */
653
+ export type Model =
654
+ | 'claude-opus-4-6'
655
+ | 'claude-opus-4-5-20251101'
656
+ | 'claude-sonnet-4-5-20250929'
657
+ | 'claude-haiku-4-5-20251001'
658
+ | '<synthetic>'
659
+ | (string & {});
660
+
661
+ /**
662
+ * Reason the model stopped generating.
663
+ *
664
+ * - `end_turn` — Natural stopping point. May have empty `content` array.
665
+ * - `max_tokens` — Hit `max_tokens` limit or the model's maximum.
666
+ * - `stop_sequence` — Generated one of the provided `stop_sequences`.
667
+ * - `tool_use` — Model invoked one or more tools.
668
+ * - `pause_turn` — Server-side sampling loop hit its iteration limit while executing server tools; pass the response back as-is to continue.
669
+ * - `refusal` — Streaming classifiers intervened for potential policy violation.
670
+ */
671
+ export type StopReason =
672
+ | 'end_turn'
673
+ | 'max_tokens'
674
+ | 'stop_sequence'
675
+ | 'tool_use'
676
+ | 'pause_turn'
677
+ | 'refusal';
678
+
679
+ /** Claude Code permission mode set by the user. */
680
+ export type PermissionMode =
681
+ | 'default'
682
+ | 'plan'
683
+ | 'acceptEdits'
684
+ | 'dontAsk'
685
+ | 'bypassPermissions';
686
+
687
+ /**
688
+ * Built-in Claude Code tools. This list is non-exhaustive and may change
689
+ * between Claude Code versions. MCP tools use the `mcp__<server>__<tool>` pattern.
690
+ */
691
+ export type BuiltinToolName =
692
+ | 'AskUserQuestion'
693
+ | 'Bash'
694
+ | 'Edit'
695
+ | 'EnterPlanMode'
696
+ | 'ExitPlanMode'
697
+ | 'Glob'
698
+ | 'Grep'
699
+ | 'KillShell'
700
+ | 'ListMcpResourcesTool'
701
+ | 'NotebookEdit'
702
+ | 'Read'
703
+ | 'ReadMcpResourceTool'
704
+ | 'SendMessage'
705
+ | 'Skill'
706
+ | 'Task'
707
+ | 'TaskCreate'
708
+ | 'TaskGet'
709
+ | 'TaskList'
710
+ | 'TaskOutput'
711
+ | 'TaskStop'
712
+ | 'TaskUpdate'
713
+ | 'TodoWrite'
714
+ | 'WebFetch'
715
+ | 'WebSearch'
716
+ | 'Write';
717
+
718
+ // ---------------------------------------------------------------------------
719
+ // Parser helper type
720
+ // ---------------------------------------------------------------------------
721
+
722
+ /**
723
+ * Signature for a function that parses a single JSONL line into a typed entry.
724
+ *
725
+ * @example
726
+ * ```ts
727
+ * const parseLine: ParseLine = (line) => {
728
+ * try { return JSON.parse(line) as TranscriptEntry; }
729
+ * catch { return null; }
730
+ * };
731
+ * ```
732
+ */
733
+ export type ParseLine = (line: string) => TranscriptEntry | null;