echo-t3-bridge 0.1.6 → 0.1.8
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/README.md +7 -0
- package/dist/cli.mjs +321 -43
- package/dist/tsc/bridge.js +137 -34
- package/dist/tsc/config.js +1 -1
- package/dist/tsc/index.js +9 -0
- package/dist/tsc/local-t3.js +177 -9
- package/dist/tsc/logging.js +77 -0
- package/dist/tsc/provider-quotas.js +11 -3
- package/package.json +1 -1
- package/scripts/service.mjs +97 -8
|
@@ -375,12 +375,14 @@ async function probeDriverUsage(driver, force = false) {
|
|
|
375
375
|
* Enriches all installed/enabled providers reported by the unmodified local T3
|
|
376
376
|
* server using toc-ocra's proven quota readers (`agy`, `codex`, `claude`, `kiro`).
|
|
377
377
|
*/
|
|
378
|
-
export async function enrichProvidersWithQuotas(providers, forceQuota = false) {
|
|
378
|
+
export async function enrichProvidersWithQuotas(providers, forceQuota = false, providerId) {
|
|
379
379
|
const neededDrivers = new Set();
|
|
380
380
|
for (const p of providers) {
|
|
381
381
|
if (!p || typeof p !== 'object')
|
|
382
382
|
continue;
|
|
383
383
|
const rec = p;
|
|
384
|
+
if (providerId && String(rec.instanceId ?? '').toLowerCase() !== providerId.toLowerCase())
|
|
385
|
+
continue;
|
|
384
386
|
if (rec.installed === false)
|
|
385
387
|
continue;
|
|
386
388
|
const driver = resolveQuotaDriver(rec);
|
|
@@ -393,8 +395,11 @@ export async function enrichProvidersWithQuotas(providers, forceQuota = false) {
|
|
|
393
395
|
neededDrivers.add(driver);
|
|
394
396
|
}
|
|
395
397
|
}
|
|
396
|
-
if (neededDrivers.size === 0)
|
|
398
|
+
if (neededDrivers.size === 0) {
|
|
399
|
+
if (providerId)
|
|
400
|
+
throw new Error('未找到可同步额度的 CLI');
|
|
397
401
|
return providers;
|
|
402
|
+
}
|
|
398
403
|
const probed = new Map();
|
|
399
404
|
await Promise.all([...neededDrivers].map(async (driver) => {
|
|
400
405
|
const limits = await probeDriverUsage(driver, forceQuota).catch(() => null);
|
|
@@ -402,8 +407,11 @@ export async function enrichProvidersWithQuotas(providers, forceQuota = false) {
|
|
|
402
407
|
probed.set(driver, limits);
|
|
403
408
|
}
|
|
404
409
|
}));
|
|
405
|
-
if (probed.size === 0)
|
|
410
|
+
if (probed.size === 0) {
|
|
411
|
+
if (providerId)
|
|
412
|
+
throw new Error('该 CLI 暂时无法读取额度');
|
|
406
413
|
return providers;
|
|
414
|
+
}
|
|
407
415
|
return providers.map((p) => {
|
|
408
416
|
if (!p || typeof p !== 'object')
|
|
409
417
|
return p;
|
package/package.json
CHANGED
package/scripts/service.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { existsSync } from 'node:fs';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
3
|
import { chmod, mkdir, unlink, writeFile } from 'node:fs/promises';
|
|
4
4
|
import { homedir } from 'node:os';
|
|
5
5
|
import { dirname, join, resolve } from 'node:path';
|
|
6
6
|
import { fileURLToPath } from 'node:url';
|
|
7
|
-
import { spawnSync } from 'node:child_process';
|
|
7
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
8
8
|
|
|
9
9
|
const SERVICE_NAME = 'echo-t3-bridge';
|
|
10
10
|
const LAUNCHD_LABEL = 'top.stormapex.echo-t3-bridge';
|
|
@@ -272,6 +272,12 @@ async function manageWindows(mode) {
|
|
|
272
272
|
);
|
|
273
273
|
const vbsPath = join(startupDir, `${SERVICE_NAME}.vbs`);
|
|
274
274
|
const cmdPath = join(dataDir, 'run-bridge.cmd');
|
|
275
|
+
const logPath = join(dataDir, 'bridge.log');
|
|
276
|
+
|
|
277
|
+
// Never two bridges for one device: stop whatever runs now (also 0.1.6
|
|
278
|
+
// instances that predate the pid file) before installing, restarting or removing.
|
|
279
|
+
const stopped = stopWindowsBridge();
|
|
280
|
+
if (stopped > 0) console.log(`已停止正在运行的后台进程 (${stopped} 个)`);
|
|
275
281
|
|
|
276
282
|
if (mode === 'uninstall') {
|
|
277
283
|
await unlink(vbsPath).catch(() => undefined);
|
|
@@ -282,11 +288,24 @@ async function manageWindows(mode) {
|
|
|
282
288
|
|
|
283
289
|
await mkdir(dataDir, { recursive: true });
|
|
284
290
|
await mkdir(startupDir, { recursive: true });
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
);
|
|
291
|
+
const proxyLines = collectProxyEnvEntries()
|
|
292
|
+
.map(([k, v]) => `set "${k}=${v}"`)
|
|
293
|
+
.join('\r\n');
|
|
294
|
+
// stdout/stderr are appended to bridge.log (the bridge timestamps each line
|
|
295
|
+
// and redacts credentials); a log over ~5MB is rotated to bridge.log.1 at start.
|
|
296
|
+
const cmd = [
|
|
297
|
+
'@echo off',
|
|
298
|
+
'chcp 65001 >nul',
|
|
299
|
+
'setlocal',
|
|
300
|
+
`set "ECHO_T3_BRIDGE_CONFIG=${configPath}"`,
|
|
301
|
+
`set "ECHO_T3_BRIDGE_DATA_DIR=${dataDir}"`,
|
|
302
|
+
`set "LOG=${logPath}"`,
|
|
303
|
+
...(proxyLines ? [proxyLines] : []),
|
|
304
|
+
'if exist "%LOG%" for %%F in ("%LOG%") do if %%~zF GTR 5242880 move /Y "%LOG%" "%LOG%.1" >nul',
|
|
305
|
+
`"${process.execPath}" "${cliEntry}" run >> "%LOG%" 2>&1`,
|
|
306
|
+
'',
|
|
307
|
+
].join('\r\n');
|
|
308
|
+
await writeFile(cmdPath, cmd, 'utf8');
|
|
290
309
|
await writeFile(
|
|
291
310
|
vbsPath,
|
|
292
311
|
`Set WshShell = CreateObject("WScript.Shell")\r\nWshShell.Run """${cmdPath}""", 0, False\r\n`,
|
|
@@ -294,5 +313,75 @@ async function manageWindows(mode) {
|
|
|
294
313
|
);
|
|
295
314
|
await chmod(cmdPath, 0o755).catch(() => undefined);
|
|
296
315
|
console.log(`✓ 已写入 Windows 自启脚本: ${vbsPath}`);
|
|
297
|
-
|
|
316
|
+
|
|
317
|
+
// Start now, hidden, exactly like a logon would.
|
|
318
|
+
const child = spawn('wscript.exe', [vbsPath], { detached: true, stdio: 'ignore', windowsHide: true });
|
|
319
|
+
child.on('error', (err) => console.error(`启动后台进程失败: ${err.message}`));
|
|
320
|
+
child.unref();
|
|
321
|
+
const pid = await waitForPid(8_000);
|
|
322
|
+
if (pid) console.log(`✓ 后台进程已启动 (pid=${pid})`);
|
|
323
|
+
else console.warn('⚠ 未能确认后台进程已启动,请查看日志或手动运行 `echo-t3-bridge run`。');
|
|
324
|
+
console.log(` 日志: ${logPath}`);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function pidFile() {
|
|
328
|
+
return join(dirname(configPath), 'bridge.pid');
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function readPid() {
|
|
332
|
+
try {
|
|
333
|
+
const pid = Number(readFileSync(pidFile(), 'utf8').trim());
|
|
334
|
+
return Number.isInteger(pid) && pid > 0 ? pid : null;
|
|
335
|
+
} catch {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function isAlive(pid) {
|
|
341
|
+
try {
|
|
342
|
+
process.kill(pid, 0);
|
|
343
|
+
return true;
|
|
344
|
+
} catch (err) {
|
|
345
|
+
return err?.code === 'EPERM';
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
async function waitForPid(timeoutMs) {
|
|
350
|
+
const deadline = Date.now() + timeoutMs;
|
|
351
|
+
while (Date.now() < deadline) {
|
|
352
|
+
const pid = readPid();
|
|
353
|
+
if (pid && isAlive(pid)) return pid;
|
|
354
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
355
|
+
}
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Stops running bridge daemons (pid file, plus any `cli.mjs run` node process). */
|
|
360
|
+
function stopWindowsBridge() {
|
|
361
|
+
const pids = new Set();
|
|
362
|
+
const fromFile = readPid();
|
|
363
|
+
if (fromFile && isAlive(fromFile)) pids.add(fromFile);
|
|
364
|
+
const script = [
|
|
365
|
+
"$ErrorActionPreference='SilentlyContinue'",
|
|
366
|
+
'Get-CimInstance Win32_Process |',
|
|
367
|
+
"Where-Object { $_.Name -eq 'node.exe' -and $_.CommandLine -match 'echo-t3-bridge' -and $_.CommandLine -match 'cli[.]mjs.?\\s+run(\\s|$)' } |",
|
|
368
|
+
'ForEach-Object { $_.ProcessId }',
|
|
369
|
+
].join(' ');
|
|
370
|
+
const res = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], {
|
|
371
|
+
encoding: 'utf8',
|
|
372
|
+
timeout: 15_000,
|
|
373
|
+
windowsHide: true,
|
|
374
|
+
});
|
|
375
|
+
for (const line of (res.stdout ?? '').split(/\r?\n/)) {
|
|
376
|
+
const pid = Number(line.trim());
|
|
377
|
+
if (Number.isInteger(pid) && pid > 0) pids.add(pid);
|
|
378
|
+
}
|
|
379
|
+
pids.delete(process.pid);
|
|
380
|
+
pids.delete(process.ppid);
|
|
381
|
+
let count = 0;
|
|
382
|
+
for (const pid of pids) {
|
|
383
|
+
const kill = spawnSync('taskkill', ['/PID', String(pid), '/T', '/F'], { encoding: 'utf8', windowsHide: true });
|
|
384
|
+
if (kill.status === 0) count += 1;
|
|
385
|
+
}
|
|
386
|
+
return count;
|
|
298
387
|
}
|