@anthropic-ai/claude-code 1.0.84 → 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.84",
3
+ "version": "1.0.85",
4
4
  "main": "sdk.mjs",
5
5
  "types": "sdk.d.ts",
6
6
  "bin": {
package/sdk.d.ts CHANGED
@@ -31,6 +31,87 @@ export type PermissionResult = {
31
31
  export type CanUseTool = (toolName: string, input: Record<string, unknown>, options: {
32
32
  signal: AbortSignal;
33
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[];
42
+ }
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
+ };
114
+ }
34
115
  export type Options = {
35
116
  abortController?: AbortController;
36
117
  additionalDirectories?: string[];
@@ -45,6 +126,7 @@ export type Options = {
45
126
  executable?: 'bun' | 'deno' | 'node';
46
127
  executableArgs?: string[];
47
128
  fallbackModel?: string;
129
+ hooks?: Partial<Record<HookEvent, HookCallbackMatcher[]>>;
48
130
  maxThinkingTokens?: number;
49
131
  maxTurns?: number;
50
132
  mcpServers?: Record<string, McpServerConfig>;
@@ -112,6 +194,7 @@ export type SDKSystemMessage = {
112
194
  model: string;
113
195
  permissionMode: PermissionMode;
114
196
  slash_commands: string[];
197
+ output_style: string;
115
198
  };
116
199
  export type SDKMessage = SDKAssistantMessage | SDKUserMessage | SDKResultMessage | SDKSystemMessage;
117
200
  export interface Query extends AsyncGenerator<SDKMessage, void> {
@@ -120,6 +203,11 @@ export interface Query extends AsyncGenerator<SDKMessage, void> {
120
203
  * Only supported when streaming input is used.
121
204
  */
122
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>;
123
211
  }
124
212
  /**
125
213
  * Query Claude Code
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.84
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
@@ -106,27 +106,28 @@ function query({
106
106
  prompt,
107
107
  options: {
108
108
  abortController = createAbortController(),
109
+ additionalDirectories = [],
109
110
  allowedTools = [],
110
111
  appendSystemPrompt,
112
+ canUseTool,
113
+ continue: continueConversation,
111
114
  customSystemPrompt,
112
115
  cwd,
113
116
  disallowedTools = [],
117
+ env,
114
118
  executable = isRunningWithBun() ? "bun" : "node",
115
119
  executableArgs = [],
120
+ fallbackModel,
121
+ hooks,
116
122
  maxTurns,
117
123
  mcpServers,
124
+ model,
118
125
  pathToClaudeCodeExecutable,
119
126
  permissionMode = "default",
120
127
  permissionPromptToolName,
121
- canUseTool,
122
- continue: continueConversation,
123
128
  resume,
124
- model,
125
- fallbackModel,
126
- strictMcpConfig,
127
129
  stderr,
128
- env,
129
- additionalDirectories = []
130
+ strictMcpConfig
130
131
  } = {}
131
132
  }) {
132
133
  if (!env) {
@@ -242,7 +243,7 @@ function query({
242
243
  }
243
244
  });
244
245
  });
245
- const query2 = new Query(childStdin, child.stdout, processExitPromise, canUseTool);
246
+ const query2 = new Query(childStdin, child.stdout, processExitPromise, canUseTool, hooks);
246
247
  child.on("error", (error) => {
247
248
  if (abortController.signal.aborted) {
248
249
  query2.setError(new AbortError("Claude Code process aborted by user"));
@@ -262,16 +263,20 @@ class Query {
262
263
  childStdout;
263
264
  processExitPromise;
264
265
  canUseTool;
266
+ hooks;
265
267
  pendingControlResponses = new Map;
266
268
  sdkMessages;
267
269
  inputStream = new Stream;
268
270
  intialization;
269
271
  cancelControllers = new Map;
270
- constructor(childStdin, childStdout, processExitPromise, canUseTool) {
272
+ hookCallbacks = new Map;
273
+ nextCallbackId = 0;
274
+ constructor(childStdin, childStdout, processExitPromise, canUseTool, hooks) {
271
275
  this.childStdin = childStdin;
272
276
  this.childStdout = childStdout;
273
277
  this.processExitPromise = processExitPromise;
274
278
  this.canUseTool = canUseTool;
279
+ this.hooks = hooks;
275
280
  this.readMessages();
276
281
  this.sdkMessages = this.readSdkMessages();
277
282
  if (this.childStdin) {
@@ -371,6 +376,9 @@ class Query {
371
376
  return this.canUseTool(request.request.tool_name, request.request.input, {
372
377
  signal
373
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;
374
382
  }
375
383
  throw new Error("Unsupported control request subtype: " + request.request.subtype);
376
384
  }
@@ -383,8 +391,29 @@ class Query {
383
391
  if (!this.childStdin) {
384
392
  throw new Error("Cannot initialize without child stdin");
385
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
+ }
386
414
  const initRequest = {
387
- subtype: "initialize"
415
+ subtype: "initialize",
416
+ hooks
388
417
  };
389
418
  const response = await this.request(initRequest, this.childStdin);
390
419
  return response.response;
@@ -397,6 +426,15 @@ class Query {
397
426
  subtype: "interrupt"
398
427
  }, this.childStdin);
399
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
+ }
400
438
  request(request, childStdin) {
401
439
  const requestId = Math.random().toString(36).substring(2, 15);
402
440
  const sdkRequest = {
@@ -422,6 +460,15 @@ class Query {
422
460
  }
423
461
  return (await this.intialization).commands;
424
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
+ }
425
472
  }
426
473
  async function streamToStdin(stream, stdin, abortController) {
427
474
  for await (const message of stream) {
Binary file