echo-t3-bridge 0.1.3 → 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/cli.mjs +415 -259
- package/dist/tsc/bridge.js +132 -6
- package/dist/tsc/config.js +1 -1
- package/dist/tsc/index.js +40 -19
- package/dist/tsc/local-t3.js +51 -17
- package/dist/tsc/proxy.js +7 -1
- package/package.json +1 -1
- package/scripts/service.mjs +47 -6
package/dist/cli.mjs
CHANGED
|
@@ -5,235 +5,33 @@ import { spawnSync } from "node:child_process";
|
|
|
5
5
|
import { dirname as dirname3, resolve as resolve3 } from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
7
|
|
|
8
|
-
// src/antigravity-quota.ts
|
|
9
|
-
import { execFile, spawn } from "node:child_process";
|
|
10
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
11
|
-
import { homedir } from "node:os";
|
|
12
|
-
import { join } from "node:path";
|
|
13
|
-
import { promisify } from "node:util";
|
|
14
|
-
var execFileAsync = promisify(execFile);
|
|
15
|
-
async function hydrateProxyEnv(baseEnv = process.env) {
|
|
16
|
-
const next = { ...baseEnv };
|
|
17
|
-
const hasProxy = Boolean(
|
|
18
|
-
next.https_proxy?.trim() || next.HTTPS_PROXY?.trim() || next.http_proxy?.trim() || next.HTTP_PROXY?.trim() || next.all_proxy?.trim() || next.ALL_PROXY?.trim()
|
|
19
|
-
);
|
|
20
|
-
if (hasProxy || process.platform !== "darwin") return next;
|
|
21
|
-
try {
|
|
22
|
-
const { stdout } = await execFileAsync("scutil", ["--proxy"], { timeout: 2e3 });
|
|
23
|
-
const field = (name) => {
|
|
24
|
-
const m = new RegExp(`^\\s*${name}\\s*:\\s*(.+?)\\s*$`, "m").exec(stdout);
|
|
25
|
-
return m?.[1]?.trim() || void 0;
|
|
26
|
-
};
|
|
27
|
-
if (field("HTTPEnable") === "1" && field("HTTPProxy") && field("HTTPPort")) {
|
|
28
|
-
const url = `http://${field("HTTPProxy")}:${field("HTTPPort")}`;
|
|
29
|
-
next.http_proxy = url;
|
|
30
|
-
next.HTTP_PROXY = url;
|
|
31
|
-
}
|
|
32
|
-
if (field("HTTPSEnable") === "1" && field("HTTPSProxy") && field("HTTPSPort")) {
|
|
33
|
-
const url = `http://${field("HTTPSProxy")}:${field("HTTPSPort")}`;
|
|
34
|
-
next.https_proxy = url;
|
|
35
|
-
next.HTTPS_PROXY = url;
|
|
36
|
-
}
|
|
37
|
-
if (field("SOCKSEnable") === "1" && field("SOCKSProxy") && field("SOCKSPort")) {
|
|
38
|
-
const url = `socks5://${field("SOCKSProxy")}:${field("SOCKSPort")}`;
|
|
39
|
-
next.all_proxy = url;
|
|
40
|
-
next.ALL_PROXY = url;
|
|
41
|
-
if (!next.http_proxy && !next.https_proxy) {
|
|
42
|
-
next.http_proxy = url;
|
|
43
|
-
next.HTTP_PROXY = url;
|
|
44
|
-
next.https_proxy = url;
|
|
45
|
-
next.HTTPS_PROXY = url;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
} catch {
|
|
49
|
-
}
|
|
50
|
-
return next;
|
|
51
|
-
}
|
|
52
|
-
var USAGE_ARGS = ["-p", "/usage", "--output-format", "json", "--print-timeout", "20s"];
|
|
53
|
-
var USAGE_TIMEOUT_MS = 25e3;
|
|
54
|
-
var MAX_OUTPUT_BYTES = 512 * 1024;
|
|
55
|
-
var CACHE_TTL_MS = 6e4;
|
|
56
|
-
var cachedLimits = null;
|
|
57
|
-
var inflightProbe = null;
|
|
58
|
-
function antigravityUsesApiKeyProvider() {
|
|
59
|
-
try {
|
|
60
|
-
const raw = readFileSync(join(homedir(), ".gemini", "antigravity-cli", "settings.json"), "utf8");
|
|
61
|
-
const parsed = JSON.parse(raw);
|
|
62
|
-
return Boolean(parsed?.modelProvider);
|
|
63
|
-
} catch {
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function scrubAntigravityEnv(baseEnv = process.env) {
|
|
68
|
-
const next = { ...baseEnv };
|
|
69
|
-
if (!antigravityUsesApiKeyProvider()) {
|
|
70
|
-
for (const key of Object.keys(next)) {
|
|
71
|
-
const normalized = process.platform === "win32" ? key.toUpperCase() : key;
|
|
72
|
-
if (normalized === "GEMINI_API_KEY" || normalized === "GOOGLE_API_KEY") {
|
|
73
|
-
delete next[key];
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
next.AGY_CLI_DISABLE_AUTO_UPDATE = "true";
|
|
78
|
-
return next;
|
|
79
|
-
}
|
|
80
|
-
function resolveAgyBinary() {
|
|
81
|
-
const candidates = [
|
|
82
|
-
process.env.AGY_BIN,
|
|
83
|
-
join(homedir(), ".local", "bin", "agy"),
|
|
84
|
-
"/opt/homebrew/bin/agy",
|
|
85
|
-
"/usr/local/bin/agy"
|
|
86
|
-
].filter((v) => Boolean(v));
|
|
87
|
-
for (const candidate of candidates) {
|
|
88
|
-
if (existsSync(candidate)) return candidate;
|
|
89
|
-
}
|
|
90
|
-
return "agy";
|
|
91
|
-
}
|
|
92
|
-
function validResetIso(raw) {
|
|
93
|
-
if (typeof raw !== "string" || !raw.trim()) return void 0;
|
|
94
|
-
const ms = Date.parse(raw);
|
|
95
|
-
if (!Number.isFinite(ms) || ms <= 864e5) return void 0;
|
|
96
|
-
return new Date(ms).toISOString();
|
|
97
|
-
}
|
|
98
|
-
function parseAgyUsageJson(stdout) {
|
|
99
|
-
const checkedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
100
|
-
for (const line of stdout.split(/\r?\n/)) {
|
|
101
|
-
const trimmed = line.trim();
|
|
102
|
-
if (!trimmed.startsWith("{")) continue;
|
|
103
|
-
let parsed;
|
|
104
|
-
try {
|
|
105
|
-
parsed = JSON.parse(trimmed);
|
|
106
|
-
} catch {
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) continue;
|
|
110
|
-
if (typeof parsed.num_turns === "number" && parsed.num_turns > 0) return null;
|
|
111
|
-
if (typeof parsed.conversation_id === "string" && parsed.conversation_id.trim()) return null;
|
|
112
|
-
if (parsed.status !== "SUCCESS") continue;
|
|
113
|
-
const command = parsed.command;
|
|
114
|
-
if (!command || command.name !== "usage") continue;
|
|
115
|
-
const data = command.data;
|
|
116
|
-
if (!data || !Array.isArray(data.groups)) continue;
|
|
117
|
-
const windows = /* @__PURE__ */ new Map();
|
|
118
|
-
for (const rawGroup of data.groups) {
|
|
119
|
-
if (!rawGroup || typeof rawGroup !== "object") continue;
|
|
120
|
-
const group = rawGroup;
|
|
121
|
-
const groupName = typeof group.name === "string" ? group.name.trim() : "";
|
|
122
|
-
if (!Array.isArray(group.buckets)) continue;
|
|
123
|
-
for (const rawBucket of group.buckets) {
|
|
124
|
-
if (!rawBucket || typeof rawBucket !== "object") continue;
|
|
125
|
-
const bucket = rawBucket;
|
|
126
|
-
if (bucket.disabled === true) continue;
|
|
127
|
-
if (typeof bucket.remaining_fraction !== "number" || !Number.isFinite(bucket.remaining_fraction)) {
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
const id = typeof bucket.id === "string" && bucket.id.trim() ? bucket.id.trim() : `${groupName}-${String(bucket.window ?? "quota")}`;
|
|
131
|
-
const winRaw = typeof bucket.window === "string" ? bucket.window.trim() : "";
|
|
132
|
-
const weekly = winRaw === "weekly";
|
|
133
|
-
const session = winRaw === "5h";
|
|
134
|
-
const family = id.startsWith("gemini-argon-") ? groupName || "Gemini Argon" : id.startsWith("gemini-carbon-") ? groupName || "Gemini Carbon" : id.startsWith("gemini-") || groupName.toLowerCase().includes("gemini") ? "Gemini" : id.startsWith("3p-") || groupName.toLowerCase().includes("claude") ? "Claude / GPT" : groupName || id;
|
|
135
|
-
const kind = weekly ? "weekly" : session ? "session" : "other";
|
|
136
|
-
const windowDurationMins = weekly ? 7 * 24 * 60 : session ? 5 * 60 : void 0;
|
|
137
|
-
const usedPercent = (1 - Math.max(0, Math.min(1, bucket.remaining_fraction))) * 100;
|
|
138
|
-
const resetsAt = validResetIso(bucket.reset_time);
|
|
139
|
-
windows.set(id, {
|
|
140
|
-
id,
|
|
141
|
-
label: `${family} \xB7 ${weekly ? "\u6BCF\u5468\u989D\u5EA6" : session ? "5 \u5C0F\u65F6\u989D\u5EA6" : typeof bucket.name === "string" && bucket.name.trim() || "\u989D\u5EA6"}`,
|
|
142
|
-
kind,
|
|
143
|
-
usedPercent,
|
|
144
|
-
...resetsAt ? { resetsAt } : {},
|
|
145
|
-
...windowDurationMins !== void 0 ? { windowDurationMins } : {}
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
if (windows.size > 0) {
|
|
150
|
-
return {
|
|
151
|
-
checkedAt,
|
|
152
|
-
windows: [...windows.values()]
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
async function probeAntigravityUsage(force = false) {
|
|
159
|
-
if (!force && cachedLimits && Date.now() - cachedLimits.fetchedAt < CACHE_TTL_MS) {
|
|
160
|
-
return cachedLimits.limits;
|
|
161
|
-
}
|
|
162
|
-
if (inflightProbe) return inflightProbe;
|
|
163
|
-
inflightProbe = (async () => {
|
|
164
|
-
const proxyEnv = await hydrateProxyEnv();
|
|
165
|
-
return new Promise((resolve4) => {
|
|
166
|
-
let settled = false;
|
|
167
|
-
let stdout = "";
|
|
168
|
-
const done = (val) => {
|
|
169
|
-
if (settled) return;
|
|
170
|
-
settled = true;
|
|
171
|
-
clearTimeout(timer);
|
|
172
|
-
if (val) {
|
|
173
|
-
cachedLimits = { fetchedAt: Date.now(), limits: val };
|
|
174
|
-
}
|
|
175
|
-
resolve4(val);
|
|
176
|
-
};
|
|
177
|
-
const bin = resolveAgyBinary();
|
|
178
|
-
const child = spawn(bin, [...USAGE_ARGS], {
|
|
179
|
-
env: scrubAntigravityEnv(proxyEnv),
|
|
180
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
181
|
-
windowsHide: true
|
|
182
|
-
});
|
|
183
|
-
const timer = setTimeout(() => {
|
|
184
|
-
try {
|
|
185
|
-
child.kill("SIGKILL");
|
|
186
|
-
} catch {
|
|
187
|
-
}
|
|
188
|
-
done(null);
|
|
189
|
-
}, USAGE_TIMEOUT_MS);
|
|
190
|
-
child.stdout?.on("data", (chunk) => {
|
|
191
|
-
if (stdout.length < MAX_OUTPUT_BYTES) {
|
|
192
|
-
stdout += typeof chunk === "string" ? chunk : chunk.toString("utf8");
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
child.on("error", () => done(null));
|
|
196
|
-
child.on("close", (code) => {
|
|
197
|
-
if (code !== 0) {
|
|
198
|
-
done(null);
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
done(parseAgyUsageJson(stdout));
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
})().finally(() => {
|
|
205
|
-
inflightProbe = null;
|
|
206
|
-
});
|
|
207
|
-
return inflightProbe;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
8
|
// src/bridge.ts
|
|
211
9
|
import WebSocket2 from "ws";
|
|
212
10
|
|
|
213
11
|
// src/config.ts
|
|
214
12
|
import { chmod, mkdir, readFile, unlink, writeFile } from "node:fs/promises";
|
|
215
|
-
import { homedir
|
|
216
|
-
import { dirname, join
|
|
13
|
+
import { homedir, hostname } from "node:os";
|
|
14
|
+
import { dirname, join, resolve } from "node:path";
|
|
217
15
|
var DEFAULT_HUB_SERVER = "https://wq.stormapex.top";
|
|
218
16
|
var DEFAULT_LOCAL_T3_URL = "http://127.0.0.1:3773";
|
|
219
|
-
var APP_VERSION = "0.1.
|
|
17
|
+
var APP_VERSION = "0.1.6";
|
|
220
18
|
function defaultDataDir() {
|
|
221
19
|
if (process.env.ECHO_T3_BRIDGE_DATA_DIR?.trim()) {
|
|
222
20
|
return resolve(process.env.ECHO_T3_BRIDGE_DATA_DIR.trim());
|
|
223
21
|
}
|
|
224
22
|
if (process.platform === "darwin") {
|
|
225
|
-
return
|
|
23
|
+
return join(homedir(), "Library", "Application Support", "echo-t3-bridge");
|
|
226
24
|
}
|
|
227
25
|
if (process.platform === "win32") {
|
|
228
|
-
return
|
|
26
|
+
return join(process.env.APPDATA ?? homedir(), "echo-t3-bridge");
|
|
229
27
|
}
|
|
230
|
-
return
|
|
28
|
+
return join(process.env.XDG_STATE_HOME ?? join(homedir(), ".local", "state"), "echo-t3-bridge");
|
|
231
29
|
}
|
|
232
30
|
function defaultConfigPath() {
|
|
233
31
|
if (process.env.ECHO_T3_BRIDGE_CONFIG?.trim()) {
|
|
234
32
|
return resolve(process.env.ECHO_T3_BRIDGE_CONFIG.trim());
|
|
235
33
|
}
|
|
236
|
-
return
|
|
34
|
+
return join(defaultDataDir(), "config.json");
|
|
237
35
|
}
|
|
238
36
|
function defaultDeviceLabel() {
|
|
239
37
|
return hostname().replace(/\.local$/i, "") || "T3 Device";
|
|
@@ -468,21 +266,21 @@ async function fetchHubSelf(creds) {
|
|
|
468
266
|
|
|
469
267
|
// src/fs-list.ts
|
|
470
268
|
import { readdir, stat } from "node:fs/promises";
|
|
471
|
-
import { homedir as
|
|
472
|
-
import { dirname as dirname2, join as
|
|
269
|
+
import { homedir as homedir2 } from "node:os";
|
|
270
|
+
import { dirname as dirname2, join as join2, resolve as resolve2 } from "node:path";
|
|
473
271
|
async function listDirectories(rawPath) {
|
|
474
272
|
let target = (rawPath ?? "").trim();
|
|
475
273
|
if (!target || target === "~" || target === "~/") {
|
|
476
|
-
target =
|
|
274
|
+
target = homedir2();
|
|
477
275
|
} else if (target.startsWith("~/")) {
|
|
478
|
-
target =
|
|
276
|
+
target = join2(homedir2(), target.slice(2));
|
|
479
277
|
}
|
|
480
278
|
const absPath = resolve2(target);
|
|
481
279
|
const names = await readdir(absPath);
|
|
482
280
|
const entries = [];
|
|
483
281
|
await Promise.all(
|
|
484
282
|
names.map(async (name) => {
|
|
485
|
-
const fullPath =
|
|
283
|
+
const fullPath = join2(absPath, name);
|
|
486
284
|
try {
|
|
487
285
|
const info = await stat(fullPath);
|
|
488
286
|
if (info.isDirectory()) {
|
|
@@ -522,6 +320,210 @@ import { homedir as homedir4, tmpdir } from "node:os";
|
|
|
522
320
|
import { join as join4 } from "node:path";
|
|
523
321
|
import { createInterface } from "node:readline";
|
|
524
322
|
import { promisify as promisify2, stripVTControlCharacters } from "node:util";
|
|
323
|
+
|
|
324
|
+
// src/antigravity-quota.ts
|
|
325
|
+
import { execFile, spawn } from "node:child_process";
|
|
326
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
327
|
+
import { homedir as homedir3 } from "node:os";
|
|
328
|
+
import { join as join3 } from "node:path";
|
|
329
|
+
import { promisify } from "node:util";
|
|
330
|
+
var execFileAsync = promisify(execFile);
|
|
331
|
+
async function hydrateProxyEnv(baseEnv = process.env) {
|
|
332
|
+
const next = { ...baseEnv };
|
|
333
|
+
const hasProxy = Boolean(
|
|
334
|
+
next.https_proxy?.trim() || next.HTTPS_PROXY?.trim() || next.http_proxy?.trim() || next.HTTP_PROXY?.trim() || next.all_proxy?.trim() || next.ALL_PROXY?.trim()
|
|
335
|
+
);
|
|
336
|
+
if (hasProxy || process.platform !== "darwin") return next;
|
|
337
|
+
try {
|
|
338
|
+
const { stdout } = await execFileAsync("scutil", ["--proxy"], { timeout: 2e3 });
|
|
339
|
+
const field = (name) => {
|
|
340
|
+
const m = new RegExp(`^\\s*${name}\\s*:\\s*(.+?)\\s*$`, "m").exec(stdout);
|
|
341
|
+
return m?.[1]?.trim() || void 0;
|
|
342
|
+
};
|
|
343
|
+
if (field("HTTPEnable") === "1" && field("HTTPProxy") && field("HTTPPort")) {
|
|
344
|
+
const url = `http://${field("HTTPProxy")}:${field("HTTPPort")}`;
|
|
345
|
+
next.http_proxy = url;
|
|
346
|
+
next.HTTP_PROXY = url;
|
|
347
|
+
}
|
|
348
|
+
if (field("HTTPSEnable") === "1" && field("HTTPSProxy") && field("HTTPSPort")) {
|
|
349
|
+
const url = `http://${field("HTTPSProxy")}:${field("HTTPSPort")}`;
|
|
350
|
+
next.https_proxy = url;
|
|
351
|
+
next.HTTPS_PROXY = url;
|
|
352
|
+
}
|
|
353
|
+
if (field("SOCKSEnable") === "1" && field("SOCKSProxy") && field("SOCKSPort")) {
|
|
354
|
+
const url = `socks5://${field("SOCKSProxy")}:${field("SOCKSPort")}`;
|
|
355
|
+
next.all_proxy = url;
|
|
356
|
+
next.ALL_PROXY = url;
|
|
357
|
+
if (!next.http_proxy && !next.https_proxy) {
|
|
358
|
+
next.http_proxy = url;
|
|
359
|
+
next.HTTP_PROXY = url;
|
|
360
|
+
next.https_proxy = url;
|
|
361
|
+
next.HTTPS_PROXY = url;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
} catch {
|
|
365
|
+
}
|
|
366
|
+
return next;
|
|
367
|
+
}
|
|
368
|
+
var USAGE_ARGS = ["-p", "/usage", "--output-format", "json", "--print-timeout", "20s"];
|
|
369
|
+
var USAGE_TIMEOUT_MS = 25e3;
|
|
370
|
+
var MAX_OUTPUT_BYTES = 512 * 1024;
|
|
371
|
+
var CACHE_TTL_MS = 6e4;
|
|
372
|
+
var cachedLimits = null;
|
|
373
|
+
var inflightProbe = null;
|
|
374
|
+
function antigravityUsesApiKeyProvider() {
|
|
375
|
+
try {
|
|
376
|
+
const raw = readFileSync(join3(homedir3(), ".gemini", "antigravity-cli", "settings.json"), "utf8");
|
|
377
|
+
const parsed = JSON.parse(raw);
|
|
378
|
+
return Boolean(parsed?.modelProvider);
|
|
379
|
+
} catch {
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
function scrubAntigravityEnv(baseEnv = process.env) {
|
|
384
|
+
const next = { ...baseEnv };
|
|
385
|
+
if (!antigravityUsesApiKeyProvider()) {
|
|
386
|
+
for (const key of Object.keys(next)) {
|
|
387
|
+
const normalized = process.platform === "win32" ? key.toUpperCase() : key;
|
|
388
|
+
if (normalized === "GEMINI_API_KEY" || normalized === "GOOGLE_API_KEY") {
|
|
389
|
+
delete next[key];
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
next.AGY_CLI_DISABLE_AUTO_UPDATE = "true";
|
|
394
|
+
return next;
|
|
395
|
+
}
|
|
396
|
+
function resolveAgyBinary() {
|
|
397
|
+
const candidates = [
|
|
398
|
+
process.env.AGY_BIN,
|
|
399
|
+
join3(homedir3(), ".local", "bin", "agy"),
|
|
400
|
+
"/opt/homebrew/bin/agy",
|
|
401
|
+
"/usr/local/bin/agy"
|
|
402
|
+
].filter((v) => Boolean(v));
|
|
403
|
+
for (const candidate of candidates) {
|
|
404
|
+
if (existsSync(candidate)) return candidate;
|
|
405
|
+
}
|
|
406
|
+
return "agy";
|
|
407
|
+
}
|
|
408
|
+
function validResetIso(raw) {
|
|
409
|
+
if (typeof raw !== "string" || !raw.trim()) return void 0;
|
|
410
|
+
const ms = Date.parse(raw);
|
|
411
|
+
if (!Number.isFinite(ms) || ms <= 864e5) return void 0;
|
|
412
|
+
return new Date(ms).toISOString();
|
|
413
|
+
}
|
|
414
|
+
function parseAgyUsageJson(stdout) {
|
|
415
|
+
const checkedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
416
|
+
for (const line of stdout.split(/\r?\n/)) {
|
|
417
|
+
const trimmed = line.trim();
|
|
418
|
+
if (!trimmed.startsWith("{")) continue;
|
|
419
|
+
let parsed;
|
|
420
|
+
try {
|
|
421
|
+
parsed = JSON.parse(trimmed);
|
|
422
|
+
} catch {
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) continue;
|
|
426
|
+
if (typeof parsed.num_turns === "number" && parsed.num_turns > 0) return null;
|
|
427
|
+
if (typeof parsed.conversation_id === "string" && parsed.conversation_id.trim()) return null;
|
|
428
|
+
if (parsed.status !== "SUCCESS") continue;
|
|
429
|
+
const command = parsed.command;
|
|
430
|
+
if (!command || command.name !== "usage") continue;
|
|
431
|
+
const data = command.data;
|
|
432
|
+
if (!data || !Array.isArray(data.groups)) continue;
|
|
433
|
+
const windows = /* @__PURE__ */ new Map();
|
|
434
|
+
for (const rawGroup of data.groups) {
|
|
435
|
+
if (!rawGroup || typeof rawGroup !== "object") continue;
|
|
436
|
+
const group = rawGroup;
|
|
437
|
+
const groupName = typeof group.name === "string" ? group.name.trim() : "";
|
|
438
|
+
if (!Array.isArray(group.buckets)) continue;
|
|
439
|
+
for (const rawBucket of group.buckets) {
|
|
440
|
+
if (!rawBucket || typeof rawBucket !== "object") continue;
|
|
441
|
+
const bucket = rawBucket;
|
|
442
|
+
if (bucket.disabled === true) continue;
|
|
443
|
+
if (typeof bucket.remaining_fraction !== "number" || !Number.isFinite(bucket.remaining_fraction)) {
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
const id = typeof bucket.id === "string" && bucket.id.trim() ? bucket.id.trim() : `${groupName}-${String(bucket.window ?? "quota")}`;
|
|
447
|
+
const winRaw = typeof bucket.window === "string" ? bucket.window.trim() : "";
|
|
448
|
+
const weekly = winRaw === "weekly";
|
|
449
|
+
const session = winRaw === "5h";
|
|
450
|
+
const family = id.startsWith("gemini-argon-") ? groupName || "Gemini Argon" : id.startsWith("gemini-carbon-") ? groupName || "Gemini Carbon" : id.startsWith("gemini-") || groupName.toLowerCase().includes("gemini") ? "Gemini" : id.startsWith("3p-") || groupName.toLowerCase().includes("claude") ? "Claude / GPT" : groupName || id;
|
|
451
|
+
const kind = weekly ? "weekly" : session ? "session" : "other";
|
|
452
|
+
const windowDurationMins = weekly ? 7 * 24 * 60 : session ? 5 * 60 : void 0;
|
|
453
|
+
const usedPercent = (1 - Math.max(0, Math.min(1, bucket.remaining_fraction))) * 100;
|
|
454
|
+
const resetsAt = validResetIso(bucket.reset_time);
|
|
455
|
+
windows.set(id, {
|
|
456
|
+
id,
|
|
457
|
+
label: `${family} \xB7 ${weekly ? "\u6BCF\u5468\u989D\u5EA6" : session ? "5 \u5C0F\u65F6\u989D\u5EA6" : typeof bucket.name === "string" && bucket.name.trim() || "\u989D\u5EA6"}`,
|
|
458
|
+
kind,
|
|
459
|
+
usedPercent,
|
|
460
|
+
...resetsAt ? { resetsAt } : {},
|
|
461
|
+
...windowDurationMins !== void 0 ? { windowDurationMins } : {}
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (windows.size > 0) {
|
|
466
|
+
return {
|
|
467
|
+
checkedAt,
|
|
468
|
+
windows: [...windows.values()]
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return null;
|
|
473
|
+
}
|
|
474
|
+
async function probeAntigravityUsage(force = false) {
|
|
475
|
+
if (!force && cachedLimits && Date.now() - cachedLimits.fetchedAt < CACHE_TTL_MS) {
|
|
476
|
+
return cachedLimits.limits;
|
|
477
|
+
}
|
|
478
|
+
if (inflightProbe) return inflightProbe;
|
|
479
|
+
inflightProbe = (async () => {
|
|
480
|
+
const proxyEnv = await hydrateProxyEnv();
|
|
481
|
+
return new Promise((resolve4) => {
|
|
482
|
+
let settled = false;
|
|
483
|
+
let stdout = "";
|
|
484
|
+
const done = (val) => {
|
|
485
|
+
if (settled) return;
|
|
486
|
+
settled = true;
|
|
487
|
+
clearTimeout(timer);
|
|
488
|
+
if (val) {
|
|
489
|
+
cachedLimits = { fetchedAt: Date.now(), limits: val };
|
|
490
|
+
}
|
|
491
|
+
resolve4(val);
|
|
492
|
+
};
|
|
493
|
+
const bin = resolveAgyBinary();
|
|
494
|
+
const child = spawn(bin, [...USAGE_ARGS], {
|
|
495
|
+
env: scrubAntigravityEnv(proxyEnv),
|
|
496
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
497
|
+
windowsHide: true
|
|
498
|
+
});
|
|
499
|
+
const timer = setTimeout(() => {
|
|
500
|
+
try {
|
|
501
|
+
child.kill("SIGKILL");
|
|
502
|
+
} catch {
|
|
503
|
+
}
|
|
504
|
+
done(null);
|
|
505
|
+
}, USAGE_TIMEOUT_MS);
|
|
506
|
+
child.stdout?.on("data", (chunk) => {
|
|
507
|
+
if (stdout.length < MAX_OUTPUT_BYTES) {
|
|
508
|
+
stdout += typeof chunk === "string" ? chunk : chunk.toString("utf8");
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
child.on("error", () => done(null));
|
|
512
|
+
child.on("close", (code) => {
|
|
513
|
+
if (code !== 0) {
|
|
514
|
+
done(null);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
done(parseAgyUsageJson(stdout));
|
|
518
|
+
});
|
|
519
|
+
});
|
|
520
|
+
})().finally(() => {
|
|
521
|
+
inflightProbe = null;
|
|
522
|
+
});
|
|
523
|
+
return inflightProbe;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// src/provider-quotas.ts
|
|
525
527
|
var execFileAsync2 = promisify2(execFile2);
|
|
526
528
|
var CACHE_TTL_MS2 = 6e4;
|
|
527
529
|
var quotaCache = /* @__PURE__ */ new Map();
|
|
@@ -1023,6 +1025,8 @@ function normalizeDispatchCommand(rawCommand) {
|
|
|
1023
1025
|
}
|
|
1024
1026
|
return { ...command, modelSelection: selObj };
|
|
1025
1027
|
}
|
|
1028
|
+
var PING_INTERVAL_MS = 25e3;
|
|
1029
|
+
var PING_SILENCE_LIMIT_MS = 6e4;
|
|
1026
1030
|
var LocalT3Client = class {
|
|
1027
1031
|
baseUrl;
|
|
1028
1032
|
accessToken;
|
|
@@ -1042,6 +1046,8 @@ var LocalT3Client = class {
|
|
|
1042
1046
|
terminalMetadata = /* @__PURE__ */ new Map();
|
|
1043
1047
|
terminalStreams = /* @__PURE__ */ new Map();
|
|
1044
1048
|
pingTimer = null;
|
|
1049
|
+
/** Last time anything (Pong, Chunk, Exit…) arrived from the local T3 socket. */
|
|
1050
|
+
lastInboundAt = 0;
|
|
1045
1051
|
reconnectTimer = null;
|
|
1046
1052
|
backoffMs = 2e3;
|
|
1047
1053
|
constructor(config, callbacks, initialThreads = []) {
|
|
@@ -1165,15 +1171,21 @@ var LocalT3Client = class {
|
|
|
1165
1171
|
this.backoffMs = 2e3;
|
|
1166
1172
|
console.log(`[local-t3] \u5DF2\u8FDE\u63A5\u672C\u673A\u539F\u7248 T3 Server (${this.baseUrl})`);
|
|
1167
1173
|
this.callbacks.onStatusChange?.(true);
|
|
1174
|
+
this.lastInboundAt = Date.now();
|
|
1168
1175
|
this.pingTimer = setInterval(() => {
|
|
1169
|
-
if (ws.readyState
|
|
1170
|
-
|
|
1176
|
+
if (ws.readyState !== WebSocket.OPEN) return;
|
|
1177
|
+
if (Date.now() - this.lastInboundAt > PING_SILENCE_LIMIT_MS) {
|
|
1178
|
+
console.error("[local-t3] \u672C\u673A T3 \u8D85\u8FC7 60 \u79D2\u65E0\u4EFB\u4F55\u56DE\u5305\uFF0C\u65AD\u5F00\u5E76\u91CD\u8FDE\u2026");
|
|
1179
|
+
ws.terminate();
|
|
1180
|
+
return;
|
|
1171
1181
|
}
|
|
1172
|
-
|
|
1182
|
+
ws.send(JSON.stringify({ _tag: "Ping" }));
|
|
1183
|
+
}, PING_INTERVAL_MS);
|
|
1173
1184
|
void this.bootstrapStreams();
|
|
1174
1185
|
});
|
|
1175
1186
|
ws.on("message", (data) => {
|
|
1176
1187
|
if (this.ws !== ws) return;
|
|
1188
|
+
this.lastInboundAt = Date.now();
|
|
1177
1189
|
this.handleWsMessage(data.toString());
|
|
1178
1190
|
});
|
|
1179
1191
|
ws.on("close", (code, reason) => {
|
|
@@ -1297,13 +1309,9 @@ var LocalT3Client = class {
|
|
|
1297
1309
|
}
|
|
1298
1310
|
}
|
|
1299
1311
|
async bootstrapStreams() {
|
|
1300
|
-
this.startStream(
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
(item) => {
|
|
1304
|
-
void this.handleConfigEvent(item);
|
|
1305
|
-
}
|
|
1306
|
-
);
|
|
1312
|
+
this.startStream("subscribeServerConfig", {}, (item) => {
|
|
1313
|
+
void this.handleConfigEvent(item);
|
|
1314
|
+
});
|
|
1307
1315
|
this.startStream("subscribeTerminalMetadata", {}, (item) => {
|
|
1308
1316
|
this.handleTerminalMetadataEvent(item);
|
|
1309
1317
|
});
|
|
@@ -1340,14 +1348,12 @@ var LocalT3Client = class {
|
|
|
1340
1348
|
if (evt.type === "snapshot" && evt.config) {
|
|
1341
1349
|
if (evt.config.cwd) this.defaultCwd = evt.config.cwd;
|
|
1342
1350
|
if (Array.isArray(evt.config.providers)) {
|
|
1343
|
-
|
|
1344
|
-
this.
|
|
1345
|
-
this.callbacks.onProviders(enriched);
|
|
1351
|
+
this.lastProviders = evt.config.providers;
|
|
1352
|
+
this.callbacks.onProviders(evt.config.providers);
|
|
1346
1353
|
}
|
|
1347
1354
|
} else if ((evt.type === "providerStatuses" || evt.type === "providerStatusDeltas") && Array.isArray(evt.payload?.providers)) {
|
|
1348
|
-
|
|
1349
|
-
this.
|
|
1350
|
-
this.callbacks.onProviders(enriched);
|
|
1355
|
+
this.lastProviders = evt.payload.providers;
|
|
1356
|
+
this.callbacks.onProviders(evt.payload.providers);
|
|
1351
1357
|
}
|
|
1352
1358
|
}
|
|
1353
1359
|
ensureThreadSubscribed(threadId, afterSequence = 0) {
|
|
@@ -1382,17 +1388,35 @@ var LocalT3Client = class {
|
|
|
1382
1388
|
this.subscribedThreads.delete(threadId);
|
|
1383
1389
|
}
|
|
1384
1390
|
}
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
+
/**
|
|
1392
|
+
* Re-subscribe every known thread after `afterSequence` so events the Hub
|
|
1393
|
+
* missed (while the Hub link was down or its buffer overflowed) are replayed
|
|
1394
|
+
* from T3's own log. Replayed events are forwarded verbatim; the caller
|
|
1395
|
+
* de-duplicates by sequence.
|
|
1396
|
+
*/
|
|
1397
|
+
resubscribeAfter(afterSequence) {
|
|
1398
|
+
if (!Number.isFinite(afterSequence) || afterSequence < 0) return;
|
|
1399
|
+
for (const threadId of [...this.wantedThreads.keys()]) {
|
|
1400
|
+
for (const [reqId, tid] of [...this.streamThreads.entries()]) {
|
|
1401
|
+
if (tid === threadId) {
|
|
1402
|
+
this.streamThreads.delete(reqId);
|
|
1403
|
+
this.interruptStream(reqId);
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
this.subscribedThreads.delete(threadId);
|
|
1407
|
+
const cur = this.wantedThreads.get(threadId) ?? 0;
|
|
1408
|
+
this.wantedThreads.set(threadId, Math.min(cur, afterSequence));
|
|
1409
|
+
this.ensureThreadSubscribed(threadId, 0);
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
async refreshProviders(includeQuota = false) {
|
|
1413
|
+
const payload = includeQuota ? { fresh: true, refreshModels: true, usageLimitsCommand: true } : { fresh: true };
|
|
1414
|
+
const raw = await this.callRpc("server.refreshProviders", payload, 45e3);
|
|
1391
1415
|
const list = Array.isArray(raw) ? raw : Array.isArray(raw?.providers) ? raw.providers : this.lastProviders;
|
|
1392
|
-
const
|
|
1393
|
-
this.lastProviders =
|
|
1394
|
-
this.callbacks.onProviders(
|
|
1395
|
-
return { providers:
|
|
1416
|
+
const finalList = includeQuota ? await enrichProvidersWithQuotas(list, true) : list;
|
|
1417
|
+
this.lastProviders = finalList;
|
|
1418
|
+
this.callbacks.onProviders(finalList);
|
|
1419
|
+
return { providers: finalList };
|
|
1396
1420
|
}
|
|
1397
1421
|
async ensureProject(workspaceRoot) {
|
|
1398
1422
|
let targetRoot = (workspaceRoot ?? "").trim();
|
|
@@ -1959,13 +1983,21 @@ function createHubWebSocketAgent(targetUrl) {
|
|
|
1959
1983
|
return void 0;
|
|
1960
1984
|
}
|
|
1961
1985
|
}
|
|
1986
|
+
const agent = new HttpConnectAgent({ keepAlive: false });
|
|
1987
|
+
Object.assign(agent, {
|
|
1988
|
+
protocol: isSecureTarget ? "https:" : "http:",
|
|
1989
|
+
defaultPort: isSecureTarget ? 443 : 80
|
|
1990
|
+
});
|
|
1962
1991
|
return {
|
|
1963
|
-
agent
|
|
1992
|
+
agent,
|
|
1964
1993
|
proxyUrl: proxyUrlStr
|
|
1965
1994
|
};
|
|
1966
1995
|
}
|
|
1967
1996
|
|
|
1968
1997
|
// src/bridge.ts
|
|
1998
|
+
var MAX_BUFFERED_EVENTS = 5e3;
|
|
1999
|
+
var MAX_SENT_SEQUENCES = 2e4;
|
|
2000
|
+
var MAX_MISSED_HEARTBEAT_ACKS = 3;
|
|
1969
2001
|
var EchoT3Bridge = class {
|
|
1970
2002
|
configPath;
|
|
1971
2003
|
config;
|
|
@@ -1973,10 +2005,17 @@ var EchoT3Bridge = class {
|
|
|
1973
2005
|
hubWs = null;
|
|
1974
2006
|
stopped = false;
|
|
1975
2007
|
heartbeatTimer = null;
|
|
2008
|
+
tokenRefreshTimer = null;
|
|
1976
2009
|
reconnectTimer = null;
|
|
1977
2010
|
backoffMs = 2e3;
|
|
1978
2011
|
saveThreadsTimer = null;
|
|
1979
2012
|
bypassProxyOnNextAttempt = false;
|
|
2013
|
+
/** True once the Hub's welcome arrived on the current link; events flow only then. */
|
|
2014
|
+
hubReady = false;
|
|
2015
|
+
unackedHeartbeats = 0;
|
|
2016
|
+
bufferedEvents = /* @__PURE__ */ new Map();
|
|
2017
|
+
bufferOverflowed = false;
|
|
2018
|
+
sentSequences = /* @__PURE__ */ new Set();
|
|
1980
2019
|
constructor(configPath) {
|
|
1981
2020
|
this.configPath = configPath;
|
|
1982
2021
|
}
|
|
@@ -1995,7 +2034,7 @@ var EchoT3Bridge = class {
|
|
|
1995
2034
|
this.sendHubFrame({ type: "catalog", data: { providers } });
|
|
1996
2035
|
},
|
|
1997
2036
|
onThreadEvent: (envelope) => {
|
|
1998
|
-
this.
|
|
2037
|
+
this.forwardThreadEvent(envelope);
|
|
1999
2038
|
this.scheduleSaveKnownThreads();
|
|
2000
2039
|
},
|
|
2001
2040
|
onTerminalEvent: (envelope) => {
|
|
@@ -2032,12 +2071,59 @@ var EchoT3Bridge = class {
|
|
|
2032
2071
|
void saveConfig(this.config, this.configPath).catch(() => void 0);
|
|
2033
2072
|
}, 5e3);
|
|
2034
2073
|
}
|
|
2074
|
+
/**
|
|
2075
|
+
* Forward a T3 event to the Hub verbatim (sequence, occurredAt, driver,
|
|
2076
|
+
* providerInstanceId all stay inside `event`). While the Hub link is not
|
|
2077
|
+
* ready the event is buffered (bounded) and flushed in sequence order after
|
|
2078
|
+
* the welcome frame; overflow is recovered by re-subscribing from T3.
|
|
2079
|
+
*/
|
|
2080
|
+
forwardThreadEvent(envelope) {
|
|
2081
|
+
const seq = envelope.sequence;
|
|
2082
|
+
if (this.sentSequences.has(seq)) return;
|
|
2083
|
+
if (this.hubReady && this.hubWs?.readyState === WebSocket2.OPEN) {
|
|
2084
|
+
this.sendEventNow(envelope);
|
|
2085
|
+
return;
|
|
2086
|
+
}
|
|
2087
|
+
if (this.bufferedEvents.has(seq)) return;
|
|
2088
|
+
if (this.bufferedEvents.size >= MAX_BUFFERED_EVENTS) {
|
|
2089
|
+
this.bufferOverflowed = true;
|
|
2090
|
+
return;
|
|
2091
|
+
}
|
|
2092
|
+
this.bufferedEvents.set(seq, envelope);
|
|
2093
|
+
}
|
|
2094
|
+
sendEventNow(envelope) {
|
|
2095
|
+
this.sentSequences.add(envelope.sequence);
|
|
2096
|
+
if (this.sentSequences.size > MAX_SENT_SEQUENCES) {
|
|
2097
|
+
const oldest = this.sentSequences.values().next().value;
|
|
2098
|
+
if (oldest !== void 0) this.sentSequences.delete(oldest);
|
|
2099
|
+
}
|
|
2100
|
+
this.sendHubFrame({ type: "event", data: envelope });
|
|
2101
|
+
}
|
|
2102
|
+
flushBufferedEvents() {
|
|
2103
|
+
const ordered = [...this.bufferedEvents.values()].sort((a, b) => a.sequence - b.sequence);
|
|
2104
|
+
this.bufferedEvents.clear();
|
|
2105
|
+
for (const envelope of ordered) {
|
|
2106
|
+
if (!this.sentSequences.has(envelope.sequence)) this.sendEventNow(envelope);
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
/** Ask T3 to replay everything after `afterSequence`; duplicates are dropped by sequence. */
|
|
2110
|
+
resumeEventsAfter(afterSequence) {
|
|
2111
|
+
const after = typeof afterSequence === "number" ? afterSequence : Number(afterSequence);
|
|
2112
|
+
if (!Number.isFinite(after) || after < 0) return;
|
|
2113
|
+
this.localT3.resubscribeAfter(after);
|
|
2114
|
+
}
|
|
2035
2115
|
teardownHub() {
|
|
2116
|
+
this.hubReady = false;
|
|
2117
|
+
this.unackedHeartbeats = 0;
|
|
2036
2118
|
this.localT3?.detachAllTerminals();
|
|
2037
2119
|
if (this.heartbeatTimer) {
|
|
2038
2120
|
clearInterval(this.heartbeatTimer);
|
|
2039
2121
|
this.heartbeatTimer = null;
|
|
2040
2122
|
}
|
|
2123
|
+
if (this.tokenRefreshTimer) {
|
|
2124
|
+
clearInterval(this.tokenRefreshTimer);
|
|
2125
|
+
this.tokenRefreshTimer = null;
|
|
2126
|
+
}
|
|
2041
2127
|
const ws = this.hubWs;
|
|
2042
2128
|
this.hubWs = null;
|
|
2043
2129
|
if (ws) {
|
|
@@ -2056,9 +2142,24 @@ var EchoT3Bridge = class {
|
|
|
2056
2142
|
void this.connectHub();
|
|
2057
2143
|
}, wait);
|
|
2058
2144
|
}
|
|
2059
|
-
async
|
|
2145
|
+
async syncHubCredentialsFromDiskIfNewer() {
|
|
2146
|
+
const current = this.config.hub;
|
|
2147
|
+
if (!current) return false;
|
|
2148
|
+
try {
|
|
2149
|
+
const onDisk = await loadConfig(this.configPath);
|
|
2150
|
+
const diskHub = onDisk.hub;
|
|
2151
|
+
if (diskHub && diskHub.environmentId === current.environmentId && diskHub.accessToken !== current.accessToken && diskHub.accessTokenExpiresAt > current.accessTokenExpiresAt) {
|
|
2152
|
+
this.config.hub = diskHub;
|
|
2153
|
+
return true;
|
|
2154
|
+
}
|
|
2155
|
+
} catch {
|
|
2156
|
+
}
|
|
2157
|
+
return false;
|
|
2158
|
+
}
|
|
2159
|
+
async ensureFreshHubCredentials(minRemainingMs = 5 * 6e4) {
|
|
2160
|
+
await this.syncHubCredentialsFromDiskIfNewer();
|
|
2060
2161
|
const creds = this.config.hub;
|
|
2061
|
-
if (creds.accessTokenExpiresAt - Date.now() <
|
|
2162
|
+
if (creds.accessTokenExpiresAt - Date.now() < minRemainingMs) {
|
|
2062
2163
|
console.log("[hub] Access token \u5373\u5C06\u8FC7\u671F\uFF0C\u6B63\u5728\u7EED\u671F\u2026");
|
|
2063
2164
|
const renewed = await renewHubToken(creds);
|
|
2064
2165
|
this.config.hub = renewed;
|
|
@@ -2108,6 +2209,11 @@ var EchoT3Bridge = class {
|
|
|
2108
2209
|
this.teardownHub();
|
|
2109
2210
|
if (status === 401 && !retriedAfterRenew) {
|
|
2110
2211
|
try {
|
|
2212
|
+
const adoptedNewer = await this.syncHubCredentialsFromDiskIfNewer();
|
|
2213
|
+
if (adoptedNewer) {
|
|
2214
|
+
void this.connectHub(true);
|
|
2215
|
+
return;
|
|
2216
|
+
}
|
|
2111
2217
|
console.log("[hub] \u5C1D\u8BD5\u4F7F\u7528 DPoP \u5BC6\u94A5\u5237\u65B0 Access Token\u2026");
|
|
2112
2218
|
const renewed = await renewHubToken(creds);
|
|
2113
2219
|
this.config.hub = renewed;
|
|
@@ -2127,6 +2233,12 @@ var EchoT3Bridge = class {
|
|
|
2127
2233
|
this.bypassProxyOnNextAttempt = false;
|
|
2128
2234
|
const via = proxyInfo ? ` via proxy ${proxyInfo.proxyUrl}` : "";
|
|
2129
2235
|
console.log(`[hub] \u5DF2\u5EFA\u7ACB\u516C\u7F51\u53CD\u5411\u957F\u8FDE\u63A5 (${creds.serverUrl}, env=${creds.environmentId}${via})`);
|
|
2236
|
+
if (this.tokenRefreshTimer) clearInterval(this.tokenRefreshTimer);
|
|
2237
|
+
this.tokenRefreshTimer = setInterval(() => {
|
|
2238
|
+
void this.ensureFreshHubCredentials(10 * 6e4).catch((err) => {
|
|
2239
|
+
console.error(`[hub] \u540E\u53F0\u7EED\u671F\u5931\u8D25: ${err instanceof Error ? err.message : String(err)}`);
|
|
2240
|
+
});
|
|
2241
|
+
}, 5 * 6e4);
|
|
2130
2242
|
this.sendHubFrame({
|
|
2131
2243
|
type: "hello",
|
|
2132
2244
|
data: {
|
|
@@ -2194,15 +2306,40 @@ var EchoT3Bridge = class {
|
|
|
2194
2306
|
case "welcome": {
|
|
2195
2307
|
const data = frame.data ?? {};
|
|
2196
2308
|
const interval = Math.max(5e3, Number(data.heartbeatIntervalMs) || 2e4);
|
|
2309
|
+
this.unackedHeartbeats = 0;
|
|
2197
2310
|
if (this.heartbeatTimer) clearInterval(this.heartbeatTimer);
|
|
2198
2311
|
this.heartbeatTimer = setInterval(() => {
|
|
2312
|
+
if (this.unackedHeartbeats >= MAX_MISSED_HEARTBEAT_ACKS) {
|
|
2313
|
+
console.error(`[hub] \u8FDE\u7EED ${MAX_MISSED_HEARTBEAT_ACKS} \u4E2A\u5FC3\u8DF3\u5468\u671F\u672A\u6536\u5230 heartbeat_ack\uFF0C\u65AD\u5F00\u5E76\u91CD\u8FDE\u2026`);
|
|
2314
|
+
const ws = this.hubWs;
|
|
2315
|
+
this.teardownHub();
|
|
2316
|
+
try {
|
|
2317
|
+
ws?.terminate();
|
|
2318
|
+
} catch {
|
|
2319
|
+
}
|
|
2320
|
+
this.scheduleHubReconnect();
|
|
2321
|
+
return;
|
|
2322
|
+
}
|
|
2323
|
+
this.unackedHeartbeats += 1;
|
|
2199
2324
|
this.sendHubFrame({ type: "heartbeat", id: `hb_${Date.now()}` });
|
|
2200
2325
|
}, interval);
|
|
2326
|
+
this.hubReady = true;
|
|
2327
|
+
this.flushBufferedEvents();
|
|
2328
|
+
const cursor = data.lastEventSequence;
|
|
2329
|
+
if (typeof cursor === "number" && cursor > 0) {
|
|
2330
|
+
this.bufferOverflowed = false;
|
|
2331
|
+
this.resumeEventsAfter(cursor);
|
|
2332
|
+
}
|
|
2201
2333
|
return;
|
|
2202
2334
|
}
|
|
2203
2335
|
case "heartbeat_ack":
|
|
2204
|
-
|
|
2336
|
+
this.unackedHeartbeats = 0;
|
|
2205
2337
|
return;
|
|
2338
|
+
case "events.resume": {
|
|
2339
|
+
const data = frame.data ?? {};
|
|
2340
|
+
this.resumeEventsAfter(data.lastEventSequence ?? data.afterSequence);
|
|
2341
|
+
return;
|
|
2342
|
+
}
|
|
2206
2343
|
case "providers.refresh": {
|
|
2207
2344
|
try {
|
|
2208
2345
|
const result = await this.localT3.refreshProviders(true);
|
|
@@ -2415,10 +2552,10 @@ function printHelp() {
|
|
|
2415
2552
|
\uFF08\u522B\u540D: enroll\uFF09\u4EC5\u6267\u884C\u516C\u7F51\u914D\u5BF9\u4E0E\u672C\u673A\u539F\u7248 T3 \u6388\u6743\uFF0C\u4E0D\u5B89\u88C5\u540E\u53F0\u670D\u52A1
|
|
2416
2553
|
|
|
2417
2554
|
echo-t3-bridge run
|
|
2418
|
-
\u524D\u53F0\u8FD0\u884C\u53CD\u5411\u6865\u63A5\u5B88\u62A4\u8FDB\u7A0B\uFF08\u53CC\u5411\u65AD\u7EBF\u81EA\u52A8\u91CD\u8FDE + \u5B9E\u65F6\u4E8B\u4EF6\u6D41
|
|
2555
|
+
\u524D\u53F0\u8FD0\u884C\u53CD\u5411\u6865\u63A5\u5B88\u62A4\u8FDB\u7A0B\uFF08\u53CC\u5411\u65AD\u7EBF\u81EA\u52A8\u91CD\u8FDE + \u5B9E\u65F6\u4E8B\u4EF6\u6D41\uFF09
|
|
2419
2556
|
|
|
2420
2557
|
echo-t3-bridge status | doctor
|
|
2421
|
-
\u68C0\u67E5\u516C\u7F51\u8FDE\u63A5\u72B6\u6001\
|
|
2558
|
+
\u68C0\u67E5\u516C\u7F51\u8FDE\u63A5\u72B6\u6001\u4E0E\u672C\u673A\u5B98\u65B9\u539F\u7248 T3 \u670D\u52A1\u72B6\u6001
|
|
2422
2559
|
|
|
2423
2560
|
echo-t3-bridge service <install|uninstall|restart>
|
|
2424
2561
|
\uFF08\u522B\u540D: service:install / service:uninstall\uFF09\u7BA1\u7406\u540E\u53F0\u5F00\u673A\u81EA\u542F\u670D\u52A1
|
|
@@ -2478,18 +2615,45 @@ async function doPair(flags, configPath) {
|
|
|
2478
2615
|
await saveConfig(cfg, configPath);
|
|
2479
2616
|
console.log(`\u2713 \u51ED\u636E\u5DF2\u4FDD\u5B58\u5230 ${configPath}`);
|
|
2480
2617
|
}
|
|
2618
|
+
function sleepMs(ms) {
|
|
2619
|
+
return new Promise((resolve4) => setTimeout(resolve4, ms));
|
|
2620
|
+
}
|
|
2481
2621
|
async function doDoctor(configPath) {
|
|
2482
|
-
|
|
2622
|
+
let cfg = await loadConfig(configPath);
|
|
2483
2623
|
console.log(`echo-t3-bridge v${APP_VERSION}`);
|
|
2484
2624
|
console.log(`\u914D\u7F6E\u6587\u4EF6: ${configPath}`);
|
|
2485
2625
|
if (!cfg.hub) {
|
|
2486
2626
|
console.log("\u516C\u7F51 Hub: \u672A\u914D\u5BF9\uFF08\u8BF7\u8FD0\u884C echo-t3-bridge setup --code <\u914D\u5BF9\u7801>\uFF09");
|
|
2487
2627
|
} else {
|
|
2488
|
-
|
|
2489
|
-
|
|
2628
|
+
let hub = cfg.hub;
|
|
2629
|
+
let self = await fetchHubSelf(hub).catch(() => ({ ok: false, status: 0 }));
|
|
2630
|
+
if (!self.ok && self.status === 401) {
|
|
2631
|
+
await sleepMs(1500);
|
|
2632
|
+
const reloaded = await loadConfig(configPath);
|
|
2633
|
+
if (reloaded.hub && reloaded.hub.accessToken !== hub.accessToken) {
|
|
2634
|
+
cfg = reloaded;
|
|
2635
|
+
hub = reloaded.hub;
|
|
2636
|
+
self = await fetchHubSelf(hub).catch(() => ({ ok: false, status: 0 }));
|
|
2637
|
+
} else if (reloaded.hub && reloaded.hub.sessionExpiresAt > Date.now()) {
|
|
2638
|
+
try {
|
|
2639
|
+
const renewed = await renewHubToken(reloaded.hub);
|
|
2640
|
+
cfg = { ...reloaded, hub: renewed };
|
|
2641
|
+
hub = renewed;
|
|
2642
|
+
await saveConfig(cfg, configPath);
|
|
2643
|
+
self = await fetchHubSelf(hub).catch(() => ({ ok: false, status: 0 }));
|
|
2644
|
+
} catch {
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
}
|
|
2648
|
+
if (self.ok && !self.online) {
|
|
2649
|
+
await sleepMs(1500);
|
|
2650
|
+
const retry = await fetchHubSelf(hub).catch(() => null);
|
|
2651
|
+
if (retry && retry.ok) self = retry;
|
|
2652
|
+
}
|
|
2653
|
+
const envName = self.environment?.name || hub.label;
|
|
2490
2654
|
const online = self.online;
|
|
2491
2655
|
console.log(
|
|
2492
|
-
`\u516C\u7F51 Hub: ${
|
|
2656
|
+
`\u516C\u7F51 Hub: ${hub.serverUrl} | \u8BBE\u5907: ${envName} (${hub.environmentId}) | \u51ED\u636E: ${self.ok ? "\u6709\u6548" : `\u5F02\u5E38 (HTTP ${self.status})`} | \u5728\u7EBF: ${online ? "\u25CF \u5728\u7EBF" : "\u25CB \u79BB\u7EBF"}`
|
|
2493
2657
|
);
|
|
2494
2658
|
}
|
|
2495
2659
|
const t3Bin = resolveLocalT3Binary(cfg.localT3.t3Binary);
|
|
@@ -2497,14 +2661,6 @@ async function doDoctor(configPath) {
|
|
|
2497
2661
|
console.log(
|
|
2498
2662
|
`\u672C\u673A\u539F\u7248 T3: ${cfg.localT3.baseUrl} | \u4E8C\u8FDB\u5236: ${t3Bin} | \u670D\u52A1: ${localOk ? "\u25CF \u8FD0\u884C\u4E2D" : "\u25CB \u672A\u542F\u52A8\uFF08\u8BF7\u8FD0\u884C t3 up\uFF09"} | \u672C\u5730\u4EE4\u724C: ${cfg.localT3.accessToken ? "\u5DF2\u6388\u6743" : "\u672A\u6388\u6743"}`
|
|
2499
2663
|
);
|
|
2500
|
-
const agyBin = resolveAgyBinary();
|
|
2501
|
-
const agyUsage = await probeAntigravityUsage(true);
|
|
2502
|
-
if (agyUsage && agyUsage.windows.length > 0) {
|
|
2503
|
-
const summary = agyUsage.windows.map((w) => `${w.label} \u5269 ${Math.round(100 - w.usedPercent)}%`).join(" | ");
|
|
2504
|
-
console.log(`Antigravity \u989D\u5EA6 (${agyBin}): \u2713 ${summary}`);
|
|
2505
|
-
} else {
|
|
2506
|
-
console.log(`Antigravity \u989D\u5EA6 (${agyBin}): \u672A\u68C0\u6D4B\u5230\uFF08\u5982\u672A\u5B89\u88C5 agy \u53EF\u5FFD\u7565\uFF09`);
|
|
2507
|
-
}
|
|
2508
2664
|
}
|
|
2509
2665
|
async function main() {
|
|
2510
2666
|
const parsed = parseArgs(process.argv.slice(2));
|