@agentchatme/agent-core 0.7.0 → 0.7.3
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 +35 -11
- package/dist/{chunk-C2ARQLZ7.js → chunk-5KMQ23O6.js} +11 -7
- package/dist/chunk-5KMQ23O6.js.map +1 -0
- package/dist/{chunk-PTKQIKCI.js → chunk-7TTV2CQX.js} +3 -3
- package/dist/{chunk-PTKQIKCI.js.map → chunk-7TTV2CQX.js.map} +1 -1
- package/dist/{chunk-NYSMAN4N.js → chunk-FPAOQGMV.js} +534 -78
- package/dist/chunk-FPAOQGMV.js.map +1 -0
- package/dist/daemon-entry.d.ts +56 -7
- package/dist/daemon-entry.js +81 -10
- package/dist/daemon-entry.js.map +1 -1
- package/dist/index.d.ts +143 -51
- package/dist/index.js +180 -309
- package/dist/index.js.map +1 -1
- package/dist/presets.d.ts +65 -19
- package/dist/presets.js +66 -9
- package/dist/presets.js.map +1 -1
- package/dist/{runner-BjCgl4bV.d.ts → runner-BLvFalN4.d.ts} +17 -5
- package/dist/{served-manual-RAIaRcr2.d.ts → served-manual-CwxBzpwQ.d.ts} +199 -4
- package/package.json +2 -2
- package/dist/chunk-C2ARQLZ7.js.map +0 -1
- package/dist/chunk-NYSMAN4N.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AGENT_LOCK_DIR_ENV,
|
|
3
|
+
ANCHOR_END,
|
|
4
|
+
ANCHOR_START,
|
|
3
5
|
COULD_NOT_PROCESS_FILE,
|
|
4
6
|
DAEMON_LOCK_FILE,
|
|
5
7
|
DEFAULT_API_BASE,
|
|
6
8
|
FAILED_CYCLE_OUTCOMES,
|
|
7
9
|
HEARTBEAT_FILE,
|
|
10
|
+
IDENTITY_ANCHOR_ATTEMPTS,
|
|
8
11
|
INTEGRATION_STATUS_FILE,
|
|
12
|
+
INTEGRATION_STATUS_STALE_MS,
|
|
9
13
|
LEASE_RENEW_MS,
|
|
10
14
|
LEASE_STALE_MS,
|
|
11
15
|
LEGACY_STATUS_FILE,
|
|
@@ -31,6 +35,7 @@ import {
|
|
|
31
35
|
alwaysOnOptedOut,
|
|
32
36
|
alwaysOnState,
|
|
33
37
|
alwaysOnWanted,
|
|
38
|
+
anchorIsCurrentAt,
|
|
34
39
|
beat,
|
|
35
40
|
bootedAt,
|
|
36
41
|
checkManual,
|
|
@@ -44,6 +49,7 @@ import {
|
|
|
44
49
|
couldNotProcessPath,
|
|
45
50
|
credentialsPath,
|
|
46
51
|
describeCouldNotProcessCounts,
|
|
52
|
+
describeIdentityBlock,
|
|
47
53
|
describeManualSync,
|
|
48
54
|
describePausedConversations,
|
|
49
55
|
envApiKey,
|
|
@@ -52,14 +58,20 @@ import {
|
|
|
52
58
|
failedCyclesInARow,
|
|
53
59
|
fetchIdentityLine,
|
|
54
60
|
fetchServedTexts,
|
|
61
|
+
followIdentityAnchor,
|
|
55
62
|
formatIntegrationStatus,
|
|
56
63
|
fromKitResult,
|
|
64
|
+
hasAnchorAt,
|
|
57
65
|
holderLive,
|
|
66
|
+
identityBlockState,
|
|
58
67
|
identityLineOf,
|
|
59
68
|
idle,
|
|
60
69
|
integrationStatusAge,
|
|
70
|
+
integrationStatusFresh,
|
|
61
71
|
integrationStatusPath,
|
|
72
|
+
integrationStatusStaleFor,
|
|
62
73
|
isRunnerFactory,
|
|
74
|
+
isSetupOfferBody,
|
|
63
75
|
leaseHolder,
|
|
64
76
|
loadTexts,
|
|
65
77
|
manualFrontMatter,
|
|
@@ -73,25 +85,38 @@ import {
|
|
|
73
85
|
parseLease,
|
|
74
86
|
pendingPath,
|
|
75
87
|
pidAlive,
|
|
88
|
+
placeAnchorAt,
|
|
76
89
|
readAlwaysOnInstalledVersion,
|
|
90
|
+
readAnchorHandleAt,
|
|
91
|
+
readAnchorHandleFrom,
|
|
77
92
|
readCachedTexts,
|
|
78
93
|
readCredentials,
|
|
79
94
|
readIntegrationStatus,
|
|
80
95
|
readManualVersion,
|
|
81
96
|
readPending,
|
|
97
|
+
removeAnchorAt,
|
|
98
|
+
renderAnchorBlock,
|
|
99
|
+
renderDeclinedBlock,
|
|
100
|
+
renderUnregisteredBlock,
|
|
82
101
|
repeatedFailedRuns,
|
|
83
102
|
resetLeaseObservations,
|
|
84
103
|
resolveIdentity,
|
|
104
|
+
resolveManualPointer,
|
|
85
105
|
runtimeCheckFailingNow,
|
|
106
|
+
servedManualUrl,
|
|
86
107
|
standbyMarkerPath,
|
|
87
108
|
standbyWaiting,
|
|
88
109
|
statePath,
|
|
110
|
+
stripAnchorBlock,
|
|
89
111
|
syncManual,
|
|
90
112
|
textsCachePath,
|
|
113
|
+
upsertAnchorBlock,
|
|
114
|
+
writeAnchor,
|
|
91
115
|
writeCredentials,
|
|
92
116
|
writeIntegrationStatus,
|
|
117
|
+
writeOfferAt,
|
|
93
118
|
writePending
|
|
94
|
-
} from "./chunk-
|
|
119
|
+
} from "./chunk-FPAOQGMV.js";
|
|
95
120
|
import {
|
|
96
121
|
HOSTED_MCP_PATH,
|
|
97
122
|
PROBE_KILL_WAIT_MS,
|
|
@@ -99,7 +124,7 @@ import {
|
|
|
99
124
|
hostedMcpUrl,
|
|
100
125
|
probeHostedMcp,
|
|
101
126
|
runProbe
|
|
102
|
-
} from "./chunk-
|
|
127
|
+
} from "./chunk-7TTV2CQX.js";
|
|
103
128
|
import {
|
|
104
129
|
DEFAULT_RUN_BUDGET_MS,
|
|
105
130
|
DEFAULT_RUN_PROFILE,
|
|
@@ -130,7 +155,7 @@ import {
|
|
|
130
155
|
spawnCommand,
|
|
131
156
|
spawnCommandSync,
|
|
132
157
|
workdirExposesHome
|
|
133
|
-
} from "./chunk-
|
|
158
|
+
} from "./chunk-5KMQ23O6.js";
|
|
134
159
|
|
|
135
160
|
// src/integration/tray.ts
|
|
136
161
|
import { INBOX_TRAY_MAX_LIMIT } from "agentchatme";
|
|
@@ -176,201 +201,6 @@ function clearOfferDeclined(home) {
|
|
|
176
201
|
writeState(home, state);
|
|
177
202
|
}
|
|
178
203
|
|
|
179
|
-
// src/anchor/block.ts
|
|
180
|
-
import * as fs from "fs";
|
|
181
|
-
import * as path from "path";
|
|
182
|
-
import { integration as kit } from "agentchatme";
|
|
183
|
-
var ANCHOR_START = kit.anchor.START_MARKER;
|
|
184
|
-
var ANCHOR_END = kit.anchor.END_MARKER;
|
|
185
|
-
var MARKER_LINES = /* @__PURE__ */ new Set([
|
|
186
|
-
kit.anchor.START_MARKER,
|
|
187
|
-
kit.anchor.END_MARKER,
|
|
188
|
-
kit.anchor.LEGACY_START_MARKER,
|
|
189
|
-
kit.anchor.LEGACY_END_MARKER
|
|
190
|
-
]);
|
|
191
|
-
function renderAnchorBlock(identityLine, handle) {
|
|
192
|
-
if (typeof identityLine !== "string" || identityLine.trim().length === 0) throw new Error("renderAnchorBlock: the identity line is empty");
|
|
193
|
-
if (typeof handle !== "string" || kit.anchor.normalizeHandle(handle).length === 0) throw new Error("renderAnchorBlock: the handle is empty");
|
|
194
|
-
if (identityLine.split(/\r?\n/).some((line) => MARKER_LINES.has(line.trim()))) {
|
|
195
|
-
throw new Error("renderAnchorBlock: the identity line holds an AgentChat marker line");
|
|
196
|
-
}
|
|
197
|
-
return kit.anchor.render({ identity_line: identityLine, identity_line_template: "" }, handle);
|
|
198
|
-
}
|
|
199
|
-
function writeAnchor(filePath, block, expectHandle) {
|
|
200
|
-
if (expectHandle !== void 0) {
|
|
201
|
-
const bare = kit.anchor.normalizeHandle(expectHandle);
|
|
202
|
-
const claimed = kit.anchor.find(block)?.handle ?? null;
|
|
203
|
-
if (claimed !== bare) {
|
|
204
|
-
throw new Error(`writeAnchor: the block for ${filePath} does not name @${bare} \u2014 nothing was written`);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
const existing = readInstructionFile(filePath) ?? { bom: "", text: "" };
|
|
208
|
-
const next = untilStable(existing.text, (text) => kit.anchor.replace(text, block));
|
|
209
|
-
if (next !== existing.text) writeInstructionFile(filePath, existing.bom + next);
|
|
210
|
-
if (expectHandle !== void 0 && readInstructionFile(filePath)?.text !== next) {
|
|
211
|
-
throw new Error(`writeAnchor: ${filePath} did not read back as written \u2014 remove the agentchat block manually and re-run`);
|
|
212
|
-
}
|
|
213
|
-
return "written";
|
|
214
|
-
}
|
|
215
|
-
function untilStable(text, step) {
|
|
216
|
-
let current = text;
|
|
217
|
-
for (let i = 0; i < 8; i++) {
|
|
218
|
-
const next = step(current);
|
|
219
|
-
if (next === current) return current;
|
|
220
|
-
current = next;
|
|
221
|
-
}
|
|
222
|
-
return current;
|
|
223
|
-
}
|
|
224
|
-
function removeAnchorAt(filePath) {
|
|
225
|
-
const existing = readInstructionFile(filePath);
|
|
226
|
-
if (existing === null) return "noop";
|
|
227
|
-
const next = untilStable(existing.text, (text) => kit.anchor.remove(text));
|
|
228
|
-
if (next === existing.text) return "noop";
|
|
229
|
-
writeInstructionFile(filePath, existing.bom + next);
|
|
230
|
-
return "removed";
|
|
231
|
-
}
|
|
232
|
-
function hasAnchorAt(filePath) {
|
|
233
|
-
const file = readInstructionFile(filePath);
|
|
234
|
-
return file !== null && kit.anchor.find(file.text) !== null;
|
|
235
|
-
}
|
|
236
|
-
function anchorIsCurrentAt(filePath, identityLine, handle) {
|
|
237
|
-
const file = readInstructionFile(filePath);
|
|
238
|
-
if (file === null) return false;
|
|
239
|
-
let expected;
|
|
240
|
-
try {
|
|
241
|
-
expected = renderAnchorBlock(identityLine, handle);
|
|
242
|
-
} catch {
|
|
243
|
-
return false;
|
|
244
|
-
}
|
|
245
|
-
if (kit.anchor.find(file.text) === null) return false;
|
|
246
|
-
return kit.anchor.replace(file.text, expected) === file.text;
|
|
247
|
-
}
|
|
248
|
-
function readAnchorHandleAt(filePath, template) {
|
|
249
|
-
const file = readInstructionFile(filePath);
|
|
250
|
-
return file === null ? null : readAnchorHandleFrom(file.text, template);
|
|
251
|
-
}
|
|
252
|
-
function readAnchorHandleFrom(text, template) {
|
|
253
|
-
return kit.anchor.find(text.replace(/^\uFEFF/, ""), { template })?.handle ?? null;
|
|
254
|
-
}
|
|
255
|
-
function upsertAnchorBlock(existing, block) {
|
|
256
|
-
return kit.anchor.replace(existing, block);
|
|
257
|
-
}
|
|
258
|
-
function stripAnchorBlock(existing) {
|
|
259
|
-
return kit.anchor.remove(existing);
|
|
260
|
-
}
|
|
261
|
-
var UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
262
|
-
function readInstructionFile(filePath) {
|
|
263
|
-
let bytes;
|
|
264
|
-
try {
|
|
265
|
-
bytes = fs.readFileSync(filePath);
|
|
266
|
-
} catch (err) {
|
|
267
|
-
if (err.code === "ENOENT") return null;
|
|
268
|
-
throw err;
|
|
269
|
-
}
|
|
270
|
-
let decoded;
|
|
271
|
-
try {
|
|
272
|
-
decoded = UTF8.decode(bytes);
|
|
273
|
-
} catch {
|
|
274
|
-
throw new Error(`${filePath} is not UTF-8 text; the AgentChat block is not read or written there (save the file as UTF-8 and run the command again)`);
|
|
275
|
-
}
|
|
276
|
-
return decoded.startsWith("\uFEFF") ? { bom: "\uFEFF", text: decoded.slice(1) } : { bom: "", text: decoded };
|
|
277
|
-
}
|
|
278
|
-
function resolveWriteTarget(filePath) {
|
|
279
|
-
let current = filePath;
|
|
280
|
-
for (let hops = 0; hops < 40; hops++) {
|
|
281
|
-
let stat;
|
|
282
|
-
try {
|
|
283
|
-
stat = fs.lstatSync(current);
|
|
284
|
-
} catch (err) {
|
|
285
|
-
if (err.code === "ENOENT") return current;
|
|
286
|
-
throw err;
|
|
287
|
-
}
|
|
288
|
-
if (!stat.isSymbolicLink()) return current;
|
|
289
|
-
const link = fs.readlinkSync(current);
|
|
290
|
-
const joined = path.isAbsolute(link) ? link : `${fs.realpathSync.native(path.dirname(current))}${path.sep}${link}`;
|
|
291
|
-
current = path.join(fs.realpathSync.native(path.dirname(joined)), path.basename(joined));
|
|
292
|
-
}
|
|
293
|
-
throw Object.assign(new Error(`${filePath}: too many symbolic links`), { code: "ELOOP" });
|
|
294
|
-
}
|
|
295
|
-
var IN_PLACE_FALLBACK_CODES = /* @__PURE__ */ new Set(["EACCES", "EPERM", "EROFS", "EBUSY", "EXDEV"]);
|
|
296
|
-
function stillWritable(file) {
|
|
297
|
-
try {
|
|
298
|
-
fs.accessSync(file, fs.constants.W_OK);
|
|
299
|
-
return true;
|
|
300
|
-
} catch {
|
|
301
|
-
return false;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
function writeInstructionFile(filePath, data) {
|
|
305
|
-
const target = resolveWriteTarget(filePath);
|
|
306
|
-
let existing = null;
|
|
307
|
-
try {
|
|
308
|
-
existing = fs.statSync(target);
|
|
309
|
-
} catch (err) {
|
|
310
|
-
if (err.code !== "ENOENT") throw err;
|
|
311
|
-
}
|
|
312
|
-
if (existing !== null) {
|
|
313
|
-
if (!existing.isFile()) throw new Error(`${filePath} is not a regular file`);
|
|
314
|
-
fs.accessSync(target, fs.constants.W_OK);
|
|
315
|
-
} else {
|
|
316
|
-
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
317
|
-
}
|
|
318
|
-
try {
|
|
319
|
-
atomicWriteFile(target, data, existing === null ? void 0 : existing.mode & 4095);
|
|
320
|
-
} catch (err) {
|
|
321
|
-
const code = err.code;
|
|
322
|
-
if (existing !== null && code !== void 0 && IN_PLACE_FALLBACK_CODES.has(code) && stillWritable(target)) {
|
|
323
|
-
fs.writeFileSync(target, data, "utf-8");
|
|
324
|
-
return;
|
|
325
|
-
}
|
|
326
|
-
throw err;
|
|
327
|
-
}
|
|
328
|
-
if (existing !== null && typeof process.getuid === "function" && process.getuid() === 0 && (existing.uid !== 0 || existing.gid !== 0)) {
|
|
329
|
-
try {
|
|
330
|
-
fs.chownSync(target, existing.uid, existing.gid);
|
|
331
|
-
} catch {
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// src/anchor/offer.ts
|
|
337
|
-
function renderUnregisteredBlock(copy) {
|
|
338
|
-
const { invoke, label } = copy;
|
|
339
|
-
return [
|
|
340
|
-
ANCHOR_START,
|
|
341
|
-
"## AgentChat is installed \u2014 this agent has no account yet",
|
|
342
|
-
"",
|
|
343
|
-
`[AgentChat](https://agentchat.me) gives you, the ${label} agent, a persistent \`@handle\` other AI agents can DM. AgentChat is installed for this ${label} agent, but it does not have an AgentChat account yet.`,
|
|
344
|
-
"",
|
|
345
|
-
"**Offer this ONCE, early, and only if it fits the conversation.** Never interrupt work in progress for it, and never raise it twice in one session.",
|
|
346
|
-
"",
|
|
347
|
-
`FIRST ask exactly: "Do you already have an AgentChat account for this ${label} agent, or should I create a new one?"`,
|
|
348
|
-
"",
|
|
349
|
-
"**Run the commands yourself, and ask the user one question per turn.** Each command prints AgentChat\u2019s answer, then the command to run next. 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. When a command prints no command to run next, stop and tell the user what it said.",
|
|
350
|
-
"",
|
|
351
|
-
`- **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.`,
|
|
352
|
-
`- **Existing account, with its API key:** ask the user to run \`${invoke} login\` in their own terminal and paste the key when it asks. Never ask them to paste the key into this conversation, and never put a key in a command you run: a command line is visible to every user of the machine.`,
|
|
353
|
-
`- **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>\``,
|
|
354
|
-
"- **When it is done:** tell the user the @handle and where the API key is stored, exactly as the command printed them.",
|
|
355
|
-
"- Never reuse another active coding agent\u2019s account; each reachable agent needs its own.",
|
|
356
|
-
"",
|
|
357
|
-
`**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.`,
|
|
358
|
-
ANCHOR_END
|
|
359
|
-
].join("\n");
|
|
360
|
-
}
|
|
361
|
-
function renderDeclinedBlock(copy) {
|
|
362
|
-
const { invoke, label } = copy;
|
|
363
|
-
return [
|
|
364
|
-
ANCHOR_START,
|
|
365
|
-
"## AgentChat",
|
|
366
|
-
"",
|
|
367
|
-
`AgentChat is installed for this ${label} agent, but no handle is configured and the user has said not for now. **Do not offer to set it up.** Mention it only if they ask about AgentChat or about messaging other agents.`,
|
|
368
|
-
"",
|
|
369
|
-
`If they ask to set it up later: ${invoke} register --email <email> --handle <handle>`,
|
|
370
|
-
ANCHOR_END
|
|
371
|
-
].join("\n");
|
|
372
|
-
}
|
|
373
|
-
|
|
374
204
|
// src/integration/run-context.ts
|
|
375
205
|
var RUN_CONTEXT_ENV = {
|
|
376
206
|
wakeKind: "AGENTCHAT_WAKE_KIND",
|
|
@@ -393,7 +223,7 @@ function runContextEnv(env, request) {
|
|
|
393
223
|
|
|
394
224
|
// src/identity/commands.ts
|
|
395
225
|
import * as readline from "readline";
|
|
396
|
-
import { AgentChatClient, integration as
|
|
226
|
+
import { AgentChatClient, integration as kit } from "agentchatme";
|
|
397
227
|
|
|
398
228
|
// src/identity/host-profile.ts
|
|
399
229
|
function anchorLabelOf(profile) {
|
|
@@ -535,7 +365,10 @@ function createIdentityCommands(profile) {
|
|
|
535
365
|
` ${label}: identity line not written \u2014 GET /v1/texts could not be reached and nothing is cached; run \`${invocation()} doctor --fix\` once online`
|
|
536
366
|
];
|
|
537
367
|
}
|
|
538
|
-
|
|
368
|
+
const placed = placeAnchorAt(file, { handle, identityLine, template: null, mode: "setup", fix: `${invocation()} doctor --fix` });
|
|
369
|
+
if (placed.outcome === "hand_edited" || placed.outcome === "several_blocks") return [` ${label}: WARNING \u2014 ${placed.detail}`];
|
|
370
|
+
if (placed.outcome === "failed") return [` ${label}: FAILED \u2014 ${placed.detail}`];
|
|
371
|
+
if (placed.outcome === "replaced_other") return [` ${label}: ${placed.detail}`];
|
|
539
372
|
return [` ${label}: @${handle} \u2192 ${file}`];
|
|
540
373
|
} catch (err) {
|
|
541
374
|
return [` ${label}: FAILED \u2014 ${String(err)}`];
|
|
@@ -616,7 +449,7 @@ function createIdentityCommands(profile) {
|
|
|
616
449
|
const held = before !== null && before.kind === kind && before.email === input.email ? before : null;
|
|
617
450
|
let result;
|
|
618
451
|
try {
|
|
619
|
-
result = kind === "register" ? await
|
|
452
|
+
result = kind === "register" ? await kit.account.start(
|
|
620
453
|
"register",
|
|
621
454
|
{
|
|
622
455
|
email: input.email,
|
|
@@ -625,7 +458,7 @@ function createIdentityCommands(profile) {
|
|
|
625
458
|
...input.description !== void 0 ? { description: input.description } : {}
|
|
626
459
|
},
|
|
627
460
|
accountOptions(apiBase)
|
|
628
|
-
) : await
|
|
461
|
+
) : await kit.account.start(
|
|
629
462
|
"recover",
|
|
630
463
|
{ email: input.email, ...input.handle !== void 0 ? { handle: input.handle } : {} },
|
|
631
464
|
accountOptions(apiBase)
|
|
@@ -671,7 +504,7 @@ ${runLine(startCommand(kind, input))} again.`);
|
|
|
671
504
|
const base = pending.api_base ?? apiBase;
|
|
672
505
|
let result;
|
|
673
506
|
try {
|
|
674
|
-
result = await
|
|
507
|
+
result = await kit.account.submitCode(pending.flow, code, accountOptions(base));
|
|
675
508
|
} catch (err) {
|
|
676
509
|
console.error(`Could not reach AgentChat: ${errorText(err)}
|
|
677
510
|
${runLine(codeCommand(kind, pending.flow, code))} again.`);
|
|
@@ -695,7 +528,7 @@ ${runLine(codeCommand(kind, pending.flow, code))} again.`);
|
|
|
695
528
|
async function converse(kind, input, apiBase) {
|
|
696
529
|
let outcome;
|
|
697
530
|
try {
|
|
698
|
-
outcome = await
|
|
531
|
+
outcome = await kit.account.run({
|
|
699
532
|
kind,
|
|
700
533
|
input,
|
|
701
534
|
...accountOptions(apiBase),
|
|
@@ -774,7 +607,7 @@ login does not adopt an exported key in place of the one this home holds; nothin
|
|
|
774
607
|
}
|
|
775
608
|
let check;
|
|
776
609
|
try {
|
|
777
|
-
check = await
|
|
610
|
+
check = await kit.account.checkKey(apiKey, accountOptions(apiBase));
|
|
778
611
|
} catch (err) {
|
|
779
612
|
console.error(`Login failed: ${errorText(err)}
|
|
780
613
|
The key was not saved.`);
|
|
@@ -828,16 +661,38 @@ The key was not saved.`);
|
|
|
828
661
|
}
|
|
829
662
|
return startSignup("recover", { email, ...handle !== void 0 ? { handle } : {} }, apiBase);
|
|
830
663
|
}
|
|
831
|
-
function
|
|
832
|
-
if (file === null) return { present: null, current: null, error: null };
|
|
664
|
+
function loadNoteFor(handle, texts) {
|
|
833
665
|
try {
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
return
|
|
837
|
-
} catch (err) {
|
|
838
|
-
return { present: null, current: null, error: errorText(err) };
|
|
666
|
+
return profile.anchorLoadNote?.(handle, { identityLine: texts.identityLine, template: texts.identityLineTemplate }) ?? null;
|
|
667
|
+
} catch {
|
|
668
|
+
return null;
|
|
839
669
|
}
|
|
840
670
|
}
|
|
671
|
+
function anchorView(file, texts, handle) {
|
|
672
|
+
if (file === null) return { present: null, current: null, error: null, state: null };
|
|
673
|
+
if (handle === null) {
|
|
674
|
+
try {
|
|
675
|
+
return { present: hasAnchorAt(file), current: null, error: null, state: null };
|
|
676
|
+
} catch (err) {
|
|
677
|
+
return { present: null, current: null, error: errorText(err), state: null };
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
const state = identityBlockState(file, { handle, identityLine: texts.identityLine, template: texts.identityLineTemplate, loadNote: loadNoteFor(handle, texts) });
|
|
681
|
+
if (state.state === "unreadable") return { present: null, current: null, error: state.what.replace(/^cannot read [^:]*: /, ""), state };
|
|
682
|
+
return {
|
|
683
|
+
present: state.state !== "missing",
|
|
684
|
+
current: texts.identityLine === null ? null : state.state === "current",
|
|
685
|
+
error: null,
|
|
686
|
+
state
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
function anchorWords(file, view) {
|
|
690
|
+
if (view.error !== null) return `unreadable \u2014 ${view.error} (${file})`;
|
|
691
|
+
const state = view.state;
|
|
692
|
+
if (state === null) return `${view.present ? "present" : "missing"} (${file})`;
|
|
693
|
+
if (state.verdict === "PASS") return `current (${file})${state.loadNote !== null ? `; ${state.loadNote.note}` : ""}`;
|
|
694
|
+
return describeIdentityBlock(state, `${invocation()} doctor --fix`);
|
|
695
|
+
}
|
|
841
696
|
async function runStatus(opts) {
|
|
842
697
|
const home = profile.home();
|
|
843
698
|
const anchorFile = profile.anchorFile?.() ?? null;
|
|
@@ -880,7 +735,7 @@ The key was not saved.`);
|
|
|
880
735
|
const refused = status === 401 || status === 403;
|
|
881
736
|
const detail = describeApiError(err);
|
|
882
737
|
if (opts.json) {
|
|
883
|
-
const offline = anchorView(anchorFile,
|
|
738
|
+
const offline = anchorView(anchorFile, NO_TEXTS, null);
|
|
884
739
|
console.log(
|
|
885
740
|
JSON.stringify({
|
|
886
741
|
configured: true,
|
|
@@ -899,23 +754,24 @@ The key was not saved.`);
|
|
|
899
754
|
console.error(
|
|
900
755
|
refused ? `AgentChat refused this agent's key (${detail}). Sign in with a current key: \`${invocation()} login\`; lost it? \`${recoverUsage(invocation())}\`.` : `Could not reach AgentChat: ${detail}`
|
|
901
756
|
);
|
|
757
|
+
const cached = readCachedTexts(home, profile.client.runtime);
|
|
758
|
+
const context = cached === null ? NO_TEXTS : contextOf(cached.texts);
|
|
902
759
|
console.log(
|
|
903
760
|
[
|
|
904
761
|
`${identity.handle !== null ? `@${identity.handle}` : "This agent"} \u2014 ${refused ? `key refused (HTTP ${status})` : "AgentChat unreachable"} (${LABEL})`,
|
|
905
762
|
`Key source: ${identity.source} (${identity.source === "file" ? credentialsPath(home) : "AGENTCHAT_API_KEY"})`,
|
|
906
763
|
`API: ${identity.apiBase}`,
|
|
764
|
+
...anchorFile === null ? [] : [`Identity line: ${anchorWords(anchorFile, anchorView(anchorFile, context, identity.handle))}`],
|
|
907
765
|
...formatIntegrationStatus(integration)
|
|
908
766
|
].join("\n")
|
|
909
767
|
);
|
|
910
|
-
|
|
911
|
-
await printExtraStatus(cached === null ? NO_TEXTS : contextOf(cached.texts));
|
|
768
|
+
await printExtraStatus(context);
|
|
912
769
|
}
|
|
913
770
|
return 1;
|
|
914
771
|
}
|
|
915
772
|
const tray = await traySummary(client).catch(() => null);
|
|
916
773
|
const texts = await served(identity.apiKey, identity.apiBase);
|
|
917
|
-
const
|
|
918
|
-
const anchor = anchorView(anchorFile, identityLine, me.handle);
|
|
774
|
+
const anchor = anchorView(anchorFile, texts, me.handle);
|
|
919
775
|
if (opts.json) {
|
|
920
776
|
console.log(
|
|
921
777
|
JSON.stringify({
|
|
@@ -932,6 +788,10 @@ The key was not saved.`);
|
|
|
932
788
|
anchor_current: anchor.current,
|
|
933
789
|
/** Why the instruction file could not be read, or null. */
|
|
934
790
|
anchor_error: anchor.error,
|
|
791
|
+
/** The block as the daemon reads it (`identityBlockState`: current, stale, missing, setup_offer, hand_edited, other_agent, several_blocks, unreadable), or null. */
|
|
792
|
+
anchor_state: anchor.state?.state ?? null,
|
|
793
|
+
/** Why the runtime does not load the instruction file now, or null. */
|
|
794
|
+
anchor_not_loaded: anchor.state?.notLoaded ?? null,
|
|
935
795
|
integration
|
|
936
796
|
})
|
|
937
797
|
);
|
|
@@ -944,7 +804,7 @@ The key was not saved.`);
|
|
|
944
804
|
`Key source: ${identity.source} (${identity.source === "file" ? credentialsPath(home) : "AGENTCHAT_API_KEY"})`,
|
|
945
805
|
`API: ${identity.apiBase}`,
|
|
946
806
|
...anchorFile === null ? [] : [
|
|
947
|
-
`Identity line: ${
|
|
807
|
+
`Identity line: ${anchorWords(anchorFile, anchor)}`
|
|
948
808
|
],
|
|
949
809
|
...formatIntegrationStatus(integration)
|
|
950
810
|
].join("\n")
|
|
@@ -965,9 +825,18 @@ The key was not saved.`);
|
|
|
965
825
|
}
|
|
966
826
|
if (anchorFile !== null) {
|
|
967
827
|
try {
|
|
968
|
-
|
|
828
|
+
const cached = readCachedTexts(home, profile.client.runtime);
|
|
829
|
+
const removed = removeAnchorAt(anchorFile, {
|
|
830
|
+
handle: identity?.handle ?? null,
|
|
831
|
+
identityLine: cached?.texts.identity_line ?? null,
|
|
832
|
+
template: cached?.texts.identity_line_template ?? null
|
|
833
|
+
});
|
|
834
|
+
if (removed.action === "removed") {
|
|
969
835
|
any = true;
|
|
970
836
|
reports.push(` ${anchorLabelOf(profile)} identity line removed`);
|
|
837
|
+
} else if (removed.action === "kept") {
|
|
838
|
+
any = true;
|
|
839
|
+
reports.push(` ${removed.detail}`);
|
|
971
840
|
}
|
|
972
841
|
} catch (err) {
|
|
973
842
|
any = true;
|
|
@@ -996,41 +865,25 @@ The key was not saved.`);
|
|
|
996
865
|
return 0;
|
|
997
866
|
}
|
|
998
867
|
function readIdentityLine(anchorFile, handle, texts) {
|
|
999
|
-
|
|
1000
|
-
const current = texts.identityLine === null ? null : anchorIsCurrentAt(anchorFile, texts.identityLine, handle);
|
|
1001
|
-
return { claimed, current };
|
|
868
|
+
return identityBlockState(anchorFile, { handle, identityLine: texts.identityLine, template: texts.identityLineTemplate, loadNote: loadNoteFor(handle, texts) });
|
|
1002
869
|
}
|
|
1003
870
|
async function identityLineCheck(anchorFile, creds, apiBase, texts, fix) {
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
} catch (err) {
|
|
1010
|
-
return { name: "identity-line", verdict: "FAIL", detail: `cannot read ${anchorFile}: ${errorText(err)}` };
|
|
1011
|
-
}
|
|
1012
|
-
if (claimed === creds.handle && current === true) {
|
|
1013
|
-
return { name: "identity-line", verdict: "PASS", detail: `@${claimed} in ${anchorFile}` };
|
|
1014
|
-
}
|
|
1015
|
-
if (claimed === creds.handle && current === null) {
|
|
1016
|
-
return { name: "identity-line", verdict: "WARN", detail: `@${claimed} in ${anchorFile} (currency not checked: served texts unavailable)` };
|
|
1017
|
-
}
|
|
1018
|
-
const why = claimed === null ? `no identity line in ${anchorFile}` : claimed !== creds.handle ? `${anchorFile} says @${claimed} but this agent is @${creds.handle}` : `${anchorFile} carries an older identity text for @${claimed}`;
|
|
1019
|
-
if (fix) {
|
|
871
|
+
const fixCommand = `${invocation()} doctor --fix`;
|
|
872
|
+
const before = readIdentityLine(anchorFile, creds.handle, texts);
|
|
873
|
+
if (before.state === "unreadable") return { name: "identity-line", verdict: "FAIL", detail: before.what };
|
|
874
|
+
const repairable = before.state === "missing" || before.state === "setup_offer" || before.state === "stale";
|
|
875
|
+
if (fix && repairable) {
|
|
1020
876
|
const report2 = await placeIdentity(creds.handle, creds.api_key, apiBase);
|
|
1021
877
|
const failed = report2.some((l) => l.includes("FAILED") || l.includes("not written"));
|
|
1022
878
|
if (failed) return { name: "identity-line", verdict: "FAIL", detail: `could not repair: ${report2.join("; ")}` };
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
return { name: "identity-line", verdict: "PASS", detail: `repaired \u2192 @${creds.handle}` };
|
|
1027
|
-
}
|
|
1028
|
-
return { name: "identity-line", verdict: "FAIL", detail: `repair did not take: ${anchorFile} still ${after.claimed === null ? "has no identity line" : after.claimed !== creds.handle ? `says @${after.claimed}` : "differs from the served block"}${report2.length > 0 ? ` (${report2.join("; ").trim()})` : ""}` };
|
|
1029
|
-
} catch (err) {
|
|
1030
|
-
return { name: "identity-line", verdict: "FAIL", detail: `cannot read ${anchorFile} after the repair: ${errorText(err)}` };
|
|
879
|
+
const after = readIdentityLine(anchorFile, creds.handle, texts);
|
|
880
|
+
if (after.state === "current") {
|
|
881
|
+
return { name: "identity-line", verdict: after.verdict, detail: after.loadNote === null ? `repaired \u2192 @${creds.handle}` : `repaired \u2192 @${creds.handle} in ${anchorFile}; ${after.loadNote.note}` };
|
|
1031
882
|
}
|
|
883
|
+
if (after.state === "unreadable") return { name: "identity-line", verdict: "FAIL", detail: `${after.what} (after the repair)` };
|
|
884
|
+
return { name: "identity-line", verdict: "FAIL", detail: `repair did not take: ${after.what}${report2.length > 0 ? ` (${report2.join("; ").trim()})` : ""}` };
|
|
1032
885
|
}
|
|
1033
|
-
return { name: "identity-line", verdict:
|
|
886
|
+
return { name: "identity-line", verdict: before.verdict, detail: describeIdentityBlock(before, fixCommand) };
|
|
1034
887
|
}
|
|
1035
888
|
async function runDoctor(opts = {}) {
|
|
1036
889
|
const home = profile.home();
|
|
@@ -1133,11 +986,17 @@ The key was not saved.`);
|
|
|
1133
986
|
if (paused !== null) {
|
|
1134
987
|
checks.push({ name: "could-not-process", verdict: "WARN", detail: paused.replace(/^could not process: /, "") });
|
|
1135
988
|
} else if (lastCheck !== null && runtimeCheckFailingNow(integration)) {
|
|
1136
|
-
checks.push(
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
989
|
+
checks.push(
|
|
990
|
+
integrationStatusFresh(integration) ? {
|
|
991
|
+
name: "could-not-process",
|
|
992
|
+
verdict: "WARN",
|
|
993
|
+
detail: `the runtime check failed (${lastCheck.detail ?? "no detail"}; ${lastCheck.at}): while it fails, a conversation whose run fails is not reported, its card stays on the tray and the server wakes the agent again`
|
|
994
|
+
} : {
|
|
995
|
+
name: "could-not-process",
|
|
996
|
+
verdict: "WARN",
|
|
997
|
+
detail: `the daemon has not written its status for ${integrationStatusStaleFor(integration)} \u2014 it may be down (a running daemon rewrites it every 30 s); the runtime check that failed at ${lastCheck.at} says nothing about now`
|
|
998
|
+
}
|
|
999
|
+
);
|
|
1141
1000
|
} else if (counts !== null) {
|
|
1142
1001
|
checks.push({ name: "could-not-process", verdict: "PASS", detail: counts.replace(/^could not process: /, "") });
|
|
1143
1002
|
}
|
|
@@ -1164,12 +1023,12 @@ The key was not saved.`);
|
|
|
1164
1023
|
|
|
1165
1024
|
// src/daemon/service.ts
|
|
1166
1025
|
import * as crypto from "crypto";
|
|
1167
|
-
import * as
|
|
1026
|
+
import * as fs from "fs";
|
|
1168
1027
|
import * as os from "os";
|
|
1169
|
-
import * as
|
|
1028
|
+
import * as path from "path";
|
|
1170
1029
|
function serviceLabelFor(base, home, defaultHome) {
|
|
1171
1030
|
const key = (p) => {
|
|
1172
|
-
const resolved =
|
|
1031
|
+
const resolved = path.resolve(p);
|
|
1173
1032
|
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
1174
1033
|
};
|
|
1175
1034
|
if (key(home) === key(defaultHome)) return serviceLabel(base);
|
|
@@ -1196,7 +1055,7 @@ function transientPathEntry(value) {
|
|
|
1196
1055
|
return /\/node_modules\/\.bin\/?$/i.test(normalized) || /\/node_modules\/npm\/node_modules\/@npmcli\/run-script\/lib\/node-gyp-bin\/?$/i.test(normalized) || /\/\.[^/]+\/tmp\//i.test(normalized);
|
|
1197
1056
|
}
|
|
1198
1057
|
function stableServicePath(value, nodePath = process.execPath) {
|
|
1199
|
-
const entries = [
|
|
1058
|
+
const entries = [path.dirname(nodePath), ...value.split(path.delimiter)];
|
|
1200
1059
|
const seen = /* @__PURE__ */ new Set();
|
|
1201
1060
|
const stable = [];
|
|
1202
1061
|
for (const raw of entries) {
|
|
@@ -1204,7 +1063,7 @@ function stableServicePath(value, nodePath = process.execPath) {
|
|
|
1204
1063
|
if (!trimmed || transientPathEntry(trimmed)) continue;
|
|
1205
1064
|
let resolved = trimmed;
|
|
1206
1065
|
try {
|
|
1207
|
-
resolved =
|
|
1066
|
+
resolved = fs.realpathSync(trimmed);
|
|
1208
1067
|
} catch {
|
|
1209
1068
|
}
|
|
1210
1069
|
const key = process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
@@ -1212,7 +1071,7 @@ function stableServicePath(value, nodePath = process.execPath) {
|
|
|
1212
1071
|
seen.add(key);
|
|
1213
1072
|
stable.push(resolved);
|
|
1214
1073
|
}
|
|
1215
|
-
return stable.join(
|
|
1074
|
+
return stable.join(path.delimiter);
|
|
1216
1075
|
}
|
|
1217
1076
|
function plan(opts) {
|
|
1218
1077
|
const env = {};
|
|
@@ -1234,8 +1093,8 @@ function plan(opts) {
|
|
|
1234
1093
|
return {
|
|
1235
1094
|
label: serviceLabel(opts.label),
|
|
1236
1095
|
node: serviceValue("Node path", process.execPath),
|
|
1237
|
-
bin: serviceValue("entry path",
|
|
1238
|
-
home: serviceValue("home path",
|
|
1096
|
+
bin: serviceValue("entry path", path.resolve(opts.entry)),
|
|
1097
|
+
home: serviceValue("home path", path.resolve(opts.home)),
|
|
1239
1098
|
env
|
|
1240
1099
|
};
|
|
1241
1100
|
}
|
|
@@ -1315,9 +1174,9 @@ function parseServiceHome(text, format) {
|
|
|
1315
1174
|
return exec === void 0 ? null : homeFromArgs(systemdWords(exec.slice("ExecStart=".length)));
|
|
1316
1175
|
}
|
|
1317
1176
|
function canonicalPath(p) {
|
|
1318
|
-
const resolved =
|
|
1177
|
+
const resolved = path.resolve(p);
|
|
1319
1178
|
try {
|
|
1320
|
-
return
|
|
1179
|
+
return fs.realpathSync(resolved);
|
|
1321
1180
|
} catch {
|
|
1322
1181
|
return resolved;
|
|
1323
1182
|
}
|
|
@@ -1330,10 +1189,10 @@ function sameHome(a, b) {
|
|
|
1330
1189
|
function launchdSightings(label) {
|
|
1331
1190
|
const out = [];
|
|
1332
1191
|
const plistPath = launchdPlistPath(label);
|
|
1333
|
-
if (
|
|
1192
|
+
if (fs.existsSync(plistPath)) {
|
|
1334
1193
|
let text = "";
|
|
1335
1194
|
try {
|
|
1336
|
-
text =
|
|
1195
|
+
text = fs.readFileSync(plistPath, "utf-8");
|
|
1337
1196
|
} catch {
|
|
1338
1197
|
}
|
|
1339
1198
|
out.push({ where: plistPath, home: parseServiceHome(text, "plist"), loaded: false });
|
|
@@ -1350,7 +1209,7 @@ function launchdSightings(label) {
|
|
|
1350
1209
|
const loadedFrom = /^\s*path = (.+)$/m.exec(text)?.[1]?.trim();
|
|
1351
1210
|
if (loadedFrom !== void 0) {
|
|
1352
1211
|
try {
|
|
1353
|
-
home = parseServiceHome(
|
|
1212
|
+
home = parseServiceHome(fs.readFileSync(loadedFrom, "utf-8"), "plist");
|
|
1354
1213
|
} catch {
|
|
1355
1214
|
}
|
|
1356
1215
|
}
|
|
@@ -1363,10 +1222,10 @@ function launchdSightings(label) {
|
|
|
1363
1222
|
function systemdSightings(label) {
|
|
1364
1223
|
const out = [];
|
|
1365
1224
|
const unitPath = systemdUnitPath(label);
|
|
1366
|
-
if (
|
|
1225
|
+
if (fs.existsSync(unitPath)) {
|
|
1367
1226
|
let text = "";
|
|
1368
1227
|
try {
|
|
1369
|
-
text =
|
|
1228
|
+
text = fs.readFileSync(unitPath, "utf-8");
|
|
1370
1229
|
} catch {
|
|
1371
1230
|
}
|
|
1372
1231
|
out.push({ where: unitPath, home: parseServiceHome(text, "systemd"), loaded: false });
|
|
@@ -1384,7 +1243,7 @@ function systemdSightings(label) {
|
|
|
1384
1243
|
const fragment = props.get("FragmentPath") ?? "";
|
|
1385
1244
|
let home = null;
|
|
1386
1245
|
try {
|
|
1387
|
-
if (fragment) home = parseServiceHome(
|
|
1246
|
+
if (fragment) home = parseServiceHome(fs.readFileSync(fragment, "utf-8"), "systemd");
|
|
1388
1247
|
} catch {
|
|
1389
1248
|
}
|
|
1390
1249
|
out.push({ where: `the loaded systemd unit ${label}${fragment ? ` (${fragment})` : ""}`, home, loaded: true });
|
|
@@ -1418,7 +1277,7 @@ function servesHome(label, home) {
|
|
|
1418
1277
|
return sightings(label).some((s) => s.home !== null && sameHome(s.home, home));
|
|
1419
1278
|
}
|
|
1420
1279
|
function systemdUnitPath(label) {
|
|
1421
|
-
return
|
|
1280
|
+
return path.join(os.homedir(), ".config", "systemd", "user", `${label}.service`);
|
|
1422
1281
|
}
|
|
1423
1282
|
function systemdQuote(value) {
|
|
1424
1283
|
return '"' + value.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/"/g, '\\"').replace(/\$/g, "$$$$").replace(/%/g, "%%") + '"';
|
|
@@ -1446,7 +1305,7 @@ function systemdUnit(p) {
|
|
|
1446
1305
|
function installSystemd(p) {
|
|
1447
1306
|
assertServesHome(p.label, p.home, "replace");
|
|
1448
1307
|
const unitPath = systemdUnitPath(p.label);
|
|
1449
|
-
|
|
1308
|
+
fs.mkdirSync(path.dirname(unitPath), { recursive: true });
|
|
1450
1309
|
atomicWriteFile(unitPath, systemdUnit(p), 384);
|
|
1451
1310
|
log.info(`wrote ${unitPath}`);
|
|
1452
1311
|
if (registrationSkipped()) return;
|
|
@@ -1476,7 +1335,7 @@ function uninstallSystemd(label, home) {
|
|
|
1476
1335
|
}
|
|
1477
1336
|
}
|
|
1478
1337
|
const unitPath = systemdUnitPath(label);
|
|
1479
|
-
if (
|
|
1338
|
+
if (fs.existsSync(unitPath)) fs.rmSync(unitPath);
|
|
1480
1339
|
if (!registrationSkipped()) {
|
|
1481
1340
|
const reload = run("systemctl", ["--user", "daemon-reload"]);
|
|
1482
1341
|
if (!reload.ok) log.warn(`systemctl daemon-reload failed after uninstall: ${reload.out}`);
|
|
@@ -1490,7 +1349,7 @@ function statusSystemd(label, home) {
|
|
|
1490
1349
|
return `systemd ${label}: ${active} (${enabled})${foreignNote(label, home)}`;
|
|
1491
1350
|
}
|
|
1492
1351
|
function dryRunStatus(name, definitionPath) {
|
|
1493
|
-
return `${name}: not queried (AGENTCHAT_SERVICE_DRY_RUN) \u2014 definition ${
|
|
1352
|
+
return `${name}: not queried (AGENTCHAT_SERVICE_DRY_RUN) \u2014 definition ${fs.existsSync(definitionPath) ? `written at ${definitionPath}` : "not written"}`;
|
|
1494
1353
|
}
|
|
1495
1354
|
function foreignNote(label, home) {
|
|
1496
1355
|
const loaded = sightings(label).find((s) => s.loaded);
|
|
@@ -1498,7 +1357,7 @@ function foreignNote(label, home) {
|
|
|
1498
1357
|
return ` \u2014 but it serves ${loaded.home}, not this config (${home})`;
|
|
1499
1358
|
}
|
|
1500
1359
|
function launchdPlistPath(label) {
|
|
1501
|
-
return
|
|
1360
|
+
return path.join(os.homedir(), "Library", "LaunchAgents", `${launchdLabel(label)}.plist`);
|
|
1502
1361
|
}
|
|
1503
1362
|
var launchdLabel = (label) => `me.agentchat.${label}`;
|
|
1504
1363
|
function xmlEscape(value) {
|
|
@@ -1508,7 +1367,7 @@ function launchdPlist(p) {
|
|
|
1508
1367
|
const args = [p.node, p.bin, "--home", p.home];
|
|
1509
1368
|
const argXml = args.map((a) => ` <string>${xmlEscape(a)}</string>`).join("\n");
|
|
1510
1369
|
const envXml = Object.entries(p.env).map(([k, v]) => ` <key>${xmlEscape(k)}</key><string>${xmlEscape(v)}</string>`).join("\n");
|
|
1511
|
-
const logPath =
|
|
1370
|
+
const logPath = path.join(p.home, "daemon.log");
|
|
1512
1371
|
return [
|
|
1513
1372
|
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
1514
1373
|
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
|
|
@@ -1535,7 +1394,7 @@ function launchdPlist(p) {
|
|
|
1535
1394
|
function installLaunchd(p) {
|
|
1536
1395
|
assertServesHome(p.label, p.home, "replace");
|
|
1537
1396
|
const plistPath = launchdPlistPath(p.label);
|
|
1538
|
-
|
|
1397
|
+
fs.mkdirSync(path.dirname(plistPath), { recursive: true });
|
|
1539
1398
|
atomicWriteFile(plistPath, launchdPlist(p), 384);
|
|
1540
1399
|
log.info(`wrote ${plistPath}`);
|
|
1541
1400
|
if (registrationSkipped()) return;
|
|
@@ -1563,7 +1422,7 @@ function uninstallLaunchd(label, home) {
|
|
|
1563
1422
|
);
|
|
1564
1423
|
}
|
|
1565
1424
|
}
|
|
1566
|
-
if (
|
|
1425
|
+
if (fs.existsSync(plistPath)) fs.rmSync(plistPath);
|
|
1567
1426
|
log.info(`service ${launchdLabel(label)} removed`);
|
|
1568
1427
|
}
|
|
1569
1428
|
function statusLaunchd(label, home) {
|
|
@@ -1580,7 +1439,7 @@ function isWslFromVersion(version) {
|
|
|
1580
1439
|
function isWsl() {
|
|
1581
1440
|
if (process.platform !== "linux") return false;
|
|
1582
1441
|
try {
|
|
1583
|
-
return isWslFromVersion(
|
|
1442
|
+
return isWslFromVersion(fs.readFileSync("/proc/version", "utf-8"));
|
|
1584
1443
|
} catch {
|
|
1585
1444
|
return false;
|
|
1586
1445
|
}
|
|
@@ -1601,16 +1460,16 @@ function winMode() {
|
|
|
1601
1460
|
return hostOs === "win32" || hostOs === "wsl" ? hostOs : null;
|
|
1602
1461
|
}
|
|
1603
1462
|
function winMasterDir() {
|
|
1604
|
-
return
|
|
1463
|
+
return path.join(os.homedir(), ".agentchat", "daemon");
|
|
1605
1464
|
}
|
|
1606
1465
|
function winStartupDir(mode) {
|
|
1607
1466
|
if (mode === "win32") {
|
|
1608
|
-
return
|
|
1467
|
+
return path.join(os.homedir(), "AppData", "Roaming", "Microsoft", "Windows", "Start Menu", "Programs", "Startup");
|
|
1609
1468
|
}
|
|
1610
1469
|
const appdata = run("cmd.exe", ["/c", "echo %APPDATA%"]).out.split(/\r?\n/).pop()?.trim() ?? "";
|
|
1611
1470
|
const base = run("wslpath", ["-u", appdata]).out.trim();
|
|
1612
1471
|
if (!base) throw new Error("could not locate the Windows Startup folder from WSL (is /mnt/c mounted?)");
|
|
1613
|
-
return
|
|
1472
|
+
return path.join(base, "Microsoft", "Windows", "Start Menu", "Programs", "Startup");
|
|
1614
1473
|
}
|
|
1615
1474
|
var vbsEscape = (s) => s.replace(/"/g, '""');
|
|
1616
1475
|
var shQuote = (s) => `'${s.replace(/'/g, `'\\''`)}'`;
|
|
@@ -1653,11 +1512,11 @@ function killLauncher(label, mode) {
|
|
|
1653
1512
|
return run(mode === "win32" ? "powershell" : "powershell.exe", ["-NoProfile", "-Command", ps]);
|
|
1654
1513
|
}
|
|
1655
1514
|
function lockedDaemonPid(home) {
|
|
1656
|
-
const file =
|
|
1515
|
+
const file = path.join(home, DAEMON_LOCK_FILE);
|
|
1657
1516
|
try {
|
|
1658
|
-
const holder = parseLease(
|
|
1517
|
+
const holder = parseLease(fs.readFileSync(file, "utf-8"));
|
|
1659
1518
|
if (holder === null || !Number.isInteger(holder.pid) || holder.pid <= 0) return null;
|
|
1660
|
-
return holderLive(holder, { mtimeMs:
|
|
1519
|
+
return holderLive(holder, { mtimeMs: fs.statSync(file).mtimeMs }) ? holder.pid : null;
|
|
1661
1520
|
} catch {
|
|
1662
1521
|
return null;
|
|
1663
1522
|
}
|
|
@@ -1673,12 +1532,12 @@ function stopRunningWindowsDaemon(label, mode, home) {
|
|
|
1673
1532
|
}
|
|
1674
1533
|
function installWindows(p, mode) {
|
|
1675
1534
|
const master = winMasterDir();
|
|
1676
|
-
|
|
1677
|
-
const masterVbs =
|
|
1535
|
+
fs.mkdirSync(master, { recursive: true });
|
|
1536
|
+
const masterVbs = path.join(master, `${p.label}.vbs`);
|
|
1678
1537
|
if (mode === "win32") {
|
|
1679
1538
|
atomicWriteFile(masterVbs, launcherVbs(winCommandNative(p), p.env), 384);
|
|
1680
1539
|
} else {
|
|
1681
|
-
const scriptPath =
|
|
1540
|
+
const scriptPath = path.join(master, `${p.label}.sh`);
|
|
1682
1541
|
atomicWriteFile(scriptPath, wslScriptContent(p), 448);
|
|
1683
1542
|
const distro = serviceValue("WSL distro", process.env["WSL_DISTRO_NAME"] ?? "");
|
|
1684
1543
|
if (!distro) throw new Error("WSL_DISTRO_NAME is not set \u2014 cannot target the right WSL distro");
|
|
@@ -1693,20 +1552,20 @@ function installWindows(p, mode) {
|
|
|
1693
1552
|
log.info(`service ${p.label} installed (starts at login)`);
|
|
1694
1553
|
}
|
|
1695
1554
|
function enableWindows(label, mode, home) {
|
|
1696
|
-
const masterVbs =
|
|
1697
|
-
if (!
|
|
1555
|
+
const masterVbs = path.join(winMasterDir(), `${label}.vbs`);
|
|
1556
|
+
if (!fs.existsSync(masterVbs)) throw new Error(`no ${label} daemon installed \u2014 run install first`);
|
|
1698
1557
|
const startup = winStartupDir(mode);
|
|
1699
|
-
|
|
1700
|
-
const startupVbs =
|
|
1701
|
-
|
|
1558
|
+
fs.mkdirSync(startup, { recursive: true });
|
|
1559
|
+
const startupVbs = path.join(startup, `${label}.vbs`);
|
|
1560
|
+
fs.copyFileSync(masterVbs, startupVbs);
|
|
1702
1561
|
if (registrationSkipped()) return;
|
|
1703
1562
|
stopRunningWindowsDaemon(label, mode, home);
|
|
1704
1563
|
if (mode === "win32") startDetached("wscript.exe", [startupVbs]);
|
|
1705
1564
|
else startDetached("cmd.exe", ["/c", "start", "", "/b", "wscript.exe", winPathFromWsl(startupVbs)]);
|
|
1706
1565
|
}
|
|
1707
1566
|
function disableWindows(label, mode) {
|
|
1708
|
-
const startupVbs =
|
|
1709
|
-
if (
|
|
1567
|
+
const startupVbs = path.join(winStartupDir(mode), `${label}.vbs`);
|
|
1568
|
+
if (fs.existsSync(startupVbs)) fs.rmSync(startupVbs);
|
|
1710
1569
|
if (!registrationSkipped()) {
|
|
1711
1570
|
const killed = killLauncher(label, mode);
|
|
1712
1571
|
if (!killed.ok) {
|
|
@@ -1717,15 +1576,15 @@ function disableWindows(label, mode) {
|
|
|
1717
1576
|
function uninstallWindows(label, mode) {
|
|
1718
1577
|
disableWindows(label, mode);
|
|
1719
1578
|
for (const f of [`${label}.vbs`, `${label}.sh`]) {
|
|
1720
|
-
const m =
|
|
1721
|
-
if (
|
|
1579
|
+
const m = path.join(winMasterDir(), f);
|
|
1580
|
+
if (fs.existsSync(m)) fs.rmSync(m);
|
|
1722
1581
|
}
|
|
1723
1582
|
}
|
|
1724
1583
|
function statusWindows(label, mode) {
|
|
1725
|
-
const installed =
|
|
1584
|
+
const installed = fs.existsSync(path.join(winMasterDir(), `${label}.vbs`));
|
|
1726
1585
|
let enabled = false;
|
|
1727
1586
|
try {
|
|
1728
|
-
enabled =
|
|
1587
|
+
enabled = fs.existsSync(path.join(winStartupDir(mode), `${label}.vbs`));
|
|
1729
1588
|
} catch {
|
|
1730
1589
|
}
|
|
1731
1590
|
const host = mode === "wsl" ? "wsl" : "windows";
|
|
@@ -1752,7 +1611,7 @@ function installService(opts) {
|
|
|
1752
1611
|
}
|
|
1753
1612
|
function uninstallService(opts) {
|
|
1754
1613
|
const label = serviceLabel(opts.label);
|
|
1755
|
-
const home =
|
|
1614
|
+
const home = path.resolve(opts.home);
|
|
1756
1615
|
const wm = winMode();
|
|
1757
1616
|
if (wm) return uninstallWindows(label, wm);
|
|
1758
1617
|
if (serviceHostOs() === "linux") {
|
|
@@ -1769,7 +1628,7 @@ function uninstallService(opts) {
|
|
|
1769
1628
|
}
|
|
1770
1629
|
function serviceStatus(opts) {
|
|
1771
1630
|
const label = serviceLabel(opts.label);
|
|
1772
|
-
const home =
|
|
1631
|
+
const home = path.resolve(opts.home);
|
|
1773
1632
|
const wm = winMode();
|
|
1774
1633
|
if (wm) return statusWindows(label, wm);
|
|
1775
1634
|
if (serviceHostOs() === "linux") return statusSystemd(label, home);
|
|
@@ -1781,23 +1640,23 @@ function serviceDefinitionCurrent(opts) {
|
|
|
1781
1640
|
const p = plan(opts);
|
|
1782
1641
|
const wm = winMode();
|
|
1783
1642
|
if (wm === "win32") {
|
|
1784
|
-
const file =
|
|
1785
|
-
return
|
|
1643
|
+
const file = path.join(winMasterDir(), `${p.label}.vbs`);
|
|
1644
|
+
return fs.existsSync(file) && fs.readFileSync(file, "utf-8") === launcherVbs(winCommandNative(p), p.env);
|
|
1786
1645
|
}
|
|
1787
1646
|
if (wm === "wsl") {
|
|
1788
|
-
const script =
|
|
1789
|
-
const vbs =
|
|
1647
|
+
const script = path.join(winMasterDir(), `${p.label}.sh`);
|
|
1648
|
+
const vbs = path.join(winMasterDir(), `${p.label}.vbs`);
|
|
1790
1649
|
const distro = serviceValue("WSL distro", process.env["WSL_DISTRO_NAME"] ?? "");
|
|
1791
1650
|
if (!distro) return false;
|
|
1792
|
-
return
|
|
1651
|
+
return fs.existsSync(script) && fs.existsSync(vbs) && fs.readFileSync(script, "utf-8") === wslScriptContent(p) && fs.readFileSync(vbs, "utf-8") === launcherVbs(`wsl.exe -d "${distro}" -e bash "${script}"`, {});
|
|
1793
1652
|
}
|
|
1794
1653
|
if (serviceHostOs() === "linux") {
|
|
1795
1654
|
const file = systemdUnitPath(p.label);
|
|
1796
|
-
return
|
|
1655
|
+
return fs.existsSync(file) && fs.readFileSync(file, "utf-8") === systemdUnit(p);
|
|
1797
1656
|
}
|
|
1798
1657
|
if (serviceHostOs() === "darwin") {
|
|
1799
1658
|
const file = launchdPlistPath(p.label);
|
|
1800
|
-
return
|
|
1659
|
+
return fs.existsSync(file) && fs.readFileSync(file, "utf-8") === launchdPlist(p);
|
|
1801
1660
|
}
|
|
1802
1661
|
return false;
|
|
1803
1662
|
} catch {
|
|
@@ -1805,9 +1664,9 @@ function serviceDefinitionCurrent(opts) {
|
|
|
1805
1664
|
}
|
|
1806
1665
|
}
|
|
1807
1666
|
function unitInstalled(label) {
|
|
1808
|
-
if (winMode()) return
|
|
1809
|
-
if (serviceHostOs() === "linux") return
|
|
1810
|
-
if (serviceHostOs() === "darwin") return
|
|
1667
|
+
if (winMode()) return fs.existsSync(path.join(winMasterDir(), `${label}.vbs`));
|
|
1668
|
+
if (serviceHostOs() === "linux") return fs.existsSync(systemdUnitPath(label));
|
|
1669
|
+
if (serviceHostOs() === "darwin") return fs.existsSync(launchdPlistPath(label));
|
|
1811
1670
|
return false;
|
|
1812
1671
|
}
|
|
1813
1672
|
function serviceInstalled(opts) {
|
|
@@ -1826,10 +1685,12 @@ export {
|
|
|
1826
1685
|
FALLBACK_RUN_PROFILE,
|
|
1827
1686
|
HEARTBEAT_FILE,
|
|
1828
1687
|
HOSTED_MCP_PATH,
|
|
1688
|
+
IDENTITY_ANCHOR_ATTEMPTS,
|
|
1829
1689
|
INTEGRATION_ENV,
|
|
1830
1690
|
INTEGRATION_ENV_LEGACY,
|
|
1831
1691
|
INTEGRATION_ENV_LEGACY_REMOVAL,
|
|
1832
1692
|
INTEGRATION_STATUS_FILE,
|
|
1693
|
+
INTEGRATION_STATUS_STALE_MS,
|
|
1833
1694
|
LEASE_RENEW_MS,
|
|
1834
1695
|
LEASE_STALE_MS,
|
|
1835
1696
|
LEGACY_STATUS_FILE,
|
|
@@ -1885,6 +1746,7 @@ export {
|
|
|
1885
1746
|
createIdentityCommands,
|
|
1886
1747
|
credentialsPath,
|
|
1887
1748
|
describeCouldNotProcessCounts,
|
|
1749
|
+
describeIdentityBlock,
|
|
1888
1750
|
describeManualSync,
|
|
1889
1751
|
describePausedConversations,
|
|
1890
1752
|
describeRunProfile,
|
|
@@ -1893,20 +1755,25 @@ export {
|
|
|
1893
1755
|
failedCyclesInARow,
|
|
1894
1756
|
fetchIdentityLine,
|
|
1895
1757
|
fetchServedTexts,
|
|
1758
|
+
followIdentityAnchor,
|
|
1896
1759
|
formatIntegrationStatus,
|
|
1897
1760
|
fromKitResult,
|
|
1898
1761
|
hasAnchorAt,
|
|
1899
1762
|
holderLive,
|
|
1900
1763
|
hostedMcpUrl,
|
|
1764
|
+
identityBlockState,
|
|
1901
1765
|
identityLineOf,
|
|
1902
1766
|
idle,
|
|
1903
1767
|
installService,
|
|
1904
1768
|
integrationDisabled,
|
|
1905
1769
|
integrationDisabledBy,
|
|
1906
1770
|
integrationStatusAge,
|
|
1771
|
+
integrationStatusFresh,
|
|
1907
1772
|
integrationStatusPath,
|
|
1773
|
+
integrationStatusStaleFor,
|
|
1908
1774
|
isRunProfile,
|
|
1909
1775
|
isRunnerFactory,
|
|
1776
|
+
isSetupOfferBody,
|
|
1910
1777
|
launchdPlist,
|
|
1911
1778
|
leaseHolder,
|
|
1912
1779
|
loadTexts,
|
|
@@ -1925,6 +1792,7 @@ export {
|
|
|
1925
1792
|
parseServiceHome,
|
|
1926
1793
|
pendingPath,
|
|
1927
1794
|
pidAlive,
|
|
1795
|
+
placeAnchorAt,
|
|
1928
1796
|
planForTest,
|
|
1929
1797
|
probeHostedMcp,
|
|
1930
1798
|
readAlwaysOnInstalledVersion,
|
|
@@ -1945,12 +1813,14 @@ export {
|
|
|
1945
1813
|
repeatedFailedRuns,
|
|
1946
1814
|
resetLeaseObservations,
|
|
1947
1815
|
resolveIdentity,
|
|
1816
|
+
resolveManualPointer,
|
|
1948
1817
|
resolveRunBudgetMs,
|
|
1949
1818
|
resolveRunProfile,
|
|
1950
1819
|
runContextEnv,
|
|
1951
1820
|
runProbe,
|
|
1952
1821
|
runProfileWarning,
|
|
1953
1822
|
runtimeCheckFailingNow,
|
|
1823
|
+
servedManualUrl,
|
|
1954
1824
|
serviceDefinitionCurrent,
|
|
1955
1825
|
serviceInstalled,
|
|
1956
1826
|
serviceLabelFor,
|
|
@@ -1974,6 +1844,7 @@ export {
|
|
|
1974
1844
|
writeAnchor,
|
|
1975
1845
|
writeCredentials,
|
|
1976
1846
|
writeIntegrationStatus,
|
|
1847
|
+
writeOfferAt,
|
|
1977
1848
|
writePending,
|
|
1978
1849
|
writeState,
|
|
1979
1850
|
xmlEscape
|