-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntimeEvent.ts
More file actions
449 lines (411 loc) · 15.1 KB
/
Copy pathruntimeEvent.ts
File metadata and controls
449 lines (411 loc) · 15.1 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import { z } from "zod";
/**
* Canonical chat-runtime events.
*
* The renderer's chat UI consumes only these — never provider-native messages.
* Each provider's adapter is responsible for translating its native protocol
* into this vocabulary (`acp/canonicalMapping.ts`, `codex/canonicalMapping.ts`).
*
* A discriminated `type` union with `itemId`-addressed updates so streaming
* deltas append to a known item rather than re-emitting the whole message.
*/
export const canonicalItemTypeSchema = z.enum([
"user_message",
"assistant_message",
"reasoning",
"plan",
"goal",
"command_execution",
"file_change",
"tool_call",
"mcp_tool_call",
"image_view",
"dynamic_tool_call",
"web_search",
"question_answer",
"error",
]);
export type CanonicalItemType = z.infer<typeof canonicalItemTypeSchema>;
export const canonicalRequestTypeSchema = z.enum([
"command_execution_approval",
"file_read_approval",
"file_change_approval",
"apply_patch_approval",
"tool_call_approval",
"tool_user_input",
"auth_refresh",
]);
export type CanonicalRequestType = z.infer<typeof canonicalRequestTypeSchema>;
export const runtimeContentStreamKindSchema = z.enum([
"assistant_text",
"reasoning_text",
"plan_text",
"command_output",
"file_change_output",
]);
export type RuntimeContentStreamKind = z.infer<typeof runtimeContentStreamKindSchema>;
export const canonicalContentBlockSchema = z.discriminatedUnion("kind", [
z.object({ kind: z.literal("text"), text: z.string() }),
z.object({ kind: z.literal("skill"), name: z.string(), invocation: z.string() }),
z.object({
kind: z.literal("image"),
mimeType: z.string(),
dataUrl: z.string(),
path: z.string().optional(),
name: z.string().optional(),
source: z.enum(["attachment", "mention"]).optional(),
}),
z.object({
kind: z.literal("file"),
path: z.string(),
name: z.string().optional(),
source: z.enum(["attachment", "mention"]).optional(),
}),
]);
export type CanonicalContentBlock = z.infer<typeof canonicalContentBlockSchema>;
// ── Per-item payload shapes ──────────────────────────────────────────
export const messageItemPayloadSchema = z.object({
content: z.array(canonicalContentBlockSchema),
});
export type MessageItemPayload = z.infer<typeof messageItemPayloadSchema>;
export const reasoningItemPayloadSchema = z.object({
summary: z.string().optional(),
durationMs: z.number().int().optional(),
});
export type ReasoningItemPayload = z.infer<typeof reasoningItemPayloadSchema>;
export const toolCallStatusSchema = z.enum(["running", "success", "error"]);
export const planStepStatusSchema = z.enum(["pending", "in_progress", "completed"]);
export const planItemPayloadSchema = z.object({
steps: z.array(
z.object({
step: z.string(),
status: planStepStatusSchema,
}),
),
});
export type PlanItemPayload = z.infer<typeof planItemPayloadSchema>;
export const goalStatusSchema = z.enum(["active", "paused", "budget_limited", "complete"]);
export type GoalStatus = z.infer<typeof goalStatusSchema>;
export const goalItemPayloadSchema = z.object({
action: z.enum(["set", "updated", "cleared", "viewed"]).optional(),
objective: z.string().optional(),
status: goalStatusSchema.optional(),
tokenBudget: z.number().int().nonnegative().nullable().optional(),
tokensUsed: z.number().int().nonnegative().optional(),
timeUsedSeconds: z.number().nonnegative().optional(),
providerThreadId: z.string().optional(),
updatedAt: z.number().optional(),
});
export type GoalItemPayload = z.infer<typeof goalItemPayloadSchema>;
export const commandExecutionPayloadSchema = z.object({
command: z.string(),
cwd: z.string().optional(),
exitCode: z.number().int().optional(),
durationMs: z.number().int().optional(),
status: toolCallStatusSchema.optional(),
errorMessage: z.string().optional(),
});
export type CommandExecutionPayload = z.infer<typeof commandExecutionPayloadSchema>;
export const fileChangeKindSchema = z.enum(["create", "edit", "delete"]);
export const fileChangePayloadSchema = z.object({
path: z.string(),
changeKind: fileChangeKindSchema,
diffSummary: z
.object({
added: z.number().int().nonnegative(),
removed: z.number().int().nonnegative(),
})
.optional(),
status: toolCallStatusSchema.optional(),
errorMessage: z.string().optional(),
});
export type FileChangePayload = z.infer<typeof fileChangePayloadSchema>;
/**
* Live progress signal a sub-agent (e.g. Claude `Task`) emits while running.
* Surfaced on the parent tool_call so a collapsed sub-agent row can still
* show what the inner agent is doing without expanding.
*/
export const toolCallProgressSchema = z.object({
description: z.string().optional(),
lastToolName: z.string().optional(),
/** Provider-reported model used by this sub-agent, when the provider exposes the actual sub-agent model. */
model: z.string().min(1).optional(),
/** Provider-reported reasoning effort used by this sub-agent, when available. */
effort: z.string().min(1).optional(),
summary: z.string().optional(),
tokens: z.number().int().nonnegative().optional(),
toolUses: z.number().int().nonnegative().optional(),
durationMs: z.number().int().nonnegative().optional(),
/**
* Number of child items the sub-agent has started so far. Surfaced on the
* collapsed sub-agent pill so closing the overlay (which gates child events
* off the IPC stream for perf) doesn't lose the live counter.
*/
stepCount: z.number().int().nonnegative().optional(),
});
export type ToolCallProgress = z.infer<typeof toolCallProgressSchema>;
export const acpToolKindSchema = z.enum([
"read",
"edit",
"delete",
"move",
"search",
"execute",
"think",
"fetch",
"switch_mode",
"other",
]);
export type AcpToolKind = z.infer<typeof acpToolKindSchema>;
export const acpToolLocationSchema = z.object({
path: z.string(),
line: z.number().int().optional(),
});
export type AcpToolLocation = z.infer<typeof acpToolLocationSchema>;
export const toolCallPayloadSchema = z.object({
name: z.string(),
title: z.string().optional(),
kind: acpToolKindSchema.optional(),
locations: z.array(acpToolLocationSchema).optional(),
serverId: z.string().optional(),
args: z.unknown().optional(),
result: z.unknown().optional(),
/**
* Inline images produced by the tool, as renderable `data:` URLs. Populated by
* the agent mappers when a tool result carries image content blocks (ACP
* `{ type: "image", data, mimeType }`, Claude `{ type: "image", source: { … } }`)
* so the renderer can show them inline. Codex's `imageGeneration` instead
* carries its base64 on `result`; both are handled by the renderer.
*/
images: z.array(z.string()).optional(),
status: toolCallStatusSchema,
progress: toolCallProgressSchema.optional(),
isSubAgent: z.boolean().optional(),
});
export type ToolCallPayload = z.infer<typeof toolCallPayloadSchema>;
export const webSearchPayloadSchema = z.object({
query: z.string(),
resultCount: z.number().int().optional(),
});
export type WebSearchPayload = z.infer<typeof webSearchPayloadSchema>;
export const errorItemPayloadSchema = z.object({
message: z.string(),
});
export type ErrorItemPayload = z.infer<typeof errorItemPayloadSchema>;
/**
* Provider-agnostic record of a user's reply to a structured user-input request
* (e.g. Claude's `AskUserQuestion`, Codex's user_input, ACP elicitation forms).
* Renders as a compact inline module showing each question, the chosen options
* with their descriptions, and any custom freeform answer the user typed.
*/
export const questionAnswerSelectionSchema = z.object({
label: z.string(),
description: z.string().optional(),
});
export type QuestionAnswerSelection = z.infer<typeof questionAnswerSelectionSchema>;
export const questionAnswerEntrySchema = z.object({
header: z.string(),
question: z.string(),
selected: z.array(questionAnswerSelectionSchema),
customAnswer: z.string().optional(),
});
export type QuestionAnswerEntry = z.infer<typeof questionAnswerEntrySchema>;
export const questionAnswerItemPayloadSchema = z.object({
questions: z.array(questionAnswerEntrySchema),
});
export type QuestionAnswerItemPayload = z.infer<typeof questionAnswerItemPayloadSchema>;
export const contextUsageBreakdownEntrySchema = z.object({
id: z.string().min(1),
label: z.string().min(1),
tokens: z.number().int().nonnegative(),
});
export type ContextUsageBreakdownEntry = z.infer<typeof contextUsageBreakdownEntrySchema>;
export const threadContextUsageSchema = z.object({
usedTokens: z.number().int().nonnegative().optional(),
maxTokens: z.number().int().positive().optional(),
breakdown: z.array(contextUsageBreakdownEntrySchema).optional(),
});
export type ThreadContextUsage = z.infer<typeof threadContextUsageSchema>;
// ── Request payloads ─────────────────────────────────────────────────
export const userInputOptionSchema = z.object({
optionId: z.string(),
label: z.string(),
description: z.string().optional(),
});
export type UserInputOption = z.infer<typeof userInputOptionSchema>;
export const permissionUpdateDestinationSchema = z.enum([
"userSettings",
"projectSettings",
"localSettings",
"session",
"cliArg",
]);
export type PermissionUpdateDestination = z.infer<typeof permissionUpdateDestinationSchema>;
export const permissionBehaviorSchema = z.enum(["allow", "deny", "ask"]);
export type PermissionBehavior = z.infer<typeof permissionBehaviorSchema>;
const permissionRuleSchema = z.object({
toolName: z.string(),
ruleContent: z.string().optional(),
});
export const permissionSuggestionSchema = z.discriminatedUnion("type", [
z.object({
type: z.literal("addRules"),
rules: z.array(permissionRuleSchema),
behavior: permissionBehaviorSchema,
destination: permissionUpdateDestinationSchema,
}),
z.object({
type: z.literal("replaceRules"),
rules: z.array(permissionRuleSchema),
behavior: permissionBehaviorSchema,
destination: permissionUpdateDestinationSchema,
}),
z.object({
type: z.literal("removeRules"),
rules: z.array(permissionRuleSchema),
behavior: permissionBehaviorSchema,
destination: permissionUpdateDestinationSchema,
}),
z.object({
type: z.literal("setMode"),
mode: z.string(),
destination: permissionUpdateDestinationSchema,
}),
z.object({
type: z.literal("addDirectories"),
directories: z.array(z.string()),
destination: permissionUpdateDestinationSchema,
}),
z.object({
type: z.literal("removeDirectories"),
directories: z.array(z.string()),
destination: permissionUpdateDestinationSchema,
}),
]);
export type PermissionSuggestion = z.infer<typeof permissionSuggestionSchema>;
/**
* Typed shape for `RequestPayload.details` when the request originates from a
* tool-permission prompt (Claude SDK `canUseTool`, ACP equivalents). Renderers
* may consume this directly when present; `details` itself stays `unknown` so
* adapters can carry arbitrary shapes for non-permission requests.
*/
export const permissionRequestDetailsSchema = z.object({
toolName: z.string(),
displayName: z.string().optional(),
description: z.string().optional(),
blockedPath: z.string().optional(),
decisionReason: z.string().optional(),
toolUseID: z.string().optional(),
input: z.unknown().optional(),
suggestions: z.array(permissionSuggestionSchema).optional(),
});
export type PermissionRequestDetails = z.infer<typeof permissionRequestDetailsSchema>;
export const requestPayloadSchema = z.object({
/** Human-readable summary of what is being asked. */
summary: z.string(),
/** Free-form details (path / command / patch text — depends on `requestType`). */
details: z.unknown().optional(),
/** When the request is a multi-option pick (tool_user_input). */
options: z.array(userInputOptionSchema).optional(),
multiSelect: z.boolean().optional(),
});
export type RequestPayload = z.infer<typeof requestPayloadSchema>;
/**
* Decode `RequestPayload.details` as a permission-request shape. Returns
* `undefined` if details is absent or doesn't conform — callers should fall
* back to the raw `details` for non-permission requests.
*/
export function asPermissionRequestDetails(details: unknown): PermissionRequestDetails | undefined {
if (!details || typeof details !== "object") return undefined;
const parsed = permissionRequestDetailsSchema.safeParse(details);
return parsed.success ? parsed.data : undefined;
}
// ── Discriminated event union ────────────────────────────────────────
export const turnStateSchema = z.enum(["completed", "failed", "interrupted", "cancelled"]);
export type TurnState = z.infer<typeof turnStateSchema>;
export const requestOutcomeSchema = z.enum(["accepted", "declined", "answered", "cancelled"]);
export type RequestOutcome = z.infer<typeof requestOutcomeSchema>;
export const runtimeEventSchema = z.discriminatedUnion("type", [
z.object({
type: z.literal("session.started"),
threadId: z.string(),
turnId: z.string().optional(),
}),
z.object({
type: z.literal("session.exited"),
threadId: z.string(),
reason: z.string().optional(),
}),
z.object({
type: z.literal("turn.started"),
threadId: z.string(),
turnId: z.string(),
}),
z.object({
type: z.literal("turn.completed"),
threadId: z.string(),
turnId: z.string(),
state: turnStateSchema,
}),
z.object({
type: z.literal("item.started"),
threadId: z.string(),
itemId: z.string(),
itemType: canonicalItemTypeSchema,
payload: z.unknown().optional(),
/**
* Set when the item was emitted by a sub-agent (e.g. Claude `Task` tool use).
* The renderer groups items with the same `parentItemId` under their parent
* tool_call row instead of rendering them at the top of the chat timeline.
*/
parentItemId: z.string().optional(),
}),
z.object({
type: z.literal("item.updated"),
threadId: z.string(),
itemId: z.string(),
payload: z.unknown(),
}),
z.object({
type: z.literal("item.completed"),
threadId: z.string(),
itemId: z.string(),
payload: z.unknown().optional(),
}),
z.object({
type: z.literal("content.delta"),
threadId: z.string(),
itemId: z.string(),
stream: runtimeContentStreamKindSchema,
delta: z.string(),
}),
z.object({
type: z.literal("context.updated"),
threadId: z.string(),
usage: threadContextUsageSchema,
}),
z.object({
type: z.literal("request.opened"),
threadId: z.string(),
requestId: z.string(),
requestType: canonicalRequestTypeSchema,
payload: requestPayloadSchema,
}),
z.object({
type: z.literal("request.resolved"),
threadId: z.string(),
requestId: z.string(),
outcome: requestOutcomeSchema,
}),
z.object({
type: z.literal("warning"),
threadId: z.string(),
message: z.string(),
}),
z.object({
type: z.literal("error"),
threadId: z.string(),
message: z.string(),
}),
]);
export type RuntimeEvent = z.infer<typeof runtimeEventSchema>;