@anthropic-ai/claude-code 1.0.57 → 1.0.59

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthropic-ai/claude-code",
3
- "version": "1.0.57",
3
+ "version": "1.0.59",
4
4
  "main": "sdk.mjs",
5
5
  "types": "sdk.d.ts",
6
6
  "bin": {
package/sdk.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // (c) Anthropic PBC. All rights reserved. Use is subject to Anthropic's Commercial Terms of Service (https://www.anthropic.com/legal/commercial-terms).
2
2
 
3
- // Version: 1.0.57
3
+ // Version: 1.0.59
4
4
 
5
5
  // src/entrypoints/sdk.ts
6
6
  import { spawn } from "child_process";
@@ -83,11 +83,20 @@ class Stream {
83
83
  }
84
84
  }
85
85
 
86
+ // src/utils/abortController.ts
87
+ import { setMaxListeners } from "events";
88
+ var DEFAULT_MAX_LISTENERS = 50;
89
+ function createAbortController(maxListeners = DEFAULT_MAX_LISTENERS) {
90
+ const controller = new AbortController;
91
+ setMaxListeners(maxListeners, controller.signal);
92
+ return controller;
93
+ }
94
+
86
95
  // src/entrypoints/sdk.ts
87
96
  function query({
88
97
  prompt,
89
98
  options: {
90
- abortController = new AbortController,
99
+ abortController = createAbortController(),
91
100
  allowedTools = [],
92
101
  appendSystemPrompt,
93
102
  customSystemPrompt,
@@ -100,16 +109,21 @@ function query({
100
109
  pathToClaudeCodeExecutable,
101
110
  permissionMode = "default",
102
111
  permissionPromptToolName,
112
+ canUseTool,
103
113
  continue: continueConversation,
104
114
  resume,
105
115
  model,
106
116
  fallbackModel,
107
117
  strictMcpConfig,
108
- stderr
118
+ stderr,
119
+ env
109
120
  } = {}
110
121
  }) {
111
- if (!process.env.CLAUDE_CODE_ENTRYPOINT) {
112
- process.env.CLAUDE_CODE_ENTRYPOINT = "sdk-ts";
122
+ if (!env) {
123
+ env = { ...process.env };
124
+ }
125
+ if (!env.CLAUDE_CODE_ENTRYPOINT) {
126
+ env.CLAUDE_CODE_ENTRYPOINT = "sdk-ts";
113
127
  }
114
128
  if (pathToClaudeCodeExecutable === undefined) {
115
129
  const filename = fileURLToPath(import.meta.url);
@@ -125,8 +139,18 @@ function query({
125
139
  args.push("--max-turns", maxTurns.toString());
126
140
  if (model)
127
141
  args.push("--model", model);
128
- if (permissionPromptToolName)
142
+ if (canUseTool) {
143
+ if (typeof prompt === "string") {
144
+ throw new Error("canUseTool callback requires --input-format stream-json. Please set prompt as an AsyncIterable.");
145
+ }
146
+ if (permissionPromptToolName) {
147
+ throw new Error("canUseTool callback cannot be used with permissionPromptToolName. Please use one or the other.");
148
+ }
149
+ permissionPromptToolName = "stdio";
150
+ }
151
+ if (permissionPromptToolName) {
129
152
  args.push("--permission-prompt-tool", permissionPromptToolName);
153
+ }
130
154
  if (continueConversation)
131
155
  args.push("--continue");
132
156
  if (resume)
@@ -165,9 +189,7 @@ function query({
165
189
  cwd,
166
190
  stdio: ["pipe", "pipe", "pipe"],
167
191
  signal: abortController.signal,
168
- env: {
169
- ...process.env
170
- }
192
+ env
171
193
  });
172
194
  let childStdin;
173
195
  if (typeof prompt === "string") {
@@ -176,9 +198,9 @@ function query({
176
198
  streamToStdin(prompt, child.stdin, abortController);
177
199
  childStdin = child.stdin;
178
200
  }
179
- if (process.env.DEBUG || stderr) {
201
+ if (env.DEBUG || stderr) {
180
202
  child.stderr.on("data", (data) => {
181
- if (process.env.DEBUG) {
203
+ if (env.DEBUG) {
182
204
  console.error("Claude Code stderr:", data.toString());
183
205
  }
184
206
  if (stderr) {
@@ -205,7 +227,7 @@ function query({
205
227
  }
206
228
  });
207
229
  });
208
- const query2 = new Query(childStdin, child.stdout, processExitPromise);
230
+ const query2 = new Query(childStdin, child.stdout, processExitPromise, canUseTool);
209
231
  child.on("error", (error) => {
210
232
  if (abortController.signal.aborted) {
211
233
  query2.setError(new AbortError("Claude Code process aborted by user"));
@@ -216,9 +238,6 @@ function query({
216
238
  processExitPromise.finally(() => {
217
239
  cleanup();
218
240
  abortController.signal.removeEventListener("abort", cleanup);
219
- if (process.env.CLAUDE_SDK_MCP_SERVERS) {
220
- delete process.env.CLAUDE_SDK_MCP_SERVERS;
221
- }
222
241
  });
223
242
  return query2;
224
243
  }
@@ -227,13 +246,15 @@ class Query {
227
246
  childStdin;
228
247
  childStdout;
229
248
  processExitPromise;
249
+ canUseTool;
230
250
  pendingControlResponses = new Map;
231
251
  sdkMessages;
232
252
  inputStream = new Stream;
233
- constructor(childStdin, childStdout, processExitPromise) {
253
+ constructor(childStdin, childStdout, processExitPromise, canUseTool) {
234
254
  this.childStdin = childStdin;
235
255
  this.childStdout = childStdout;
236
256
  this.processExitPromise = processExitPromise;
257
+ this.canUseTool = canUseTool;
237
258
  this.readMessages();
238
259
  this.sdkMessages = this.readSdkMessages();
239
260
  }
@@ -267,6 +288,9 @@ class Query {
267
288
  handler(message.response);
268
289
  }
269
290
  continue;
291
+ } else if (message.type === "control_request") {
292
+ this.handleControlRequest(message);
293
+ continue;
270
294
  }
271
295
  this.inputStream.enqueue(message);
272
296
  }
@@ -279,6 +303,41 @@ class Query {
279
303
  rl.close();
280
304
  }
281
305
  }
306
+ async handleControlRequest(request) {
307
+ try {
308
+ const response = await this.processControlRequest(request);
309
+ const controlResponse = {
310
+ type: "control_response",
311
+ response: {
312
+ subtype: "success",
313
+ request_id: request.request_id,
314
+ response
315
+ }
316
+ };
317
+ this.childStdin?.write(JSON.stringify(controlResponse) + `
318
+ `);
319
+ } catch (error) {
320
+ const controlErrorResponse = {
321
+ type: "control_response",
322
+ response: {
323
+ subtype: "error",
324
+ request_id: request.request_id,
325
+ error: error.message || String(error)
326
+ }
327
+ };
328
+ this.childStdin?.write(JSON.stringify(controlErrorResponse) + `
329
+ `);
330
+ }
331
+ }
332
+ async processControlRequest(request) {
333
+ if (request.request.subtype === "can_use_tool") {
334
+ if (!this.canUseTool) {
335
+ throw new Error("canUseTool callback is not provided.");
336
+ }
337
+ return this.canUseTool(request.request.tool_name, request.request.input);
338
+ }
339
+ throw new Error("Unsupported control request subtype: " + request.request.subtype);
340
+ }
282
341
  async* readSdkMessages() {
283
342
  for await (const message of this.inputStream) {
284
343
  yield message;
Binary file