claude-code-controller 0.3.0 → 0.4.0
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/README.md +285 -302
- package/dist/api/index.cjs +506 -53
- package/dist/api/index.cjs.map +1 -1
- package/dist/api/index.d.cts +36 -8
- package/dist/api/index.d.ts +36 -8
- package/dist/api/index.js +506 -53
- package/dist/api/index.js.map +1 -1
- package/dist/{controller-CqCBbQYK.d.cts → claude-DectLQVR.d.cts} +237 -1
- package/dist/{controller-CqCBbQYK.d.ts → claude-DectLQVR.d.ts} +237 -1
- package/dist/index.cjs +463 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +460 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
// src/claude.ts
|
|
2
|
+
import { EventEmitter as EventEmitter2 } from "events";
|
|
3
|
+
import { randomUUID as randomUUID3 } from "crypto";
|
|
4
|
+
|
|
1
5
|
// src/controller.ts
|
|
2
6
|
import { EventEmitter } from "events";
|
|
3
7
|
import { execSync as execSync2 } from "child_process";
|
|
@@ -698,6 +702,11 @@ var silentLogger = {
|
|
|
698
702
|
};
|
|
699
703
|
|
|
700
704
|
// src/controller.ts
|
|
705
|
+
var PROTOCOL_ONLY_TYPES = /* @__PURE__ */ new Set([
|
|
706
|
+
"shutdown_approved",
|
|
707
|
+
"plan_approval_response",
|
|
708
|
+
"permission_response"
|
|
709
|
+
]);
|
|
701
710
|
var AGENT_COLORS = [
|
|
702
711
|
"#00FF00",
|
|
703
712
|
"#00BFFF",
|
|
@@ -891,11 +900,7 @@ var ClaudeCodeController = class extends EventEmitter {
|
|
|
891
900
|
const unread = await readUnread(this.teamName, "controller");
|
|
892
901
|
const fromAgent = unread.filter((m) => m.from === agentName);
|
|
893
902
|
if (fromAgent.length > 0) {
|
|
894
|
-
const PROTOCOL_TYPES =
|
|
895
|
-
"shutdown_approved",
|
|
896
|
-
"plan_approval_response",
|
|
897
|
-
"permission_response"
|
|
898
|
-
]);
|
|
903
|
+
const PROTOCOL_TYPES = PROTOCOL_ONLY_TYPES;
|
|
899
904
|
const meaningful = fromAgent.filter((m) => {
|
|
900
905
|
const parsed = parseMessage(m);
|
|
901
906
|
return parsed.type !== "idle_notification" && !PROTOCOL_TYPES.has(parsed.type);
|
|
@@ -928,7 +933,7 @@ var ClaudeCodeController = class extends EventEmitter {
|
|
|
928
933
|
const unread = await readUnread(this.teamName, "controller");
|
|
929
934
|
const meaningful = unread.filter((m) => {
|
|
930
935
|
const parsed = parseMessage(m);
|
|
931
|
-
return parsed.type !== "idle_notification";
|
|
936
|
+
return parsed.type !== "idle_notification" && !PROTOCOL_ONLY_TYPES.has(parsed.type);
|
|
932
937
|
});
|
|
933
938
|
if (meaningful.length > 0) {
|
|
934
939
|
return meaningful[0];
|
|
@@ -1089,13 +1094,462 @@ var ClaudeCodeController = class extends EventEmitter {
|
|
|
1089
1094
|
function sleep2(ms) {
|
|
1090
1095
|
return new Promise((r) => setTimeout(r, ms));
|
|
1091
1096
|
}
|
|
1097
|
+
|
|
1098
|
+
// src/claude.ts
|
|
1099
|
+
Symbol.asyncDispose ??= /* @__PURE__ */ Symbol("Symbol.asyncDispose");
|
|
1100
|
+
function buildEnv(opts) {
|
|
1101
|
+
const env = { ...opts.env };
|
|
1102
|
+
if (opts.apiKey) env.ANTHROPIC_AUTH_TOKEN = opts.apiKey;
|
|
1103
|
+
if (opts.baseUrl) env.ANTHROPIC_BASE_URL = opts.baseUrl;
|
|
1104
|
+
if (opts.timeout != null) env.API_TIMEOUT_MS = String(opts.timeout);
|
|
1105
|
+
return env;
|
|
1106
|
+
}
|
|
1107
|
+
function resolvePermissions(preset) {
|
|
1108
|
+
switch (preset) {
|
|
1109
|
+
case "edit":
|
|
1110
|
+
return { permissionMode: "acceptEdits" };
|
|
1111
|
+
case "plan":
|
|
1112
|
+
return { permissionMode: "plan" };
|
|
1113
|
+
case "ask":
|
|
1114
|
+
return { permissionMode: "default" };
|
|
1115
|
+
case "full":
|
|
1116
|
+
default:
|
|
1117
|
+
return { permissionMode: void 0 };
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
function waitForReady(controller, agentName, timeoutMs = 15e3) {
|
|
1121
|
+
return new Promise((resolve, reject) => {
|
|
1122
|
+
let settled = false;
|
|
1123
|
+
const timer = setTimeout(() => {
|
|
1124
|
+
if (settled) return;
|
|
1125
|
+
cleanup();
|
|
1126
|
+
reject(
|
|
1127
|
+
new Error(
|
|
1128
|
+
`Agent "${agentName}" did not become ready within ${timeoutMs}ms`
|
|
1129
|
+
)
|
|
1130
|
+
);
|
|
1131
|
+
}, timeoutMs);
|
|
1132
|
+
const onReady = (name) => {
|
|
1133
|
+
if (name === agentName && !settled) {
|
|
1134
|
+
settled = true;
|
|
1135
|
+
cleanup();
|
|
1136
|
+
resolve();
|
|
1137
|
+
}
|
|
1138
|
+
};
|
|
1139
|
+
const onExit = (name, code) => {
|
|
1140
|
+
if (name === agentName && !settled) {
|
|
1141
|
+
settled = true;
|
|
1142
|
+
cleanup();
|
|
1143
|
+
reject(
|
|
1144
|
+
new Error(
|
|
1145
|
+
`Agent "${agentName}" exited before becoming ready (code=${code})`
|
|
1146
|
+
)
|
|
1147
|
+
);
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
const onSpawned = (name) => {
|
|
1151
|
+
if (name === agentName && !settled) {
|
|
1152
|
+
settled = true;
|
|
1153
|
+
cleanup();
|
|
1154
|
+
resolve();
|
|
1155
|
+
}
|
|
1156
|
+
};
|
|
1157
|
+
const cleanup = () => {
|
|
1158
|
+
clearTimeout(timer);
|
|
1159
|
+
controller.removeListener("idle", onReady);
|
|
1160
|
+
controller.removeListener("message", onReady);
|
|
1161
|
+
controller.removeListener("agent:spawned", onSpawned);
|
|
1162
|
+
controller.removeListener("agent:exited", onExit);
|
|
1163
|
+
};
|
|
1164
|
+
controller.on("idle", onReady);
|
|
1165
|
+
controller.on("message", onReady);
|
|
1166
|
+
controller.on("agent:spawned", onSpawned);
|
|
1167
|
+
controller.on("agent:exited", onExit);
|
|
1168
|
+
});
|
|
1169
|
+
}
|
|
1170
|
+
var Agent = class _Agent extends EventEmitter2 {
|
|
1171
|
+
controller;
|
|
1172
|
+
handle;
|
|
1173
|
+
ownsController;
|
|
1174
|
+
disposed = false;
|
|
1175
|
+
boundListeners = [];
|
|
1176
|
+
constructor(controller, handle, ownsController, behavior) {
|
|
1177
|
+
super();
|
|
1178
|
+
this.controller = controller;
|
|
1179
|
+
this.handle = handle;
|
|
1180
|
+
this.ownsController = ownsController;
|
|
1181
|
+
this.wireEvents();
|
|
1182
|
+
this.wireBehavior(behavior);
|
|
1183
|
+
}
|
|
1184
|
+
/** Create a standalone agent (owns its own controller). */
|
|
1185
|
+
static async create(opts = {}) {
|
|
1186
|
+
const name = opts.name ?? `agent-${randomUUID3().slice(0, 8)}`;
|
|
1187
|
+
const env = buildEnv(opts);
|
|
1188
|
+
const { permissionMode } = resolvePermissions(opts.permissions);
|
|
1189
|
+
const controller = new ClaudeCodeController({
|
|
1190
|
+
teamName: `claude-${randomUUID3().slice(0, 8)}`,
|
|
1191
|
+
cwd: opts.cwd,
|
|
1192
|
+
claudeBinary: opts.claudeBinary,
|
|
1193
|
+
env,
|
|
1194
|
+
logLevel: opts.logLevel ?? "warn",
|
|
1195
|
+
logger: opts.logger
|
|
1196
|
+
});
|
|
1197
|
+
await controller.init();
|
|
1198
|
+
const ready = waitForReady(controller, name, opts.readyTimeout);
|
|
1199
|
+
try {
|
|
1200
|
+
const handle = await controller.spawnAgent({
|
|
1201
|
+
name,
|
|
1202
|
+
type: opts.type ?? "general-purpose",
|
|
1203
|
+
model: opts.model,
|
|
1204
|
+
cwd: opts.cwd,
|
|
1205
|
+
permissionMode
|
|
1206
|
+
});
|
|
1207
|
+
const agent = new _Agent(controller, handle, true, {
|
|
1208
|
+
autoApprove: opts.autoApprove,
|
|
1209
|
+
onPermission: opts.onPermission,
|
|
1210
|
+
onPlan: opts.onPlan
|
|
1211
|
+
});
|
|
1212
|
+
await ready;
|
|
1213
|
+
return agent;
|
|
1214
|
+
} catch (err) {
|
|
1215
|
+
await controller.shutdown().catch(() => {
|
|
1216
|
+
});
|
|
1217
|
+
throw err;
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
/** Create an agent within an existing session (session owns the controller). */
|
|
1221
|
+
static async createInSession(controller, name, opts = {}) {
|
|
1222
|
+
const { permissionMode } = resolvePermissions(opts.permissions);
|
|
1223
|
+
const ready = waitForReady(controller, name, opts.readyTimeout);
|
|
1224
|
+
const handle = await controller.spawnAgent({
|
|
1225
|
+
name,
|
|
1226
|
+
type: opts.type ?? "general-purpose",
|
|
1227
|
+
model: opts.model,
|
|
1228
|
+
cwd: opts.cwd,
|
|
1229
|
+
permissionMode,
|
|
1230
|
+
env: opts.env
|
|
1231
|
+
});
|
|
1232
|
+
const agent = new _Agent(controller, handle, false, {
|
|
1233
|
+
autoApprove: opts.autoApprove,
|
|
1234
|
+
onPermission: opts.onPermission,
|
|
1235
|
+
onPlan: opts.onPlan
|
|
1236
|
+
});
|
|
1237
|
+
await ready;
|
|
1238
|
+
return agent;
|
|
1239
|
+
}
|
|
1240
|
+
/** The agent's name. */
|
|
1241
|
+
get name() {
|
|
1242
|
+
return this.handle.name;
|
|
1243
|
+
}
|
|
1244
|
+
/** The agent process PID. */
|
|
1245
|
+
get pid() {
|
|
1246
|
+
return this.handle.pid;
|
|
1247
|
+
}
|
|
1248
|
+
/** Whether the agent process is still running. */
|
|
1249
|
+
get isRunning() {
|
|
1250
|
+
return this.handle.isRunning;
|
|
1251
|
+
}
|
|
1252
|
+
/**
|
|
1253
|
+
* Send a message and wait for the response.
|
|
1254
|
+
*
|
|
1255
|
+
* Uses event-based waiting (via the controller's InboxPoller) instead of
|
|
1256
|
+
* polling `readUnread()` directly, which avoids a race condition where the
|
|
1257
|
+
* poller marks inbox messages as read before `receive()` can see them.
|
|
1258
|
+
*/
|
|
1259
|
+
async ask(question, opts) {
|
|
1260
|
+
this.ensureNotDisposed();
|
|
1261
|
+
const timeout = opts?.timeout ?? 12e4;
|
|
1262
|
+
const responsePromise = new Promise((resolve, reject) => {
|
|
1263
|
+
const timer = setTimeout(() => {
|
|
1264
|
+
cleanup();
|
|
1265
|
+
reject(new Error(`Timeout (${timeout}ms) waiting for response`));
|
|
1266
|
+
}, timeout);
|
|
1267
|
+
const onMsg = (text) => {
|
|
1268
|
+
cleanup();
|
|
1269
|
+
resolve(text);
|
|
1270
|
+
};
|
|
1271
|
+
const onExit = (code) => {
|
|
1272
|
+
cleanup();
|
|
1273
|
+
reject(new Error(`Agent exited (code=${code}) before responding`));
|
|
1274
|
+
};
|
|
1275
|
+
const cleanup = () => {
|
|
1276
|
+
clearTimeout(timer);
|
|
1277
|
+
this.removeListener("message", onMsg);
|
|
1278
|
+
this.removeListener("exit", onExit);
|
|
1279
|
+
};
|
|
1280
|
+
this.on("message", onMsg);
|
|
1281
|
+
this.on("exit", onExit);
|
|
1282
|
+
});
|
|
1283
|
+
const wrapped = `${question}
|
|
1284
|
+
|
|
1285
|
+
IMPORTANT: You MUST send your complete answer back using the SendMessage tool. Do NOT just think your answer \u2014 use the SendMessage tool to reply.`;
|
|
1286
|
+
await this.handle.send(wrapped);
|
|
1287
|
+
return responsePromise;
|
|
1288
|
+
}
|
|
1289
|
+
/** Send a message without waiting for a response. */
|
|
1290
|
+
async send(message) {
|
|
1291
|
+
this.ensureNotDisposed();
|
|
1292
|
+
return this.handle.send(message);
|
|
1293
|
+
}
|
|
1294
|
+
/** Wait for the next response from this agent. */
|
|
1295
|
+
async receive(opts) {
|
|
1296
|
+
this.ensureNotDisposed();
|
|
1297
|
+
const timeout = opts?.timeout ?? 12e4;
|
|
1298
|
+
return new Promise((resolve, reject) => {
|
|
1299
|
+
const timer = setTimeout(() => {
|
|
1300
|
+
cleanup();
|
|
1301
|
+
reject(new Error(`Timeout (${timeout}ms) waiting for response`));
|
|
1302
|
+
}, timeout);
|
|
1303
|
+
const onMsg = (text) => {
|
|
1304
|
+
cleanup();
|
|
1305
|
+
resolve(text);
|
|
1306
|
+
};
|
|
1307
|
+
const onExit = (code) => {
|
|
1308
|
+
cleanup();
|
|
1309
|
+
reject(new Error(`Agent exited (code=${code}) before responding`));
|
|
1310
|
+
};
|
|
1311
|
+
const cleanup = () => {
|
|
1312
|
+
clearTimeout(timer);
|
|
1313
|
+
this.removeListener("message", onMsg);
|
|
1314
|
+
this.removeListener("exit", onExit);
|
|
1315
|
+
};
|
|
1316
|
+
this.on("message", onMsg);
|
|
1317
|
+
this.on("exit", onExit);
|
|
1318
|
+
});
|
|
1319
|
+
}
|
|
1320
|
+
/**
|
|
1321
|
+
* Close this agent. If standalone, shuts down the entire controller.
|
|
1322
|
+
* If session-owned, kills only this agent's process.
|
|
1323
|
+
*/
|
|
1324
|
+
async close() {
|
|
1325
|
+
if (this.disposed) return;
|
|
1326
|
+
this.disposed = true;
|
|
1327
|
+
this.unwireEvents();
|
|
1328
|
+
if (this.ownsController) {
|
|
1329
|
+
await this.controller.shutdown();
|
|
1330
|
+
} else {
|
|
1331
|
+
await this.handle.kill();
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
/** Mark as disposed (used by Session when it closes). */
|
|
1335
|
+
markDisposed() {
|
|
1336
|
+
this.disposed = true;
|
|
1337
|
+
this.unwireEvents();
|
|
1338
|
+
}
|
|
1339
|
+
async [Symbol.asyncDispose]() {
|
|
1340
|
+
await this.close();
|
|
1341
|
+
}
|
|
1342
|
+
wireEvents() {
|
|
1343
|
+
const agentName = this.handle.name;
|
|
1344
|
+
const onMessage = (name, msg) => {
|
|
1345
|
+
if (name === agentName) this.emit("message", msg.text);
|
|
1346
|
+
};
|
|
1347
|
+
const onIdle = (name) => {
|
|
1348
|
+
if (name === agentName) this.emit("idle");
|
|
1349
|
+
};
|
|
1350
|
+
const onPermission = (name, parsed) => {
|
|
1351
|
+
if (name !== agentName) return;
|
|
1352
|
+
let handled = false;
|
|
1353
|
+
const guard = (fn) => () => {
|
|
1354
|
+
if (handled) return Promise.resolve();
|
|
1355
|
+
handled = true;
|
|
1356
|
+
return fn();
|
|
1357
|
+
};
|
|
1358
|
+
this.emit("permission", {
|
|
1359
|
+
requestId: parsed.requestId,
|
|
1360
|
+
toolName: parsed.toolName,
|
|
1361
|
+
description: parsed.description,
|
|
1362
|
+
input: parsed.input,
|
|
1363
|
+
approve: guard(
|
|
1364
|
+
() => this.controller.sendPermissionResponse(agentName, parsed.requestId, true)
|
|
1365
|
+
),
|
|
1366
|
+
reject: guard(
|
|
1367
|
+
() => this.controller.sendPermissionResponse(agentName, parsed.requestId, false)
|
|
1368
|
+
)
|
|
1369
|
+
});
|
|
1370
|
+
};
|
|
1371
|
+
const onPlan = (name, parsed) => {
|
|
1372
|
+
if (name !== agentName) return;
|
|
1373
|
+
let handled = false;
|
|
1374
|
+
const guard = (fn) => (...args) => {
|
|
1375
|
+
if (handled) return Promise.resolve();
|
|
1376
|
+
handled = true;
|
|
1377
|
+
return fn(...args);
|
|
1378
|
+
};
|
|
1379
|
+
this.emit("plan", {
|
|
1380
|
+
requestId: parsed.requestId,
|
|
1381
|
+
planContent: parsed.planContent,
|
|
1382
|
+
approve: guard(
|
|
1383
|
+
(feedback) => this.controller.sendPlanApproval(agentName, parsed.requestId, true, feedback)
|
|
1384
|
+
),
|
|
1385
|
+
reject: guard(
|
|
1386
|
+
(feedback) => this.controller.sendPlanApproval(agentName, parsed.requestId, false, feedback)
|
|
1387
|
+
)
|
|
1388
|
+
});
|
|
1389
|
+
};
|
|
1390
|
+
const onExit = (name, code) => {
|
|
1391
|
+
if (name === agentName) this.emit("exit", code);
|
|
1392
|
+
};
|
|
1393
|
+
const onError = (err) => {
|
|
1394
|
+
this.emit("error", err);
|
|
1395
|
+
};
|
|
1396
|
+
this.controller.on("message", onMessage);
|
|
1397
|
+
this.controller.on("idle", onIdle);
|
|
1398
|
+
this.controller.on("permission:request", onPermission);
|
|
1399
|
+
this.controller.on("plan:approval_request", onPlan);
|
|
1400
|
+
this.controller.on("agent:exited", onExit);
|
|
1401
|
+
this.controller.on("error", onError);
|
|
1402
|
+
this.boundListeners = [
|
|
1403
|
+
{ event: "message", fn: onMessage },
|
|
1404
|
+
{ event: "idle", fn: onIdle },
|
|
1405
|
+
{ event: "permission:request", fn: onPermission },
|
|
1406
|
+
{ event: "plan:approval_request", fn: onPlan },
|
|
1407
|
+
{ event: "agent:exited", fn: onExit },
|
|
1408
|
+
{ event: "error", fn: onError }
|
|
1409
|
+
];
|
|
1410
|
+
}
|
|
1411
|
+
unwireEvents() {
|
|
1412
|
+
for (const { event, fn } of this.boundListeners) {
|
|
1413
|
+
this.controller.removeListener(event, fn);
|
|
1414
|
+
}
|
|
1415
|
+
this.boundListeners = [];
|
|
1416
|
+
}
|
|
1417
|
+
wireBehavior(behavior) {
|
|
1418
|
+
if (!behavior) return;
|
|
1419
|
+
const { autoApprove, onPermission, onPlan } = behavior;
|
|
1420
|
+
if (autoApprove != null) {
|
|
1421
|
+
this.on("permission", (req) => {
|
|
1422
|
+
if (autoApprove === true) {
|
|
1423
|
+
req.approve();
|
|
1424
|
+
} else if (Array.isArray(autoApprove)) {
|
|
1425
|
+
autoApprove.includes(req.toolName) ? req.approve() : req.reject();
|
|
1426
|
+
}
|
|
1427
|
+
});
|
|
1428
|
+
if (autoApprove === true) {
|
|
1429
|
+
this.on("plan", (req) => req.approve());
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
if (onPermission) {
|
|
1433
|
+
this.on("permission", onPermission);
|
|
1434
|
+
}
|
|
1435
|
+
if (onPlan) {
|
|
1436
|
+
this.on("plan", onPlan);
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
ensureNotDisposed() {
|
|
1440
|
+
if (this.disposed) {
|
|
1441
|
+
throw new Error("Agent has been closed");
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
};
|
|
1445
|
+
var Session = class _Session {
|
|
1446
|
+
controller;
|
|
1447
|
+
defaults;
|
|
1448
|
+
agents = /* @__PURE__ */ new Map();
|
|
1449
|
+
disposed = false;
|
|
1450
|
+
constructor(controller, defaults) {
|
|
1451
|
+
this.controller = controller;
|
|
1452
|
+
this.defaults = defaults;
|
|
1453
|
+
}
|
|
1454
|
+
static async create(opts = {}) {
|
|
1455
|
+
const env = buildEnv(opts);
|
|
1456
|
+
const controller = new ClaudeCodeController({
|
|
1457
|
+
teamName: opts.teamName ?? `session-${randomUUID3().slice(0, 8)}`,
|
|
1458
|
+
cwd: opts.cwd,
|
|
1459
|
+
claudeBinary: opts.claudeBinary,
|
|
1460
|
+
env,
|
|
1461
|
+
logLevel: opts.logLevel ?? "warn",
|
|
1462
|
+
logger: opts.logger
|
|
1463
|
+
});
|
|
1464
|
+
await controller.init();
|
|
1465
|
+
return new _Session(controller, opts);
|
|
1466
|
+
}
|
|
1467
|
+
/** Spawn a named agent in this session. Inherits session defaults. */
|
|
1468
|
+
async agent(name, opts = {}) {
|
|
1469
|
+
this.ensureNotDisposed();
|
|
1470
|
+
const merged = {
|
|
1471
|
+
model: this.defaults.model,
|
|
1472
|
+
cwd: this.defaults.cwd,
|
|
1473
|
+
permissions: this.defaults.permissions,
|
|
1474
|
+
readyTimeout: this.defaults.readyTimeout,
|
|
1475
|
+
autoApprove: this.defaults.autoApprove,
|
|
1476
|
+
onPermission: this.defaults.onPermission,
|
|
1477
|
+
onPlan: this.defaults.onPlan,
|
|
1478
|
+
...opts
|
|
1479
|
+
};
|
|
1480
|
+
const agent = await Agent.createInSession(
|
|
1481
|
+
this.controller,
|
|
1482
|
+
name,
|
|
1483
|
+
merged
|
|
1484
|
+
);
|
|
1485
|
+
this.agents.set(name, agent);
|
|
1486
|
+
return agent;
|
|
1487
|
+
}
|
|
1488
|
+
/** Get an existing agent by name. */
|
|
1489
|
+
get(name) {
|
|
1490
|
+
return this.agents.get(name);
|
|
1491
|
+
}
|
|
1492
|
+
/** Close all agents and shut down the session. */
|
|
1493
|
+
async close() {
|
|
1494
|
+
if (this.disposed) return;
|
|
1495
|
+
this.disposed = true;
|
|
1496
|
+
for (const agent of this.agents.values()) {
|
|
1497
|
+
agent.markDisposed();
|
|
1498
|
+
}
|
|
1499
|
+
await this.controller.shutdown();
|
|
1500
|
+
}
|
|
1501
|
+
async [Symbol.asyncDispose]() {
|
|
1502
|
+
await this.close();
|
|
1503
|
+
}
|
|
1504
|
+
ensureNotDisposed() {
|
|
1505
|
+
if (this.disposed) {
|
|
1506
|
+
throw new Error("Session has been closed");
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
};
|
|
1510
|
+
async function claudeCall(prompt, opts = {}) {
|
|
1511
|
+
const agent = await Agent.create(opts);
|
|
1512
|
+
try {
|
|
1513
|
+
return await agent.ask(prompt, { timeout: opts.timeout ?? 12e4 });
|
|
1514
|
+
} finally {
|
|
1515
|
+
await agent.close();
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
var claude = Object.assign(claudeCall, {
|
|
1519
|
+
/**
|
|
1520
|
+
* Create a persistent agent for multi-turn conversations.
|
|
1521
|
+
*
|
|
1522
|
+
* @example
|
|
1523
|
+
* ```ts
|
|
1524
|
+
* const agent = await claude.agent({ model: "sonnet" });
|
|
1525
|
+
* const answer = await agent.ask("What is 2+2?");
|
|
1526
|
+
* await agent.close();
|
|
1527
|
+
* ```
|
|
1528
|
+
*/
|
|
1529
|
+
agent: (opts) => Agent.create(opts),
|
|
1530
|
+
/**
|
|
1531
|
+
* Create a multi-agent session.
|
|
1532
|
+
*
|
|
1533
|
+
* @example
|
|
1534
|
+
* ```ts
|
|
1535
|
+
* const session = await claude.session({ model: "sonnet" });
|
|
1536
|
+
* const reviewer = await session.agent("reviewer", { model: "opus" });
|
|
1537
|
+
* const coder = await session.agent("coder");
|
|
1538
|
+
* await session.close();
|
|
1539
|
+
* ```
|
|
1540
|
+
*/
|
|
1541
|
+
session: (opts) => Session.create(opts)
|
|
1542
|
+
});
|
|
1092
1543
|
export {
|
|
1544
|
+
Agent,
|
|
1093
1545
|
AgentHandle,
|
|
1094
1546
|
ClaudeCodeController,
|
|
1095
1547
|
InboxPoller,
|
|
1096
1548
|
ProcessManager,
|
|
1549
|
+
Session,
|
|
1097
1550
|
TaskManager,
|
|
1098
1551
|
TeamManager,
|
|
1552
|
+
claude,
|
|
1099
1553
|
createLogger,
|
|
1100
1554
|
inboxPath,
|
|
1101
1555
|
inboxesDir,
|