@anthropic-ai/claude-code 1.0.83 → 1.0.85

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.83",
3
+ "version": "1.0.85",
4
4
  "main": "sdk.mjs",
5
5
  "types": "sdk.d.ts",
6
6
  "bin": {
package/sdk.d.ts CHANGED
@@ -1,171 +1,214 @@
1
- import type {
2
- Message as APIAssistantMessage,
3
- MessageParam as APIUserMessage,
4
- Usage,
5
- } from '@anthropic-ai/sdk/resources/index.mjs'
6
-
1
+ import type { Message as APIAssistantMessage, MessageParam as APIUserMessage, Usage } from '@anthropic-ai/sdk/resources/index.mjs';
7
2
  export type NonNullableUsage = {
8
- [K in keyof Usage]: NonNullable<Usage[K]>
9
- }
10
-
11
- export type ApiKeySource = 'user' | 'project' | 'org' | 'temporary'
12
-
13
- export type ConfigScope = 'local' | 'user' | 'project'
14
-
3
+ [K in keyof Usage]: NonNullable<Usage[K]>;
4
+ };
5
+ export type ApiKeySource = 'user' | 'project' | 'org' | 'temporary';
6
+ export type ConfigScope = 'local' | 'user' | 'project';
15
7
  export type McpStdioServerConfig = {
16
- type?: 'stdio' // Optional for backwards compatibility
17
- command: string
18
- args?: string[]
19
- env?: Record<string, string>
20
- }
21
-
8
+ type?: 'stdio';
9
+ command: string;
10
+ args?: string[];
11
+ env?: Record<string, string>;
12
+ };
22
13
  export type McpSSEServerConfig = {
23
- type: 'sse'
24
- url: string
25
- headers?: Record<string, string>
26
- }
27
-
14
+ type: 'sse';
15
+ url: string;
16
+ headers?: Record<string, string>;
17
+ };
28
18
  export type McpHttpServerConfig = {
29
- type: 'http'
30
- url: string
31
- headers?: Record<string, string>
19
+ type: 'http';
20
+ url: string;
21
+ headers?: Record<string, string>;
22
+ };
23
+ export type McpServerConfig = McpStdioServerConfig | McpSSEServerConfig | McpHttpServerConfig;
24
+ export type PermissionResult = {
25
+ behavior: 'allow';
26
+ updatedInput: Record<string, unknown>;
27
+ } | {
28
+ behavior: 'deny';
29
+ message: string;
30
+ };
31
+ export type CanUseTool = (toolName: string, input: Record<string, unknown>, options: {
32
+ signal: AbortSignal;
33
+ }) => Promise<PermissionResult>;
34
+ export declare const HOOK_EVENTS: readonly ["PreToolUse", "PostToolUse", "Notification", "UserPromptSubmit", "SessionStart", "SessionEnd", "Stop", "SubagentStop", "PreCompact"];
35
+ export type HookEvent = (typeof HOOK_EVENTS)[number];
36
+ export type HookCallback = (input: HookInput, toolUseID: string | undefined, options: {
37
+ signal: AbortSignal;
38
+ }) => Promise<HookJSONOutput>;
39
+ export interface HookCallbackMatcher {
40
+ matcher?: string;
41
+ hooks: HookCallback[];
32
42
  }
