@anthropic-ai/claude-code 1.0.44 → 1.0.45

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.44",
3
+ "version": "1.0.45",
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.44
3
+ // Version: 1.0.45
4
4
 
5
5
  // src/entrypoints/sdk.ts
6
6
  import { spawn } from "child_process";
@@ -8,9 +8,85 @@ import { join } from "path";
8
8
  import { fileURLToPath } from "url";
9
9
  import { createInterface } from "readline";
10
10
  import { existsSync } from "fs";
11
+
12
+ // src/utils/stream.ts
13
+ class Stream {
14
+ returned;
15
+ queue = [];
16
+ readResolve;
17
+ readReject;
18
+ isDone = false;
19
+ hasError;
20
+ started = false;
21
+ constructor(returned) {
22
+ this.returned = returned;
23
+ }
24
+ [Symbol.asyncIterator]() {
25
+ if (this.started) {
26
+ throw new Error("Stream can only be iterated once");
27
+ }
28
+ this.started = true;
29
+ return this;
30
+ }
31
+ next() {
32
+ if (this.queue.length > 0) {
33
+ return Promise.resolve({
34
+ done: false,
35
+ value: this.queue.shift()
36
+ });
37
+ }
38
+ if (this.isDone) {
39
+ return Promise.resolve({ done: true, value: undefined });
40
+ }
41
+ if (this.hasError) {
42
+ return Promise.reject(this.hasError);
43
+ }
44
+ return new Promise((resolve, reject) => {
45
+ this.readResolve = resolve;
46
+ this.readReject = reject;
47
+ });
48
+ }
49
+ enqueue(value) {
50
+ if (this.readResolve) {
51
+ const resolve = this.readResolve;
52
+ this.readResolve = undefined;
53
+ this.readReject = undefined;
54
+ resolve({ done: false, value });
55
+ } else {
56
+ this.queue.push(value);
57
+ }
58
+ }
59
+ done() {
60
+ this.isDone = true;
61
+ if (this.readResolve) {
62
+ const resolve = this.readResolve;
63
+ this.readResolve = undefined;
64
+ this.readReject = undefined;
65
+ resolve({ done: true, value: undefined });
66
+ }
67
+ }
68
+ error(error) {
69
+ this.hasError = error;
70
+ if (this.readReject) {
71
+ const reject = this.readReject;
72
+ this.readResolve = undefined;
73
+ this.readReject = undefined;
74
+ reject(error);
75
+ }
76
+ }
77
+ return() {
78
+ this.isDone = true;
79
+ if (this.returned) {
80
+ this.returned();
81
+ }
82
+ return Promise.resolve({ done: true, value: undefined });
83
+ }
84
+ }
85
+
86
+ // src/entrypoints/sdk.ts
11
87
  var __filename2 = fileURLToPath(import.meta.url);
12
88
  var __dirname2 = join(__filename2, "..");
