@yitom/acp-client 0.1.5 → 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/index.cjs +276 -13
- package/dist/index.js +247 -10
- package/dist/sidecar.cjs +250 -13
- 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)
|
|
@@ -337,7 +363,7 @@ function findBuiltinAcpRuntime(id) {
|
|
|
337
363
|
return BUILTIN_ACP_RUNTIMES.find((item) => item.id === id);
|
|
338
364
|
}
|
|
339
365
|
// src/version.ts
|
|
340
|
-
var ACP_CLIENT_VERSION = "0.1.
|
|
366
|
+
var ACP_CLIENT_VERSION = "0.1.6";
|
|
341
367
|
// src/session/available-commands.ts
|
|
342
368
|
function normalizeCommand(raw) {
|
|
343
369
|
if (!raw || typeof raw !== "object")
|
|
@@ -587,6 +613,8 @@ var import_sdk4 = require("@agentclientprotocol/sdk");
|
|
|
587
613
|
// src/runtimes/codex/quota.ts
|
|
588
614
|
var import_node_fs2 = require("node:fs");
|
|
589
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);
|
|
590
618
|
|
|
591
619
|
// src/runtimes/codex/codex-auth-preflight.ts
|
|
592
620
|
var import_node_fs = require("node:fs");
|
|
@@ -636,6 +664,101 @@ function decodeJwtPayload(token) {
|
|
|
636
664
|
return null;
|
|
637
665
|
}
|
|
638
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
|
+
}
|
|
639
762
|
async function probeCodexQuota(options) {
|
|
640
763
|
const codexHome = resolveCodexHome();
|
|
641
764
|
const authPath = import_node_path2.join(codexHome, "auth.json");
|
|
@@ -669,6 +792,14 @@ async function probeCodexQuota(options) {
|
|
|
669
792
|
isLoggedIn = true;
|
|
670
793
|
accountPlan = "API Key (Env)";
|
|
671
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
|
+
}
|
|
672
803
|
if (!isLoggedIn) {
|
|
673
804
|
return {
|
|
674
805
|
runtimeId: "codex-acp",
|
|
@@ -678,14 +809,49 @@ async function probeCodexQuota(options) {
|
|
|
678
809
|
updatedAt: Date.now()
|
|
679
810
|
};
|
|
680
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
|
+
}
|
|
681
844
|
return {
|
|
682
845
|
runtimeId: "codex-acp",
|
|
683
846
|
provider: "OpenAI / Codex",
|
|
684
847
|
account: {
|
|
685
848
|
email: accountEmail,
|
|
686
849
|
plan: accountPlan,
|
|
687
|
-
type: "chatgpt"
|
|
850
|
+
type: dynamic?.account?.type || "chatgpt"
|
|
688
851
|
},
|
|
852
|
+
primaryWindow,
|
|
853
|
+
secondaryWindow,
|
|
854
|
+
credits,
|
|
689
855
|
status: "ok",
|
|
690
856
|
updatedAt: Date.now()
|
|
691
857
|
};
|
|
@@ -861,6 +1027,30 @@ var codexAdapter = {
|
|
|
861
1027
|
var import_node_fs4 = require("node:fs");
|
|
862
1028
|
var import_node_os3 = require("node:os");
|
|
863
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
|
+
}
|
|
864
1054
|
async function probeClaudeQuota(options) {
|
|
865
1055
|
const claudeHome = resolveClaudeHome();
|
|
866
1056
|
const claudeJsonPath = import_node_path6.join(import_node_os3.homedir(), ".claude.json");
|
|
@@ -893,6 +1083,17 @@ async function probeClaudeQuota(options) {
|
|
|
893
1083
|
isLoggedIn = true;
|
|
894
1084
|
plan = "API Key (Env)";
|
|
895
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
|
+
}
|
|
896
1097
|
if (isBanned) {
|
|
897
1098
|
return {
|
|
898
1099
|
runtimeId: "claude",
|
|
@@ -916,7 +1117,8 @@ async function probeClaudeQuota(options) {
|
|
|
916
1117
|
provider: "Anthropic / Claude Code",
|
|
917
1118
|
account: {
|
|
918
1119
|
email: accountEmail,
|
|
919
|
-
plan
|
|
1120
|
+
plan,
|
|
1121
|
+
type: dynamic?.authMethod || "oauth"
|
|
920
1122
|
},
|
|
921
1123
|
status: "ok",
|
|
922
1124
|
updatedAt: Date.now()
|
|
@@ -1022,6 +1224,7 @@ var copilotAdapter = {
|
|
|
1022
1224
|
|
|
1023
1225
|
// src/runtimes/opencode/quota.ts
|
|
1024
1226
|
var import_node_fs8 = require("node:fs");
|
|
1227
|
+
var import_node_child_process3 = require("node:child_process");
|
|
1025
1228
|
function resolveOpencodeAuthPath() {
|
|
1026
1229
|
const candidates = resolveOpencodeAuthCandidates();
|
|
1027
1230
|
for (const c of candidates) {
|
|
@@ -1030,6 +1233,44 @@ function resolveOpencodeAuthPath() {
|
|
|
1030
1233
|
}
|
|
1031
1234
|
return candidates[0] ?? null;
|
|
1032
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
|
+
}
|
|
1033
1274
|
async function probeOpencodeQuota(runtimeId = "opencode") {
|
|
1034
1275
|
const authPath = resolveOpencodeAuthPath();
|
|
1035
1276
|
let isLoggedIn = false;
|
|
@@ -1050,22 +1291,44 @@ async function probeOpencodeQuota(runtimeId = "opencode") {
|
|
|
1050
1291
|
}
|
|
1051
1292
|
} catch {}
|
|
1052
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
|
+
}
|
|
1053
1308
|
if (!isLoggedIn) {
|
|
1054
1309
|
return {
|
|
1055
1310
|
runtimeId,
|
|
1056
1311
|
provider: "OpenCode",
|
|
1057
1312
|
status: "unauthorized",
|
|
1058
|
-
message: "
|
|
1313
|
+
message: "未登录 OpenCode 或未配置 Provider (No stored credentials)",
|
|
1059
1314
|
updatedAt: Date.now()
|
|
1060
1315
|
};
|
|
1061
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
|
+
}
|
|
1062
1323
|
return {
|
|
1063
1324
|
runtimeId,
|
|
1064
1325
|
provider: "OpenCode",
|
|
1065
1326
|
account: {
|
|
1066
1327
|
email: accountEmail,
|
|
1067
|
-
plan
|
|
1328
|
+
plan,
|
|
1329
|
+
type: "provider_oauth"
|
|
1068
1330
|
},
|
|
1331
|
+
credits,
|
|
1069
1332
|
status: "ok",
|
|
1070
1333
|
updatedAt: Date.now()
|
|
1071
1334
|
};
|
|
@@ -1201,7 +1464,7 @@ async function probeCursorQuota() {
|
|
|
1201
1464
|
}
|
|
1202
1465
|
|
|
1203
1466
|
// src/runtimes/cursor/index.ts
|
|
1204
|
-
var
|
|
1467
|
+
var import_node_child_process4 = require("node:child_process");
|
|
1205
1468
|
var import_node_fs10 = require("node:fs");
|
|
1206
1469
|
var import_node_os8 = require("node:os");
|
|
1207
1470
|
var import_node_path11 = require("node:path");
|
|
@@ -1446,7 +1709,7 @@ function getCursorCatalogIds(execFile, overrides) {
|
|
|
1446
1709
|
}
|
|
1447
1710
|
try {
|
|
1448
1711
|
const shell = command.toLowerCase().endsWith(".cmd");
|
|
1449
|
-
const result =
|
|
1712
|
+
const result = import_node_child_process4.spawnSync(command, ["--list-models"], {
|
|
1450
1713
|
encoding: "utf8",
|
|
1451
1714
|
timeout: 6000,
|
|
1452
1715
|
windowsHide: true,
|
|
@@ -1616,7 +1879,7 @@ function getAcpRuntimeAdapter(runtimeId) {
|
|
|
1616
1879
|
}
|
|
1617
1880
|
|
|
1618
1881
|
// src/process/process-manager.ts
|
|
1619
|
-
var
|
|
1882
|
+
var import_node_child_process5 = require("node:child_process");
|
|
1620
1883
|
var active = new Map;
|
|
1621
1884
|
var ACP_EARLY_EXIT_WINDOW_MS = 3000;
|
|
1622
1885
|
var ACP_EARLY_EXIT_STDERR_TAIL_LINES = 20;
|
|
@@ -1628,7 +1891,7 @@ function killProcessTree(pid) {
|
|
|
1628
1891
|
if (process.platform !== "win32")
|
|
1629
1892
|
return;
|
|
1630
1893
|
try {
|
|
1631
|
-
|
|
1894
|
+
import_node_child_process5.spawnSync("taskkill", ["/F", "/T", "/PID", String(pid)], {
|
|
1632
1895
|
windowsHide: true,
|
|
1633
1896
|
timeout: 8000
|
|
1634
1897
|
});
|
|
@@ -1671,7 +1934,7 @@ function spawnAcpProcess(options) {
|
|
|
1671
1934
|
} else {
|
|
1672
1935
|
delete env.NO_BROWSER;
|
|
1673
1936
|
}
|
|
1674
|
-
const child =
|
|
1937
|
+
const child = import_node_child_process5.spawn(file, options.runtime.args, {
|
|
1675
1938
|
cwd: options.cwd,
|
|
1676
1939
|
env,
|
|
1677
1940
|
stdio: ["pipe", "pipe", "pipe"],
|
|
@@ -2391,7 +2654,7 @@ async function restoreOrCreateAcpSession(input) {
|
|
|
2391
2654
|
}
|
|
2392
2655
|
|
|
2393
2656
|
// src/client/acp-terminal.ts
|
|
2394
|
-
var
|
|
2657
|
+
var import_node_child_process6 = require("node:child_process");
|
|
2395
2658
|
var import_node_crypto = require("node:crypto");
|
|
2396
2659
|
|
|
2397
2660
|
// src/transport/terminal-output-buffer.ts
|
|
@@ -2444,7 +2707,7 @@ class AcpTerminalManager {
|
|
|
2444
2707
|
throw new Error("terminal/create 需要 command");
|
|
2445
2708
|
const cwd = resolveCwd(params.cwd, params.workspaceRoot);
|
|
2446
2709
|
const outputByteLimit = typeof params.outputByteLimit === "number" && params.outputByteLimit > 0 ? params.outputByteLimit : DEFAULT_OUTPUT_BYTE_LIMIT;
|
|
2447
|
-
const child =
|
|
2710
|
+
const child = import_node_child_process6.spawn(command, params.args ?? [], {
|
|
2448
2711
|
cwd,
|
|
2449
2712
|
env: buildEnv(params.env),
|
|
2450
2713
|
windowsHide: true,
|
|
@@ -2540,7 +2803,7 @@ class AcpTerminalManager {
|
|
|
2540
2803
|
return;
|
|
2541
2804
|
if (process.platform === "win32") {
|
|
2542
2805
|
try {
|
|
2543
|
-
|
|
2806
|
+
import_node_child_process6.spawn("taskkill", ["/pid", String(pid), "/T", "/F"], {
|
|
2544
2807
|
windowsHide: true,
|
|
2545
2808
|
stdio: "ignore"
|
|
2546
2809
|
});
|
package/dist/index.js
CHANGED
|
@@ -159,7 +159,7 @@ function findBuiltinAcpRuntime(id) {
|
|
|
159
159
|
return BUILTIN_ACP_RUNTIMES.find((item) => item.id === id);
|
|
160
160
|
}
|
|
161
161
|
// src/version.ts
|
|
162
|
-
var ACP_CLIENT_VERSION = "0.1.
|
|
162
|
+
var ACP_CLIENT_VERSION = "0.1.6";
|
|
163
163
|
// src/session/available-commands.ts
|
|
164
164
|
function normalizeCommand(raw) {
|
|
165
165
|
if (!raw || typeof raw !== "object")
|
|
@@ -411,6 +411,8 @@ import {
|
|
|
411
411
|
// src/runtimes/codex/quota.ts
|
|
412
412
|
import { existsSync as existsSync2, readFileSync } from "node:fs";
|
|
413
413
|
import { join as join2 } from "node:path";
|
|
414
|
+
import { spawn } from "node:child_process";
|
|
415
|
+
import readline from "node:readline";
|
|
414
416
|
|
|
415
417
|
// src/runtimes/codex/codex-auth-preflight.ts
|
|
416
418
|
import { existsSync } from "node:fs";
|
|
@@ -460,6 +462,101 @@ function decodeJwtPayload(token) {
|
|
|
460
462
|
return null;
|
|
461
463
|
}
|
|
462
464
|
}
|
|
465
|
+
var cachedDynamicResult = null;
|
|
466
|
+
async function queryCodexAppServer(timeoutMs = 4000) {
|
|
467
|
+
if (cachedDynamicResult && Date.now() - cachedDynamicResult.time < 15000) {
|
|
468
|
+
return cachedDynamicResult.res;
|
|
469
|
+
}
|
|
470
|
+
return new Promise((resolve) => {
|
|
471
|
+
let timer = null;
|
|
472
|
+
let proc = null;
|
|
473
|
+
let finished = false;
|
|
474
|
+
const cleanup = (res) => {
|
|
475
|
+
if (finished)
|
|
476
|
+
return;
|
|
477
|
+
finished = true;
|
|
478
|
+
if (timer)
|
|
479
|
+
clearTimeout(timer);
|
|
480
|
+
if (proc) {
|
|
481
|
+
try {
|
|
482
|
+
proc.kill();
|
|
483
|
+
} catch {}
|
|
484
|
+
}
|
|
485
|
+
if (res) {
|
|
486
|
+
cachedDynamicResult = { res, time: Date.now() };
|
|
487
|
+
}
|
|
488
|
+
resolve(res);
|
|
489
|
+
};
|
|
490
|
+
timer = setTimeout(() => cleanup(null), timeoutMs);
|
|
491
|
+
try {
|
|
492
|
+
proc = spawn("codex", ["app-server"], {
|
|
493
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
494
|
+
shell: process.platform === "win32",
|
|
495
|
+
windowsHide: true
|
|
496
|
+
});
|
|
497
|
+
if (!proc.stdin || !proc.stdout) {
|
|
498
|
+
return cleanup(null);
|
|
499
|
+
}
|
|
500
|
+
const rl = readline.createInterface({ input: proc.stdout });
|
|
501
|
+
let reqSeq = 1;
|
|
502
|
+
const pending = new Map;
|
|
503
|
+
const result = {};
|
|
504
|
+
rl.on("line", (line) => {
|
|
505
|
+
try {
|
|
506
|
+
const data = JSON.parse(line.trim());
|
|
507
|
+
if (data && typeof data.id === "number" && pending.has(data.id)) {
|
|
508
|
+
const cb = pending.get(data.id);
|
|
509
|
+
pending.delete(data.id);
|
|
510
|
+
cb(data.result);
|
|
511
|
+
}
|
|
512
|
+
} catch {}
|
|
513
|
+
});
|
|
514
|
+
proc.on("error", () => cleanup(null));
|
|
515
|
+
proc.on("exit", () => cleanup(Object.keys(result).length > 0 ? result : null));
|
|
516
|
+
const send = (method, params) => {
|
|
517
|
+
const id = reqSeq++;
|
|
518
|
+
const msg = JSON.stringify({ jsonrpc: "2.0", id, method, params }) + `
|
|
519
|
+
`;
|
|
520
|
+
proc.stdin.write(msg);
|
|
521
|
+
return new Promise((res) => {
|
|
522
|
+
pending.set(id, res);
|
|
523
|
+
});
|
|
524
|
+
};
|
|
525
|
+
(async () => {
|
|
526
|
+
try {
|
|
527
|
+
await send("initialize", {
|
|
528
|
+
clientInfo: { name: "acp-client", version: "0.1.6" }
|
|
529
|
+
});
|
|
530
|
+
const acc = await send("account/read", { refreshToken: false });
|
|
531
|
+
if (acc?.account) {
|
|
532
|
+
result.account = acc.account;
|
|
533
|
+
}
|
|
534
|
+
const limits = await send("account/rateLimits/read");
|
|
535
|
+
if (limits?.rateLimits) {
|
|
536
|
+
result.rateLimits = limits.rateLimits;
|
|
537
|
+
}
|
|
538
|
+
cleanup(result);
|
|
539
|
+
} catch {
|
|
540
|
+
cleanup(null);
|
|
541
|
+
}
|
|
542
|
+
})();
|
|
543
|
+
} catch {
|
|
544
|
+
cleanup(null);
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
function formatEpochTime(epochSeconds) {
|
|
549
|
+
if (!epochSeconds)
|
|
550
|
+
return;
|
|
551
|
+
try {
|
|
552
|
+
const d = new Date(epochSeconds * 1000);
|
|
553
|
+
const hours = String(d.getHours()).padStart(2, "0");
|
|
554
|
+
const mins = String(d.getMinutes()).padStart(2, "0");
|
|
555
|
+
return `${hours}:${mins}`;
|
|
556
|
+
} catch {
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
463
560
|
async function probeCodexQuota(options) {
|
|
464
561
|
const codexHome = resolveCodexHome();
|
|
465
562
|
const authPath = join2(codexHome, "auth.json");
|
|
@@ -493,6 +590,14 @@ async function probeCodexQuota(options) {
|
|
|
493
590
|
isLoggedIn = true;
|
|
494
591
|
accountPlan = "API Key (Env)";
|
|
495
592
|
}
|
|
593
|
+
const dynamic = await queryCodexAppServer(4000);
|
|
594
|
+
if (dynamic) {
|
|
595
|
+
isLoggedIn = true;
|
|
596
|
+
if (dynamic.account?.email)
|
|
597
|
+
accountEmail = dynamic.account.email;
|
|
598
|
+
if (dynamic.account?.planType)
|
|
599
|
+
accountPlan = `ChatGPT ${dynamic.account.planType}`;
|
|
600
|
+
}
|
|
496
601
|
if (!isLoggedIn) {
|
|
497
602
|
return {
|
|
498
603
|
runtimeId: "codex-acp",
|
|
@@ -502,14 +607,49 @@ async function probeCodexQuota(options) {
|
|
|
502
607
|
updatedAt: Date.now()
|
|
503
608
|
};
|
|
504
609
|
}
|
|
610
|
+
let primaryWindow;
|
|
611
|
+
let secondaryWindow;
|
|
612
|
+
let credits;
|
|
613
|
+
if (dynamic?.rateLimits) {
|
|
614
|
+
const rl = dynamic.rateLimits;
|
|
615
|
+
if (rl.primary) {
|
|
616
|
+
const used = rl.primary.usedPercent ?? 0;
|
|
617
|
+
primaryWindow = {
|
|
618
|
+
label: "5h limit",
|
|
619
|
+
percentLeft: Math.max(0, Math.min(100, Math.round(100 - used))),
|
|
620
|
+
usedPercent: used,
|
|
621
|
+
windowDurationMins: rl.primary.windowDurationMins || 300,
|
|
622
|
+
resetsAt: formatEpochTime(rl.primary.resetsAt) || (rl.primary.resetsAt ? String(rl.primary.resetsAt) : undefined)
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
if (rl.secondary) {
|
|
626
|
+
const used = rl.secondary.usedPercent ?? 0;
|
|
627
|
+
secondaryWindow = {
|
|
628
|
+
label: "Weekly limit",
|
|
629
|
+
percentLeft: Math.max(0, Math.min(100, Math.round(100 - used))),
|
|
630
|
+
usedPercent: used,
|
|
631
|
+
windowDurationMins: rl.secondary.windowDurationMins || 10080,
|
|
632
|
+
resetsAt: formatEpochTime(rl.secondary.resetsAt) || (rl.secondary.resetsAt ? String(rl.secondary.resetsAt) : undefined)
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
if (rl.credits) {
|
|
636
|
+
credits = {
|
|
637
|
+
balance: rl.credits.balance,
|
|
638
|
+
unlimited: rl.credits.unlimited
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
}
|
|
505
642
|
return {
|
|
506
643
|
runtimeId: "codex-acp",
|
|
507
644
|
provider: "OpenAI / Codex",
|
|
508
645
|
account: {
|
|
509
646
|
email: accountEmail,
|
|
510
647
|
plan: accountPlan,
|
|
511
|
-
type: "chatgpt"
|
|
648
|
+
type: dynamic?.account?.type || "chatgpt"
|
|
512
649
|
},
|
|
650
|
+
primaryWindow,
|
|
651
|
+
secondaryWindow,
|
|
652
|
+
credits,
|
|
513
653
|
status: "ok",
|
|
514
654
|
updatedAt: Date.now()
|
|
515
655
|
};
|
|
@@ -685,6 +825,30 @@ var codexAdapter = {
|
|
|
685
825
|
import { existsSync as existsSync4, readFileSync as readFileSync2 } from "node:fs";
|
|
686
826
|
import { homedir as homedir3 } from "node:os";
|
|
687
827
|
import { join as join6 } from "node:path";
|
|
828
|
+
import { execFile } from "node:child_process";
|
|
829
|
+
var cachedClaudeAuth = null;
|
|
830
|
+
async function queryClaudeAuthStatus(timeoutMs = 1200) {
|
|
831
|
+
if (cachedClaudeAuth && Date.now() - cachedClaudeAuth.time < 30000) {
|
|
832
|
+
return cachedClaudeAuth.res;
|
|
833
|
+
}
|
|
834
|
+
return new Promise((resolve) => {
|
|
835
|
+
try {
|
|
836
|
+
execFile("claude", ["auth", "status", "--json"], { timeout: timeoutMs, windowsHide: true, shell: process.platform === "win32" }, (err, stdout) => {
|
|
837
|
+
if (!stdout)
|
|
838
|
+
return resolve(null);
|
|
839
|
+
try {
|
|
840
|
+
const data = JSON.parse(stdout.trim());
|
|
841
|
+
cachedClaudeAuth = { res: data, time: Date.now() };
|
|
842
|
+
resolve(data);
|
|
843
|
+
} catch {
|
|
844
|
+
resolve(null);
|
|
845
|
+
}
|
|
846
|
+
});
|
|
847
|
+
} catch {
|
|
848
|
+
resolve(null);
|
|
849
|
+
}
|
|
850
|
+
});
|
|
851
|
+
}
|
|
688
852
|
async function probeClaudeQuota(options) {
|
|
689
853
|
const claudeHome = resolveClaudeHome();
|
|
690
854
|
const claudeJsonPath = join6(homedir3(), ".claude.json");
|
|
@@ -717,6 +881,17 @@ async function probeClaudeQuota(options) {
|
|
|
717
881
|
isLoggedIn = true;
|
|
718
882
|
plan = "API Key (Env)";
|
|
719
883
|
}
|
|
884
|
+
const dynamic = await queryClaudeAuthStatus(1200);
|
|
885
|
+
if (dynamic) {
|
|
886
|
+
if (dynamic.loggedIn)
|
|
887
|
+
isLoggedIn = true;
|
|
888
|
+
if (dynamic.email)
|
|
889
|
+
accountEmail = dynamic.email;
|
|
890
|
+
if (dynamic.subscriptionType)
|
|
891
|
+
plan = dynamic.subscriptionType;
|
|
892
|
+
else if (dynamic.plan)
|
|
893
|
+
plan = dynamic.plan;
|
|
894
|
+
}
|
|
720
895
|
if (isBanned) {
|
|
721
896
|
return {
|
|
722
897
|
runtimeId: "claude",
|
|
@@ -740,7 +915,8 @@ async function probeClaudeQuota(options) {
|
|
|
740
915
|
provider: "Anthropic / Claude Code",
|
|
741
916
|
account: {
|
|
742
917
|
email: accountEmail,
|
|
743
|
-
plan
|
|
918
|
+
plan,
|
|
919
|
+
type: dynamic?.authMethod || "oauth"
|
|
744
920
|
},
|
|
745
921
|
status: "ok",
|
|
746
922
|
updatedAt: Date.now()
|
|
@@ -846,6 +1022,7 @@ var copilotAdapter = {
|
|
|
846
1022
|
|
|
847
1023
|
// src/runtimes/opencode/quota.ts
|
|
848
1024
|
import { existsSync as existsSync8, readFileSync as readFileSync4 } from "node:fs";
|
|
1025
|
+
import { execFile as execFile2 } from "node:child_process";
|
|
849
1026
|
function resolveOpencodeAuthPath() {
|
|
850
1027
|
const candidates = resolveOpencodeAuthCandidates();
|
|
851
1028
|
for (const c of candidates) {
|
|
@@ -854,6 +1031,44 @@ function resolveOpencodeAuthPath() {
|
|
|
854
1031
|
}
|
|
855
1032
|
return candidates[0] ?? null;
|
|
856
1033
|
}
|
|
1034
|
+
var cachedStats = null;
|
|
1035
|
+
async function queryOpencodeStats(timeoutMs = 1200) {
|
|
1036
|
+
if (cachedStats && Date.now() - cachedStats.time < 30000) {
|
|
1037
|
+
return cachedStats.res;
|
|
1038
|
+
}
|
|
1039
|
+
return new Promise((resolve) => {
|
|
1040
|
+
try {
|
|
1041
|
+
execFile2("opencode", ["stats", "--standalone", "--cost", "--json"], { timeout: timeoutMs, windowsHide: true }, (err, stdout) => {
|
|
1042
|
+
if (!stdout)
|
|
1043
|
+
return resolve(null);
|
|
1044
|
+
try {
|
|
1045
|
+
const data = JSON.parse(stdout.trim());
|
|
1046
|
+
cachedStats = { res: data, time: Date.now() };
|
|
1047
|
+
resolve(data);
|
|
1048
|
+
} catch {
|
|
1049
|
+
resolve(null);
|
|
1050
|
+
}
|
|
1051
|
+
});
|
|
1052
|
+
} catch {
|
|
1053
|
+
resolve(null);
|
|
1054
|
+
}
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
async function queryOpencodeAuthList(timeoutMs = 1200) {
|
|
1058
|
+
return new Promise((resolve) => {
|
|
1059
|
+
try {
|
|
1060
|
+
execFile2("opencode", ["auth", "list"], { timeout: timeoutMs, windowsHide: true }, (err, stdout) => {
|
|
1061
|
+
if (!stdout)
|
|
1062
|
+
return resolve([]);
|
|
1063
|
+
const lines = stdout.split(`
|
|
1064
|
+
`).map((l) => l.trim()).filter((l) => l.includes("stored"));
|
|
1065
|
+
resolve(lines);
|
|
1066
|
+
});
|
|
1067
|
+
} catch {
|
|
1068
|
+
resolve([]);
|
|
1069
|
+
}
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
857
1072
|
async function probeOpencodeQuota(runtimeId = "opencode") {
|
|
858
1073
|
const authPath = resolveOpencodeAuthPath();
|
|
859
1074
|
let isLoggedIn = false;
|
|
@@ -874,22 +1089,44 @@ async function probeOpencodeQuota(runtimeId = "opencode") {
|
|
|
874
1089
|
}
|
|
875
1090
|
} catch {}
|
|
876
1091
|
}
|
|
1092
|
+
const [stats, authList] = await Promise.all([
|
|
1093
|
+
queryOpencodeStats(1200),
|
|
1094
|
+
queryOpencodeAuthList(1200)
|
|
1095
|
+
]);
|
|
1096
|
+
if (authList.length > 0) {
|
|
1097
|
+
isLoggedIn = true;
|
|
1098
|
+
const providers = authList.map((l) => l.split(/\s{2,}/)[0]).filter(Boolean);
|
|
1099
|
+
if (providers.length > 0) {
|
|
1100
|
+
plan = `OpenCode (${providers.join(", ")})`;
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
if (process.env.OPENCODE_API_KEY || process.env.OPENAI_API_KEY) {
|
|
1104
|
+
isLoggedIn = true;
|
|
1105
|
+
}
|
|
877
1106
|
if (!isLoggedIn) {
|
|
878
1107
|
return {
|
|
879
1108
|
runtimeId,
|
|
880
1109
|
provider: "OpenCode",
|
|
881
1110
|
status: "unauthorized",
|
|
882
|
-
message: "
|
|
1111
|
+
message: "未登录 OpenCode 或未配置 Provider (No stored credentials)",
|
|
883
1112
|
updatedAt: Date.now()
|
|
884
1113
|
};
|
|
885
1114
|
}
|
|
1115
|
+
let credits;
|
|
1116
|
+
if (stats && typeof stats.cost === "number") {
|
|
1117
|
+
credits = {
|
|
1118
|
+
balance: stats.cost > 0 ? `$${stats.cost.toFixed(2)} spent` : "0.00"
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
886
1121
|
return {
|
|
887
1122
|
runtimeId,
|
|
888
1123
|
provider: "OpenCode",
|
|
889
1124
|
account: {
|
|
890
1125
|
email: accountEmail,
|
|
891
|
-
plan
|
|
1126
|
+
plan,
|
|
1127
|
+
type: "provider_oauth"
|
|
892
1128
|
},
|
|
1129
|
+
credits,
|
|
893
1130
|
status: "ok",
|
|
894
1131
|
updatedAt: Date.now()
|
|
895
1132
|
};
|
|
@@ -1440,7 +1677,7 @@ function getAcpRuntimeAdapter(runtimeId) {
|
|
|
1440
1677
|
}
|
|
1441
1678
|
|
|
1442
1679
|
// src/process/process-manager.ts
|
|
1443
|
-
import { spawn, spawnSync as spawnSync2 } from "node:child_process";
|
|
1680
|
+
import { spawn as spawn2, spawnSync as spawnSync2 } from "node:child_process";
|
|
1444
1681
|
var active = new Map;
|
|
1445
1682
|
var ACP_EARLY_EXIT_WINDOW_MS = 3000;
|
|
1446
1683
|
var ACP_EARLY_EXIT_STDERR_TAIL_LINES = 20;
|
|
@@ -1495,7 +1732,7 @@ function spawnAcpProcess(options) {
|
|
|
1495
1732
|
} else {
|
|
1496
1733
|
delete env.NO_BROWSER;
|
|
1497
1734
|
}
|
|
1498
|
-
const child =
|
|
1735
|
+
const child = spawn2(file, options.runtime.args, {
|
|
1499
1736
|
cwd: options.cwd,
|
|
1500
1737
|
env,
|
|
1501
1738
|
stdio: ["pipe", "pipe", "pipe"],
|
|
@@ -2218,7 +2455,7 @@ async function restoreOrCreateAcpSession(input) {
|
|
|
2218
2455
|
}
|
|
2219
2456
|
|
|
2220
2457
|
// src/client/acp-terminal.ts
|
|
2221
|
-
import { spawn as
|
|
2458
|
+
import { spawn as spawn3 } from "node:child_process";
|
|
2222
2459
|
import { randomUUID } from "node:crypto";
|
|
2223
2460
|
|
|
2224
2461
|
// src/transport/terminal-output-buffer.ts
|
|
@@ -2271,7 +2508,7 @@ class AcpTerminalManager {
|
|
|
2271
2508
|
throw new Error("terminal/create 需要 command");
|
|
2272
2509
|
const cwd = resolveCwd(params.cwd, params.workspaceRoot);
|
|
2273
2510
|
const outputByteLimit = typeof params.outputByteLimit === "number" && params.outputByteLimit > 0 ? params.outputByteLimit : DEFAULT_OUTPUT_BYTE_LIMIT;
|
|
2274
|
-
const child =
|
|
2511
|
+
const child = spawn3(command, params.args ?? [], {
|
|
2275
2512
|
cwd,
|
|
2276
2513
|
env: buildEnv(params.env),
|
|
2277
2514
|
windowsHide: true,
|
|
@@ -2367,7 +2604,7 @@ class AcpTerminalManager {
|
|
|
2367
2604
|
return;
|
|
2368
2605
|
if (process.platform === "win32") {
|
|
2369
2606
|
try {
|
|
2370
|
-
|
|
2607
|
+
spawn3("taskkill", ["/pid", String(pid), "/T", "/F"], {
|
|
2371
2608
|
windowsHide: true,
|
|
2372
2609
|
stdio: "ignore"
|
|
2373
2610
|
});
|
package/dist/sidecar.cjs
CHANGED
|
@@ -33,10 +33,10 @@ var __toESM = (mod, isNodeMode, target) => {
|
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
// src/sidecar/main.ts
|
|
36
|
-
var
|
|
36
|
+
var readline2 = __toESM(require("node:readline"), 1);
|
|
37
37
|
|
|
38
38
|
// src/version.ts
|
|
39
|
-
var ACP_CLIENT_VERSION = "0.1.
|
|
39
|
+
var ACP_CLIENT_VERSION = "0.1.6";
|
|
40
40
|
// src/contracts/index.ts
|
|
41
41
|
function ok(value) {
|
|
42
42
|
return { ok: true, value };
|
|
@@ -10300,6 +10300,8 @@ var legacyClientNotificationMethods = new Set([
|
|
|
10300
10300
|
// src/runtimes/codex/quota.ts
|
|
10301
10301
|
var import_node_fs2 = require("node:fs");
|
|
10302
10302
|
var import_node_path2 = require("node:path");
|
|
10303
|
+
var import_node_child_process = require("node:child_process");
|
|
10304
|
+
var import_node_readline = __toESM(require("node:readline"), 1);
|
|
10303
10305
|
|
|
10304
10306
|
// src/runtimes/codex/codex-auth-preflight.ts
|
|
10305
10307
|
var import_node_fs = require("node:fs");
|
|
@@ -10346,6 +10348,101 @@ function decodeJwtPayload(token) {
|
|
|
10346
10348
|
return null;
|
|
10347
10349
|
}
|
|
10348
10350
|
}
|
|
10351
|
+
var cachedDynamicResult = null;
|
|
10352
|
+
async function queryCodexAppServer(timeoutMs = 4000) {
|
|
10353
|
+
if (cachedDynamicResult && Date.now() - cachedDynamicResult.time < 15000) {
|
|
10354
|
+
return cachedDynamicResult.res;
|
|
10355
|
+
}
|
|
10356
|
+
return new Promise((resolve) => {
|
|
10357
|
+
let timer = null;
|
|
10358
|
+
let proc = null;
|
|
10359
|
+
let finished = false;
|
|
10360
|
+
const cleanup = (res) => {
|
|
10361
|
+
if (finished)
|
|
10362
|
+
return;
|
|
10363
|
+
finished = true;
|
|
10364
|
+
if (timer)
|
|
10365
|
+
clearTimeout(timer);
|
|
10366
|
+
if (proc) {
|
|
10367
|
+
try {
|
|
10368
|
+
proc.kill();
|
|
10369
|
+
} catch {}
|
|
10370
|
+
}
|
|
10371
|
+
if (res) {
|
|
10372
|
+
cachedDynamicResult = { res, time: Date.now() };
|
|
10373
|
+
}
|
|
10374
|
+
resolve(res);
|
|
10375
|
+
};
|
|
10376
|
+
timer = setTimeout(() => cleanup(null), timeoutMs);
|
|
10377
|
+
try {
|
|
10378
|
+
proc = import_node_child_process.spawn("codex", ["app-server"], {
|
|
10379
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
10380
|
+
shell: process.platform === "win32",
|
|
10381
|
+
windowsHide: true
|
|
10382
|
+
});
|
|
10383
|
+
if (!proc.stdin || !proc.stdout) {
|
|
10384
|
+
return cleanup(null);
|
|
10385
|
+
}
|
|
10386
|
+
const rl = import_node_readline.default.createInterface({ input: proc.stdout });
|
|
10387
|
+
let reqSeq = 1;
|
|
10388
|
+
const pending = new Map;
|
|
10389
|
+
const result = {};
|
|
10390
|
+
rl.on("line", (line) => {
|
|
10391
|
+
try {
|
|
10392
|
+
const data = JSON.parse(line.trim());
|
|
10393
|
+
if (data && typeof data.id === "number" && pending.has(data.id)) {
|
|
10394
|
+
const cb = pending.get(data.id);
|
|
10395
|
+
pending.delete(data.id);
|
|
10396
|
+
cb(data.result);
|
|
10397
|
+
}
|
|
10398
|
+
} catch {}
|
|
10399
|
+
});
|
|
10400
|
+
proc.on("error", () => cleanup(null));
|
|
10401
|
+
proc.on("exit", () => cleanup(Object.keys(result).length > 0 ? result : null));
|
|
10402
|
+
const send = (method, params) => {
|
|
10403
|
+
const id = reqSeq++;
|
|
10404
|
+
const msg = JSON.stringify({ jsonrpc: "2.0", id, method, params }) + `
|
|
10405
|
+
`;
|
|
10406
|
+
proc.stdin.write(msg);
|
|
10407
|
+
return new Promise((res) => {
|
|
10408
|
+
pending.set(id, res);
|
|
10409
|
+
});
|
|
10410
|
+
};
|
|
10411
|
+
(async () => {
|
|
10412
|
+
try {
|
|
10413
|
+
await send("initialize", {
|
|
10414
|
+
clientInfo: { name: "acp-client", version: "0.1.6" }
|
|
10415
|
+
});
|
|
10416
|
+
const acc = await send("account/read", { refreshToken: false });
|
|
10417
|
+
if (acc?.account) {
|
|
10418
|
+
result.account = acc.account;
|
|
10419
|
+
}
|
|
10420
|
+
const limits = await send("account/rateLimits/read");
|
|
10421
|
+
if (limits?.rateLimits) {
|
|
10422
|
+
result.rateLimits = limits.rateLimits;
|
|
10423
|
+
}
|
|
10424
|
+
cleanup(result);
|
|
10425
|
+
} catch {
|
|
10426
|
+
cleanup(null);
|
|
10427
|
+
}
|
|
10428
|
+
})();
|
|
10429
|
+
} catch {
|
|
10430
|
+
cleanup(null);
|
|
10431
|
+
}
|
|
10432
|
+
});
|
|
10433
|
+
}
|
|
10434
|
+
function formatEpochTime(epochSeconds) {
|
|
10435
|
+
if (!epochSeconds)
|
|
10436
|
+
return;
|
|
10437
|
+
try {
|
|
10438
|
+
const d = new Date(epochSeconds * 1000);
|
|
10439
|
+
const hours = String(d.getHours()).padStart(2, "0");
|
|
10440
|
+
const mins = String(d.getMinutes()).padStart(2, "0");
|
|
10441
|
+
return `${hours}:${mins}`;
|
|
10442
|
+
} catch {
|
|
10443
|
+
return;
|
|
10444
|
+
}
|
|
10445
|
+
}
|
|
10349
10446
|
async function probeCodexQuota(options) {
|
|
10350
10447
|
const codexHome = resolveCodexHome();
|
|
10351
10448
|
const authPath = import_node_path2.join(codexHome, "auth.json");
|
|
@@ -10379,6 +10476,14 @@ async function probeCodexQuota(options) {
|
|
|
10379
10476
|
isLoggedIn = true;
|
|
10380
10477
|
accountPlan = "API Key (Env)";
|
|
10381
10478
|
}
|
|
10479
|
+
const dynamic = await queryCodexAppServer(4000);
|
|
10480
|
+
if (dynamic) {
|
|
10481
|
+
isLoggedIn = true;
|
|
10482
|
+
if (dynamic.account?.email)
|
|
10483
|
+
accountEmail = dynamic.account.email;
|
|
10484
|
+
if (dynamic.account?.planType)
|
|
10485
|
+
accountPlan = `ChatGPT ${dynamic.account.planType}`;
|
|
10486
|
+
}
|
|
10382
10487
|
if (!isLoggedIn) {
|
|
10383
10488
|
return {
|
|
10384
10489
|
runtimeId: "codex-acp",
|
|
@@ -10388,14 +10493,49 @@ async function probeCodexQuota(options) {
|
|
|
10388
10493
|
updatedAt: Date.now()
|
|
10389
10494
|
};
|
|
10390
10495
|
}
|
|
10496
|
+
let primaryWindow;
|
|
10497
|
+
let secondaryWindow;
|
|
10498
|
+
let credits;
|
|
10499
|
+
if (dynamic?.rateLimits) {
|
|
10500
|
+
const rl = dynamic.rateLimits;
|
|
10501
|
+
if (rl.primary) {
|
|
10502
|
+
const used = rl.primary.usedPercent ?? 0;
|
|
10503
|
+
primaryWindow = {
|
|
10504
|
+
label: "5h limit",
|
|
10505
|
+
percentLeft: Math.max(0, Math.min(100, Math.round(100 - used))),
|
|
10506
|
+
usedPercent: used,
|
|
10507
|
+
windowDurationMins: rl.primary.windowDurationMins || 300,
|
|
10508
|
+
resetsAt: formatEpochTime(rl.primary.resetsAt) || (rl.primary.resetsAt ? String(rl.primary.resetsAt) : undefined)
|
|
10509
|
+
};
|
|
10510
|
+
}
|
|
10511
|
+
if (rl.secondary) {
|
|
10512
|
+
const used = rl.secondary.usedPercent ?? 0;
|
|
10513
|
+
secondaryWindow = {
|
|
10514
|
+
label: "Weekly limit",
|
|
10515
|
+
percentLeft: Math.max(0, Math.min(100, Math.round(100 - used))),
|
|
10516
|
+
usedPercent: used,
|
|
10517
|
+
windowDurationMins: rl.secondary.windowDurationMins || 10080,
|
|
10518
|
+
resetsAt: formatEpochTime(rl.secondary.resetsAt) || (rl.secondary.resetsAt ? String(rl.secondary.resetsAt) : undefined)
|
|
10519
|
+
};
|
|
10520
|
+
}
|
|
10521
|
+
if (rl.credits) {
|
|
10522
|
+
credits = {
|
|
10523
|
+
balance: rl.credits.balance,
|
|
10524
|
+
unlimited: rl.credits.unlimited
|
|
10525
|
+
};
|
|
10526
|
+
}
|
|
10527
|
+
}
|
|
10391
10528
|
return {
|
|
10392
10529
|
runtimeId: "codex-acp",
|
|
10393
10530
|
provider: "OpenAI / Codex",
|
|
10394
10531
|
account: {
|
|
10395
10532
|
email: accountEmail,
|
|
10396
10533
|
plan: accountPlan,
|
|
10397
|
-
type: "chatgpt"
|
|
10534
|
+
type: dynamic?.account?.type || "chatgpt"
|
|
10398
10535
|
},
|
|
10536
|
+
primaryWindow,
|
|
10537
|
+
secondaryWindow,
|
|
10538
|
+
credits,
|
|
10399
10539
|
status: "ok",
|
|
10400
10540
|
updatedAt: Date.now()
|
|
10401
10541
|
};
|
|
@@ -10523,6 +10663,30 @@ var codexAdapter = {
|
|
|
10523
10663
|
var import_node_fs4 = require("node:fs");
|
|
10524
10664
|
var import_node_os3 = require("node:os");
|
|
10525
10665
|
var import_node_path6 = require("node:path");
|
|
10666
|
+
var import_node_child_process2 = require("node:child_process");
|
|
10667
|
+
var cachedClaudeAuth = null;
|
|
10668
|
+
async function queryClaudeAuthStatus(timeoutMs = 1200) {
|
|
10669
|
+
if (cachedClaudeAuth && Date.now() - cachedClaudeAuth.time < 30000) {
|
|
10670
|
+
return cachedClaudeAuth.res;
|
|
10671
|
+
}
|
|
10672
|
+
return new Promise((resolve) => {
|
|
10673
|
+
try {
|
|
10674
|
+
import_node_child_process2.execFile("claude", ["auth", "status", "--json"], { timeout: timeoutMs, windowsHide: true, shell: process.platform === "win32" }, (err, stdout) => {
|
|
10675
|
+
if (!stdout)
|
|
10676
|
+
return resolve(null);
|
|
10677
|
+
try {
|
|
10678
|
+
const data = JSON.parse(stdout.trim());
|
|
10679
|
+
cachedClaudeAuth = { res: data, time: Date.now() };
|
|
10680
|
+
resolve(data);
|
|
10681
|
+
} catch {
|
|
10682
|
+
resolve(null);
|
|
10683
|
+
}
|
|
10684
|
+
});
|
|
10685
|
+
} catch {
|
|
10686
|
+
resolve(null);
|
|
10687
|
+
}
|
|
10688
|
+
});
|
|
10689
|
+
}
|
|
10526
10690
|
async function probeClaudeQuota(options) {
|
|
10527
10691
|
const claudeHome = resolveClaudeHome();
|
|
10528
10692
|
const claudeJsonPath = import_node_path6.join(import_node_os3.homedir(), ".claude.json");
|
|
@@ -10555,6 +10719,17 @@ async function probeClaudeQuota(options) {
|
|
|
10555
10719
|
isLoggedIn = true;
|
|
10556
10720
|
plan = "API Key (Env)";
|
|
10557
10721
|
}
|
|
10722
|
+
const dynamic = await queryClaudeAuthStatus(1200);
|
|
10723
|
+
if (dynamic) {
|
|
10724
|
+
if (dynamic.loggedIn)
|
|
10725
|
+
isLoggedIn = true;
|
|
10726
|
+
if (dynamic.email)
|
|
10727
|
+
accountEmail = dynamic.email;
|
|
10728
|
+
if (dynamic.subscriptionType)
|
|
10729
|
+
plan = dynamic.subscriptionType;
|
|
10730
|
+
else if (dynamic.plan)
|
|
10731
|
+
plan = dynamic.plan;
|
|
10732
|
+
}
|
|
10558
10733
|
if (isBanned) {
|
|
10559
10734
|
return {
|
|
10560
10735
|
runtimeId: "claude",
|
|
@@ -10578,7 +10753,8 @@ async function probeClaudeQuota(options) {
|
|
|
10578
10753
|
provider: "Anthropic / Claude Code",
|
|
10579
10754
|
account: {
|
|
10580
10755
|
email: accountEmail,
|
|
10581
|
-
plan
|
|
10756
|
+
plan,
|
|
10757
|
+
type: dynamic?.authMethod || "oauth"
|
|
10582
10758
|
},
|
|
10583
10759
|
status: "ok",
|
|
10584
10760
|
updatedAt: Date.now()
|
|
@@ -10684,6 +10860,7 @@ var copilotAdapter = {
|
|
|
10684
10860
|
|
|
10685
10861
|
// src/runtimes/opencode/quota.ts
|
|
10686
10862
|
var import_node_fs8 = require("node:fs");
|
|
10863
|
+
var import_node_child_process3 = require("node:child_process");
|
|
10687
10864
|
function resolveOpencodeAuthPath() {
|
|
10688
10865
|
const candidates = resolveOpencodeAuthCandidates();
|
|
10689
10866
|
for (const c of candidates) {
|
|
@@ -10692,6 +10869,44 @@ function resolveOpencodeAuthPath() {
|
|
|
10692
10869
|
}
|
|
10693
10870
|
return candidates[0] ?? null;
|
|
10694
10871
|
}
|
|
10872
|
+
var cachedStats = null;
|
|
10873
|
+
async function queryOpencodeStats(timeoutMs = 1200) {
|
|
10874
|
+
if (cachedStats && Date.now() - cachedStats.time < 30000) {
|
|
10875
|
+
return cachedStats.res;
|
|
10876
|
+
}
|
|
10877
|
+
return new Promise((resolve) => {
|
|
10878
|
+
try {
|
|
10879
|
+
import_node_child_process3.execFile("opencode", ["stats", "--standalone", "--cost", "--json"], { timeout: timeoutMs, windowsHide: true }, (err, stdout) => {
|
|
10880
|
+
if (!stdout)
|
|
10881
|
+
return resolve(null);
|
|
10882
|
+
try {
|
|
10883
|
+
const data = JSON.parse(stdout.trim());
|
|
10884
|
+
cachedStats = { res: data, time: Date.now() };
|
|
10885
|
+
resolve(data);
|
|
10886
|
+
} catch {
|
|
10887
|
+
resolve(null);
|
|
10888
|
+
}
|
|
10889
|
+
});
|
|
10890
|
+
} catch {
|
|
10891
|
+
resolve(null);
|
|
10892
|
+
}
|
|
10893
|
+
});
|
|
10894
|
+
}
|
|
10895
|
+
async function queryOpencodeAuthList(timeoutMs = 1200) {
|
|
10896
|
+
return new Promise((resolve) => {
|
|
10897
|
+
try {
|
|
10898
|
+
import_node_child_process3.execFile("opencode", ["auth", "list"], { timeout: timeoutMs, windowsHide: true }, (err, stdout) => {
|
|
10899
|
+
if (!stdout)
|
|
10900
|
+
return resolve([]);
|
|
10901
|
+
const lines = stdout.split(`
|
|
10902
|
+
`).map((l) => l.trim()).filter((l) => l.includes("stored"));
|
|
10903
|
+
resolve(lines);
|
|
10904
|
+
});
|
|
10905
|
+
} catch {
|
|
10906
|
+
resolve([]);
|
|
10907
|
+
}
|
|
10908
|
+
});
|
|
10909
|
+
}
|
|
10695
10910
|
async function probeOpencodeQuota(runtimeId = "opencode") {
|
|
10696
10911
|
const authPath = resolveOpencodeAuthPath();
|
|
10697
10912
|
let isLoggedIn = false;
|
|
@@ -10712,22 +10927,44 @@ async function probeOpencodeQuota(runtimeId = "opencode") {
|
|
|
10712
10927
|
}
|
|
10713
10928
|
} catch {}
|
|
10714
10929
|
}
|
|
10930
|
+
const [stats, authList] = await Promise.all([
|
|
10931
|
+
queryOpencodeStats(1200),
|
|
10932
|
+
queryOpencodeAuthList(1200)
|
|
10933
|
+
]);
|
|
10934
|
+
if (authList.length > 0) {
|
|
10935
|
+
isLoggedIn = true;
|
|
10936
|
+
const providers = authList.map((l) => l.split(/\s{2,}/)[0]).filter(Boolean);
|
|
10937
|
+
if (providers.length > 0) {
|
|
10938
|
+
plan = `OpenCode (${providers.join(", ")})`;
|
|
10939
|
+
}
|
|
10940
|
+
}
|
|
10941
|
+
if (process.env.OPENCODE_API_KEY || process.env.OPENAI_API_KEY) {
|
|
10942
|
+
isLoggedIn = true;
|
|
10943
|
+
}
|
|
10715
10944
|
if (!isLoggedIn) {
|
|
10716
10945
|
return {
|
|
10717
10946
|
runtimeId,
|
|
10718
10947
|
provider: "OpenCode",
|
|
10719
10948
|
status: "unauthorized",
|
|
10720
|
-
message: "
|
|
10949
|
+
message: "未登录 OpenCode 或未配置 Provider (No stored credentials)",
|
|
10721
10950
|
updatedAt: Date.now()
|
|
10722
10951
|
};
|
|
10723
10952
|
}
|
|
10953
|
+
let credits;
|
|
10954
|
+
if (stats && typeof stats.cost === "number") {
|
|
10955
|
+
credits = {
|
|
10956
|
+
balance: stats.cost > 0 ? `$${stats.cost.toFixed(2)} spent` : "0.00"
|
|
10957
|
+
};
|
|
10958
|
+
}
|
|
10724
10959
|
return {
|
|
10725
10960
|
runtimeId,
|
|
10726
10961
|
provider: "OpenCode",
|
|
10727
10962
|
account: {
|
|
10728
10963
|
email: accountEmail,
|
|
10729
|
-
plan
|
|
10964
|
+
plan,
|
|
10965
|
+
type: "provider_oauth"
|
|
10730
10966
|
},
|
|
10967
|
+
credits,
|
|
10731
10968
|
status: "ok",
|
|
10732
10969
|
updatedAt: Date.now()
|
|
10733
10970
|
};
|
|
@@ -11186,7 +11423,7 @@ function getAcpRuntimeAdapter(runtimeId) {
|
|
|
11186
11423
|
}
|
|
11187
11424
|
|
|
11188
11425
|
// src/process/process-manager.ts
|
|
11189
|
-
var
|
|
11426
|
+
var import_node_child_process4 = require("node:child_process");
|
|
11190
11427
|
var active = new Map;
|
|
11191
11428
|
var ACP_EARLY_EXIT_STDERR_TAIL_LINES = 20;
|
|
11192
11429
|
function isSpawnedAcpProcessAlive(handle) {
|
|
@@ -11197,7 +11434,7 @@ function killProcessTree(pid) {
|
|
|
11197
11434
|
if (process.platform !== "win32")
|
|
11198
11435
|
return;
|
|
11199
11436
|
try {
|
|
11200
|
-
|
|
11437
|
+
import_node_child_process4.spawnSync("taskkill", ["/F", "/T", "/PID", String(pid)], {
|
|
11201
11438
|
windowsHide: true,
|
|
11202
11439
|
timeout: 8000
|
|
11203
11440
|
});
|
|
@@ -11240,7 +11477,7 @@ function spawnAcpProcess(options) {
|
|
|
11240
11477
|
} else {
|
|
11241
11478
|
delete env.NO_BROWSER;
|
|
11242
11479
|
}
|
|
11243
|
-
const child =
|
|
11480
|
+
const child = import_node_child_process4.spawn(file, options.runtime.args, {
|
|
11244
11481
|
cwd: options.cwd,
|
|
11245
11482
|
env,
|
|
11246
11483
|
stdio: ["pipe", "pipe", "pipe"],
|
|
@@ -11877,7 +12114,7 @@ async function restoreOrCreateAcpSession(input) {
|
|
|
11877
12114
|
}
|
|
11878
12115
|
|
|
11879
12116
|
// src/client/acp-terminal.ts
|
|
11880
|
-
var
|
|
12117
|
+
var import_node_child_process5 = require("node:child_process");
|
|
11881
12118
|
var import_node_crypto = require("node:crypto");
|
|
11882
12119
|
|
|
11883
12120
|
// src/transport/terminal-output-buffer.ts
|
|
@@ -11930,7 +12167,7 @@ class AcpTerminalManager {
|
|
|
11930
12167
|
throw new Error("terminal/create 需要 command");
|
|
11931
12168
|
const cwd = resolveCwd(params.cwd, params.workspaceRoot);
|
|
11932
12169
|
const outputByteLimit = typeof params.outputByteLimit === "number" && params.outputByteLimit > 0 ? params.outputByteLimit : DEFAULT_OUTPUT_BYTE_LIMIT;
|
|
11933
|
-
const child =
|
|
12170
|
+
const child = import_node_child_process5.spawn(command, params.args ?? [], {
|
|
11934
12171
|
cwd,
|
|
11935
12172
|
env: buildEnv(params.env),
|
|
11936
12173
|
windowsHide: true,
|
|
@@ -12026,7 +12263,7 @@ class AcpTerminalManager {
|
|
|
12026
12263
|
return;
|
|
12027
12264
|
if (process.platform === "win32") {
|
|
12028
12265
|
try {
|
|
12029
|
-
|
|
12266
|
+
import_node_child_process5.spawn("taskkill", ["/pid", String(pid), "/T", "/F"], {
|
|
12030
12267
|
windowsHide: true,
|
|
12031
12268
|
stdio: "ignore"
|
|
12032
12269
|
});
|
|
@@ -13212,7 +13449,7 @@ async function handleRpcRequest(id, method, params) {
|
|
|
13212
13449
|
}
|
|
13213
13450
|
}
|
|
13214
13451
|
function startSidecar() {
|
|
13215
|
-
const rl =
|
|
13452
|
+
const rl = readline2.createInterface({
|
|
13216
13453
|
input: process.stdin,
|
|
13217
13454
|
terminal: false
|
|
13218
13455
|
});
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const ACP_CLIENT_VERSION = "0.1.
|
|
1
|
+
export declare const ACP_CLIENT_VERSION = "0.1.6";
|
package/package.json
CHANGED