33
-
34
- export type McpServerConfig =
35
- | McpStdioServerConfig
36
- | McpSSEServerConfig
37
- | McpHttpServerConfig
38
-
39
- export type PermissionResult =
40
- | {
41
- behavior: 'allow'
42
- updatedInput: Record<string, unknown>
43
- }
44
- | {
45
- behavior: 'deny'
46
- message: string
47
- }
48
-
49
- export type CanUseTool = (
50
- toolName: string,
51
- input: Record<string, unknown>,
52
- options: {
53
- // Signaled if the operation should be aborted
54
- signal: AbortSignal
55
- },
56
- ) => Promise<PermissionResult>
57
-
58
- export type Options = {
59
- abortController?: AbortController
60
- allowedTools?: string[]
61
- appendSystemPrompt?: string
62
- customSystemPrompt?: string
63
- cwd?: string
64
- disallowedTools?: string[]
65
- executable?: 'bun' | 'deno' | 'node'
66
- executableArgs?: string[]
67
- maxThinkingTokens?: number
68
- maxTurns?: number
69
- mcpServers?: Record<string, McpServerConfig>
70
- pathToClaudeCodeExecutable?: string
71
- permissionMode?: PermissionMode
72
- permissionPromptToolName?: string
73
- continue?: boolean
74
- resume?: string
75
- model?: string
76
- fallbackModel?: string
77
- stderr?: (data: string) => void
78
- canUseTool?: CanUseTool
43
+ export type BaseHookInput = {
44
+ session_id: string;
45
+ transcript_path: string;
46
+ cwd: string;
47
+ };
48
+ export type PreToolUseHookInput = BaseHookInput & {
49
+ hook_event_name: 'PreToolUse';
50
+ tool_name: string;
51
+ tool_input: unknown;
52
+ };
53
+ export type PostToolUseHookInput = BaseHookInput & {
54
+ hook_event_name: 'PostToolUse';
55
+ tool_name: string;
56
+ tool_input: unknown;
57
+ tool_response: unknown;
58
+ };
59
+ export type NotificationHookInput = BaseHookInput & {
60
+ hook_event_name: 'Notification';
61
+ message: string;
62
+ title?: string;
63
+ };
64
+ export type UserPromptSubmitHookInput = BaseHookInput & {
65
+ hook_event_name: 'UserPromptSubmit';
66
+ prompt: string;
67
+ };
68
+ export type SessionStartHookInput = BaseHookInput & {
69
+ hook_event_name: 'SessionStart';
70
+ source: 'startup' | 'resume' | 'clear' | 'compact';
71
+ };
72
+ export type StopHookInput = BaseHookInput & {
73
+ hook_event_name: 'Stop';
74
+ stop_hook_active: boolean;
75
+ };
76
+ export type SubagentStopHookInput = BaseHookInput & {
77
+ hook_event_name: 'SubagentStop';
78
+ stop_hook_active: boolean;
79
+ };
80
+ export type PreCompactHookInput = BaseHookInput & {
81
+ hook_event_name: 'PreCompact';
82
+ trigger: 'manual' | 'auto';
83
+ custom_instructions: string | null;
84
+ };
85
+ export declare const EXIT_REASONS: string[];
86
+ export type ExitReason = (typeof EXIT_REASONS)[number];
87
+ export type SessionEndHookInput = BaseHookInput & {
88
+ hook_event_name: 'SessionEnd';
89
+ reason: ExitReason;
90
+ };
91
+ export type HookInput = PreToolUseHookInput | PostToolUseHookInput | NotificationHookInput | UserPromptSubmitHookInput | SessionStartHookInput | SessionEndHookInput | StopHookInput | SubagentStopHookInput | PreCompactHookInput;
92
+ export interface HookJSONOutput {
93
+ continue?: boolean;
94
+ suppressOutput?: boolean;
95
+ stopReason?: string;
96
+ decision?: 'approve' | 'block';
97
+ systemMessage?: string;
98
+ permissionDecision?: 'allow' | 'deny' | 'ask';
99
+ reason?: string;
100
+ hookSpecificOutput?: {
101
+ hookEventName: 'PreToolUse';
102
+ permissionDecision?: 'allow' | 'deny' | 'ask';
103
+ permissionDecisionReason?: string;
104
+ } | {
105
+ hookEventName: 'UserPromptSubmit';
106
+ additionalContext?: string;
107
+ } | {
108
+ hookEventName: 'SessionStart';
109
+ additionalContext?: string;
110
+ } | {
111
+ hookEventName: 'PostToolUse';
112
+ additionalContext?: string;
113
+ };
79
114
  }
80
-
81
- export type PermissionMode =
82
- | 'default'
83
- | 'acceptEdits'
84
- | 'bypassPermissions'
85
- | 'plan'
86
-
115
+ export type Options = {
116
+ abortController?: AbortController;
117
+ additionalDirectories?: string[];
118
+ allowedTools?: string[];
119
+ appendSystemPrompt?: string;
120
+ canUseTool?: CanUseTool;
121
+ continue?: boolean;
122
+ customSystemPrompt?: string;
123
+ cwd?: string;
124
+ disallowedTools?: string[];
125
+ env?: Dict<string>;
126
+ executable?: 'bun' | 'deno' | 'node';
127
+ executableArgs?: string[];
128
+ fallbackModel?: string;
129
+ hooks?: Partial<Record<HookEvent, HookCallbackMatcher[]>>;
130
+ maxThinkingTokens?: number;
131
+ maxTurns?: number;
132
+ mcpServers?: Record<string, McpServerConfig>;
133
+ model?: string;
134
+ pathToClaudeCodeExecutable?: string;
135
+ permissionMode?: PermissionMode;
136
+ permissionPromptToolName?: string;
137
+ resume?: string;
138
+ stderr?: (data: string) => void;
139
+ strictMcpConfig?: boolean;
140
+ };
141
+ export type PermissionMode = 'default' | 'acceptEdits' | 'bypassPermissions' | 'plan';
87
142
  export type SDKUserMessage = {
88
- type: 'user'
89
- message: APIUserMessage
90
- parent_tool_use_id: string | null
91
- session_id: string
92
- }
93
-
143
+ type: 'user';
144
+ message: APIUserMessage;
145
+ parent_tool_use_id: string | null;
146
+ session_id: string;
147
+ };
94
148
  export type SDKAssistantMessage = {
95
- type: 'assistant'
96
- message: APIAssistantMessage
97
- parent_tool_use_id: string | null
98
- session_id: string
99
- }
100
-
149
+ type: 'assistant';
150
+ message: APIAssistantMessage;
151
+ parent_tool_use_id: string | null;
152
+ session_id: string;
153
+ };
101
154
  export type SDKPermissionDenial = {
102
- tool_name: string
103
- tool_use_id: string
104
- tool_input: Record<string, unknown>
105
- }
106
-
107
- export type SDKResultMessage =
108
- | {
109
- type: 'result'
110
- subtype: 'success'
111
- duration_ms: number
112
- duration_api_ms: number
113
- is_error: boolean
114
- num_turns: number
115
- result: string
116
- session_id: string
117
- total_cost_usd: number
118
- usage: NonNullableUsage
119
- permission_denials: SDKPermissionDenial[]
120
- }
121
- | {
122
- type: 'result'
123
- subtype: 'error_max_turns' | 'error_during_execution'
124
- duration_ms: number
125
- duration_api_ms: number
126
- is_error: boolean
127
- num_turns: number
128
- session_id: string
129
- total_cost_usd: number
130
- usage: NonNullableUsage
131
- permission_denials: SDKPermissionDenial[]
132
- }
133
-
155
+ tool_name: string;
156
+ tool_use_id: string;
157
+ tool_input: Record<string, unknown>;
158
+ };
159
+ export type SDKResultMessage = {
160
+ type: 'result';
161
+ subtype: 'success';
162
+ duration_ms: number;
163
+ duration_api_ms: number;
164
+ is_error: boolean;
165
+ num_turns: number;
166
+ result: string;
167
+ session_id: string;
168
+ total_cost_usd: number;
169
+ usage: NonNullableUsage;
170
+ permission_denials: SDKPermissionDenial[];
171
+ } | {
172
+ type: 'result';
173
+ subtype: 'error_max_turns' | 'error_during_execution';
174
+ duration_ms: number;
175
+ duration_api_ms: number;
176
+ is_error: boolean;
177
+ num_turns: number;
178
+ session_id: string;
179
+ total_cost_usd: number;
180
+ usage: NonNullableUsage;
181
+ permission_denials: SDKPermissionDenial[];
182
+ };
134
183
  export type SDKSystemMessage = {
135
- type: 'system'
136
- subtype: 'init'
137
- apiKeySource: ApiKeySource
138
- cwd: string
139
- session_id: string
140
- tools: string[]
141
- mcp_servers: {
142
- name: string
143
- status: string
144
- }[]
145
- model: string
146
- permissionMode: PermissionMode
147
- slash_commands: string[]
148
- }
149
-
150
- export type SDKMessage =
151
- | SDKAssistantMessage
152
- | SDKUserMessage
153
- | SDKResultMessage
154
- | SDKSystemMessage
155
-
156
- type Props = {
157
- prompt: string | AsyncIterable<SDKUserMessage>
158
- options?: Options
159
- }
160
-
184
+ type: 'system';
185
+ subtype: 'init';
186
+ apiKeySource: ApiKeySource;
187
+ cwd: string;
188
+ session_id: string;
189
+ tools: string[];
190
+ mcp_servers: {
191
+ name: string;
192
+ status: string;
193
+ }[];
194
+ model: string;
195
+ permissionMode: PermissionMode;
196
+ slash_commands: string[];
197
+ output_style: string;
198
+ };
199
+ export type SDKMessage = SDKAssistantMessage | SDKUserMessage | SDKResultMessage | SDKSystemMessage;
161
200
  export interface Query extends AsyncGenerator<SDKMessage, void> {
162
- /**
163
- * Interrupt the query.
164
- * Only supported when streaming input is used.
165
- */
166
- interrupt(): Promise<void>
201
+ /**
202
+ * Interrupt the query.
203
+ * Only supported when streaming input is used.
204
+ */
205
+ interrupt(): Promise<void>;
206
+ /**
207
+ * Sets the permission mode.
208
+ * Only supported when streaming input is used.
209
+ */
210
+ setPermissionMode(mode: PermissionMode): Promise<void>;
167
211
  }
