dsh-xdpool 2.7.2 → 2.7.4
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 +101 -0
- package/lib/bin.js +1 -1
- package/lib/client.js +293 -16
- package/lib/index.d.ts +12 -0
- package/lib/index.js +589 -68
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -4,12 +4,12 @@ import { appendFileSync, cpSync, existsSync, mkdirSync, readFileSync, readdirSyn
|
|
|
4
4
|
import { homedir, hostname, tmpdir } from "node:os";
|
|
5
5
|
import { basename, dirname, join, resolve } from "node:path";
|
|
6
6
|
import crypto$1, { createDecipheriv, createHash, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
|
7
|
+
import { LlmAdapter, LlmError, ReasoningEffortId, resolveImageAttachmentAccess, resolveRetryPolicy } from "@deepseek-ai/dsh-llm";
|
|
7
8
|
import { chmod, mkdir, readFile, readdir, rename, rm, unlink, writeFile } from "node:fs/promises";
|
|
8
9
|
import z from "@deepseek-ai/schemastery";
|
|
9
10
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
10
11
|
import { createProvider } from "@earendil-works/pi-ai";
|
|
11
12
|
import { openAICompletionsApi } from "@earendil-works/pi-ai/api/openai-completions.lazy";
|
|
12
|
-
import { resolveImageAttachmentAccess, resolveRetryPolicy } from "@deepseek-ai/dsh-llm";
|
|
13
13
|
import { PiAiAdapter } from "@deepseek-ai/dsh-llm-pi-ai";
|
|
14
14
|
import { env } from "node:process";
|
|
15
15
|
import http, { createServer, request } from "node:http";
|
|
@@ -791,11 +791,22 @@ function parseCacheBuffer(buf) {
|
|
|
791
791
|
if (!/^person-[a-z0-9-]+-\d{6}$/u.test(slug)) continue;
|
|
792
792
|
if (out.has(slug)) continue;
|
|
793
793
|
const name = nameForSlug(buf, slug);
|
|
794
|
+
/**
|
|
795
|
+
* The window and the multiplier travel WITH the model in this cache.
|
|
796
|
+
*
|
|
797
|
+
* Measured on a real record: `total_window_size: 300000` and a `tag.text` of `"1.48x"` sit
|
|
798
|
+
* beside `model_item_key` and `name`. Neither is on any HTTP endpoint this lane can call —
|
|
799
|
+
* `brief_list` returns feature entries and no model fields at all — so the cache is the only
|
|
800
|
+
* source for the two numbers the picker wants to print.
|
|
801
|
+
*/
|
|
802
|
+
const meta = metaNear(buf, slug);
|
|
794
803
|
out.set(slug, {
|
|
795
804
|
id: slug,
|
|
796
805
|
name: name ?? nameFromSlug(slug),
|
|
797
806
|
badge: "工作任务",
|
|
798
|
-
reasoning: true
|
|
807
|
+
reasoning: true,
|
|
808
|
+
...meta.contextWindow === void 0 ? {} : { contextWindow: meta.contextWindow },
|
|
809
|
+
...meta.rate === void 0 ? {} : { rate: meta.rate }
|
|
799
810
|
});
|
|
800
811
|
}
|
|
801
812
|
const KEY = Buffer.from("model_item_key");
|
|
@@ -856,6 +867,32 @@ function nameForSlug(buf, slug) {
|
|
|
856
867
|
}
|
|
857
868
|
}
|
|
858
869
|
/**
|
|
870
|
+
* The window and multiplier recorded beside a model key.
|
|
871
|
+
*
|
|
872
|
+
* Both live in the same V8 record as the key, a few hundred bytes after it, so they are read
|
|
873
|
+
* from the same window the name is: `total_window_size` (or `context_window`) as a number, and
|
|
874
|
+
* the first `tag.text` that reads like a multiplier. Either may be absent — a model on a plan
|
|
875
|
+
* that publishes no figure simply has neither, and printing an invented one would be worse than
|
|
876
|
+
* printing nothing.
|
|
877
|
+
*/
|
|
878
|
+
function metaNear(buf, slug) {
|
|
879
|
+
const at = buf.indexOf(Buffer.from(slug, "latin1"));
|
|
880
|
+
if (at < 0) return {};
|
|
881
|
+
const text = buf.subarray(at, Math.min(buf.length, at + 1200)).toString("latin1");
|
|
882
|
+
const out = {};
|
|
883
|
+
const rate = /(\d+(?:\.\d+)?x|免费|限时免费)/iu.exec(text);
|
|
884
|
+
if (rate?.[1] !== void 0) out.rate = rate[1].trim();
|
|
885
|
+
/**
|
|
886
|
+
* A context window: `total_window_size` followed by its digits.
|
|
887
|
+
*
|
|
888
|
+
* Matched loosely on the boundary between the name and the number because the serialised form
|
|
889
|
+
* has no separator to anchor on — any non-digit run of up to 24 bytes is allowed between them.
|
|
890
|
+
*/
|
|
891
|
+
const size = /total_window_size[^0-9]{1,24}(\d{4,7})/u.exec(text) ?? /context_window[^0-9]{1,24}(\d{4,7})/u.exec(text) ?? /(\d{3})k_\d{3}/u.exec(text);
|
|
892
|
+
if (size?.[1] !== void 0) out.contextWindow = size[0].includes("k_") ? Number(size[1]) * 1e3 : Number(size[1]);
|
|
893
|
+
return out;
|
|
894
|
+
}
|
|
895
|
+
/**
|
|
859
896
|
* A presentable name derived from a model slug.
|
|
860
897
|
*
|
|
861
898
|
* `person-kimi-k3-1-pro-highspeed` reads as "Kimi K3.1 Pro Highspeed": the vendor prefix and
|
|
@@ -961,7 +998,7 @@ const BRIEF_LIST_ENDPOINT = "https://www.doubao.com/alice/slot/action_bar_v3/bri
|
|
|
961
998
|
/** The bot whose roster is being asked for — the default Doubao assistant. */
|
|
962
999
|
const BOT_ID = "7338286299411103781";
|
|
963
1000
|
/** How long a live listing is reused. */
|
|
964
|
-
const TTL_MS = 6e5;
|
|
1001
|
+
const TTL_MS$1 = 6e5;
|
|
965
1002
|
/** The query the endpoint expects. */
|
|
966
1003
|
const CLIENT_PARAMS$2 = {
|
|
967
1004
|
aid: "497858",
|
|
@@ -981,7 +1018,7 @@ const CLIENT_PARAMS$2 = {
|
|
|
981
1018
|
samantha_web: "1",
|
|
982
1019
|
"use-olympus-account": "1"
|
|
983
1020
|
};
|
|
984
|
-
let cached$
|
|
1021
|
+
let cached$2;
|
|
985
1022
|
/**
|
|
986
1023
|
* Read the roster from the account.
|
|
987
1024
|
*
|
|
@@ -989,7 +1026,7 @@ let cached$1;
|
|
|
989
1026
|
*/
|
|
990
1027
|
async function fetchLiveModels(session, options = {}) {
|
|
991
1028
|
const now = options.now ?? Date.now();
|
|
992
|
-
if (options.force !== true && cached$
|
|
1029
|
+
if (options.force !== true && cached$2 !== void 0 && now - cached$2.at < TTL_MS$1) return cached$2.models;
|
|
993
1030
|
if (session === void 0 || !sessionUsable(session)) return DOUBAO_MODELS;
|
|
994
1031
|
const params = new URLSearchParams({
|
|
995
1032
|
...CLIENT_PARAMS$2,
|
|
@@ -1049,7 +1086,7 @@ async function fetchLiveModels(session, options = {}) {
|
|
|
1049
1086
|
byId.set(model.id, model);
|
|
1050
1087
|
}
|
|
1051
1088
|
const models = [...byId.values()];
|
|
1052
|
-
cached$
|
|
1089
|
+
cached$2 = {
|
|
1053
1090
|
at: now,
|
|
1054
1091
|
models
|
|
1055
1092
|
};
|
|
@@ -2429,6 +2466,77 @@ function createDoubaoLlm(options) {
|
|
|
2429
2466
|
};
|
|
2430
2467
|
}
|
|
2431
2468
|
//#endregion
|
|
2469
|
+
//#region src/origin-trust.ts
|
|
2470
|
+
/** Whether a hostname is the local machine. */
|
|
2471
|
+
function isLoopback(hostname) {
|
|
2472
|
+
const bare = hostname.startsWith("[") && hostname.endsWith("]") ? hostname.slice(1, -1) : hostname;
|
|
2473
|
+
return bare === "localhost" || bare === "127.0.0.1" || bare === "::1" || bare.startsWith("127.");
|
|
2474
|
+
}
|
|
2475
|
+
/**
|
|
2476
|
+
* A `trustedHosts` entry and an origin, compared the way the host compares them.
|
|
2477
|
+
*
|
|
2478
|
+
* An entry WITH a port matches exactly; a port-less entry matches the hostname on any port. The
|
|
2479
|
+
* host's own `isTrustedAuthority` works this way, and agreeing with it matters more than picking
|
|
2480
|
+
* a rule of our own — two components disagreeing about trust is how this defect happened.
|
|
2481
|
+
*/
|
|
2482
|
+
function matchesTrusted(origin, trusted) {
|
|
2483
|
+
for (const entry of trusted) {
|
|
2484
|
+
if (typeof entry !== "string" || entry.trim() === "") continue;
|
|
2485
|
+
const wanted = entry.trim().toLowerCase();
|
|
2486
|
+
if (/:\d+$/u.test(wanted)) {
|
|
2487
|
+
if (origin.host.toLowerCase() === wanted) return true;
|
|
2488
|
+
continue;
|
|
2489
|
+
}
|
|
2490
|
+
if (origin.hostname.toLowerCase() === wanted) return true;
|
|
2491
|
+
}
|
|
2492
|
+
return false;
|
|
2493
|
+
}
|
|
2494
|
+
/** The `Host` header's hostname, or undefined when it is absent or unparseable. */
|
|
2495
|
+
function hostHeaderName(req) {
|
|
2496
|
+
const host = req.headers.host;
|
|
2497
|
+
if (typeof host !== "string" || host === "") return void 0;
|
|
2498
|
+
try {
|
|
2499
|
+
return new URL(`http://${host}`).hostname.toLowerCase();
|
|
2500
|
+
} catch {
|
|
2501
|
+
return;
|
|
2502
|
+
}
|
|
2503
|
+
}
|
|
2504
|
+
/**
|
|
2505
|
+
* Decide whether `req` may act.
|
|
2506
|
+
*
|
|
2507
|
+
* `trusted` is read fresh on each call so a configuration change reaches the next request
|
|
2508
|
+
* without a plugin restart — the host's own bridge does the same.
|
|
2509
|
+
*/
|
|
2510
|
+
function originTrusted(req, trusted = []) {
|
|
2511
|
+
const origin = req.headers.origin;
|
|
2512
|
+
/**
|
|
2513
|
+
* No `Origin` at all.
|
|
2514
|
+
*
|
|
2515
|
+
* A same-origin `fetch()` in Chromium omits it for GET/HEAD, and the card's own reads are
|
|
2516
|
+
* exactly that. Refusing this would break the page on the machine it was installed on.
|
|
2517
|
+
*/
|
|
2518
|
+
if (typeof origin !== "string" || origin === "") return true;
|
|
2519
|
+
let url;
|
|
2520
|
+
try {
|
|
2521
|
+
url = new URL(origin);
|
|
2522
|
+
} catch {
|
|
2523
|
+
return false;
|
|
2524
|
+
}
|
|
2525
|
+
if (isLoopback(url.hostname)) return true;
|
|
2526
|
+
if (matchesTrusted(url, trusted)) return true;
|
|
2527
|
+
/**
|
|
2528
|
+
* Same-origin, judged by the browser's own two headers.
|
|
2529
|
+
*
|
|
2530
|
+
* This is the case a reverse proxy produces: the page is served from
|
|
2531
|
+
* `https://node.tailXXXX.ts.net`, so that is what `Origin` says, and the request arrives at a
|
|
2532
|
+
* plugin route on that same authority — so `Host` says the same thing. A malicious page on
|
|
2533
|
+
* another site cannot make these agree.
|
|
2534
|
+
*/
|
|
2535
|
+
const host = hostHeaderName(req);
|
|
2536
|
+
if (host !== void 0 && url.hostname.toLowerCase() === host) return true;
|
|
2537
|
+
return false;
|
|
2538
|
+
}
|
|
2539
|
+
//#endregion
|
|
2432
2540
|
//#region src/status-paths.ts
|
|
2433
2541
|
/**
|
|
2434
2542
|
* Node-free constants and types shared by the Host and browser halves of the
|
|
@@ -2473,6 +2581,8 @@ const POOL_ACCOUNT_DISABLE_PATH = "/plugins/dsh-xdpool/accounts/disabled";
|
|
|
2473
2581
|
const POOL_ACCOUNT_IGNORE_PATH = "/plugins/dsh-xdpool/accounts/ignored";
|
|
2474
2582
|
/** Run one automation job immediately, so the card can verify it on demand. */
|
|
2475
2583
|
const POOL_AUTOMATION_RUN_PATH = "/plugins/dsh-xdpool/automation/run";
|
|
2584
|
+
/** Is there a newer build of this plugin? */
|
|
2585
|
+
const POOL_UPDATE_CHECK_PATH = "/plugins/dsh-xdpool/update";
|
|
2476
2586
|
/** Set or clear one account's reserved-credit floor. */
|
|
2477
2587
|
const POOL_CREDIT_RESERVE_PATH = "/plugins/dsh-xdpool/accounts/credit-reserve";
|
|
2478
2588
|
/**
|
|
@@ -3078,7 +3188,7 @@ function evaluateQuotaExhausted(data) {
|
|
|
3078
3188
|
}
|
|
3079
3189
|
async function fetchQuota(options = {}) {
|
|
3080
3190
|
const now = Date.now();
|
|
3081
|
-
if (!options.force && cached && now - cached.fetchedAt < 3e5) return cached;
|
|
3191
|
+
if (!options.force && cached$1 && now - cached$1.fetchedAt < 3e5) return cached$1;
|
|
3082
3192
|
if (!options.force && cachedFailure !== void 0 && now - cachedFailureAt < QUOTA_FAILURE_TTL_MS) return cachedFailure;
|
|
3083
3193
|
/**
|
|
3084
3194
|
* No session is a quotable answer, not a throw.
|
|
@@ -3125,7 +3235,7 @@ async function fetchQuota(options = {}) {
|
|
|
3125
3235
|
const plan = payload.data?.current_subscription?.display?.short_name;
|
|
3126
3236
|
if (plan) quota.plan = plan;
|
|
3127
3237
|
if (payload.data?.member_info?.hasActiveSubscription !== void 0) quota.planActive = payload.data.member_info.hasActiveSubscription;
|
|
3128
|
-
cached = quota;
|
|
3238
|
+
cached$1 = quota;
|
|
3129
3239
|
cachedFailure = void 0;
|
|
3130
3240
|
return quota;
|
|
3131
3241
|
} catch (error) {
|
|
@@ -3137,7 +3247,7 @@ async function fetchQuota(options = {}) {
|
|
|
3137
3247
|
});
|
|
3138
3248
|
}
|
|
3139
3249
|
}
|
|
3140
|
-
let cached;
|
|
3250
|
+
let cached$1;
|
|
3141
3251
|
/** The last FAILED reading, reused for {@link QUOTA_FAILURE_TTL_MS}. */
|
|
3142
3252
|
let cachedFailure;
|
|
3143
3253
|
let cachedFailureAt = 0;
|
|
@@ -3161,25 +3271,6 @@ function json$1(res, status, body) {
|
|
|
3161
3271
|
res.end(JSON.stringify(body));
|
|
3162
3272
|
}
|
|
3163
3273
|
/**
|
|
3164
|
-
* Whether the request came from the host's own page.
|
|
3165
|
-
*
|
|
3166
|
-
* An ABSENT `Origin` is allowed and is the common case: only cross-origin requests carry the
|
|
3167
|
-
* header, so a same-origin request from the card sends none. Requiring it refused every call
|
|
3168
|
-
* the UI made — the paste arrived, was rejected `403` before any parsing ran, and the card
|
|
3169
|
-
* reported it as a bad cookie. A page in the user's browser driving this endpoint is
|
|
3170
|
-
* cross-origin by construction and always sends `Origin`, so the hostname check covers it.
|
|
3171
|
-
*/
|
|
3172
|
-
function loopbackOrigin$1(req) {
|
|
3173
|
-
const origin = req.headers.origin;
|
|
3174
|
-
if (typeof origin !== "string" || origin === "") return true;
|
|
3175
|
-
try {
|
|
3176
|
-
const url = new URL(origin);
|
|
3177
|
-
return url.hostname === "127.0.0.1" || url.hostname === "localhost" || url.hostname === "::1";
|
|
3178
|
-
} catch {
|
|
3179
|
-
return false;
|
|
3180
|
-
}
|
|
3181
|
-
}
|
|
3182
|
-
/**
|
|
3183
3274
|
* Mount the Doubao routes.
|
|
3184
3275
|
*
|
|
3185
3276
|
* The sign-in route answers `signedIn: false` rather than an error when the user closes the
|
|
@@ -3191,7 +3282,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3191
3282
|
kind: "exact",
|
|
3192
3283
|
path: POOL_DOUBAO_SESSION_PATH,
|
|
3193
3284
|
handler: async (req, res) => {
|
|
3194
|
-
if (!
|
|
3285
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3195
3286
|
try {
|
|
3196
3287
|
if (req.method === "DELETE") {
|
|
3197
3288
|
deps.clear();
|
|
@@ -3268,7 +3359,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3268
3359
|
path: POOL_DOUBAO_QUOTA_PATH,
|
|
3269
3360
|
handler: async (req, res) => {
|
|
3270
3361
|
if (req.method !== "GET") return json$1(res, 405, { error: "method not allowed" });
|
|
3271
|
-
if (!
|
|
3362
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3272
3363
|
const session = deps.session();
|
|
3273
3364
|
if (session === void 0 || !sessionUsable(session)) return json$1(res, 200, {
|
|
3274
3365
|
ok: false,
|
|
@@ -3300,7 +3391,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3300
3391
|
path: POOL_DOUBAO_MODELS_PATH,
|
|
3301
3392
|
handler: async (req, res) => {
|
|
3302
3393
|
if (req.method !== "GET") return json$1(res, 405, { error: "method not allowed" });
|
|
3303
|
-
if (!
|
|
3394
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3304
3395
|
/**
|
|
3305
3396
|
* The same list the picker gets.
|
|
3306
3397
|
*
|
|
@@ -3327,7 +3418,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3327
3418
|
path: POOL_DOUBAO_CAMPAIGN_PATH,
|
|
3328
3419
|
handler: async (req, res) => {
|
|
3329
3420
|
if (req.method !== "GET") return json$1(res, 405, { error: "method not allowed" });
|
|
3330
|
-
if (!
|
|
3421
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3331
3422
|
const session = deps.session();
|
|
3332
3423
|
const campaign = await fetchCampaign(session);
|
|
3333
3424
|
return json$1(res, 200, {
|
|
@@ -3342,7 +3433,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3342
3433
|
path: POOL_DOUBAO_CLAIM_PATH,
|
|
3343
3434
|
handler: async (req, res) => {
|
|
3344
3435
|
if (req.method !== "POST") return json$1(res, 405, { error: "method not allowed" });
|
|
3345
|
-
if (!
|
|
3436
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3346
3437
|
const result = await claimTasks(deps.session());
|
|
3347
3438
|
return json$1(res, 200, {
|
|
3348
3439
|
ok: result.failed === 0,
|
|
@@ -3354,7 +3445,7 @@ function registerDoubaoRoutes(ctx, deps) {
|
|
|
3354
3445
|
kind: "exact",
|
|
3355
3446
|
path: POOL_DOUBAO_AUTOMATION_PATH,
|
|
3356
3447
|
handler: async (req, res) => {
|
|
3357
|
-
if (!
|
|
3448
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json$1(res, 403, { error: "origin-not-trusted" });
|
|
3358
3449
|
const automation = deps.automation();
|
|
3359
3450
|
try {
|
|
3360
3451
|
if (req.method === "POST") {
|
|
@@ -3517,6 +3608,246 @@ function createDoubaoAutomation(options) {
|
|
|
3517
3608
|
};
|
|
3518
3609
|
}
|
|
3519
3610
|
//#endregion
|
|
3611
|
+
//#region src/doubao-host-adapter.ts
|
|
3612
|
+
/**
|
|
3613
|
+
* Doubao as a host adapter.
|
|
3614
|
+
*
|
|
3615
|
+
* WHY THIS IS NEEDED
|
|
3616
|
+
*
|
|
3617
|
+
* `channel-lane.ts` serves `<channel>:<model>` ids for a gateway route, but the model PICKER
|
|
3618
|
+
* reads the host's adapter registry — `ctx.llm.registerAdapter([id], adapter)`. Every channel in
|
|
3619
|
+
* the pack registers one; Doubao did not, so a signed-in account with ten models enabled showed
|
|
3620
|
+
* nothing in the picker at all. The lane made the models reachable over HTTP; this makes them
|
|
3621
|
+
* visible in the list.
|
|
3622
|
+
*
|
|
3623
|
+
* WHAT IT IMPLEMENTS
|
|
3624
|
+
*
|
|
3625
|
+
* The five methods `LlmAdapter` requires, in the shape the pack's own adapters use:
|
|
3626
|
+
* `providerInfo`, `listModels`, `resolveModel`, `prepareCall`, `stream`. The wire stays in
|
|
3627
|
+
* `doubao-upstream.ts` — the module ported from a working implementation — so nothing here
|
|
3628
|
+
* invents request details.
|
|
3629
|
+
*
|
|
3630
|
+
* WHAT IT REPORTS ABOUT A MODEL
|
|
3631
|
+
*
|
|
3632
|
+
* The picker shows what these fields say, so they carry the same facts the other channels do:
|
|
3633
|
+
* context window, output ceiling, whether the model reasons, and the multiplier as a tag. A
|
|
3634
|
+
* model with no published figure says nothing rather than inventing one.
|
|
3635
|
+
*
|
|
3636
|
+
* @module dsh-xdpool/doubao-host-adapter
|
|
3637
|
+
*/
|
|
3638
|
+
/** The provider id the picker groups by. */
|
|
3639
|
+
const DOUBAO_PROVIDER_ID = "doubao";
|
|
3640
|
+
/** The name the picker shows for the group. */
|
|
3641
|
+
const DOUBAO_PROVIDER_NAME = "豆包";
|
|
3642
|
+
/**
|
|
3643
|
+
* The context window this lane reports.
|
|
3644
|
+
*
|
|
3645
|
+
* Doubao does not publish a per-model window on the endpoints this lane calls, and the chat
|
|
3646
|
+
* itself accepts far more than the client advertises. 256k is what the reference implementation
|
|
3647
|
+
* settled on after measuring; reporting nothing would make the picker show "—" beside every
|
|
3648
|
+
* model, which reads as a broken channel.
|
|
3649
|
+
*/
|
|
3650
|
+
const CONTEXT_WINDOW = 256e3;
|
|
3651
|
+
/** The output ceiling the lane requests. */
|
|
3652
|
+
const MAX_OUTPUT_TOKENS = 65536;
|
|
3653
|
+
/**
|
|
3654
|
+
* The multiplier, when the name carries one.
|
|
3655
|
+
*
|
|
3656
|
+
* Every vendor in this plugin writes the price into the model name; Doubao's own models are
|
|
3657
|
+
* included in the promotion programme, so the same shape appears (`… · x0.00`, `… · 免费`). The
|
|
3658
|
+
* split is done here rather than in the card so the PICKER sees it too.
|
|
3659
|
+
*/
|
|
3660
|
+
function rateOf(name) {
|
|
3661
|
+
return /[·・]\s*(x\s*[\d.]+(?:\s*→\s*x?[\d.]+)?|免费|限时免费)/iu.exec(name)?.[1]?.trim();
|
|
3662
|
+
}
|
|
3663
|
+
/** The name without its trailing price. */
|
|
3664
|
+
function bareName(name) {
|
|
3665
|
+
return name.replace(/\s*[·・]\s*(?:x\s*[\d.]+(?:\s*→\s*x?[\d.]+)?|免费|限时免费|正式版.*)$/iu, "").trim();
|
|
3666
|
+
}
|
|
3667
|
+
/**
|
|
3668
|
+
* The adapter.
|
|
3669
|
+
*
|
|
3670
|
+
* `listModels` reads the account's live roster through `fetchLiveModels`, which merges the
|
|
3671
|
+
* measured six with whatever `brief_list` returns and whatever the desktop client's cache holds —
|
|
3672
|
+
* that is where the `person-*` models (DeepSeek, Kimi, GLM) come from.
|
|
3673
|
+
*/
|
|
3674
|
+
var DoubaoHostAdapter = class extends LlmAdapter {
|
|
3675
|
+
options;
|
|
3676
|
+
/** The last roster read, so a picker redraw does not re-fetch. */
|
|
3677
|
+
cached;
|
|
3678
|
+
constructor(options) {
|
|
3679
|
+
super();
|
|
3680
|
+
this.options = options;
|
|
3681
|
+
}
|
|
3682
|
+
providerInfo(_provider) {
|
|
3683
|
+
return {
|
|
3684
|
+
id: DOUBAO_PROVIDER_ID,
|
|
3685
|
+
name: DOUBAO_PROVIDER_NAME
|
|
3686
|
+
};
|
|
3687
|
+
}
|
|
3688
|
+
async listModels(_provider) {
|
|
3689
|
+
const models = await fetchLiveModels(this.options.session());
|
|
3690
|
+
this.cached = {
|
|
3691
|
+
at: Date.now(),
|
|
3692
|
+
models
|
|
3693
|
+
};
|
|
3694
|
+
return models.map((model) => this.describe(model));
|
|
3695
|
+
}
|
|
3696
|
+
async resolveModel(provider, model, _signal) {
|
|
3697
|
+
const known = this.cached?.models.find((entry) => entry.id === model) ?? DOUBAO_MODELS.find((entry) => entry.id === model);
|
|
3698
|
+
if (known === void 0) return this.describe({
|
|
3699
|
+
id: model,
|
|
3700
|
+
name: model,
|
|
3701
|
+
reasoning: true
|
|
3702
|
+
});
|
|
3703
|
+
return this.describe(known);
|
|
3704
|
+
}
|
|
3705
|
+
async prepareCall(provider, model, signal) {
|
|
3706
|
+
return {
|
|
3707
|
+
model: await this.resolveModel(provider, model, signal),
|
|
3708
|
+
stream: (streamOptions) => this.stream(streamOptions)
|
|
3709
|
+
};
|
|
3710
|
+
}
|
|
3711
|
+
/**
|
|
3712
|
+
* One turn.
|
|
3713
|
+
*
|
|
3714
|
+
* Text and reasoning are queued separately and drained as they arrive, so a long answer
|
|
3715
|
+
* appears progressively instead of landing whole. An image answer is appended as markdown —
|
|
3716
|
+
* the client renders it without knowing about Doubao's own image blocks.
|
|
3717
|
+
*/
|
|
3718
|
+
async *stream(options) {
|
|
3719
|
+
const session = this.options.session();
|
|
3720
|
+
if (session === void 0 || !sessionUsable(session)) throw new LlmError("还没有浏览器登录态。请在豆包卡片里点「重新登录」扫码一次。", "auth");
|
|
3721
|
+
const modelId = typeof options["model"] === "string" ? options["model"] : "";
|
|
3722
|
+
if (modelId === "") throw new LlmError("这条请求没有指定模型", "invalid");
|
|
3723
|
+
const messages = Array.isArray(options["messages"]) ? options["messages"] : [];
|
|
3724
|
+
const system = typeof options["system"] === "string" ? options["system"] : "";
|
|
3725
|
+
const body = promptOf(messages);
|
|
3726
|
+
const prompt = system === "" ? body : `${system}\n\n${body}`;
|
|
3727
|
+
if (prompt === "") throw new LlmError("这条请求没有可发送的文字", "invalid");
|
|
3728
|
+
const requested = options["reasoningEffort"];
|
|
3729
|
+
const level = typeof requested === "string" && [
|
|
3730
|
+
"low",
|
|
3731
|
+
"mid",
|
|
3732
|
+
"high"
|
|
3733
|
+
].includes(requested) ? requested : "mid";
|
|
3734
|
+
const signal = options["signal"] instanceof AbortSignal ? options["signal"] : new AbortController().signal;
|
|
3735
|
+
const textQueue = [];
|
|
3736
|
+
const reasoningQueue = [];
|
|
3737
|
+
let done = false;
|
|
3738
|
+
let failure;
|
|
3739
|
+
let images = [];
|
|
3740
|
+
const turn = chat(prompt, {
|
|
3741
|
+
modelKey: modelId,
|
|
3742
|
+
reasoningLevel: level,
|
|
3743
|
+
signal,
|
|
3744
|
+
callbacks: {
|
|
3745
|
+
onText: (chunk) => {
|
|
3746
|
+
if (chunk !== "") textQueue.push(chunk);
|
|
3747
|
+
},
|
|
3748
|
+
onThinking: (chunk) => {
|
|
3749
|
+
if (chunk !== "") reasoningQueue.push(chunk);
|
|
3750
|
+
}
|
|
3751
|
+
}
|
|
3752
|
+
}).then((result) => {
|
|
3753
|
+
if (typeof result.conversationId === "string" && result.conversationId !== "") this.options.onConversationId?.(result.conversationId);
|
|
3754
|
+
if (Array.isArray(result.images)) images = result.images;
|
|
3755
|
+
}).catch((error) => {
|
|
3756
|
+
failure = error instanceof Error ? error : new Error(String(error));
|
|
3757
|
+
}).finally(() => {
|
|
3758
|
+
done = true;
|
|
3759
|
+
});
|
|
3760
|
+
while (!done || textQueue.length > 0 || reasoningQueue.length > 0) {
|
|
3761
|
+
const thinking = reasoningQueue.shift();
|
|
3762
|
+
if (thinking !== void 0) {
|
|
3763
|
+
yield {
|
|
3764
|
+
type: "reasoning-delta",
|
|
3765
|
+
index: 0,
|
|
3766
|
+
text: thinking
|
|
3767
|
+
};
|
|
3768
|
+
continue;
|
|
3769
|
+
}
|
|
3770
|
+
const text = textQueue.shift();
|
|
3771
|
+
if (text !== void 0) {
|
|
3772
|
+
yield {
|
|
3773
|
+
type: "text-delta",
|
|
3774
|
+
index: 0,
|
|
3775
|
+
text
|
|
3776
|
+
};
|
|
3777
|
+
continue;
|
|
3778
|
+
}
|
|
3779
|
+
if (done) break;
|
|
3780
|
+
await Promise.race([turn, new Promise((resolve) => setTimeout(resolve, 20))]);
|
|
3781
|
+
}
|
|
3782
|
+
await turn;
|
|
3783
|
+
if (failure !== void 0) throw failure;
|
|
3784
|
+
for (const image of images) if (typeof image.url === "string" && image.url !== "") yield {
|
|
3785
|
+
type: "text-delta",
|
|
3786
|
+
index: 0,
|
|
3787
|
+
text: `\n\n\n`
|
|
3788
|
+
};
|
|
3789
|
+
}
|
|
3790
|
+
/** Everything the picker shows for one model. */
|
|
3791
|
+
describe(model) {
|
|
3792
|
+
const rate = rateOf(model.name);
|
|
3793
|
+
const free = rate !== void 0 && (/免费/u.test(rate) || /^x\s*0*\.?0+$/iu.test(rate));
|
|
3794
|
+
return {
|
|
3795
|
+
provider: DOUBAO_PROVIDER_ID,
|
|
3796
|
+
id: model.id,
|
|
3797
|
+
name: bareName(model.name),
|
|
3798
|
+
inputModalities: ["text", "image"],
|
|
3799
|
+
defaultMaxTokens: MAX_OUTPUT_TOKENS,
|
|
3800
|
+
context: { contextWindow: CONTEXT_WINDOW },
|
|
3801
|
+
reasoning: {
|
|
3802
|
+
efforts: [
|
|
3803
|
+
{
|
|
3804
|
+
id: ReasoningEffortId("low"),
|
|
3805
|
+
name: "低"
|
|
3806
|
+
},
|
|
3807
|
+
{
|
|
3808
|
+
id: ReasoningEffortId("mid"),
|
|
3809
|
+
name: "中"
|
|
3810
|
+
},
|
|
3811
|
+
{
|
|
3812
|
+
id: ReasoningEffortId("high"),
|
|
3813
|
+
name: "高"
|
|
3814
|
+
}
|
|
3815
|
+
],
|
|
3816
|
+
defaultEffort: ReasoningEffortId("mid")
|
|
3817
|
+
},
|
|
3818
|
+
...rate === void 0 ? {} : { tags: [{
|
|
3819
|
+
text: rate,
|
|
3820
|
+
tone: free ? "free" : "price"
|
|
3821
|
+
}] }
|
|
3822
|
+
};
|
|
3823
|
+
}
|
|
3824
|
+
};
|
|
3825
|
+
/** The readable text of the host's message list. */
|
|
3826
|
+
function promptOf(messages) {
|
|
3827
|
+
const parts = [];
|
|
3828
|
+
for (const message of messages) {
|
|
3829
|
+
if (typeof message !== "object" || message === null) continue;
|
|
3830
|
+
const content = message["content"];
|
|
3831
|
+
if (typeof content === "string") {
|
|
3832
|
+
if (content !== "") parts.push(content);
|
|
3833
|
+
continue;
|
|
3834
|
+
}
|
|
3835
|
+
if (!Array.isArray(content)) continue;
|
|
3836
|
+
const texts = [];
|
|
3837
|
+
for (const block of content) {
|
|
3838
|
+
if (typeof block === "string") {
|
|
3839
|
+
texts.push(block);
|
|
3840
|
+
continue;
|
|
3841
|
+
}
|
|
3842
|
+
if (typeof block !== "object" || block === null) continue;
|
|
3843
|
+
const text = block["text"];
|
|
3844
|
+
if (typeof text === "string") texts.push(text);
|
|
3845
|
+
}
|
|
3846
|
+
if (texts.length > 0) parts.push(texts.join("\n"));
|
|
3847
|
+
}
|
|
3848
|
+
return parts.join("\n\n");
|
|
3849
|
+
}
|
|
3850
|
+
//#endregion
|
|
3520
3851
|
//#region src/file-perms.ts
|
|
3521
3852
|
/**
|
|
3522
3853
|
* Write a file that holds credentials, without leaving it world-readable.
|
|
@@ -19387,6 +19718,149 @@ function parseBody(body) {
|
|
|
19387
19718
|
throw new Error("the request body was not a JSON object");
|
|
19388
19719
|
}
|
|
19389
19720
|
//#endregion
|
|
19721
|
+
//#region src/version.ts
|
|
19722
|
+
/**
|
|
19723
|
+
* The plugin's own version, hand-maintained for the browser bundle.
|
|
19724
|
+
*
|
|
19725
|
+
* The card ships as `lib/client.js`, served to the host dialog as a browser
|
|
19726
|
+
* script: `package.json` is neither reachable nor guaranteed to be the file
|
|
19727
|
+
* the running build came from. A manifest lying next to the code is worse than
|
|
19728
|
+
* a number a test keeps honest.
|
|
19729
|
+
*
|
|
19730
|
+
* `tests/version-sync.test.ts` fails when this drifts from `package.json`, so
|
|
19731
|
+
* the release checklist cannot ship a card that calls itself the wrong version.
|
|
19732
|
+
*
|
|
19733
|
+
* @module dsh-xdpool/version
|
|
19734
|
+
*/
|
|
19735
|
+
const POOL_VERSION = "2.7.4";
|
|
19736
|
+
//#endregion
|
|
19737
|
+
//#region src/update-check.ts
|
|
19738
|
+
/**
|
|
19739
|
+
* Whether a newer build of this plugin exists.
|
|
19740
|
+
*
|
|
19741
|
+
* WHY IT LIVES IN THE PLUGIN
|
|
19742
|
+
*
|
|
19743
|
+
* Updating meant watching the repository by hand or asking an agent to check — neither is
|
|
19744
|
+
* something a user should have to do to stay current. A version read from the registry on demand
|
|
19745
|
+
* answers the only question worth asking ("is there something newer than what I run?") without a
|
|
19746
|
+
* background task that phones home on its own schedule.
|
|
19747
|
+
*
|
|
19748
|
+
* WHAT IT ASKS
|
|
19749
|
+
*
|
|
19750
|
+
* The npm registry's abbreviated document for this package: `dist-tags.latest` and the publish
|
|
19751
|
+
* time of that version. Nothing else is read, nothing is sent beyond the package name, and the
|
|
19752
|
+
* answer is cached for a few minutes so opening the panel repeatedly is one request.
|
|
19753
|
+
*
|
|
19754
|
+
* A FAILURE IS AN ORDINARY ANSWER
|
|
19755
|
+
*
|
|
19756
|
+
* A machine behind a proxy, offline, or with no registry access still has a working plugin. The
|
|
19757
|
+
* result carries `error` and the card says "could not check" rather than hiding the control or
|
|
19758
|
+
* claiming there is nothing new — telling a user they are current when the check did not run is
|
|
19759
|
+
* the one wrong answer here.
|
|
19760
|
+
*
|
|
19761
|
+
* @module dsh-xdpool/update-check
|
|
19762
|
+
*/
|
|
19763
|
+
/** The package name this plugin publishes under. */
|
|
19764
|
+
const PACKAGE = "dsh-xdpool";
|
|
19765
|
+
/** The registries tried, in order. npm first: it is the canonical one. */
|
|
19766
|
+
const REGISTRIES = [`https://registry.npmjs.org/${PACKAGE}/latest`, `https://registry.npmjs.org/${PACKAGE}`];
|
|
19767
|
+
/** How long an answer is reused. */
|
|
19768
|
+
const TTL_MS = 6e5;
|
|
19769
|
+
let cached;
|
|
19770
|
+
/**
|
|
19771
|
+
* Compare two dotted versions.
|
|
19772
|
+
*
|
|
19773
|
+
* Numeric segments compare as numbers and everything else as text, which is the ordering npm
|
|
19774
|
+
* itself uses for the plain `x.y.z` releases this package publishes. A prerelease tag is older
|
|
19775
|
+
* than its release (`2.7.3-rc.1 < 2.7.3`), which is what the `-` check below states.
|
|
19776
|
+
*/
|
|
19777
|
+
function compareVersions(left, right) {
|
|
19778
|
+
const parse = (value) => {
|
|
19779
|
+
const [core, ...rest] = value.trim().replace(/^v/u, "").split("-");
|
|
19780
|
+
return {
|
|
19781
|
+
parts: (core ?? "").split(".").map((segment) => {
|
|
19782
|
+
const number = Number.parseInt(segment, 10);
|
|
19783
|
+
return Number.isFinite(number) ? number : 0;
|
|
19784
|
+
}),
|
|
19785
|
+
pre: rest.join("-")
|
|
19786
|
+
};
|
|
19787
|
+
};
|
|
19788
|
+
const a = parse(left);
|
|
19789
|
+
const b = parse(right);
|
|
19790
|
+
const length = Math.max(a.parts.length, b.parts.length);
|
|
19791
|
+
for (let i = 0; i < length; i += 1) {
|
|
19792
|
+
const left1 = a.parts[i] ?? 0;
|
|
19793
|
+
const right1 = b.parts[i] ?? 0;
|
|
19794
|
+
if (left1 !== right1) return left1 < right1 ? -1 : 1;
|
|
19795
|
+
}
|
|
19796
|
+
if (a.pre === "" && b.pre !== "") return 1;
|
|
19797
|
+
if (a.pre !== "" && b.pre === "") return -1;
|
|
19798
|
+
return a.pre === b.pre ? 0 : a.pre < b.pre ? -1 : 1;
|
|
19799
|
+
}
|
|
19800
|
+
/** Read `dist-tags.latest` and its publish time from a registry document. */
|
|
19801
|
+
function parseRegistry(body) {
|
|
19802
|
+
if (body === null || typeof body !== "object") return {};
|
|
19803
|
+
const record = body;
|
|
19804
|
+
const direct = record["version"];
|
|
19805
|
+
if (typeof direct === "string" && direct !== "") return { latest: direct };
|
|
19806
|
+
const tags = record["dist-tags"];
|
|
19807
|
+
if (tags !== null && typeof tags === "object") {
|
|
19808
|
+
const latest = tags["latest"];
|
|
19809
|
+
if (typeof latest === "string" && latest !== "") {
|
|
19810
|
+
const times = record["time"];
|
|
19811
|
+
const when = times !== null && typeof times === "object" ? times[latest] : void 0;
|
|
19812
|
+
return {
|
|
19813
|
+
latest,
|
|
19814
|
+
...typeof when === "string" ? { publishedAt: when } : {}
|
|
19815
|
+
};
|
|
19816
|
+
}
|
|
19817
|
+
}
|
|
19818
|
+
return {};
|
|
19819
|
+
}
|
|
19820
|
+
/**
|
|
19821
|
+
* Ask the registry, or answer from the cache.
|
|
19822
|
+
*
|
|
19823
|
+
* `force` skips the cache — that is what the card's button does, and a button that silently
|
|
19824
|
+
* returns a ten-minute-old answer is indistinguishable from one that did nothing.
|
|
19825
|
+
*/
|
|
19826
|
+
async function checkForUpdate(options = {}) {
|
|
19827
|
+
const now = options.now ?? Date.now();
|
|
19828
|
+
if (options.force !== true && cached !== void 0 && now - cached.at < TTL_MS) return cached.status;
|
|
19829
|
+
const checkedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
19830
|
+
for (const url of REGISTRIES) try {
|
|
19831
|
+
const response = await fetch(url, {
|
|
19832
|
+
headers: { accept: "application/vnd.npm.install-v1+json, application/json" },
|
|
19833
|
+
signal: AbortSignal.timeout(8e3)
|
|
19834
|
+
});
|
|
19835
|
+
if (!response.ok) continue;
|
|
19836
|
+
const parsed = parseRegistry(await response.json());
|
|
19837
|
+
if (parsed.latest === void 0) continue;
|
|
19838
|
+
const status = {
|
|
19839
|
+
current: POOL_VERSION,
|
|
19840
|
+
latest: parsed.latest,
|
|
19841
|
+
newer: compareVersions(parsed.latest, POOL_VERSION) > 0,
|
|
19842
|
+
...parsed.publishedAt === void 0 ? {} : { publishedAt: parsed.publishedAt },
|
|
19843
|
+
checkedAt
|
|
19844
|
+
};
|
|
19845
|
+
cached = {
|
|
19846
|
+
at: now,
|
|
19847
|
+
status
|
|
19848
|
+
};
|
|
19849
|
+
return status;
|
|
19850
|
+
} catch {}
|
|
19851
|
+
const status = {
|
|
19852
|
+
current: POOL_VERSION,
|
|
19853
|
+
newer: false,
|
|
19854
|
+
error: "无法访问 npm registry(离线或代理未放行)",
|
|
19855
|
+
checkedAt
|
|
19856
|
+
};
|
|
19857
|
+
cached = {
|
|
19858
|
+
at: now - TTL_MS + 6e4,
|
|
19859
|
+
status
|
|
19860
|
+
};
|
|
19861
|
+
return status;
|
|
19862
|
+
}
|
|
19863
|
+
//#endregion
|
|
19390
19864
|
//#region src/task-events.ts
|
|
19391
19865
|
/**
|
|
19392
19866
|
* The app the buddy chain enters.
|
|
@@ -21210,17 +21684,6 @@ function json(res, status, body) {
|
|
|
21210
21684
|
});
|
|
21211
21685
|
res.end(payload);
|
|
21212
21686
|
}
|
|
21213
|
-
/** Loopback browser origins only; other devices are refused. */
|
|
21214
|
-
function loopbackOrigin(req) {
|
|
21215
|
-
const origin = req.headers.origin;
|
|
21216
|
-
if (origin === void 0) return true;
|
|
21217
|
-
try {
|
|
21218
|
-
const { hostname } = new URL(origin);
|
|
21219
|
-
return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]" || hostname === "::1";
|
|
21220
|
-
} catch {
|
|
21221
|
-
return false;
|
|
21222
|
-
}
|
|
21223
|
-
}
|
|
21224
21687
|
/** Smallest possible POST body reader, capped so a hung or oversized body
|
|
21225
21688
|
* cannot pin memory on the Host. Returns `{}` for an empty body. */
|
|
21226
21689
|
function readJsonBody(req) {
|
|
@@ -21767,7 +22230,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
21767
22230
|
path: POOL_STATUS_PATH,
|
|
21768
22231
|
handler: async (req, res) => {
|
|
21769
22232
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
21770
|
-
if (!
|
|
22233
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
21771
22234
|
try {
|
|
21772
22235
|
json(res, 200, await poolWebStatus(deps, new URL(req.url ?? "/", "http://localhost").searchParams.get("region") === "global" ? "global" : "cn"));
|
|
21773
22236
|
} catch (error) {
|
|
@@ -21787,7 +22250,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
21787
22250
|
path: POOL_USAGE_PATH,
|
|
21788
22251
|
handler: async (req, res) => {
|
|
21789
22252
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
21790
|
-
if (!
|
|
22253
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
21791
22254
|
if (deps.usage === void 0) return json(res, 503, { error: "usage accounting is not available in this build" });
|
|
21792
22255
|
try {
|
|
21793
22256
|
json(res, 200, deps.usage());
|
|
@@ -21813,7 +22276,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
21813
22276
|
path: POOL_CATALOG_REFRESH_PATH,
|
|
21814
22277
|
handler: async (req, res) => {
|
|
21815
22278
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
21816
|
-
if (!
|
|
22279
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
21817
22280
|
if (deps.refreshCatalog === void 0) return json(res, 503, { error: "catalog refresh is not available in this build" });
|
|
21818
22281
|
try {
|
|
21819
22282
|
json(res, 200, {
|
|
@@ -21830,7 +22293,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
21830
22293
|
path: POOL_RESCAN_PATH,
|
|
21831
22294
|
handler: async (req, res) => {
|
|
21832
22295
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
21833
|
-
if (!
|
|
22296
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
21834
22297
|
try {
|
|
21835
22298
|
json(res, 200, { accounts: (await deps.pool.scan()).length });
|
|
21836
22299
|
} catch (error) {
|
|
@@ -21843,7 +22306,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
21843
22306
|
path: POOL_RESET_COOLDOWN_PATH,
|
|
21844
22307
|
handler: async (req, res) => {
|
|
21845
22308
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
21846
|
-
if (!
|
|
22309
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
21847
22310
|
deps.pool.resetCooldowns();
|
|
21848
22311
|
json(res, 200, { ok: true });
|
|
21849
22312
|
}
|
|
@@ -21853,7 +22316,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
21853
22316
|
path: POOL_CHECKIN_PATH,
|
|
21854
22317
|
handler: async (req, res) => {
|
|
21855
22318
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
21856
|
-
if (!
|
|
22319
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
21857
22320
|
try {
|
|
21858
22321
|
const body = await readJsonBody(req);
|
|
21859
22322
|
const accountId = typeof body["accountId"] === "string" ? body["accountId"] : "";
|
|
@@ -21885,7 +22348,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
21885
22348
|
path: POOL_MODELS_SAVE_PATH,
|
|
21886
22349
|
handler: async (req, res) => {
|
|
21887
22350
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
21888
|
-
if (!
|
|
22351
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
21889
22352
|
if (deps.saveSelection === void 0) return json(res, 503, { error: "settings service unavailable; model selection cannot be saved" });
|
|
21890
22353
|
try {
|
|
21891
22354
|
const body = await readJsonBody(req);
|
|
@@ -21908,7 +22371,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
21908
22371
|
path: POOL_ACCOUNT_DISABLE_PATH,
|
|
21909
22372
|
handler: async (req, res) => {
|
|
21910
22373
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
21911
|
-
if (!
|
|
22374
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
21912
22375
|
if (deps.setAccountDisabled === void 0) return json(res, 503, { error: "settings service unavailable; the account switch cannot be saved" });
|
|
21913
22376
|
try {
|
|
21914
22377
|
const body = await readJsonBody(req);
|
|
@@ -21943,7 +22406,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
21943
22406
|
path: POOL_ACCOUNT_IGNORE_PATH,
|
|
21944
22407
|
handler: async (req, res) => {
|
|
21945
22408
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
21946
|
-
if (!
|
|
22409
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
21947
22410
|
if (deps.setAccountIgnored === void 0) return json(res, 503, { error: "settings service unavailable; the ignore list cannot be saved" });
|
|
21948
22411
|
try {
|
|
21949
22412
|
const body = await readJsonBody(req);
|
|
@@ -21966,7 +22429,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
21966
22429
|
path: POOL_ACCOUNT_PREFER_PATH,
|
|
21967
22430
|
handler: async (req, res) => {
|
|
21968
22431
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
21969
|
-
if (!
|
|
22432
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
21970
22433
|
if (deps.setPreferredAccount === void 0) return json(res, 503, { error: "the preferred account cannot be saved in this build" });
|
|
21971
22434
|
try {
|
|
21972
22435
|
const body = await readJsonBody(req);
|
|
@@ -21995,7 +22458,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
21995
22458
|
path: POOL_AUTOMATION_RUN_PATH,
|
|
21996
22459
|
handler: async (req, res) => {
|
|
21997
22460
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
21998
|
-
if (!
|
|
22461
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
21999
22462
|
if (deps.runAutomation === void 0) return json(res, 503, { error: "automation is not available in this build" });
|
|
22000
22463
|
try {
|
|
22001
22464
|
const run = parseAutomationRun(await readJsonBody(req));
|
|
@@ -22023,7 +22486,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22023
22486
|
path: POOL_CREDIT_RESERVE_PATH,
|
|
22024
22487
|
handler: async (req, res) => {
|
|
22025
22488
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22026
|
-
if (!
|
|
22489
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22027
22490
|
if (deps.setCreditReserve === void 0) return json(res, 503, { error: "settings service unavailable; the reserve cannot be saved" });
|
|
22028
22491
|
try {
|
|
22029
22492
|
const body = await readJsonBody(req);
|
|
@@ -22058,7 +22521,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22058
22521
|
path: POOL_AGGREGATE_PATH,
|
|
22059
22522
|
handler: async (req, res) => {
|
|
22060
22523
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
22061
|
-
if (!
|
|
22524
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22062
22525
|
if (deps.aggregateDocument === void 0) return json(res, 503, { error: "the aggregate lane is not available in this build" });
|
|
22063
22526
|
try {
|
|
22064
22527
|
json(res, 200, await deps.aggregateDocument());
|
|
@@ -22084,7 +22547,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22084
22547
|
path: POOL_AGGREGATE_SAVE_PATH,
|
|
22085
22548
|
handler: async (req, res) => {
|
|
22086
22549
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22087
|
-
if (!
|
|
22550
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22088
22551
|
if (deps.saveAggregate === void 0) return json(res, 503, { error: "the aggregate lane is not available in this build" });
|
|
22089
22552
|
try {
|
|
22090
22553
|
const patch = parseAggregatePatch(await readJsonBody(req));
|
|
@@ -22116,7 +22579,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22116
22579
|
path: POOL_AGGREGATE_TEST_PATH,
|
|
22117
22580
|
handler: async (req, res) => {
|
|
22118
22581
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22119
|
-
if (!
|
|
22582
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22120
22583
|
if (deps.probeAggregate === void 0) return json(res, 503, { error: "the aggregate lane is not available in this build" });
|
|
22121
22584
|
try {
|
|
22122
22585
|
const providerId = parseProviderId(await readJsonBody(req));
|
|
@@ -22145,7 +22608,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22145
22608
|
path: POOL_MODEL_PROBE_PATH,
|
|
22146
22609
|
handler: async (req, res) => {
|
|
22147
22610
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22148
|
-
if (!
|
|
22611
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22149
22612
|
if (deps.probeModel === void 0) return json(res, 503, { error: "the connectivity probe is not available in this build" });
|
|
22150
22613
|
const controller = new AbortController();
|
|
22151
22614
|
req.on("aborted", () => {
|
|
@@ -22193,7 +22656,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22193
22656
|
path: POOL_CHANNEL_MODELS_PATH,
|
|
22194
22657
|
handler: async (req, res) => {
|
|
22195
22658
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22196
|
-
if (!
|
|
22659
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22197
22660
|
if (deps.channelModels === void 0) return json(res, 503, { error: "the login channels are not available in this build" });
|
|
22198
22661
|
try {
|
|
22199
22662
|
const body = await readJsonBody(req);
|
|
@@ -22210,7 +22673,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22210
22673
|
path: POOL_AGGREGATE_DISCOVER_PATH,
|
|
22211
22674
|
handler: async (req, res) => {
|
|
22212
22675
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22213
|
-
if (!
|
|
22676
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22214
22677
|
if (deps.discoverAggregate === void 0) return json(res, 503, { error: "the aggregate lane is not available in this build" });
|
|
22215
22678
|
try {
|
|
22216
22679
|
const providerId = parseProviderId(await readJsonBody(req));
|
|
@@ -22226,7 +22689,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22226
22689
|
path: POOL_CHANNELS_PATH,
|
|
22227
22690
|
handler: (req, res) => {
|
|
22228
22691
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
22229
|
-
if (!
|
|
22692
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22230
22693
|
if (deps.channelsDocument === void 0) return json(res, 503, { error: "the login channels are not available in this build" });
|
|
22231
22694
|
try {
|
|
22232
22695
|
json(res, 200, deps.channelsDocument());
|
|
@@ -22248,7 +22711,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22248
22711
|
path: POOL_COMBOS_PATH,
|
|
22249
22712
|
handler: async (req, res) => {
|
|
22250
22713
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
22251
|
-
if (!
|
|
22714
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22252
22715
|
if (deps.combosDocument === void 0) return json(res, 503, { error: "the routing chains are not available in this build" });
|
|
22253
22716
|
try {
|
|
22254
22717
|
json(res, 200, await deps.combosDocument());
|
|
@@ -22269,7 +22732,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22269
22732
|
path: POOL_CREDITS_SPEND_PATH,
|
|
22270
22733
|
handler: async (req, res) => {
|
|
22271
22734
|
if (req.method !== "GET") return json(res, 405, { error: "method not allowed" });
|
|
22272
|
-
if (!
|
|
22735
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22273
22736
|
if (deps.creditsSpend === void 0) return json(res, 503, { error: "credit accounting is not available in this build" });
|
|
22274
22737
|
try {
|
|
22275
22738
|
const raw = new URL(req.url ?? "/", "http://127.0.0.1").searchParams.get("days");
|
|
@@ -22292,7 +22755,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22292
22755
|
path: POOL_COMBOS_SAVE_PATH,
|
|
22293
22756
|
handler: async (req, res) => {
|
|
22294
22757
|
if (req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22295
|
-
if (!
|
|
22758
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22296
22759
|
if (deps.saveCombos === void 0) return json(res, 503, { error: "the routing chains are not available in this build" });
|
|
22297
22760
|
try {
|
|
22298
22761
|
const body = await readJsonBody(req);
|
|
@@ -22318,7 +22781,7 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22318
22781
|
path: POOL_RELAY_PATH,
|
|
22319
22782
|
handler: async (req, res) => {
|
|
22320
22783
|
if (req.method !== "GET" && req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22321
|
-
if (!
|
|
22784
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22322
22785
|
if (deps.relayDocument === void 0 || deps.saveRelay === void 0) return json(res, 503, { error: "the LAN relay is not available in this build" });
|
|
22323
22786
|
try {
|
|
22324
22787
|
if (req.method === "GET") {
|
|
@@ -22332,7 +22795,32 @@ function registerPoolStatusRoute(ctx, deps) {
|
|
|
22332
22795
|
}
|
|
22333
22796
|
}
|
|
22334
22797
|
});
|
|
22798
|
+
/**
|
|
22799
|
+
* Is there a newer build?
|
|
22800
|
+
*
|
|
22801
|
+
* `POST` forces a fresh read — that is the panel's button, and a button that silently
|
|
22802
|
+
* returns a cached answer is indistinguishable from one that did nothing.
|
|
22803
|
+
*/
|
|
22804
|
+
const disposeUpdate = ctx.webServer.register({
|
|
22805
|
+
kind: "exact",
|
|
22806
|
+
path: POOL_UPDATE_CHECK_PATH,
|
|
22807
|
+
handler: async (req, res) => {
|
|
22808
|
+
if (req.method !== "GET" && req.method !== "POST") return json(res, 405, { error: "method not allowed" });
|
|
22809
|
+
if (!originTrusted(req, deps.trustedHosts?.() ?? [])) return json(res, 403, { error: "origin-not-trusted" });
|
|
22810
|
+
try {
|
|
22811
|
+
return json(res, 200, await checkForUpdate({ force: req.method === "POST" }));
|
|
22812
|
+
} catch (error) {
|
|
22813
|
+
return json(res, 200, {
|
|
22814
|
+
current: "unknown",
|
|
22815
|
+
newer: false,
|
|
22816
|
+
error: safeMessage(error),
|
|
22817
|
+
checkedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
22818
|
+
});
|
|
22819
|
+
}
|
|
22820
|
+
}
|
|
22821
|
+
});
|
|
22335
22822
|
return () => {
|
|
22823
|
+
disposeUpdate();
|
|
22336
22824
|
disposeRelay();
|
|
22337
22825
|
disposeCombosSave();
|
|
22338
22826
|
disposeCombos();
|
|
@@ -27289,6 +27777,26 @@ function apply(ctx, config = {}) {
|
|
|
27289
27777
|
rateOf
|
|
27290
27778
|
});
|
|
27291
27779
|
};
|
|
27780
|
+
/**
|
|
27781
|
+
* The authorities this deployment serves, read per request.
|
|
27782
|
+
*
|
|
27783
|
+
* `connection.trustedHosts` is what the host's own `/api` bridge trusts — Tailscale serve, a
|
|
27784
|
+
* LAN hostname, a reverse proxy. The plugin's routes used to accept loopback only, so behind
|
|
27785
|
+
* any of those the card displayed its data and refused every button (#164). Reading it fresh
|
|
27786
|
+
* means a configuration change reaches the next request without a restart.
|
|
27787
|
+
*
|
|
27788
|
+
* A missing service is not an error: it simply means nothing beyond loopback is trusted, which
|
|
27789
|
+
* is the desktop default.
|
|
27790
|
+
*/
|
|
27791
|
+
const trustedHosts = () => {
|
|
27792
|
+
try {
|
|
27793
|
+
const list = ctx.connection?.config?.trustedHosts;
|
|
27794
|
+
if (!Array.isArray(list)) return [];
|
|
27795
|
+
return list.filter((entry) => typeof entry === "string");
|
|
27796
|
+
} catch {
|
|
27797
|
+
return [];
|
|
27798
|
+
}
|
|
27799
|
+
};
|
|
27292
27800
|
ctx.inject(["webServer"], (webCtx) => {
|
|
27293
27801
|
/**
|
|
27294
27802
|
* Doubao's three routes.
|
|
@@ -27299,6 +27807,7 @@ function apply(ctx, config = {}) {
|
|
|
27299
27807
|
*/
|
|
27300
27808
|
registerDoubaoRoutes(webCtx, {
|
|
27301
27809
|
session: () => doubaoSession,
|
|
27810
|
+
trustedHosts,
|
|
27302
27811
|
save: (session) => {
|
|
27303
27812
|
adoptDoubaoSession(session);
|
|
27304
27813
|
writeStoredSession(session);
|
|
@@ -27311,6 +27820,7 @@ function apply(ctx, config = {}) {
|
|
|
27311
27820
|
});
|
|
27312
27821
|
return registerPoolStatusRoute(webCtx, {
|
|
27313
27822
|
pool: core.pool,
|
|
27823
|
+
trustedHosts,
|
|
27314
27824
|
catalogs: core.catalogs,
|
|
27315
27825
|
client: core.client,
|
|
27316
27826
|
usage: () => usageRecorder.summary(),
|
|
@@ -27627,6 +28137,7 @@ function apply(ctx, config = {}) {
|
|
|
27627
28137
|
let releaseAdapterGlobal;
|
|
27628
28138
|
let releaseAdapterAggregate;
|
|
27629
28139
|
let releaseAdapterFree;
|
|
28140
|
+
let releaseAdapterDoubao;
|
|
27630
28141
|
let releaseDirectory;
|
|
27631
28142
|
try {
|
|
27632
28143
|
/**
|
|
@@ -27657,6 +28168,14 @@ function apply(ctx, config = {}) {
|
|
|
27657
28168
|
releaseAdapterGlobal = detached;
|
|
27658
28169
|
releaseAdapterAggregate = ctx.llm.registerAdapter([XDPOOL_AGGREGATE_PROVIDER], adaptersByRegion.aggregate.adapter);
|
|
27659
28170
|
releaseAdapterFree = ctx.llm.registerAdapter([XDPOOL_FREE_PROVIDER], adaptersByRegion.free.adapter);
|
|
28171
|
+
/**
|
|
28172
|
+
* Doubao, registered the same way every other channel is.
|
|
28173
|
+
*
|
|
28174
|
+
* The lane over HTTP is not enough: the model PICKER reads the host's adapter
|
|
28175
|
+
* registry, so a signed-in Doubao account with ten models enabled appeared nowhere in
|
|
28176
|
+
* the list. This is the registration the pack performs for each of its own channels.
|
|
28177
|
+
*/
|
|
28178
|
+
releaseAdapterDoubao = ctx.llm.registerAdapter([DOUBAO_PROVIDER_ID], new DoubaoHostAdapter({ session: () => doubaoSession }));
|
|
27660
28179
|
} catch (error) {
|
|
27661
28180
|
ctx.logger.error("dsh-xdpool: adapter registration failed", error);
|
|
27662
28181
|
recordStartupNote(`adapters REJECTED: ${describeError(error)}`);
|
|
@@ -27664,6 +28183,7 @@ function apply(ctx, config = {}) {
|
|
|
27664
28183
|
releaseAdapterGlobal?.();
|
|
27665
28184
|
releaseAdapterAggregate?.();
|
|
27666
28185
|
releaseAdapterFree?.();
|
|
28186
|
+
releaseAdapterDoubao?.();
|
|
27667
28187
|
return;
|
|
27668
28188
|
}
|
|
27669
28189
|
try {
|
|
@@ -27734,6 +28254,7 @@ function apply(ctx, config = {}) {
|
|
|
27734
28254
|
releaseAdapterGlobal?.();
|
|
27735
28255
|
releaseAdapterAggregate?.();
|
|
27736
28256
|
releaseAdapterFree?.();
|
|
28257
|
+
releaseAdapterDoubao?.();
|
|
27737
28258
|
releaseDirectory?.();
|
|
27738
28259
|
};
|
|
27739
28260
|
try {
|