@agentknight/client 0.9.48 → 0.9.50
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/LICENSE +201 -201
- package/README.md +356 -324
- package/dist/chunks/aad-relay-login-NABH6HML.js +14 -0
- package/dist/chunks/chunk-CYCBYXFV.js +32 -0
- package/dist/chunks/chunk-HWCSV7QP.js +2902 -0
- package/dist/chunks/chunk-KYTI7A5R.js +735 -0
- package/dist/chunks/chunk-SPZWIFFP.js +52 -0
- package/dist/chunks/chunk-WZIU4HVH.js +318 -0
- package/dist/chunks/chunk-XODJ7TIT.js +475 -0
- package/dist/chunks/chunk-YNKNHR5K.js +74 -0
- package/dist/chunks/client-home-LAKFDPXO.js +268 -0
- package/dist/chunks/local-sessions-CLY45IWB.js +29 -0
- package/dist/chunks/runtime-CTACRJCK.js +20 -0
- package/dist/chunks/start-client-VGGH7W42.js +3971 -0
- package/dist/chunks/upgrade-T32WY4HC.js +181 -0
- package/dist/chunks/upgrade-state-QXZT3TTS.js +17 -0
- package/dist/cli.js +98 -7353
- package/dist/upgrade-worker.js +200 -4
- package/package.json +13 -4
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
clearClientConfiguration,
|
|
4
|
+
defaultClientHome,
|
|
5
|
+
defaultPairingPagePort,
|
|
6
|
+
hasDeprecatedClientEnvironment,
|
|
7
|
+
normalizeClientEnvironment,
|
|
8
|
+
sameClientPath
|
|
9
|
+
} from "./chunk-YNKNHR5K.js";
|
|
10
|
+
|
|
11
|
+
// src/client-home.ts
|
|
12
|
+
import { createHash } from "node:crypto";
|
|
13
|
+
import { constants, createReadStream } from "node:fs";
|
|
14
|
+
import { chmod, copyFile, lstat, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
|
15
|
+
import { homedir } from "node:os";
|
|
16
|
+
import { dirname, join, resolve, sep } from "node:path";
|
|
17
|
+
import { parseEnv } from "node:util";
|
|
18
|
+
var environmentFileName = "agentknight.env";
|
|
19
|
+
var migrationMarker = ".legacy-home-migrated";
|
|
20
|
+
function hasCode(error, code) {
|
|
21
|
+
return error instanceof Error && "code" in error && error.code === code;
|
|
22
|
+
}
|
|
23
|
+
async function exists(path) {
|
|
24
|
+
try {
|
|
25
|
+
await lstat(path);
|
|
26
|
+
return true;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (hasCode(error, "ENOENT")) return false;
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function environmentAssignment(key, value) {
|
|
33
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(key)) {
|
|
34
|
+
throw new Error("Cannot safely migrate an environment variable name; select the original file with --env.");
|
|
35
|
+
}
|
|
36
|
+
for (const quote of ["'", '"', "`"]) {
|
|
37
|
+
const assignment = `${key}=${quote}${value}${quote}`;
|
|
38
|
+
const parsed = parseEnv(assignment);
|
|
39
|
+
if (parsed[key] === value && Object.keys(parsed).length === 1) return assignment;
|
|
40
|
+
}
|
|
41
|
+
throw new Error(`Cannot safely quote ${key} in agentknight.env; select the original file with --env.`);
|
|
42
|
+
}
|
|
43
|
+
function connectionRefused(error) {
|
|
44
|
+
if (hasCode(error, "ECONNREFUSED")) return true;
|
|
45
|
+
if (error instanceof AggregateError) return error.errors.length > 0 && error.errors.every(connectionRefused);
|
|
46
|
+
return error instanceof Error && "cause" in error && connectionRefused(error.cause);
|
|
47
|
+
}
|
|
48
|
+
async function requireStoppedLegacyHome(legacyHome, environments) {
|
|
49
|
+
const instruction = `Stop the legacy PC Agent/service before migrating ${legacyHome}, then retry. For a headless or unverifiable instance, keep using an explicit AGENTKNIGHT_HOME or copy its stopped data to .ak manually and explicitly set AGENTKNIGHT_HOME to that copy.`;
|
|
50
|
+
for (const name of ["pc-agent.lock", "instance.lock", "agent.lock", join("run", "instance.lock")]) {
|
|
51
|
+
if (await exists(join(legacyHome, name))) throw new Error(`Legacy home contains ${name}. ${instruction}`);
|
|
52
|
+
}
|
|
53
|
+
if (environments.some((environment) => environment.PAIRING_PAGE_ENABLED === "false")) {
|
|
54
|
+
throw new Error(`Cannot verify that the headless legacy PC Agent is stopped. ${instruction}`);
|
|
55
|
+
}
|
|
56
|
+
const ports = /* @__PURE__ */ new Set([defaultPairingPagePort]);
|
|
57
|
+
for (const environment of environments) {
|
|
58
|
+
if (environment.PAIRING_PAGE_PORT === void 0) continue;
|
|
59
|
+
const port = Number(environment.PAIRING_PAGE_PORT);
|
|
60
|
+
if (!Number.isInteger(port) || port < 1024 || port > 65535) {
|
|
61
|
+
throw new Error(`Cannot verify the legacy pairing-page port. ${instruction}`);
|
|
62
|
+
}
|
|
63
|
+
ports.add(port);
|
|
64
|
+
}
|
|
65
|
+
for (const port of ports) {
|
|
66
|
+
try {
|
|
67
|
+
const response = await fetch(`http://127.0.0.1:${port}/health`, {
|
|
68
|
+
signal: AbortSignal.timeout(1e3),
|
|
69
|
+
redirect: "manual"
|
|
70
|
+
});
|
|
71
|
+
await response.body?.cancel();
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (connectionRefused(error)) continue;
|
|
74
|
+
throw new Error(`Cannot verify whether the legacy PC Agent is running on port ${port}. ${instruction}`, { cause: error });
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`A local service is running on legacy port ${port}. ${instruction}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function fileDigest(path) {
|
|
80
|
+
const hash = createHash("sha256");
|
|
81
|
+
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
|
82
|
+
return hash.digest("hex");
|
|
83
|
+
}
|
|
84
|
+
async function filesIn(directory, relative = "") {
|
|
85
|
+
const result = /* @__PURE__ */ new Map();
|
|
86
|
+
const info = await lstat(directory);
|
|
87
|
+
if (!info.isDirectory() || info.isSymbolicLink()) {
|
|
88
|
+
throw new Error(`Cannot safely migrate a linked or non-directory home: ${directory}`);
|
|
89
|
+
}
|
|
90
|
+
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
|
91
|
+
const path = join(directory, entry.name);
|
|
92
|
+
const key = join(relative, entry.name);
|
|
93
|
+
if (entry.isDirectory()) {
|
|
94
|
+
result.set(key, null);
|
|
95
|
+
for (const [child, data] of await filesIn(path, key)) result.set(child, data);
|
|
96
|
+
} else if (entry.isFile()) {
|
|
97
|
+
result.set(key, await fileDigest(path));
|
|
98
|
+
} else {
|
|
99
|
+
throw new Error(`Cannot safely migrate a linked or special file: ${path}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
async function legacyHomeMigrated(legacyHome, canonicalHome) {
|
|
105
|
+
const marker = join(canonicalHome, migrationMarker);
|
|
106
|
+
if (await exists(canonicalHome)) {
|
|
107
|
+
const info = await lstat(canonicalHome);
|
|
108
|
+
if (!info.isDirectory() || info.isSymbolicLink()) {
|
|
109
|
+
throw new Error(`Cannot safely migrate a linked or non-directory home: ${canonicalHome}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (await exists(marker)) {
|
|
113
|
+
if (!(await lstat(marker)).isFile()) throw new Error(`Unsafe migration marker in ${canonicalHome}`);
|
|
114
|
+
if (await readFile(marker, "utf8") !== legacyHome) {
|
|
115
|
+
throw new Error(`Unrecognized migration marker in ${canonicalHome}; review the homes before starting.`);
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
async function migrateLegacyHome(legacyHome, canonicalHome, environments) {
|
|
122
|
+
if (await legacyHomeMigrated(legacyHome, canonicalHome)) return;
|
|
123
|
+
if (!await exists(legacyHome)) return;
|
|
124
|
+
await requireStoppedLegacyHome(legacyHome, environments);
|
|
125
|
+
const source = await filesIn(legacyHome);
|
|
126
|
+
if (source.has(migrationMarker)) throw new Error(`Unexpected migration marker in ${legacyHome}`);
|
|
127
|
+
const destination = await exists(canonicalHome) ? await filesIn(canonicalHome) : /* @__PURE__ */ new Map();
|
|
128
|
+
for (const [name, data] of destination) {
|
|
129
|
+
if (name === environmentFileName || name === "service" || name.startsWith(`service${sep}`) || name === "logs" && data === null || name === join("logs", "service") || name.startsWith(join("logs", "service") + sep)) continue;
|
|
130
|
+
if (source.get(name) !== data) {
|
|
131
|
+
throw new Error(`Conflicting legacy and canonical AgentKnight homes: ${legacyHome} and ${canonicalHome}. Choose the correct home with AGENTKNIGHT_HOME or reconcile them while both agents are stopped.`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
await mkdir(canonicalHome, { recursive: true, mode: 448 });
|
|
135
|
+
for (const [name, data] of source) {
|
|
136
|
+
if (name === environmentFileName || destination.has(name)) continue;
|
|
137
|
+
const target = join(canonicalHome, name);
|
|
138
|
+
if (data === null) {
|
|
139
|
+
await mkdir(target, { recursive: true, mode: 448 });
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
await mkdir(dirname(target), { recursive: true, mode: 448 });
|
|
143
|
+
await copyFile(join(legacyHome, name), target, constants.COPYFILE_EXCL);
|
|
144
|
+
await chmod(target, 384);
|
|
145
|
+
if (await fileDigest(target) !== data) {
|
|
146
|
+
throw new Error(`Legacy state changed during migration; stop the old agent and reconcile ${canonicalHome}.`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
await requireStoppedLegacyHome(legacyHome, environments);
|
|
150
|
+
const after = await filesIn(legacyHome);
|
|
151
|
+
if (source.size !== after.size || [...source].some(([name, data]) => after.get(name) !== data)) {
|
|
152
|
+
throw new Error(`Legacy state changed during migration; stop the old agent and reconcile ${canonicalHome}.`);
|
|
153
|
+
}
|
|
154
|
+
await writeFile(join(canonicalHome, migrationMarker), legacyHome, { flag: "wx", mode: 384 });
|
|
155
|
+
}
|
|
156
|
+
async function resolveClientEnvironment(options = {}) {
|
|
157
|
+
if (options.noEnv && options.envFile !== void 0) throw new Error("--no-env cannot be combined with --env.");
|
|
158
|
+
const cwd = resolve(options.cwd ?? process.cwd());
|
|
159
|
+
const home = resolve(options.homeDirectory ?? homedir());
|
|
160
|
+
const canonicalHome = defaultClientHome(home);
|
|
161
|
+
const legacyHome = join(home, ".copilot-remote");
|
|
162
|
+
const explicitEnvFile = options.envFile === void 0 ? void 0 : resolve(cwd, options.envFile);
|
|
163
|
+
const warnings = /* @__PURE__ */ new Set();
|
|
164
|
+
const inherited = { ...options.environment ?? process.env };
|
|
165
|
+
if (explicitEnvFile !== void 0) clearClientConfiguration(inherited);
|
|
166
|
+
const normalize = (source) => normalizeClientEnvironment(source, cwd, (message) => warnings.add(message));
|
|
167
|
+
const supplied = normalize(inherited);
|
|
168
|
+
const suppliedHome = supplied.AGENTKNIGHT_HOME?.trim();
|
|
169
|
+
if (suppliedHome === "") throw new Error("AGENTKNIGHT_HOME must not be empty");
|
|
170
|
+
if (options.noEnv) {
|
|
171
|
+
const dataDirectory2 = resolve(cwd, suppliedHome ?? canonicalHome);
|
|
172
|
+
return {
|
|
173
|
+
environment: { ...supplied, AGENTKNIGHT_HOME: dataDirectory2 },
|
|
174
|
+
dataDirectory: dataDirectory2,
|
|
175
|
+
...warnings.size === 0 ? {} : { warnings: [...warnings] }
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const configHome = resolve(cwd, suppliedHome ?? canonicalHome);
|
|
179
|
+
const canonicalEnv = join(configHome, environmentFileName);
|
|
180
|
+
const legacyServiceEnv = join(home, "Library", "Application Support", "AgentKnight", environmentFileName);
|
|
181
|
+
let envFile = explicitEnvFile;
|
|
182
|
+
let text = "";
|
|
183
|
+
let fileEnvironment = {};
|
|
184
|
+
let fallbackEnv = false;
|
|
185
|
+
if (envFile !== void 0) {
|
|
186
|
+
if (!await exists(envFile)) throw new Error(`Environment file does not exist: ${envFile}`);
|
|
187
|
+
} else if (await exists(canonicalEnv)) {
|
|
188
|
+
envFile = canonicalEnv;
|
|
189
|
+
} else if (suppliedHome === void 0) {
|
|
190
|
+
for (const candidate of /* @__PURE__ */ new Set([
|
|
191
|
+
legacyServiceEnv,
|
|
192
|
+
join(legacyHome, environmentFileName),
|
|
193
|
+
join(cwd, ".env"),
|
|
194
|
+
resolve(cwd, "..", "..", ".env")
|
|
195
|
+
])) {
|
|
196
|
+
if (!await exists(candidate)) continue;
|
|
197
|
+
const candidateText = await readFile(candidate, "utf8");
|
|
198
|
+
const parsed = normalize(parseEnv(candidateText));
|
|
199
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
200
|
+
if (fileEnvironment[key] !== void 0 && fileEnvironment[key] !== value) {
|
|
201
|
+
throw new Error(`Conflicting legacy configuration for ${key}; select one file with --env or reconcile the files.`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
fileEnvironment = { ...fileEnvironment, ...parsed };
|
|
205
|
+
text += (text === "" ? "" : "\n") + candidateText;
|
|
206
|
+
envFile ??= candidate;
|
|
207
|
+
fallbackEnv = true;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (envFile !== void 0 && !fallbackEnv) {
|
|
211
|
+
text = await readFile(envFile, "utf8");
|
|
212
|
+
fileEnvironment = normalize(parseEnv(text));
|
|
213
|
+
}
|
|
214
|
+
const environment = explicitEnvFile === void 0 ? { ...fileEnvironment } : { ...supplied };
|
|
215
|
+
for (const [key, value] of Object.entries(explicitEnvFile === void 0 ? supplied : fileEnvironment)) {
|
|
216
|
+
if (value !== void 0) environment[key] = value;
|
|
217
|
+
}
|
|
218
|
+
const fileHome = fileEnvironment.AGENTKNIGHT_HOME?.trim();
|
|
219
|
+
const configuredHome = environment.AGENTKNIGHT_HOME?.trim();
|
|
220
|
+
if (configuredHome === "") throw new Error("AGENTKNIGHT_HOME must not be empty");
|
|
221
|
+
const fallbackLegacyHome = fallbackEnv && fileHome !== void 0 && sameClientPath(fileHome, legacyHome, cwd);
|
|
222
|
+
const canonicalDefaultHome = explicitEnvFile === void 0 && envFile === canonicalEnv && fileHome !== void 0 && sameClientPath(fileHome, canonicalHome, cwd);
|
|
223
|
+
const implicitHome = configuredHome === void 0 || (fallbackLegacyHome || canonicalDefaultHome) && suppliedHome === void 0;
|
|
224
|
+
let dataDirectory = configuredHome !== void 0 && !implicitHome ? resolve(cwd, configuredHome) : canonicalHome;
|
|
225
|
+
const migrationPending = options.migrate === false && implicitHome && !await legacyHomeMigrated(legacyHome, canonicalHome) && await exists(legacyHome);
|
|
226
|
+
if (migrationPending && !await exists(canonicalHome)) {
|
|
227
|
+
dataDirectory = legacyHome;
|
|
228
|
+
}
|
|
229
|
+
if (options.migrate !== false) {
|
|
230
|
+
if (fallbackEnv && envFile === legacyServiceEnv) {
|
|
231
|
+
await requireStoppedLegacyHome(dataDirectory, [fileEnvironment, environment]);
|
|
232
|
+
}
|
|
233
|
+
if (implicitHome) {
|
|
234
|
+
await migrateLegacyHome(legacyHome, canonicalHome, [fileEnvironment, environment]);
|
|
235
|
+
}
|
|
236
|
+
if (fallbackEnv) {
|
|
237
|
+
await mkdir(canonicalHome, { recursive: true, mode: 448 });
|
|
238
|
+
let canonicalText = hasDeprecatedClientEnvironment(parseEnv(text)) ? Object.entries(fileEnvironment).flatMap(([key, value]) => value === void 0 ? [] : [environmentAssignment(key, value)]).join("\n") + "\n" : text;
|
|
239
|
+
if (fileHome !== void 0 && fileHome.length > 0) {
|
|
240
|
+
const persistedHome = fallbackLegacyHome ? canonicalHome : resolve(cwd, fileHome);
|
|
241
|
+
canonicalText += `
|
|
242
|
+
${environmentAssignment("AGENTKNIGHT_HOME", persistedHome)}
|
|
243
|
+
`;
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
await writeFile(canonicalEnv, canonicalText, { flag: "wx", mode: 384 });
|
|
247
|
+
} catch (error) {
|
|
248
|
+
if (hasCode(error, "EEXIST")) {
|
|
249
|
+
throw new Error(`Canonical configuration already exists: ${canonicalEnv}. Retry to load it without overwriting.`);
|
|
250
|
+
}
|
|
251
|
+
throw error;
|
|
252
|
+
}
|
|
253
|
+
envFile = canonicalEnv;
|
|
254
|
+
warnings.add("Imported legacy configuration into ~/.ak/agentknight.env; legacy files are retained only for rollback.");
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
environment.AGENTKNIGHT_HOME = dataDirectory;
|
|
258
|
+
return {
|
|
259
|
+
environment,
|
|
260
|
+
...envFile === void 0 ? {} : { envFile },
|
|
261
|
+
dataDirectory,
|
|
262
|
+
...migrationPending ? { migrationPending: true } : {},
|
|
263
|
+
...warnings.size === 0 ? {} : { warnings: [...warnings] }
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
export {
|
|
267
|
+
resolveClientEnvironment
|
|
268
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
SESSION_TIMELINE_PAGE_SIZE
|
|
4
|
+
} from "./chunk-HWCSV7QP.js";
|
|
5
|
+
|
|
6
|
+
// src/local-sessions.ts
|
|
7
|
+
import { supportsAgentSelection, codingAgentPresentation } from "@agentknight/sdk";
|
|
8
|
+
function createLocalSessions(adapter, owner) {
|
|
9
|
+
return {
|
|
10
|
+
async list() {
|
|
11
|
+
if (supportsAgentSelection(adapter)) {
|
|
12
|
+
return (await adapter.listAgents()).flatMap((agent) => agent.sessions.map((session) => ({ ...session, agentName: agent.name })));
|
|
13
|
+
}
|
|
14
|
+
return (await adapter.listSessions(owner.userId, owner.machineId)).map((session) => ({
|
|
15
|
+
id: session.sessionId,
|
|
16
|
+
name: session.summary || session.sessionId,
|
|
17
|
+
agentName: codingAgentPresentation(adapter.agentId).name,
|
|
18
|
+
...session.workingDirectory === void 0 ? {} : { workingDirectory: session.workingDirectory }
|
|
19
|
+
}));
|
|
20
|
+
},
|
|
21
|
+
readPage(sessionId, cursor) {
|
|
22
|
+
const options = { pageSize: SESSION_TIMELINE_PAGE_SIZE, ...cursor === void 0 ? {} : { cursor } };
|
|
23
|
+
return supportsAgentSelection(adapter) && adapter.readLocalSessionTimelinePage !== void 0 ? adapter.readLocalSessionTimelinePage(sessionId, options) : adapter.readSessionTimelinePage(sessionId, options);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export {
|
|
28
|
+
createLocalSessions
|
|
29
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
RuntimeCommandError,
|
|
4
|
+
RuntimeStatusSchema,
|
|
5
|
+
processExists,
|
|
6
|
+
readRuntimeDescriptor,
|
|
7
|
+
requestPeerChat,
|
|
8
|
+
requestRuntime,
|
|
9
|
+
startRuntimeControl
|
|
10
|
+
} from "./chunk-WZIU4HVH.js";
|
|
11
|
+
import "./chunk-SPZWIFFP.js";
|
|
12
|
+
export {
|
|
13
|
+
RuntimeCommandError,
|
|
14
|
+
RuntimeStatusSchema,
|
|
15
|
+
processExists,
|
|
16
|
+
readRuntimeDescriptor,
|
|
17
|
+
requestPeerChat,
|
|
18
|
+
requestRuntime,
|
|
19
|
+
startRuntimeControl
|
|
20
|
+
};
|