@agentchatme/codex 0.0.14 → 0.0.16
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/dist/daemon-main.js +124 -46
- package/dist/daemon-main.js.map +1 -1
- package/dist/index.js +307 -155
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/daemon-main.js
CHANGED
|
@@ -3724,9 +3724,9 @@ var init_wrapper = __esm({
|
|
|
3724
3724
|
|
|
3725
3725
|
// src/daemon-main.ts
|
|
3726
3726
|
import { parseArgs } from "util";
|
|
3727
|
-
import * as
|
|
3727
|
+
import * as path7 from "path";
|
|
3728
3728
|
|
|
3729
|
-
// node_modules/.pnpm/@agentchatme+agent-core@0.0.
|
|
3729
|
+
// node_modules/.pnpm/@agentchatme+agent-core@0.0.12/node_modules/@agentchatme/agent-core/dist/chunk-NEGTEBFK.js
|
|
3730
3730
|
import * as fs from "fs";
|
|
3731
3731
|
import * as path from "path";
|
|
3732
3732
|
import * as fs2 from "fs";
|
|
@@ -7956,11 +7956,19 @@ function beat(home) {
|
|
|
7956
7956
|
} catch {
|
|
7957
7957
|
}
|
|
7958
7958
|
}
|
|
7959
|
+
function idle(home) {
|
|
7960
|
+
try {
|
|
7961
|
+
fs4.rmSync(path4.join(home, HEARTBEAT_FILE), { force: true });
|
|
7962
|
+
} catch {
|
|
7963
|
+
}
|
|
7964
|
+
}
|
|
7959
7965
|
|
|
7960
|
-
// node_modules/.pnpm/@agentchatme+agent-core@0.0.
|
|
7966
|
+
// node_modules/.pnpm/@agentchatme+agent-core@0.0.12/node_modules/@agentchatme/agent-core/dist/daemon-entry.js
|
|
7961
7967
|
init_wrapper();
|
|
7962
7968
|
import { EventEmitter } from "events";
|
|
7963
7969
|
import * as path2 from "path";
|
|
7970
|
+
import * as fs3 from "fs";
|
|
7971
|
+
import * as path5 from "path";
|
|
7964
7972
|
import * as os from "os";
|
|
7965
7973
|
var SyncRowSchema2 = external_exports.object({
|
|
7966
7974
|
id: external_exports.string(),
|
|
@@ -8207,7 +8215,7 @@ function wsUrlFor(apiBase) {
|
|
|
8207
8215
|
return apiBase.replace(/^http/, "ws").replace(/\/+$/, "") + "/v1/ws";
|
|
8208
8216
|
}
|
|
8209
8217
|
async function resolveDaemonConfig(opts) {
|
|
8210
|
-
const home =
|
|
8218
|
+
const home = path5.resolve(opts.home);
|
|
8211
8219
|
const id = resolveIdentity(home);
|
|
8212
8220
|
if (id === null) {
|
|
8213
8221
|
throw new Error(`no AgentChat identity in ${home} \u2014 register this agent first`);
|
|
@@ -8228,7 +8236,7 @@ async function resolveDaemonConfig(opts) {
|
|
|
8228
8236
|
apiBase: id.apiBase,
|
|
8229
8237
|
wsUrl: wsUrlFor(id.apiBase),
|
|
8230
8238
|
home,
|
|
8231
|
-
workdir: opts.workdir ??
|
|
8239
|
+
workdir: opts.workdir ?? path5.join(home, "daemon-workdir")
|
|
8232
8240
|
};
|
|
8233
8241
|
}
|
|
8234
8242
|
var MAX_CONCURRENT_TURNS = 3;
|
|
@@ -8237,9 +8245,10 @@ var HEARTBEAT_MS = 3e4;
|
|
|
8237
8245
|
var YIELD_MS = Number(process.env["AGENTCHATD_YIELD_MS"] ?? 1e4);
|
|
8238
8246
|
var delay = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
8239
8247
|
var Daemon = class {
|
|
8240
|
-
constructor(cfg, adapter, ws) {
|
|
8248
|
+
constructor(cfg, adapter, ws, onTerminal) {
|
|
8241
8249
|
this.cfg = cfg;
|
|
8242
8250
|
this.adapter = adapter;
|
|
8251
|
+
this.onTerminal = onTerminal;
|
|
8243
8252
|
this.coord = new ReplyCoord({
|
|
8244
8253
|
apiKey: cfg.apiKey,
|
|
8245
8254
|
apiBase: cfg.apiBase,
|
|
@@ -8251,11 +8260,12 @@ var Daemon = class {
|
|
|
8251
8260
|
this.ws.on("terminal", (reason) => {
|
|
8252
8261
|
log.error(`daemon terminal: ${reason}`);
|
|
8253
8262
|
this.stop();
|
|
8254
|
-
|
|
8263
|
+
this.onTerminal?.(reason);
|
|
8255
8264
|
});
|
|
8256
8265
|
}
|
|
8257
8266
|
cfg;
|
|
8258
8267
|
adapter;
|
|
8268
|
+
onTerminal;
|
|
8259
8269
|
ws;
|
|
8260
8270
|
coord;
|
|
8261
8271
|
seen = /* @__PURE__ */ new Map();
|
|
@@ -8350,7 +8360,7 @@ var Daemon = class {
|
|
|
8350
8360
|
this.inFlight++;
|
|
8351
8361
|
return Promise.resolve();
|
|
8352
8362
|
}
|
|
8353
|
-
return new Promise((
|
|
8363
|
+
return new Promise((resolve32) => this.waiters.push(resolve32));
|
|
8354
8364
|
}
|
|
8355
8365
|
releaseSlot() {
|
|
8356
8366
|
const next = this.waiters.shift();
|
|
@@ -8358,47 +8368,110 @@ var Daemon = class {
|
|
|
8358
8368
|
else this.inFlight--;
|
|
8359
8369
|
}
|
|
8360
8370
|
};
|
|
8371
|
+
var POLL_MS = 5e3;
|
|
8372
|
+
var TICK_MS = 250;
|
|
8373
|
+
var MAX_BACKOFF_MS2 = 5 * 6e4;
|
|
8374
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
8375
|
+
function fingerprint(home) {
|
|
8376
|
+
const id = resolveIdentity(home);
|
|
8377
|
+
return id === null ? null : `${id.apiKey}:${id.handle ?? ""}`;
|
|
8378
|
+
}
|
|
8361
8379
|
async function runDaemon(opts) {
|
|
8362
|
-
|
|
8363
|
-
|
|
8364
|
-
|
|
8365
|
-
|
|
8366
|
-
...opts.workdir !== void 0 ? { workdir: opts.workdir } : {}
|
|
8367
|
-
});
|
|
8368
|
-
} catch (err) {
|
|
8369
|
-
console.error(String(err instanceof Error ? err.message : err));
|
|
8370
|
-
return 1;
|
|
8371
|
-
}
|
|
8372
|
-
const lock = acquireLeaderLock(cfg.home);
|
|
8380
|
+
const home = path2.resolve(opts.home);
|
|
8381
|
+
const workdir = opts.workdir ?? path2.join(home, "daemon-workdir");
|
|
8382
|
+
if (process.env["AGENTCHAT_LOG_LEVEL"] === void 0) process.env["AGENTCHAT_LOG_LEVEL"] = "info";
|
|
8383
|
+
const lock = acquireLeaderLock(home);
|
|
8373
8384
|
if (lock === null) return 1;
|
|
8374
|
-
|
|
8375
|
-
let
|
|
8385
|
+
let live = null;
|
|
8386
|
+
let liveFingerprint = null;
|
|
8387
|
+
let refused = null;
|
|
8388
|
+
let failures = 0;
|
|
8389
|
+
let lastFailure = null;
|
|
8390
|
+
let shuttingDown = false;
|
|
8391
|
+
const disconnect = (why) => {
|
|
8392
|
+
if (live === null) return;
|
|
8393
|
+
log.info(`${why} \u2014 disconnecting, staying resident`);
|
|
8394
|
+
live.stop();
|
|
8395
|
+
live = null;
|
|
8396
|
+
liveFingerprint = null;
|
|
8397
|
+
idle(home);
|
|
8398
|
+
};
|
|
8376
8399
|
const shutdown = (sig) => {
|
|
8377
|
-
if (
|
|
8378
|
-
|
|
8400
|
+
if (shuttingDown) return;
|
|
8401
|
+
shuttingDown = true;
|
|
8379
8402
|
log.info(`${sig} \u2014 shutting down`);
|
|
8380
|
-
|
|
8403
|
+
live?.stop();
|
|
8404
|
+
idle(home);
|
|
8381
8405
|
lock.release();
|
|
8382
8406
|
process.exit(0);
|
|
8383
8407
|
};
|
|
8384
8408
|
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
8385
8409
|
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
|
8410
|
+
let nudged = false;
|
|
8386
8411
|
try {
|
|
8387
|
-
|
|
8388
|
-
|
|
8389
|
-
|
|
8390
|
-
|
|
8391
|
-
|
|
8412
|
+
fs3.mkdirSync(home, { recursive: true });
|
|
8413
|
+
const watcher = fs3.watch(home, (_event, filename) => {
|
|
8414
|
+
if (filename === null || String(filename).startsWith("credentials")) nudged = true;
|
|
8415
|
+
});
|
|
8416
|
+
watcher.unref();
|
|
8417
|
+
} catch {
|
|
8392
8418
|
}
|
|
8393
|
-
|
|
8394
|
-
|
|
8419
|
+
log.info(`always-on resident for ${home} (${credentialsPath(home)})`);
|
|
8420
|
+
idle(home);
|
|
8421
|
+
for (; ; ) {
|
|
8422
|
+
if (shuttingDown) break;
|
|
8423
|
+
const fp = fingerprint(home);
|
|
8424
|
+
if (fp === null) {
|
|
8425
|
+
disconnect("signed out");
|
|
8426
|
+
if (refused !== null) refused = null;
|
|
8427
|
+
} else if (fp !== liveFingerprint) {
|
|
8428
|
+
disconnect("identity changed");
|
|
8429
|
+
failures = 0;
|
|
8430
|
+
if (fp === refused) {
|
|
8431
|
+
} else {
|
|
8432
|
+
try {
|
|
8433
|
+
const cfg = await resolveDaemonConfig({ home, workdir });
|
|
8434
|
+
const candidate = new Daemon(cfg, opts.adapter, void 0, (reason) => {
|
|
8435
|
+
log.warn(`credential refused (${reason}) \u2014 idling until it changes`);
|
|
8436
|
+
refused = fp;
|
|
8437
|
+
live = null;
|
|
8438
|
+
liveFingerprint = null;
|
|
8439
|
+
idle(home);
|
|
8440
|
+
});
|
|
8441
|
+
await candidate.start();
|
|
8442
|
+
live = candidate;
|
|
8443
|
+
liveFingerprint = fp;
|
|
8444
|
+
failures = 0;
|
|
8445
|
+
lastFailure = null;
|
|
8446
|
+
} catch (err) {
|
|
8447
|
+
const msg = String(err instanceof Error ? err.message : err);
|
|
8448
|
+
if (msg !== lastFailure) {
|
|
8449
|
+
log.warn(`not connecting yet: ${msg}`);
|
|
8450
|
+
lastFailure = msg;
|
|
8451
|
+
}
|
|
8452
|
+
failures += 1;
|
|
8453
|
+
live = null;
|
|
8454
|
+
liveFingerprint = null;
|
|
8455
|
+
idle(home);
|
|
8456
|
+
}
|
|
8457
|
+
}
|
|
8458
|
+
}
|
|
8459
|
+
const waitMs = failures === 0 ? POLL_MS : Math.min(POLL_MS * 2 ** Math.min(failures, 6), MAX_BACKOFF_MS2);
|
|
8460
|
+
nudged = false;
|
|
8461
|
+
const deadline = Date.now() + waitMs;
|
|
8462
|
+
while (!nudged && !shuttingDown && Date.now() < deadline) {
|
|
8463
|
+
await sleep(TICK_MS);
|
|
8464
|
+
}
|
|
8465
|
+
}
|
|
8466
|
+
lock.release();
|
|
8467
|
+
return 0;
|
|
8395
8468
|
}
|
|
8396
8469
|
|
|
8397
8470
|
// src/adapter.ts
|
|
8398
|
-
import * as
|
|
8471
|
+
import * as fs5 from "fs";
|
|
8399
8472
|
import { spawn, spawnSync } from "child_process";
|
|
8400
8473
|
|
|
8401
|
-
// node_modules/.pnpm/@agentchatme+agent-core@0.0.
|
|
8474
|
+
// node_modules/.pnpm/@agentchatme+agent-core@0.0.12/node_modules/@agentchatme/agent-core/dist/index.js
|
|
8402
8475
|
var SESSION_TTL_MS = 48 * 60 * 60 * 1e3;
|
|
8403
8476
|
var SessionStateSchema = external_exports.object({
|
|
8404
8477
|
continuations: external_exports.number().int().min(0),
|
|
@@ -8416,7 +8489,12 @@ var StateSchema = external_exports.object({
|
|
|
8416
8489
|
// Machine-wide timestamp of the last registration offer injected by the
|
|
8417
8490
|
// session-start hook. Keeps the unregistered-plugin nag to once a day
|
|
8418
8491
|
// instead of once per session.
|
|
8419
|
-
last_offer_at: external_exports.string().optional()
|
|
8492
|
+
last_offer_at: external_exports.string().optional(),
|
|
8493
|
+
// Set when the user has said "not now" to setting up a handle. Unlike the
|
|
8494
|
+
// cooldown above this is PERMANENT until they change their mind, because the
|
|
8495
|
+
// prompt that needs suppressing lives in the always-loaded instruction file
|
|
8496
|
+
// and would otherwise be re-read — and re-acted on — every single session.
|
|
8497
|
+
offer_declined_at: external_exports.string().optional()
|
|
8420
8498
|
});
|
|
8421
8499
|
var OFFER_COOLDOWN_MS = 24 * 60 * 60 * 1e3;
|
|
8422
8500
|
var SEC = 1e3;
|
|
@@ -8466,7 +8544,7 @@ var CodexAdapter = class {
|
|
|
8466
8544
|
if ((status.stdout + status.stderr).toLowerCase().includes("not logged in")) {
|
|
8467
8545
|
return { ok: false, detail: "codex is not logged in (run `codex login` on this machine)" };
|
|
8468
8546
|
}
|
|
8469
|
-
|
|
8547
|
+
fs5.mkdirSync(this.workdir, { recursive: true });
|
|
8470
8548
|
return { ok: true };
|
|
8471
8549
|
}
|
|
8472
8550
|
async runTurn(ctx) {
|
|
@@ -8474,7 +8552,7 @@ var CodexAdapter = class {
|
|
|
8474
8552
|
const prompt = buildPrompt(ctx);
|
|
8475
8553
|
const common = ["--json", "--dangerously-bypass-hook-trust", "--skip-git-repo-check"];
|
|
8476
8554
|
const args = prior ? ["exec", "resume", prior, ...common, prompt] : ["exec", ...common, "-C", this.workdir, prompt];
|
|
8477
|
-
return new Promise((
|
|
8555
|
+
return new Promise((resolve5) => {
|
|
8478
8556
|
const child = spawn("codex", args, {
|
|
8479
8557
|
// stdin MUST be closed or codex exec hangs forever waiting on EOF.
|
|
8480
8558
|
stdio: ["ignore", "pipe", "pipe"],
|
|
@@ -8502,20 +8580,20 @@ var CodexAdapter = class {
|
|
|
8502
8580
|
child.kill("SIGKILL");
|
|
8503
8581
|
} catch {
|
|
8504
8582
|
}
|
|
8505
|
-
|
|
8583
|
+
resolve5({ ok: false, detail: "turn timed out" });
|
|
8506
8584
|
}, TURN_TIMEOUT_MS);
|
|
8507
8585
|
child.on("error", (err) => {
|
|
8508
8586
|
clearTimeout(killTimer);
|
|
8509
|
-
|
|
8587
|
+
resolve5({ ok: false, fatal: true, detail: `codex spawn failed: ${String(err)}` });
|
|
8510
8588
|
});
|
|
8511
8589
|
child.on("close", (code) => {
|
|
8512
8590
|
clearTimeout(killTimer);
|
|
8513
8591
|
if (threadId && !prior) this.threads.set(ctx.conversationId, threadId);
|
|
8514
8592
|
if (code === 0) {
|
|
8515
8593
|
log.info(`codex turn done for ${ctx.conversationId} (sent=${sawSend})`);
|
|
8516
|
-
|
|
8594
|
+
resolve5({ ok: true, detail: sawSend ? "replied" : "silent" });
|
|
8517
8595
|
} else {
|
|
8518
|
-
|
|
8596
|
+
resolve5({ ok: false, detail: `codex exited ${code}` });
|
|
8519
8597
|
}
|
|
8520
8598
|
});
|
|
8521
8599
|
});
|
|
@@ -8547,14 +8625,14 @@ function buildPrompt(ctx) {
|
|
|
8547
8625
|
|
|
8548
8626
|
// src/host.ts
|
|
8549
8627
|
import * as os2 from "os";
|
|
8550
|
-
import * as
|
|
8628
|
+
import * as path6 from "path";
|
|
8551
8629
|
function codexHome() {
|
|
8552
8630
|
const override = process.env["CODEX_HOME"];
|
|
8553
|
-
if (override !== void 0 && override.trim().length > 0) return
|
|
8554
|
-
return
|
|
8631
|
+
if (override !== void 0 && override.trim().length > 0) return path6.resolve(override);
|
|
8632
|
+
return path6.join(os2.homedir(), ".codex");
|
|
8555
8633
|
}
|
|
8556
8634
|
function identityHome() {
|
|
8557
|
-
return
|
|
8635
|
+
return path6.join(codexHome(), "agentchat");
|
|
8558
8636
|
}
|
|
8559
8637
|
|
|
8560
8638
|
// src/daemon-main.ts
|
|
@@ -8572,8 +8650,8 @@ async function main(argv = process.argv.slice(2)) {
|
|
|
8572
8650
|
console.error("Usage: agentchat-daemon [--home <dir>] [--workdir <dir>]");
|
|
8573
8651
|
return 1;
|
|
8574
8652
|
}
|
|
8575
|
-
const home = values.home !== void 0 ?
|
|
8576
|
-
const workdir = values.workdir !== void 0 ?
|
|
8653
|
+
const home = values.home !== void 0 ? path7.resolve(values.home) : identityHome();
|
|
8654
|
+
const workdir = values.workdir !== void 0 ? path7.resolve(values.workdir) : path7.join(home, "daemon-workdir");
|
|
8577
8655
|
return await runDaemon({
|
|
8578
8656
|
home,
|
|
8579
8657
|
workdir,
|