@agentconnect/cli 0.1.6 → 0.2.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/dist/index.js CHANGED
@@ -1,4146 +1,38 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/index.ts
4
- import path13 from "path";
5
- import { promises as fs7 } from "fs";
6
- import { createPrivateKey, createPublicKey as createPublicKey2, sign as signData } from "crypto";
7
-
8
- // src/host.ts
9
- import http from "http";
10
- import { WebSocketServer } from "ws";
11
- import { spawn as spawn5 } from "child_process";
12
- import fs2 from "fs";
13
- import { promises as fsp } from "fs";
14
- import net from "net";
15
- import path6 from "path";
16
-
17
- // src/providers/claude.ts
18
- import { spawn as spawn2 } from "child_process";
19
- import { access, mkdir, readFile, rm, writeFile } from "fs/promises";
20
- import https from "https";
21
- import os2 from "os";
22
- import path2 from "path";
23
-
24
- // src/providers/utils.ts
25
- import { spawn } from "child_process";
26
- import { existsSync, realpathSync } from "fs";
27
- import os from "os";
28
- import path from "path";
29
- var DEBUG_ENABLED = Boolean(process.env.AGENTCONNECT_DEBUG?.trim());
30
- function debugLog(scope, message, details) {
31
- if (!DEBUG_ENABLED) return;
32
- let suffix = "";
33
- if (details) {
34
- try {
35
- suffix = ` ${JSON.stringify(details)}`;
36
- } catch {
37
- suffix = "";
38
- }
39
- }
40
- console.log(`[AgentConnect][${scope}] ${message}${suffix}`);
41
- }
42
- function splitCommand(value) {
43
- if (!value) return { command: "", args: [] };
44
- if (Array.isArray(value)) return { command: value[0] ?? "", args: value.slice(1) };
45
- const input = String(value).trim();
46
- const parts = [];
47
- let current = "";
48
- let quote = null;
49
- for (let i = 0; i < input.length; i += 1) {
50
- const char = input[i];
51
- if (quote) {
52
- if (char === quote) {
53
- quote = null;
54
- } else {
55
- current += char;
56
- }
57
- continue;
58
- }
59
- if (char === '"' || char === "'") {
60
- quote = char;
61
- continue;
62
- }
63
- if (char === " ") {
64
- if (current) {
65
- parts.push(current);
66
- current = "";
67
- }
68
- continue;
69
- }
70
- current += char;
71
- }
72
- if (current) parts.push(current);
73
- return { command: parts[0] ?? "", args: parts.slice(1) };
74
- }
75
- function resolveWindowsCommand(command2) {
76
- if (process.platform !== "win32") return command2;
77
- if (!command2) return command2;
78
- if (command2.endsWith(".cmd") || command2.endsWith(".exe") || command2.includes("\\")) {
79
- return command2;
80
- }
81
- return `${command2}.cmd`;
82
- }
83
- function getCommonBinPaths() {
84
- const home = os.homedir();
85
- const bunInstall = process.env.BUN_INSTALL || path.join(home, ".bun");
86
- const pnpmHome = process.env.PNPM_HOME || path.join(home, "Library", "pnpm");
87
- const npmPrefix = process.env.NPM_CONFIG_PREFIX;
88
- const npmBin = npmPrefix ? path.join(npmPrefix, "bin") : "";
89
- const claudeLocal = process.env.CLAUDE_CONFIG_DIR ? path.join(process.env.CLAUDE_CONFIG_DIR, "local") : path.join(home, ".claude", "local");
90
- return [
91
- path.join(bunInstall, "bin"),
92
- pnpmHome,
93
- path.join(home, ".local", "bin"),
94
- claudeLocal,
95
- path.join(home, ".claude", "bin"),
96
- npmBin,
97
- "/opt/homebrew/bin",
98
- "/usr/local/bin",
99
- "/usr/bin",
100
- "/bin"
101
- ].filter(Boolean);
102
- }
103
- function getCommandCandidates(command2) {
104
- if (process.platform !== "win32") return [command2];
105
- if (command2.endsWith(".cmd") || command2.endsWith(".exe") || command2.endsWith(".bat")) {
106
- return [command2];
107
- }
108
- return [command2, `${command2}.cmd`, `${command2}.exe`, `${command2}.bat`];
109
- }
110
- function resolveCommandPath(command2) {
111
- if (!command2) return null;
112
- if (command2.includes("/") || command2.includes("\\")) {
113
- return existsSync(command2) ? command2 : null;
114
- }
115
- const candidates = getCommandCandidates(command2);
116
- const searchPaths = /* @__PURE__ */ new Set();
117
- const pathEntries = process.env.PATH ? process.env.PATH.split(path.delimiter) : [];
118
- for (const entry of pathEntries) {
119
- if (entry) searchPaths.add(entry);
120
- }
121
- for (const entry of getCommonBinPaths()) {
122
- if (entry) searchPaths.add(entry);
123
- }
124
- for (const dir of searchPaths) {
125
- for (const candidate of candidates) {
126
- const fullPath = path.join(dir, candidate);
127
- if (existsSync(fullPath)) return fullPath;
128
- }
129
- }
130
- return null;
131
- }
132
- function resolveCommandRealPath(command2) {
133
- const resolved = resolveCommandPath(command2);
134
- if (!resolved) return null;
135
- try {
136
- return realpathSync(resolved);
137
- } catch {
138
- return resolved;
139
- }
140
- }
141
- function commandExists(command2) {
142
- return Boolean(resolveCommandPath(command2));
143
- }
144
- function runCommand(command2, args2, options = {}) {
145
- return new Promise((resolve) => {
146
- const { input, timeoutMs, ...spawnOptions } = options;
147
- if (!command2) {
148
- resolve({ code: -1, stdout: "", stderr: "Command is empty" });
149
- return;
150
- }
151
- const resolved = resolveCommandPath(command2);
152
- if (!resolved) {
153
- resolve({ code: 127, stdout: "", stderr: `Executable not found in PATH: "${command2}"` });
154
- return;
155
- }
156
- const startedAt = Date.now();
157
- debugLog("Command", "run", {
158
- command: resolved,
159
- args: args2,
160
- startedAt: new Date(startedAt).toISOString()
161
- });
162
- const child = spawn(resolved, args2, {
163
- ...spawnOptions,
164
- stdio: ["pipe", "pipe", "pipe"]
165
- });
166
- let stdout = "";
167
- let stderr = "";
168
- let timeout;
169
- if (typeof timeoutMs === "number" && timeoutMs > 0) {
170
- timeout = setTimeout(() => {
171
- child.kill();
172
- resolve({ code: -1, stdout, stderr: `${stderr}Command timed out` });
173
- }, timeoutMs);
174
- }
175
- if (input) {
176
- child.stdin?.write(input);
177
- }
178
- child.stdin?.end();
179
- child.stdout?.on("data", (chunk) => {
180
- stdout += chunk.toString("utf8");
181
- });
182
- child.stderr?.on("data", (chunk) => {
183
- stderr += chunk.toString("utf8");
184
- });
185
- child.on("error", (err) => {
186
- if (timeout) clearTimeout(timeout);
187
- debugLog("Command", "result", {
188
- command: resolved,
189
- code: -1,
190
- durationMs: Date.now() - startedAt,
191
- error: err.message
192
- });
193
- resolve({ code: -1, stdout, stderr: `${stderr}${err.message}` });
194
- });
195
- child.on("close", (code) => {
196
- if (timeout) clearTimeout(timeout);
197
- debugLog("Command", "result", {
198
- command: resolved,
199
- code: code ?? 0,
200
- durationMs: Date.now() - startedAt
201
- });
202
- resolve({ code: code ?? 0, stdout, stderr });
203
- });
204
- });
205
- }
206
- function createLineParser(onLine) {
207
- let buffer = "";
208
- return (chunk) => {
209
- const text = typeof chunk === "string" ? chunk : chunk.toString("utf8");
210
- buffer += text;
211
- let idx;
212
- while ((idx = buffer.indexOf("\n")) !== -1) {
213
- const line = buffer.slice(0, idx).trim();
214
- buffer = buffer.slice(idx + 1);
215
- if (line) onLine(line);
216
- }
217
- };
218
- }
219
- async function checkCommandVersion(command2, argsList) {
220
- for (const args2 of argsList) {
221
- const result = await runCommand(command2, args2);
222
- if (result.code === 0) {
223
- const version = result.stdout.trim().split("\n")[0] ?? "";
224
- return { ok: true, version };
225
- }
226
- }
227
- return { ok: false, version: "" };
228
- }
229
- var packageManagerCache = { detected: null };
230
- async function isCommandAvailable(command2) {
231
- const result = await runCommand(command2, ["--version"]);
232
- return result.code === 0;
233
- }
234
- async function detectPackageManager() {
235
- if (packageManagerCache.detected) {
236
- return packageManagerCache.detected;
237
- }
238
- if (await isCommandAvailable("bun")) {
239
- packageManagerCache.detected = "bun";
240
- return "bun";
241
- }
242
- if (await isCommandAvailable("pnpm")) {
243
- packageManagerCache.detected = "pnpm";
244
- return "pnpm";
245
- }
246
- if (await isCommandAvailable("npm")) {
247
- packageManagerCache.detected = "npm";
248
- return "npm";
249
- }
250
- if (process.platform === "darwin" && await isCommandAvailable("brew")) {
251
- packageManagerCache.detected = "brew";
252
- return "brew";
253
- }
254
- packageManagerCache.detected = "unknown";
255
- return "unknown";
256
- }
257
- function getInstallCommand(packageManager, packageName) {
258
- switch (packageManager) {
259
- case "bun":
260
- return { command: "bun", args: ["add", "-g", packageName] };
261
- case "pnpm":
262
- return { command: "pnpm", args: ["add", "-g", packageName] };
263
- case "npm":
264
- return { command: "npm", args: ["install", "-g", packageName] };
265
- case "brew":
266
- return { command: "brew", args: ["install", packageName] };
267
- default:
268
- return { command: "", args: [] };
269
- }
270
- }
271
- async function buildInstallCommandAuto(packageName) {
272
- const pm = await detectPackageManager();
273
- const cmd = getInstallCommand(pm, packageName);
274
- return { ...cmd, packageManager: pm };
275
- }
276
- function buildInstallCommand(envVar, fallback) {
277
- const value = process.env[envVar] || fallback;
278
- return splitCommand(value);
279
- }
280
- function buildLoginCommand(envVar, fallback) {
281
- const value = process.env[envVar] || fallback;
282
- return splitCommand(value);
283
- }
284
- function buildStatusCommand(envVar, fallback) {
285
- const value = process.env[envVar] || fallback;
286
- return splitCommand(value);
287
- }
288
-
289
- // src/providers/claude.ts
290
- var CLAUDE_PACKAGE = "@anthropic-ai/claude-code";
291
- var INSTALL_UNIX = "curl -fsSL https://claude.ai/install.sh | bash";
292
- var INSTALL_WINDOWS_PS = "irm https://claude.ai/install.ps1 | iex";
293
- var INSTALL_WINDOWS_CMD = "curl -fsSL https://claude.ai/install.cmd -o install.cmd && install.cmd && del install.cmd";
294
- var DEFAULT_LOGIN = "";
295
- var DEFAULT_STATUS = "";
296
- var CLAUDE_MODELS_CACHE_TTL_MS = 6e4;
297
- var CLAUDE_RECENT_MODELS_CACHE_TTL_MS = 6e4;
298
- var CLAUDE_UPDATE_CACHE_TTL_MS = 6 * 60 * 60 * 1e3;
299
- var claudeModelsCache = null;
300
- var claudeModelsCacheAt = 0;
301
- var claudeRecentModelsCache = null;
302
- var claudeRecentModelsCacheAt = 0;
303
- var claudeUpdateCache = null;
304
- var claudeUpdatePromise = null;
305
- var CLAUDE_LOGIN_CACHE_TTL_MS = 3e4;
306
- var claudeLoginCache = null;
307
- var claudeLoginPromise = null;
308
- function trimOutput(value, limit = 400) {
309
- const cleaned = value.trim();
310
- if (!cleaned) return "";
311
- if (cleaned.length <= limit) return cleaned;
312
- return `${cleaned.slice(0, limit)}...`;
313
- }
314
- function normalizePath(value) {
315
- const normalized = value.replace(/\\/g, "/");
316
- return process.platform === "win32" ? normalized.toLowerCase() : normalized;
317
- }
318
- function parseSemver(value) {
319
- if (!value) return null;
320
- const match = value.match(/(\d+)\.(\d+)\.(\d+)/);
321
- if (!match) return null;
322
- return [Number(match[1]), Number(match[2]), Number(match[3])];
323
- }
324
- function compareSemver(a, b) {
325
- if (a[0] !== b[0]) return a[0] - b[0];
326
- if (a[1] !== b[1]) return a[1] - b[1];
327
- return a[2] - b[2];
328
- }
329
- async function fetchLatestNpmVersion(pkg) {
330
- const encoded = encodeURIComponent(pkg);
331
- const data = await fetchJson(`https://registry.npmjs.org/${encoded}`);
332
- if (!data || typeof data !== "object") return null;
333
- const latest = data["dist-tags"]?.latest;
334
- return typeof latest === "string" ? latest : null;
335
- }
336
- async function fetchBrewCaskVersion(cask) {
337
- if (!commandExists("brew")) return null;
338
- const result = await runCommand("brew", ["info", "--json=v2", "--cask", cask]);
339
- if (result.code !== 0) return null;
340
- try {
341
- const parsed = JSON.parse(result.stdout);
342
- const version = parsed?.casks?.[0]?.version;
343
- return typeof version === "string" ? version : null;
344
- } catch {
345
- return null;
346
- }
347
- }
348
- function getClaudeUpdateAction(commandPath) {
349
- if (!commandPath) return null;
350
- const normalized = normalizePath(commandPath);
351
- const home = normalizePath(os2.homedir());
352
- if (normalized.startsWith(`${home}/.bun/bin/`)) {
353
- return {
354
- command: "bun",
355
- args: ["install", "-g", CLAUDE_PACKAGE],
356
- source: "bun",
357
- commandLabel: "bun install -g @anthropic-ai/claude-code"
358
- };
359
- }
360
- if (normalized.includes("/node_modules/.bin/")) {
361
- return {
362
- command: "npm",
363
- args: ["install", "-g", CLAUDE_PACKAGE],
364
- source: "npm",
365
- commandLabel: "npm install -g @anthropic-ai/claude-code"
366
- };
367
- }
368
- if (normalized.includes("/cellar/") || normalized.includes("/caskroom/") || normalized.includes("/homebrew/")) {
369
- return {
370
- command: "brew",
371
- args: ["upgrade", "--cask", "claude-code"],
372
- source: "brew",
373
- commandLabel: "brew upgrade --cask claude-code"
374
- };
375
- }
376
- if (process.platform === "win32" && (normalized.includes("/program files/claudecode") || normalized.includes("/programdata/claudecode"))) {
377
- return {
378
- command: "winget",
379
- args: ["upgrade", "Anthropic.ClaudeCode"],
380
- source: "winget",
381
- commandLabel: "winget upgrade Anthropic.ClaudeCode"
382
- };
383
- }
384
- if (normalized.includes("/.local/bin/") || normalized.includes("/.local/share/claude/versions/") || normalized.includes("/.local/share/claude-code/versions/")) {
385
- if (process.platform === "win32") {
386
- return {
387
- command: "powershell",
388
- args: ["-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", INSTALL_WINDOWS_PS],
389
- source: "script",
390
- commandLabel: INSTALL_WINDOWS_PS
391
- };
392
- }
393
- return {
394
- command: "bash",
395
- args: ["-lc", INSTALL_UNIX],
396
- source: "script",
397
- commandLabel: INSTALL_UNIX
398
- };
399
- }
400
- return null;
401
- }
402
- var DEFAULT_CLAUDE_MODELS = [
403
- {
404
- id: "default",
405
- displayName: "Default \xB7 Opus 4.5"
406
- },
407
- {
408
- id: "sonnet",
409
- displayName: "Sonnet 4.5"
410
- },
411
- {
412
- id: "haiku",
413
- displayName: "Haiku 4.5"
414
- },
415
- {
416
- id: "opus",
417
- displayName: "Opus"
418
- }
419
- ];
420
- function getClaudeCommand() {
421
- const override = process.env.AGENTCONNECT_CLAUDE_COMMAND;
422
- const base = override || "claude";
423
- const resolved = resolveCommandPath(base);
424
- return resolved || resolveWindowsCommand(base);
425
- }
426
- function getClaudeConfigDir() {
427
- return process.env.CLAUDE_CONFIG_DIR || path2.join(os2.homedir(), ".claude");
428
- }
429
- function fetchJson(url) {
430
- return new Promise((resolve) => {
431
- https.get(url, (res) => {
432
- let data = "";
433
- res.on("data", (chunk) => {
434
- data += chunk;
435
- });
436
- res.on("end", () => {
437
- try {
438
- resolve(JSON.parse(data));
439
- } catch {
440
- resolve(null);
441
- }
442
- });
443
- }).on("error", () => resolve(null));
444
- });
445
- }
446
- function formatClaudeDisplayName(modelId) {
447
- const value = modelId.trim();
448
- if (!value.startsWith("claude-")) return value;
449
- const parts = value.replace(/^claude-/, "").split("-").filter(Boolean);
450
- if (!parts.length) return value;
451
- const family = parts[0];
452
- const numeric = parts.slice(1).filter((entry) => /^\d+$/.test(entry));
453
- let version = "";
454
- if (numeric.length >= 2) {
455
- version = `${numeric[0]}.${numeric[1]}`;
456
- } else if (numeric.length === 1) {
457
- version = numeric[0];
458
- }
459
- const familyLabel = family.charAt(0).toUpperCase() + family.slice(1);
460
- return `Claude ${familyLabel}${version ? ` ${version}` : ""}`;
461
- }
462
- async function readClaudeStatsModels() {
463
- const statsPath = path2.join(getClaudeConfigDir(), "stats-cache.json");
464
- try {
465
- const raw = await readFile(statsPath, "utf8");
466
- const parsed = JSON.parse(raw);
467
- const usage = parsed?.modelUsage;
468
- if (!usage || typeof usage !== "object") return [];
469
- return Object.keys(usage).filter(Boolean);
470
- } catch {
471
- return [];
472
- }
473
- }
474
- async function listClaudeModels() {
475
- if (claudeModelsCache && Date.now() - claudeModelsCacheAt < CLAUDE_MODELS_CACHE_TTL_MS) {
476
- return claudeModelsCache;
477
- }
478
- const list = DEFAULT_CLAUDE_MODELS.map((entry) => ({
479
- id: entry.id,
480
- provider: "claude",
481
- displayName: entry.displayName
482
- }));
483
- claudeModelsCache = list;
484
- claudeModelsCacheAt = Date.now();
485
- return list;
486
- }
487
- async function listClaudeRecentModels() {
488
- if (claudeRecentModelsCache && Date.now() - claudeRecentModelsCacheAt < CLAUDE_RECENT_MODELS_CACHE_TTL_MS) {
489
- return claudeRecentModelsCache;
490
- }
491
- const discovered = await readClaudeStatsModels();
492
- const mapped = [];
493
- const seen = /* @__PURE__ */ new Set();
494
- for (const modelId of discovered) {
495
- const id = modelId.trim();
496
- if (!id || seen.has(id)) continue;
497
- seen.add(id);
498
- mapped.push({
499
- id,
500
- provider: "claude",
501
- displayName: formatClaudeDisplayName(id)
502
- });
503
- }
504
- claudeRecentModelsCache = mapped;
505
- claudeRecentModelsCacheAt = Date.now();
506
- return mapped;
507
- }
508
- function getClaudeAuthPaths() {
509
- const home = os2.homedir();
510
- return [
511
- path2.join(home, ".claude.json"),
512
- path2.join(home, ".claude", "settings.json"),
513
- path2.join(home, ".config", "claude", "auth.json")
514
- ];
515
- }
516
- function resolveClaudeTheme() {
517
- const raw = process.env.AGENTCONNECT_CLAUDE_THEME;
518
- return raw && raw.trim() ? raw.trim() : "dark";
519
- }
520
- function resolveClaudeLoginMethod(options) {
521
- const raw = options?.loginMethod ?? process.env.AGENTCONNECT_CLAUDE_LOGIN_METHOD;
522
- if (!raw) return "claudeai";
523
- const normalized = String(raw).trim().toLowerCase();
524
- if (normalized === "console") return "console";
525
- if (normalized === "claudeai" || normalized === "claude") return "claudeai";
526
- return "claudeai";
527
- }
528
- function resolveClaudeLoginExperience(options) {
529
- const raw = options?.loginExperience ?? process.env.AGENTCONNECT_CLAUDE_LOGIN_EXPERIENCE ?? process.env.AGENTCONNECT_LOGIN_EXPERIENCE;
530
- if (raw) {
531
- const normalized = String(raw).trim().toLowerCase();
532
- if (normalized === "terminal" || normalized === "manual") return "terminal";
533
- if (normalized === "embedded" || normalized === "pty") return "embedded";
534
- }
535
- if (process.env.AGENTCONNECT_HOST_MODE === "dev") return "terminal";
536
- return "embedded";
537
- }
538
- async function resolveClaudeLoginHint(options) {
539
- const raw = process.env.AGENTCONNECT_CLAUDE_LOGIN_HINT;
540
- if (raw) {
541
- const normalized = String(raw).trim().toLowerCase();
542
- if (normalized === "setup") return "setup";
543
- if (normalized === "login") return "login";
544
- }
545
- if (options?.loginExperience) {
546
- }
547
- const status = await checkClaudeCliStatus();
548
- return status.loginHint ?? "login";
549
- }
550
- async function createClaudeLoginSettingsFile(loginMethod) {
551
- if (!loginMethod) return null;
552
- const fileName = `agentconnect-claude-login-${Date.now()}-${Math.random().toString(36).slice(2, 8)}.json`;
553
- const filePath = path2.join(os2.tmpdir(), fileName);
554
- const theme = resolveClaudeTheme();
555
- const payload = {
556
- forceLoginMethod: loginMethod,
557
- theme,
558
- hasCompletedOnboarding: true
559
- };
560
- await writeFile(filePath, JSON.stringify(payload), "utf8");
561
- return filePath;
562
- }
563
- async function ensureClaudeOnboardingSettings() {
564
- const configDir = process.env.CLAUDE_CONFIG_DIR || path2.join(os2.homedir(), ".claude");
565
- const settingsPath = path2.join(configDir, "settings.json");
566
- let settings = {};
567
- try {
568
- const raw = await readFile(settingsPath, "utf8");
569
- try {
570
- settings = JSON.parse(raw);
571
- } catch {
572
- return;
573
- }
574
- } catch (err) {
575
- const code = err?.code;
576
- if (code && code !== "ENOENT") return;
577
- }
578
- let updated = false;
579
- if (!settings.theme) {
580
- settings.theme = resolveClaudeTheme();
581
- updated = true;
582
- }
583
- if (settings.hasCompletedOnboarding !== true) {
584
- settings.hasCompletedOnboarding = true;
585
- updated = true;
586
- }
587
- if (!updated) return;
588
- await mkdir(configDir, { recursive: true });
589
- await writeFile(settingsPath, `${JSON.stringify(settings, null, 2)}
590
- `, "utf8");
591
- }
592
- async function loadPtyModule() {
593
- try {
594
- const mod = await import("node-pty");
595
- if (typeof mod.spawn === "function") return mod;
596
- if (mod.default && typeof mod.default.spawn === "function") return mod.default;
597
- return null;
598
- } catch {
599
- return null;
600
- }
601
- }
602
- function shellEscape(value) {
603
- return `'${value.replace(/'/g, `'\\''`)}'`;
604
- }
605
- function cmdEscape(value) {
606
- if (!value) return '""';
607
- const escaped = value.replace(/"/g, '""');
608
- return `"${escaped}"`;
609
- }
610
- function buildClaudeCommand(command2, args2, includeLogin = false) {
611
- const parts = includeLogin ? [command2, ...args2, "/login"] : [command2, ...args2];
612
- return parts.map(shellEscape).join(" ");
613
- }
614
- function buildClaudeCmd(command2, args2, includeLogin = false) {
615
- const parts = includeLogin ? [command2, ...args2, "/login"] : [command2, ...args2];
616
- return parts.map(cmdEscape).join(" ");
617
- }
618
- async function openClaudeLoginTerminal(command2, args2, includeLogin = false) {
619
- const message = "AgentConnect: complete Claude login in this terminal. If login does not start automatically, run /login.";
620
- if (process.platform === "win32") {
621
- const cmdLine = `echo ${message} & ${buildClaudeCmd(command2, args2, includeLogin)}`;
622
- await runCommand("cmd", ["/c", "start", "", "cmd", "/k", cmdLine], { shell: true });
623
- return;
624
- }
625
- const loginCommand = buildClaudeCommand(command2, args2, includeLogin);
626
- const shellCommand = `printf "%s\\n\\n" ${shellEscape(message)}; ${loginCommand}`;
627
- if (process.platform === "darwin") {
628
- const script = `tell application "Terminal"
629
- activate
630
- do script "${shellCommand.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"
631
- end tell`;
632
- await runCommand("osascript", ["-e", script]);
633
- return;
634
- }
635
- if (commandExists("x-terminal-emulator")) {
636
- await runCommand("x-terminal-emulator", ["-e", "bash", "-lc", shellCommand]);
637
- return;
638
- }
639
- if (commandExists("gnome-terminal")) {
640
- await runCommand("gnome-terminal", ["--", "bash", "-lc", shellCommand]);
641
- return;
642
- }
643
- if (commandExists("konsole")) {
644
- await runCommand("konsole", ["-e", "bash", "-lc", shellCommand]);
645
- return;
646
- }
647
- if (commandExists("xterm")) {
648
- await runCommand("xterm", ["-e", "bash", "-lc", shellCommand]);
649
- return;
650
- }
651
- throw new Error("No terminal emulator found to launch Claude login.");
652
- }
653
- function maybeAdvanceClaudeOnboarding(output, loginMethod, write) {
654
- const text = output.toLowerCase();
655
- if (text.includes("select login method") || text.includes("claude account with subscription")) {
656
- if (loginMethod === "console") {
657
- write("\x1B[B");
658
- }
659
- write("\r");
660
- return true;
661
- }
662
- if (text.includes("choose the text style") || text.includes("text style that looks best")) {
663
- write("\r");
664
- return true;
665
- }
666
- if (text.includes("press enter") || text.includes("enter to confirm")) {
667
- write("\r");
668
- return true;
669
- }
670
- return false;
671
- }
672
- async function hasClaudeAuth() {
673
- if (typeof process.env.CLAUDE_CODE_OAUTH_TOKEN === "string") {
674
- return process.env.CLAUDE_CODE_OAUTH_TOKEN.trim().length > 0;
675
- }
676
- for (const filePath of getClaudeAuthPaths()) {
677
- try {
678
- await access(filePath);
679
- const raw = await readFile(filePath, "utf8");
680
- const parsed = JSON.parse(raw);
681
- const auth = parsed?.claudeAiOauth;
682
- if (typeof auth?.accessToken === "string" && auth.accessToken.trim()) {
683
- return true;
684
- }
685
- if (typeof parsed.primaryApiKey === "string" && parsed.primaryApiKey.trim()) {
686
- return true;
687
- }
688
- if (typeof parsed.accessToken === "string" && parsed.accessToken.trim()) {
689
- return true;
690
- }
691
- if (typeof parsed.token === "string" && parsed.token.trim()) {
692
- return true;
693
- }
694
- const oauthAccount = parsed.oauthAccount;
695
- if (typeof oauthAccount?.emailAddress === "string" && oauthAccount.emailAddress.trim()) {
696
- return true;
697
- }
698
- if (typeof oauthAccount?.accountUuid === "string" && oauthAccount.accountUuid.trim()) {
699
- return true;
700
- }
701
- const oauth = parsed.oauth;
702
- if (typeof oauth?.access_token === "string" && oauth.access_token.trim()) {
703
- return true;
704
- }
705
- } catch {
706
- }
707
- }
708
- return false;
709
- }
710
- function isClaudeAuthErrorText(value) {
711
- const text = value.toLowerCase();
712
- return text.includes("authentication_error") || text.includes("authentication_failed") || text.includes("oauth token has expired") || text.includes("token has expired") || text.includes("please run /login") || text.includes("unauthorized") || text.includes("api error: 401") || text.includes("status 401") || text.includes("invalid api key");
713
- }
714
- function extractClaudeMessageText(content) {
715
- if (Array.isArray(content)) {
716
- return content.map((part) => part?.text ?? "").join(" ");
717
- }
718
- return typeof content === "string" ? content : "";
719
- }
720
- function resolveClaudeLoginHintFromSource(apiKeySource) {
721
- if (apiKeySource && apiKeySource.toLowerCase() === "none") return "setup";
722
- return "login";
723
- }
724
- async function checkClaudeCliStatus() {
725
- if (claudeLoginCache && Date.now() - claudeLoginCache.checkedAt < CLAUDE_LOGIN_CACHE_TTL_MS) {
726
- return claudeLoginCache.status;
727
- }
728
- if (claudeLoginPromise) return claudeLoginPromise;
729
- claudeLoginPromise = (async () => {
730
- const command2 = resolveWindowsCommand(getClaudeCommand());
731
- const args2 = [
732
- "--print",
733
- "--output-format",
734
- "stream-json",
735
- "--no-session-persistence",
736
- "--max-budget-usd",
737
- "0.01"
738
- ];
739
- const result = await runCommand(command2, args2, {
740
- env: { ...process.env, CI: "1" },
741
- input: "ping\n",
742
- timeoutMs: 8e3
743
- });
744
- const output = `${result.stdout}
745
- ${result.stderr}`.trim();
746
- if (!output) return { loggedIn: null };
747
- let apiKeySource;
748
- let authError = null;
749
- let sawAssistant = false;
750
- let sawSuccess = false;
751
- const lines = output.split("\n");
752
- for (const line of lines) {
753
- const parsed = safeJsonParse(line);
754
- if (!parsed || typeof parsed !== "object") continue;
755
- const record = parsed;
756
- const type = typeof record.type === "string" ? record.type : "";
757
- if (type === "system" && record.subtype === "init") {
758
- const source = typeof record.apiKeySource === "string" ? record.apiKeySource : void 0;
759
- if (source) apiKeySource = source;
760
- }
761
- if (type === "result") {
762
- const isError = Boolean(record.is_error);
763
- const resultText = typeof record.result === "string" ? record.result : typeof record.error === "string" ? record.error : JSON.stringify(record.error || record);
764
- if (isError && isClaudeAuthErrorText(resultText)) {
765
- authError = authError ?? resultText;
766
- }
767
- if (!isError) sawSuccess = true;
768
- }
769
- if (type === "message") {
770
- const message = record.message;
771
- const role = typeof message?.role === "string" ? message.role : "";
772
- if (role === "assistant") {
773
- const text = extractClaudeMessageText(message?.content);
774
- const errorText = typeof record.error === "string" ? record.error : typeof message?.error === "string" ? message.error : "";
775
- if (isClaudeAuthErrorText(text) || errorText && isClaudeAuthErrorText(errorText)) {
776
- authError = authError ?? (text || errorText);
777
- } else if (text.trim()) {
778
- sawAssistant = true;
779
- }
780
- }
781
- }
782
- }
783
- if (authError) {
784
- return {
785
- loggedIn: false,
786
- apiKeySource,
787
- loginHint: resolveClaudeLoginHintFromSource(apiKeySource),
788
- authError
789
- };
790
- }
791
- if (sawAssistant || sawSuccess) {
792
- return { loggedIn: true, apiKeySource };
793
- }
794
- if (apiKeySource && apiKeySource.toLowerCase() === "none") {
795
- return { loggedIn: false, apiKeySource, loginHint: "setup" };
796
- }
797
- return { loggedIn: null, apiKeySource };
798
- })().then((status) => {
799
- claudeLoginCache = { checkedAt: Date.now(), status };
800
- return status;
801
- }).finally(() => {
802
- claudeLoginPromise = null;
803
- });
804
- return claudeLoginPromise;
805
- }
806
- function getClaudeUpdateSnapshot(commandPath) {
807
- if (claudeUpdateCache && Date.now() - claudeUpdateCache.checkedAt < CLAUDE_UPDATE_CACHE_TTL_MS) {
808
- const action = getClaudeUpdateAction(commandPath);
809
- return {
810
- updateAvailable: claudeUpdateCache.updateAvailable,
811
- latestVersion: claudeUpdateCache.latestVersion,
812
- updateCheckedAt: claudeUpdateCache.checkedAt,
813
- updateSource: action?.source ?? "unknown",
814
- updateCommand: action?.commandLabel,
815
- updateMessage: claudeUpdateCache.updateMessage
816
- };
817
- }
818
- return {};
819
- }
820
- function ensureClaudeUpdateCheck(currentVersion, commandPath) {
821
- if (claudeUpdateCache && Date.now() - claudeUpdateCache.checkedAt < CLAUDE_UPDATE_CACHE_TTL_MS) {
822
- return;
823
- }
824
- if (claudeUpdatePromise) return;
825
- claudeUpdatePromise = (async () => {
826
- const action = getClaudeUpdateAction(commandPath || null);
827
- let latest = null;
828
- let updateAvailable;
829
- let updateMessage;
830
- if (action?.source === "npm" || action?.source === "bun") {
831
- latest = await fetchLatestNpmVersion(CLAUDE_PACKAGE);
832
- } else if (action?.source === "brew") {
833
- latest = await fetchBrewCaskVersion("claude-code");
834
- }
835
- if (latest && currentVersion) {
836
- const a = parseSemver(currentVersion);
837
- const b = parseSemver(latest);
838
- if (a && b) {
839
- updateAvailable = compareSemver(a, b) < 0;
840
- updateMessage = updateAvailable ? `Update available: ${currentVersion} -> ${latest}` : `Up to date (${currentVersion})`;
841
- }
842
- } else if (!action) {
843
- updateMessage = "Update check unavailable";
844
- }
845
- debugLog("Claude", "update-check", {
846
- updateAvailable,
847
- message: updateMessage
848
- });
849
- claudeUpdateCache = {
850
- checkedAt: Date.now(),
851
- updateAvailable,
852
- latestVersion: latest ?? void 0,
853
- updateMessage
854
- };
855
- })().finally(() => {
856
- claudeUpdatePromise = null;
857
- });
858
- }
859
- async function ensureClaudeInstalled() {
860
- const command2 = getClaudeCommand();
861
- const versionCheck = await checkCommandVersion(command2, [["--version"], ["-V"]]);
862
- if (versionCheck.ok) {
863
- return { installed: true, version: versionCheck.version || void 0 };
864
- }
865
- if (commandExists(command2)) {
866
- return { installed: true, version: void 0 };
867
- }
868
- const override = buildInstallCommand("AGENTCONNECT_CLAUDE_INSTALL", "");
869
- let install = override;
870
- let packageManager = override.command ? "unknown" : "unknown";
871
- if (!install.command) {
872
- if (process.platform === "win32") {
873
- if (commandExists("powershell")) {
874
- install = {
875
- command: "powershell",
876
- args: ["-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", INSTALL_WINDOWS_PS]
877
- };
878
- packageManager = "script";
879
- } else if (commandExists("pwsh")) {
880
- install = {
881
- command: "pwsh",
882
- args: ["-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", INSTALL_WINDOWS_PS]
883
- };
884
- packageManager = "script";
885
- } else if (commandExists("cmd") && commandExists("curl")) {
886
- install = { command: "cmd", args: ["/c", INSTALL_WINDOWS_CMD] };
887
- packageManager = "script";
888
- }
889
- } else if (commandExists("bash") && commandExists("curl")) {
890
- install = { command: "bash", args: ["-lc", INSTALL_UNIX] };
891
- packageManager = "script";
892
- } else {
893
- const auto = await buildInstallCommandAuto(CLAUDE_PACKAGE);
894
- install = { command: auto.command, args: auto.args };
895
- packageManager = auto.packageManager;
896
- }
897
- }
898
- if (!install.command) {
899
- return { installed: false, version: void 0, packageManager };
900
- }
901
- await runCommand(install.command, install.args, { shell: process.platform === "win32" });
902
- const after = await checkCommandVersion(command2, [["--version"], ["-V"]]);
903
- return {
904
- installed: after.ok,
905
- version: after.version || void 0,
906
- packageManager
907
- };
908
- }
909
- async function getClaudeStatus() {
910
- const command2 = getClaudeCommand();
911
- const versionCheck = await checkCommandVersion(command2, [["--version"], ["-V"]]);
912
- const installed = versionCheck.ok || commandExists(command2);
913
- let loggedIn = false;
914
- if (installed) {
915
- const status = buildStatusCommand("AGENTCONNECT_CLAUDE_STATUS", DEFAULT_STATUS);
916
- if (status.command) {
917
- const statusCommand = resolveWindowsCommand(status.command);
918
- const result = await runCommand(statusCommand, status.args);
919
- loggedIn = result.code === 0;
920
- } else {
921
- const cliStatus = await checkClaudeCliStatus();
922
- if (cliStatus.loggedIn === false) {
923
- loggedIn = false;
924
- } else if (cliStatus.loggedIn === true) {
925
- loggedIn = true;
926
- } else if (cliStatus.apiKeySource?.toLowerCase() === "none") {
927
- loggedIn = false;
928
- } else {
929
- loggedIn = await hasClaudeAuth();
930
- }
931
- }
932
- }
933
- if (installed) {
934
- const resolved2 = resolveCommandRealPath(command2);
935
- ensureClaudeUpdateCheck(versionCheck.version, resolved2 || null);
936
- }
937
- const resolved = resolveCommandRealPath(command2);
938
- const updateInfo = installed ? getClaudeUpdateSnapshot(resolved || null) : {};
939
- return { installed, loggedIn, version: versionCheck.version || void 0, ...updateInfo };
940
- }
941
- async function getClaudeFastStatus() {
942
- const command2 = getClaudeCommand();
943
- const installed = commandExists(command2);
944
- const loggedIn = installed ? await hasClaudeAuth() : false;
945
- return { installed, loggedIn };
946
- }
947
- async function updateClaude() {
948
- const command2 = getClaudeCommand();
949
- if (!commandExists(command2)) {
950
- return { installed: false, loggedIn: false };
951
- }
952
- const resolved = resolveCommandRealPath(command2);
953
- const updateOverride = buildStatusCommand("AGENTCONNECT_CLAUDE_UPDATE", "");
954
- const action = updateOverride.command ? null : getClaudeUpdateAction(resolved || null);
955
- const updateCommand = updateOverride.command || action?.command || "";
956
- const updateArgs = updateOverride.command ? updateOverride.args : action?.args || [];
957
- if (!updateCommand) {
958
- throw new Error("No update command available. Please update Claude manually.");
959
- }
960
- const cmd = resolveWindowsCommand(updateCommand);
961
- debugLog("Claude", "update-run", { command: cmd, args: updateArgs });
962
- const result = await runCommand(cmd, updateArgs, { env: { ...process.env, CI: "1" } });
963
- debugLog("Claude", "update-result", {
964
- code: result.code,
965
- stdout: trimOutput(result.stdout),
966
- stderr: trimOutput(result.stderr)
967
- });
968
- if (result.code !== 0 && result.code !== null) {
969
- const message = trimOutput(`${result.stdout}
970
- ${result.stderr}`, 800) || "Update failed";
971
- throw new Error(message);
972
- }
973
- claudeUpdateCache = null;
974
- claudeUpdatePromise = null;
975
- return getClaudeStatus();
976
- }
977
- async function loginClaude(options) {
978
- const login = buildLoginCommand("AGENTCONNECT_CLAUDE_LOGIN", DEFAULT_LOGIN);
979
- if (login.command) {
980
- const command2 = resolveWindowsCommand(login.command);
981
- await runCommand(command2, login.args);
982
- } else {
983
- await runClaudeLoginFlow(options);
984
- }
985
- const status = await getClaudeStatus();
986
- return { loggedIn: status.loggedIn };
987
- }
988
- async function runClaudeLoginFlow(options) {
989
- const command2 = resolveWindowsCommand(getClaudeCommand());
990
- const loginMethod = resolveClaudeLoginMethod(options);
991
- const loginExperience = resolveClaudeLoginExperience(options);
992
- const loginHint = await resolveClaudeLoginHint(options);
993
- await ensureClaudeOnboardingSettings();
994
- const settingsPath = await createClaudeLoginSettingsFile(loginMethod);
995
- const loginTimeoutMs = Number(process.env.AGENTCONNECT_CLAUDE_LOGIN_TIMEOUT_MS || 18e4);
996
- const loginArgs = settingsPath ? ["--settings", settingsPath] : [];
997
- const includeLogin = loginHint === "login";
998
- let ptyProcess = null;
999
- let childExited = false;
1000
- const cleanup = async () => {
1001
- if (ptyProcess) {
1002
- try {
1003
- ptyProcess.kill();
1004
- } catch {
1005
- }
1006
- }
1007
- if (settingsPath && loginExperience !== "terminal") {
1008
- try {
1009
- await rm(settingsPath, { force: true });
1010
- } catch {
1011
- }
1012
- }
1013
- };
1014
- try {
1015
- if (loginExperience === "terminal") {
1016
- await openClaudeLoginTerminal(command2, loginArgs, includeLogin);
1017
- if (settingsPath) {
1018
- setTimeout(() => {
1019
- rm(settingsPath, { force: true }).catch(() => {
1020
- });
1021
- }, loginTimeoutMs);
1022
- }
1023
- } else {
1024
- const ptyModule = await loadPtyModule();
1025
- if (!ptyModule) {
1026
- throw new Error(
1027
- "Claude login requires node-pty. Reinstall AgentConnect or run `claude /login` manually."
1028
- );
1029
- }
1030
- const spawnArgs = includeLogin ? [...loginArgs, "/login"] : loginArgs;
1031
- ptyProcess = ptyModule.spawn(command2, spawnArgs, {
1032
- name: "xterm-256color",
1033
- cols: 100,
1034
- rows: 30,
1035
- cwd: os2.homedir(),
1036
- env: { ...process.env, TERM: "xterm-256color" }
1037
- });
1038
- let outputBuffer = "";
1039
- ptyProcess.onData((data) => {
1040
- outputBuffer += data;
1041
- if (outputBuffer.length > 6e3) {
1042
- outputBuffer = outputBuffer.slice(-3e3);
1043
- }
1044
- const advanced = maybeAdvanceClaudeOnboarding(outputBuffer, loginMethod, (input) => {
1045
- ptyProcess?.write(input);
1046
- });
1047
- if (advanced) outputBuffer = "";
1048
- });
1049
- ptyProcess.onExit(() => {
1050
- childExited = true;
1051
- });
1052
- }
1053
- const start = Date.now();
1054
- while (Date.now() - start < loginTimeoutMs) {
1055
- const authed = await hasClaudeAuth();
1056
- if (authed) {
1057
- return;
1058
- }
1059
- if (childExited) {
1060
- break;
1061
- }
1062
- await new Promise((resolve) => setTimeout(resolve, 1e3));
1063
- }
1064
- throw new Error(
1065
- "Claude login timed out. Finish login in your browser or run `claude` manually to authenticate."
1066
- );
1067
- } finally {
1068
- await cleanup();
1069
- }
1070
- }
1071
- function safeJsonParse(line) {
1072
- try {
1073
- return JSON.parse(line);
1074
- } catch {
1075
- return null;
1076
- }
1077
- }
1078
- function mapClaudeModel(model) {
1079
- if (!model) return null;
1080
- const value = String(model).toLowerCase();
1081
- if (value === "default" || value === "recommended") return null;
1082
- if (value === "claude-default" || value === "claude-recommended") return null;
1083
- if (value.includes("opus")) return "opus";
1084
- if (value.includes("sonnet")) return "sonnet";
1085
- if (value.includes("haiku")) return "haiku";
1086
- if (value.startsWith("claude-")) return value.replace("claude-", "");
1087
- return model;
1088
- }
1089
- function extractSessionId(msg) {
1090
- const direct = msg.session_id ?? msg.sessionId;
1091
- if (typeof direct === "string" && direct) return direct;
1092
- const nested = msg.message?.session_id ?? msg.message?.sessionId;
1093
- return typeof nested === "string" && nested ? nested : null;
1094
- }
1095
- function extractAssistantDelta(msg) {
1096
- const type = String(msg.type ?? "");
1097
- if (type === "stream_event") {
1098
- const ev = msg.event ?? {};
1099
- if (ev.type === "content_block_delta") {
1100
- const text2 = ev.delta?.text;
1101
- return typeof text2 === "string" && text2 ? text2 : null;
1102
- }
1103
- }
1104
- if (type === "content_block_delta") {
1105
- const text2 = msg.delta?.text;
1106
- return typeof text2 === "string" && text2 ? text2 : null;
1107
- }
1108
- const text = msg.delta?.text;
1109
- return typeof text === "string" && text ? text : null;
1110
- }
1111
- function extractTextFromContent(content) {
1112
- if (!content) return "";
1113
- if (typeof content === "string") return content;
1114
- if (Array.isArray(content)) {
1115
- return content.map((part) => {
1116
- if (!part || typeof part !== "object") return "";
1117
- const text = part.text;
1118
- if (typeof text === "string") return text;
1119
- return "";
1120
- }).filter(Boolean).join("");
1121
- }
1122
- return "";
1123
- }
1124
- function extractAssistantText(msg) {
1125
- if (String(msg.type ?? "") !== "assistant") return null;
1126
- const content = msg.message?.content;
1127
- if (!Array.isArray(content)) return null;
1128
- const textBlock = content.find((c) => c && typeof c === "object" && c.type === "text");
1129
- const text = textBlock?.text;
1130
- return typeof text === "string" && text ? text : null;
1131
- }
1132
- function extractResultText(msg) {
1133
- if (String(msg.type ?? "") !== "result") return null;
1134
- const text = msg.result;
1135
- return typeof text === "string" && text ? text : null;
1136
- }
1137
- function runClaudePrompt({
1138
- prompt,
1139
- resumeSessionId,
1140
- model,
1141
- cwd,
1142
- providerDetailLevel,
1143
- onEvent,
1144
- signal
1145
- }) {
1146
- return new Promise((resolve) => {
1147
- const command2 = getClaudeCommand();
1148
- const args2 = [
1149
- "-p",
1150
- "--output-format=stream-json",
1151
- "--verbose",
1152
- "--permission-mode",
1153
- "bypassPermissions"
1154
- ];
1155
- const modelValue = mapClaudeModel(model);
1156
- if (modelValue) {
1157
- args2.push("--model", modelValue);
1158
- }
1159
- if (resumeSessionId) args2.push("--resume", resumeSessionId);
1160
- args2.push(prompt);
1161
- const child = spawn2(command2, args2, {
1162
- cwd,
1163
- env: { ...process.env },
1164
- stdio: ["ignore", "pipe", "pipe"]
1165
- });
1166
- if (signal) {
1167
- signal.addEventListener("abort", () => {
1168
- child.kill("SIGTERM");
1169
- });
1170
- }
1171
- let aggregated = "";
1172
- let finalSessionId = null;
1173
- let didFinalize = false;
1174
- let sawError = false;
1175
- const toolBlocks = /* @__PURE__ */ new Map();
1176
- const thinkingBlocks = /* @__PURE__ */ new Set();
1177
- const includeRaw = providerDetailLevel === "raw";
1178
- const buildProviderDetail = (eventType, data, raw) => {
1179
- const detail = { eventType };
1180
- if (data && Object.keys(data).length) detail.data = data;
1181
- if (includeRaw && raw !== void 0) detail.raw = raw;
1182
- return detail;
1183
- };
1184
- const emit = (event) => {
1185
- if (finalSessionId) {
1186
- onEvent({ ...event, providerSessionId: finalSessionId });
1187
- } else {
1188
- onEvent(event);
1189
- }
1190
- };
1191
- const emitError = (message) => {
1192
- if (sawError) return;
1193
- sawError = true;
1194
- emit({ type: "error", message });
1195
- };
1196
- const emitFinal = (text, providerDetail) => {
1197
- emit({ type: "final", text, providerDetail });
1198
- };
1199
- const handleLine = (line) => {
1200
- const parsed = safeJsonParse(line);
1201
- if (!parsed || typeof parsed !== "object") {
1202
- if (line.trim()) {
1203
- emit({ type: "raw_line", line });
1204
- }
1205
- return;
1206
- }
1207
- const msg = parsed;
1208
- const sid = extractSessionId(msg);
1209
- if (sid) finalSessionId = sid;
1210
- const msgType = String(msg.type ?? "");
1211
- let handled = false;
1212
- if (msgType === "assistant" || msgType === "user" || msgType === "system") {
1213
- const role = msg.message?.role === "assistant" || msg.message?.role === "user" || msg.message?.role === "system" ? msg.message.role : msgType;
1214
- const rawContent = msg.message?.content;
1215
- const content = extractTextFromContent(rawContent);
1216
- emit({
1217
- type: "message",
1218
- provider: "claude",
1219
- role,
1220
- content,
1221
- contentParts: rawContent ?? null,
1222
- providerDetail: buildProviderDetail(msgType, {}, msg)
1223
- });
1224
- handled = true;
1225
- }
1226
- if (msgType === "stream_event" && msg.event) {
1227
- const evType = String(msg.event.type ?? "");
1228
- const index = typeof msg.event.index === "number" ? msg.event.index : void 0;
1229
- const block = msg.event.content_block;
1230
- if (evType === "content_block_start" && block && typeof index === "number") {
1231
- if (block.type === "tool_use" || block.type === "server_tool_use" || block.type === "mcp_tool_use") {
1232
- toolBlocks.set(index, { id: block.id, name: block.name });
1233
- emit({
1234
- type: "tool_call",
1235
- provider: "claude",
1236
- name: block.name,
1237
- callId: block.id,
1238
- input: block.input,
1239
- phase: "start",
1240
- providerDetail: buildProviderDetail(
1241
- "content_block_start",
1242
- { blockType: block.type, index, name: block.name, id: block.id },
1243
- msg
1244
- )
1245
- });
1246
- }
1247
- if (block.type === "thinking" || block.type === "redacted_thinking") {
1248
- thinkingBlocks.add(index);
1249
- emit({
1250
- type: "thinking",
1251
- provider: "claude",
1252
- phase: "start",
1253
- text: typeof block.thinking === "string" ? block.thinking : void 0,
1254
- providerDetail: buildProviderDetail(
1255
- "content_block_start",
1256
- { blockType: block.type, index },
1257
- msg
1258
- )
1259
- });
1260
- }
1261
- handled = true;
1262
- }
1263
- if (evType === "content_block_delta") {
1264
- const delta2 = msg.event.delta ?? {};
1265
- if (delta2.type === "thinking_delta") {
1266
- emit({
1267
- type: "thinking",
1268
- provider: "claude",
1269
- phase: "delta",
1270
- text: typeof delta2.thinking === "string" ? delta2.thinking : void 0,
1271
- providerDetail: buildProviderDetail(
1272
- "content_block_delta",
1273
- { deltaType: delta2.type, index },
1274
- msg
1275
- )
1276
- });
1277
- handled = true;
1278
- }
1279
- if (delta2.type === "input_json_delta") {
1280
- const tool = typeof index === "number" ? toolBlocks.get(index) : void 0;
1281
- emit({
1282
- type: "tool_call",
1283
- provider: "claude",
1284
- name: tool?.name,
1285
- callId: tool?.id,
1286
- input: delta2.partial_json,
1287
- phase: "delta",
1288
- providerDetail: buildProviderDetail(
1289
- "content_block_delta",
1290
- { deltaType: delta2.type, index, name: tool?.name, id: tool?.id },
1291
- msg
1292
- )
1293
- });
1294
- handled = true;
1295
- }
1296
- }
1297
- if (evType === "content_block_stop" && typeof index === "number") {
1298
- if (toolBlocks.has(index)) {
1299
- const tool = toolBlocks.get(index);
1300
- emit({
1301
- type: "tool_call",
1302
- provider: "claude",
1303
- name: tool?.name,
1304
- callId: tool?.id,
1305
- phase: "completed",
1306
- providerDetail: buildProviderDetail(
1307
- "content_block_stop",
1308
- { index, name: tool?.name, id: tool?.id },
1309
- msg
1310
- )
1311
- });
1312
- toolBlocks.delete(index);
1313
- }
1314
- if (thinkingBlocks.has(index)) {
1315
- emit({
1316
- type: "thinking",
1317
- provider: "claude",
1318
- phase: "completed",
1319
- providerDetail: buildProviderDetail("content_block_stop", { index }, msg)
1320
- });
1321
- thinkingBlocks.delete(index);
1322
- }
1323
- handled = true;
1324
- }
1325
- }
1326
- const delta = extractAssistantDelta(msg);
1327
- if (delta) {
1328
- aggregated += delta;
1329
- emit({
1330
- type: "delta",
1331
- text: delta,
1332
- providerDetail: buildProviderDetail(msgType || "delta", {}, msg)
1333
- });
1334
- return;
1335
- }
1336
- const assistant = extractAssistantText(msg);
1337
- if (assistant && !aggregated) {
1338
- aggregated = assistant;
1339
- emit({
1340
- type: "delta",
1341
- text: assistant,
1342
- providerDetail: buildProviderDetail(msgType || "assistant", {}, msg)
1343
- });
1344
- return;
1345
- }
1346
- const result = extractResultText(msg);
1347
- if (result && !didFinalize && !sawError) {
1348
- didFinalize = true;
1349
- emitFinal(aggregated || result, buildProviderDetail("result", {}, msg));
1350
- handled = true;
1351
- }
1352
- if (!handled) {
1353
- emit({
1354
- type: "detail",
1355
- provider: "claude",
1356
- providerDetail: buildProviderDetail(msgType || "unknown", {}, msg)
1357
- });
1358
- }
1359
- };
1360
- const stdoutParser = createLineParser(handleLine);
1361
- const stderrParser = createLineParser(handleLine);
1362
- child.stdout?.on("data", stdoutParser);
1363
- child.stderr?.on("data", stderrParser);
1364
- child.on("close", (code) => {
1365
- if (!didFinalize) {
1366
- if (code && code !== 0) {
1367
- emitError(`Claude exited with code ${code}`);
1368
- } else if (!sawError) {
1369
- emitFinal(aggregated);
1370
- }
1371
- }
1372
- resolve({ sessionId: finalSessionId });
1373
- });
1374
- child.on("error", (err) => {
1375
- emitError(err?.message ?? "Claude failed to start");
1376
- resolve({ sessionId: finalSessionId });
1377
- });
1378
- });
1379
- }
1380
-
1381
- // src/providers/codex.ts
1382
- import { spawn as spawn3 } from "child_process";
1383
- import { readFile as readFile2 } from "fs/promises";
1384
- import https2 from "https";
1385
- import os3 from "os";
1386
- import path3 from "path";
1387
- var CODEX_PACKAGE = "@openai/codex";
1388
- var DEFAULT_LOGIN2 = "codex login";
1389
- var DEFAULT_STATUS2 = "codex login status";
1390
- var CODEX_MODELS_CACHE_TTL_MS = 6e4;
1391
- var CODEX_UPDATE_CACHE_TTL_MS = 6 * 60 * 60 * 1e3;
1392
- var codexModelsCache = null;
1393
- var codexModelsCacheAt = 0;
1394
- var codexUpdateCache = null;
1395
- var codexUpdatePromise = null;
1396
- function trimOutput2(value, limit = 400) {
1397
- const cleaned = value.trim();
1398
- if (!cleaned) return "";
1399
- if (cleaned.length <= limit) return cleaned;
1400
- return `${cleaned.slice(0, limit)}...`;
1401
- }
1402
- function normalizePath2(value) {
1403
- const normalized = value.replace(/\\/g, "/");
1404
- return process.platform === "win32" ? normalized.toLowerCase() : normalized;
1405
- }
1406
- function getCodexConfigDir() {
1407
- return process.env.CODEX_CONFIG_DIR || path3.join(os3.homedir(), ".codex");
1408
- }
1409
- function fetchJson2(url) {
1410
- return new Promise((resolve) => {
1411
- https2.get(url, (res) => {
1412
- let data = "";
1413
- res.on("data", (chunk) => {
1414
- data += chunk;
1415
- });
1416
- res.on("end", () => {
1417
- try {
1418
- resolve(JSON.parse(data));
1419
- } catch {
1420
- resolve(null);
1421
- }
1422
- });
1423
- }).on("error", () => resolve(null));
1424
- });
1425
- }
1426
- function parseSemver2(value) {
1427
- if (!value) return null;
1428
- const match = value.match(/(\d+)\.(\d+)\.(\d+)/);
1429
- if (!match) return null;
1430
- return [Number(match[1]), Number(match[2]), Number(match[3])];
1431
- }
1432
- function compareSemver2(a, b) {
1433
- if (a[0] !== b[0]) return a[0] - b[0];
1434
- if (a[1] !== b[1]) return a[1] - b[1];
1435
- return a[2] - b[2];
1436
- }
1437
- async function fetchLatestNpmVersion2(pkg) {
1438
- const encoded = encodeURIComponent(pkg);
1439
- const data = await fetchJson2(`https://registry.npmjs.org/${encoded}`);
1440
- if (!data || typeof data !== "object") return null;
1441
- const latest = data["dist-tags"]?.latest;
1442
- return typeof latest === "string" ? latest : null;
1443
- }
1444
- async function fetchBrewFormulaVersion(formula) {
1445
- if (!commandExists("brew")) return null;
1446
- const result = await runCommand("brew", ["info", "--json=v2", formula]);
1447
- if (result.code !== 0) return null;
1448
- try {
1449
- const parsed = JSON.parse(result.stdout);
1450
- const version = parsed?.formulae?.[0]?.versions?.stable;
1451
- return typeof version === "string" ? version : null;
1452
- } catch {
1453
- return null;
1454
- }
1455
- }
1456
- function getCodexUpdateAction(commandPath) {
1457
- if (process.env.CODEX_MANAGED_BY_NPM) {
1458
- return {
1459
- command: "npm",
1460
- args: ["install", "-g", CODEX_PACKAGE],
1461
- source: "npm",
1462
- commandLabel: "npm install -g @openai/codex"
1463
- };
1464
- }
1465
- if (process.env.CODEX_MANAGED_BY_BUN) {
1466
- return {
1467
- command: "bun",
1468
- args: ["install", "-g", CODEX_PACKAGE],
1469
- source: "bun",
1470
- commandLabel: "bun install -g @openai/codex"
1471
- };
1472
- }
1473
- if (commandPath) {
1474
- const normalized = normalizePath2(commandPath);
1475
- if (normalized.includes(".bun/install/global")) {
1476
- return {
1477
- command: "bun",
1478
- args: ["install", "-g", CODEX_PACKAGE],
1479
- source: "bun",
1480
- commandLabel: "bun install -g @openai/codex"
1481
- };
1482
- }
1483
- if (normalized.includes("/node_modules/.bin/") || normalized.includes("/lib/node_modules/")) {
1484
- return {
1485
- command: "npm",
1486
- args: ["install", "-g", CODEX_PACKAGE],
1487
- source: "npm",
1488
- commandLabel: "npm install -g @openai/codex"
1489
- };
1490
- }
1491
- }
1492
- if (process.platform === "darwin" && commandPath && (commandPath.startsWith("/opt/homebrew") || commandPath.startsWith("/usr/local"))) {
1493
- return {
1494
- command: "brew",
1495
- args: ["upgrade", "codex"],
1496
- source: "brew",
1497
- commandLabel: "brew upgrade codex"
1498
- };
1499
- }
1500
- return null;
1501
- }
1502
- function getCodexUpdateSnapshot(commandPath) {
1503
- if (codexUpdateCache && Date.now() - codexUpdateCache.checkedAt < CODEX_UPDATE_CACHE_TTL_MS) {
1504
- const action = getCodexUpdateAction(commandPath);
1505
- return {
1506
- updateAvailable: codexUpdateCache.updateAvailable,
1507
- latestVersion: codexUpdateCache.latestVersion,
1508
- updateCheckedAt: codexUpdateCache.checkedAt,
1509
- updateSource: action?.source ?? "unknown",
1510
- updateCommand: action?.commandLabel,
1511
- updateMessage: codexUpdateCache.updateMessage
1512
- };
1513
- }
1514
- return {};
1515
- }
1516
- function ensureCodexUpdateCheck(currentVersion, commandPath) {
1517
- if (codexUpdateCache && Date.now() - codexUpdateCache.checkedAt < CODEX_UPDATE_CACHE_TTL_MS) {
1518
- return;
1519
- }
1520
- if (codexUpdatePromise) return;
1521
- codexUpdatePromise = (async () => {
1522
- const action = getCodexUpdateAction(commandPath || null);
1523
- let latest = null;
1524
- if (action?.source === "brew") {
1525
- latest = await fetchBrewFormulaVersion("codex");
1526
- } else {
1527
- latest = await fetchLatestNpmVersion2(CODEX_PACKAGE);
1528
- }
1529
- let updateAvailable;
1530
- let updateMessage;
1531
- if (latest && currentVersion) {
1532
- const a = parseSemver2(currentVersion);
1533
- const b = parseSemver2(latest);
1534
- if (a && b) {
1535
- updateAvailable = compareSemver2(a, b) < 0;
1536
- updateMessage = updateAvailable ? `Update available: ${currentVersion} -> ${latest}` : `Up to date (${currentVersion})`;
1537
- }
1538
- }
1539
- codexUpdateCache = {
1540
- checkedAt: Date.now(),
1541
- updateAvailable,
1542
- latestVersion: latest ?? void 0,
1543
- updateMessage
1544
- };
1545
- debugLog("Codex", "update-check", {
1546
- currentVersion,
1547
- latest,
1548
- updateAvailable,
1549
- message: updateMessage
1550
- });
1551
- })().finally(() => {
1552
- codexUpdatePromise = null;
1553
- });
1554
- }
1555
- function hasAuthValue(value) {
1556
- return typeof value === "string" && value.trim().length > 0;
1557
- }
1558
- async function hasCodexAuth() {
1559
- const home = os3.homedir();
1560
- const candidates = [
1561
- path3.join(getCodexConfigDir(), "auth.json"),
1562
- path3.join(home, ".config", "codex", "auth.json")
1563
- ];
1564
- for (const filePath of candidates) {
1565
- try {
1566
- const raw = await readFile2(filePath, "utf8");
1567
- const parsed = JSON.parse(raw);
1568
- if (hasAuthValue(parsed.OPENAI_API_KEY)) return true;
1569
- if (hasAuthValue(parsed.access_token)) return true;
1570
- if (hasAuthValue(parsed.token)) return true;
1571
- const tokens = parsed.tokens;
1572
- if (tokens) {
1573
- if (hasAuthValue(tokens.access_token)) return true;
1574
- if (hasAuthValue(tokens.refresh_token)) return true;
1575
- if (hasAuthValue(tokens.id_token)) return true;
1576
- }
1577
- } catch {
1578
- }
1579
- }
1580
- return false;
1581
- }
1582
- function buildCodexExecArgs(options) {
1583
- const { prompt, cdTarget, resumeSessionId, model, reasoningEffort, mode } = options;
1584
- const args2 = ["exec", "--skip-git-repo-check"];
1585
- if (mode === "legacy") {
1586
- args2.push("--json", "-C", cdTarget);
1587
- } else {
1588
- args2.push("--experimental-json", "--cd", cdTarget);
1589
- }
1590
- args2.push("--yolo");
1591
- if (model) {
1592
- args2.push("--model", String(model));
1593
- }
1594
- if (reasoningEffort) {
1595
- args2.push("--config", `model_reasoning_effort=${reasoningEffort}`);
1596
- }
1597
- if (resumeSessionId) {
1598
- args2.push("resume", resumeSessionId);
1599
- }
1600
- args2.push(prompt);
1601
- return args2;
1602
- }
1603
- function shouldFallbackToLegacy(lines) {
1604
- const combined = lines.join(" ").toLowerCase();
1605
- if (!(combined.includes("unknown flag") || combined.includes("unknown option") || combined.includes("unrecognized option") || combined.includes("unknown argument") || combined.includes("unexpected argument") || combined.includes("invalid option") || combined.includes("invalid argument"))) {
1606
- return false;
1607
- }
1608
- return combined.includes("experimental-json") || combined.includes("--cd");
1609
- }
1610
- function getCodexCommand() {
1611
- const override = process.env.AGENTCONNECT_CODEX_COMMAND;
1612
- const base = override || "codex";
1613
- const resolved = resolveCommandPath(base);
1614
- return resolved || resolveWindowsCommand(base);
1615
- }
1616
- async function ensureCodexInstalled() {
1617
- const command2 = getCodexCommand();
1618
- const versionCheck = await checkCommandVersion(command2, [["--version"], ["-V"]]);
1619
- if (versionCheck.ok) {
1620
- return { installed: true, version: versionCheck.version || void 0 };
1621
- }
1622
- if (commandExists(command2)) {
1623
- return { installed: true, version: void 0 };
1624
- }
1625
- const install = await buildInstallCommandAuto(CODEX_PACKAGE);
1626
- if (!install.command) {
1627
- return { installed: false, version: void 0, packageManager: install.packageManager };
1628
- }
1629
- debugLog("Codex", "install", { command: install.command, args: install.args });
1630
- const installResult = await runCommand(install.command, install.args, {
1631
- shell: process.platform === "win32"
1632
- });
1633
- debugLog("Codex", "install-result", {
1634
- code: installResult.code,
1635
- stderr: trimOutput2(installResult.stderr)
1636
- });
1637
- const after = await checkCommandVersion(command2, [["--version"], ["-V"]]);
1638
- codexModelsCache = null;
1639
- codexModelsCacheAt = 0;
1640
- return {
1641
- installed: after.ok,
1642
- version: after.version || void 0,
1643
- packageManager: install.packageManager
1644
- };
1645
- }
1646
- async function getCodexStatus() {
1647
- const command2 = getCodexCommand();
1648
- const versionCheck = await checkCommandVersion(command2, [["--version"], ["-V"]]);
1649
- const installed = versionCheck.ok || commandExists(command2);
1650
- let loggedIn = false;
1651
- let explicitLoggedOut = false;
1652
- if (installed) {
1653
- const status = buildStatusCommand("AGENTCONNECT_CODEX_STATUS", DEFAULT_STATUS2);
1654
- if (status.command) {
1655
- const statusCommand = resolveWindowsCommand(status.command);
1656
- const result = await runCommand(statusCommand, status.args, { env: { ...process.env, CI: "1" } });
1657
- const output = `${result.stdout}
1658
- ${result.stderr}`.toLowerCase();
1659
- if (output.includes("not logged in") || output.includes("not logged") || output.includes("login required") || output.includes("please login") || output.includes("run codex login")) {
1660
- explicitLoggedOut = true;
1661
- loggedIn = false;
1662
- } else if (output.includes("logged in") || output.includes("signed in") || output.includes("authenticated")) {
1663
- loggedIn = true;
1664
- } else {
1665
- loggedIn = result.code === 0;
1666
- }
1667
- }
1668
- if (!loggedIn && !explicitLoggedOut) {
1669
- loggedIn = await hasCodexAuth();
1670
- }
1671
- }
1672
- const resolved = resolveCommandRealPath(command2);
1673
- if (installed) {
1674
- ensureCodexUpdateCheck(versionCheck.version, resolved || null);
1675
- }
1676
- const updateInfo = installed ? getCodexUpdateSnapshot(resolved || null) : {};
1677
- return { installed, loggedIn, version: versionCheck.version || void 0, ...updateInfo };
1678
- }
1679
- async function getCodexFastStatus() {
1680
- const command2 = getCodexCommand();
1681
- const loggedIn = await hasCodexAuth();
1682
- const installed = commandExists(command2) || loggedIn;
1683
- return { installed, loggedIn };
1684
- }
1685
- async function updateCodex() {
1686
- const command2 = getCodexCommand();
1687
- if (!commandExists(command2)) {
1688
- return { installed: false, loggedIn: false };
1689
- }
1690
- const resolved = resolveCommandRealPath(command2);
1691
- const updateOverride = buildStatusCommand("AGENTCONNECT_CODEX_UPDATE", "");
1692
- const action = updateOverride.command ? null : getCodexUpdateAction(resolved || null);
1693
- const updateCommand = updateOverride.command || action?.command || "";
1694
- const updateArgs = updateOverride.command ? updateOverride.args : action?.args || [];
1695
- if (!updateCommand) {
1696
- throw new Error("No update command available. Please update Codex manually.");
1697
- }
1698
- const cmd = resolveWindowsCommand(updateCommand);
1699
- debugLog("Codex", "update-run", { command: cmd, args: updateArgs });
1700
- const result = await runCommand(cmd, updateArgs, {
1701
- env: { ...process.env, CI: "1" }
1702
- });
1703
- debugLog("Codex", "update-result", {
1704
- code: result.code,
1705
- stdout: trimOutput2(result.stdout),
1706
- stderr: trimOutput2(result.stderr)
1707
- });
1708
- if (result.code !== 0 && result.code !== null) {
1709
- const message = trimOutput2(`${result.stdout}
1710
- ${result.stderr}`, 800) || "Update failed";
1711
- throw new Error(message);
1712
- }
1713
- codexUpdateCache = null;
1714
- codexUpdatePromise = null;
1715
- return getCodexStatus();
1716
- }
1717
- async function loginCodex() {
1718
- const login = buildLoginCommand("AGENTCONNECT_CODEX_LOGIN", DEFAULT_LOGIN2);
1719
- if (!login.command) {
1720
- return { loggedIn: false };
1721
- }
1722
- const command2 = resolveWindowsCommand(login.command);
1723
- debugLog("Codex", "login", { command: command2, args: login.args });
1724
- const result = await runCommand(command2, login.args, { env: { ...process.env, CI: "1" } });
1725
- debugLog("Codex", "login-result", { code: result.code, stderr: trimOutput2(result.stderr) });
1726
- const status = await getCodexStatus();
1727
- codexModelsCache = null;
1728
- codexModelsCacheAt = 0;
1729
- return { loggedIn: status.loggedIn };
1730
- }
1731
- function safeJsonParse2(line) {
1732
- try {
1733
- return JSON.parse(line);
1734
- } catch {
1735
- return null;
1736
- }
1737
- }
1738
- function extractSessionId2(ev) {
1739
- const t = String(ev.type ?? "");
1740
- if (t === "thread.started") {
1741
- return typeof ev.thread_id === "string" ? ev.thread_id : null;
1742
- }
1743
- const id = ev.thread_id ?? ev.threadId ?? ev.session_id ?? ev.sessionId;
1744
- return typeof id === "string" ? id : null;
1745
- }
1746
- function extractUsage(ev) {
1747
- const usage = ev.usage ?? ev.token_usage ?? ev.tokens ?? ev.tokenUsage;
1748
- if (!usage || typeof usage !== "object") return null;
1749
- const toNumber = (v) => typeof v === "number" && Number.isFinite(v) ? v : void 0;
1750
- const input = toNumber(
1751
- usage.input_tokens ?? usage.prompt_tokens ?? usage.inputTokens ?? usage.promptTokens
1752
- );
1753
- const output = toNumber(
1754
- usage.output_tokens ?? usage.completion_tokens ?? usage.outputTokens ?? usage.completionTokens
1755
- );
1756
- const total = toNumber(usage.total_tokens ?? usage.totalTokens);
1757
- const cached = toNumber(usage.cached_input_tokens ?? usage.cachedInputTokens);
1758
- const out = {};
1759
- if (input !== void 0) out.input_tokens = input;
1760
- if (output !== void 0) out.output_tokens = output;
1761
- if (total !== void 0) out.total_tokens = total;
1762
- if (cached !== void 0) out.cached_input_tokens = cached;
1763
- return Object.keys(out).length ? out : null;
1764
- }
1765
- function normalizeItem(raw) {
1766
- if (!raw || typeof raw !== "object") return raw;
1767
- const item = raw;
1768
- const type = item.type;
1769
- const id = item.id;
1770
- if (type === "command_execution" && id) {
1771
- return {
1772
- id,
1773
- type,
1774
- command: item.command || "",
1775
- aggregated_output: item.aggregated_output,
1776
- exit_code: item.exit_code,
1777
- status: item.status
1778
- };
1779
- }
1780
- if (type === "reasoning" && id) {
1781
- return { id, type, text: item.text || "" };
1782
- }
1783
- if (type === "agent_message" && id) {
1784
- return { id, type, text: item.text || "" };
1785
- }
1786
- return raw;
1787
- }
1788
- function normalizeEvent(raw) {
1789
- const type = typeof raw.type === "string" ? raw.type : "unknown";
1790
- if (type === "item.started" || type === "item.completed") {
1791
- return { type, item: normalizeItem(raw.item) };
1792
- }
1793
- if (type === "agent_message") {
1794
- return { type, text: raw.text || "" };
1795
- }
1796
- if (type === "error") {
1797
- return { type, message: raw.message || "Unknown error" };
1798
- }
1799
- return { ...raw, type };
1800
- }
1801
- function isTerminalEvent(ev) {
1802
- const t = String(ev.type ?? "");
1803
- return t === "turn.completed" || t === "turn.failed";
1804
- }
1805
- function normalizeEffortId(raw) {
1806
- if (!raw) return null;
1807
- return String(raw).trim().toLowerCase();
1808
- }
1809
- function formatEffortLabel(id) {
1810
- if (!id) return "";
1811
- const normalized = String(id).trim().toLowerCase();
1812
- if (normalized === "xhigh") return "X-High";
1813
- if (normalized === "none") return "None";
1814
- if (normalized === "minimal") return "Minimal";
1815
- if (normalized === "low") return "Low";
1816
- if (normalized === "medium") return "Medium";
1817
- if (normalized === "high") return "High";
1818
- return normalized.toUpperCase();
1819
- }
1820
- function normalizeEffortOptions(raw) {
1821
- if (!Array.isArray(raw)) return [];
1822
- const options = raw.map((entry) => {
1823
- if (!entry || typeof entry !== "object") return null;
1824
- const opt = entry;
1825
- const id = normalizeEffortId(
1826
- opt.reasoning_effort ?? opt.reasoningEffort ?? opt.effort ?? opt.level
1827
- );
1828
- if (!id) return null;
1829
- const label = formatEffortLabel(id);
1830
- return { id, label };
1831
- }).filter((x) => x !== null);
1832
- const seen = /* @__PURE__ */ new Set();
1833
- return options.filter((option) => {
1834
- if (seen.has(option.id)) return false;
1835
- seen.add(option.id);
1836
- return true;
1837
- });
1838
- }
1839
- function normalizeCodexModels(raw) {
1840
- const response = raw;
1841
- const list = Array.isArray(response?.data) ? response.data : Array.isArray(response?.items) ? response.items : [];
1842
- const mapped = [];
1843
- for (const item of list) {
1844
- if (!item || typeof item !== "object") continue;
1845
- const id = item.id || item.model;
1846
- if (!id) continue;
1847
- const displayName = item.displayName || item.display_name || item.name || item.title || String(id);
1848
- const reasoningEfforts = normalizeEffortOptions(
1849
- item.supportedReasoningEfforts || item.supported_reasoning_efforts || item.supportedReasoningLevels || item.supported_reasoning_levels
1850
- );
1851
- const defaultReasoningEffort = normalizeEffortId(
1852
- item.defaultReasoningEffort || item.default_reasoning_effort
1853
- );
1854
- mapped.push({
1855
- id: String(id),
1856
- provider: "codex",
1857
- displayName: String(displayName),
1858
- reasoningEfforts: reasoningEfforts.length ? reasoningEfforts : void 0,
1859
- defaultReasoningEffort: defaultReasoningEffort || void 0
1860
- });
1861
- }
1862
- if (!mapped.length) return [];
1863
- const seen = /* @__PURE__ */ new Set();
1864
- return mapped.filter((model) => {
1865
- const key = model.id;
1866
- if (seen.has(key)) return false;
1867
- seen.add(key);
1868
- return true;
1869
- });
1870
- }
1871
- async function fetchCodexModels(command2) {
1872
- return new Promise((resolve) => {
1873
- const child = spawn3(command2, ["app-server"], {
1874
- env: { ...process.env },
1875
- stdio: ["pipe", "pipe", "pipe"]
1876
- });
1877
- let settled = false;
1878
- const initId = 1;
1879
- const listId = 2;
1880
- const timeout = setTimeout(() => {
1881
- finalize(null);
1882
- }, 8e3);
1883
- const finalize = (models) => {
1884
- if (settled) return;
1885
- settled = true;
1886
- clearTimeout(timeout);
1887
- child.kill("SIGTERM");
1888
- resolve(models);
1889
- };
1890
- const handleLine = (line) => {
1891
- const parsed = safeJsonParse2(line);
1892
- if (!parsed || typeof parsed !== "object") return;
1893
- if (parsed.id === listId && parsed.result) {
1894
- finalize(normalizeCodexModels(parsed.result));
1895
- }
1896
- if (parsed.id === listId && parsed.error) {
1897
- finalize(null);
1898
- }
1899
- };
1900
- const stdoutParser = createLineParser(handleLine);
1901
- child.stdout?.on("data", stdoutParser);
1902
- child.stderr?.on("data", () => {
1903
- });
1904
- child.on("error", () => finalize(null));
1905
- child.on("close", () => finalize(null));
1906
- if (!child.stdin) {
1907
- finalize(null);
1908
- return;
1909
- }
1910
- const initialize = {
1911
- method: "initialize",
1912
- id: initId,
1913
- params: {
1914
- clientInfo: { name: "agentconnect", title: "AgentConnect", version: "0.1.0" }
1915
- }
1916
- };
1917
- const initialized = { method: "initialized" };
1918
- const listRequest = { method: "model/list", id: listId, params: { cursor: null, limit: null } };
1919
- const payload = `${JSON.stringify(initialize)}
1920
- ${JSON.stringify(initialized)}
1921
- ${JSON.stringify(
1922
- listRequest
1923
- )}
1924
- `;
1925
- child.stdin.write(payload);
1926
- });
1927
- }
1928
- async function listCodexModels() {
1929
- if (codexModelsCache && Date.now() - codexModelsCacheAt < CODEX_MODELS_CACHE_TTL_MS) {
1930
- return codexModelsCache;
1931
- }
1932
- const command2 = getCodexCommand();
1933
- const versionCheck = await checkCommandVersion(command2, [["--version"], ["-V"]]);
1934
- if (!versionCheck.ok) {
1935
- return [];
1936
- }
1937
- const models = await fetchCodexModels(command2);
1938
- if (models && models.length) {
1939
- codexModelsCache = models;
1940
- codexModelsCacheAt = Date.now();
1941
- return models;
1942
- }
1943
- return [];
1944
- }
1945
- function runCodexPrompt({
1946
- prompt,
1947
- resumeSessionId,
1948
- model,
1949
- reasoningEffort,
1950
- repoRoot,
1951
- cwd,
1952
- providerDetailLevel,
1953
- onEvent,
1954
- signal
1955
- }) {
1956
- return new Promise((resolve) => {
1957
- const command2 = getCodexCommand();
1958
- const resolvedRepoRoot = repoRoot ? path3.resolve(repoRoot) : null;
1959
- const resolvedCwd = cwd ? path3.resolve(cwd) : null;
1960
- const runDir = resolvedCwd || resolvedRepoRoot || process.cwd();
1961
- const cdTarget = resolvedRepoRoot || resolvedCwd || ".";
1962
- const runAttempt = (mode) => new Promise((attemptResolve) => {
1963
- const args2 = buildCodexExecArgs({
1964
- prompt,
1965
- cdTarget,
1966
- resumeSessionId,
1967
- model,
1968
- reasoningEffort,
1969
- mode
1970
- });
1971
- const argsPreview = [...args2];
1972
- if (argsPreview.length > 0) {
1973
- argsPreview[argsPreview.length - 1] = "[prompt]";
1974
- }
1975
- debugLog("Codex", "spawn", { command: command2, args: argsPreview, cwd: runDir, mode });
1976
- const child = spawn3(command2, args2, {
1977
- cwd: runDir,
1978
- env: { ...process.env },
1979
- stdio: ["ignore", "pipe", "pipe"]
1980
- });
1981
- if (signal) {
1982
- signal.addEventListener("abort", () => {
1983
- child.kill("SIGTERM");
1984
- });
1985
- }
1986
- let aggregated = "";
1987
- let finalSessionId = null;
1988
- let didFinalize = false;
1989
- let sawError = false;
1990
- const includeRaw = providerDetailLevel === "raw";
1991
- const buildProviderDetail = (eventType, data, raw) => {
1992
- const detail = { eventType };
1993
- if (data && Object.keys(data).length) detail.data = data;
1994
- if (includeRaw && raw !== void 0) detail.raw = raw;
1995
- return detail;
1996
- };
1997
- const emit = (event) => {
1998
- if (finalSessionId) {
1999
- onEvent({ ...event, providerSessionId: finalSessionId });
2000
- } else {
2001
- onEvent(event);
2002
- }
2003
- };
2004
- const emitError = (message, providerDetail) => {
2005
- if (sawError) return;
2006
- sawError = true;
2007
- emit({ type: "error", message, providerDetail });
2008
- };
2009
- const emitItemEvent = (item, phase) => {
2010
- const itemType = typeof item.type === "string" ? item.type : "";
2011
- if (!itemType) return;
2012
- const providerDetail = buildProviderDetail(
2013
- phase === "start" ? "item.started" : "item.completed",
2014
- {
2015
- itemType,
2016
- itemId: item.id,
2017
- status: item.status
2018
- },
2019
- item
2020
- );
2021
- if (itemType === "agent_message") {
2022
- if (phase === "completed" && typeof item.text === "string") {
2023
- emit({
2024
- type: "message",
2025
- provider: "codex",
2026
- role: "assistant",
2027
- content: item.text,
2028
- contentParts: item,
2029
- providerDetail
2030
- });
2031
- }
2032
- return;
2033
- }
2034
- if (itemType === "reasoning") {
2035
- emit({
2036
- type: "thinking",
2037
- provider: "codex",
2038
- phase,
2039
- text: typeof item.text === "string" ? item.text : void 0,
2040
- providerDetail
2041
- });
2042
- return;
2043
- }
2044
- if (itemType === "command_execution") {
2045
- const output = phase === "completed" ? {
2046
- output: item.aggregated_output,
2047
- exitCode: item.exit_code,
2048
- status: item.status
2049
- } : void 0;
2050
- emit({
2051
- type: "tool_call",
2052
- provider: "codex",
2053
- name: "command_execution",
2054
- callId: item.id,
2055
- input: { command: item.command },
2056
- output,
2057
- phase,
2058
- providerDetail
2059
- });
2060
- return;
2061
- }
2062
- emit({
2063
- type: "tool_call",
2064
- provider: "codex",
2065
- name: itemType,
2066
- callId: item.id,
2067
- input: phase === "start" ? item : void 0,
2068
- output: phase === "completed" ? item : void 0,
2069
- phase,
2070
- providerDetail
2071
- });
2072
- };
2073
- let sawJson = false;
2074
- const stdoutLines = [];
2075
- const stderrLines = [];
2076
- const pushLine = (list, line) => {
2077
- if (!line) return;
2078
- list.push(line);
2079
- if (list.length > 12) list.shift();
2080
- };
2081
- const emitFinal = (text, providerDetail) => {
2082
- emit({ type: "final", text, providerDetail });
2083
- };
2084
- const handleLine = (line, source) => {
2085
- const parsed = safeJsonParse2(line);
2086
- if (!parsed || typeof parsed !== "object") {
2087
- if (line.trim()) {
2088
- emit({ type: "raw_line", line });
2089
- }
2090
- if (source === "stdout") {
2091
- pushLine(stdoutLines, line);
2092
- } else {
2093
- pushLine(stderrLines, line);
2094
- }
2095
- return;
2096
- }
2097
- sawJson = true;
2098
- const ev = parsed;
2099
- const normalized = normalizeEvent(ev);
2100
- const sid = extractSessionId2(ev);
2101
- if (sid) finalSessionId = sid;
2102
- const eventType = typeof ev.type === "string" ? ev.type : normalized.type;
2103
- const detailData = {};
2104
- const threadId = ev.thread_id ?? ev.threadId;
2105
- if (typeof threadId === "string" && threadId) detailData.threadId = threadId;
2106
- const providerDetail = buildProviderDetail(eventType || "unknown", detailData, ev);
2107
- let handled = false;
2108
- const usage = extractUsage(ev);
2109
- if (usage) {
2110
- emit({
2111
- type: "usage",
2112
- inputTokens: usage.input_tokens,
2113
- outputTokens: usage.output_tokens,
2114
- providerDetail
2115
- });
2116
- handled = true;
2117
- }
2118
- if (normalized.type === "agent_message") {
2119
- const text = normalized.text;
2120
- if (typeof text === "string" && text) {
2121
- aggregated += text;
2122
- emit({ type: "delta", text, providerDetail });
2123
- emit({
2124
- type: "message",
2125
- provider: "codex",
2126
- role: "assistant",
2127
- content: text,
2128
- contentParts: ev,
2129
- providerDetail
2130
- });
2131
- handled = true;
2132
- }
2133
- } else if (normalized.type === "item.completed") {
2134
- const item = normalized.item;
2135
- if (item && typeof item === "object") {
2136
- const itemDetail = buildProviderDetail("item.completed", {
2137
- itemType: item.type,
2138
- itemId: item.id,
2139
- status: item.status
2140
- }, item);
2141
- if (item.type === "command_execution" && typeof item.aggregated_output === "string") {
2142
- emit({ type: "delta", text: item.aggregated_output, providerDetail: itemDetail });
2143
- }
2144
- if (item.type === "agent_message" && typeof item.text === "string") {
2145
- aggregated += item.text;
2146
- emit({ type: "delta", text: item.text, providerDetail: itemDetail });
2147
- }
2148
- }
2149
- }
2150
- if (normalized.type === "item.started" && ev.item && typeof ev.item === "object") {
2151
- emitItemEvent(ev.item, "start");
2152
- handled = true;
2153
- }
2154
- if (normalized.type === "item.completed" && ev.item && typeof ev.item === "object") {
2155
- emitItemEvent(ev.item, "completed");
2156
- handled = true;
2157
- }
2158
- if (normalized.type === "error") {
2159
- emitError(normalized.message || "Codex run failed", providerDetail);
2160
- handled = true;
2161
- }
2162
- if (isTerminalEvent(ev) && !didFinalize) {
2163
- if (ev.type === "turn.failed") {
2164
- const message = ev.error?.message;
2165
- emitError(typeof message === "string" ? message : "Codex run failed", providerDetail);
2166
- didFinalize = true;
2167
- handled = true;
2168
- return;
2169
- }
2170
- if (!sawError) {
2171
- didFinalize = true;
2172
- emitFinal(aggregated, providerDetail);
2173
- handled = true;
2174
- }
2175
- }
2176
- if (!handled) {
2177
- emit({ type: "detail", provider: "codex", providerDetail });
2178
- }
2179
- };
2180
- const stdoutParser = createLineParser((line) => handleLine(line, "stdout"));
2181
- const stderrParser = createLineParser((line) => handleLine(line, "stderr"));
2182
- child.stdout?.on("data", stdoutParser);
2183
- child.stderr?.on("data", stderrParser);
2184
- child.on("close", (code) => {
2185
- if (!didFinalize) {
2186
- if (code && code !== 0) {
2187
- const hint = stderrLines.at(-1) || stdoutLines.at(-1) || "";
2188
- const suffix = hint ? `: ${hint}` : "";
2189
- const fallback = mode === "modern" && !sawJson && shouldFallbackToLegacy([
2190
- ...stderrLines,
2191
- ...stdoutLines
2192
- ]);
2193
- debugLog("Codex", "exit", {
2194
- code,
2195
- stderr: stderrLines,
2196
- stdout: stdoutLines,
2197
- fallback
2198
- });
2199
- if (fallback) {
2200
- attemptResolve({ sessionId: finalSessionId, fallback: true });
2201
- return;
2202
- }
2203
- emitError(`Codex exited with code ${code}${suffix}`);
2204
- } else if (!sawError) {
2205
- emitFinal(aggregated);
2206
- }
2207
- }
2208
- attemptResolve({ sessionId: finalSessionId, fallback: false });
2209
- });
2210
- child.on("error", (err) => {
2211
- debugLog("Codex", "spawn-error", { message: err?.message });
2212
- emitError(err?.message ?? "Codex failed to start");
2213
- attemptResolve({ sessionId: finalSessionId, fallback: false });
2214
- });
2215
- });
2216
- void (async () => {
2217
- const primary = await runAttempt("modern");
2218
- if (primary.fallback) {
2219
- debugLog("Codex", "fallback", { from: "modern", to: "legacy" });
2220
- const legacy = await runAttempt("legacy");
2221
- resolve({ sessionId: legacy.sessionId });
2222
- return;
2223
- }
2224
- resolve({ sessionId: primary.sessionId });
2225
- })();
2226
- });
2227
- }
2228
-
2229
- // src/providers/cursor.ts
2230
- import { spawn as spawn4 } from "child_process";
2231
- import path4 from "path";
2232
- var INSTALL_UNIX2 = "curl https://cursor.com/install -fsS | bash";
2233
- var DEFAULT_LOGIN3 = "cursor-agent login";
2234
- var DEFAULT_STATUS3 = "cursor-agent status";
2235
- var CURSOR_MODELS_COMMAND = "cursor-agent models";
2236
- var CURSOR_MODELS_CACHE_TTL_MS = 6e4;
2237
- var CURSOR_UPDATE_CACHE_TTL_MS = 6 * 60 * 60 * 1e3;
2238
- var cursorModelsCache = null;
2239
- var cursorModelsCacheAt = 0;
2240
- var cursorUpdateCache = null;
2241
- var cursorUpdatePromise = null;
2242
- function trimOutput3(value, limit = 400) {
2243
- const cleaned = value.trim();
2244
- if (!cleaned) return "";
2245
- if (cleaned.length <= limit) return cleaned;
2246
- return `${cleaned.slice(0, limit)}...`;
2247
- }
2248
- function normalizePath3(value) {
2249
- const normalized = value.replace(/\\/g, "/");
2250
- return process.platform === "win32" ? normalized.toLowerCase() : normalized;
2251
- }
2252
- function parseSemver3(value) {
2253
- if (!value) return null;
2254
- const match = value.match(/(\d+)\.(\d+)\.(\d+)/);
2255
- if (!match) return null;
2256
- return [Number(match[1]), Number(match[2]), Number(match[3])];
2257
- }
2258
- function compareSemver3(a, b) {
2259
- if (a[0] !== b[0]) return a[0] - b[0];
2260
- if (a[1] !== b[1]) return a[1] - b[1];
2261
- return a[2] - b[2];
2262
- }
2263
- async function fetchBrewCaskVersion2(cask) {
2264
- if (!commandExists("brew")) return null;
2265
- const result = await runCommand("brew", ["info", "--json=v2", "--cask", cask]);
2266
- if (result.code !== 0) return null;
2267
- try {
2268
- const parsed = JSON.parse(result.stdout);
2269
- const version = parsed?.casks?.[0]?.version;
2270
- return typeof version === "string" ? version : null;
2271
- } catch {
2272
- return null;
2273
- }
2274
- }
2275
- function getCursorUpdateAction(commandPath) {
2276
- if (!commandPath) return null;
2277
- const normalized = normalizePath3(commandPath);
2278
- if (normalized.includes("/cellar/") || normalized.includes("/caskroom/") || normalized.includes("/homebrew/")) {
2279
- return {
2280
- command: "brew",
2281
- args: ["upgrade", "--cask", "cursor"],
2282
- source: "brew",
2283
- commandLabel: "brew upgrade --cask cursor"
2284
- };
2285
- }
2286
- if (normalized.includes("/.local/bin/") || normalized.includes("/.local/share/cursor-agent/versions/")) {
2287
- return {
2288
- command: "bash",
2289
- args: ["-lc", INSTALL_UNIX2],
2290
- source: "script",
2291
- commandLabel: INSTALL_UNIX2
2292
- };
2293
- }
2294
- return null;
2295
- }
2296
- function getCursorCommand() {
2297
- const override = process.env.AGENTCONNECT_CURSOR_COMMAND;
2298
- const base = override || "cursor-agent";
2299
- const resolved = resolveCommandPath(base);
2300
- return resolved || resolveWindowsCommand(base);
2301
- }
2302
- function getCursorApiKey() {
2303
- return process.env.CURSOR_API_KEY || process.env.AGENTCONNECT_CURSOR_API_KEY || "";
2304
- }
2305
- function getCursorDefaultModel() {
2306
- return process.env.AGENTCONNECT_CURSOR_MODEL?.trim() || "";
2307
- }
2308
- function resolveCursorEndpoint() {
2309
- return process.env.AGENTCONNECT_CURSOR_ENDPOINT?.trim() || "";
2310
- }
2311
- function withCursorEndpoint(args2) {
2312
- const endpoint = resolveCursorEndpoint();
2313
- if (!endpoint) return args2;
2314
- if (args2.includes("--endpoint")) return args2;
2315
- return [...args2, "--endpoint", endpoint];
2316
- }
2317
- function resolveCursorModel(model, fallback) {
2318
- if (!model) return fallback;
2319
- const raw = String(model);
2320
- if (raw === "cursor" || raw === "cursor-default") return fallback;
2321
- if (raw.startsWith("cursor:")) return raw.slice("cursor:".length);
2322
- if (raw.startsWith("cursor/")) return raw.slice("cursor/".length);
2323
- return raw;
2324
- }
2325
- function formatCursorDefaultLabel(fallback) {
2326
- if (!fallback) return "Default";
2327
- return `Default \xB7 ${fallback}`;
2328
- }
2329
- function normalizeCursorModelId(value) {
2330
- if (value.startsWith("cursor:") || value.startsWith("cursor/")) return value;
2331
- return `cursor:${value}`;
2332
- }
2333
- function normalizeCursorModelDisplay(value) {
2334
- if (value.startsWith("cursor:")) return value.slice("cursor:".length);
2335
- if (value.startsWith("cursor/")) return value.slice("cursor/".length);
2336
- return value;
2337
- }
2338
- async function listCursorModelsFromCli() {
2339
- const command2 = getCursorCommand();
2340
- if (!commandExists(command2)) return [];
2341
- const modelsCommand = buildStatusCommand(
2342
- "AGENTCONNECT_CURSOR_MODELS_COMMAND",
2343
- CURSOR_MODELS_COMMAND
2344
- );
2345
- if (!modelsCommand.command) return [];
2346
- const resolvedCommand = resolveWindowsCommand(modelsCommand.command);
2347
- const result = await runCommand(resolvedCommand, withCursorEndpoint(modelsCommand.args), {
2348
- env: buildCursorEnv()
2349
- });
2350
- const output = `${result.stdout}
2351
- ${result.stderr}`.trim();
2352
- if (!output) return [];
2353
- const parsed = safeJsonParse3(output);
2354
- if (Array.isArray(parsed)) {
2355
- const models = parsed.map((entry) => {
2356
- if (typeof entry === "string" && entry.trim()) {
2357
- const value = entry.trim();
2358
- return {
2359
- id: normalizeCursorModelId(value),
2360
- provider: "cursor",
2361
- displayName: normalizeCursorModelDisplay(value)
2362
- };
2363
- }
2364
- if (entry && typeof entry === "object") {
2365
- const record = entry;
2366
- const idRaw = typeof record.id === "string" ? record.id.trim() : "";
2367
- const nameRaw = typeof record.name === "string" ? record.name.trim() : "";
2368
- const displayRaw = typeof record.displayName === "string" ? record.displayName.trim() : "";
2369
- const value = idRaw || nameRaw || displayRaw;
2370
- if (!value) return null;
2371
- return {
2372
- id: normalizeCursorModelId(value),
2373
- provider: "cursor",
2374
- displayName: normalizeCursorModelDisplay(displayRaw || nameRaw || value)
2375
- };
2376
- }
2377
- return null;
2378
- }).filter(Boolean);
2379
- return models;
2380
- }
2381
- const lines = output.split("\n").map((line) => line.trim()).filter(Boolean).filter((line) => !line.toLowerCase().startsWith("model")).filter((line) => !/^[-=]{2,}$/.test(line));
2382
- return lines.map((line) => {
2383
- const cleaned = line.replace(/^[-*•]\s*/, "");
2384
- const value = cleaned.split(/\s+/)[0] || cleaned;
2385
- return {
2386
- id: normalizeCursorModelId(value),
2387
- provider: "cursor",
2388
- displayName: normalizeCursorModelDisplay(value)
2389
- };
2390
- });
2391
- }
2392
- async function listCursorModels() {
2393
- if (cursorModelsCache && Date.now() - cursorModelsCacheAt < CURSOR_MODELS_CACHE_TTL_MS) {
2394
- return cursorModelsCache;
2395
- }
2396
- const fallback = getCursorDefaultModel();
2397
- const base = [
2398
- {
2399
- id: "cursor-default",
2400
- provider: "cursor",
2401
- displayName: formatCursorDefaultLabel(fallback)
2402
- }
2403
- ];
2404
- const envModels = process.env.AGENTCONNECT_CURSOR_MODELS;
2405
- if (envModels) {
2406
- try {
2407
- const parsed = JSON.parse(envModels);
2408
- if (Array.isArray(parsed)) {
2409
- for (const entry of parsed) {
2410
- if (typeof entry === "string" && entry.trim()) {
2411
- const trimmed = entry.trim();
2412
- base.push({
2413
- id: normalizeCursorModelId(trimmed),
2414
- provider: "cursor",
2415
- displayName: normalizeCursorModelDisplay(trimmed)
2416
- });
2417
- }
2418
- }
2419
- }
2420
- } catch {
2421
- }
2422
- }
2423
- const cliModels = await listCursorModelsFromCli();
2424
- base.push(...cliModels);
2425
- const seen = /* @__PURE__ */ new Set();
2426
- const list = base.filter((entry) => {
2427
- const key = `${entry.provider}:${entry.id}`;
2428
- if (seen.has(key)) return false;
2429
- seen.add(key);
2430
- return true;
2431
- });
2432
- cursorModelsCache = list;
2433
- cursorModelsCacheAt = Date.now();
2434
- return list;
2435
- }
2436
- function normalizeCursorStatusOutput(output) {
2437
- const text = output.toLowerCase();
2438
- if (text.includes("not authenticated") || text.includes("not logged in") || text.includes("login required") || text.includes("please login") || text.includes("please log in") || text.includes("unauthorized")) {
2439
- return false;
2440
- }
2441
- if (text.includes("authenticated") || text.includes("logged in") || text.includes("signed in") || text.includes("account")) {
2442
- return true;
2443
- }
2444
- return null;
2445
- }
2446
- function getCursorUpdateSnapshot(commandPath) {
2447
- if (cursorUpdateCache && Date.now() - cursorUpdateCache.checkedAt < CURSOR_UPDATE_CACHE_TTL_MS) {
2448
- const action = getCursorUpdateAction(commandPath);
2449
- return {
2450
- updateAvailable: cursorUpdateCache.updateAvailable,
2451
- latestVersion: cursorUpdateCache.latestVersion,
2452
- updateCheckedAt: cursorUpdateCache.checkedAt,
2453
- updateSource: action?.source ?? "unknown",
2454
- updateCommand: action?.commandLabel,
2455
- updateMessage: cursorUpdateCache.updateMessage
2456
- };
2457
- }
2458
- return {};
2459
- }
2460
- function ensureCursorUpdateCheck(currentVersion, commandPath) {
2461
- if (cursorUpdateCache && Date.now() - cursorUpdateCache.checkedAt < CURSOR_UPDATE_CACHE_TTL_MS) {
2462
- return;
2463
- }
2464
- if (cursorUpdatePromise) return;
2465
- cursorUpdatePromise = (async () => {
2466
- const action = getCursorUpdateAction(commandPath || null);
2467
- let latest = null;
2468
- let updateAvailable;
2469
- let updateMessage;
2470
- if (action?.source === "brew") {
2471
- latest = await fetchBrewCaskVersion2("cursor");
2472
- }
2473
- if (latest && currentVersion) {
2474
- const a = parseSemver3(currentVersion);
2475
- const b = parseSemver3(latest);
2476
- if (a && b) {
2477
- updateAvailable = compareSemver3(a, b) < 0;
2478
- updateMessage = updateAvailable ? `Update available: ${currentVersion} -> ${latest}` : `Up to date (${currentVersion})`;
2479
- }
2480
- } else if (!action) {
2481
- updateMessage = "Update check unavailable";
2482
- }
2483
- debugLog("Cursor", "update-check", {
2484
- updateAvailable,
2485
- message: updateMessage
2486
- });
2487
- cursorUpdateCache = {
2488
- checkedAt: Date.now(),
2489
- updateAvailable,
2490
- latestVersion: latest ?? void 0,
2491
- updateMessage
2492
- };
2493
- })().finally(() => {
2494
- cursorUpdatePromise = null;
2495
- });
2496
- }
2497
- async function ensureCursorInstalled() {
2498
- const command2 = getCursorCommand();
2499
- const versionCheck = await checkCommandVersion(command2, [["--version"], ["-V"]]);
2500
- debugLog("Cursor", "install-check", {
2501
- command: command2,
2502
- versionOk: versionCheck.ok,
2503
- version: versionCheck.version
2504
- });
2505
- if (versionCheck.ok) {
2506
- return { installed: true, version: versionCheck.version || void 0 };
2507
- }
2508
- if (commandExists(command2)) {
2509
- return { installed: true, version: void 0 };
2510
- }
2511
- const override = buildInstallCommand("AGENTCONNECT_CURSOR_INSTALL", "");
2512
- let install = override;
2513
- let packageManager = override.command ? "unknown" : "unknown";
2514
- if (!install.command) {
2515
- if (process.platform !== "win32" && commandExists("bash") && commandExists("curl")) {
2516
- install = { command: "bash", args: ["-lc", INSTALL_UNIX2] };
2517
- packageManager = "script";
2518
- }
2519
- }
2520
- if (!install.command) {
2521
- return { installed: false, version: void 0, packageManager };
2522
- }
2523
- await runCommand(install.command, install.args, { shell: process.platform === "win32" });
2524
- const after = await checkCommandVersion(command2, [["--version"], ["-V"]]);
2525
- return {
2526
- installed: after.ok,
2527
- version: after.version || void 0,
2528
- packageManager
2529
- };
2530
- }
2531
- async function getCursorStatus() {
2532
- const command2 = getCursorCommand();
2533
- const versionCheck = await checkCommandVersion(command2, [["--version"], ["-V"]]);
2534
- const installed = versionCheck.ok || commandExists(command2);
2535
- let loggedIn = false;
2536
- if (installed) {
2537
- const status = buildStatusCommand("AGENTCONNECT_CURSOR_STATUS", DEFAULT_STATUS3);
2538
- if (status.command) {
2539
- const statusCommand = resolveWindowsCommand(status.command);
2540
- const result = await runCommand(statusCommand, withCursorEndpoint(status.args), {
2541
- env: buildCursorEnv()
2542
- });
2543
- const output = `${result.stdout}
2544
- ${result.stderr}`;
2545
- const parsed = normalizeCursorStatusOutput(output);
2546
- loggedIn = parsed ?? result.code === 0;
2547
- }
2548
- if (!loggedIn) {
2549
- loggedIn = Boolean(getCursorApiKey().trim());
2550
- }
2551
- }
2552
- if (installed) {
2553
- const resolved2 = resolveCommandRealPath(command2);
2554
- ensureCursorUpdateCheck(versionCheck.version, resolved2 || null);
2555
- }
2556
- const resolved = resolveCommandRealPath(command2);
2557
- const updateInfo = installed ? getCursorUpdateSnapshot(resolved || null) : {};
2558
- return { installed, loggedIn, version: versionCheck.version || void 0, ...updateInfo };
2559
- }
2560
- async function getCursorFastStatus() {
2561
- const command2 = getCursorCommand();
2562
- const installed = commandExists(command2);
2563
- if (!installed) {
2564
- return { installed: false, loggedIn: false };
2565
- }
2566
- return { installed: true, loggedIn: true };
2567
- }
2568
- async function updateCursor() {
2569
- const command2 = getCursorCommand();
2570
- if (!commandExists(command2)) {
2571
- return { installed: false, loggedIn: false };
2572
- }
2573
- const resolved = resolveCommandRealPath(command2);
2574
- const updateOverride = buildStatusCommand("AGENTCONNECT_CURSOR_UPDATE", "");
2575
- const action = updateOverride.command ? null : getCursorUpdateAction(resolved || null);
2576
- const updateCommand = updateOverride.command || action?.command || "";
2577
- const updateArgs = updateOverride.command ? updateOverride.args : action?.args || [];
2578
- if (!updateCommand) {
2579
- throw new Error("No update command available. Please update Cursor manually.");
2580
- }
2581
- const cmd = resolveWindowsCommand(updateCommand);
2582
- debugLog("Cursor", "update-run", { command: cmd, args: updateArgs });
2583
- const result = await runCommand(cmd, updateArgs, { env: buildCursorEnv() });
2584
- debugLog("Cursor", "update-result", {
2585
- code: result.code,
2586
- stdout: trimOutput3(result.stdout),
2587
- stderr: trimOutput3(result.stderr)
2588
- });
2589
- if (result.code !== 0 && result.code !== null) {
2590
- const message = trimOutput3(`${result.stdout}
2591
- ${result.stderr}`, 800) || "Update failed";
2592
- throw new Error(message);
2593
- }
2594
- cursorUpdateCache = null;
2595
- cursorUpdatePromise = null;
2596
- return getCursorStatus();
2597
- }
2598
- function buildCursorEnv() {
2599
- const env = { ...process.env };
2600
- const apiKey = getCursorApiKey().trim();
2601
- if (apiKey) {
2602
- env.CURSOR_API_KEY = apiKey;
2603
- }
2604
- return env;
2605
- }
2606
- async function loginCursor(options) {
2607
- if (typeof options?.apiKey === "string") {
2608
- process.env.CURSOR_API_KEY = options.apiKey;
2609
- }
2610
- if (typeof options?.baseUrl === "string") {
2611
- process.env.AGENTCONNECT_CURSOR_ENDPOINT = options.baseUrl;
2612
- }
2613
- if (typeof options?.model === "string") {
2614
- process.env.AGENTCONNECT_CURSOR_MODEL = options.model;
2615
- cursorModelsCache = null;
2616
- cursorModelsCacheAt = 0;
2617
- }
2618
- if (Array.isArray(options?.models)) {
2619
- process.env.AGENTCONNECT_CURSOR_MODELS = JSON.stringify(options.models.filter(Boolean));
2620
- cursorModelsCache = null;
2621
- cursorModelsCacheAt = 0;
2622
- }
2623
- if (!options?.apiKey) {
2624
- const login = buildLoginCommand("AGENTCONNECT_CURSOR_LOGIN", DEFAULT_LOGIN3);
2625
- if (login.command) {
2626
- const command2 = resolveWindowsCommand(login.command);
2627
- await runCommand(command2, withCursorEndpoint(login.args), { env: buildCursorEnv() });
2628
- }
2629
- }
2630
- const timeoutMs = Number(process.env.AGENTCONNECT_CURSOR_LOGIN_TIMEOUT_MS || 2e4);
2631
- const pollIntervalMs = Number(process.env.AGENTCONNECT_CURSOR_LOGIN_POLL_MS || 1e3);
2632
- const start = Date.now();
2633
- let status = await getCursorStatus();
2634
- while (!status.loggedIn && Date.now() - start < timeoutMs) {
2635
- await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
2636
- status = await getCursorStatus();
2637
- }
2638
- return { loggedIn: status.loggedIn };
2639
- }
2640
- function safeJsonParse3(line) {
2641
- try {
2642
- return JSON.parse(line);
2643
- } catch {
2644
- return null;
2645
- }
2646
- }
2647
- function extractSessionId3(ev) {
2648
- const id = ev.session_id ?? ev.sessionId;
2649
- return typeof id === "string" ? id : null;
2650
- }
2651
- function extractUsage2(ev) {
2652
- const usage = ev.usage ?? ev.token_usage ?? ev.tokenUsage ?? ev.tokens;
2653
- if (!usage || typeof usage !== "object") return null;
2654
- const toNumber = (v) => typeof v === "number" && Number.isFinite(v) ? v : void 0;
2655
- const input = toNumber(
2656
- usage.input_tokens ?? usage.prompt_tokens ?? usage.inputTokens ?? usage.promptTokens
2657
- );
2658
- const output = toNumber(
2659
- usage.output_tokens ?? usage.completion_tokens ?? usage.outputTokens ?? usage.completionTokens
2660
- );
2661
- const total = toNumber(usage.total_tokens ?? usage.totalTokens);
2662
- const out = {};
2663
- if (input !== void 0) out.input_tokens = input;
2664
- if (output !== void 0) out.output_tokens = output;
2665
- if (total !== void 0) out.total_tokens = total;
2666
- return Object.keys(out).length ? out : null;
2667
- }
2668
- function extractTextFromContent2(content) {
2669
- if (!content) return "";
2670
- if (typeof content === "string") return content;
2671
- if (Array.isArray(content)) {
2672
- return content.map((part) => {
2673
- if (!part) return "";
2674
- if (typeof part === "string") return part;
2675
- if (typeof part === "object") {
2676
- const text = part.text;
2677
- if (typeof text === "string") return text;
2678
- }
2679
- return "";
2680
- }).join("");
2681
- }
2682
- if (typeof content === "object") {
2683
- const text = content.text;
2684
- if (typeof text === "string") return text;
2685
- }
2686
- return "";
2687
- }
2688
- function extractToolCall(ev) {
2689
- if (ev.type !== "tool_call") return null;
2690
- const toolCall = ev.tool_call && typeof ev.tool_call === "object" ? ev.tool_call : null;
2691
- if (!toolCall) return null;
2692
- const keys = Object.keys(toolCall);
2693
- if (!keys.length) return null;
2694
- const name = keys[0];
2695
- const entry = toolCall[name];
2696
- const record = entry && typeof entry === "object" ? entry : null;
2697
- const input = record?.args ?? record?.input ?? void 0;
2698
- const output = record?.output ?? record?.result ?? void 0;
2699
- const subtype = typeof ev.subtype === "string" ? ev.subtype : "";
2700
- const phase = subtype === "completed" ? "completed" : subtype === "started" ? "start" : subtype === "error" ? "error" : subtype === "delta" ? "delta" : void 0;
2701
- return {
2702
- name,
2703
- input,
2704
- output,
2705
- callId: typeof ev.call_id === "string" ? ev.call_id : void 0,
2706
- phase
2707
- };
2708
- }
2709
- function extractTextFromMessage(message) {
2710
- if (!message || typeof message !== "object") return "";
2711
- const content = message.content;
2712
- return extractTextFromContent2(content);
2713
- }
2714
- function extractAssistantDelta2(ev) {
2715
- if (ev.type !== "assistant" && ev.message?.role !== "assistant") return null;
2716
- if (typeof ev.text === "string") return ev.text;
2717
- if (typeof ev.delta === "string") return ev.delta;
2718
- if (ev.message) {
2719
- const text = extractTextFromMessage(ev.message);
2720
- return text || null;
2721
- }
2722
- if (ev.content) {
2723
- const text = extractTextFromContent2(ev.content);
2724
- return text || null;
2725
- }
2726
- return null;
2727
- }
2728
- function extractResultText2(ev) {
2729
- if (typeof ev.result === "string") return ev.result;
2730
- if (ev.result && typeof ev.result === "object") {
2731
- const result = ev.result;
2732
- if (typeof result.text === "string") return result.text;
2733
- if (result.message) {
2734
- const text = extractTextFromMessage(result.message);
2735
- if (text) return text;
2736
- }
2737
- if (result.content) {
2738
- const text = extractTextFromContent2(result.content);
2739
- if (text) return text;
2740
- }
2741
- }
2742
- if (ev.message) {
2743
- const text = extractTextFromMessage(ev.message);
2744
- if (text) return text;
2745
- }
2746
- return null;
2747
- }
2748
- function isErrorEvent(ev) {
2749
- if (ev.type === "error") return true;
2750
- if (ev.type === "result" && ev.subtype) {
2751
- const subtype = String(ev.subtype).toLowerCase();
2752
- if (subtype.includes("error") || subtype.includes("failed")) return true;
2753
- }
2754
- return false;
2755
- }
2756
- function extractErrorMessage(ev) {
2757
- if (typeof ev.error === "string") return ev.error;
2758
- if (ev.error && typeof ev.error === "object" && typeof ev.error.message === "string") {
2759
- return ev.error.message;
2760
- }
2761
- if (typeof ev.text === "string" && ev.type === "error") return ev.text;
2762
- if (typeof ev.result === "string" && ev.type === "result") return ev.result;
2763
- return null;
2764
- }
2765
- function normalizeCursorEvent(raw) {
2766
- if (!raw || typeof raw !== "object") return { type: "unknown" };
2767
- const type = typeof raw.type === "string" ? raw.type : "unknown";
2768
- return { ...raw, type };
2769
- }
2770
- function runCursorPrompt({
2771
- prompt,
2772
- resumeSessionId,
2773
- model,
2774
- repoRoot,
2775
- cwd,
2776
- providerDetailLevel,
2777
- onEvent,
2778
- signal
2779
- }) {
2780
- return new Promise((resolve) => {
2781
- const command2 = getCursorCommand();
2782
- const resolvedRepoRoot = repoRoot ? path4.resolve(repoRoot) : null;
2783
- const resolvedCwd = cwd ? path4.resolve(cwd) : null;
2784
- const runDir = resolvedCwd || resolvedRepoRoot || process.cwd();
2785
- const args2 = ["--print", "--output-format", "stream-json"];
2786
- if (resumeSessionId) {
2787
- args2.push("--resume", resumeSessionId);
2788
- }
2789
- const fallbackModel = getCursorDefaultModel();
2790
- const resolvedModel = resolveCursorModel(model, fallbackModel);
2791
- if (resolvedModel) {
2792
- args2.push("--model", resolvedModel);
2793
- }
2794
- const endpoint = resolveCursorEndpoint();
2795
- if (endpoint) {
2796
- args2.push("--endpoint", endpoint);
2797
- }
2798
- args2.push(prompt);
2799
- const argsPreview = [...args2];
2800
- if (argsPreview.length > 0) {
2801
- argsPreview[argsPreview.length - 1] = "[prompt]";
2802
- }
2803
- debugLog("Cursor", "spawn", {
2804
- command: command2,
2805
- args: argsPreview,
2806
- cwd: runDir,
2807
- model: resolvedModel || null,
2808
- endpoint: endpoint || null,
2809
- resume: resumeSessionId || null,
2810
- apiKeyConfigured: Boolean(getCursorApiKey().trim()),
2811
- promptChars: prompt.length
2812
- });
2813
- const child = spawn4(command2, args2, {
2814
- cwd: runDir,
2815
- env: buildCursorEnv(),
2816
- stdio: ["ignore", "pipe", "pipe"]
2817
- });
2818
- if (signal) {
2819
- signal.addEventListener("abort", () => {
2820
- child.kill("SIGTERM");
2821
- });
2822
- }
2823
- let aggregated = "";
2824
- let finalSessionId = null;
2825
- let didFinalize = false;
2826
- let sawError = false;
2827
- let sawJson = false;
2828
- let rawOutput = "";
2829
- const stdoutLines = [];
2830
- const stderrLines = [];
2831
- const includeRaw = providerDetailLevel === "raw";
2832
- const buildProviderDetail = (eventType, data, raw) => {
2833
- const detail = { eventType };
2834
- if (data && Object.keys(data).length) detail.data = data;
2835
- if (includeRaw && raw !== void 0) detail.raw = raw;
2836
- return detail;
2837
- };
2838
- const emit = (event) => {
2839
- if (finalSessionId) {
2840
- onEvent({ ...event, providerSessionId: finalSessionId });
2841
- } else {
2842
- onEvent(event);
2843
- }
2844
- };
2845
- const pushLine = (list, line) => {
2846
- if (!line) return;
2847
- list.push(line);
2848
- if (list.length > 12) list.shift();
2849
- };
2850
- const emitError = (message, providerDetail) => {
2851
- if (sawError) return;
2852
- sawError = true;
2853
- emit({ type: "error", message, providerDetail });
2854
- };
2855
- const emitFinal = (text) => {
2856
- if (didFinalize) return;
2857
- didFinalize = true;
2858
- emit({ type: "final", text });
2859
- };
2860
- const handleEvent = (ev) => {
2861
- const normalized = normalizeCursorEvent(ev);
2862
- if (ev?.type === "system" && ev?.subtype === "init") {
2863
- debugLog("Cursor", "init", {
2864
- apiKeySource: ev.apiKeySource || null,
2865
- cwd: ev.cwd || null,
2866
- model: ev.model || null,
2867
- permissionMode: ev.permissionMode || null,
2868
- sessionId: ev.session_id ?? ev.sessionId ?? null
2869
- });
2870
- emit({
2871
- type: "detail",
2872
- provider: "cursor",
2873
- providerDetail: buildProviderDetail(
2874
- "system.init",
2875
- {
2876
- apiKeySource: ev.apiKeySource,
2877
- cwd: ev.cwd,
2878
- model: ev.model,
2879
- permissionMode: ev.permissionMode
2880
- },
2881
- ev
2882
- )
2883
- });
2884
- }
2885
- const sid = extractSessionId3(ev);
2886
- if (sid) finalSessionId = sid;
2887
- if (ev?.type === "thinking") {
2888
- const subtype = typeof ev.subtype === "string" ? ev.subtype : "";
2889
- const phase = subtype === "completed" ? "completed" : subtype === "started" ? "start" : subtype === "error" ? "error" : "delta";
2890
- emit({
2891
- type: "thinking",
2892
- provider: "cursor",
2893
- phase,
2894
- text: typeof ev.text === "string" ? ev.text : "",
2895
- timestampMs: typeof ev.timestamp_ms === "number" ? ev.timestamp_ms : void 0,
2896
- providerDetail: buildProviderDetail(
2897
- subtype ? `thinking.${subtype}` : "thinking",
2898
- {
2899
- subtype: subtype || void 0
2900
- },
2901
- ev
2902
- )
2903
- });
2904
- }
2905
- if (ev?.type === "assistant" || ev?.type === "user") {
2906
- const role = ev.message?.role === "assistant" || ev.message?.role === "user" ? ev.message?.role : ev.type;
2907
- const rawContent = ev.message?.content ?? ev.content;
2908
- const content = extractTextFromContent2(rawContent);
2909
- emit({
2910
- type: "message",
2911
- provider: "cursor",
2912
- role,
2913
- content,
2914
- contentParts: rawContent ?? null,
2915
- providerDetail: buildProviderDetail(ev.type, {}, ev)
2916
- });
2917
- }
2918
- const toolCall = extractToolCall(ev);
2919
- if (toolCall) {
2920
- emit({
2921
- type: "tool_call",
2922
- provider: "cursor",
2923
- name: toolCall.name,
2924
- callId: toolCall.callId,
2925
- input: toolCall.input,
2926
- output: toolCall.output,
2927
- phase: toolCall.phase,
2928
- providerDetail: buildProviderDetail(
2929
- ev.subtype ? `tool_call.${ev.subtype}` : "tool_call",
2930
- {
2931
- name: toolCall.name,
2932
- callId: toolCall.callId,
2933
- subtype: ev.subtype
2934
- },
2935
- ev
2936
- )
2937
- });
2938
- }
2939
- const usage = extractUsage2(ev);
2940
- if (usage) {
2941
- emit({
2942
- type: "usage",
2943
- inputTokens: usage.input_tokens,
2944
- outputTokens: usage.output_tokens
2945
- });
2946
- }
2947
- if (isErrorEvent(ev)) {
2948
- const message = extractErrorMessage(ev) || "Cursor run failed";
2949
- emitError(message, buildProviderDetail(ev.subtype ? `error.${ev.subtype}` : "error", {}, ev));
2950
- return;
2951
- }
2952
- const delta = extractAssistantDelta2(ev);
2953
- if (delta) {
2954
- aggregated += delta;
2955
- emit({ type: "delta", text: delta, providerDetail: buildProviderDetail("delta", {}, ev) });
2956
- }
2957
- if (ev.type === "result") {
2958
- const resultText = extractResultText2(ev);
2959
- if (!aggregated && resultText) {
2960
- aggregated = resultText;
2961
- }
2962
- if (!sawError) {
2963
- emitFinal(aggregated || resultText || "");
2964
- emit({
2965
- type: "detail",
2966
- provider: "cursor",
2967
- providerDetail: buildProviderDetail(
2968
- "result",
2969
- {
2970
- subtype: ev.subtype,
2971
- duration_ms: typeof ev.duration_ms === "number" ? ev.duration_ms : void 0,
2972
- request_id: typeof ev.request_id === "string" ? ev.request_id : void 0,
2973
- is_error: typeof ev.is_error === "boolean" ? ev.is_error : void 0
2974
- },
2975
- ev
2976
- )
2977
- });
2978
- }
2979
- }
2980
- };
2981
- const handleLine = (line, source) => {
2982
- const trimmed = line.trim();
2983
- if (!trimmed) return;
2984
- const payload = trimmed.startsWith("data: ") ? trimmed.slice(6).trim() : trimmed;
2985
- const parsed = safeJsonParse3(payload);
2986
- if (!parsed || typeof parsed !== "object") {
2987
- emit({ type: "raw_line", line });
2988
- if (source === "stdout") {
2989
- rawOutput += `${line}
2990
- `;
2991
- pushLine(stdoutLines, line);
2992
- } else {
2993
- pushLine(stderrLines, line);
2994
- }
2995
- return;
2996
- }
2997
- sawJson = true;
2998
- handleEvent(parsed);
2999
- };
3000
- const stdoutParser = createLineParser((line) => handleLine(line, "stdout"));
3001
- const stderrParser = createLineParser((line) => handleLine(line, "stderr"));
3002
- child.stdout?.on("data", stdoutParser);
3003
- child.stderr?.on("data", stderrParser);
3004
- child.on("close", (code) => {
3005
- if (!didFinalize) {
3006
- if (code && code !== 0) {
3007
- const hint = stderrLines.at(-1) || stdoutLines.at(-1) || "";
3008
- const suffix = hint ? `: ${hint}` : "";
3009
- debugLog("Cursor", "exit", { code, stderr: stderrLines, stdout: stdoutLines });
3010
- emitError(`Cursor CLI exited with code ${code}${suffix}`);
3011
- } else if (!sawError) {
3012
- const fallback = !sawJson ? rawOutput.trim() : "";
3013
- emitFinal(aggregated || fallback);
3014
- }
3015
- }
3016
- resolve({ sessionId: finalSessionId });
3017
- });
3018
- child.on("error", (err) => {
3019
- debugLog("Cursor", "spawn-error", { message: err?.message });
3020
- emitError(err?.message ?? "Cursor failed to start");
3021
- resolve({ sessionId: finalSessionId });
3022
- });
3023
- });
3024
- }
3025
-
3026
- // src/providers/local.ts
3027
- function getLocalBaseUrl() {
3028
- const base = process.env.AGENTCONNECT_LOCAL_BASE_URL || "http://localhost:11434/v1";
3029
- return base.replace(/\/+$/, "");
3030
- }
3031
- function getLocalApiKey() {
3032
- return process.env.AGENTCONNECT_LOCAL_API_KEY || "";
3033
- }
3034
- function resolveLocalModel(model, fallback) {
3035
- if (!model) return fallback;
3036
- const raw = String(model);
3037
- if (raw === "local") return fallback;
3038
- if (raw.startsWith("local:")) return raw.slice("local:".length);
3039
- if (raw.startsWith("local/")) return raw.slice("local/".length);
3040
- return raw;
3041
- }
3042
- async function fetchJson3(url, options = {}) {
3043
- const controller = new AbortController();
3044
- const timer = setTimeout(() => controller.abort(), 4e3);
3045
- try {
3046
- const res = await fetch(url, { ...options, signal: controller.signal });
3047
- if (!res.ok) {
3048
- return { ok: false, status: res.status, data: null };
3049
- }
3050
- const data = await res.json();
3051
- return { ok: true, status: res.status, data };
3052
- } catch {
3053
- return { ok: false, status: 0, data: null };
3054
- } finally {
3055
- clearTimeout(timer);
3056
- }
3057
- }
3058
- async function ensureLocalInstalled() {
3059
- const base = getLocalBaseUrl();
3060
- const res = await fetchJson3(`${base}/models`);
3061
- return { installed: res.ok };
3062
- }
3063
- async function getLocalStatus() {
3064
- const base = getLocalBaseUrl();
3065
- const res = await fetchJson3(`${base}/models`);
3066
- if (!res.ok) return { installed: false, loggedIn: false };
3067
- return { installed: true, loggedIn: true };
3068
- }
3069
- async function updateLocal() {
3070
- return getLocalStatus();
3071
- }
3072
- async function loginLocal(options = {}) {
3073
- if (typeof options.baseUrl === "string") {
3074
- process.env.AGENTCONNECT_LOCAL_BASE_URL = options.baseUrl;
3075
- }
3076
- if (typeof options.apiKey === "string") {
3077
- process.env.AGENTCONNECT_LOCAL_API_KEY = options.apiKey;
3078
- }
3079
- if (typeof options.model === "string") {
3080
- process.env.AGENTCONNECT_LOCAL_MODEL = options.model;
3081
- }
3082
- if (Array.isArray(options.models)) {
3083
- process.env.AGENTCONNECT_LOCAL_MODELS = JSON.stringify(options.models.filter(Boolean));
3084
- }
3085
- const status = await getLocalStatus();
3086
- return { loggedIn: status.installed };
3087
- }
3088
- async function listLocalModels() {
3089
- const base = getLocalBaseUrl();
3090
- const res = await fetchJson3(`${base}/models`);
3091
- if (!res.ok || !res.data || !Array.isArray(res.data.data)) return [];
3092
- return res.data.data.map((entry) => ({ id: entry.id, provider: "local", displayName: entry.id })).filter((entry) => entry.id);
3093
- }
3094
- async function runLocalPrompt({
3095
- prompt,
3096
- model,
3097
- onEvent
3098
- }) {
3099
- const base = getLocalBaseUrl();
3100
- const fallback = process.env.AGENTCONNECT_LOCAL_MODEL || "";
3101
- const resolvedModel = resolveLocalModel(model, fallback);
3102
- if (!resolvedModel) {
3103
- onEvent({ type: "error", message: "Local provider model is not configured." });
3104
- return { sessionId: null };
3105
- }
3106
- const payload = {
3107
- model: resolvedModel,
3108
- messages: [{ role: "user", content: prompt }],
3109
- stream: false
3110
- };
3111
- const headers = { "Content-Type": "application/json" };
3112
- const apiKey = getLocalApiKey();
3113
- if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
3114
- const res = await fetchJson3(`${base}/chat/completions`, {
3115
- method: "POST",
3116
- headers,
3117
- body: JSON.stringify(payload)
3118
- });
3119
- if (!res.ok) {
3120
- onEvent({ type: "error", message: "Local provider request failed." });
3121
- return { sessionId: null };
3122
- }
3123
- const message = res.data?.choices?.[0]?.message?.content;
3124
- const text = typeof message === "string" ? message : "";
3125
- if (text) {
3126
- onEvent({ type: "delta", text });
3127
- onEvent({ type: "final", text });
3128
- } else {
3129
- onEvent({ type: "error", message: "Local provider returned no content." });
3130
- }
3131
- return { sessionId: null };
3132
- }
3133
-
3134
- // src/providers/index.ts
3135
- var providers = {
3136
- claude: {
3137
- id: "claude",
3138
- name: "Claude",
3139
- ensureInstalled: ensureClaudeInstalled,
3140
- fastStatus: getClaudeFastStatus,
3141
- status: getClaudeStatus,
3142
- update: updateClaude,
3143
- login: loginClaude,
3144
- logout: async () => {
3145
- },
3146
- runPrompt: runClaudePrompt
3147
- },
3148
- codex: {
3149
- id: "codex",
3150
- name: "Codex",
3151
- ensureInstalled: ensureCodexInstalled,
3152
- fastStatus: getCodexFastStatus,
3153
- status: getCodexStatus,
3154
- update: updateCodex,
3155
- login: loginCodex,
3156
- logout: async () => {
3157
- },
3158
- runPrompt: runCodexPrompt
3159
- },
3160
- cursor: {
3161
- id: "cursor",
3162
- name: "Cursor",
3163
- ensureInstalled: ensureCursorInstalled,
3164
- fastStatus: getCursorFastStatus,
3165
- status: getCursorStatus,
3166
- update: updateCursor,
3167
- login: loginCursor,
3168
- logout: async () => {
3169
- },
3170
- runPrompt: runCursorPrompt
3171
- },
3172
- local: {
3173
- id: "local",
3174
- name: "Local",
3175
- ensureInstalled: ensureLocalInstalled,
3176
- status: getLocalStatus,
3177
- update: updateLocal,
3178
- login: loginLocal,
3179
- logout: async () => {
3180
- },
3181
- runPrompt: runLocalPrompt
3182
- }
3183
- };
3184
- async function listModels() {
3185
- const claudeModels = await listClaudeModels();
3186
- const codexModels = await listCodexModels();
3187
- const cursorModels = await listCursorModels();
3188
- const base = [
3189
- ...claudeModels,
3190
- ...cursorModels,
3191
- { id: "local", provider: "local", displayName: "Local Model" }
3192
- ];
3193
- const envModels = process.env.AGENTCONNECT_LOCAL_MODELS;
3194
- if (envModels) {
3195
- try {
3196
- const parsed = JSON.parse(envModels);
3197
- if (Array.isArray(parsed)) {
3198
- for (const entry of parsed) {
3199
- if (typeof entry === "string" && entry) {
3200
- base.push({ id: entry, provider: "local", displayName: entry });
3201
- }
3202
- }
3203
- }
3204
- } catch {
3205
- }
3206
- }
3207
- const discovered = await listLocalModels();
3208
- const all = [...base, ...codexModels, ...discovered.filter((entry) => entry.id !== "local")];
3209
- const seen = /* @__PURE__ */ new Set();
3210
- return all.filter((entry) => {
3211
- const key = `${entry.provider}:${entry.id}`;
3212
- if (seen.has(key)) return false;
3213
- seen.add(key);
3214
- return true;
3215
- });
3216
- }
3217
- async function listRecentModels(providerId) {
3218
- if (providerId && providerId !== "claude") return [];
3219
- const recent = await listClaudeRecentModels();
3220
- return recent.filter((entry) => entry.provider === "claude");
3221
- }
3222
- function resolveProviderForModel(model) {
3223
- const lower = String(model || "").toLowerCase();
3224
- if (lower.includes("cursor")) return "cursor";
3225
- if (lower.includes("codex")) return "codex";
3226
- if (lower.startsWith("gpt") || lower.startsWith("o1") || lower.startsWith("o3") || lower.startsWith("o4")) {
3227
- return "codex";
3228
- }
3229
- if (lower.includes("local")) return "local";
3230
- if (lower.includes("opus") || lower.includes("sonnet") || lower.includes("haiku"))
3231
- return "claude";
3232
- if (lower.includes("claude")) return "claude";
3233
- return "claude";
3234
- }
3235
-
3236
- // src/observed.ts
3237
- import fs from "fs";
3238
- import path5 from "path";
3239
- function createObservedTracker({
3240
- basePath,
3241
- appId,
3242
- requested = []
3243
- }) {
3244
- const requestedList = Array.isArray(requested) ? requested.filter(Boolean) : [];
3245
- const dirPath = path5.join(basePath, ".agentconnect");
3246
- const filePath = path5.join(dirPath, "observed-capabilities.json");
3247
- const observed = /* @__PURE__ */ new Set();
3248
- let writeTimer = null;
3249
- function load() {
3250
- if (!fs.existsSync(filePath)) return;
3251
- try {
3252
- const raw = fs.readFileSync(filePath, "utf8");
3253
- const parsed = JSON.parse(raw);
3254
- if (Array.isArray(parsed?.observed)) {
3255
- for (const entry of parsed.observed) {
3256
- if (typeof entry === "string" && entry) observed.add(entry);
3257
- }
3258
- }
3259
- } catch {
3260
- }
3261
- }
3262
- function snapshot() {
3263
- return {
3264
- appId,
3265
- requested: requestedList,
3266
- observed: Array.from(observed).sort(),
3267
- updatedAt: (/* @__PURE__ */ new Date()).toISOString()
3268
- };
3269
- }
3270
- function flush() {
3271
- if (writeTimer) {
3272
- clearTimeout(writeTimer);
3273
- writeTimer = null;
3274
- }
3275
- const payload = JSON.stringify(snapshot(), null, 2);
3276
- fs.mkdirSync(dirPath, { recursive: true });
3277
- fs.writeFileSync(filePath, payload);
3278
- }
3279
- function scheduleFlush() {
3280
- if (writeTimer) return;
3281
- writeTimer = setTimeout(() => {
3282
- flush();
3283
- }, 400);
3284
- }
3285
- function record(capability) {
3286
- const value = typeof capability === "string" ? capability.trim() : "";
3287
- if (!value) return;
3288
- if (observed.has(value)) return;
3289
- observed.add(value);
3290
- scheduleFlush();
3291
- }
3292
- function list() {
3293
- return Array.from(observed).sort();
3294
- }
3295
- load();
3296
- return {
3297
- record,
3298
- list,
3299
- snapshot,
3300
- flush
3301
- };
3302
- }
3303
-
3304
- // src/host.ts
3305
- function send(socket, payload) {
3306
- socket.send(JSON.stringify(payload));
3307
- }
3308
- function reply(socket, id, result) {
3309
- send(socket, { jsonrpc: "2.0", id, result });
3310
- }
3311
- function replyError(socket, id, code, message) {
3312
- send(socket, {
3313
- jsonrpc: "2.0",
3314
- id,
3315
- error: { code, message }
3316
- });
3317
- }
3318
- function sessionEvent(socket, sessionId, type, data) {
3319
- if (process.env.AGENTCONNECT_DEBUG?.trim()) {
3320
- try {
3321
- console.log(
3322
- `[AgentConnect][Session ${sessionId}] ${type} ${JSON.stringify(data)}`
3323
- );
3324
- } catch {
3325
- console.log(`[AgentConnect][Session ${sessionId}] ${type}`);
3326
- }
3327
- }
3328
- send(socket, {
3329
- jsonrpc: "2.0",
3330
- method: "acp.session.event",
3331
- params: { sessionId, type, data }
3332
- });
3333
- }
3334
- function buildProviderList(statuses) {
3335
- return Object.values(providers).map((provider) => {
3336
- const info = statuses[provider.id] || {};
3337
- return {
3338
- id: provider.id,
3339
- name: provider.name,
3340
- installed: info.installed ?? false,
3341
- loggedIn: info.loggedIn ?? false,
3342
- version: info.version,
3343
- updateAvailable: info.updateAvailable,
3344
- latestVersion: info.latestVersion,
3345
- updateCheckedAt: info.updateCheckedAt,
3346
- updateSource: info.updateSource,
3347
- updateCommand: info.updateCommand,
3348
- updateMessage: info.updateMessage,
3349
- updateInProgress: info.updateInProgress
3350
- };
3351
- });
3352
- }
3353
- function startDevHost({
3354
- host = "127.0.0.1",
3355
- port = 9630,
3356
- appPath,
3357
- uiUrl
3358
- } = {}) {
3359
- process.env.AGENTCONNECT_HOST_MODE ||= "dev";
3360
- const server = http.createServer();
3361
- const wss = new WebSocketServer({ server });
3362
- const sessions = /* @__PURE__ */ new Map();
3363
- const activeRuns = /* @__PURE__ */ new Map();
3364
- const updatingProviders = /* @__PURE__ */ new Map();
3365
- const processTable = /* @__PURE__ */ new Map();
3366
- const backendState = /* @__PURE__ */ new Map();
3367
- const statusCache = /* @__PURE__ */ new Map();
3368
- const statusCacheTtlMs = 8e3;
3369
- const statusInFlight = /* @__PURE__ */ new Map();
3370
- const basePath = appPath || process.cwd();
3371
- const manifest = readManifest(basePath);
3372
- const appId = manifest?.id || "agentconnect-dev-app";
3373
- const requestedCapabilities = Array.isArray(manifest?.capabilities) ? manifest.capabilities : [];
3374
- const observedTracker = createObservedTracker({
3375
- basePath,
3376
- appId,
3377
- requested: requestedCapabilities
3378
- });
3379
- function resolveAppPathInternal(input) {
3380
- if (!input) return basePath;
3381
- const value = String(input);
3382
- return path6.isAbsolute(value) ? value : path6.resolve(basePath, value);
3383
- }
3384
- function mapFileType(stat) {
3385
- if (stat.isFile()) return "file";
3386
- if (stat.isDirectory()) return "dir";
3387
- if (stat.isSymbolicLink()) return "link";
3388
- return "other";
3389
- }
3390
- async function allocatePort() {
3391
- return new Promise((resolve, reject) => {
3392
- const socket = net.createServer();
3393
- socket.listen(0, host, () => {
3394
- const address = socket.address();
3395
- if (!address || typeof address === "string") {
3396
- socket.close();
3397
- reject(new Error("Failed to allocate port."));
3398
- return;
3399
- }
3400
- const portValue = address.port;
3401
- socket.close(() => resolve(portValue));
3402
- });
3403
- socket.on("error", reject);
3404
- });
3405
- }
3406
- async function waitForHealthcheck(url, timeoutMs = 15e3) {
3407
- const start = Date.now();
3408
- while (Date.now() - start < timeoutMs) {
3409
- try {
3410
- const res = await fetch(url, { method: "GET" });
3411
- if (res.ok) return true;
3412
- } catch {
3413
- }
3414
- await new Promise((resolve) => setTimeout(resolve, 500));
3415
- }
3416
- return false;
3417
- }
3418
- function readManifest(root) {
3419
- try {
3420
- const raw = fs2.readFileSync(path6.join(root, "agentconnect.app.json"), "utf8");
3421
- return JSON.parse(raw);
3422
- } catch {
3423
- return null;
3424
- }
3425
- }
3426
- function recordCapability(capability) {
3427
- observedTracker.record(capability);
3428
- }
3429
- function recordModelCapability(model) {
3430
- const providerId = resolveProviderForModel(model);
3431
- if (!providerId) return;
3432
- recordCapability(`model.${providerId}`);
3433
- }
3434
- async function getCachedStatus(provider, options = {}) {
3435
- const cached = statusCache.get(provider.id);
3436
- const now = Date.now();
3437
- if (cached && now - cached.at < statusCacheTtlMs) {
3438
- return cached.status;
3439
- }
3440
- const existing = statusInFlight.get(provider.id);
3441
- if (existing) return existing;
3442
- if (options.allowFast && provider.fastStatus) {
3443
- try {
3444
- const fast = await provider.fastStatus();
3445
- const startedAt2 = Date.now();
3446
- const promise2 = provider.status().then((status) => {
3447
- debugLog("Providers", "status-check", {
3448
- providerId: provider.id,
3449
- durationMs: Date.now() - startedAt2,
3450
- completedAt: (/* @__PURE__ */ new Date()).toISOString()
3451
- });
3452
- statusCache.set(provider.id, { status, at: Date.now() });
3453
- return status;
3454
- }).finally(() => {
3455
- statusInFlight.delete(provider.id);
3456
- });
3457
- statusInFlight.set(provider.id, promise2);
3458
- return fast;
3459
- } catch {
3460
- }
3461
- }
3462
- const startedAt = Date.now();
3463
- const promise = provider.status().then((status) => {
3464
- debugLog("Providers", "status-check", {
3465
- providerId: provider.id,
3466
- durationMs: Date.now() - startedAt,
3467
- completedAt: (/* @__PURE__ */ new Date()).toISOString()
3468
- });
3469
- statusCache.set(provider.id, { status, at: Date.now() });
3470
- return status;
3471
- }).finally(() => {
3472
- statusInFlight.delete(provider.id);
3473
- });
3474
- statusInFlight.set(provider.id, promise);
3475
- return promise;
3476
- }
3477
- function invalidateStatus(providerId) {
3478
- if (!providerId) return;
3479
- statusCache.delete(providerId);
3480
- }
3481
- wss.on("connection", (socket) => {
3482
- socket.on("message", async (raw) => {
3483
- let payload;
3484
- try {
3485
- payload = JSON.parse(String(raw));
3486
- } catch {
3487
- return;
3488
- }
3489
- if (!payload || payload.jsonrpc !== "2.0" || payload.id === void 0) {
3490
- return;
3491
- }
3492
- const id = payload.id;
3493
- const method = payload.method;
3494
- const params = payload.params ?? {};
3495
- if (typeof method === "string" && method.startsWith("acp.")) {
3496
- recordCapability("agent.connect");
3497
- }
3498
- if (method === "acp.hello") {
3499
- const loginExperience = process.env.AGENTCONNECT_LOGIN_EXPERIENCE || process.env.AGENTCONNECT_CLAUDE_LOGIN_EXPERIENCE || (process.env.AGENTCONNECT_HOST_MODE === "dev" ? "terminal" : "embedded");
3500
- reply(socket, id, {
3501
- hostId: "agentconnect-dev",
3502
- hostName: "AgentConnect Dev Host",
3503
- hostVersion: "0.1.0",
3504
- protocolVersion: "0.1",
3505
- mode: "local",
3506
- capabilities: [],
3507
- providers: Object.keys(providers),
3508
- loginExperience
3509
- });
3510
- return;
3511
- }
3512
- if (method === "acp.providers.list") {
3513
- const statusEntries = await Promise.all(
3514
- Object.values(providers).map(async (provider) => {
3515
- try {
3516
- return [provider.id, await getCachedStatus(provider, { allowFast: true })];
3517
- } catch {
3518
- return [provider.id, { installed: false, loggedIn: false }];
3519
- }
3520
- })
3521
- );
3522
- const statuses = Object.fromEntries(statusEntries);
3523
- const list = buildProviderList(statuses).map((entry) => ({
3524
- ...entry,
3525
- updateInProgress: updatingProviders.has(entry.id)
3526
- }));
3527
- reply(socket, id, { providers: list });
3528
- return;
3529
- }
3530
- if (method === "acp.providers.status") {
3531
- const providerId = params.provider;
3532
- const provider = providers[providerId];
3533
- if (!provider) {
3534
- replyError(socket, id, "AC_ERR_UNSUPPORTED", "Unknown provider");
3535
- return;
3536
- }
3537
- const status = await getCachedStatus(provider, { allowFast: true });
3538
- reply(socket, id, {
3539
- provider: {
3540
- id: provider.id,
3541
- name: provider.name,
3542
- installed: status.installed,
3543
- loggedIn: status.loggedIn,
3544
- version: status.version,
3545
- updateAvailable: status.updateAvailable,
3546
- latestVersion: status.latestVersion,
3547
- updateCheckedAt: status.updateCheckedAt,
3548
- updateSource: status.updateSource,
3549
- updateCommand: status.updateCommand,
3550
- updateMessage: status.updateMessage,
3551
- updateInProgress: updatingProviders.has(provider.id) || status.updateInProgress
3552
- }
3553
- });
3554
- return;
3555
- }
3556
- if (method === "acp.providers.update") {
3557
- const providerId = params.provider;
3558
- const provider = providers[providerId];
3559
- if (!provider) {
3560
- replyError(socket, id, "AC_ERR_UNSUPPORTED", "Unknown provider");
3561
- return;
3562
- }
3563
- debugLog("Providers", "update-start", { providerId });
3564
- if (!updatingProviders.has(providerId)) {
3565
- const promise = provider.update().finally(() => {
3566
- updatingProviders.delete(providerId);
3567
- invalidateStatus(providerId);
3568
- });
3569
- updatingProviders.set(providerId, promise);
3570
- }
3571
- try {
3572
- const status = await updatingProviders.get(providerId);
3573
- debugLog("Providers", "update-complete", {
3574
- providerId,
3575
- updateAvailable: status.updateAvailable,
3576
- latestVersion: status.latestVersion,
3577
- updateMessage: status.updateMessage
3578
- });
3579
- reply(socket, id, {
3580
- provider: {
3581
- id: provider.id,
3582
- name: provider.name,
3583
- installed: status.installed,
3584
- loggedIn: status.loggedIn,
3585
- version: status.version,
3586
- updateAvailable: status.updateAvailable,
3587
- latestVersion: status.latestVersion,
3588
- updateCheckedAt: status.updateCheckedAt,
3589
- updateSource: status.updateSource,
3590
- updateCommand: status.updateCommand,
3591
- updateMessage: status.updateMessage,
3592
- updateInProgress: false
3593
- }
3594
- });
3595
- } catch (err) {
3596
- debugLog("Providers", "update-error", {
3597
- providerId,
3598
- message: err instanceof Error ? err.message : String(err)
3599
- });
3600
- replyError(socket, id, "AC_ERR_INTERNAL", err?.message || "Update failed");
3601
- }
3602
- return;
3603
- }
3604
- if (method === "acp.providers.ensureInstalled") {
3605
- const providerId = params.provider;
3606
- const provider = providers[providerId];
3607
- if (!provider) {
3608
- replyError(socket, id, "AC_ERR_UNSUPPORTED", "Unknown provider");
3609
- return;
3610
- }
3611
- const result = await provider.ensureInstalled();
3612
- invalidateStatus(provider.id);
3613
- reply(socket, id, result);
3614
- return;
3615
- }
3616
- if (method === "acp.providers.login") {
3617
- const providerId = params.provider;
3618
- const provider = providers[providerId];
3619
- if (!provider) {
3620
- replyError(socket, id, "AC_ERR_UNSUPPORTED", "Unknown provider");
3621
- return;
3622
- }
3623
- try {
3624
- const result = await provider.login(params.options);
3625
- invalidateStatus(provider.id);
3626
- reply(socket, id, result);
3627
- } catch (err) {
3628
- replyError(
3629
- socket,
3630
- id,
3631
- "AC_ERR_INTERNAL",
3632
- err?.message || "Provider login failed."
3633
- );
3634
- }
3635
- return;
3636
- }
3637
- if (method === "acp.providers.logout") {
3638
- const providerId = params.provider;
3639
- const provider = providers[providerId];
3640
- if (!provider) {
3641
- replyError(socket, id, "AC_ERR_UNSUPPORTED", "Unknown provider");
3642
- return;
3643
- }
3644
- await provider.logout();
3645
- invalidateStatus(provider.id);
3646
- reply(socket, id, {});
3647
- return;
3648
- }
3649
- if (method === "acp.models.list") {
3650
- const models = await listModels();
3651
- const providerId = params.provider;
3652
- if (providerId) {
3653
- reply(socket, id, { models: models.filter((m) => m.provider === providerId) });
3654
- } else {
3655
- reply(socket, id, { models });
3656
- }
3657
- return;
3658
- }
3659
- if (method === "acp.models.recent") {
3660
- const providerId = params.provider;
3661
- const models = await listRecentModels(providerId);
3662
- reply(socket, id, { models });
3663
- return;
3664
- }
3665
- if (method === "acp.models.info") {
3666
- const modelId = params.model;
3667
- const model = (await listModels()).find((m) => m.id === modelId);
3668
- if (!model) {
3669
- replyError(socket, id, "AC_ERR_INVALID_ARGS", "Unknown model");
3670
- return;
3671
- }
3672
- reply(socket, id, { model });
3673
- return;
3674
- }
3675
- if (method === "acp.sessions.create") {
3676
- const sessionId = `sess_${Math.random().toString(36).slice(2, 10)}`;
3677
- const model = params.model || "claude-opus";
3678
- const reasoningEffort = params.reasoningEffort || null;
3679
- const cwd = params.cwd ? resolveAppPathInternal(params.cwd) : void 0;
3680
- const repoRoot = params.repoRoot ? resolveAppPathInternal(params.repoRoot) : void 0;
3681
- const providerDetailLevel = params.providerDetailLevel || void 0;
3682
- const providerId = resolveProviderForModel(model);
3683
- recordModelCapability(model);
3684
- sessions.set(sessionId, {
3685
- id: sessionId,
3686
- providerId,
3687
- model,
3688
- providerSessionId: null,
3689
- reasoningEffort,
3690
- cwd,
3691
- repoRoot,
3692
- providerDetailLevel: providerDetailLevel === "raw" || providerDetailLevel === "minimal" ? providerDetailLevel : void 0
3693
- });
3694
- reply(socket, id, { sessionId });
3695
- return;
3696
- }
3697
- if (method === "acp.sessions.resume") {
3698
- const sessionId = params.sessionId;
3699
- const existing = sessions.get(sessionId);
3700
- if (!existing) {
3701
- const model = params.model || "claude-opus";
3702
- const reasoningEffort = params.reasoningEffort || null;
3703
- const cwd = params.cwd ? resolveAppPathInternal(params.cwd) : void 0;
3704
- const repoRoot = params.repoRoot ? resolveAppPathInternal(params.repoRoot) : void 0;
3705
- const providerDetailLevel = params.providerDetailLevel || void 0;
3706
- recordModelCapability(model);
3707
- sessions.set(sessionId, {
3708
- id: sessionId,
3709
- providerId: resolveProviderForModel(model),
3710
- model,
3711
- providerSessionId: params.providerSessionId || null,
3712
- reasoningEffort,
3713
- cwd,
3714
- repoRoot,
3715
- providerDetailLevel: providerDetailLevel === "raw" || providerDetailLevel === "minimal" ? providerDetailLevel : void 0
3716
- });
3717
- } else {
3718
- if (params.providerSessionId) {
3719
- existing.providerSessionId = String(params.providerSessionId);
3720
- }
3721
- if (params.cwd) {
3722
- existing.cwd = resolveAppPathInternal(params.cwd);
3723
- }
3724
- if (params.repoRoot) {
3725
- existing.repoRoot = resolveAppPathInternal(params.repoRoot);
3726
- }
3727
- if (params.providerDetailLevel) {
3728
- const level = String(params.providerDetailLevel);
3729
- if (level === "raw" || level === "minimal") {
3730
- existing.providerDetailLevel = level;
3731
- }
3732
- }
3733
- recordModelCapability(existing.model);
3734
- }
3735
- reply(socket, id, { sessionId });
3736
- return;
3737
- }
3738
- if (method === "acp.sessions.send") {
3739
- const sessionId = params.sessionId;
3740
- const message = params.message?.content || "";
3741
- const session = sessions.get(sessionId);
3742
- if (!session) {
3743
- replyError(socket, id, "AC_ERR_INVALID_ARGS", "Unknown session");
3744
- return;
3745
- }
3746
- recordModelCapability(session.model);
3747
- const provider = providers[session.providerId];
3748
- if (!provider) {
3749
- replyError(socket, id, "AC_ERR_UNSUPPORTED", "Unknown provider");
3750
- return;
3751
- }
3752
- if (updatingProviders.has(session.providerId)) {
3753
- replyError(socket, id, "AC_ERR_BUSY", "Provider update in progress.");
3754
- return;
3755
- }
3756
- const status = await provider.status();
3757
- if (!status.installed) {
3758
- const installed = await provider.ensureInstalled();
3759
- if (!installed.installed) {
3760
- replyError(socket, id, "AC_ERR_NOT_INSTALLED", "Provider CLI is not installed.");
3761
- return;
3762
- }
3763
- }
3764
- const controller = new AbortController();
3765
- const cwd = params.cwd ? resolveAppPathInternal(params.cwd) : session.cwd || basePath;
3766
- const repoRoot = params.repoRoot ? resolveAppPathInternal(params.repoRoot) : session.repoRoot || basePath;
3767
- const providerDetailLevel = params.providerDetailLevel === "raw" || params.providerDetailLevel === "minimal" ? params.providerDetailLevel : session.providerDetailLevel || "minimal";
3768
- activeRuns.set(sessionId, controller);
3769
- let sawError = false;
3770
- provider.runPrompt({
3771
- prompt: message,
3772
- resumeSessionId: session.providerSessionId,
3773
- model: session.model,
3774
- reasoningEffort: session.reasoningEffort,
3775
- repoRoot,
3776
- cwd,
3777
- providerDetailLevel,
3778
- signal: controller.signal,
3779
- onEvent: (event) => {
3780
- if (event.type === "error") {
3781
- sawError = true;
3782
- }
3783
- if (sawError && event.type === "final") {
3784
- return;
3785
- }
3786
- sessionEvent(socket, sessionId, event.type, { ...event });
3787
- }
3788
- }).then((result) => {
3789
- if (result?.sessionId) {
3790
- session.providerSessionId = result.sessionId;
3791
- }
3792
- }).catch((err) => {
3793
- if (!sawError) {
3794
- sessionEvent(socket, sessionId, "error", {
3795
- message: err?.message || "Provider error"
3796
- });
3797
- }
3798
- }).finally(() => {
3799
- activeRuns.delete(sessionId);
3800
- });
3801
- reply(socket, id, { accepted: true });
3802
- return;
3803
- }
3804
- if (method === "acp.sessions.cancel") {
3805
- const sessionId = params.sessionId;
3806
- const controller = activeRuns.get(sessionId);
3807
- if (controller) {
3808
- controller.abort();
3809
- activeRuns.delete(sessionId);
3810
- }
3811
- reply(socket, id, { cancelled: true });
3812
- return;
3813
- }
3814
- if (method === "acp.sessions.close") {
3815
- const sessionId = params.sessionId;
3816
- sessions.delete(sessionId);
3817
- reply(socket, id, { closed: true });
3818
- return;
3819
- }
3820
- if (method === "acp.fs.read") {
3821
- recordCapability("fs.read");
3822
- try {
3823
- const filePath = resolveAppPathInternal(params.path);
3824
- const encoding = params.encoding || "utf8";
3825
- const content = await fsp.readFile(filePath, encoding);
3826
- reply(socket, id, { content, encoding });
3827
- } catch (err) {
3828
- replyError(
3829
- socket,
3830
- id,
3831
- "AC_ERR_FS_READ",
3832
- err?.message || "Failed to read file."
3833
- );
3834
- }
3835
- return;
3836
- }
3837
- if (method === "acp.fs.write") {
3838
- recordCapability("fs.write");
3839
- try {
3840
- const filePath = resolveAppPathInternal(params.path);
3841
- const encoding = params.encoding || "utf8";
3842
- const content = params.content ?? "";
3843
- await fsp.mkdir(path6.dirname(filePath), { recursive: true });
3844
- await fsp.writeFile(filePath, content, {
3845
- encoding,
3846
- mode: params.mode
3847
- });
3848
- const bytes = Buffer.byteLength(String(content), encoding);
3849
- reply(socket, id, { bytes });
3850
- } catch (err) {
3851
- replyError(
3852
- socket,
3853
- id,
3854
- "AC_ERR_FS_WRITE",
3855
- err?.message || "Failed to write file."
3856
- );
3857
- }
3858
- return;
3859
- }
3860
- if (method === "acp.fs.list") {
3861
- recordCapability("fs.read");
3862
- try {
3863
- const dirPath = resolveAppPathInternal(params.path);
3864
- const entries = await fsp.readdir(dirPath, { withFileTypes: true });
3865
- const results = [];
3866
- for (const entry of entries) {
3867
- const entryPath = path6.join(dirPath, entry.name);
3868
- let size = 0;
3869
- let type = "other";
3870
- try {
3871
- const stat = await fsp.lstat(entryPath);
3872
- type = mapFileType(stat);
3873
- if (type === "file") size = stat.size;
3874
- } catch {
3875
- type = entry.isDirectory() ? "dir" : entry.isFile() ? "file" : "other";
3876
- }
3877
- results.push({
3878
- name: entry.name,
3879
- path: entryPath,
3880
- type,
3881
- size
3882
- });
3883
- }
3884
- reply(socket, id, { entries: results });
3885
- } catch (err) {
3886
- replyError(
3887
- socket,
3888
- id,
3889
- "AC_ERR_FS_LIST",
3890
- err?.message || "Failed to list directory."
3891
- );
3892
- }
3893
- return;
3894
- }
3895
- if (method === "acp.fs.stat") {
3896
- recordCapability("fs.read");
3897
- try {
3898
- const filePath = resolveAppPathInternal(params.path);
3899
- const stat = await fsp.lstat(filePath);
3900
- reply(socket, id, {
3901
- type: mapFileType(stat),
3902
- size: stat.size,
3903
- mtime: stat.mtime.toISOString()
3904
- });
3905
- } catch (err) {
3906
- replyError(
3907
- socket,
3908
- id,
3909
- "AC_ERR_FS_STAT",
3910
- err?.message || "Failed to stat file."
3911
- );
3912
- }
3913
- return;
3914
- }
3915
- if (method === "acp.process.spawn") {
3916
- recordCapability("process.spawn");
3917
- try {
3918
- const command2 = String(params.command || "");
3919
- const args2 = Array.isArray(params.args) ? params.args.map(String) : [];
3920
- const cwd = params.cwd ? resolveAppPathInternal(params.cwd) : basePath;
3921
- const env = { ...process.env, ...params.env || {} };
3922
- const useTty = Boolean(params.tty);
3923
- const child = spawn5(command2, args2, {
3924
- cwd,
3925
- env,
3926
- stdio: useTty ? "inherit" : ["pipe", "pipe", "pipe"]
3927
- });
3928
- if (!useTty) {
3929
- child.stdout?.on("data", () => void 0);
3930
- child.stderr?.on("data", () => void 0);
3931
- }
3932
- if (typeof params.stdin === "string" && child.stdin) {
3933
- child.stdin.write(params.stdin);
3934
- child.stdin.end();
3935
- }
3936
- if (child.pid) {
3937
- processTable.set(child.pid, child);
3938
- child.on("close", () => {
3939
- if (child.pid) processTable.delete(child.pid);
3940
- });
3941
- }
3942
- reply(socket, id, { pid: child.pid });
3943
- } catch (err) {
3944
- replyError(
3945
- socket,
3946
- id,
3947
- "AC_ERR_PROCESS",
3948
- err?.message || "Failed to spawn process."
3949
- );
3950
- }
3951
- return;
3952
- }
3953
- if (method === "acp.process.kill") {
3954
- recordCapability("process.kill");
3955
- const pid = Number(params.pid);
3956
- const signal = params.signal || "SIGTERM";
3957
- const child = processTable.get(pid);
3958
- try {
3959
- const success = child ? child.kill(signal) : process.kill(pid, signal);
3960
- reply(socket, id, { success: Boolean(success) });
3961
- } catch (err) {
3962
- replyError(
3963
- socket,
3964
- id,
3965
- "AC_ERR_PROCESS",
3966
- err?.message || "Failed to kill process."
3967
- );
3968
- }
3969
- return;
3970
- }
3971
- if (method === "acp.net.request") {
3972
- recordCapability("network.request");
3973
- try {
3974
- if (typeof fetch !== "function") {
3975
- replyError(socket, id, "AC_ERR_NET", "Fetch is not available.");
3976
- return;
3977
- }
3978
- const controller = new AbortController();
3979
- const timeout = params.timeoutMs;
3980
- let timer = null;
3981
- if (timeout) {
3982
- timer = setTimeout(() => controller.abort(), Number(timeout));
3983
- }
3984
- const res = await fetch(String(params.url), {
3985
- method: params.method || "GET",
3986
- headers: params.headers || {},
3987
- body: params.body,
3988
- signal: controller.signal
3989
- });
3990
- if (timer) clearTimeout(timer);
3991
- const body = await res.text();
3992
- const headers = {};
3993
- res.headers.forEach((value, key) => {
3994
- headers[key] = value;
3995
- });
3996
- reply(socket, id, { status: res.status, headers, body });
3997
- } catch (err) {
3998
- replyError(
3999
- socket,
4000
- id,
4001
- "AC_ERR_NET",
4002
- err?.message || "Network request failed."
4003
- );
4004
- }
4005
- return;
4006
- }
4007
- if (method === "acp.backend.start") {
4008
- recordCapability("backend.run");
4009
- if (!manifest?.backend) {
4010
- reply(socket, id, { status: "disabled" });
4011
- return;
4012
- }
4013
- const backendConfig = manifest.backend;
4014
- const existing = backendState.get(appId);
4015
- if (existing?.status === "running") {
4016
- reply(socket, id, { status: "running", url: existing.url });
4017
- return;
4018
- }
4019
- try {
4020
- let assignedPort = null;
4021
- const declaredPort = backendConfig.env?.PORT;
4022
- if (declaredPort === void 0 || String(declaredPort) === "0") {
4023
- assignedPort = await allocatePort();
4024
- } else if (!Number.isNaN(Number(declaredPort))) {
4025
- assignedPort = Number(declaredPort);
4026
- }
4027
- const env = {
4028
- ...process.env,
4029
- ...backendConfig.env || {},
4030
- AGENTCONNECT_HOST: `ws://${host}:${port}`,
4031
- AGENTCONNECT_APP_ID: appId
4032
- };
4033
- if (assignedPort) {
4034
- env.PORT = String(assignedPort);
4035
- env.AGENTCONNECT_APP_PORT = String(assignedPort);
4036
- }
4037
- const cwd = backendConfig.cwd ? resolveAppPathInternal(backendConfig.cwd) : basePath;
4038
- const args2 = backendConfig.args || [];
4039
- const child = spawn5(backendConfig.command, args2, {
4040
- cwd,
4041
- env,
4042
- stdio: ["ignore", "pipe", "pipe"]
4043
- });
4044
- child.stdout?.on("data", () => void 0);
4045
- child.stderr?.on("data", () => void 0);
4046
- const url = assignedPort ? `http://${host}:${assignedPort}` : void 0;
4047
- const record = { status: "starting", pid: child.pid, url };
4048
- backendState.set(appId, record);
4049
- child.on("exit", () => {
4050
- backendState.set(appId, { status: "stopped" });
4051
- });
4052
- if (backendConfig.healthcheck?.type === "http" && assignedPort) {
4053
- const healthUrl = `http://${host}:${assignedPort}${backendConfig.healthcheck.path}`;
4054
- const ok = await waitForHealthcheck(healthUrl);
4055
- if (!ok) {
4056
- child.kill("SIGTERM");
4057
- backendState.set(appId, { status: "error" });
4058
- reply(socket, id, { status: "error" });
4059
- return;
4060
- }
4061
- }
4062
- backendState.set(appId, { status: "running", pid: child.pid, url });
4063
- reply(socket, id, { status: "running", url });
4064
- } catch (err) {
4065
- replyError(
4066
- socket,
4067
- id,
4068
- "AC_ERR_BACKEND",
4069
- err?.message || "Failed to start backend."
4070
- );
4071
- }
4072
- return;
4073
- }
4074
- if (method === "acp.backend.stop") {
4075
- recordCapability("backend.run");
4076
- const current = backendState.get(appId);
4077
- if (!current?.pid) {
4078
- backendState.set(appId, { status: "stopped" });
4079
- reply(socket, id, { status: "stopped" });
4080
- return;
4081
- }
4082
- try {
4083
- process.kill(current.pid, "SIGTERM");
4084
- } catch {
4085
- }
4086
- backendState.set(appId, { status: "stopped" });
4087
- reply(socket, id, { status: "stopped" });
4088
- return;
4089
- }
4090
- if (method === "acp.backend.status") {
4091
- recordCapability("backend.run");
4092
- const current = backendState.get(appId) || { status: "stopped" };
4093
- reply(socket, id, { status: current.status, url: current.url });
4094
- return;
4095
- }
4096
- if (method === "acp.capabilities.observed") {
4097
- reply(socket, id, { ...observedTracker.snapshot() });
4098
- return;
4099
- }
4100
- reply(socket, id, {});
4101
- });
4102
- });
4103
- server.listen(port, host, () => {
4104
- console.log(`AgentConnect dev host running at ws://${host}:${port}`);
4105
- if (appPath) console.log(`App path: ${appPath}`);
4106
- if (uiUrl) console.log(`UI dev server: ${uiUrl}`);
4107
- });
4108
- process.on("SIGINT", () => {
4109
- try {
4110
- observedTracker.flush();
4111
- } catch {
4112
- }
4113
- server.close(() => process.exit(0));
4114
- });
4115
- }
4
+ import path7 from "path";
5
+ import { promises as fs5 } from "fs";
6
+ import { createPrivateKey, createPublicKey as createPublicKey2, sign as signData } from "crypto";
7
+ import { startDevHost } from "@agentconnect/host";
4116
8
 