168
-
169
212
  /**
170
213
  * Query Claude Code
171
214
  *
@@ -181,6 +224,9 @@ export interface Query extends AsyncGenerator<SDKMessage, void> {
181
224
  * }
182
225
  * ```
183
226
  */
184
- export function query({ prompt, options }: Props): Query
185
-
186
- export class AbortError extends Error {}
227
+ export declare function query({ prompt, options, }: {
228
+ prompt: string | AsyncIterable<SDKUserMessage>;
229
+ options?: Options;
230
+ }): Query;
231
+ export declare class AbortError extends Error {
232
+ }
package/sdk.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  // (c) Anthropic PBC. All rights reserved. Use is subject to Anthropic's Commercial Terms of Service (https://www.anthropic.com/legal/commercial-terms).
4
4
 
5
- // Version: 1.0.83
5
+ // Version: 1.0.85
6
6
 
7
7
  // Want to see the unminified source? We're hiring!
8
8
  // https://job-boards.greenhouse.io/anthropic/jobs/4816199008
@@ -97,31 +97,37 @@ function createAbortController(maxListeners = DEFAULT_MAX_LISTENERS) {
97
97
  return controller;
98
98
  }
99
99
 
100
+ // src/entrypoints/sdkTypes.ts
101
+ class AbortError extends Error {
102
+ }
103
+
100
104
  // src/entrypoints/sdk.ts
