stitchkit 0.129.0 → 0.130.1

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 (163) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/dist/agent-executor/acp/v1-connect.d.ts +87 -19
  3. package/dist/agent-executor/acp/v1-connect.d.ts.map +1 -1
  4. package/dist/agent-executor/acp/v1-driver.d.ts +4 -2
  5. package/dist/agent-executor/acp/v1-driver.d.ts.map +1 -1
  6. package/dist/agent-executor/acp/v1-handlers.d.ts +56 -0
  7. package/dist/agent-executor/acp/v1-handlers.d.ts.map +1 -0
  8. package/dist/agent-executor/acp/v1-map.d.ts +13 -2
  9. package/dist/agent-executor/acp/v1-map.d.ts.map +1 -1
  10. package/dist/agent-executor/acp/v1-permission.d.ts +44 -0
  11. package/dist/agent-executor/acp/v1-permission.d.ts.map +1 -0
  12. package/dist/agent-executor/acp/v1-protocol.d.ts +63 -10
  13. package/dist/agent-executor/acp/v1-protocol.d.ts.map +1 -1
  14. package/dist/agent-executor/acp/v1-session.d.ts +34 -7
  15. package/dist/agent-executor/acp/v1-session.d.ts.map +1 -1
  16. package/dist/agent-executor/acp/v1-turn.d.ts +51 -13
  17. package/dist/agent-executor/acp/v1-turn.d.ts.map +1 -1
  18. package/dist/agent-executor/claude/classify.d.ts +10 -0
  19. package/dist/agent-executor/claude/classify.d.ts.map +1 -1
  20. package/dist/agent-executor/claude/cli-driver.d.ts +4 -1
  21. package/dist/agent-executor/claude/cli-driver.d.ts.map +1 -1
  22. package/dist/agent-executor/claude/events.d.ts +146 -0
  23. package/dist/agent-executor/claude/events.d.ts.map +1 -0
  24. package/dist/agent-executor/claude/limits.d.ts +7 -1
  25. package/dist/agent-executor/claude/limits.d.ts.map +1 -1
  26. package/dist/agent-executor/claude/protocol.d.ts +49 -9
  27. package/dist/agent-executor/claude/protocol.d.ts.map +1 -1
  28. package/dist/agent-executor/claude/session.d.ts +2 -0
  29. package/dist/agent-executor/claude/session.d.ts.map +1 -1
  30. package/dist/agent-executor/claude-sdk/controls.d.ts +10 -1
  31. package/dist/agent-executor/claude-sdk/controls.d.ts.map +1 -1
  32. package/dist/agent-executor/claude-sdk/driver.d.ts +97 -3
  33. package/dist/agent-executor/claude-sdk/driver.d.ts.map +1 -1
  34. package/dist/agent-executor/claude-sdk/probe.d.ts +29 -0
  35. package/dist/agent-executor/claude-sdk/probe.d.ts.map +1 -0
  36. package/dist/agent-executor/claude-sdk/query-loop.d.ts +53 -5
  37. package/dist/agent-executor/claude-sdk/query-loop.d.ts.map +1 -1
  38. package/dist/agent-executor/claude-sdk/query-options.d.ts +2 -0
  39. package/dist/agent-executor/claude-sdk/query-options.d.ts.map +1 -1
  40. package/dist/agent-executor/claude-sdk/session.d.ts +2 -3
  41. package/dist/agent-executor/claude-sdk/session.d.ts.map +1 -1
  42. package/dist/agent-executor/claude-sdk/turn-usage.d.ts +81 -0
  43. package/dist/agent-executor/claude-sdk/turn-usage.d.ts.map +1 -0
  44. package/dist/agent-executor/codex/classify.d.ts +4 -0
  45. package/dist/agent-executor/codex/classify.d.ts.map +1 -1
  46. package/dist/agent-executor/codex/connect.d.ts +5 -2
  47. package/dist/agent-executor/codex/connect.d.ts.map +1 -1
  48. package/dist/agent-executor/codex/control-client.d.ts +206 -0
  49. package/dist/agent-executor/codex/control-client.d.ts.map +1 -0
  50. package/dist/agent-executor/codex/driver.d.ts +35 -5
  51. package/dist/agent-executor/codex/driver.d.ts.map +1 -1
  52. package/dist/agent-executor/codex/handlers.d.ts +52 -0
  53. package/dist/agent-executor/codex/handlers.d.ts.map +1 -0
  54. package/dist/agent-executor/codex/limits.d.ts.map +1 -1
  55. package/dist/agent-executor/codex/protocol.d.ts +128 -0
  56. package/dist/agent-executor/codex/protocol.d.ts.map +1 -1
  57. package/dist/agent-executor/codex/request-answers.d.ts +101 -0
  58. package/dist/agent-executor/codex/request-answers.d.ts.map +1 -0
  59. package/dist/agent-executor/codex/session-controls.d.ts +124 -61
  60. package/dist/agent-executor/codex/session-controls.d.ts.map +1 -1
  61. package/dist/agent-executor/codex/session.d.ts +10 -36
  62. package/dist/agent-executor/codex/session.d.ts.map +1 -1
  63. package/dist/agent-executor/codex/shared-server.d.ts +31 -0
  64. package/dist/agent-executor/codex/shared-server.d.ts.map +1 -0
  65. package/dist/agent-executor/codex/shared-thread.d.ts +46 -0
  66. package/dist/agent-executor/codex/shared-thread.d.ts.map +1 -0
  67. package/dist/agent-executor/codex/thread-controls.d.ts +144 -0
  68. package/dist/agent-executor/codex/thread-controls.d.ts.map +1 -0
  69. package/dist/agent-executor/codex/thread-open.d.ts +66 -0
  70. package/dist/agent-executor/codex/thread-open.d.ts.map +1 -0
  71. package/dist/agent-executor/codex/turn-input.d.ts +47 -0
  72. package/dist/agent-executor/codex/turn-input.d.ts.map +1 -0
  73. package/dist/agent-executor/contract.d.ts +77 -0
  74. package/dist/agent-executor/contract.d.ts.map +1 -1
  75. package/dist/agent-executor/engine.d.ts +6 -2
  76. package/dist/agent-executor/engine.d.ts.map +1 -1
  77. package/dist/agent-executor/executor-contract.d.ts +54 -0
  78. package/dist/agent-executor/executor-contract.d.ts.map +1 -1
  79. package/dist/agent-executor/handoff-schemas.d.ts +407 -0
  80. package/dist/agent-executor/handoff-schemas.d.ts.map +1 -0
  81. package/dist/agent-executor/handoff.d.ts +12 -0
  82. package/dist/agent-executor/handoff.d.ts.map +1 -0
  83. package/dist/agent-executor/hello.d.ts +72 -0
  84. package/dist/agent-executor/hello.d.ts.map +1 -1
  85. package/dist/agent-executor/image-input.d.ts +22 -0
  86. package/dist/agent-executor/image-input.d.ts.map +1 -1
  87. package/dist/agent-executor/projection.d.ts +2 -2
  88. package/dist/agent-executor/projection.d.ts.map +1 -1
  89. package/dist/agent-executor/session-primitives.d.ts +10 -1
  90. package/dist/agent-executor/session-primitives.d.ts.map +1 -1
  91. package/dist/agent-executor/testing/conformance.d.ts.map +1 -1
  92. package/dist/agent-executor/testing/replay-driver.d.ts +41 -0
  93. package/dist/agent-executor/testing/replay-driver.d.ts.map +1 -0
  94. package/dist/agent-executor/testing/replay-gate.d.ts +11 -0
  95. package/dist/agent-executor/testing/replay-gate.d.ts.map +1 -0
  96. package/dist/agent-executor/testing/replay-runner.d.ts +27 -0
  97. package/dist/agent-executor/testing/replay-runner.d.ts.map +1 -0
  98. package/dist/agent-executor/testing/replay-scenarios.d.ts +21 -0
  99. package/dist/agent-executor/testing/replay-scenarios.d.ts.map +1 -0
  100. package/dist/agent-executor/testing/replay-transcript.d.ts +296 -0
  101. package/dist/agent-executor/testing/replay-transcript.d.ts.map +1 -0
  102. package/dist/agent-executor/vocabulary.d.ts +135 -0
  103. package/dist/agent-executor/vocabulary.d.ts.map +1 -1
  104. package/dist/agent-runtime/branch-observations.d.ts +18 -18
  105. package/dist/agent-runtime/branch-provider.d.ts +2 -2
  106. package/dist/agent-runtime/deferred-tool-selection.d.ts.map +1 -1
  107. package/dist/agent-runtime/sql-rows.d.ts +10 -10
  108. package/dist/agent-runtime/store-conformance-lifecycle.d.ts +20 -20
  109. package/dist/agent-runtime/store-conformance-support.d.ts +10 -10
  110. package/dist/agent-runtime/terminal-commit.d.ts +10 -10
  111. package/dist/entrypoints/agent-executor/acp.d.ts +4 -1
  112. package/dist/entrypoints/agent-executor/acp.d.ts.map +1 -1
  113. package/dist/entrypoints/agent-executor/acp.js +847 -214
  114. package/dist/entrypoints/agent-executor/claude-sdk.d.ts +5 -1
  115. package/dist/entrypoints/agent-executor/claude-sdk.d.ts.map +1 -1
  116. package/dist/entrypoints/agent-executor/claude-sdk.js +713 -205
  117. package/dist/entrypoints/agent-executor/claude.js +44 -19
  118. package/dist/entrypoints/agent-executor/codex.d.ts +10 -2
  119. package/dist/entrypoints/agent-executor/codex.d.ts.map +1 -1
  120. package/dist/entrypoints/agent-executor/codex.js +2269 -600
  121. package/dist/entrypoints/agent-executor/testing.d.ts +4 -0
  122. package/dist/entrypoints/agent-executor/testing.d.ts.map +1 -1
  123. package/dist/entrypoints/agent-executor/testing.js +1002 -2
  124. package/dist/entrypoints/agent-executor.d.ts +4 -2
  125. package/dist/entrypoints/agent-executor.d.ts.map +1 -1
  126. package/dist/entrypoints/agent-executor.js +411 -23
  127. package/dist/entrypoints/agent-runtime.js +4 -23
  128. package/dist/entrypoints/tools/mcp.d.ts +1 -0
  129. package/dist/entrypoints/tools/mcp.d.ts.map +1 -1
  130. package/dist/entrypoints/tools/mcp.js +1 -1
  131. package/dist/entrypoints/tools/toolkit.js +1 -1
  132. package/dist/{index-wv2amqm4.js → index-2s8sy57y.js} +36 -103
  133. package/dist/{index-afb6z77w.js → index-dbp6xemn.js} +75 -7
  134. package/dist/{index-bjmj2q1z.js → index-jrn3prgm.js} +1 -1
  135. package/dist/index-kne9g2c5.js +30 -0
  136. package/dist/index-p1qmpq0p.js +1300 -0
  137. package/dist/{index-7jqn3b3g.js → index-v759mzed.js} +152 -2
  138. package/dist/{index-cbhxhma2.js → index-x5ayy5bn.js} +1 -0
  139. package/dist/index-y0a4gt1s.js +142 -0
  140. package/dist/tools/internal/rank-tools.d.ts +10 -0
  141. package/dist/tools/internal/rank-tools.d.ts.map +1 -0
  142. package/dist/tools/mcp/handler.d.ts +7 -3
  143. package/dist/tools/mcp/handler.d.ts.map +1 -1
  144. package/dist/tools/mcp/lazy-discovery.d.ts +47 -0
  145. package/dist/tools/mcp/lazy-discovery.d.ts.map +1 -0
  146. package/dist/tools/mcp/mount.d.ts +7 -0
  147. package/dist/tools/mcp/mount.d.ts.map +1 -1
  148. package/dist/tools/mcp/register.d.ts +2 -2
  149. package/dist/tools/mcp/register.d.ts.map +1 -1
  150. package/llms/agent-executor-acp.txt +7 -8
  151. package/llms/agent-executor-claude-sdk.txt +4 -2
  152. package/llms/agent-executor-codex.txt +8 -4
  153. package/llms/agent-executor-testing.txt +5 -1
  154. package/llms/agent-executor.txt +7 -6
  155. package/llms/stitchkit.txt +74 -74
  156. package/llms/tools-mcp.txt +3 -2
  157. package/llms/tools.txt +24 -0
  158. package/llms/{upgrading-0.128.0-0.129.0.txt → upgrading-0.128.0-0.130.0.txt} +30 -3
  159. package/llms/upgrading.txt +1 -1
  160. package/llms-full.txt +159 -97
  161. package/llms.txt +8 -8
  162. package/package.json +1 -1
  163. package/dist/index-y71zn1gw.js +0 -586
