dsh-xdpool 3.0.8 → 3.0.9
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 +2451 -2432
- package/LICENSE +21 -21
- package/README.en.md +308 -308
- package/README.md +5 -21
- package/assets/community-qr-qq.jpg +0 -0
- package/assets/icon.png +0 -0
- package/assets/xdpool-banner.gif +0 -0
- package/cordis.patch.yml +24 -24
- package/lib/bin.js +356 -59
- package/lib/client.js +396 -172
- package/lib/index.d.ts +41 -9
- package/lib/index.js +948 -345
- package/package.json +1 -2
- package/screenshots.json +9 -9
- package/scripts/extract-design-css.mjs +322 -322
- package/scripts/link-host-peers.mjs +359 -359
- package/scripts/make-rail-icons.mjs +46 -46
- package/scripts/probe-channels.mjs +142 -142
- package/vendor/channel-pack/LICENSE +21 -21
- package/vendor/channel-pack/NOTICE.md +53 -53
- package/vendor/channel-pack/pack.js +30 -93
package/lib/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { execFile, execFileSync, spawn, spawnSync } from "node:child_process";
|
|
3
3
|
import { appendFileSync, copyFileSync, cpSync, createWriteStream, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, renameSync, rmSync, statSync, truncateSync, unlinkSync, writeFileSync } from "node:fs";
|
|
4
|
-
import { basename, dirname, join, resolve } from "node:path";
|
|
5
4
|
import { homedir, hostname, tmpdir, userInfo } from "node:os";
|
|
5
|
+
import { basename, dirname, join, posix, resolve } from "node:path";
|
|
6
6
|
import crypto$1, { createDecipheriv, createHash, createHmac, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
|
7
|
-
import { chmod, mkdir, readFile, readdir, rename, rm, unlink, writeFile } from "node:fs/promises";
|
|
7
|
+
import { chmod, mkdir, open, readFile, readdir, rename, rm, unlink, writeFile } from "node:fs/promises";
|
|
8
8
|
import { LlmAdapter, LlmError, ReasoningEffortId, resolveImageAttachmentAccess, resolveRetryPolicy } from "@deepseek-ai/dsh-llm";
|
|
9
9
|
import { createServer } from "node:net";
|
|
10
10
|
import http, { createServer as createServer$1, request } from "node:http";
|
|
@@ -41,13 +41,29 @@ function creditReservesWith(current, accountId, reserve) {
|
|
|
41
41
|
* a machine can have no default set, and a default can name a browser that cannot be driven
|
|
42
42
|
* (Firefox has no DevTools protocol of this shape).
|
|
43
43
|
*
|
|
44
|
+
* BOTH PLATFORMS, ONE LIST
|
|
45
|
+
*
|
|
46
|
+
* The discovery used to be Windows-only: `reg query` and `%PROGRAMFILES%` paths. On macOS and
|
|
47
|
+
* Linux it answered nothing at all, so the launch fell through to whatever the individual lane
|
|
48
|
+
* had hard-coded. The list is now built per platform, and the default is read from the place that
|
|
49
|
+
* platform keeps it (`UrlAssociations` on Windows, the LaunchServices plist on macOS,
|
|
50
|
+
* `xdg-settings` on Linux), with PATH as a last resort for all three.
|
|
51
|
+
*
|
|
44
52
|
* @module dsh-xdpool/default-browser
|
|
45
53
|
*/
|
|
46
|
-
/**
|
|
47
|
-
|
|
48
|
-
|
|
54
|
+
/**
|
|
55
|
+
* The separator the target platform uses.
|
|
56
|
+
*
|
|
57
|
+
* `node:path` joins with the host's separator, which is right in production but wrong when a test
|
|
58
|
+
* on Windows exercises the macOS branch. The POSIX branches therefore join explicitly.
|
|
59
|
+
*/
|
|
60
|
+
function joinFor(host) {
|
|
61
|
+
return host.platform === "win32" ? join : posix.join;
|
|
62
|
+
}
|
|
63
|
+
/** Run a program for discovery, swallowing every failure into an empty answer. */
|
|
64
|
+
function runQuietly(file, args) {
|
|
49
65
|
try {
|
|
50
|
-
return execFileSync(
|
|
66
|
+
return execFileSync(file, [...args], {
|
|
51
67
|
encoding: "utf8",
|
|
52
68
|
windowsHide: true,
|
|
53
69
|
timeout: 4e3
|
|
@@ -56,6 +72,20 @@ function regQuery(key) {
|
|
|
56
72
|
return "";
|
|
57
73
|
}
|
|
58
74
|
}
|
|
75
|
+
/** The real platform, environment and filesystem. */
|
|
76
|
+
function systemHost() {
|
|
77
|
+
return {
|
|
78
|
+
platform: process.platform,
|
|
79
|
+
env: process.env,
|
|
80
|
+
exists: (path) => path !== "" && existsSync(path),
|
|
81
|
+
run: runQuietly
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** Run `reg query` and return its output, or an empty string. */
|
|
85
|
+
function regQuery(host, key) {
|
|
86
|
+
if (host.platform !== "win32") return "";
|
|
87
|
+
return host.run("reg", ["query", key]);
|
|
88
|
+
}
|
|
59
89
|
/**
|
|
60
90
|
* The executable behind a Windows ProgId.
|
|
61
91
|
*
|
|
@@ -63,19 +93,19 @@ function regQuery(key) {
|
|
|
63
93
|
* `"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --single-argument %1`;
|
|
64
94
|
* only the quoted path is used, because the rest is the shell's own argument.
|
|
65
95
|
*/
|
|
66
|
-
function exeFromProgId(progId) {
|
|
96
|
+
function exeFromProgId(host, progId) {
|
|
67
97
|
for (const root of [
|
|
68
98
|
"HKCR",
|
|
69
99
|
"HKCU\\SOFTWARE\\Classes",
|
|
70
100
|
"HKLM\\SOFTWARE\\Classes"
|
|
71
101
|
]) {
|
|
72
|
-
const output = regQuery(`${root}\\${progId}\\shell\\open\\command`);
|
|
102
|
+
const output = regQuery(host, `${root}\\${progId}\\shell\\open\\command`);
|
|
73
103
|
if (output === "") continue;
|
|
74
104
|
const line = output.split("\n").find((entry) => /REG_SZ/iu.test(entry)) ?? "";
|
|
75
105
|
const bare = /"([^"]+\.exe)"/iu.exec(line)?.[1];
|
|
76
|
-
if (bare !== void 0 &&
|
|
106
|
+
if (bare !== void 0 && host.exists(bare)) return bare;
|
|
77
107
|
const loose = /([A-Za-z]:\\[^\s"]+\.exe)/iu.exec(line);
|
|
78
|
-
if (loose?.[1] !== void 0 &&
|
|
108
|
+
if (loose?.[1] !== void 0 && host.exists(loose[1])) return loose[1];
|
|
79
109
|
}
|
|
80
110
|
}
|
|
81
111
|
/** A readable name for an executable path. */
|
|
@@ -83,24 +113,134 @@ function nameOf(path) {
|
|
|
83
113
|
const base = path.replaceAll("/", "\\").split("\\").pop()?.toLowerCase() ?? "";
|
|
84
114
|
if (base.includes("msedge")) return "Edge";
|
|
85
115
|
if (base.includes("chrome")) return "Chrome";
|
|
116
|
+
if (base.includes("chromium")) return "Chromium";
|
|
86
117
|
if (base.includes("brave")) return "Brave";
|
|
87
118
|
if (base.includes("vivaldi")) return "Vivaldi";
|
|
119
|
+
if (base.includes("arc")) return "Arc";
|
|
120
|
+
if (base.includes("opera")) return "Opera";
|
|
88
121
|
if (base.includes("qqbrowser")) return "QQ浏览器";
|
|
89
|
-
if (base.includes("
|
|
122
|
+
if (base.includes("360chrome")) return "360极速";
|
|
123
|
+
if (base.includes("360se")) return "360安全";
|
|
90
124
|
if (base.includes("quark")) return "夸克";
|
|
91
125
|
if (base.includes("sogou")) return "搜狗";
|
|
92
126
|
return base.replace(/\.exe$/iu, "");
|
|
93
127
|
}
|
|
94
128
|
/**
|
|
129
|
+
* The bundle identifiers macOS uses, mapped to the app that owns them.
|
|
130
|
+
*
|
|
131
|
+
* `open` accepts a bundle id, but the launcher needs an executable path, and the LaunchServices
|
|
132
|
+
* plist only stores the id. These are the identifiers the Chromium browsers ship with.
|
|
133
|
+
*/
|
|
134
|
+
const MAC_BUNDLE_PATHS = {
|
|
135
|
+
"com.google.chrome": ["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"],
|
|
136
|
+
"com.google.chrome.canary": ["/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"],
|
|
137
|
+
"com.microsoft.edgemac": ["/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"],
|
|
138
|
+
"com.brave.browser": ["/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"],
|
|
139
|
+
"com.vivaldi.vivaldi": ["/Applications/Vivaldi.app/Contents/MacOS/Vivaldi"],
|
|
140
|
+
"company.thebrowser.browser": ["/Applications/Arc.app/Contents/MacOS/Arc"],
|
|
141
|
+
"com.operasoftware.opera": ["/Applications/Opera.app/Contents/MacOS/Opera"],
|
|
142
|
+
"org.chromium.chromium": ["/Applications/Chromium.app/Contents/MacOS/Chromium"]
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* The default handler on macOS.
|
|
146
|
+
*
|
|
147
|
+
* LaunchServices keeps the answer in `com.apple.launchservices.secure.plist`, a plist rather than
|
|
148
|
+
* a text file. `plutil` converts it to JSON without writing anything; the `https` handler's role
|
|
149
|
+
* is the bundle id, which {@link MAC_BUNDLE_PATHS} turns back into an executable.
|
|
150
|
+
*/
|
|
151
|
+
function macDefault(host) {
|
|
152
|
+
const plist = posix.join(homedir(), "Library", "Preferences", "com.apple.LaunchServices", "com.apple.launchservices.secure.plist");
|
|
153
|
+
if (!host.exists(plist)) return void 0;
|
|
154
|
+
const json = host.run("plutil", [
|
|
155
|
+
"-convert",
|
|
156
|
+
"json",
|
|
157
|
+
"-o",
|
|
158
|
+
"-",
|
|
159
|
+
plist
|
|
160
|
+
]);
|
|
161
|
+
if (json === "") return void 0;
|
|
162
|
+
let handlers;
|
|
163
|
+
try {
|
|
164
|
+
handlers = JSON.parse(json).LSHandlers;
|
|
165
|
+
} catch {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (!Array.isArray(handlers)) return void 0;
|
|
169
|
+
for (const entry of handlers) {
|
|
170
|
+
if (entry === null || typeof entry !== "object") continue;
|
|
171
|
+
const row = entry;
|
|
172
|
+
if (row["LSHandlerURLScheme"] !== "https") continue;
|
|
173
|
+
const bundle = row["LSHandlerRoleAll"] ?? row["LSHandlerRoleViewer"];
|
|
174
|
+
if (typeof bundle !== "string" || bundle === "") continue;
|
|
175
|
+
if (/firefox|mozilla|safari/iu.test(bundle)) return void 0;
|
|
176
|
+
const paths = MAC_BUNDLE_PATHS[bundle.toLowerCase()];
|
|
177
|
+
if (paths === void 0) continue;
|
|
178
|
+
for (const candidate of [...paths, posix.join(homedir(), "Applications", ...paths[0].split("/").slice(2))]) if (host.exists(candidate)) return {
|
|
179
|
+
name: nameOf(candidate),
|
|
180
|
+
path: candidate
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/** Normalize a desktop id or executable name so `google-chrome` matches `google-chrome-stable`. */
|
|
185
|
+
function normalizeId(value) {
|
|
186
|
+
return value.toLowerCase().replace(/\.desktop$/u, "").replace(/[^a-z0-9]/gu, "");
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* The default handler on Linux.
|
|
190
|
+
*
|
|
191
|
+
* `xdg-settings` speaks the desktop-entry id (`google-chrome.desktop`), not a path, so the answer
|
|
192
|
+
* is matched against the candidates that already exist. `$BROWSER` is honoured first because it
|
|
193
|
+
* is what the user set explicitly.
|
|
194
|
+
*/
|
|
195
|
+
function linuxDefault(host, candidates) {
|
|
196
|
+
const declared = (host.env["BROWSER"] ?? "").trim();
|
|
197
|
+
if (declared !== "") {
|
|
198
|
+
if (/firefox|mozilla/iu.test(declared)) return void 0;
|
|
199
|
+
const asPath = declared.split(":")[0] ?? "";
|
|
200
|
+
if (asPath.includes("/") && host.exists(asPath)) return {
|
|
201
|
+
name: nameOf(asPath),
|
|
202
|
+
path: asPath
|
|
203
|
+
};
|
|
204
|
+
const wanted = normalizeId(basename(asPath));
|
|
205
|
+
const match = candidates.find((browser) => normalizeId(basename(browser.path)).includes(wanted));
|
|
206
|
+
if (match !== void 0) return match;
|
|
207
|
+
}
|
|
208
|
+
const desktopId = host.run("xdg-settings", ["get", "default-web-browser"]).trim();
|
|
209
|
+
if (desktopId === "") return void 0;
|
|
210
|
+
if (/firefox|mozilla/iu.test(desktopId)) return void 0;
|
|
211
|
+
const wanted = normalizeId(desktopId);
|
|
212
|
+
const match = candidates.find((browser) => {
|
|
213
|
+
const have = normalizeId(basename(browser.path));
|
|
214
|
+
return have === wanted || have.includes(wanted) || wanted.includes(have);
|
|
215
|
+
});
|
|
216
|
+
if (match !== void 0) return match;
|
|
217
|
+
for (const dir of [
|
|
218
|
+
"/usr/bin",
|
|
219
|
+
"/usr/local/bin",
|
|
220
|
+
"/snap/bin"
|
|
221
|
+
]) {
|
|
222
|
+
const path = posix.join(dir, desktopId.replace(/\.desktop$/u, ""));
|
|
223
|
+
if (host.exists(path)) return {
|
|
224
|
+
name: nameOf(path),
|
|
225
|
+
path
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
95
230
|
* The browser the user opens links with, when it is one this can drive.
|
|
96
231
|
*
|
|
97
232
|
* Returns undefined when there is no default, when the default is not a Chromium browser (Firefox
|
|
98
|
-
* cannot be driven this way), or when the
|
|
233
|
+
* cannot be driven this way), or when the answer cannot be read — the caller then falls back to
|
|
99
234
|
* its own list.
|
|
100
235
|
*/
|
|
101
|
-
function defaultBrowser() {
|
|
102
|
-
if (
|
|
103
|
-
|
|
236
|
+
function defaultBrowser(host = systemHost()) {
|
|
237
|
+
if (host.platform === "win32") return windowsDefault(host);
|
|
238
|
+
if (host.platform === "darwin") return macDefault(host);
|
|
239
|
+
if (host.platform === "linux") return linuxDefault(host, chromiumCandidates(host));
|
|
240
|
+
}
|
|
241
|
+
/** The default handler on Windows, from `UrlAssociations`. */
|
|
242
|
+
function windowsDefault(host) {
|
|
243
|
+
const choice = regQuery(host, "HKCU\\SOFTWARE\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\https\\UserChoice");
|
|
104
244
|
const progId = /ProgId\s+REG_SZ\s+(\S+)/iu.exec(choice)?.[1];
|
|
105
245
|
if (progId === void 0 || progId === "") return void 0;
|
|
106
246
|
/**
|
|
@@ -110,13 +250,17 @@ function defaultBrowser() {
|
|
|
110
250
|
* silently cannot be captured is worse than falling back to a browser that can.
|
|
111
251
|
*/
|
|
112
252
|
if (/firefox|mozilla/iu.test(progId)) return void 0;
|
|
113
|
-
const path = exeFromProgId(progId);
|
|
253
|
+
const path = exeFromProgId(host, progId);
|
|
114
254
|
if (path === void 0) return void 0;
|
|
115
255
|
return {
|
|
116
256
|
name: nameOf(path),
|
|
117
257
|
path
|
|
118
258
|
};
|
|
119
259
|
}
|
|
260
|
+
/** Every directory on PATH, split the way the platform splits it. */
|
|
261
|
+
function pathDirs(host) {
|
|
262
|
+
return (host.env["PATH"] ?? host.env["Path"] ?? "").split(host.platform === "win32" ? ";" : ":").map((dir) => dir.trim()).filter((dir) => dir !== "");
|
|
263
|
+
}
|
|
120
264
|
/**
|
|
121
265
|
* Every Chromium browser worth trying, most likely first.
|
|
122
266
|
*
|
|
@@ -124,26 +268,33 @@ function defaultBrowser() {
|
|
|
124
268
|
* each used to carry its own list — one with ten hard-coded paths, one with twenty-seven — and a
|
|
125
269
|
* browser added to only one of them was invisible to the other channel.
|
|
126
270
|
*/
|
|
127
|
-
function chromiumCandidates() {
|
|
128
|
-
const local = process.env["LOCALAPPDATA"] ?? "";
|
|
129
|
-
process.env["APPDATA"];
|
|
130
|
-
const pf = process.env["PROGRAMFILES"] ?? "";
|
|
131
|
-
const pf86 = process.env["PROGRAMFILES(X86)"] ?? process.env["ProgramFiles(x86)"] ?? "";
|
|
271
|
+
function chromiumCandidates(host = systemHost()) {
|
|
132
272
|
const found = [];
|
|
133
273
|
const add = (name, path) => {
|
|
134
|
-
if (
|
|
274
|
+
if (host.exists(path)) found.push({
|
|
135
275
|
name,
|
|
136
276
|
path
|
|
137
277
|
});
|
|
138
278
|
};
|
|
139
|
-
|
|
140
|
-
add(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
279
|
+
const addPath = (name) => {
|
|
280
|
+
for (const dir of pathDirs(host)) add(name, joinFor(host)(dir, name));
|
|
281
|
+
};
|
|
282
|
+
if (host.platform === "win32") windowsCandidates(host, add, addPath);
|
|
283
|
+
else if (host.platform === "darwin") macCandidates(host, add, addPath);
|
|
284
|
+
else if (host.platform === "linux") linuxCandidates(host, add, addPath);
|
|
285
|
+
const seen = /* @__PURE__ */ new Set();
|
|
286
|
+
return found.filter((browser) => {
|
|
287
|
+
const key = browser.path.toLowerCase();
|
|
288
|
+
if (seen.has(key)) return false;
|
|
289
|
+
seen.add(key);
|
|
290
|
+
return true;
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
/** Windows: registry ProgIds first, then the installer's usual directories. */
|
|
294
|
+
function windowsCandidates(host, add, addPath) {
|
|
295
|
+
const local = host.env["LOCALAPPDATA"] ?? "";
|
|
296
|
+
const pf = host.env["PROGRAMFILES"] ?? "";
|
|
297
|
+
const pf86 = host.env["PROGRAMFILES(X86)"] ?? host.env["ProgramFiles(x86)"] ?? "";
|
|
147
298
|
for (const exe of [
|
|
148
299
|
"chrome.exe",
|
|
149
300
|
"360ChromeX.exe",
|
|
@@ -157,8 +308,9 @@ function chromiumCandidates() {
|
|
|
157
308
|
"Quark.exe",
|
|
158
309
|
"msedge.exe"
|
|
159
310
|
]) {
|
|
160
|
-
const path =
|
|
311
|
+
const path = exeFromProgId(host, exe);
|
|
161
312
|
if (path !== void 0) add(nameOf(path), path);
|
|
313
|
+
addPath(exe);
|
|
162
314
|
}
|
|
163
315
|
add("Chrome", join(pf, "Google", "Chrome", "Application", "chrome.exe"));
|
|
164
316
|
add("Chrome", join(pf86, "Google", "Chrome", "Application", "chrome.exe"));
|
|
@@ -172,17 +324,60 @@ function chromiumCandidates() {
|
|
|
172
324
|
add("夸克", join(local, "Quark", "Quark.exe"));
|
|
173
325
|
add("Edge", join(pf86, "Microsoft", "Edge", "Application", "msedge.exe"));
|
|
174
326
|
add("Edge", join(pf, "Microsoft", "Edge", "Application", "msedge.exe"));
|
|
175
|
-
const seen = /* @__PURE__ */ new Set();
|
|
176
|
-
return found.filter((browser) => {
|
|
177
|
-
const key = browser.path.toLowerCase();
|
|
178
|
-
if (seen.has(key)) return false;
|
|
179
|
-
seen.add(key);
|
|
180
|
-
return true;
|
|
181
|
-
});
|
|
182
327
|
}
|
|
183
|
-
/**
|
|
184
|
-
function
|
|
185
|
-
|
|
328
|
+
/** macOS: `/Applications`, the per-user Applications folder, then PATH. */
|
|
329
|
+
function macCandidates(host, add, addPath) {
|
|
330
|
+
const roots = ["/Applications", posix.join(homedir(), "Applications")];
|
|
331
|
+
const apps = [
|
|
332
|
+
["Google Chrome", "Google Chrome"],
|
|
333
|
+
["Microsoft Edge", "Microsoft Edge"],
|
|
334
|
+
["Brave Browser", "Brave Browser"],
|
|
335
|
+
["Vivaldi", "Vivaldi"],
|
|
336
|
+
["Arc", "Arc"],
|
|
337
|
+
["Opera", "Opera"],
|
|
338
|
+
["Chromium", "Chromium"],
|
|
339
|
+
["Google Chrome Canary", "Google Chrome Canary"]
|
|
340
|
+
];
|
|
341
|
+
for (const root of roots) for (const [app, binary] of apps) add(app, posix.join(root, `${app}.app`, "Contents", "MacOS", binary));
|
|
342
|
+
for (const name of [
|
|
343
|
+
"google-chrome",
|
|
344
|
+
"chromium",
|
|
345
|
+
"microsoft-edge",
|
|
346
|
+
"brave-browser",
|
|
347
|
+
"vivaldi",
|
|
348
|
+
"opera"
|
|
349
|
+
]) addPath(name);
|
|
350
|
+
}
|
|
351
|
+
/** Linux: the usual distro, snap and /opt locations, then PATH. */
|
|
352
|
+
function linuxCandidates(host, add, addPath) {
|
|
353
|
+
for (const [name, path] of [
|
|
354
|
+
["Chrome", "/opt/google/chrome/chrome"],
|
|
355
|
+
["Chrome", "/usr/bin/google-chrome"],
|
|
356
|
+
["Chrome", "/usr/bin/google-chrome-stable"],
|
|
357
|
+
["Chromium", "/usr/bin/chromium"],
|
|
358
|
+
["Chromium", "/usr/bin/chromium-browser"],
|
|
359
|
+
["Chromium", "/snap/bin/chromium"],
|
|
360
|
+
["Chromium", "/usr/lib/chromium/chromium"],
|
|
361
|
+
["Edge", "/usr/bin/microsoft-edge"],
|
|
362
|
+
["Edge", "/usr/bin/microsoft-edge-stable"],
|
|
363
|
+
["Edge", "/opt/microsoft/msedge/msedge"],
|
|
364
|
+
["Brave", "/usr/bin/brave-browser"],
|
|
365
|
+
["Brave", "/opt/brave.com/brave/brave-browser"],
|
|
366
|
+
["Vivaldi", "/usr/bin/vivaldi"],
|
|
367
|
+
["Vivaldi", "/usr/bin/vivaldi-stable"],
|
|
368
|
+
["Opera", "/usr/bin/opera"],
|
|
369
|
+
["Opera", "/usr/lib/x86_64-linux-gnu/opera/opera"]
|
|
370
|
+
]) add(name, path);
|
|
371
|
+
for (const name of [
|
|
372
|
+
"google-chrome",
|
|
373
|
+
"google-chrome-stable",
|
|
374
|
+
"chromium",
|
|
375
|
+
"chromium-browser",
|
|
376
|
+
"microsoft-edge",
|
|
377
|
+
"brave-browser",
|
|
378
|
+
"vivaldi",
|
|
379
|
+
"opera"
|
|
380
|
+
]) addPath(name);
|
|
186
381
|
}
|
|
187
382
|
/**
|
|
188
383
|
* The browser to use for a sign-in window, preferring the one the user actually uses.
|
|
@@ -195,10 +390,10 @@ function exeFromProgIdPath(progId) {
|
|
|
195
390
|
*
|
|
196
391
|
* Order: the system default when it can be driven over CDP, then the known Chromium installs.
|
|
197
392
|
*/
|
|
198
|
-
function preferredLoginBrowser() {
|
|
199
|
-
const systemDefault = defaultBrowser();
|
|
393
|
+
function preferredLoginBrowser(host = systemHost()) {
|
|
394
|
+
const systemDefault = defaultBrowser(host);
|
|
200
395
|
if (systemDefault !== void 0) return systemDefault;
|
|
201
|
-
return chromiumCandidates()[0];
|
|
396
|
+
return chromiumCandidates(host)[0];
|
|
202
397
|
}
|
|
203
398
|
//#endregion
|
|
204
399
|
//#region src/doubao-auth.ts
|
|
@@ -638,7 +833,8 @@ async function evaluateOnPage(pageWs, expression) {
|
|
|
638
833
|
}
|
|
639
834
|
});
|
|
640
835
|
if (!answer.ok) throw new Error(answer.error);
|
|
641
|
-
|
|
836
|
+
const result = answer.result;
|
|
837
|
+
return result !== null && typeof result === "object" && "value" in result ? result.value : void 0;
|
|
642
838
|
}
|
|
643
839
|
/** The client version the reference implementation was measured against. */
|
|
644
840
|
const DOUBAO_VERSION$1 = "2.31.4";
|
|
@@ -3451,12 +3647,26 @@ function originTrusted(req, trusted = []) {
|
|
|
3451
3647
|
* browser card renders. Route paths are plugin-owned and mounted on the Host's
|
|
3452
3648
|
* same-origin web server (see `src/web-status.ts`).
|
|
3453
3649
|
*
|
|
3650
|
+
* ## Why `/dsh-xdpool`, and not `/plugins/dsh-xdpool`
|
|
3651
|
+
*
|
|
3652
|
+
* DSH 0.2.x gives the `/plugins/` namespace to the client-module asset server
|
|
3653
|
+
* (`/plugins/??@scope/pkg/client.js`). That server answers every request under
|
|
3654
|
+
* the prefix before named routes are considered: an unknown asset path is a
|
|
3655
|
+
* bodyless 404 and a non-GET is `405 Allow: GET, HEAD`. Routes this plugin
|
|
3656
|
+
* registered under `/dsh-xdpool/...` were therefore unreachable on that
|
|
3657
|
+
* line — status answered 404, the settings card's rescan answered 405 — while
|
|
3658
|
+
* the plugin logged "routes ... ok" because registration itself succeeded.
|
|
3659
|
+
*
|
|
3660
|
+
* A plugin-owned top-level prefix is what the host's own plugins use
|
|
3661
|
+
* (`/dsh-prompt-optimizer/optimize`), so these paths live under `/dsh-xdpool/`.
|
|
3662
|
+
* Keep them there.
|
|
3663
|
+
*
|
|
3454
3664
|
* @module dsh-xdpool/status-paths
|
|
3455
3665
|
*/
|
|
3456
3666
|
/** Plugin-owned read-only pool status endpoint (account rows + models + shim). */
|
|
3457
|
-
const POOL_STATUS_PATH = "/
|
|
3667
|
+
const POOL_STATUS_PATH = "/dsh-xdpool/status";
|
|
3458
3668
|
/** Plugin-owned local account rescan endpoint (re-read desktop snapshots). */
|
|
3459
|
-
const POOL_RESCAN_PATH = "/
|
|
3669
|
+
const POOL_RESCAN_PATH = "/dsh-xdpool/accounts/rescan";
|
|
3460
3670
|
/**
|
|
3461
3671
|
* Re-fetch the upstream model catalog for both regions.
|
|
3462
3672
|
*
|
|
@@ -3466,98 +3676,98 @@ const POOL_RESCAN_PATH = "/plugins/dsh-xdpool/accounts/rescan";
|
|
|
3466
3676
|
* fallen back to the static table after a failed startup fetch. The only way
|
|
3467
3677
|
* out was restarting DSH.
|
|
3468
3678
|
*/
|
|
3469
|
-
const POOL_CATALOG_REFRESH_PATH = "/
|
|
3679
|
+
const POOL_CATALOG_REFRESH_PATH = "/dsh-xdpool/models/refresh";
|
|
3470
3680
|
/** Plugin-owned cooldown reset endpoint (clear all 429 cooldowns). */
|
|
3471
|
-
const POOL_RESET_COOLDOWN_PATH = "/
|
|
3681
|
+
const POOL_RESET_COOLDOWN_PATH = "/dsh-xdpool/cooldowns/reset";
|
|
3472
3682
|
/** Plugin-owned daily check-in action endpoint (claim today's reward). */
|
|
3473
|
-
const POOL_CHECKIN_PATH = "/
|
|
3683
|
+
const POOL_CHECKIN_PATH = "/dsh-xdpool/checkin";
|
|
3474
3684
|
/** Plugin-owned model-selection save endpoint (writes the settings section). */
|
|
3475
|
-
const POOL_MODELS_SAVE_PATH = "/
|
|
3685
|
+
const POOL_MODELS_SAVE_PATH = "/dsh-xdpool/models/save";
|
|
3476
3686
|
/** Switch one account in or out of the pool (card toggle). */
|
|
3477
|
-
const POOL_ACCOUNT_DISABLE_PATH = "/
|
|
3687
|
+
const POOL_ACCOUNT_DISABLE_PATH = "/dsh-xdpool/accounts/disabled";
|
|
3478
3688
|
/**
|
|
3479
3689
|
* Throw one account out of the pool for good, or take it back.
|
|
3480
3690
|
*
|
|
3481
3691
|
* Separate from the disable route because the semantics differ: disabling is a
|
|
3482
3692
|
* rotation preference the account survives, ignoring survives the account.
|
|
3483
3693
|
*/
|
|
3484
|
-
const POOL_ACCOUNT_IGNORE_PATH = "/
|
|
3694
|
+
const POOL_ACCOUNT_IGNORE_PATH = "/dsh-xdpool/accounts/ignored";
|
|
3485
3695
|
/** Run one automation job immediately, so the card can verify it on demand. */
|
|
3486
|
-
const POOL_AUTOMATION_RUN_PATH = "/
|
|
3696
|
+
const POOL_AUTOMATION_RUN_PATH = "/dsh-xdpool/automation/run";
|
|
3487
3697
|
/** Is there a newer build of this plugin? */
|
|
3488
|
-
const POOL_UPDATE_CHECK_PATH = "/
|
|
3698
|
+
const POOL_UPDATE_CHECK_PATH = "/dsh-xdpool/update";
|
|
3489
3699
|
/** Marvis (腾讯应用宝 AI): the stored accounts, and how to forget them. */
|
|
3490
|
-
const POOL_MARVIS_SESSION_PATH = "/
|
|
3700
|
+
const POOL_MARVIS_SESSION_PATH = "/dsh-xdpool/marvis/session";
|
|
3491
3701
|
/** Open a browser and drive a QQ / WeChat sign-in there. */
|
|
3492
|
-
const POOL_MARVIS_LOGIN_PATH = "/
|
|
3702
|
+
const POOL_MARVIS_LOGIN_PATH = "/dsh-xdpool/marvis/login";
|
|
3493
3703
|
/** Swap a pasted callback URL for a session. */
|
|
3494
|
-
const POOL_MARVIS_PASTE_PATH = "/
|
|
3704
|
+
const POOL_MARVIS_PASTE_PATH = "/dsh-xdpool/marvis/paste";
|
|
3495
3705
|
/** The daily token wallet. */
|
|
3496
|
-
const POOL_MARVIS_WALLET_PATH = "/
|
|
3706
|
+
const POOL_MARVIS_WALLET_PATH = "/dsh-xdpool/marvis/wallet";
|
|
3497
3707
|
/** AStudio (科大讯飞 Astron Studio): the stored accounts. */
|
|
3498
|
-
const POOL_ASTUDIO_SESSION_PATH = "/
|
|
3708
|
+
const POOL_ASTUDIO_SESSION_PATH = "/dsh-xdpool/astudio/session";
|
|
3499
3709
|
/** AStudio: browser sign-in on the web console (phone + SMS code). */
|
|
3500
|
-
const POOL_ASTUDIO_BROWSER_LOGIN_PATH = "/
|
|
3710
|
+
const POOL_ASTUDIO_BROWSER_LOGIN_PATH = "/dsh-xdpool/astudio/browser-login";
|
|
3501
3711
|
/** Take a pasted credential. */
|
|
3502
|
-
const POOL_ASTUDIO_LOGIN_PATH = "/
|
|
3712
|
+
const POOL_ASTUDIO_LOGIN_PATH = "/dsh-xdpool/astudio/login";
|
|
3503
3713
|
/** Points balance and membership. */
|
|
3504
|
-
const POOL_ASTUDIO_WALLET_PATH = "/
|
|
3714
|
+
const POOL_ASTUDIO_WALLET_PATH = "/dsh-xdpool/astudio/wallet";
|
|
3505
3715
|
/** The daily login reward. */
|
|
3506
|
-
const POOL_ASTUDIO_CHECKIN_PATH = "/
|
|
3716
|
+
const POOL_ASTUDIO_CHECKIN_PATH = "/dsh-xdpool/astudio/checkin";
|
|
3507
3717
|
/** MagiCrew 国内: session, points, check-in. */
|
|
3508
3718
|
/** MagiCrew: phone + password sign-in (the JWT paste stays as the fallback). */
|
|
3509
|
-
const POOL_MAGICREW_CN_LOGIN_PATH = "/
|
|
3510
|
-
const POOL_MAGICREW_INTL_LOGIN_PATH = "/
|
|
3511
|
-
const POOL_MAGICREW_CN_SESSION_PATH = "/
|
|
3512
|
-
const POOL_MAGICREW_CN_WALLET_PATH = "/
|
|
3513
|
-
const POOL_MAGICREW_CN_CHECKIN_PATH = "/
|
|
3719
|
+
const POOL_MAGICREW_CN_LOGIN_PATH = "/dsh-xdpool/magicrew/login";
|
|
3720
|
+
const POOL_MAGICREW_INTL_LOGIN_PATH = "/dsh-xdpool/magicrew-intl/login";
|
|
3721
|
+
const POOL_MAGICREW_CN_SESSION_PATH = "/dsh-xdpool/magicrew/session";
|
|
3722
|
+
const POOL_MAGICREW_CN_WALLET_PATH = "/dsh-xdpool/magicrew/wallet";
|
|
3723
|
+
const POOL_MAGICREW_CN_CHECKIN_PATH = "/dsh-xdpool/magicrew/checkin";
|
|
3514
3724
|
/** MagiCrew 国际: its own account, its own balance. */
|
|
3515
|
-
const POOL_MAGICREW_INTL_SESSION_PATH = "/
|
|
3516
|
-
const POOL_MAGICREW_INTL_WALLET_PATH = "/
|
|
3517
|
-
const POOL_MAGICREW_INTL_CHECKIN_PATH = "/
|
|
3725
|
+
const POOL_MAGICREW_INTL_SESSION_PATH = "/dsh-xdpool/magicrew-intl/session";
|
|
3726
|
+
const POOL_MAGICREW_INTL_WALLET_PATH = "/dsh-xdpool/magicrew-intl/wallet";
|
|
3727
|
+
const POOL_MAGICREW_INTL_CHECKIN_PATH = "/dsh-xdpool/magicrew-intl/checkin";
|
|
3518
3728
|
/**
|
|
3519
3729
|
* 账号列表 / 开关 / 删单个账号。
|
|
3520
3730
|
*
|
|
3521
3731
|
* 和 session 分开:session 管「登录/退出」这条线,accounts 管「池子里这几个号」。
|
|
3522
3732
|
* 一个渠道多个账号时,退出整站和删掉某一个号是两件事。
|
|
3523
3733
|
*/
|
|
3524
|
-
const POOL_MAGICREW_CN_ACCOUNTS_PATH = "/
|
|
3525
|
-
const POOL_MAGICREW_INTL_ACCOUNTS_PATH = "/
|
|
3734
|
+
const POOL_MAGICREW_CN_ACCOUNTS_PATH = "/dsh-xdpool/magicrew/accounts";
|
|
3735
|
+
const POOL_MAGICREW_INTL_ACCOUNTS_PATH = "/dsh-xdpool/magicrew-intl/accounts";
|
|
3526
3736
|
/** Accio (阿里): the stored account, and the quota buckets. */
|
|
3527
|
-
const POOL_ACCIO_SESSION_PATH = "/
|
|
3737
|
+
const POOL_ACCIO_SESSION_PATH = "/dsh-xdpool/accio/session";
|
|
3528
3738
|
/** Accio: OAuth 2.0 + PKCE, on a loopback of our own. */
|
|
3529
|
-
const POOL_ACCIO_OAUTH_LOGIN_PATH = "/
|
|
3530
|
-
const POOL_ACCIO_QUOTA_PATH = "/
|
|
3739
|
+
const POOL_ACCIO_OAUTH_LOGIN_PATH = "/dsh-xdpool/accio/oauth-login";
|
|
3740
|
+
const POOL_ACCIO_QUOTA_PATH = "/dsh-xdpool/accio/quota";
|
|
3531
3741
|
/** MonkeyCode 国内: session and wallet. */
|
|
3532
|
-
const POOL_MONKEYCODE_CN_SESSION_PATH = "/
|
|
3742
|
+
const POOL_MONKEYCODE_CN_SESSION_PATH = "/dsh-xdpool/monkeycode/session";
|
|
3533
3743
|
/** MonkeyCode: browser sign-in (the session cookie is HttpOnly). */
|
|
3534
|
-
const POOL_MONKEYCODE_CN_BROWSER_LOGIN_PATH = "/
|
|
3535
|
-
const POOL_MONKEYCODE_INTL_BROWSER_LOGIN_PATH = "/
|
|
3536
|
-
const POOL_MONKEYCODE_CN_WALLET_PATH = "/
|
|
3744
|
+
const POOL_MONKEYCODE_CN_BROWSER_LOGIN_PATH = "/dsh-xdpool/monkeycode/browser-login";
|
|
3745
|
+
const POOL_MONKEYCODE_INTL_BROWSER_LOGIN_PATH = "/dsh-xdpool/monkeycode-intl/browser-login";
|
|
3746
|
+
const POOL_MONKEYCODE_CN_WALLET_PATH = "/dsh-xdpool/monkeycode/wallet";
|
|
3537
3747
|
/** MonkeyCode 国际: its own account. */
|
|
3538
|
-
const POOL_MONKEYCODE_INTL_SESSION_PATH = "/
|
|
3539
|
-
const POOL_MONKEYCODE_INTL_WALLET_PATH = "/
|
|
3748
|
+
const POOL_MONKEYCODE_INTL_SESSION_PATH = "/dsh-xdpool/monkeycode-intl/session";
|
|
3749
|
+
const POOL_MONKEYCODE_INTL_WALLET_PATH = "/dsh-xdpool/monkeycode-intl/wallet";
|
|
3540
3750
|
/** 百智云: the stored key and session, the wallet, and the daily credit. */
|
|
3541
|
-
const POOL_BAIZHI_SESSION_PATH = "/
|
|
3751
|
+
const POOL_BAIZHI_SESSION_PATH = "/dsh-xdpool/baizhi/session";
|
|
3542
3752
|
/** 百智云: browser sign-in, which also fetches the API key. */
|
|
3543
|
-
const POOL_BAIZHI_BROWSER_LOGIN_PATH = "/
|
|
3544
|
-
const POOL_BAIZHI_WALLET_PATH = "/
|
|
3545
|
-
const POOL_BAIZHI_CHECKIN_PATH = "/
|
|
3753
|
+
const POOL_BAIZHI_BROWSER_LOGIN_PATH = "/dsh-xdpool/baizhi/browser-login";
|
|
3754
|
+
const POOL_BAIZHI_WALLET_PATH = "/dsh-xdpool/baizhi/wallet";
|
|
3755
|
+
const POOL_BAIZHI_CHECKIN_PATH = "/dsh-xdpool/baizhi/checkin";
|
|
3546
3756
|
/** AutoClaw 国际: session and the five-bucket wallet. */
|
|
3547
|
-
const POOL_AUTOCLAW_INTL_SESSION_PATH = "/
|
|
3757
|
+
const POOL_AUTOCLAW_INTL_SESSION_PATH = "/dsh-xdpool/autoclaw/session";
|
|
3548
3758
|
/** AutoClaw: SMS sign-in (send a code, then exchange it). */
|
|
3549
|
-
const POOL_AUTOCLAW_INTL_SEND_CODE_PATH = "/
|
|
3550
|
-
const POOL_AUTOCLAW_INTL_CODE_LOGIN_PATH = "/
|
|
3551
|
-
const POOL_AUTOCLAW_CN_SEND_CODE_PATH = "/
|
|
3552
|
-
const POOL_AUTOCLAW_CN_CODE_LOGIN_PATH = "/
|
|
3553
|
-
const POOL_AUTOCLAW_INTL_WALLET_PATH = "/
|
|
3759
|
+
const POOL_AUTOCLAW_INTL_SEND_CODE_PATH = "/dsh-xdpool/autoclaw/send-code";
|
|
3760
|
+
const POOL_AUTOCLAW_INTL_CODE_LOGIN_PATH = "/dsh-xdpool/autoclaw/code-login";
|
|
3761
|
+
const POOL_AUTOCLAW_CN_SEND_CODE_PATH = "/dsh-xdpool/autoclaw-cn/send-code";
|
|
3762
|
+
const POOL_AUTOCLAW_CN_CODE_LOGIN_PATH = "/dsh-xdpool/autoclaw-cn/code-login";
|
|
3763
|
+
const POOL_AUTOCLAW_INTL_WALLET_PATH = "/dsh-xdpool/autoclaw/wallet";
|
|
3554
3764
|
/** AutoClaw 国内: its own account on a different host. */
|
|
3555
|
-
const POOL_AUTOCLAW_CN_SESSION_PATH = "/
|
|
3556
|
-
const POOL_AUTOCLAW_CN_WALLET_PATH = "/
|
|
3765
|
+
const POOL_AUTOCLAW_CN_SESSION_PATH = "/dsh-xdpool/autoclaw-cn/session";
|
|
3766
|
+
const POOL_AUTOCLAW_CN_WALLET_PATH = "/dsh-xdpool/autoclaw-cn/wallet";
|
|
3557
3767
|
/** Install the newest build. The host applies it on the next start. */
|
|
3558
|
-
const POOL_UPDATE_INSTALL_PATH = "/
|
|
3768
|
+
const POOL_UPDATE_INSTALL_PATH = "/dsh-xdpool/update/install";
|
|
3559
3769
|
/** Set or clear one account's reserved-credit floor. */
|
|
3560
|
-
const POOL_CREDIT_RESERVE_PATH = "/
|
|
3770
|
+
const POOL_CREDIT_RESERVE_PATH = "/dsh-xdpool/accounts/credit-reserve";
|
|
3561
3771
|
/**
|
|
3562
3772
|
* Estimated credit spend, per channel and per model.
|
|
3563
3773
|
*
|
|
@@ -3568,7 +3778,7 @@ const POOL_CREDIT_RESERVE_PATH = "/plugins/dsh-xdpool/accounts/credit-reserve";
|
|
|
3568
3778
|
*
|
|
3569
3779
|
* `?days=N` limits the window; the ledger keeps 90 days.
|
|
3570
3780
|
*/
|
|
3571
|
-
const POOL_CREDITS_SPEND_PATH = "/
|
|
3781
|
+
const POOL_CREDITS_SPEND_PATH = "/dsh-xdpool/credits/spend";
|
|
3572
3782
|
/**
|
|
3573
3783
|
* Credits actually spent TODAY, measured rather than priced.
|
|
3574
3784
|
*
|
|
@@ -3583,7 +3793,7 @@ const POOL_CREDITS_SPEND_PATH = "/plugins/dsh-xdpool/credits/spend";
|
|
|
3583
3793
|
* GET only, no parameters. `available: false` means nothing has been recorded for the
|
|
3584
3794
|
* day, which is not the same answer as `total: 0`.
|
|
3585
3795
|
*/
|
|
3586
|
-
const POOL_CREDITS_TODAY_PATH = "/
|
|
3796
|
+
const POOL_CREDITS_TODAY_PATH = "/dsh-xdpool/credits/today";
|
|
3587
3797
|
/**
|
|
3588
3798
|
* The dashboard's own document: real credit spend and grants per account and day.
|
|
3589
3799
|
*
|
|
@@ -3602,7 +3812,7 @@ const POOL_CREDITS_TODAY_PATH = "/plugins/dsh-xdpool/credits/today";
|
|
|
3602
3812
|
* The host caches the assembled document for a few minutes; `?refresh=1` forces a
|
|
3603
3813
|
* re-read, which is what the panel's own "refresh" button sends.
|
|
3604
3814
|
*/
|
|
3605
|
-
const POOL_SPEND_REPORT_PATH = "/
|
|
3815
|
+
const POOL_SPEND_REPORT_PATH = "/dsh-xdpool/spend/report";
|
|
3606
3816
|
/**
|
|
3607
3817
|
* Name the account to try first, or hand the choice back to the strategy.
|
|
3608
3818
|
*
|
|
@@ -3612,7 +3822,7 @@ const POOL_SPEND_REPORT_PATH = "/plugins/dsh-xdpool/spend/report";
|
|
|
3612
3822
|
* write, pushes it into the pool immediately so the very next request sees it,
|
|
3613
3823
|
* and is the one path both halves share.
|
|
3614
3824
|
*/
|
|
3615
|
-
const POOL_ACCOUNT_PREFER_PATH = "/
|
|
3825
|
+
const POOL_ACCOUNT_PREFER_PATH = "/dsh-xdpool/accounts/preferred";
|
|
3616
3826
|
/**
|
|
3617
3827
|
* Read the aggregate lane's state: every registry provider, its saved setting
|
|
3618
3828
|
* (never its key), and whether it can serve anything right now.
|
|
@@ -3623,7 +3833,7 @@ const POOL_ACCOUNT_PREFER_PATH = "/plugins/dsh-xdpool/accounts/preferred";
|
|
|
3623
3833
|
* status document would make every 30-second poll of the pool card carry a
|
|
3624
3834
|
* directory nobody reads most of the time.
|
|
3625
3835
|
*/
|
|
3626
|
-
const POOL_AGGREGATE_PATH = "/
|
|
3836
|
+
const POOL_AGGREGATE_PATH = "/dsh-xdpool/aggregate";
|
|
3627
3837
|
/**
|
|
3628
3838
|
* Save one aggregate provider's setting: its key, its on/off switch, its base-URL
|
|
3629
3839
|
* override, or the model ids a listing round discovered.
|
|
@@ -3631,7 +3841,7 @@ const POOL_AGGREGATE_PATH = "/plugins/dsh-xdpool/aggregate";
|
|
|
3631
3841
|
* POST only, and one provider per request — read-modify-write on the host side,
|
|
3632
3842
|
* so two tabs editing two providers cannot clobber each other.
|
|
3633
3843
|
*/
|
|
3634
|
-
const POOL_AGGREGATE_SAVE_PATH = "/
|
|
3844
|
+
const POOL_AGGREGATE_SAVE_PATH = "/dsh-xdpool/aggregate/save";
|
|
3635
3845
|
/**
|
|
3636
3846
|
* Verify one provider's key with a cheap non-generating call, and report what
|
|
3637
3847
|
* its listing answered.
|
|
@@ -3640,7 +3850,7 @@ const POOL_AGGREGATE_SAVE_PATH = "/plugins/dsh-xdpool/aggregate/save";
|
|
|
3640
3850
|
* network: a probe must never run as a side effect of saving a setting, or
|
|
3641
3851
|
* "save" would silently spend the user's rate limit.
|
|
3642
3852
|
*/
|
|
3643
|
-
const POOL_AGGREGATE_TEST_PATH = "/
|
|
3853
|
+
const POOL_AGGREGATE_TEST_PATH = "/dsh-xdpool/aggregate/test";
|
|
3644
3854
|
/**
|
|
3645
3855
|
* Send one real streaming call to one model and report how it answered.
|
|
3646
3856
|
*
|
|
@@ -3654,7 +3864,7 @@ const POOL_AGGREGATE_TEST_PATH = "/plugins/dsh-xdpool/aggregate/test";
|
|
|
3654
3864
|
* rather than through this plugin's lanes, because that registry is the only
|
|
3655
3865
|
* place that knows which credential and endpoint a given provider id serves.
|
|
3656
3866
|
*/
|
|
3657
|
-
const POOL_MODEL_PROBE_PATH = "/
|
|
3867
|
+
const POOL_MODEL_PROBE_PATH = "/dsh-xdpool/model/test";
|
|
3658
3868
|
/**
|
|
3659
3869
|
* POST-only: ask what models several login channels contribute right now.
|
|
3660
3870
|
*
|
|
@@ -3669,7 +3879,7 @@ const POOL_MODEL_PROBE_PATH = "/plugins/dsh-xdpool/model/test";
|
|
|
3669
3879
|
* this is for. The host's catalogue is the same one the model picker reads, so
|
|
3670
3880
|
* the two agree by construction instead of by luck.
|
|
3671
3881
|
*/
|
|
3672
|
-
const POOL_CHANNEL_MODELS_PATH = "/
|
|
3882
|
+
const POOL_CHANNEL_MODELS_PATH = "/dsh-xdpool/channels/models";
|
|
3673
3883
|
/**
|
|
3674
3884
|
* Fetch one provider's live model list and write it back as its discovered set.
|
|
3675
3885
|
*
|
|
@@ -3677,7 +3887,7 @@ const POOL_CHANNEL_MODELS_PATH = "/plugins/dsh-xdpool/channels/models";
|
|
|
3677
3887
|
* changes what the provider contributes to the picker — so it is its own route
|
|
3678
3888
|
* with its own confirmation, and a failed round keeps the previous roster.
|
|
3679
3889
|
*/
|
|
3680
|
-
const POOL_AGGREGATE_DISCOVER_PATH = "/
|
|
3890
|
+
const POOL_AGGREGATE_DISCOVER_PATH = "/dsh-xdpool/aggregate/discover";
|
|
3681
3891
|
/**
|
|
3682
3892
|
* The thirteen login channels the vendored pack provides.
|
|
3683
3893
|
*
|
|
@@ -3686,7 +3896,7 @@ const POOL_AGGREGATE_DISCOVER_PATH = "/plugins/dsh-xdpool/aggregate/discover";
|
|
|
3686
3896
|
* channel list is "what can I scan a QR code into", and mixing them in one poll
|
|
3687
3897
|
* would put a third-party directory on the critical path of the pool card.
|
|
3688
3898
|
*/
|
|
3689
|
-
const POOL_CHANNELS_PATH = "/
|
|
3899
|
+
const POOL_CHANNELS_PATH = "/dsh-xdpool/channels";
|
|
3690
3900
|
/**
|
|
3691
3901
|
* Per-channel usage order.
|
|
3692
3902
|
*
|
|
@@ -3695,22 +3905,22 @@ const POOL_CHANNELS_PATH = "/plugins/dsh-xdpool/channels";
|
|
|
3695
3905
|
* this route is how it reads and writes — the alternative is a page that cannot change a control
|
|
3696
3906
|
* it displays.
|
|
3697
3907
|
*/
|
|
3698
|
-
const POOL_CHANNEL_USAGE_PATH = "/
|
|
3908
|
+
const POOL_CHANNEL_USAGE_PATH = "/dsh-xdpool/channels/usage";
|
|
3699
3909
|
/**
|
|
3700
3910
|
* The Doubao lane's session.
|
|
3701
3911
|
*
|
|
3702
3912
|
* POST starts an interactive browser sign-in and answers once it lands;
|
|
3703
3913
|
* GET reports whether a session is stored; DELETE forgets it.
|
|
3704
3914
|
*/
|
|
3705
|
-
const POOL_DOUBAO_SESSION_PATH = "/
|
|
3915
|
+
const POOL_DOUBAO_SESSION_PATH = "/dsh-xdpool/doubao/session";
|
|
3706
3916
|
/** The Doubao quota: the windows the account has, with their reset countdowns. */
|
|
3707
|
-
const POOL_DOUBAO_QUOTA_PATH = "/
|
|
3917
|
+
const POOL_DOUBAO_QUOTA_PATH = "/dsh-xdpool/doubao/quota";
|
|
3708
3918
|
/** The models the account may use, as the vendor publishes them. */
|
|
3709
|
-
const POOL_DOUBAO_MODELS_PATH = "/
|
|
3919
|
+
const POOL_DOUBAO_MODELS_PATH = "/dsh-xdpool/doubao/models";
|
|
3710
3920
|
/** The account's credit tasks: what is waiting, and what has been collected. */
|
|
3711
|
-
const POOL_DOUBAO_CAMPAIGN_PATH = "/
|
|
3921
|
+
const POOL_DOUBAO_CAMPAIGN_PATH = "/dsh-xdpool/doubao/campaign";
|
|
3712
3922
|
/** Collect every claimable task. */
|
|
3713
|
-
const POOL_DOUBAO_CLAIM_PATH = "/
|
|
3923
|
+
const POOL_DOUBAO_CLAIM_PATH = "/dsh-xdpool/doubao/claim";
|
|
3714
3924
|
/**
|
|
3715
3925
|
* Doubao's credit automation: read the switch, or turn it on and off.
|
|
3716
3926
|
*
|
|
@@ -3718,14 +3928,14 @@ const POOL_DOUBAO_CLAIM_PATH = "/plugins/dsh-xdpool/doubao/claim";
|
|
|
3718
3928
|
* "collect whatever is claimable, on a schedule" — the same promise WorkBuddy's automation
|
|
3719
3929
|
* makes, over a different mechanism.
|
|
3720
3930
|
*/
|
|
3721
|
-
const POOL_DOUBAO_AUTOMATION_PATH = "/
|
|
3931
|
+
const POOL_DOUBAO_AUTOMATION_PATH = "/dsh-xdpool/doubao/automation";
|
|
3722
3932
|
/**
|
|
3723
3933
|
* 自动打开登录页面的偏好(阶段 A4):读当前值 / 写入开关。
|
|
3724
3934
|
* 默认开,只有显式 `false` 才关 —— 语义见 login-preferences.ts。
|
|
3725
3935
|
*/
|
|
3726
|
-
const POOL_LOGIN_PREFERENCES_PATH = "/
|
|
3936
|
+
const POOL_LOGIN_PREFERENCES_PATH = "/dsh-xdpool/login-preferences";
|
|
3727
3937
|
/** 实时路由决策轨迹(CreditDot 弹窗轮询)。 */
|
|
3728
|
-
const POOL_ROUTE_TRACE_PATH = "/
|
|
3938
|
+
const POOL_ROUTE_TRACE_PATH = "/dsh-xdpool/route-trace";
|
|
3729
3939
|
/**
|
|
3730
3940
|
* The `reason` the channel document carries when the login-channel route is
|
|
3731
3941
|
* already owned by another plugin.
|
|
@@ -3748,7 +3958,7 @@ const ROUTE_OWNED_ELSEWHERE = "already-mounted-elsewhere";
|
|
|
3748
3958
|
* vendors is a list nobody can pick from. Reading two documents on one panel
|
|
3749
3959
|
* open would also mean the roster could be a second older than the chains.
|
|
3750
3960
|
*/
|
|
3751
|
-
const POOL_COMBOS_PATH = "/
|
|
3961
|
+
const POOL_COMBOS_PATH = "/dsh-xdpool/combos";
|
|
3752
3962
|
/**
|
|
3753
3963
|
* Replace the whole chain list in one write.
|
|
3754
3964
|
*
|
|
@@ -3758,10 +3968,10 @@ const POOL_COMBOS_PATH = "/plugins/dsh-xdpool/combos";
|
|
|
3758
3968
|
* merge rule would have to guess, and a wrong guess silently resurrects a row the
|
|
3759
3969
|
* user deleted.
|
|
3760
3970
|
*/
|
|
3761
|
-
const POOL_COMBOS_SAVE_PATH = "/
|
|
3971
|
+
const POOL_COMBOS_SAVE_PATH = "/dsh-xdpool/combos/save";
|
|
3762
3972
|
/** 路由组的读写(阶段 B4-4):与 combos 同构的文档 + 保存端点。 */
|
|
3763
|
-
const POOL_GROUPS_PATH = "/
|
|
3764
|
-
const POOL_GROUPS_SAVE_PATH = "/
|
|
3973
|
+
const POOL_GROUPS_PATH = "/dsh-xdpool/groups";
|
|
3974
|
+
const POOL_GROUPS_SAVE_PATH = "/dsh-xdpool/groups/save";
|
|
3765
3975
|
/**
|
|
3766
3976
|
* The LAN relay's own state, and the only way to change it.
|
|
3767
3977
|
*
|
|
@@ -3770,7 +3980,7 @@ const POOL_GROUPS_SAVE_PATH = "/plugins/dsh-xdpool/groups/save";
|
|
|
3770
3980
|
* a relay left listening on a network interface is the one failure here that
|
|
3771
3981
|
* keeps mattering after the plugin is closed.
|
|
3772
3982
|
*/
|
|
3773
|
-
const POOL_RELAY_PATH = "/
|
|
3983
|
+
const POOL_RELAY_PATH = "/dsh-xdpool/relay";
|
|
3774
3984
|
/**
|
|
3775
3985
|
* Read the usage panel's aggregate.
|
|
3776
3986
|
*
|
|
@@ -3780,7 +3990,7 @@ const POOL_RELAY_PATH = "/plugins/dsh-xdpool/relay";
|
|
|
3780
3990
|
* Node-free precisely so the browser half can import the SAME declaration
|
|
3781
3991
|
* instead of a second one that could quietly drift.
|
|
3782
3992
|
*/
|
|
3783
|
-
const POOL_USAGE_PATH = "/
|
|
3993
|
+
const POOL_USAGE_PATH = "/dsh-xdpool/usage";
|
|
3784
3994
|
/**
|
|
3785
3995
|
* 某个会话「现在走哪家、用哪个号」。
|
|
3786
3996
|
*
|
|
@@ -3792,15 +4002,15 @@ const POOL_USAGE_PATH = "/plugins/dsh-xdpool/usage";
|
|
|
3792
4002
|
* (角标每次换模型/每轮结束都要读,账本是用户主动点开),以及形状不同
|
|
3793
4003
|
* (这里只有一条记录,账本是一份多表文档)。
|
|
3794
4004
|
*/
|
|
3795
|
-
const POOL_SESSION_ROUTE_PATH = "/
|
|
4005
|
+
const POOL_SESSION_ROUTE_PATH = "/dsh-xdpool/session";
|
|
3796
4006
|
/**
|
|
3797
|
-
* 强制某个会话用指定账号:`POST /
|
|
4007
|
+
* 强制某个会话用指定账号:`POST /dsh-xdpool/session/pin?sessionId=<id>`,
|
|
3798
4008
|
* body `{accountId: string | null}`,`null` 表示取消。
|
|
3799
4009
|
*
|
|
3800
4010
|
* 与只读的归属路由分开:这一条会改选号行为,所以只收 POST、只信本机 origin、
|
|
3801
4011
|
* 只接一个很小的 JSON body。它只存账号 id 字符串,不碰凭据(见 session-route.ts)。
|
|
3802
4012
|
*/
|
|
3803
|
-
const POOL_SESSION_PIN_PATH = "/
|
|
4013
|
+
const POOL_SESSION_PIN_PATH = "/dsh-xdpool/session/pin";
|
|
3804
4014
|
/** The five strategies, as a value, so a request body can be validated against it. */
|
|
3805
4015
|
const POOL_DISTRIBUTIONS = Object.freeze([
|
|
3806
4016
|
"priority",
|
|
@@ -4494,7 +4704,7 @@ function doubaoSessionPath() {
|
|
|
4494
4704
|
* `sessionid` prefix is the only stable thing, and it changes when the session is rotated — which
|
|
4495
4705
|
* is the case a re-login SHOULD produce a new entry for anyway.
|
|
4496
4706
|
*/
|
|
4497
|
-
function accountIdOf(session) {
|
|
4707
|
+
function accountIdOf$1(session) {
|
|
4498
4708
|
const uin = session.cookies["uin"];
|
|
4499
4709
|
if (typeof uin === "string" && uin !== "") return `uin-${uin}`;
|
|
4500
4710
|
return `sid-${(session.cookies["sessionid"] ?? "").slice(0, 12)}`;
|
|
@@ -4541,7 +4751,7 @@ async function readStoredAccounts() {
|
|
|
4541
4751
|
const session = asSession(record.session);
|
|
4542
4752
|
if (session === void 0) continue;
|
|
4543
4753
|
accounts.push({
|
|
4544
|
-
id: typeof record.id === "string" && record.id !== "" ? record.id : accountIdOf(session),
|
|
4754
|
+
id: typeof record.id === "string" && record.id !== "" ? record.id : accountIdOf$1(session),
|
|
4545
4755
|
session,
|
|
4546
4756
|
...typeof record.nickname === "string" && record.nickname !== "" ? { nickname: record.nickname } : {},
|
|
4547
4757
|
savedAt: typeof record.savedAt === "string" ? record.savedAt : session.savedAt,
|
|
@@ -4556,7 +4766,7 @@ async function readStoredAccounts() {
|
|
|
4556
4766
|
*/
|
|
4557
4767
|
const legacy = await readLegacy();
|
|
4558
4768
|
if (legacy !== void 0) {
|
|
4559
|
-
const id = accountIdOf(legacy);
|
|
4769
|
+
const id = accountIdOf$1(legacy);
|
|
4560
4770
|
if (!accounts.some((account) => account.id === id)) accounts.push({
|
|
4561
4771
|
id,
|
|
4562
4772
|
session: legacy,
|
|
@@ -4584,7 +4794,7 @@ async function writeAccounts$2(accounts) {
|
|
|
4584
4794
|
* instead of appending a duplicate — which is what a user does when a session expires.
|
|
4585
4795
|
*/
|
|
4586
4796
|
async function writeStoredAccount(session, nickname, slot) {
|
|
4587
|
-
const id = accountIdOf(session);
|
|
4797
|
+
const id = accountIdOf$1(session);
|
|
4588
4798
|
const existing = await readStoredAccounts();
|
|
4589
4799
|
const entry = {
|
|
4590
4800
|
id,
|
|
@@ -4616,7 +4826,7 @@ function lowestFreeSlot(accounts) {
|
|
|
4616
4826
|
async function removeStoredAccount(id) {
|
|
4617
4827
|
await writeAccounts$2((await readStoredAccounts()).filter((account) => account.id !== id));
|
|
4618
4828
|
const legacy = await readLegacy();
|
|
4619
|
-
if (legacy !== void 0 && accountIdOf(legacy) === id) try {
|
|
4829
|
+
if (legacy !== void 0 && accountIdOf$1(legacy) === id) try {
|
|
4620
4830
|
await unlink(doubaoSessionPath());
|
|
4621
4831
|
} catch {}
|
|
4622
4832
|
}
|
|
@@ -4953,7 +5163,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
4953
5163
|
ok: true,
|
|
4954
5164
|
signedIn: true,
|
|
4955
5165
|
accounts: [{
|
|
4956
|
-
id: accountIdOf(active),
|
|
5166
|
+
id: accountIdOf$1(active),
|
|
4957
5167
|
...name === void 0 ? {} : { name },
|
|
4958
5168
|
...active.savedAt === void 0 ? {} : { savedAt: active.savedAt },
|
|
4959
5169
|
active: true
|
|
@@ -4962,7 +5172,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
4962
5172
|
}
|
|
4963
5173
|
const rows = [];
|
|
4964
5174
|
for (const entry of accounts) {
|
|
4965
|
-
const isActive = active !== void 0 && accountIdOf(active) === entry.id;
|
|
5175
|
+
const isActive = active !== void 0 && accountIdOf$1(active) === entry.id;
|
|
4966
5176
|
/**
|
|
4967
5177
|
* Ask the vendor for the name of an account that has none stored.
|
|
4968
5178
|
*
|
|
@@ -5292,7 +5502,7 @@ async function readJson(req) {
|
|
|
5292
5502
|
return {};
|
|
5293
5503
|
}
|
|
5294
5504
|
}
|
|
5295
|
-
/** Register `GET`/`POST /
|
|
5505
|
+
/** Register `GET`/`POST /dsh-xdpool/channels/usage`. */
|
|
5296
5506
|
function registerChannelUsageRoute(ctx, deps) {
|
|
5297
5507
|
const state = () => ({
|
|
5298
5508
|
pool: deps.pool(),
|
|
@@ -13244,7 +13454,20 @@ async function refreshAutoclawToken(site, refreshToken, options = {}) {
|
|
|
13244
13454
|
//#endregion
|
|
13245
13455
|
//#region src/autoclaw-store.ts
|
|
13246
13456
|
/**
|
|
13247
|
-
* Where AutoClaw accounts live,
|
|
13457
|
+
* Where AutoClaw accounts live, several per site.
|
|
13458
|
+
*
|
|
13459
|
+
* ONE SITE, MORE THAN ONE ACCOUNT
|
|
13460
|
+
*
|
|
13461
|
+
* The store used to be keyed by site alone: `save` replaced the row for that site, `read` took
|
|
13462
|
+
* the first match and `remove` dropped every row for the site. Signing in with a second account
|
|
13463
|
+
* therefore made the first one disappear with no message, and a token rotation looked like a new
|
|
13464
|
+
* account. Accounts are now kept with a stable id, and the site is only the provider they belong
|
|
13465
|
+
* to.
|
|
13466
|
+
*
|
|
13467
|
+
* The id is derived from something the vendor owns — the site's own `userId`, a JWT subject, or
|
|
13468
|
+
* the refresh token — so a repeated sign-in replaces its row instead of appending a duplicate.
|
|
13469
|
+
* Records written before ids existed are still read: their id is derived on read and written on
|
|
13470
|
+
* the next save.
|
|
13248
13471
|
*
|
|
13249
13472
|
* @module dsh-xdpool/autoclaw-store
|
|
13250
13473
|
*/
|
|
@@ -13254,19 +13477,61 @@ const FILE = "autoclaw-accounts.json";
|
|
|
13254
13477
|
function autoclawAccountsPath() {
|
|
13255
13478
|
return join(pluginDataDir(), FILE);
|
|
13256
13479
|
}
|
|
13480
|
+
/** Read one JWT claim from a token, when the token is a JWT at all. */
|
|
13481
|
+
function jwtClaim(token, names) {
|
|
13482
|
+
try {
|
|
13483
|
+
const parts = token.split(".");
|
|
13484
|
+
if (parts.length < 2) return void 0;
|
|
13485
|
+
const payload = JSON.parse(Buffer.from(parts[1] ?? "", "base64").toString("utf8"));
|
|
13486
|
+
for (const name of names) {
|
|
13487
|
+
const value = payload[name];
|
|
13488
|
+
if (typeof value === "string" && value !== "") return value;
|
|
13489
|
+
if (typeof value === "number") return String(value);
|
|
13490
|
+
}
|
|
13491
|
+
} catch {}
|
|
13492
|
+
}
|
|
13493
|
+
/**
|
|
13494
|
+
* The seed that identifies an account across token rotations.
|
|
13495
|
+
*
|
|
13496
|
+
* Preference order: the vendor's own user id, the JWT subject, the refresh token, the access
|
|
13497
|
+
* token. The first two survive rotation; the refresh token survives an access-token rotation;
|
|
13498
|
+
* the access token is the last resort and at least keeps two different accounts apart.
|
|
13499
|
+
*/
|
|
13500
|
+
function identitySeed(credential) {
|
|
13501
|
+
if (credential.accountId !== void 0 && credential.accountId !== "") return `u:${credential.accountId}`;
|
|
13502
|
+
const subject = jwtClaim(credential.accessToken, [
|
|
13503
|
+
"sub",
|
|
13504
|
+
"userId",
|
|
13505
|
+
"user_id",
|
|
13506
|
+
"uid"
|
|
13507
|
+
]);
|
|
13508
|
+
if (subject !== void 0) return `j:${subject}`;
|
|
13509
|
+
if (credential.refreshToken !== void 0 && credential.refreshToken !== "") return `r:${credential.refreshToken}`;
|
|
13510
|
+
return `t:${credential.accessToken}`;
|
|
13511
|
+
}
|
|
13512
|
+
/** The stable id of one account, scoped to the site it belongs to. */
|
|
13513
|
+
function autoclawAccountId(credential) {
|
|
13514
|
+
const digest = createHash("sha256").update(identitySeed(credential)).digest("hex").slice(0, 12);
|
|
13515
|
+
return `${credential.site}:${digest}`;
|
|
13516
|
+
}
|
|
13257
13517
|
/** Whether a value is a usable stored credential. */
|
|
13258
13518
|
function asCredential(value) {
|
|
13259
13519
|
if (value === null || typeof value !== "object") return void 0;
|
|
13260
13520
|
const record = value;
|
|
13261
13521
|
if (typeof record.site !== "string" || record.site === "") return void 0;
|
|
13262
13522
|
if (typeof record.accessToken !== "string" || record.accessToken === "") return void 0;
|
|
13263
|
-
|
|
13523
|
+
const credential = {
|
|
13264
13524
|
site: record.site,
|
|
13265
13525
|
accessToken: record.accessToken,
|
|
13266
13526
|
...typeof record.refreshToken === "string" && record.refreshToken !== "" ? { refreshToken: record.refreshToken } : {},
|
|
13267
13527
|
...typeof record.nickname === "string" && record.nickname !== "" ? { nickname: record.nickname } : {},
|
|
13528
|
+
...typeof record.accountId === "string" && record.accountId !== "" ? { accountId: record.accountId } : {},
|
|
13268
13529
|
...typeof record.expiresAt === "number" ? { expiresAt: record.expiresAt } : {}
|
|
13269
13530
|
};
|
|
13531
|
+
return {
|
|
13532
|
+
...credential,
|
|
13533
|
+
id: typeof record.id === "string" && record.id !== "" ? record.id : autoclawAccountId(credential)
|
|
13534
|
+
};
|
|
13270
13535
|
}
|
|
13271
13536
|
/** Every stored credential. */
|
|
13272
13537
|
async function readAutoclawCredentials() {
|
|
@@ -13289,59 +13554,90 @@ async function writeCredentials(credentials) {
|
|
|
13289
13554
|
});
|
|
13290
13555
|
await rename(temporary, target);
|
|
13291
13556
|
}
|
|
13292
|
-
/** Add or replace one site's credential. */
|
|
13293
13557
|
/**
|
|
13294
|
-
*
|
|
13295
|
-
*
|
|
13296
|
-
* Keyed by SITE + ACCOUNT.
|
|
13558
|
+
* Whether two records are the same account.
|
|
13297
13559
|
*
|
|
13298
|
-
*
|
|
13299
|
-
* the
|
|
13300
|
-
*
|
|
13301
|
-
|
|
13560
|
+
* A rotation arrives as a new access token, so identity cannot be the token. The vendor account
|
|
13561
|
+
* id and the refresh token are both stable across that; a repeated sign-in of the same person
|
|
13562
|
+
* keeps the original row and its id.
|
|
13563
|
+
*/
|
|
13564
|
+
function sameAccount(entry, incoming) {
|
|
13565
|
+
if (entry.site !== incoming.site) return false;
|
|
13566
|
+
if (incoming.id !== void 0 && entry.id === incoming.id) return true;
|
|
13567
|
+
if (incoming.accessToken === "") return entry.accessToken === "";
|
|
13568
|
+
if (incoming.accountId !== void 0 && entry.accountId === incoming.accountId) return true;
|
|
13569
|
+
if (incoming.refreshToken !== void 0 && entry.refreshToken === incoming.refreshToken) return true;
|
|
13570
|
+
return entry.accessToken === incoming.accessToken;
|
|
13571
|
+
}
|
|
13572
|
+
/**
|
|
13573
|
+
* Add or replace one account.
|
|
13302
13574
|
*
|
|
13303
|
-
*
|
|
13304
|
-
* leave a row per
|
|
13575
|
+
* Returns the stored record, so the caller can report an id the card can delete later. An
|
|
13576
|
+
* unfinished sign-in (empty token) is matched by site, so retrying does not leave a row per
|
|
13577
|
+
* attempt.
|
|
13305
13578
|
*/
|
|
13306
13579
|
async function saveAutoclawCredential(credential) {
|
|
13307
13580
|
return await withStoreLock(autoclawAccountsPath(), async () => {
|
|
13308
13581
|
const existing = await readAutoclawCredentials();
|
|
13309
|
-
const
|
|
13310
|
-
const
|
|
13311
|
-
|
|
13582
|
+
const matched = existing.find((entry) => sameAccount(entry, credential));
|
|
13583
|
+
const stored = {
|
|
13584
|
+
...credential,
|
|
13585
|
+
id: credential.id ?? matched?.id ?? autoclawAccountId(credential)
|
|
13586
|
+
};
|
|
13587
|
+
const next = existing.filter((entry) => entry !== matched);
|
|
13588
|
+
next.push(stored);
|
|
13312
13589
|
await writeCredentials(next);
|
|
13590
|
+
return stored;
|
|
13313
13591
|
});
|
|
13314
13592
|
}
|
|
13315
|
-
/**
|
|
13316
|
-
async function removeAutoclawCredential(site) {
|
|
13317
|
-
await writeCredentials((await readAutoclawCredentials()).filter((entry) => entry.site !== site));
|
|
13318
|
-
}
|
|
13319
|
-
/** All credentials for one site (issue #223: same-site multi-account). */
|
|
13593
|
+
/** Every account of one site, in the order they were added. */
|
|
13320
13594
|
async function autoclawCredentialsFor(site) {
|
|
13321
13595
|
return (await readAutoclawCredentials()).filter((entry) => entry.site === site);
|
|
13322
13596
|
}
|
|
13323
13597
|
/**
|
|
13324
|
-
*
|
|
13598
|
+
* One account of a site.
|
|
13325
13599
|
*
|
|
13326
|
-
*
|
|
13327
|
-
*
|
|
13328
|
-
* 全过期则挑到期最晚的(交给 adapter 尝试刷新);没有 expiresAt 视为无限远,
|
|
13329
|
-
* 最优先 —— 未知死线不该被当成已过期。
|
|
13600
|
+
* With an `id` this is that exact account; without one it is the first, which is what every
|
|
13601
|
+
* caller that predates multiple accounts expects.
|
|
13330
13602
|
*/
|
|
13331
|
-
async function autoclawCredentialFor(site) {
|
|
13332
|
-
const
|
|
13333
|
-
if (
|
|
13334
|
-
|
|
13335
|
-
|
|
13336
|
-
|
|
13337
|
-
|
|
13338
|
-
|
|
13339
|
-
|
|
13340
|
-
|
|
13603
|
+
async function autoclawCredentialFor(site, id) {
|
|
13604
|
+
const all = await autoclawCredentialsFor(site);
|
|
13605
|
+
if (id === void 0) return all[0];
|
|
13606
|
+
return all.find((entry) => entry.id === id);
|
|
13607
|
+
}
|
|
13608
|
+
/** Whether the vendor's own expiry says this credential is still good. */
|
|
13609
|
+
function usable(credential, now) {
|
|
13610
|
+
return credential.expiresAt === void 0 || credential.expiresAt > now;
|
|
13611
|
+
}
|
|
13612
|
+
/**
|
|
13613
|
+
* The account a lane should use: a live one if the site has one, otherwise the first.
|
|
13614
|
+
*
|
|
13615
|
+
* With two accounts on a site, the first may be the expired one. Falling through to a live
|
|
13616
|
+
* sibling is the difference between a working lane and an auth error that looks like the whole
|
|
13617
|
+
* site is broken.
|
|
13618
|
+
*/
|
|
13619
|
+
async function autoclawUsableCredentialFor(site, now = Date.now()) {
|
|
13620
|
+
const all = await autoclawCredentialsFor(site);
|
|
13621
|
+
return all.find((entry) => usable(entry, now)) ?? all[0];
|
|
13341
13622
|
}
|
|
13342
13623
|
/**
|
|
13343
|
-
*
|
|
13344
|
-
*
|
|
13624
|
+
* Forget one account, or every account of a site.
|
|
13625
|
+
*
|
|
13626
|
+
* Without an `id` this keeps the old meaning — signing out of the card removes the site — so a
|
|
13627
|
+
* client that has not been told about ids cannot leave a stale row behind. With an `id` only that
|
|
13628
|
+
* account goes, and its sibling stays signed in.
|
|
13629
|
+
*/
|
|
13630
|
+
async function removeAutoclawCredential(site, id) {
|
|
13631
|
+
await withStoreLock(autoclawAccountsPath(), async () => {
|
|
13632
|
+
const existing = await readAutoclawCredentials();
|
|
13633
|
+
const next = id === void 0 ? existing.filter((entry) => entry.site !== site) : existing.filter((entry) => !(entry.site === site && entry.id === id));
|
|
13634
|
+
if (next.length === existing.length) return;
|
|
13635
|
+
await writeCredentials(next);
|
|
13636
|
+
});
|
|
13637
|
+
}
|
|
13638
|
+
/**
|
|
13639
|
+
* Replace one credential identified by site + old access token.
|
|
13640
|
+
* Kept from main 3.0.8: the host adapter refreshes through this writer.
|
|
13345
13641
|
*/
|
|
13346
13642
|
async function updateAutoclawCredential(site, oldAccessToken, next) {
|
|
13347
13643
|
await withStoreLock(autoclawAccountsPath(), async () => {
|
|
@@ -13653,6 +13949,15 @@ async function readBody$2(req) {
|
|
|
13653
13949
|
function messageOf(error) {
|
|
13654
13950
|
return error instanceof Error ? error.message : String(error);
|
|
13655
13951
|
}
|
|
13952
|
+
/** The account a DELETE names, when the caller knows about account ids. */
|
|
13953
|
+
function accountIdOf(req) {
|
|
13954
|
+
try {
|
|
13955
|
+
const id = new URL(req.url ?? "/", "http://localhost").searchParams.get("id");
|
|
13956
|
+
return id === null || id === "" ? void 0 : id;
|
|
13957
|
+
} catch {
|
|
13958
|
+
return;
|
|
13959
|
+
}
|
|
13960
|
+
}
|
|
13656
13961
|
/** Mount one site's two routes. */
|
|
13657
13962
|
function mountSite$1(ctx, site, paths, deps) {
|
|
13658
13963
|
const trust = (req) => originTrusted(req, deps.trustedHosts?.() ?? []);
|
|
@@ -13663,13 +13968,13 @@ function mountSite$1(ctx, site, paths, deps) {
|
|
|
13663
13968
|
if (!trust(req)) return json$2(res, 403, { error: "origin-not-trusted" });
|
|
13664
13969
|
try {
|
|
13665
13970
|
if (req.method === "DELETE") {
|
|
13666
|
-
await removeAutoclawCredential(site.id);
|
|
13971
|
+
await removeAutoclawCredential(site.id, accountIdOf(req));
|
|
13667
13972
|
deps.adopted();
|
|
13668
|
-
const
|
|
13973
|
+
const remaining = await autoclawCredentialsFor(site.id);
|
|
13669
13974
|
return json$2(res, 200, {
|
|
13670
13975
|
ok: true,
|
|
13671
|
-
signedIn:
|
|
13672
|
-
accounts:
|
|
13976
|
+
signedIn: remaining.length > 0,
|
|
13977
|
+
accounts: remaining.map(strip)
|
|
13673
13978
|
});
|
|
13674
13979
|
}
|
|
13675
13980
|
if (req.method === "POST") {
|
|
@@ -13689,12 +13994,13 @@ function mountSite$1(ctx, site, paths, deps) {
|
|
|
13689
13994
|
ok: false,
|
|
13690
13995
|
reason: `凭据验证失败:${catalog.error}`
|
|
13691
13996
|
});
|
|
13692
|
-
await saveAutoclawCredential(credential);
|
|
13997
|
+
const stored = await saveAutoclawCredential(credential);
|
|
13693
13998
|
deps.adopted();
|
|
13694
13999
|
return json$2(res, 200, {
|
|
13695
14000
|
ok: true,
|
|
13696
14001
|
signedIn: true,
|
|
13697
14002
|
modelCount: catalog.models.length,
|
|
14003
|
+
account: strip(stored),
|
|
13698
14004
|
models: catalog.models.map((model) => ({
|
|
13699
14005
|
id: model.id,
|
|
13700
14006
|
name: model.name,
|
|
@@ -13703,13 +14009,11 @@ function mountSite$1(ctx, site, paths, deps) {
|
|
|
13703
14009
|
});
|
|
13704
14010
|
}
|
|
13705
14011
|
if (req.method !== "GET") return json$2(res, 405, { error: "method not allowed" });
|
|
13706
|
-
const
|
|
13707
|
-
const expired = credential !== void 0 && credential.expiresAt !== void 0 && Date.now() > credential.expiresAt;
|
|
14012
|
+
const accounts = await autoclawCredentialsFor(site.id);
|
|
13708
14013
|
return json$2(res, 200, {
|
|
13709
14014
|
ok: true,
|
|
13710
|
-
signedIn:
|
|
13711
|
-
|
|
13712
|
-
accounts: credential === void 0 ? [] : [strip(credential)]
|
|
14015
|
+
signedIn: accounts.length > 0,
|
|
14016
|
+
accounts: accounts.map(strip)
|
|
13713
14017
|
});
|
|
13714
14018
|
} catch (error) {
|
|
13715
14019
|
return json$2(res, 500, { error: messageOf(error) });
|
|
@@ -13764,7 +14068,7 @@ function mountSite$1(ctx, site, paths, deps) {
|
|
|
13764
14068
|
if (!trust(req)) return json$2(res, 403, { error: "origin-not-trusted" });
|
|
13765
14069
|
if (req.method !== "POST") return json$2(res, 405, { error: "method not allowed" });
|
|
13766
14070
|
try {
|
|
13767
|
-
const previous = await autoclawCredentialFor(site);
|
|
14071
|
+
const previous = await autoclawCredentialFor(site.id);
|
|
13768
14072
|
const result = await autoclawBrowserSignIn({
|
|
13769
14073
|
site,
|
|
13770
14074
|
...previous === void 0 ? {} : { previousToken: previous.accessToken }
|
|
@@ -13777,19 +14081,23 @@ function mountSite$1(ctx, site, paths, deps) {
|
|
|
13777
14081
|
site: site.id,
|
|
13778
14082
|
accessToken: result.accessToken,
|
|
13779
14083
|
...result.refreshToken === void 0 ? {} : { refreshToken: result.refreshToken },
|
|
13780
|
-
...result.userId === void 0 ? {} : {
|
|
14084
|
+
...result.userId === void 0 ? {} : {
|
|
14085
|
+
nickname: result.userId,
|
|
14086
|
+
accountId: result.userId
|
|
14087
|
+
}
|
|
13781
14088
|
};
|
|
13782
14089
|
const catalog = await autoclawCatalog(site, credential);
|
|
13783
14090
|
if (catalog.error !== void 0) return json$2(res, 200, {
|
|
13784
14091
|
ok: false,
|
|
13785
14092
|
reason: `凭据验证失败:${catalog.error}`
|
|
13786
14093
|
});
|
|
13787
|
-
await saveAutoclawCredential(credential);
|
|
14094
|
+
const stored = await saveAutoclawCredential(credential);
|
|
13788
14095
|
deps.adopted();
|
|
13789
14096
|
return json$2(res, 200, {
|
|
13790
14097
|
ok: true,
|
|
13791
14098
|
signedIn: true,
|
|
13792
|
-
modelCount: catalog.models.length
|
|
14099
|
+
modelCount: catalog.models.length,
|
|
14100
|
+
account: strip(stored)
|
|
13793
14101
|
});
|
|
13794
14102
|
} catch (error) {
|
|
13795
14103
|
return json$2(res, 200, {
|
|
@@ -13806,7 +14114,7 @@ function mountSite$1(ctx, site, paths, deps) {
|
|
|
13806
14114
|
if (req.method !== "GET") return json$2(res, 405, { error: "method not allowed" });
|
|
13807
14115
|
if (!trust(req)) return json$2(res, 403, { error: "origin-not-trusted" });
|
|
13808
14116
|
try {
|
|
13809
|
-
const credential = await
|
|
14117
|
+
const credential = await autoclawUsableCredentialFor(site.id);
|
|
13810
14118
|
if (credential === void 0) return json$2(res, 200, {
|
|
13811
14119
|
ok: false,
|
|
13812
14120
|
signedIn: false
|
|
@@ -13858,9 +14166,10 @@ function registerAutoclawRoutes(ctx, deps) {
|
|
|
13858
14166
|
}
|
|
13859
14167
|
/** A credential as the card may see it: no token. */
|
|
13860
14168
|
function strip(credential) {
|
|
14169
|
+
const label = credential.nickname ?? credential.accountId;
|
|
13861
14170
|
return {
|
|
13862
|
-
id: credential.site,
|
|
13863
|
-
...
|
|
14171
|
+
id: credential.id ?? credential.site,
|
|
14172
|
+
...label === void 0 ? {} : { nickname: label }
|
|
13864
14173
|
};
|
|
13865
14174
|
}
|
|
13866
14175
|
//#endregion
|
|
@@ -14096,7 +14405,7 @@ async function call(product, path, init = {}) {
|
|
|
14096
14405
|
ok: false,
|
|
14097
14406
|
status: response.status,
|
|
14098
14407
|
body: parsed,
|
|
14099
|
-
error: "
|
|
14408
|
+
error: "组织码缺失或与 API key 不配套(上游 2182)"
|
|
14100
14409
|
};
|
|
14101
14410
|
if (numericCode === 401 || numericCode === 403) return {
|
|
14102
14411
|
ok: false,
|
|
@@ -17610,43 +17919,205 @@ function isStoredAccount(value) {
|
|
|
17610
17919
|
if (typeof value !== "object" || value === null) return false;
|
|
17611
17920
|
const record = value;
|
|
17612
17921
|
const credential = record["credential"];
|
|
17613
|
-
return typeof record["id"] === "string" && typeof record["label"] === "string" && typeof credential === "object" && credential !== null && typeof credential["accessToken"] === "string" && typeof credential["refreshToken"] === "string";
|
|
17922
|
+
return typeof record["id"] === "string" && typeof record["label"] === "string" && typeof credential === "object" && credential !== null && typeof credential["accessToken"] === "string" && typeof credential["refreshToken"] === "string" && typeof credential["sourcePath"] === "string" && typeof credential["domain"] === "string" && Number.isFinite(credential["expiresAtMs"]);
|
|
17614
17923
|
}
|
|
17615
|
-
|
|
17616
|
-
* Read the durable list. Tolerant on purpose: a malformed or half-written file
|
|
17617
|
-
* yields `[]` and the pool simply rediscovers from disk, rather than a corrupt
|
|
17618
|
-
* store taking the whole pool down.
|
|
17619
|
-
*/
|
|
17620
|
-
async function loadStoredAccounts(path) {
|
|
17924
|
+
async function inspectStore(path) {
|
|
17621
17925
|
let text;
|
|
17622
17926
|
try {
|
|
17623
17927
|
text = await readFile(path, "utf8");
|
|
17928
|
+
} catch (error) {
|
|
17929
|
+
if (error.code === "ENOENT") return {
|
|
17930
|
+
accounts: [],
|
|
17931
|
+
safe: true
|
|
17932
|
+
};
|
|
17933
|
+
throw error;
|
|
17934
|
+
}
|
|
17935
|
+
try {
|
|
17936
|
+
const document = JSON.parse(text);
|
|
17937
|
+
if (document?.version !== 1 || !Array.isArray(document.accounts)) return {
|
|
17938
|
+
text,
|
|
17939
|
+
accounts: [],
|
|
17940
|
+
safe: false
|
|
17941
|
+
};
|
|
17942
|
+
const accounts = [];
|
|
17943
|
+
const seen = /* @__PURE__ */ new Set();
|
|
17944
|
+
let safe = true;
|
|
17945
|
+
for (const entry of document.accounts) {
|
|
17946
|
+
if (!isStoredAccount(entry) || seen.has(entry.id)) {
|
|
17947
|
+
safe = false;
|
|
17948
|
+
continue;
|
|
17949
|
+
}
|
|
17950
|
+
seen.add(entry.id);
|
|
17951
|
+
accounts.push(entry);
|
|
17952
|
+
}
|
|
17953
|
+
return {
|
|
17954
|
+
text,
|
|
17955
|
+
accounts,
|
|
17956
|
+
safe
|
|
17957
|
+
};
|
|
17624
17958
|
} catch {
|
|
17625
|
-
return
|
|
17959
|
+
return {
|
|
17960
|
+
text,
|
|
17961
|
+
accounts: [],
|
|
17962
|
+
safe: false
|
|
17963
|
+
};
|
|
17626
17964
|
}
|
|
17627
|
-
|
|
17965
|
+
}
|
|
17966
|
+
/** Keep the old primary present until replacement; every temporary has a unique name. */
|
|
17967
|
+
async function replaceSecret(path, text) {
|
|
17968
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
17628
17969
|
try {
|
|
17629
|
-
|
|
17970
|
+
await writeSecretFile(temporary, text);
|
|
17971
|
+
await rename(temporary, path);
|
|
17972
|
+
} finally {
|
|
17973
|
+
await unlink(temporary).catch((error) => {
|
|
17974
|
+
if (error.code !== "ENOENT") throw error;
|
|
17975
|
+
});
|
|
17976
|
+
}
|
|
17977
|
+
}
|
|
17978
|
+
/**
|
|
17979
|
+
* How long one transaction waits for a live holder before giving up.
|
|
17980
|
+
*
|
|
17981
|
+
* Read per call rather than at import time so a test can shorten the wait; the
|
|
17982
|
+
* default is the production 5 s.
|
|
17983
|
+
*/
|
|
17984
|
+
function storeLockWaitMs() {
|
|
17985
|
+
const raw = Number(process.env["DSH_XDPOOL_STORE_LOCK_WAIT_MS"] ?? "");
|
|
17986
|
+
return Number.isSafeInteger(raw) && raw >= 50 ? raw : 5e3;
|
|
17987
|
+
}
|
|
17988
|
+
/** Read the owner record, or undefined when the file cannot prove one. */
|
|
17989
|
+
async function readStoreLockOwner(lockPath) {
|
|
17990
|
+
try {
|
|
17991
|
+
const parsed = JSON.parse(await readFile(lockPath, "utf8"));
|
|
17992
|
+
if (typeof parsed.pid !== "number" || !Number.isSafeInteger(parsed.pid) || parsed.pid <= 0) return void 0;
|
|
17993
|
+
return {
|
|
17994
|
+
pid: parsed.pid,
|
|
17995
|
+
host: typeof parsed.host === "string" ? parsed.host : "",
|
|
17996
|
+
startedAt: typeof parsed.startedAt === "string" ? parsed.startedAt : ""
|
|
17997
|
+
};
|
|
17630
17998
|
} catch {
|
|
17631
|
-
return
|
|
17999
|
+
return;
|
|
17632
18000
|
}
|
|
17633
|
-
|
|
17634
|
-
|
|
17635
|
-
|
|
17636
|
-
|
|
17637
|
-
|
|
17638
|
-
|
|
17639
|
-
|
|
17640
|
-
|
|
17641
|
-
|
|
18001
|
+
}
|
|
18002
|
+
/**
|
|
18003
|
+
* Whether the recorded owner is provably gone.
|
|
18004
|
+
*
|
|
18005
|
+
* Only a PID from THIS host can be judged; another machine's lock stays
|
|
18006
|
+
* untouchable because its PID numbers mean nothing here. A PID that still
|
|
18007
|
+
* exists counts as alive even when its holder is unrelated (EPERM), and a
|
|
18008
|
+
* reused PID can therefore only ever make us MORE conservative.
|
|
18009
|
+
*/
|
|
18010
|
+
function storeLockOwnerIsDead(owner) {
|
|
18011
|
+
if (owner.host !== hostname()) return false;
|
|
18012
|
+
try {
|
|
18013
|
+
process.kill(owner.pid, 0);
|
|
18014
|
+
return false;
|
|
18015
|
+
} catch (error) {
|
|
18016
|
+
return error.code === "ESRCH";
|
|
17642
18017
|
}
|
|
17643
|
-
return out;
|
|
17644
18018
|
}
|
|
17645
18019
|
/**
|
|
17646
|
-
*
|
|
17647
|
-
*
|
|
18020
|
+
* Serialize the entire scan/read/merge/write, including across cooperating processes.
|
|
18021
|
+
*
|
|
18022
|
+
* A lock whose recorded owner is provably dead is reclaimed once; anything else
|
|
18023
|
+
* fails closed, because a live holder must never have its write interleaved.
|
|
18024
|
+
*
|
|
18025
|
+
* This used to fail closed for EVERY orphan as well, which bricked the store:
|
|
18026
|
+
* a crash between create and release left an empty lock that the plugin then
|
|
18027
|
+
* refused to pass forever — no write, no repair, and no way out short of
|
|
18028
|
+
* deleting the file by hand (found on the real profile, 2026-12-05).
|
|
18029
|
+
*/
|
|
18030
|
+
async function withAccountStoreTransaction(path, work) {
|
|
18031
|
+
return withStoreLock(path, async () => {
|
|
18032
|
+
await ensureSecretDir(join(path, ".."));
|
|
18033
|
+
const lockPath = `${path}.lock`;
|
|
18034
|
+
const deadline = Date.now() + storeLockWaitMs();
|
|
18035
|
+
let handle;
|
|
18036
|
+
let reclaimed = false;
|
|
18037
|
+
while (handle === void 0) try {
|
|
18038
|
+
handle = await open(lockPath, "wx", 384);
|
|
18039
|
+
const owner = {
|
|
18040
|
+
pid: process.pid,
|
|
18041
|
+
host: hostname(),
|
|
18042
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
18043
|
+
};
|
|
18044
|
+
await handle.write(JSON.stringify(owner));
|
|
18045
|
+
} catch (error) {
|
|
18046
|
+
if (handle !== void 0) {
|
|
18047
|
+
await handle.close().catch(() => void 0);
|
|
18048
|
+
await unlink(lockPath).catch(() => void 0);
|
|
18049
|
+
handle = void 0;
|
|
18050
|
+
}
|
|
18051
|
+
if (error.code !== "EEXIST") throw error;
|
|
18052
|
+
if (Date.now() >= deadline) {
|
|
18053
|
+
if (!reclaimed) {
|
|
18054
|
+
const owner = await readStoreLockOwner(lockPath);
|
|
18055
|
+
if (owner !== void 0 && storeLockOwnerIsDead(owner)) {
|
|
18056
|
+
await unlink(lockPath).catch(() => void 0);
|
|
18057
|
+
reclaimed = true;
|
|
18058
|
+
continue;
|
|
18059
|
+
}
|
|
18060
|
+
throw new Error(`Account store is locked by ${owner === void 0 ? "an unidentifiable owner (no readable owner record)" : `pid ${owner.pid} on ${owner.host} since ${owner.startedAt}`}; lock file: ${lockPath}. If that process is gone, remove the file or run the plugin's "unlock" command.`);
|
|
18061
|
+
}
|
|
18062
|
+
throw new Error(`Account store stayed locked after reclaiming a dead owner; lock file: ${lockPath}`);
|
|
18063
|
+
}
|
|
18064
|
+
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
18065
|
+
}
|
|
18066
|
+
try {
|
|
18067
|
+
return await work();
|
|
18068
|
+
} finally {
|
|
18069
|
+
await handle.close();
|
|
18070
|
+
await unlink(lockPath);
|
|
18071
|
+
}
|
|
18072
|
+
});
|
|
18073
|
+
}
|
|
18074
|
+
/**
|
|
18075
|
+
* Read valid records with diagnostics. A damaged primary is never rewritten;
|
|
18076
|
+
* a validated previous generation can supply missing records in memory.
|
|
18077
|
+
*/
|
|
18078
|
+
async function loadStoredAccounts(path, warn = (message) => process.emitWarning(message)) {
|
|
18079
|
+
const primary = await inspectStore(path);
|
|
18080
|
+
if (primary.safe) return primary.accounts;
|
|
18081
|
+
warn("Account store has invalid entries or structure; preserving the original and blocking replacement");
|
|
18082
|
+
const backup = await inspectStore(`${path}.bak`);
|
|
18083
|
+
if (!backup.safe) throw new Error("Account store and backup are invalid; manual recovery required");
|
|
18084
|
+
const merged = new Map(primary.accounts.map((account) => [account.id, account]));
|
|
18085
|
+
for (const account of backup.accounts) if (!merged.has(account.id)) merged.set(account.id, account);
|
|
18086
|
+
return [...merged.values()];
|
|
18087
|
+
}
|
|
18088
|
+
/**
|
|
18089
|
+
* Back up the validated primary before replacing it with a unique temporary.
|
|
18090
|
+
* The caller must hold the transaction over its entire read/merge/write cycle.
|
|
17648
18091
|
*/
|
|
17649
18092
|
async function saveStoredAccounts(path, accounts) {
|
|
18093
|
+
const previous = await inspectStore(path);
|
|
18094
|
+
/**
|
|
18095
|
+
* An invalid primary is quarantined rather than written over.
|
|
18096
|
+
*
|
|
18097
|
+
* Refusing outright left the store permanently unwritable: the pool kept
|
|
18098
|
+
* serving from the backup-derived list in memory, but every new sign-in was
|
|
18099
|
+
* lost on the next restart and nothing ever repaired the file. Moving the bad
|
|
18100
|
+
* generation aside keeps it for diagnosis (it is never deleted) while letting
|
|
18101
|
+
* a validated backup become the new baseline.
|
|
18102
|
+
*
|
|
18103
|
+
* The damaged text deliberately does NOT become the new `.bak`: that slot is
|
|
18104
|
+
* the last known-good generation, and overwriting it with garbage would turn
|
|
18105
|
+
* one corrupt file into two. When even the backup is unusable, nothing is
|
|
18106
|
+
* touched and the caller keeps the manual-recovery error.
|
|
18107
|
+
*/
|
|
18108
|
+
let quarantined;
|
|
18109
|
+
if (!previous.safe) {
|
|
18110
|
+
const backup = await inspectStore(`${path}.bak`);
|
|
18111
|
+
/**
|
|
18112
|
+
* Rebuilding needs a backup that actually holds something.
|
|
18113
|
+
*
|
|
18114
|
+
* An absent or empty backup would turn "repair" into "replace the user's list with whatever
|
|
18115
|
+
* is in memory", and a damaged file may still be hand-recoverable. Those cases keep refusing.
|
|
18116
|
+
*/
|
|
18117
|
+
if (backup.text === void 0 || !backup.safe || backup.accounts.length === 0) throw new Error("Refusing to overwrite an invalid account store; original preserved for recovery");
|
|
18118
|
+
quarantined = `${path}.corrupt-${(/* @__PURE__ */ new Date()).toISOString().replaceAll(":", "-").replaceAll(".", "-")}`;
|
|
18119
|
+
await rename(path, quarantined);
|
|
18120
|
+
}
|
|
17650
18121
|
const stored = accounts.map((account) => ({
|
|
17651
18122
|
id: account.id,
|
|
17652
18123
|
label: account.label,
|
|
@@ -17658,9 +18129,9 @@ async function saveStoredAccounts(path, accounts) {
|
|
|
17658
18129
|
accounts: stored
|
|
17659
18130
|
}, null, 2) + "\n";
|
|
17660
18131
|
await ensureSecretDir(join(path, ".."));
|
|
17661
|
-
|
|
17662
|
-
await
|
|
17663
|
-
|
|
18132
|
+
if (quarantined === void 0 && previous.text !== void 0) await replaceSecret(`${path}.bak`, previous.text);
|
|
18133
|
+
await replaceSecret(path, document);
|
|
18134
|
+
if (quarantined !== void 0) process.emitWarning(`dsh-xdpool: account store was invalid and has been preserved at ${quarantined}; the list was rebuilt from the last good backup`);
|
|
17664
18135
|
}
|
|
17665
18136
|
//#endregion
|
|
17666
18137
|
//#region src/at-rest.ts
|
|
@@ -18497,9 +18968,9 @@ function isChannelPackSource(path) {
|
|
|
18497
18968
|
* cleared. The live file stays on top: that is the session the running desktop
|
|
18498
18969
|
* app is genuinely using right now, and it is newer than anything we mint.
|
|
18499
18970
|
*/
|
|
18500
|
-
function compareFreshness(a, b) {
|
|
18971
|
+
function compareFreshness(a, b, liveCredentials) {
|
|
18501
18972
|
const rankOf = (credential) => {
|
|
18502
|
-
if (
|
|
18973
|
+
if (liveCredentials.has(credential)) return 2;
|
|
18503
18974
|
if (isChannelPackSource(credential.sourcePath)) return 1;
|
|
18504
18975
|
return 0;
|
|
18505
18976
|
};
|
|
@@ -18512,8 +18983,8 @@ function compareFreshness(a, b) {
|
|
|
18512
18983
|
return b.expiresAtMs - a.expiresAtMs;
|
|
18513
18984
|
}
|
|
18514
18985
|
/** True when `candidate` should replace `incumbent` for the same account. */
|
|
18515
|
-
function isFresher(candidate, incumbent) {
|
|
18516
|
-
return compareFreshness(candidate, incumbent) < 0;
|
|
18986
|
+
function isFresher(candidate, incumbent, liveCredentials) {
|
|
18987
|
+
return compareFreshness(candidate, incumbent, liveCredentials) < 0;
|
|
18517
18988
|
}
|
|
18518
18989
|
function workbuddyAccountId(credential) {
|
|
18519
18990
|
const stable = credential.uin ?? credential.uid ?? credential.nickname ?? "unknown";
|
|
@@ -18988,6 +19459,8 @@ var WorkBuddyAccountPool = class {
|
|
|
18988
19459
|
*/
|
|
18989
19460
|
lastUsedAt = /* @__PURE__ */ new Map();
|
|
18990
19461
|
refreshInflight = /* @__PURE__ */ new Map();
|
|
19462
|
+
/** Three-way reconciliation anchor: only advanced after a successful store write. */
|
|
19463
|
+
persistedCredentials = /* @__PURE__ */ new Map();
|
|
18991
19464
|
/**
|
|
18992
19465
|
* Whether `scan()` seeds from and writes back the durable account store.
|
|
18993
19466
|
*
|
|
@@ -19127,6 +19600,10 @@ var WorkBuddyAccountPool = class {
|
|
|
19127
19600
|
}
|
|
19128
19601
|
/** Rescan the auth directories and merge newly discovered accounts. */
|
|
19129
19602
|
async scan() {
|
|
19603
|
+
if (this.persistent) return withAccountStoreTransaction(this.storePath, () => this.scanTransaction());
|
|
19604
|
+
return this.scanTransaction();
|
|
19605
|
+
}
|
|
19606
|
+
async scanTransaction() {
|
|
19130
19607
|
const found = [];
|
|
19131
19608
|
const skipped = [];
|
|
19132
19609
|
/**
|
|
@@ -19211,13 +19688,36 @@ var WorkBuddyAccountPool = class {
|
|
|
19211
19688
|
} catch (error) {
|
|
19212
19689
|
this.logger?.warn?.("dsh-xdpool: could not read channel-pack accounts; desktop accounts only", error);
|
|
19213
19690
|
}
|
|
19691
|
+
const liveCredentials = new Set(found.filter((credential) => isLiveAuthFile(credential.sourcePath)));
|
|
19214
19692
|
const byId = /* @__PURE__ */ new Map();
|
|
19215
19693
|
for (const account of this.accounts) byId.set(account.id, account);
|
|
19216
19694
|
if (this.persistent) {
|
|
19217
|
-
const stored = await loadStoredAccounts(this.storePath)
|
|
19695
|
+
const stored = await loadStoredAccounts(this.storePath, (message) => {
|
|
19696
|
+
if (this.logger?.warn !== void 0) this.logger.warn(message);
|
|
19697
|
+
else process.emitWarning(message);
|
|
19698
|
+
});
|
|
19218
19699
|
for (const entry of stored) {
|
|
19219
19700
|
if (this.ignoredIds.size > 0 && this.ignoredIds.has(entry.id)) continue;
|
|
19220
|
-
|
|
19701
|
+
const existing = byId.get(entry.id);
|
|
19702
|
+
if (existing !== void 0) {
|
|
19703
|
+
const anchor = this.persistedCredentials.get(entry.id);
|
|
19704
|
+
const memory = JSON.stringify(existing.credential);
|
|
19705
|
+
const disk = JSON.stringify(entry.credential);
|
|
19706
|
+
if (anchor !== void 0 && disk !== anchor && memory !== disk) {
|
|
19707
|
+
if (memory !== anchor) throw new Error(`Account store conflict for ${entry.id}; both versions preserved, no write performed`);
|
|
19708
|
+
byId.set(entry.id, {
|
|
19709
|
+
...existing,
|
|
19710
|
+
credential: entry.credential,
|
|
19711
|
+
label: entry.label,
|
|
19712
|
+
credentialDeadUntilMs: 0
|
|
19713
|
+
});
|
|
19714
|
+
} else if (anchor === void 0 && isFresher(entry.credential, existing.credential, liveCredentials)) byId.set(entry.id, {
|
|
19715
|
+
...existing,
|
|
19716
|
+
credential: entry.credential,
|
|
19717
|
+
label: entry.label
|
|
19718
|
+
});
|
|
19719
|
+
continue;
|
|
19720
|
+
}
|
|
19221
19721
|
byId.set(entry.id, {
|
|
19222
19722
|
id: entry.id,
|
|
19223
19723
|
label: entry.label,
|
|
@@ -19251,7 +19751,7 @@ var WorkBuddyAccountPool = class {
|
|
|
19251
19751
|
});
|
|
19252
19752
|
continue;
|
|
19253
19753
|
}
|
|
19254
|
-
if (isFresher(credential, existing.credential)) {
|
|
19754
|
+
if (isFresher(credential, existing.credential, liveCredentials)) {
|
|
19255
19755
|
const signedInAgain = credential.refreshToken !== existing.credential.refreshToken || credential.accessToken !== existing.credential.accessToken;
|
|
19256
19756
|
byId.set(id, {
|
|
19257
19757
|
...existing,
|
|
@@ -19262,7 +19762,7 @@ var WorkBuddyAccountPool = class {
|
|
|
19262
19762
|
}
|
|
19263
19763
|
}
|
|
19264
19764
|
const ordered = [...byId.values()];
|
|
19265
|
-
ordered.sort((a, b) => compareFreshness(a.credential, b.credential));
|
|
19765
|
+
ordered.sort((a, b) => compareFreshness(a.credential, b.credential, liveCredentials));
|
|
19266
19766
|
this.accounts = ordered;
|
|
19267
19767
|
this.lastScanAtMs = Date.now();
|
|
19268
19768
|
this.warmExpiryReadings();
|
|
@@ -19270,6 +19770,7 @@ var WorkBuddyAccountPool = class {
|
|
|
19270
19770
|
const leaving = this.lastPersistedIds === void 0 ? [] : [...this.lastPersistedIds].filter((id) => !this.accounts.some((account) => account.id === id));
|
|
19271
19771
|
if (leaving.length > 0) this.logger?.warn?.(`dsh-xdpool: ${String(leaving.length)} account(s) are no longer in the pool after a rescan and were removed from the store: ${leaving.join(", ")}. Their credentials are gone unless they are signed in again.`);
|
|
19272
19772
|
await saveStoredAccounts(this.storePath, this.accounts);
|
|
19773
|
+
this.persistedCredentials = new Map(this.accounts.map((account) => [account.id, JSON.stringify(account.credential)]));
|
|
19273
19774
|
this.lastPersistedIds = new Set(this.accounts.map((account) => account.id));
|
|
19274
19775
|
} catch (error) {
|
|
19275
19776
|
this.logger?.warn?.("dsh-xdpool: could not persist the account store", error);
|
|
@@ -19545,6 +20046,13 @@ var WorkBuddyAccountPool = class {
|
|
|
19545
20046
|
}
|
|
19546
20047
|
usable += 1;
|
|
19547
20048
|
}
|
|
20049
|
+
if (usable > 0) return {
|
|
20050
|
+
total: inRegion.length,
|
|
20051
|
+
cooling,
|
|
20052
|
+
disabled,
|
|
20053
|
+
dead,
|
|
20054
|
+
reason: "none"
|
|
20055
|
+
};
|
|
19548
20056
|
if (dead > 0) return {
|
|
19549
20057
|
total: inRegion.length,
|
|
19550
20058
|
cooling,
|
|
@@ -20794,18 +21302,8 @@ var WorkBuddyCatalog = class {
|
|
|
20794
21302
|
* is recoverable in one click whereas "nothing" looks like a broken install.
|
|
20795
21303
|
*/
|
|
20796
21304
|
const allow = enabled === void 0 || enabled.length === 0 ? void 0 : new Set(enabled);
|
|
20797
|
-
/**
|
|
20798
|
-
* Same reading, and here it is the difference between "images on" and "images off".
|
|
20799
|
-
*
|
|
20800
|
-
* `enabledModelIds` degrades safely when an empty list is misread — the "everything is
|
|
20801
|
-
* enabled" recovery is one click away. `imageModelIds` does not: an empty set overrides
|
|
20802
|
-
* every `supportsImages: true` the upstream gateway reported, so a config that never chose
|
|
20803
|
-
* (the schema used to write `[]` by default) published the whole pool as text-only and
|
|
20804
|
-
* the host refused image attachments. A host that HAS saved a selection still wins — the
|
|
20805
|
-
* set is only consulted once something is actually in it.
|
|
20806
|
-
*/
|
|
20807
21305
|
const images = this.selection.imageModelIds;
|
|
20808
|
-
const imageSet = images === void 0
|
|
21306
|
+
const imageSet = images === void 0 ? void 0 : new Set(images);
|
|
20809
21307
|
const budgets = this.selection.contextBudgets;
|
|
20810
21308
|
return sortFreeFirst(this.models.filter((model) => allow === void 0 || allow.has(model.id)).map((model) => {
|
|
20811
21309
|
const next = { ...model };
|
|
@@ -20890,6 +21388,41 @@ var WorkBuddyCatalog = class {
|
|
|
20890
21388
|
for (const listener of this.listeners) listener();
|
|
20891
21389
|
}
|
|
20892
21390
|
};
|
|
21391
|
+
/** Legacy host defaults wrote []; regional selections retain [] as an explicit off switch.
|
|
21392
|
+
* Source-PR: https://github.com/XDTrees/dsh-xdpool/pull/235
|
|
21393
|
+
* Source-Commit: d3b764815f5ea80480c3413a887d78135c6de9ee (adapted to preserve explicit regional off).
|
|
21394
|
+
*/
|
|
21395
|
+
function legacyModelSelection(selection) {
|
|
21396
|
+
const { imageModelIds, ...rest } = selection;
|
|
21397
|
+
return {
|
|
21398
|
+
...rest,
|
|
21399
|
+
...imageModelIds === void 0 || imageModelIds.length === 0 ? {} : { imageModelIds }
|
|
21400
|
+
};
|
|
21401
|
+
}
|
|
21402
|
+
/**
|
|
21403
|
+
* A saved selection whose image list is empty means "no opinion".
|
|
21404
|
+
*
|
|
21405
|
+
* The card builds `imageModelIds` from its per-model ticks and writes the array
|
|
21406
|
+
* unconditionally, so "the user never touched the image switches" and "the user
|
|
21407
|
+
* turned every model's images off" both arrive as `[]`. Reading that as an
|
|
21408
|
+
* explicit all-off made one routine save (toggling a text model, renaming
|
|
21409
|
+
* something) disable image input for the whole region — which is exactly how a
|
|
21410
|
+
* pool model that the upstream marks `supportsImages: true` started being
|
|
21411
|
+
* refused with `MODEL_DOES_NOT_SUPPORT_IMAGES`.
|
|
21412
|
+
*
|
|
21413
|
+
* Empty therefore degrades to "follow the upstream flag". An explicit disable is
|
|
21414
|
+
* expressed by a NON-EMPTY list that omits the model, which the per-model tick
|
|
21415
|
+
* boxes can always produce. This is the same reading the legacy migration
|
|
21416
|
+
* already applied to the flat key (#122, #235); it now applies to a region's own
|
|
21417
|
+
* key too, because the card cannot tell the two intents apart.
|
|
21418
|
+
*/
|
|
21419
|
+
function normalizeModelSelection(selection) {
|
|
21420
|
+
const { imageModelIds, ...rest } = selection;
|
|
21421
|
+
return imageModelIds === void 0 || imageModelIds.length === 0 ? rest : {
|
|
21422
|
+
...rest,
|
|
21423
|
+
imageModelIds
|
|
21424
|
+
};
|
|
21425
|
+
}
|
|
20893
21426
|
/** Convert one upstream catalog entry into the plugin's model-info shape. */
|
|
20894
21427
|
function toModelInfo(model) {
|
|
20895
21428
|
return {
|
|
@@ -26514,7 +27047,7 @@ function flattenPlatforms(platforms) {
|
|
|
26514
27047
|
for (const raw of platform.models) {
|
|
26515
27048
|
const model = text$6(raw);
|
|
26516
27049
|
if (model === "" || isComboId(model)) continue;
|
|
26517
|
-
const id = owner === "workbuddy" ? model : `${owner}/${model}`;
|
|
27050
|
+
const id = owner === "workbuddy" || model.startsWith(`${owner}:`) ? model : `${owner}/${model}`;
|
|
26518
27051
|
if (seen.has(id)) continue;
|
|
26519
27052
|
seen.add(id);
|
|
26520
27053
|
out.push(id);
|
|
@@ -26558,7 +27091,7 @@ function splitPlatforms(models) {
|
|
|
26558
27091
|
const channelMember = channelMemberOf(id);
|
|
26559
27092
|
const parsed = channelMember === void 0 ? parseProviderModelId(id) : null;
|
|
26560
27093
|
const owner = channelMember === void 0 ? parsed === null ? POOL_OWNER : parsed.providerId : channelMember.channel;
|
|
26561
|
-
const model = channelMember === void 0 ? parsed === null ? id : parsed.modelId :
|
|
27094
|
+
const model = channelMember === void 0 ? parsed === null ? id : parsed.modelId : id;
|
|
26562
27095
|
let bucket = groups.get(owner);
|
|
26563
27096
|
if (bucket === void 0) {
|
|
26564
27097
|
bucket = [];
|
|
@@ -28296,25 +28829,6 @@ function buildAggregateCatalog(store) {
|
|
|
28296
28829
|
}
|
|
28297
28830
|
return rows;
|
|
28298
28831
|
}
|
|
28299
|
-
/**
|
|
28300
|
-
* A row set is unchanged only when it is field-for-field equal.
|
|
28301
|
-
*
|
|
28302
|
-
* Compared field by field rather than by id. An edit can keep every id and still
|
|
28303
|
-
* change what the row CLAIMS — a chain member swapped for another while the count
|
|
28304
|
-
* stays the same, a renamed chain, a window or image flag that moved — and an
|
|
28305
|
-
* id-only comparison publishes none of those: the picker and the adapter keep
|
|
28306
|
-
* serving the previous numbers until the process restarts.
|
|
28307
|
-
*/
|
|
28308
|
-
function sameRows(previous, next) {
|
|
28309
|
-
if (previous.length !== next.length) return false;
|
|
28310
|
-
return next.every((row, index) => {
|
|
28311
|
-
const before = previous[index];
|
|
28312
|
-
if (before === void 0) return false;
|
|
28313
|
-
const keys = Object.keys(row);
|
|
28314
|
-
if (keys.length !== Object.keys(before).length) return false;
|
|
28315
|
-
return keys.every((key) => JSON.stringify(row[key]) === JSON.stringify(before[key]));
|
|
28316
|
-
});
|
|
28317
|
-
}
|
|
28318
28832
|
/** One stored aggregate row set, refreshed on change, for the shim and adapter. */
|
|
28319
28833
|
var AggregateCatalog = class {
|
|
28320
28834
|
read;
|
|
@@ -28367,7 +28881,7 @@ var AggregateCatalog = class {
|
|
|
28367
28881
|
const groups = buildGroupCatalog(store.groups, sources);
|
|
28368
28882
|
const allChains = [...chains, ...groups];
|
|
28369
28883
|
pruneRotation(combosOf(store));
|
|
28370
|
-
if (
|
|
28884
|
+
if (JSON.stringify(next) === JSON.stringify(this.rows) && JSON.stringify(chains) === JSON.stringify(this.chainRows)) return this.current();
|
|
28371
28885
|
this.rows = next;
|
|
28372
28886
|
this.chainRows = allChains;
|
|
28373
28887
|
for (const listener of this.listeners) listener();
|
|
@@ -29105,12 +29619,34 @@ const POOL_NAME_BY_REGION = {
|
|
|
29105
29619
|
};
|
|
29106
29620
|
/** Provider idle ceiling while one stream read is outstanding. */
|
|
29107
29621
|
const WORKBUDDY_STREAM_IDLE_TIMEOUT_MS = 3e5;
|
|
29108
|
-
/**
|
|
29622
|
+
/**
|
|
29623
|
+
* Image-request budgets for every route this factory builds.
|
|
29624
|
+
*
|
|
29625
|
+
* `requestImageBudgetCount` is load-bearing on the 0.2.x host, not decoration.
|
|
29626
|
+
* `dsh-llm-pi-ai` derives the per-image cap with
|
|
29627
|
+
*
|
|
29628
|
+
* floor(maxRequestImageBytes / requestImageBudgetCount / 4) * 3
|
|
29629
|
+
*
|
|
29630
|
+
* so an absent count makes that NaN, and `dsh-attachment-local` then refuses the
|
|
29631
|
+
* request with `INVALID_ATTACHMENT_REF: Image request maxImageBytes must be a
|
|
29632
|
+
* positive integer.` — the image pass the capability gate, then fail at
|
|
29633
|
+
* serialization. The pinned 0.1.5 build this plugin was verified against never
|
|
29634
|
+
* read the field (#239 recorded it as absent from that line), which is why it
|
|
29635
|
+
* went missing precisely where a host upgrade made it mandatory.
|
|
29636
|
+
*
|
|
29637
|
+
* `maxImageBytes` is the same host's hard per-image cap; keeping it at the old
|
|
29638
|
+
* encoded-byte target preserves the behaviour this plugin had before the field
|
|
29639
|
+
* existed, instead of silently raising every image to the host default.
|
|
29640
|
+
*/
|
|
29109
29641
|
const REQUEST_IMAGE_BUDGETS = {
|
|
29110
29642
|
maxRequestImageBytes: 20971520,
|
|
29111
29643
|
requestImagePixelBudget: 4194304,
|
|
29644
|
+
/** 0.1.x pin: the encoded-byte target. Still spread for older hosts. */
|
|
29112
29645
|
requestImageMaxBytes: 1048576,
|
|
29113
|
-
|
|
29646
|
+
/** 0.2.x: how many images share `maxRequestImageBytes`. */
|
|
29647
|
+
requestImageBudgetCount: 8,
|
|
29648
|
+
/** 0.2.x: hard per-image byte cap. */
|
|
29649
|
+
maxImageBytes: 1048576
|
|
29114
29650
|
};
|
|
29115
29651
|
/**
|
|
29116
29652
|
* Inert pi-ai auth plane. The route authenticates only through the shim shared
|
|
@@ -31736,7 +32272,7 @@ function parseBody(body) {
|
|
|
31736
32272
|
*
|
|
31737
32273
|
* @module dsh-xdpool/version
|
|
31738
32274
|
*/
|
|
31739
|
-
const POOL_VERSION = "3.0.
|
|
32275
|
+
const POOL_VERSION = "3.0.9";
|
|
31740
32276
|
//#endregion
|
|
31741
32277
|
//#region src/update-check.ts
|
|
31742
32278
|
/**
|
|
@@ -32876,10 +33412,24 @@ var WorkBuddyScheduler = class {
|
|
|
32876
33412
|
* ignored rather than queued: pressing the button twice means hurry up, and
|
|
32877
33413
|
* the run already under way covers it.
|
|
32878
33414
|
*/
|
|
32879
|
-
startRunAll(
|
|
33415
|
+
startRunAll() {
|
|
33416
|
+
return this.startRun("all");
|
|
33417
|
+
}
|
|
33418
|
+
/**
|
|
33419
|
+
* Start one job — or the whole pass — in the background.
|
|
33420
|
+
*
|
|
33421
|
+
* The card's automation row names a job, and `force` is what a second press
|
|
33422
|
+
* within the same hour means. Both used to be dropped on the floor by the
|
|
33423
|
+
* route wiring, so "run check-in" ran every job and a repeat press inside the
|
|
33424
|
+
* hour silently did nothing. A single-job start goes through the same
|
|
33425
|
+
* in-flight guard as a full pass: overlapping passes would double-spend the
|
|
33426
|
+
* per-account delays and interleave two runs into one status document.
|
|
33427
|
+
*/
|
|
33428
|
+
startRun(kind, force = false) {
|
|
32880
33429
|
if (this.runInFlight) return false;
|
|
33430
|
+
if (!force && kind !== "all" && this.states[kind].lastFiredHour === slotKey(this.now())) return false;
|
|
32881
33431
|
this.runInFlight = true;
|
|
32882
|
-
(
|
|
33432
|
+
(kind === "all" ? this.runAll() : this.runNow(kind, force).then(() => void 0)).catch((error) => {
|
|
32883
33433
|
this.logger.warn?.("dsh-xdpool automation run failed:", error);
|
|
32884
33434
|
}).finally(() => {
|
|
32885
33435
|
this.runInFlight = false;
|
|
@@ -35197,6 +35747,28 @@ function toWebModel(model, selection) {
|
|
|
35197
35747
|
* human, and the card says so.
|
|
35198
35748
|
*/
|
|
35199
35749
|
const AUTOMATABLE_BY_OTHER_JOB = /* @__PURE__ */ new Set(["first_buddy", "chat_5"]);
|
|
35750
|
+
/**
|
|
35751
|
+
* Run `work` over `items` with at most `limit` in flight, preserving order.
|
|
35752
|
+
*
|
|
35753
|
+
* Results land at their input index, so callers that build an ordered document
|
|
35754
|
+
* do not have to re-sort: the card's account list must not reshuffle because
|
|
35755
|
+
* one account answered first.
|
|
35756
|
+
*/
|
|
35757
|
+
async function mapWithConcurrency(items, limit, work) {
|
|
35758
|
+
const results = new Array(items.length);
|
|
35759
|
+
const width = Math.max(1, Math.min(Math.trunc(limit) || 1, items.length));
|
|
35760
|
+
let cursor = 0;
|
|
35761
|
+
const workers = Array.from({ length: width }, async () => {
|
|
35762
|
+
for (;;) {
|
|
35763
|
+
const index = cursor;
|
|
35764
|
+
cursor += 1;
|
|
35765
|
+
if (index >= items.length) return;
|
|
35766
|
+
results[index] = await work(items[index], index);
|
|
35767
|
+
}
|
|
35768
|
+
});
|
|
35769
|
+
await Promise.all(workers);
|
|
35770
|
+
return results;
|
|
35771
|
+
}
|
|
35200
35772
|
async function taskBoardOf(deps, account) {
|
|
35201
35773
|
const tasks = await deps.client.listTasks(account.credential);
|
|
35202
35774
|
const rows = [];
|
|
@@ -35246,16 +35818,15 @@ async function poolWebStatus(deps, region = "cn") {
|
|
|
35246
35818
|
const pinnedId = deps.pool.preferredId();
|
|
35247
35819
|
const preferredAccountId = pinnedId === void 0 || !accounts.some((account) => account.id === pinnedId) ? void 0 : pinnedId;
|
|
35248
35820
|
const selection = deps.catalogs[region].currentSelection();
|
|
35821
|
+
const rows = new Array(accounts.length);
|
|
35249
35822
|
const now = Date.now();
|
|
35250
35823
|
/**
|
|
35251
|
-
*
|
|
35252
|
-
* 逐个 await —— 19 个账号就是 38 次串行往返,首屏实测 12.6 秒。
|
|
35824
|
+
* One account's live probes.
|
|
35253
35825
|
*
|
|
35254
|
-
*
|
|
35255
|
-
*
|
|
35256
|
-
* 用 Promise.all 收集,rows 顺序与 accounts 一致(map 保序),卡片看到的行序不变。
|
|
35826
|
+
* The two calls stay sequential inside an account: a dead sign-in must not be
|
|
35827
|
+
* probed for check-in, and the mark this iteration makes decides that.
|
|
35257
35828
|
*/
|
|
35258
|
-
const
|
|
35829
|
+
const probe = async (account, index) => {
|
|
35259
35830
|
const row = toWebAccount(account, deps.pool.isDisabled(account.id), deps.pool.preferredId() === account.id, deps.pool.creditReserveOf(account.id), deps.pool.isReserved(account.id));
|
|
35260
35831
|
const earned = deps.scheduler?.().earningsToday[account.id];
|
|
35261
35832
|
if (earned !== void 0) Object.assign(row, { automationToday: earned });
|
|
@@ -35303,8 +35874,9 @@ async function poolWebStatus(deps, region = "cn") {
|
|
|
35303
35874
|
}
|
|
35304
35875
|
}
|
|
35305
35876
|
}
|
|
35306
|
-
|
|
35307
|
-
}
|
|
35877
|
+
rows[index] = row;
|
|
35878
|
+
};
|
|
35879
|
+
await mapWithConcurrency(accounts, 4, probe);
|
|
35308
35880
|
const cooling = rows.filter((row) => row.cooling).length;
|
|
35309
35881
|
const lastServed = deps.pool.lastServedId();
|
|
35310
35882
|
const firstUsable = lastServed !== void 0 ? accounts.find((account) => account.id === lastServed) : accounts.find((account) => account.cooldownUntilMs <= now);
|
|
@@ -37137,27 +37709,6 @@ function sanitizeToolSchemas(tools) {
|
|
|
37137
37709
|
*/
|
|
37138
37710
|
/** The slice of the host's `llm` service this lane needs. */
|
|
37139
37711
|
/**
|
|
37140
|
-
* OpenAI function-calling 形状 → 宿主 LlmAdapter 期望的扁平形状(issue #234)。
|
|
37141
|
-
*
|
|
37142
|
-
* 只做形状翻译,不改语义:name / description / parameters 原样保留。
|
|
37143
|
-
* 扁平输入(已经带 name)直接透传,避免二次包装。
|
|
37144
|
-
*/
|
|
37145
|
-
function toHostTools(raw) {
|
|
37146
|
-
return raw.map((entry) => {
|
|
37147
|
-
if (entry === null || typeof entry !== "object") return entry;
|
|
37148
|
-
const record = entry;
|
|
37149
|
-
if (typeof record["name"] === "string") return record;
|
|
37150
|
-
const fn = record["function"];
|
|
37151
|
-
if (fn === null || typeof fn !== "object") return record;
|
|
37152
|
-
const spec = fn;
|
|
37153
|
-
return {
|
|
37154
|
-
name: typeof spec["name"] === "string" ? spec["name"] : record["name"],
|
|
37155
|
-
...typeof spec["description"] === "string" ? { description: spec["description"] } : {},
|
|
37156
|
-
...spec["parameters"] === void 0 ? {} : { parameters: spec["parameters"] }
|
|
37157
|
-
};
|
|
37158
|
-
});
|
|
37159
|
-
}
|
|
37160
|
-
/**
|
|
37161
37712
|
* A request this lane will not serve as written, and that is the caller's to fix.
|
|
37162
37713
|
*
|
|
37163
37714
|
* Kept as its own type so the route can answer 400 for it: a refused image and a
|
|
@@ -37311,6 +37862,37 @@ function normalizeRole(value) {
|
|
|
37311
37862
|
async function toHostMessage(message, attachments) {
|
|
37312
37863
|
const role = normalizeRole(message.role);
|
|
37313
37864
|
const content = message.content;
|
|
37865
|
+
if (role === "tool") {
|
|
37866
|
+
if (typeof message.tool_call_id !== "string" || message.tool_call_id === "") throw new ChannelRequestError("tool result requires tool_call_id");
|
|
37867
|
+
const inner = await toHostMessage({
|
|
37868
|
+
role: "user",
|
|
37869
|
+
content
|
|
37870
|
+
}, attachments);
|
|
37871
|
+
return {
|
|
37872
|
+
role: "user",
|
|
37873
|
+
content: [{
|
|
37874
|
+
type: "tool-result",
|
|
37875
|
+
toolCallId: message.tool_call_id,
|
|
37876
|
+
content: inner.content
|
|
37877
|
+
}]
|
|
37878
|
+
};
|
|
37879
|
+
}
|
|
37880
|
+
if (role === "assistant" && Array.isArray(message.tool_calls)) {
|
|
37881
|
+
const inner = await toHostMessage({
|
|
37882
|
+
role,
|
|
37883
|
+
content
|
|
37884
|
+
}, attachments);
|
|
37885
|
+
for (const call of message.tool_calls) {
|
|
37886
|
+
if (call?.type !== "function" || typeof call.id !== "string" || typeof call.function?.name !== "string" || typeof call.function.arguments !== "string") throw new ChannelRequestError("invalid assistant tool call");
|
|
37887
|
+
inner.content.push({
|
|
37888
|
+
type: "tool-call",
|
|
37889
|
+
id: call.id,
|
|
37890
|
+
name: call.function.name,
|
|
37891
|
+
arguments: call.function.arguments
|
|
37892
|
+
});
|
|
37893
|
+
}
|
|
37894
|
+
return inner;
|
|
37895
|
+
}
|
|
37314
37896
|
if (typeof content === "string") return {
|
|
37315
37897
|
role,
|
|
37316
37898
|
content: content === "" ? [] : [{
|
|
@@ -37511,19 +38093,19 @@ async function* runTurn(input) {
|
|
|
37511
38093
|
* request is refused with a path (`properties.show`) the caller cannot act on. Cutting the
|
|
37512
38094
|
* cycle here keeps the tool callable and the request sendable.
|
|
37513
38095
|
*/
|
|
37514
|
-
|
|
37515
|
-
|
|
37516
|
-
|
|
37517
|
-
|
|
37518
|
-
|
|
37519
|
-
|
|
37520
|
-
|
|
37521
|
-
|
|
37522
|
-
|
|
37523
|
-
|
|
37524
|
-
|
|
37525
|
-
|
|
37526
|
-
|
|
38096
|
+
if (body.tool_choice !== void 0 && body.tool_choice !== "auto" && body.tool_choice !== "none") throw new ChannelRequestError("this host cannot represent forced tool_choice; use auto or none");
|
|
38097
|
+
if (body.tool_choice !== "none" && Array.isArray(body.tools) && body.tools.length > 0) options.tools = sanitizeToolSchemas(body.tools.map((tool) => {
|
|
38098
|
+
const fn = tool.type === "function" ? tool.function : tool;
|
|
38099
|
+
if (typeof fn?.name !== "string" || fn.name === "") throw new ChannelRequestError("invalid function tool name");
|
|
38100
|
+
return {
|
|
38101
|
+
name: fn.name,
|
|
38102
|
+
description: typeof fn.description === "string" ? fn.description : "",
|
|
38103
|
+
parameters: fn.parameters ?? {
|
|
38104
|
+
type: "object",
|
|
38105
|
+
properties: {}
|
|
38106
|
+
}
|
|
38107
|
+
};
|
|
38108
|
+
}));
|
|
37527
38109
|
for await (const chunk of llm.stream(options)) {
|
|
37528
38110
|
const event = toChannelEvent(chunk);
|
|
37529
38111
|
if (event !== void 0) yield event;
|
|
@@ -42043,7 +42625,7 @@ async function sessionRowsOnDisk(path) {
|
|
|
42043
42625
|
continue;
|
|
42044
42626
|
}
|
|
42045
42627
|
const session = row["session"];
|
|
42046
|
-
if (isRecord(isRecord(session) ? session["cookies"] : void 0)) ids.add(accountIdOf(session));
|
|
42628
|
+
if (isRecord(isRecord(session) ? session["cookies"] : void 0)) ids.add(accountIdOf$1(session));
|
|
42047
42629
|
}
|
|
42048
42630
|
return {
|
|
42049
42631
|
rows: parsed,
|
|
@@ -42660,16 +43242,7 @@ const Config = z.object({
|
|
|
42660
43242
|
disabledAccountIds: asVolatile(z.array(z.string()).default([]).description("Account ids excluded from the pool (empty = every discovered account participates)")),
|
|
42661
43243
|
creditReserves: asVolatile(z.dict(z.number().step(1).min(0)).default({}).description("Per-account credit floor: stop using an account once its balance reaches this value")),
|
|
42662
43244
|
enabledModelIds: asVolatile(z.array(z.string()).default([]).description("Legacy shared model-id list; used by a region that has no per-region selection yet")),
|
|
42663
|
-
|
|
42664
|
-
* No `.default([])` on purpose.
|
|
42665
|
-
*
|
|
42666
|
-
* A default makes "never chose" and "chose none" the same JSON, and the two are read very
|
|
42667
|
-
* differently downstream: absent means "follow the upstream image flag", while an empty list
|
|
42668
|
-
* overrides it to "no model accepts images". With the default, every fresh install and every
|
|
42669
|
-
* document written before this key existed started out text-only — the host then refused
|
|
42670
|
-
* every attachment for models the gateway serves with `supportsImages: true`.
|
|
42671
|
-
*/
|
|
42672
|
-
imageModelIds: asVolatile(z.array(z.string()).description("Legacy shared image-id list; used by a region that has no per-region selection yet (absent = follow the upstream image flag)")),
|
|
43245
|
+
imageModelIds: asVolatile(z.array(z.string()).description("Legacy shared image-id list; used by a region that has no per-region selection yet")),
|
|
42673
43246
|
contextBudgets: asVolatile(z.dict(z.number().step(1).min(1)).default({}).description("Legacy shared context budgets; used by a region with no per-region selection yet")),
|
|
42674
43247
|
modelSelectionCn: asVolatile(modelSelectionSchema.description("Model selection for the domestic gateway")),
|
|
42675
43248
|
modelSelectionGlobal: asVolatile(modelSelectionSchema.description("Model selection for the international gateway")),
|
|
@@ -42890,30 +43463,16 @@ function apply(ctx, config = {}) {
|
|
|
42890
43463
|
...creditReserves === void 0 ? {} : { creditReserves },
|
|
42891
43464
|
...automation?.exhaustCooldownMs === void 0 ? {} : { exhaustCooldownMs: automation.exhaustCooldownMs }
|
|
42892
43465
|
});
|
|
42893
|
-
|
|
42894
|
-
|
|
42895
|
-
|
|
42896
|
-
|
|
42897
|
-
|
|
42898
|
-
* through then told `fallbackSelection` "the user did choose — they chose nothing", which
|
|
42899
|
-
* is the exact inversion the `enabledModelIds` guard in `visible()` was written for, and
|
|
42900
|
-
* it cost image input: an empty `imageModelIds` means "no model accepts images", so every
|
|
42901
|
-
* model the upstream gateway reports as `supportsImages: true` was published as text-only
|
|
42902
|
-
* and the host refused the attachment (the "can't upload images with xdpool" report).
|
|
42903
|
-
*
|
|
42904
|
-
* Dropping the empties here keeps ONE reading of "empty = absent" for the whole fallback
|
|
42905
|
-
* chain instead of a special case per key.
|
|
42906
|
-
*/
|
|
42907
|
-
const legacySelection = {
|
|
42908
|
-
...enabledModelIds === void 0 || enabledModelIds.length === 0 ? {} : { enabledModelIds },
|
|
42909
|
-
...imageModelIds === void 0 || imageModelIds.length === 0 ? {} : { imageModelIds },
|
|
42910
|
-
...contextBudgets === void 0 || Object.keys(contextBudgets).length === 0 ? {} : { contextBudgets }
|
|
42911
|
-
};
|
|
43466
|
+
const legacySelection = legacyModelSelection({
|
|
43467
|
+
...enabledModelIds === void 0 ? {} : { enabledModelIds },
|
|
43468
|
+
...imageModelIds === void 0 ? {} : { imageModelIds },
|
|
43469
|
+
...contextBudgets === void 0 ? {} : { contextBudgets }
|
|
43470
|
+
});
|
|
42912
43471
|
const fallbackSelection = (region) => {
|
|
42913
43472
|
return legacySelection.enabledModelIds === void 0 && legacySelection.imageModelIds === void 0 && legacySelection.contextBudgets === void 0 ? core.catalogs[region].currentSelection() : legacySelection;
|
|
42914
43473
|
};
|
|
42915
|
-
core.catalogs.cn.applySelection(modelSelectionCn ?? fallbackSelection("cn"));
|
|
42916
|
-
core.catalogs.global.applySelection(modelSelectionGlobal ?? fallbackSelection("global"));
|
|
43474
|
+
core.catalogs.cn.applySelection(normalizeModelSelection(modelSelectionCn ?? fallbackSelection("cn")));
|
|
43475
|
+
core.catalogs.global.applySelection(normalizeModelSelection(modelSelectionGlobal ?? fallbackSelection("global")));
|
|
42917
43476
|
applyLaneSelection(laneSelectionFrom(core.catalogs.cn.currentSelection().enabledModelIds, core.catalogs.global.currentSelection().enabledModelIds));
|
|
42918
43477
|
core.scheduler.applyConfig(automationOptions(automation));
|
|
42919
43478
|
/**
|
|
@@ -43152,6 +43711,7 @@ function apply(ctx, config = {}) {
|
|
|
43152
43711
|
attachments: () => ctx.get("attachments"),
|
|
43153
43712
|
logger: ctx.logger
|
|
43154
43713
|
});
|
|
43714
|
+
channelLaneForChains = channelLane;
|
|
43155
43715
|
/**
|
|
43156
43716
|
* The gateway allowlist, re-read per request.
|
|
43157
43717
|
*
|
|
@@ -43773,14 +44333,7 @@ function apply(ctx, config = {}) {
|
|
|
43773
44333
|
usage: () => usageRecorder.summary(),
|
|
43774
44334
|
shim: () => shimInfo("cn"),
|
|
43775
44335
|
scheduler: () => core.scheduler.status(),
|
|
43776
|
-
|
|
43777
|
-
* issue #222:把 job 和 force 真正转发给 scheduler。
|
|
43778
|
-
*
|
|
43779
|
-
* 以前两个参数都被丢弃,点任何任务都全量跑;现在 `all` 走原来的全量入口,
|
|
43780
|
-
* 单个 job 走 startRunNow(带 force 跳过同小时节流)。返回值仍是 boolean,
|
|
43781
|
-
* 由路由层把 false 翻成「已在运行」的理由。
|
|
43782
|
-
*/
|
|
43783
|
-
runAutomation: (job, force) => job === "all" ? core.scheduler.startRunAll() : core.scheduler.startRunNow(job, force),
|
|
44336
|
+
runAutomation: (job, force) => core.scheduler.startRun(job === "all" || !isAutomationJobKind(job) ? "all" : job, force),
|
|
43784
44337
|
saveSelection: async (region, selection) => {
|
|
43785
44338
|
/**
|
|
43786
44339
|
* Write the WHOLE region selection, never just the fields the request named.
|
|
@@ -44258,11 +44811,11 @@ function apply(ctx, config = {}) {
|
|
|
44258
44811
|
/** AutoClaw, both sites: a signed gateway with a five-bucket wallet. */
|
|
44259
44812
|
releaseAdapterAutoclaw = ctx.llm.registerAdapter([AUTOCLAW_INTL.id], new AutoclawHostAdapter({
|
|
44260
44813
|
site: AUTOCLAW_INTL,
|
|
44261
|
-
credential: () =>
|
|
44814
|
+
credential: () => autoclawUsableCredentialFor(AUTOCLAW_INTL.id)
|
|
44262
44815
|
}));
|
|
44263
44816
|
releaseAdapterAutoclawCn = ctx.llm.registerAdapter([AUTOCLAW_CN.id], new AutoclawHostAdapter({
|
|
44264
44817
|
site: AUTOCLAW_CN,
|
|
44265
|
-
credential: () =>
|
|
44818
|
+
credential: () => autoclawUsableCredentialFor(AUTOCLAW_CN.id)
|
|
44266
44819
|
}));
|
|
44267
44820
|
} catch (error) {
|
|
44268
44821
|
ctx.logger.error("dsh-xdpool: adapter registration failed", error);
|
|
@@ -44432,14 +44985,64 @@ function apply(ctx, config = {}) {
|
|
|
44432
44985
|
ctx.logger.warn?.("dsh-xdpool: usage backfill failed", error);
|
|
44433
44986
|
});
|
|
44434
44987
|
if (stopped) return;
|
|
44988
|
+
/**
|
|
44989
|
+
* What the host actually believes about one pool model's capabilities.
|
|
44990
|
+
*
|
|
44991
|
+
* The image gate, the token limits and the picker each read a different
|
|
44992
|
+
* snapshot; when they disagree the user sees "this model does not support
|
|
44993
|
+
* images" for a model the upstream serves with `supportsImages: true`.
|
|
44994
|
+
* Probing both the adapter's published list and the registry's resolve
|
|
44995
|
+
* path puts the three answers in one place instead of guessing which one
|
|
44996
|
+
* is stale.
|
|
44997
|
+
*/
|
|
44998
|
+
const probeResolvedCapabilities = async (label) => {
|
|
44999
|
+
const modelId = "deepseek-v4.1-flash";
|
|
45000
|
+
const rows = core.catalogs.cn.visible();
|
|
45001
|
+
const row = rows.find((model) => model.id === modelId);
|
|
45002
|
+
const llm = ctx.llm;
|
|
45003
|
+
let listed = "n/a";
|
|
45004
|
+
try {
|
|
45005
|
+
const models = await llm.listModels?.(POOL_PROVIDER_BY_REGION.cn) ?? [];
|
|
45006
|
+
listed = JSON.stringify(models.find((model) => model.id === modelId)?.inputModalities ?? null);
|
|
45007
|
+
} catch (error) {
|
|
45008
|
+
listed = `error:${describeError(error)}`;
|
|
45009
|
+
}
|
|
45010
|
+
let resolved = "n/a";
|
|
45011
|
+
try {
|
|
45012
|
+
const info = await llm.resolveModelInfo?.(POOL_PROVIDER_BY_REGION.cn, modelId);
|
|
45013
|
+
resolved = JSON.stringify(info?.inputModalities ?? null);
|
|
45014
|
+
} catch (error) {
|
|
45015
|
+
resolved = `error:${describeError(error)}`;
|
|
45016
|
+
}
|
|
45017
|
+
recordStartupNote(`capability probe [${label}] accounts=${core.pool.list("cn").length} catalogRows=${rows.length} catalogImages=${String(row?.supportsImages)} adapterList=${listed} resolved=${resolved}`);
|
|
45018
|
+
};
|
|
44435
45019
|
core.pool.scan().then((accounts) => {
|
|
44436
45020
|
ctx.logger.info?.(`dsh-xdpool: ${accounts.length} WorkBuddy account(s) in rotation`);
|
|
45021
|
+
/**
|
|
45022
|
+
* Rebuild the model snapshots once the pool knows its accounts.
|
|
45023
|
+
*
|
|
45024
|
+
* Registration runs BEFORE this scan, so every `buildModels()` answered
|
|
45025
|
+
* `[]` (its `hasAccounts` gate: publishing models for a region with no
|
|
45026
|
+
* account would offer rows that cannot answer, #204). The picker still
|
|
45027
|
+
* listed models because it reads the live directory, but
|
|
45028
|
+
* `resolveModelInfo` — which the host's image gate and the token limits
|
|
45029
|
+
* use — answered from that empty snapshot as a text-only model. The
|
|
45030
|
+
* visible symptom: a pool model that upstream marks `supportsImages`
|
|
45031
|
+
* refused every image with `MODEL_DOES_NOT_SUPPORT_IMAGES`.
|
|
45032
|
+
*
|
|
45033
|
+
* Re-committing here makes the two snapshots agree as soon as accounts
|
|
45034
|
+
* land, instead of waiting for the next settings change or the model
|
|
45035
|
+
* catalog to be refreshed by something else.
|
|
45036
|
+
*/
|
|
45037
|
+
invalidateCatalog();
|
|
45038
|
+
probeResolvedCapabilities("after-scan");
|
|
44437
45039
|
}, (error) => {
|
|
44438
45040
|
ctx.logger.warn("dsh-xdpool: account discovery failed", error);
|
|
44439
45041
|
});
|
|
44440
45042
|
(async () => {
|
|
44441
45043
|
await seedCatalog();
|
|
44442
45044
|
invalidateCatalog();
|
|
45045
|
+
probeResolvedCapabilities("after-seed");
|
|
44443
45046
|
})().catch((error) => {
|
|
44444
45047
|
ctx.logger.warn("dsh-xdpool: account catalog seed failed", error);
|
|
44445
45048
|
});
|