dsh-codearts 1.4.4 → 1.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/lib/{auth-Cd2SvQIy.mjs → auth-B5PDMG7r.mjs} +272 -26
- package/lib/bin.mjs +3 -3
- package/lib/client/index.js +104 -104
- package/lib/index.d.mts +8 -0
- package/lib/index.mjs +4 -4
- package/lib/{keeper-DJLwIiR4.mjs → keeper-CA6PKtcv.mjs} +32 -2
- package/lib/keeper-main.mjs +66 -22
- package/lib/{upstream-BV4XWB4l.mjs → upstream-B_KAbdPy.mjs} +1 -1
- package/lib/{version-CHCdx9qJ.mjs → version-DZ47veC-.mjs} +3 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,36 @@
|
|
|
3
3
|
本文件记录本插件的重要改动。格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),
|
|
4
4
|
版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
|
5
5
|
|
|
6
|
+
## [1.4.5] - 2026-11-18
|
|
7
|
+
|
|
8
|
+
### 修复
|
|
9
|
+
|
|
10
|
+
- **企业版自定义模型的输出上限不再被压到 8000**(#11):企业内部专业版的模型由
|
|
11
|
+
管理侧注册为自定义模型,上游把输出上限放在 `model_parameters.output_context_window`、
|
|
12
|
+
而 `max_tokens` 留 0。插件以前只读 `max_tokens`,这类模型的 `maxTokens` 全部落到
|
|
13
|
+
8000 兜底,长回答在 8K token 处被截断。现在 `max_tokens` 为 0 时退回读
|
|
14
|
+
`output_context_window`,随后仍按该模型的实测上限钳制(声明 100000 会被钳到
|
|
15
|
+
65536);内置模型与福利模型的取值不变。
|
|
16
|
+
- **续期不再把两个账号同时刷死**:插件的保活循环和守护进程 keeper 会各自拿同一份
|
|
17
|
+
`refresh_token` 去换新令牌,而 CodeArts 的 refresh_token 是一次性的 —— 谁后到
|
|
18
|
+
谁收到 `HTTP 400 STS5.1806 invalid refresh token: 'the refresh token has been used'`,
|
|
19
|
+
两个令牌一起作废,所有账号掉到需要重新登录。现在跨进程用 `.codearts-refresh.lock`
|
|
20
|
+
(PID + 心跳,60ms 轮询抢锁、20s 重申、90s 判僵尸)串行化刷新,拿不到锁的一方
|
|
21
|
+
直接跳过本轮,等对端写完再读盘。
|
|
22
|
+
- **「refresh token 已用过」不再当场判死**:这条错误以前被当成永久失败,直接把账号
|
|
23
|
+
标成 `stale` 停在登录页。现在按瞬态处理:先重读磁盘看对端是否已换上新令牌,没换
|
|
24
|
+
就连续 3 轮(插件侧 3 次独立刷新)才真正判死,期间保持可重试。
|
|
25
|
+
- **PID 探活不再误杀进程**:Windows 上 `process.kill(pid, 0)` 会真的把目标进程
|
|
26
|
+
干掉(Node 不看信号值,直接 TerminateProcess)。锁文件的僵尸判定与 keeper 的
|
|
27
|
+
`keeperStats()` 探活都踩了这一条 —— 后者每探一次就可能杀掉守护进程,keeper 一死
|
|
28
|
+
插件就整段时间不再自己续期。现在 Windows 分支改用 `tasklist /FI "PID eq N"` 判定,
|
|
29
|
+
探活失败按「活着」处理,宁可等心跳超时也不误删。
|
|
30
|
+
- **机器睡眠后 keeper 立即补一轮**:Windows 睡眠会让 20 分钟一次的 `setTimeout`
|
|
31
|
+
停摆(11-18 凌晨实测有 4 轮 tick 全空,而进程一直活着),期间没有续期。现在加了
|
|
32
|
+
30 秒看门狗,发现距上次 tick 超过 25 分钟就重排定时器并立刻补跑一轮。
|
|
33
|
+
- **旧版本误标的 `stale` 账号自动复活**:1.3.0 及以前任何一次续期失败(含网络抖动)
|
|
34
|
+
都会永久落 `stale`;现在重新登录前会先用一次续期复验,成功就直接恢复,不必手动重登。
|
|
35
|
+
|
|
6
36
|
## [1.4.4] - 2026-11-17
|
|
7
37
|
|
|
8
38
|
### 调整
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { createHash, createPrivateKey, createSign, generateKeyPairSync, randomBytes } from "node:crypto";
|
|
2
|
-
import { chmod, mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
|
|
2
|
+
import { chmod, mkdir, open, readFile, rename, unlink, utimes, writeFile } from "node:fs/promises";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import { dirname, join } from "node:path";
|
|
5
5
|
import http from "node:http";
|
|
6
|
+
import { execFileSync } from "node:child_process";
|
|
6
7
|
//#region src/dpop.ts
|
|
7
8
|
/**
|
|
8
9
|
* DPoP(RFC 9449)证明 JWT:ES256 + P-256,供 oauth2/tokens 请求头使用。
|
|
@@ -343,6 +344,161 @@ async function startLogin(cfg = defaultLoginConfig()) {
|
|
|
343
344
|
close: () => codeCh.server.close()
|
|
344
345
|
};
|
|
345
346
|
}
|
|
347
|
+
/** Windows 上探活要起一次 tasklist:超时按毫秒给足,卡住就按「活着」处理。 */
|
|
348
|
+
const PID_PROBE_TIMEOUT_MS = 5e3;
|
|
349
|
+
/** 抢锁的轮询间隔(带上少量抖动,避免两个进程同拍重试)。 */
|
|
350
|
+
const POLL_MS = 60;
|
|
351
|
+
/** 持锁期间的重申间隔:一次续期里超过这个时长没重申,别人就可以认定我们死了。 */
|
|
352
|
+
const RENEW_MS = 2e4;
|
|
353
|
+
/** 锁文件路径:与账号文档同目录,保证同一份凭证只有一个锁。 */
|
|
354
|
+
function refreshLockPath(home) {
|
|
355
|
+
return join(home, ".codearts-refresh.lock");
|
|
356
|
+
}
|
|
357
|
+
const sleep$1 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
358
|
+
/**
|
|
359
|
+
* 判断某个 pid 是否还活着。
|
|
360
|
+
*
|
|
361
|
+
* **绝不能用 `process.kill(pid, 0)`**:这个技巧只在 POSIX 上成立。Windows 上 Node 的
|
|
362
|
+
* 实现不看信号值,直接用 `TerminateProcess` —— `kill(pid, 0)` 会把那个进程**直接杀掉**
|
|
363
|
+
* (实测:同步执行的两段代码互相探活,持锁那一段被当场干掉,锁一直留到 90 秒超时)。
|
|
364
|
+
* 续期锁要探的正是插件自己或守护进程,杀掉它们就是杀掉登录态本身。
|
|
365
|
+
*
|
|
366
|
+
* 这里改用 Windows 自带的 tasklist 查表;POSIX 上继续用 `kill(pid, 0)`。
|
|
367
|
+
*/
|
|
368
|
+
function pidAlive(pid) {
|
|
369
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
370
|
+
if (process.platform !== "win32") try {
|
|
371
|
+
process.kill(pid, 0);
|
|
372
|
+
return true;
|
|
373
|
+
} catch (error) {
|
|
374
|
+
return error?.code === "EPERM";
|
|
375
|
+
}
|
|
376
|
+
if (pid === process.pid) return true;
|
|
377
|
+
try {
|
|
378
|
+
const out = execFileSync("tasklist", [
|
|
379
|
+
"/FI",
|
|
380
|
+
`PID eq ${pid}`,
|
|
381
|
+
"/NH"
|
|
382
|
+
], {
|
|
383
|
+
encoding: "utf8",
|
|
384
|
+
stdio: [
|
|
385
|
+
"ignore",
|
|
386
|
+
"pipe",
|
|
387
|
+
"ignore"
|
|
388
|
+
],
|
|
389
|
+
timeout: PID_PROBE_TIMEOUT_MS,
|
|
390
|
+
windowsHide: true
|
|
391
|
+
});
|
|
392
|
+
return /\b\d+/.test(out) && out.includes(String(pid));
|
|
393
|
+
} catch {
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
/** 读锁文件;读不到/内容坏掉都返回 undefined(坏锁当成「可以清掉」)。 */
|
|
398
|
+
async function readLock(path) {
|
|
399
|
+
try {
|
|
400
|
+
const parsed = JSON.parse(await readFile(path, "utf8"));
|
|
401
|
+
if (typeof parsed.pid === "number" && typeof parsed.at === "number") return {
|
|
402
|
+
pid: parsed.pid,
|
|
403
|
+
at: parsed.at
|
|
404
|
+
};
|
|
405
|
+
} catch {}
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* 这个锁还能不能算数。
|
|
409
|
+
*
|
|
410
|
+
* pid 复用是 Windows 上的真实风险:被 kill 的进程留下的 pid 很快会分给新进程,
|
|
411
|
+
* 只看 `pidAlive` 会把死锁当成活锁,永远抢不到。所以「pid 还活着」也要配上
|
|
412
|
+
* `at` 在合理范围内才算活着。
|
|
413
|
+
*/
|
|
414
|
+
function isStale(lock, now) {
|
|
415
|
+
if (now - lock.at > 9e4) return true;
|
|
416
|
+
return !pidAlive(lock.pid);
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* 清掉一个陈旧锁。
|
|
420
|
+
*
|
|
421
|
+
* 删之前重读一次内容、和刚才判定时逐字段比对:中间可能有人刚抢到新锁,
|
|
422
|
+
* 那就不许删(删了会让两个进程同时持锁,比不加锁更糟)。
|
|
423
|
+
*/
|
|
424
|
+
async function breakStale(path, seen) {
|
|
425
|
+
const now = await readLock(path);
|
|
426
|
+
if (now !== void 0 && seen !== void 0 && (now.pid !== seen.pid || now.at !== seen.at)) return;
|
|
427
|
+
try {
|
|
428
|
+
await unlink(path);
|
|
429
|
+
} catch {}
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* 抢锁。拿到了返回释放函数,等不到返回 undefined。
|
|
433
|
+
*
|
|
434
|
+
* 调用方**必须**在 `finally` 里释放(或依赖它自己超时 —— 不释放只是让别人多等
|
|
435
|
+
* 一个 `REFRESH_LOCK_STALE_MS`,不会把锁永久卡死)。
|
|
436
|
+
*/
|
|
437
|
+
async function acquireRefreshLock(path, options = {}) {
|
|
438
|
+
const now = options.now ?? Date.now;
|
|
439
|
+
const deadline = now() + (options.waitMs ?? 2e4);
|
|
440
|
+
for (;;) {
|
|
441
|
+
const payload = {
|
|
442
|
+
pid: process.pid,
|
|
443
|
+
at: now()
|
|
444
|
+
};
|
|
445
|
+
let handle;
|
|
446
|
+
try {
|
|
447
|
+
handle = await open(path, "wx", 384);
|
|
448
|
+
await handle.writeFile(JSON.stringify(payload));
|
|
449
|
+
await handle.close();
|
|
450
|
+
handle = void 0;
|
|
451
|
+
return releaser(path, payload);
|
|
452
|
+
} catch (error) {
|
|
453
|
+
await handle?.close().catch(() => {});
|
|
454
|
+
if (error?.code !== "EEXIST") return;
|
|
455
|
+
}
|
|
456
|
+
const held = await readLock(path);
|
|
457
|
+
if (held === void 0 || isStale(held, now())) {
|
|
458
|
+
await breakStale(path, held);
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
if (now() >= deadline) return void 0;
|
|
462
|
+
await sleep$1(POLL_MS + Math.floor(Math.random() * POLL_MS));
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
function releaser(path, payload) {
|
|
466
|
+
let released = false;
|
|
467
|
+
const renew = setInterval(() => {
|
|
468
|
+
if (released) return;
|
|
469
|
+
payload.at = Date.now();
|
|
470
|
+
writeFile(path, JSON.stringify(payload), { mode: 384 }).then(() => utimes(path, /* @__PURE__ */ new Date(), /* @__PURE__ */ new Date())).catch(() => {});
|
|
471
|
+
}, RENEW_MS);
|
|
472
|
+
renew.unref?.();
|
|
473
|
+
return async () => {
|
|
474
|
+
if (released) return;
|
|
475
|
+
released = true;
|
|
476
|
+
clearInterval(renew);
|
|
477
|
+
const now_ = await readLock(path);
|
|
478
|
+
if (now_ !== void 0 && now_.pid !== payload.pid) return;
|
|
479
|
+
try {
|
|
480
|
+
await unlink(path);
|
|
481
|
+
} catch {}
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* 拿着锁做一件事。
|
|
486
|
+
*
|
|
487
|
+
* 拿不到锁返回 `undefined` —— 调用方据此知道「这一轮没轮到我们,什么都不用做」,
|
|
488
|
+
* 而不是把「没刷成」当成「刷新失败」(后者会误判 stale)。
|
|
489
|
+
*/
|
|
490
|
+
async function withRefreshLock(path, fn, options = {}) {
|
|
491
|
+
const release = await acquireRefreshLock(path, options);
|
|
492
|
+
if (release === void 0) {
|
|
493
|
+
options.onBusy?.();
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
try {
|
|
497
|
+
return await fn();
|
|
498
|
+
} finally {
|
|
499
|
+
await release();
|
|
500
|
+
}
|
|
501
|
+
}
|
|
346
502
|
//#endregion
|
|
347
503
|
//#region src/auth.ts
|
|
348
504
|
/**
|
|
@@ -428,7 +584,7 @@ async function clearCredential(path) {
|
|
|
428
584
|
async function claimIfPossible(cred) {
|
|
429
585
|
if (!cred.accessKeyId || !cred.secretAccessKey) return;
|
|
430
586
|
try {
|
|
431
|
-
const { claimBenefit } = await import("./upstream-
|
|
587
|
+
const { claimBenefit } = await import("./upstream-B_KAbdPy.mjs").then((n) => n.P);
|
|
432
588
|
await claimBenefit({
|
|
433
589
|
token: cred.token,
|
|
434
590
|
accessKeyId: cred.accessKeyId,
|
|
@@ -467,6 +623,15 @@ const REFRESH_RETRY_BASE_MS = 250;
|
|
|
467
623
|
* 一次性 refresh_token 反复烧掉,把「暂时失败」放大成「真的失效」。
|
|
468
624
|
*/
|
|
469
625
|
const REFRESH_TRANSIENT_BLOCK_MS = 6e4;
|
|
626
|
+
/**
|
|
627
|
+
* `STS5.1806`(refresh_token 已被用掉)连续撞上几次才判死。
|
|
628
|
+
*
|
|
629
|
+
* 这个码本身**不能**证明凭证坏了:另一个写入方(守护进程)恰好先用掉了同一个令牌,
|
|
630
|
+
* 撞上的这一方手里拿的就是旧值。绝大多数情况下下一轮重读磁盘就能拿到新令牌。
|
|
631
|
+
* 连续 3 次、每次之间隔一个瞬时冷却还都是这个结果,才算真的没救了 ——
|
|
632
|
+
* 早先一次就判死,2026-11-18 00:06 两个号同时被误杀(见 `refresh-lock.ts` 的说明)。
|
|
633
|
+
*/
|
|
634
|
+
const REFRESH_USED_MAX_STREAK = 3;
|
|
470
635
|
/** 失败是不是「凭证真的没了」;只有这一类才允许写 `stale`。 */
|
|
471
636
|
function isPermanentRefreshFailure(error) {
|
|
472
637
|
return error instanceof OAuthTokenError && error.permanent;
|
|
@@ -722,14 +887,19 @@ var AccountSlot = class {
|
|
|
722
887
|
_gen = 0;
|
|
723
888
|
persist;
|
|
724
889
|
log;
|
|
890
|
+
/** 跨进程续期锁的路径(由 store 注入;单凭证时代用默认目录)。 */
|
|
891
|
+
lockPath;
|
|
725
892
|
/** 瞬时失败后的冷却:这之前不再拿 refresh_token 去撞上游。 */
|
|
726
893
|
_refreshBlockedUntil = 0;
|
|
727
894
|
/** 最近一次续期失败的类型,供 store 判断要不要「重读磁盘再试一次」。 */
|
|
728
895
|
_lastFailure;
|
|
729
|
-
|
|
896
|
+
/** `STS5.1806` 连续撞了几次(成功一次就清零);见 `REFRESH_USED_MAX_STREAK`。 */
|
|
897
|
+
_usedStreak = 0;
|
|
898
|
+
constructor(cred, persist, log, lockPath) {
|
|
730
899
|
this._cred = cred;
|
|
731
900
|
this.persist = persist;
|
|
732
901
|
this.log = log;
|
|
902
|
+
this.lockPath = lockPath ?? refreshLockPath(dirname(codeartsAccountsPath()));
|
|
733
903
|
}
|
|
734
904
|
get credential() {
|
|
735
905
|
return this._cred;
|
|
@@ -738,6 +908,17 @@ var AccountSlot = class {
|
|
|
738
908
|
get lastFailure() {
|
|
739
909
|
return this._lastFailure;
|
|
740
910
|
}
|
|
911
|
+
/**
|
|
912
|
+
* 这一份凭证是不是真的完了。
|
|
913
|
+
*
|
|
914
|
+
* `_lastFailure === 'used'` 只代表「这一趟撞上了已用过的令牌」,还要看连续撞了几次:
|
|
915
|
+
* 一两次是并发轮换的正常现象,重读磁盘就能恢复。
|
|
916
|
+
*/
|
|
917
|
+
get exhausted() {
|
|
918
|
+
if (this._cred.stale === true) return true;
|
|
919
|
+
if (this._lastFailure === "permanent") return true;
|
|
920
|
+
return this._lastFailure === "used" && this._usedStreak >= REFRESH_USED_MAX_STREAK;
|
|
921
|
+
}
|
|
741
922
|
/** 把这份凭证标成 stale 并落库(store 在「令牌被别人用掉且磁盘也没有更新」时调)。 */
|
|
742
923
|
async markStale() {
|
|
743
924
|
if (this._cred.stale === true) return;
|
|
@@ -791,20 +972,57 @@ var AccountSlot = class {
|
|
|
791
972
|
*/
|
|
792
973
|
async verifyStale() {
|
|
793
974
|
const cred = this._cred;
|
|
794
|
-
if (
|
|
975
|
+
if (this._cred.stale === true) {
|
|
976
|
+
if (!cred.refreshToken || !cred.dpopPrivateKey || !cred.clientId) return false;
|
|
977
|
+
if (Date.now() < this._refreshBlockedUntil) return false;
|
|
978
|
+
const outcome = await this.refresh(cred, {
|
|
979
|
+
refreshToken: cred.refreshToken,
|
|
980
|
+
dpopPrivateKey: cred.dpopPrivateKey,
|
|
981
|
+
clientId: cred.clientId
|
|
982
|
+
});
|
|
983
|
+
if (outcome.ok) this._cred = outcome.cred;
|
|
984
|
+
return outcome.ok;
|
|
985
|
+
}
|
|
795
986
|
if (!cred.refreshToken || !cred.dpopPrivateKey || !cred.clientId) return false;
|
|
796
987
|
if (Date.now() < this._refreshBlockedUntil) return false;
|
|
797
|
-
|
|
988
|
+
const outcome = await this.refresh(cred, {
|
|
989
|
+
refreshToken: cred.refreshToken,
|
|
990
|
+
dpopPrivateKey: cred.dpopPrivateKey,
|
|
991
|
+
clientId: cred.clientId
|
|
992
|
+
});
|
|
993
|
+
if (outcome.ok) this._cred = outcome.cred;
|
|
994
|
+
return outcome.ok;
|
|
798
995
|
}
|
|
799
|
-
/**
|
|
996
|
+
/**
|
|
997
|
+
* 单飞执行一次续期,**并在跨进程文件锁里做**。
|
|
998
|
+
*
|
|
999
|
+
* 单飞只挡得住同一进程内的并发;守护进程(`keeper-main.ts`)是另一个进程,
|
|
1000
|
+
* 它和我们各持一份同一个账号的 refresh_token,同时刷必然有一方撞上 `STS5.1806`。
|
|
1001
|
+
* 锁保证「同一时刻只有一个进程拿 refresh_token 去撞上游」。拿不到锁(对方正在刷)
|
|
1002
|
+
* 就当这一轮没轮到我们:不写 stale,只冷却一会儿等下一轮重读磁盘拿它写回的新令牌。
|
|
1003
|
+
*/
|
|
800
1004
|
async runRefresh(cred) {
|
|
801
|
-
return this._inflight ??= this.
|
|
1005
|
+
return this._inflight ??= this.refreshLocked(cred).finally(() => {
|
|
1006
|
+
this._inflight = void 0;
|
|
1007
|
+
});
|
|
1008
|
+
}
|
|
1009
|
+
async refreshLocked(cred) {
|
|
1010
|
+
const outcome = await withRefreshLock(this.lockPath, () => this.refresh(cred, {
|
|
802
1011
|
refreshToken: cred.refreshToken ?? "",
|
|
803
1012
|
dpopPrivateKey: cred.dpopPrivateKey,
|
|
804
1013
|
clientId: cred.clientId ?? ""
|
|
805
|
-
})
|
|
806
|
-
this.
|
|
807
|
-
|
|
1014
|
+
}), { onBusy: () => {
|
|
1015
|
+
this._refreshBlockedUntil = Date.now() + REFRESH_TRANSIENT_BLOCK_MS;
|
|
1016
|
+
this.log?.(`[dsh-codearts] refresh ${cred.label ?? "account"} skipped: another process holds the refresh lock, will reload from disk`);
|
|
1017
|
+
} });
|
|
1018
|
+
if (outcome !== void 0) return outcome;
|
|
1019
|
+
this._refreshBlockedUntil = Date.now() + REFRESH_TRANSIENT_BLOCK_MS;
|
|
1020
|
+
return {
|
|
1021
|
+
ok: false,
|
|
1022
|
+
cred: this._cred,
|
|
1023
|
+
permanent: false,
|
|
1024
|
+
reason: "refresh lock busy"
|
|
1025
|
+
};
|
|
808
1026
|
}
|
|
809
1027
|
async refresh(cred, mat) {
|
|
810
1028
|
const gen = this._gen;
|
|
@@ -825,6 +1043,7 @@ var AccountSlot = class {
|
|
|
825
1043
|
}), cred.label);
|
|
826
1044
|
this._refreshBlockedUntil = 0;
|
|
827
1045
|
this._lastFailure = void 0;
|
|
1046
|
+
this._usedStreak = 0;
|
|
828
1047
|
if (!stillCurrent()) return {
|
|
829
1048
|
ok: true,
|
|
830
1049
|
cred: next
|
|
@@ -838,15 +1057,34 @@ var AccountSlot = class {
|
|
|
838
1057
|
};
|
|
839
1058
|
} catch (error) {
|
|
840
1059
|
lastError = error;
|
|
841
|
-
if (isPermanentRefreshFailure(error)) break;
|
|
1060
|
+
if (isPermanentRefreshFailure(error) || isRefreshTokenUsed(error)) break;
|
|
842
1061
|
this.log?.(`[dsh-codearts] refresh ${cred.label ?? "account"} attempt ${attempt}/${REFRESH_MAX_ATTEMPTS} failed (transient): ${describeRefreshFailure(error)}`);
|
|
843
1062
|
if (attempt < REFRESH_MAX_ATTEMPTS) await sleep(REFRESH_RETRY_BASE_MS * 2 ** (attempt - 1));
|
|
844
1063
|
}
|
|
845
1064
|
const used = isRefreshTokenUsed(lastError);
|
|
846
1065
|
const permanent = !used && isPermanentRefreshFailure(lastError);
|
|
847
1066
|
const reason = describeRefreshFailure(lastError);
|
|
848
|
-
if (used)
|
|
849
|
-
|
|
1067
|
+
if (used) {
|
|
1068
|
+
this._usedStreak += 1;
|
|
1069
|
+
this._lastFailure = "used";
|
|
1070
|
+
this._refreshBlockedUntil = Date.now() + REFRESH_TRANSIENT_BLOCK_MS;
|
|
1071
|
+
this.log?.(`[dsh-codearts] refresh ${cred.label ?? "account"} hit a used refresh token (${this._usedStreak}/${REFRESH_USED_MAX_STREAK}), reloading from disk`);
|
|
1072
|
+
if (!stillCurrent()) return {
|
|
1073
|
+
ok: false,
|
|
1074
|
+
cred: this._cred,
|
|
1075
|
+
permanent: false,
|
|
1076
|
+
reason
|
|
1077
|
+
};
|
|
1078
|
+
this._cred = cred;
|
|
1079
|
+
return {
|
|
1080
|
+
ok: false,
|
|
1081
|
+
cred,
|
|
1082
|
+
permanent: false,
|
|
1083
|
+
reason
|
|
1084
|
+
};
|
|
1085
|
+
}
|
|
1086
|
+
this._usedStreak = 0;
|
|
1087
|
+
if (permanent) {
|
|
850
1088
|
this._lastFailure = "permanent";
|
|
851
1089
|
cred.stale = true;
|
|
852
1090
|
} else {
|
|
@@ -911,6 +1149,16 @@ var CodeArtsAccountStore = class {
|
|
|
911
1149
|
this.legacyPath = legacyPath ?? join(dirname(path), ".codearts-auth.json");
|
|
912
1150
|
this.log = options.log;
|
|
913
1151
|
}
|
|
1152
|
+
/**
|
|
1153
|
+
* 所有槽位共用的跨进程续期锁(与守护进程同一个文件)。
|
|
1154
|
+
*
|
|
1155
|
+
* 注意是**整个文档一把锁**,不是每个账号一把:`AccountSlot` 是先刷后整份写回的,
|
|
1156
|
+
* 两个账号同时在刷会让后写的那次把前一个刚轮换出来的 refresh_token 覆盖掉。
|
|
1157
|
+
* 一把锁的代价只是多账号时串行几秒,换来的是不可能互相踩。
|
|
1158
|
+
*/
|
|
1159
|
+
slotLockPath() {
|
|
1160
|
+
return refreshLockPath(dirname(this.path));
|
|
1161
|
+
}
|
|
914
1162
|
async loaded() {
|
|
915
1163
|
this._loading ??= this.load();
|
|
916
1164
|
return this._loading;
|
|
@@ -938,7 +1186,8 @@ var CodeArtsAccountStore = class {
|
|
|
938
1186
|
adopt(doc) {
|
|
939
1187
|
this._doc = doc;
|
|
940
1188
|
this._slots.clear();
|
|
941
|
-
|
|
1189
|
+
const lockPath = this.slotLockPath();
|
|
1190
|
+
for (const [id, cred] of Object.entries(doc.accounts)) this._slots.set(id, new AccountSlot(cred, this.persisterFor(id), this.log, lockPath));
|
|
942
1191
|
}
|
|
943
1192
|
persisterFor(id) {
|
|
944
1193
|
return async (cred) => {
|
|
@@ -985,7 +1234,7 @@ var CodeArtsAccountStore = class {
|
|
|
985
1234
|
this._slots.get(id)?.invalidate();
|
|
986
1235
|
doc.accounts[id] = cred;
|
|
987
1236
|
if (!doc.order.includes(id)) doc.order.push(id);
|
|
988
|
-
this._slots.set(id, new AccountSlot(cred, this.persisterFor(id), this.log));
|
|
1237
|
+
this._slots.set(id, new AccountSlot(cred, this.persisterFor(id), this.log, this.slotLockPath()));
|
|
989
1238
|
await this.persistDoc();
|
|
990
1239
|
await claimIfPossible(cred);
|
|
991
1240
|
return id;
|
|
@@ -1030,7 +1279,7 @@ var CodeArtsAccountStore = class {
|
|
|
1030
1279
|
...identity.domainId === "" ? {} : { domainId: identity.domainId }
|
|
1031
1280
|
};
|
|
1032
1281
|
this._slots.get(id)?.invalidate();
|
|
1033
|
-
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id), this.log));
|
|
1282
|
+
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id), this.log, this.slotLockPath()));
|
|
1034
1283
|
await this.persistDoc();
|
|
1035
1284
|
return "noop";
|
|
1036
1285
|
}
|
|
@@ -1048,7 +1297,7 @@ var CodeArtsAccountStore = class {
|
|
|
1048
1297
|
this._slots.get(id)?.invalidate();
|
|
1049
1298
|
this._slots.delete(id);
|
|
1050
1299
|
this._slots.get(targetKey)?.invalidate();
|
|
1051
|
-
this._slots.set(targetKey, new AccountSlot(doc.accounts[targetKey], this.persisterFor(targetKey), this.log));
|
|
1300
|
+
this._slots.set(targetKey, new AccountSlot(doc.accounts[targetKey], this.persisterFor(targetKey), this.log, this.slotLockPath()));
|
|
1052
1301
|
await this.persistDoc();
|
|
1053
1302
|
return target === void 0 ? "moved" : "merged";
|
|
1054
1303
|
}
|
|
@@ -1085,7 +1334,7 @@ var CodeArtsAccountStore = class {
|
|
|
1085
1334
|
paused
|
|
1086
1335
|
};
|
|
1087
1336
|
this._slots.get(id)?.invalidate();
|
|
1088
|
-
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id), this.log));
|
|
1337
|
+
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id), this.log, this.slotLockPath()));
|
|
1089
1338
|
await this.persistDoc();
|
|
1090
1339
|
return true;
|
|
1091
1340
|
}
|
|
@@ -1100,7 +1349,7 @@ var CodeArtsAccountStore = class {
|
|
|
1100
1349
|
pinned
|
|
1101
1350
|
};
|
|
1102
1351
|
this._slots.get(id)?.invalidate();
|
|
1103
|
-
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id), this.log));
|
|
1352
|
+
this._slots.set(id, new AccountSlot(doc.accounts[id], this.persisterFor(id), this.log, this.slotLockPath()));
|
|
1104
1353
|
await this.persistDoc();
|
|
1105
1354
|
return true;
|
|
1106
1355
|
}
|
|
@@ -1144,11 +1393,8 @@ var CodeArtsAccountStore = class {
|
|
|
1144
1393
|
if (await this.reloadIfPeerAdvanced(id)) {
|
|
1145
1394
|
const retry = this._slots.get(id);
|
|
1146
1395
|
if (retry !== void 0) cred = await retry.ensureFresh();
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
cred = this._slots.get(id)?.credential ?? cred;
|
|
1150
|
-
}
|
|
1151
|
-
} else {
|
|
1396
|
+
}
|
|
1397
|
+
if (this._slots.get(id)?.exhausted === true) {
|
|
1152
1398
|
await this.markStale(id);
|
|
1153
1399
|
cred = this._slots.get(id)?.credential ?? cred;
|
|
1154
1400
|
}
|
|
@@ -1177,7 +1423,7 @@ var CodeArtsAccountStore = class {
|
|
|
1177
1423
|
slot = this._slots.get(id);
|
|
1178
1424
|
next = slot === void 0 ? void 0 : await slot.forceRefresh();
|
|
1179
1425
|
}
|
|
1180
|
-
if (next === void 0) await this.markStale(id);
|
|
1426
|
+
if (next === void 0 && this._slots.get(id)?.exhausted === true) await this.markStale(id);
|
|
1181
1427
|
}
|
|
1182
1428
|
return next;
|
|
1183
1429
|
}
|
|
@@ -1284,4 +1530,4 @@ var CodeArtsAccountStore = class {
|
|
|
1284
1530
|
}
|
|
1285
1531
|
};
|
|
1286
1532
|
//#endregion
|
|
1287
|
-
export { codeartsAuthPath as a, credentialFromOAuth as c,
|
|
1533
|
+
export { codeartsAuthPath as a, credentialFromOAuth as c, refreshLockPath as d, withRefreshLock as f, startLogin as g, refreshToken as h, codeartsAccountsPath as i, readAccountDocument as l, defaultLoginConfig as m, CodeArtsCredentialStore as n, codeartsDebugLogPath as o, OAuthTokenError as p, MANUAL_ACCOUNT_ID as r, codeartsRefreshLogPath as s, CodeArtsAccountStore as t, writeJsonAtomic as u };
|
package/lib/bin.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { i as codeartsAccountsPath, t as CodeArtsAccountStore } from "./auth-
|
|
3
|
-
import { H as isBenefitModel, l as fetchCallerIdentity } from "./upstream-
|
|
4
|
-
import { i as discoverModels, n as CodeArtsUsageTracker, r as codeartsUsagePath, t as CODEARTS_VERSION } from "./version-
|
|
2
|
+
import { i as codeartsAccountsPath, t as CodeArtsAccountStore } from "./auth-B5PDMG7r.mjs";
|
|
3
|
+
import { H as isBenefitModel, l as fetchCallerIdentity } from "./upstream-B_KAbdPy.mjs";
|
|
4
|
+
import { i as discoverModels, n as CodeArtsUsageTracker, r as codeartsUsagePath, t as CODEARTS_VERSION } from "./version-DZ47veC-.mjs";
|
|
5
5
|
import { existsSync, statSync } from "node:fs";
|
|
6
6
|
//#region src/bin.ts
|
|
7
7
|
/**
|
package/lib/client/index.js
CHANGED
|
@@ -156,123 +156,123 @@ if (typeof document !== "undefined" && document.querySelector("style[data-plugin
|
|
|
156
156
|
document.head.appendChild(tag);
|
|
157
157
|
}
|
|
158
158
|
var CodeArtsPluginCard_module_css_default = {
|
|
159
|
-
"
|
|
160
|
-
"
|
|
161
|
-
"
|
|
162
|
-
"
|
|
159
|
+
"count": "xoziZG_count",
|
|
160
|
+
"field": "xoziZG_field",
|
|
161
|
+
"hint": "xoziZG_hint",
|
|
162
|
+
"meta": "xoziZG_meta",
|
|
163
|
+
"check": "xoziZG_check",
|
|
164
|
+
"area": "xoziZG_area",
|
|
165
|
+
"modelMeta": "xoziZG_modelMeta",
|
|
166
|
+
"choiceRow": "xoziZG_choiceRow",
|
|
167
|
+
"updated": "xoziZG_updated",
|
|
168
|
+
"statValue": "xoziZG_statValue",
|
|
169
|
+
"skeletonRow": "xoziZG_skeletonRow",
|
|
170
|
+
"card": "xoziZG_card",
|
|
171
|
+
"headText": "xoziZG_headText",
|
|
163
172
|
"windowPopoverValue": "xoziZG_windowPopoverValue",
|
|
164
|
-
"
|
|
165
|
-
"heatLegendChip": "xoziZG_heatLegendChip",
|
|
166
|
-
"sectionHead": "xoziZG_sectionHead",
|
|
167
|
-
"creditLink": "xoziZG_creditLink",
|
|
168
|
-
"groupLabel": "xoziZG_groupLabel",
|
|
169
|
-
"usageTipName": "xoziZG_usageTipName",
|
|
170
|
-
"accountMenu": "xoziZG_accountMenu",
|
|
171
|
-
"expiryRow": "xoziZG_expiryRow",
|
|
172
|
-
"accountMenuItem": "xoziZG_accountMenuItem",
|
|
173
|
-
"tabPanel": "xoziZG_tabPanel",
|
|
174
|
-
"header": "xoziZG_header",
|
|
175
|
-
"accountGrid": "xoziZG_accountGrid",
|
|
176
|
-
"creditHint": "xoziZG_creditHint",
|
|
177
|
-
"statSub": "xoziZG_statSub",
|
|
173
|
+
"windowButtonSet": "xoziZG_windowButtonSet",
|
|
178
174
|
"heatAxis": "xoziZG_heatAxis",
|
|
175
|
+
"usageTipRow": "xoziZG_usageTipRow",
|
|
176
|
+
"desc": "xoziZG_desc",
|
|
179
177
|
"windowCaret": "xoziZG_windowCaret",
|
|
180
|
-
"
|
|
181
|
-
"windowPopoverRow": "xoziZG_windowPopoverRow",
|
|
182
|
-
"choiceTail": "xoziZG_choiceTail",
|
|
183
|
-
"accountClearHint": "xoziZG_accountClearHint",
|
|
184
|
-
"name": "xoziZG_name",
|
|
178
|
+
"body": "xoziZG_body",
|
|
185
179
|
"usageFill": "xoziZG_usageFill",
|
|
186
|
-
"
|
|
187
|
-
"
|
|
188
|
-
"
|
|
189
|
-
"
|
|
190
|
-
"
|
|
191
|
-
"
|
|
192
|
-
"
|
|
180
|
+
"danger": "xoziZG_danger",
|
|
181
|
+
"usageTipName": "xoziZG_usageTipName",
|
|
182
|
+
"statusLine": "xoziZG_statusLine",
|
|
183
|
+
"balance": "xoziZG_balance",
|
|
184
|
+
"balanceUnit": "xoziZG_balanceUnit",
|
|
185
|
+
"actions": "xoziZG_actions",
|
|
186
|
+
"statSub": "xoziZG_statSub",
|
|
187
|
+
"accountRowActions": "xoziZG_accountRowActions",
|
|
188
|
+
"choiceTail": "xoziZG_choiceTail",
|
|
193
189
|
"doctorList": "xoziZG_doctorList",
|
|
190
|
+
"bar": "xoziZG_bar",
|
|
191
|
+
"accountToolbar": "xoziZG_accountToolbar",
|
|
192
|
+
"windowPopover": "xoziZG_windowPopover",
|
|
194
193
|
"tabs": "xoziZG_tabs",
|
|
195
|
-
"
|
|
196
|
-
"
|
|
197
|
-
"
|
|
198
|
-
"
|
|
194
|
+
"windowButton": "xoziZG_windowButton",
|
|
195
|
+
"accountNameRow": "xoziZG_accountNameRow",
|
|
196
|
+
"accountRow": "xoziZG_accountRow",
|
|
197
|
+
"choices": "xoziZG_choices",
|
|
198
|
+
"windowPopoverRow": "xoziZG_windowPopoverRow",
|
|
199
|
+
"accountClearHint": "xoziZG_accountClearHint",
|
|
200
|
+
"doctorName": "xoziZG_doctorName",
|
|
201
|
+
"accountName": "xoziZG_accountName",
|
|
199
202
|
"creditRow": "xoziZG_creditRow",
|
|
200
|
-
"
|
|
203
|
+
"accountToggle": "xoziZG_accountToggle",
|
|
204
|
+
"groupHeadFirst": "xoziZG_groupHeadFirst",
|
|
205
|
+
"groupLabel": "xoziZG_groupLabel",
|
|
206
|
+
"sectionHead": "xoziZG_sectionHead",
|
|
207
|
+
"windowRange": "xoziZG_windowRange",
|
|
208
|
+
"usageTipSep": "xoziZG_usageTipSep",
|
|
209
|
+
"manual": "xoziZG_manual",
|
|
210
|
+
"error": "xoziZG_error",
|
|
201
211
|
"accountMenuAnchor": "xoziZG_accountMenuAnchor",
|
|
202
|
-
"
|
|
203
|
-
"
|
|
212
|
+
"sectionLabel": "xoziZG_sectionLabel",
|
|
213
|
+
"modelShare": "xoziZG_modelShare",
|
|
214
|
+
"statCard": "xoziZG_statCard",
|
|
215
|
+
"manualForm": "xoziZG_manualForm",
|
|
216
|
+
"modelCheck": "xoziZG_modelCheck",
|
|
217
|
+
"name": "xoziZG_name",
|
|
218
|
+
"accountGrid": "xoziZG_accountGrid",
|
|
204
219
|
"creditFooter": "xoziZG_creditFooter",
|
|
205
|
-
"
|
|
206
|
-
"doctorName": "xoziZG_doctorName",
|
|
220
|
+
"balanceRow": "xoziZG_balanceRow",
|
|
207
221
|
"fieldLabel": "xoziZG_fieldLabel",
|
|
208
|
-
"
|
|
209
|
-
"
|
|
210
|
-
"modelRow": "xoziZG_modelRow",
|
|
211
|
-
"usageTipRow": "xoziZG_usageTipRow",
|
|
212
|
-
"windowUnit": "xoziZG_windowUnit",
|
|
213
|
-
"error": "xoziZG_error",
|
|
222
|
+
"usageBar": "xoziZG_usageBar",
|
|
223
|
+
"statLabel": "xoziZG_statLabel",
|
|
214
224
|
"doctorRow": "xoziZG_doctorRow",
|
|
215
|
-
"
|
|
216
|
-
"
|
|
217
|
-
"
|
|
218
|
-
"
|
|
219
|
-
"
|
|
220
|
-
"
|
|
221
|
-
"
|
|
222
|
-
"
|
|
223
|
-
"modelChart": "xoziZG_modelChart",
|
|
224
|
-
"modelCheck": "xoziZG_modelCheck",
|
|
225
|
-
"headText": "xoziZG_headText",
|
|
226
|
-
"balance": "xoziZG_balance",
|
|
227
|
-
"windowPopover": "xoziZG_windowPopover",
|
|
225
|
+
"modelName": "xoziZG_modelName",
|
|
226
|
+
"accountInfo": "xoziZG_accountInfo",
|
|
227
|
+
"heatHours": "xoziZG_heatHours",
|
|
228
|
+
"modelFill": "xoziZG_modelFill",
|
|
229
|
+
"creditRows": "xoziZG_creditRows",
|
|
230
|
+
"balanceMeta": "xoziZG_balanceMeta",
|
|
231
|
+
"heatAxisSlot": "xoziZG_heatAxisSlot",
|
|
232
|
+
"heatLegendChip": "xoziZG_heatLegendChip",
|
|
228
233
|
"heatCell": "xoziZG_heatCell",
|
|
229
|
-
"
|
|
230
|
-
"
|
|
231
|
-
"usageTip": "xoziZG_usageTip",
|
|
232
|
-
"body": "xoziZG_body",
|
|
234
|
+
"skeletonBreath": "xoziZG_skeletonBreath",
|
|
235
|
+
"creditHint": "xoziZG_creditHint",
|
|
233
236
|
"modelSpec": "xoziZG_modelSpec",
|
|
234
|
-
"
|
|
235
|
-
"modelTrack": "xoziZG_modelTrack",
|
|
236
|
-
"accountEntry": "xoziZG_accountEntry",
|
|
237
|
-
"meta": "xoziZG_meta",
|
|
238
|
-
"groupHead": "xoziZG_groupHead",
|
|
239
|
-
"avatar": "xoziZG_avatar",
|
|
237
|
+
"accountCard": "xoziZG_accountCard",
|
|
240
238
|
"statGrid": "xoziZG_statGrid",
|
|
239
|
+
"heatHourCell": "xoziZG_heatHourCell",
|
|
240
|
+
"header": "xoziZG_header",
|
|
241
241
|
"trendTip": "xoziZG_trendTip",
|
|
242
|
-
"
|
|
243
|
-
"
|
|
244
|
-
"usageTipSep": "xoziZG_usageTipSep",
|
|
245
|
-
"modelFill": "xoziZG_modelFill",
|
|
246
|
-
"statusLine": "xoziZG_statusLine",
|
|
247
|
-
"updated": "xoziZG_updated",
|
|
248
|
-
"windowButtonSet": "xoziZG_windowButtonSet",
|
|
249
|
-
"choices": "xoziZG_choices",
|
|
250
|
-
"windowInputSet": "xoziZG_windowInputSet",
|
|
251
|
-
"creditBlock": "xoziZG_creditBlock",
|
|
252
|
-
"accountToggle": "xoziZG_accountToggle",
|
|
253
|
-
"field": "xoziZG_field",
|
|
254
|
-
"expiryLabel": "xoziZG_expiryLabel",
|
|
255
|
-
"skeletonBreath": "xoziZG_skeletonBreath",
|
|
256
|
-
"accountNameRow": "xoziZG_accountNameRow",
|
|
242
|
+
"modelChart": "xoziZG_modelChart",
|
|
243
|
+
"section": "xoziZG_section",
|
|
257
244
|
"manualSummary": "xoziZG_manualSummary",
|
|
258
|
-
"
|
|
245
|
+
"windowButtonOpen": "xoziZG_windowButtonOpen",
|
|
246
|
+
"accountClear": "xoziZG_accountClear",
|
|
247
|
+
"modelTrack": "xoziZG_modelTrack",
|
|
248
|
+
"windowInputSet": "xoziZG_windowInputSet",
|
|
249
|
+
"skeleton": "xoziZG_skeleton",
|
|
250
|
+
"tabPanel": "xoziZG_tabPanel",
|
|
251
|
+
"windowHint": "xoziZG_windowHint",
|
|
252
|
+
"link": "xoziZG_link",
|
|
253
|
+
"modelRequests": "xoziZG_modelRequests",
|
|
254
|
+
"barFill": "xoziZG_barFill",
|
|
255
|
+
"accountHint": "xoziZG_accountHint",
|
|
256
|
+
"heatGrid": "xoziZG_heatGrid",
|
|
257
|
+
"windowInput": "xoziZG_windowInput",
|
|
258
|
+
"accountMenuItem": "xoziZG_accountMenuItem",
|
|
259
|
+
"modelRow": "xoziZG_modelRow",
|
|
260
|
+
"accountEntry": "xoziZG_accountEntry",
|
|
261
|
+
"expiryRow": "xoziZG_expiryRow",
|
|
262
|
+
"windowPopoverHead": "xoziZG_windowPopoverHead",
|
|
263
|
+
"creditLink": "xoziZG_creditLink",
|
|
264
|
+
"modelTokens": "xoziZG_modelTokens",
|
|
265
|
+
"accountMenu": "xoziZG_accountMenu",
|
|
266
|
+
"avatar": "xoziZG_avatar",
|
|
267
|
+
"usageTip": "xoziZG_usageTip",
|
|
268
|
+
"windowAnchor": "xoziZG_windowAnchor",
|
|
269
|
+
"windowUnit": "xoziZG_windowUnit",
|
|
270
|
+
"groupHead": "xoziZG_groupHead",
|
|
259
271
|
"heatLegend": "xoziZG_heatLegend",
|
|
260
|
-
"
|
|
261
|
-
"accountCard": "xoziZG_accountCard",
|
|
262
|
-
"card": "xoziZG_card",
|
|
263
|
-
"area": "xoziZG_area",
|
|
264
|
-
"heatHours": "xoziZG_heatHours",
|
|
265
|
-
"manualForm": "xoziZG_manualForm",
|
|
272
|
+
"heatWeekday": "xoziZG_heatWeekday",
|
|
266
273
|
"claimAllRow": "xoziZG_claimAllRow",
|
|
267
|
-
"
|
|
268
|
-
"
|
|
269
|
-
"balanceUnit": "xoziZG_balanceUnit",
|
|
270
|
-
"bar": "xoziZG_bar",
|
|
271
|
-
"actions": "xoziZG_actions",
|
|
272
|
-
"windowPopoverHead": "xoziZG_windowPopoverHead",
|
|
273
|
-
"windowHint": "xoziZG_windowHint",
|
|
274
|
-
"modelName": "xoziZG_modelName",
|
|
275
|
-
"skeletonRow": "xoziZG_skeletonRow"
|
|
274
|
+
"expiryLabel": "xoziZG_expiryLabel",
|
|
275
|
+
"creditBlock": "xoziZG_creditBlock"
|
|
276
276
|
};
|
|
277
277
|
//#endregion
|
|
278
278
|
//#region src/client/CodeArtsPluginCard.tsx
|
|
@@ -3289,14 +3289,14 @@ if (typeof document !== "undefined" && document.querySelector("style[data-plugin
|
|
|
3289
3289
|
document.head.appendChild(tag);
|
|
3290
3290
|
}
|
|
3291
3291
|
var CodeArtsPanel_module_css_default = {
|
|
3292
|
-
"title": "Z66cgG_title",
|
|
3293
3292
|
"back": "Z66cgG_back",
|
|
3294
|
-
"head": "Z66cgG_head",
|
|
3295
|
-
"body": "Z66cgG_body",
|
|
3296
3293
|
"desc": "Z66cgG_desc",
|
|
3294
|
+
"icon": "Z66cgG_icon",
|
|
3297
3295
|
"page": "Z66cgG_page",
|
|
3298
|
-
"
|
|
3299
|
-
"
|
|
3296
|
+
"body": "Z66cgG_body",
|
|
3297
|
+
"title": "Z66cgG_title",
|
|
3298
|
+
"head": "Z66cgG_head",
|
|
3299
|
+
"backCaret": "Z66cgG_backCaret"
|
|
3300
3300
|
};
|
|
3301
3301
|
//#endregion
|
|
3302
3302
|
//#region src/client/CodeArtsPanel.tsx
|
package/lib/index.d.mts
CHANGED
|
@@ -1127,6 +1127,14 @@ declare class CodeArtsAccountStore {
|
|
|
1127
1127
|
constructor(path: string, legacyPath?: string, options?: {
|
|
1128
1128
|
log?: (message: string) => void;
|
|
1129
1129
|
});
|
|
1130
|
+
/**
|
|
1131
|
+
* 所有槽位共用的跨进程续期锁(与守护进程同一个文件)。
|
|
1132
|
+
*
|
|
1133
|
+
* 注意是**整个文档一把锁**,不是每个账号一把:`AccountSlot` 是先刷后整份写回的,
|
|
1134
|
+
* 两个账号同时在刷会让后写的那次把前一个刚轮换出来的 refresh_token 覆盖掉。
|
|
1135
|
+
* 一把锁的代价只是多账号时串行几秒,换来的是不可能互相踩。
|
|
1136
|
+
*/
|
|
1137
|
+
private slotLockPath;
|
|
1130
1138
|
private loaded;
|
|
1131
1139
|
private load;
|
|
1132
1140
|
private adopt;
|
package/lib/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { a as codeartsAuthPath, c as credentialFromOAuth,
|
|
2
|
-
import { A as queueFrame, B as forgetModelUnavailable, C as isWelfareClaimedStatus, D as modelUnavailableReasonText, E as maxTokensCeilingFor, F as CodeArtsCatalog, H as isBenefitModel, I as FALLBACK_CODEARTS_MODELS, K as modelUnavailableReason, L as clearBenefitBlocked, M as signedGet, N as upstreamErrorMessage, O as pickDailyWelfareCampaign, R as creditRatioLabel, S as isTpmLimit, T as learnedContextWindowFor, U as markModelUnavailable, V as isBenefitBlocked, _ as isNotMultimodal, a as claimDailyWelfare, b as isQueueRejectText, c as degradeTokenCeiling, d as fetchQueueStatus, f as fetchWelfareCampaigns, g as isModelNotAccessible, h as isIamTokenError, i as chatStream, j as rateLimitFrame, k as prepareChatBody, l as fetchCallerIdentity, m as isConcurrencyCap, n as SNAP_HOST, o as clearQueueStatus, p as firstFrameError, r as canonicalModel, s as contextWindowOverrideFor, t as MAX_TOKENS_FLOOR, u as fetchModels, v as isParamInvalidFrame, w as learnContextWindow, x as isQueueableRate, y as isQueueReject, z as filterEnabledModels } from "./upstream-
|
|
3
|
-
import { a as refreshCatalog, i as discoverModels, n as CodeArtsUsageTracker, r as codeartsUsagePath, t as CODEARTS_VERSION } from "./version-
|
|
1
|
+
import { a as codeartsAuthPath, c as credentialFromOAuth, g as startLogin, i as codeartsAccountsPath, n as CodeArtsCredentialStore, o as codeartsDebugLogPath, r as MANUAL_ACCOUNT_ID, s as codeartsRefreshLogPath, t as CodeArtsAccountStore } from "./auth-B5PDMG7r.mjs";
|
|
2
|
+
import { A as queueFrame, B as forgetModelUnavailable, C as isWelfareClaimedStatus, D as modelUnavailableReasonText, E as maxTokensCeilingFor, F as CodeArtsCatalog, H as isBenefitModel, I as FALLBACK_CODEARTS_MODELS, K as modelUnavailableReason, L as clearBenefitBlocked, M as signedGet, N as upstreamErrorMessage, O as pickDailyWelfareCampaign, R as creditRatioLabel, S as isTpmLimit, T as learnedContextWindowFor, U as markModelUnavailable, V as isBenefitBlocked, _ as isNotMultimodal, a as claimDailyWelfare, b as isQueueRejectText, c as degradeTokenCeiling, d as fetchQueueStatus, f as fetchWelfareCampaigns, g as isModelNotAccessible, h as isIamTokenError, i as chatStream, j as rateLimitFrame, k as prepareChatBody, l as fetchCallerIdentity, m as isConcurrencyCap, n as SNAP_HOST, o as clearQueueStatus, p as firstFrameError, r as canonicalModel, s as contextWindowOverrideFor, t as MAX_TOKENS_FLOOR, u as fetchModels, v as isParamInvalidFrame, w as learnContextWindow, x as isQueueableRate, y as isQueueReject, z as filterEnabledModels } from "./upstream-B_KAbdPy.mjs";
|
|
3
|
+
import { a as refreshCatalog, i as discoverModels, n as CodeArtsUsageTracker, r as codeartsUsagePath, t as CODEARTS_VERSION } from "./version-DZ47veC-.mjs";
|
|
4
4
|
import { i as lazyApi, r as createProvider } from "./models-CgKCibpk.mjs";
|
|
5
|
-
import { c as keeperLogPath, d as keeperStats, f as spawnKeeper, i as clearKeeperHandoff, l as keeperScriptPath, n as KEEPER_STALE_MS, p as stopKeeper, s as keeperHandoffRecent } from "./keeper-
|
|
5
|
+
import { c as keeperLogPath, d as keeperStats, f as spawnKeeper, i as clearKeeperHandoff, l as keeperScriptPath, n as KEEPER_STALE_MS, p as stopKeeper, s as keeperHandoffRecent } from "./keeper-CA6PKtcv.mjs";
|
|
6
6
|
import { appendFileSync, statSync, writeFileSync } from "node:fs";
|
|
7
7
|
import z from "@deepseek-ai/schemastery";
|
|
8
8
|
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { existsSync, readFileSync, readdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { basename, dirname, join } from "node:path";
|
|
4
|
-
import { spawn } from "node:child_process";
|
|
4
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
//#region src/keeper.ts
|
|
7
7
|
/**
|
|
@@ -41,6 +41,8 @@ import { fileURLToPath } from "node:url";
|
|
|
41
41
|
*/
|
|
42
42
|
/** 心跳超过这么久没更新,就认为守护进程已经不在(它每 30 秒写一次)。 */
|
|
43
43
|
const KEEPER_STALE_MS = 9e4;
|
|
44
|
+
/** Windows 上探活要起一次 tasklist:给足超时,卡住就按「活着」处理。 */
|
|
45
|
+
const PID_PROBE_TIMEOUT_MS = 5e3;
|
|
44
46
|
/** 心跳写入间隔。 */
|
|
45
47
|
const KEEPER_HEARTBEAT_MS = 3e4;
|
|
46
48
|
/** 守护进程的续期节奏:每 20 分钟过一遍,与插件里的 `KEEP_ALIVE_INTERVAL_MS` 对齐。 */
|
|
@@ -79,14 +81,42 @@ function writeKeeperState(state, home) {
|
|
|
79
81
|
} catch {}
|
|
80
82
|
}
|
|
81
83
|
/** 进程还在吗。信号 0 只探活,不真的发信号(Windows 上同样适用)。 */
|
|
84
|
+
/**
|
|
85
|
+
* 某个 pid 是否还活着。
|
|
86
|
+
*
|
|
87
|
+
* **不能用 `process.kill(pid, 0)`**:那个「发信号 0 只是探活」的约定只在 POSIX 上
|
|
88
|
+
* 成立。Windows 上 Node 的实现不看信号值,直接 `TerminateProcess` —— 探活会把目标
|
|
89
|
+
* 进程**当场杀掉**。这里的调用方是 `keeperStats()`,探的正是守护进程本身,用错实现
|
|
90
|
+
* 就成了「插件每查一次状态就杀一次 keeper」,而 keeper 一死插件就再也不自己续期。
|
|
91
|
+
*/
|
|
82
92
|
function pidAlive(pid) {
|
|
83
93
|
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
84
|
-
|
|
94
|
+
if (pid === process.pid) return true;
|
|
95
|
+
if (process.platform !== "win32") try {
|
|
85
96
|
process.kill(pid, 0);
|
|
86
97
|
return true;
|
|
87
98
|
} catch (error) {
|
|
88
99
|
return error?.code === "EPERM";
|
|
89
100
|
}
|
|
101
|
+
try {
|
|
102
|
+
const out = execFileSync("tasklist", [
|
|
103
|
+
"/FI",
|
|
104
|
+
`PID eq ${pid}`,
|
|
105
|
+
"/NH"
|
|
106
|
+
], {
|
|
107
|
+
encoding: "utf8",
|
|
108
|
+
stdio: [
|
|
109
|
+
"ignore",
|
|
110
|
+
"pipe",
|
|
111
|
+
"ignore"
|
|
112
|
+
],
|
|
113
|
+
timeout: PID_PROBE_TIMEOUT_MS,
|
|
114
|
+
windowsHide: true
|
|
115
|
+
});
|
|
116
|
+
return new RegExp(`\\b${pid}\\b`).test(out);
|
|
117
|
+
} catch {
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
90
120
|
}
|
|
91
121
|
/** 状态文件的 mtime(用来判断账号文档是不是被守护进程改过)。 */
|
|
92
122
|
function keeperStats(home) {
|
package/lib/keeper-main.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { d as
|
|
2
|
-
import { a as clearKeeperState, c as keeperLogPath, d as keeperStats, g as writeKeeperState, h as writeKeeperHandoff, i as clearKeeperHandoff, m as stopRequested, o as isDshInstallerTempDir, r as KEEPER_TICK_MS, t as KEEPER_HEARTBEAT_MS, u as keeperStatePath } from "./keeper-
|
|
1
|
+
import { d as refreshLockPath, f as withRefreshLock, h as refreshToken, i as codeartsAccountsPath, l as readAccountDocument, m as defaultLoginConfig, p as OAuthTokenError, u as writeJsonAtomic } from "./auth-B5PDMG7r.mjs";
|
|
2
|
+
import { a as clearKeeperState, c as keeperLogPath, d as keeperStats, g as writeKeeperState, h as writeKeeperHandoff, i as clearKeeperHandoff, m as stopRequested, o as isDshInstallerTempDir, r as KEEPER_TICK_MS, t as KEEPER_HEARTBEAT_MS, u as keeperStatePath } from "./keeper-CA6PKtcv.mjs";
|
|
3
3
|
import { appendFileSync, existsSync, unlinkSync, watch } from "node:fs";
|
|
4
|
-
import { join } from "node:path";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
5
|
//#region src/keeper-main.ts
|
|
6
6
|
/**
|
|
7
7
|
* dsh-codearts 凭证守护进程(独立入口,由 DSH 用 `ELECTRON_RUN_AS_NODE=1` 拉起)。
|
|
@@ -23,6 +23,16 @@ import { join } from "node:path";
|
|
|
23
23
|
*/
|
|
24
24
|
/** 距过期不足这么久就续(与服务端插件保持一致)。 */
|
|
25
25
|
const REFRESH_SKEW_MS = 18e5;
|
|
26
|
+
/**
|
|
27
|
+
* `STS5.1806` 连续撞上几次才判死(与 `auth.ts` 的 `REFRESH_USED_MAX_STREAK` 对齐)。
|
|
28
|
+
*
|
|
29
|
+
* 这个码只说明「这个 refresh_token 已经被用掉了」,最常见的原因是插件刚刚先刷了
|
|
30
|
+
* 一次。磁盘上若已经有新令牌,重读就行;磁盘还没更新(对方正在写)时也会撞上,
|
|
31
|
+
* 那更不能判死 —— 下一轮就恢复正常了。
|
|
32
|
+
*/
|
|
33
|
+
const USED_MAX_STREAK = 3;
|
|
34
|
+
/** 睡眠看门狗的检查间隔:机器醒过来最多 30 秒就会补跑一轮。 */
|
|
35
|
+
const SLEEP_WATCHDOG_MS = 3e4;
|
|
26
36
|
function parseHomeArg(argv) {
|
|
27
37
|
const at = argv.indexOf("--home");
|
|
28
38
|
if (at >= 0 && at + 1 < argv.length) return argv[at + 1];
|
|
@@ -58,6 +68,8 @@ function alreadyRunning() {
|
|
|
58
68
|
* 凭证,守护进程必须看见这些变化,否则会把一份过期的内存快照写回去,把刚登录的
|
|
59
69
|
* 号覆盖掉。
|
|
60
70
|
*/
|
|
71
|
+
/** 每个账号连续撞上「令牌已被用掉」的次数;成功续上一次就清零。 */
|
|
72
|
+
const usedStreak = /* @__PURE__ */ new Map();
|
|
61
73
|
async function tick(state) {
|
|
62
74
|
const accountsPath = codeartsAccountsPath(home);
|
|
63
75
|
const doc = await readAccountDocument(accountsPath);
|
|
@@ -86,19 +98,26 @@ async function tick(state) {
|
|
|
86
98
|
if (Number.isNaN(exp)) continue;
|
|
87
99
|
if (exp - Date.now() >= REFRESH_SKEW_MS) continue;
|
|
88
100
|
checked += 1;
|
|
101
|
+
const refreshTokenValue = cred.refreshToken;
|
|
102
|
+
const clientIdValue = cred.clientId;
|
|
103
|
+
const dpopPrivateKeyValue = cred.dpopPrivateKey;
|
|
104
|
+
if (refreshTokenValue === void 0 || clientIdValue === void 0 || dpopPrivateKeyValue === void 0) continue;
|
|
105
|
+
const locked = await withRefreshLock(refreshLockPath(dirname(accountsPath)), () => refreshToken(defaultLoginConfig(), {
|
|
106
|
+
userId: cred.userId ?? "",
|
|
107
|
+
userName: cred.userName ?? "",
|
|
108
|
+
domainId: "",
|
|
109
|
+
securityToken: cred.token,
|
|
110
|
+
accessKeyId: cred.accessKeyId ?? "",
|
|
111
|
+
secretAccessKey: cred.secretAccessKey ?? "",
|
|
112
|
+
expiration: cred.expiration ?? "",
|
|
113
|
+
refreshToken: refreshTokenValue,
|
|
114
|
+
clientId: clientIdValue,
|
|
115
|
+
dpopPrivateKey: dpopPrivateKeyValue
|
|
116
|
+
}), { onBusy: () => log(`refresh SKIPPED ${id.slice(0, 12)} (plugin holds the refresh lock)`) });
|
|
117
|
+
if (locked === void 0) continue;
|
|
89
118
|
try {
|
|
90
|
-
const next =
|
|
91
|
-
|
|
92
|
-
userName: cred.userName ?? "",
|
|
93
|
-
domainId: "",
|
|
94
|
-
securityToken: cred.token,
|
|
95
|
-
accessKeyId: cred.accessKeyId ?? "",
|
|
96
|
-
secretAccessKey: cred.secretAccessKey ?? "",
|
|
97
|
-
expiration: cred.expiration ?? "",
|
|
98
|
-
refreshToken: cred.refreshToken,
|
|
99
|
-
clientId: cred.clientId,
|
|
100
|
-
dpopPrivateKey: cred.dpopPrivateKey
|
|
101
|
-
});
|
|
119
|
+
const next = locked;
|
|
120
|
+
usedStreak.delete(id);
|
|
102
121
|
doc.accounts[id] = {
|
|
103
122
|
...cred,
|
|
104
123
|
token: next.securityToken,
|
|
@@ -116,14 +135,19 @@ async function tick(state) {
|
|
|
116
135
|
log(`refreshed ${id.slice(0, 12)} → expires ${next.expiration}`);
|
|
117
136
|
} catch (error) {
|
|
118
137
|
const used = error instanceof OAuthTokenError && error.refreshTokenUsed;
|
|
138
|
+
let markStale = false;
|
|
119
139
|
if (used) {
|
|
120
140
|
const fresh = await readAccountDocument(accountsPath);
|
|
121
141
|
const advanced = fresh !== void 0 && fresh.accounts[id]?.refreshToken !== void 0 && fresh.accounts[id]?.refreshToken !== cred.refreshToken;
|
|
122
|
-
|
|
123
|
-
|
|
142
|
+
const streak = (usedStreak.get(id) ?? 0) + 1;
|
|
143
|
+
usedStreak.set(id, streak);
|
|
144
|
+
markStale = !advanced && streak >= USED_MAX_STREAK;
|
|
145
|
+
log(`refresh SKIPPED ${id.slice(0, 12)} (refresh token already used${advanced ? ", newer token on disk" : ""}, streak ${streak}/${USED_MAX_STREAK})`);
|
|
146
|
+
} else {
|
|
147
|
+
usedStreak.delete(id);
|
|
148
|
+
markStale = error instanceof OAuthTokenError && error.permanent;
|
|
124
149
|
}
|
|
125
|
-
|
|
126
|
-
if (permanent) {
|
|
150
|
+
if (markStale) {
|
|
127
151
|
doc.accounts[id] = {
|
|
128
152
|
...cred,
|
|
129
153
|
stale: true
|
|
@@ -132,7 +156,7 @@ async function tick(state) {
|
|
|
132
156
|
needsLogin.push(id);
|
|
133
157
|
}
|
|
134
158
|
const reason = error instanceof OAuthTokenError ? `HTTP ${error.status} ${error.body.slice(0, 200)}` : error instanceof Error ? error.message : String(error);
|
|
135
|
-
log(`refresh FAILED ${id.slice(0, 12)} (${
|
|
159
|
+
log(`refresh FAILED ${id.slice(0, 12)} (${markStale ? "permanent, marked stale" : "transient, retry next pass"}): ${reason}`);
|
|
136
160
|
}
|
|
137
161
|
}
|
|
138
162
|
if (dirty) await writeJsonAtomic(accountsPath, doc);
|
|
@@ -231,6 +255,8 @@ async function main() {
|
|
|
231
255
|
writeKeeperState(state, home);
|
|
232
256
|
if (stopRequested(home)) shutdown("stop requested by plugin");
|
|
233
257
|
}, KEEPER_HEARTBEAT_MS);
|
|
258
|
+
let timer;
|
|
259
|
+
let lastPassAt = 0;
|
|
234
260
|
const loop = async () => {
|
|
235
261
|
if (stopped) return;
|
|
236
262
|
if (stopRequested(home)) {
|
|
@@ -242,11 +268,29 @@ async function main() {
|
|
|
242
268
|
} catch (error) {
|
|
243
269
|
log(`pass failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
244
270
|
}
|
|
245
|
-
|
|
271
|
+
lastPassAt = Date.now();
|
|
272
|
+
state.heartbeatAt = lastPassAt;
|
|
246
273
|
writeKeeperState(state, home);
|
|
247
274
|
if (stopped) return;
|
|
248
|
-
setTimeout(() => void loop(), KEEPER_TICK_MS);
|
|
275
|
+
timer = setTimeout(() => void loop(), KEEPER_TICK_MS);
|
|
249
276
|
};
|
|
277
|
+
/**
|
|
278
|
+
* 睡眠/休眠补偿:setTimeout 只保证「不早于」,机器睡过去的那段时间它是停摆的。
|
|
279
|
+
*
|
|
280
|
+
* 2026-11-18 的现场:守护进程 pid 全程活着,日志却在 06:46–08:06 之间缺了 4 轮
|
|
281
|
+
* tick —— 机器睡了,定时器跟着停。而插件那边看到心跳文件在、pid 也活,就按
|
|
282
|
+
* `keeperRunning()` 让路,于是两边一起不刷,等醒过来凭证已经过期。
|
|
283
|
+
*
|
|
284
|
+
* 这里每 30 秒对一次表:超过一个 tick 周期没跑过一轮就立刻补一轮。补跑会重排
|
|
285
|
+
* 定时器,所以睡醒后不会连撞两次。
|
|
286
|
+
*/
|
|
287
|
+
setInterval(() => {
|
|
288
|
+
if (stopped || timer === void 0) return;
|
|
289
|
+
if (Date.now() - lastPassAt < 15e5) return;
|
|
290
|
+
clearTimeout(timer);
|
|
291
|
+
log(`wake-from-sleep: no pass for ${Math.round((Date.now() - lastPassAt) / 1e3)}s, running one now`);
|
|
292
|
+
loop();
|
|
293
|
+
}, SLEEP_WATCHDOG_MS);
|
|
250
294
|
loop();
|
|
251
295
|
}
|
|
252
296
|
main().catch((error) => {
|
|
@@ -1586,7 +1586,7 @@ async function fetchAgentModels(credential, signal) {
|
|
|
1586
1586
|
name: firstString(model.model_name, model.model_alias, id),
|
|
1587
1587
|
contextWindow,
|
|
1588
1588
|
...contextWindow > 0 ? { contextWindowKnown: true } : {},
|
|
1589
|
-
maxTokens: positive(params.max_tokens),
|
|
1589
|
+
maxTokens: positive(params.max_tokens) || positive(params.output_context_window),
|
|
1590
1590
|
supportsImages: model.supports_images === true,
|
|
1591
1591
|
...ratio === void 0 ? {} : { ratio }
|
|
1592
1592
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { u as writeJsonAtomic } from "./auth-
|
|
2
|
-
import { G as modelDisplayName, H as isBenefitModel, W as mergeModelLists, q as setDiscoveredBenefitModels, u as fetchModels } from "./upstream-
|
|
1
|
+
import { u as writeJsonAtomic } from "./auth-B5PDMG7r.mjs";
|
|
2
|
+
import { G as modelDisplayName, H as isBenefitModel, W as mergeModelLists, q as setDiscoveredBenefitModels, u as fetchModels } from "./upstream-B_KAbdPy.mjs";
|
|
3
3
|
import { existsSync } from "node:fs";
|
|
4
4
|
import { readFile } from "node:fs/promises";
|
|
5
5
|
import { homedir } from "node:os";
|
|
@@ -535,6 +535,6 @@ function percentileOf(buckets, ratio) {
|
|
|
535
535
|
}
|
|
536
536
|
//#endregion
|
|
537
537
|
//#region src/version.ts
|
|
538
|
-
const CODEARTS_VERSION = "1.4.
|
|
538
|
+
const CODEARTS_VERSION = "1.4.5";
|
|
539
539
|
//#endregion
|
|
540
540
|
export { refreshCatalog as a, discoverModels as i, CodeArtsUsageTracker as n, codeartsUsagePath as r, CODEARTS_VERSION as t };
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "dsh-codearts",
|
|
3
3
|
"displayName": "DSH CodeArts Connect",
|
|
4
4
|
"description": "把华为云 CodeArts 接入 DeepSeek Harness —— 内置 OAuth2 登录(PKCE + DPoP),登录后即可在 DSH 对话窗口使用 CodeArts 的模型。",
|
|
5
|
-
"version": "1.4.
|
|
5
|
+
"version": "1.4.5",
|
|
6
6
|
"packageManager": "pnpm@11.24.0",
|
|
7
7
|
"author": "elegant01",
|
|
8
8
|
"keywords": [
|