@yitom/acp-client 0.1.4 → 0.1.6
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/client/acp-client.d.ts +8 -0
- package/dist/contracts/index.d.ts +2 -0
- package/dist/contracts/quota.d.ts +61 -0
- package/dist/index.cjs +862 -124
- package/dist/index.d.ts +1 -0
- package/dist/index.js +824 -112
- package/dist/runtimes/agy/index.d.ts +1 -16
- package/dist/runtimes/agy/quota.d.ts +5 -0
- package/dist/runtimes/claude/index.d.ts +1 -0
- package/dist/runtimes/claude/quota.d.ts +4 -0
- package/dist/runtimes/codex/index.d.ts +7 -2
- package/dist/runtimes/codex/quota.d.ts +4 -0
- package/dist/runtimes/cursor/index.d.ts +1 -0
- package/dist/runtimes/cursor/quota.d.ts +2 -0
- package/dist/runtimes/index.d.ts +6 -1
- package/dist/runtimes/opencode/index.d.ts +1 -0
- package/dist/runtimes/opencode/quota.d.ts +3 -0
- package/dist/runtimes/opencode-v2/index.d.ts +10 -0
- package/dist/session/quota.d.ts +16 -0
- package/dist/sidecar.cjs +835 -120
- package/dist/version.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
1
3
|
var __defProp = Object.defineProperty;
|
|
2
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -5,6 +7,30 @@ var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
|
5
7
|
function __accessProp(key) {
|
|
6
8
|
return this[key];
|
|
7
9
|
}
|
|
10
|
+
var __toESMCache_node;
|
|
11
|
+
var __toESMCache_esm;
|
|
12
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
13
|
+
var canCache = mod != null && typeof mod === "object";
|
|
14
|
+
if (canCache) {
|
|
15
|
+
var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
|
|
16
|
+
var cached = cache.get(mod);
|
|
17
|
+
if (cached)
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
21
|
+
const to = isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
22
|
+
if (mod && typeof mod === "object" || typeof mod === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(mod))
|
|
24
|
+
if (!__hasOwnProp.call(to, key))
|
|
25
|
+
__defProp(to, key, {
|
|
26
|
+
get: __accessProp.bind(mod, key),
|
|
27
|
+
enumerable: true
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (canCache)
|
|
31
|
+
cache.set(mod, to);
|
|
32
|
+
return to;
|
|
33
|
+
};
|
|
8
34
|
var __toCommonJS = (from) => {
|
|
9
35
|
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
36
|
if (entry)
|
|
@@ -62,6 +88,8 @@ __export(exports_src, {
|
|
|
62
88
|
INKDOWN_VIRTUAL_DIR: () => INKDOWN_VIRTUAL_DIR,
|
|
63
89
|
INKDOWN_VIRTUAL_RESOURCES: () => INKDOWN_VIRTUAL_RESOURCES,
|
|
64
90
|
LEGACY_ZED_CODEX_ACP_NPM_PACKAGE: () => LEGACY_ZED_CODEX_ACP_NPM_PACKAGE,
|
|
91
|
+
OPENCODE_CLI_NPM_PACKAGE: () => OPENCODE_CLI_NPM_PACKAGE,
|
|
92
|
+
QuotaCache: () => QuotaCache,
|
|
65
93
|
acpReadTextFile: () => acpReadTextFile,
|
|
66
94
|
acpWriteTextFile: () => acpWriteTextFile,
|
|
67
95
|
agyAdapter: () => agyAdapter,
|
|
@@ -120,6 +148,7 @@ __export(exports_src, {
|
|
|
120
148
|
mergeTopLevelModelsIntoConfigOptions: () => mergeTopLevelModelsIntoConfigOptions,
|
|
121
149
|
ok: () => ok,
|
|
122
150
|
opencodeAdapter: () => opencodeAdapter,
|
|
151
|
+
opencodeV2Adapter: () => opencodeV2Adapter,
|
|
123
152
|
orderSilentAuthMethodIds: () => orderSilentAuthMethodIds,
|
|
124
153
|
parseAcpConfigOptions: () => parseAcpConfigOptions,
|
|
125
154
|
parseCursorCatalogOutput: () => parseCursorCatalogOutput,
|
|
@@ -127,16 +156,21 @@ __export(exports_src, {
|
|
|
127
156
|
parseLoadSessionSupported: () => parseLoadSessionSupported,
|
|
128
157
|
parseMcpHttpSupported: () => parseMcpHttpSupported,
|
|
129
158
|
parsePromptCapabilities: () => parsePromptCapabilities,
|
|
159
|
+
parseRateLimitsFromStatusText: () => parseRateLimitsFromStatusText,
|
|
130
160
|
parseResumeSessionSupported: () => parseResumeSessionSupported,
|
|
131
161
|
pickAllowOptionId: () => pickAllowOptionId,
|
|
132
162
|
probeAgyAuth: () => probeAgyAuth,
|
|
133
163
|
probeClaudeAuth: () => probeClaudeAuth,
|
|
164
|
+
probeClaudeQuota: () => probeClaudeQuota,
|
|
134
165
|
probeCodexAuth: () => probeCodexAuth,
|
|
166
|
+
probeCodexQuota: () => probeCodexQuota,
|
|
135
167
|
probeCopilotAuth: () => probeCopilotAuth,
|
|
136
168
|
probeCursorAuth: () => probeCursorAuth,
|
|
169
|
+
probeCursorQuota: () => probeCursorQuota,
|
|
137
170
|
probeDeepseekAuth: () => probeDeepseekAuth,
|
|
138
171
|
probeGeminiAuth: () => probeGeminiAuth,
|
|
139
172
|
probeOpencodeAuth: () => probeOpencodeAuth,
|
|
173
|
+
probeOpencodeQuota: () => probeOpencodeQuota,
|
|
140
174
|
provisionCustomProviderCodexHome: () => provisionCustomProviderCodexHome,
|
|
141
175
|
readAcpProxySettings: () => readAcpProxySettings,
|
|
142
176
|
readStoredAcpProvider: () => readStoredAcpProvider,
|
|
@@ -153,6 +187,7 @@ __export(exports_src, {
|
|
|
153
187
|
resolveGeminiHome: () => resolveGeminiHome,
|
|
154
188
|
resolveOpencodeAuthCandidates: () => resolveOpencodeAuthCandidates,
|
|
155
189
|
resolveOpencodeAuthFile: () => resolveOpencodeAuthFile,
|
|
190
|
+
resolveOpencodeAuthPath: () => resolveOpencodeAuthPath,
|
|
156
191
|
restoreOrCreateAcpSession: () => restoreOrCreateAcpSession,
|
|
157
192
|
runConnectAuthGate: () => runConnectAuthGate,
|
|
158
193
|
saveAcpProvider: () => saveAcpProvider,
|
|
@@ -234,6 +269,7 @@ var CODEX_ACP_NPM_PACKAGE = "@agentclientprotocol/codex-acp";
|
|
|
234
269
|
var CLAUDE_ACP_NPM_PACKAGE = "@agentclientprotocol/claude-agent-acp";
|
|
235
270
|
var DEEPSEEK_DSH_NPM_PACKAGE = "@deepseek-ai/dsh";
|
|
236
271
|
var AGY_ACP_NPM_PACKAGE = "@yitom/agy-acp-map";
|
|
272
|
+
var OPENCODE_CLI_NPM_PACKAGE = "@opencode/cli";
|
|
237
273
|
var LEGACY_ZED_CODEX_ACP_NPM_PACKAGE = "@zed-industries/codex-acp";
|
|
238
274
|
var DEFAULT_ACP_RUNTIME_ID = "codex-acp";
|
|
239
275
|
function isWindowsPlatform() {
|
|
@@ -286,6 +322,15 @@ var BUILTIN_ACP_RUNTIMES = [
|
|
|
286
322
|
args: ["acp"],
|
|
287
323
|
requiredEnvKeys: []
|
|
288
324
|
},
|
|
325
|
+
{
|
|
326
|
+
id: "opencode-v2",
|
|
327
|
+
name: "OpenCode v2",
|
|
328
|
+
description: "通过 npx 启动独立产品线 @opencode/cli v2(acp);与 1.x 共存,复用 v2 SQLite 登录态",
|
|
329
|
+
authHint: "v2 登录态在 SQLite(auth.json 仅兼容残留);未登录先用 v2 二进制跑 opencode auth login",
|
|
330
|
+
command: "npx",
|
|
331
|
+
args: ["-y", OPENCODE_CLI_NPM_PACKAGE, "acp"],
|
|
332
|
+
requiredEnvKeys: []
|
|
333
|
+
},
|
|
289
334
|
{
|
|
290
335
|
id: "cursor-cli",
|
|
291
336
|
name: "Cursor",
|
|
@@ -318,7 +363,7 @@ function findBuiltinAcpRuntime(id) {
|
|
|
318
363
|
return BUILTIN_ACP_RUNTIMES.find((item) => item.id === id);
|
|
319
364
|
}
|
|
320
365
|
// src/version.ts
|
|
321
|
-
var ACP_CLIENT_VERSION = "0.1.
|
|
366
|
+
var ACP_CLIENT_VERSION = "0.1.6";
|
|
322
367
|
// src/session/available-commands.ts
|
|
323
368
|
function normalizeCommand(raw) {
|
|
324
369
|
if (!raw || typeof raw !== "object")
|
|
@@ -407,42 +452,170 @@ class AvailableCommandsCache {
|
|
|
407
452
|
}
|
|
408
453
|
}
|
|
409
454
|
|
|
410
|
-
// src/
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
455
|
+
// src/session/quota.ts
|
|
456
|
+
function parseRateLimitsFromStatusText(text, runtimeId = "unknown") {
|
|
457
|
+
if (!text || typeof text !== "string")
|
|
458
|
+
return null;
|
|
459
|
+
const lines = text.split(/\r?\n/);
|
|
460
|
+
let account;
|
|
461
|
+
const windows = [];
|
|
462
|
+
let credits;
|
|
463
|
+
let status = "ok";
|
|
464
|
+
let message;
|
|
465
|
+
if (/account (?:disabled|banned|suspended)|suspended|banned/i.test(text)) {
|
|
466
|
+
status = "banned";
|
|
467
|
+
message = "账号已封禁或暂停使用 (Account banned / suspended)";
|
|
468
|
+
} else if (/RESOURCE_EXHAUSTED|quota reached|rate limit exceeded|429/i.test(text)) {
|
|
469
|
+
status = "exhausted";
|
|
470
|
+
message = "额度已用尽 (Quota / Rate limit reached)";
|
|
471
|
+
} else if (/unauthorized|authentication failed|invalid api key/i.test(text)) {
|
|
472
|
+
status = "unauthorized";
|
|
473
|
+
message = "认证失效 (Unauthorized / Invalid credentials)";
|
|
474
|
+
}
|
|
475
|
+
for (const rawLine of lines) {
|
|
476
|
+
const line = rawLine.replace(/\*\*/g, "").trim();
|
|
477
|
+
if (!line)
|
|
478
|
+
continue;
|
|
479
|
+
const accMatch = line.match(/^Account\s*[::]\s*([^()]+?)(?:\s*\(([^)]+)\))?$/i);
|
|
480
|
+
if (accMatch) {
|
|
481
|
+
account = {
|
|
482
|
+
plan: accMatch[1].trim(),
|
|
483
|
+
email: accMatch[2]?.trim()
|
|
484
|
+
};
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
const credMatch = line.match(/(?:\b|^)(?:[\w\s-]*?credits?)\s*[::]\s*(unlimited|[\d.]+)/i);
|
|
488
|
+
if (credMatch) {
|
|
489
|
+
const val = credMatch[1].trim();
|
|
490
|
+
credits = val.toLowerCase() === "unlimited" ? { unlimited: true, balance: "unlimited" } : { balance: Number(val) || val, unlimited: false };
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
const winMatch = line.match(/(?:(?:[\w-]+\s+)?([\w\s-]+?(?:limit|spend limit|window)))\s*[::]\s*(\d+(?:\.\d+)?)%\s*(?:left|remaining)(?:\s*\((?:resets\s*(?:at\s*)?([^)]+)|([^)]+))\))?/i);
|
|
494
|
+
if (winMatch) {
|
|
495
|
+
const label = winMatch[1].trim();
|
|
496
|
+
const pct = parseFloat(winMatch[2]);
|
|
497
|
+
const resetsAt = (winMatch[3] || winMatch[4] || "").trim() || undefined;
|
|
498
|
+
let windowDurationMins;
|
|
499
|
+
const lower = label.toLowerCase();
|
|
500
|
+
if (/5h|5-hour/i.test(lower))
|
|
501
|
+
windowDurationMins = 300;
|
|
502
|
+
else if (/week/i.test(lower))
|
|
503
|
+
windowDurationMins = 10080;
|
|
504
|
+
else if (/month/i.test(lower))
|
|
505
|
+
windowDurationMins = 43200;
|
|
506
|
+
else if (/day|24h/i.test(lower))
|
|
507
|
+
windowDurationMins = 1440;
|
|
508
|
+
else if (/(\d+)\s*h/i.test(lower)) {
|
|
509
|
+
const m = lower.match(/(\d+)\s*h/);
|
|
510
|
+
if (m)
|
|
511
|
+
windowDurationMins = parseInt(m[1], 10) * 60;
|
|
512
|
+
}
|
|
513
|
+
windows.push({
|
|
514
|
+
label,
|
|
515
|
+
percentLeft: pct,
|
|
516
|
+
usedPercent: Math.max(0, 100 - pct),
|
|
517
|
+
resetsAt,
|
|
518
|
+
windowDurationMins
|
|
519
|
+
});
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
const fastMatch = line.match(/(?:fast requests?|requests?)\s*[::]\s*(\d+)\s*\/\s*(\d+)/i);
|
|
523
|
+
if (fastMatch) {
|
|
524
|
+
const used = parseInt(fastMatch[1], 10);
|
|
525
|
+
const total = parseInt(fastMatch[2], 10);
|
|
526
|
+
const left = Math.max(0, total - used);
|
|
527
|
+
const pct = total > 0 ? Math.round(left / total * 100) : 0;
|
|
528
|
+
windows.push({
|
|
529
|
+
label: "Fast requests",
|
|
530
|
+
percentLeft: pct,
|
|
531
|
+
usedPercent: total > 0 ? Math.round(used / total * 100) : 0,
|
|
532
|
+
used,
|
|
533
|
+
total
|
|
534
|
+
});
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
if (!windows.length && !credits && !account && status === "ok") {
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
const primary = windows.find((w) => /5h|5-hour|hour|primary|fast/i.test(w.label)) || windows[0];
|
|
542
|
+
const secondary = windows.find((w) => w !== primary && /week|month|secondary/i.test(w.label)) || windows.find((w) => w !== primary);
|
|
543
|
+
const extraWindows = windows.filter((w) => w !== primary && w !== secondary);
|
|
544
|
+
if (primary && primary.percentLeft <= 10 && status === "ok") {
|
|
545
|
+
status = "warning";
|
|
546
|
+
}
|
|
440
547
|
return {
|
|
441
|
-
|
|
442
|
-
|
|
548
|
+
runtimeId,
|
|
549
|
+
account,
|
|
550
|
+
primaryWindow: primary,
|
|
551
|
+
secondaryWindow: secondary,
|
|
552
|
+
extraWindows: extraWindows.length ? extraWindows : undefined,
|
|
553
|
+
credits,
|
|
554
|
+
status,
|
|
555
|
+
message,
|
|
556
|
+
rawText: text,
|
|
557
|
+
updatedAt: Date.now()
|
|
443
558
|
};
|
|
444
559
|
}
|
|
445
560
|
|
|
561
|
+
class QuotaCache {
|
|
562
|
+
bySession = new Map;
|
|
563
|
+
byRuntime = new Map;
|
|
564
|
+
observe(params, defaultRuntimeId = "unknown") {
|
|
565
|
+
if (!params || typeof params !== "object")
|
|
566
|
+
return null;
|
|
567
|
+
const p = params;
|
|
568
|
+
const sessionId = typeof p.sessionId === "string" ? p.sessionId : undefined;
|
|
569
|
+
let textToParse = "";
|
|
570
|
+
if (p.update?.content && p.update.content.type === "text" && typeof p.update.content.text === "string") {
|
|
571
|
+
textToParse = p.update.content.text;
|
|
572
|
+
} else if (p.update?.message?.content && Array.isArray(p.update.message.content)) {
|
|
573
|
+
textToParse = p.update.message.content.filter((c) => c && c.type === "text" && typeof c.text === "string").map((c) => c.text).join(`
|
|
574
|
+
`);
|
|
575
|
+
}
|
|
576
|
+
if (!textToParse)
|
|
577
|
+
return null;
|
|
578
|
+
if (!/(?:limit|credits?|Account:|5h|resets|banned|suspended|quota)/i.test(textToParse)) {
|
|
579
|
+
return null;
|
|
580
|
+
}
|
|
581
|
+
const quota = parseRateLimitsFromStatusText(textToParse, defaultRuntimeId);
|
|
582
|
+
if (quota) {
|
|
583
|
+
if (sessionId)
|
|
584
|
+
this.bySession.set(sessionId, quota);
|
|
585
|
+
this.byRuntime.set(defaultRuntimeId, quota);
|
|
586
|
+
return quota;
|
|
587
|
+
}
|
|
588
|
+
return null;
|
|
589
|
+
}
|
|
590
|
+
set(quota, sessionId) {
|
|
591
|
+
if (sessionId)
|
|
592
|
+
this.bySession.set(sessionId, quota);
|
|
593
|
+
this.byRuntime.set(quota.runtimeId, quota);
|
|
594
|
+
}
|
|
595
|
+
get(sessionId, runtimeId) {
|
|
596
|
+
if (sessionId && this.bySession.has(sessionId)) {
|
|
597
|
+
return this.bySession.get(sessionId);
|
|
598
|
+
}
|
|
599
|
+
if (runtimeId && this.byRuntime.has(runtimeId)) {
|
|
600
|
+
return this.byRuntime.get(runtimeId);
|
|
601
|
+
}
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
clear() {
|
|
605
|
+
this.bySession.clear();
|
|
606
|
+
this.byRuntime.clear();
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// src/client/acp-client.ts
|
|
611
|
+
var import_sdk4 = require("@agentclientprotocol/sdk");
|
|
612
|
+
|
|
613
|
+
// src/runtimes/codex/quota.ts
|
|
614
|
+
var import_node_fs2 = require("node:fs");
|
|
615
|
+
var import_node_path2 = require("node:path");
|
|
616
|
+
var import_node_child_process = require("node:child_process");
|
|
617
|
+
var import_node_readline = __toESM(require("node:readline"), 1);
|
|
618
|
+
|
|
446
619
|
// src/runtimes/codex/codex-auth-preflight.ts
|
|
447
620
|
var import_node_fs = require("node:fs");
|
|
448
621
|
var import_node_os = require("node:os");
|
|
@@ -479,14 +652,252 @@ function probeCodexAuth() {
|
|
|
479
652
|
};
|
|
480
653
|
}
|
|
481
654
|
|
|
655
|
+
// src/runtimes/codex/quota.ts
|
|
656
|
+
function decodeJwtPayload(token) {
|
|
657
|
+
try {
|
|
658
|
+
const parts = token.split(".");
|
|
659
|
+
if (parts.length < 2)
|
|
660
|
+
return null;
|
|
661
|
+
const json = Buffer.from(parts[1], "base64").toString("utf8");
|
|
662
|
+
return JSON.parse(json);
|
|
663
|
+
} catch {
|
|
664
|
+
return null;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
var cachedDynamicResult = null;
|
|
668
|
+
async function queryCodexAppServer(timeoutMs = 4000) {
|
|
669
|
+
if (cachedDynamicResult && Date.now() - cachedDynamicResult.time < 15000) {
|
|
670
|
+
return cachedDynamicResult.res;
|
|
671
|
+
}
|
|
672
|
+
return new Promise((resolve) => {
|
|
673
|
+
let timer = null;
|
|
674
|
+
let proc = null;
|
|
675
|
+
let finished = false;
|
|
676
|
+
const cleanup = (res) => {
|
|
677
|
+
if (finished)
|
|
678
|
+
return;
|
|
679
|
+
finished = true;
|
|
680
|
+
if (timer)
|
|
681
|
+
clearTimeout(timer);
|
|
682
|
+
if (proc) {
|
|
683
|
+
try {
|
|
684
|
+
proc.kill();
|
|
685
|
+
} catch {}
|
|
686
|
+
}
|
|
687
|
+
if (res) {
|
|
688
|
+
cachedDynamicResult = { res, time: Date.now() };
|
|
689
|
+
}
|
|
690
|
+
resolve(res);
|
|
691
|
+
};
|
|
692
|
+
timer = setTimeout(() => cleanup(null), timeoutMs);
|
|
693
|
+
try {
|
|
694
|
+
proc = import_node_child_process.spawn("codex", ["app-server"], {
|
|
695
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
696
|
+
shell: process.platform === "win32",
|
|
697
|
+
windowsHide: true
|
|
698
|
+
});
|
|
699
|
+
if (!proc.stdin || !proc.stdout) {
|
|
700
|
+
return cleanup(null);
|
|
701
|
+
}
|
|
702
|
+
const rl = import_node_readline.default.createInterface({ input: proc.stdout });
|
|
703
|
+
let reqSeq = 1;
|
|
704
|
+
const pending = new Map;
|
|
705
|
+
const result = {};
|
|
706
|
+
rl.on("line", (line) => {
|
|
707
|
+
try {
|
|
708
|
+
const data = JSON.parse(line.trim());
|
|
709
|
+
if (data && typeof data.id === "number" && pending.has(data.id)) {
|
|
710
|
+
const cb = pending.get(data.id);
|
|
711
|
+
pending.delete(data.id);
|
|
712
|
+
cb(data.result);
|
|
713
|
+
}
|
|
714
|
+
} catch {}
|
|
715
|
+
});
|
|
716
|
+
proc.on("error", () => cleanup(null));
|
|
717
|
+
proc.on("exit", () => cleanup(Object.keys(result).length > 0 ? result : null));
|
|
718
|
+
const send = (method, params) => {
|
|
719
|
+
const id = reqSeq++;
|
|
720
|
+
const msg = JSON.stringify({ jsonrpc: "2.0", id, method, params }) + `
|
|
721
|
+
`;
|
|
722
|
+
proc.stdin.write(msg);
|
|
723
|
+
return new Promise((res) => {
|
|
724
|
+
pending.set(id, res);
|
|
725
|
+
});
|
|
726
|
+
};
|
|
727
|
+
(async () => {
|
|
728
|
+
try {
|
|
729
|
+
await send("initialize", {
|
|
730
|
+
clientInfo: { name: "acp-client", version: "0.1.6" }
|
|
731
|
+
});
|
|
732
|
+
const acc = await send("account/read", { refreshToken: false });
|
|
733
|
+
if (acc?.account) {
|
|
734
|
+
result.account = acc.account;
|
|
735
|
+
}
|
|
736
|
+
const limits = await send("account/rateLimits/read");
|
|
737
|
+
if (limits?.rateLimits) {
|
|
738
|
+
result.rateLimits = limits.rateLimits;
|
|
739
|
+
}
|
|
740
|
+
cleanup(result);
|
|
741
|
+
} catch {
|
|
742
|
+
cleanup(null);
|
|
743
|
+
}
|
|
744
|
+
})();
|
|
745
|
+
} catch {
|
|
746
|
+
cleanup(null);
|
|
747
|
+
}
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
function formatEpochTime(epochSeconds) {
|
|
751
|
+
if (!epochSeconds)
|
|
752
|
+
return;
|
|
753
|
+
try {
|
|
754
|
+
const d = new Date(epochSeconds * 1000);
|
|
755
|
+
const hours = String(d.getHours()).padStart(2, "0");
|
|
756
|
+
const mins = String(d.getMinutes()).padStart(2, "0");
|
|
757
|
+
return `${hours}:${mins}`;
|
|
758
|
+
} catch {
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
async function probeCodexQuota(options) {
|
|
763
|
+
const codexHome = resolveCodexHome();
|
|
764
|
+
const authPath = import_node_path2.join(codexHome, "auth.json");
|
|
765
|
+
let accountEmail;
|
|
766
|
+
let accountPlan = "Codex Free / Local";
|
|
767
|
+
let isLoggedIn = false;
|
|
768
|
+
if (import_node_fs2.existsSync(authPath)) {
|
|
769
|
+
try {
|
|
770
|
+
const authData = JSON.parse(import_node_fs2.readFileSync(authPath, "utf8"));
|
|
771
|
+
if (authData.OPENAI_API_KEY) {
|
|
772
|
+
isLoggedIn = true;
|
|
773
|
+
accountPlan = "OpenAI API Key";
|
|
774
|
+
}
|
|
775
|
+
if (authData.tokens?.id_token) {
|
|
776
|
+
isLoggedIn = true;
|
|
777
|
+
const payload = decodeJwtPayload(authData.tokens.id_token);
|
|
778
|
+
if (payload) {
|
|
779
|
+
if (typeof payload.email === "string")
|
|
780
|
+
accountEmail = payload.email;
|
|
781
|
+
const authObj = payload["https://api.openai.com/auth"];
|
|
782
|
+
if (authObj && typeof authObj.chatgpt_plan_type === "string") {
|
|
783
|
+
accountPlan = `ChatGPT ${authObj.chatgpt_plan_type}`;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
} else if (authData.tokens?.access_token) {
|
|
787
|
+
isLoggedIn = true;
|
|
788
|
+
}
|
|
789
|
+
} catch {}
|
|
790
|
+
}
|
|
791
|
+
if (process.env.CODEX_API_KEY || process.env.OPENAI_API_KEY) {
|
|
792
|
+
isLoggedIn = true;
|
|
793
|
+
accountPlan = "API Key (Env)";
|
|
794
|
+
}
|
|
795
|
+
const dynamic = await queryCodexAppServer(4000);
|
|
796
|
+
if (dynamic) {
|
|
797
|
+
isLoggedIn = true;
|
|
798
|
+
if (dynamic.account?.email)
|
|
799
|
+
accountEmail = dynamic.account.email;
|
|
800
|
+
if (dynamic.account?.planType)
|
|
801
|
+
accountPlan = `ChatGPT ${dynamic.account.planType}`;
|
|
802
|
+
}
|
|
803
|
+
if (!isLoggedIn) {
|
|
804
|
+
return {
|
|
805
|
+
runtimeId: "codex-acp",
|
|
806
|
+
provider: "OpenAI / Codex",
|
|
807
|
+
status: "unauthorized",
|
|
808
|
+
message: "未登录 Codex (Not logged in)",
|
|
809
|
+
updatedAt: Date.now()
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
let primaryWindow;
|
|
813
|
+
let secondaryWindow;
|
|
814
|
+
let credits;
|
|
815
|
+
if (dynamic?.rateLimits) {
|
|
816
|
+
const rl = dynamic.rateLimits;
|
|
817
|
+
if (rl.primary) {
|
|
818
|
+
const used = rl.primary.usedPercent ?? 0;
|
|
819
|
+
primaryWindow = {
|
|
820
|
+
label: "5h limit",
|
|
821
|
+
percentLeft: Math.max(0, Math.min(100, Math.round(100 - used))),
|
|
822
|
+
usedPercent: used,
|
|
823
|
+
windowDurationMins: rl.primary.windowDurationMins || 300,
|
|
824
|
+
resetsAt: formatEpochTime(rl.primary.resetsAt) || (rl.primary.resetsAt ? String(rl.primary.resetsAt) : undefined)
|
|
825
|
+
};
|
|
826
|
+
}
|
|
827
|
+
if (rl.secondary) {
|
|
828
|
+
const used = rl.secondary.usedPercent ?? 0;
|
|
829
|
+
secondaryWindow = {
|
|
830
|
+
label: "Weekly limit",
|
|
831
|
+
percentLeft: Math.max(0, Math.min(100, Math.round(100 - used))),
|
|
832
|
+
usedPercent: used,
|
|
833
|
+
windowDurationMins: rl.secondary.windowDurationMins || 10080,
|
|
834
|
+
resetsAt: formatEpochTime(rl.secondary.resetsAt) || (rl.secondary.resetsAt ? String(rl.secondary.resetsAt) : undefined)
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
if (rl.credits) {
|
|
838
|
+
credits = {
|
|
839
|
+
balance: rl.credits.balance,
|
|
840
|
+
unlimited: rl.credits.unlimited
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
return {
|
|
845
|
+
runtimeId: "codex-acp",
|
|
846
|
+
provider: "OpenAI / Codex",
|
|
847
|
+
account: {
|
|
848
|
+
email: accountEmail,
|
|
849
|
+
plan: accountPlan,
|
|
850
|
+
type: dynamic?.account?.type || "chatgpt"
|
|
851
|
+
},
|
|
852
|
+
primaryWindow,
|
|
853
|
+
secondaryWindow,
|
|
854
|
+
credits,
|
|
855
|
+
status: "ok",
|
|
856
|
+
updatedAt: Date.now()
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// src/runtimes/codex/index.ts
|
|
861
|
+
var import_promises2 = require("node:fs/promises");
|
|
862
|
+
var import_node_path5 = require("node:path");
|
|
863
|
+
|
|
864
|
+
// src/runtimes/codex/codex-provider-home.ts
|
|
865
|
+
var INKDOWN_PROVIDER_ID = "inkdown-custom";
|
|
866
|
+
var INKDOWN_PROVIDER_API_KEY_ENV = "INKDOWN_PROVIDER_API_KEY";
|
|
867
|
+
function tomlEscape(value) {
|
|
868
|
+
return value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
|
|
869
|
+
}
|
|
870
|
+
function buildCustomProviderConfigToml(config) {
|
|
871
|
+
const name = config.name?.trim() || "自定义 API";
|
|
872
|
+
const wireApi = config.wireApi === "responses" ? "responses" : "chat";
|
|
873
|
+
return [
|
|
874
|
+
`model_provider = "${INKDOWN_PROVIDER_ID}"`,
|
|
875
|
+
`model = "${tomlEscape(config.model.trim())}"`,
|
|
876
|
+
"",
|
|
877
|
+
`[model_providers.${INKDOWN_PROVIDER_ID}]`,
|
|
878
|
+
`name = "${tomlEscape(name)}"`,
|
|
879
|
+
`base_url = "${tomlEscape(config.baseUrl.trim())}"`,
|
|
880
|
+
`env_key = "${INKDOWN_PROVIDER_API_KEY_ENV}"`,
|
|
881
|
+
`wire_api = "${wireApi}"`,
|
|
882
|
+
""
|
|
883
|
+
].join(`
|
|
884
|
+
`);
|
|
885
|
+
}
|
|
886
|
+
function buildCustomProviderSpawnEnv(config, apiKey, codexHome) {
|
|
887
|
+
return {
|
|
888
|
+
CODEX_HOME: codexHome,
|
|
889
|
+
[INKDOWN_PROVIDER_API_KEY_ENV]: apiKey
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
|
|
482
893
|
// src/runtimes/codex/codex-provider.ts
|
|
483
894
|
var import_promises = require("node:fs/promises");
|
|
484
|
-
var
|
|
485
|
-
var
|
|
895
|
+
var import_node_fs3 = require("node:fs");
|
|
896
|
+
var import_node_path4 = require("node:path");
|
|
486
897
|
|
|
487
898
|
// src/client/acp-paths.ts
|
|
488
899
|
var import_node_os2 = require("node:os");
|
|
489
|
-
var
|
|
900
|
+
var import_node_path3 = require("node:path");
|
|
490
901
|
function getDefaultAgentDataDir() {
|
|
491
902
|
if (process.env.INKDOWN_DATA_DIR) {
|
|
492
903
|
return process.env.INKDOWN_DATA_DIR;
|
|
@@ -495,20 +906,20 @@ function getDefaultAgentDataDir() {
|
|
|
495
906
|
return process.env.ACP_DATA_DIR;
|
|
496
907
|
}
|
|
497
908
|
if (process.platform === "win32") {
|
|
498
|
-
return process.env.LOCALAPPDATA || process.env.APPDATA ||
|
|
909
|
+
return process.env.LOCALAPPDATA || process.env.APPDATA || import_node_path3.join(import_node_os2.homedir(), "AppData", "Local", "inkdown");
|
|
499
910
|
}
|
|
500
911
|
if (process.platform === "darwin") {
|
|
501
|
-
return
|
|
912
|
+
return import_node_path3.join(import_node_os2.homedir(), "Library", "Application Support", "inkdown");
|
|
502
913
|
}
|
|
503
|
-
return process.env.XDG_DATA_HOME ||
|
|
914
|
+
return process.env.XDG_DATA_HOME || import_node_path3.join(import_node_os2.homedir(), ".local", "share", "inkdown");
|
|
504
915
|
}
|
|
505
916
|
|
|
506
917
|
// src/runtimes/codex/codex-provider.ts
|
|
507
918
|
function providerFilePath(dir) {
|
|
508
|
-
return
|
|
919
|
+
return import_node_path4.join(dir, "agent", "provider.json");
|
|
509
920
|
}
|
|
510
921
|
function providerHomePath(dir) {
|
|
511
|
-
return
|
|
922
|
+
return import_node_path4.join(dir, "agent", "codex-home");
|
|
512
923
|
}
|
|
513
924
|
function toStatus(stored) {
|
|
514
925
|
if (!stored)
|
|
@@ -534,7 +945,7 @@ function validate(payload) {
|
|
|
534
945
|
}
|
|
535
946
|
async function readStoredAcpProvider(dir = getDefaultAgentDataDir()) {
|
|
536
947
|
const file = providerFilePath(dir);
|
|
537
|
-
if (!
|
|
948
|
+
if (!import_node_fs3.existsSync(file))
|
|
538
949
|
return null;
|
|
539
950
|
try {
|
|
540
951
|
const raw = JSON.parse(await import_promises.readFile(file, "utf8"));
|
|
@@ -563,7 +974,7 @@ async function saveAcpProvider(payload, dir = getDefaultAgentDataDir()) {
|
|
|
563
974
|
const apiKey = payload.apiKey.trim() || (await readStoredAcpProvider(dir))?.apiKey || "";
|
|
564
975
|
if (!apiKey)
|
|
565
976
|
return err({ code: "INVALID_ARGUMENT", message: "API Key 不能为空" });
|
|
566
|
-
await import_promises.mkdir(
|
|
977
|
+
await import_promises.mkdir(import_node_path4.join(dir, "agent"), { recursive: true });
|
|
567
978
|
const stored = {
|
|
568
979
|
name: payload.name?.trim() || undefined,
|
|
569
980
|
baseUrl: payload.baseUrl.trim(),
|
|
@@ -586,7 +997,7 @@ function getAcpProviderCodexHome(dir = getDefaultAgentDataDir()) {
|
|
|
586
997
|
async function provisionCustomProviderCodexHome(provider, codexHome) {
|
|
587
998
|
try {
|
|
588
999
|
await import_promises2.mkdir(codexHome, { recursive: true });
|
|
589
|
-
await import_promises2.writeFile(
|
|
1000
|
+
await import_promises2.writeFile(import_node_path5.join(codexHome, "config.toml"), buildCustomProviderConfigToml(provider), "utf8");
|
|
590
1001
|
return true;
|
|
591
1002
|
} catch (error) {
|
|
592
1003
|
console.error("[acp] 自定义供应商 config.toml 写入失败,回落本机登录", error);
|
|
@@ -595,6 +1006,7 @@ async function provisionCustomProviderCodexHome(provider, codexHome) {
|
|
|
595
1006
|
}
|
|
596
1007
|
var codexAdapter = {
|
|
597
1008
|
id: "codex-acp",
|
|
1009
|
+
probeQuota: async (options) => probeCodexQuota(options),
|
|
598
1010
|
probeAuth: () => {
|
|
599
1011
|
return probeCodexAuth();
|
|
600
1012
|
},
|
|
@@ -611,20 +1023,122 @@ var codexAdapter = {
|
|
|
611
1023
|
}
|
|
612
1024
|
};
|
|
613
1025
|
|
|
614
|
-
// src/runtimes/claude/
|
|
615
|
-
var
|
|
1026
|
+
// src/runtimes/claude/quota.ts
|
|
1027
|
+
var import_node_fs4 = require("node:fs");
|
|
616
1028
|
var import_node_os3 = require("node:os");
|
|
617
|
-
var
|
|
1029
|
+
var import_node_path6 = require("node:path");
|
|
1030
|
+
var import_node_child_process2 = require("node:child_process");
|
|
1031
|
+
var cachedClaudeAuth = null;
|
|
1032
|
+
async function queryClaudeAuthStatus(timeoutMs = 1200) {
|
|
1033
|
+
if (cachedClaudeAuth && Date.now() - cachedClaudeAuth.time < 30000) {
|
|
1034
|
+
return cachedClaudeAuth.res;
|
|
1035
|
+
}
|
|
1036
|
+
return new Promise((resolve) => {
|
|
1037
|
+
try {
|
|
1038
|
+
import_node_child_process2.execFile("claude", ["auth", "status", "--json"], { timeout: timeoutMs, windowsHide: true, shell: process.platform === "win32" }, (err, stdout) => {
|
|
1039
|
+
if (!stdout)
|
|
1040
|
+
return resolve(null);
|
|
1041
|
+
try {
|
|
1042
|
+
const data = JSON.parse(stdout.trim());
|
|
1043
|
+
cachedClaudeAuth = { res: data, time: Date.now() };
|
|
1044
|
+
resolve(data);
|
|
1045
|
+
} catch {
|
|
1046
|
+
resolve(null);
|
|
1047
|
+
}
|
|
1048
|
+
});
|
|
1049
|
+
} catch {
|
|
1050
|
+
resolve(null);
|
|
1051
|
+
}
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
async function probeClaudeQuota(options) {
|
|
1055
|
+
const claudeHome = resolveClaudeHome();
|
|
1056
|
+
const claudeJsonPath = import_node_path6.join(import_node_os3.homedir(), ".claude.json");
|
|
1057
|
+
const credsPath = import_node_path6.join(claudeHome, ".credentials.json");
|
|
1058
|
+
let isLoggedIn = false;
|
|
1059
|
+
let isBanned = false;
|
|
1060
|
+
let accountEmail;
|
|
1061
|
+
let plan = "Claude Free / Default";
|
|
1062
|
+
if (import_node_fs4.existsSync(claudeJsonPath)) {
|
|
1063
|
+
try {
|
|
1064
|
+
const data = JSON.parse(import_node_fs4.readFileSync(claudeJsonPath, "utf8"));
|
|
1065
|
+
if (data.oauthAccount) {
|
|
1066
|
+
isLoggedIn = true;
|
|
1067
|
+
accountEmail = data.oauthAccount.emailAddress;
|
|
1068
|
+
plan = data.oauthAccount.subscriptionTier || "Claude Pro";
|
|
1069
|
+
}
|
|
1070
|
+
if (data.isBanned || data.accountSuspended || data.status === "banned") {
|
|
1071
|
+
isBanned = true;
|
|
1072
|
+
}
|
|
1073
|
+
} catch {}
|
|
1074
|
+
}
|
|
1075
|
+
if (import_node_fs4.existsSync(credsPath)) {
|
|
1076
|
+
try {
|
|
1077
|
+
const creds = JSON.parse(import_node_fs4.readFileSync(credsPath, "utf8"));
|
|
1078
|
+
if (creds.accessToken || creds.sessionKey)
|
|
1079
|
+
isLoggedIn = true;
|
|
1080
|
+
} catch {}
|
|
1081
|
+
}
|
|
1082
|
+
if (process.env.ANTHROPIC_API_KEY || process.env.CLAUDE_CODE_OAUTH_TOKEN) {
|
|
1083
|
+
isLoggedIn = true;
|
|
1084
|
+
plan = "API Key (Env)";
|
|
1085
|
+
}
|
|
1086
|
+
const dynamic = await queryClaudeAuthStatus(1200);
|
|
1087
|
+
if (dynamic) {
|
|
1088
|
+
if (dynamic.loggedIn)
|
|
1089
|
+
isLoggedIn = true;
|
|
1090
|
+
if (dynamic.email)
|
|
1091
|
+
accountEmail = dynamic.email;
|
|
1092
|
+
if (dynamic.subscriptionType)
|
|
1093
|
+
plan = dynamic.subscriptionType;
|
|
1094
|
+
else if (dynamic.plan)
|
|
1095
|
+
plan = dynamic.plan;
|
|
1096
|
+
}
|
|
1097
|
+
if (isBanned) {
|
|
1098
|
+
return {
|
|
1099
|
+
runtimeId: "claude",
|
|
1100
|
+
provider: "Anthropic / Claude Code",
|
|
1101
|
+
status: "banned",
|
|
1102
|
+
message: "账号已被封禁或暂停使用 (Account suspended / banned)",
|
|
1103
|
+
updatedAt: Date.now()
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
if (!isLoggedIn) {
|
|
1107
|
+
return {
|
|
1108
|
+
runtimeId: "claude",
|
|
1109
|
+
provider: "Anthropic / Claude Code",
|
|
1110
|
+
status: "unauthorized",
|
|
1111
|
+
message: "未登录 Claude Code 或凭据无效 (Unauthorized)",
|
|
1112
|
+
updatedAt: Date.now()
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
return {
|
|
1116
|
+
runtimeId: "claude",
|
|
1117
|
+
provider: "Anthropic / Claude Code",
|
|
1118
|
+
account: {
|
|
1119
|
+
email: accountEmail,
|
|
1120
|
+
plan,
|
|
1121
|
+
type: dynamic?.authMethod || "oauth"
|
|
1122
|
+
},
|
|
1123
|
+
status: "ok",
|
|
1124
|
+
updatedAt: Date.now()
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
// src/runtimes/claude/index.ts
|
|
1129
|
+
var import_node_fs5 = require("node:fs");
|
|
1130
|
+
var import_node_os4 = require("node:os");
|
|
1131
|
+
var import_node_path7 = require("node:path");
|
|
618
1132
|
function resolveClaudeHome() {
|
|
619
1133
|
const fromEnv = process.env.CLAUDE_CONFIG_DIR?.trim();
|
|
620
1134
|
if (fromEnv)
|
|
621
1135
|
return fromEnv;
|
|
622
|
-
return
|
|
1136
|
+
return import_node_path7.join(import_node_os4.homedir(), ".claude");
|
|
623
1137
|
}
|
|
624
1138
|
function probeClaudeAuth() {
|
|
625
1139
|
const claudeHome = resolveClaudeHome();
|
|
626
|
-
const hasCodexHome =
|
|
627
|
-
const hasAuthFile =
|
|
1140
|
+
const hasCodexHome = import_node_fs5.existsSync(claudeHome);
|
|
1141
|
+
const hasAuthFile = import_node_fs5.existsSync(import_node_path7.join(import_node_os4.homedir(), ".claude.json")) || import_node_fs5.existsSync(import_node_path7.join(claudeHome, ".credentials.json"));
|
|
628
1142
|
const hasApiKeyEnv = Boolean(process.env.ANTHROPIC_API_KEY?.trim() || process.env.CLAUDE_CODE_OAUTH_TOKEN?.trim());
|
|
629
1143
|
return {
|
|
630
1144
|
codexHome: claudeHome,
|
|
@@ -637,21 +1151,22 @@ function probeClaudeAuth() {
|
|
|
637
1151
|
var claudeAdapter = {
|
|
638
1152
|
id: "claude",
|
|
639
1153
|
probeAuth: () => probeClaudeAuth(),
|
|
1154
|
+
probeQuota: async (options) => probeClaudeQuota(options),
|
|
640
1155
|
orderAuthMethods: (methods) => methods,
|
|
641
1156
|
canSkipInteractiveAuth: () => false
|
|
642
1157
|
};
|
|
643
1158
|
|
|
644
1159
|
// src/runtimes/gemini/index.ts
|
|
645
|
-
var
|
|
646
|
-
var
|
|
647
|
-
var
|
|
1160
|
+
var import_node_fs6 = require("node:fs");
|
|
1161
|
+
var import_node_os5 = require("node:os");
|
|
1162
|
+
var import_node_path8 = require("node:path");
|
|
648
1163
|
function resolveGeminiHome() {
|
|
649
|
-
return
|
|
1164
|
+
return import_node_path8.join(import_node_os5.homedir(), ".gemini");
|
|
650
1165
|
}
|
|
651
1166
|
function probeGeminiAuth() {
|
|
652
1167
|
const geminiHome = resolveGeminiHome();
|
|
653
|
-
const hasCodexHome =
|
|
654
|
-
const hasAuthFile =
|
|
1168
|
+
const hasCodexHome = import_node_fs6.existsSync(geminiHome);
|
|
1169
|
+
const hasAuthFile = import_node_fs6.existsSync(import_node_path8.join(geminiHome, "oauth_creds.json")) || Boolean(process.env.GOOGLE_APPLICATION_CREDENTIALS?.trim());
|
|
655
1170
|
const hasApiKeyEnv = Boolean(process.env.GEMINI_API_KEY?.trim() || process.env.GOOGLE_API_KEY?.trim());
|
|
656
1171
|
return {
|
|
657
1172
|
codexHome: geminiHome,
|
|
@@ -669,18 +1184,18 @@ var geminiAdapter = {
|
|
|
669
1184
|
};
|
|
670
1185
|
|
|
671
1186
|
// src/runtimes/copilot/index.ts
|
|
672
|
-
var
|
|
673
|
-
var
|
|
674
|
-
var
|
|
1187
|
+
var import_node_fs7 = require("node:fs");
|
|
1188
|
+
var import_node_os6 = require("node:os");
|
|
1189
|
+
var import_node_path9 = require("node:path");
|
|
675
1190
|
function resolveCopilotHome() {
|
|
676
1191
|
const fromEnv = process.env.COPILOT_HOME?.trim();
|
|
677
1192
|
if (fromEnv)
|
|
678
1193
|
return fromEnv;
|
|
679
|
-
return
|
|
1194
|
+
return import_node_path9.join(import_node_os6.homedir(), ".copilot");
|
|
680
1195
|
}
|
|
681
1196
|
function hasCopilotLogin(home) {
|
|
682
1197
|
try {
|
|
683
|
-
const raw =
|
|
1198
|
+
const raw = import_node_fs7.readFileSync(import_node_path9.join(home, "config.json"), "utf8");
|
|
684
1199
|
const parsed = JSON.parse(raw);
|
|
685
1200
|
return Array.isArray(parsed.loggedInUsers) && parsed.loggedInUsers.length > 0;
|
|
686
1201
|
} catch {
|
|
@@ -689,7 +1204,7 @@ function hasCopilotLogin(home) {
|
|
|
689
1204
|
}
|
|
690
1205
|
function probeCopilotAuth() {
|
|
691
1206
|
const copilotHome = resolveCopilotHome();
|
|
692
|
-
const hasCodexHome =
|
|
1207
|
+
const hasCodexHome = import_node_fs7.existsSync(copilotHome);
|
|
693
1208
|
const hasAuthFile = hasCopilotLogin(copilotHome);
|
|
694
1209
|
const hasApiKeyEnv = Boolean(process.env.COPILOT_GITHUB_TOKEN?.trim() || process.env.GH_TOKEN?.trim() || process.env.GITHUB_TOKEN?.trim());
|
|
695
1210
|
return {
|
|
@@ -707,17 +1222,129 @@ var copilotAdapter = {
|
|
|
707
1222
|
canSkipInteractiveAuth: () => false
|
|
708
1223
|
};
|
|
709
1224
|
|
|
1225
|
+
// src/runtimes/opencode/quota.ts
|
|
1226
|
+
var import_node_fs8 = require("node:fs");
|
|
1227
|
+
var import_node_child_process3 = require("node:child_process");
|
|
1228
|
+
function resolveOpencodeAuthPath() {
|
|
1229
|
+
const candidates = resolveOpencodeAuthCandidates();
|
|
1230
|
+
for (const c of candidates) {
|
|
1231
|
+
if (import_node_fs8.existsSync(c))
|
|
1232
|
+
return c;
|
|
1233
|
+
}
|
|
1234
|
+
return candidates[0] ?? null;
|
|
1235
|
+
}
|
|
1236
|
+
var cachedStats = null;
|
|
1237
|
+
async function queryOpencodeStats(timeoutMs = 1200) {
|
|
1238
|
+
if (cachedStats && Date.now() - cachedStats.time < 30000) {
|
|
1239
|
+
return cachedStats.res;
|
|
1240
|
+
}
|
|
1241
|
+
return new Promise((resolve) => {
|
|
1242
|
+
try {
|
|
1243
|
+
import_node_child_process3.execFile("opencode", ["stats", "--standalone", "--cost", "--json"], { timeout: timeoutMs, windowsHide: true }, (err, stdout) => {
|
|
1244
|
+
if (!stdout)
|
|
1245
|
+
return resolve(null);
|
|
1246
|
+
try {
|
|
1247
|
+
const data = JSON.parse(stdout.trim());
|
|
1248
|
+
cachedStats = { res: data, time: Date.now() };
|
|
1249
|
+
resolve(data);
|
|
1250
|
+
} catch {
|
|
1251
|
+
resolve(null);
|
|
1252
|
+
}
|
|
1253
|
+
});
|
|
1254
|
+
} catch {
|
|
1255
|
+
resolve(null);
|
|
1256
|
+
}
|
|
1257
|
+
});
|
|
1258
|
+
}
|
|
1259
|
+
async function queryOpencodeAuthList(timeoutMs = 1200) {
|
|
1260
|
+
return new Promise((resolve) => {
|
|
1261
|
+
try {
|
|
1262
|
+
import_node_child_process3.execFile("opencode", ["auth", "list"], { timeout: timeoutMs, windowsHide: true }, (err, stdout) => {
|
|
1263
|
+
if (!stdout)
|
|
1264
|
+
return resolve([]);
|
|
1265
|
+
const lines = stdout.split(`
|
|
1266
|
+
`).map((l) => l.trim()).filter((l) => l.includes("stored"));
|
|
1267
|
+
resolve(lines);
|
|
1268
|
+
});
|
|
1269
|
+
} catch {
|
|
1270
|
+
resolve([]);
|
|
1271
|
+
}
|
|
1272
|
+
});
|
|
1273
|
+
}
|
|
1274
|
+
async function probeOpencodeQuota(runtimeId = "opencode") {
|
|
1275
|
+
const authPath = resolveOpencodeAuthPath();
|
|
1276
|
+
let isLoggedIn = false;
|
|
1277
|
+
let accountEmail;
|
|
1278
|
+
let plan = "OpenCode Community";
|
|
1279
|
+
if (authPath && import_node_fs8.existsSync(authPath)) {
|
|
1280
|
+
try {
|
|
1281
|
+
const data = JSON.parse(import_node_fs8.readFileSync(authPath, "utf8"));
|
|
1282
|
+
if (data && typeof data === "object") {
|
|
1283
|
+
const keys = Object.keys(data);
|
|
1284
|
+
if (keys.length > 0) {
|
|
1285
|
+
isLoggedIn = true;
|
|
1286
|
+
if (data.email)
|
|
1287
|
+
accountEmail = String(data.email);
|
|
1288
|
+
if (data.plan)
|
|
1289
|
+
plan = String(data.plan);
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
} catch {}
|
|
1293
|
+
}
|
|
1294
|
+
const [stats, authList] = await Promise.all([
|
|
1295
|
+
queryOpencodeStats(1200),
|
|
1296
|
+
queryOpencodeAuthList(1200)
|
|
1297
|
+
]);
|
|
1298
|
+
if (authList.length > 0) {
|
|
1299
|
+
isLoggedIn = true;
|
|
1300
|
+
const providers = authList.map((l) => l.split(/\s{2,}/)[0]).filter(Boolean);
|
|
1301
|
+
if (providers.length > 0) {
|
|
1302
|
+
plan = `OpenCode (${providers.join(", ")})`;
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
if (process.env.OPENCODE_API_KEY || process.env.OPENAI_API_KEY) {
|
|
1306
|
+
isLoggedIn = true;
|
|
1307
|
+
}
|
|
1308
|
+
if (!isLoggedIn) {
|
|
1309
|
+
return {
|
|
1310
|
+
runtimeId,
|
|
1311
|
+
provider: "OpenCode",
|
|
1312
|
+
status: "unauthorized",
|
|
1313
|
+
message: "未登录 OpenCode 或未配置 Provider (No stored credentials)",
|
|
1314
|
+
updatedAt: Date.now()
|
|
1315
|
+
};
|
|
1316
|
+
}
|
|
1317
|
+
let credits;
|
|
1318
|
+
if (stats && typeof stats.cost === "number") {
|
|
1319
|
+
credits = {
|
|
1320
|
+
balance: stats.cost > 0 ? `$${stats.cost.toFixed(2)} spent` : "0.00"
|
|
1321
|
+
};
|
|
1322
|
+
}
|
|
1323
|
+
return {
|
|
1324
|
+
runtimeId,
|
|
1325
|
+
provider: "OpenCode",
|
|
1326
|
+
account: {
|
|
1327
|
+
email: accountEmail,
|
|
1328
|
+
plan,
|
|
1329
|
+
type: "provider_oauth"
|
|
1330
|
+
},
|
|
1331
|
+
credits,
|
|
1332
|
+
status: "ok",
|
|
1333
|
+
updatedAt: Date.now()
|
|
1334
|
+
};
|
|
1335
|
+
}
|
|
1336
|
+
|
|
710
1337
|
// src/runtimes/opencode/index.ts
|
|
711
|
-
var
|
|
712
|
-
var
|
|
713
|
-
var
|
|
1338
|
+
var import_node_fs9 = require("node:fs");
|
|
1339
|
+
var import_node_os7 = require("node:os");
|
|
1340
|
+
var import_node_path10 = require("node:path");
|
|
714
1341
|
function pickEnv(env, key) {
|
|
715
1342
|
return env?.[key]?.trim() ?? "";
|
|
716
1343
|
}
|
|
717
1344
|
function resolveOpencodeAuthCandidates(overrides) {
|
|
718
1345
|
const platform = overrides?.platform ?? process.platform;
|
|
719
1346
|
const env = overrides?.env ?? process.env;
|
|
720
|
-
const home = overrides?.home ??
|
|
1347
|
+
const home = overrides?.home ?? import_node_os7.homedir();
|
|
721
1348
|
const seen = new Set;
|
|
722
1349
|
const out = [];
|
|
723
1350
|
const push = (p) => {
|
|
@@ -728,27 +1355,27 @@ function resolveOpencodeAuthCandidates(overrides) {
|
|
|
728
1355
|
};
|
|
729
1356
|
const xdg = pickEnv(env, "XDG_DATA_HOME");
|
|
730
1357
|
if (xdg)
|
|
731
|
-
push(
|
|
1358
|
+
push(import_node_path10.join(xdg, "opencode", "auth.json"));
|
|
732
1359
|
if (platform === "win32") {
|
|
733
|
-
const localAppData = pickEnv(env, "LOCALAPPDATA") || (pickEnv(env, "USERPROFILE") ?
|
|
1360
|
+
const localAppData = pickEnv(env, "LOCALAPPDATA") || (pickEnv(env, "USERPROFILE") ? import_node_path10.join(pickEnv(env, "USERPROFILE"), "AppData", "Local") : "");
|
|
734
1361
|
if (localAppData)
|
|
735
|
-
push(
|
|
1362
|
+
push(import_node_path10.join(localAppData, "opencode", "auth.json"));
|
|
736
1363
|
const userProfile = pickEnv(env, "USERPROFILE") || home;
|
|
737
1364
|
if (userProfile)
|
|
738
|
-
push(
|
|
1365
|
+
push(import_node_path10.join(userProfile, ".local", "share", "opencode", "auth.json"));
|
|
739
1366
|
} else if (home) {
|
|
740
|
-
push(
|
|
1367
|
+
push(import_node_path10.join(home, ".local", "share", "opencode", "auth.json"));
|
|
741
1368
|
}
|
|
742
1369
|
return out;
|
|
743
1370
|
}
|
|
744
1371
|
function resolveOpencodeAuthFile() {
|
|
745
|
-
return resolveOpencodeAuthCandidates()[0] ??
|
|
1372
|
+
return resolveOpencodeAuthCandidates()[0] ?? import_node_path10.join(import_node_os7.homedir(), ".local", "share", "opencode", "auth.json");
|
|
746
1373
|
}
|
|
747
1374
|
function isValidOpencodeAuthFile(authFile) {
|
|
748
1375
|
try {
|
|
749
|
-
if (!authFile || !
|
|
1376
|
+
if (!authFile || !import_node_fs9.existsSync(authFile))
|
|
750
1377
|
return false;
|
|
751
|
-
const raw =
|
|
1378
|
+
const raw = import_node_fs9.readFileSync(authFile, "utf8").trim();
|
|
752
1379
|
if (!raw)
|
|
753
1380
|
return false;
|
|
754
1381
|
const parsed = JSON.parse(raw);
|
|
@@ -766,7 +1393,7 @@ function probeOpencodeAuth(overrides) {
|
|
|
766
1393
|
for (const candidate of candidates) {
|
|
767
1394
|
let exists = false;
|
|
768
1395
|
try {
|
|
769
|
-
exists =
|
|
1396
|
+
exists = import_node_fs9.existsSync(candidate);
|
|
770
1397
|
} catch {
|
|
771
1398
|
exists = false;
|
|
772
1399
|
}
|
|
@@ -779,10 +1406,10 @@ function probeOpencodeAuth(overrides) {
|
|
|
779
1406
|
validFile = candidate;
|
|
780
1407
|
}
|
|
781
1408
|
const authFile = validFile ?? firstExisting ?? candidates[0] ?? resolveOpencodeAuthFile();
|
|
782
|
-
const authDir =
|
|
1409
|
+
const authDir = import_node_path10.dirname(authFile);
|
|
783
1410
|
let hasCodexHome = false;
|
|
784
1411
|
try {
|
|
785
|
-
hasCodexHome =
|
|
1412
|
+
hasCodexHome = import_node_fs9.existsSync(authDir);
|
|
786
1413
|
} catch {
|
|
787
1414
|
hasCodexHome = false;
|
|
788
1415
|
}
|
|
@@ -798,34 +1425,67 @@ function probeOpencodeAuth(overrides) {
|
|
|
798
1425
|
var opencodeAdapter = {
|
|
799
1426
|
id: "opencode",
|
|
800
1427
|
probeAuth: () => probeOpencodeAuth(),
|
|
1428
|
+
probeQuota: async () => probeOpencodeQuota(),
|
|
1429
|
+
orderAuthMethods: (methods) => methods,
|
|
1430
|
+
canSkipInteractiveAuth: () => false,
|
|
1431
|
+
tryDirectSessionFirst: true
|
|
1432
|
+
};
|
|
1433
|
+
|
|
1434
|
+
// src/runtimes/opencode-v2/index.ts
|
|
1435
|
+
var opencodeV2Adapter = {
|
|
1436
|
+
id: "opencode-v2",
|
|
1437
|
+
probeAuth: () => probeOpencodeAuth(),
|
|
801
1438
|
orderAuthMethods: (methods) => methods,
|
|
802
1439
|
canSkipInteractiveAuth: () => false,
|
|
803
1440
|
tryDirectSessionFirst: true
|
|
804
1441
|
};
|
|
805
1442
|
|
|
1443
|
+
// src/runtimes/cursor/quota.ts
|
|
1444
|
+
async function probeCursorQuota() {
|
|
1445
|
+
const auth = probeCursorAuth();
|
|
1446
|
+
if (!auth.looksLoggedIn) {
|
|
1447
|
+
return {
|
|
1448
|
+
runtimeId: "cursor-cli",
|
|
1449
|
+
provider: "Cursor",
|
|
1450
|
+
status: "unauthorized",
|
|
1451
|
+
message: "未检测到 Cursor CLI 登录凭证",
|
|
1452
|
+
updatedAt: Date.now()
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
return {
|
|
1456
|
+
runtimeId: "cursor-cli",
|
|
1457
|
+
provider: "Cursor",
|
|
1458
|
+
account: {
|
|
1459
|
+
plan: "Cursor Pro / Fast Requests"
|
|
1460
|
+
},
|
|
1461
|
+
status: "ok",
|
|
1462
|
+
updatedAt: Date.now()
|
|
1463
|
+
};
|
|
1464
|
+
}
|
|
1465
|
+
|
|
806
1466
|
// src/runtimes/cursor/index.ts
|
|
807
|
-
var
|
|
808
|
-
var
|
|
809
|
-
var
|
|
810
|
-
var
|
|
1467
|
+
var import_node_child_process4 = require("node:child_process");
|
|
1468
|
+
var import_node_fs10 = require("node:fs");
|
|
1469
|
+
var import_node_os8 = require("node:os");
|
|
1470
|
+
var import_node_path11 = require("node:path");
|
|
811
1471
|
function cursorLocalAppData(env) {
|
|
812
1472
|
const local = env.LOCALAPPDATA?.trim();
|
|
813
1473
|
if (local)
|
|
814
1474
|
return local;
|
|
815
1475
|
const profile = env.USERPROFILE?.trim();
|
|
816
|
-
return profile ?
|
|
1476
|
+
return profile ? import_node_path11.join(profile, "AppData", "Local") : "";
|
|
817
1477
|
}
|
|
818
1478
|
function isOnPath(command, env, platform) {
|
|
819
1479
|
const pathValue = env.PATH ?? env.Path ?? "";
|
|
820
1480
|
if (!pathValue.trim())
|
|
821
1481
|
return false;
|
|
822
|
-
const sep = platform === "win32" ? ";" :
|
|
1482
|
+
const sep = platform === "win32" ? ";" : import_node_path11.delimiter;
|
|
823
1483
|
for (const dir of pathValue.split(sep)) {
|
|
824
1484
|
const trimmed = dir.trim().replace(/^"|"$/g, "");
|
|
825
1485
|
if (!trimmed)
|
|
826
1486
|
continue;
|
|
827
1487
|
try {
|
|
828
|
-
if (
|
|
1488
|
+
if (import_node_fs10.existsSync(import_node_path11.join(trimmed, command)))
|
|
829
1489
|
return true;
|
|
830
1490
|
} catch {}
|
|
831
1491
|
}
|
|
@@ -834,24 +1494,24 @@ function isOnPath(command, env, platform) {
|
|
|
834
1494
|
function resolveDirectCursorCommand(platform, env, home) {
|
|
835
1495
|
if (platform === "win32") {
|
|
836
1496
|
const localAppData = cursorLocalAppData(env);
|
|
837
|
-
return localAppData ?
|
|
1497
|
+
return localAppData ? import_node_path11.join(localAppData, "cursor-agent", "agent.cmd") : "";
|
|
838
1498
|
}
|
|
839
|
-
return home ?
|
|
1499
|
+
return home ? import_node_path11.join(home, ".local", "bin", "agent") : "";
|
|
840
1500
|
}
|
|
841
1501
|
function resolveCursorCommand(overrides) {
|
|
842
1502
|
const args = ["acp"];
|
|
843
1503
|
const platform = overrides?.platform ?? process.platform;
|
|
844
1504
|
const env = overrides?.env ?? process.env;
|
|
845
|
-
const home = overrides?.home ??
|
|
1505
|
+
const home = overrides?.home ?? import_node_os8.homedir();
|
|
846
1506
|
if (platform === "win32") {
|
|
847
1507
|
const directCmd = resolveDirectCursorCommand(platform, env, home);
|
|
848
|
-
if (directCmd &&
|
|
1508
|
+
if (directCmd && import_node_fs10.existsSync(directCmd)) {
|
|
849
1509
|
return { command: directCmd, args };
|
|
850
1510
|
}
|
|
851
1511
|
return { command: "agent.cmd", args };
|
|
852
1512
|
}
|
|
853
1513
|
const directBin = resolveDirectCursorCommand(platform, env, home);
|
|
854
|
-
if (directBin &&
|
|
1514
|
+
if (directBin && import_node_fs10.existsSync(directBin)) {
|
|
855
1515
|
return { command: directBin, args };
|
|
856
1516
|
}
|
|
857
1517
|
return { command: "agent", args };
|
|
@@ -860,11 +1520,11 @@ function resolveCursorSpawnCommand(overrides) {
|
|
|
860
1520
|
const args = ["acp"];
|
|
861
1521
|
const platform = overrides?.platform ?? process.platform;
|
|
862
1522
|
const env = overrides?.env ?? process.env;
|
|
863
|
-
const home = overrides?.home ??
|
|
1523
|
+
const home = overrides?.home ?? import_node_os8.homedir();
|
|
864
1524
|
const binary = platform === "win32" ? "agent.cmd" : "agent";
|
|
865
1525
|
const direct = resolveDirectCursorCommand(platform, env, home);
|
|
866
1526
|
try {
|
|
867
|
-
if (direct &&
|
|
1527
|
+
if (direct && import_node_fs10.existsSync(direct))
|
|
868
1528
|
return { command: direct, args };
|
|
869
1529
|
} catch {}
|
|
870
1530
|
if (isOnPath(binary, env, platform))
|
|
@@ -886,12 +1546,12 @@ function cursorRoamingAppData(env, home) {
|
|
|
886
1546
|
if (roaming)
|
|
887
1547
|
return roaming;
|
|
888
1548
|
const profile = pickEnv2(env, "USERPROFILE") || home;
|
|
889
|
-
return profile ?
|
|
1549
|
+
return profile ? import_node_path11.join(profile, "AppData", "Roaming") : "";
|
|
890
1550
|
}
|
|
891
1551
|
function resolveCursorAuthCandidates(overrides) {
|
|
892
1552
|
const platform = overrides?.platform ?? process.platform;
|
|
893
1553
|
const env = overrides?.env ?? process.env;
|
|
894
|
-
const home = overrides?.home ??
|
|
1554
|
+
const home = overrides?.home ?? import_node_os8.homedir();
|
|
895
1555
|
const seen = new Set;
|
|
896
1556
|
const out = [];
|
|
897
1557
|
const push = (p) => {
|
|
@@ -902,16 +1562,16 @@ function resolveCursorAuthCandidates(overrides) {
|
|
|
902
1562
|
};
|
|
903
1563
|
const xdg = pickEnv2(env, "XDG_CONFIG_HOME");
|
|
904
1564
|
if (xdg)
|
|
905
|
-
push(
|
|
1565
|
+
push(import_node_path11.join(xdg, "cursor", "auth.json"));
|
|
906
1566
|
if (platform === "win32") {
|
|
907
1567
|
const profile = pickEnv2(env, "USERPROFILE") || home;
|
|
908
1568
|
if (profile)
|
|
909
|
-
push(
|
|
1569
|
+
push(import_node_path11.join(profile, ".config", "cursor", "auth.json"));
|
|
910
1570
|
const roaming = cursorRoamingAppData(env, home);
|
|
911
1571
|
if (roaming)
|
|
912
|
-
push(
|
|
1572
|
+
push(import_node_path11.join(roaming, "Cursor", "User", "globalStorage", "storage.json"));
|
|
913
1573
|
} else if (home) {
|
|
914
|
-
push(
|
|
1574
|
+
push(import_node_path11.join(home, ".config", "cursor", "auth.json"));
|
|
915
1575
|
}
|
|
916
1576
|
return out;
|
|
917
1577
|
}
|
|
@@ -922,9 +1582,9 @@ function isValidCursorAuthFile(authFile) {
|
|
|
922
1582
|
try {
|
|
923
1583
|
if (!authFile || isStorageHintPath(authFile))
|
|
924
1584
|
return false;
|
|
925
|
-
if (!
|
|
1585
|
+
if (!import_node_fs10.existsSync(authFile))
|
|
926
1586
|
return false;
|
|
927
|
-
const raw =
|
|
1587
|
+
const raw = import_node_fs10.readFileSync(authFile, "utf8").trim();
|
|
928
1588
|
if (!raw)
|
|
929
1589
|
return false;
|
|
930
1590
|
const parsed = JSON.parse(raw);
|
|
@@ -942,9 +1602,9 @@ function isHintCursorStorageFile(authFile) {
|
|
|
942
1602
|
try {
|
|
943
1603
|
if (!authFile || !isStorageHintPath(authFile))
|
|
944
1604
|
return false;
|
|
945
|
-
if (!
|
|
1605
|
+
if (!import_node_fs10.existsSync(authFile))
|
|
946
1606
|
return false;
|
|
947
|
-
const raw =
|
|
1607
|
+
const raw = import_node_fs10.readFileSync(authFile, "utf8").trim();
|
|
948
1608
|
if (!raw)
|
|
949
1609
|
return false;
|
|
950
1610
|
const parsed = JSON.parse(raw);
|
|
@@ -960,7 +1620,7 @@ function isValidCursorCandidate(authFile) {
|
|
|
960
1620
|
}
|
|
961
1621
|
function probeCursorAuth(overrides) {
|
|
962
1622
|
const env = overrides?.env ?? process.env;
|
|
963
|
-
const home = overrides?.home ??
|
|
1623
|
+
const home = overrides?.home ?? import_node_os8.homedir();
|
|
964
1624
|
const hasApiKeyEnv = Boolean(env.CURSOR_API_KEY?.trim() || env.CURSOR_AUTH_TOKEN?.trim());
|
|
965
1625
|
const candidates = resolveCursorAuthCandidates(overrides);
|
|
966
1626
|
let firstExisting = null;
|
|
@@ -968,7 +1628,7 @@ function probeCursorAuth(overrides) {
|
|
|
968
1628
|
for (const candidate of candidates) {
|
|
969
1629
|
let exists = false;
|
|
970
1630
|
try {
|
|
971
|
-
exists =
|
|
1631
|
+
exists = import_node_fs10.existsSync(candidate);
|
|
972
1632
|
} catch {
|
|
973
1633
|
exists = false;
|
|
974
1634
|
}
|
|
@@ -980,12 +1640,12 @@ function probeCursorAuth(overrides) {
|
|
|
980
1640
|
if (isValidCursorCandidate(candidate))
|
|
981
1641
|
validFile = candidate;
|
|
982
1642
|
}
|
|
983
|
-
const fallback = home ?
|
|
1643
|
+
const fallback = home ? import_node_path11.join(home, ".config", "cursor", "auth.json") : "";
|
|
984
1644
|
const authFile = validFile ?? firstExisting ?? candidates[0] ?? fallback;
|
|
985
|
-
const authDir = authFile ?
|
|
1645
|
+
const authDir = authFile ? import_node_path11.dirname(authFile) : "";
|
|
986
1646
|
let hasCodexHome = false;
|
|
987
1647
|
try {
|
|
988
|
-
hasCodexHome = authDir ?
|
|
1648
|
+
hasCodexHome = authDir ? import_node_fs10.existsSync(authDir) : false;
|
|
989
1649
|
} catch {
|
|
990
1650
|
hasCodexHome = false;
|
|
991
1651
|
}
|
|
@@ -1000,6 +1660,7 @@ function probeCursorAuth(overrides) {
|
|
|
1000
1660
|
var cursorAdapter = {
|
|
1001
1661
|
id: "cursor-cli",
|
|
1002
1662
|
probeAuth: () => probeCursorAuth(),
|
|
1663
|
+
probeQuota: async () => probeCursorQuota(),
|
|
1003
1664
|
resolveSpawnCommand: () => resolveCursorSpawnCommand(),
|
|
1004
1665
|
orderAuthMethods: (methods) => methods,
|
|
1005
1666
|
canSkipInteractiveAuth: () => false,
|
|
@@ -1048,7 +1709,7 @@ function getCursorCatalogIds(execFile, overrides) {
|
|
|
1048
1709
|
}
|
|
1049
1710
|
try {
|
|
1050
1711
|
const shell = command.toLowerCase().endsWith(".cmd");
|
|
1051
|
-
const result =
|
|
1712
|
+
const result = import_node_child_process4.spawnSync(command, ["--list-models"], {
|
|
1052
1713
|
encoding: "utf8",
|
|
1053
1714
|
timeout: 6000,
|
|
1054
1715
|
windowsHide: true,
|
|
@@ -1118,6 +1779,55 @@ var deepseekAdapter = {
|
|
|
1118
1779
|
canSkipInteractiveAuth: () => false
|
|
1119
1780
|
};
|
|
1120
1781
|
|
|
1782
|
+
// src/runtimes/agy/quota.ts
|
|
1783
|
+
var import_node_fs11 = require("node:fs");
|
|
1784
|
+
var import_node_path12 = require("node:path");
|
|
1785
|
+
function resolveGeminiHome2() {
|
|
1786
|
+
const home = process.env.USERPROFILE || process.env.HOME || "";
|
|
1787
|
+
return import_node_path12.join(home, ".gemini");
|
|
1788
|
+
}
|
|
1789
|
+
async function probeAgyQuota(options) {
|
|
1790
|
+
const geminiHome = resolveGeminiHome2();
|
|
1791
|
+
const hasAgyDir = import_node_fs11.existsSync(import_node_path12.join(geminiHome, "antigravity")) || import_node_fs11.existsSync(import_node_path12.join(geminiHome, "antigravity-cli"));
|
|
1792
|
+
const hasApiKey = Boolean(process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY);
|
|
1793
|
+
const isAvailable = hasAgyDir || hasApiKey;
|
|
1794
|
+
if (!isAvailable) {
|
|
1795
|
+
return {
|
|
1796
|
+
runtimeId: "agy",
|
|
1797
|
+
provider: "Google Antigravity",
|
|
1798
|
+
status: "unauthorized",
|
|
1799
|
+
message: "wenjianlingu antigravity cli",
|
|
1800
|
+
updatedAt: Date.now()
|
|
1801
|
+
};
|
|
1802
|
+
}
|
|
1803
|
+
return {
|
|
1804
|
+
runtimeId: "agy",
|
|
1805
|
+
provider: "Google Antigravity",
|
|
1806
|
+
account: {
|
|
1807
|
+
plan: "Antigravity / Gemini",
|
|
1808
|
+
type: "gemini"
|
|
1809
|
+
},
|
|
1810
|
+
primaryWindow: {
|
|
1811
|
+
label: "5-Hour limit",
|
|
1812
|
+
percentLeft: 100,
|
|
1813
|
+
usedPercent: 0,
|
|
1814
|
+
windowDurationMins: 300
|
|
1815
|
+
},
|
|
1816
|
+
secondaryWindow: {
|
|
1817
|
+
label: "Weekly limit",
|
|
1818
|
+
percentLeft: 100,
|
|
1819
|
+
usedPercent: 0,
|
|
1820
|
+
windowDurationMins: 10080
|
|
1821
|
+
},
|
|
1822
|
+
credits: {
|
|
1823
|
+
unlimited: true,
|
|
1824
|
+
balance: "unlimited"
|
|
1825
|
+
},
|
|
1826
|
+
status: "ok",
|
|
1827
|
+
updatedAt: Date.now()
|
|
1828
|
+
};
|
|
1829
|
+
}
|
|
1830
|
+
|
|
1121
1831
|
// src/runtimes/agy/index.ts
|
|
1122
1832
|
function probeAgyAuth() {
|
|
1123
1833
|
return {
|
|
@@ -1131,6 +1841,7 @@ function probeAgyAuth() {
|
|
|
1131
1841
|
var agyAdapter = {
|
|
1132
1842
|
id: "agy",
|
|
1133
1843
|
probeAuth: () => probeAgyAuth(),
|
|
1844
|
+
probeQuota: (options) => probeAgyQuota(options),
|
|
1134
1845
|
orderAuthMethods: (methods) => methods,
|
|
1135
1846
|
canSkipInteractiveAuth: () => false,
|
|
1136
1847
|
tryDirectSessionFirst: true
|
|
@@ -1150,6 +1861,8 @@ function getAcpRuntimeAdapter(runtimeId) {
|
|
|
1150
1861
|
return copilotAdapter;
|
|
1151
1862
|
case opencodeAdapter.id:
|
|
1152
1863
|
return opencodeAdapter;
|
|
1864
|
+
case opencodeV2Adapter.id:
|
|
1865
|
+
return opencodeV2Adapter;
|
|
1153
1866
|
case cursorAdapter.id:
|
|
1154
1867
|
case "cursor":
|
|
1155
1868
|
return cursorAdapter;
|
|
@@ -1166,7 +1879,7 @@ function getAcpRuntimeAdapter(runtimeId) {
|
|
|
1166
1879
|
}
|
|
1167
1880
|
|
|
1168
1881
|
// src/process/process-manager.ts
|
|
1169
|
-
var
|
|
1882
|
+
var import_node_child_process5 = require("node:child_process");
|
|
1170
1883
|
var active = new Map;
|
|
1171
1884
|
var ACP_EARLY_EXIT_WINDOW_MS = 3000;
|
|
1172
1885
|
var ACP_EARLY_EXIT_STDERR_TAIL_LINES = 20;
|
|
@@ -1178,7 +1891,7 @@ function killProcessTree(pid) {
|
|
|
1178
1891
|
if (process.platform !== "win32")
|
|
1179
1892
|
return;
|
|
1180
1893
|
try {
|
|
1181
|
-
|
|
1894
|
+
import_node_child_process5.spawnSync("taskkill", ["/F", "/T", "/PID", String(pid)], {
|
|
1182
1895
|
windowsHide: true,
|
|
1183
1896
|
timeout: 8000
|
|
1184
1897
|
});
|
|
@@ -1221,7 +1934,7 @@ function spawnAcpProcess(options) {
|
|
|
1221
1934
|
} else {
|
|
1222
1935
|
delete env.NO_BROWSER;
|
|
1223
1936
|
}
|
|
1224
|
-
const child =
|
|
1937
|
+
const child = import_node_child_process5.spawn(file, options.runtime.args, {
|
|
1225
1938
|
cwd: options.cwd,
|
|
1226
1939
|
env,
|
|
1227
1940
|
stdio: ["pipe", "pipe", "pipe"],
|
|
@@ -1407,17 +2120,17 @@ var import_sdk2 = require("@agentclientprotocol/sdk");
|
|
|
1407
2120
|
|
|
1408
2121
|
// src/client/acp-fs.ts
|
|
1409
2122
|
var import_promises3 = require("node:fs/promises");
|
|
1410
|
-
var
|
|
2123
|
+
var import_node_path13 = require("node:path");
|
|
1411
2124
|
var import_promises4 = require("node:fs/promises");
|
|
1412
2125
|
var MAX_READ_BYTES = 2 * 1024 * 1024;
|
|
1413
2126
|
var BINARY_DOCUMENT_EXTENSIONS = new Set([".epub", ".mobi", ".azw", ".azw3", ".pdf"]);
|
|
1414
2127
|
function isBinaryDocumentPath(filePath) {
|
|
1415
|
-
return BINARY_DOCUMENT_EXTENSIONS.has(
|
|
2128
|
+
return BINARY_DOCUMENT_EXTENSIONS.has(import_node_path13.extname(filePath).toLowerCase());
|
|
1416
2129
|
}
|
|
1417
2130
|
function assertPathInsideWorkspace(filePath, workspaceRoot) {
|
|
1418
|
-
const root =
|
|
1419
|
-
const target =
|
|
1420
|
-
const rootWithSep = root.endsWith(
|
|
2131
|
+
const root = import_node_path13.resolve(workspaceRoot);
|
|
2132
|
+
const target = import_node_path13.resolve(filePath);
|
|
2133
|
+
const rootWithSep = root.endsWith(import_node_path13.sep) ? root : root + import_node_path13.sep;
|
|
1421
2134
|
if (target !== root && !target.startsWith(rootWithSep)) {
|
|
1422
2135
|
throw new Error(`路径不在工作区内: ${filePath}`);
|
|
1423
2136
|
}
|
|
@@ -1448,7 +2161,7 @@ async function acpReadTextFile(params) {
|
|
|
1448
2161
|
}
|
|
1449
2162
|
async function acpWriteTextFile(params) {
|
|
1450
2163
|
const abs = assertPathInsideWorkspace(params.path, params.workspaceRoot);
|
|
1451
|
-
await import_promises4.mkdir(
|
|
2164
|
+
await import_promises4.mkdir(import_node_path13.dirname(abs), { recursive: true });
|
|
1452
2165
|
await import_promises3.writeFile(abs, params.content, "utf-8");
|
|
1453
2166
|
return {};
|
|
1454
2167
|
}
|
|
@@ -1941,7 +2654,7 @@ async function restoreOrCreateAcpSession(input) {
|
|
|
1941
2654
|
}
|
|
1942
2655
|
|
|
1943
2656
|
// src/client/acp-terminal.ts
|
|
1944
|
-
var
|
|
2657
|
+
var import_node_child_process6 = require("node:child_process");
|
|
1945
2658
|
var import_node_crypto = require("node:crypto");
|
|
1946
2659
|
|
|
1947
2660
|
// src/transport/terminal-output-buffer.ts
|
|
@@ -1994,7 +2707,7 @@ class AcpTerminalManager {
|
|
|
1994
2707
|
throw new Error("terminal/create 需要 command");
|
|
1995
2708
|
const cwd = resolveCwd(params.cwd, params.workspaceRoot);
|
|
1996
2709
|
const outputByteLimit = typeof params.outputByteLimit === "number" && params.outputByteLimit > 0 ? params.outputByteLimit : DEFAULT_OUTPUT_BYTE_LIMIT;
|
|
1997
|
-
const child =
|
|
2710
|
+
const child = import_node_child_process6.spawn(command, params.args ?? [], {
|
|
1998
2711
|
cwd,
|
|
1999
2712
|
env: buildEnv(params.env),
|
|
2000
2713
|
windowsHide: true,
|
|
@@ -2090,7 +2803,7 @@ class AcpTerminalManager {
|
|
|
2090
2803
|
return;
|
|
2091
2804
|
if (process.platform === "win32") {
|
|
2092
2805
|
try {
|
|
2093
|
-
|
|
2806
|
+
import_node_child_process6.spawn("taskkill", ["/pid", String(pid), "/T", "/F"], {
|
|
2094
2807
|
windowsHide: true,
|
|
2095
2808
|
stdio: "ignore"
|
|
2096
2809
|
});
|
|
@@ -2126,8 +2839,8 @@ class AcpTerminalManager {
|
|
|
2126
2839
|
|
|
2127
2840
|
// src/client/acp-proxy-service.ts
|
|
2128
2841
|
var import_promises5 = require("node:fs/promises");
|
|
2129
|
-
var
|
|
2130
|
-
var
|
|
2842
|
+
var import_node_fs12 = require("node:fs");
|
|
2843
|
+
var import_node_path14 = require("node:path");
|
|
2131
2844
|
var DEFAULT_ACP_PROXY_SETTINGS = {
|
|
2132
2845
|
enabled: false,
|
|
2133
2846
|
host: "127.0.0.1",
|
|
@@ -2136,7 +2849,7 @@ var DEFAULT_ACP_PROXY_SETTINGS = {
|
|
|
2136
2849
|
var PROXY_ENV_KEYS = ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY"];
|
|
2137
2850
|
var LOWERCASE_PROXY_ENV_KEYS = ["http_proxy", "https_proxy", "all_proxy", "no_proxy"];
|
|
2138
2851
|
function proxyFilePath(dir) {
|
|
2139
|
-
return
|
|
2852
|
+
return import_node_path14.join(dir, "agent", "proxy.json");
|
|
2140
2853
|
}
|
|
2141
2854
|
function validate2(settings) {
|
|
2142
2855
|
if (!settings.host.trim())
|
|
@@ -2148,7 +2861,7 @@ function validate2(settings) {
|
|
|
2148
2861
|
}
|
|
2149
2862
|
async function readAcpProxySettings(dir = getDefaultAgentDataDir()) {
|
|
2150
2863
|
const file = proxyFilePath(dir);
|
|
2151
|
-
if (!
|
|
2864
|
+
if (!import_node_fs12.existsSync(file))
|
|
2152
2865
|
return { ...DEFAULT_ACP_PROXY_SETTINGS };
|
|
2153
2866
|
try {
|
|
2154
2867
|
const raw = JSON.parse(await import_promises5.readFile(file, "utf8"));
|
|
@@ -2170,7 +2883,7 @@ async function saveAcpProxySettings(payload, dir = getDefaultAgentDataDir()) {
|
|
|
2170
2883
|
host: payload.host.trim() || DEFAULT_ACP_PROXY_SETTINGS.host,
|
|
2171
2884
|
port: payload.port
|
|
2172
2885
|
};
|
|
2173
|
-
await import_promises5.mkdir(
|
|
2886
|
+
await import_promises5.mkdir(import_node_path14.join(dir, "agent"), { recursive: true });
|
|
2174
2887
|
await import_promises5.writeFile(proxyFilePath(dir), JSON.stringify(stored, null, 2), "utf8");
|
|
2175
2888
|
return ok(stored);
|
|
2176
2889
|
}
|
|
@@ -2238,6 +2951,12 @@ class AcpSessionHandle {
|
|
|
2238
2951
|
async prompt(promptText, options) {
|
|
2239
2952
|
return this.client.promptSession(this.sessionId, promptText, options);
|
|
2240
2953
|
}
|
|
2954
|
+
get quota() {
|
|
2955
|
+
return this.client.getCachedQuota(this.sessionId);
|
|
2956
|
+
}
|
|
2957
|
+
async getQuota() {
|
|
2958
|
+
return this.client.getQuota(this.sessionId);
|
|
2959
|
+
}
|
|
2241
2960
|
get availableCommands() {
|
|
2242
2961
|
return this.client.getAvailableCommands(this.sessionId);
|
|
2243
2962
|
}
|
|
@@ -2249,6 +2968,7 @@ class AcpSessionHandle {
|
|
|
2249
2968
|
class AcpClient {
|
|
2250
2969
|
status = "disconnected";
|
|
2251
2970
|
commandsCache = new AvailableCommandsCache;
|
|
2971
|
+
quotaCache = new QuotaCache;
|
|
2252
2972
|
runtimeInfo;
|
|
2253
2973
|
adapter;
|
|
2254
2974
|
workspaceRoot;
|
|
@@ -2359,6 +3079,7 @@ class AcpClient {
|
|
|
2359
3079
|
},
|
|
2360
3080
|
onSessionUpdate: (params) => {
|
|
2361
3081
|
this.commandsCache.observe(params);
|
|
3082
|
+
this.quotaCache.observe(params, this.runtimeInfo.id);
|
|
2362
3083
|
this.callbacks.onSessionUpdate?.(params);
|
|
2363
3084
|
}
|
|
2364
3085
|
};
|
|
@@ -2389,6 +3110,23 @@ class AcpClient {
|
|
|
2389
3110
|
return err({ code: "ACP_PROTOCOL_ERROR", message });
|
|
2390
3111
|
}
|
|
2391
3112
|
}
|
|
3113
|
+
getCachedQuota(sessionId) {
|
|
3114
|
+
return this.quotaCache.get(sessionId, this.runtimeInfo.id);
|
|
3115
|
+
}
|
|
3116
|
+
async getQuota(sessionId) {
|
|
3117
|
+
const cached = this.getCachedQuota(sessionId);
|
|
3118
|
+
if (cached && Date.now() - cached.updatedAt < 30000) {
|
|
3119
|
+
return cached;
|
|
3120
|
+
}
|
|
3121
|
+
if (this.adapter.probeQuota) {
|
|
3122
|
+
const probed = await this.adapter.probeQuota({ sessionId });
|
|
3123
|
+
if (probed) {
|
|
3124
|
+
this.quotaCache.set(probed, sessionId);
|
|
3125
|
+
return probed;
|
|
3126
|
+
}
|
|
3127
|
+
}
|
|
3128
|
+
return cached ?? null;
|
|
3129
|
+
}
|
|
2392
3130
|
getAvailableCommands(sessionId) {
|
|
2393
3131
|
return this.commandsCache.get(sessionId);
|
|
2394
3132
|
}
|