@@ -1,586 +0,0 @@
1
- import {
2
- PlanWindowSchema2,
3
- engineProfile
4
- } from "./index-afb6z77w.js";
5
- import {
6
- nonEmptyString,
7
- epochSecondsToIso,
8
- safeJsonValue,
9
- isSupportedImageMediaType
10
- } from "./index-wv2amqm4.js";
11
- import {
12
- errnoOf
13
- } from "./index-jj9228tt.js";
14
-
15
- // src/agent-executor/claude/protocol.ts
16
- import { z } from "zod";
17
- var TextBlock = z.looseObject({ type: z.literal("text"), text: z.string() });
18
- var ThinkingBlock = z.looseObject({
19
- type: z.literal("thinking"),
20
- thinking: z.string().optional()
21
- });
22
- var ToolUseBlock = z.looseObject({
23
- type: z.literal("tool_use"),
24
- id: z.string().min(1),
25
- name: z.string().min(1),
26
- input: z.json()
27
- });
28
- var ToolResultBlock = z.looseObject({
29
- type: z.literal("tool_result"),
30
- tool_use_id: z.string().min(1),
31
- content: z.json().optional(),
32
- is_error: z.boolean().optional()
33
- });
34
- var OtherBlock = z.looseObject({ type: z.string() });
35
- var ClaudeContentBlockSchema = z.union([
36
- TextBlock,
37
- ThinkingBlock,
38
- ToolUseBlock,
39
- ToolResultBlock,
40
- OtherBlock
41
- ]);
42
- var Usage = z.looseObject({
43
- input_tokens: z.int().nonnegative().optional(),
44
- output_tokens: z.int().nonnegative().optional(),
45
- cache_read_input_tokens: z.int().nonnegative().optional()
46
- });
47
- var ClaudeRecordSchema = z.discriminatedUnion("type", [
48
- z.looseObject({
49
- type: z.literal("system"),
50
- subtype: z.string(),
51
- session_id: z.string().optional(),
52
- model: z.string().optional()
53
- }),
54
- z.looseObject({
55
- type: z.literal("user"),
56
- uuid: z.string().optional(),
57
- isReplay: z.boolean().optional(),
58
- message: z.looseObject({
59
- content: z.union([z.string(), z.array(ClaudeContentBlockSchema)])
60
- })
61
- }),
62
- z.looseObject({
63
- type: z.literal("assistant"),
64
- message: z.looseObject({
65
- id: z.string().min(1),
66
- content: z.array(ClaudeContentBlockSchema)
67
- })
68
- }),
69
- z.looseObject({
70
- type: z.literal("stream_event"),
71
- api_message_id: z.string().optional(),
72
- event: z.looseObject({
73
- type: z.string(),
74
- index: z.int().nonnegative().optional(),
75
- delta: z.union([
76
- z.looseObject({ type: z.literal("text_delta"), text: z.string() }),
77
- z.looseObject({ type: z.literal("thinking_delta"), thinking: z.string() }),
78
- z.looseObject({ type: z.string().optional() })
79
- ]).optional(),
80
- message: z.looseObject({ id: z.string().optional() }).optional()
81
- })
82
- }),
83
- z.looseObject({
84
- type: z.literal("result"),
85
- subtype: z.string(),
86
- is_error: z.boolean(),
87
- session_id: z.string().optional(),
88
- terminal_reason: z.string().optional(),
89
- api_error_status: z.union([z.int(), z.string()]).nullable().optional(),
90
- result: z.string().optional(),
91
- total_cost_usd: z.number().nonnegative().optional(),
92
- usage: Usage.optional()
93
- }),
94
- z.looseObject({ type: z.literal("rate_limit_event"), rate_limit_info: z.looseObject({}) }),
95
- z.looseObject({
96
- type: z.literal("control_request"),
97
- request_id: z.string().min(1),
98
- request: z.looseObject({
99
- subtype: z.string(),
100
- tool_name: z.string().optional(),
101
- input: z.json().optional(),
102
- tool_use_id: z.string().optional()
103
- })
104
- }),
105
- z.looseObject({
106
- type: z.literal("control_response"),
107
- response: z.looseObject({ subtype: z.string(), request_id: z.string().optional() })
108
- }),
109
- z.looseObject({
110
- type: z.literal("command_lifecycle"),
111
- command_uuid: z.string().optional(),
112
- state: z.string()
113
- })
114
- ]);
115
- var ClaudeUnknownRecordSchema = z.looseObject({ type: z.string() });
116
- function claudeUserContent(parts) {
117
- if (parts.every((part) => part.type === "text")) {
118
- return parts.map((part) => part.text).join(`
119
- `);
120
- }
121
- return parts.map((part) => {
122
- if (part.type === "text")
123
- return { type: "text", text: part.text };
124
- if (!isSupportedImageMediaType(part.mediaType)) {
125
- throw new TypeError(`Unsupported Claude image media type: ${part.mediaType}`);
126
- }
127
- return {
128
- type: "image",
129
- source: { type: "base64", media_type: part.mediaType, data: part.base64 }
130
- };
131
- });
132
- }
133
- function claudeUserMessage(input) {
134
- return {
135
- type: "user",
136
- message: { role: "user", content: input.content },
137
- uuid: input.clientId,
138
- parent_tool_use_id: null,
139
- session_id: input.sessionId
140
- };
141
- }
142
- function claudePermissionResponse(requestId, decision) {
143
- return {
144
- type: "control_response",
145
- response: {
146
- subtype: "success",
147
- request_id: requestId,
148
- response: decision.behavior === "allow" ? { behavior: "allow", updatedInput: decision.input } : { behavior: "deny", message: decision.message }
149
- }
150
- };
151
- }
152
- function claudeControlError(requestId, error) {
153
- return {
154
- type: "control_response",
155
- response: { subtype: "error", request_id: requestId, error }
156
- };
157
- }
158
- function claudeInterrupt(requestId) {
159
- return { type: "control_request", request_id: requestId, request: { subtype: "interrupt" } };
160
- }
161
-
162
- // src/agent-executor/claude/classify.ts
163
- function newClaudeTurnState(turnId) {
164
- return { turnId, toolNames: new Map, interruptRequested: false };
165
- }
166
- var QUIET_SYSTEM = new Set([
167
- "hook_started",
168
- "hook_response",
169
- "status",
170
- "thinking_tokens",
171
- "compact_boundary",
172
- "api_retry"
173
- ]);
174
- var QUIET_STREAM = new Set([
175
- "message_start",
176
- "message_delta",
177
- "message_stop",
178
- "content_block_start",
179
- "content_block_stop"
180
- ]);
181
- function raw(type, subtype) {
182
- return { source: "claude", type: subtype === undefined ? type : `${type}/${subtype}` };
183
- }
184
- function unhandled(state, type, subtype) {
185
- return {
186
- kind: "unhandled",
187
- delta: {
188
- type: "error",
189
- turnId: state.turnId,
190
- itemId: `unhandled:${type}${subtype === undefined ? "" : `/${subtype}`}`,
191
- raw: raw(type, subtype),
192
- kind: "unhandled",
193
- message: `Claude record ${type}${subtype === undefined ? "" : `/${subtype}`} is not known to this driver`
194
- }
195
- };
196
- }
197
- function failureKind(status) {
198
- const code = Number(status);
199
- if (code === 401 || code === 403)
200
- return "authRequired";
201
- if (code === 429)
202
- return "rateLimited";
203
- return "restartRecommended";
204
- }
205
- function outcomeOf(record, state) {
206
- if (state.interruptRequested)
207
- return "interrupted";
208
- return record.is_error ? "failed" : "completed";
209
- }
210
- function classifyClaudeRecord(value, state) {
211
- const parsed = ClaudeRecordSchema.safeParse(value);
212
- if (!parsed.success) {
213
- const shape = ClaudeUnknownRecordSchema.safeParse(value);
214
- return unhandled(state, shape.success ? shape.data.type : "invalid");
215
- }
216
- const record = parsed.data;
217
- switch (record.type) {
218
- case "system":
219
- if (record.subtype === "init" && record.session_id !== undefined) {
220
- return {
221
- kind: "session",
222
- sessionId: record.session_id,
223
- ...record.model !== undefined && { model: record.model }
224
- };
225
- }
226
- return QUIET_SYSTEM.has(record.subtype) ? { kind: "ignore", reason: `system/${record.subtype}` } : unhandled(state, "system", record.subtype);
227
- case "user": {
228
- if (record.isReplay)
229
- return { kind: "ignore", reason: "the echo of the input" };
230
- const content = record.message.content;
231
- if (typeof content === "string")
232
- return { kind: "ignore", reason: "a note the CLI addressed to itself" };
233
- const deltas = [];
234
- content.forEach((block, index) => {
235
- if (block.type !== "tool_result" || !("tool_use_id" in block))
236
- return;
237
- const callId = nonEmptyString(block.tool_use_id) ?? `result-${index}`;
238
- deltas.push({
239
- type: "tool-result",
240
- turnId: state.turnId,
241
- itemId: `result:${callId}`,
242
- raw: raw("user", "tool_result"),
243
- callId,
244
- toolName: state.toolNames.get(callId) ?? "unknown",
245
- outcome: "is_error" in block && block.is_error === true ? "error" : "success",
246
- ..."content" in block && block.content !== undefined && {
247
- output: safeJsonValue(block.content)
248
- }
249
- });
250
- });
251
- return { kind: "deltas", deltas };
252
- }
253
- case "assistant": {
254
- const deltas = [];
255
- record.message.content.forEach((block, index) => {
256
- if (block.type !== "tool_use" || !("id" in block) || !("name" in block))
257
- return;
258
- const itemId = `${record.message.id}:${index}`;
259
- const callId = nonEmptyString(block.id) ?? itemId;
260
- const toolName = nonEmptyString(block.name) ?? "unknown";
261
- state.toolNames.set(callId, toolName);
262
- deltas.push({
263
- type: "tool-call",
264
- turnId: state.turnId,
265
- itemId,
266
- raw: raw("assistant", "tool_use"),
267
- callId,
268
- toolName,
269
- input: safeJsonValue("input" in block && block.input !== undefined ? block.input : null)
270
- });
271
- });
272
- return { kind: "deltas", deltas };
273
- }
274
- case "stream_event": {
275
- const event = record.event;
276
- if (event.type === "content_block_delta" && event.delta !== undefined) {
277
- const itemId = `${record.api_message_id ?? "message"}:${event.index ?? 0}`;
278
- const delta = event.delta;
279
- if (delta.type === "text_delta" && "text" in delta) {
280
- return {
281
- kind: "deltas",
282
- deltas: [
283
- {
284
- type: "text",
285
- turnId: state.turnId,
286
- itemId,
287
- raw: raw("stream_event", "text_delta"),
288
- text: String(delta.text)
289
- }
290
- ]
291
- };
292
- }
293
- if (delta.type === "thinking_delta" && "thinking" in delta) {
294
- return {
295
- kind: "deltas",
296
- deltas: [
297
- {
298
- type: "reasoning",
299
- turnId: state.turnId,
300
- itemId,
301
- raw: raw("stream_event", "thinking_delta"),
302
- text: String(delta.thinking)
303
- }
304
- ]
305
- };
306
- }
307
- return {
308
- kind: "ignore",
309
- reason: `stream_event/${delta.type}: arrives in the final message`
310
- };
311
- }
312
- return QUIET_STREAM.has(event.type) ? { kind: "ignore", reason: `stream_event/${event.type}` } : unhandled(state, "stream_event", event.type);
313
- }
314
- case "result": {
315
- const outcome = outcomeOf(record, state);
316
- const usage = record.usage;
317
- const deltas = [
318
- {
319
- type: "usage",
320
- turnId: state.turnId,
321
- itemId: "usage",
322
- raw: raw("result", record.subtype),
323
- ...usage?.input_tokens !== undefined && { inputTokens: usage.input_tokens },
324
- ...usage?.output_tokens !== undefined && { outputTokens: usage.output_tokens },
325
- ...usage?.cache_read_input_tokens !== undefined && {
326
- cachedInputTokens: usage.cache_read_input_tokens
327
- },
328
- ...record.total_cost_usd !== undefined && { costUsd: record.total_cost_usd }
329
- }
330
- ];
331
- if (outcome === "failed") {
332
- deltas.push({
333
- type: "error",
334
- turnId: state.turnId,
335
- itemId: "failure",
336
- raw: raw("result", record.subtype),
337
- kind: failureKind(record.api_error_status),
338
- message: record.result?.trim() || `Claude ended the turn with ${record.subtype}`
339
- });
340
- }
341
- deltas.push({
342
- type: "turn-end",
343
- turnId: state.turnId,
344
- itemId: "end",
345
- raw: raw("result", record.subtype),
346
- outcome
347
- });
348
- return { kind: "end", deltas, outcome };
349
- }
350
- case "rate_limit_event":
351
- return { kind: "limits", info: record.rate_limit_info };
352
- case "control_request": {
353
- const request = record.request;
354
- if (request.subtype !== "can_use_tool" || request.tool_name === undefined) {
355
- return {
356
- ...unhandled(state, "control_request", request.subtype),
357
- refuse: {
358
- requestId: record.request_id,
359
- message: `Claude control request ${request.subtype} is not known to this driver`
360
- }
361
- };
362
- }
363
- const callId = nonEmptyString(request.tool_use_id);
364
- return {
365
- kind: "request",
366
- input: request.input ?? null,
367
- delta: {
368
- type: "request",
369
- turnId: state.turnId,
370
- itemId: `request:${record.request_id}`,
371
- raw: raw("control_request", "can_use_tool"),
372
- interactionId: record.request_id,
373
- kind: "approval",
374
- toolName: nonEmptyString(request.tool_name) ?? "tool",
375
- ...callId !== undefined && { callId },
376
- ...request.input !== undefined && { input: request.input }
377
- }
378
- };
379
- }
380
- case "control_response":
381
- return { kind: "ignore", reason: "the answer to a request the executor sent" };
382
- case "command_lifecycle":
383
- return { kind: "ignore", reason: `the input's own lifecycle (${record.state})` };
384
- }
385
- }
386
-
387
- // src/agent-executor/claude/limits.ts
388
- import { z as z2 } from "zod";
389
- var ClaudeWindowSchema = z2.looseObject({
390
- utilization: z2.number().min(0),
391
- resetsAt: z2.number().positive().optional()
392
- });
393
- var ClaudeRateLimitInfoSchema = z2.looseObject({
394
- rateLimitType: z2.string().optional(),
395
- utilization: z2.number().min(0).optional(),
396
- resetsAt: z2.number().positive().optional(),
397
- unifiedWindows: z2.record(z2.string(), ClaudeWindowSchema).optional()
398
- });
399
- function claudePlanWindows2(info) {
400
- const parsed = ClaudeRateLimitInfoSchema.safeParse(info);
401
- if (!parsed.success)
402
- return [];
403
- const toWindow = (name, value) => {
404
- const resetsAt = epochSecondsToIso(value.resetsAt);
405
- return PlanWindowSchema2.parse({
406
- name,
407
- usedPercent: Math.min(100, value.utilization * 100),
408
- ...resetsAt !== undefined && { resetsAt }
409
- });
410
- };
411
- const { unifiedWindows, rateLimitType, utilization, resetsAt } = parsed.data;
412
- if (unifiedWindows !== undefined) {
413
- return Object.entries(unifiedWindows).flatMap(([name, value]) => nonEmptyString(name) !== undefined ? [toWindow(name, value)] : []);
414
- }
415
- const singleName = nonEmptyString(rateLimitType);
416
- if (singleName !== undefined && utilization !== undefined) {
417
- return [
418
- toWindow(singleName, { utilization, ...resetsAt !== undefined && { resetsAt } })
419
- ];
420
- }
421
- return [];
422
- }
423
-
424
- // src/agent-executor/claude/transcript.ts
425
- import { realpathSync } from "node:fs";
426
- import { open } from "node:fs/promises";
427
- import { basename, dirname, join } from "node:path";
428
- var SAFE_SESSION_ID = /^[A-Za-z0-9_-]+$/;
429
- function stripTrailingSlashes(path) {
430
- const stripped = path.replace(/\/+$/, "");
431
- return stripped.length === 0 && path.startsWith("/") ? "/" : stripped;
432
- }
433
- function canonicalClaudeCwd(cwd) {
434
- if (cwd.length === 0)
435
- return cwd;
436
- const normalized = stripTrailingSlashes(cwd);
437
- const missingTail = [];
438
- let probe = normalized;
439
- for (;; ) {
440
- try {
441
- const resolved = realpathSync(probe);
442
- return missingTail.length === 0 ? resolved : join(resolved, ...missingTail.reverse());
443
- } catch (error) {
444
- const code = errnoOf(error);
445
- if (code !== "ENOENT" && code !== "ENOTDIR")
446
- throw error;
447
- const parent = dirname(probe);
448
- if (parent === probe)
449
- return normalized;
450
- missingTail.push(basename(probe));
451
- probe = parent;
452
- }
453
- }
454
- }
455
- function claudeTranscriptPath2(input) {
456
- if (!SAFE_SESSION_ID.test(input.sessionId)) {
457
- throw new TypeError(`Invalid Claude sessionId: ${input.sessionId}`);
458
- }
459
- const canonicalCwd = canonicalClaudeCwd(input.cwd);
460
- return join(input.home, "projects", canonicalCwd.replace(/[^A-Za-z0-9-]/g, "-"), `${input.sessionId}.jsonl`);
461
- }
462
- var MAX_RECORD_BYTES = 16 * 1024 * 1024;
463
- var INITIAL_RECORD_BYTES = 64 * 1024;
464
- var READ_CHUNK_BYTES = 64 * 1024;
465
- var UTF8_STRICT = new TextDecoder("utf-8", { fatal: true });
466
- function classifyTranscriptLine(bytes, uuid) {
467
- const end = bytes.length > 0 && bytes[bytes.length - 1] === 13 ? bytes.length - 1 : bytes.length;
468
- if (end === 0)
469
- return "other";
470
- let record;
471
- try {
472
- record = JSON.parse(UTF8_STRICT.decode(bytes.subarray(0, end)));
473
- } catch {
474
- return "invalid";
475
- }
476
- if (typeof record !== "object" || record === null || Array.isArray(record))
477
- return "invalid";
478
- if (Reflect.get(record, "type") !== "user")
479
- return "other";
480
- const recordUuid = Reflect.get(record, "uuid");
481
- if (typeof recordUuid !== "string")
482
- return "invalid";
483
- return recordUuid === uuid ? "match" : "other";
484
- }
485
- async function claudeTranscriptHasUuid(path, uuid) {
486
- let handle;
487
- try {
488
- handle = await open(path, "r");
489
- } catch (error) {
490
- if (errnoOf(error) === "ENOENT")
491
- return false;
492
- throw error;
493
- }
494
- try {
495
- const chunk = new Uint8Array(READ_CHUNK_BYTES);
496
- let lineBuf = new Uint8Array(INITIAL_RECORD_BYTES);
497
- let lineLen = 0;
498
- let lineOverflow = false;
499
- let lineNumber = 1;
500
- let problem;
501
- const appendSlice = (src, start, end) => {
502
- if (lineOverflow || end <= start)
503
- return;
504
- const add = end - start;
505
- const needed = lineLen + add;
506
- if (needed > MAX_RECORD_BYTES) {
507
- lineOverflow = true;
508
- lineLen = 0;
509
- return;
510
- }
511
- if (needed > lineBuf.length) {
512
- let nextCap = lineBuf.length;
513
- while (nextCap < needed)
514
- nextCap = Math.min(MAX_RECORD_BYTES, nextCap * 2);
515
- const grown = new Uint8Array(nextCap);
516
- grown.set(lineBuf.subarray(0, lineLen));
517
- lineBuf = grown;
518
- }
519
- lineBuf.set(src.subarray(start, end), lineLen);
520
- lineLen = needed;
521
- };
522
- const finishLine = () => {
523
- const currentLine = lineNumber;
524
- lineNumber += 1;
525
- if (lineOverflow) {
526
- problem ??= `line ${currentLine} exceeds the ${MAX_RECORD_BYTES}-byte record limit`;
527
- lineOverflow = false;
528
- lineLen = 0;
529
- return false;
530
- }
531
- const verdict = classifyTranscriptLine(lineBuf.subarray(0, lineLen), uuid);
532
- lineLen = 0;
533
- if (verdict === "match")
534
- return true;
535
- if (verdict === "invalid") {
536
- problem ??= `line ${currentLine} could not be parsed as a valid transcript record`;
537
- }
538
- return false;
539
- };
540
- for (;; ) {
541
- const { bytesRead } = await handle.read(chunk, 0, chunk.length, null);
542
- if (bytesRead === 0)
543
- break;
544
- let start = 0;
545
- for (let i = 0;i < bytesRead; i += 1) {
546
- if (chunk[i] === 10) {
547
- appendSlice(chunk, start, i);
548
- if (finishLine())
549
- return true;
550
- start = i + 1;
551
- }
552
- }
553
- appendSlice(chunk, start, bytesRead);
554
- }
555
- if ((lineLen > 0 || lineOverflow) && finishLine())
556
- return true;
557
- if (problem !== undefined) {
558
- throw new Error(`Claude transcript ${path} could not prove absence of input: ${problem}`);
559
- }
560
- return false;
561
- } finally {
562
- await handle.close();
563
- }
564
- }
565
- async function claudeTranscriptExists(path) {
566
- let handle;
567
- try {
568
- handle = await open(path, "r");
569
- } catch (error) {
570
- if (errnoOf(error) === "ENOENT")
571
- return false;
572
- throw error;
573
- }
574
- await handle.close();
575
- return true;
576
- }
577
- async function findTranscriptInput(engine, input) {
578
- const profile = engineProfile(engine, input.profile);
579
- return claudeTranscriptHasUuid(claudeTranscriptPath2({
580
- home: profile.home,
581
- cwd: input.cwd,
582
- sessionId: input.nativeThreadId
583
- }), input.clientId);
584
- }
585
-
586
- export { claudeUserContent, claudeUserMessage, claudePermissionResponse, claudeControlError, claudeInterrupt, newClaudeTurnState, classifyClaudeRecord, claudePlanWindows2, claudeTranscriptPath2, claudeTranscriptExists, findTranscriptInput };