dsh-codearts 1.2.13 → 1.3.0
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 +88 -0
- package/lib/auth-CF9XHMX-.mjs +1008 -0
- package/lib/bin.mjs +3 -1
- package/lib/client/index.d.cts +19 -1
- package/lib/client/index.js +308 -117
- package/lib/index.d.mts +63 -1
- package/lib/index.mjs +474 -77
- package/lib/keeper-P4FkDUEd.mjs +215 -0
- package/lib/keeper-main.d.mts +1 -0
- package/lib/keeper-main.mjs +199 -0
- package/lib/{version-E5iTxpWh.mjs → upstream-CVtspzAv.mjs} +19 -1473
- package/lib/version-CJTTrulR.mjs +539 -0
- package/package.json +1 -1
package/lib/index.mjs
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as codeartsAuthPath, d as startLogin, i as codeartsAccountsPath, n as CodeArtsCredentialStore, o as credentialFromOAuth, r as MANUAL_ACCOUNT_ID, t as CodeArtsAccountStore } from "./auth-CF9XHMX-.mjs";
|
|
2
|
+
import { A as clearBenefitBlocked, C as prepareChatBody, E as upstreamErrorMessage, F as isBenefitModel, I as markModelUnavailable, M as filterEnabledModels, N as forgetModelUnavailable, O as CodeArtsCatalog, P as isBenefitBlocked, S as pickDailyWelfareCampaign, T as signedGet, _ as isQueueableRate, a as clearQueueStatus, b as maxTokensCeilingFor, c as fetchModels, d as firstFrameError, f as isConcurrencyCap, g as isParamInvalidFrame, h as isNotMultimodal, i as claimDailyWelfare, j as creditRatioLabel, k as FALLBACK_CODEARTS_MODELS, l as fetchQueueStatus, m as isModelNotAccessible, n as canonicalModel, o as degradeTokenCeiling, p as isIamTokenError, r as chatStream, s as fetchCallerIdentity, t as SNAP_HOST, u as fetchWelfareCampaigns, v as isTpmLimit, w as rateLimitFrame, x as modelUnavailableReasonText, y as isWelfareClaimedStatus, z as modelUnavailableReason } from "./upstream-CVtspzAv.mjs";
|
|
3
|
+
import { a as refreshCatalog, i as discoverModels, n as CodeArtsUsageTracker, r as codeartsUsagePath, t as CODEARTS_VERSION } from "./version-CJTTrulR.mjs";
|
|
2
4
|
import { i as lazyApi, r as createProvider } from "./models-CgKCibpk.mjs";
|
|
5
|
+
import { a as keeperLogPath, c as keeperStats, d as stopKeeper, n as KEEPER_STALE_MS, o as keeperScriptPath, u as spawnKeeper } from "./keeper-P4FkDUEd.mjs";
|
|
3
6
|
import z from "@deepseek-ai/schemastery";
|
|
4
7
|
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
5
8
|
import { createServer } from "node:http";
|
|
@@ -12,8 +15,35 @@ import * as settingsModule from "@deepseek-ai/dsh-settings";
|
|
|
12
15
|
* 这不是冷却:账号本身没坏,只是它的会话槽位暂时满了(约 52 秒才释放)。
|
|
13
16
|
* 所以只是一个「倾向」——池里还有别的账号就先换一个;全都这样时照样会选它,
|
|
14
17
|
* 因为排队等它总比直接失败强。
|
|
18
|
+
*
|
|
19
|
+
* 这里只是**默认值**:每个号的真实释放时长不一样(52 秒是实测均值,不是常数),
|
|
20
|
+
* 一旦量到过就不再用它 —— 见 `noteCapRelease` 与 `capHintFor`。以前固定 30 秒比
|
|
21
|
+
* 实测短 20 多秒,窗口一过这个号又变成优先候选,于是白打一次上游、再记一次 cap,
|
|
22
|
+
* 而每次 cap 又把窗口重置成同样的 30 秒,永远学不到真实时长。
|
|
15
23
|
*/
|
|
16
24
|
const CAPPED_HINT_MS = 3e4;
|
|
25
|
+
/** 实测样本的下界:比这还短说明量的不是槽位释放(比如立刻就成功了)。 */
|
|
26
|
+
const CAP_SAMPLE_MIN_MS = 5e3;
|
|
27
|
+
/**
|
|
28
|
+
* 实测样本的上界:比这还长说明样本不来自「同一请求内排队等槽位」。
|
|
29
|
+
* 脏样本(用户隔几分钟才再问一次)会把避让窗口拉到几分钟,卡片会长时间显示「避让中」,
|
|
30
|
+
* 比固定 30 秒更糟 —— 所以宁可丢掉。
|
|
31
|
+
*/
|
|
32
|
+
const CAP_SAMPLE_MAX_MS = 12e4;
|
|
33
|
+
/** 参与中位数的最近样本数。 */
|
|
34
|
+
const CAP_SAMPLE_KEEP = 5;
|
|
35
|
+
/**
|
|
36
|
+
* 这个号的「优先避开」窗口:量到过就用实测中位数,没量过用默认提示。
|
|
37
|
+
*
|
|
38
|
+
* 取中位数而不是均值:一次被打断的请求可能让某个样本偏长,中位数不会被单点带跑。
|
|
39
|
+
*/
|
|
40
|
+
function capHintFor(account) {
|
|
41
|
+
const samples = account.capSamples;
|
|
42
|
+
if (samples.length === 0) return CAPPED_HINT_MS;
|
|
43
|
+
const sorted = [...samples].sort((a, b) => a - b);
|
|
44
|
+
const mid = Math.floor(sorted.length / 2);
|
|
45
|
+
return sorted.length % 2 === 1 ? sorted[mid] ?? 3e4 : Math.round(((sorted[mid - 1] ?? 3e4) + (sorted[mid] ?? 3e4)) / 2);
|
|
46
|
+
}
|
|
17
47
|
var CodeArtsAccountPool = class {
|
|
18
48
|
accounts = /* @__PURE__ */ new Map();
|
|
19
49
|
/** 用户手动暂停的账号:运行时状态留着(卡片还看得见槽位),但不参与轮转。 */
|
|
@@ -39,6 +69,8 @@ var CodeArtsAccountPool = class {
|
|
|
39
69
|
maxConcurrent: this.maxConcurrent,
|
|
40
70
|
coolUntil: 0,
|
|
41
71
|
cappedUntil: 0,
|
|
72
|
+
lastCapAt: 0,
|
|
73
|
+
capSamples: [],
|
|
42
74
|
disabled: false,
|
|
43
75
|
lastUsed: 0,
|
|
44
76
|
errorCount: 0
|
|
@@ -113,7 +145,25 @@ var CodeArtsAccountPool = class {
|
|
|
113
145
|
noteConcurrencyCap(id) {
|
|
114
146
|
const account = this.accounts.get(id);
|
|
115
147
|
if (account === void 0) return;
|
|
116
|
-
account.
|
|
148
|
+
account.lastCapAt = this.now();
|
|
149
|
+
account.cappedUntil = this.now() + capHintFor(account);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 撞过上限、又在**同一个请求里**排队等到成功 —— 这就是这个号槽位释放的实测时长。
|
|
153
|
+
*
|
|
154
|
+
* 只在兜底排队那条路上调用:主循环里普通成功不能当样本,那样量到的是「用户下一次
|
|
155
|
+
* 提问隔了多久」(常常是几分钟),窗口会被拉到几分钟,比固定 30 秒更糟。
|
|
156
|
+
*/
|
|
157
|
+
noteCapRelease(id) {
|
|
158
|
+
const account = this.accounts.get(id);
|
|
159
|
+
if (account === void 0) return;
|
|
160
|
+
const cappedAt = account.lastCapAt;
|
|
161
|
+
account.lastCapAt = 0;
|
|
162
|
+
if (cappedAt === 0) return;
|
|
163
|
+
const sample = this.now() - cappedAt;
|
|
164
|
+
if (sample < CAP_SAMPLE_MIN_MS || sample > CAP_SAMPLE_MAX_MS) return;
|
|
165
|
+
account.capSamples.push(sample);
|
|
166
|
+
if (account.capSamples.length > CAP_SAMPLE_KEEP) account.capSamples.shift();
|
|
117
167
|
}
|
|
118
168
|
/** 凭据失效等硬错误:直接停用,等用户重新登录。 */
|
|
119
169
|
disable(id, reason) {
|
|
@@ -135,6 +185,7 @@ var CodeArtsAccountPool = class {
|
|
|
135
185
|
account.disabled = false;
|
|
136
186
|
account.coolUntil = 0;
|
|
137
187
|
account.cappedUntil = 0;
|
|
188
|
+
account.lastCapAt = 0;
|
|
138
189
|
account.errorCount = 0;
|
|
139
190
|
account.lastError = void 0;
|
|
140
191
|
}
|
|
@@ -160,6 +211,8 @@ var CodeArtsAccountPool = class {
|
|
|
160
211
|
account.errorCount = 0;
|
|
161
212
|
account.coolUntil = 0;
|
|
162
213
|
account.lastError = void 0;
|
|
214
|
+
account.cappedUntil = 0;
|
|
215
|
+
account.lastCapAt = 0;
|
|
163
216
|
}
|
|
164
217
|
/** 有没有任何账号可用(含被撞上限提示的)。 */
|
|
165
218
|
hasUsable() {
|
|
@@ -953,11 +1006,25 @@ function readBody$1(req) {
|
|
|
953
1006
|
req.on("error", reject);
|
|
954
1007
|
});
|
|
955
1008
|
}
|
|
956
|
-
/**
|
|
957
|
-
const MAX_ROTATE = 3;
|
|
1009
|
+
/** 一次请求最多把 max_tokens 降几档(65536 → 32768 → 16384 → 8192)。 */
|
|
958
1010
|
const MAX_CEILING_HEALS = 3;
|
|
959
1011
|
/**
|
|
960
|
-
*
|
|
1012
|
+
* 一次请求最多试几个账号:clamp(可用账号数, ROTATE_MIN_ATTEMPTS, ROTATE_MAX_ATTEMPTS)。
|
|
1013
|
+
*
|
|
1014
|
+
* 原来固定试 3 个(与 Go 参考的 MaxRotate 一致)。多号才是这个插件的核心场景:
|
|
1015
|
+
* 5 个号时前 3 个在这一轮里都新报了并发上限,第 4 个明明空着也轮不到,只能落到
|
|
1016
|
+
* 下面那根「原地排队等 52 秒」的稻草上 —— 那 52 秒本来是可以绕开的。
|
|
1017
|
+
*
|
|
1018
|
+
* 上界 6 只是次数天花板,真正的刹车是 CAP_WAIT_LIMIT 那个共用截止点:每次尝试都要
|
|
1019
|
+
* 挂号 + 开一次流,号再多也不能无限试下去。
|
|
1020
|
+
*/
|
|
1021
|
+
const ROTATE_MIN_ATTEMPTS = 3;
|
|
1022
|
+
const ROTATE_MAX_ATTEMPTS = 6;
|
|
1023
|
+
/** 开始下一次尝试前必须留下的预算:一次挂号最多等这么久。 */
|
|
1024
|
+
const ATTEMPT_RESERVE_MS = 2e4;
|
|
1025
|
+
/**
|
|
1026
|
+
* 一次请求在「换号轮换 + 回落到同一账号原地重发」上总共能花的墙上时间,从进入
|
|
1027
|
+
* 轮换起算,两段共用一个截止点。
|
|
961
1028
|
*
|
|
962
1029
|
* 2026-09-30 实测:一个上游会话要 ~52s 才被释放,而且**中途打断并不减免** ——
|
|
963
1030
|
* 只跑了 98ms 就中止的请求,同样占满一个名额 52s。所以用户「打断 → 马上重问」
|
|
@@ -965,7 +1032,8 @@ const MAX_CEILING_HEALS = 3;
|
|
|
965
1032
|
* 稻草,必须盖过 52s,否则用户看到的不是「稍等一下」而是直接失败。
|
|
966
1033
|
*
|
|
967
1034
|
* 上限受 pi-ai 的 90s idle 超时约束:等待期间我们发不出任何内容,超过 90s 用户
|
|
968
|
-
* 看到的就是超时。75s 给 52s 留了余量,也还在 90s
|
|
1035
|
+
* 看到的就是超时。75s 给 52s 留了余量,也还在 90s 之内。以前这个预算只归兜底那一段
|
|
1036
|
+
* 自己,轮换花掉的时间是额外加的,多号最坏能顶穿 90s —— 所以现在是共用截止点。
|
|
969
1037
|
*/
|
|
970
1038
|
const CAP_WAIT_LIMIT = 75e3;
|
|
971
1039
|
const CAP_RETRY_GAP = 3e3;
|
|
@@ -1175,6 +1243,9 @@ function createCodeArtsShim(options) {
|
|
|
1175
1243
|
/**
|
|
1176
1244
|
* 在**同一个账号**上重发,直到不再被限流或预算耗尽。
|
|
1177
1245
|
*
|
|
1246
|
+
* `untilAt` 由调用方给:它和前面的换号轮换是同一个截止点,不是这段自己重新起算,
|
|
1247
|
+
* 否则两段各自 75s 会顶穿宿主的 90s 流空闲超时。
|
|
1248
|
+
*
|
|
1178
1249
|
* 并发槽位是账号级的:有别的号就该换号,等 52s 是最后手段。
|
|
1179
1250
|
* TPM 是模型级配额:换号只是换个账号撞同一堵墙,只能原地等窗口滑过去 ——
|
|
1180
1251
|
* 间隔逐次翻倍(3s → 6s → 12s → 24s,封顶 30s),避免在配额已经满的时候
|
|
@@ -1182,9 +1253,8 @@ function createCodeArtsShim(options) {
|
|
|
1182
1253
|
*
|
|
1183
1254
|
* 命中限流不甩给 pi-ai:429 会被重试 5 次,每次再撞一次限流,越试越满。
|
|
1184
1255
|
*/
|
|
1185
|
-
async function openStream(credential, raw, contextWindow, signal) {
|
|
1256
|
+
async function openStream(credential, raw, contextWindow, signal, untilAt) {
|
|
1186
1257
|
const started = Date.now();
|
|
1187
|
-
const until = started + CAP_WAIT_LIMIT;
|
|
1188
1258
|
let gap = CAP_RETRY_GAP;
|
|
1189
1259
|
for (;;) {
|
|
1190
1260
|
const opened = await openWithCeilingHeal(credential, raw, contextWindow, signal);
|
|
@@ -1198,7 +1268,7 @@ function createCodeArtsShim(options) {
|
|
|
1198
1268
|
waitedMs
|
|
1199
1269
|
};
|
|
1200
1270
|
if (isTpmLimit(opened.result)) gap = Math.min(gap * 2, 3e4);
|
|
1201
|
-
if (Date.now() + gap >
|
|
1271
|
+
if (Date.now() + gap > untilAt) return {
|
|
1202
1272
|
...opened,
|
|
1203
1273
|
waitedMs
|
|
1204
1274
|
};
|
|
@@ -1268,7 +1338,10 @@ function createCodeArtsShim(options) {
|
|
|
1268
1338
|
let lastAccountId;
|
|
1269
1339
|
let lastStatus = 0;
|
|
1270
1340
|
let lastMessage = "";
|
|
1271
|
-
|
|
1341
|
+
const rotateDeadline = Date.now() + CAP_WAIT_LIMIT;
|
|
1342
|
+
const attempts = Math.min(Math.max(ids.length, ROTATE_MIN_ATTEMPTS), ROTATE_MAX_ATTEMPTS);
|
|
1343
|
+
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
1344
|
+
if (Date.now() + ATTEMPT_RESERVE_MS > rotateDeadline) break;
|
|
1272
1345
|
const id = pool.pick(tried);
|
|
1273
1346
|
if (id === void 0) break;
|
|
1274
1347
|
tried.add(id);
|
|
@@ -1353,7 +1426,7 @@ function createCodeArtsShim(options) {
|
|
|
1353
1426
|
if (pool.acquire(queueAccount)) try {
|
|
1354
1427
|
const credential = await store.resolve(queueAccount);
|
|
1355
1428
|
if (credential !== void 0) {
|
|
1356
|
-
const opened = await openStream(credential, raw, contextWindow, controller.signal);
|
|
1429
|
+
const opened = await openStream(credential, raw, contextWindow, controller.signal, rotateDeadline);
|
|
1357
1430
|
const { result } = opened;
|
|
1358
1431
|
queueWaitedMs = opened.waitedMs;
|
|
1359
1432
|
if (controller.signal.aborted) {
|
|
@@ -1361,6 +1434,7 @@ function createCodeArtsShim(options) {
|
|
|
1361
1434
|
return;
|
|
1362
1435
|
}
|
|
1363
1436
|
if (result.ok) {
|
|
1437
|
+
pool.noteCapRelease(queueAccount);
|
|
1364
1438
|
pool.noteSuccess(queueAccount);
|
|
1365
1439
|
await pipeToResponse(res, result.stream, controller.signal, {
|
|
1366
1440
|
accountId: queueAccount,
|
|
@@ -1513,6 +1587,15 @@ const CODEARTS_QUEUE_PATH = "/api/codearts/queue";
|
|
|
1513
1587
|
* GET 给当前值与 writable,POST 收 `{credits?, queue?}` 写进 settings。
|
|
1514
1588
|
*/
|
|
1515
1589
|
const CODEARTS_DOCK_PATH = "/api/codearts/dock";
|
|
1590
|
+
/**
|
|
1591
|
+
* POST /api/codearts/keeper/stop — 结束常驻守护进程,并把开关**同时**关掉。
|
|
1592
|
+
*
|
|
1593
|
+
* 为什么必须在服务端把开关一起关:进程被 kill 之后,「开关开着但没进程」是个自相
|
|
1594
|
+
* 矛盾的状态。若只杀进程不改开关,用户下次打开 DSH 会看到开关还是开的、PID 却是空
|
|
1595
|
+
* 的,以为功能坏了;而且插件会按「开关是开的」去重起它 —— 用户刚 kill 掉的东西自己
|
|
1596
|
+
* 又回来了。所以这两件事必须原子地一起做。
|
|
1597
|
+
*/
|
|
1598
|
+
const CODEARTS_KEEPER_STOP_PATH = "/api/codearts/keeper/stop";
|
|
1516
1599
|
/** 最近出站请求的**形状**摘要(只有 role 名与计数,不含内容),排障用。 */
|
|
1517
1600
|
const CODEARTS_DEBUG_PATH = "/api/codearts/debug";
|
|
1518
1601
|
/**
|
|
@@ -1586,11 +1669,15 @@ function modelBadgesOf(models) {
|
|
|
1586
1669
|
/**
|
|
1587
1670
|
* 开关的默认值:没有 settings provider(或老配置没这两个键)时保持原有行为 ——
|
|
1588
1671
|
* 底部两行都显示;侧边栏相反,缺省不开(那一格是所有插件共用的全局座位)。
|
|
1672
|
+
* `modelDetail` 缺省开(保持老版本的样子),`autoClaim` 缺省关。
|
|
1589
1673
|
*/
|
|
1590
1674
|
const DEFAULT_DOCK_FLAGS = {
|
|
1591
1675
|
credits: true,
|
|
1592
1676
|
queue: true,
|
|
1593
|
-
sidebar: false
|
|
1677
|
+
sidebar: false,
|
|
1678
|
+
modelDetail: true,
|
|
1679
|
+
autoClaim: false,
|
|
1680
|
+
keepAliveDaemon: false
|
|
1594
1681
|
};
|
|
1595
1682
|
/**
|
|
1596
1683
|
* 把逐账号的领取结果汇总成一条:HTTP 层只看 ok,卡片看 results 逐号展示。
|
|
@@ -1803,6 +1890,7 @@ async function buildStatus(deps, force = false) {
|
|
|
1803
1890
|
const cred = await deps.store.current();
|
|
1804
1891
|
const usage = usageOf(deps);
|
|
1805
1892
|
const dock = deps.dock === void 0 ? {} : { dock: deps.dock() };
|
|
1893
|
+
const keeperView = deps.keeper === void 0 ? {} : { keeper: await deps.keeper() };
|
|
1806
1894
|
const piAi = await probePiAiCompat();
|
|
1807
1895
|
const compat = piAi === void 0 ? {} : { piAiError: piAi };
|
|
1808
1896
|
if (accounts.length === 0 || cred === void 0) return {
|
|
@@ -1811,6 +1899,7 @@ async function buildStatus(deps, force = false) {
|
|
|
1811
1899
|
accounts,
|
|
1812
1900
|
usage,
|
|
1813
1901
|
...dock,
|
|
1902
|
+
...keeperView,
|
|
1814
1903
|
...compat
|
|
1815
1904
|
};
|
|
1816
1905
|
const usable = accounts.some(accountUsable);
|
|
@@ -1820,7 +1909,8 @@ async function buildStatus(deps, force = false) {
|
|
|
1820
1909
|
models,
|
|
1821
1910
|
accounts,
|
|
1822
1911
|
usage,
|
|
1823
|
-
...dock
|
|
1912
|
+
...dock,
|
|
1913
|
+
...keeperView
|
|
1824
1914
|
};
|
|
1825
1915
|
if (!usable) return {
|
|
1826
1916
|
status: "expired",
|
|
@@ -1970,10 +2060,13 @@ function dockHandler(deps) {
|
|
|
1970
2060
|
const touched = [
|
|
1971
2061
|
"credits",
|
|
1972
2062
|
"queue",
|
|
1973
|
-
"sidebar"
|
|
2063
|
+
"sidebar",
|
|
2064
|
+
"modelDetail",
|
|
2065
|
+
"autoClaim",
|
|
2066
|
+
"keepAliveDaemon"
|
|
1974
2067
|
].filter((key) => body[key] !== void 0);
|
|
1975
2068
|
if (touched.length === 0 || touched.some((key) => typeof body[key] !== "boolean")) {
|
|
1976
|
-
json(res, 400, { error: "expected {\"credits\"?: boolean, \"queue\"?: boolean, \"sidebar\"?: boolean}" });
|
|
2069
|
+
json(res, 400, { error: "expected {\"credits\"?: boolean, \"queue\"?: boolean, \"sidebar\"?: boolean, \"modelDetail\"?: boolean, \"autoClaim\"?: boolean, \"keepAliveDaemon\"?: boolean}" });
|
|
1977
2070
|
return;
|
|
1978
2071
|
}
|
|
1979
2072
|
if (!await deps.setDock(Object.fromEntries(touched.map((key) => [key, body[key]])))) {
|
|
@@ -1992,6 +2085,42 @@ function dockHandler(deps) {
|
|
|
1992
2085
|
json(res, 405, { error: "method not allowed" });
|
|
1993
2086
|
};
|
|
1994
2087
|
}
|
|
2088
|
+
/**
|
|
2089
|
+
* POST /api/codearts/keeper/stop — 结束守护进程,并把开关一起关掉。
|
|
2090
|
+
*
|
|
2091
|
+
* 顺序不能反:先杀进程、再写开关。反过来的话,写开关的那一瞬间如果正好触发了插件的
|
|
2092
|
+
* 重排(`armKeepAliveDaemon`),它会按「开关是开的」再起一个 —— 用户点一次结束,
|
|
2093
|
+
* 结果又冒出来一个新 pid。杀完再写,重排看到的是 false,自然什么都不做。
|
|
2094
|
+
*/
|
|
2095
|
+
function keeperStopHandler(deps) {
|
|
2096
|
+
return async (req, res) => {
|
|
2097
|
+
if (!loopbackRequest(req)) {
|
|
2098
|
+
json(res, 403, { error: "request-not-trusted" });
|
|
2099
|
+
return;
|
|
2100
|
+
}
|
|
2101
|
+
if (req.method !== "POST") {
|
|
2102
|
+
json(res, 405, { error: "method not allowed" });
|
|
2103
|
+
return;
|
|
2104
|
+
}
|
|
2105
|
+
if (deps.stopKeeper === void 0) {
|
|
2106
|
+
json(res, 503, { error: "keeper-control-unavailable" });
|
|
2107
|
+
return;
|
|
2108
|
+
}
|
|
2109
|
+
try {
|
|
2110
|
+
const outcome = await deps.stopKeeper();
|
|
2111
|
+
const writable = deps.setDock === void 0 ? false : await deps.setDock({ keepAliveDaemon: false }).catch(() => false);
|
|
2112
|
+
json(res, outcome.ok ? 200 : 500, {
|
|
2113
|
+
ok: outcome.ok,
|
|
2114
|
+
...outcome.pid === void 0 ? {} : { pid: outcome.pid },
|
|
2115
|
+
...outcome.reason === void 0 ? {} : { reason: outcome.reason },
|
|
2116
|
+
writable,
|
|
2117
|
+
dock: deps.dock?.() ?? DEFAULT_DOCK_FLAGS
|
|
2118
|
+
});
|
|
2119
|
+
} catch (error) {
|
|
2120
|
+
json(res, 500, { error: safeMessage(error) });
|
|
2121
|
+
}
|
|
2122
|
+
};
|
|
2123
|
+
}
|
|
1995
2124
|
/** GET /api/codearts/models — currently offered model ids (honours allowlist). */
|
|
1996
2125
|
function modelsHandler(deps) {
|
|
1997
2126
|
return async (req, res) => {
|
|
@@ -2467,6 +2596,11 @@ function registerCodeArtsStatusRoute(ctx, deps) {
|
|
|
2467
2596
|
path: CODEARTS_DOCK_PATH,
|
|
2468
2597
|
handler: dockHandler(deps)
|
|
2469
2598
|
});
|
|
2599
|
+
const disposeKeeperStop = ctx.webServer.register({
|
|
2600
|
+
kind: "exact",
|
|
2601
|
+
path: CODEARTS_KEEPER_STOP_PATH,
|
|
2602
|
+
handler: keeperStopHandler(deps)
|
|
2603
|
+
});
|
|
2470
2604
|
const disposeDebug = ctx.webServer.register({
|
|
2471
2605
|
kind: "exact",
|
|
2472
2606
|
path: CODEARTS_DEBUG_PATH,
|
|
@@ -2490,6 +2624,7 @@ function registerCodeArtsStatusRoute(ctx, deps) {
|
|
|
2490
2624
|
disposeTestModel();
|
|
2491
2625
|
disposeQueue();
|
|
2492
2626
|
disposeDock();
|
|
2627
|
+
disposeKeeperStop();
|
|
2493
2628
|
disposeDebug();
|
|
2494
2629
|
disposeDoctor();
|
|
2495
2630
|
for (const s of activeLogins.values()) s.close();
|
|
@@ -2669,7 +2804,10 @@ const Config = z.object({
|
|
|
2669
2804
|
enabledModels: z.array(z.string()).description("Model ids offered in the model pickers (empty means every model)").volatile(),
|
|
2670
2805
|
showCreditDock: z.boolean().default(true).description("Show the remaining-credits line under the chat input box").volatile(),
|
|
2671
2806
|
showQueueDock: z.boolean().default(true).description("Show the upstream queue position under the chat input box").volatile(),
|
|
2672
|
-
showSidebarPanel: z.boolean().default(false).description("Show a CodeArts icon in the DSH sidebar icon column, opening this page in the middle column").volatile()
|
|
2807
|
+
showSidebarPanel: z.boolean().default(false).description("Show a CodeArts icon in the DSH sidebar icon column, opening this page in the middle column").volatile(),
|
|
2808
|
+
showModelDetailDock: z.boolean().default(true).description("Show \"Model … · x ratio\" on the line under the chat input box (Provider CodeArts always shows)").volatile(),
|
|
2809
|
+
autoClaim: z.boolean().default(false).description("Automatically claim the daily sign-in credits for every signed-in account").volatile(),
|
|
2810
|
+
keepAliveDaemon: z.boolean().default(false).description("Keep a detached background process that refreshes the OAuth credential while DSH is closed").volatile()
|
|
2673
2811
|
});
|
|
2674
2812
|
function settingsCompatNamespace(ctx) {
|
|
2675
2813
|
return resolveSettingsNamespaceCompat(ctx, ctx.get("settings"), CODEARTS_SETTINGS_NS);
|
|
@@ -2686,6 +2824,11 @@ function apply(ctx, rawConfig) {
|
|
|
2686
2824
|
/** /status 是卡片与对话底部都会轮询的接口,积分慢请求不能把它拖成转圈。 */
|
|
2687
2825
|
const CREDIT_TIMEOUT_MS = 8e3;
|
|
2688
2826
|
/**
|
|
2827
|
+
* 领取签到积分的单号预算。福利中心偶尔很慢(要拉活动列表再认领),给足 20 秒,
|
|
2828
|
+
* 但必须有个上限:自动领取是在后台跑的,卡死的请求会把这个号的后续重试全堵住。
|
|
2829
|
+
*/
|
|
2830
|
+
const CLAIM_TIMEOUT_MS = 2e4;
|
|
2831
|
+
/**
|
|
2689
2832
|
* 拉一次积分(每个账号各自一份)。
|
|
2690
2833
|
*
|
|
2691
2834
|
* 积分是**账号级**属性:多账号下每个号的余额、到期、签到状态都不同,所以逐号
|
|
@@ -2745,7 +2888,10 @@ function apply(ctx, rawConfig) {
|
|
|
2745
2888
|
const dock = () => ({
|
|
2746
2889
|
credits: current().showCreditDock !== false,
|
|
2747
2890
|
queue: current().showQueueDock !== false,
|
|
2748
|
-
sidebar: current().showSidebarPanel === true
|
|
2891
|
+
sidebar: current().showSidebarPanel === true,
|
|
2892
|
+
modelDetail: current().showModelDetailDock !== false,
|
|
2893
|
+
autoClaim: current().autoClaim === true,
|
|
2894
|
+
keepAliveDaemon: current().keepAliveDaemon === true
|
|
2749
2895
|
});
|
|
2750
2896
|
const setDock = async (patch) => {
|
|
2751
2897
|
const settings = ctx.get("settings");
|
|
@@ -2753,12 +2899,201 @@ function apply(ctx, rawConfig) {
|
|
|
2753
2899
|
await settings.update(settingsCompatNamespace(ctx), {
|
|
2754
2900
|
...patch.credits === void 0 ? {} : { showCreditDock: patch.credits },
|
|
2755
2901
|
...patch.queue === void 0 ? {} : { showQueueDock: patch.queue },
|
|
2756
|
-
...patch.sidebar === void 0 ? {} : { showSidebarPanel: patch.sidebar }
|
|
2902
|
+
...patch.sidebar === void 0 ? {} : { showSidebarPanel: patch.sidebar },
|
|
2903
|
+
...patch.modelDetail === void 0 ? {} : { showModelDetailDock: patch.modelDetail },
|
|
2904
|
+
...patch.autoClaim === void 0 ? {} : { autoClaim: patch.autoClaim },
|
|
2905
|
+
...patch.keepAliveDaemon === void 0 ? {} : { keepAliveDaemon: patch.keepAliveDaemon }
|
|
2757
2906
|
});
|
|
2758
2907
|
return true;
|
|
2759
2908
|
};
|
|
2760
2909
|
/** 手动「刷新模型列表」的预算:三路来源各打一次,比每小时的后台刷新更该给足时间。 */
|
|
2761
2910
|
const MANUAL_REFRESH_MS = 6e4;
|
|
2911
|
+
/**
|
|
2912
|
+
* 守护进程:当前活着吗。
|
|
2913
|
+
*
|
|
2914
|
+
* 「活着」= 状态文件里心跳新鲜 **且** 那个 pid 真能探到(`keeper.ts` 里两个条件都查)。
|
|
2915
|
+
* 只看心跳会把刚被 kill 的进程当成活的 —— 心跳是它死前 30 秒内写的,不会自己消失。
|
|
2916
|
+
*/
|
|
2917
|
+
const keeperRunning = () => keeperStats().alive;
|
|
2918
|
+
/**
|
|
2919
|
+
* 刚点开开关、守护进程还没写出状态文件的那段时间。
|
|
2920
|
+
*
|
|
2921
|
+
* 秒级的进程启动 + 首次 `writeKeeperState` 之间确实有一段「探不到活」的窗口。
|
|
2922
|
+
* 不区分的话,卡片刚被勾上就会因为「没探到进程」而自己把开关关掉 —— 用户看到的是
|
|
2923
|
+
* 勾了一下马上弹回去。这段时间里 `alive` 仍是 false(它确实还没活),但带 `starting`。
|
|
2924
|
+
*/
|
|
2925
|
+
const KEEPER_START_GRACE_MS = KEEPER_STALE_MS;
|
|
2926
|
+
const keeperView = async () => {
|
|
2927
|
+
const logPath = keeperLogPath();
|
|
2928
|
+
const stats = keeperStats();
|
|
2929
|
+
if (stats.alive && stats.state !== void 0) {
|
|
2930
|
+
keeperLastError = void 0;
|
|
2931
|
+
return {
|
|
2932
|
+
alive: true,
|
|
2933
|
+
pid: stats.state.pid,
|
|
2934
|
+
startedAt: stats.state.startedAt,
|
|
2935
|
+
heartbeatAt: stats.state.heartbeatAt,
|
|
2936
|
+
...stats.state.lastPass === void 0 ? {} : { lastPass: stats.state.lastPass },
|
|
2937
|
+
logPath
|
|
2938
|
+
};
|
|
2939
|
+
}
|
|
2940
|
+
if (!(current().keepAliveDaemon === true)) return {
|
|
2941
|
+
alive: false,
|
|
2942
|
+
logPath
|
|
2943
|
+
};
|
|
2944
|
+
if (Date.now() - keeperSpawnedAt < KEEPER_START_GRACE_MS) return {
|
|
2945
|
+
alive: false,
|
|
2946
|
+
starting: true,
|
|
2947
|
+
...keeperLastError === void 0 ? {} : { error: keeperLastError },
|
|
2948
|
+
logPath
|
|
2949
|
+
};
|
|
2950
|
+
try {
|
|
2951
|
+
await setDock({ keepAliveDaemon: false });
|
|
2952
|
+
} catch {}
|
|
2953
|
+
return {
|
|
2954
|
+
alive: false,
|
|
2955
|
+
error: keeperLastError ?? "守护进程已退出,已自动关闭保活开关",
|
|
2956
|
+
logPath
|
|
2957
|
+
};
|
|
2958
|
+
};
|
|
2959
|
+
/**
|
|
2960
|
+
* 常驻守护进程:在 DSH 之外续 refresh_token。
|
|
2961
|
+
*
|
|
2962
|
+
* 与上面 `keepAliveLoop` 的分工:那个只在 DSH 活着时有用,关掉 DSH 就没了;这个起的是
|
|
2963
|
+
* 一个 detached 进程,DSH 退出后它还在,20 分钟续一次,把 30 天有效期的 refresh_token
|
|
2964
|
+
* 一直续下去 —— 这才是「十几天不用重新登录」的正解。
|
|
2965
|
+
*
|
|
2966
|
+
* refresh_token 是一次性的:两边同时刷会让后刷的一方拿着作废的 token 失败。所以开关
|
|
2967
|
+
* 打开时 `keepAliveLoop` 会主动让路(只从磁盘重读),刷新权独占在守护进程手里。
|
|
2968
|
+
*/
|
|
2969
|
+
const KEEPER_ENSURE_MS = 6e4;
|
|
2970
|
+
let keeperSpawnedAt = 0;
|
|
2971
|
+
let keeperLastError;
|
|
2972
|
+
let keeperTimer;
|
|
2973
|
+
/**
|
|
2974
|
+
* 按开关状态把守护进程拉起来。
|
|
2975
|
+
*
|
|
2976
|
+
* 只在「开关刚被打开」时调用(`armKeepAliveDaemon`)。**不做定时重起**:用户明确要求
|
|
2977
|
+
* 「被 kill 了开关就关掉」,而不是「被 kill 了自己又冒出来」—— 后者正是用户按了结束
|
|
2978
|
+
* 却看到进程又活了的那种体验。
|
|
2979
|
+
*/
|
|
2980
|
+
const ensureKeeper = () => {
|
|
2981
|
+
if (stopped) return;
|
|
2982
|
+
if (current().keepAliveDaemon !== true) return;
|
|
2983
|
+
if (keeperRunning()) return;
|
|
2984
|
+
const outcome = spawnKeeper({
|
|
2985
|
+
scriptPath: keeperScriptPath(),
|
|
2986
|
+
pluginVersion: CODEARTS_VERSION
|
|
2987
|
+
});
|
|
2988
|
+
keeperSpawnedAt = Date.now();
|
|
2989
|
+
if (outcome.ok) {
|
|
2990
|
+
keeperLastError = void 0;
|
|
2991
|
+
ctx.logger.debug(`dsh-codearts: credential keeper running (pid ${outcome.pid ?? "?"})`);
|
|
2992
|
+
} else {
|
|
2993
|
+
keeperLastError = outcome.reason ?? "spawn failed";
|
|
2994
|
+
ctx.logger.debug(`dsh-codearts: credential keeper failed to start: ${keeperLastError}`);
|
|
2995
|
+
}
|
|
2996
|
+
};
|
|
2997
|
+
/**
|
|
2998
|
+
* 守护巡检:只在旁边看着,不重起。
|
|
2999
|
+
*
|
|
3000
|
+
* 三种结局:开关被关掉 → 收工;进程还活着 → 继续看;进程没了(超出启动宽限期)→
|
|
3001
|
+
* 把开关关掉并收工。判死必须在服务端做,不能只靠卡片轮询 —— 卡片没打开时也得让
|
|
3002
|
+
* 那个「开着却空转」的开关自己消失。
|
|
3003
|
+
*/
|
|
3004
|
+
const keeperLoop = async () => {
|
|
3005
|
+
if (stopped) return;
|
|
3006
|
+
if (current().keepAliveDaemon !== true) {
|
|
3007
|
+
keeperTimer = void 0;
|
|
3008
|
+
return;
|
|
3009
|
+
}
|
|
3010
|
+
if (!keeperRunning() && Date.now() - keeperSpawnedAt >= KEEPER_START_GRACE_MS) {
|
|
3011
|
+
keeperTimer = void 0;
|
|
3012
|
+
keeperSpawnedAt = 0;
|
|
3013
|
+
await setDock({ keepAliveDaemon: false }).catch(() => false);
|
|
3014
|
+
return;
|
|
3015
|
+
}
|
|
3016
|
+
keeperTimer = setTimeout(() => void keeperLoop(), KEEPER_ENSURE_MS);
|
|
3017
|
+
keeperTimer.unref?.();
|
|
3018
|
+
};
|
|
3019
|
+
/** 开关变化时立刻响应:打开就马上起,关掉就撤掉巡检(进程本身留给「结束」按钮)。 */
|
|
3020
|
+
const armKeepAliveDaemon = () => {
|
|
3021
|
+
if (stopped) return;
|
|
3022
|
+
if (current().keepAliveDaemon !== true) {
|
|
3023
|
+
if (keeperTimer !== void 0) clearTimeout(keeperTimer);
|
|
3024
|
+
keeperTimer = void 0;
|
|
3025
|
+
keeperSpawnedAt = 0;
|
|
3026
|
+
return;
|
|
3027
|
+
}
|
|
3028
|
+
if (keeperTimer !== void 0) return;
|
|
3029
|
+
ensureKeeper();
|
|
3030
|
+
keeperTimer = setTimeout(() => void keeperLoop(), KEEPER_ENSURE_MS);
|
|
3031
|
+
keeperTimer.unref?.();
|
|
3032
|
+
};
|
|
3033
|
+
/**
|
|
3034
|
+
* 逐号领取每日签到积分。
|
|
3035
|
+
*
|
|
3036
|
+
* 手动「领取」按钮和后台自动领取走的是**同一条**路径,只是入口不同:领的是什么、
|
|
3037
|
+
* 失败怎么归类、什么时候作废缓存,两处必须完全一致,否则「手动能领到、自动领不到」
|
|
3038
|
+
* 这种差异排查起来极其费劲。
|
|
3039
|
+
*/
|
|
3040
|
+
const claimAll = async (accountId) => {
|
|
3041
|
+
const ids = accountId === void 0 ? await store.ids() : [accountId];
|
|
3042
|
+
if (ids.length === 0) return {
|
|
3043
|
+
ok: false,
|
|
3044
|
+
claimed: false,
|
|
3045
|
+
state: "failed",
|
|
3046
|
+
amount: 0,
|
|
3047
|
+
message: "no usable credential"
|
|
3048
|
+
};
|
|
3049
|
+
const results = [];
|
|
3050
|
+
for (const id of ids) try {
|
|
3051
|
+
const stored = await store.credential(id);
|
|
3052
|
+
if (stored === void 0 || stored.stale === true) {
|
|
3053
|
+
results.push({
|
|
3054
|
+
id,
|
|
3055
|
+
ok: false,
|
|
3056
|
+
claimed: false,
|
|
3057
|
+
state: "failed",
|
|
3058
|
+
amount: 0,
|
|
3059
|
+
message: stored === void 0 ? "account not found" : "account needs sign-in"
|
|
3060
|
+
});
|
|
3061
|
+
continue;
|
|
3062
|
+
}
|
|
3063
|
+
const resolved = await store.resolve(id);
|
|
3064
|
+
if (resolved === void 0) {
|
|
3065
|
+
results.push({
|
|
3066
|
+
id,
|
|
3067
|
+
ok: false,
|
|
3068
|
+
claimed: false,
|
|
3069
|
+
state: "failed",
|
|
3070
|
+
amount: 0,
|
|
3071
|
+
message: "no usable credential"
|
|
3072
|
+
});
|
|
3073
|
+
continue;
|
|
3074
|
+
}
|
|
3075
|
+
const outcome = await claimDailyWelfare(resolved, AbortSignal.timeout(CLAIM_TIMEOUT_MS));
|
|
3076
|
+
if (outcome.state === "claimed") credits.invalidate(id);
|
|
3077
|
+
results.push({
|
|
3078
|
+
id,
|
|
3079
|
+
ok: outcome.state !== "failed",
|
|
3080
|
+
claimed: outcome.state === "claimed",
|
|
3081
|
+
state: outcome.state,
|
|
3082
|
+
amount: outcome.amount,
|
|
3083
|
+
message: outcome.message
|
|
3084
|
+
});
|
|
3085
|
+
} catch (error) {
|
|
3086
|
+
results.push({
|
|
3087
|
+
id,
|
|
3088
|
+
ok: false,
|
|
3089
|
+
claimed: false,
|
|
3090
|
+
state: "failed",
|
|
3091
|
+
amount: 0,
|
|
3092
|
+
message: error instanceof Error ? error.message : String(error)
|
|
3093
|
+
});
|
|
3094
|
+
}
|
|
3095
|
+
return summarizeClaims(results);
|
|
3096
|
+
};
|
|
2762
3097
|
ctx.inject(["webServer"], (webCtx) => registerCodeArtsStatusRoute(webCtx, {
|
|
2763
3098
|
store,
|
|
2764
3099
|
models: () => catalog.current(),
|
|
@@ -2777,62 +3112,17 @@ function apply(ctx, rawConfig) {
|
|
|
2777
3112
|
},
|
|
2778
3113
|
dock,
|
|
2779
3114
|
setDock,
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
const stored = await store.credential(id);
|
|
2792
|
-
if (stored === void 0 || stored.stale === true) {
|
|
2793
|
-
results.push({
|
|
2794
|
-
id,
|
|
2795
|
-
ok: false,
|
|
2796
|
-
claimed: false,
|
|
2797
|
-
state: "failed",
|
|
2798
|
-
amount: 0,
|
|
2799
|
-
message: stored === void 0 ? "account not found" : "account needs sign-in"
|
|
2800
|
-
});
|
|
2801
|
-
continue;
|
|
2802
|
-
}
|
|
2803
|
-
const resolved = await store.resolve(id);
|
|
2804
|
-
if (resolved === void 0) {
|
|
2805
|
-
results.push({
|
|
2806
|
-
id,
|
|
2807
|
-
ok: false,
|
|
2808
|
-
claimed: false,
|
|
2809
|
-
state: "failed",
|
|
2810
|
-
amount: 0,
|
|
2811
|
-
message: "no usable credential"
|
|
2812
|
-
});
|
|
2813
|
-
continue;
|
|
2814
|
-
}
|
|
2815
|
-
const outcome = await claimDailyWelfare(resolved, AbortSignal.timeout(2e4));
|
|
2816
|
-
if (outcome.state === "claimed") credits.invalidate(id);
|
|
2817
|
-
results.push({
|
|
2818
|
-
id,
|
|
2819
|
-
ok: outcome.state !== "failed",
|
|
2820
|
-
claimed: outcome.state === "claimed",
|
|
2821
|
-
state: outcome.state,
|
|
2822
|
-
amount: outcome.amount,
|
|
2823
|
-
message: outcome.message
|
|
2824
|
-
});
|
|
2825
|
-
} catch (error) {
|
|
2826
|
-
results.push({
|
|
2827
|
-
id,
|
|
2828
|
-
ok: false,
|
|
2829
|
-
claimed: false,
|
|
2830
|
-
state: "failed",
|
|
2831
|
-
amount: 0,
|
|
2832
|
-
message: error instanceof Error ? error.message : String(error)
|
|
2833
|
-
});
|
|
2834
|
-
}
|
|
2835
|
-
return summarizeClaims(results);
|
|
3115
|
+
keeper: keeperView,
|
|
3116
|
+
stopKeeper: async () => {
|
|
3117
|
+
const outcome = await stopKeeper();
|
|
3118
|
+
keeperSpawnedAt = 0;
|
|
3119
|
+
return outcome;
|
|
3120
|
+
},
|
|
3121
|
+
claim: claimAll,
|
|
3122
|
+
autoClaim: async () => {
|
|
3123
|
+
const outcome = await claimAll();
|
|
3124
|
+
outcome.claimed, outcome.state, outcome.amount, outcome.message;
|
|
3125
|
+
return outcome;
|
|
2836
3126
|
},
|
|
2837
3127
|
refreshModels: async () => {
|
|
2838
3128
|
const outcome = await refreshCatalog(store, catalog, AbortSignal.timeout(MANUAL_REFRESH_MS));
|
|
@@ -2847,8 +3137,13 @@ function apply(ctx, rawConfig) {
|
|
|
2847
3137
|
installSettingsSectionCompat(ctx, CODEARTS_SETTINGS_NS, Config, config, {
|
|
2848
3138
|
setSource(source) {
|
|
2849
3139
|
current = () => unwrapConfig(source());
|
|
3140
|
+
armAutoClaim();
|
|
3141
|
+
armKeepAliveDaemon();
|
|
2850
3142
|
},
|
|
2851
|
-
onChange() {
|
|
3143
|
+
onChange() {
|
|
3144
|
+
armAutoClaim();
|
|
3145
|
+
armKeepAliveDaemon();
|
|
3146
|
+
}
|
|
2852
3147
|
});
|
|
2853
3148
|
let stopped = false;
|
|
2854
3149
|
ctx.effect(() => () => {
|
|
@@ -2859,6 +3154,7 @@ function apply(ctx, rawConfig) {
|
|
|
2859
3154
|
});
|
|
2860
3155
|
const CATALOG_REFRESH_MS = 36e5;
|
|
2861
3156
|
const CATALOG_EMPTY_RETRY_MS = 6e4;
|
|
3157
|
+
const CATALOG_EMPTY_RETRY_MAX_MS = 3e5;
|
|
2862
3158
|
const abort = new AbortController();
|
|
2863
3159
|
const discover = async () => {
|
|
2864
3160
|
if (stopped) return;
|
|
@@ -2874,11 +3170,20 @@ function apply(ctx, rawConfig) {
|
|
|
2874
3170
|
}
|
|
2875
3171
|
};
|
|
2876
3172
|
let discoveryTimer;
|
|
2877
|
-
/**
|
|
3173
|
+
/** 连续多少轮目录是空的(用来算退避);一旦有货就归零。 */
|
|
3174
|
+
let emptyStreak = 0;
|
|
3175
|
+
/** 自我重排的发现循环:目录为空时退避着重试(60s → 5min 封顶),有货后回到每小时。 */
|
|
2878
3176
|
const discoverLoop = async () => {
|
|
2879
3177
|
await discover();
|
|
2880
3178
|
if (stopped) return;
|
|
2881
|
-
|
|
3179
|
+
let delay;
|
|
3180
|
+
if (catalog.current().length === 0) {
|
|
3181
|
+
delay = Math.min(CATALOG_EMPTY_RETRY_MS * 2 ** emptyStreak, CATALOG_EMPTY_RETRY_MAX_MS);
|
|
3182
|
+
emptyStreak += 1;
|
|
3183
|
+
} else {
|
|
3184
|
+
emptyStreak = 0;
|
|
3185
|
+
delay = CATALOG_REFRESH_MS;
|
|
3186
|
+
}
|
|
2882
3187
|
discoveryTimer = setTimeout(() => void discoverLoop(), delay);
|
|
2883
3188
|
discoveryTimer.unref?.();
|
|
2884
3189
|
};
|
|
@@ -2886,6 +3191,95 @@ function apply(ctx, rawConfig) {
|
|
|
2886
3191
|
abort.abort();
|
|
2887
3192
|
if (discoveryTimer !== void 0) clearTimeout(discoveryTimer);
|
|
2888
3193
|
});
|
|
3194
|
+
/**
|
|
3195
|
+
* 凭证保活:每 20 分钟把所有「快过期」的账号提前续掉。
|
|
3196
|
+
*
|
|
3197
|
+
* 为什么必须有这个循环:上游签发的 token 有效期是固定的 2 小时(实测,见
|
|
3198
|
+
* `auth.ts` 的 `REFRESH_SKEW_MS` 注释),而 `ensureFresh` 只在「有请求进来」时才被
|
|
3199
|
+
* 触发。用户开着 DSH 却不聊天时没有任何请求,凭证就一路裸奔到过期 —— 表现就是
|
|
3200
|
+
* 「隔天就要重新登录」。有了这个循环,只要 DSH 活着,凭证就不会真的到期。
|
|
3201
|
+
*
|
|
3202
|
+
* 间隔 20 分钟 + 续期阈值 30 分钟 = 剩 30 分钟以内一定会被扫到至少一次。
|
|
3203
|
+
* 关掉 DSH 就无从保活(插件不在进程里),那部分只能靠下次启动时重新登录,
|
|
3204
|
+
* 这是纯客户端方案的硬边界,卡片文案里必须讲明白。
|
|
3205
|
+
*/
|
|
3206
|
+
const KEEP_ALIVE_INTERVAL_MS = 12e5;
|
|
3207
|
+
let keepAliveTimer;
|
|
3208
|
+
const keepAliveLoop = async () => {
|
|
3209
|
+
if (stopped) return;
|
|
3210
|
+
if (keeperRunning()) await store.reloadFromDisk().catch(() => {});
|
|
3211
|
+
else try {
|
|
3212
|
+
const outcome = await store.keepAlive();
|
|
3213
|
+
if (outcome.refreshed > 0) ctx.logger.debug(`dsh-codearts: refreshed ${outcome.refreshed} credential(s) in the background`);
|
|
3214
|
+
if (outcome.needsLogin.length > 0) ctx.logger.debug(`dsh-codearts: ${outcome.needsLogin.length} account(s) need a fresh sign-in`);
|
|
3215
|
+
} catch (error) {
|
|
3216
|
+
ctx.logger.debug("dsh-codearts: credential keep-alive failed; will retry", error);
|
|
3217
|
+
}
|
|
3218
|
+
if (stopped) return;
|
|
3219
|
+
keepAliveTimer = setTimeout(() => void keepAliveLoop(), KEEP_ALIVE_INTERVAL_MS);
|
|
3220
|
+
keepAliveTimer.unref?.();
|
|
3221
|
+
};
|
|
3222
|
+
/**
|
|
3223
|
+
* 自动领取每日签到积分。
|
|
3224
|
+
*
|
|
3225
|
+
* 节奏的取舍:上游按天去重(重复领返回 40001 → already),所以多领不会出事;
|
|
3226
|
+
* 但也正因为幂等,**没必要**高频轮询 —— 单号一天只值 200 分,每 10 秒刷一次的
|
|
3227
|
+
* 上行成本和风控风险都远大于收益。取「启动后 30 秒试一次 + 失败才退避重试
|
|
3228
|
+
* (30 分钟一次、最多 3 次)」:装了就领到,领不到也不无限缠着上游。
|
|
3229
|
+
*
|
|
3230
|
+
* 终止条件:任意一轮里「有号真领到」或「所有号都是 already/unavailable」就停。
|
|
3231
|
+
* 只有 genuinely 失败(网络、凭据异常)才值得再试。
|
|
3232
|
+
*/
|
|
3233
|
+
const AUTO_CLAIM_FIRST_DELAY_MS = 3e4;
|
|
3234
|
+
const AUTO_CLAIM_RETRY_MS = 18e5;
|
|
3235
|
+
const AUTO_CLAIM_MAX_TRIES = 3;
|
|
3236
|
+
let autoClaimTimer;
|
|
3237
|
+
let autoClaimTries = 0;
|
|
3238
|
+
/** 自我重排的自动领取循环;开关关掉时直接不排下一轮,下次打开才重新开始。 */
|
|
3239
|
+
const autoClaimLoop = async () => {
|
|
3240
|
+
if (stopped) return;
|
|
3241
|
+
if (!dock().autoClaim) {
|
|
3242
|
+
autoClaimTimer = void 0;
|
|
3243
|
+
return;
|
|
3244
|
+
}
|
|
3245
|
+
autoClaimTries += 1;
|
|
3246
|
+
let retry = false;
|
|
3247
|
+
try {
|
|
3248
|
+
const outcome = await claimAll();
|
|
3249
|
+
outcome.claimed, outcome.state, outcome.amount, outcome.message;
|
|
3250
|
+
if (outcome.claimed) ctx.logger.debug(`dsh-codearts: auto-claimed daily credits (${outcome.amount})`);
|
|
3251
|
+
else if (outcome.state === "failed") retry = true;
|
|
3252
|
+
} catch (error) {
|
|
3253
|
+
retry = true;
|
|
3254
|
+
ctx.logger.debug("dsh-codearts: auto-claim failed; will retry", error);
|
|
3255
|
+
}
|
|
3256
|
+
if (stopped) return;
|
|
3257
|
+
if (!retry || autoClaimTries >= AUTO_CLAIM_MAX_TRIES) {
|
|
3258
|
+
autoClaimTimer = void 0;
|
|
3259
|
+
return;
|
|
3260
|
+
}
|
|
3261
|
+
autoClaimTimer = setTimeout(() => void autoClaimLoop(), AUTO_CLAIM_RETRY_MS);
|
|
3262
|
+
autoClaimTimer.unref?.();
|
|
3263
|
+
};
|
|
3264
|
+
/** 按当前开关状态(重新)拉起自动领取;开关关掉时把在等的定时器撤掉。 */
|
|
3265
|
+
const armAutoClaim = () => {
|
|
3266
|
+
if (stopped) return;
|
|
3267
|
+
if (!dock().autoClaim) {
|
|
3268
|
+
if (autoClaimTimer !== void 0) clearTimeout(autoClaimTimer);
|
|
3269
|
+
autoClaimTimer = void 0;
|
|
3270
|
+
autoClaimTries = 0;
|
|
3271
|
+
return;
|
|
3272
|
+
}
|
|
3273
|
+
if (autoClaimTimer !== void 0) return;
|
|
3274
|
+
autoClaimTries = 0;
|
|
3275
|
+
autoClaimTimer = setTimeout(() => void autoClaimLoop(), AUTO_CLAIM_FIRST_DELAY_MS);
|
|
3276
|
+
autoClaimTimer.unref?.();
|
|
3277
|
+
};
|
|
3278
|
+
ctx.effect(() => () => {
|
|
3279
|
+
if (keepAliveTimer !== void 0) clearTimeout(keepAliveTimer);
|
|
3280
|
+
if (autoClaimTimer !== void 0) clearTimeout(autoClaimTimer);
|
|
3281
|
+
if (keeperTimer !== void 0) clearTimeout(keeperTimer);
|
|
3282
|
+
});
|
|
2889
3283
|
const shim = createCodeArtsShim({
|
|
2890
3284
|
store,
|
|
2891
3285
|
catalog,
|
|
@@ -2898,6 +3292,9 @@ function apply(ctx, rawConfig) {
|
|
|
2898
3292
|
shim.ready.then(async () => {
|
|
2899
3293
|
if (stopped) return;
|
|
2900
3294
|
discoverLoop();
|
|
3295
|
+
keepAliveLoop();
|
|
3296
|
+
armAutoClaim();
|
|
3297
|
+
armKeepAliveDaemon();
|
|
2901
3298
|
const incompatible = await probePiAiCompat();
|
|
2902
3299
|
if (incompatible !== void 0) ctx.logger.error(`dsh-codearts: ${incompatible}`);
|
|
2903
3300
|
const codearts = createCodeArtsAdapter({
|