coding-agents-sdk 0.0.1 → 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.
- package/README.md +242 -0
- package/dist/Agent-D8WkUilj.mjs +262 -0
- package/dist/SdkAgent-B47mJiIE.mjs +38 -0
- package/dist/adapters/claude-code-cli/index.d.mts +2 -0
- package/dist/adapters/claude-code-cli/index.mjs +490 -0
- package/dist/adapters/claude-code-sdk/index.d.mts +2 -0
- package/dist/adapters/claude-code-sdk/index.mjs +483 -0
- package/dist/adapters/codex-cli/index.d.mts +2 -0
- package/dist/adapters/codex-cli/index.mjs +626 -0
- package/dist/adapters/codex-sdk/index.d.mts +2 -0
- package/dist/adapters/codex-sdk/index.mjs +286 -0
- package/dist/adapters/gemini-cli/index.d.mts +2 -0
- package/dist/adapters/gemini-cli/index.mjs +292 -0
- package/dist/classify-error-pL6jeu4T.mjs +456 -0
- package/dist/container/index.d.mts +2 -0
- package/dist/container/index.mjs +24 -0
- package/dist/container-2UmPZ0CI.mjs +22 -0
- package/dist/container-CHxKIonn.mjs +440 -0
- package/dist/container-D2Z0ITDJ.mjs +22 -0
- package/dist/diff-De8d3MVb.mjs +333 -0
- package/dist/errors-BAmHDQu8.mjs +45 -0
- package/dist/events-nxuRbYIu.d.mts +239 -0
- package/dist/index-B3YqrgIp.d.mts +45 -0
- package/dist/index-ByAOGMUM.d.mts +224 -0
- package/dist/index-C3ZxLAd0.d.mts +315 -0
- package/dist/index-CFpNOmdA.d.mts +145 -0
- package/dist/index-dRVpEAr8.d.mts +39 -0
- package/dist/index-nzo1sBiK.d.mts +110 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.mjs +61 -0
- package/dist/oci-DMZZQZ47.mjs +438 -0
- package/dist/schemas/index.d.mts +2 -0
- package/dist/schemas/index.mjs +2 -0
- package/dist/schemas-DwD4pwJB.mjs +96 -0
- package/dist/spawner-Bw9UBEGX.mjs +54 -0
- package/dist/structured-output-BHtr_zpz.mjs +19 -0
- package/dist/types-Cb_EXIEe.d.mts +177 -0
- package/dist/types-aNMD8h3x.mjs +19 -0
- package/dist/util-B4RQZkKr.mjs +77 -0
- package/package.json +86 -9
- package/index.js +0 -7
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
import { n as AgentValidationError } from "../../types-aNMD8h3x.mjs";
|
|
2
|
+
import { a as toArray, i as sumTokens } from "../../util-B4RQZkKr.mjs";
|
|
3
|
+
import { a as buildBaseProcessEnv, c as getInputText, l as toTextPart, o as createAgentEvent, s as createEventParser } from "../../classify-error-pL6jeu4T.mjs";
|
|
4
|
+
import { t as Agent } from "../../Agent-D8WkUilj.mjs";
|
|
5
|
+
import { t as extractClaudeStructuredOutput } from "../../structured-output-BHtr_zpz.mjs";
|
|
6
|
+
import { z } from "zod/v4";
|
|
7
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { rm } from "node:fs/promises";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
//#region src/adapters/claude-code-cli/events/mappers.ts
|
|
12
|
+
const getClaudeSessionId = (event) => {
|
|
13
|
+
return "session_id" in event ? event.session_id : void 0;
|
|
14
|
+
};
|
|
15
|
+
const getToolResultOutput = (content) => {
|
|
16
|
+
if (typeof content.content === "string") return content.content;
|
|
17
|
+
return content.content.map((part) => part.text).join("\n");
|
|
18
|
+
};
|
|
19
|
+
const getClaudeResultError = (event) => {
|
|
20
|
+
if (typeof event.result === "string" && event.result.trim() !== "") return event.result;
|
|
21
|
+
return event.subtype;
|
|
22
|
+
};
|
|
23
|
+
const mapSystemEvent = (event, context) => {
|
|
24
|
+
const parts = [`[${event.subtype}]`];
|
|
25
|
+
if (event.subtype === "init") {
|
|
26
|
+
if (event.tools) parts.push(`tools: ${event.tools.length}`);
|
|
27
|
+
if (event.mcp_servers) {
|
|
28
|
+
const connected = event.mcp_servers.filter((server) => server.status === "connected").length;
|
|
29
|
+
parts.push(`mcp: ${connected}/${event.mcp_servers.length} connected`);
|
|
30
|
+
}
|
|
31
|
+
} else if (event.hook_name) {
|
|
32
|
+
parts.push(event.hook_name);
|
|
33
|
+
if (event.exit_code !== void 0) parts.push(`exit: ${event.exit_code}`);
|
|
34
|
+
}
|
|
35
|
+
const text = parts.join(" ");
|
|
36
|
+
return { events: [createAgentEvent(context.runId, "message", {
|
|
37
|
+
sessionId: context.sessionId,
|
|
38
|
+
model: event.model ?? null,
|
|
39
|
+
role: "system",
|
|
40
|
+
content: [toTextPart(text)],
|
|
41
|
+
text
|
|
42
|
+
})] };
|
|
43
|
+
};
|
|
44
|
+
const mapAssistantEvent = (event, context) => {
|
|
45
|
+
const events = [];
|
|
46
|
+
const toolCalls = [];
|
|
47
|
+
for (const block of event.message.content) switch (block.type) {
|
|
48
|
+
case "text":
|
|
49
|
+
events.push(createAgentEvent(context.runId, "message", {
|
|
50
|
+
sessionId: context.sessionId,
|
|
51
|
+
model: event.message.model ?? null,
|
|
52
|
+
role: "assistant",
|
|
53
|
+
content: [toTextPart(block.text)],
|
|
54
|
+
text: block.text
|
|
55
|
+
}));
|
|
56
|
+
break;
|
|
57
|
+
case "thinking":
|
|
58
|
+
events.push(createAgentEvent(context.runId, "reasoning", {
|
|
59
|
+
sessionId: context.sessionId,
|
|
60
|
+
model: event.message.model ?? null,
|
|
61
|
+
content: [toTextPart(block.thinking)],
|
|
62
|
+
text: block.thinking
|
|
63
|
+
}));
|
|
64
|
+
break;
|
|
65
|
+
case "tool_use":
|
|
66
|
+
toolCalls.push({
|
|
67
|
+
toolCallId: block.id,
|
|
68
|
+
toolName: block.name,
|
|
69
|
+
input: block.input
|
|
70
|
+
});
|
|
71
|
+
events.push(createAgentEvent(context.runId, "tool-call", {
|
|
72
|
+
sessionId: context.sessionId,
|
|
73
|
+
model: event.message.model ?? null,
|
|
74
|
+
toolCallId: block.id,
|
|
75
|
+
toolName: block.name,
|
|
76
|
+
input: block.input
|
|
77
|
+
}));
|
|
78
|
+
break;
|
|
79
|
+
default: break;
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
events,
|
|
83
|
+
toolCalls
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
const mapUserEvent = (event, context) => {
|
|
87
|
+
const events = [];
|
|
88
|
+
for (const block of event.message.content) switch (block.type) {
|
|
89
|
+
case "text":
|
|
90
|
+
events.push(createAgentEvent(context.runId, "message", {
|
|
91
|
+
sessionId: context.sessionId,
|
|
92
|
+
model: null,
|
|
93
|
+
role: "user",
|
|
94
|
+
content: [toTextPart(block.text)],
|
|
95
|
+
text: block.text
|
|
96
|
+
}));
|
|
97
|
+
break;
|
|
98
|
+
case "tool_result": {
|
|
99
|
+
const tool = context.toolCalls.get(block.tool_use_id);
|
|
100
|
+
const output = getToolResultOutput(block);
|
|
101
|
+
events.push(createAgentEvent(context.runId, "tool-result", {
|
|
102
|
+
sessionId: context.sessionId,
|
|
103
|
+
toolCallId: block.tool_use_id,
|
|
104
|
+
toolName: tool?.toolName,
|
|
105
|
+
input: tool?.input,
|
|
106
|
+
output,
|
|
107
|
+
isError: block.is_error,
|
|
108
|
+
error: block.is_error ? output : void 0
|
|
109
|
+
}));
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
default: break;
|
|
113
|
+
}
|
|
114
|
+
return { events };
|
|
115
|
+
};
|
|
116
|
+
const mapResultEvent = (event, context) => {
|
|
117
|
+
const inputTokens = event.usage?.input_tokens;
|
|
118
|
+
const cacheCreationInputTokens = event.usage?.cache_creation_input_tokens;
|
|
119
|
+
const cacheReadInputTokens = event.usage?.cache_read_input_tokens;
|
|
120
|
+
const outputTokens = event.usage?.output_tokens;
|
|
121
|
+
const totalTokens = sumTokens(inputTokens, cacheCreationInputTokens, cacheReadInputTokens, outputTokens);
|
|
122
|
+
const state = { stats: {
|
|
123
|
+
durationMs: event.duration_ms,
|
|
124
|
+
apiDurationMs: event.duration_api_ms,
|
|
125
|
+
turns: event.num_turns,
|
|
126
|
+
inputTokens,
|
|
127
|
+
cacheCreationInputTokens,
|
|
128
|
+
cacheReadInputTokens,
|
|
129
|
+
outputTokens,
|
|
130
|
+
totalTokens,
|
|
131
|
+
costUsd: event.total_cost_usd
|
|
132
|
+
} };
|
|
133
|
+
if (typeof event.result === "string") state.output = {
|
|
134
|
+
...state.output,
|
|
135
|
+
text: event.result
|
|
136
|
+
};
|
|
137
|
+
const extraction = extractClaudeStructuredOutput(event.structured_output, context, { isSuccess: !event.is_error && event.subtype === "success" }, "Structured output was requested but Claude did not return structured_output.");
|
|
138
|
+
if (extraction.value !== void 0) state.output = {
|
|
139
|
+
...state.output,
|
|
140
|
+
value: extraction.value
|
|
141
|
+
};
|
|
142
|
+
if (extraction.error) state.error = extraction.error;
|
|
143
|
+
if (event.is_error || event.subtype !== "success") {
|
|
144
|
+
state.status = "failed";
|
|
145
|
+
state.error = {
|
|
146
|
+
kind: "provider",
|
|
147
|
+
message: getClaudeResultError(event)
|
|
148
|
+
};
|
|
149
|
+
} else if (!state.error) state.status = "completed";
|
|
150
|
+
return {
|
|
151
|
+
events: [],
|
|
152
|
+
state
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
const mapRateLimitEvent = (event, context) => {
|
|
156
|
+
const text = `rate limited: ${event.rate_limit_info.status}`;
|
|
157
|
+
return { events: [createAgentEvent(context.runId, "message", {
|
|
158
|
+
sessionId: context.sessionId,
|
|
159
|
+
model: null,
|
|
160
|
+
role: "system",
|
|
161
|
+
content: [toTextPart(text)],
|
|
162
|
+
text
|
|
163
|
+
})] };
|
|
164
|
+
};
|
|
165
|
+
const mapClaudeEvent = (event, context) => {
|
|
166
|
+
switch (event.type) {
|
|
167
|
+
case "system": return mapSystemEvent(event, context);
|
|
168
|
+
case "assistant": return mapAssistantEvent(event, context);
|
|
169
|
+
case "user": return mapUserEvent(event, context);
|
|
170
|
+
case "result": return mapResultEvent(event, context);
|
|
171
|
+
case "rate_limit_event": return mapRateLimitEvent(event, context);
|
|
172
|
+
default: return { events: [] };
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/adapters/claude-code-cli/events/schemas.ts
|
|
177
|
+
const systemEventSubtypeSchema = z.enum([
|
|
178
|
+
"init",
|
|
179
|
+
"hook_response",
|
|
180
|
+
"hook_started",
|
|
181
|
+
"hook_finished",
|
|
182
|
+
"hook_failed",
|
|
183
|
+
"hook_skipped"
|
|
184
|
+
]);
|
|
185
|
+
const claudeSystemEventSchema = z.object({
|
|
186
|
+
type: z.literal("system"),
|
|
187
|
+
session_id: z.string(),
|
|
188
|
+
subtype: systemEventSubtypeSchema,
|
|
189
|
+
uuid: z.string(),
|
|
190
|
+
tools: z.array(z.string()).optional(),
|
|
191
|
+
mcp_servers: z.array(z.object({
|
|
192
|
+
name: z.string(),
|
|
193
|
+
status: z.enum([
|
|
194
|
+
"connected",
|
|
195
|
+
"disconnected",
|
|
196
|
+
"error",
|
|
197
|
+
"failed"
|
|
198
|
+
])
|
|
199
|
+
})).optional(),
|
|
200
|
+
model: z.string().optional(),
|
|
201
|
+
cwd: z.string().optional(),
|
|
202
|
+
hook_name: z.string().optional(),
|
|
203
|
+
hook_event: z.string().optional(),
|
|
204
|
+
stdout: z.string().optional(),
|
|
205
|
+
stderr: z.string().optional(),
|
|
206
|
+
exit_code: z.number().optional()
|
|
207
|
+
});
|
|
208
|
+
const claudeAssistantEventSchema = z.object({
|
|
209
|
+
type: z.literal("assistant"),
|
|
210
|
+
session_id: z.string(),
|
|
211
|
+
uuid: z.string(),
|
|
212
|
+
parent_tool_use_id: z.string().nullable().optional(),
|
|
213
|
+
message: z.object({
|
|
214
|
+
id: z.string(),
|
|
215
|
+
type: z.literal("message"),
|
|
216
|
+
role: z.literal("assistant"),
|
|
217
|
+
model: z.string(),
|
|
218
|
+
content: z.array(z.union([
|
|
219
|
+
z.object({
|
|
220
|
+
type: z.literal("text"),
|
|
221
|
+
text: z.string()
|
|
222
|
+
}),
|
|
223
|
+
z.object({
|
|
224
|
+
type: z.literal("tool_use"),
|
|
225
|
+
id: z.string(),
|
|
226
|
+
name: z.string(),
|
|
227
|
+
input: z.record(z.string(), z.unknown())
|
|
228
|
+
}),
|
|
229
|
+
z.object({
|
|
230
|
+
type: z.literal("thinking"),
|
|
231
|
+
thinking: z.string(),
|
|
232
|
+
signature: z.string().optional()
|
|
233
|
+
})
|
|
234
|
+
])),
|
|
235
|
+
stop_reason: z.enum([
|
|
236
|
+
"end_turn",
|
|
237
|
+
"max_tokens",
|
|
238
|
+
"tool_use",
|
|
239
|
+
"stop_sequence"
|
|
240
|
+
]).nullable(),
|
|
241
|
+
stop_sequence: z.string().nullable().optional(),
|
|
242
|
+
context_management: z.unknown().nullable().optional(),
|
|
243
|
+
usage: z.object({
|
|
244
|
+
input_tokens: z.number(),
|
|
245
|
+
output_tokens: z.number(),
|
|
246
|
+
cache_creation_input_tokens: z.number().optional(),
|
|
247
|
+
cache_read_input_tokens: z.number().optional(),
|
|
248
|
+
service_tier: z.enum(["premium", "standard"]).nullable().optional()
|
|
249
|
+
}),
|
|
250
|
+
ttftMs: z.number().optional()
|
|
251
|
+
})
|
|
252
|
+
});
|
|
253
|
+
const claudeUserEventSchema = z.object({
|
|
254
|
+
type: z.literal("user"),
|
|
255
|
+
session_id: z.string(),
|
|
256
|
+
message: z.object({
|
|
257
|
+
role: z.literal("user"),
|
|
258
|
+
content: z.array(z.union([z.object({
|
|
259
|
+
type: z.literal("tool_result"),
|
|
260
|
+
tool_use_id: z.string(),
|
|
261
|
+
content: z.union([z.string(), z.array(z.object({
|
|
262
|
+
type: z.literal("text"),
|
|
263
|
+
text: z.string()
|
|
264
|
+
}))]),
|
|
265
|
+
is_error: z.boolean().optional()
|
|
266
|
+
}), z.object({
|
|
267
|
+
type: z.literal("text"),
|
|
268
|
+
text: z.string()
|
|
269
|
+
})]))
|
|
270
|
+
}),
|
|
271
|
+
parent_tool_use_id: z.string().nullable().optional(),
|
|
272
|
+
uuid: z.string(),
|
|
273
|
+
isSynthetic: z.boolean().optional()
|
|
274
|
+
});
|
|
275
|
+
const claudeResultEventSchema = z.object({
|
|
276
|
+
type: z.literal("result"),
|
|
277
|
+
session_id: z.string(),
|
|
278
|
+
uuid: z.string(),
|
|
279
|
+
subtype: z.enum([
|
|
280
|
+
"success",
|
|
281
|
+
"error_max_turns",
|
|
282
|
+
"error_during_execution"
|
|
283
|
+
]),
|
|
284
|
+
duration_ms: z.number(),
|
|
285
|
+
duration_api_ms: z.number(),
|
|
286
|
+
is_error: z.boolean(),
|
|
287
|
+
num_turns: z.number(),
|
|
288
|
+
result: z.unknown().optional(),
|
|
289
|
+
structured_output: z.unknown().optional(),
|
|
290
|
+
total_cost_usd: z.number(),
|
|
291
|
+
usage: z.object({
|
|
292
|
+
input_tokens: z.number(),
|
|
293
|
+
cache_creation_input_tokens: z.number().optional(),
|
|
294
|
+
cache_read_input_tokens: z.number().optional(),
|
|
295
|
+
output_tokens: z.number(),
|
|
296
|
+
server_tool_use: z.object({ web_search_requests: z.number() }).optional()
|
|
297
|
+
}).optional()
|
|
298
|
+
});
|
|
299
|
+
const claudeRateLimitEventSchema = z.object({
|
|
300
|
+
type: z.literal("rate_limit_event"),
|
|
301
|
+
uuid: z.string(),
|
|
302
|
+
session_id: z.string().optional(),
|
|
303
|
+
rate_limit_info: z.object({
|
|
304
|
+
status: z.string(),
|
|
305
|
+
resetsAt: z.number().optional(),
|
|
306
|
+
rateLimitType: z.string().optional(),
|
|
307
|
+
overageStatus: z.string().optional(),
|
|
308
|
+
overageDisabledReason: z.string().optional(),
|
|
309
|
+
isUsingOverage: z.boolean().optional()
|
|
310
|
+
})
|
|
311
|
+
});
|
|
312
|
+
//#endregion
|
|
313
|
+
//#region src/adapters/claude-code-cli/events/parser.ts
|
|
314
|
+
const parseClaudeEvent = createEventParser(z.discriminatedUnion("type", [
|
|
315
|
+
claudeSystemEventSchema,
|
|
316
|
+
claudeAssistantEventSchema,
|
|
317
|
+
claudeUserEventSchema,
|
|
318
|
+
claudeResultEventSchema,
|
|
319
|
+
claudeRateLimitEventSchema
|
|
320
|
+
]));
|
|
321
|
+
//#endregion
|
|
322
|
+
//#region src/adapters/claude-code-cli/adapter.ts
|
|
323
|
+
const CLAUDE_CODE_CLI_CAPABILITIES = {
|
|
324
|
+
structuredOutput: true,
|
|
325
|
+
sessionResume: true,
|
|
326
|
+
imageInput: false,
|
|
327
|
+
mcp: true,
|
|
328
|
+
eventStreaming: true,
|
|
329
|
+
sessionFork: true
|
|
330
|
+
};
|
|
331
|
+
const toClaudeJsonSchema = (schema) => {
|
|
332
|
+
const { $schema, ...rest } = schema;
|
|
333
|
+
return rest;
|
|
334
|
+
};
|
|
335
|
+
var ClaudeCodeCliAgentImpl = class ClaudeCodeCliAgentImpl extends Agent {
|
|
336
|
+
type = "claude-code-cli";
|
|
337
|
+
creationOptions;
|
|
338
|
+
runDefaults;
|
|
339
|
+
forkOnNextRun;
|
|
340
|
+
mcpTempDir;
|
|
341
|
+
constructor(options = {}) {
|
|
342
|
+
super({
|
|
343
|
+
type: "claude-code-cli",
|
|
344
|
+
capabilities: CLAUDE_CODE_CLI_CAPABILITIES,
|
|
345
|
+
defaults: {
|
|
346
|
+
cwd: options.cwd,
|
|
347
|
+
env: options.env,
|
|
348
|
+
command: options.command ? toArray(options.command) : void 0,
|
|
349
|
+
sessionId: options.sessionId,
|
|
350
|
+
model: options.model,
|
|
351
|
+
systemPrompt: options.systemPrompt
|
|
352
|
+
},
|
|
353
|
+
spawner: options.spawner,
|
|
354
|
+
container: options.container,
|
|
355
|
+
logPath: options.logPath,
|
|
356
|
+
onEvent: options.onEvent
|
|
357
|
+
});
|
|
358
|
+
this.creationOptions = {
|
|
359
|
+
...options,
|
|
360
|
+
command: options.command ? [...toArray(options.command)] : void 0,
|
|
361
|
+
env: options.env ? { ...options.env } : void 0,
|
|
362
|
+
allowedTools: options.allowedTools ? [...options.allowedTools] : void 0,
|
|
363
|
+
disallowedTools: options.disallowedTools ? [...options.disallowedTools] : void 0,
|
|
364
|
+
mcpConfig: Array.isArray(options.mcpConfig) ? [...options.mcpConfig] : options.mcpConfig,
|
|
365
|
+
mcpServers: options.mcpServers ? { ...options.mcpServers } : void 0,
|
|
366
|
+
extraArgs: options.extraArgs ? [...options.extraArgs] : void 0
|
|
367
|
+
};
|
|
368
|
+
this.runDefaults = {
|
|
369
|
+
allowedTools: options.allowedTools ? [...options.allowedTools] : void 0,
|
|
370
|
+
disallowedTools: options.disallowedTools ? [...options.disallowedTools] : void 0,
|
|
371
|
+
appendSystemPrompt: options.appendSystemPrompt,
|
|
372
|
+
permissionMode: options.permissionMode,
|
|
373
|
+
mcpConfig: Array.isArray(options.mcpConfig) ? [...options.mcpConfig] : options.mcpConfig,
|
|
374
|
+
mcpServers: options.mcpServers ? { ...options.mcpServers } : void 0,
|
|
375
|
+
forkSession: options.forkSession,
|
|
376
|
+
extraArgs: options.extraArgs ? [...options.extraArgs] : void 0
|
|
377
|
+
};
|
|
378
|
+
this.forkOnNextRun = options.forkOnNextRun ?? false;
|
|
379
|
+
}
|
|
380
|
+
fork() {
|
|
381
|
+
if (!this.sessionId) throw new AgentValidationError("Cannot fork a Claude Code CLI agent before it has a sessionId.");
|
|
382
|
+
return new ClaudeCodeCliAgentImpl({
|
|
383
|
+
...this.creationOptions,
|
|
384
|
+
sessionId: this.sessionId,
|
|
385
|
+
forkSession: void 0,
|
|
386
|
+
forkOnNextRun: true
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
onValidateRun(request, _normalizedRequest) {
|
|
390
|
+
if ((request.forkSession ?? this.runDefaults.forkSession ?? this.forkOnNextRun) && !this.sessionId) throw new AgentValidationError("forkSession requires an existing sessionId on the agent.");
|
|
391
|
+
}
|
|
392
|
+
async onRunFinished(run) {
|
|
393
|
+
await super.onRunFinished(run);
|
|
394
|
+
await this.cleanupMcpTempDir();
|
|
395
|
+
}
|
|
396
|
+
async onDispose() {
|
|
397
|
+
await super.onDispose();
|
|
398
|
+
await this.cleanupMcpTempDir();
|
|
399
|
+
}
|
|
400
|
+
async cleanupMcpTempDir() {
|
|
401
|
+
if (this.mcpTempDir) {
|
|
402
|
+
const dir = this.mcpTempDir;
|
|
403
|
+
this.mcpTempDir = void 0;
|
|
404
|
+
await rm(dir, {
|
|
405
|
+
recursive: true,
|
|
406
|
+
force: true
|
|
407
|
+
}).catch(() => {});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
ensureMcpTempFile(servers) {
|
|
411
|
+
if (this.mcpTempDir) {
|
|
412
|
+
rmSync(this.mcpTempDir, {
|
|
413
|
+
recursive: true,
|
|
414
|
+
force: true
|
|
415
|
+
});
|
|
416
|
+
this.mcpTempDir = void 0;
|
|
417
|
+
}
|
|
418
|
+
this.mcpTempDir = mkdtempSync(join(tmpdir(), "cw-mcp-"));
|
|
419
|
+
const configPath = join(this.mcpTempDir, "mcp.json");
|
|
420
|
+
writeFileSync(configPath, JSON.stringify({ mcpServers: servers }), {
|
|
421
|
+
encoding: "utf8",
|
|
422
|
+
mode: 384
|
|
423
|
+
});
|
|
424
|
+
return configPath;
|
|
425
|
+
}
|
|
426
|
+
buildArgs(request, run) {
|
|
427
|
+
const args = [
|
|
428
|
+
"--verbose",
|
|
429
|
+
"--output-format",
|
|
430
|
+
"stream-json"
|
|
431
|
+
];
|
|
432
|
+
const inputText = getInputText(request.input);
|
|
433
|
+
const permissionMode = request.permissionMode ?? this.runDefaults.permissionMode;
|
|
434
|
+
const appendSystemPrompt = request.appendSystemPrompt ?? this.runDefaults.appendSystemPrompt;
|
|
435
|
+
const allowedTools = request.allowedTools ?? this.runDefaults.allowedTools;
|
|
436
|
+
const disallowedTools = request.disallowedTools ?? this.runDefaults.disallowedTools;
|
|
437
|
+
const mcpConfig = request.mcpConfig ?? this.runDefaults.mcpConfig;
|
|
438
|
+
const mcpServers = request.mcpServers ?? this.runDefaults.mcpServers;
|
|
439
|
+
const extraArgs = request.extraArgs ?? this.runDefaults.extraArgs;
|
|
440
|
+
const forkSession = request.forkSession ?? this.runDefaults.forkSession ?? this.forkOnNextRun;
|
|
441
|
+
if (request.model) args.push("--model", request.model);
|
|
442
|
+
if (request.systemPrompt) args.push("--system-prompt", request.systemPrompt);
|
|
443
|
+
if (appendSystemPrompt) args.push("--append-system-prompt", appendSystemPrompt);
|
|
444
|
+
if (permissionMode) args.push("--permission-mode", permissionMode);
|
|
445
|
+
if (allowedTools && allowedTools.length > 0) args.push("--allowed-tools", allowedTools.join(","));
|
|
446
|
+
if (disallowedTools && disallowedTools.length > 0) args.push("--disallowed-tools", disallowedTools.join(","));
|
|
447
|
+
if (mcpConfig) for (const config of toArray(mcpConfig)) args.push("--mcp-config", config);
|
|
448
|
+
if (mcpServers) args.push("--mcp-config", this.ensureMcpTempFile(mcpServers));
|
|
449
|
+
if (request.schema.kind !== "none") args.push("--json-schema", JSON.stringify(toClaudeJsonSchema(request.schema.jsonSchema)));
|
|
450
|
+
if (run.requestedSessionId) {
|
|
451
|
+
args.push("--resume", run.requestedSessionId);
|
|
452
|
+
if (forkSession) args.push("--fork-session");
|
|
453
|
+
}
|
|
454
|
+
if (extraArgs && extraArgs.length > 0) args.push(...extraArgs);
|
|
455
|
+
args.push("--print", inputText);
|
|
456
|
+
return args;
|
|
457
|
+
}
|
|
458
|
+
buildEnv(_request, _run) {
|
|
459
|
+
const env = buildBaseProcessEnv({ propagateEnv: this.creationOptions.propagateEnv });
|
|
460
|
+
if (this.creationOptions.ci !== false) env.CI = env.CI ?? "true";
|
|
461
|
+
if (this.defaults.env) Object.assign(env, this.defaults.env);
|
|
462
|
+
delete env.CLAUDECODE;
|
|
463
|
+
return env;
|
|
464
|
+
}
|
|
465
|
+
mapRawEvent(raw, context) {
|
|
466
|
+
this.appendRawLog(raw);
|
|
467
|
+
const parsed = parseClaudeEvent(raw);
|
|
468
|
+
if (!parsed) {
|
|
469
|
+
this.onParseError(JSON.stringify(raw).slice(0, 200), /* @__PURE__ */ new Error("schema validation failed"));
|
|
470
|
+
return { events: [] };
|
|
471
|
+
}
|
|
472
|
+
const sessionId = getClaudeSessionId(parsed) ?? context.sessionId;
|
|
473
|
+
if (this.forkOnNextRun && context.requestedSessionId && sessionId && sessionId !== context.requestedSessionId) this.forkOnNextRun = false;
|
|
474
|
+
const batch = mapClaudeEvent(parsed, {
|
|
475
|
+
...context,
|
|
476
|
+
sessionId
|
|
477
|
+
});
|
|
478
|
+
if (sessionId && batch.sessionId === void 0) batch.sessionId = sessionId;
|
|
479
|
+
return batch;
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
const createClaudeCodeCliAgent = (options) => {
|
|
483
|
+
if (!options?.container) return new ClaudeCodeCliAgentImpl(options);
|
|
484
|
+
return new ClaudeCodeCliAgentImpl({
|
|
485
|
+
...options,
|
|
486
|
+
spawner: options.container.spawner
|
|
487
|
+
});
|
|
488
|
+
};
|
|
489
|
+
//#endregion
|
|
490
|
+
export { createClaudeCodeCliAgent };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as ClaudeCodeSdkExecutable, c as ClaudeCodeSdkSettingSource, i as ClaudeCodeSdkEvent, l as ClaudeCodeSdkSettings, n as ClaudeCodeSdkAgent, o as ClaudeCodeSdkPermissionMode, r as ClaudeCodeSdkAgentOptions, s as ClaudeCodeSdkRunOptions, t as createClaudeCodeSdkAgent } from "../../index-B3YqrgIp.mjs";
|
|
2
|
+
export { ClaudeCodeSdkAgent, ClaudeCodeSdkAgentOptions, ClaudeCodeSdkEvent, ClaudeCodeSdkExecutable, ClaudeCodeSdkPermissionMode, ClaudeCodeSdkRunOptions, ClaudeCodeSdkSettingSource, ClaudeCodeSdkSettings, createClaudeCodeSdkAgent };
|