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,483 @@
|
|
|
1
|
+
import { n as AgentValidationError } from "../../types-aNMD8h3x.mjs";
|
|
2
|
+
import { i as sumTokens, n as isRecord, r as parseJsonResponseText } from "../../util-B4RQZkKr.mjs";
|
|
3
|
+
import { a as buildBaseProcessEnv, c as getInputText, l as toTextPart, o as createAgentEvent } from "../../classify-error-pL6jeu4T.mjs";
|
|
4
|
+
import { t as extractClaudeStructuredOutput } from "../../structured-output-BHtr_zpz.mjs";
|
|
5
|
+
import { t as SdkAgent } from "../../SdkAgent-B47mJiIE.mjs";
|
|
6
|
+
import { query } from "@anthropic-ai/claude-agent-sdk";
|
|
7
|
+
//#region src/adapters/claude-code-sdk/events/mappers.ts
|
|
8
|
+
const getString = (value, key) => {
|
|
9
|
+
if (!isRecord(value) || typeof value[key] !== "string") return;
|
|
10
|
+
return value[key];
|
|
11
|
+
};
|
|
12
|
+
const getNumber = (value, key) => {
|
|
13
|
+
if (!isRecord(value) || typeof value[key] !== "number") return;
|
|
14
|
+
return value[key];
|
|
15
|
+
};
|
|
16
|
+
const isTextBlock = (value) => {
|
|
17
|
+
return isRecord(value) && value.type === "text" && typeof value.text === "string";
|
|
18
|
+
};
|
|
19
|
+
const isThinkingBlock = (value) => {
|
|
20
|
+
return isRecord(value) && value.type === "thinking" && typeof value.thinking === "string";
|
|
21
|
+
};
|
|
22
|
+
const isToolUseBlock = (value) => {
|
|
23
|
+
return isRecord(value) && value.type === "tool_use" && typeof value.id === "string" && typeof value.name === "string";
|
|
24
|
+
};
|
|
25
|
+
const isToolResultBlock = (value) => {
|
|
26
|
+
return isRecord(value) && value.type === "tool_result" && typeof value.tool_use_id === "string";
|
|
27
|
+
};
|
|
28
|
+
const getAssistantBlocks = (event) => {
|
|
29
|
+
const content = isRecord(event.message) ? event.message.content : void 0;
|
|
30
|
+
return Array.isArray(content) ? content : [];
|
|
31
|
+
};
|
|
32
|
+
const getAssistantModel = (event, options) => {
|
|
33
|
+
return getString(event.message, "model") ?? options.model ?? null;
|
|
34
|
+
};
|
|
35
|
+
const getUserBlocks = (event) => {
|
|
36
|
+
const content = isRecord(event.message) ? event.message.content : void 0;
|
|
37
|
+
if (typeof content === "string") return [content];
|
|
38
|
+
return Array.isArray(content) ? content : [];
|
|
39
|
+
};
|
|
40
|
+
const getToolResultOutput = (content) => {
|
|
41
|
+
if (typeof content === "string") return content;
|
|
42
|
+
if (!Array.isArray(content)) return content;
|
|
43
|
+
const textParts = content.flatMap((part) => isTextBlock(part) ? [part.text] : []);
|
|
44
|
+
if (textParts.length === content.length) return textParts.join("\n");
|
|
45
|
+
return content;
|
|
46
|
+
};
|
|
47
|
+
const toErrorText = (value) => {
|
|
48
|
+
if (typeof value === "string") return value;
|
|
49
|
+
try {
|
|
50
|
+
return JSON.stringify(value);
|
|
51
|
+
} catch {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const toStats = (event) => {
|
|
56
|
+
const inputTokens = getNumber(event.usage, "input_tokens");
|
|
57
|
+
const cacheCreationInputTokens = getNumber(event.usage, "cache_creation_input_tokens");
|
|
58
|
+
const cacheReadInputTokens = getNumber(event.usage, "cache_read_input_tokens");
|
|
59
|
+
const outputTokens = getNumber(event.usage, "output_tokens");
|
|
60
|
+
const totalTokens = sumTokens(inputTokens, cacheCreationInputTokens, cacheReadInputTokens, outputTokens);
|
|
61
|
+
return {
|
|
62
|
+
durationMs: event.duration_ms,
|
|
63
|
+
apiDurationMs: event.duration_api_ms,
|
|
64
|
+
turns: event.num_turns,
|
|
65
|
+
costUsd: event.total_cost_usd,
|
|
66
|
+
inputTokens,
|
|
67
|
+
cacheCreationInputTokens,
|
|
68
|
+
cacheReadInputTokens,
|
|
69
|
+
outputTokens,
|
|
70
|
+
totalTokens
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
const getResultErrorMessage = (event) => {
|
|
74
|
+
if ("errors" in event && Array.isArray(event.errors)) {
|
|
75
|
+
const messages = event.errors.filter((value) => typeof value === "string" && value.trim() !== "");
|
|
76
|
+
if (messages.length > 0) return messages.join("\n");
|
|
77
|
+
}
|
|
78
|
+
if (event.subtype === "error_max_structured_output_retries") return "Structured output retries were exhausted.";
|
|
79
|
+
return event.subtype;
|
|
80
|
+
};
|
|
81
|
+
const getClaudeCodeSdkSessionId = (event) => "session_id" in event ? event.session_id : void 0;
|
|
82
|
+
const createSystemNoticeEvent = (runId, sessionId, text) => createAgentEvent(runId, "message", {
|
|
83
|
+
sessionId,
|
|
84
|
+
model: null,
|
|
85
|
+
role: "system",
|
|
86
|
+
content: [toTextPart(text)],
|
|
87
|
+
text
|
|
88
|
+
});
|
|
89
|
+
const mapAssistantEvent = (event, context, options) => {
|
|
90
|
+
const events = [];
|
|
91
|
+
const toolCalls = [];
|
|
92
|
+
const model = getAssistantModel(event, options);
|
|
93
|
+
for (const block of getAssistantBlocks(event)) {
|
|
94
|
+
if (isTextBlock(block)) {
|
|
95
|
+
events.push(createAgentEvent(context.runId, "message", {
|
|
96
|
+
sessionId: context.sessionId,
|
|
97
|
+
model,
|
|
98
|
+
role: "assistant",
|
|
99
|
+
content: [toTextPart(block.text)],
|
|
100
|
+
text: block.text
|
|
101
|
+
}));
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (isThinkingBlock(block)) {
|
|
105
|
+
events.push(createAgentEvent(context.runId, "reasoning", {
|
|
106
|
+
sessionId: context.sessionId,
|
|
107
|
+
model,
|
|
108
|
+
content: [toTextPart(block.thinking)],
|
|
109
|
+
text: block.thinking
|
|
110
|
+
}));
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (isToolUseBlock(block)) {
|
|
114
|
+
toolCalls.push({
|
|
115
|
+
toolCallId: block.id,
|
|
116
|
+
toolName: block.name,
|
|
117
|
+
input: block.input
|
|
118
|
+
});
|
|
119
|
+
events.push(createAgentEvent(context.runId, "tool-call", {
|
|
120
|
+
sessionId: context.sessionId,
|
|
121
|
+
model,
|
|
122
|
+
toolCallId: block.id,
|
|
123
|
+
toolName: block.name,
|
|
124
|
+
input: block.input
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
events,
|
|
130
|
+
toolCalls
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
const mapUserEvent = (event, context) => {
|
|
134
|
+
const events = [];
|
|
135
|
+
for (const block of getUserBlocks(event)) {
|
|
136
|
+
if (typeof block === "string") {
|
|
137
|
+
events.push(createAgentEvent(context.runId, "message", {
|
|
138
|
+
sessionId: context.sessionId,
|
|
139
|
+
model: null,
|
|
140
|
+
role: "user",
|
|
141
|
+
content: [toTextPart(block)],
|
|
142
|
+
text: block
|
|
143
|
+
}));
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (isTextBlock(block)) {
|
|
147
|
+
events.push(createAgentEvent(context.runId, "message", {
|
|
148
|
+
sessionId: context.sessionId,
|
|
149
|
+
model: null,
|
|
150
|
+
role: "user",
|
|
151
|
+
content: [toTextPart(block.text)],
|
|
152
|
+
text: block.text
|
|
153
|
+
}));
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (isToolResultBlock(block)) {
|
|
157
|
+
const tool = context.toolCalls.get(block.tool_use_id);
|
|
158
|
+
const output = getToolResultOutput(block.content);
|
|
159
|
+
events.push(createAgentEvent(context.runId, "tool-result", {
|
|
160
|
+
sessionId: context.sessionId,
|
|
161
|
+
toolCallId: block.tool_use_id,
|
|
162
|
+
toolName: tool?.toolName,
|
|
163
|
+
input: tool?.input,
|
|
164
|
+
output,
|
|
165
|
+
isError: block.is_error,
|
|
166
|
+
error: block.is_error ? toErrorText(output) : void 0
|
|
167
|
+
}));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return { events };
|
|
171
|
+
};
|
|
172
|
+
const mapToolProgressEvent = (event, context, options) => {
|
|
173
|
+
if (context.toolCalls.has(event.tool_use_id)) return { events: [] };
|
|
174
|
+
return {
|
|
175
|
+
events: [createAgentEvent(context.runId, "tool-call", {
|
|
176
|
+
sessionId: context.sessionId,
|
|
177
|
+
model: options.model ?? null,
|
|
178
|
+
toolCallId: event.tool_use_id,
|
|
179
|
+
toolName: event.tool_name,
|
|
180
|
+
input: void 0
|
|
181
|
+
})],
|
|
182
|
+
toolCalls: [{
|
|
183
|
+
toolCallId: event.tool_use_id,
|
|
184
|
+
toolName: event.tool_name
|
|
185
|
+
}]
|
|
186
|
+
};
|
|
187
|
+
};
|
|
188
|
+
const getSystemNoticeText = (event) => {
|
|
189
|
+
switch (event.subtype) {
|
|
190
|
+
case "init": return;
|
|
191
|
+
case "status": return `status: ${event.status ?? "idle"}`;
|
|
192
|
+
case "local_command_output": return event.content;
|
|
193
|
+
case "hook_started": return `[hook_started] ${event.hook_name}`;
|
|
194
|
+
case "hook_progress": return `[hook_progress] ${event.hook_name}`;
|
|
195
|
+
case "hook_response": return `[hook_response] ${event.hook_name}: ${event.outcome}`;
|
|
196
|
+
case "task_started": return `[task_started] ${event.description}`;
|
|
197
|
+
case "task_progress": return `[task_progress] ${event.summary ?? event.description}`;
|
|
198
|
+
case "task_notification": return `[task_${event.status}] ${event.summary}`;
|
|
199
|
+
case "compact_boundary": return `[compact_boundary] ${event.compact_metadata.trigger}`;
|
|
200
|
+
case "files_persisted": return `[files_persisted] ${event.files.length} file${event.files.length === 1 ? "" : "s"}`;
|
|
201
|
+
default: return;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
const mapSystemEvent = (event, context) => {
|
|
205
|
+
const text = getSystemNoticeText(event);
|
|
206
|
+
if (!text) return { events: [] };
|
|
207
|
+
return { events: [createSystemNoticeEvent(context.runId, context.sessionId, text)] };
|
|
208
|
+
};
|
|
209
|
+
const mapNoticeEvent = (event, context) => {
|
|
210
|
+
switch (event.type) {
|
|
211
|
+
case "auth_status": {
|
|
212
|
+
let text = "authenticated";
|
|
213
|
+
if (event.error) text = `auth error: ${event.error}`;
|
|
214
|
+
else if (event.isAuthenticating) text = "authenticating";
|
|
215
|
+
return { events: [createSystemNoticeEvent(context.runId, context.sessionId, text)] };
|
|
216
|
+
}
|
|
217
|
+
case "prompt_suggestion": return { events: [createSystemNoticeEvent(context.runId, context.sessionId, `prompt suggestion: ${event.suggestion}`)] };
|
|
218
|
+
case "rate_limit_event": return { events: [createSystemNoticeEvent(context.runId, context.sessionId, `rate limited: ${event.rate_limit_info.status}`)] };
|
|
219
|
+
case "tool_use_summary": return { events: [createSystemNoticeEvent(context.runId, context.sessionId, event.summary)] };
|
|
220
|
+
default: return { events: [] };
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
const mapResultEvent = (event, context) => {
|
|
224
|
+
const state = { stats: toStats(event) };
|
|
225
|
+
const resultText = "result" in event ? event.result : void 0;
|
|
226
|
+
const isSuccess = event.subtype === "success" && !event.is_error;
|
|
227
|
+
if (typeof resultText === "string") state.output = {
|
|
228
|
+
...state.output,
|
|
229
|
+
text: resultText
|
|
230
|
+
};
|
|
231
|
+
const extraction = extractClaudeStructuredOutput("structured_output" in event ? event.structured_output : void 0, context, { isSuccess }, "Structured output was requested but Claude Agent SDK did not return structured_output.");
|
|
232
|
+
if (extraction.value === void 0 && isSuccess && context.schemaRequested && typeof resultText === "string") try {
|
|
233
|
+
extraction.value = parseJsonResponseText(resultText);
|
|
234
|
+
extraction.error = void 0;
|
|
235
|
+
} catch {}
|
|
236
|
+
if (extraction.value !== void 0) state.output = {
|
|
237
|
+
...state.output,
|
|
238
|
+
value: extraction.value
|
|
239
|
+
};
|
|
240
|
+
if (extraction.error) {
|
|
241
|
+
state.status = "failed";
|
|
242
|
+
state.error = extraction.error;
|
|
243
|
+
}
|
|
244
|
+
if (!isSuccess) {
|
|
245
|
+
state.status = "failed";
|
|
246
|
+
state.error = {
|
|
247
|
+
kind: "provider",
|
|
248
|
+
message: getResultErrorMessage(event)
|
|
249
|
+
};
|
|
250
|
+
} else if (!state.error) state.status = "completed";
|
|
251
|
+
return {
|
|
252
|
+
events: [],
|
|
253
|
+
state
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
const mapClaudeCodeSdkEvent = (event, context, options = {}) => {
|
|
257
|
+
let batch;
|
|
258
|
+
switch (event.type) {
|
|
259
|
+
case "assistant":
|
|
260
|
+
batch = mapAssistantEvent(event, context, options);
|
|
261
|
+
break;
|
|
262
|
+
case "result":
|
|
263
|
+
batch = mapResultEvent(event, context);
|
|
264
|
+
break;
|
|
265
|
+
case "system":
|
|
266
|
+
batch = mapSystemEvent(event, context);
|
|
267
|
+
break;
|
|
268
|
+
case "tool_progress":
|
|
269
|
+
batch = mapToolProgressEvent(event, context, options);
|
|
270
|
+
break;
|
|
271
|
+
case "user":
|
|
272
|
+
batch = mapUserEvent(event, context);
|
|
273
|
+
break;
|
|
274
|
+
case "auth_status":
|
|
275
|
+
case "prompt_suggestion":
|
|
276
|
+
case "rate_limit_event":
|
|
277
|
+
case "tool_use_summary":
|
|
278
|
+
batch = mapNoticeEvent(event, context);
|
|
279
|
+
break;
|
|
280
|
+
default:
|
|
281
|
+
batch = { events: [] };
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
const sessionId = getClaudeCodeSdkSessionId(event);
|
|
285
|
+
if (sessionId && batch.sessionId === void 0) batch.sessionId = sessionId;
|
|
286
|
+
return batch;
|
|
287
|
+
};
|
|
288
|
+
//#endregion
|
|
289
|
+
//#region src/adapters/claude-code-sdk/adapter.ts
|
|
290
|
+
const CLAUDE_CODE_SDK_CAPABILITIES = {
|
|
291
|
+
structuredOutput: true,
|
|
292
|
+
sessionResume: true,
|
|
293
|
+
imageInput: false,
|
|
294
|
+
mcp: true,
|
|
295
|
+
eventStreaming: true,
|
|
296
|
+
sessionFork: true
|
|
297
|
+
};
|
|
298
|
+
const resolveSystemPrompt = (systemPrompt, appendSystemPrompt) => {
|
|
299
|
+
if (!systemPrompt) return appendSystemPrompt ? {
|
|
300
|
+
type: "preset",
|
|
301
|
+
preset: "claude_code",
|
|
302
|
+
append: appendSystemPrompt
|
|
303
|
+
} : void 0;
|
|
304
|
+
return appendSystemPrompt ? `${systemPrompt}\n\n${appendSystemPrompt}` : systemPrompt;
|
|
305
|
+
};
|
|
306
|
+
const defaultCreateQuery = ({ prompt, options }) => query({
|
|
307
|
+
prompt,
|
|
308
|
+
options
|
|
309
|
+
});
|
|
310
|
+
const toClaudeSdkSettings = (settings) => {
|
|
311
|
+
if (settings === void 0 || typeof settings === "string") return settings;
|
|
312
|
+
if (Array.isArray(settings)) throw new AgentValidationError("settings must be an object or a path string.");
|
|
313
|
+
return settings;
|
|
314
|
+
};
|
|
315
|
+
const toClaudeSdkMcpServerConfig = (name, server) => {
|
|
316
|
+
if (server.command) {
|
|
317
|
+
if (server.url) throw new AgentValidationError(`mcpServers.${name} cannot set both command and url.`);
|
|
318
|
+
if (server.headers) throw new AgentValidationError(`mcpServers.${name}.headers is only supported with url transports.`);
|
|
319
|
+
if (server.cwd) throw new AgentValidationError(`mcpServers.${name}.cwd is not supported by claude-code-sdk.`);
|
|
320
|
+
return {
|
|
321
|
+
type: "stdio",
|
|
322
|
+
command: server.command,
|
|
323
|
+
args: server.args,
|
|
324
|
+
env: server.env
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
if (server.url) {
|
|
328
|
+
if (server.args) throw new AgentValidationError(`mcpServers.${name}.args is only supported with command transports.`);
|
|
329
|
+
if (server.env) throw new AgentValidationError(`mcpServers.${name}.env is only supported with command transports.`);
|
|
330
|
+
if (server.cwd) throw new AgentValidationError(`mcpServers.${name}.cwd is not supported by claude-code-sdk.`);
|
|
331
|
+
return {
|
|
332
|
+
type: "http",
|
|
333
|
+
url: server.url,
|
|
334
|
+
headers: server.headers
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
throw new AgentValidationError(`mcpServers.${name} must set either command or url.`);
|
|
338
|
+
};
|
|
339
|
+
const toClaudeSdkMcpServers = (servers) => {
|
|
340
|
+
if (!servers) return;
|
|
341
|
+
return Object.fromEntries(Object.entries(servers).map(([name, server]) => [name, toClaudeSdkMcpServerConfig(name, server)]));
|
|
342
|
+
};
|
|
343
|
+
var ClaudeCodeSdkAgentImpl = class ClaudeCodeSdkAgentImpl extends SdkAgent {
|
|
344
|
+
type = "claude-code-sdk";
|
|
345
|
+
defaults;
|
|
346
|
+
creationOptions;
|
|
347
|
+
runDefaults;
|
|
348
|
+
createQueryFn;
|
|
349
|
+
command;
|
|
350
|
+
forkOnNextRun;
|
|
351
|
+
currentQuery;
|
|
352
|
+
constructor(options = {}) {
|
|
353
|
+
super({
|
|
354
|
+
type: "claude-code-sdk",
|
|
355
|
+
capabilities: CLAUDE_CODE_SDK_CAPABILITIES,
|
|
356
|
+
sessionId: options.sessionId,
|
|
357
|
+
onEvent: options.onEvent
|
|
358
|
+
});
|
|
359
|
+
this.command = options.command;
|
|
360
|
+
this.defaults = {
|
|
361
|
+
cwd: options.cwd,
|
|
362
|
+
model: options.model,
|
|
363
|
+
systemPrompt: options.systemPrompt
|
|
364
|
+
};
|
|
365
|
+
this.creationOptions = {
|
|
366
|
+
...options,
|
|
367
|
+
command: options.command,
|
|
368
|
+
env: options.env ? { ...options.env } : void 0,
|
|
369
|
+
executableArgs: options.executableArgs ? [...options.executableArgs] : void 0,
|
|
370
|
+
mcpServers: options.mcpServers ? { ...options.mcpServers } : void 0,
|
|
371
|
+
settingSources: options.settingSources ? [...options.settingSources] : void 0
|
|
372
|
+
};
|
|
373
|
+
this.runDefaults = {
|
|
374
|
+
allowedTools: options.allowedTools ? [...options.allowedTools] : void 0,
|
|
375
|
+
disallowedTools: options.disallowedTools ? [...options.disallowedTools] : void 0,
|
|
376
|
+
appendSystemPrompt: options.appendSystemPrompt,
|
|
377
|
+
permissionMode: options.permissionMode,
|
|
378
|
+
mcpServers: options.mcpServers ? { ...options.mcpServers } : void 0,
|
|
379
|
+
forkSession: options.forkSession,
|
|
380
|
+
maxTurns: options.maxTurns
|
|
381
|
+
};
|
|
382
|
+
this.createQueryFn = options.createQuery ?? defaultCreateQuery;
|
|
383
|
+
this.forkOnNextRun = options.forkOnNextRun ?? false;
|
|
384
|
+
}
|
|
385
|
+
fork() {
|
|
386
|
+
if (!this.sessionId) throw new AgentValidationError("Cannot fork a Claude Code SDK agent before it has a sessionId.");
|
|
387
|
+
return new ClaudeCodeSdkAgentImpl({
|
|
388
|
+
...this.creationOptions,
|
|
389
|
+
sessionId: this.sessionId,
|
|
390
|
+
forkSession: void 0,
|
|
391
|
+
forkOnNextRun: true
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
get runRequestDefaults() {
|
|
395
|
+
return this.defaults;
|
|
396
|
+
}
|
|
397
|
+
onValidateRun(request, _normalizedRequest) {
|
|
398
|
+
if ((request.forkSession ?? this.runDefaults.forkSession ?? this.forkOnNextRun) && !this.sessionId) throw new AgentValidationError("forkSession requires an existing sessionId on the agent.");
|
|
399
|
+
if (request.maxTurns !== void 0 && (!Number.isInteger(request.maxTurns) || request.maxTurns <= 0)) throw new AgentValidationError("maxTurns must be a positive integer.");
|
|
400
|
+
}
|
|
401
|
+
onStopRequested(run) {
|
|
402
|
+
this.currentQuery?.interrupt?.().catch(() => {});
|
|
403
|
+
super.onStopRequested(run);
|
|
404
|
+
}
|
|
405
|
+
async onDispose() {
|
|
406
|
+
this.currentQuery?.close?.();
|
|
407
|
+
this.currentQuery = void 0;
|
|
408
|
+
}
|
|
409
|
+
cleanupSdkResources() {
|
|
410
|
+
if (this.currentQuery) {
|
|
411
|
+
this.currentQuery.close?.();
|
|
412
|
+
this.currentQuery = void 0;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
async executeSdkRun(run, request, abortController) {
|
|
416
|
+
const activeQuery = this.createQueryFn({
|
|
417
|
+
prompt: getInputText(run.normalizedRequest.input),
|
|
418
|
+
options: this.buildQueryOptions(request, run, abortController)
|
|
419
|
+
});
|
|
420
|
+
this.currentQuery = activeQuery;
|
|
421
|
+
for await (const message of activeQuery) {
|
|
422
|
+
const sessionId = getClaudeCodeSdkSessionId(message) ?? run.sessionId;
|
|
423
|
+
if (sessionId && run.sessionId !== sessionId) {
|
|
424
|
+
run.sessionId = sessionId;
|
|
425
|
+
this.setSessionId(sessionId);
|
|
426
|
+
if (this.forkOnNextRun && run.requestedSessionId && sessionId !== run.requestedSessionId) this.forkOnNextRun = false;
|
|
427
|
+
}
|
|
428
|
+
const batch = mapClaudeCodeSdkEvent(message, {
|
|
429
|
+
runId: run.runId,
|
|
430
|
+
requestedSessionId: run.requestedSessionId,
|
|
431
|
+
sessionId: run.sessionId,
|
|
432
|
+
schemaRequested: run.normalizedRequest.schema.kind !== "none",
|
|
433
|
+
toolCalls: run.toolCalls
|
|
434
|
+
}, { model: run.normalizedRequest.model });
|
|
435
|
+
this.applyBatch(run, batch);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
buildQueryOptions(request, run, abortController) {
|
|
439
|
+
const permissionMode = request.permissionMode ?? this.runDefaults.permissionMode;
|
|
440
|
+
const forkSession = request.forkSession ?? this.runDefaults.forkSession ?? this.forkOnNextRun;
|
|
441
|
+
const appendSystemPrompt = request.appendSystemPrompt ?? this.runDefaults.appendSystemPrompt;
|
|
442
|
+
const env = buildBaseProcessEnv({ propagateEnv: this.creationOptions.propagateEnv });
|
|
443
|
+
if (this.creationOptions.env) Object.assign(env, this.creationOptions.env);
|
|
444
|
+
delete env.CLAUDECODE;
|
|
445
|
+
return {
|
|
446
|
+
abortController,
|
|
447
|
+
cwd: run.normalizedRequest.cwd,
|
|
448
|
+
model: run.normalizedRequest.model,
|
|
449
|
+
systemPrompt: resolveSystemPrompt(run.normalizedRequest.systemPrompt, appendSystemPrompt),
|
|
450
|
+
allowedTools: request.allowedTools ?? this.runDefaults.allowedTools,
|
|
451
|
+
disallowedTools: request.disallowedTools ?? this.runDefaults.disallowedTools,
|
|
452
|
+
mcpServers: toClaudeSdkMcpServers(request.mcpServers ?? this.runDefaults.mcpServers),
|
|
453
|
+
maxTurns: request.maxTurns ?? this.runDefaults.maxTurns,
|
|
454
|
+
outputFormat: run.normalizedRequest.schema.kind === "none" ? void 0 : {
|
|
455
|
+
type: "json_schema",
|
|
456
|
+
schema: run.normalizedRequest.schema.jsonSchema
|
|
457
|
+
},
|
|
458
|
+
permissionMode,
|
|
459
|
+
allowDangerouslySkipPermissions: permissionMode === "bypassPermissions" ? true : void 0,
|
|
460
|
+
resume: run.requestedSessionId,
|
|
461
|
+
forkSession: run.requestedSessionId ? forkSession : void 0,
|
|
462
|
+
env,
|
|
463
|
+
executable: this.creationOptions.executable,
|
|
464
|
+
executableArgs: this.creationOptions.executableArgs,
|
|
465
|
+
pathToClaudeCodeExecutable: this.command,
|
|
466
|
+
settings: toClaudeSdkSettings(this.creationOptions.settings),
|
|
467
|
+
settingSources: this.creationOptions.settingSources,
|
|
468
|
+
stderr: (data) => {
|
|
469
|
+
for (const line of data.split("\n")) {
|
|
470
|
+
if (!line) continue;
|
|
471
|
+
run.stderrLines.push(line);
|
|
472
|
+
this.emit(run, createAgentEvent(run.runId, "stderr", {
|
|
473
|
+
sessionId: run.sessionId,
|
|
474
|
+
text: line
|
|
475
|
+
}));
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
const createClaudeCodeSdkAgent = (options) => new ClaudeCodeSdkAgentImpl(options);
|
|
482
|
+
//#endregion
|
|
483
|
+
export { createClaudeCodeSdkAgent };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as CodexCliRunOptions, c as CodexEvent, d as CodexItemUpdatedEvent, f as CodexThreadStartedEvent, i as CodexCliPermissionMode, l as CodexItemCompletedEvent, m as CodexTurnFailedEvent, n as CodexCliAgent, o as CodexCliSandboxMode, p as CodexTurnCompletedEvent, r as CodexCliAgentOptions, s as CodexErrorEvent, t as createCodexCliAgent, u as CodexItemStartedEvent } from "../../index-nzo1sBiK.mjs";
|
|
2
|
+
export { CodexCliAgent, CodexCliAgentOptions, CodexCliPermissionMode, CodexCliRunOptions, CodexCliSandboxMode, CodexErrorEvent, CodexEvent, CodexItemCompletedEvent, CodexItemStartedEvent, CodexItemUpdatedEvent, CodexThreadStartedEvent, CodexTurnCompletedEvent, CodexTurnFailedEvent, createCodexCliAgent };
|