101
105
  function query({
102
106
  prompt,
103
107
  options: {
104
108
  abortController = createAbortController(),
109
+ additionalDirectories = [],
105
110
  allowedTools = [],
106
111
  appendSystemPrompt,
112
+ canUseTool,
113
+ continue: continueConversation,
107
114
  customSystemPrompt,
108
115
  cwd,
109
116
  disallowedTools = [],
117
+ env,
110
118
  executable = isRunningWithBun() ? "bun" : "node",
111
119
  executableArgs = [],
120
+ fallbackModel,
121
+ hooks,
112
122
  maxTurns,
113
123
  mcpServers,
124
+ model,
114
125
  pathToClaudeCodeExecutable,
115
126
  permissionMode = "default",
116
127
  permissionPromptToolName,
117
- canUseTool,
118
- continue: continueConversation,
119
128
  resume,
120
- model,
121
- fallbackModel,
122
- strictMcpConfig,
123
129
  stderr,
124
- env
130
+ strictMcpConfig
125
131
  } = {}
126
132
  }) {
127
133
  if (!env) {
@@ -187,6 +193,9 @@ function query({
187
193
  } else {
188
194
  args.push("--input-format", "stream-json");
189
195
  }
196
+ for (const dir of additionalDirectories) {
197
+ args.push("--add-dir", dir);
198
+ }
190
199
  if (!existsSync(pathToClaudeCodeExecutable)) {
191
200
  throw new ReferenceError(`Claude Code executable not found at ${pathToClaudeCodeExecutable}. Is options.pathToClaudeCodeExecutable set?`);
192
201
  }
@@ -234,7 +243,7 @@ function query({
234
243
  }
235
244
  });
236
245
  });