4117
9
  // src/paths.ts
4118
- import path7 from "path";
10
+ import path from "path";
4119
11
  import { fileURLToPath } from "url";
4120
- import { promises as fs3 } from "fs";
12
+ import { promises as fs } from "fs";
4121
13
  async function exists(target) {
4122
14
  try {
4123
- await fs3.stat(target);
15
+ await fs.stat(target);
4124
16
  return true;
4125
17
  } catch {
4126
18
  return false;
4127
19
  }
4128
20
  }
4129
21
  function resolveAppPath(input) {
4130
- const candidate = input ? path7.resolve(input) : process.cwd();
22
+ const candidate = input ? path.resolve(input) : process.cwd();
4131
23
  return candidate;
4132
24
  }
4133
25
  async function findSchemaDir() {
4134
26
  const envDir = process.env.AGENTCONNECT_SCHEMA_DIR;
4135
27
  if (envDir && await exists(envDir)) return envDir;
4136
- const start = path7.dirname(fileURLToPath(import.meta.url));
28
+ const start = path.dirname(fileURLToPath(import.meta.url));
4137
29
  const roots = [start, process.cwd()];
4138
30
  for (const root of roots) {
4139
31
  let current = root;
4140
32
  for (let i = 0; i < 8; i += 1) {
4141
- const candidate = path7.join(current, "schemas");
33
+ const candidate = path.join(current, "schemas");
4142
34
  if (await exists(candidate)) return candidate;
4143
- const parent = path7.dirname(current);
35
+ const parent = path.dirname(current);
4144
36
  if (parent === current) break;
4145
37
  current = parent;
4146
38
  }
@@ -4149,25 +41,25 @@ async function findSchemaDir() {
4149
41
  }
4150
42
 
4151
43
  // src/zip.ts
4152
- import fs5 from "fs";
4153
- import path9 from "path";
44
+ import fs3 from "fs";
45
+ import path3 from "path";
4154
46
  import { createHash } from "crypto";
4155
47
  import yazl from "yazl";
4156
48
  import yauzl from "yauzl";
4157
49
 
4158
50
  // src/fs-utils.ts
4159
- import path8 from "path";
4160
- import { promises as fs4 } from "fs";
51
+ import path2 from "path";
52
+ import { promises as fs2 } from "fs";
4161
53
  async function collectFiles(root, options = {}) {
4162
54
  const ignoreNames = new Set(options.ignoreNames || []);
4163
55
  const ignorePaths = new Set(options.ignorePaths || []);
4164
56
  const files = [];
4165
57
  async function walk(dir) {
4166
- const entries = await fs4.readdir(dir, { withFileTypes: true });
58
+ const entries = await fs2.readdir(dir, { withFileTypes: true });
4167
59
  for (const entry of entries) {
4168
60
  if (ignoreNames.has(entry.name)) continue;
4169
- const fullPath = path8.join(dir, entry.name);
4170
- const rel = path8.relative(root, fullPath);
61
+ const fullPath = path2.join(dir, entry.name);
62
+ const rel = path2.relative(root, fullPath);
4171
63
  if (ignorePaths.has(rel)) continue;
4172
64
  if (entry.isDirectory()) {
4173
65
  await walk(fullPath);
@@ -4180,16 +72,16 @@ async function collectFiles(root, options = {}) {
4180
72
  return files.sort((a, b) => a.rel.localeCompare(b.rel));
4181
73
  }
4182
74
  async function readJson(filePath) {
4183
- const raw = await fs4.readFile(filePath, "utf8");
75
+ const raw = await fs2.readFile(filePath, "utf8");
4184
76
  return JSON.parse(raw);
4185
77
  }
4186
78
  async function writeJson(filePath, data) {
4187
- await fs4.mkdir(path8.dirname(filePath), { recursive: true });
4188
- await fs4.writeFile(filePath, JSON.stringify(data, null, 2), "utf8");
79
+ await fs2.mkdir(path2.dirname(filePath), { recursive: true });
80
+ await fs2.writeFile(filePath, JSON.stringify(data, null, 2), "utf8");
4189
81
  }
4190
82
  async function fileExists(filePath) {
4191
83
  try {
4192
- await fs4.stat(filePath);
84
+ await fs2.stat(filePath);
4193
85
  return true;
4194
86
  } catch {
4195
87
  return false;
@@ -4200,7 +92,7 @@ async function fileExists(filePath) {
4200
92
  async function hashFile(filePath) {
4201
93
  return new Promise((resolve, reject) => {
4202
94
  const hash = createHash("sha256");
4203
- const stream = fs5.createReadStream(filePath);
95
+ const stream = fs3.createReadStream(filePath);
4204
96
  stream.on("data", (chunk) => hash.update(chunk));
4205
97
  stream.on("error", reject);
4206
98
  stream.on("end", () => resolve(hash.digest("hex")));
@@ -4212,14 +104,14 @@ async function zipDirectory({
4212
104
  ignoreNames = [],
4213
105
  ignorePaths = []
4214
106
  }) {
4215
- await fs5.promises.mkdir(path9.dirname(outputPath), { recursive: true });
107
+ await fs3.promises.mkdir(path3.dirname(outputPath), { recursive: true });
4216
108
  const zipfile = new yazl.ZipFile();
4217
109
  const files = await collectFiles(inputDir, { ignoreNames, ignorePaths });
4218
110
  for (const file of files) {
4219
111
  zipfile.addFile(file.fullPath, file.rel);
4220
112
  }
4221
113
  return new Promise((resolve, reject) => {
4222
- const outStream = fs5.createWriteStream(outputPath);
114
+ const outStream = fs3.createWriteStream(outputPath);
4223
115
  zipfile.outputStream.pipe(outStream);
4224
116
  outStream.on("close", () => resolve());
4225
117
  outStream.on("error", reject);
@@ -4268,10 +160,10 @@ async function readZipEntry(zipPath, entryName) {
4268
160
  }
4269
161
 
4270
162
  // src/manifest.ts
4271
- import path10 from "path";
163
+ import path4 from "path";
4272
164
  import Ajv from "ajv";
4273
165
  async function readManifestFromDir(appPath) {
4274
- const manifestPath = path10.join(appPath, "agentconnect.app.json");
166
+ const manifestPath = path4.join(appPath, "agentconnect.app.json");
4275
167
  if (!await fileExists(manifestPath)) {
4276
168
  throw new Error("agentconnect.app.json not found in app directory.");
4277
169
  }
@@ -4289,7 +181,7 @@ async function loadManifestSchema() {
4289
181
  if (!schemaDir) {
4290
182
  throw new Error("Unable to locate schemas directory.");
4291
183
  }
4292
- const schemaPath = path10.join(schemaDir, "app-manifest.json");
184
+ const schemaPath = path4.join(schemaDir, "app-manifest.json");
4293
185
  return readJson(schemaPath);
4294
186
  }
4295
187
  async function validateManifest(manifest) {
@@ -4301,9 +193,9 @@ async function validateManifest(manifest) {
4301
193
  }
4302
194
 
4303
195
  // src/registry.ts
4304
- import path11 from "path";
4305
- import { promises as fs6 } from "fs";
4306
- function compareSemver4(a, b) {
196
+ import path5 from "path";
197
+ import { promises as fs4 } from "fs";
198
+ function compareSemver(a, b) {
4307
199
  const parse = (v) => v.split("-")[0].split(".").map((n) => Number(n));
4308
200
  const pa = parse(a);
4309
201
  const pb = parse(b);
@@ -4325,26 +217,26 @@ async function publishPackage({
4325
217
  if (!appId || !version) {
4326
218
  throw new Error("Manifest must include id and version.");
4327
219
  }
4328
- const entryDir = path11.join(registryPath, "apps", appId, version);
4329
- await fs6.mkdir(entryDir, { recursive: true });
4330
- const targetZip = path11.join(entryDir, "app.zip");
4331
- await fs6.copyFile(zipPath, targetZip);
4332
- const manifestPath = path11.join(entryDir, "manifest.json");
220
+ const entryDir = path5.join(registryPath, "apps", appId, version);
221
+ await fs4.mkdir(entryDir, { recursive: true });
222
+ const targetZip = path5.join(entryDir, "app.zip");
223
+ await fs4.copyFile(zipPath, targetZip);
224
+ const manifestPath = path5.join(entryDir, "manifest.json");
4333
225
  await writeJson(manifestPath, resolvedManifest);
4334
226
  let signatureOut = null;
4335
227
  if (signaturePath) {
4336
- signatureOut = path11.join(entryDir, "signature.json");
4337
- await fs6.copyFile(signaturePath, signatureOut);
228
+ signatureOut = path5.join(entryDir, "signature.json");
229
+ await fs4.copyFile(signaturePath, signatureOut);
4338
230
  }
4339
231
  const hash = await hashFile(zipPath);
4340
- const indexPath = path11.join(registryPath, "index.json");
232
+ const indexPath = path5.join(registryPath, "index.json");
4341
233
  const index = await fileExists(indexPath) ? await readJson(indexPath) : { apps: {} };
4342
234
  if (!index.apps) index.apps = {};
4343
235
  if (!index.apps[appId]) {
4344
236
  index.apps[appId] = { latest: version, versions: {} };
4345
237
  }
4346
238
  index.apps[appId].versions[version] = {
4347
- path: path11.relative(registryPath, targetZip).replace(/\\/g, "/"),
239
+ path: path5.relative(registryPath, targetZip).replace(/\\/g, "/"),
4348
240
  manifest: resolvedManifest,
4349
241
  signature: signatureOut ? {
4350
242
  algorithm: "unknown",
@@ -4354,7 +246,7 @@ async function publishPackage({
4354
246
  hash
4355
247
  };
4356
248
  const currentLatest = index.apps[appId].latest;
4357
- if (!currentLatest || compareSemver4(version, currentLatest) > 0) {
249
+ if (!currentLatest || compareSemver(version, currentLatest) > 0) {
4358
250
  index.apps[appId].latest = version;
4359
251
  }
4360
252
  await writeJson(indexPath, index);
@@ -4362,9 +254,9 @@ async function publishPackage({
4362
254
  }
4363
255
 
4364
256
  // src/registry-validate.ts
4365
- import path12 from "path";
257
+ import path6 from "path";
4366
258
  import { createPublicKey, verify as verifySignature } from "crypto";
4367
- function compareSemver5(a, b) {
259
+ function compareSemver2(a, b) {
4368
260
  const parse = (v) => v.split("-")[0].split(".").map((n) => Number(n));
4369
261
  const pa = parse(a);
4370
262
  const pb = parse(b);
@@ -4376,7 +268,7 @@ function compareSemver5(a, b) {
4376
268
  }
4377
269
  function resolveEntry(registryPath, entryPath) {
4378
270
  if (!entryPath || typeof entryPath !== "string") return null;
4379
- return path12.resolve(registryPath, entryPath);
271
+ return path6.resolve(registryPath, entryPath);
4380
272
  }
4381
273
  function normalizeSignatureAlg(signatureAlg) {
4382
274
  const value = String(signatureAlg || "").toLowerCase();
@@ -4417,7 +309,7 @@ async function validateRegistry({
4417
309
  }) {
4418
310
  const errors = [];
4419
311
  const warnings = [];
4420
- const indexPath = path12.join(registryPath, "index.json");
312
+ const indexPath = path6.join(registryPath, "index.json");
4421
313
  if (!await fileExists(indexPath)) {
4422
314
  return {
4423
315
  valid: false,
@@ -4472,9 +364,9 @@ async function validateRegistry({
4472
364
  message: `App ${appId} latest (${latest}) not found in versions.`
4473
365
  });
4474
366
  }
4475
- const sorted = [...versionKeys].sort(compareSemver5);
367
+ const sorted = [...versionKeys].sort(compareSemver2);
4476
368
  const expectedLatest = sorted[sorted.length - 1];
4477
- if (latest && expectedLatest && compareSemver5(latest, expectedLatest) !== 0) {
369
+ if (latest && expectedLatest && compareSemver2(latest, expectedLatest) !== 0) {
4478
370
  warnings.push({
4479
371
  path: `apps.${appId}`,
4480
372
  message: `App ${appId} latest (${latest}) is not the newest (${expectedLatest}).`
@@ -4633,7 +525,7 @@ async function main() {
4633
525
  }
4634
526
  if (command === "pack") {
4635
527
  const appPath = resolveAppPath(getFlag("--app", "-a") ?? void 0);
4636
- const outPath = getFlag("--out", "-o") || path13.join(appPath, "dist", "app.zip");
528
+ const outPath = getFlag("--out", "-o") || path7.join(appPath, "dist", "app.zip");
4637
529
  const manifest = await readManifestFromDir(appPath);
4638
530
  const validation = await validateManifest(manifest);
4639
531
  if (!validation.valid) {
@@ -4643,7 +535,7 @@ async function main() {
4643
535
  }
4644
536
  const ignoreNames = ["node_modules", ".git", ".DS_Store"];
4645
537
  const ignorePaths = [];
4646
- const outputRel = path13.relative(appPath, outPath);
538
+ const outputRel = path7.relative(appPath, outPath);
4647
539
  if (!outputRel.startsWith("..") && outputRel !== "") {
4648
540
  ignorePaths.push(outputRel);
4649
541
  }
@@ -4655,7 +547,7 @@ async function main() {
4655
547
  const appArg = getFlag("--app", "-a");
4656
548
  const jsonOut = args.includes("--json");
4657
549
  const appPath = resolveAppPath(appArg ?? void 0);
4658
- const stats = await fs7.stat(appPath).catch(() => null);
550
+ const stats = await fs5.stat(appPath).catch(() => null);
4659
551
  if (!stats) {
4660
552
  console.error("App path not found.");
4661
553
  return 1;
@@ -4696,16 +588,16 @@ async function main() {
4696
588
  return 1;
4697
589
  }
4698
590
  const appPath = resolveAppPath(appArg);
4699
- const appStats = await fs7.stat(appPath).catch(() => null);
591
+ const appStats = await fs5.stat(appPath).catch(() => null);
4700
592
  if (!appStats || !appStats.isFile()) {
4701
593
  console.error("Sign requires a zip file path.");
4702
594
  return 1;
4703
595
  }
4704
- const outPath = getFlag("--out", "-o") || path13.join(path13.dirname(appPath), "app.sig.json");
596
+ const outPath = getFlag("--out", "-o") || path7.join(path7.dirname(appPath), "app.sig.json");
4705
597
  const manifest = await readManifestFromZip(appPath);
4706
598
  const hash = await hashFile(appPath);
4707
599
  const hashBuffer = Buffer.from(hash, "hex");
4708
- const privateKeyPem = await fs7.readFile(keyPath, "utf8");
600
+ const privateKeyPem = await fs5.readFile(keyPath, "utf8");
4709
601
  const privateKey = createPrivateKey(privateKeyPem);
4710
602
  const keyType = privateKey.asymmetricKeyType;
4711
603
  const algorithm = keyType === "ed25519" ? null : "sha256";
@@ -4725,8 +617,8 @@ async function main() {
4725
617
  publicKey: publicKeyPem,
4726
618
  createdAt: (/* @__PURE__ */ new Date()).toISOString()
4727
619
  };
4728
- await fs7.mkdir(path13.dirname(outPath), { recursive: true });
4729
- await fs7.writeFile(outPath, JSON.stringify(signaturePayload, null, 2), "utf8");
620
+ await fs5.mkdir(path7.dirname(outPath), { recursive: true });
621
+ await fs5.writeFile(outPath, JSON.stringify(signaturePayload, null, 2), "utf8");
4730
622
  console.log(`Signature written to ${outPath}`);
4731
623
  return 0;
4732
624
  }
@@ -4741,7 +633,7 @@ async function main() {
4741
633
  return 1;
4742
634
  }
4743
635
  const appPath = resolveAppPath(appArg);
4744
- const appStats = await fs7.stat(appPath).catch(() => null);
636
+ const appStats = await fs5.stat(appPath).catch(() => null);
4745
637
  if (!appStats || !appStats.isFile()) {
4746
638
  console.error("Publish requires a zip file path.");
4747
639
  return 1;