mslxdff 0.1.203 → 0.1.207
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 +3 -3
- package/bin/mslxdff.js +1 -1
- package/docs/cli_help_mini.md +146 -0
- package/package.json +38 -38
- package/plugins/README.md +14 -14
- package/plugins/prefer-model.mjs.example +14 -14
- package/src/anthropic/canonical-json.js +55 -0
- package/src/anthropic/count-tokens.js +61 -61
- package/src/anthropic/request-body.js +183 -126
- package/src/anthropic/request-shape.js +60 -0
- package/src/anthropic/session-id.js +58 -0
- package/src/anthropic/shared.js +18 -18
- package/src/anthropic/sse-aggregate.js +56 -56
- package/src/anthropic/tool-media.js +75 -0
- package/src/anthropic/translate.js +296 -287
- package/src/anthropic/web-search.js +361 -361
- package/src/api-kind.js +31 -31
- package/src/autostart.js +288 -288
- package/src/bench/probe.js +1 -1
- package/src/bench/runner.js +1 -1
- package/src/bench/via-probe.js +1 -1
- package/src/bench/via.js +144 -144
- package/src/bench/workbuddy-bench.js +1 -1
- package/src/chat/config.js +11 -11
- package/src/chat/cooling.js +81 -81
- package/src/chat/direct.js +58 -58
- package/src/chat/index.js +6 -6
- package/src/chat/spinner.js +38 -38
- package/src/chat/sse.js +69 -69
- package/src/chat/stats.js +247 -247
- package/src/chat/store.js +48 -48
- package/src/chat/tool-handlers.js +67 -67
- package/src/chat/tools.js +1 -1
- package/src/chat/upstream.js +59 -59
- package/src/chat-pipeline/auto-race.js +158 -158
- package/src/chat-pipeline/empty-turn.js +147 -147
- package/src/chat-pipeline/engine.js +17 -17
- package/src/chat-pipeline/index.js +8 -40
- package/src/chat-pipeline/serial-trial.js +279 -283
- package/src/chat-pipeline/terminal-verdict.js +132 -147
- package/src/chooser.js +54 -54
- package/src/claude-desktop/apply.js +101 -101
- package/src/claude-desktop/context1m.js +38 -38
- package/src/claude-desktop/doctor.js +146 -146
- package/src/claude-desktop/slots.js +152 -152
- package/src/cli/bootstrap.js +1 -1
- package/src/cli/commands/claude-desktop.js +284 -284
- package/src/cli/commands/daemon.js +231 -231
- package/src/cli/commands/model/list-live.js +22 -22
- package/src/cli/commands/model/list-providers.js +76 -76
- package/src/cli/commands/model/list-render.js +77 -77
- package/src/cli/commands/model/list-sort.js +52 -52
- package/src/cli/commands/model/list-tty.js +76 -76
- package/src/cli/commands/model/list.js +166 -166
- package/src/cli/commands/model/live-models.js +24 -24
- package/src/cli/commands/model/stats.js +43 -43
- package/src/cli/commands/model/status.js +47 -47
- package/src/cli/commands/model.js +51 -51
- package/src/cli/commands/provider/add.js +108 -108
- package/src/cli/commands/provider/allowlist.js +99 -99
- package/src/cli/commands/provider/cline-free.js +99 -99
- package/src/cli/commands/provider/cline-login.js +218 -218
- package/src/cli/commands/provider/cline-quota.js +65 -65
- package/src/cli/commands/provider/codearts-login.js +45 -45
- package/src/cli/commands/provider/config.js +119 -119
- package/src/cli/commands/provider/globalqwenwork-login.js +126 -126
- package/src/cli/commands/provider/keys-guard.js +50 -50
- package/src/cli/commands/provider/keys.js +161 -161
- package/src/cli/commands/provider/qoder-checkin.js +81 -81
- package/src/cli/commands/provider/qoder-login.js +102 -102
- package/src/cli/commands/provider/raccoon-login.js +180 -180
- package/src/cli/commands/provider/raccoon-password.js +162 -162
- package/src/cli/commands/provider/raccoon-quota.js +109 -109
- package/src/cli/commands/provider/traework-checkin.js +144 -144
- package/src/cli/commands/provider/traework-login.js +164 -164
- package/src/cli/commands/provider/workbuddy-login.js +153 -153
- package/src/cli/commands/provider/zcode-login.js +77 -77
- package/src/cli/commands/provider/zcode-quota.js +55 -55
- package/src/cli/commands/provider.js +1 -1
- package/src/cli/commands/stats.js +158 -149
- package/src/cli/commands/system.js +261 -261
- package/src/cli/commands/timezone.js +47 -47
- package/src/cli/commands/use-group.js +60 -60
- package/src/cli/commands/workbuddy.js +189 -189
- package/src/cli/format.js +141 -141
- package/src/cli/group-helpers.js +70 -70
- package/src/cli/help.js +80 -80
- package/src/cli/index.js +70 -70
- package/src/cli/interactive.js +121 -121
- package/src/cli/join-core.js +99 -99
- package/src/cli/join-wizard.js +175 -175
- package/src/cli/policy.js +136 -136
- package/src/cli/provider-row.js +126 -126
- package/src/cli/util.js +24 -24
- package/src/compat.js +53 -53
- package/src/events.js +38 -38
- package/src/free-lane.js +131 -131
- package/src/free-watcher.js +63 -63
- package/src/groups.js +262 -262
- package/src/logs.js +195 -195
- package/src/metrics.js +71 -62
- package/src/model-capabilities/context-lookup.js +65 -65
- package/src/model-capabilities/merge.js +142 -142
- package/src/model-trace.js +187 -187
- package/src/models.js +1 -1
- package/src/opencode-identity.js +72 -72
- package/src/peers.js +254 -254
- package/src/plugins.js +107 -107
- package/src/providers/AGENTS.md +49 -0
- package/src/providers/base.js +164 -164
- package/src/providers/classify.js +22 -22
- package/src/providers/cline/allowlist-sync.js +93 -93
- package/src/providers/cline/free-catalog.js +71 -71
- package/src/providers/cline/headers.js +37 -37
- package/src/providers/cline/index.js +130 -130
- package/src/providers/cline/models.js +150 -150
- package/src/providers/cline/usage.js +274 -274
- package/src/providers/codearts/auth-pool.js +148 -148
- package/src/providers/codearts/concurrency.js +34 -34
- package/src/providers/codearts/const.js +65 -65
- package/src/providers/codearts/dpop.js +95 -95
- package/src/providers/codearts/index.js +175 -175
- package/src/providers/codearts/login.js +214 -214
- package/src/providers/codearts/models.js +182 -182
- package/src/providers/codearts/sign.js +96 -96
- package/src/providers/codearts/sse.js +204 -204
- package/src/providers/codearts/stream.js +209 -209
- package/src/providers/codearts/sts.js +124 -124
- package/src/providers/codearts.js +1 -1
- package/src/providers/dispatcher.js +146 -146
- package/src/providers/generic.js +156 -156
- package/src/providers/globalqwenwork/account-store.js +141 -141
- package/src/providers/globalqwenwork/constants.js +73 -73
- package/src/providers/globalqwenwork/crypto.js +2 -2
- package/src/providers/globalqwenwork/http.js +2 -2
- package/src/providers/globalqwenwork/index.js +331 -331
- package/src/providers/globalqwenwork/payload.js +147 -147
- package/src/providers/globalqwenwork/rsa.js +2 -2
- package/src/providers/globalqwenwork/sse.js +2 -2
- package/src/providers/globalqwenwork/stream.js +2 -2
- package/src/providers/globalqwenwork.js +1 -1
- package/src/providers/globalworkbuddy/account-store.js +198 -198
- package/src/providers/globalworkbuddy/auth.js +97 -97
- package/src/providers/globalworkbuddy/catalog.js +138 -138
- package/src/providers/globalworkbuddy/constants.js +73 -73
- package/src/providers/globalworkbuddy/device-auth.js +120 -120
- package/src/providers/globalworkbuddy/errors.js +98 -98
- package/src/providers/globalworkbuddy/headers.js +89 -89
- package/src/providers/globalworkbuddy/models.js +96 -96
- package/src/providers/globalworkbuddy/payload.js +80 -80
- package/src/providers/index.js +5 -5
- package/src/providers/model-id.js +172 -172
- package/src/providers/opencode.js +19 -19
- package/src/providers/openrouter.js +31 -31
- package/src/providers/qoder/account-store.js +106 -106
- package/src/providers/qoder/aggregate.js +60 -60
- package/src/providers/qoder/baseprompt.json +591 -591
- package/src/providers/qoder/checkin.js +160 -160
- package/src/providers/qoder/constants.js +44 -44
- package/src/providers/qoder/encode.js +41 -41
- package/src/providers/qoder/fingerprint.js +38 -38
- package/src/providers/qoder/models.js +77 -77
- package/src/providers/qoder/oauth.js +84 -84
- package/src/providers/qoder/payload.js +162 -162
- package/src/providers/qoder/request.js +27 -27
- package/src/providers/qoder/session.js +134 -134
- package/src/providers/qoder/sticky.js +38 -38
- package/src/providers/qoder/stream.js +116 -116
- package/src/providers/qoder.js +1 -1
- package/src/providers/raccoon/account-store.js +216 -216
- package/src/providers/raccoon/auth.js +67 -67
- package/src/providers/raccoon/chat.js +228 -228
- package/src/providers/raccoon/const.js +180 -180
- package/src/providers/raccoon/credits.js +185 -185
- package/src/providers/raccoon/crypto.js +43 -43
- package/src/providers/raccoon/envelope.js +31 -31
- package/src/providers/raccoon/headers.js +45 -45
- package/src/providers/raccoon/index.js +216 -216
- package/src/providers/raccoon/login.js +279 -279
- package/src/providers/raccoon/models.js +160 -160
- package/src/providers/raccoon/password-login.js +102 -102
- package/src/providers/raccoon/qr-codewords.js +115 -115
- package/src/providers/raccoon/qr.js +236 -236
- package/src/providers/raccoon/sse.js +135 -135
- package/src/providers/responses-channel.js +120 -120
- package/src/providers/share-keys.js +53 -53
- package/src/providers/traework/account-store.js +234 -234
- package/src/providers/traework/chat.js +217 -217
- package/src/providers/traework/checkin.js +77 -77
- package/src/providers/traework/constants.js +41 -41
- package/src/providers/traework/errors.js +52 -52
- package/src/providers/traework/headers.js +79 -79
- package/src/providers/traework/index.js +132 -132
- package/src/providers/traework/market-identity.js +116 -116
- package/src/providers/traework/models.js +98 -98
- package/src/providers/traework/payload.js +86 -86
- package/src/providers/traework/sse.js +184 -184
- package/src/providers/traework/token.js +52 -52
- package/src/providers/traework.js +2 -2
- package/src/providers/workbuddy/account-store.js +186 -186
- package/src/providers/workbuddy/auth.js +158 -158
- package/src/providers/workbuddy/balance.js +86 -86
- package/src/providers/workbuddy/cat-travel.js +125 -125
- package/src/providers/workbuddy/checkin.js +75 -75
- package/src/providers/workbuddy/desktop-info.js +78 -78
- package/src/providers/workbuddy/growth-api.js +67 -67
- package/src/providers/workbuddy/growth-plans.js +63 -63
- package/src/providers/workbuddy/growth.js +181 -181
- package/src/providers/workbuddy/index.js +217 -217
- package/src/providers/workbuddy/models.js +126 -126
- package/src/providers/workbuddy/payload.js +141 -141
- package/src/providers/workbuddy/rotation-log.js +83 -83
- package/src/providers/workbuddy/sanitize-tools.js +61 -61
- package/src/providers/workbuddy/sdk-chat.js +26 -26
- package/src/providers/workbuddy-balance.js +78 -78
- package/src/providers/zcode/account-store.js +129 -129
- package/src/providers/zcode/auth.js +28 -28
- package/src/providers/zcode/chat.js +179 -179
- package/src/providers/zcode/const.js +54 -54
- package/src/providers/zcode/context-shape.js +162 -162
- package/src/providers/zcode/headers.js +93 -93
- package/src/providers/zcode/index.js +124 -124
- package/src/providers/zcode/models.js +65 -65
- package/src/providers/zcode/oauth.js +120 -120
- package/src/providers/zcode/quota.js +176 -176
- package/src/providers/zcode/sse.js +196 -196
- package/src/readline-compat.js +25 -25
- package/src/responses/translate.js +311 -311
- package/src/routes/AGENTS.md +37 -0
- package/src/routes/chat/broadband-handler.js +72 -72
- package/src/routes/chat/empty-turn-report.js +31 -31
- package/src/routes/chat/exhausted-handler.js +87 -87
- package/src/routes/chat/hedge-handler.js +130 -130
- package/src/routes/chat/index.js +1 -1
- package/src/routes/chat/local-handler.js +63 -63
- package/src/routes/chat/peer-handler.js +81 -81
- package/src/routes/chat.js +3 -3
- package/src/routes/fallback.js +88 -88
- package/src/routes/groups-relay.js +214 -214
- package/src/routes/groups.js +122 -122
- package/src/routes/helpers.js +93 -83
- package/src/routes/index.js +147 -147
- package/src/routes/messages-route.js +375 -365
- package/src/routes/peers.js +10 -2
- package/src/routes/relay-queue.js +181 -181
- package/src/routes/relay.js +60 -60
- package/src/routes/responses-route.js +213 -213
- package/src/routes/stream-body.js +84 -84
- package/src/routes/stream-hold.js +337 -337
- package/src/routes/stream-scan.js +366 -366
- package/src/routes.js +3 -3
- package/src/runtime/auto-update.js +113 -113
- package/src/runtime/broadband-stream.js +79 -79
- package/src/runtime/broadband.js +103 -103
- package/src/runtime/group-sync.js +81 -81
- package/src/runtime/keep-warm.js +120 -120
- package/src/runtime/lifecycle-forensics.js +116 -116
- package/src/runtime/lifecycle-log.js +87 -87
- package/src/runtime/log-stamp.js +73 -73
- package/src/runtime/proc-info.js +137 -137
- package/src/runtime/qoder-checkin.js +93 -93
- package/src/runtime/raccoon-checkin.js +126 -126
- package/src/runtime/server-lifecycle.js +189 -189
- package/src/runtime/traework-checkin.js +118 -118
- package/src/runtime/workbuddy-checkin.js +120 -120
- package/src/runtime/workbuddy-growth.js +133 -133
- package/src/state/facade.js +58 -58
- package/src/state/memory.js +162 -162
- package/src/state/merge.js +25 -25
- package/src/state/migrations/cline-unify.js +94 -94
- package/src/state/migrations.js +33 -33
- package/src/state/persist.js +42 -42
- package/src/state/provider-config.js +142 -142
- package/src/state/schemas/group.js +31 -31
- package/src/state/schemas/model.js +94 -94
- package/src/state/schemas/peer.js +31 -31
- package/src/state/schemas/port.js +10 -10
- package/src/state/schemas/provider.js +202 -202
- package/src/state/schemas/timezone.js +70 -70
- package/src/state/schemas/token.js +48 -48
- package/src/state/schemas/use-group.js +102 -102
- package/src/state/store.js +176 -176
- package/src/state.js +1 -1
- package/src/sync-claude-desktop.js +234 -234
- package/src/sync-claude.js +152 -152
- package/src/sync-codex.js +116 -116
- package/src/sync-workbuddy.js +158 -158
- package/src/talk-full-redact.js +129 -129
- package/src/talk-full.js +131 -131
- package/src/talk-log.js +257 -257
- package/src/text-tokens.js +52 -52
- package/src/thinking-effort.js +67 -67
- package/src/time.js +106 -106
- package/src/timeline.js +26 -26
- package/src/transport/index.js +1 -1
- package/src/upstream-engine/index.js +74 -74
- package/src/upstream-engine/mode.js +15 -15
- package/src/upstream-engine/sdk/attempt.js +221 -221
- package/src/upstream-engine/sdk/chat.js +28 -28
- package/src/upstream-engine/sdk/convert.js +192 -192
- package/src/upstream-engine/sdk/diagnose.js +62 -62
- package/src/upstream-engine/sdk/dispatch.js +55 -55
- package/src/upstream-engine/sdk/responses.js +176 -176
- package/src/upstream-engine/sdk/sse.js +166 -166
- package/src/upstream-responses.js +305 -305
- package/src/usage/cache-fields.js +58 -0
- package/src/usage/record.js +109 -106
- package/src/usage/report.js +209 -195
- package/src/v6pool/claim.js +157 -157
- package/src/v6pool/config.js +65 -65
- package/src/v6pool/detect.js +130 -130
- package/src/v6pool/index.js +152 -152
- package/src/v6pool/prefix.js +140 -140
- package/src/v6pool/probe.js +40 -40
- package/src/v6pool/report.js +104 -104
- package/src/v6pool/rotate.js +263 -263
- package/src/warp-pool/config.js +74 -74
- package/src/warp-pool/health.js +106 -106
- package/src/warp-pool/identity.js +141 -141
- package/src/warp-pool/index.js +358 -358
- package/src/warp-pool/keys.js +29 -29
- package/src/warp-pool/nodes.js +95 -95
- package/src/warp-pool/ports.js +160 -160
- package/src/warp-pool/register.js +113 -113
- package/src/warp-pool/report.js +232 -232
- package/src/warp-pool/revive.js +168 -168
- package/src/warp-pool/rotate.js +214 -214
- package/src/warp-pool/strategy.js +43 -43
- package/src/warp-pool/verdict.js +69 -69
- package/src/warp-pool/wireproxy.js +162 -162
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import { mkdirSync, readFileSync, writeFileSync, existsSync, statSync, chmodSync } from "node:fs";
|
|
2
|
-
import { dirname } from "node:path";
|
|
3
|
-
import { defaultStateFile, tokenFile, generateToken, readState, writeStateImmediate, getEntry } from "../store.js";
|
|
4
|
-
import { fmtShanghaiYMDHMS } from "../../time.js";
|
|
5
|
-
|
|
6
|
-
// 主 Bearer token 镜像落盘:0600 + 显式 chmod。
|
|
7
|
-
// 注意 writeFileSync 的 mode 只在“新建文件”时生效,已存在的文件权限不会被它改;
|
|
8
|
-
// 升级前生成的老文件会永远停在 0644(同机其他用户可读),所以必须再 chmod 一次兜住。
|
|
9
|
-
function syncTokenFile(token, file) {
|
|
10
|
-
try {
|
|
11
|
-
const tf = tokenFile(file);
|
|
12
|
-
mkdirSync(dirname(tf), { recursive: true });
|
|
13
|
-
writeFileSync(tf, String(token || "").trim() + "\n", { encoding: "utf8", mode: 0o600 });
|
|
14
|
-
try { chmodSync(tf, 0o600); } catch {}
|
|
15
|
-
} catch {}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export async function loadToken({ file = defaultStateFile() } = {}) {
|
|
19
|
-
const state = readState(file);
|
|
20
|
-
if (typeof state.token === "string" && state.token.length > 0) {
|
|
21
|
-
syncTokenFile(state.token, file);
|
|
22
|
-
return { token: state.token, created: false };
|
|
23
|
-
}
|
|
24
|
-
if (existsSync(file)) {
|
|
25
|
-
try {
|
|
26
|
-
const raw = JSON.parse(readFileSync(file, "utf8"));
|
|
27
|
-
if (typeof raw.token === "string" && raw.token.length > 0) {
|
|
28
|
-
syncTokenFile(raw.token, file);
|
|
29
|
-
const e = getEntry(file);
|
|
30
|
-
e.data = raw;
|
|
31
|
-
try { e.mtimeMs = statSync(file).mtimeMs; } catch {}
|
|
32
|
-
return { token: raw.token, created: false };
|
|
33
|
-
}
|
|
34
|
-
} catch {}
|
|
35
|
-
}
|
|
36
|
-
const tok = generateToken();
|
|
37
|
-
const saved = writeStateImmediate(file, { token: tok, createdAt: fmtShanghaiYMDHMS(new Date()) }).token;
|
|
38
|
-
syncTokenFile(saved, file);
|
|
39
|
-
return { token: saved, created: true };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export async function refreshToken({ file = defaultStateFile() } = {}) {
|
|
43
|
-
const tok = writeStateImmediate(file, { token: generateToken(), createdAt: fmtShanghaiYMDHMS(new Date()) }).token;
|
|
44
|
-
syncTokenFile(tok, file);
|
|
45
|
-
return tok;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export { generateToken, tokenFile };
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync, existsSync, statSync, chmodSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { defaultStateFile, tokenFile, generateToken, readState, writeStateImmediate, getEntry } from "../store.js";
|
|
4
|
+
import { fmtShanghaiYMDHMS } from "../../time.js";
|
|
5
|
+
|
|
6
|
+
// 主 Bearer token 镜像落盘:0600 + 显式 chmod。
|
|
7
|
+
// 注意 writeFileSync 的 mode 只在“新建文件”时生效,已存在的文件权限不会被它改;
|
|
8
|
+
// 升级前生成的老文件会永远停在 0644(同机其他用户可读),所以必须再 chmod 一次兜住。
|
|
9
|
+
function syncTokenFile(token, file) {
|
|
10
|
+
try {
|
|
11
|
+
const tf = tokenFile(file);
|
|
12
|
+
mkdirSync(dirname(tf), { recursive: true });
|
|
13
|
+
writeFileSync(tf, String(token || "").trim() + "\n", { encoding: "utf8", mode: 0o600 });
|
|
14
|
+
try { chmodSync(tf, 0o600); } catch {}
|
|
15
|
+
} catch {}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function loadToken({ file = defaultStateFile() } = {}) {
|
|
19
|
+
const state = readState(file);
|
|
20
|
+
if (typeof state.token === "string" && state.token.length > 0) {
|
|
21
|
+
syncTokenFile(state.token, file);
|
|
22
|
+
return { token: state.token, created: false };
|
|
23
|
+
}
|
|
24
|
+
if (existsSync(file)) {
|
|
25
|
+
try {
|
|
26
|
+
const raw = JSON.parse(readFileSync(file, "utf8"));
|
|
27
|
+
if (typeof raw.token === "string" && raw.token.length > 0) {
|
|
28
|
+
syncTokenFile(raw.token, file);
|
|
29
|
+
const e = getEntry(file);
|
|
30
|
+
e.data = raw;
|
|
31
|
+
try { e.mtimeMs = statSync(file).mtimeMs; } catch {}
|
|
32
|
+
return { token: raw.token, created: false };
|
|
33
|
+
}
|
|
34
|
+
} catch {}
|
|
35
|
+
}
|
|
36
|
+
const tok = generateToken();
|
|
37
|
+
const saved = writeStateImmediate(file, { token: tok, createdAt: fmtShanghaiYMDHMS(new Date()) }).token;
|
|
38
|
+
syncTokenFile(saved, file);
|
|
39
|
+
return { token: saved, created: true };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function refreshToken({ file = defaultStateFile() } = {}) {
|
|
43
|
+
const tok = writeStateImmediate(file, { token: generateToken(), createdAt: fmtShanghaiYMDHMS(new Date()) }).token;
|
|
44
|
+
syncTokenFile(tok, file);
|
|
45
|
+
return tok;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { generateToken, tokenFile };
|
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
import { defaultStateFile, readState, writeStateImmediate } from "../store.js";
|
|
2
|
-
import { classifyProvider } from "../../providers/classify.js";
|
|
3
|
-
import { getModelAlias, normalizeProviderId, DEFAULT_PROVIDER } from "../../providers/model-id.js";
|
|
4
|
-
|
|
5
|
-
function parseBool(v) {
|
|
6
|
-
if (typeof v === "boolean") return v;
|
|
7
|
-
if (typeof v === "number") return v !== 0;
|
|
8
|
-
if (typeof v === "string") {
|
|
9
|
-
const s = v.trim().toLowerCase();
|
|
10
|
-
if (["0", "false", "off", "no", "disable", "disabled", "close", "closed"].includes(s)) return false;
|
|
11
|
-
if (["1", "true", "on", "yes", "enable", "enabled", "open"].includes(s)) return true;
|
|
12
|
-
}
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function loadUseGroup({ file = defaultStateFile() } = {}) {
|
|
17
|
-
const raw = readState(file).useGroup;
|
|
18
|
-
const parsed = parseBool(raw);
|
|
19
|
-
return parsed === null ? true : parsed; // 默认开
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function saveUseGroup(value, { file = defaultStateFile() } = {}) {
|
|
23
|
-
const b = Boolean(value);
|
|
24
|
-
writeStateImmediate(file, { useGroup: b });
|
|
25
|
-
return b;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function getUseGroupEnv() {
|
|
29
|
-
const raw = process.env.MSLXDFF_USE_GROUP;
|
|
30
|
-
if (raw === undefined || raw === null || raw === "") return null;
|
|
31
|
-
return parseBool(raw);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function getEffectiveUseGroup({ file = defaultStateFile() } = {}) {
|
|
35
|
-
const env = getUseGroupEnv();
|
|
36
|
-
if (env !== null) return env;
|
|
37
|
-
return loadUseGroup({ file });
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// key 供应商组员开关:默认 off(仅 opencode 免费池走 peer 兜底,其他带 key 上游恒直连)。
|
|
41
|
-
// MSLXDFF_USE_GROUP_KEYS=1/on 可显式开回来(调试/弱网应急);同样受全局 off 约束。
|
|
42
|
-
export function getUseGroupKeysEnv() {
|
|
43
|
-
const raw = process.env.MSLXDFF_USE_GROUP_KEYS;
|
|
44
|
-
if (raw === undefined || raw === null || raw === "") return false;
|
|
45
|
-
return parseBool(raw) === true;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// 取模型对应的供应商 head:兼容 canonical(workbuddy/xxx、cline/xxx)、
|
|
49
|
-
// dash 别名(cline-xxx → 还原后取 head)、裸 id(归 opencode)。
|
|
50
|
-
// oc/ 别名归一到 opencode(与 splitModelId 一致:别名表小写 key,先小写再归一)。
|
|
51
|
-
export function providerHeadOf(model) {
|
|
52
|
-
let s = String(model || "").trim();
|
|
53
|
-
if (!s) return "";
|
|
54
|
-
try {
|
|
55
|
-
const aliased = getModelAlias(s);
|
|
56
|
-
if (aliased) s = aliased;
|
|
57
|
-
} catch {}
|
|
58
|
-
const slash = s.indexOf("/");
|
|
59
|
-
if (slash > 0) {
|
|
60
|
-
try {
|
|
61
|
-
const head = normalizeProviderId(s.slice(0, slash).toLowerCase());
|
|
62
|
-
return String(head || "").toLowerCase();
|
|
63
|
-
} catch {
|
|
64
|
-
return s.slice(0, slash).toLowerCase();
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return DEFAULT_PROVIDER;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// 全局开关:off 则所有供应商都不走组员网络(via-route/hedge/peer/broadband 全禁),仅本机直连
|
|
71
|
-
// workbuddy/cline 系硬禁组员(ADR-0015 local-only):本机账号绑定(workbuddy auths/*.json + uid / cline refreshToken),
|
|
72
|
-
// 组员没有该账号转过去也用不了,且本地直连最快——无论全局开关一律仅本机直连。
|
|
73
|
-
// model 兼容 canonical(workbuddy/xxx、cline/xxx)与 dash(workbuddy-xxx、cline-xxx)两种形态。
|
|
74
|
-
export function isHardLocalOnly(model) {
|
|
75
|
-
const head = providerHeadOf(model);
|
|
76
|
-
if (!head) return false;
|
|
77
|
-
try {
|
|
78
|
-
return classifyProvider(head) === "local-only";
|
|
79
|
-
} catch {
|
|
80
|
-
return head === "workbuddy" || head === "cline";
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// key 供应商默认直连(ADR-0023):opencode(quota-pool,裸 id/oc 前缀)沿用全局开关;
|
|
85
|
-
// 其余带前缀的一律仅本机直连,除非 MSLXDFF_USE_GROUP_KEYS=1 显式开回。
|
|
86
|
-
export function isKeyProviderDirectOnly(model) {
|
|
87
|
-
const head = providerHeadOf(model);
|
|
88
|
-
if (!head || head === "opencode") return false;
|
|
89
|
-
if (isHardLocalOnly(model)) return true;
|
|
90
|
-
if (getUseGroupKeysEnv()) return false;
|
|
91
|
-
return true;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function shouldUseGroupForModel(model, { file = defaultStateFile() } = {}) {
|
|
95
|
-
if (isHardLocalOnly(model)) return false;
|
|
96
|
-
if (isKeyProviderDirectOnly(model)) return false;
|
|
97
|
-
return getEffectiveUseGroup({ file });
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function isUseGroupEnabled({ file } = {}) {
|
|
101
|
-
return getEffectiveUseGroup({ file });
|
|
102
|
-
}
|
|
1
|
+
import { defaultStateFile, readState, writeStateImmediate } from "../store.js";
|
|
2
|
+
import { classifyProvider } from "../../providers/classify.js";
|
|
3
|
+
import { getModelAlias, normalizeProviderId, DEFAULT_PROVIDER } from "../../providers/model-id.js";
|
|
4
|
+
|
|
5
|
+
function parseBool(v) {
|
|
6
|
+
if (typeof v === "boolean") return v;
|
|
7
|
+
if (typeof v === "number") return v !== 0;
|
|
8
|
+
if (typeof v === "string") {
|
|
9
|
+
const s = v.trim().toLowerCase();
|
|
10
|
+
if (["0", "false", "off", "no", "disable", "disabled", "close", "closed"].includes(s)) return false;
|
|
11
|
+
if (["1", "true", "on", "yes", "enable", "enabled", "open"].includes(s)) return true;
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function loadUseGroup({ file = defaultStateFile() } = {}) {
|
|
17
|
+
const raw = readState(file).useGroup;
|
|
18
|
+
const parsed = parseBool(raw);
|
|
19
|
+
return parsed === null ? true : parsed; // 默认开
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function saveUseGroup(value, { file = defaultStateFile() } = {}) {
|
|
23
|
+
const b = Boolean(value);
|
|
24
|
+
writeStateImmediate(file, { useGroup: b });
|
|
25
|
+
return b;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getUseGroupEnv() {
|
|
29
|
+
const raw = process.env.MSLXDFF_USE_GROUP;
|
|
30
|
+
if (raw === undefined || raw === null || raw === "") return null;
|
|
31
|
+
return parseBool(raw);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function getEffectiveUseGroup({ file = defaultStateFile() } = {}) {
|
|
35
|
+
const env = getUseGroupEnv();
|
|
36
|
+
if (env !== null) return env;
|
|
37
|
+
return loadUseGroup({ file });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// key 供应商组员开关:默认 off(仅 opencode 免费池走 peer 兜底,其他带 key 上游恒直连)。
|
|
41
|
+
// MSLXDFF_USE_GROUP_KEYS=1/on 可显式开回来(调试/弱网应急);同样受全局 off 约束。
|
|
42
|
+
export function getUseGroupKeysEnv() {
|
|
43
|
+
const raw = process.env.MSLXDFF_USE_GROUP_KEYS;
|
|
44
|
+
if (raw === undefined || raw === null || raw === "") return false;
|
|
45
|
+
return parseBool(raw) === true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 取模型对应的供应商 head:兼容 canonical(workbuddy/xxx、cline/xxx)、
|
|
49
|
+
// dash 别名(cline-xxx → 还原后取 head)、裸 id(归 opencode)。
|
|
50
|
+
// oc/ 别名归一到 opencode(与 splitModelId 一致:别名表小写 key,先小写再归一)。
|
|
51
|
+
export function providerHeadOf(model) {
|
|
52
|
+
let s = String(model || "").trim();
|
|
53
|
+
if (!s) return "";
|
|
54
|
+
try {
|
|
55
|
+
const aliased = getModelAlias(s);
|
|
56
|
+
if (aliased) s = aliased;
|
|
57
|
+
} catch {}
|
|
58
|
+
const slash = s.indexOf("/");
|
|
59
|
+
if (slash > 0) {
|
|
60
|
+
try {
|
|
61
|
+
const head = normalizeProviderId(s.slice(0, slash).toLowerCase());
|
|
62
|
+
return String(head || "").toLowerCase();
|
|
63
|
+
} catch {
|
|
64
|
+
return s.slice(0, slash).toLowerCase();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return DEFAULT_PROVIDER;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 全局开关:off 则所有供应商都不走组员网络(via-route/hedge/peer/broadband 全禁),仅本机直连
|
|
71
|
+
// workbuddy/cline 系硬禁组员(ADR-0015 local-only):本机账号绑定(workbuddy auths/*.json + uid / cline refreshToken),
|
|
72
|
+
// 组员没有该账号转过去也用不了,且本地直连最快——无论全局开关一律仅本机直连。
|
|
73
|
+
// model 兼容 canonical(workbuddy/xxx、cline/xxx)与 dash(workbuddy-xxx、cline-xxx)两种形态。
|
|
74
|
+
export function isHardLocalOnly(model) {
|
|
75
|
+
const head = providerHeadOf(model);
|
|
76
|
+
if (!head) return false;
|
|
77
|
+
try {
|
|
78
|
+
return classifyProvider(head) === "local-only";
|
|
79
|
+
} catch {
|
|
80
|
+
return head === "workbuddy" || head === "cline";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// key 供应商默认直连(ADR-0023):opencode(quota-pool,裸 id/oc 前缀)沿用全局开关;
|
|
85
|
+
// 其余带前缀的一律仅本机直连,除非 MSLXDFF_USE_GROUP_KEYS=1 显式开回。
|
|
86
|
+
export function isKeyProviderDirectOnly(model) {
|
|
87
|
+
const head = providerHeadOf(model);
|
|
88
|
+
if (!head || head === "opencode") return false;
|
|
89
|
+
if (isHardLocalOnly(model)) return true;
|
|
90
|
+
if (getUseGroupKeysEnv()) return false;
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function shouldUseGroupForModel(model, { file = defaultStateFile() } = {}) {
|
|
95
|
+
if (isHardLocalOnly(model)) return false;
|
|
96
|
+
if (isKeyProviderDirectOnly(model)) return false;
|
|
97
|
+
return getEffectiveUseGroup({ file });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function isUseGroupEnabled({ file } = {}) {
|
|
101
|
+
return getEffectiveUseGroup({ file });
|
|
102
|
+
}
|
package/src/state/store.js
CHANGED
|
@@ -1,176 +1,176 @@
|
|
|
1
|
-
import { randomBytes } from "node:crypto";
|
|
2
|
-
import { mkdirSync, readFileSync, writeFileSync, statSync } from "node:fs";
|
|
3
|
-
import { writeFile } from "node:fs/promises";
|
|
4
|
-
import { dirname, join } from "node:path";
|
|
5
|
-
import os from "node:os";
|
|
6
|
-
import { loadFromDisk, atomicWriteSync, getMtimeMs, ensureDir } from "./persist.js";
|
|
7
|
-
import { mergeState, COLD_WINS } from "./merge.js";
|
|
8
|
-
|
|
9
|
-
export const DEFAULT_PORT = 8989;
|
|
10
|
-
|
|
11
|
-
export function isTestEnv() {
|
|
12
|
-
if (process.env.NODE_ENV === "test") return true;
|
|
13
|
-
if (process.env.MSLXDFF_STATE_FILE && String(process.env.MSLXDFF_STATE_FILE).includes("mslxdff-test")) return true;
|
|
14
|
-
if (process.argv.some((a) => String(a).includes("--test") || String(a).endsWith(".test.js"))) return true;
|
|
15
|
-
if (Array.isArray(process.execArgv) && process.execArgv.some((a) => String(a).includes("--test"))) return true;
|
|
16
|
-
if (process.env.NODE_TEST_CONTEXT) return true;
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function defaultStateFile() {
|
|
21
|
-
if (process.env.MSLXDFF_STATE_FILE) return process.env.MSLXDFF_STATE_FILE;
|
|
22
|
-
if (isTestEnv()) return join(os.tmpdir(), "mslxdff-test-state.json");
|
|
23
|
-
return join(os.homedir(), ".config", "mslxdff", "state.json");
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function tokenFile(file) {
|
|
27
|
-
if (process.env.MSLXDFF_TOKEN_FILE) return process.env.MSLXDFF_TOKEN_FILE;
|
|
28
|
-
const sf = file || defaultStateFile();
|
|
29
|
-
const realDefault = join(os.homedir(), ".config", "mslxdff", "state.json");
|
|
30
|
-
if (sf !== realDefault) return join(dirname(sf), "token");
|
|
31
|
-
if (isTestEnv()) return join(os.tmpdir(), "mslxdff-test-token");
|
|
32
|
-
return join(os.homedir(), ".config", "mslxdff", "token");
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function generateToken() {
|
|
36
|
-
return randomBytes(32).toString("hex");
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// ---- 缓存层:读走内存,写分“热/冷”两档,热数据 500ms 批量刷盘 ----
|
|
40
|
-
const stateCache = new Map(); // file -> { data, dirty, timer, mtimeMs }
|
|
41
|
-
const FLUSH_MS = (() => {
|
|
42
|
-
const n = Number(process.env.MSLXDFF_STATE_FLUSH_MS);
|
|
43
|
-
return Number.isInteger(n) && n >= 0 ? n : 500;
|
|
44
|
-
})();
|
|
45
|
-
|
|
46
|
-
function getEntry(file) {
|
|
47
|
-
let e = stateCache.get(file);
|
|
48
|
-
if (!e) {
|
|
49
|
-
e = { data: null, dirty: false, timer: null, mtimeMs: 0 };
|
|
50
|
-
stateCache.set(file, e);
|
|
51
|
-
}
|
|
52
|
-
return e;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// 合并规则已抽至 merge.js 单一真相
|
|
56
|
-
|
|
57
|
-
function readState(file) {
|
|
58
|
-
const e = getEntry(file);
|
|
59
|
-
if (e.data !== null) {
|
|
60
|
-
try {
|
|
61
|
-
const st = statSync(file);
|
|
62
|
-
if (st.mtimeMs === e.mtimeMs) return e.data;
|
|
63
|
-
if (e.dirty) {
|
|
64
|
-
const disk = loadFromDisk(file);
|
|
65
|
-
const diskObj = typeof disk === "object" && disk !== null ? disk : {};
|
|
66
|
-
const merged = mergeState(diskObj, e.data);
|
|
67
|
-
e.data = merged;
|
|
68
|
-
e.mtimeMs = st.mtimeMs;
|
|
69
|
-
return e.data;
|
|
70
|
-
}
|
|
71
|
-
} catch {
|
|
72
|
-
if (e.data) return e.data;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
const disk = loadFromDisk(file);
|
|
76
|
-
const obj = typeof disk === "object" && disk !== null ? disk : {};
|
|
77
|
-
e.data = obj;
|
|
78
|
-
try {
|
|
79
|
-
const st = statSync(file);
|
|
80
|
-
e.mtimeMs = st.mtimeMs;
|
|
81
|
-
} catch {
|
|
82
|
-
e.mtimeMs = Date.now();
|
|
83
|
-
}
|
|
84
|
-
e.dirty = false;
|
|
85
|
-
return e.data;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function writeStateImmediate(file, patch) {
|
|
89
|
-
const e = getEntry(file);
|
|
90
|
-
const base = e.data !== null ? e.data : readState(file);
|
|
91
|
-
const merged = { ...base, ...patch };
|
|
92
|
-
e.data = merged;
|
|
93
|
-
e.dirty = false;
|
|
94
|
-
if (e.timer) {
|
|
95
|
-
clearTimeout(e.timer);
|
|
96
|
-
e.timer = null;
|
|
97
|
-
}
|
|
98
|
-
ensureDir(file);
|
|
99
|
-
const mtime = atomicWriteSync(file, merged);
|
|
100
|
-
e.mtimeMs = mtime;
|
|
101
|
-
return merged;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function scheduleFlush(file) {
|
|
105
|
-
const e = getEntry(file);
|
|
106
|
-
if (e.timer) return;
|
|
107
|
-
if (FLUSH_MS === 0) {
|
|
108
|
-
flushStateSync(file);
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
e.timer = setTimeout(() => {
|
|
112
|
-
e.timer = null;
|
|
113
|
-
void flushState(file);
|
|
114
|
-
}, FLUSH_MS);
|
|
115
|
-
e.timer.unref?.();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async function flushState(file) {
|
|
119
|
-
const e = stateCache.get(file);
|
|
120
|
-
if (!e || !e.dirty || !e.data) return;
|
|
121
|
-
const snapshot = e.data;
|
|
122
|
-
try {
|
|
123
|
-
ensureDir(file);
|
|
124
|
-
const mtime = await atomicWriteAsync(file, snapshot);
|
|
125
|
-
e.dirty = false;
|
|
126
|
-
e.mtimeMs = mtime;
|
|
127
|
-
} catch {
|
|
128
|
-
// retain dirty
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function writeStateDeferred(file, patch) {
|
|
133
|
-
const e = getEntry(file);
|
|
134
|
-
const base = e.data !== null ? e.data : readState(file);
|
|
135
|
-
const merged = { ...base, ...patch };
|
|
136
|
-
e.data = merged;
|
|
137
|
-
e.dirty = true;
|
|
138
|
-
scheduleFlush(file);
|
|
139
|
-
return merged;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export function flushStateSync(file = defaultStateFile()) {
|
|
143
|
-
const e = stateCache.get(file);
|
|
144
|
-
if (!e || !e.dirty || !e.data) return;
|
|
145
|
-
try {
|
|
146
|
-
ensureDir(file);
|
|
147
|
-
const mtime = atomicWriteSync(file, e.data);
|
|
148
|
-
e.dirty = false;
|
|
149
|
-
e.mtimeMs = mtime;
|
|
150
|
-
if (e.timer) {
|
|
151
|
-
clearTimeout(e.timer);
|
|
152
|
-
e.timer = null;
|
|
153
|
-
}
|
|
154
|
-
} catch {}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export function clearStateCache(file) {
|
|
158
|
-
if (file) stateCache.delete(file);
|
|
159
|
-
else stateCache.clear();
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// internal helper for facade compat
|
|
163
|
-
function writeState(file, patch) {
|
|
164
|
-
return writeStateImmediate(file, patch);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export {
|
|
168
|
-
readState,
|
|
169
|
-
writeStateImmediate,
|
|
170
|
-
writeStateDeferred,
|
|
171
|
-
scheduleFlush,
|
|
172
|
-
flushState,
|
|
173
|
-
writeState,
|
|
174
|
-
getEntry,
|
|
175
|
-
stateCache,
|
|
176
|
-
};
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync, statSync } from "node:fs";
|
|
3
|
+
import { writeFile } from "node:fs/promises";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import { loadFromDisk, atomicWriteSync, getMtimeMs, ensureDir } from "./persist.js";
|
|
7
|
+
import { mergeState, COLD_WINS } from "./merge.js";
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_PORT = 8989;
|
|
10
|
+
|
|
11
|
+
export function isTestEnv() {
|
|
12
|
+
if (process.env.NODE_ENV === "test") return true;
|
|
13
|
+
if (process.env.MSLXDFF_STATE_FILE && String(process.env.MSLXDFF_STATE_FILE).includes("mslxdff-test")) return true;
|
|
14
|
+
if (process.argv.some((a) => String(a).includes("--test") || String(a).endsWith(".test.js"))) return true;
|
|
15
|
+
if (Array.isArray(process.execArgv) && process.execArgv.some((a) => String(a).includes("--test"))) return true;
|
|
16
|
+
if (process.env.NODE_TEST_CONTEXT) return true;
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function defaultStateFile() {
|
|
21
|
+
if (process.env.MSLXDFF_STATE_FILE) return process.env.MSLXDFF_STATE_FILE;
|
|
22
|
+
if (isTestEnv()) return join(os.tmpdir(), "mslxdff-test-state.json");
|
|
23
|
+
return join(os.homedir(), ".config", "mslxdff", "state.json");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function tokenFile(file) {
|
|
27
|
+
if (process.env.MSLXDFF_TOKEN_FILE) return process.env.MSLXDFF_TOKEN_FILE;
|
|
28
|
+
const sf = file || defaultStateFile();
|
|
29
|
+
const realDefault = join(os.homedir(), ".config", "mslxdff", "state.json");
|
|
30
|
+
if (sf !== realDefault) return join(dirname(sf), "token");
|
|
31
|
+
if (isTestEnv()) return join(os.tmpdir(), "mslxdff-test-token");
|
|
32
|
+
return join(os.homedir(), ".config", "mslxdff", "token");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function generateToken() {
|
|
36
|
+
return randomBytes(32).toString("hex");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ---- 缓存层:读走内存,写分“热/冷”两档,热数据 500ms 批量刷盘 ----
|
|
40
|
+
const stateCache = new Map(); // file -> { data, dirty, timer, mtimeMs }
|
|
41
|
+
const FLUSH_MS = (() => {
|
|
42
|
+
const n = Number(process.env.MSLXDFF_STATE_FLUSH_MS);
|
|
43
|
+
return Number.isInteger(n) && n >= 0 ? n : 500;
|
|
44
|
+
})();
|
|
45
|
+
|
|
46
|
+
function getEntry(file) {
|
|
47
|
+
let e = stateCache.get(file);
|
|
48
|
+
if (!e) {
|
|
49
|
+
e = { data: null, dirty: false, timer: null, mtimeMs: 0 };
|
|
50
|
+
stateCache.set(file, e);
|
|
51
|
+
}
|
|
52
|
+
return e;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 合并规则已抽至 merge.js 单一真相
|
|
56
|
+
|
|
57
|
+
function readState(file) {
|
|
58
|
+
const e = getEntry(file);
|
|
59
|
+
if (e.data !== null) {
|
|
60
|
+
try {
|
|
61
|
+
const st = statSync(file);
|
|
62
|
+
if (st.mtimeMs === e.mtimeMs) return e.data;
|
|
63
|
+
if (e.dirty) {
|
|
64
|
+
const disk = loadFromDisk(file);
|
|
65
|
+
const diskObj = typeof disk === "object" && disk !== null ? disk : {};
|
|
66
|
+
const merged = mergeState(diskObj, e.data);
|
|
67
|
+
e.data = merged;
|
|
68
|
+
e.mtimeMs = st.mtimeMs;
|
|
69
|
+
return e.data;
|
|
70
|
+
}
|
|
71
|
+
} catch {
|
|
72
|
+
if (e.data) return e.data;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const disk = loadFromDisk(file);
|
|
76
|
+
const obj = typeof disk === "object" && disk !== null ? disk : {};
|
|
77
|
+
e.data = obj;
|
|
78
|
+
try {
|
|
79
|
+
const st = statSync(file);
|
|
80
|
+
e.mtimeMs = st.mtimeMs;
|
|
81
|
+
} catch {
|
|
82
|
+
e.mtimeMs = Date.now();
|
|
83
|
+
}
|
|
84
|
+
e.dirty = false;
|
|
85
|
+
return e.data;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function writeStateImmediate(file, patch) {
|
|
89
|
+
const e = getEntry(file);
|
|
90
|
+
const base = e.data !== null ? e.data : readState(file);
|
|
91
|
+
const merged = { ...base, ...patch };
|
|
92
|
+
e.data = merged;
|
|
93
|
+
e.dirty = false;
|
|
94
|
+
if (e.timer) {
|
|
95
|
+
clearTimeout(e.timer);
|
|
96
|
+
e.timer = null;
|
|
97
|
+
}
|
|
98
|
+
ensureDir(file);
|
|
99
|
+
const mtime = atomicWriteSync(file, merged);
|
|
100
|
+
e.mtimeMs = mtime;
|
|
101
|
+
return merged;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function scheduleFlush(file) {
|
|
105
|
+
const e = getEntry(file);
|
|
106
|
+
if (e.timer) return;
|
|
107
|
+
if (FLUSH_MS === 0) {
|
|
108
|
+
flushStateSync(file);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
e.timer = setTimeout(() => {
|
|
112
|
+
e.timer = null;
|
|
113
|
+
void flushState(file);
|
|
114
|
+
}, FLUSH_MS);
|
|
115
|
+
e.timer.unref?.();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function flushState(file) {
|
|
119
|
+
const e = stateCache.get(file);
|
|
120
|
+
if (!e || !e.dirty || !e.data) return;
|
|
121
|
+
const snapshot = e.data;
|
|
122
|
+
try {
|
|
123
|
+
ensureDir(file);
|
|
124
|
+
const mtime = await atomicWriteAsync(file, snapshot);
|
|
125
|
+
e.dirty = false;
|
|
126
|
+
e.mtimeMs = mtime;
|
|
127
|
+
} catch {
|
|
128
|
+
// retain dirty
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function writeStateDeferred(file, patch) {
|
|
133
|
+
const e = getEntry(file);
|
|
134
|
+
const base = e.data !== null ? e.data : readState(file);
|
|
135
|
+
const merged = { ...base, ...patch };
|
|
136
|
+
e.data = merged;
|
|
137
|
+
e.dirty = true;
|
|
138
|
+
scheduleFlush(file);
|
|
139
|
+
return merged;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function flushStateSync(file = defaultStateFile()) {
|
|
143
|
+
const e = stateCache.get(file);
|
|
144
|
+
if (!e || !e.dirty || !e.data) return;
|
|
145
|
+
try {
|
|
146
|
+
ensureDir(file);
|
|
147
|
+
const mtime = atomicWriteSync(file, e.data);
|
|
148
|
+
e.dirty = false;
|
|
149
|
+
e.mtimeMs = mtime;
|
|
150
|
+
if (e.timer) {
|
|
151
|
+
clearTimeout(e.timer);
|
|
152
|
+
e.timer = null;
|
|
153
|
+
}
|
|
154
|
+
} catch {}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function clearStateCache(file) {
|
|
158
|
+
if (file) stateCache.delete(file);
|
|
159
|
+
else stateCache.clear();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// internal helper for facade compat
|
|
163
|
+
function writeState(file, patch) {
|
|
164
|
+
return writeStateImmediate(file, patch);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export {
|
|
168
|
+
readState,
|
|
169
|
+
writeStateImmediate,
|
|
170
|
+
writeStateDeferred,
|
|
171
|
+
scheduleFlush,
|
|
172
|
+
flushState,
|
|
173
|
+
writeState,
|
|
174
|
+
getEntry,
|
|
175
|
+
stateCache,
|
|
176
|
+
};
|
package/src/state.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./state/facade.js";
|
|
1
|
+
export * from "./state/facade.js";
|