forked from 777genius/claude-code-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.ts
More file actions
415 lines (363 loc) · 10.8 KB
/
Copy pathmessage.ts
File metadata and controls
415 lines (363 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/**
* Pure message type definitions extracted to break import cycles.
*
* This file contains only type definitions with no runtime dependencies.
* Message types are discriminated unions based on `.type` field.
* System messages are further discriminated by `.subtype`.
*/
import type { UUID } from 'crypto'
import type {
BetaContentBlock,
BetaMessage,
BetaRawMessageStreamEvent,
} from '@anthropic-ai/sdk/resources/beta/messages/messages.mjs'
import type {
ContentBlockParam,
} from '@anthropic-ai/sdk/resources/messages.mjs'
import type { APIError } from '@anthropic-ai/sdk'
import type { PermissionMode } from './permissions.js'
// ============================================================================
// Scalar / Enum Types
// ============================================================================
/** Where a message originated. undefined = human (typed at keyboard). */
export type MessageOrigin =
| 'agent'
| 'teammate'
| 'command'
| 'system'
| 'hook'
| undefined
/** Direction for partial compact summarization. */
export type PartialCompactDirection = 'earlier' | 'later'
/** System message severity levels. */
export type SystemMessageLevel = 'info' | 'warning' | 'error'
/** Hook execution info for stop hooks. */
export interface StopHookInfo {
hookName: string
executionTime?: number
success: boolean
error?: string
}
// ============================================================================
// Progress Types
// ============================================================================
/** Generic progress data for ongoing tool operations. */
export interface Progress {
type: string
[key: string]: unknown
}
/** Progress message for streaming tool execution updates. */
export interface ProgressMessage<P extends Progress = Progress> {
type: 'progress'
data: P
toolUseID: string
parentToolUseID: string
uuid: UUID
timestamp: string
}
// ============================================================================
// AssistantMessage
// ============================================================================
export interface AssistantMessage {
type: 'assistant'
uuid: UUID
timestamp: string
message: BetaMessage
requestId?: string
isMeta?: true
isVirtual?: true
isApiErrorMessage?: boolean
apiError?: string
error?: unknown
errorDetails?: string
advisorModel?: string
/** AgentId of the agent that produced this message. */
agentId?: string
/** Caller info for debugging/display. */
caller?: string
}
// ============================================================================
// UserMessage
// ============================================================================
export interface UserMessage {
type: 'user'
message: {
role: 'user'
content: string | ContentBlockParam[]
}
uuid: UUID
timestamp: string
isMeta?: true
isVisibleInTranscriptOnly?: true
isVirtual?: true
isCompactSummary?: true
toolUseResult?: unknown
mcpMeta?: {
_meta?: Record<string, unknown>
structuredContent?: Record<string, unknown>
}
imagePasteIds?: number[]
sourceToolAssistantUUID?: UUID
permissionMode?: PermissionMode
summarizeMetadata?: {
messagesSummarized: number
userContext?: string
direction?: PartialCompactDirection
}
origin?: MessageOrigin
}
// ============================================================================
// SystemMessage (base) + all subtypes
// ============================================================================
/** Base fields shared by all system messages. */
interface SystemMessageBase {
type: 'system'
uuid: UUID
timestamp: string
isMeta?: boolean
content?: string
level?: SystemMessageLevel
toolUseID?: string
}
export interface SystemInformationalMessage extends SystemMessageBase {
subtype: 'informational'
content: string
level: SystemMessageLevel
preventContinuation?: boolean
}
export interface SystemAPIErrorMessage extends SystemMessageBase {
subtype: 'api_error'
level: 'error'
error: APIError
cause?: Error
retryInMs: number
retryAttempt: number
maxRetries: number
}
export interface SystemLocalCommandMessage extends SystemMessageBase {
subtype: 'local_command'
content: string
level?: SystemMessageLevel
}
export interface SystemStopHookSummaryMessage extends SystemMessageBase {
subtype: 'stop_hook_summary'
hookCount: number
hookInfos: StopHookInfo[]
hookErrors: string[]
preventedContinuation: boolean
stopReason: string | undefined
hasOutput: boolean
level: SystemMessageLevel
hookLabel?: string
totalDurationMs?: number
}
export interface SystemBridgeStatusMessage extends SystemMessageBase {
subtype: 'bridge_status'
content: string
url: string
upgradeNudge?: string
}
export interface SystemTurnDurationMessage extends SystemMessageBase {
subtype: 'turn_duration'
durationMs: number
budgetTokens?: number
budgetLimit?: number
budgetNudges?: number
messageCount?: number
}
export interface SystemThinkingMessage extends SystemMessageBase {
subtype: 'thinking'
content: string
}
export interface SystemMemorySavedMessage extends SystemMessageBase {
subtype: 'memory_saved'
writtenPaths: string[]
}
export interface SystemAwaySummaryMessage extends SystemMessageBase {
subtype: 'away_summary'
content: string
}
export interface SystemAgentsKilledMessage extends SystemMessageBase {
subtype: 'agents_killed'
}
export interface SystemCompactBoundaryMessage extends SystemMessageBase {
subtype: 'compact_boundary'
content: string
compactMetadata?: {
trigger: 'manual' | 'auto'
preTokens: number
userContext?: string
messagesSummarized?: number
preservedSegment?: {
tailUuid?: string
headUuid?: string
}
}
logicalParentUuid?: UUID
}
export interface SystemMicrocompactBoundaryMessage extends SystemMessageBase {
subtype: 'microcompact_boundary'
content: string
microcompactMetadata?: {
trigger: 'auto'
preTokens: number
tokensSaved: number
compactedToolIds: string[]
clearedAttachmentUUIDs: string[]
}
}
export interface SystemPermissionRetryMessage extends SystemMessageBase {
subtype: 'permission_retry'
content: string
commands: string[]
}
export interface SystemScheduledTaskFireMessage extends SystemMessageBase {
subtype: 'scheduled_task_fire'
content: string
}
export interface SystemApiMetricsMessage extends SystemMessageBase {
subtype: 'api_metrics'
ttftMs: number
otps: number
isP50?: boolean
hookDurationMs?: number
turnDurationMs?: number
toolDurationMs?: number
classifierDurationMs?: number
toolCount?: number
hookCount?: number
classifierCount?: number
configWriteCount?: number
}
/** Discriminated union of all system message subtypes. */
export type SystemMessage =
| SystemInformationalMessage
| SystemAPIErrorMessage
| SystemLocalCommandMessage
| SystemStopHookSummaryMessage
| SystemBridgeStatusMessage
| SystemTurnDurationMessage
| SystemThinkingMessage
| SystemMemorySavedMessage
| SystemAwaySummaryMessage
| SystemAgentsKilledMessage
| SystemCompactBoundaryMessage
| SystemMicrocompactBoundaryMessage
| SystemPermissionRetryMessage
| SystemScheduledTaskFireMessage
| SystemApiMetricsMessage
// ============================================================================
// AttachmentMessage
// ============================================================================
export interface AttachmentMessage<A extends Record<string, unknown> = Record<string, unknown>> {
type: 'attachment'
attachment: A & { type: string }
uuid: UUID
timestamp: string
isMeta?: true
}
// ============================================================================
// TombstoneMessage
// ============================================================================
export interface TombstoneMessage {
type: 'tombstone'
originalType: 'assistant' | 'user' | 'system'
uuid: UUID
timestamp: string
}
// ============================================================================
// ToolUseSummaryMessage
// ============================================================================
export interface ToolUseSummaryMessage {
type: 'tool_use_summary'
summary: string
precedingToolUseIds: string[]
uuid: UUID
timestamp: string
}
// ============================================================================
// HookResultMessage
// ============================================================================
export interface HookResultMessage {
type: 'user'
message: {
role: 'user'
content: ContentBlockParam[]
}
uuid: UUID
timestamp: string
isMeta?: true
}
// ============================================================================
// Discriminated Message Union
// ============================================================================
/** Union of all message types used in the conversation history. */
export type Message =
| AssistantMessage
| UserMessage
| SystemMessage
| AttachmentMessage
| ProgressMessage
| TombstoneMessage
// ============================================================================
// Grouped / Collapsed display types
// ============================================================================
export interface GroupedToolUseMessage {
type: 'assistant'
uuid: UUID
timestamp: string
message: BetaMessage
toolUseCount: number
}
export interface CollapsedReadSearchGroup {
type: 'assistant'
uuid: UUID
timestamp: string
message: BetaMessage
collapsedCount: number
}
/** Messages that can be rendered in UI. */
export type RenderableMessage =
| AssistantMessage
| UserMessage
| SystemMessage
| AttachmentMessage
| GroupedToolUseMessage
| CollapsedReadSearchGroup
// ============================================================================
// Normalized Message Types
// ============================================================================
/** Normalized assistant message with single content block. */
export interface NormalizedAssistantMessage extends AssistantMessage {
message: BetaMessage & {
content: [BetaContentBlock]
}
}
/** Normalized user message with content always as array. */
export interface NormalizedUserMessage extends UserMessage {
message: {
role: 'user'
content: ContentBlockParam[]
}
}
/** Union of all normalized message types for API processing. */
export type NormalizedMessage =
| NormalizedAssistantMessage
| NormalizedUserMessage
| SystemMessage
| AttachmentMessage
| ProgressMessage
| TombstoneMessage
// ============================================================================
// Stream / Event Types
// ============================================================================
/** Event fired at the start of a stream request. */
export interface RequestStartEvent {
type: 'stream_request_start'
}
/** Wrapper for streaming events from the Anthropic API. */
export interface StreamEvent {
type: 'stream_event'
event: BetaRawMessageStreamEvent
ttftMs?: number
}