13
- async function* query({
89
+ function query({
14
90
  prompt,
15
91
  options: {
16
92
  abortController = new AbortController,
@@ -89,10 +165,12 @@ async function* query({
89
165
  ...process.env
90
166
  }
91
167
  });
168
+ let childStdin;
92
169
  if (typeof prompt === "string") {
93
170
  child.stdin.end();
94
171
  } else {
95
172
  streamToStdin(prompt, child.stdin, abortController);
173
+ childStdin = child.stdin;
96
174
  }
97
175
  if (process.env.DEBUG) {
98
176
  child.stderr.on("data", (data) => {
@@ -106,44 +184,124 @@ async function* query({
106
184
  };
107
185
  abortController.signal.addEventListener("abort", cleanup);
108
186
  process.on("exit", cleanup);
109
- try {
110
- let processError = null;
111
- child.on("error", (error) => {
112
- processError = new Error(`Failed to spawn Claude Code process: ${error.message}`);
113
- });
114
- const processExitPromise = new Promise((resolve, reject) => {
115
- child.on("close", (code) => {
116
- if (abortController.signal.aborted) {
117
- reject(new AbortError("Claude Code process aborted by user"));
118
- }
119
- if (code !== 0) {
120
- reject(new Error(`Claude Code process exited with code ${code}`));
121
- } else {
122
- resolve();
123
- }
124
- });
187
+ const processExitPromise = new Promise((resolve) => {
188
+ child.on("close", (code) => {
189
+ if (abortController.signal.aborted) {
190
+ query2.setError(new AbortError("Claude Code process aborted by user"));
191
+ }
192
+ if (code !== 0) {
193
+ query2.setError(new Error(`Claude Code process exited with code ${code}`));
194
+ } else {
195
+ resolve();
196
+ }
125
197
  });
126
- const rl = createInterface({ input: child.stdout });
198
+ });
199
+ const query2 = new Query(childStdin, child.stdout, processExitPromise);
200
+ child.on("error", (error) => {
201
+ if (abortController.signal.aborted) {
202
+ query2.setError(new AbortError("Claude Code process aborted by user"));
203
+ } else {
204
+ query2.setError(new Error(`Failed to spawn Claude Code process: ${error.message}`));
205
+ }
206
+ });
207
+ processExitPromise.finally(() => {
208
+ cleanup();
209
+ abortController.signal.removeEventListener("abort", cleanup);
210
+ if (process.env.CLAUDE_SDK_MCP_SERVERS) {
211
+ delete process.env.CLAUDE_SDK_MCP_SERVERS;
212
+ }
213
+ });
214
+ return query2;
215
+ }
216
+
217
+ class Query {
218
+ childStdin;
219
+ childStdout;
220
+ processExitPromise;
221
+ pendingControlResponses = new Map;
222
+ sdkMessages;
223
+ inputStream = new Stream;
224
+ constructor(childStdin, childStdout, processExitPromise) {
225
+ this.childStdin = childStdin;
226
+ this.childStdout = childStdout;
227
+ this.processExitPromise = processExitPromise;
228
+ this.readMessages();
229
+ this.sdkMessages = this.readSdkMessages();
230
+ }
231
+ setError(error) {
232
+ this.inputStream.error(error);
233
+ }
234
+ next(...[value]) {
235
+ return this.sdkMessages.next(...[value]);
236
+ }
237
+ return(value) {
238
+ return this.sdkMessages.return(value);
239
+ }
240
+ throw(e) {
241
+ return this.sdkMessages.throw(e);
242
+ }
243
+ [Symbol.asyncIterator]() {
244
+ return this.sdkMessages;
245
+ }
246
+ [Symbol.asyncDispose]() {
247
+ return this.sdkMessages[Symbol.asyncDispose]();
248
+ }
249
+ async readMessages() {
250
+ const rl = createInterface({ input: this.childStdout });
127
251
  try {
128
252
  for await (const line of rl) {
129
- if (processError) {
130
- throw processError;
131
- }
132
253
  if (line.trim()) {
133
- yield JSON.parse(line);
254
+ const message = JSON.parse(line);
255
+ if (message.type === "control_response") {
256
+ const handler = this.pendingControlResponses.get(message.response.request_id);
257
+ if (handler) {
258
+ handler(message.response);
259
+ }
260
+ continue;
261
+ }
262
+ this.inputStream.enqueue(message);
134
263
  }
135
264
  }
265
+ await this.processExitPromise;
266
+ } catch (error) {
267
+ this.inputStream.error(error);
136
268
  } finally {
269
+ this.inputStream.done();
137
270
  rl.close();
138
271
  }
139
- await processExitPromise;
140
- } finally {
141
- cleanup();
142
- abortController.signal.removeEventListener("abort", cleanup);
143
- if (process.env.CLAUDE_SDK_MCP_SERVERS) {
144
- delete process.env.CLAUDE_SDK_MCP_SERVERS;
272
+ }
273
+ async* readSdkMessages() {
274
+ for await (const message of this.inputStream) {
275
+ yield message;
145
276
  }
146
277
  }
278
+ async interrupt() {
279
+ if (!this.childStdin) {
280
+ throw new Error("Interrupt requires --input-format stream-json");
281
+ }
282
+ await this.request({
283
+ subtype: "interrupt"
284
+ }, this.childStdin);
285
+ }
286
+ request(request, childStdin) {
287
+ const requestId = Math.random().toString(36).substring(2, 15);
288
+ const sdkRequest = {
289
+ request_id: requestId,
290
+ type: "control_request",
291
+ request
292
+ };
293
+ return new Promise((resolve, reject) => {
294
+ this.pendingControlResponses.set(requestId, (response) => {
295
+ if (response.subtype === "success") {
296
+ resolve(response);
297
+ } else {
298
+ reject(new Error(response.error));
299
+ }
300
+ });
301
+ childStdin.write(JSON.stringify(sdkRequest) + `
302
+ `);
303
+ });
304
+ }
147
305
  }
148
306
  async function streamToStdin(stream, stdin, abortController) {
149
307
  for await (const message of stream) {
Binary file