@yitom/acp-client 0.1.3 → 0.1.5
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 +593 -115
- package/dist/index.d.ts +2 -0
- package/dist/index.js +584 -106
- 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 +592 -111
- package/dist/version.d.ts +1 -0
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -65,6 +65,7 @@ var CODEX_ACP_NPM_PACKAGE = "@agentclientprotocol/codex-acp";
|
|
|
65
65
|
var CLAUDE_ACP_NPM_PACKAGE = "@agentclientprotocol/claude-agent-acp";
|
|
66
66
|
var DEEPSEEK_DSH_NPM_PACKAGE = "@deepseek-ai/dsh";
|
|
67
67
|
var AGY_ACP_NPM_PACKAGE = "@yitom/agy-acp-map";
|
|
68
|
+
var OPENCODE_CLI_NPM_PACKAGE = "@opencode/cli";
|
|
68
69
|
var LEGACY_ZED_CODEX_ACP_NPM_PACKAGE = "@zed-industries/codex-acp";
|
|
69
70
|
var DEFAULT_ACP_RUNTIME_ID = "codex-acp";
|
|
70
71
|
function isWindowsPlatform() {
|
|
@@ -117,6 +118,15 @@ var BUILTIN_ACP_RUNTIMES = [
|
|
|
117
118
|
args: ["acp"],
|
|
118
119
|
requiredEnvKeys: []
|
|
119
120
|
},
|
|
121
|
+
{
|
|
122
|
+
id: "opencode-v2",
|
|
123
|
+
name: "OpenCode v2",
|
|
124
|
+
description: "通过 npx 启动独立产品线 @opencode/cli v2(acp);与 1.x 共存,复用 v2 SQLite 登录态",
|
|
125
|
+
authHint: "v2 登录态在 SQLite(auth.json 仅兼容残留);未登录先用 v2 二进制跑 opencode auth login",
|
|
126
|
+
command: "npx",
|
|
127
|
+
args: ["-y", OPENCODE_CLI_NPM_PACKAGE, "acp"],
|
|
128
|
+
requiredEnvKeys: []
|
|
129
|
+
},
|
|
120
130
|
{
|
|
121
131
|
id: "cursor-cli",
|
|
122
132
|
name: "Cursor",
|
|
@@ -148,6 +158,8 @@ var BUILTIN_ACP_RUNTIMES = [
|
|
|
148
158
|
function findBuiltinAcpRuntime(id) {
|
|
149
159
|
return BUILTIN_ACP_RUNTIMES.find((item) => item.id === id);
|
|
150
160
|
}
|
|
161
|
+
// src/version.ts
|
|
162
|
+
var ACP_CLIENT_VERSION = "0.1.5";
|
|
151
163
|
// src/session/available-commands.ts
|
|
152
164
|
function normalizeCommand(raw) {
|
|
153
165
|
if (!raw || typeof raw !== "object")
|
|
@@ -236,43 +248,169 @@ class AvailableCommandsCache {
|
|
|
236
248
|
}
|
|
237
249
|
}
|
|
238
250
|
|
|
251
|
+
// src/session/quota.ts
|
|
252
|
+
function parseRateLimitsFromStatusText(text, runtimeId = "unknown") {
|
|
253
|
+
if (!text || typeof text !== "string")
|
|
254
|
+
return null;
|
|
255
|
+
const lines = text.split(/\r?\n/);
|
|
256
|
+
let account;
|
|
257
|
+
const windows = [];
|
|
258
|
+
let credits;
|
|
259
|
+
let status = "ok";
|
|
260
|
+
let message;
|
|
261
|
+
if (/account (?:disabled|banned|suspended)|suspended|banned/i.test(text)) {
|
|
262
|
+
status = "banned";
|
|
263
|
+
message = "账号已封禁或暂停使用 (Account banned / suspended)";
|
|
264
|
+
} else if (/RESOURCE_EXHAUSTED|quota reached|rate limit exceeded|429/i.test(text)) {
|
|
265
|
+
status = "exhausted";
|
|
266
|
+
message = "额度已用尽 (Quota / Rate limit reached)";
|
|
267
|
+
} else if (/unauthorized|authentication failed|invalid api key/i.test(text)) {
|
|
268
|
+
status = "unauthorized";
|
|
269
|
+
message = "认证失效 (Unauthorized / Invalid credentials)";
|
|
270
|
+
}
|
|
271
|
+
for (const rawLine of lines) {
|
|
272
|
+
const line = rawLine.replace(/\*\*/g, "").trim();
|
|
273
|
+
if (!line)
|
|
274
|
+
continue;
|
|
275
|
+
const accMatch = line.match(/^Account\s*[::]\s*([^()]+?)(?:\s*\(([^)]+)\))?$/i);
|
|
276
|
+
if (accMatch) {
|
|
277
|
+
account = {
|
|
278
|
+
plan: accMatch[1].trim(),
|
|
279
|
+
email: accMatch[2]?.trim()
|
|
280
|
+
};
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const credMatch = line.match(/(?:\b|^)(?:[\w\s-]*?credits?)\s*[::]\s*(unlimited|[\d.]+)/i);
|
|
284
|
+
if (credMatch) {
|
|
285
|
+
const val = credMatch[1].trim();
|
|
286
|
+
credits = val.toLowerCase() === "unlimited" ? { unlimited: true, balance: "unlimited" } : { balance: Number(val) || val, unlimited: false };
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
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);
|
|
290
|
+
if (winMatch) {
|
|
291
|
+
const label = winMatch[1].trim();
|
|
292
|
+
const pct = parseFloat(winMatch[2]);
|
|
293
|
+
const resetsAt = (winMatch[3] || winMatch[4] || "").trim() || undefined;
|
|
294
|
+
let windowDurationMins;
|
|
295
|
+
const lower = label.toLowerCase();
|
|
296
|
+
if (/5h|5-hour/i.test(lower))
|
|
297
|
+
windowDurationMins = 300;
|
|
298
|
+
else if (/week/i.test(lower))
|
|
299
|
+
windowDurationMins = 10080;
|
|
300
|
+
else if (/month/i.test(lower))
|
|
301
|
+
windowDurationMins = 43200;
|
|
302
|
+
else if (/day|24h/i.test(lower))
|
|
303
|
+
windowDurationMins = 1440;
|
|
304
|
+
else if (/(\d+)\s*h/i.test(lower)) {
|
|
305
|
+
const m = lower.match(/(\d+)\s*h/);
|
|
306
|
+
if (m)
|
|
307
|
+
windowDurationMins = parseInt(m[1], 10) * 60;
|
|
308
|
+
}
|
|
309
|
+
windows.push({
|
|
310
|
+
label,
|
|
311
|
+
percentLeft: pct,
|
|
312
|
+
usedPercent: Math.max(0, 100 - pct),
|
|
313
|
+
resetsAt,
|
|
314
|
+
windowDurationMins
|
|
315
|
+
});
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
const fastMatch = line.match(/(?:fast requests?|requests?)\s*[::]\s*(\d+)\s*\/\s*(\d+)/i);
|
|
319
|
+
if (fastMatch) {
|
|
320
|
+
const used = parseInt(fastMatch[1], 10);
|
|
321
|
+
const total = parseInt(fastMatch[2], 10);
|
|
322
|
+
const left = Math.max(0, total - used);
|
|
323
|
+
const pct = total > 0 ? Math.round(left / total * 100) : 0;
|
|
324
|
+
windows.push({
|
|
325
|
+
label: "Fast requests",
|
|
326
|
+
percentLeft: pct,
|
|
327
|
+
usedPercent: total > 0 ? Math.round(used / total * 100) : 0,
|
|
328
|
+
used,
|
|
329
|
+
total
|
|
330
|
+
});
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (!windows.length && !credits && !account && status === "ok") {
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
const primary = windows.find((w) => /5h|5-hour|hour|primary|fast/i.test(w.label)) || windows[0];
|
|
338
|
+
const secondary = windows.find((w) => w !== primary && /week|month|secondary/i.test(w.label)) || windows.find((w) => w !== primary);
|
|
339
|
+
const extraWindows = windows.filter((w) => w !== primary && w !== secondary);
|
|
340
|
+
if (primary && primary.percentLeft <= 10 && status === "ok") {
|
|
341
|
+
status = "warning";
|
|
342
|
+
}
|
|
343
|
+
return {
|
|
344
|
+
runtimeId,
|
|
345
|
+
account,
|
|
346
|
+
primaryWindow: primary,
|
|
347
|
+
secondaryWindow: secondary,
|
|
348
|
+
extraWindows: extraWindows.length ? extraWindows : undefined,
|
|
349
|
+
credits,
|
|
350
|
+
status,
|
|
351
|
+
message,
|
|
352
|
+
rawText: text,
|
|
353
|
+
updatedAt: Date.now()
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
class QuotaCache {
|
|
358
|
+
bySession = new Map;
|
|
359
|
+
byRuntime = new Map;
|
|
360
|
+
observe(params, defaultRuntimeId = "unknown") {
|
|
361
|
+
if (!params || typeof params !== "object")
|
|
362
|
+
return null;
|
|
363
|
+
const p = params;
|
|
364
|
+
const sessionId = typeof p.sessionId === "string" ? p.sessionId : undefined;
|
|
365
|
+
let textToParse = "";
|
|
366
|
+
if (p.update?.content && p.update.content.type === "text" && typeof p.update.content.text === "string") {
|
|
367
|
+
textToParse = p.update.content.text;
|
|
368
|
+
} else if (p.update?.message?.content && Array.isArray(p.update.message.content)) {
|
|
369
|
+
textToParse = p.update.message.content.filter((c) => c && c.type === "text" && typeof c.text === "string").map((c) => c.text).join(`
|
|
370
|
+
`);
|
|
371
|
+
}
|
|
372
|
+
if (!textToParse)
|
|
373
|
+
return null;
|
|
374
|
+
if (!/(?:limit|credits?|Account:|5h|resets|banned|suspended|quota)/i.test(textToParse)) {
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
const quota = parseRateLimitsFromStatusText(textToParse, defaultRuntimeId);
|
|
378
|
+
if (quota) {
|
|
379
|
+
if (sessionId)
|
|
380
|
+
this.bySession.set(sessionId, quota);
|
|
381
|
+
this.byRuntime.set(defaultRuntimeId, quota);
|
|
382
|
+
return quota;
|
|
383
|
+
}
|
|
384
|
+
return null;
|
|
385
|
+
}
|
|
386
|
+
set(quota, sessionId) {
|
|
387
|
+
if (sessionId)
|
|
388
|
+
this.bySession.set(sessionId, quota);
|
|
389
|
+
this.byRuntime.set(quota.runtimeId, quota);
|
|
390
|
+
}
|
|
391
|
+
get(sessionId, runtimeId) {
|
|
392
|
+
if (sessionId && this.bySession.has(sessionId)) {
|
|
393
|
+
return this.bySession.get(sessionId);
|
|
394
|
+
}
|
|
395
|
+
if (runtimeId && this.byRuntime.has(runtimeId)) {
|
|
396
|
+
return this.byRuntime.get(runtimeId);
|
|
397
|
+
}
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
clear() {
|
|
401
|
+
this.bySession.clear();
|
|
402
|
+
this.byRuntime.clear();
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
239
406
|
// src/client/acp-client.ts
|
|
240
407
|
import {
|
|
241
408
|
methods as methods2
|
|
242
409
|
} from "@agentclientprotocol/sdk";
|
|
243
410
|
|
|
244
|
-
// src/runtimes/codex/
|
|
245
|
-
import {
|
|
246
|
-
import { join as
|
|
247
|
-
|
|
248
|
-
// src/runtimes/codex/codex-provider-home.ts
|
|
249
|
-
var INKDOWN_PROVIDER_ID = "inkdown-custom";
|
|
250
|
-
var INKDOWN_PROVIDER_API_KEY_ENV = "INKDOWN_PROVIDER_API_KEY";
|
|
251
|
-
function tomlEscape(value) {
|
|
252
|
-
return value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
|
|
253
|
-
}
|
|
254
|
-
function buildCustomProviderConfigToml(config) {
|
|
255
|
-
const name = config.name?.trim() || "自定义 API";
|
|
256
|
-
const wireApi = config.wireApi === "responses" ? "responses" : "chat";
|
|
257
|
-
return [
|
|
258
|
-
`model_provider = "${INKDOWN_PROVIDER_ID}"`,
|
|
259
|
-
`model = "${tomlEscape(config.model.trim())}"`,
|
|
260
|
-
"",
|
|
261
|
-
`[model_providers.${INKDOWN_PROVIDER_ID}]`,
|
|
262
|
-
`name = "${tomlEscape(name)}"`,
|
|
263
|
-
`base_url = "${tomlEscape(config.baseUrl.trim())}"`,
|
|
264
|
-
`env_key = "${INKDOWN_PROVIDER_API_KEY_ENV}"`,
|
|
265
|
-
`wire_api = "${wireApi}"`,
|
|
266
|
-
""
|
|
267
|
-
].join(`
|
|
268
|
-
`);
|
|
269
|
-
}
|
|
270
|
-
function buildCustomProviderSpawnEnv(config, apiKey, codexHome) {
|
|
271
|
-
return {
|
|
272
|
-
CODEX_HOME: codexHome,
|
|
273
|
-
[INKDOWN_PROVIDER_API_KEY_ENV]: apiKey
|
|
274
|
-
};
|
|
275
|
-
}
|
|
411
|
+
// src/runtimes/codex/quota.ts
|
|
412
|
+
import { existsSync as existsSync2, readFileSync } from "node:fs";
|
|
413
|
+
import { join as join2 } from "node:path";
|
|
276
414
|
|
|
277
415
|
// src/runtimes/codex/codex-auth-preflight.ts
|
|
278
416
|
import { existsSync } from "node:fs";
|
|
@@ -310,14 +448,114 @@ function probeCodexAuth() {
|
|
|
310
448
|
};
|
|
311
449
|
}
|
|
312
450
|
|
|
451
|
+
// src/runtimes/codex/quota.ts
|
|
452
|
+
function decodeJwtPayload(token) {
|
|
453
|
+
try {
|
|
454
|
+
const parts = token.split(".");
|
|
455
|
+
if (parts.length < 2)
|
|
456
|
+
return null;
|
|
457
|
+
const json = Buffer.from(parts[1], "base64").toString("utf8");
|
|
458
|
+
return JSON.parse(json);
|
|
459
|
+
} catch {
|
|
460
|
+
return null;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
async function probeCodexQuota(options) {
|
|
464
|
+
const codexHome = resolveCodexHome();
|
|
465
|
+
const authPath = join2(codexHome, "auth.json");
|
|
466
|
+
let accountEmail;
|
|
467
|
+
let accountPlan = "Codex Free / Local";
|
|
468
|
+
let isLoggedIn = false;
|
|
469
|
+
if (existsSync2(authPath)) {
|
|
470
|
+
try {
|
|
471
|
+
const authData = JSON.parse(readFileSync(authPath, "utf8"));
|
|
472
|
+
if (authData.OPENAI_API_KEY) {
|
|
473
|
+
isLoggedIn = true;
|
|
474
|
+
accountPlan = "OpenAI API Key";
|
|
475
|
+
}
|
|
476
|
+
if (authData.tokens?.id_token) {
|
|
477
|
+
isLoggedIn = true;
|
|
478
|
+
const payload = decodeJwtPayload(authData.tokens.id_token);
|
|
479
|
+
if (payload) {
|
|
480
|
+
if (typeof payload.email === "string")
|
|
481
|
+
accountEmail = payload.email;
|
|
482
|
+
const authObj = payload["https://api.openai.com/auth"];
|
|
483
|
+
if (authObj && typeof authObj.chatgpt_plan_type === "string") {
|
|
484
|
+
accountPlan = `ChatGPT ${authObj.chatgpt_plan_type}`;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
} else if (authData.tokens?.access_token) {
|
|
488
|
+
isLoggedIn = true;
|
|
489
|
+
}
|
|
490
|
+
} catch {}
|
|
491
|
+
}
|
|
492
|
+
if (process.env.CODEX_API_KEY || process.env.OPENAI_API_KEY) {
|
|
493
|
+
isLoggedIn = true;
|
|
494
|
+
accountPlan = "API Key (Env)";
|
|
495
|
+
}
|
|
496
|
+
if (!isLoggedIn) {
|
|
497
|
+
return {
|
|
498
|
+
runtimeId: "codex-acp",
|
|
499
|
+
provider: "OpenAI / Codex",
|
|
500
|
+
status: "unauthorized",
|
|
501
|
+
message: "未登录 Codex (Not logged in)",
|
|
502
|
+
updatedAt: Date.now()
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
return {
|
|
506
|
+
runtimeId: "codex-acp",
|
|
507
|
+
provider: "OpenAI / Codex",
|
|
508
|
+
account: {
|
|
509
|
+
email: accountEmail,
|
|
510
|
+
plan: accountPlan,
|
|
511
|
+
type: "chatgpt"
|
|
512
|
+
},
|
|
513
|
+
status: "ok",
|
|
514
|
+
updatedAt: Date.now()
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// src/runtimes/codex/index.ts
|
|
519
|
+
import { mkdir as mkdir2, writeFile as writeFile2 } from "node:fs/promises";
|
|
520
|
+
import { join as join5 } from "node:path";
|
|
521
|
+
|
|
522
|
+
// src/runtimes/codex/codex-provider-home.ts
|
|
523
|
+
var INKDOWN_PROVIDER_ID = "inkdown-custom";
|
|
524
|
+
var INKDOWN_PROVIDER_API_KEY_ENV = "INKDOWN_PROVIDER_API_KEY";
|
|
525
|
+
function tomlEscape(value) {
|
|
526
|
+
return value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
|
|
527
|
+
}
|
|
528
|
+
function buildCustomProviderConfigToml(config) {
|
|
529
|
+
const name = config.name?.trim() || "自定义 API";
|
|
530
|
+
const wireApi = config.wireApi === "responses" ? "responses" : "chat";
|
|
531
|
+
return [
|
|
532
|
+
`model_provider = "${INKDOWN_PROVIDER_ID}"`,
|
|
533
|
+
`model = "${tomlEscape(config.model.trim())}"`,
|
|
534
|
+
"",
|
|
535
|
+
`[model_providers.${INKDOWN_PROVIDER_ID}]`,
|
|
536
|
+
`name = "${tomlEscape(name)}"`,
|
|
537
|
+
`base_url = "${tomlEscape(config.baseUrl.trim())}"`,
|
|
538
|
+
`env_key = "${INKDOWN_PROVIDER_API_KEY_ENV}"`,
|
|
539
|
+
`wire_api = "${wireApi}"`,
|
|
540
|
+
""
|
|
541
|
+
].join(`
|
|
542
|
+
`);
|
|
543
|
+
}
|
|
544
|
+
function buildCustomProviderSpawnEnv(config, apiKey, codexHome) {
|
|
545
|
+
return {
|
|
546
|
+
CODEX_HOME: codexHome,
|
|
547
|
+
[INKDOWN_PROVIDER_API_KEY_ENV]: apiKey
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
|
|
313
551
|
// src/runtimes/codex/codex-provider.ts
|
|
314
552
|
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
315
|
-
import { existsSync as
|
|
316
|
-
import { join as
|
|
553
|
+
import { existsSync as existsSync3 } from "node:fs";
|
|
554
|
+
import { join as join4 } from "node:path";
|
|
317
555
|
|
|
318
556
|
// src/client/acp-paths.ts
|
|
319
557
|
import { homedir as homedir2 } from "node:os";
|
|
320
|
-
import { join as
|
|
558
|
+
import { join as join3 } from "node:path";
|
|
321
559
|
function getDefaultAgentDataDir() {
|
|
322
560
|
if (process.env.INKDOWN_DATA_DIR) {
|
|
323
561
|
return process.env.INKDOWN_DATA_DIR;
|
|
@@ -326,20 +564,20 @@ function getDefaultAgentDataDir() {
|
|
|
326
564
|
return process.env.ACP_DATA_DIR;
|
|
327
565
|
}
|
|
328
566
|
if (process.platform === "win32") {
|
|
329
|
-
return process.env.LOCALAPPDATA || process.env.APPDATA ||
|
|
567
|
+
return process.env.LOCALAPPDATA || process.env.APPDATA || join3(homedir2(), "AppData", "Local", "inkdown");
|
|
330
568
|
}
|
|
331
569
|
if (process.platform === "darwin") {
|
|
332
|
-
return
|
|
570
|
+
return join3(homedir2(), "Library", "Application Support", "inkdown");
|
|
333
571
|
}
|
|
334
|
-
return process.env.XDG_DATA_HOME ||
|
|
572
|
+
return process.env.XDG_DATA_HOME || join3(homedir2(), ".local", "share", "inkdown");
|
|
335
573
|
}
|
|
336
574
|
|
|
337
575
|
// src/runtimes/codex/codex-provider.ts
|
|
338
576
|
function providerFilePath(dir) {
|
|
339
|
-
return
|
|
577
|
+
return join4(dir, "agent", "provider.json");
|
|
340
578
|
}
|
|
341
579
|
function providerHomePath(dir) {
|
|
342
|
-
return
|
|
580
|
+
return join4(dir, "agent", "codex-home");
|
|
343
581
|
}
|
|
344
582
|
function toStatus(stored) {
|
|
345
583
|
if (!stored)
|
|
@@ -365,7 +603,7 @@ function validate(payload) {
|
|
|
365
603
|
}
|
|
366
604
|
async function readStoredAcpProvider(dir = getDefaultAgentDataDir()) {
|
|
367
605
|
const file = providerFilePath(dir);
|
|
368
|
-
if (!
|
|
606
|
+
if (!existsSync3(file))
|
|
369
607
|
return null;
|
|
370
608
|
try {
|
|
371
609
|
const raw = JSON.parse(await readFile(file, "utf8"));
|
|
@@ -394,7 +632,7 @@ async function saveAcpProvider(payload, dir = getDefaultAgentDataDir()) {
|
|
|
394
632
|
const apiKey = payload.apiKey.trim() || (await readStoredAcpProvider(dir))?.apiKey || "";
|
|
395
633
|
if (!apiKey)
|
|
396
634
|
return err({ code: "INVALID_ARGUMENT", message: "API Key 不能为空" });
|
|
397
|
-
await mkdir(
|
|
635
|
+
await mkdir(join4(dir, "agent"), { recursive: true });
|
|
398
636
|
const stored = {
|
|
399
637
|
name: payload.name?.trim() || undefined,
|
|
400
638
|
baseUrl: payload.baseUrl.trim(),
|
|
@@ -417,7 +655,7 @@ function getAcpProviderCodexHome(dir = getDefaultAgentDataDir()) {
|
|
|
417
655
|
async function provisionCustomProviderCodexHome(provider, codexHome) {
|
|
418
656
|
try {
|
|
419
657
|
await mkdir2(codexHome, { recursive: true });
|
|
420
|
-
await writeFile2(
|
|
658
|
+
await writeFile2(join5(codexHome, "config.toml"), buildCustomProviderConfigToml(provider), "utf8");
|
|
421
659
|
return true;
|
|
422
660
|
} catch (error) {
|
|
423
661
|
console.error("[acp] 自定义供应商 config.toml 写入失败,回落本机登录", error);
|
|
@@ -426,6 +664,7 @@ async function provisionCustomProviderCodexHome(provider, codexHome) {
|
|
|
426
664
|
}
|
|
427
665
|
var codexAdapter = {
|
|
428
666
|
id: "codex-acp",
|
|
667
|
+
probeQuota: async (options) => probeCodexQuota(options),
|
|
429
668
|
probeAuth: () => {
|
|
430
669
|
return probeCodexAuth();
|
|
431
670
|
},
|
|
@@ -442,20 +681,86 @@ var codexAdapter = {
|
|
|
442
681
|
}
|
|
443
682
|
};
|
|
444
683
|
|
|
445
|
-
// src/runtimes/claude/
|
|
446
|
-
import { existsSync as
|
|
684
|
+
// src/runtimes/claude/quota.ts
|
|
685
|
+
import { existsSync as existsSync4, readFileSync as readFileSync2 } from "node:fs";
|
|
447
686
|
import { homedir as homedir3 } from "node:os";
|
|
448
|
-
import { join as
|
|
687
|
+
import { join as join6 } from "node:path";
|
|
688
|
+
async function probeClaudeQuota(options) {
|
|
689
|
+
const claudeHome = resolveClaudeHome();
|
|
690
|
+
const claudeJsonPath = join6(homedir3(), ".claude.json");
|
|
691
|
+
const credsPath = join6(claudeHome, ".credentials.json");
|
|
692
|
+
let isLoggedIn = false;
|
|
693
|
+
let isBanned = false;
|
|
694
|
+
let accountEmail;
|
|
695
|
+
let plan = "Claude Free / Default";
|
|
696
|
+
if (existsSync4(claudeJsonPath)) {
|
|
697
|
+
try {
|
|
698
|
+
const data = JSON.parse(readFileSync2(claudeJsonPath, "utf8"));
|
|
699
|
+
if (data.oauthAccount) {
|
|
700
|
+
isLoggedIn = true;
|
|
701
|
+
accountEmail = data.oauthAccount.emailAddress;
|
|
702
|
+
plan = data.oauthAccount.subscriptionTier || "Claude Pro";
|
|
703
|
+
}
|
|
704
|
+
if (data.isBanned || data.accountSuspended || data.status === "banned") {
|
|
705
|
+
isBanned = true;
|
|
706
|
+
}
|
|
707
|
+
} catch {}
|
|
708
|
+
}
|
|
709
|
+
if (existsSync4(credsPath)) {
|
|
710
|
+
try {
|
|
711
|
+
const creds = JSON.parse(readFileSync2(credsPath, "utf8"));
|
|
712
|
+
if (creds.accessToken || creds.sessionKey)
|
|
713
|
+
isLoggedIn = true;
|
|
714
|
+
} catch {}
|
|
715
|
+
}
|
|
716
|
+
if (process.env.ANTHROPIC_API_KEY || process.env.CLAUDE_CODE_OAUTH_TOKEN) {
|
|
717
|
+
isLoggedIn = true;
|
|
718
|
+
plan = "API Key (Env)";
|
|
719
|
+
}
|
|
720
|
+
if (isBanned) {
|
|
721
|
+
return {
|
|
722
|
+
runtimeId: "claude",
|
|
723
|
+
provider: "Anthropic / Claude Code",
|
|
724
|
+
status: "banned",
|
|
725
|
+
message: "账号已被封禁或暂停使用 (Account suspended / banned)",
|
|
726
|
+
updatedAt: Date.now()
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
if (!isLoggedIn) {
|
|
730
|
+
return {
|
|
731
|
+
runtimeId: "claude",
|
|
732
|
+
provider: "Anthropic / Claude Code",
|
|
733
|
+
status: "unauthorized",
|
|
734
|
+
message: "未登录 Claude Code 或凭据无效 (Unauthorized)",
|
|
735
|
+
updatedAt: Date.now()
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
return {
|
|
739
|
+
runtimeId: "claude",
|
|
740
|
+
provider: "Anthropic / Claude Code",
|
|
741
|
+
account: {
|
|
742
|
+
email: accountEmail,
|
|
743
|
+
plan
|
|
744
|
+
},
|
|
745
|
+
status: "ok",
|
|
746
|
+
updatedAt: Date.now()
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
// src/runtimes/claude/index.ts
|
|
751
|
+
import { existsSync as existsSync5 } from "node:fs";
|
|
752
|
+
import { homedir as homedir4 } from "node:os";
|
|
753
|
+
import { join as join7 } from "node:path";
|
|
449
754
|
function resolveClaudeHome() {
|
|
450
755
|
const fromEnv = process.env.CLAUDE_CONFIG_DIR?.trim();
|
|
451
756
|
if (fromEnv)
|
|
452
757
|
return fromEnv;
|
|
453
|
-
return
|
|
758
|
+
return join7(homedir4(), ".claude");
|
|
454
759
|
}
|
|
455
760
|
function probeClaudeAuth() {
|
|
456
761
|
const claudeHome = resolveClaudeHome();
|
|
457
|
-
const hasCodexHome =
|
|
458
|
-
const hasAuthFile =
|
|
762
|
+
const hasCodexHome = existsSync5(claudeHome);
|
|
763
|
+
const hasAuthFile = existsSync5(join7(homedir4(), ".claude.json")) || existsSync5(join7(claudeHome, ".credentials.json"));
|
|
459
764
|
const hasApiKeyEnv = Boolean(process.env.ANTHROPIC_API_KEY?.trim() || process.env.CLAUDE_CODE_OAUTH_TOKEN?.trim());
|
|
460
765
|
return {
|
|
461
766
|
codexHome: claudeHome,
|
|
@@ -468,21 +773,22 @@ function probeClaudeAuth() {
|
|
|
468
773
|
var claudeAdapter = {
|
|
469
774
|
id: "claude",
|
|
470
775
|
probeAuth: () => probeClaudeAuth(),
|
|
776
|
+
probeQuota: async (options) => probeClaudeQuota(options),
|
|
471
777
|
orderAuthMethods: (methods) => methods,
|
|
472
778
|
canSkipInteractiveAuth: () => false
|
|
473
779
|
};
|
|
474
780
|
|
|
475
781
|
// src/runtimes/gemini/index.ts
|
|
476
|
-
import { existsSync as
|
|
477
|
-
import { homedir as
|
|
478
|
-
import { join as
|
|
782
|
+
import { existsSync as existsSync6 } from "node:fs";
|
|
783
|
+
import { homedir as homedir5 } from "node:os";
|
|
784
|
+
import { join as join8 } from "node:path";
|
|
479
785
|
function resolveGeminiHome() {
|
|
480
|
-
return
|
|
786
|
+
return join8(homedir5(), ".gemini");
|
|
481
787
|
}
|
|
482
788
|
function probeGeminiAuth() {
|
|
483
789
|
const geminiHome = resolveGeminiHome();
|
|
484
|
-
const hasCodexHome =
|
|
485
|
-
const hasAuthFile =
|
|
790
|
+
const hasCodexHome = existsSync6(geminiHome);
|
|
791
|
+
const hasAuthFile = existsSync6(join8(geminiHome, "oauth_creds.json")) || Boolean(process.env.GOOGLE_APPLICATION_CREDENTIALS?.trim());
|
|
486
792
|
const hasApiKeyEnv = Boolean(process.env.GEMINI_API_KEY?.trim() || process.env.GOOGLE_API_KEY?.trim());
|
|
487
793
|
return {
|
|
488
794
|
codexHome: geminiHome,
|
|
@@ -500,18 +806,18 @@ var geminiAdapter = {
|
|
|
500
806
|
};
|
|
501
807
|
|
|
502
808
|
// src/runtimes/copilot/index.ts
|
|
503
|
-
import { existsSync as
|
|
504
|
-
import { homedir as
|
|
505
|
-
import { join as
|
|
809
|
+
import { existsSync as existsSync7, readFileSync as readFileSync3 } from "node:fs";
|
|
810
|
+
import { homedir as homedir6 } from "node:os";
|
|
811
|
+
import { join as join9 } from "node:path";
|
|
506
812
|
function resolveCopilotHome() {
|
|
507
813
|
const fromEnv = process.env.COPILOT_HOME?.trim();
|
|
508
814
|
if (fromEnv)
|
|
509
815
|
return fromEnv;
|
|
510
|
-
return
|
|
816
|
+
return join9(homedir6(), ".copilot");
|
|
511
817
|
}
|
|
512
818
|
function hasCopilotLogin(home) {
|
|
513
819
|
try {
|
|
514
|
-
const raw =
|
|
820
|
+
const raw = readFileSync3(join9(home, "config.json"), "utf8");
|
|
515
821
|
const parsed = JSON.parse(raw);
|
|
516
822
|
return Array.isArray(parsed.loggedInUsers) && parsed.loggedInUsers.length > 0;
|
|
517
823
|
} catch {
|
|
@@ -520,7 +826,7 @@ function hasCopilotLogin(home) {
|
|
|
520
826
|
}
|
|
521
827
|
function probeCopilotAuth() {
|
|
522
828
|
const copilotHome = resolveCopilotHome();
|
|
523
|
-
const hasCodexHome =
|
|
829
|
+
const hasCodexHome = existsSync7(copilotHome);
|
|
524
830
|
const hasAuthFile = hasCopilotLogin(copilotHome);
|
|
525
831
|
const hasApiKeyEnv = Boolean(process.env.COPILOT_GITHUB_TOKEN?.trim() || process.env.GH_TOKEN?.trim() || process.env.GITHUB_TOKEN?.trim());
|
|
526
832
|
return {
|
|
@@ -538,17 +844,68 @@ var copilotAdapter = {
|
|
|
538
844
|
canSkipInteractiveAuth: () => false
|
|
539
845
|
};
|
|
540
846
|
|
|
847
|
+
// src/runtimes/opencode/quota.ts
|
|
848
|
+
import { existsSync as existsSync8, readFileSync as readFileSync4 } from "node:fs";
|
|
849
|
+
function resolveOpencodeAuthPath() {
|
|
850
|
+
const candidates = resolveOpencodeAuthCandidates();
|
|
851
|
+
for (const c of candidates) {
|
|
852
|
+
if (existsSync8(c))
|
|
853
|
+
return c;
|
|
854
|
+
}
|
|
855
|
+
return candidates[0] ?? null;
|
|
856
|
+
}
|
|
857
|
+
async function probeOpencodeQuota(runtimeId = "opencode") {
|
|
858
|
+
const authPath = resolveOpencodeAuthPath();
|
|
859
|
+
let isLoggedIn = false;
|
|
860
|
+
let accountEmail;
|
|
861
|
+
let plan = "OpenCode Community";
|
|
862
|
+
if (authPath && existsSync8(authPath)) {
|
|
863
|
+
try {
|
|
864
|
+
const data = JSON.parse(readFileSync4(authPath, "utf8"));
|
|
865
|
+
if (data && typeof data === "object") {
|
|
866
|
+
const keys = Object.keys(data);
|
|
867
|
+
if (keys.length > 0) {
|
|
868
|
+
isLoggedIn = true;
|
|
869
|
+
if (data.email)
|
|
870
|
+
accountEmail = String(data.email);
|
|
871
|
+
if (data.plan)
|
|
872
|
+
plan = String(data.plan);
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
} catch {}
|
|
876
|
+
}
|
|
877
|
+
if (!isLoggedIn) {
|
|
878
|
+
return {
|
|
879
|
+
runtimeId,
|
|
880
|
+
provider: "OpenCode",
|
|
881
|
+
status: "unauthorized",
|
|
882
|
+
message: "未检测到 OpenCode 登录凭证 (auth.json / SQLite)",
|
|
883
|
+
updatedAt: Date.now()
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
return {
|
|
887
|
+
runtimeId,
|
|
888
|
+
provider: "OpenCode",
|
|
889
|
+
account: {
|
|
890
|
+
email: accountEmail,
|
|
891
|
+
plan
|
|
892
|
+
},
|
|
893
|
+
status: "ok",
|
|
894
|
+
updatedAt: Date.now()
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
|
|
541
898
|
// src/runtimes/opencode/index.ts
|
|
542
|
-
import { existsSync as
|
|
543
|
-
import { homedir as
|
|
544
|
-
import { dirname, join as
|
|
899
|
+
import { existsSync as existsSync9, readFileSync as readFileSync5 } from "node:fs";
|
|
900
|
+
import { homedir as homedir7 } from "node:os";
|
|
901
|
+
import { dirname, join as join10 } from "node:path";
|
|
545
902
|
function pickEnv(env, key) {
|
|
546
903
|
return env?.[key]?.trim() ?? "";
|
|
547
904
|
}
|
|
548
905
|
function resolveOpencodeAuthCandidates(overrides) {
|
|
549
906
|
const platform = overrides?.platform ?? process.platform;
|
|
550
907
|
const env = overrides?.env ?? process.env;
|
|
551
|
-
const home = overrides?.home ??
|
|
908
|
+
const home = overrides?.home ?? homedir7();
|
|
552
909
|
const seen = new Set;
|
|
553
910
|
const out = [];
|
|
554
911
|
const push = (p) => {
|
|
@@ -559,27 +916,27 @@ function resolveOpencodeAuthCandidates(overrides) {
|
|
|
559
916
|
};
|
|
560
917
|
const xdg = pickEnv(env, "XDG_DATA_HOME");
|
|
561
918
|
if (xdg)
|
|
562
|
-
push(
|
|
919
|
+
push(join10(xdg, "opencode", "auth.json"));
|
|
563
920
|
if (platform === "win32") {
|
|
564
|
-
const localAppData = pickEnv(env, "LOCALAPPDATA") || (pickEnv(env, "USERPROFILE") ?
|
|
921
|
+
const localAppData = pickEnv(env, "LOCALAPPDATA") || (pickEnv(env, "USERPROFILE") ? join10(pickEnv(env, "USERPROFILE"), "AppData", "Local") : "");
|
|
565
922
|
if (localAppData)
|
|
566
|
-
push(
|
|
923
|
+
push(join10(localAppData, "opencode", "auth.json"));
|
|
567
924
|
const userProfile = pickEnv(env, "USERPROFILE") || home;
|
|
568
925
|
if (userProfile)
|
|
569
|
-
push(
|
|
926
|
+
push(join10(userProfile, ".local", "share", "opencode", "auth.json"));
|
|
570
927
|
} else if (home) {
|
|
571
|
-
push(
|
|
928
|
+
push(join10(home, ".local", "share", "opencode", "auth.json"));
|
|
572
929
|
}
|
|
573
930
|
return out;
|
|
574
931
|
}
|
|
575
932
|
function resolveOpencodeAuthFile() {
|
|
576
|
-
return resolveOpencodeAuthCandidates()[0] ??
|
|
933
|
+
return resolveOpencodeAuthCandidates()[0] ?? join10(homedir7(), ".local", "share", "opencode", "auth.json");
|
|
577
934
|
}
|
|
578
935
|
function isValidOpencodeAuthFile(authFile) {
|
|
579
936
|
try {
|
|
580
|
-
if (!authFile || !
|
|
937
|
+
if (!authFile || !existsSync9(authFile))
|
|
581
938
|
return false;
|
|
582
|
-
const raw =
|
|
939
|
+
const raw = readFileSync5(authFile, "utf8").trim();
|
|
583
940
|
if (!raw)
|
|
584
941
|
return false;
|
|
585
942
|
const parsed = JSON.parse(raw);
|
|
@@ -597,7 +954,7 @@ function probeOpencodeAuth(overrides) {
|
|
|
597
954
|
for (const candidate of candidates) {
|
|
598
955
|
let exists = false;
|
|
599
956
|
try {
|
|
600
|
-
exists =
|
|
957
|
+
exists = existsSync9(candidate);
|
|
601
958
|
} catch {
|
|
602
959
|
exists = false;
|
|
603
960
|
}
|
|
@@ -613,7 +970,7 @@ function probeOpencodeAuth(overrides) {
|
|
|
613
970
|
const authDir = dirname(authFile);
|
|
614
971
|
let hasCodexHome = false;
|
|
615
972
|
try {
|
|
616
|
-
hasCodexHome =
|
|
973
|
+
hasCodexHome = existsSync9(authDir);
|
|
617
974
|
} catch {
|
|
618
975
|
hasCodexHome = false;
|
|
619
976
|
}
|
|
@@ -629,22 +986,55 @@ function probeOpencodeAuth(overrides) {
|
|
|
629
986
|
var opencodeAdapter = {
|
|
630
987
|
id: "opencode",
|
|
631
988
|
probeAuth: () => probeOpencodeAuth(),
|
|
989
|
+
probeQuota: async () => probeOpencodeQuota(),
|
|
990
|
+
orderAuthMethods: (methods) => methods,
|
|
991
|
+
canSkipInteractiveAuth: () => false,
|
|
992
|
+
tryDirectSessionFirst: true
|
|
993
|
+
};
|
|
994
|
+
|
|
995
|
+
// src/runtimes/opencode-v2/index.ts
|
|
996
|
+
var opencodeV2Adapter = {
|
|
997
|
+
id: "opencode-v2",
|
|
998
|
+
probeAuth: () => probeOpencodeAuth(),
|
|
632
999
|
orderAuthMethods: (methods) => methods,
|
|
633
1000
|
canSkipInteractiveAuth: () => false,
|
|
634
1001
|
tryDirectSessionFirst: true
|
|
635
1002
|
};
|
|
636
1003
|
|
|
1004
|
+
// src/runtimes/cursor/quota.ts
|
|
1005
|
+
async function probeCursorQuota() {
|
|
1006
|
+
const auth = probeCursorAuth();
|
|
1007
|
+
if (!auth.looksLoggedIn) {
|
|
1008
|
+
return {
|
|
1009
|
+
runtimeId: "cursor-cli",
|
|
1010
|
+
provider: "Cursor",
|
|
1011
|
+
status: "unauthorized",
|
|
1012
|
+
message: "未检测到 Cursor CLI 登录凭证",
|
|
1013
|
+
updatedAt: Date.now()
|
|
1014
|
+
};
|
|
1015
|
+
}
|
|
1016
|
+
return {
|
|
1017
|
+
runtimeId: "cursor-cli",
|
|
1018
|
+
provider: "Cursor",
|
|
1019
|
+
account: {
|
|
1020
|
+
plan: "Cursor Pro / Fast Requests"
|
|
1021
|
+
},
|
|
1022
|
+
status: "ok",
|
|
1023
|
+
updatedAt: Date.now()
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
1026
|
+
|
|
637
1027
|
// src/runtimes/cursor/index.ts
|
|
638
1028
|
import { spawnSync } from "node:child_process";
|
|
639
|
-
import { existsSync as
|
|
640
|
-
import { homedir as
|
|
641
|
-
import { delimiter, dirname as dirname2, join as
|
|
1029
|
+
import { existsSync as existsSync10, readFileSync as readFileSync6 } from "node:fs";
|
|
1030
|
+
import { homedir as homedir8 } from "node:os";
|
|
1031
|
+
import { delimiter, dirname as dirname2, join as join11 } from "node:path";
|
|
642
1032
|
function cursorLocalAppData(env) {
|
|
643
1033
|
const local = env.LOCALAPPDATA?.trim();
|
|
644
1034
|
if (local)
|
|
645
1035
|
return local;
|
|
646
1036
|
const profile = env.USERPROFILE?.trim();
|
|
647
|
-
return profile ?
|
|
1037
|
+
return profile ? join11(profile, "AppData", "Local") : "";
|
|
648
1038
|
}
|
|
649
1039
|
function isOnPath(command, env, platform) {
|
|
650
1040
|
const pathValue = env.PATH ?? env.Path ?? "";
|
|
@@ -656,7 +1046,7 @@ function isOnPath(command, env, platform) {
|
|
|
656
1046
|
if (!trimmed)
|
|
657
1047
|
continue;
|
|
658
1048
|
try {
|
|
659
|
-
if (
|
|
1049
|
+
if (existsSync10(join11(trimmed, command)))
|
|
660
1050
|
return true;
|
|
661
1051
|
} catch {}
|
|
662
1052
|
}
|
|
@@ -665,24 +1055,24 @@ function isOnPath(command, env, platform) {
|
|
|
665
1055
|
function resolveDirectCursorCommand(platform, env, home) {
|
|
666
1056
|
if (platform === "win32") {
|
|
667
1057
|
const localAppData = cursorLocalAppData(env);
|
|
668
|
-
return localAppData ?
|
|
1058
|
+
return localAppData ? join11(localAppData, "cursor-agent", "agent.cmd") : "";
|
|
669
1059
|
}
|
|
670
|
-
return home ?
|
|
1060
|
+
return home ? join11(home, ".local", "bin", "agent") : "";
|
|
671
1061
|
}
|
|
672
1062
|
function resolveCursorCommand(overrides) {
|
|
673
1063
|
const args = ["acp"];
|
|
674
1064
|
const platform = overrides?.platform ?? process.platform;
|
|
675
1065
|
const env = overrides?.env ?? process.env;
|
|
676
|
-
const home = overrides?.home ??
|
|
1066
|
+
const home = overrides?.home ?? homedir8();
|
|
677
1067
|
if (platform === "win32") {
|
|
678
1068
|
const directCmd = resolveDirectCursorCommand(platform, env, home);
|
|
679
|
-
if (directCmd &&
|
|
1069
|
+
if (directCmd && existsSync10(directCmd)) {
|
|
680
1070
|
return { command: directCmd, args };
|
|
681
1071
|
}
|
|
682
1072
|
return { command: "agent.cmd", args };
|
|
683
1073
|
}
|
|
684
1074
|
const directBin = resolveDirectCursorCommand(platform, env, home);
|
|
685
|
-
if (directBin &&
|
|
1075
|
+
if (directBin && existsSync10(directBin)) {
|
|
686
1076
|
return { command: directBin, args };
|
|
687
1077
|
}
|
|
688
1078
|
return { command: "agent", args };
|
|
@@ -691,11 +1081,11 @@ function resolveCursorSpawnCommand(overrides) {
|
|
|
691
1081
|
const args = ["acp"];
|
|
692
1082
|
const platform = overrides?.platform ?? process.platform;
|
|
693
1083
|
const env = overrides?.env ?? process.env;
|
|
694
|
-
const home = overrides?.home ??
|
|
1084
|
+
const home = overrides?.home ?? homedir8();
|
|
695
1085
|
const binary = platform === "win32" ? "agent.cmd" : "agent";
|
|
696
1086
|
const direct = resolveDirectCursorCommand(platform, env, home);
|
|
697
1087
|
try {
|
|
698
|
-
if (direct &&
|
|
1088
|
+
if (direct && existsSync10(direct))
|
|
699
1089
|
return { command: direct, args };
|
|
700
1090
|
} catch {}
|
|
701
1091
|
if (isOnPath(binary, env, platform))
|
|
@@ -717,12 +1107,12 @@ function cursorRoamingAppData(env, home) {
|
|
|
717
1107
|
if (roaming)
|
|
718
1108
|
return roaming;
|
|
719
1109
|
const profile = pickEnv2(env, "USERPROFILE") || home;
|
|
720
|
-
return profile ?
|
|
1110
|
+
return profile ? join11(profile, "AppData", "Roaming") : "";
|
|
721
1111
|
}
|
|
722
1112
|
function resolveCursorAuthCandidates(overrides) {
|
|
723
1113
|
const platform = overrides?.platform ?? process.platform;
|
|
724
1114
|
const env = overrides?.env ?? process.env;
|
|
725
|
-
const home = overrides?.home ??
|
|
1115
|
+
const home = overrides?.home ?? homedir8();
|
|
726
1116
|
const seen = new Set;
|
|
727
1117
|
const out = [];
|
|
728
1118
|
const push = (p) => {
|
|
@@ -733,16 +1123,16 @@ function resolveCursorAuthCandidates(overrides) {
|
|
|
733
1123
|
};
|
|
734
1124
|
const xdg = pickEnv2(env, "XDG_CONFIG_HOME");
|
|
735
1125
|
if (xdg)
|
|
736
|
-
push(
|
|
1126
|
+
push(join11(xdg, "cursor", "auth.json"));
|
|
737
1127
|
if (platform === "win32") {
|
|
738
1128
|
const profile = pickEnv2(env, "USERPROFILE") || home;
|
|
739
1129
|
if (profile)
|
|
740
|
-
push(
|
|
1130
|
+
push(join11(profile, ".config", "cursor", "auth.json"));
|
|
741
1131
|
const roaming = cursorRoamingAppData(env, home);
|
|
742
1132
|
if (roaming)
|
|
743
|
-
push(
|
|
1133
|
+
push(join11(roaming, "Cursor", "User", "globalStorage", "storage.json"));
|
|
744
1134
|
} else if (home) {
|
|
745
|
-
push(
|
|
1135
|
+
push(join11(home, ".config", "cursor", "auth.json"));
|
|
746
1136
|
}
|
|
747
1137
|
return out;
|
|
748
1138
|
}
|
|
@@ -753,9 +1143,9 @@ function isValidCursorAuthFile(authFile) {
|
|
|
753
1143
|
try {
|
|
754
1144
|
if (!authFile || isStorageHintPath(authFile))
|
|
755
1145
|
return false;
|
|
756
|
-
if (!
|
|
1146
|
+
if (!existsSync10(authFile))
|
|
757
1147
|
return false;
|
|
758
|
-
const raw =
|
|
1148
|
+
const raw = readFileSync6(authFile, "utf8").trim();
|
|
759
1149
|
if (!raw)
|
|
760
1150
|
return false;
|
|
761
1151
|
const parsed = JSON.parse(raw);
|
|
@@ -773,9 +1163,9 @@ function isHintCursorStorageFile(authFile) {
|
|
|
773
1163
|
try {
|
|
774
1164
|
if (!authFile || !isStorageHintPath(authFile))
|
|
775
1165
|
return false;
|
|
776
|
-
if (!
|
|
1166
|
+
if (!existsSync10(authFile))
|
|
777
1167
|
return false;
|
|
778
|
-
const raw =
|
|
1168
|
+
const raw = readFileSync6(authFile, "utf8").trim();
|
|
779
1169
|
if (!raw)
|
|
780
1170
|
return false;
|
|
781
1171
|
const parsed = JSON.parse(raw);
|
|
@@ -791,7 +1181,7 @@ function isValidCursorCandidate(authFile) {
|
|
|
791
1181
|
}
|
|
792
1182
|
function probeCursorAuth(overrides) {
|
|
793
1183
|
const env = overrides?.env ?? process.env;
|
|
794
|
-
const home = overrides?.home ??
|
|
1184
|
+
const home = overrides?.home ?? homedir8();
|
|
795
1185
|
const hasApiKeyEnv = Boolean(env.CURSOR_API_KEY?.trim() || env.CURSOR_AUTH_TOKEN?.trim());
|
|
796
1186
|
const candidates = resolveCursorAuthCandidates(overrides);
|
|
797
1187
|
let firstExisting = null;
|
|
@@ -799,7 +1189,7 @@ function probeCursorAuth(overrides) {
|
|
|
799
1189
|
for (const candidate of candidates) {
|
|
800
1190
|
let exists = false;
|
|
801
1191
|
try {
|
|
802
|
-
exists =
|
|
1192
|
+
exists = existsSync10(candidate);
|
|
803
1193
|
} catch {
|
|
804
1194
|
exists = false;
|
|
805
1195
|
}
|
|
@@ -811,12 +1201,12 @@ function probeCursorAuth(overrides) {
|
|
|
811
1201
|
if (isValidCursorCandidate(candidate))
|
|
812
1202
|
validFile = candidate;
|
|
813
1203
|
}
|
|
814
|
-
const fallback = home ?
|
|
1204
|
+
const fallback = home ? join11(home, ".config", "cursor", "auth.json") : "";
|
|
815
1205
|
const authFile = validFile ?? firstExisting ?? candidates[0] ?? fallback;
|
|
816
1206
|
const authDir = authFile ? dirname2(authFile) : "";
|
|
817
1207
|
let hasCodexHome = false;
|
|
818
1208
|
try {
|
|
819
|
-
hasCodexHome = authDir ?
|
|
1209
|
+
hasCodexHome = authDir ? existsSync10(authDir) : false;
|
|
820
1210
|
} catch {
|
|
821
1211
|
hasCodexHome = false;
|
|
822
1212
|
}
|
|
@@ -831,6 +1221,7 @@ function probeCursorAuth(overrides) {
|
|
|
831
1221
|
var cursorAdapter = {
|
|
832
1222
|
id: "cursor-cli",
|
|
833
1223
|
probeAuth: () => probeCursorAuth(),
|
|
1224
|
+
probeQuota: async () => probeCursorQuota(),
|
|
834
1225
|
resolveSpawnCommand: () => resolveCursorSpawnCommand(),
|
|
835
1226
|
orderAuthMethods: (methods) => methods,
|
|
836
1227
|
canSkipInteractiveAuth: () => false,
|
|
@@ -949,6 +1340,55 @@ var deepseekAdapter = {
|
|
|
949
1340
|
canSkipInteractiveAuth: () => false
|
|
950
1341
|
};
|
|
951
1342
|
|
|
1343
|
+
// src/runtimes/agy/quota.ts
|
|
1344
|
+
import { existsSync as existsSync11 } from "node:fs";
|
|
1345
|
+
import { join as join12 } from "node:path";
|
|
1346
|
+
function resolveGeminiHome2() {
|
|
1347
|
+
const home = process.env.USERPROFILE || process.env.HOME || "";
|
|
1348
|
+
return join12(home, ".gemini");
|
|
1349
|
+
}
|
|
1350
|
+
async function probeAgyQuota(options) {
|
|
1351
|
+
const geminiHome = resolveGeminiHome2();
|
|
1352
|
+
const hasAgyDir = existsSync11(join12(geminiHome, "antigravity")) || existsSync11(join12(geminiHome, "antigravity-cli"));
|
|
1353
|
+
const hasApiKey = Boolean(process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY);
|
|
1354
|
+
const isAvailable = hasAgyDir || hasApiKey;
|
|
1355
|
+
if (!isAvailable) {
|
|
1356
|
+
return {
|
|
1357
|
+
runtimeId: "agy",
|
|
1358
|
+
provider: "Google Antigravity",
|
|
1359
|
+
status: "unauthorized",
|
|
1360
|
+
message: "wenjianlingu antigravity cli",
|
|
1361
|
+
updatedAt: Date.now()
|
|
1362
|
+
};
|
|
1363
|
+
}
|
|
1364
|
+
return {
|
|
1365
|
+
runtimeId: "agy",
|
|
1366
|
+
provider: "Google Antigravity",
|
|
1367
|
+
account: {
|
|
1368
|
+
plan: "Antigravity / Gemini",
|
|
1369
|
+
type: "gemini"
|
|
1370
|
+
},
|
|
1371
|
+
primaryWindow: {
|
|
1372
|
+
label: "5-Hour limit",
|
|
1373
|
+
percentLeft: 100,
|
|
1374
|
+
usedPercent: 0,
|
|
1375
|
+
windowDurationMins: 300
|
|
1376
|
+
},
|
|
1377
|
+
secondaryWindow: {
|
|
1378
|
+
label: "Weekly limit",
|
|
1379
|
+
percentLeft: 100,
|
|
1380
|
+
usedPercent: 0,
|
|
1381
|
+
windowDurationMins: 10080
|
|
1382
|
+
},
|
|
1383
|
+
credits: {
|
|
1384
|
+
unlimited: true,
|
|
1385
|
+
balance: "unlimited"
|
|
1386
|
+
},
|
|
1387
|
+
status: "ok",
|
|
1388
|
+
updatedAt: Date.now()
|
|
1389
|
+
};
|
|
1390
|
+
}
|
|
1391
|
+
|
|
952
1392
|
// src/runtimes/agy/index.ts
|
|
953
1393
|
function probeAgyAuth() {
|
|
954
1394
|
return {
|
|
@@ -962,6 +1402,7 @@ function probeAgyAuth() {
|
|
|
962
1402
|
var agyAdapter = {
|
|
963
1403
|
id: "agy",
|
|
964
1404
|
probeAuth: () => probeAgyAuth(),
|
|
1405
|
+
probeQuota: (options) => probeAgyQuota(options),
|
|
965
1406
|
orderAuthMethods: (methods) => methods,
|
|
966
1407
|
canSkipInteractiveAuth: () => false,
|
|
967
1408
|
tryDirectSessionFirst: true
|
|
@@ -981,6 +1422,8 @@ function getAcpRuntimeAdapter(runtimeId) {
|
|
|
981
1422
|
return copilotAdapter;
|
|
982
1423
|
case opencodeAdapter.id:
|
|
983
1424
|
return opencodeAdapter;
|
|
1425
|
+
case opencodeV2Adapter.id:
|
|
1426
|
+
return opencodeV2Adapter;
|
|
984
1427
|
case cursorAdapter.id:
|
|
985
1428
|
case "cursor":
|
|
986
1429
|
return cursorAdapter;
|
|
@@ -1960,8 +2403,8 @@ class AcpTerminalManager {
|
|
|
1960
2403
|
|
|
1961
2404
|
// src/client/acp-proxy-service.ts
|
|
1962
2405
|
import { mkdir as mkdir4, readFile as readFile3, writeFile as writeFile4 } from "node:fs/promises";
|
|
1963
|
-
import { existsSync as
|
|
1964
|
-
import { join as
|
|
2406
|
+
import { existsSync as existsSync12 } from "node:fs";
|
|
2407
|
+
import { join as join13 } from "node:path";
|
|
1965
2408
|
var DEFAULT_ACP_PROXY_SETTINGS = {
|
|
1966
2409
|
enabled: false,
|
|
1967
2410
|
host: "127.0.0.1",
|
|
@@ -1970,7 +2413,7 @@ var DEFAULT_ACP_PROXY_SETTINGS = {
|
|
|
1970
2413
|
var PROXY_ENV_KEYS = ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY"];
|
|
1971
2414
|
var LOWERCASE_PROXY_ENV_KEYS = ["http_proxy", "https_proxy", "all_proxy", "no_proxy"];
|
|
1972
2415
|
function proxyFilePath(dir) {
|
|
1973
|
-
return
|
|
2416
|
+
return join13(dir, "agent", "proxy.json");
|
|
1974
2417
|
}
|
|
1975
2418
|
function validate2(settings) {
|
|
1976
2419
|
if (!settings.host.trim())
|
|
@@ -1982,7 +2425,7 @@ function validate2(settings) {
|
|
|
1982
2425
|
}
|
|
1983
2426
|
async function readAcpProxySettings(dir = getDefaultAgentDataDir()) {
|
|
1984
2427
|
const file = proxyFilePath(dir);
|
|
1985
|
-
if (!
|
|
2428
|
+
if (!existsSync12(file))
|
|
1986
2429
|
return { ...DEFAULT_ACP_PROXY_SETTINGS };
|
|
1987
2430
|
try {
|
|
1988
2431
|
const raw = JSON.parse(await readFile3(file, "utf8"));
|
|
@@ -2004,7 +2447,7 @@ async function saveAcpProxySettings(payload, dir = getDefaultAgentDataDir()) {
|
|
|
2004
2447
|
host: payload.host.trim() || DEFAULT_ACP_PROXY_SETTINGS.host,
|
|
2005
2448
|
port: payload.port
|
|
2006
2449
|
};
|
|
2007
|
-
await mkdir4(
|
|
2450
|
+
await mkdir4(join13(dir, "agent"), { recursive: true });
|
|
2008
2451
|
await writeFile4(proxyFilePath(dir), JSON.stringify(stored, null, 2), "utf8");
|
|
2009
2452
|
return ok(stored);
|
|
2010
2453
|
}
|
|
@@ -2072,6 +2515,12 @@ class AcpSessionHandle {
|
|
|
2072
2515
|
async prompt(promptText, options) {
|
|
2073
2516
|
return this.client.promptSession(this.sessionId, promptText, options);
|
|
2074
2517
|
}
|
|
2518
|
+
get quota() {
|
|
2519
|
+
return this.client.getCachedQuota(this.sessionId);
|
|
2520
|
+
}
|
|
2521
|
+
async getQuota() {
|
|
2522
|
+
return this.client.getQuota(this.sessionId);
|
|
2523
|
+
}
|
|
2075
2524
|
get availableCommands() {
|
|
2076
2525
|
return this.client.getAvailableCommands(this.sessionId);
|
|
2077
2526
|
}
|
|
@@ -2083,6 +2532,7 @@ class AcpSessionHandle {
|
|
|
2083
2532
|
class AcpClient {
|
|
2084
2533
|
status = "disconnected";
|
|
2085
2534
|
commandsCache = new AvailableCommandsCache;
|
|
2535
|
+
quotaCache = new QuotaCache;
|
|
2086
2536
|
runtimeInfo;
|
|
2087
2537
|
adapter;
|
|
2088
2538
|
workspaceRoot;
|
|
@@ -2193,6 +2643,7 @@ class AcpClient {
|
|
|
2193
2643
|
},
|
|
2194
2644
|
onSessionUpdate: (params) => {
|
|
2195
2645
|
this.commandsCache.observe(params);
|
|
2646
|
+
this.quotaCache.observe(params, this.runtimeInfo.id);
|
|
2196
2647
|
this.callbacks.onSessionUpdate?.(params);
|
|
2197
2648
|
}
|
|
2198
2649
|
};
|
|
@@ -2223,6 +2674,23 @@ class AcpClient {
|
|
|
2223
2674
|
return err({ code: "ACP_PROTOCOL_ERROR", message });
|
|
2224
2675
|
}
|
|
2225
2676
|
}
|
|
2677
|
+
getCachedQuota(sessionId) {
|
|
2678
|
+
return this.quotaCache.get(sessionId, this.runtimeInfo.id);
|
|
2679
|
+
}
|
|
2680
|
+
async getQuota(sessionId) {
|
|
2681
|
+
const cached = this.getCachedQuota(sessionId);
|
|
2682
|
+
if (cached && Date.now() - cached.updatedAt < 30000) {
|
|
2683
|
+
return cached;
|
|
2684
|
+
}
|
|
2685
|
+
if (this.adapter.probeQuota) {
|
|
2686
|
+
const probed = await this.adapter.probeQuota({ sessionId });
|
|
2687
|
+
if (probed) {
|
|
2688
|
+
this.quotaCache.set(probed, sessionId);
|
|
2689
|
+
return probed;
|
|
2690
|
+
}
|
|
2691
|
+
}
|
|
2692
|
+
return cached ?? null;
|
|
2693
|
+
}
|
|
2226
2694
|
getAvailableCommands(sessionId) {
|
|
2227
2695
|
return this.commandsCache.get(sessionId);
|
|
2228
2696
|
}
|
|
@@ -3232,6 +3700,7 @@ async function handleInkdownMcpRpc(message, context, tools = INKDOWN_MCP_TOOLS,
|
|
|
3232
3700
|
}
|
|
3233
3701
|
}
|
|
3234
3702
|
export {
|
|
3703
|
+
ACP_CLIENT_VERSION,
|
|
3235
3704
|
ACP_EARLY_EXIT_STDERR_TAIL_LINES,
|
|
3236
3705
|
ACP_EARLY_EXIT_WINDOW_MS,
|
|
3237
3706
|
ACP_SDK_REQUEST_TIMEOUT_MS,
|
|
@@ -3254,6 +3723,8 @@ export {
|
|
|
3254
3723
|
INKDOWN_VIRTUAL_DIR,
|
|
3255
3724
|
INKDOWN_VIRTUAL_RESOURCES,
|
|
3256
3725
|
LEGACY_ZED_CODEX_ACP_NPM_PACKAGE,
|
|
3726
|
+
OPENCODE_CLI_NPM_PACKAGE,
|
|
3727
|
+
QuotaCache,
|
|
3257
3728
|
acpReadTextFile,
|
|
3258
3729
|
acpWriteTextFile,
|
|
3259
3730
|
agyAdapter,
|
|
@@ -3312,6 +3783,7 @@ export {
|
|
|
3312
3783
|
mergeTopLevelModelsIntoConfigOptions,
|
|
3313
3784
|
ok,
|
|
3314
3785
|
opencodeAdapter,
|
|
3786
|
+
opencodeV2Adapter,
|
|
3315
3787
|
orderSilentAuthMethodIds,
|
|
3316
3788
|
parseAcpConfigOptions,
|
|
3317
3789
|
parseCursorCatalogOutput,
|
|
@@ -3319,16 +3791,21 @@ export {
|
|
|
3319
3791
|
parseLoadSessionSupported,
|
|
3320
3792
|
parseMcpHttpSupported,
|
|
3321
3793
|
parsePromptCapabilities,
|
|
3794
|
+
parseRateLimitsFromStatusText,
|
|
3322
3795
|
parseResumeSessionSupported,
|
|
3323
3796
|
pickAllowOptionId,
|
|
3324
3797
|
probeAgyAuth,
|
|
3325
3798
|
probeClaudeAuth,
|
|
3799
|
+
probeClaudeQuota,
|
|
3326
3800
|
probeCodexAuth,
|
|
3801
|
+
probeCodexQuota,
|
|
3327
3802
|
probeCopilotAuth,
|
|
3328
3803
|
probeCursorAuth,
|
|
3804
|
+
probeCursorQuota,
|
|
3329
3805
|
probeDeepseekAuth,
|
|
3330
3806
|
probeGeminiAuth,
|
|
3331
3807
|
probeOpencodeAuth,
|
|
3808
|
+
probeOpencodeQuota,
|
|
3332
3809
|
provisionCustomProviderCodexHome,
|
|
3333
3810
|
readAcpProxySettings,
|
|
3334
3811
|
readStoredAcpProvider,
|
|
@@ -3345,6 +3822,7 @@ export {
|
|
|
3345
3822
|
resolveGeminiHome,
|
|
3346
3823
|
resolveOpencodeAuthCandidates,
|
|
3347
3824
|
resolveOpencodeAuthFile,
|
|
3825
|
+
resolveOpencodeAuthPath,
|
|
3348
3826
|
restoreOrCreateAcpSession,
|
|
3349
3827
|
runConnectAuthGate,
|
|
3350
3828
|
saveAcpProvider,
|