@agentchatme/agent-core 0.7.3 → 0.8.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 +73 -3
- package/dist/{chunk-FPAOQGMV.js → chunk-6GMYBMID.js} +869 -197
- package/dist/chunk-6GMYBMID.js.map +1 -0
- package/dist/{chunk-5KMQ23O6.js → chunk-LPTOKVEE.js} +17 -2
- package/dist/chunk-LPTOKVEE.js.map +1 -0
- package/dist/{chunk-7TTV2CQX.js → chunk-WYBLSTYB.js} +3 -3
- package/dist/{chunk-7TTV2CQX.js.map → chunk-WYBLSTYB.js.map} +1 -1
- package/dist/daemon-entry.d.ts +28 -2
- package/dist/daemon-entry.js +121 -39
- package/dist/daemon-entry.js.map +1 -1
- package/dist/index.d.ts +297 -10
- package/dist/index.js +451 -124
- package/dist/index.js.map +1 -1
- package/dist/{served-manual-CwxBzpwQ.d.ts → listener-report-V3oMkCj2.d.ts} +622 -397
- package/dist/presets.js +3 -3
- package/dist/presets.js.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-5KMQ23O6.js.map +0 -1
- package/dist/chunk-FPAOQGMV.js.map +0 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
INTEGRATION_ENV,
|
|
2
3
|
__export,
|
|
3
4
|
atomicWriteFile,
|
|
4
5
|
clientIdentityOf,
|
|
@@ -6,7 +7,7 @@ import {
|
|
|
6
7
|
log,
|
|
7
8
|
readJsonFile,
|
|
8
9
|
workdirExposesHome
|
|
9
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-LPTOKVEE.js";
|
|
10
11
|
|
|
11
12
|
// src/integration/texts.ts
|
|
12
13
|
import * as path from "path";
|
|
@@ -54,6 +55,7 @@ async function fetchIdentityLine(options) {
|
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
// src/identity/credentials.ts
|
|
58
|
+
import * as crypto from "crypto";
|
|
57
59
|
import * as fs from "fs";
|
|
58
60
|
import * as path2 from "path";
|
|
59
61
|
|
|
@@ -535,8 +537,8 @@ function getErrorMap() {
|
|
|
535
537
|
|
|
536
538
|
// node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/helpers/parseUtil.js
|
|
537
539
|
var makeIssue = (params) => {
|
|
538
|
-
const { data, path:
|
|
539
|
-
const fullPath = [...
|
|
540
|
+
const { data, path: path12, errorMaps, issueData } = params;
|
|
541
|
+
const fullPath = [...path12, ...issueData.path || []];
|
|
540
542
|
const fullIssue = {
|
|
541
543
|
...issueData,
|
|
542
544
|
path: fullPath
|
|
@@ -652,11 +654,11 @@ var errorUtil;
|
|
|
652
654
|
|
|
653
655
|
// node_modules/.pnpm/zod@3.25.76/node_modules/zod/v3/types.js
|
|
654
656
|
var ParseInputLazyPath = class {
|
|
655
|
-
constructor(parent, value,
|
|
657
|
+
constructor(parent, value, path12, key) {
|
|
656
658
|
this._cachedPath = [];
|
|
657
659
|
this.parent = parent;
|
|
658
660
|
this.data = value;
|
|
659
|
-
this._path =
|
|
661
|
+
this._path = path12;
|
|
660
662
|
this._key = key;
|
|
661
663
|
}
|
|
662
664
|
get path() {
|
|
@@ -4158,6 +4160,70 @@ function envKeyConflict(home, env = process.env) {
|
|
|
4158
4160
|
function writeCredentials(home, creds) {
|
|
4159
4161
|
atomicWriteFile(credentialsPath(home), JSON.stringify(creds, null, 2) + "\n", 384);
|
|
4160
4162
|
}
|
|
4163
|
+
var INSTALL_ID_FILE = "install-id";
|
|
4164
|
+
var INSTALL_ID_SHAPE = /^ins_[A-Za-z0-9_-]{22}$/;
|
|
4165
|
+
function installIdPath(home) {
|
|
4166
|
+
return path2.join(home, INSTALL_ID_FILE);
|
|
4167
|
+
}
|
|
4168
|
+
function readInstallId(home) {
|
|
4169
|
+
try {
|
|
4170
|
+
const id = fs.readFileSync(installIdPath(home), "utf-8").trim();
|
|
4171
|
+
return INSTALL_ID_SHAPE.test(id) ? id : null;
|
|
4172
|
+
} catch {
|
|
4173
|
+
return null;
|
|
4174
|
+
}
|
|
4175
|
+
}
|
|
4176
|
+
function newInstallId() {
|
|
4177
|
+
return `ins_${crypto.randomBytes(16).toString("base64url")}`;
|
|
4178
|
+
}
|
|
4179
|
+
function ensureInstallId(home) {
|
|
4180
|
+
const existing = readInstallId(home);
|
|
4181
|
+
if (existing !== null) return existing;
|
|
4182
|
+
try {
|
|
4183
|
+
fs.mkdirSync(home, { recursive: true, mode: 448 });
|
|
4184
|
+
const id = newInstallId();
|
|
4185
|
+
try {
|
|
4186
|
+
fs.writeFileSync(installIdPath(home), `${id}
|
|
4187
|
+
`, { flag: "wx", mode: 384 });
|
|
4188
|
+
return id;
|
|
4189
|
+
} catch (err) {
|
|
4190
|
+
if (err.code !== "EEXIST") return null;
|
|
4191
|
+
const theirs = readInstallId(home);
|
|
4192
|
+
if (theirs !== null) return theirs;
|
|
4193
|
+
atomicWriteFile(installIdPath(home), `${id}
|
|
4194
|
+
`, 384);
|
|
4195
|
+
return id;
|
|
4196
|
+
}
|
|
4197
|
+
} catch {
|
|
4198
|
+
return null;
|
|
4199
|
+
}
|
|
4200
|
+
}
|
|
4201
|
+
var HOST_FILE = "runtime-host";
|
|
4202
|
+
var HOST_SHAPE = /^[A-Za-z0-9][A-Za-z0-9._+/-]{0,63}$/;
|
|
4203
|
+
function readRememberedHost(home) {
|
|
4204
|
+
try {
|
|
4205
|
+
const host = fs.readFileSync(path2.join(home, HOST_FILE), "utf-8").trim();
|
|
4206
|
+
return HOST_SHAPE.test(host) ? host : null;
|
|
4207
|
+
} catch {
|
|
4208
|
+
return null;
|
|
4209
|
+
}
|
|
4210
|
+
}
|
|
4211
|
+
function rememberHost(home, host) {
|
|
4212
|
+
if (!HOST_SHAPE.test(host) || readRememberedHost(home) === host) return;
|
|
4213
|
+
try {
|
|
4214
|
+
atomicWriteFile(path2.join(home, HOST_FILE), `${host}
|
|
4215
|
+
`, 384);
|
|
4216
|
+
} catch {
|
|
4217
|
+
}
|
|
4218
|
+
}
|
|
4219
|
+
function clearInstallId(home) {
|
|
4220
|
+
try {
|
|
4221
|
+
fs.unlinkSync(installIdPath(home));
|
|
4222
|
+
return true;
|
|
4223
|
+
} catch {
|
|
4224
|
+
return false;
|
|
4225
|
+
}
|
|
4226
|
+
}
|
|
4161
4227
|
function clearCredentials(home) {
|
|
4162
4228
|
let removed = false;
|
|
4163
4229
|
for (const p of [credentialsPath(home), pendingPath(home)]) {
|
|
@@ -4318,12 +4384,12 @@ function checkManual(options) {
|
|
|
4318
4384
|
const local = readLocal(file);
|
|
4319
4385
|
const version = local.version;
|
|
4320
4386
|
const have = version ?? "an unversioned file";
|
|
4321
|
-
const result = (state,
|
|
4387
|
+
const result = (state, verdict2, summary) => ({
|
|
4322
4388
|
state,
|
|
4323
4389
|
path: file,
|
|
4324
4390
|
version,
|
|
4325
4391
|
served: served?.version ?? null,
|
|
4326
|
-
verdict,
|
|
4392
|
+
verdict: verdict2,
|
|
4327
4393
|
summary,
|
|
4328
4394
|
detail: `${summary} \u2014 ${file}`
|
|
4329
4395
|
});
|
|
@@ -4399,12 +4465,12 @@ function renderUnregisteredBlock(copy) {
|
|
|
4399
4465
|
"",
|
|
4400
4466
|
`FIRST ask exactly: "Do you already have an AgentChat account for this ${label} agent, or should I create a new one?"`,
|
|
4401
4467
|
"",
|
|
4402
|
-
|
|
4468
|
+
`**Run the commands yourself, and ask the user one question per turn.** Each command prints AgentChat\u2019s answer, then the command to run next, and ends with a \`Setup:\` line: done (\`ready\`), the next command, or what to ask the user. Relay the answer to the user as it is; ask them for what the next command needs (a value it leaves as \`<\u2026>\`, such as the code AgentChat emailed them); if it says to wait, wait; then run it. Follow the \`Setup:\` line until it says \`ready\`. ${label} may ask the user to approve a command.`,
|
|
4403
4469
|
"",
|
|
4404
4470
|
`- **New account:** ask for their email, then for the @handle they want (offer a suggestion if they like). Run: \`${invoke} register --email <email> --handle <handle>\`. Never submit a handle the user has not chosen or accepted.`,
|
|
4405
|
-
`- **Existing account, with its API key:**
|
|
4471
|
+
`- **Existing account, with its API key:** the key never goes in this chat or on a command line. If an AgentChat answer gave you the key, write it alone into a file with your file-editing tool; if the user holds it, they save it into a file and tell you where. Run \`${invoke} login --api-key - < <that file>\`, then delete the file.`,
|
|
4406
4472
|
`- **Existing account, key lost:** recovery replaces the old API key, so anything still using it stops working; say so and ask before going on. Ask for the email the account uses, then which @handle it is. Run: \`${invoke} recover --handle <handle> --email <email>\``,
|
|
4407
|
-
|
|
4473
|
+
`- **When it says \`ready\`:** tell the user the @handle and where the API key is stored, exactly as the command printed them, and that a new ${label} session gives this chat the AgentChat tools.`,
|
|
4408
4474
|
"- Never reuse another active coding agent\u2019s account; each reachable agent needs its own.",
|
|
4409
4475
|
"",
|
|
4410
4476
|
`**If they decline or say "later", run \`${invoke} register --not-now\` immediately.** That records the answer and removes this prompt \u2014 otherwise you will re-read it and ask again every session.`,
|
|
@@ -4718,10 +4784,10 @@ function identityBlockState(filePath, options) {
|
|
|
4718
4784
|
const handle = kit5.anchor.normalizeHandle(options.handle);
|
|
4719
4785
|
const loadNote = options.loadNote ?? null;
|
|
4720
4786
|
const notLoaded = loadNote !== null && !loadNote.ok ? loadNote.note : null;
|
|
4721
|
-
const state = (name,
|
|
4787
|
+
const state = (name, verdict2, claimed, what, checked2) => ({
|
|
4722
4788
|
state: name,
|
|
4723
4789
|
// A runtime that does not get the served line is never a pass.
|
|
4724
|
-
verdict: notLoaded !== null &&
|
|
4790
|
+
verdict: notLoaded !== null && verdict2 === "PASS" ? "WARN" : verdict2,
|
|
4725
4791
|
checked: checked2,
|
|
4726
4792
|
claimed,
|
|
4727
4793
|
loadNote,
|
|
@@ -4921,8 +4987,24 @@ var StatusSchema = external_exports.object({
|
|
|
4921
4987
|
failed_runs_in_a_row: external_exports.number().nullable().optional(),
|
|
4922
4988
|
last_ok_run_at: external_exports.string().nullable().optional(),
|
|
4923
4989
|
could_not_process: CouldNotProcessSchema.nullable().optional(),
|
|
4924
|
-
identity_line: external_exports.object({ outcome: external_exports.string(), path: external_exports.string(), at: external_exports.string(), texts_version: external_exports.string(), detail: external_exports.string(), not_loaded: external_exports.string().nullable().optional() }).nullable().optional()
|
|
4990
|
+
identity_line: external_exports.object({ outcome: external_exports.string(), path: external_exports.string(), at: external_exports.string(), texts_version: external_exports.string(), detail: external_exports.string(), not_loaded: external_exports.string().nullable().optional() }).nullable().optional(),
|
|
4991
|
+
// Kept leniently: a reader never loses the whole file to a block it cannot read.
|
|
4992
|
+
listener_report: external_exports.unknown().optional()
|
|
4925
4993
|
}).passthrough();
|
|
4994
|
+
var ListenerReportSchema = external_exports.object({
|
|
4995
|
+
instance_id: external_exports.string(),
|
|
4996
|
+
state: external_exports.string().nullable(),
|
|
4997
|
+
reason_kind: external_exports.string().nullable(),
|
|
4998
|
+
reported: external_exports.object({ state: external_exports.string(), reason_kind: external_exports.string().nullable(), at: external_exports.string() }).nullable(),
|
|
4999
|
+
pending: external_exports.boolean(),
|
|
5000
|
+
failures: external_exports.number(),
|
|
5001
|
+
last_error: external_exports.string().nullable(),
|
|
5002
|
+
next_attempt_at: external_exports.string().nullable()
|
|
5003
|
+
});
|
|
5004
|
+
function listenerReportOf(raw) {
|
|
5005
|
+
const parsed = ListenerReportSchema.safeParse(raw);
|
|
5006
|
+
return parsed.success ? parsed.data : null;
|
|
5007
|
+
}
|
|
4926
5008
|
function normalize(raw) {
|
|
4927
5009
|
const lastRun = raw.last_run === null ? null : {
|
|
4928
5010
|
...raw.last_run,
|
|
@@ -4964,7 +5046,8 @@ function normalize(raw) {
|
|
|
4964
5046
|
failed_runs_in_a_row: raw.failed_runs_in_a_row ?? null,
|
|
4965
5047
|
last_ok_run_at: raw.last_ok_run_at ?? null,
|
|
4966
5048
|
could_not_process: raw.could_not_process ?? null,
|
|
4967
|
-
identity_line: raw.identity_line === void 0 || raw.identity_line === null ? null : { ...raw.identity_line, not_loaded: raw.identity_line.not_loaded ?? null }
|
|
5049
|
+
identity_line: raw.identity_line === void 0 || raw.identity_line === null ? null : { ...raw.identity_line, not_loaded: raw.identity_line.not_loaded ?? null },
|
|
5050
|
+
listener_report: listenerReportOf(raw.listener_report)
|
|
4968
5051
|
};
|
|
4969
5052
|
}
|
|
4970
5053
|
function integrationStatusPath(home) {
|
|
@@ -5081,24 +5164,38 @@ function textsLine(texts, now) {
|
|
|
5081
5164
|
const versions = `${texts.version !== null ? `v${texts.version}` : "unversioned"}${texts.manual_version !== null ? `, manual ${texts.manual_version}` : ""}`;
|
|
5082
5165
|
return texts.source === "server" ? ` texts: served (${versions}${fetched}; refreshed daily)` : ` texts: from the identity-home cache (${versions}${fetched}; the server has not answered since the daemon started)`;
|
|
5083
5166
|
}
|
|
5167
|
+
function listenerReportLines(report, now) {
|
|
5168
|
+
if (report === null || report.state === null) return [];
|
|
5169
|
+
const what = `${report.state}${report.reason_kind !== null ? ` (${report.reason_kind})` : ""}`;
|
|
5170
|
+
if (!report.pending && report.reported !== null) {
|
|
5171
|
+
const at = Date.parse(report.reported.at);
|
|
5172
|
+
return [` listener report: ${what} \u2014 AgentChat has it${Number.isNaN(at) ? "" : ` (accepted ${ago(Math.max(0, now - at))})`}`];
|
|
5173
|
+
}
|
|
5174
|
+
if (report.failures === 0) return [` listener report: ${what} \u2014 being sent`];
|
|
5175
|
+
const next = report.next_attempt_at === null ? null : Date.parse(report.next_attempt_at) - now;
|
|
5176
|
+
return [
|
|
5177
|
+
` listener report: ${what} \u2014 not delivered yet (${report.failures} attempt(s) failed${report.last_error !== null ? `, last: ${report.last_error}` : ""})${next === null || Number.isNaN(next) ? "" : next > 0 ? `; tried again ${fromNow(next)}` : "; tried again now"}`
|
|
5178
|
+
];
|
|
5179
|
+
}
|
|
5084
5180
|
var DAEMON_ONLY_OUTCOMES = /* @__PURE__ */ new Set(["failed", "edited_meanwhile"]);
|
|
5085
5181
|
function formatIntegrationStatus(status, now = Date.now()) {
|
|
5086
5182
|
if (status === null) return ["Integration: no status recorded yet (the daemon has not run, or is still starting)"];
|
|
5087
5183
|
const age = integrationStatusAge(status, now);
|
|
5088
5184
|
const stale = age !== null && age > INTEGRATION_STATUS_STALE_MS;
|
|
5089
5185
|
const standby = status.standby ?? null;
|
|
5090
|
-
const
|
|
5186
|
+
const failing2 = status.runs_failing ?? null;
|
|
5091
5187
|
const unservable = status.unservable ?? null;
|
|
5092
5188
|
const lines = [
|
|
5093
|
-
`Integration: socket ${status.socket}${stale ? ` (status last written ${ago(age)} \u2014 the daemon may be down)` : ""}${unservable !== null ? " \u2014 this home gave the agent up (below)" : standby !== null ? " \u2014 standing by: another listener serves this agent" :
|
|
5189
|
+
`Integration: socket ${status.socket}${stale ? ` (status last written ${ago(age)} \u2014 the daemon may be down)` : ""}${unservable !== null ? " \u2014 this home gave the agent up (below)" : standby !== null ? " \u2014 standing by: another listener serves this agent" : failing2 !== null ? " \u2014 but runs are failing (below)" : ""}`,
|
|
5094
5190
|
...unservable !== null ? [
|
|
5095
5191
|
` gave up: ${unservable.reason === "key_refused" ? "the server refuses this home's key" : "runs fail for a runtime reason, and another listener for the agent was waiting"} \u2014 ${unservable.detail} (since ${unservable.since}). No socket, no run here while another listener can serve it; looks again at ${unservable.retry_at}${unservable.reason === "runs_failing" ? " (the runtime checked first: a check that passes clears this)" : " (signing in again here clears this at once)"}, and takes the agent back whenever no other listener holds it.`
|
|
5096
5192
|
] : [],
|
|
5097
5193
|
...standby !== null ? [` standby: ${standby.home ?? "another identity home"}${standby.pid !== null ? ` (pid ${standby.pid})` : ""} serves this agent${standby.since !== null ? ` since ${standby.since}` : ""}; this one holds no socket and starts no run while it does (no message is answered twice), and takes over when it stops`] : [],
|
|
5098
|
-
...
|
|
5099
|
-
` runs failing: ${
|
|
5194
|
+
...failing2 !== null ? [
|
|
5195
|
+
` runs failing: ${failing2.reason} \u2014 since ${failing2.since} (${ago(Math.max(0, now - Date.parse(failing2.since)))}), ${failing2.runs_missed} run(s) missed; checked ${ago(Math.max(0, now - Date.parse(failing2.checked_at)))}. ${unservable !== null ? "The runtime recheck clears this." : "Wakes still arrive, are counted and start runs; the next run that succeeds clears this."}`
|
|
5100
5196
|
] : [],
|
|
5101
5197
|
...status.lease_errors !== void 0 && status.lease_errors !== null && status.lease_errors.count > 0 ? [` lock: ${status.lease_errors.count} renewal(s) failed${status.lease_errors.last ? ` (last: ${status.lease_errors.last})` : ""}; a lock that cannot be renewed for 45 s is given up`] : [],
|
|
5198
|
+
...listenerReportLines(status.listener_report ?? null, now),
|
|
5102
5199
|
" delivery: acknowledged on receipt by the SDK socket (delivered = this machine received it; read is the agent\u2019s own verdict inside its run)",
|
|
5103
5200
|
` wakes: ${status.wakes} (${status.wakes_absorbed} absorbed into a pending run), invites: ${status.invites}`,
|
|
5104
5201
|
` runs: ${status.runs_started} started, ${status.runs_ok} ok, ${status.runs_failed} failed, ${status.runs_timed_out} timed out, ${status.runs_not_started} did not start; ${status.runs_skipped_empty} wake(s) found the tray empty (no run)`,
|
|
@@ -5143,9 +5240,9 @@ function formatIntegrationStatus(status, now = Date.now()) {
|
|
|
5143
5240
|
if (cycles.length > 0) {
|
|
5144
5241
|
const last = cycles[cycles.length - 1];
|
|
5145
5242
|
lines.push(` cycles: ${cycles.length} recent; the last one ${last.outcome} (${last.cards} card(s), ${last.invites} invite(s)) ${ago(Math.max(0, now - Date.parse(last.at)))}`);
|
|
5146
|
-
const
|
|
5147
|
-
if (
|
|
5148
|
-
lines.push(` WARNING: the last ${
|
|
5243
|
+
const failing3 = failedCyclesInARow(status);
|
|
5244
|
+
if (failing3 !== null && failing3.count >= REPEATED_FAILED_CYCLES) {
|
|
5245
|
+
lines.push(` WARNING: the last ${failing3.count} wake cycles did not get done (latest: ${failing3.latest.outcome}${failing3.latest.failure ? ` \u2014 ${failing3.latest.failure}` : ""})`);
|
|
5149
5246
|
}
|
|
5150
5247
|
}
|
|
5151
5248
|
if (status.retry_due_at) {
|
|
@@ -5161,106 +5258,11 @@ function isRunnerFactory(source) {
|
|
|
5161
5258
|
return typeof source.create === "function";
|
|
5162
5259
|
}
|
|
5163
5260
|
|
|
5164
|
-
// src/daemon/health.ts
|
|
5165
|
-
import * as fs5 from "fs";
|
|
5166
|
-
import * as path6 from "path";
|
|
5167
|
-
var ALWAYS_ON_WANTED = "always-on.wanted";
|
|
5168
|
-
var ALWAYS_ON_INSTALLED_VERSION = "always-on.installed-version";
|
|
5169
|
-
var HEARTBEAT_FILE = "daemon.heartbeat";
|
|
5170
|
-
var HEARTBEAT_STALE_MS = 3 * 6e4;
|
|
5171
|
-
function markAlwaysOnWanted(home) {
|
|
5172
|
-
try {
|
|
5173
|
-
fs5.mkdirSync(home, { recursive: true });
|
|
5174
|
-
fs5.writeFileSync(path6.join(home, ALWAYS_ON_WANTED), (/* @__PURE__ */ new Date()).toISOString());
|
|
5175
|
-
} catch {
|
|
5176
|
-
}
|
|
5177
|
-
}
|
|
5178
|
-
function clearAlwaysOnWanted(home) {
|
|
5179
|
-
try {
|
|
5180
|
-
fs5.rmSync(path6.join(home, ALWAYS_ON_WANTED), { force: true });
|
|
5181
|
-
} catch {
|
|
5182
|
-
}
|
|
5183
|
-
}
|
|
5184
|
-
function alwaysOnWanted(home) {
|
|
5185
|
-
return fs5.existsSync(path6.join(home, ALWAYS_ON_WANTED));
|
|
5186
|
-
}
|
|
5187
|
-
function readAlwaysOnInstalledVersion(home) {
|
|
5188
|
-
try {
|
|
5189
|
-
const version = fs5.readFileSync(path6.join(home, ALWAYS_ON_INSTALLED_VERSION), "utf-8").trim();
|
|
5190
|
-
return version.length > 0 ? version : null;
|
|
5191
|
-
} catch {
|
|
5192
|
-
return null;
|
|
5193
|
-
}
|
|
5194
|
-
}
|
|
5195
|
-
function markAlwaysOnInstalledVersion(home, version) {
|
|
5196
|
-
atomicWriteFile(path6.join(home, ALWAYS_ON_INSTALLED_VERSION), `${version}
|
|
5197
|
-
`, 384);
|
|
5198
|
-
}
|
|
5199
|
-
function clearAlwaysOnInstalledVersion(home) {
|
|
5200
|
-
try {
|
|
5201
|
-
fs5.rmSync(path6.join(home, ALWAYS_ON_INSTALLED_VERSION), { force: true });
|
|
5202
|
-
} catch {
|
|
5203
|
-
}
|
|
5204
|
-
}
|
|
5205
|
-
var ALWAYS_ON_OPTOUT = "always-on.optout";
|
|
5206
|
-
function markAlwaysOnOptOut(home) {
|
|
5207
|
-
try {
|
|
5208
|
-
fs5.mkdirSync(home, { recursive: true });
|
|
5209
|
-
fs5.writeFileSync(path6.join(home, ALWAYS_ON_OPTOUT), (/* @__PURE__ */ new Date()).toISOString());
|
|
5210
|
-
} catch {
|
|
5211
|
-
}
|
|
5212
|
-
}
|
|
5213
|
-
function clearAlwaysOnOptOut(home) {
|
|
5214
|
-
try {
|
|
5215
|
-
fs5.rmSync(path6.join(home, ALWAYS_ON_OPTOUT), { force: true });
|
|
5216
|
-
} catch {
|
|
5217
|
-
}
|
|
5218
|
-
}
|
|
5219
|
-
function alwaysOnOptedOut(home) {
|
|
5220
|
-
return fs5.existsSync(path6.join(home, ALWAYS_ON_OPTOUT));
|
|
5221
|
-
}
|
|
5222
|
-
function beat(home) {
|
|
5223
|
-
try {
|
|
5224
|
-
fs5.mkdirSync(home, { recursive: true });
|
|
5225
|
-
fs5.writeFileSync(path6.join(home, HEARTBEAT_FILE), (/* @__PURE__ */ new Date()).toISOString());
|
|
5226
|
-
} catch {
|
|
5227
|
-
}
|
|
5228
|
-
}
|
|
5229
|
-
function idle(home) {
|
|
5230
|
-
try {
|
|
5231
|
-
fs5.rmSync(path6.join(home, HEARTBEAT_FILE), { force: true });
|
|
5232
|
-
} catch {
|
|
5233
|
-
}
|
|
5234
|
-
}
|
|
5235
|
-
var STARTUP_GRACE_MS = 9e4;
|
|
5236
|
-
function alwaysOnState(home) {
|
|
5237
|
-
if (!alwaysOnWanted(home)) return "off";
|
|
5238
|
-
if (!fs5.existsSync(path6.join(home, "credentials"))) return "idle";
|
|
5239
|
-
let beating = false;
|
|
5240
|
-
try {
|
|
5241
|
-
const age = Date.now() - fs5.statSync(path6.join(home, HEARTBEAT_FILE)).mtimeMs;
|
|
5242
|
-
beating = age <= HEARTBEAT_STALE_MS;
|
|
5243
|
-
} catch {
|
|
5244
|
-
beating = false;
|
|
5245
|
-
}
|
|
5246
|
-
if (beating) return "connected";
|
|
5247
|
-
try {
|
|
5248
|
-
const since = Date.now() - fs5.statSync(path6.join(home, ALWAYS_ON_WANTED)).mtimeMs;
|
|
5249
|
-
if (since <= STARTUP_GRACE_MS) return "starting";
|
|
5250
|
-
} catch {
|
|
5251
|
-
}
|
|
5252
|
-
return "down";
|
|
5253
|
-
}
|
|
5254
|
-
function alwaysOnHealth(home) {
|
|
5255
|
-
const state = alwaysOnState(home);
|
|
5256
|
-
return { wanted: state !== "off", healthy: state !== "down" };
|
|
5257
|
-
}
|
|
5258
|
-
|
|
5259
5261
|
// src/util/lease.ts
|
|
5260
|
-
import * as
|
|
5261
|
-
import * as
|
|
5262
|
+
import * as crypto2 from "crypto";
|
|
5263
|
+
import * as fs5 from "fs";
|
|
5262
5264
|
import * as os from "os";
|
|
5263
|
-
import * as
|
|
5265
|
+
import * as path6 from "path";
|
|
5264
5266
|
var LEASE_RENEW_MS = 1e4;
|
|
5265
5267
|
var LEASE_STALE_MS = 45e3;
|
|
5266
5268
|
var WRITE_GRACE_MS = 5e3;
|
|
@@ -5268,14 +5270,14 @@ var GUARD_STALE_MS = 1e4;
|
|
|
5268
5270
|
var BOOT_MARGIN_MS = 6e4;
|
|
5269
5271
|
var ATTEMPTS = 40;
|
|
5270
5272
|
var BACKOFF_MS = 25;
|
|
5271
|
-
var sleep = (ms) => new Promise((
|
|
5273
|
+
var sleep = (ms) => new Promise((resolve4) => setTimeout(resolve4, ms));
|
|
5272
5274
|
function pause(ms, signal) {
|
|
5273
5275
|
if (signal?.aborted === true) return Promise.resolve();
|
|
5274
|
-
return new Promise((
|
|
5276
|
+
return new Promise((resolve4) => {
|
|
5275
5277
|
const done = () => {
|
|
5276
5278
|
clearTimeout(timer);
|
|
5277
5279
|
signal?.removeEventListener("abort", done);
|
|
5278
|
-
|
|
5280
|
+
resolve4();
|
|
5279
5281
|
};
|
|
5280
5282
|
const timer = setTimeout(done, ms);
|
|
5281
5283
|
signal?.addEventListener("abort", done, { once: true });
|
|
@@ -5329,9 +5331,9 @@ function parseLease(raw) {
|
|
|
5329
5331
|
}
|
|
5330
5332
|
}
|
|
5331
5333
|
var sameHost = (holder) => holder.host === null || holder.host === hostname2();
|
|
5332
|
-
function legacyLive(holder,
|
|
5334
|
+
function legacyLive(holder, mtimeMs2, booted) {
|
|
5333
5335
|
if (!pidAlive(holder.pid)) return false;
|
|
5334
|
-
return
|
|
5336
|
+
return mtimeMs2 === null || mtimeMs2 >= booted - BOOT_MARGIN_MS;
|
|
5335
5337
|
}
|
|
5336
5338
|
function holderLive(holder, at = {}) {
|
|
5337
5339
|
const now = at.now ?? Date.now();
|
|
@@ -5361,14 +5363,14 @@ function signatureOf(holder) {
|
|
|
5361
5363
|
}
|
|
5362
5364
|
function readRaw(file) {
|
|
5363
5365
|
try {
|
|
5364
|
-
return
|
|
5366
|
+
return fs5.readFileSync(file, "utf-8");
|
|
5365
5367
|
} catch {
|
|
5366
5368
|
return null;
|
|
5367
5369
|
}
|
|
5368
5370
|
}
|
|
5369
5371
|
function mtimeOf(file) {
|
|
5370
5372
|
try {
|
|
5371
|
-
return
|
|
5373
|
+
return fs5.statSync(file).mtimeMs;
|
|
5372
5374
|
} catch {
|
|
5373
5375
|
return null;
|
|
5374
5376
|
}
|
|
@@ -5376,50 +5378,50 @@ function mtimeOf(file) {
|
|
|
5376
5378
|
function createExclusive(file, content) {
|
|
5377
5379
|
let fd;
|
|
5378
5380
|
try {
|
|
5379
|
-
fd =
|
|
5381
|
+
fd = fs5.openSync(file, "wx", 384);
|
|
5380
5382
|
} catch (err) {
|
|
5381
5383
|
if (err.code === "EEXIST") return false;
|
|
5382
5384
|
throw err;
|
|
5383
5385
|
}
|
|
5384
5386
|
try {
|
|
5385
|
-
|
|
5386
|
-
|
|
5387
|
+
fs5.writeSync(fd, content);
|
|
5388
|
+
fs5.fsyncSync(fd);
|
|
5387
5389
|
} finally {
|
|
5388
|
-
|
|
5390
|
+
fs5.closeSync(fd);
|
|
5389
5391
|
}
|
|
5390
5392
|
return true;
|
|
5391
5393
|
}
|
|
5392
5394
|
function removeIfStill(file, content) {
|
|
5393
5395
|
if (readRaw(file) !== content) return;
|
|
5394
|
-
const aside = `${file}.gone-${
|
|
5396
|
+
const aside = `${file}.gone-${crypto2.randomUUID()}`;
|
|
5395
5397
|
try {
|
|
5396
|
-
|
|
5398
|
+
fs5.renameSync(file, aside);
|
|
5397
5399
|
} catch {
|
|
5398
5400
|
return;
|
|
5399
5401
|
}
|
|
5400
|
-
if (readRaw(aside) === content)
|
|
5402
|
+
if (readRaw(aside) === content) fs5.rmSync(aside, { force: true });
|
|
5401
5403
|
else {
|
|
5402
5404
|
try {
|
|
5403
|
-
|
|
5405
|
+
fs5.linkSync(aside, file);
|
|
5404
5406
|
} catch {
|
|
5405
5407
|
}
|
|
5406
|
-
|
|
5408
|
+
fs5.rmSync(aside, { force: true });
|
|
5407
5409
|
}
|
|
5408
5410
|
}
|
|
5409
5411
|
function replaceContent(file, content) {
|
|
5410
|
-
const temp = `${file}.renew-${
|
|
5412
|
+
const temp = `${file}.renew-${crypto2.randomUUID()}`;
|
|
5411
5413
|
try {
|
|
5412
|
-
|
|
5413
|
-
|
|
5414
|
+
fs5.writeFileSync(temp, content, { mode: 384 });
|
|
5415
|
+
fs5.renameSync(temp, file);
|
|
5414
5416
|
} catch {
|
|
5415
|
-
|
|
5416
|
-
|
|
5417
|
+
fs5.rmSync(temp, { force: true });
|
|
5418
|
+
fs5.writeFileSync(file, content, { mode: 384 });
|
|
5417
5419
|
}
|
|
5418
5420
|
}
|
|
5419
5421
|
function withGuard(file, now, fn) {
|
|
5420
5422
|
const guard = `${file}.takeover`;
|
|
5421
5423
|
const mine = `${process.pid}
|
|
5422
|
-
${JSON.stringify({ pid: process.pid, token:
|
|
5424
|
+
${JSON.stringify({ pid: process.pid, token: crypto2.randomUUID(), host: hostname2(), renewed_at: now })}
|
|
5423
5425
|
`;
|
|
5424
5426
|
if (!createExclusive(guard, mine)) {
|
|
5425
5427
|
const raw = readRaw(guard);
|
|
@@ -5446,8 +5448,8 @@ async function acquireLease(file, info = {}, options = {}) {
|
|
|
5446
5448
|
const pollMs = Math.max(50, Math.min(1e3, Math.round(renewMs / 4)));
|
|
5447
5449
|
let firstSignature = null;
|
|
5448
5450
|
let confirmed = null;
|
|
5449
|
-
|
|
5450
|
-
const token =
|
|
5451
|
+
fs5.mkdirSync(path6.dirname(file), { recursive: true, mode: 448 });
|
|
5452
|
+
const token = crypto2.randomUUID();
|
|
5451
5453
|
const acquiredAt = new Date(now()).toISOString();
|
|
5452
5454
|
const render = (renewedAt) => `${process.pid}
|
|
5453
5455
|
${JSON.stringify({ pid: process.pid, token, host: hostname2(), ...info, acquired_at: acquiredAt, renewed_at: renewedAt })}
|
|
@@ -5464,18 +5466,18 @@ ${JSON.stringify({ pid: process.pid, token, host: hostname2(), ...info, acquired
|
|
|
5464
5466
|
continue;
|
|
5465
5467
|
}
|
|
5466
5468
|
const holder2 = parseLease(raw2);
|
|
5467
|
-
const
|
|
5468
|
-
if (
|
|
5469
|
+
const verdict2 = judge(file, holder2, { now: now(), mono: mono(), staleMs, booted });
|
|
5470
|
+
if (verdict2 === "writing") {
|
|
5469
5471
|
attempt += 1;
|
|
5470
5472
|
await sleep(BACKOFF_MS);
|
|
5471
5473
|
continue;
|
|
5472
5474
|
}
|
|
5473
|
-
if (
|
|
5475
|
+
if (verdict2 === "observed" && raw2 !== confirmed) {
|
|
5474
5476
|
await pause(leaseConfirmMs(renewMs), signal);
|
|
5475
5477
|
if (signal?.aborted === true) return { ok: false, path: file, holder: holder2 };
|
|
5476
5478
|
if (readRaw(file) !== raw2) continue;
|
|
5477
5479
|
confirmed = raw2;
|
|
5478
|
-
} else if (
|
|
5480
|
+
} else if (verdict2 === "held") {
|
|
5479
5481
|
const signature = signatureOf(holder2);
|
|
5480
5482
|
if (firstSignature !== null && signature !== firstSignature) return { ok: false, path: file, holder: holder2 };
|
|
5481
5483
|
firstSignature ??= signature;
|
|
@@ -5574,27 +5576,16 @@ function resetLeaseObservations() {
|
|
|
5574
5576
|
observations.clear();
|
|
5575
5577
|
}
|
|
5576
5578
|
|
|
5577
|
-
// src/daemon/leader-lock.ts
|
|
5578
|
-
import * as path8 from "path";
|
|
5579
|
-
var DAEMON_LOCK_FILE = "daemon.lock";
|
|
5580
|
-
async function acquireLeaderLock(home, options = {}) {
|
|
5581
|
-
const lockPath = path8.join(home, DAEMON_LOCK_FILE);
|
|
5582
|
-
const attempt = await acquireLease(lockPath, { home: path8.resolve(home) }, { observeMs: leaseStartupObserveMs(options), ...options });
|
|
5583
|
-
if (attempt.ok) return attempt.lease;
|
|
5584
|
-
log.error(`another agentchat daemon already holds ${lockPath}${attempt.holder !== null ? ` (pid ${attempt.holder.pid})` : ""}`);
|
|
5585
|
-
return null;
|
|
5586
|
-
}
|
|
5587
|
-
|
|
5588
5579
|
// src/daemon/agent-lock.ts
|
|
5589
|
-
import * as
|
|
5590
|
-
import * as
|
|
5580
|
+
import * as crypto3 from "crypto";
|
|
5581
|
+
import * as fs6 from "fs";
|
|
5591
5582
|
import * as os2 from "os";
|
|
5592
|
-
import * as
|
|
5583
|
+
import * as path7 from "path";
|
|
5593
5584
|
var AGENT_LOCK_DIR_ENV = "AGENTCHAT_LOCK_DIR";
|
|
5594
5585
|
function agentLockDir(env = process.env, system = process.platform, home = os2.homedir()) {
|
|
5595
5586
|
const override = env[AGENT_LOCK_DIR_ENV]?.trim();
|
|
5596
|
-
if (override !== void 0 && override.length > 0) return
|
|
5597
|
-
const p = system === "win32" ?
|
|
5587
|
+
if (override !== void 0 && override.length > 0) return path7.resolve(override);
|
|
5588
|
+
const p = system === "win32" ? path7.win32 : path7.posix;
|
|
5598
5589
|
if (system === "darwin") return p.join(home, "Library", "Application Support", "AgentChat", "locks");
|
|
5599
5590
|
if (system === "win32") return p.join(home, "AppData", "Local", "AgentChat", "locks");
|
|
5600
5591
|
return p.join(home, ".local", "state", "agentchat", "locks");
|
|
@@ -5607,16 +5598,16 @@ function originOf(apiBase) {
|
|
|
5607
5598
|
}
|
|
5608
5599
|
}
|
|
5609
5600
|
function agentLockPath(agent, env = process.env) {
|
|
5610
|
-
const id =
|
|
5601
|
+
const id = crypto3.createHash("sha256").update(`${originOf(agent.apiBase)}
|
|
5611
5602
|
${normalizeHandle(agent.handle)}`).digest("hex").slice(0, 32);
|
|
5612
|
-
return
|
|
5603
|
+
return path7.join(agentLockDir(env), `agent-${id}.lock`);
|
|
5613
5604
|
}
|
|
5614
5605
|
function acquireAgentLock(agent, env = process.env, options = {}) {
|
|
5615
|
-
return acquireLease(agentLockPath(agent, env), { home:
|
|
5606
|
+
return acquireLease(agentLockPath(agent, env), { home: path7.resolve(agent.home), label: `@${normalizeHandle(agent.handle)}` }, options);
|
|
5616
5607
|
}
|
|
5617
5608
|
function agentLockRefusal(agent, holder, file) {
|
|
5618
5609
|
const who = holder === null ? `another listener (its lock ${file} is being taken over right now)` : `${holder.home !== void 0 ? holder.home : "another identity home"} (pid ${holder.pid}${holder.host !== null ? ` on ${holder.host}` : ""}${holder.acquired_at !== void 0 ? `, since ${holder.acquired_at}` : ""})`;
|
|
5619
|
-
return `@${normalizeHandle(agent.handle)} is served by ${who}: this one (${
|
|
5610
|
+
return `@${normalizeHandle(agent.handle)} is served by ${who}: this one (${path7.resolve(agent.home)}) holds no socket and starts no run while it does, so no message is answered twice. Stop that one, or log out here; this one takes over when it stops.`;
|
|
5620
5611
|
}
|
|
5621
5612
|
var STANDBY_MARKER_FRESH_MS = 9e4;
|
|
5622
5613
|
function hostnameOf() {
|
|
@@ -5627,44 +5618,44 @@ function hostnameOf() {
|
|
|
5627
5618
|
}
|
|
5628
5619
|
}
|
|
5629
5620
|
function standbyMarkerPath(lockFile, home) {
|
|
5630
|
-
return `${lockFile}.standby-${
|
|
5621
|
+
return `${lockFile}.standby-${crypto3.createHash("sha256").update(path7.resolve(home)).digest("hex").slice(0, 16)}`;
|
|
5631
5622
|
}
|
|
5632
5623
|
function markStandby(lockFile, home) {
|
|
5633
5624
|
const file = standbyMarkerPath(lockFile, home);
|
|
5634
5625
|
const content = `${process.pid}
|
|
5635
|
-
${JSON.stringify({ pid: process.pid, host: hostnameOf(), home:
|
|
5626
|
+
${JSON.stringify({ pid: process.pid, host: hostnameOf(), home: path7.resolve(home), at: Date.now() })}
|
|
5636
5627
|
`;
|
|
5637
5628
|
try {
|
|
5638
|
-
|
|
5639
|
-
const temp = `${file}.${
|
|
5640
|
-
|
|
5641
|
-
|
|
5629
|
+
fs6.mkdirSync(path7.dirname(file), { recursive: true, mode: 448 });
|
|
5630
|
+
const temp = `${file}.${crypto3.randomUUID()}`;
|
|
5631
|
+
fs6.writeFileSync(temp, content, { mode: 384 });
|
|
5632
|
+
fs6.renameSync(temp, file);
|
|
5642
5633
|
} catch {
|
|
5643
5634
|
}
|
|
5644
5635
|
}
|
|
5645
5636
|
function clearStandby(lockFile, home) {
|
|
5646
5637
|
try {
|
|
5647
|
-
|
|
5638
|
+
fs6.rmSync(standbyMarkerPath(lockFile, home), { force: true });
|
|
5648
5639
|
} catch {
|
|
5649
5640
|
}
|
|
5650
5641
|
}
|
|
5651
5642
|
function standbyWaiting(lockFile, home, now = Date.now()) {
|
|
5652
5643
|
const own = standbyMarkerPath(lockFile, home);
|
|
5653
|
-
const prefix = `${
|
|
5644
|
+
const prefix = `${path7.basename(lockFile)}.standby-`;
|
|
5654
5645
|
let names;
|
|
5655
5646
|
try {
|
|
5656
|
-
names =
|
|
5647
|
+
names = fs6.readdirSync(path7.dirname(lockFile));
|
|
5657
5648
|
} catch {
|
|
5658
5649
|
return false;
|
|
5659
5650
|
}
|
|
5660
5651
|
for (const name of names) {
|
|
5661
5652
|
if (!name.startsWith(prefix) || name.includes(".", prefix.length)) continue;
|
|
5662
|
-
const file =
|
|
5653
|
+
const file = path7.join(path7.dirname(lockFile), name);
|
|
5663
5654
|
if (file === own) continue;
|
|
5664
5655
|
try {
|
|
5665
|
-
const st =
|
|
5656
|
+
const st = fs6.statSync(file);
|
|
5666
5657
|
if (Math.abs(now - st.mtimeMs) >= STANDBY_MARKER_FRESH_MS) continue;
|
|
5667
|
-
const marker = parseLease(
|
|
5658
|
+
const marker = parseLease(fs6.readFileSync(file, "utf-8"));
|
|
5668
5659
|
if (marker === null) continue;
|
|
5669
5660
|
const sameHost2 = marker.host === null || marker.host === hostnameOf();
|
|
5670
5661
|
if (sameHost2 && !pidAlive(marker.pid)) continue;
|
|
@@ -5676,6 +5667,651 @@ function standbyWaiting(lockFile, home, now = Date.now()) {
|
|
|
5676
5667
|
return false;
|
|
5677
5668
|
}
|
|
5678
5669
|
|
|
5670
|
+
// src/integration/listener-report.ts
|
|
5671
|
+
import * as fs7 from "fs";
|
|
5672
|
+
import * as path8 from "path";
|
|
5673
|
+
import { AgentChatClient as AgentChatClient2, integration as kit6 } from "agentchatme";
|
|
5674
|
+
var LISTENER_ID_FILE = "listener-id";
|
|
5675
|
+
function listenerIdPath(home) {
|
|
5676
|
+
return path8.join(home, LISTENER_ID_FILE);
|
|
5677
|
+
}
|
|
5678
|
+
async function ensureListenerInstanceId(home) {
|
|
5679
|
+
try {
|
|
5680
|
+
return await kit6.listenerInstanceId(listenerIdPath(home));
|
|
5681
|
+
} catch (err) {
|
|
5682
|
+
log.warn(`listener report: no listener id could be kept in ${listenerIdPath(home)} (${err instanceof Error ? err.message : String(err)}); this listener serves without telling AgentChat its state`);
|
|
5683
|
+
return null;
|
|
5684
|
+
}
|
|
5685
|
+
}
|
|
5686
|
+
function readListenerInstanceId(home) {
|
|
5687
|
+
try {
|
|
5688
|
+
const id = fs7.readFileSync(listenerIdPath(home), "utf-8").trim();
|
|
5689
|
+
return kit6.isListenerInstanceId(id) ? id : null;
|
|
5690
|
+
} catch {
|
|
5691
|
+
return null;
|
|
5692
|
+
}
|
|
5693
|
+
}
|
|
5694
|
+
var DAEMON_REPORT_TIMEOUT_MS = 1e4;
|
|
5695
|
+
var STOPPED_REPORT_TIMEOUT_MS = 5e3;
|
|
5696
|
+
function reportClient(identity, client, timeoutMs) {
|
|
5697
|
+
return new AgentChatClient2({
|
|
5698
|
+
apiKey: identity.apiKey,
|
|
5699
|
+
baseUrl: identity.apiBase,
|
|
5700
|
+
clientIdentity: clientIdentityOf(client),
|
|
5701
|
+
timeoutMs,
|
|
5702
|
+
retry: { maxRetries: 0, baseDelayMs: 0, maxDelayMs: 0 }
|
|
5703
|
+
});
|
|
5704
|
+
}
|
|
5705
|
+
function createListenerReporter(options) {
|
|
5706
|
+
return new kit6.ListenerReporter({
|
|
5707
|
+
client: options.api ?? reportClient(options, options.client, DAEMON_REPORT_TIMEOUT_MS),
|
|
5708
|
+
instanceId: options.instanceId,
|
|
5709
|
+
...options.timing ?? {},
|
|
5710
|
+
onError: (err) => log.warn(err.message)
|
|
5711
|
+
});
|
|
5712
|
+
}
|
|
5713
|
+
function tellListener(sink, state, reasonKind) {
|
|
5714
|
+
if (sink === null || sink === void 0) return;
|
|
5715
|
+
const reason = state === "ok" ? null : reasonKind ?? "other";
|
|
5716
|
+
const now = sink.status();
|
|
5717
|
+
if (now.state !== state || now.reasonKind !== reason) {
|
|
5718
|
+
log.info(`listener report: ${state}${reason !== null ? ` (${reason})` : ""} \u2014 what AgentChat shows as this agent's reachability`);
|
|
5719
|
+
}
|
|
5720
|
+
sink.set(state, reason ?? void 0);
|
|
5721
|
+
}
|
|
5722
|
+
function listenerReportStatusOf(sink) {
|
|
5723
|
+
if (sink === null || sink === void 0) return null;
|
|
5724
|
+
const s = sink.status();
|
|
5725
|
+
return {
|
|
5726
|
+
instance_id: s.instanceId,
|
|
5727
|
+
state: s.state,
|
|
5728
|
+
reason_kind: s.reasonKind,
|
|
5729
|
+
reported: s.reported === null ? null : { state: s.reported.state, reason_kind: s.reported.reasonKind, at: s.reported.at },
|
|
5730
|
+
pending: s.pending,
|
|
5731
|
+
failures: s.failures,
|
|
5732
|
+
last_error: s.lastError,
|
|
5733
|
+
next_attempt_at: s.nextAttemptAt
|
|
5734
|
+
};
|
|
5735
|
+
}
|
|
5736
|
+
async function reportListenerStopped(options) {
|
|
5737
|
+
const { home, identity } = options;
|
|
5738
|
+
if (identity === null || identity.source === "env") return { outcome: "skipped", reason: "no_identity" };
|
|
5739
|
+
const instanceId = readListenerInstanceId(home);
|
|
5740
|
+
if (instanceId === null) return { outcome: "skipped", reason: "no_listener" };
|
|
5741
|
+
if (identity.handle !== null) {
|
|
5742
|
+
let holder = null;
|
|
5743
|
+
try {
|
|
5744
|
+
holder = leaseHolder(agentLockPath({ apiBase: identity.apiBase, handle: identity.handle }));
|
|
5745
|
+
} catch {
|
|
5746
|
+
holder = null;
|
|
5747
|
+
}
|
|
5748
|
+
if (holder !== null && holder.home !== void 0 && path8.resolve(holder.home) !== path8.resolve(home)) return { outcome: "skipped", reason: "standby" };
|
|
5749
|
+
}
|
|
5750
|
+
const reporter = new kit6.ListenerReporter({
|
|
5751
|
+
client: options.api ?? reportClient(identity, options.client, options.timeoutMs ?? STOPPED_REPORT_TIMEOUT_MS),
|
|
5752
|
+
instanceId
|
|
5753
|
+
});
|
|
5754
|
+
try {
|
|
5755
|
+
reporter.set("stopped");
|
|
5756
|
+
if (await reporter.flush()) return { outcome: "reported" };
|
|
5757
|
+
return { outcome: "failed", detail: reporter.status().lastError ?? "AgentChat did not take it" };
|
|
5758
|
+
} catch (err) {
|
|
5759
|
+
return { outcome: "failed", detail: err instanceof Error ? err.message : String(err) };
|
|
5760
|
+
} finally {
|
|
5761
|
+
reporter.stop();
|
|
5762
|
+
}
|
|
5763
|
+
}
|
|
5764
|
+
function describeListenerStopped(result) {
|
|
5765
|
+
if (result.outcome === "reported") return "listener: AgentChat was told it stopped on purpose";
|
|
5766
|
+
if (result.outcome === "failed") return `listener: could not tell AgentChat it stopped (${result.detail}); AgentChat shows it as offline instead`;
|
|
5767
|
+
return null;
|
|
5768
|
+
}
|
|
5769
|
+
|
|
5770
|
+
// src/daemon/health.ts
|
|
5771
|
+
import * as fs8 from "fs";
|
|
5772
|
+
import * as path9 from "path";
|
|
5773
|
+
var ALWAYS_ON_WANTED = "always-on.wanted";
|
|
5774
|
+
var ALWAYS_ON_INSTALLED_VERSION = "always-on.installed-version";
|
|
5775
|
+
var HEARTBEAT_FILE = "daemon.heartbeat";
|
|
5776
|
+
var HEARTBEAT_STALE_MS = 3 * 6e4;
|
|
5777
|
+
function markAlwaysOnWanted(home) {
|
|
5778
|
+
try {
|
|
5779
|
+
fs8.mkdirSync(home, { recursive: true });
|
|
5780
|
+
fs8.writeFileSync(path9.join(home, ALWAYS_ON_WANTED), (/* @__PURE__ */ new Date()).toISOString());
|
|
5781
|
+
} catch {
|
|
5782
|
+
}
|
|
5783
|
+
}
|
|
5784
|
+
function clearAlwaysOnWanted(home) {
|
|
5785
|
+
try {
|
|
5786
|
+
fs8.rmSync(path9.join(home, ALWAYS_ON_WANTED), { force: true });
|
|
5787
|
+
} catch {
|
|
5788
|
+
}
|
|
5789
|
+
}
|
|
5790
|
+
function alwaysOnWanted(home) {
|
|
5791
|
+
return fs8.existsSync(path9.join(home, ALWAYS_ON_WANTED));
|
|
5792
|
+
}
|
|
5793
|
+
function readAlwaysOnInstalledVersion(home) {
|
|
5794
|
+
try {
|
|
5795
|
+
const version = fs8.readFileSync(path9.join(home, ALWAYS_ON_INSTALLED_VERSION), "utf-8").trim();
|
|
5796
|
+
return version.length > 0 ? version : null;
|
|
5797
|
+
} catch {
|
|
5798
|
+
return null;
|
|
5799
|
+
}
|
|
5800
|
+
}
|
|
5801
|
+
function markAlwaysOnInstalledVersion(home, version) {
|
|
5802
|
+
atomicWriteFile(path9.join(home, ALWAYS_ON_INSTALLED_VERSION), `${version}
|
|
5803
|
+
`, 384);
|
|
5804
|
+
}
|
|
5805
|
+
function clearAlwaysOnInstalledVersion(home) {
|
|
5806
|
+
try {
|
|
5807
|
+
fs8.rmSync(path9.join(home, ALWAYS_ON_INSTALLED_VERSION), { force: true });
|
|
5808
|
+
} catch {
|
|
5809
|
+
}
|
|
5810
|
+
}
|
|
5811
|
+
var ALWAYS_ON_OPTOUT = "always-on.optout";
|
|
5812
|
+
function markAlwaysOnOptOut(home) {
|
|
5813
|
+
try {
|
|
5814
|
+
fs8.mkdirSync(home, { recursive: true });
|
|
5815
|
+
fs8.writeFileSync(path9.join(home, ALWAYS_ON_OPTOUT), (/* @__PURE__ */ new Date()).toISOString());
|
|
5816
|
+
} catch {
|
|
5817
|
+
}
|
|
5818
|
+
}
|
|
5819
|
+
function clearAlwaysOnOptOut(home) {
|
|
5820
|
+
try {
|
|
5821
|
+
fs8.rmSync(path9.join(home, ALWAYS_ON_OPTOUT), { force: true });
|
|
5822
|
+
} catch {
|
|
5823
|
+
}
|
|
5824
|
+
}
|
|
5825
|
+
function alwaysOnOptedOut(home) {
|
|
5826
|
+
return fs8.existsSync(path9.join(home, ALWAYS_ON_OPTOUT));
|
|
5827
|
+
}
|
|
5828
|
+
function beat(home) {
|
|
5829
|
+
try {
|
|
5830
|
+
fs8.mkdirSync(home, { recursive: true });
|
|
5831
|
+
fs8.writeFileSync(path9.join(home, HEARTBEAT_FILE), (/* @__PURE__ */ new Date()).toISOString());
|
|
5832
|
+
} catch {
|
|
5833
|
+
}
|
|
5834
|
+
}
|
|
5835
|
+
function idle(home) {
|
|
5836
|
+
try {
|
|
5837
|
+
fs8.rmSync(path9.join(home, HEARTBEAT_FILE), { force: true });
|
|
5838
|
+
} catch {
|
|
5839
|
+
}
|
|
5840
|
+
}
|
|
5841
|
+
var STARTUP_GRACE_MS = 9e4;
|
|
5842
|
+
function alwaysOnState(home) {
|
|
5843
|
+
if (!alwaysOnWanted(home)) return "off";
|
|
5844
|
+
if (!fs8.existsSync(path9.join(home, "credentials"))) return "idle";
|
|
5845
|
+
let beating = false;
|
|
5846
|
+
try {
|
|
5847
|
+
const age = Date.now() - fs8.statSync(path9.join(home, HEARTBEAT_FILE)).mtimeMs;
|
|
5848
|
+
beating = age <= HEARTBEAT_STALE_MS;
|
|
5849
|
+
} catch {
|
|
5850
|
+
beating = false;
|
|
5851
|
+
}
|
|
5852
|
+
if (beating) return "connected";
|
|
5853
|
+
try {
|
|
5854
|
+
const since = Date.now() - fs8.statSync(path9.join(home, ALWAYS_ON_WANTED)).mtimeMs;
|
|
5855
|
+
if (since <= STARTUP_GRACE_MS) return "starting";
|
|
5856
|
+
} catch {
|
|
5857
|
+
}
|
|
5858
|
+
return "down";
|
|
5859
|
+
}
|
|
5860
|
+
function alwaysOnHealth(home) {
|
|
5861
|
+
const state = alwaysOnState(home);
|
|
5862
|
+
return { wanted: state !== "off", healthy: state !== "down" };
|
|
5863
|
+
}
|
|
5864
|
+
|
|
5865
|
+
// src/daemon/leader-lock.ts
|
|
5866
|
+
import * as path10 from "path";
|
|
5867
|
+
var DAEMON_LOCK_FILE = "daemon.lock";
|
|
5868
|
+
async function acquireLeaderLock(home, options = {}) {
|
|
5869
|
+
const lockPath = path10.join(home, DAEMON_LOCK_FILE);
|
|
5870
|
+
const attempt = await acquireLease(lockPath, { home: path10.resolve(home) }, { observeMs: leaseStartupObserveMs(options), ...options });
|
|
5871
|
+
if (attempt.ok) return attempt.lease;
|
|
5872
|
+
log.error(`another agentchat daemon already holds ${lockPath}${attempt.holder !== null ? ` (pid ${attempt.holder.pid})` : ""}`);
|
|
5873
|
+
return null;
|
|
5874
|
+
}
|
|
5875
|
+
|
|
5876
|
+
// src/identity/setup.ts
|
|
5877
|
+
import * as fs9 from "fs";
|
|
5878
|
+
import * as path11 from "path";
|
|
5879
|
+
var SETUP_GRACE_MS = 9e4;
|
|
5880
|
+
var KEY_FOLLOW_MS = 15e3;
|
|
5881
|
+
var CLOCK_SLACK_MS = 2e3;
|
|
5882
|
+
var SETUP_STATES = [
|
|
5883
|
+
"no_account",
|
|
5884
|
+
"code_pending",
|
|
5885
|
+
"code_expired",
|
|
5886
|
+
"key_refused",
|
|
5887
|
+
"suspended",
|
|
5888
|
+
"unreachable",
|
|
5889
|
+
"paused",
|
|
5890
|
+
"tools_missing",
|
|
5891
|
+
"listener_off",
|
|
5892
|
+
"listener_missing",
|
|
5893
|
+
"listener_down",
|
|
5894
|
+
"listener_starting",
|
|
5895
|
+
"listener_failing",
|
|
5896
|
+
"ready"
|
|
5897
|
+
];
|
|
5898
|
+
function formatSetupVerdict(verdict2) {
|
|
5899
|
+
return `Setup: ${verdict2.state} \u2014 ${verdict2.next}`;
|
|
5900
|
+
}
|
|
5901
|
+
var VERIFY_STEP = { register: "verify", recover: "recover_verify" };
|
|
5902
|
+
function shellValue(value) {
|
|
5903
|
+
return /^[A-Za-z0-9@%+=:,./_-]+$/.test(value) ? value : JSON.stringify(value);
|
|
5904
|
+
}
|
|
5905
|
+
function receivedAtMs(pending) {
|
|
5906
|
+
const at = Date.parse(pending.received_at);
|
|
5907
|
+
return Number.isFinite(at) ? at : null;
|
|
5908
|
+
}
|
|
5909
|
+
function codeExpiresAtMs(pending) {
|
|
5910
|
+
const received = receivedAtMs(pending);
|
|
5911
|
+
if (pending.flow.expires_in_s !== void 0 && received !== null) return received + pending.flow.expires_in_s * 1e3;
|
|
5912
|
+
if (pending.flow.expires_at !== void 0) {
|
|
5913
|
+
const at = Date.parse(pending.flow.expires_at);
|
|
5914
|
+
if (Number.isFinite(at)) return at;
|
|
5915
|
+
}
|
|
5916
|
+
return null;
|
|
5917
|
+
}
|
|
5918
|
+
function codeMayWork(pending, now = Date.now()) {
|
|
5919
|
+
const at = codeExpiresAtMs(pending);
|
|
5920
|
+
return at === null || at > now;
|
|
5921
|
+
}
|
|
5922
|
+
function resendLeftS(pending, now = Date.now()) {
|
|
5923
|
+
if (pending === null || pending.flow.resend_after_s === void 0) return 0;
|
|
5924
|
+
const received = receivedAtMs(pending);
|
|
5925
|
+
if (received === null) return 0;
|
|
5926
|
+
return Math.max(0, Math.ceil((pending.flow.resend_after_s * 1e3 - (now - received)) / 1e3));
|
|
5927
|
+
}
|
|
5928
|
+
function runLine(command, waitS = 0) {
|
|
5929
|
+
return waitS > 0 ? `Wait ${waitS} s, then run \`${command}\`` : `Run \`${command}\``;
|
|
5930
|
+
}
|
|
5931
|
+
function startCommandOf(invocation, kind, input, blank) {
|
|
5932
|
+
const email = blank === "email" ? "<email>" : shellValue(input.email);
|
|
5933
|
+
const handle = blank === "handle" ? "<handle>" : input.handle !== void 0 ? shellValue(input.handle) : null;
|
|
5934
|
+
if (kind === "recover") return `${invocation} recover${handle !== null ? ` --handle ${handle}` : ""} --email ${email}`;
|
|
5935
|
+
return [
|
|
5936
|
+
`${invocation} register --email ${email} --handle ${handle ?? "<handle>"}`,
|
|
5937
|
+
...input.display_name !== void 0 ? [`--display-name ${shellValue(input.display_name)}`] : [],
|
|
5938
|
+
...input.description !== void 0 ? [`--description ${shellValue(input.description)}`] : []
|
|
5939
|
+
].join(" ");
|
|
5940
|
+
}
|
|
5941
|
+
function codeCommandOf(invocation, kind, flow, code) {
|
|
5942
|
+
const value = code !== void 0 ? shellValue(code) : flow.code_length !== void 0 ? `<${flow.code_length} digits>` : "<code>";
|
|
5943
|
+
return `${invocation} ${kind} --code ${value}`;
|
|
5944
|
+
}
|
|
5945
|
+
var ReachabilitySchema = external_exports.object({
|
|
5946
|
+
state: external_exports.string(),
|
|
5947
|
+
via: external_exports.string().nullable().optional(),
|
|
5948
|
+
problem: external_exports.object({ kind: external_exports.string(), text: external_exports.string().optional() }).passthrough().nullable().optional(),
|
|
5949
|
+
text: external_exports.string().optional()
|
|
5950
|
+
}).passthrough();
|
|
5951
|
+
function reachabilityOf(me) {
|
|
5952
|
+
const raw = me !== null && typeof me === "object" ? me.reachability : void 0;
|
|
5953
|
+
if (raw === void 0 || raw === null) return null;
|
|
5954
|
+
const parsed = ReachabilitySchema.safeParse(raw);
|
|
5955
|
+
if (!parsed.success) return null;
|
|
5956
|
+
const r = parsed.data;
|
|
5957
|
+
return {
|
|
5958
|
+
state: r.state,
|
|
5959
|
+
via: r.via ?? null,
|
|
5960
|
+
problemKind: r.problem?.kind ?? null,
|
|
5961
|
+
problemText: r.problem?.text ?? null,
|
|
5962
|
+
text: r.text ?? null
|
|
5963
|
+
};
|
|
5964
|
+
}
|
|
5965
|
+
function mtimeMs(file) {
|
|
5966
|
+
try {
|
|
5967
|
+
return fs9.statSync(file).mtimeMs;
|
|
5968
|
+
} catch {
|
|
5969
|
+
return null;
|
|
5970
|
+
}
|
|
5971
|
+
}
|
|
5972
|
+
function daemonPidOf(home) {
|
|
5973
|
+
return leaseHolder(path11.join(home, DAEMON_LOCK_FILE))?.pid ?? null;
|
|
5974
|
+
}
|
|
5975
|
+
function readListener(home, handle, options = {}) {
|
|
5976
|
+
const now = options.now ?? Date.now();
|
|
5977
|
+
const status = readIntegrationStatus(home);
|
|
5978
|
+
const alive = leaseHolder(path11.join(home, DAEMON_LOCK_FILE)) !== null;
|
|
5979
|
+
const keySavedAt = mtimeMs(credentialsPath(home));
|
|
5980
|
+
const writtenAt = status === null ? Number.NaN : Date.parse(status.updated_at);
|
|
5981
|
+
const replaced = options.replacedPid ?? null;
|
|
5982
|
+
const forThisKey = status !== null && handle !== null && status.handle === handle && (replaced === null || status.pid !== replaced) && (keySavedAt === null || writtenAt >= Math.floor(keySavedAt) || // The same key saved again: nothing for the daemon to follow, so its last word stands.
|
|
5983
|
+
alive && now - keySavedAt > KEY_FOLLOW_MS);
|
|
5984
|
+
const fresh = integrationStatusFresh(status, now);
|
|
5985
|
+
let daemon = { kind: "none" };
|
|
5986
|
+
if (status !== null && forThisKey && (fresh || alive)) {
|
|
5987
|
+
const unservable = status.unservable ?? null;
|
|
5988
|
+
const standby = status.standby ?? null;
|
|
5989
|
+
if (unservable !== null) daemon = { kind: "unservable", reason: unservable.reason, detail: unservable.detail };
|
|
5990
|
+
else if (standby !== null) {
|
|
5991
|
+
if (alive) daemon = { kind: "standby", home: standby.home, pid: standby.pid };
|
|
5992
|
+
} else if (status.socket === "off") daemon = { kind: "off" };
|
|
5993
|
+
else if (status.socket === "ready") {
|
|
5994
|
+
if (fresh && alive) {
|
|
5995
|
+
const failing2 = status.runs_failing ?? null;
|
|
5996
|
+
daemon = { kind: "ready", runsFailing: failing2 === null ? null : { reason: failing2.reason, since: failing2.since }, runtimeCheckFailing: runtimeCheckFailingNow(status) };
|
|
5997
|
+
}
|
|
5998
|
+
} else if (status.socket === "blocked") daemon = { kind: "blocked", detail: status.last_error };
|
|
5999
|
+
else if (status.socket === "idle" && status.last_error !== null) daemon = { kind: "start_failure", detail: status.last_error };
|
|
6000
|
+
else daemon = { kind: "connecting" };
|
|
6001
|
+
}
|
|
6002
|
+
const sinceSaved = keySavedAt === null ? null : now - keySavedAt;
|
|
6003
|
+
const withinGrace = alwaysOnState(home) === "starting" || sinceSaved !== null && sinceSaved > -CLOCK_SLACK_MS && sinceSaved <= SETUP_GRACE_MS;
|
|
6004
|
+
return {
|
|
6005
|
+
optedOut: alwaysOnOptedOut(home),
|
|
6006
|
+
wanted: alwaysOnWanted(home),
|
|
6007
|
+
installedVersion: readAlwaysOnInstalledVersion(home),
|
|
6008
|
+
daemon,
|
|
6009
|
+
alive,
|
|
6010
|
+
withinGrace,
|
|
6011
|
+
lastAliveAt: status?.updated_at ?? null
|
|
6012
|
+
};
|
|
6013
|
+
}
|
|
6014
|
+
function listenerReasonKindOf(detail) {
|
|
6015
|
+
const text = detail.toLowerCase();
|
|
6016
|
+
if (/not signed in|not logged in|login status failed/.test(text)) return "not_signed_in";
|
|
6017
|
+
if (/not found on path|\benoent\b/.test(text)) return "runtime_missing";
|
|
6018
|
+
if (/agentchat requires [^;]*>=/.test(text)) return "runtime_outdated";
|
|
6019
|
+
if (/runtime check/.test(text)) return "runtime_check_failed";
|
|
6020
|
+
return "other";
|
|
6021
|
+
}
|
|
6022
|
+
var PERSON = "the person you work with";
|
|
6023
|
+
function verdict(state, next, extra = {}) {
|
|
6024
|
+
return {
|
|
6025
|
+
state,
|
|
6026
|
+
next,
|
|
6027
|
+
command: extra.command ?? null,
|
|
6028
|
+
needs_person: extra.needs_person ?? false,
|
|
6029
|
+
ask: extra.ask ?? null,
|
|
6030
|
+
detail: extra.detail ?? null,
|
|
6031
|
+
reason: extra.reason ?? null
|
|
6032
|
+
};
|
|
6033
|
+
}
|
|
6034
|
+
function versionOf(text) {
|
|
6035
|
+
const m = text === null ? null : /^(\d+)\.(\d+)\.(\d+)/.exec(text.trim());
|
|
6036
|
+
return m === null ? null : [Number(m[1]), Number(m[2]), Number(m[3])];
|
|
6037
|
+
}
|
|
6038
|
+
function newerRelease(a, b) {
|
|
6039
|
+
const x = versionOf(a);
|
|
6040
|
+
const y = versionOf(b);
|
|
6041
|
+
if (x === null || y === null) return false;
|
|
6042
|
+
for (let i = 0; i < 3; i++) if (x[i] !== y[i]) return x[i] > y[i];
|
|
6043
|
+
return false;
|
|
6044
|
+
}
|
|
6045
|
+
function minutesLeft(pending, now) {
|
|
6046
|
+
const at = codeExpiresAtMs(pending);
|
|
6047
|
+
return at === null ? null : Math.max(1, Math.ceil((at - now) / 6e4));
|
|
6048
|
+
}
|
|
6049
|
+
function accountVerdict(i, now) {
|
|
6050
|
+
const inv = i.invocation;
|
|
6051
|
+
const pending = i.pending;
|
|
6052
|
+
if (pending === null) {
|
|
6053
|
+
const register = `${inv} register --email <email> --handle <handle>`;
|
|
6054
|
+
return verdict(
|
|
6055
|
+
"no_account",
|
|
6056
|
+
`this ${i.label} agent has no AgentChat account yet. New account: run \`${register}\` (an email whose inbox you or ${PERSON} can read, and a @handle they chose or accepted). Existing account with its API key: if an AgentChat answer gave you the key, write it alone into a file with your file-editing tool; if ${PERSON} holds it, ask them to save it into a file and tell you where (never to paste it here). Then run \`${inv} login --api-key - < <that file>\` and delete the file.`,
|
|
6057
|
+
{ command: register }
|
|
6058
|
+
);
|
|
6059
|
+
}
|
|
6060
|
+
const what = pending.kind === "recover" ? `a recovery${pending.handle !== void 0 ? ` for @${pending.handle}` : ` of the account on ${pending.email}`}` : `a registration of @${pending.handle ?? "?"}`;
|
|
6061
|
+
if (pending.flow.next_step === VERIFY_STEP[pending.kind] && pending.flow.pending_id !== void 0 && codeMayWork(pending, now)) {
|
|
6062
|
+
const command2 = codeCommandOf(inv, pending.kind, pending.flow);
|
|
6063
|
+
const code = pending.flow.code_length !== void 0 ? `the ${pending.flow.code_length}-digit code` : "the code";
|
|
6064
|
+
const left = minutesLeft(pending, now);
|
|
6065
|
+
return verdict(
|
|
6066
|
+
"code_pending",
|
|
6067
|
+
`${what} is waiting on ${code} AgentChat emailed to ${pending.email}${left !== null ? `; it expires in ${left} minute${left === 1 ? "" : "s"}` : ""}. Run \`${command2}\` with it. If you cannot read that inbox, ask ${PERSON} for the code.`,
|
|
6068
|
+
{ command: command2, ask: `AgentChat emailed a code to ${pending.email}. What is it?` }
|
|
6069
|
+
);
|
|
6070
|
+
}
|
|
6071
|
+
const command = startCommandOf(inv, pending.kind, pending);
|
|
6072
|
+
const wait = resendLeftS(pending, now);
|
|
6073
|
+
return verdict(
|
|
6074
|
+
"code_expired",
|
|
6075
|
+
`the code AgentChat emailed for ${what} has expired. For a new one, ${wait > 0 ? `wait ${wait} s, then run` : "run"} \`${command}\`.`,
|
|
6076
|
+
{ command }
|
|
6077
|
+
);
|
|
6078
|
+
}
|
|
6079
|
+
function setupVerdict(i) {
|
|
6080
|
+
const now = i.now ?? Date.now();
|
|
6081
|
+
const inv = i.invocation;
|
|
6082
|
+
if (i.identity === null) return accountVerdict(i, now);
|
|
6083
|
+
const handle = i.server?.kind === "ok" ? i.server.handle : i.identity.handle;
|
|
6084
|
+
const who = handle !== null ? `@${handle}` : "this agent";
|
|
6085
|
+
const server = i.server;
|
|
6086
|
+
if (server?.kind === "suspended") {
|
|
6087
|
+
return verdict(
|
|
6088
|
+
"suspended",
|
|
6089
|
+
`AgentChat has suspended ${who} (${server.detail}): nothing on this computer changes that, and recovering the account does not help. ${PERSON[0].toUpperCase()}${PERSON.slice(1)} emails support@agentchat.me.`,
|
|
6090
|
+
{ needs_person: true, ask: `AgentChat has suspended ${who}. Could you email support@agentchat.me about it?`, detail: server.detail }
|
|
6091
|
+
);
|
|
6092
|
+
}
|
|
6093
|
+
if (server?.kind === "refused") {
|
|
6094
|
+
const pending = i.pending;
|
|
6095
|
+
if (pending !== null && pending.kind === "recover" && pending.flow.next_step === VERIFY_STEP.recover && codeMayWork(pending, now)) {
|
|
6096
|
+
const command = codeCommandOf(inv, "recover", pending.flow);
|
|
6097
|
+
return verdict(
|
|
6098
|
+
"key_refused",
|
|
6099
|
+
`AgentChat refuses the key stored for ${who} (${server.detail}), and a recovery code was emailed to ${pending.email}: run \`${command}\` with it. If you cannot read that inbox, ask ${PERSON} for the code.`,
|
|
6100
|
+
{ command, ask: `AgentChat emailed a code to ${pending.email}. What is it?`, detail: server.detail, reason: "recovery_code_waiting" }
|
|
6101
|
+
);
|
|
6102
|
+
}
|
|
6103
|
+
const recover = `${inv} recover --handle ${handle ?? "<handle>"} --email <email>`;
|
|
6104
|
+
return verdict(
|
|
6105
|
+
"key_refused",
|
|
6106
|
+
`AgentChat refuses the key stored for ${who} (${server.detail}). Ask ${PERSON} whether they have its current key: if they do, they save it into a file and you run \`${inv} login --api-key - < <that file>\`; if not, \`${recover}\` emails a code and replaces the key, and anything still using the old key stops working.`,
|
|
6107
|
+
{
|
|
6108
|
+
command: recover,
|
|
6109
|
+
needs_person: true,
|
|
6110
|
+
ask: `AgentChat refuses ${who}'s key. Do you have its current API key? If not, may I recover the account by email? That replaces the key, and anything still using the old one stops working.`,
|
|
6111
|
+
detail: server.detail
|
|
6112
|
+
}
|
|
6113
|
+
);
|
|
6114
|
+
}
|
|
6115
|
+
if (server?.kind === "unreachable") {
|
|
6116
|
+
return verdict("unreachable", `AgentChat could not be reached from this computer (${server.detail}). Check the network, then run \`${inv} status\` again.`, {
|
|
6117
|
+
command: `${inv} status`,
|
|
6118
|
+
detail: server.detail
|
|
6119
|
+
});
|
|
6120
|
+
}
|
|
6121
|
+
const reach = server?.kind === "ok" ? server.reachability : null;
|
|
6122
|
+
if (reach?.state === "paused") {
|
|
6123
|
+
return verdict(
|
|
6124
|
+
"paused",
|
|
6125
|
+
`${reach.text ?? `the person who claimed ${who} paused it on AgentChat, so nothing reaches it until they resume it.`} Only they can resume it: ask them.`,
|
|
6126
|
+
{ needs_person: true, ask: `${who} is paused on AgentChat, so other agents' messages do not reach it. Could you resume it?`, detail: reach.text }
|
|
6127
|
+
);
|
|
6128
|
+
}
|
|
6129
|
+
const tools = i.tools ?? null;
|
|
6130
|
+
if (tools !== null) {
|
|
6131
|
+
return verdict("tools_missing", tools.text, {
|
|
6132
|
+
command: tools.command ?? (tools.needsPerson === true ? null : `${inv} doctor --fix`),
|
|
6133
|
+
needs_person: tools.needsPerson === true,
|
|
6134
|
+
ask: tools.ask ?? null,
|
|
6135
|
+
detail: tools.detail ?? null
|
|
6136
|
+
});
|
|
6137
|
+
}
|
|
6138
|
+
const r = i.listener;
|
|
6139
|
+
if (r === null) return null;
|
|
6140
|
+
return listenerVerdict(i, r, who, reach);
|
|
6141
|
+
}
|
|
6142
|
+
function listenerVerdict(i, r, who, reach) {
|
|
6143
|
+
const inv = i.invocation;
|
|
6144
|
+
const label = i.label;
|
|
6145
|
+
const enable = `${inv} daemon install`;
|
|
6146
|
+
const ensure = i.ensure ?? null;
|
|
6147
|
+
if (r.optedOut || ensure?.outcome === "opted_out") {
|
|
6148
|
+
return verdict(
|
|
6149
|
+
"listener_off",
|
|
6150
|
+
`AgentChat's listener for ${label} is switched off on this computer, so a peer's message does not start a run here; messages wait on AgentChat. Only ${PERSON} turns it back on: ask them, and if they agree, run \`${enable}\`.`,
|
|
6151
|
+
{
|
|
6152
|
+
command: enable,
|
|
6153
|
+
needs_person: true,
|
|
6154
|
+
ask: `AgentChat's listener is off on this computer, so other agents' messages to ${who} wait unanswered. May I turn it back on? It starts a ${label} run here when one of them writes.`,
|
|
6155
|
+
reason: "opted_out"
|
|
6156
|
+
}
|
|
6157
|
+
);
|
|
6158
|
+
}
|
|
6159
|
+
if (r.daemon.kind === "off") {
|
|
6160
|
+
return verdict(
|
|
6161
|
+
"listener_off",
|
|
6162
|
+
`the listener on this computer was installed with ${INTEGRATION_ENV}=off, so it holds no connection and starts no run; messages wait on AgentChat. Ask ${PERSON}; if they agree, run \`${enable}\` from a shell where ${INTEGRATION_ENV} is not set.`,
|
|
6163
|
+
{
|
|
6164
|
+
command: enable,
|
|
6165
|
+
needs_person: true,
|
|
6166
|
+
ask: `AgentChat's listener on this computer was installed switched off (${INTEGRATION_ENV}=off), so other agents' messages to ${who} wait unanswered. May I turn it on?`,
|
|
6167
|
+
reason: "switched_off"
|
|
6168
|
+
}
|
|
6169
|
+
);
|
|
6170
|
+
}
|
|
6171
|
+
if (ensure?.outcome === "failed") {
|
|
6172
|
+
return verdict(
|
|
6173
|
+
"listener_missing",
|
|
6174
|
+
`the listener could not be installed (${ensure.detail ?? "no reason given"}). Run \`${enable}\` to try again; if it fails the same way, show ${PERSON} this line (${label} may ask them to approve the command).`,
|
|
6175
|
+
{ command: enable, detail: ensure.detail, reason: "register_failed" }
|
|
6176
|
+
);
|
|
6177
|
+
}
|
|
6178
|
+
if (!r.wanted) {
|
|
6179
|
+
return verdict("listener_missing", `nothing on this computer listens for ${who}, so a peer's message cannot start a run here. Run \`${inv}\` to install the listener.`, { command: inv, reason: "not_registered" });
|
|
6180
|
+
}
|
|
6181
|
+
if (i.identity?.source === "env") {
|
|
6182
|
+
const command = `printenv AGENTCHAT_API_KEY | ${inv} login --api-key -`;
|
|
6183
|
+
return verdict(
|
|
6184
|
+
"listener_failing",
|
|
6185
|
+
`the key is only in AGENTCHAT_API_KEY, and the listener reads only ${credentialsPath(i.home)}. Save it there: run \`${command}\`.`,
|
|
6186
|
+
{ command, reason: "env_key_only" }
|
|
6187
|
+
);
|
|
6188
|
+
}
|
|
6189
|
+
const stale = newerRelease(i.version, r.installedVersion);
|
|
6190
|
+
const staleNote = stale ? `the listener installed here is ${r.installedVersion}; \`${inv}\` updates it to ${i.version}` : null;
|
|
6191
|
+
const d = r.daemon;
|
|
6192
|
+
switch (d.kind) {
|
|
6193
|
+
case "ready": {
|
|
6194
|
+
if (d.runsFailing !== null) {
|
|
6195
|
+
const kind = listenerReasonKindOf(d.runsFailing.reason);
|
|
6196
|
+
return failing(i, who, kind === "other" && d.runtimeCheckFailing ? "runtime_check_failed" : kind, d.runsFailing.reason, true, d.runsFailing.since, "runs_failing");
|
|
6197
|
+
}
|
|
6198
|
+
const server = serverSays(i, who, reach);
|
|
6199
|
+
if (server !== null) return server;
|
|
6200
|
+
return verdict("ready", `${who} is listening on this computer: a peer's message starts a ${label} run here.${staleNote !== null ? ` (${staleNote}.)` : ""}${readyTail(i)}`, { detail: staleNote, reason: staleNote !== null ? "older_listener" : null });
|
|
6201
|
+
}
|
|
6202
|
+
case "standby": {
|
|
6203
|
+
const other = `${d.home ?? "another identity home"}${d.pid !== null ? `, pid ${d.pid}` : ""}`;
|
|
6204
|
+
const holder = d.home !== null ? readListener(d.home, i.server?.kind === "ok" ? i.server.handle : i.identity?.handle ?? null, { now: i.now ?? Date.now() }).daemon : null;
|
|
6205
|
+
if (holder === null || holder.kind !== "ready") {
|
|
6206
|
+
return starting(i, who, ` (another listener on this computer holds it, ${other}, and does not say it is connected)`, `another listener on this computer holds ${who}: ${other}`, "standby_unconfirmed");
|
|
6207
|
+
}
|
|
6208
|
+
const server = serverSays(i, who, reach);
|
|
6209
|
+
if (server !== null) return server;
|
|
6210
|
+
return verdict(
|
|
6211
|
+
"ready",
|
|
6212
|
+
`${who} is listening on this computer through another listener (${other}): a peer's message starts a run there, and this ${label} listener stands by until it stops.${readyTail(i)}`,
|
|
6213
|
+
{ detail: [`another listener on this computer serves ${who} from ${d.home}`, ...staleNote !== null ? [staleNote] : []].join("; "), reason: "standby" }
|
|
6214
|
+
);
|
|
6215
|
+
}
|
|
6216
|
+
case "unservable":
|
|
6217
|
+
return failing(i, who, d.reason === "runs_failing" ? listenerReasonKindOf(d.detail) : "other", d.detail, false, null, "unservable");
|
|
6218
|
+
case "start_failure":
|
|
6219
|
+
return failing(i, who, listenerReasonKindOf(d.detail), d.detail, false, null, "start_failed");
|
|
6220
|
+
case "blocked":
|
|
6221
|
+
return failing(i, who, "other", d.detail ?? "the server refused its connection", false, null, "blocked");
|
|
6222
|
+
case "connecting":
|
|
6223
|
+
return starting(i, who, "", null);
|
|
6224
|
+
case "none": {
|
|
6225
|
+
if (r.withinGrace || ensure?.outcome === "installed" || r.alive) return starting(i, who, "", null);
|
|
6226
|
+
if (stale) {
|
|
6227
|
+
return verdict(
|
|
6228
|
+
"listener_missing",
|
|
6229
|
+
`the listener installed here is from an older release (${r.installedVersion}; this is ${i.version}) and is not connected. Run \`${inv}\` to update it.`,
|
|
6230
|
+
{ command: inv, detail: staleNote, reason: "older_listener" }
|
|
6231
|
+
);
|
|
6232
|
+
}
|
|
6233
|
+
return verdict(
|
|
6234
|
+
"listener_down",
|
|
6235
|
+
`the listener for ${who} is installed but not running${r.lastAliveAt !== null ? ` (last sign of life ${r.lastAliveAt})` : ""}. Run \`${enable}\` to start it again, then \`${inv} status\`.`,
|
|
6236
|
+
{ command: enable }
|
|
6237
|
+
);
|
|
6238
|
+
}
|
|
6239
|
+
}
|
|
6240
|
+
}
|
|
6241
|
+
function readyTail(i) {
|
|
6242
|
+
return i.afterSignIn === true ? ` Last, tell ${PERSON}: open a new ${i.label} session to have the AgentChat tools in this chat; receiving does not need it.` : ` A new ${i.label} session has the AgentChat tools in its chat; receiving does not need one.`;
|
|
6243
|
+
}
|
|
6244
|
+
function serverSays(i, who, reach) {
|
|
6245
|
+
if (reach === null || reach.state === "ready" && reach.via === "socket") return null;
|
|
6246
|
+
const inv = i.invocation;
|
|
6247
|
+
if (reach.state === "failing" && reach.via === "socket") {
|
|
6248
|
+
const text = reach.problemText ?? reach.text ?? "AgentChat says the listener cannot start runs.";
|
|
6249
|
+
return verdict("listener_failing", `${text} Then run \`${inv} status\`.`, {
|
|
6250
|
+
command: `${inv} status`,
|
|
6251
|
+
needs_person: true,
|
|
6252
|
+
ask: `AgentChat says about the listener on this computer: "${text}" Could you look at it?`,
|
|
6253
|
+
detail: text,
|
|
6254
|
+
reason: reach.problemKind !== null && /^[a-z][a-z0-9_]{0,31}$/.test(reach.problemKind) ? reach.problemKind : "server_failing"
|
|
6255
|
+
});
|
|
6256
|
+
}
|
|
6257
|
+
return starting(i, who, " (it says it is connected; AgentChat does not see the connection yet)", reach.text, "not_seen");
|
|
6258
|
+
}
|
|
6259
|
+
function starting(i, who, why, detail, reason = null) {
|
|
6260
|
+
const inv = i.invocation;
|
|
6261
|
+
return verdict(
|
|
6262
|
+
"listener_starting",
|
|
6263
|
+
`the listener is connecting as ${who}${why}. Wait 10 s, then run \`${inv} status\`; if it still says starting after a minute, run \`${inv} doctor\`.`,
|
|
6264
|
+
{ command: `${inv} status`, detail, reason }
|
|
6265
|
+
);
|
|
6266
|
+
}
|
|
6267
|
+
function failing(i, who, kind, detail, connected, since, otherReason) {
|
|
6268
|
+
const inv = i.invocation;
|
|
6269
|
+
const label = i.label;
|
|
6270
|
+
const status = `${inv} status`;
|
|
6271
|
+
switch (kind) {
|
|
6272
|
+
case "not_signed_in":
|
|
6273
|
+
return verdict(
|
|
6274
|
+
"listener_failing",
|
|
6275
|
+
`the listener for ${who} cannot start ${label} runs: ${label} is not signed in for it (${detail}). Only ${PERSON} can sign it in: ask them to sign ${label} in on this computer (the words in brackets say how), then run \`${status}\`.`,
|
|
6276
|
+
{
|
|
6277
|
+
command: status,
|
|
6278
|
+
needs_person: true,
|
|
6279
|
+
ask: `${label} is not signed in for AgentChat's listener on this computer, so it cannot answer other agents. Could you sign it in? Its check says: ${detail}`,
|
|
6280
|
+
detail,
|
|
6281
|
+
reason: kind
|
|
6282
|
+
}
|
|
6283
|
+
);
|
|
6284
|
+
case "runtime_missing":
|
|
6285
|
+
return verdict(
|
|
6286
|
+
"listener_failing",
|
|
6287
|
+
`the listener for ${who} cannot find ${label} (${detail}). Ask ${PERSON} to install ${label} where the listener finds it, then run \`${inv} daemon install\`.`,
|
|
6288
|
+
{ command: `${inv} daemon install`, needs_person: true, ask: `AgentChat's listener cannot find ${label} on this computer. Could you install it, or tell me where it is?`, detail, reason: kind }
|
|
6289
|
+
);
|
|
6290
|
+
case "runtime_outdated":
|
|
6291
|
+
return verdict("listener_failing", `${label} on this computer is older than AgentChat needs (${detail}). Ask ${PERSON} to update ${label}, then run \`${status}\`.`, {
|
|
6292
|
+
command: status,
|
|
6293
|
+
needs_person: true,
|
|
6294
|
+
ask: `AgentChat needs a newer ${label} on this computer. Could you update it?`,
|
|
6295
|
+
detail,
|
|
6296
|
+
reason: kind
|
|
6297
|
+
});
|
|
6298
|
+
case "runtime_check_failed":
|
|
6299
|
+
case "other":
|
|
6300
|
+
if (connected) {
|
|
6301
|
+
return verdict(
|
|
6302
|
+
"listener_failing",
|
|
6303
|
+
`the listener for ${who} is connected, but ${label} runs fail (${detail}${since !== null ? `; since ${since}` : ""}), which usually means a usage limit or a provider outage. Ask ${PERSON} to check ${label}'s account and limits on this computer.`,
|
|
6304
|
+
{ command: status, needs_person: true, ask: `Runs of ${label} started by AgentChat keep failing on this computer (${detail}). Could you check its account and usage limits?`, detail, reason: kind === "runtime_check_failed" ? kind : otherReason }
|
|
6305
|
+
);
|
|
6306
|
+
}
|
|
6307
|
+
return verdict("listener_failing", `the listener for ${who} cannot start runs: ${detail}. Run \`${inv} doctor\` and follow what it says.`, { command: `${inv} doctor`, detail, reason: kind === "runtime_check_failed" ? kind : otherReason });
|
|
6308
|
+
}
|
|
6309
|
+
}
|
|
6310
|
+
function serviceDryRun(env = process.env) {
|
|
6311
|
+
const v = env["AGENTCHAT_SERVICE_DRY_RUN"];
|
|
6312
|
+
return v !== void 0 && v !== "" && v !== "0";
|
|
6313
|
+
}
|
|
6314
|
+
|
|
5679
6315
|
export {
|
|
5680
6316
|
TEXTS_CACHE_FILE,
|
|
5681
6317
|
textsCachePath,
|
|
@@ -5693,6 +6329,15 @@ export {
|
|
|
5693
6329
|
resolveIdentity,
|
|
5694
6330
|
envKeyConflict,
|
|
5695
6331
|
writeCredentials,
|
|
6332
|
+
INSTALL_ID_FILE,
|
|
6333
|
+
installIdPath,
|
|
6334
|
+
readInstallId,
|
|
6335
|
+
newInstallId,
|
|
6336
|
+
ensureInstallId,
|
|
6337
|
+
HOST_FILE,
|
|
6338
|
+
readRememberedHost,
|
|
6339
|
+
rememberHost,
|
|
6340
|
+
clearInstallId,
|
|
5696
6341
|
clearCredentials,
|
|
5697
6342
|
readPending,
|
|
5698
6343
|
writePending,
|
|
@@ -5759,20 +6404,6 @@ export {
|
|
|
5759
6404
|
repeatedFailedRuns,
|
|
5760
6405
|
formatIntegrationStatus,
|
|
5761
6406
|
isRunnerFactory,
|
|
5762
|
-
HEARTBEAT_FILE,
|
|
5763
|
-
markAlwaysOnWanted,
|
|
5764
|
-
clearAlwaysOnWanted,
|
|
5765
|
-
alwaysOnWanted,
|
|
5766
|
-
readAlwaysOnInstalledVersion,
|
|
5767
|
-
markAlwaysOnInstalledVersion,
|
|
5768
|
-
clearAlwaysOnInstalledVersion,
|
|
5769
|
-
markAlwaysOnOptOut,
|
|
5770
|
-
clearAlwaysOnOptOut,
|
|
5771
|
-
alwaysOnOptedOut,
|
|
5772
|
-
beat,
|
|
5773
|
-
idle,
|
|
5774
|
-
alwaysOnState,
|
|
5775
|
-
alwaysOnHealth,
|
|
5776
6407
|
LEASE_RENEW_MS,
|
|
5777
6408
|
LEASE_STALE_MS,
|
|
5778
6409
|
bootedAt,
|
|
@@ -5782,8 +6413,6 @@ export {
|
|
|
5782
6413
|
acquireLease,
|
|
5783
6414
|
leaseHolder,
|
|
5784
6415
|
resetLeaseObservations,
|
|
5785
|
-
DAEMON_LOCK_FILE,
|
|
5786
|
-
acquireLeaderLock,
|
|
5787
6416
|
AGENT_LOCK_DIR_ENV,
|
|
5788
6417
|
agentLockDir,
|
|
5789
6418
|
agentLockPath,
|
|
@@ -5793,6 +6422,49 @@ export {
|
|
|
5793
6422
|
standbyMarkerPath,
|
|
5794
6423
|
markStandby,
|
|
5795
6424
|
clearStandby,
|
|
5796
|
-
standbyWaiting
|
|
6425
|
+
standbyWaiting,
|
|
6426
|
+
LISTENER_ID_FILE,
|
|
6427
|
+
listenerIdPath,
|
|
6428
|
+
ensureListenerInstanceId,
|
|
6429
|
+
readListenerInstanceId,
|
|
6430
|
+
STOPPED_REPORT_TIMEOUT_MS,
|
|
6431
|
+
createListenerReporter,
|
|
6432
|
+
tellListener,
|
|
6433
|
+
listenerReportStatusOf,
|
|
6434
|
+
reportListenerStopped,
|
|
6435
|
+
describeListenerStopped,
|
|
6436
|
+
HEARTBEAT_FILE,
|
|
6437
|
+
markAlwaysOnWanted,
|
|
6438
|
+
clearAlwaysOnWanted,
|
|
6439
|
+
alwaysOnWanted,
|
|
6440
|
+
readAlwaysOnInstalledVersion,
|
|
6441
|
+
markAlwaysOnInstalledVersion,
|
|
6442
|
+
clearAlwaysOnInstalledVersion,
|
|
6443
|
+
markAlwaysOnOptOut,
|
|
6444
|
+
clearAlwaysOnOptOut,
|
|
6445
|
+
alwaysOnOptedOut,
|
|
6446
|
+
beat,
|
|
6447
|
+
idle,
|
|
6448
|
+
alwaysOnState,
|
|
6449
|
+
alwaysOnHealth,
|
|
6450
|
+
DAEMON_LOCK_FILE,
|
|
6451
|
+
acquireLeaderLock,
|
|
6452
|
+
SETUP_GRACE_MS,
|
|
6453
|
+
SETUP_STATES,
|
|
6454
|
+
formatSetupVerdict,
|
|
6455
|
+
VERIFY_STEP,
|
|
6456
|
+
shellValue,
|
|
6457
|
+
codeMayWork,
|
|
6458
|
+
resendLeftS,
|
|
6459
|
+
runLine,
|
|
6460
|
+
startCommandOf,
|
|
6461
|
+
codeCommandOf,
|
|
6462
|
+
reachabilityOf,
|
|
6463
|
+
daemonPidOf,
|
|
6464
|
+
readListener,
|
|
6465
|
+
listenerReasonKindOf,
|
|
6466
|
+
newerRelease,
|
|
6467
|
+
setupVerdict,
|
|
6468
|
+
serviceDryRun
|
|
5797
6469
|
};
|
|
5798
|
-
//# sourceMappingURL=chunk-
|
|
6470
|
+
//# sourceMappingURL=chunk-6GMYBMID.js.map
|