@anthropic-ai/claude-agent-sdk 0.1.61 → 0.1.63
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/cli.js +1387 -1487
- package/package.json +1 -1
- package/resvg.wasm +0 -0
- package/sandboxTypes.d.ts +12 -0
- package/sdk.d.ts +24 -0
- package/sdk.mjs +1400 -1338
package/sdk.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// (c) Anthropic PBC. All rights reserved. Use is subject to the Legal Agreements outlined here: https://code.claude.com/docs/en/legal-and-compliance.
|
|
3
3
|
|
|
4
|
-
// Version: 0.1.
|
|
4
|
+
// Version: 0.1.63
|
|
5
5
|
|
|
6
6
|
// Want to see the unminified source? We're hiring!
|
|
7
7
|
// https://job-boards.greenhouse.io/anthropic/jobs/4816199008
|
|
@@ -6256,1447 +6256,1476 @@ import { createInterface } from "readline";
|
|
|
6256
6256
|
// ../src/utils/fsOperations.ts
|
|
6257
6257
|
import * as fs from "fs";
|
|
6258
6258
|
import { stat as statPromise, open } from "fs/promises";
|
|
6259
|
-
|
|
6260
|
-
|
|
6261
|
-
|
|
6262
|
-
|
|
6263
|
-
|
|
6264
|
-
|
|
6265
|
-
|
|
6266
|
-
|
|
6267
|
-
|
|
6268
|
-
|
|
6269
|
-
|
|
6270
|
-
|
|
6271
|
-
|
|
6272
|
-
|
|
6273
|
-
|
|
6274
|
-
|
|
6275
|
-
|
|
6276
|
-
|
|
6277
|
-
|
|
6278
|
-
|
|
6279
|
-
|
|
6280
|
-
|
|
6281
|
-
|
|
6282
|
-
|
|
6283
|
-
|
|
6284
|
-
|
|
6285
|
-
|
|
6286
|
-
|
|
6287
|
-
|
|
6288
|
-
}
|
|
6289
|
-
},
|
|
6290
|
-
writeFileSync(fsPath, data, options) {
|
|
6291
|
-
const fileExists = fs.existsSync(fsPath);
|
|
6292
|
-
if (!options.flush) {
|
|
6293
|
-
const writeOptions = {
|
|
6294
|
-
encoding: options.encoding
|
|
6295
|
-
};
|
|
6296
|
-
if (!fileExists) {
|
|
6297
|
-
writeOptions.mode = options.mode ?? 384;
|
|
6298
|
-
} else if (options.mode !== undefined) {
|
|
6299
|
-
writeOptions.mode = options.mode;
|
|
6300
|
-
}
|
|
6301
|
-
fs.writeFileSync(fsPath, data, writeOptions);
|
|
6302
|
-
return;
|
|
6303
|
-
}
|
|
6304
|
-
let fd;
|
|
6305
|
-
try {
|
|
6306
|
-
const mode = !fileExists ? options.mode ?? 384 : options.mode;
|
|
6307
|
-
fd = fs.openSync(fsPath, "w", mode);
|
|
6308
|
-
fs.writeFileSync(fd, data, { encoding: options.encoding });
|
|
6309
|
-
fs.fsyncSync(fd);
|
|
6310
|
-
} finally {
|
|
6311
|
-
if (fd) {
|
|
6312
|
-
fs.closeSync(fd);
|
|
6313
|
-
}
|
|
6314
|
-
}
|
|
6315
|
-
},
|
|
6316
|
-
appendFileSync(path, data, options) {
|
|
6317
|
-
if (!fs.existsSync(path)) {
|
|
6318
|
-
const mode = options?.mode ?? 384;
|
|
6319
|
-
const fd = fs.openSync(path, "a", mode);
|
|
6320
|
-
try {
|
|
6321
|
-
fs.appendFileSync(fd, data);
|
|
6322
|
-
} finally {
|
|
6323
|
-
fs.closeSync(fd);
|
|
6324
|
-
}
|
|
6259
|
+
|
|
6260
|
+
// ../node_modules/lodash-es/_freeGlobal.js
|
|
6261
|
+
var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
|
|
6262
|
+
var _freeGlobal_default = freeGlobal;
|
|
6263
|
+
|
|
6264
|
+
// ../node_modules/lodash-es/_root.js
|
|
6265
|
+
var freeSelf = typeof self == "object" && self && self.Object === Object && self;
|
|
6266
|
+
var root = _freeGlobal_default || freeSelf || Function("return this")();
|
|
6267
|
+
var _root_default = root;
|
|
6268
|
+
|
|
6269
|
+
// ../node_modules/lodash-es/_Symbol.js
|
|
6270
|
+
var Symbol2 = _root_default.Symbol;
|
|
6271
|
+
var _Symbol_default = Symbol2;
|
|
6272
|
+
|
|
6273
|
+
// ../node_modules/lodash-es/_getRawTag.js
|
|
6274
|
+
var objectProto = Object.prototype;
|
|
6275
|
+
var hasOwnProperty = objectProto.hasOwnProperty;
|
|
6276
|
+
var nativeObjectToString = objectProto.toString;
|
|
6277
|
+
var symToStringTag = _Symbol_default ? _Symbol_default.toStringTag : undefined;
|
|
6278
|
+
function getRawTag(value) {
|
|
6279
|
+
var isOwn = hasOwnProperty.call(value, symToStringTag), tag = value[symToStringTag];
|
|
6280
|
+
try {
|
|
6281
|
+
value[symToStringTag] = undefined;
|
|
6282
|
+
var unmasked = true;
|
|
6283
|
+
} catch (e) {}
|
|
6284
|
+
var result = nativeObjectToString.call(value);
|
|
6285
|
+
if (unmasked) {
|
|
6286
|
+
if (isOwn) {
|
|
6287
|
+
value[symToStringTag] = tag;
|
|
6325
6288
|
} else {
|
|
6326
|
-
|
|
6327
|
-
}
|
|
6328
|
-
},
|
|
6329
|
-
copyFileSync(src, dest) {
|
|
6330
|
-
fs.copyFileSync(src, dest);
|
|
6331
|
-
},
|
|
6332
|
-
unlinkSync(path) {
|
|
6333
|
-
fs.unlinkSync(path);
|
|
6334
|
-
},
|
|
6335
|
-
renameSync(oldPath, newPath) {
|
|
6336
|
-
fs.renameSync(oldPath, newPath);
|
|
6337
|
-
},
|
|
6338
|
-
linkSync(target, path) {
|
|
6339
|
-
fs.linkSync(target, path);
|
|
6340
|
-
},
|
|
6341
|
-
symlinkSync(target, path) {
|
|
6342
|
-
fs.symlinkSync(target, path);
|
|
6343
|
-
},
|
|
6344
|
-
readlinkSync(path) {
|
|
6345
|
-
return fs.readlinkSync(path);
|
|
6346
|
-
},
|
|
6347
|
-
realpathSync(path) {
|
|
6348
|
-
return fs.realpathSync(path);
|
|
6349
|
-
},
|
|
6350
|
-
mkdirSync(dirPath) {
|
|
6351
|
-
if (!fs.existsSync(dirPath)) {
|
|
6352
|
-
fs.mkdirSync(dirPath, { recursive: true, mode: 448 });
|
|
6289
|
+
delete value[symToStringTag];
|
|
6353
6290
|
}
|
|
6354
|
-
},
|
|
6355
|
-
readdirSync(dirPath) {
|
|
6356
|
-
return fs.readdirSync(dirPath, { withFileTypes: true });
|
|
6357
|
-
},
|
|
6358
|
-
readdirStringSync(dirPath) {
|
|
6359
|
-
return fs.readdirSync(dirPath);
|
|
6360
|
-
},
|
|
6361
|
-
isDirEmptySync(dirPath) {
|
|
6362
|
-
const files = this.readdirSync(dirPath);
|
|
6363
|
-
return files.length === 0;
|
|
6364
|
-
},
|
|
6365
|
-
rmdirSync(dirPath) {
|
|
6366
|
-
fs.rmdirSync(dirPath);
|
|
6367
|
-
},
|
|
6368
|
-
rmSync(path, options) {
|
|
6369
|
-
fs.rmSync(path, options);
|
|
6370
|
-
},
|
|
6371
|
-
createWriteStream(path) {
|
|
6372
|
-
return fs.createWriteStream(path);
|
|
6373
6291
|
}
|
|
6374
|
-
|
|
6375
|
-
var activeFs = NodeFsOperations;
|
|
6376
|
-
function getFsImplementation() {
|
|
6377
|
-
return activeFs;
|
|
6292
|
+
return result;
|
|
6378
6293
|
}
|
|
6294
|
+
var _getRawTag_default = getRawTag;
|
|
6379
6295
|
|
|
6380
|
-
// ../
|
|
6381
|
-
var
|
|
6382
|
-
|
|
6383
|
-
|
|
6384
|
-
|
|
6385
|
-
"Notification",
|
|
6386
|
-
"UserPromptSubmit",
|
|
6387
|
-
"SessionStart",
|
|
6388
|
-
"SessionEnd",
|
|
6389
|
-
"Stop",
|
|
6390
|
-
"SubagentStart",
|
|
6391
|
-
"SubagentStop",
|
|
6392
|
-
"PreCompact",
|
|
6393
|
-
"PermissionRequest"
|
|
6394
|
-
];
|
|
6395
|
-
var EXIT_REASONS = [
|
|
6396
|
-
"clear",
|
|
6397
|
-
"logout",
|
|
6398
|
-
"prompt_input_exit",
|
|
6399
|
-
"other",
|
|
6400
|
-
"bypass_permissions_disabled"
|
|
6401
|
-
];
|
|
6402
|
-
class AbortError extends Error {
|
|
6296
|
+
// ../node_modules/lodash-es/_objectToString.js
|
|
6297
|
+
var objectProto2 = Object.prototype;
|
|
6298
|
+
var nativeObjectToString2 = objectProto2.toString;
|
|
6299
|
+
function objectToString(value) {
|
|
6300
|
+
return nativeObjectToString2.call(value);
|
|
6403
6301
|
}
|
|
6302
|
+
var _objectToString_default = objectToString;
|
|
6404
6303
|
|
|
6405
|
-
// ../
|
|
6406
|
-
|
|
6407
|
-
|
|
6304
|
+
// ../node_modules/lodash-es/_baseGetTag.js
|
|
6305
|
+
var nullTag = "[object Null]";
|
|
6306
|
+
var undefinedTag = "[object Undefined]";
|
|
6307
|
+
var symToStringTag2 = _Symbol_default ? _Symbol_default.toStringTag : undefined;
|
|
6308
|
+
function baseGetTag(value) {
|
|
6309
|
+
if (value == null) {
|
|
6310
|
+
return value === undefined ? undefinedTag : nullTag;
|
|
6311
|
+
}
|
|
6312
|
+
return symToStringTag2 && symToStringTag2 in Object(value) ? _getRawTag_default(value) : _objectToString_default(value);
|
|
6408
6313
|
}
|
|
6314
|
+
var _baseGetTag_default = baseGetTag;
|
|
6409
6315
|
|
|
6410
|
-
// ../
|
|
6411
|
-
|
|
6412
|
-
|
|
6413
|
-
|
|
6414
|
-
|
|
6415
|
-
// ../src/utils/envUtils.ts
|
|
6416
|
-
import { join } from "path";
|
|
6417
|
-
import { homedir } from "os";
|
|
6418
|
-
function getClaudeConfigHomeDir() {
|
|
6419
|
-
return process.env.CLAUDE_CONFIG_DIR ?? join(homedir(), ".claude");
|
|
6316
|
+
// ../node_modules/lodash-es/isObject.js
|
|
6317
|
+
function isObject(value) {
|
|
6318
|
+
var type = typeof value;
|
|
6319
|
+
return value != null && (type == "object" || type == "function");
|
|
6420
6320
|
}
|
|
6421
|
-
|
|
6422
|
-
|
|
6321
|
+
var isObject_default = isObject;
|
|
6322
|
+
|
|
6323
|
+
// ../node_modules/lodash-es/isFunction.js
|
|
6324
|
+
var asyncTag = "[object AsyncFunction]";
|
|
6325
|
+
var funcTag = "[object Function]";
|
|
6326
|
+
var genTag = "[object GeneratorFunction]";
|
|
6327
|
+
var proxyTag = "[object Proxy]";
|
|
6328
|
+
function isFunction(value) {
|
|
6329
|
+
if (!isObject_default(value)) {
|
|
6423
6330
|
return false;
|
|
6424
|
-
|
|
6425
|
-
|
|
6426
|
-
|
|
6427
|
-
return ["1", "true", "yes", "on"].includes(normalizedValue);
|
|
6331
|
+
}
|
|
6332
|
+
var tag = _baseGetTag_default(value);
|
|
6333
|
+
return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;
|
|
6428
6334
|
}
|
|
6335
|
+
var isFunction_default = isFunction;
|
|
6429
6336
|
|
|
6430
|
-
// ../
|
|
6431
|
-
var
|
|
6432
|
-
var
|
|
6433
|
-
|
|
6434
|
-
|
|
6435
|
-
|
|
6436
|
-
|
|
6437
|
-
|
|
6438
|
-
|
|
6439
|
-
|
|
6440
|
-
|
|
6441
|
-
const debugDir = join2(getClaudeConfigHomeDir(), "debug");
|
|
6442
|
-
debugFilePath = join2(debugDir, `sdk-${randomUUID()}.txt`);
|
|
6443
|
-
if (!existsSync2(debugDir)) {
|
|
6444
|
-
mkdirSync2(debugDir, { recursive: true });
|
|
6445
|
-
}
|
|
6446
|
-
process.stderr.write(`SDK debug logs: ${debugFilePath}
|
|
6447
|
-
`);
|
|
6448
|
-
return debugFilePath;
|
|
6337
|
+
// ../node_modules/lodash-es/_coreJsData.js
|
|
6338
|
+
var coreJsData = _root_default["__core-js_shared__"];
|
|
6339
|
+
var _coreJsData_default = coreJsData;
|
|
6340
|
+
|
|
6341
|
+
// ../node_modules/lodash-es/_isMasked.js
|
|
6342
|
+
var maskSrcKey = function() {
|
|
6343
|
+
var uid = /[^.]+$/.exec(_coreJsData_default && _coreJsData_default.keys && _coreJsData_default.keys.IE_PROTO || "");
|
|
6344
|
+
return uid ? "Symbol(src)_1." + uid : "";
|
|
6345
|
+
}();
|
|
6346
|
+
function isMasked(func) {
|
|
6347
|
+
return !!maskSrcKey && maskSrcKey in func;
|
|
6449
6348
|
}
|
|
6450
|
-
|
|
6451
|
-
|
|
6452
|
-
|
|
6453
|
-
|
|
6349
|
+
var _isMasked_default = isMasked;
|
|
6350
|
+
|
|
6351
|
+
// ../node_modules/lodash-es/_toSource.js
|
|
6352
|
+
var funcProto = Function.prototype;
|
|
6353
|
+
var funcToString = funcProto.toString;
|
|
6354
|
+
function toSource(func) {
|
|
6355
|
+
if (func != null) {
|
|
6356
|
+
try {
|
|
6357
|
+
return funcToString.call(func);
|
|
6358
|
+
} catch (e) {}
|
|
6359
|
+
try {
|
|
6360
|
+
return func + "";
|
|
6361
|
+
} catch (e) {}
|
|
6454
6362
|
}
|
|
6455
|
-
|
|
6456
|
-
const output = `${timestamp} ${message}
|
|
6457
|
-
`;
|
|
6458
|
-
appendFileSync2(path, output);
|
|
6363
|
+
return "";
|
|
6459
6364
|
}
|
|
6365
|
+
var _toSource_default = toSource;
|
|
6460
6366
|
|
|
6461
|
-
// ../
|
|
6462
|
-
|
|
6463
|
-
|
|
6464
|
-
|
|
6465
|
-
|
|
6466
|
-
|
|
6467
|
-
|
|
6468
|
-
|
|
6469
|
-
|
|
6470
|
-
|
|
6471
|
-
|
|
6472
|
-
effectiveExtraArgs.settings = JSON.stringify(settingsObj);
|
|
6367
|
+
// ../node_modules/lodash-es/_baseIsNative.js
|
|
6368
|
+
var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
|
|
6369
|
+
var reIsHostCtor = /^\[object .+?Constructor\]$/;
|
|
6370
|
+
var funcProto2 = Function.prototype;
|
|
6371
|
+
var objectProto3 = Object.prototype;
|
|
6372
|
+
var funcToString2 = funcProto2.toString;
|
|
6373
|
+
var hasOwnProperty2 = objectProto3.hasOwnProperty;
|
|
6374
|
+
var reIsNative = RegExp("^" + funcToString2.call(hasOwnProperty2).replace(reRegExpChar, "\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, "$1.*?") + "$");
|
|
6375
|
+
function baseIsNative(value) {
|
|
6376
|
+
if (!isObject_default(value) || _isMasked_default(value)) {
|
|
6377
|
+
return false;
|
|
6473
6378
|
}
|
|
6474
|
-
|
|
6379
|
+
var pattern = isFunction_default(value) ? reIsNative : reIsHostCtor;
|
|
6380
|
+
return pattern.test(_toSource_default(value));
|
|
6475
6381
|
}
|
|
6382
|
+
var _baseIsNative_default = baseIsNative;
|
|
6476
6383
|
|
|
6477
|
-
// ../
|
|
6478
|
-
|
|
6479
|
-
|
|
6480
|
-
child;
|
|
6481
|
-
childStdin;
|
|
6482
|
-
childStdout;
|
|
6483
|
-
ready = false;
|
|
6484
|
-
abortController;
|
|
6485
|
-
exitError;
|
|
6486
|
-
exitListeners = [];
|
|
6487
|
-
processExitHandler;
|
|
6488
|
-
abortHandler;
|
|
6489
|
-
constructor(options) {
|
|
6490
|
-
this.options = options;
|
|
6491
|
-
this.abortController = options.abortController || createAbortController();
|
|
6492
|
-
this.initialize();
|
|
6493
|
-
}
|
|
6494
|
-
initialize() {
|
|
6495
|
-
try {
|
|
6496
|
-
const {
|
|
6497
|
-
additionalDirectories = [],
|
|
6498
|
-
betas,
|
|
6499
|
-
cwd,
|
|
6500
|
-
executable = isRunningWithBun() ? "bun" : "node",
|
|
6501
|
-
executableArgs = [],
|
|
6502
|
-
extraArgs = {},
|
|
6503
|
-
pathToClaudeCodeExecutable,
|
|
6504
|
-
env = { ...process.env },
|
|
6505
|
-
stderr,
|
|
6506
|
-
maxThinkingTokens,
|
|
6507
|
-
maxTurns,
|
|
6508
|
-
maxBudgetUsd,
|
|
6509
|
-
model,
|
|
6510
|
-
fallbackModel,
|
|
6511
|
-
jsonSchema,
|
|
6512
|
-
permissionMode,
|
|
6513
|
-
allowDangerouslySkipPermissions,
|
|
6514
|
-
permissionPromptToolName,
|
|
6515
|
-
continueConversation,
|
|
6516
|
-
resume,
|
|
6517
|
-
settingSources,
|
|
6518
|
-
allowedTools = [],
|
|
6519
|
-
disallowedTools = [],
|
|
6520
|
-
mcpServers,
|
|
6521
|
-
strictMcpConfig,
|
|
6522
|
-
canUseTool,
|
|
6523
|
-
includePartialMessages,
|
|
6524
|
-
plugins,
|
|
6525
|
-
sandbox
|
|
6526
|
-
} = this.options;
|
|
6527
|
-
const args = [
|
|
6528
|
-
"--output-format",
|
|
6529
|
-
"stream-json",
|
|
6530
|
-
"--verbose",
|
|
6531
|
-
"--input-format",
|
|
6532
|
-
"stream-json"
|
|
6533
|
-
];
|
|
6534
|
-
if (maxThinkingTokens !== undefined) {
|
|
6535
|
-
args.push("--max-thinking-tokens", maxThinkingTokens.toString());
|
|
6536
|
-
}
|
|
6537
|
-
if (maxTurns)
|
|
6538
|
-
args.push("--max-turns", maxTurns.toString());
|
|
6539
|
-
if (maxBudgetUsd !== undefined) {
|
|
6540
|
-
args.push("--max-budget-usd", maxBudgetUsd.toString());
|
|
6541
|
-
}
|
|
6542
|
-
if (model)
|
|
6543
|
-
args.push("--model", model);
|
|
6544
|
-
if (betas && betas.length > 0) {
|
|
6545
|
-
args.push("--betas", betas.join(","));
|
|
6546
|
-
}
|
|
6547
|
-
if (jsonSchema) {
|
|
6548
|
-
args.push("--json-schema", JSON.stringify(jsonSchema));
|
|
6549
|
-
}
|
|
6550
|
-
if (env.DEBUG_CLAUDE_AGENT_SDK) {
|
|
6551
|
-
args.push("--debug-to-stderr");
|
|
6552
|
-
}
|
|
6553
|
-
if (canUseTool) {
|
|
6554
|
-
if (permissionPromptToolName) {
|
|
6555
|
-
throw new Error("canUseTool callback cannot be used with permissionPromptToolName. Please use one or the other.");
|
|
6556
|
-
}
|
|
6557
|
-
args.push("--permission-prompt-tool", "stdio");
|
|
6558
|
-
} else if (permissionPromptToolName) {
|
|
6559
|
-
args.push("--permission-prompt-tool", permissionPromptToolName);
|
|
6560
|
-
}
|
|
6561
|
-
if (continueConversation)
|
|
6562
|
-
args.push("--continue");
|
|
6563
|
-
if (resume)
|
|
6564
|
-
args.push("--resume", resume);
|
|
6565
|
-
if (allowedTools.length > 0) {
|
|
6566
|
-
args.push("--allowedTools", allowedTools.join(","));
|
|
6567
|
-
}
|
|
6568
|
-
if (disallowedTools.length > 0) {
|
|
6569
|
-
args.push("--disallowedTools", disallowedTools.join(","));
|
|
6570
|
-
}
|
|
6571
|
-
const { tools } = this.options;
|
|
6572
|
-
if (tools !== undefined) {
|
|
6573
|
-
if (Array.isArray(tools)) {
|
|
6574
|
-
if (tools.length === 0) {
|
|
6575
|
-
args.push("--tools", "");
|
|
6576
|
-
} else {
|
|
6577
|
-
args.push("--tools", tools.join(","));
|
|
6578
|
-
}
|
|
6579
|
-
} else {
|
|
6580
|
-
args.push("--tools", "default");
|
|
6581
|
-
}
|
|
6582
|
-
}
|
|
6583
|
-
if (mcpServers && Object.keys(mcpServers).length > 0) {
|
|
6584
|
-
args.push("--mcp-config", JSON.stringify({ mcpServers }));
|
|
6585
|
-
}
|
|
6586
|
-
if (settingSources) {
|
|
6587
|
-
args.push("--setting-sources", settingSources.join(","));
|
|
6588
|
-
}
|
|
6589
|
-
if (strictMcpConfig) {
|
|
6590
|
-
args.push("--strict-mcp-config");
|
|
6591
|
-
}
|
|
6592
|
-
if (permissionMode) {
|
|
6593
|
-
args.push("--permission-mode", permissionMode);
|
|
6594
|
-
}
|
|
6595
|
-
if (allowDangerouslySkipPermissions) {
|
|
6596
|
-
args.push("--allow-dangerously-skip-permissions");
|
|
6597
|
-
}
|
|
6598
|
-
if (fallbackModel) {
|
|
6599
|
-
if (model && fallbackModel === model) {
|
|
6600
|
-
throw new Error("Fallback model cannot be the same as the main model. Please specify a different model for fallbackModel option.");
|
|
6601
|
-
}
|
|
6602
|
-
args.push("--fallback-model", fallbackModel);
|
|
6603
|
-
}
|
|
6604
|
-
if (includePartialMessages) {
|
|
6605
|
-
args.push("--include-partial-messages");
|
|
6606
|
-
}
|
|
6607
|
-
for (const dir of additionalDirectories) {
|
|
6608
|
-
args.push("--add-dir", dir);
|
|
6609
|
-
}
|
|
6610
|
-
if (plugins && plugins.length > 0) {
|
|
6611
|
-
for (const plugin of plugins) {
|
|
6612
|
-
if (plugin.type === "local") {
|
|
6613
|
-
args.push("--plugin-dir", plugin.path);
|
|
6614
|
-
} else {
|
|
6615
|
-
throw new Error(`Unsupported plugin type: ${plugin.type}`);
|
|
6616
|
-
}
|
|
6617
|
-
}
|
|
6618
|
-
}
|
|
6619
|
-
if (this.options.forkSession) {
|
|
6620
|
-
args.push("--fork-session");
|
|
6621
|
-
}
|
|
6622
|
-
if (this.options.resumeSessionAt) {
|
|
6623
|
-
args.push("--resume-session-at", this.options.resumeSessionAt);
|
|
6624
|
-
}
|
|
6625
|
-
const effectiveExtraArgs = mergeSandboxIntoExtraArgs(extraArgs ?? {}, sandbox);
|
|
6626
|
-
for (const [flag, value] of Object.entries(effectiveExtraArgs)) {
|
|
6627
|
-
if (value === null) {
|
|
6628
|
-
args.push(`--${flag}`);
|
|
6629
|
-
} else {
|
|
6630
|
-
args.push(`--${flag}`, value);
|
|
6631
|
-
}
|
|
6632
|
-
}
|
|
6633
|
-
if (!env.CLAUDE_CODE_ENTRYPOINT) {
|
|
6634
|
-
env.CLAUDE_CODE_ENTRYPOINT = "sdk-ts";
|
|
6635
|
-
}
|
|
6636
|
-
delete env.NODE_OPTIONS;
|
|
6637
|
-
if (env.DEBUG_CLAUDE_AGENT_SDK) {
|
|
6638
|
-
env.DEBUG = "1";
|
|
6639
|
-
} else {
|
|
6640
|
-
delete env.DEBUG;
|
|
6641
|
-
}
|
|
6642
|
-
const fs2 = getFsImplementation();
|
|
6643
|
-
if (!fs2.existsSync(pathToClaudeCodeExecutable)) {
|
|
6644
|
-
const errorMessage = isNativeBinary(pathToClaudeCodeExecutable) ? `Claude Code native binary not found at ${pathToClaudeCodeExecutable}. Please ensure Claude Code is installed via native installer or specify a valid path with options.pathToClaudeCodeExecutable.` : `Claude Code executable not found at ${pathToClaudeCodeExecutable}. Is options.pathToClaudeCodeExecutable set?`;
|
|
6645
|
-
throw new ReferenceError(errorMessage);
|
|
6646
|
-
}
|
|
6647
|
-
const isNative = isNativeBinary(pathToClaudeCodeExecutable);
|
|
6648
|
-
const spawnCommand = isNative ? pathToClaudeCodeExecutable : executable;
|
|
6649
|
-
const spawnArgs = isNative ? [...executableArgs, ...args] : [...executableArgs, pathToClaudeCodeExecutable, ...args];
|
|
6650
|
-
const spawnMessage = isNative ? `Spawning Claude Code native binary: ${spawnCommand} ${spawnArgs.join(" ")}` : `Spawning Claude Code process: ${spawnCommand} ${spawnArgs.join(" ")}`;
|
|
6651
|
-
logForSdkDebugging(spawnMessage);
|
|
6652
|
-
if (stderr) {
|
|
6653
|
-
stderr(spawnMessage);
|
|
6654
|
-
}
|
|
6655
|
-
const stderrMode = env.DEBUG_CLAUDE_AGENT_SDK || stderr ? "pipe" : "ignore";
|
|
6656
|
-
this.child = spawn(spawnCommand, spawnArgs, {
|
|
6657
|
-
cwd,
|
|
6658
|
-
stdio: ["pipe", "pipe", stderrMode],
|
|
6659
|
-
signal: this.abortController.signal,
|
|
6660
|
-
env
|
|
6661
|
-
});
|
|
6662
|
-
this.childStdin = this.child.stdin;
|
|
6663
|
-
this.childStdout = this.child.stdout;
|
|
6664
|
-
if (env.DEBUG_CLAUDE_AGENT_SDK || stderr) {
|
|
6665
|
-
this.child.stderr.on("data", (data) => {
|
|
6666
|
-
const message = data.toString();
|
|
6667
|
-
logForSdkDebugging(message);
|
|
6668
|
-
if (stderr) {
|
|
6669
|
-
stderr(message);
|
|
6670
|
-
}
|
|
6671
|
-
});
|
|
6672
|
-
}
|
|
6673
|
-
const cleanup = () => {
|
|
6674
|
-
if (this.child && !this.child.killed) {
|
|
6675
|
-
this.child.kill("SIGTERM");
|
|
6676
|
-
}
|
|
6677
|
-
};
|
|
6678
|
-
this.processExitHandler = cleanup;
|
|
6679
|
-
this.abortHandler = cleanup;
|
|
6680
|
-
process.on("exit", this.processExitHandler);
|
|
6681
|
-
this.abortController.signal.addEventListener("abort", this.abortHandler);
|
|
6682
|
-
this.child.on("error", (error) => {
|
|
6683
|
-
this.ready = false;
|
|
6684
|
-
if (this.abortController.signal.aborted) {
|
|
6685
|
-
this.exitError = new AbortError("Claude Code process aborted by user");
|
|
6686
|
-
} else {
|
|
6687
|
-
this.exitError = new Error(`Failed to spawn Claude Code process: ${error.message}`);
|
|
6688
|
-
logForSdkDebugging(this.exitError.message);
|
|
6689
|
-
}
|
|
6690
|
-
});
|
|
6691
|
-
this.child.on("close", (code, signal) => {
|
|
6692
|
-
this.ready = false;
|
|
6693
|
-
if (this.abortController.signal.aborted) {
|
|
6694
|
-
this.exitError = new AbortError("Claude Code process aborted by user");
|
|
6695
|
-
} else {
|
|
6696
|
-
const error = this.getProcessExitError(code, signal);
|
|
6697
|
-
if (error) {
|
|
6698
|
-
this.exitError = error;
|
|
6699
|
-
logForSdkDebugging(error.message);
|
|
6700
|
-
}
|
|
6701
|
-
}
|
|
6702
|
-
});
|
|
6703
|
-
this.ready = true;
|
|
6704
|
-
} catch (error) {
|
|
6705
|
-
this.ready = false;
|
|
6706
|
-
throw error;
|
|
6707
|
-
}
|
|
6708
|
-
}
|
|
6709
|
-
getProcessExitError(code, signal) {
|
|
6710
|
-
if (code !== 0 && code !== null) {
|
|
6711
|
-
return new Error(`Claude Code process exited with code ${code}`);
|
|
6712
|
-
} else if (signal) {
|
|
6713
|
-
return new Error(`Claude Code process terminated by signal ${signal}`);
|
|
6714
|
-
}
|
|
6715
|
-
return;
|
|
6716
|
-
}
|
|
6717
|
-
write(data) {
|
|
6718
|
-
if (this.abortController.signal.aborted) {
|
|
6719
|
-
throw new AbortError("Operation aborted");
|
|
6720
|
-
}
|
|
6721
|
-
if (!this.ready || !this.childStdin) {
|
|
6722
|
-
throw new Error("ProcessTransport is not ready for writing");
|
|
6723
|
-
}
|
|
6724
|
-
if (this.child?.killed || this.child?.exitCode !== null) {
|
|
6725
|
-
throw new Error("Cannot write to terminated process");
|
|
6726
|
-
}
|
|
6727
|
-
if (this.exitError) {
|
|
6728
|
-
throw new Error(`Cannot write to process that exited with error: ${this.exitError.message}`);
|
|
6729
|
-
}
|
|
6730
|
-
logForSdkDebugging(`[ProcessTransport] Writing to stdin: ${data.substring(0, 100)}`);
|
|
6731
|
-
try {
|
|
6732
|
-
const written = this.childStdin.write(data);
|
|
6733
|
-
if (!written) {
|
|
6734
|
-
logForSdkDebugging("[ProcessTransport] Write buffer full, data queued");
|
|
6735
|
-
}
|
|
6736
|
-
} catch (error) {
|
|
6737
|
-
this.ready = false;
|
|
6738
|
-
throw new Error(`Failed to write to process stdin: ${error.message}`);
|
|
6739
|
-
}
|
|
6740
|
-
}
|
|
6741
|
-
close() {
|
|
6742
|
-
if (this.childStdin) {
|
|
6743
|
-
this.childStdin.end();
|
|
6744
|
-
this.childStdin = undefined;
|
|
6745
|
-
}
|
|
6746
|
-
if (this.processExitHandler) {
|
|
6747
|
-
process.off("exit", this.processExitHandler);
|
|
6748
|
-
this.processExitHandler = undefined;
|
|
6749
|
-
}
|
|
6750
|
-
if (this.abortHandler) {
|
|
6751
|
-
this.abortController.signal.removeEventListener("abort", this.abortHandler);
|
|
6752
|
-
this.abortHandler = undefined;
|
|
6753
|
-
}
|
|
6754
|
-
for (const { handler } of this.exitListeners) {
|
|
6755
|
-
this.child?.off("exit", handler);
|
|
6756
|
-
}
|
|
6757
|
-
this.exitListeners = [];
|
|
6758
|
-
if (this.child && !this.child.killed) {
|
|
6759
|
-
this.child.kill("SIGTERM");
|
|
6760
|
-
setTimeout(() => {
|
|
6761
|
-
if (this.child && !this.child.killed) {
|
|
6762
|
-
this.child.kill("SIGKILL");
|
|
6763
|
-
}
|
|
6764
|
-
}, 5000);
|
|
6765
|
-
}
|
|
6766
|
-
this.ready = false;
|
|
6767
|
-
}
|
|
6768
|
-
isReady() {
|
|
6769
|
-
return this.ready;
|
|
6770
|
-
}
|
|
6771
|
-
async* readMessages() {
|
|
6772
|
-
if (!this.childStdout) {
|
|
6773
|
-
throw new Error("ProcessTransport output stream not available");
|
|
6774
|
-
}
|
|
6775
|
-
const rl = createInterface({ input: this.childStdout });
|
|
6776
|
-
try {
|
|
6777
|
-
for await (const line of rl) {
|
|
6778
|
-
if (line.trim()) {
|
|
6779
|
-
const message = JSON.parse(line);
|
|
6780
|
-
yield message;
|
|
6781
|
-
}
|
|
6782
|
-
}
|
|
6783
|
-
await this.waitForExit();
|
|
6784
|
-
} catch (error) {
|
|
6785
|
-
throw error;
|
|
6786
|
-
} finally {
|
|
6787
|
-
rl.close();
|
|
6788
|
-
}
|
|
6789
|
-
}
|
|
6790
|
-
endInput() {
|
|
6791
|
-
if (this.childStdin) {
|
|
6792
|
-
this.childStdin.end();
|
|
6793
|
-
}
|
|
6794
|
-
}
|
|
6795
|
-
getInputStream() {
|
|
6796
|
-
return this.childStdin;
|
|
6797
|
-
}
|
|
6798
|
-
onExit(callback) {
|
|
6799
|
-
if (!this.child)
|
|
6800
|
-
return () => {};
|
|
6801
|
-
const handler = (code, signal) => {
|
|
6802
|
-
const error = this.getProcessExitError(code, signal);
|
|
6803
|
-
callback(error);
|
|
6804
|
-
};
|
|
6805
|
-
this.child.on("exit", handler);
|
|
6806
|
-
this.exitListeners.push({ callback, handler });
|
|
6807
|
-
return () => {
|
|
6808
|
-
if (this.child) {
|
|
6809
|
-
this.child.off("exit", handler);
|
|
6810
|
-
}
|
|
6811
|
-
const index = this.exitListeners.findIndex((l) => l.handler === handler);
|
|
6812
|
-
if (index !== -1) {
|
|
6813
|
-
this.exitListeners.splice(index, 1);
|
|
6814
|
-
}
|
|
6815
|
-
};
|
|
6816
|
-
}
|
|
6817
|
-
async waitForExit() {
|
|
6818
|
-
if (!this.child) {
|
|
6819
|
-
if (this.exitError) {
|
|
6820
|
-
throw this.exitError;
|
|
6821
|
-
}
|
|
6822
|
-
return;
|
|
6823
|
-
}
|
|
6824
|
-
if (this.child.exitCode !== null || this.child.killed) {
|
|
6825
|
-
if (this.exitError) {
|
|
6826
|
-
throw this.exitError;
|
|
6827
|
-
}
|
|
6828
|
-
return;
|
|
6829
|
-
}
|
|
6830
|
-
return new Promise((resolve, reject) => {
|
|
6831
|
-
const exitHandler = (code, signal) => {
|
|
6832
|
-
if (this.abortController.signal.aborted) {
|
|
6833
|
-
reject(new AbortError("Operation aborted"));
|
|
6834
|
-
return;
|
|
6835
|
-
}
|
|
6836
|
-
const error = this.getProcessExitError(code, signal);
|
|
6837
|
-
if (error) {
|
|
6838
|
-
reject(error);
|
|
6839
|
-
} else {
|
|
6840
|
-
resolve();
|
|
6841
|
-
}
|
|
6842
|
-
};
|
|
6843
|
-
this.child.once("exit", exitHandler);
|
|
6844
|
-
const errorHandler = (error) => {
|
|
6845
|
-
this.child.off("exit", exitHandler);
|
|
6846
|
-
reject(error);
|
|
6847
|
-
};
|
|
6848
|
-
this.child.once("error", errorHandler);
|
|
6849
|
-
this.child.once("exit", () => {
|
|
6850
|
-
this.child.off("error", errorHandler);
|
|
6851
|
-
});
|
|
6852
|
-
});
|
|
6853
|
-
}
|
|
6384
|
+
// ../node_modules/lodash-es/_getValue.js
|
|
6385
|
+
function getValue(object, key) {
|
|
6386
|
+
return object == null ? undefined : object[key];
|
|
6854
6387
|
}
|
|
6855
|
-
|
|
6856
|
-
|
|
6857
|
-
|
|
6388
|
+
var _getValue_default = getValue;
|
|
6389
|
+
|
|
6390
|
+
// ../node_modules/lodash-es/_getNative.js
|
|
6391
|
+
function getNative(object, key) {
|
|
6392
|
+
var value = _getValue_default(object, key);
|
|
6393
|
+
return _baseIsNative_default(value) ? value : undefined;
|
|
6858
6394
|
}
|
|
6395
|
+
var _getNative_default = getNative;
|
|
6859
6396
|
|
|
6860
|
-
// ../
|
|
6861
|
-
|
|
6862
|
-
|
|
6863
|
-
|
|
6864
|
-
|
|
6865
|
-
|
|
6866
|
-
|
|
6867
|
-
|
|
6868
|
-
started = false;
|
|
6869
|
-
constructor(returned) {
|
|
6870
|
-
this.returned = returned;
|
|
6871
|
-
}
|
|
6872
|
-
[Symbol.asyncIterator]() {
|
|
6873
|
-
if (this.started) {
|
|
6874
|
-
throw new Error("Stream can only be iterated once");
|
|
6875
|
-
}
|
|
6876
|
-
this.started = true;
|
|
6877
|
-
return this;
|
|
6878
|
-
}
|
|
6879
|
-
next() {
|
|
6880
|
-
if (this.queue.length > 0) {
|
|
6881
|
-
return Promise.resolve({
|
|
6882
|
-
done: false,
|
|
6883
|
-
value: this.queue.shift()
|
|
6884
|
-
});
|
|
6885
|
-
}
|
|
6886
|
-
if (this.isDone) {
|
|
6887
|
-
return Promise.resolve({ done: true, value: undefined });
|
|
6888
|
-
}
|
|
6889
|
-
if (this.hasError) {
|
|
6890
|
-
return Promise.reject(this.hasError);
|
|
6891
|
-
}
|
|
6892
|
-
return new Promise((resolve, reject) => {
|
|
6893
|
-
this.readResolve = resolve;
|
|
6894
|
-
this.readReject = reject;
|
|
6895
|
-
});
|
|
6896
|
-
}
|
|
6897
|
-
enqueue(value) {
|
|
6898
|
-
if (this.readResolve) {
|
|
6899
|
-
const resolve = this.readResolve;
|
|
6900
|
-
this.readResolve = undefined;
|
|
6901
|
-
this.readReject = undefined;
|
|
6902
|
-
resolve({ done: false, value });
|
|
6903
|
-
} else {
|
|
6904
|
-
this.queue.push(value);
|
|
6905
|
-
}
|
|
6906
|
-
}
|
|
6907
|
-
done() {
|
|
6908
|
-
this.isDone = true;
|
|
6909
|
-
if (this.readResolve) {
|
|
6910
|
-
const resolve = this.readResolve;
|
|
6911
|
-
this.readResolve = undefined;
|
|
6912
|
-
this.readReject = undefined;
|
|
6913
|
-
resolve({ done: true, value: undefined });
|
|
6914
|
-
}
|
|
6915
|
-
}
|
|
6916
|
-
error(error) {
|
|
6917
|
-
this.hasError = error;
|
|
6918
|
-
if (this.readReject) {
|
|
6919
|
-
const reject = this.readReject;
|
|
6920
|
-
this.readResolve = undefined;
|
|
6921
|
-
this.readReject = undefined;
|
|
6922
|
-
reject(error);
|
|
6923
|
-
}
|
|
6924
|
-
}
|
|
6925
|
-
return() {
|
|
6926
|
-
this.isDone = true;
|
|
6927
|
-
if (this.returned) {
|
|
6928
|
-
this.returned();
|
|
6929
|
-
}
|
|
6930
|
-
return Promise.resolve({ done: true, value: undefined });
|
|
6931
|
-
}
|
|
6397
|
+
// ../node_modules/lodash-es/_nativeCreate.js
|
|
6398
|
+
var nativeCreate = _getNative_default(Object, "create");
|
|
6399
|
+
var _nativeCreate_default = nativeCreate;
|
|
6400
|
+
|
|
6401
|
+
// ../node_modules/lodash-es/_hashClear.js
|
|
6402
|
+
function hashClear() {
|
|
6403
|
+
this.__data__ = _nativeCreate_default ? _nativeCreate_default(null) : {};
|
|
6404
|
+
this.size = 0;
|
|
6932
6405
|
}
|
|
6406
|
+
var _hashClear_default = hashClear;
|
|
6933
6407
|
|
|
6934
|
-
// ../
|
|
6935
|
-
|
|
6936
|
-
|
|
6937
|
-
|
|
6938
|
-
|
|
6939
|
-
this.sendMcpMessage = sendMcpMessage;
|
|
6940
|
-
}
|
|
6941
|
-
onclose;
|
|
6942
|
-
onerror;
|
|
6943
|
-
onmessage;
|
|
6944
|
-
async start() {}
|
|
6945
|
-
async send(message) {
|
|
6946
|
-
if (this.isClosed) {
|
|
6947
|
-
throw new Error("Transport is closed");
|
|
6948
|
-
}
|
|
6949
|
-
this.sendMcpMessage(message);
|
|
6950
|
-
}
|
|
6951
|
-
async close() {
|
|
6952
|
-
if (this.isClosed) {
|
|
6953
|
-
return;
|
|
6954
|
-
}
|
|
6955
|
-
this.isClosed = true;
|
|
6956
|
-
this.onclose?.();
|
|
6957
|
-
}
|
|
6408
|
+
// ../node_modules/lodash-es/_hashDelete.js
|
|
6409
|
+
function hashDelete(key) {
|
|
6410
|
+
var result = this.has(key) && delete this.__data__[key];
|
|
6411
|
+
this.size -= result ? 1 : 0;
|
|
6412
|
+
return result;
|
|
6958
6413
|
}
|
|
6414
|
+
var _hashDelete_default = hashDelete;
|
|
6959
6415
|
|
|
6960
|
-
// ../node_modules/lodash-es/
|
|
6961
|
-
var
|
|
6962
|
-
var
|
|
6416
|
+
// ../node_modules/lodash-es/_hashGet.js
|
|
6417
|
+
var HASH_UNDEFINED = "__lodash_hash_undefined__";
|
|
6418
|
+
var objectProto4 = Object.prototype;
|
|
6419
|
+
var hasOwnProperty3 = objectProto4.hasOwnProperty;
|
|
6420
|
+
function hashGet(key) {
|
|
6421
|
+
var data = this.__data__;
|
|
6422
|
+
if (_nativeCreate_default) {
|
|
6423
|
+
var result = data[key];
|
|
6424
|
+
return result === HASH_UNDEFINED ? undefined : result;
|
|
6425
|
+
}
|
|
6426
|
+
return hasOwnProperty3.call(data, key) ? data[key] : undefined;
|
|
6427
|
+
}
|
|
6428
|
+
var _hashGet_default = hashGet;
|
|
6963
6429
|
|
|
6964
|
-
// ../node_modules/lodash-es/
|
|
6965
|
-
var
|
|
6966
|
-
var
|
|
6967
|
-
|
|
6430
|
+
// ../node_modules/lodash-es/_hashHas.js
|
|
6431
|
+
var objectProto5 = Object.prototype;
|
|
6432
|
+
var hasOwnProperty4 = objectProto5.hasOwnProperty;
|
|
6433
|
+
function hashHas(key) {
|
|
6434
|
+
var data = this.__data__;
|
|
6435
|
+
return _nativeCreate_default ? data[key] !== undefined : hasOwnProperty4.call(data, key);
|
|
6436
|
+
}
|
|
6437
|
+
var _hashHas_default = hashHas;
|
|
6968
6438
|
|
|
6969
|
-
// ../node_modules/lodash-es/
|
|
6970
|
-
var
|
|
6971
|
-
|
|
6439
|
+
// ../node_modules/lodash-es/_hashSet.js
|
|
6440
|
+
var HASH_UNDEFINED2 = "__lodash_hash_undefined__";
|
|
6441
|
+
function hashSet(key, value) {
|
|
6442
|
+
var data = this.__data__;
|
|
6443
|
+
this.size += this.has(key) ? 0 : 1;
|
|
6444
|
+
data[key] = _nativeCreate_default && value === undefined ? HASH_UNDEFINED2 : value;
|
|
6445
|
+
return this;
|
|
6446
|
+
}
|
|
6447
|
+
var _hashSet_default = hashSet;
|
|
6972
6448
|
|
|
6973
|
-
// ../node_modules/lodash-es/
|
|
6974
|
-
|
|
6975
|
-
var
|
|
6976
|
-
|
|
6977
|
-
|
|
6978
|
-
|
|
6979
|
-
|
|
6980
|
-
try {
|
|
6981
|
-
value[symToStringTag] = undefined;
|
|
6982
|
-
var unmasked = true;
|
|
6983
|
-
} catch (e) {}
|
|
6984
|
-
var result = nativeObjectToString.call(value);
|
|
6985
|
-
if (unmasked) {
|
|
6986
|
-
if (isOwn) {
|
|
6987
|
-
value[symToStringTag] = tag;
|
|
6988
|
-
} else {
|
|
6989
|
-
delete value[symToStringTag];
|
|
6990
|
-
}
|
|
6449
|
+
// ../node_modules/lodash-es/_Hash.js
|
|
6450
|
+
function Hash(entries) {
|
|
6451
|
+
var index = -1, length = entries == null ? 0 : entries.length;
|
|
6452
|
+
this.clear();
|
|
6453
|
+
while (++index < length) {
|
|
6454
|
+
var entry = entries[index];
|
|
6455
|
+
this.set(entry[0], entry[1]);
|
|
6991
6456
|
}
|
|
6992
|
-
return result;
|
|
6993
6457
|
}
|
|
6994
|
-
|
|
6458
|
+
Hash.prototype.clear = _hashClear_default;
|
|
6459
|
+
Hash.prototype["delete"] = _hashDelete_default;
|
|
6460
|
+
Hash.prototype.get = _hashGet_default;
|
|
6461
|
+
Hash.prototype.has = _hashHas_default;
|
|
6462
|
+
Hash.prototype.set = _hashSet_default;
|
|
6463
|
+
var _Hash_default = Hash;
|
|
6995
6464
|
|
|
6996
|
-
// ../node_modules/lodash-es/
|
|
6997
|
-
|
|
6998
|
-
|
|
6999
|
-
|
|
7000
|
-
return nativeObjectToString2.call(value);
|
|
6465
|
+
// ../node_modules/lodash-es/_listCacheClear.js
|
|
6466
|
+
function listCacheClear() {
|
|
6467
|
+
this.__data__ = [];
|
|
6468
|
+
this.size = 0;
|
|
7001
6469
|
}
|
|
7002
|
-
var
|
|
6470
|
+
var _listCacheClear_default = listCacheClear;
|
|
7003
6471
|
|
|
7004
|
-
// ../node_modules/lodash-es/
|
|
7005
|
-
|
|
7006
|
-
|
|
7007
|
-
var symToStringTag2 = _Symbol_default ? _Symbol_default.toStringTag : undefined;
|
|
7008
|
-
function baseGetTag(value) {
|
|
7009
|
-
if (value == null) {
|
|
7010
|
-
return value === undefined ? undefinedTag : nullTag;
|
|
7011
|
-
}
|
|
7012
|
-
return symToStringTag2 && symToStringTag2 in Object(value) ? _getRawTag_default(value) : _objectToString_default(value);
|
|
6472
|
+
// ../node_modules/lodash-es/eq.js
|
|
6473
|
+
function eq(value, other) {
|
|
6474
|
+
return value === other || value !== value && other !== other;
|
|
7013
6475
|
}
|
|
7014
|
-
var
|
|
6476
|
+
var eq_default = eq;
|
|
7015
6477
|
|
|
7016
|
-
// ../node_modules/lodash-es/
|
|
7017
|
-
function
|
|
7018
|
-
var
|
|
7019
|
-
|
|
6478
|
+
// ../node_modules/lodash-es/_assocIndexOf.js
|
|
6479
|
+
function assocIndexOf(array, key) {
|
|
6480
|
+
var length = array.length;
|
|
6481
|
+
while (length--) {
|
|
6482
|
+
if (eq_default(array[length][0], key)) {
|
|
6483
|
+
return length;
|
|
6484
|
+
}
|
|
6485
|
+
}
|
|
6486
|
+
return -1;
|
|
7020
6487
|
}
|
|
7021
|
-
var
|
|
6488
|
+
var _assocIndexOf_default = assocIndexOf;
|
|
7022
6489
|
|
|
7023
|
-
// ../node_modules/lodash-es/
|
|
7024
|
-
var
|
|
7025
|
-
var
|
|
7026
|
-
|
|
7027
|
-
var
|
|
7028
|
-
|
|
7029
|
-
if (!isObject_default(value)) {
|
|
6490
|
+
// ../node_modules/lodash-es/_listCacheDelete.js
|
|
6491
|
+
var arrayProto = Array.prototype;
|
|
6492
|
+
var splice = arrayProto.splice;
|
|
6493
|
+
function listCacheDelete(key) {
|
|
6494
|
+
var data = this.__data__, index = _assocIndexOf_default(data, key);
|
|
6495
|
+
if (index < 0) {
|
|
7030
6496
|
return false;
|
|
7031
6497
|
}
|
|
7032
|
-
var
|
|
7033
|
-
|
|
6498
|
+
var lastIndex = data.length - 1;
|
|
6499
|
+
if (index == lastIndex) {
|
|
6500
|
+
data.pop();
|
|
6501
|
+
} else {
|
|
6502
|
+
splice.call(data, index, 1);
|
|
6503
|
+
}
|
|
6504
|
+
--this.size;
|
|
6505
|
+
return true;
|
|
7034
6506
|
}
|
|
7035
|
-
var
|
|
6507
|
+
var _listCacheDelete_default = listCacheDelete;
|
|
7036
6508
|
|
|
7037
|
-
// ../node_modules/lodash-es/
|
|
7038
|
-
|
|
7039
|
-
var
|
|
6509
|
+
// ../node_modules/lodash-es/_listCacheGet.js
|
|
6510
|
+
function listCacheGet(key) {
|
|
6511
|
+
var data = this.__data__, index = _assocIndexOf_default(data, key);
|
|
6512
|
+
return index < 0 ? undefined : data[index][1];
|
|
6513
|
+
}
|
|
6514
|
+
var _listCacheGet_default = listCacheGet;
|
|
7040
6515
|
|
|
7041
|
-
// ../node_modules/lodash-es/
|
|
7042
|
-
|
|
7043
|
-
|
|
7044
|
-
return uid ? "Symbol(src)_1." + uid : "";
|
|
7045
|
-
}();
|
|
7046
|
-
function isMasked(func) {
|
|
7047
|
-
return !!maskSrcKey && maskSrcKey in func;
|
|
6516
|
+
// ../node_modules/lodash-es/_listCacheHas.js
|
|
6517
|
+
function listCacheHas(key) {
|
|
6518
|
+
return _assocIndexOf_default(this.__data__, key) > -1;
|
|
7048
6519
|
}
|
|
7049
|
-
var
|
|
6520
|
+
var _listCacheHas_default = listCacheHas;
|
|
7050
6521
|
|
|
7051
|
-
// ../node_modules/lodash-es/
|
|
7052
|
-
|
|
7053
|
-
var
|
|
7054
|
-
|
|
7055
|
-
|
|
7056
|
-
|
|
7057
|
-
|
|
7058
|
-
|
|
7059
|
-
try {
|
|
7060
|
-
return func + "";
|
|
7061
|
-
} catch (e) {}
|
|
6522
|
+
// ../node_modules/lodash-es/_listCacheSet.js
|
|
6523
|
+
function listCacheSet(key, value) {
|
|
6524
|
+
var data = this.__data__, index = _assocIndexOf_default(data, key);
|
|
6525
|
+
if (index < 0) {
|
|
6526
|
+
++this.size;
|
|
6527
|
+
data.push([key, value]);
|
|
6528
|
+
} else {
|
|
6529
|
+
data[index][1] = value;
|
|
7062
6530
|
}
|
|
7063
|
-
return
|
|
6531
|
+
return this;
|
|
7064
6532
|
}
|
|
7065
|
-
var
|
|
6533
|
+
var _listCacheSet_default = listCacheSet;
|
|
7066
6534
|
|
|
7067
|
-
// ../node_modules/lodash-es/
|
|
7068
|
-
|
|
7069
|
-
var
|
|
7070
|
-
|
|
7071
|
-
|
|
7072
|
-
var
|
|
7073
|
-
|
|
7074
|
-
var reIsNative = RegExp("^" + funcToString2.call(hasOwnProperty2).replace(reRegExpChar, "\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, "$1.*?") + "$");
|
|
7075
|
-
function baseIsNative(value) {
|
|
7076
|
-
if (!isObject_default(value) || _isMasked_default(value)) {
|
|
7077
|
-
return false;
|
|
6535
|
+
// ../node_modules/lodash-es/_ListCache.js
|
|
6536
|
+
function ListCache(entries) {
|
|
6537
|
+
var index = -1, length = entries == null ? 0 : entries.length;
|
|
6538
|
+
this.clear();
|
|
6539
|
+
while (++index < length) {
|
|
6540
|
+
var entry = entries[index];
|
|
6541
|
+
this.set(entry[0], entry[1]);
|
|
7078
6542
|
}
|
|
7079
|
-
var pattern = isFunction_default(value) ? reIsNative : reIsHostCtor;
|
|
7080
|
-
return pattern.test(_toSource_default(value));
|
|
7081
6543
|
}
|
|
7082
|
-
|
|
6544
|
+
ListCache.prototype.clear = _listCacheClear_default;
|
|
6545
|
+
ListCache.prototype["delete"] = _listCacheDelete_default;
|
|
6546
|
+
ListCache.prototype.get = _listCacheGet_default;
|
|
6547
|
+
ListCache.prototype.has = _listCacheHas_default;
|
|
6548
|
+
ListCache.prototype.set = _listCacheSet_default;
|
|
6549
|
+
var _ListCache_default = ListCache;
|
|
7083
6550
|
|
|
7084
|
-
// ../node_modules/lodash-es/
|
|
7085
|
-
|
|
7086
|
-
|
|
7087
|
-
}
|
|
7088
|
-
var _getValue_default = getValue;
|
|
6551
|
+
// ../node_modules/lodash-es/_Map.js
|
|
6552
|
+
var Map2 = _getNative_default(_root_default, "Map");
|
|
6553
|
+
var _Map_default = Map2;
|
|
7089
6554
|
|
|
7090
|
-
// ../node_modules/lodash-es/
|
|
7091
|
-
function
|
|
7092
|
-
|
|
7093
|
-
|
|
6555
|
+
// ../node_modules/lodash-es/_mapCacheClear.js
|
|
6556
|
+
function mapCacheClear() {
|
|
6557
|
+
this.size = 0;
|
|
6558
|
+
this.__data__ = {
|
|
6559
|
+
hash: new _Hash_default,
|
|
6560
|
+
map: new (_Map_default || _ListCache_default),
|
|
6561
|
+
string: new _Hash_default
|
|
6562
|
+
};
|
|
7094
6563
|
}
|
|
7095
|
-
var
|
|
6564
|
+
var _mapCacheClear_default = mapCacheClear;
|
|
7096
6565
|
|
|
7097
|
-
// ../node_modules/lodash-es/
|
|
7098
|
-
|
|
7099
|
-
var
|
|
6566
|
+
// ../node_modules/lodash-es/_isKeyable.js
|
|
6567
|
+
function isKeyable(value) {
|
|
6568
|
+
var type = typeof value;
|
|
6569
|
+
return type == "string" || type == "number" || type == "symbol" || type == "boolean" ? value !== "__proto__" : value === null;
|
|
6570
|
+
}
|
|
6571
|
+
var _isKeyable_default = isKeyable;
|
|
7100
6572
|
|
|
7101
|
-
// ../node_modules/lodash-es/
|
|
7102
|
-
function
|
|
7103
|
-
|
|
7104
|
-
|
|
6573
|
+
// ../node_modules/lodash-es/_getMapData.js
|
|
6574
|
+
function getMapData(map, key) {
|
|
6575
|
+
var data = map.__data__;
|
|
6576
|
+
return _isKeyable_default(key) ? data[typeof key == "string" ? "string" : "hash"] : data.map;
|
|
7105
6577
|
}
|
|
7106
|
-
var
|
|
6578
|
+
var _getMapData_default = getMapData;
|
|
7107
6579
|
|
|
7108
|
-
// ../node_modules/lodash-es/
|
|
7109
|
-
function
|
|
7110
|
-
var result = this
|
|
6580
|
+
// ../node_modules/lodash-es/_mapCacheDelete.js
|
|
6581
|
+
function mapCacheDelete(key) {
|
|
6582
|
+
var result = _getMapData_default(this, key)["delete"](key);
|
|
7111
6583
|
this.size -= result ? 1 : 0;
|
|
7112
6584
|
return result;
|
|
7113
6585
|
}
|
|
7114
|
-
var
|
|
6586
|
+
var _mapCacheDelete_default = mapCacheDelete;
|
|
7115
6587
|
|
|
7116
|
-
// ../node_modules/lodash-es/
|
|
7117
|
-
|
|
7118
|
-
|
|
7119
|
-
var hasOwnProperty3 = objectProto4.hasOwnProperty;
|
|
7120
|
-
function hashGet(key) {
|
|
7121
|
-
var data = this.__data__;
|
|
7122
|
-
if (_nativeCreate_default) {
|
|
7123
|
-
var result = data[key];
|
|
7124
|
-
return result === HASH_UNDEFINED ? undefined : result;
|
|
7125
|
-
}
|
|
7126
|
-
return hasOwnProperty3.call(data, key) ? data[key] : undefined;
|
|
6588
|
+
// ../node_modules/lodash-es/_mapCacheGet.js
|
|
6589
|
+
function mapCacheGet(key) {
|
|
6590
|
+
return _getMapData_default(this, key).get(key);
|
|
7127
6591
|
}
|
|
7128
|
-
var
|
|
6592
|
+
var _mapCacheGet_default = mapCacheGet;
|
|
7129
6593
|
|
|
7130
|
-
// ../node_modules/lodash-es/
|
|
7131
|
-
|
|
7132
|
-
|
|
7133
|
-
function hashHas(key) {
|
|
7134
|
-
var data = this.__data__;
|
|
7135
|
-
return _nativeCreate_default ? data[key] !== undefined : hasOwnProperty4.call(data, key);
|
|
6594
|
+
// ../node_modules/lodash-es/_mapCacheHas.js
|
|
6595
|
+
function mapCacheHas(key) {
|
|
6596
|
+
return _getMapData_default(this, key).has(key);
|
|
7136
6597
|
}
|
|
7137
|
-
var
|
|
6598
|
+
var _mapCacheHas_default = mapCacheHas;
|
|
7138
6599
|
|
|
7139
|
-
// ../node_modules/lodash-es/
|
|
7140
|
-
|
|
7141
|
-
|
|
7142
|
-
|
|
7143
|
-
this.size +=
|
|
7144
|
-
data[key] = _nativeCreate_default && value === undefined ? HASH_UNDEFINED2 : value;
|
|
6600
|
+
// ../node_modules/lodash-es/_mapCacheSet.js
|
|
6601
|
+
function mapCacheSet(key, value) {
|
|
6602
|
+
var data = _getMapData_default(this, key), size = data.size;
|
|
6603
|
+
data.set(key, value);
|
|
6604
|
+
this.size += data.size == size ? 0 : 1;
|
|
7145
6605
|
return this;
|
|
7146
6606
|
}
|
|
7147
|
-
var
|
|
6607
|
+
var _mapCacheSet_default = mapCacheSet;
|
|
7148
6608
|
|
|
7149
|
-
// ../node_modules/lodash-es/
|
|
7150
|
-
function
|
|
6609
|
+
// ../node_modules/lodash-es/_MapCache.js
|
|
6610
|
+
function MapCache(entries) {
|
|
7151
6611
|
var index = -1, length = entries == null ? 0 : entries.length;
|
|
7152
6612
|
this.clear();
|
|
7153
6613
|
while (++index < length) {
|
|
7154
6614
|
var entry = entries[index];
|
|
7155
6615
|
this.set(entry[0], entry[1]);
|
|
7156
6616
|
}
|
|
7157
|
-
}
|
|
7158
|
-
|
|
7159
|
-
|
|
7160
|
-
|
|
7161
|
-
|
|
7162
|
-
|
|
7163
|
-
var
|
|
6617
|
+
}
|
|
6618
|
+
MapCache.prototype.clear = _mapCacheClear_default;
|
|
6619
|
+
MapCache.prototype["delete"] = _mapCacheDelete_default;
|
|
6620
|
+
MapCache.prototype.get = _mapCacheGet_default;
|
|
6621
|
+
MapCache.prototype.has = _mapCacheHas_default;
|
|
6622
|
+
MapCache.prototype.set = _mapCacheSet_default;
|
|
6623
|
+
var _MapCache_default = MapCache;
|
|
6624
|
+
|
|
6625
|
+
// ../node_modules/lodash-es/memoize.js
|
|
6626
|
+
var FUNC_ERROR_TEXT = "Expected a function";
|
|
6627
|
+
function memoize(func, resolver) {
|
|
6628
|
+
if (typeof func != "function" || resolver != null && typeof resolver != "function") {
|
|
6629
|
+
throw new TypeError(FUNC_ERROR_TEXT);
|
|
6630
|
+
}
|
|
6631
|
+
var memoized = function() {
|
|
6632
|
+
var args = arguments, key = resolver ? resolver.apply(this, args) : args[0], cache = memoized.cache;
|
|
6633
|
+
if (cache.has(key)) {
|
|
6634
|
+
return cache.get(key);
|
|
6635
|
+
}
|
|
6636
|
+
var result = func.apply(this, args);
|
|
6637
|
+
memoized.cache = cache.set(key, result) || cache;
|
|
6638
|
+
return result;
|
|
6639
|
+
};
|
|
6640
|
+
memoized.cache = new (memoize.Cache || _MapCache_default);
|
|
6641
|
+
return memoized;
|
|
6642
|
+
}
|
|
6643
|
+
memoize.Cache = _MapCache_default;
|
|
6644
|
+
var memoize_default = memoize;
|
|
6645
|
+
|
|
6646
|
+
// ../src/utils/process.ts
|
|
6647
|
+
var CHUNK_SIZE = 2000;
|
|
6648
|
+
function writeToStderr(data) {
|
|
6649
|
+
for (let i = 0;i < data.length; i += CHUNK_SIZE) {
|
|
6650
|
+
process.stderr.write(data.substring(i, i + CHUNK_SIZE));
|
|
6651
|
+
}
|
|
6652
|
+
}
|
|
6653
|
+
|
|
6654
|
+
// ../src/utils/debugFilter.ts
|
|
6655
|
+
var parseDebugFilter = memoize_default((filterString) => {
|
|
6656
|
+
if (!filterString || filterString.trim() === "") {
|
|
6657
|
+
return null;
|
|
6658
|
+
}
|
|
6659
|
+
const filters = filterString.split(",").map((f) => f.trim()).filter(Boolean);
|
|
6660
|
+
if (filters.length === 0) {
|
|
6661
|
+
return null;
|
|
6662
|
+
}
|
|
6663
|
+
const hasExclusive = filters.some((f) => f.startsWith("!"));
|
|
6664
|
+
const hasInclusive = filters.some((f) => !f.startsWith("!"));
|
|
6665
|
+
if (hasExclusive && hasInclusive) {
|
|
6666
|
+
return null;
|
|
6667
|
+
}
|
|
6668
|
+
const cleanFilters = filters.map((f) => f.replace(/^!/, "").toLowerCase());
|
|
6669
|
+
return {
|
|
6670
|
+
include: hasExclusive ? [] : cleanFilters,
|
|
6671
|
+
exclude: hasExclusive ? cleanFilters : [],
|
|
6672
|
+
isExclusive: hasExclusive
|
|
6673
|
+
};
|
|
6674
|
+
});
|
|
6675
|
+
function extractDebugCategories(message) {
|
|
6676
|
+
const categories = [];
|
|
6677
|
+
const mcpMatch = message.match(/^MCP server ["']([^"']+)["']/);
|
|
6678
|
+
if (mcpMatch && mcpMatch[1]) {
|
|
6679
|
+
categories.push("mcp");
|
|
6680
|
+
categories.push(mcpMatch[1].toLowerCase());
|
|
6681
|
+
} else {
|
|
6682
|
+
const prefixMatch = message.match(/^([^:[]+):/);
|
|
6683
|
+
if (prefixMatch && prefixMatch[1]) {
|
|
6684
|
+
categories.push(prefixMatch[1].trim().toLowerCase());
|
|
6685
|
+
}
|
|
6686
|
+
}
|
|
6687
|
+
const bracketMatch = message.match(/^\[([^\]]+)]/);
|
|
6688
|
+
if (bracketMatch && bracketMatch[1]) {
|
|
6689
|
+
categories.push(bracketMatch[1].trim().toLowerCase());
|
|
6690
|
+
}
|
|
6691
|
+
if (message.toLowerCase().includes("statsig event:")) {
|
|
6692
|
+
categories.push("statsig");
|
|
6693
|
+
}
|
|
6694
|
+
const secondaryMatch = message.match(/:\s*([^:]+?)(?:\s+(?:type|mode|status|event))?:/);
|
|
6695
|
+
if (secondaryMatch && secondaryMatch[1]) {
|
|
6696
|
+
const secondary = secondaryMatch[1].trim().toLowerCase();
|
|
6697
|
+
if (secondary.length < 30 && !secondary.includes(" ")) {
|
|
6698
|
+
categories.push(secondary);
|
|
6699
|
+
}
|
|
6700
|
+
}
|
|
6701
|
+
return Array.from(new Set(categories));
|
|
6702
|
+
}
|
|
6703
|
+
function shouldShowDebugCategories(categories, filter) {
|
|
6704
|
+
if (!filter) {
|
|
6705
|
+
return true;
|
|
6706
|
+
}
|
|
6707
|
+
if (categories.length === 0) {
|
|
6708
|
+
return false;
|
|
6709
|
+
}
|
|
6710
|
+
if (filter.isExclusive) {
|
|
6711
|
+
return !categories.some((cat) => filter.exclude.includes(cat));
|
|
6712
|
+
} else {
|
|
6713
|
+
return categories.some((cat) => filter.include.includes(cat));
|
|
6714
|
+
}
|
|
6715
|
+
}
|
|
6716
|
+
function shouldShowDebugMessage(message, filter) {
|
|
6717
|
+
if (!filter) {
|
|
6718
|
+
return true;
|
|
6719
|
+
}
|
|
6720
|
+
const categories = extractDebugCategories(message);
|
|
6721
|
+
return shouldShowDebugCategories(categories, filter);
|
|
6722
|
+
}
|
|
6723
|
+
|
|
6724
|
+
// ../src/utils/envUtils.ts
|
|
6725
|
+
import { join } from "path";
|
|
6726
|
+
import { homedir } from "os";
|
|
6727
|
+
function getClaudeConfigHomeDir() {
|
|
6728
|
+
return process.env.CLAUDE_CONFIG_DIR ?? join(homedir(), ".claude");
|
|
6729
|
+
}
|
|
6730
|
+
function isEnvTruthy(envVar) {
|
|
6731
|
+
if (!envVar)
|
|
6732
|
+
return false;
|
|
6733
|
+
if (typeof envVar === "boolean")
|
|
6734
|
+
return envVar;
|
|
6735
|
+
const normalizedValue = envVar.toLowerCase().trim();
|
|
6736
|
+
return ["1", "true", "yes", "on"].includes(normalizedValue);
|
|
6737
|
+
}
|
|
6738
|
+
|
|
6739
|
+
// ../src/utils/debug.ts
|
|
6740
|
+
import { dirname, join as join2 } from "path";
|
|
6741
|
+
|
|
6742
|
+
// ../src/bootstrap/state.ts
|
|
6743
|
+
import { cwd } from "process";
|
|
6744
|
+
import { realpathSync } from "fs";
|
|
6745
|
+
import { randomUUID } from "crypto";
|
|
6746
|
+
|
|
6747
|
+
// ../src/bootstrap/envValidators.ts
|
|
6748
|
+
var bashMaxOutputLengthValidator = {
|
|
6749
|
+
name: "BASH_MAX_OUTPUT_LENGTH",
|
|
6750
|
+
default: 30000,
|
|
6751
|
+
validate: (value) => {
|
|
6752
|
+
const MAX_OUTPUT_LENGTH = 150000;
|
|
6753
|
+
const DEFAULT_MAX_OUTPUT_LENGTH = 30000;
|
|
6754
|
+
if (!value) {
|
|
6755
|
+
return {
|
|
6756
|
+
effective: DEFAULT_MAX_OUTPUT_LENGTH,
|
|
6757
|
+
status: "valid"
|
|
6758
|
+
};
|
|
6759
|
+
}
|
|
6760
|
+
const parsed = parseInt(value, 10);
|
|
6761
|
+
if (isNaN(parsed) || parsed <= 0) {
|
|
6762
|
+
return {
|
|
6763
|
+
effective: DEFAULT_MAX_OUTPUT_LENGTH,
|
|
6764
|
+
status: "invalid",
|
|
6765
|
+
message: `Invalid value "${value}" (using default: ${DEFAULT_MAX_OUTPUT_LENGTH})`
|
|
6766
|
+
};
|
|
6767
|
+
}
|
|
6768
|
+
if (parsed > MAX_OUTPUT_LENGTH) {
|
|
6769
|
+
return {
|
|
6770
|
+
effective: MAX_OUTPUT_LENGTH,
|
|
6771
|
+
status: "capped",
|
|
6772
|
+
message: `Capped from ${parsed} to ${MAX_OUTPUT_LENGTH}`
|
|
6773
|
+
};
|
|
6774
|
+
}
|
|
6775
|
+
return { effective: parsed, status: "valid" };
|
|
6776
|
+
}
|
|
6777
|
+
};
|
|
6778
|
+
var maxOutputTokensValidator = {
|
|
6779
|
+
name: "CLAUDE_CODE_MAX_OUTPUT_TOKENS",
|
|
6780
|
+
default: 32000,
|
|
6781
|
+
validate: (value) => {
|
|
6782
|
+
const MAX_OUTPUT_TOKENS = 64000;
|
|
6783
|
+
const DEFAULT_MAX_OUTPUT_TOKENS = 32000;
|
|
6784
|
+
if (!value) {
|
|
6785
|
+
return { effective: DEFAULT_MAX_OUTPUT_TOKENS, status: "valid" };
|
|
6786
|
+
}
|
|
6787
|
+
const parsed = parseInt(value, 10);
|
|
6788
|
+
if (isNaN(parsed) || parsed <= 0) {
|
|
6789
|
+
return {
|
|
6790
|
+
effective: DEFAULT_MAX_OUTPUT_TOKENS,
|
|
6791
|
+
status: "invalid",
|
|
6792
|
+
message: `Invalid value "${value}" (using default: ${DEFAULT_MAX_OUTPUT_TOKENS})`
|
|
6793
|
+
};
|
|
6794
|
+
}
|
|
6795
|
+
if (parsed > MAX_OUTPUT_TOKENS) {
|
|
6796
|
+
return {
|
|
6797
|
+
effective: MAX_OUTPUT_TOKENS,
|
|
6798
|
+
status: "capped",
|
|
6799
|
+
message: `Capped from ${parsed} to ${MAX_OUTPUT_TOKENS}`
|
|
6800
|
+
};
|
|
6801
|
+
}
|
|
6802
|
+
return { effective: parsed, status: "valid" };
|
|
6803
|
+
}
|
|
6804
|
+
};
|
|
7164
6805
|
|
|
7165
|
-
// ../
|
|
7166
|
-
function
|
|
7167
|
-
|
|
7168
|
-
|
|
6806
|
+
// ../src/bootstrap/state.ts
|
|
6807
|
+
function getInitialState() {
|
|
6808
|
+
let resolvedCwd = "";
|
|
6809
|
+
if (typeof process !== "undefined" && typeof process.cwd === "function") {
|
|
6810
|
+
resolvedCwd = realpathSync(cwd());
|
|
6811
|
+
}
|
|
6812
|
+
return {
|
|
6813
|
+
originalCwd: resolvedCwd,
|
|
6814
|
+
totalCostUSD: 0,
|
|
6815
|
+
totalAPIDuration: 0,
|
|
6816
|
+
totalAPIDurationWithoutRetries: 0,
|
|
6817
|
+
totalToolDuration: 0,
|
|
6818
|
+
startTime: Date.now(),
|
|
6819
|
+
lastInteractionTime: Date.now(),
|
|
6820
|
+
totalLinesAdded: 0,
|
|
6821
|
+
totalLinesRemoved: 0,
|
|
6822
|
+
hasUnknownModelCost: false,
|
|
6823
|
+
cwd: resolvedCwd,
|
|
6824
|
+
modelUsage: {},
|
|
6825
|
+
mainLoopModelOverride: undefined,
|
|
6826
|
+
initialMainLoopModel: null,
|
|
6827
|
+
modelStrings: null,
|
|
6828
|
+
isInteractive: false,
|
|
6829
|
+
clientType: "cli",
|
|
6830
|
+
sessionIngressToken: undefined,
|
|
6831
|
+
oauthTokenFromFd: undefined,
|
|
6832
|
+
apiKeyFromFd: undefined,
|
|
6833
|
+
flagSettingsPath: undefined,
|
|
6834
|
+
allowedSettingSources: [
|
|
6835
|
+
"userSettings",
|
|
6836
|
+
"projectSettings",
|
|
6837
|
+
"localSettings",
|
|
6838
|
+
"flagSettings",
|
|
6839
|
+
"policySettings"
|
|
6840
|
+
],
|
|
6841
|
+
meter: null,
|
|
6842
|
+
sessionCounter: null,
|
|
6843
|
+
locCounter: null,
|
|
6844
|
+
prCounter: null,
|
|
6845
|
+
commitCounter: null,
|
|
6846
|
+
costCounter: null,
|
|
6847
|
+
tokenCounter: null,
|
|
6848
|
+
codeEditToolDecisionCounter: null,
|
|
6849
|
+
activeTimeCounter: null,
|
|
6850
|
+
sessionId: randomUUID(),
|
|
6851
|
+
loggerProvider: null,
|
|
6852
|
+
eventLogger: null,
|
|
6853
|
+
meterProvider: null,
|
|
6854
|
+
tracerProvider: null,
|
|
6855
|
+
agentColorMap: new Map,
|
|
6856
|
+
agentColorIndex: 0,
|
|
6857
|
+
envVarValidators: [bashMaxOutputLengthValidator, maxOutputTokensValidator],
|
|
6858
|
+
lastAPIRequest: null,
|
|
6859
|
+
inMemoryErrorLog: [],
|
|
6860
|
+
inlinePlugins: [],
|
|
6861
|
+
sessionBypassPermissionsMode: false,
|
|
6862
|
+
sessionPersistenceDisabled: false,
|
|
6863
|
+
hasExitedPlanMode: false,
|
|
6864
|
+
needsPlanModeExitAttachment: false,
|
|
6865
|
+
initJsonSchema: null,
|
|
6866
|
+
registeredHooks: null,
|
|
6867
|
+
planSlugCache: new Map
|
|
6868
|
+
};
|
|
7169
6869
|
}
|
|
7170
|
-
var
|
|
7171
|
-
|
|
7172
|
-
|
|
7173
|
-
function eq(value, other) {
|
|
7174
|
-
return value === other || value !== value && other !== other;
|
|
6870
|
+
var STATE = getInitialState();
|
|
6871
|
+
function getSessionId() {
|
|
6872
|
+
return STATE.sessionId;
|
|
7175
6873
|
}
|
|
7176
|
-
var eq_default = eq;
|
|
7177
6874
|
|
|
7178
|
-
// ../
|
|
7179
|
-
function
|
|
7180
|
-
|
|
7181
|
-
|
|
7182
|
-
|
|
7183
|
-
|
|
6875
|
+
// ../src/utils/bufferedWriter.ts
|
|
6876
|
+
function createBufferedWriter({
|
|
6877
|
+
writeFn,
|
|
6878
|
+
flushIntervalMs = 1000,
|
|
6879
|
+
maxBufferSize = 100,
|
|
6880
|
+
immediateMode = false
|
|
6881
|
+
}) {
|
|
6882
|
+
let buffer = [];
|
|
6883
|
+
let flushTimer = null;
|
|
6884
|
+
function clearTimer() {
|
|
6885
|
+
if (flushTimer) {
|
|
6886
|
+
clearTimeout(flushTimer);
|
|
6887
|
+
flushTimer = null;
|
|
7184
6888
|
}
|
|
7185
6889
|
}
|
|
7186
|
-
|
|
7187
|
-
|
|
7188
|
-
|
|
7189
|
-
|
|
7190
|
-
|
|
7191
|
-
|
|
7192
|
-
var splice = arrayProto.splice;
|
|
7193
|
-
function listCacheDelete(key) {
|
|
7194
|
-
var data = this.__data__, index = _assocIndexOf_default(data, key);
|
|
7195
|
-
if (index < 0) {
|
|
7196
|
-
return false;
|
|
6890
|
+
function flush() {
|
|
6891
|
+
if (buffer.length === 0)
|
|
6892
|
+
return;
|
|
6893
|
+
writeFn(buffer.join(""));
|
|
6894
|
+
buffer = [];
|
|
6895
|
+
clearTimer();
|
|
7197
6896
|
}
|
|
7198
|
-
|
|
7199
|
-
|
|
7200
|
-
|
|
7201
|
-
|
|
7202
|
-
splice.call(data, index, 1);
|
|
6897
|
+
function scheduleFlush() {
|
|
6898
|
+
if (!flushTimer) {
|
|
6899
|
+
flushTimer = setTimeout(flush, flushIntervalMs);
|
|
6900
|
+
}
|
|
7203
6901
|
}
|
|
7204
|
-
|
|
7205
|
-
|
|
7206
|
-
|
|
7207
|
-
|
|
7208
|
-
|
|
7209
|
-
|
|
7210
|
-
|
|
7211
|
-
|
|
7212
|
-
|
|
6902
|
+
return {
|
|
6903
|
+
write(content) {
|
|
6904
|
+
if (immediateMode) {
|
|
6905
|
+
writeFn(content);
|
|
6906
|
+
return;
|
|
6907
|
+
}
|
|
6908
|
+
buffer.push(content);
|
|
6909
|
+
scheduleFlush();
|
|
6910
|
+
if (buffer.length >= maxBufferSize) {
|
|
6911
|
+
flush();
|
|
6912
|
+
}
|
|
6913
|
+
},
|
|
6914
|
+
flush,
|
|
6915
|
+
dispose() {
|
|
6916
|
+
flush();
|
|
6917
|
+
}
|
|
6918
|
+
};
|
|
7213
6919
|
}
|
|
7214
|
-
var _listCacheGet_default = listCacheGet;
|
|
7215
6920
|
|
|
7216
|
-
// ../
|
|
7217
|
-
|
|
7218
|
-
|
|
6921
|
+
// ../src/utils/cleanupRegistry.ts
|
|
6922
|
+
var cleanupFunctions = new Set;
|
|
6923
|
+
function registerCleanup(cleanupFn) {
|
|
6924
|
+
cleanupFunctions.add(cleanupFn);
|
|
6925
|
+
return () => cleanupFunctions.delete(cleanupFn);
|
|
7219
6926
|
}
|
|
7220
|
-
var _listCacheHas_default = listCacheHas;
|
|
7221
6927
|
|
|
7222
|
-
// ../
|
|
7223
|
-
|
|
7224
|
-
|
|
7225
|
-
|
|
7226
|
-
|
|
7227
|
-
|
|
7228
|
-
|
|
7229
|
-
|
|
6928
|
+
// ../src/utils/debug.ts
|
|
6929
|
+
var isDebugMode = memoize_default(() => {
|
|
6930
|
+
return isEnvTruthy(process.env.DEBUG) || isEnvTruthy(process.env.DEBUG_SDK) || process.argv.includes("--debug") || process.argv.includes("-d") || isDebugToStdErr() || process.argv.some((arg) => arg.startsWith("--debug="));
|
|
6931
|
+
});
|
|
6932
|
+
var getDebugFilter = memoize_default(() => {
|
|
6933
|
+
const debugArg = process.argv.find((arg) => arg.startsWith("--debug="));
|
|
6934
|
+
if (!debugArg) {
|
|
6935
|
+
return null;
|
|
7230
6936
|
}
|
|
7231
|
-
|
|
7232
|
-
|
|
7233
|
-
|
|
7234
|
-
|
|
7235
|
-
|
|
7236
|
-
|
|
7237
|
-
|
|
7238
|
-
|
|
7239
|
-
|
|
7240
|
-
|
|
7241
|
-
this.set(entry[0], entry[1]);
|
|
6937
|
+
const filterPattern = debugArg.substring("--debug=".length);
|
|
6938
|
+
return parseDebugFilter(filterPattern);
|
|
6939
|
+
});
|
|
6940
|
+
var isDebugToStdErr = memoize_default(() => {
|
|
6941
|
+
return process.argv.includes("--debug-to-stderr") || process.argv.includes("-d2e");
|
|
6942
|
+
});
|
|
6943
|
+
function shouldLogDebugMessage(message) {
|
|
6944
|
+
if (false) {}
|
|
6945
|
+
if (typeof process === "undefined" || typeof process.versions === "undefined" || typeof process.versions.node === "undefined") {
|
|
6946
|
+
return false;
|
|
7242
6947
|
}
|
|
6948
|
+
const filter = getDebugFilter();
|
|
6949
|
+
return shouldShowDebugMessage(message, filter);
|
|
7243
6950
|
}
|
|
7244
|
-
|
|
7245
|
-
|
|
7246
|
-
|
|
7247
|
-
|
|
7248
|
-
|
|
7249
|
-
|
|
7250
|
-
|
|
7251
|
-
|
|
7252
|
-
|
|
7253
|
-
|
|
7254
|
-
|
|
7255
|
-
|
|
7256
|
-
|
|
7257
|
-
|
|
7258
|
-
|
|
7259
|
-
|
|
7260
|
-
|
|
7261
|
-
|
|
7262
|
-
}
|
|
7263
|
-
|
|
7264
|
-
var _mapCacheClear_default = mapCacheClear;
|
|
7265
|
-
|
|
7266
|
-
// ../node_modules/lodash-es/_isKeyable.js
|
|
7267
|
-
function isKeyable(value) {
|
|
7268
|
-
var type = typeof value;
|
|
7269
|
-
return type == "string" || type == "number" || type == "symbol" || type == "boolean" ? value !== "__proto__" : value === null;
|
|
7270
|
-
}
|
|
7271
|
-
var _isKeyable_default = isKeyable;
|
|
7272
|
-
|
|
7273
|
-
// ../node_modules/lodash-es/_getMapData.js
|
|
7274
|
-
function getMapData(map, key) {
|
|
7275
|
-
var data = map.__data__;
|
|
7276
|
-
return _isKeyable_default(key) ? data[typeof key == "string" ? "string" : "hash"] : data.map;
|
|
7277
|
-
}
|
|
7278
|
-
var _getMapData_default = getMapData;
|
|
7279
|
-
|
|
7280
|
-
// ../node_modules/lodash-es/_mapCacheDelete.js
|
|
7281
|
-
function mapCacheDelete(key) {
|
|
7282
|
-
var result = _getMapData_default(this, key)["delete"](key);
|
|
7283
|
-
this.size -= result ? 1 : 0;
|
|
7284
|
-
return result;
|
|
6951
|
+
var hasFormattedOutput = false;
|
|
6952
|
+
var debugWriter = null;
|
|
6953
|
+
function getDebugWriter() {
|
|
6954
|
+
if (!debugWriter) {
|
|
6955
|
+
debugWriter = createBufferedWriter({
|
|
6956
|
+
writeFn: (content) => {
|
|
6957
|
+
const path = getDebugLogPath();
|
|
6958
|
+
if (!getFsImplementation().existsSync(dirname(path))) {
|
|
6959
|
+
getFsImplementation().mkdirSync(dirname(path));
|
|
6960
|
+
}
|
|
6961
|
+
getFsImplementation().appendFileSync(path, content);
|
|
6962
|
+
updateLatestDebugLogSymlink();
|
|
6963
|
+
},
|
|
6964
|
+
flushIntervalMs: 1000,
|
|
6965
|
+
maxBufferSize: 100,
|
|
6966
|
+
immediateMode: isDebugMode()
|
|
6967
|
+
});
|
|
6968
|
+
registerCleanup(async () => debugWriter?.dispose());
|
|
6969
|
+
}
|
|
6970
|
+
return debugWriter;
|
|
7285
6971
|
}
|
|
7286
|
-
|
|
7287
|
-
|
|
7288
|
-
|
|
7289
|
-
|
|
7290
|
-
|
|
6972
|
+
function logForDebugging(message, { level } = {
|
|
6973
|
+
level: "debug"
|
|
6974
|
+
}) {
|
|
6975
|
+
if (!shouldLogDebugMessage(message)) {
|
|
6976
|
+
return;
|
|
6977
|
+
}
|
|
6978
|
+
if (hasFormattedOutput && message.includes(`
|
|
6979
|
+
`)) {
|
|
6980
|
+
message = JSON.stringify(message);
|
|
6981
|
+
}
|
|
6982
|
+
const timestamp = new Date().toISOString();
|
|
6983
|
+
const output = `${timestamp} [${level.toUpperCase()}] ${message.trim()}
|
|
6984
|
+
`;
|
|
6985
|
+
if (isDebugToStdErr()) {
|
|
6986
|
+
writeToStderr(output);
|
|
6987
|
+
return;
|
|
6988
|
+
}
|
|
6989
|
+
getDebugWriter().write(output);
|
|
7291
6990
|
}
|
|
7292
|
-
|
|
7293
|
-
|
|
7294
|
-
// ../node_modules/lodash-es/_mapCacheHas.js
|
|
7295
|
-
function mapCacheHas(key) {
|
|
7296
|
-
return _getMapData_default(this, key).has(key);
|
|
6991
|
+
function getDebugLogPath() {
|
|
6992
|
+
return process.env.CLAUDE_CODE_DEBUG_LOGS_DIR ?? join2(getClaudeConfigHomeDir(), "debug", `${getSessionId()}.txt`);
|
|
7297
6993
|
}
|
|
7298
|
-
var
|
|
6994
|
+
var updateLatestDebugLogSymlink = memoize_default(() => {
|
|
6995
|
+
if (process.argv[2] === "--ripgrep") {
|
|
6996
|
+
return;
|
|
6997
|
+
}
|
|
6998
|
+
try {
|
|
6999
|
+
const debugLogPath = getDebugLogPath();
|
|
7000
|
+
const debugLogsDir = dirname(debugLogPath);
|
|
7001
|
+
const latestSymlinkPath = join2(debugLogsDir, "latest");
|
|
7002
|
+
if (!getFsImplementation().existsSync(debugLogsDir)) {
|
|
7003
|
+
getFsImplementation().mkdirSync(debugLogsDir);
|
|
7004
|
+
}
|
|
7005
|
+
if (getFsImplementation().existsSync(latestSymlinkPath)) {
|
|
7006
|
+
try {
|
|
7007
|
+
getFsImplementation().unlinkSync(latestSymlinkPath);
|
|
7008
|
+
} catch {}
|
|
7009
|
+
}
|
|
7010
|
+
getFsImplementation().symlinkSync(debugLogPath, latestSymlinkPath);
|
|
7011
|
+
} catch {}
|
|
7012
|
+
});
|
|
7299
7013
|
|
|
7300
|
-
// ../
|
|
7301
|
-
|
|
7302
|
-
|
|
7303
|
-
|
|
7304
|
-
|
|
7305
|
-
|
|
7014
|
+
// ../src/utils/fsOperations.ts
|
|
7015
|
+
var SLOW_SYNC_THRESHOLD_MS = 5;
|
|
7016
|
+
function withSlowLogging(operation, fn) {
|
|
7017
|
+
const startTime = performance.now();
|
|
7018
|
+
try {
|
|
7019
|
+
return fn();
|
|
7020
|
+
} finally {
|
|
7021
|
+
const duration = performance.now() - startTime;
|
|
7022
|
+
if (duration > SLOW_SYNC_THRESHOLD_MS) {
|
|
7023
|
+
logForDebugging(`[SLOW OPERATION DETECTED] fs.${operation} (${duration.toFixed(1)}ms)`);
|
|
7024
|
+
}
|
|
7025
|
+
}
|
|
7306
7026
|
}
|
|
7307
|
-
var
|
|
7308
|
-
|
|
7309
|
-
|
|
7310
|
-
|
|
7311
|
-
|
|
7312
|
-
|
|
7313
|
-
|
|
7314
|
-
|
|
7315
|
-
|
|
7027
|
+
var NodeFsOperations = {
|
|
7028
|
+
cwd() {
|
|
7029
|
+
return process.cwd();
|
|
7030
|
+
},
|
|
7031
|
+
existsSync(fsPath) {
|
|
7032
|
+
return withSlowLogging("existsSync", () => fs.existsSync(fsPath));
|
|
7033
|
+
},
|
|
7034
|
+
async stat(fsPath) {
|
|
7035
|
+
return statPromise(fsPath);
|
|
7036
|
+
},
|
|
7037
|
+
statSync(fsPath) {
|
|
7038
|
+
return withSlowLogging("statSync", () => fs.statSync(fsPath));
|
|
7039
|
+
},
|
|
7040
|
+
readFileSync(fsPath, options) {
|
|
7041
|
+
return withSlowLogging("readFileSync", () => fs.readFileSync(fsPath, { encoding: options.encoding }));
|
|
7042
|
+
},
|
|
7043
|
+
readFileBytesSync(fsPath) {
|
|
7044
|
+
return withSlowLogging("readFileBytesSync", () => fs.readFileSync(fsPath));
|
|
7045
|
+
},
|
|
7046
|
+
readSync(fsPath, options) {
|
|
7047
|
+
return withSlowLogging("readSync", () => {
|
|
7048
|
+
let fd = undefined;
|
|
7049
|
+
try {
|
|
7050
|
+
fd = fs.openSync(fsPath, "r");
|
|
7051
|
+
const buffer = Buffer.alloc(options.length);
|
|
7052
|
+
const bytesRead = fs.readSync(fd, buffer, 0, options.length, 0);
|
|
7053
|
+
return { buffer, bytesRead };
|
|
7054
|
+
} finally {
|
|
7055
|
+
if (fd)
|
|
7056
|
+
fs.closeSync(fd);
|
|
7057
|
+
}
|
|
7058
|
+
});
|
|
7059
|
+
},
|
|
7060
|
+
writeFileSync(fsPath, data, options) {
|
|
7061
|
+
return withSlowLogging("writeFileSync", () => {
|
|
7062
|
+
const fileExists = fs.existsSync(fsPath);
|
|
7063
|
+
if (!options.flush) {
|
|
7064
|
+
const writeOptions = {
|
|
7065
|
+
encoding: options.encoding
|
|
7066
|
+
};
|
|
7067
|
+
if (!fileExists) {
|
|
7068
|
+
writeOptions.mode = options.mode ?? 384;
|
|
7069
|
+
} else if (options.mode !== undefined) {
|
|
7070
|
+
writeOptions.mode = options.mode;
|
|
7071
|
+
}
|
|
7072
|
+
fs.writeFileSync(fsPath, data, writeOptions);
|
|
7073
|
+
return;
|
|
7074
|
+
}
|
|
7075
|
+
let fd;
|
|
7076
|
+
try {
|
|
7077
|
+
const mode = !fileExists ? options.mode ?? 384 : options.mode;
|
|
7078
|
+
fd = fs.openSync(fsPath, "w", mode);
|
|
7079
|
+
fs.writeFileSync(fd, data, { encoding: options.encoding });
|
|
7080
|
+
fs.fsyncSync(fd);
|
|
7081
|
+
} finally {
|
|
7082
|
+
if (fd) {
|
|
7083
|
+
fs.closeSync(fd);
|
|
7084
|
+
}
|
|
7085
|
+
}
|
|
7086
|
+
});
|
|
7087
|
+
},
|
|
7088
|
+
appendFileSync(path, data, options) {
|
|
7089
|
+
return withSlowLogging("appendFileSync", () => {
|
|
7090
|
+
if (!fs.existsSync(path)) {
|
|
7091
|
+
const mode = options?.mode ?? 384;
|
|
7092
|
+
const fd = fs.openSync(path, "a", mode);
|
|
7093
|
+
try {
|
|
7094
|
+
fs.appendFileSync(fd, data);
|
|
7095
|
+
} finally {
|
|
7096
|
+
fs.closeSync(fd);
|
|
7097
|
+
}
|
|
7098
|
+
} else {
|
|
7099
|
+
fs.appendFileSync(path, data);
|
|
7100
|
+
}
|
|
7101
|
+
});
|
|
7102
|
+
},
|
|
7103
|
+
copyFileSync(src, dest) {
|
|
7104
|
+
return withSlowLogging("copyFileSync", () => fs.copyFileSync(src, dest));
|
|
7105
|
+
},
|
|
7106
|
+
unlinkSync(path) {
|
|
7107
|
+
return withSlowLogging("unlinkSync", () => fs.unlinkSync(path));
|
|
7108
|
+
},
|
|
7109
|
+
renameSync(oldPath, newPath) {
|
|
7110
|
+
return withSlowLogging("renameSync", () => fs.renameSync(oldPath, newPath));
|
|
7111
|
+
},
|
|
7112
|
+
linkSync(target, path) {
|
|
7113
|
+
return withSlowLogging("linkSync", () => fs.linkSync(target, path));
|
|
7114
|
+
},
|
|
7115
|
+
symlinkSync(target, path) {
|
|
7116
|
+
return withSlowLogging("symlinkSync", () => fs.symlinkSync(target, path));
|
|
7117
|
+
},
|
|
7118
|
+
readlinkSync(path) {
|
|
7119
|
+
return withSlowLogging("readlinkSync", () => fs.readlinkSync(path));
|
|
7120
|
+
},
|
|
7121
|
+
realpathSync(path) {
|
|
7122
|
+
return withSlowLogging("realpathSync", () => fs.realpathSync(path));
|
|
7123
|
+
},
|
|
7124
|
+
mkdirSync(dirPath) {
|
|
7125
|
+
return withSlowLogging("mkdirSync", () => {
|
|
7126
|
+
if (!fs.existsSync(dirPath)) {
|
|
7127
|
+
fs.mkdirSync(dirPath, { recursive: true, mode: 448 });
|
|
7128
|
+
}
|
|
7129
|
+
});
|
|
7130
|
+
},
|
|
7131
|
+
readdirSync(dirPath) {
|
|
7132
|
+
return withSlowLogging("readdirSync", () => fs.readdirSync(dirPath, { withFileTypes: true }));
|
|
7133
|
+
},
|
|
7134
|
+
readdirStringSync(dirPath) {
|
|
7135
|
+
return withSlowLogging("readdirStringSync", () => fs.readdirSync(dirPath));
|
|
7136
|
+
},
|
|
7137
|
+
isDirEmptySync(dirPath) {
|
|
7138
|
+
return withSlowLogging("isDirEmptySync", () => {
|
|
7139
|
+
const files = this.readdirSync(dirPath);
|
|
7140
|
+
return files.length === 0;
|
|
7141
|
+
});
|
|
7142
|
+
},
|
|
7143
|
+
rmdirSync(dirPath) {
|
|
7144
|
+
return withSlowLogging("rmdirSync", () => fs.rmdirSync(dirPath));
|
|
7145
|
+
},
|
|
7146
|
+
rmSync(path, options) {
|
|
7147
|
+
return withSlowLogging("rmSync", () => fs.rmSync(path, options));
|
|
7148
|
+
},
|
|
7149
|
+
createWriteStream(path) {
|
|
7150
|
+
return fs.createWriteStream(path);
|
|
7316
7151
|
}
|
|
7152
|
+
};
|
|
7153
|
+
var activeFs = NodeFsOperations;
|
|
7154
|
+
function getFsImplementation() {
|
|
7155
|
+
return activeFs;
|
|
7317
7156
|
}
|
|
7318
|
-
MapCache.prototype.clear = _mapCacheClear_default;
|
|
7319
|
-
MapCache.prototype["delete"] = _mapCacheDelete_default;
|
|
7320
|
-
MapCache.prototype.get = _mapCacheGet_default;
|
|
7321
|
-
MapCache.prototype.has = _mapCacheHas_default;
|
|
7322
|
-
MapCache.prototype.set = _mapCacheSet_default;
|
|
7323
|
-
var _MapCache_default = MapCache;
|
|
7324
7157
|
|
|
7325
|
-
// ../
|
|
7326
|
-
var
|
|
7327
|
-
|
|
7328
|
-
|
|
7329
|
-
|
|
7330
|
-
|
|
7331
|
-
|
|
7332
|
-
|
|
7333
|
-
|
|
7334
|
-
|
|
7335
|
-
|
|
7336
|
-
|
|
7337
|
-
|
|
7338
|
-
|
|
7339
|
-
|
|
7340
|
-
|
|
7341
|
-
|
|
7158
|
+
// ../src/entrypoints/agentSdkTypes.ts
|
|
7159
|
+
var HOOK_EVENTS = [
|
|
7160
|
+
"PreToolUse",
|
|
7161
|
+
"PostToolUse",
|
|
7162
|
+
"PostToolUseFailure",
|
|
7163
|
+
"Notification",
|
|
7164
|
+
"UserPromptSubmit",
|
|
7165
|
+
"SessionStart",
|
|
7166
|
+
"SessionEnd",
|
|
7167
|
+
"Stop",
|
|
7168
|
+
"SubagentStart",
|
|
7169
|
+
"SubagentStop",
|
|
7170
|
+
"PreCompact",
|
|
7171
|
+
"PermissionRequest"
|
|
7172
|
+
];
|
|
7173
|
+
var EXIT_REASONS = [
|
|
7174
|
+
"clear",
|
|
7175
|
+
"logout",
|
|
7176
|
+
"prompt_input_exit",
|
|
7177
|
+
"other",
|
|
7178
|
+
"bypass_permissions_disabled"
|
|
7179
|
+
];
|
|
7180
|
+
class AbortError extends Error {
|
|
7342
7181
|
}
|
|
7343
|
-
memoize.Cache = _MapCache_default;
|
|
7344
|
-
var memoize_default = memoize;
|
|
7345
7182
|
|
|
7346
|
-
// ../src/utils/
|
|
7347
|
-
|
|
7348
|
-
|
|
7349
|
-
for (let i = 0;i < data.length; i += CHUNK_SIZE) {
|
|
7350
|
-
process.stderr.write(data.substring(i, i + CHUNK_SIZE));
|
|
7351
|
-
}
|
|
7183
|
+
// ../src/utils/bundledMode.ts
|
|
7184
|
+
function isRunningWithBun() {
|
|
7185
|
+
return process.versions.bun !== undefined;
|
|
7352
7186
|
}
|
|
7353
7187
|
|
|
7354
|
-
// ../src/utils/
|
|
7355
|
-
|
|
7356
|
-
|
|
7357
|
-
|
|
7188
|
+
// ../src/utils/sdkDebug.ts
|
|
7189
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
7190
|
+
import { appendFileSync as appendFileSync2, existsSync as existsSync2, mkdirSync as mkdirSync2 } from "fs";
|
|
7191
|
+
import { join as join3 } from "path";
|
|
7192
|
+
var debugFilePath = null;
|
|
7193
|
+
var initialized = false;
|
|
7194
|
+
function getOrCreateDebugFile() {
|
|
7195
|
+
if (initialized) {
|
|
7196
|
+
return debugFilePath;
|
|
7358
7197
|
}
|
|
7359
|
-
|
|
7360
|
-
if (
|
|
7198
|
+
initialized = true;
|
|
7199
|
+
if (!process.env.DEBUG_CLAUDE_AGENT_SDK) {
|
|
7361
7200
|
return null;
|
|
7362
7201
|
}
|
|
7363
|
-
const
|
|
7364
|
-
|
|
7365
|
-
if (
|
|
7366
|
-
|
|
7202
|
+
const debugDir = join3(getClaudeConfigHomeDir(), "debug");
|
|
7203
|
+
debugFilePath = join3(debugDir, `sdk-${randomUUID2()}.txt`);
|
|
7204
|
+
if (!existsSync2(debugDir)) {
|
|
7205
|
+
mkdirSync2(debugDir, { recursive: true });
|
|
7367
7206
|
}
|
|
7368
|
-
|
|
7369
|
-
|
|
7370
|
-
|
|
7371
|
-
|
|
7372
|
-
|
|
7373
|
-
|
|
7374
|
-
|
|
7375
|
-
|
|
7376
|
-
const categories = [];
|
|
7377
|
-
const mcpMatch = message.match(/^MCP server ["']([^"']+)["']/);
|
|
7378
|
-
if (mcpMatch && mcpMatch[1]) {
|
|
7379
|
-
categories.push("mcp");
|
|
7380
|
-
categories.push(mcpMatch[1].toLowerCase());
|
|
7381
|
-
} else {
|
|
7382
|
-
const prefixMatch = message.match(/^([^:[]+):/);
|
|
7383
|
-
if (prefixMatch && prefixMatch[1]) {
|
|
7384
|
-
categories.push(prefixMatch[1].trim().toLowerCase());
|
|
7385
|
-
}
|
|
7207
|
+
process.stderr.write(`SDK debug logs: ${debugFilePath}
|
|
7208
|
+
`);
|
|
7209
|
+
return debugFilePath;
|
|
7210
|
+
}
|
|
7211
|
+
function logForSdkDebugging(message) {
|
|
7212
|
+
const path = getOrCreateDebugFile();
|
|
7213
|
+
if (!path) {
|
|
7214
|
+
return;
|
|
7386
7215
|
}
|
|
7387
|
-
const
|
|
7388
|
-
|
|
7389
|
-
|
|
7216
|
+
const timestamp = new Date().toISOString();
|
|
7217
|
+
const output = `${timestamp} ${message}
|
|
7218
|
+
`;
|
|
7219
|
+
appendFileSync2(path, output);
|
|
7220
|
+
}
|
|
7221
|
+
|
|
7222
|
+
// ../src/transport/sandboxUtils.ts
|
|
7223
|
+
function mergeSandboxIntoExtraArgs(extraArgs, sandbox) {
|
|
7224
|
+
const effectiveExtraArgs = { ...extraArgs };
|
|
7225
|
+
if (sandbox) {
|
|
7226
|
+
let settingsObj = { sandbox };
|
|
7227
|
+
if (effectiveExtraArgs.settings) {
|
|
7228
|
+
try {
|
|
7229
|
+
const existingSettings = JSON.parse(effectiveExtraArgs.settings);
|
|
7230
|
+
settingsObj = { ...existingSettings, sandbox };
|
|
7231
|
+
} catch {}
|
|
7232
|
+
}
|
|
7233
|
+
effectiveExtraArgs.settings = JSON.stringify(settingsObj);
|
|
7390
7234
|
}
|
|
7391
|
-
|
|
7392
|
-
|
|
7235
|
+
return effectiveExtraArgs;
|
|
7236
|
+
}
|
|
7237
|
+
|
|
7238
|
+
// ../src/transport/ProcessTransport.ts
|
|
7239
|
+
class ProcessTransport {
|
|
7240
|
+
options;
|
|
7241
|
+
child;
|
|
7242
|
+
childStdin;
|
|
7243
|
+
childStdout;
|
|
7244
|
+
ready = false;
|
|
7245
|
+
abortController;
|
|
7246
|
+
exitError;
|
|
7247
|
+
exitListeners = [];
|
|
7248
|
+
processExitHandler;
|
|
7249
|
+
abortHandler;
|
|
7250
|
+
constructor(options) {
|
|
7251
|
+
this.options = options;
|
|
7252
|
+
this.abortController = options.abortController || createAbortController();
|
|
7253
|
+
this.initialize();
|
|
7393
7254
|
}
|
|
7394
|
-
|
|
7395
|
-
|
|
7396
|
-
|
|
7397
|
-
|
|
7398
|
-
|
|
7255
|
+
initialize() {
|
|
7256
|
+
try {
|
|
7257
|
+
const {
|
|
7258
|
+
additionalDirectories = [],
|
|
7259
|
+
betas,
|
|
7260
|
+
cwd: cwd2,
|
|
7261
|
+
executable = isRunningWithBun() ? "bun" : "node",
|
|
7262
|
+
executableArgs = [],
|
|
7263
|
+
extraArgs = {},
|
|
7264
|
+
pathToClaudeCodeExecutable,
|
|
7265
|
+
env = { ...process.env },
|
|
7266
|
+
stderr,
|
|
7267
|
+
maxThinkingTokens,
|
|
7268
|
+
maxTurns,
|
|
7269
|
+
maxBudgetUsd,
|
|
7270
|
+
model,
|
|
7271
|
+
fallbackModel,
|
|
7272
|
+
jsonSchema,
|
|
7273
|
+
permissionMode,
|
|
7274
|
+
allowDangerouslySkipPermissions,
|
|
7275
|
+
permissionPromptToolName,
|
|
7276
|
+
continueConversation,
|
|
7277
|
+
resume,
|
|
7278
|
+
settingSources,
|
|
7279
|
+
allowedTools = [],
|
|
7280
|
+
disallowedTools = [],
|
|
7281
|
+
mcpServers,
|
|
7282
|
+
strictMcpConfig,
|
|
7283
|
+
canUseTool,
|
|
7284
|
+
includePartialMessages,
|
|
7285
|
+
plugins,
|
|
7286
|
+
sandbox
|
|
7287
|
+
} = this.options;
|
|
7288
|
+
const args = [
|
|
7289
|
+
"--output-format",
|
|
7290
|
+
"stream-json",
|
|
7291
|
+
"--verbose",
|
|
7292
|
+
"--input-format",
|
|
7293
|
+
"stream-json"
|
|
7294
|
+
];
|
|
7295
|
+
if (maxThinkingTokens !== undefined) {
|
|
7296
|
+
args.push("--max-thinking-tokens", maxThinkingTokens.toString());
|
|
7297
|
+
}
|
|
7298
|
+
if (maxTurns)
|
|
7299
|
+
args.push("--max-turns", maxTurns.toString());
|
|
7300
|
+
if (maxBudgetUsd !== undefined) {
|
|
7301
|
+
args.push("--max-budget-usd", maxBudgetUsd.toString());
|
|
7302
|
+
}
|
|
7303
|
+
if (model)
|
|
7304
|
+
args.push("--model", model);
|
|
7305
|
+
if (betas && betas.length > 0) {
|
|
7306
|
+
args.push("--betas", betas.join(","));
|
|
7307
|
+
}
|
|
7308
|
+
if (jsonSchema) {
|
|
7309
|
+
args.push("--json-schema", JSON.stringify(jsonSchema));
|
|
7310
|
+
}
|
|
7311
|
+
if (env.DEBUG_CLAUDE_AGENT_SDK) {
|
|
7312
|
+
args.push("--debug-to-stderr");
|
|
7313
|
+
}
|
|
7314
|
+
if (canUseTool) {
|
|
7315
|
+
if (permissionPromptToolName) {
|
|
7316
|
+
throw new Error("canUseTool callback cannot be used with permissionPromptToolName. Please use one or the other.");
|
|
7317
|
+
}
|
|
7318
|
+
args.push("--permission-prompt-tool", "stdio");
|
|
7319
|
+
} else if (permissionPromptToolName) {
|
|
7320
|
+
args.push("--permission-prompt-tool", permissionPromptToolName);
|
|
7321
|
+
}
|
|
7322
|
+
if (continueConversation)
|
|
7323
|
+
args.push("--continue");
|
|
7324
|
+
if (resume)
|
|
7325
|
+
args.push("--resume", resume);
|
|
7326
|
+
if (allowedTools.length > 0) {
|
|
7327
|
+
args.push("--allowedTools", allowedTools.join(","));
|
|
7328
|
+
}
|
|
7329
|
+
if (disallowedTools.length > 0) {
|
|
7330
|
+
args.push("--disallowedTools", disallowedTools.join(","));
|
|
7331
|
+
}
|
|
7332
|
+
const { tools } = this.options;
|
|
7333
|
+
if (tools !== undefined) {
|
|
7334
|
+
if (Array.isArray(tools)) {
|
|
7335
|
+
if (tools.length === 0) {
|
|
7336
|
+
args.push("--tools", "");
|
|
7337
|
+
} else {
|
|
7338
|
+
args.push("--tools", tools.join(","));
|
|
7339
|
+
}
|
|
7340
|
+
} else {
|
|
7341
|
+
args.push("--tools", "default");
|
|
7342
|
+
}
|
|
7343
|
+
}
|
|
7344
|
+
if (mcpServers && Object.keys(mcpServers).length > 0) {
|
|
7345
|
+
args.push("--mcp-config", JSON.stringify({ mcpServers }));
|
|
7346
|
+
}
|
|
7347
|
+
if (settingSources) {
|
|
7348
|
+
args.push("--setting-sources", settingSources.join(","));
|
|
7349
|
+
}
|
|
7350
|
+
if (strictMcpConfig) {
|
|
7351
|
+
args.push("--strict-mcp-config");
|
|
7352
|
+
}
|
|
7353
|
+
if (permissionMode) {
|
|
7354
|
+
args.push("--permission-mode", permissionMode);
|
|
7355
|
+
}
|
|
7356
|
+
if (allowDangerouslySkipPermissions) {
|
|
7357
|
+
args.push("--allow-dangerously-skip-permissions");
|
|
7358
|
+
}
|
|
7359
|
+
if (fallbackModel) {
|
|
7360
|
+
if (model && fallbackModel === model) {
|
|
7361
|
+
throw new Error("Fallback model cannot be the same as the main model. Please specify a different model for fallbackModel option.");
|
|
7362
|
+
}
|
|
7363
|
+
args.push("--fallback-model", fallbackModel);
|
|
7364
|
+
}
|
|
7365
|
+
if (includePartialMessages) {
|
|
7366
|
+
args.push("--include-partial-messages");
|
|
7367
|
+
}
|
|
7368
|
+
for (const dir of additionalDirectories) {
|
|
7369
|
+
args.push("--add-dir", dir);
|
|
7370
|
+
}
|
|
7371
|
+
if (plugins && plugins.length > 0) {
|
|
7372
|
+
for (const plugin of plugins) {
|
|
7373
|
+
if (plugin.type === "local") {
|
|
7374
|
+
args.push("--plugin-dir", plugin.path);
|
|
7375
|
+
} else {
|
|
7376
|
+
throw new Error(`Unsupported plugin type: ${plugin.type}`);
|
|
7377
|
+
}
|
|
7378
|
+
}
|
|
7379
|
+
}
|
|
7380
|
+
if (this.options.forkSession) {
|
|
7381
|
+
args.push("--fork-session");
|
|
7382
|
+
}
|
|
7383
|
+
if (this.options.resumeSessionAt) {
|
|
7384
|
+
args.push("--resume-session-at", this.options.resumeSessionAt);
|
|
7385
|
+
}
|
|
7386
|
+
if (this.options.persistSession === false) {
|
|
7387
|
+
args.push("--no-session-persistence");
|
|
7388
|
+
}
|
|
7389
|
+
const effectiveExtraArgs = mergeSandboxIntoExtraArgs(extraArgs ?? {}, sandbox);
|
|
7390
|
+
for (const [flag, value] of Object.entries(effectiveExtraArgs)) {
|
|
7391
|
+
if (value === null) {
|
|
7392
|
+
args.push(`--${flag}`);
|
|
7393
|
+
} else {
|
|
7394
|
+
args.push(`--${flag}`, value);
|
|
7395
|
+
}
|
|
7396
|
+
}
|
|
7397
|
+
if (!env.CLAUDE_CODE_ENTRYPOINT) {
|
|
7398
|
+
env.CLAUDE_CODE_ENTRYPOINT = "sdk-ts";
|
|
7399
|
+
}
|
|
7400
|
+
delete env.NODE_OPTIONS;
|
|
7401
|
+
if (env.DEBUG_CLAUDE_AGENT_SDK) {
|
|
7402
|
+
env.DEBUG = "1";
|
|
7403
|
+
} else {
|
|
7404
|
+
delete env.DEBUG;
|
|
7405
|
+
}
|
|
7406
|
+
const fs2 = getFsImplementation();
|
|
7407
|
+
if (!fs2.existsSync(pathToClaudeCodeExecutable)) {
|
|
7408
|
+
const errorMessage = isNativeBinary(pathToClaudeCodeExecutable) ? `Claude Code native binary not found at ${pathToClaudeCodeExecutable}. Please ensure Claude Code is installed via native installer or specify a valid path with options.pathToClaudeCodeExecutable.` : `Claude Code executable not found at ${pathToClaudeCodeExecutable}. Is options.pathToClaudeCodeExecutable set?`;
|
|
7409
|
+
throw new ReferenceError(errorMessage);
|
|
7410
|
+
}
|
|
7411
|
+
const isNative = isNativeBinary(pathToClaudeCodeExecutable);
|
|
7412
|
+
const spawnCommand = isNative ? pathToClaudeCodeExecutable : executable;
|
|
7413
|
+
const spawnArgs = isNative ? [...executableArgs, ...args] : [...executableArgs, pathToClaudeCodeExecutable, ...args];
|
|
7414
|
+
const spawnMessage = isNative ? `Spawning Claude Code native binary: ${spawnCommand} ${spawnArgs.join(" ")}` : `Spawning Claude Code process: ${spawnCommand} ${spawnArgs.join(" ")}`;
|
|
7415
|
+
logForSdkDebugging(spawnMessage);
|
|
7416
|
+
if (stderr) {
|
|
7417
|
+
stderr(spawnMessage);
|
|
7418
|
+
}
|
|
7419
|
+
const stderrMode = env.DEBUG_CLAUDE_AGENT_SDK || stderr ? "pipe" : "ignore";
|
|
7420
|
+
this.child = spawn(spawnCommand, spawnArgs, {
|
|
7421
|
+
cwd: cwd2,
|
|
7422
|
+
stdio: ["pipe", "pipe", stderrMode],
|
|
7423
|
+
signal: this.abortController.signal,
|
|
7424
|
+
env
|
|
7425
|
+
});
|
|
7426
|
+
this.childStdin = this.child.stdin;
|
|
7427
|
+
this.childStdout = this.child.stdout;
|
|
7428
|
+
if (env.DEBUG_CLAUDE_AGENT_SDK || stderr) {
|
|
7429
|
+
this.child.stderr.on("data", (data) => {
|
|
7430
|
+
const message = data.toString();
|
|
7431
|
+
logForSdkDebugging(message);
|
|
7432
|
+
if (stderr) {
|
|
7433
|
+
stderr(message);
|
|
7434
|
+
}
|
|
7435
|
+
});
|
|
7436
|
+
}
|
|
7437
|
+
const cleanup = () => {
|
|
7438
|
+
if (this.child && !this.child.killed) {
|
|
7439
|
+
this.child.kill("SIGTERM");
|
|
7440
|
+
}
|
|
7441
|
+
};
|
|
7442
|
+
this.processExitHandler = cleanup;
|
|
7443
|
+
this.abortHandler = cleanup;
|
|
7444
|
+
process.on("exit", this.processExitHandler);
|
|
7445
|
+
this.abortController.signal.addEventListener("abort", this.abortHandler);
|
|
7446
|
+
this.child.on("error", (error) => {
|
|
7447
|
+
this.ready = false;
|
|
7448
|
+
if (this.abortController.signal.aborted) {
|
|
7449
|
+
this.exitError = new AbortError("Claude Code process aborted by user");
|
|
7450
|
+
} else {
|
|
7451
|
+
this.exitError = new Error(`Failed to spawn Claude Code process: ${error.message}`);
|
|
7452
|
+
logForSdkDebugging(this.exitError.message);
|
|
7453
|
+
}
|
|
7454
|
+
});
|
|
7455
|
+
this.child.on("close", (code, signal) => {
|
|
7456
|
+
this.ready = false;
|
|
7457
|
+
if (this.abortController.signal.aborted) {
|
|
7458
|
+
this.exitError = new AbortError("Claude Code process aborted by user");
|
|
7459
|
+
} else {
|
|
7460
|
+
const error = this.getProcessExitError(code, signal);
|
|
7461
|
+
if (error) {
|
|
7462
|
+
this.exitError = error;
|
|
7463
|
+
logForSdkDebugging(error.message);
|
|
7464
|
+
}
|
|
7465
|
+
}
|
|
7466
|
+
});
|
|
7467
|
+
this.ready = true;
|
|
7468
|
+
} catch (error) {
|
|
7469
|
+
this.ready = false;
|
|
7470
|
+
throw error;
|
|
7399
7471
|
}
|
|
7400
7472
|
}
|
|
7401
|
-
|
|
7402
|
-
|
|
7403
|
-
|
|
7404
|
-
|
|
7405
|
-
|
|
7406
|
-
|
|
7407
|
-
|
|
7408
|
-
return false;
|
|
7409
|
-
}
|
|
7410
|
-
if (filter.isExclusive) {
|
|
7411
|
-
return !categories.some((cat) => filter.exclude.includes(cat));
|
|
7412
|
-
} else {
|
|
7413
|
-
return categories.some((cat) => filter.include.includes(cat));
|
|
7414
|
-
}
|
|
7415
|
-
}
|
|
7416
|
-
function shouldShowDebugMessage(message, filter) {
|
|
7417
|
-
if (!filter) {
|
|
7418
|
-
return true;
|
|
7473
|
+
getProcessExitError(code, signal) {
|
|
7474
|
+
if (code !== 0 && code !== null) {
|
|
7475
|
+
return new Error(`Claude Code process exited with code ${code}`);
|
|
7476
|
+
} else if (signal) {
|
|
7477
|
+
return new Error(`Claude Code process terminated by signal ${signal}`);
|
|
7478
|
+
}
|
|
7479
|
+
return;
|
|
7419
7480
|
}
|
|
7420
|
-
|
|
7421
|
-
|
|
7422
|
-
|
|
7423
|
-
|
|
7424
|
-
// ../src/utils/debug.ts
|
|
7425
|
-
import { dirname, join as join3 } from "path";
|
|
7426
|
-
|
|
7427
|
-
// ../src/bootstrap/state.ts
|
|
7428
|
-
import { cwd } from "process";
|
|
7429
|
-
import { realpathSync as realpathSync2 } from "fs";
|
|
7430
|
-
import { randomUUID as randomUUID2 } from "crypto";
|
|
7431
|
-
|
|
7432
|
-
// ../src/bootstrap/envValidators.ts
|
|
7433
|
-
var bashMaxOutputLengthValidator = {
|
|
7434
|
-
name: "BASH_MAX_OUTPUT_LENGTH",
|
|
7435
|
-
default: 30000,
|
|
7436
|
-
validate: (value) => {
|
|
7437
|
-
const MAX_OUTPUT_LENGTH = 150000;
|
|
7438
|
-
const DEFAULT_MAX_OUTPUT_LENGTH = 30000;
|
|
7439
|
-
if (!value) {
|
|
7440
|
-
return {
|
|
7441
|
-
effective: DEFAULT_MAX_OUTPUT_LENGTH,
|
|
7442
|
-
status: "valid"
|
|
7443
|
-
};
|
|
7481
|
+
write(data) {
|
|
7482
|
+
if (this.abortController.signal.aborted) {
|
|
7483
|
+
throw new AbortError("Operation aborted");
|
|
7444
7484
|
}
|
|
7445
|
-
|
|
7446
|
-
|
|
7447
|
-
return {
|
|
7448
|
-
effective: DEFAULT_MAX_OUTPUT_LENGTH,
|
|
7449
|
-
status: "invalid",
|
|
7450
|
-
message: `Invalid value "${value}" (using default: ${DEFAULT_MAX_OUTPUT_LENGTH})`
|
|
7451
|
-
};
|
|
7485
|
+
if (!this.ready || !this.childStdin) {
|
|
7486
|
+
throw new Error("ProcessTransport is not ready for writing");
|
|
7452
7487
|
}
|
|
7453
|
-
if (
|
|
7454
|
-
|
|
7455
|
-
|
|
7456
|
-
|
|
7457
|
-
|
|
7458
|
-
|
|
7488
|
+
if (this.child?.killed || this.child?.exitCode !== null) {
|
|
7489
|
+
throw new Error("Cannot write to terminated process");
|
|
7490
|
+
}
|
|
7491
|
+
if (this.exitError) {
|
|
7492
|
+
throw new Error(`Cannot write to process that exited with error: ${this.exitError.message}`);
|
|
7493
|
+
}
|
|
7494
|
+
logForSdkDebugging(`[ProcessTransport] Writing to stdin: ${data.substring(0, 100)}`);
|
|
7495
|
+
try {
|
|
7496
|
+
const written = this.childStdin.write(data);
|
|
7497
|
+
if (!written) {
|
|
7498
|
+
logForSdkDebugging("[ProcessTransport] Write buffer full, data queued");
|
|
7499
|
+
}
|
|
7500
|
+
} catch (error) {
|
|
7501
|
+
this.ready = false;
|
|
7502
|
+
throw new Error(`Failed to write to process stdin: ${error.message}`);
|
|
7459
7503
|
}
|
|
7460
|
-
return { effective: parsed, status: "valid" };
|
|
7461
7504
|
}
|
|
7462
|
-
|
|
7463
|
-
|
|
7464
|
-
|
|
7465
|
-
|
|
7466
|
-
validate: (value) => {
|
|
7467
|
-
const MAX_OUTPUT_TOKENS = 64000;
|
|
7468
|
-
const DEFAULT_MAX_OUTPUT_TOKENS = 32000;
|
|
7469
|
-
if (!value) {
|
|
7470
|
-
return { effective: DEFAULT_MAX_OUTPUT_TOKENS, status: "valid" };
|
|
7505
|
+
close() {
|
|
7506
|
+
if (this.childStdin) {
|
|
7507
|
+
this.childStdin.end();
|
|
7508
|
+
this.childStdin = undefined;
|
|
7471
7509
|
}
|
|
7472
|
-
|
|
7473
|
-
|
|
7474
|
-
|
|
7475
|
-
effective: DEFAULT_MAX_OUTPUT_TOKENS,
|
|
7476
|
-
status: "invalid",
|
|
7477
|
-
message: `Invalid value "${value}" (using default: ${DEFAULT_MAX_OUTPUT_TOKENS})`
|
|
7478
|
-
};
|
|
7510
|
+
if (this.processExitHandler) {
|
|
7511
|
+
process.off("exit", this.processExitHandler);
|
|
7512
|
+
this.processExitHandler = undefined;
|
|
7479
7513
|
}
|
|
7480
|
-
if (
|
|
7481
|
-
|
|
7482
|
-
|
|
7483
|
-
status: "capped",
|
|
7484
|
-
message: `Capped from ${parsed} to ${MAX_OUTPUT_TOKENS}`
|
|
7485
|
-
};
|
|
7514
|
+
if (this.abortHandler) {
|
|
7515
|
+
this.abortController.signal.removeEventListener("abort", this.abortHandler);
|
|
7516
|
+
this.abortHandler = undefined;
|
|
7486
7517
|
}
|
|
7487
|
-
|
|
7518
|
+
for (const { handler } of this.exitListeners) {
|
|
7519
|
+
this.child?.off("exit", handler);
|
|
7520
|
+
}
|
|
7521
|
+
this.exitListeners = [];
|
|
7522
|
+
if (this.child && !this.child.killed) {
|
|
7523
|
+
this.child.kill("SIGTERM");
|
|
7524
|
+
setTimeout(() => {
|
|
7525
|
+
if (this.child && !this.child.killed) {
|
|
7526
|
+
this.child.kill("SIGKILL");
|
|
7527
|
+
}
|
|
7528
|
+
}, 5000);
|
|
7529
|
+
}
|
|
7530
|
+
this.ready = false;
|
|
7488
7531
|
}
|
|
7489
|
-
|
|
7490
|
-
|
|
7491
|
-
// ../src/bootstrap/state.ts
|
|
7492
|
-
function getInitialState() {
|
|
7493
|
-
let resolvedCwd = "";
|
|
7494
|
-
if (typeof process !== "undefined" && typeof process.cwd === "function") {
|
|
7495
|
-
resolvedCwd = realpathSync2(cwd());
|
|
7532
|
+
isReady() {
|
|
7533
|
+
return this.ready;
|
|
7496
7534
|
}
|
|
7497
|
-
|
|
7498
|
-
|
|
7499
|
-
|
|
7500
|
-
|
|
7501
|
-
|
|
7502
|
-
|
|
7503
|
-
|
|
7504
|
-
|
|
7505
|
-
|
|
7506
|
-
|
|
7507
|
-
|
|
7508
|
-
|
|
7509
|
-
|
|
7510
|
-
|
|
7511
|
-
|
|
7512
|
-
|
|
7513
|
-
|
|
7514
|
-
|
|
7515
|
-
|
|
7516
|
-
|
|
7517
|
-
|
|
7518
|
-
|
|
7519
|
-
allowedSettingSources: [
|
|
7520
|
-
"userSettings",
|
|
7521
|
-
"projectSettings",
|
|
7522
|
-
"localSettings",
|
|
7523
|
-
"flagSettings",
|
|
7524
|
-
"policySettings"
|
|
7525
|
-
],
|
|
7526
|
-
meter: null,
|
|
7527
|
-
sessionCounter: null,
|
|
7528
|
-
locCounter: null,
|
|
7529
|
-
prCounter: null,
|
|
7530
|
-
commitCounter: null,
|
|
7531
|
-
costCounter: null,
|
|
7532
|
-
tokenCounter: null,
|
|
7533
|
-
codeEditToolDecisionCounter: null,
|
|
7534
|
-
activeTimeCounter: null,
|
|
7535
|
-
sessionId: randomUUID2(),
|
|
7536
|
-
loggerProvider: null,
|
|
7537
|
-
eventLogger: null,
|
|
7538
|
-
meterProvider: null,
|
|
7539
|
-
tracerProvider: null,
|
|
7540
|
-
agentColorMap: new Map,
|
|
7541
|
-
agentColorIndex: 0,
|
|
7542
|
-
envVarValidators: [bashMaxOutputLengthValidator, maxOutputTokensValidator],
|
|
7543
|
-
lastAPIRequest: null,
|
|
7544
|
-
inMemoryErrorLog: [],
|
|
7545
|
-
inlinePlugins: [],
|
|
7546
|
-
sessionBypassPermissionsMode: false,
|
|
7547
|
-
hasExitedPlanMode: false,
|
|
7548
|
-
initJsonSchema: null,
|
|
7549
|
-
registeredHooks: null,
|
|
7550
|
-
planSlugCache: new Map
|
|
7551
|
-
};
|
|
7552
|
-
}
|
|
7553
|
-
var STATE = getInitialState();
|
|
7554
|
-
function getSessionId() {
|
|
7555
|
-
return STATE.sessionId;
|
|
7556
|
-
}
|
|
7557
|
-
|
|
7558
|
-
// ../src/utils/bufferedWriter.ts
|
|
7559
|
-
function createBufferedWriter({
|
|
7560
|
-
writeFn,
|
|
7561
|
-
flushIntervalMs = 1000,
|
|
7562
|
-
maxBufferSize = 100,
|
|
7563
|
-
immediateMode = false
|
|
7564
|
-
}) {
|
|
7565
|
-
let buffer = [];
|
|
7566
|
-
let flushTimer = null;
|
|
7567
|
-
function clearTimer() {
|
|
7568
|
-
if (flushTimer) {
|
|
7569
|
-
clearTimeout(flushTimer);
|
|
7570
|
-
flushTimer = null;
|
|
7535
|
+
async* readMessages() {
|
|
7536
|
+
if (!this.childStdout) {
|
|
7537
|
+
throw new Error("ProcessTransport output stream not available");
|
|
7538
|
+
}
|
|
7539
|
+
const rl = createInterface({ input: this.childStdout });
|
|
7540
|
+
try {
|
|
7541
|
+
for await (const line of rl) {
|
|
7542
|
+
if (line.trim()) {
|
|
7543
|
+
const message = JSON.parse(line);
|
|
7544
|
+
yield message;
|
|
7545
|
+
}
|
|
7546
|
+
}
|
|
7547
|
+
await this.waitForExit();
|
|
7548
|
+
} catch (error) {
|
|
7549
|
+
throw error;
|
|
7550
|
+
} finally {
|
|
7551
|
+
rl.close();
|
|
7552
|
+
}
|
|
7553
|
+
}
|
|
7554
|
+
endInput() {
|
|
7555
|
+
if (this.childStdin) {
|
|
7556
|
+
this.childStdin.end();
|
|
7571
7557
|
}
|
|
7572
7558
|
}
|
|
7573
|
-
|
|
7574
|
-
|
|
7559
|
+
getInputStream() {
|
|
7560
|
+
return this.childStdin;
|
|
7561
|
+
}
|
|
7562
|
+
onExit(callback) {
|
|
7563
|
+
if (!this.child)
|
|
7564
|
+
return () => {};
|
|
7565
|
+
const handler = (code, signal) => {
|
|
7566
|
+
const error = this.getProcessExitError(code, signal);
|
|
7567
|
+
callback(error);
|
|
7568
|
+
};
|
|
7569
|
+
this.child.on("exit", handler);
|
|
7570
|
+
this.exitListeners.push({ callback, handler });
|
|
7571
|
+
return () => {
|
|
7572
|
+
if (this.child) {
|
|
7573
|
+
this.child.off("exit", handler);
|
|
7574
|
+
}
|
|
7575
|
+
const index = this.exitListeners.findIndex((l) => l.handler === handler);
|
|
7576
|
+
if (index !== -1) {
|
|
7577
|
+
this.exitListeners.splice(index, 1);
|
|
7578
|
+
}
|
|
7579
|
+
};
|
|
7580
|
+
}
|
|
7581
|
+
async waitForExit() {
|
|
7582
|
+
if (!this.child) {
|
|
7583
|
+
if (this.exitError) {
|
|
7584
|
+
throw this.exitError;
|
|
7585
|
+
}
|
|
7575
7586
|
return;
|
|
7576
|
-
writeFn(buffer.join(""));
|
|
7577
|
-
buffer = [];
|
|
7578
|
-
clearTimer();
|
|
7579
|
-
}
|
|
7580
|
-
function scheduleFlush() {
|
|
7581
|
-
if (!flushTimer) {
|
|
7582
|
-
flushTimer = setTimeout(flush, flushIntervalMs);
|
|
7583
7587
|
}
|
|
7584
|
-
|
|
7585
|
-
|
|
7586
|
-
|
|
7587
|
-
if (immediateMode) {
|
|
7588
|
-
writeFn(content);
|
|
7589
|
-
return;
|
|
7590
|
-
}
|
|
7591
|
-
buffer.push(content);
|
|
7592
|
-
scheduleFlush();
|
|
7593
|
-
if (buffer.length >= maxBufferSize) {
|
|
7594
|
-
flush();
|
|
7588
|
+
if (this.child.exitCode !== null || this.child.killed) {
|
|
7589
|
+
if (this.exitError) {
|
|
7590
|
+
throw this.exitError;
|
|
7595
7591
|
}
|
|
7596
|
-
|
|
7597
|
-
flush,
|
|
7598
|
-
dispose() {
|
|
7599
|
-
flush();
|
|
7592
|
+
return;
|
|
7600
7593
|
}
|
|
7601
|
-
|
|
7594
|
+
return new Promise((resolve, reject) => {
|
|
7595
|
+
const exitHandler = (code, signal) => {
|
|
7596
|
+
if (this.abortController.signal.aborted) {
|
|
7597
|
+
reject(new AbortError("Operation aborted"));
|
|
7598
|
+
return;
|
|
7599
|
+
}
|
|
7600
|
+
const error = this.getProcessExitError(code, signal);
|
|
7601
|
+
if (error) {
|
|
7602
|
+
reject(error);
|
|
7603
|
+
} else {
|
|
7604
|
+
resolve();
|
|
7605
|
+
}
|
|
7606
|
+
};
|
|
7607
|
+
this.child.once("exit", exitHandler);
|
|
7608
|
+
const errorHandler = (error) => {
|
|
7609
|
+
this.child.off("exit", exitHandler);
|
|
7610
|
+
reject(error);
|
|
7611
|
+
};
|
|
7612
|
+
this.child.once("error", errorHandler);
|
|
7613
|
+
this.child.once("exit", () => {
|
|
7614
|
+
this.child.off("error", errorHandler);
|
|
7615
|
+
});
|
|
7616
|
+
});
|
|
7617
|
+
}
|
|
7602
7618
|
}
|
|
7603
|
-
|
|
7604
|
-
|
|
7605
|
-
|
|
7606
|
-
function registerCleanup(cleanupFn) {
|
|
7607
|
-
cleanupFunctions.add(cleanupFn);
|
|
7608
|
-
return () => cleanupFunctions.delete(cleanupFn);
|
|
7619
|
+
function isNativeBinary(executablePath) {
|
|
7620
|
+
const jsExtensions = [".js", ".mjs", ".tsx", ".ts", ".jsx"];
|
|
7621
|
+
return !jsExtensions.some((ext) => executablePath.endsWith(ext));
|
|
7609
7622
|
}
|
|
7610
7623
|
|
|
7611
|
-
// ../src/utils/
|
|
7612
|
-
|
|
7613
|
-
|
|
7614
|
-
|
|
7615
|
-
|
|
7616
|
-
|
|
7617
|
-
|
|
7618
|
-
|
|
7624
|
+
// ../src/utils/stream.ts
|
|
7625
|
+
class Stream {
|
|
7626
|
+
returned;
|
|
7627
|
+
queue = [];
|
|
7628
|
+
readResolve;
|
|
7629
|
+
readReject;
|
|
7630
|
+
isDone = false;
|
|
7631
|
+
hasError;
|
|
7632
|
+
started = false;
|
|
7633
|
+
constructor(returned) {
|
|
7634
|
+
this.returned = returned;
|
|
7619
7635
|
}
|
|
7620
|
-
|
|
7621
|
-
|
|
7622
|
-
|
|
7623
|
-
|
|
7624
|
-
|
|
7625
|
-
|
|
7626
|
-
function shouldLogDebugMessage(message) {
|
|
7627
|
-
if (false) {}
|
|
7628
|
-
if (typeof process === "undefined" || typeof process.versions === "undefined" || typeof process.versions.node === "undefined") {
|
|
7629
|
-
return false;
|
|
7636
|
+
[Symbol.asyncIterator]() {
|
|
7637
|
+
if (this.started) {
|
|
7638
|
+
throw new Error("Stream can only be iterated once");
|
|
7639
|
+
}
|
|
7640
|
+
this.started = true;
|
|
7641
|
+
return this;
|
|
7630
7642
|
}
|
|
7631
|
-
|
|
7632
|
-
|
|
7633
|
-
|
|
7634
|
-
|
|
7635
|
-
|
|
7636
|
-
|
|
7637
|
-
|
|
7638
|
-
|
|
7639
|
-
|
|
7640
|
-
|
|
7641
|
-
|
|
7642
|
-
|
|
7643
|
-
|
|
7644
|
-
|
|
7645
|
-
|
|
7646
|
-
|
|
7647
|
-
flushIntervalMs: 1000,
|
|
7648
|
-
maxBufferSize: 100,
|
|
7649
|
-
immediateMode: isDebugMode()
|
|
7643
|
+
next() {
|
|
7644
|
+
if (this.queue.length > 0) {
|
|
7645
|
+
return Promise.resolve({
|
|
7646
|
+
done: false,
|
|
7647
|
+
value: this.queue.shift()
|
|
7648
|
+
});
|
|
7649
|
+
}
|
|
7650
|
+
if (this.isDone) {
|
|
7651
|
+
return Promise.resolve({ done: true, value: undefined });
|
|
7652
|
+
}
|
|
7653
|
+
if (this.hasError) {
|
|
7654
|
+
return Promise.reject(this.hasError);
|
|
7655
|
+
}
|
|
7656
|
+
return new Promise((resolve, reject) => {
|
|
7657
|
+
this.readResolve = resolve;
|
|
7658
|
+
this.readReject = reject;
|
|
7650
7659
|
});
|
|
7651
|
-
registerCleanup(async () => debugWriter?.dispose());
|
|
7652
7660
|
}
|
|
7653
|
-
|
|
7654
|
-
|
|
7655
|
-
|
|
7656
|
-
|
|
7657
|
-
|
|
7658
|
-
|
|
7659
|
-
|
|
7661
|
+
enqueue(value) {
|
|
7662
|
+
if (this.readResolve) {
|
|
7663
|
+
const resolve = this.readResolve;
|
|
7664
|
+
this.readResolve = undefined;
|
|
7665
|
+
this.readReject = undefined;
|
|
7666
|
+
resolve({ done: false, value });
|
|
7667
|
+
} else {
|
|
7668
|
+
this.queue.push(value);
|
|
7669
|
+
}
|
|
7660
7670
|
}
|
|
7661
|
-
|
|
7662
|
-
|
|
7663
|
-
|
|
7671
|
+
done() {
|
|
7672
|
+
this.isDone = true;
|
|
7673
|
+
if (this.readResolve) {
|
|
7674
|
+
const resolve = this.readResolve;
|
|
7675
|
+
this.readResolve = undefined;
|
|
7676
|
+
this.readReject = undefined;
|
|
7677
|
+
resolve({ done: true, value: undefined });
|
|
7678
|
+
}
|
|
7664
7679
|
}
|
|
7665
|
-
|
|
7666
|
-
|
|
7667
|
-
|
|
7668
|
-
|
|
7669
|
-
|
|
7670
|
-
|
|
7680
|
+
error(error) {
|
|
7681
|
+
this.hasError = error;
|
|
7682
|
+
if (this.readReject) {
|
|
7683
|
+
const reject = this.readReject;
|
|
7684
|
+
this.readResolve = undefined;
|
|
7685
|
+
this.readReject = undefined;
|
|
7686
|
+
reject(error);
|
|
7687
|
+
}
|
|
7688
|
+
}
|
|
7689
|
+
return() {
|
|
7690
|
+
this.isDone = true;
|
|
7691
|
+
if (this.returned) {
|
|
7692
|
+
this.returned();
|
|
7693
|
+
}
|
|
7694
|
+
return Promise.resolve({ done: true, value: undefined });
|
|
7671
7695
|
}
|
|
7672
|
-
getDebugWriter().write(output);
|
|
7673
|
-
}
|
|
7674
|
-
function getDebugLogPath() {
|
|
7675
|
-
return process.env.CLAUDE_CODE_DEBUG_LOGS_DIR ?? join3(getClaudeConfigHomeDir(), "debug", `${getSessionId()}.txt`);
|
|
7676
7696
|
}
|
|
7677
|
-
|
|
7678
|
-
|
|
7679
|
-
|
|
7680
|
-
|
|
7681
|
-
|
|
7682
|
-
|
|
7683
|
-
|
|
7697
|
+
|
|
7698
|
+
// ../src/services/mcp/SdkControlTransport.ts
|
|
7699
|
+
class SdkControlServerTransport {
|
|
7700
|
+
sendMcpMessage;
|
|
7701
|
+
isClosed = false;
|
|
7702
|
+
constructor(sendMcpMessage) {
|
|
7703
|
+
this.sendMcpMessage = sendMcpMessage;
|
|
7704
|
+
}
|
|
7705
|
+
onclose;
|
|
7706
|
+
onerror;
|
|
7707
|
+
onmessage;
|
|
7708
|
+
async start() {}
|
|
7709
|
+
async send(message) {
|
|
7710
|
+
if (this.isClosed) {
|
|
7711
|
+
throw new Error("Transport is closed");
|
|
7684
7712
|
}
|
|
7685
|
-
|
|
7686
|
-
|
|
7687
|
-
|
|
7688
|
-
|
|
7713
|
+
this.sendMcpMessage(message);
|
|
7714
|
+
}
|
|
7715
|
+
async close() {
|
|
7716
|
+
if (this.isClosed) {
|
|
7717
|
+
return;
|
|
7689
7718
|
}
|
|
7690
|
-
|
|
7691
|
-
|
|
7692
|
-
}
|
|
7719
|
+
this.isClosed = true;
|
|
7720
|
+
this.onclose?.();
|
|
7721
|
+
}
|
|
7722
|
+
}
|
|
7693
7723
|
|
|
7694
7724
|
// ../src/core/Query.ts
|
|
7695
7725
|
import { randomUUID as randomUUID3 } from "crypto";
|
|
7696
7726
|
|
|
7697
7727
|
class Query {
|
|
7698
7728
|
transport;
|
|
7699
|
-
isSingleUserTurn;
|
|
7700
7729
|
canUseTool;
|
|
7701
7730
|
hooks;
|
|
7702
7731
|
abortController;
|
|
@@ -7712,18 +7741,23 @@ class Query {
|
|
|
7712
7741
|
nextCallbackId = 0;
|
|
7713
7742
|
sdkMcpTransports = new Map;
|
|
7714
7743
|
pendingMcpResponses = new Map;
|
|
7715
|
-
|
|
7716
|
-
|
|
7744
|
+
lastActivityTime = Date.now();
|
|
7745
|
+
userInputEndedResolve;
|
|
7717
7746
|
streamCloseTimeout;
|
|
7718
|
-
|
|
7747
|
+
resetLastActivityTime() {
|
|
7748
|
+
this.lastActivityTime = Date.now();
|
|
7749
|
+
}
|
|
7750
|
+
hasBidirectionalNeeds() {
|
|
7751
|
+
return this.sdkMcpTransports.size > 0 || this.hooks !== undefined && Object.keys(this.hooks).length > 0 || this.canUseTool !== undefined;
|
|
7752
|
+
}
|
|
7753
|
+
constructor(transport, _isSingleUserTurn, canUseTool, hooks, abortController, sdkMcpServers = new Map, jsonSchema, initConfig) {
|
|
7719
7754
|
this.transport = transport;
|
|
7720
|
-
this.isSingleUserTurn = isSingleUserTurn;
|
|
7721
7755
|
this.canUseTool = canUseTool;
|
|
7722
7756
|
this.hooks = hooks;
|
|
7723
7757
|
this.abortController = abortController;
|
|
7724
7758
|
this.jsonSchema = jsonSchema;
|
|
7725
7759
|
this.initConfig = initConfig;
|
|
7726
|
-
this.streamCloseTimeout =
|
|
7760
|
+
this.streamCloseTimeout = 5000;
|
|
7727
7761
|
if (typeof process !== "undefined" && process.env?.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT) {
|
|
7728
7762
|
this.streamCloseTimeout = parseInt(process.env.CLAUDE_CODE_STREAM_CLOSE_TIMEOUT);
|
|
7729
7763
|
}
|
|
@@ -7733,9 +7767,6 @@ class Query {
|
|
|
7733
7767
|
server.connect(sdkTransport);
|
|
7734
7768
|
}
|
|
7735
7769
|
this.sdkMessages = this.readSdkMessages();
|
|
7736
|
-
this.firstResultReceivedPromise = new Promise((resolve) => {
|
|
7737
|
-
this.firstResultReceivedResolve = resolve;
|
|
7738
|
-
});
|
|
7739
7770
|
this.readMessages();
|
|
7740
7771
|
this.initialization = this.initialize();
|
|
7741
7772
|
this.initialization.catch(() => {});
|
|
@@ -7751,6 +7782,14 @@ class Query {
|
|
|
7751
7782
|
this.transport.close();
|
|
7752
7783
|
this.pendingControlResponses.clear();
|
|
7753
7784
|
this.pendingMcpResponses.clear();
|
|
7785
|
+
this.cancelControllers.clear();
|
|
7786
|
+
this.hookCallbacks.clear();
|
|
7787
|
+
for (const transport of this.sdkMcpTransports.values()) {
|
|
7788
|
+
try {
|
|
7789
|
+
transport.close();
|
|
7790
|
+
} catch {}
|
|
7791
|
+
}
|
|
7792
|
+
this.sdkMcpTransports.clear();
|
|
7754
7793
|
if (error) {
|
|
7755
7794
|
this.inputStream.error(error);
|
|
7756
7795
|
} else {
|
|
@@ -7776,6 +7815,7 @@ class Query {
|
|
|
7776
7815
|
async readMessages() {
|
|
7777
7816
|
try {
|
|
7778
7817
|
for await (const message of this.transport.readMessages()) {
|
|
7818
|
+
this.resetLastActivityTime();
|
|
7779
7819
|
if (message.type === "control_response") {
|
|
7780
7820
|
const handler = this.pendingControlResponses.get(message.response.request_id);
|
|
7781
7821
|
if (handler) {
|
|
@@ -7791,19 +7831,17 @@ class Query {
|
|
|
7791
7831
|
} else if (message.type === "keep_alive") {
|
|
7792
7832
|
continue;
|
|
7793
7833
|
}
|
|
7794
|
-
if (message.type === "result") {
|
|
7795
|
-
if (this.firstResultReceivedResolve) {
|
|
7796
|
-
this.firstResultReceivedResolve();
|
|
7797
|
-
}
|
|
7798
|
-
if (this.isSingleUserTurn) {
|
|
7799
|
-
this.transport.endInput();
|
|
7800
|
-
}
|
|
7801
|
-
}
|
|
7802
7834
|
this.inputStream.enqueue(message);
|
|
7803
7835
|
}
|
|
7836
|
+
if (this.userInputEndedResolve) {
|
|
7837
|
+
this.userInputEndedResolve();
|
|
7838
|
+
}
|
|
7804
7839
|
this.inputStream.done();
|
|
7805
7840
|
this.cleanup();
|
|
7806
7841
|
} catch (error) {
|
|
7842
|
+
if (this.userInputEndedResolve) {
|
|
7843
|
+
this.userInputEndedResolve();
|
|
7844
|
+
}
|
|
7807
7845
|
this.inputStream.error(error);
|
|
7808
7846
|
this.cleanup(error);
|
|
7809
7847
|
}
|
|
@@ -7946,9 +7984,9 @@ class Query {
|
|
|
7946
7984
|
max_thinking_tokens: maxThinkingTokens
|
|
7947
7985
|
});
|
|
7948
7986
|
}
|
|
7949
|
-
async
|
|
7987
|
+
async rewindFiles(userMessageId) {
|
|
7950
7988
|
await this.request({
|
|
7951
|
-
subtype: "
|
|
7989
|
+
subtype: "rewind_files",
|
|
7952
7990
|
user_message_id: userMessageId
|
|
7953
7991
|
});
|
|
7954
7992
|
}
|
|
@@ -7999,7 +8037,6 @@ class Query {
|
|
|
7999
8037
|
}
|
|
8000
8038
|
async streamInput(stream) {
|
|
8001
8039
|
logForDebugging(`[Query.streamInput] Starting to process input stream`);
|
|
8002
|
-
logForDebugging(`[Query.streamInput] this.sdkMcpTransports.size = ${this.sdkMcpTransports.size}`);
|
|
8003
8040
|
try {
|
|
8004
8041
|
let messageCount = 0;
|
|
8005
8042
|
for await (const message of stream) {
|
|
@@ -8011,28 +8048,9 @@ class Query {
|
|
|
8011
8048
|
`));
|
|
8012
8049
|
}
|
|
8013
8050
|
logForDebugging(`[Query.streamInput] Finished processing ${messageCount} messages from input stream`);
|
|
8014
|
-
|
|
8015
|
-
|
|
8016
|
-
|
|
8017
|
-
logForDebugging(`[Query.streamInput] Entering Promise.race to wait for result`);
|
|
8018
|
-
let timeoutId;
|
|
8019
|
-
await Promise.race([
|
|
8020
|
-
this.firstResultReceivedPromise.then(() => {
|
|
8021
|
-
logForDebugging(`[Query.streamInput] Received first result, closing input stream`);
|
|
8022
|
-
if (timeoutId) {
|
|
8023
|
-
clearTimeout(timeoutId);
|
|
8024
|
-
}
|
|
8025
|
-
}),
|
|
8026
|
-
new Promise((resolve) => {
|
|
8027
|
-
timeoutId = setTimeout(() => {
|
|
8028
|
-
logForDebugging(`[Query.streamInput] Timed out waiting for first result, closing input stream`);
|
|
8029
|
-
resolve();
|
|
8030
|
-
}, this.streamCloseTimeout);
|
|
8031
|
-
})
|
|
8032
|
-
]);
|
|
8033
|
-
if (timeoutId) {
|
|
8034
|
-
clearTimeout(timeoutId);
|
|
8035
|
-
}
|
|
8051
|
+
if (this.hasBidirectionalNeeds()) {
|
|
8052
|
+
logForDebugging(`[Query.streamInput] Has bidirectional needs, waiting for inactivity`);
|
|
8053
|
+
await this.waitForInactivity();
|
|
8036
8054
|
}
|
|
8037
8055
|
logForDebugging(`[Query] Calling transport.endInput() to close stdin to CLI process`);
|
|
8038
8056
|
this.transport.endInput();
|
|
@@ -8042,6 +8060,43 @@ class Query {
|
|
|
8042
8060
|
}
|
|
8043
8061
|
}
|
|
8044
8062
|
}
|
|
8063
|
+
async handleSingleTurnInputComplete() {
|
|
8064
|
+
if (this.hasBidirectionalNeeds()) {
|
|
8065
|
+
logForDebugging(`[Query.handleSingleTurnInputComplete] Has bidirectional needs, waiting for inactivity`);
|
|
8066
|
+
await this.waitForInactivity();
|
|
8067
|
+
}
|
|
8068
|
+
logForDebugging(`[Query.handleSingleTurnInputComplete] Calling transport.endInput()`);
|
|
8069
|
+
this.transport.endInput();
|
|
8070
|
+
}
|
|
8071
|
+
async waitForInactivity() {
|
|
8072
|
+
logForDebugging(`[Query.waitForInactivity] Waiting for inactivity (timeout: ${this.streamCloseTimeout}ms)`);
|
|
8073
|
+
return new Promise((resolve) => {
|
|
8074
|
+
this.userInputEndedResolve = resolve;
|
|
8075
|
+
if (this.abortController?.signal.aborted) {
|
|
8076
|
+
resolve();
|
|
8077
|
+
return;
|
|
8078
|
+
}
|
|
8079
|
+
this.abortController?.signal.addEventListener("abort", () => resolve(), {
|
|
8080
|
+
once: true
|
|
8081
|
+
});
|
|
8082
|
+
const checkInactivity = () => {
|
|
8083
|
+
if (this.abortController?.signal.aborted) {
|
|
8084
|
+
resolve();
|
|
8085
|
+
return;
|
|
8086
|
+
}
|
|
8087
|
+
const elapsed = Date.now() - this.lastActivityTime;
|
|
8088
|
+
if (elapsed >= this.streamCloseTimeout) {
|
|
8089
|
+
logForDebugging(`[Query.waitForInactivity] Inactivity timeout reached (${elapsed}ms elapsed). ` + `Closing stdin. If your tools or hooks need more time, set CLAUDE_CODE_STREAM_CLOSE_TIMEOUT ` + `to a higher value (current: ${this.streamCloseTimeout}ms).`);
|
|
8090
|
+
resolve();
|
|
8091
|
+
} else {
|
|
8092
|
+
const remaining = this.streamCloseTimeout - elapsed;
|
|
8093
|
+
logForDebugging(`[Query.waitForInactivity] Still active, checking again in ${remaining}ms`);
|
|
8094
|
+
setTimeout(checkInactivity, remaining);
|
|
8095
|
+
}
|
|
8096
|
+
};
|
|
8097
|
+
checkInactivity();
|
|
8098
|
+
});
|
|
8099
|
+
}
|
|
8045
8100
|
handleHookCallbacks(callbackId, input, toolUseID, abortSignal) {
|
|
8046
8101
|
const callback = this.hookCallbacks.get(callbackId);
|
|
8047
8102
|
if (!callback) {
|
|
@@ -15104,7 +15159,7 @@ function query({
|
|
|
15104
15159
|
const dirname2 = join5(filename, "..");
|
|
15105
15160
|
pathToClaudeCodeExecutable = join5(dirname2, "cli.js");
|
|
15106
15161
|
}
|
|
15107
|
-
process.env.CLAUDE_AGENT_SDK_VERSION = "0.1.
|
|
15162
|
+
process.env.CLAUDE_AGENT_SDK_VERSION = "0.1.63";
|
|
15108
15163
|
const {
|
|
15109
15164
|
abortController = createAbortController(),
|
|
15110
15165
|
additionalDirectories = [],
|
|
@@ -15121,9 +15176,11 @@ function query({
|
|
|
15121
15176
|
executableArgs = [],
|
|
15122
15177
|
extraArgs = {},
|
|
15123
15178
|
fallbackModel,
|
|
15179
|
+
enableFileCheckpointing,
|
|
15124
15180
|
forkSession,
|
|
15125
15181
|
hooks,
|
|
15126
15182
|
includePartialMessages,
|
|
15183
|
+
persistSession,
|
|
15127
15184
|
maxThinkingTokens,
|
|
15128
15185
|
maxTurns,
|
|
15129
15186
|
maxBudgetUsd,
|
|
@@ -15147,6 +15204,9 @@ function query({
|
|
|
15147
15204
|
if (!processEnv.CLAUDE_CODE_ENTRYPOINT) {
|
|
15148
15205
|
processEnv.CLAUDE_CODE_ENTRYPOINT = "sdk-ts";
|
|
15149
15206
|
}
|
|
15207
|
+
if (enableFileCheckpointing) {
|
|
15208
|
+
processEnv.CLAUDE_CODE_ENABLE_SDK_FILE_CHECKPOINTING = "true";
|
|
15209
|
+
}
|
|
15150
15210
|
if (!pathToClaudeCodeExecutable) {
|
|
15151
15211
|
throw new Error("pathToClaudeCodeExecutable is required");
|
|
15152
15212
|
}
|
|
@@ -15199,6 +15259,7 @@ function query({
|
|
|
15199
15259
|
canUseTool: !!canUseTool,
|
|
15200
15260
|
hooks: !!hooks,
|
|
15201
15261
|
includePartialMessages,
|
|
15262
|
+
persistSession,
|
|
15202
15263
|
plugins,
|
|
15203
15264
|
sandbox
|
|
15204
15265
|
});
|
|
@@ -15219,6 +15280,7 @@ function query({
|
|
|
15219
15280
|
parent_tool_use_id: null
|
|
15220
15281
|
}) + `
|
|
15221
15282
|
`);
|
|
15283
|
+
queryInstance.handleSingleTurnInputComplete();
|
|
15222
15284
|
} else {
|
|
15223
15285
|
queryInstance.streamInput(prompt);
|
|
15224
15286
|
}
|