237
- const query2 = new Query(childStdin, child.stdout, processExitPromise, canUseTool);
246
+ const query2 = new Query(childStdin, child.stdout, processExitPromise, canUseTool, hooks);
238
247
  child.on("error", (error) => {
239
248
  if (abortController.signal.aborted) {
240
249
  query2.setError(new AbortError("Claude Code process aborted by user"));
@@ -254,16 +263,20 @@ class Query {
254
263
  childStdout;
255
264
  processExitPromise;
256
265
  canUseTool;
266
+ hooks;
257
267
  pendingControlResponses = new Map;
258
268
  sdkMessages;
259
269
  inputStream = new Stream;
260
270
  intialization;
261
271
  cancelControllers = new Map;
262
- constructor(childStdin, childStdout, processExitPromise, canUseTool) {
272
+ hookCallbacks = new Map;
273
+ nextCallbackId = 0;
274
+ constructor(childStdin, childStdout, processExitPromise, canUseTool, hooks) {
263
275
  this.childStdin = childStdin;
264
276
  this.childStdout = childStdout;
265
277
  this.processExitPromise = processExitPromise;
266
278
  this.canUseTool = canUseTool;
279
+ this.hooks = hooks;
267
280
  this.readMessages();
268
281
  this.sdkMessages = this.readSdkMessages();
269
282
  if (this.childStdin) {
@@ -363,6 +376,9 @@ class Query {
363
376
  return this.canUseTool(request.request.tool_name, request.request.input, {
364
377
  signal
365
378
  });
379
+ } else if (request.request.subtype === "hook_callback") {
380
+ const result = await this.handleHookCallbacks(request.request.callback_id, request.request.input, request.request.tool_use_id, signal);
381
+ return result;
366
382
  }
367
383
  throw new Error("Unsupported control request subtype: " + request.request.subtype);
368
384
  }
@@ -375,8 +391,29 @@ class Query {
375
391
  if (!this.childStdin) {
376
392
  throw new Error("Cannot initialize without child stdin");
377
393
  }
394
+ let hooks;
395
+ if (this.hooks) {
396
+ hooks = {};
397
+ for (const [event, matchers] of Object.entries(this.hooks)) {
398
+ if (matchers.length > 0) {
399
+ hooks[event] = matchers.map((matcher) => {
400
+ const callbackIds = [];
401
+ for (const callback of matcher.hooks) {
402
+ const callbackId = `hook_${this.nextCallbackId++}`;
403
+ this.hookCallbacks.set(callbackId, callback);
404
+ callbackIds.push(callbackId);
405
+ }
406
+ return {
407
+ matcher: matcher.matcher,
408
+ hookCallbackIds: callbackIds
409
+ };
410
+ });
411
+ }
412
+ }
413
+ }
378
414
  const initRequest = {
379
- subtype: "initialize"
415
+ subtype: "initialize",
416
+ hooks
380
417
  };
381
418
  const response = await this.request(initRequest, this.childStdin);
382
419
  return response.response;
@@ -389,6 +426,15 @@ class Query {
389
426
  subtype: "interrupt"
390
427
  }, this.childStdin);
391
428
  }
429
+ async setPermissionMode(mode) {
430
+ if (!this.childStdin) {
431
+ throw new Error("setPermissionMode requires --input-format stream-json");
432
+ }
433
+ await this.request({
434
+ subtype: "set_permission_mode",
435
+ mode
436
+ }, this.childStdin);
437
+ }
392
438
  request(request, childStdin) {
393
439
  const requestId = Math.random().toString(36).substring(2, 15);
394
440
  const sdkRequest = {
@@ -414,6 +460,15 @@ class Query {
414
460
  }
415
461
  return (await this.intialization).commands;
416
462
  }
463
+ handleHookCallbacks(callbackId, input, toolUseID, abortSignal) {
464
+ const callback = this.hookCallbacks.get(callbackId);
465
+ if (!callback) {
466
+ throw new Error(`No hook callback found for ID: ${callbackId}`);
467
+ }
468
+ return callback(input, toolUseID, {
469
+ signal: abortSignal
470
+ });
471
+ }
417
472
  }
418
473
  async function streamToStdin(stream, stdin, abortController) {
419
474
  for await (const message of stream) {
@@ -432,11 +487,7 @@ function logDebug(message) {
432
487
  function isRunningWithBun() {
433
488
  return process.versions.bun !== undefined || process.env.BUN_INSTALL !== undefined;
434
489
  }
435
-
436
- class AbortError extends Error {
437
- }
438
490
  export {
439
491
  query,
440
- Query,
441
- AbortError
492
+ Query
442
493
  };
Binary file
Binary file