dsh-wtq-pool 1.0.51 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -98
- package/README.en.md +28 -30
- package/README.md +29 -31
- package/lib/client.js +40 -8
- package/lib/index.d.ts +0 -2
- package/lib/index.js +2087 -578
- package/package.json +9 -12
- package/renderer/index.html +14 -23
- package/renderer/panel.css +16 -5
- package/renderer/panel.js +188 -132
package/lib/index.js
CHANGED
|
@@ -13,7 +13,7 @@ import { PiAiAdapter } from "@deepseek-ai/dsh-llm-pi-ai";
|
|
|
13
13
|
import z from "@deepseek-ai/schemastery";
|
|
14
14
|
import { createServer } from "node:http";
|
|
15
15
|
import * as net from "node:net";
|
|
16
|
-
import { isIP } from "node:net";
|
|
16
|
+
import { BlockList, isIP } from "node:net";
|
|
17
17
|
import { constants, createCipheriv, createDecipheriv, createHash, publicEncrypt, randomBytes, randomUUID } from "node:crypto";
|
|
18
18
|
import * as fs$1 from "node:fs";
|
|
19
19
|
import { statSync } from "node:fs";
|
|
@@ -25,11 +25,10 @@ import { execFile, execFileSync, spawn } from "node:child_process";
|
|
|
25
25
|
*
|
|
26
26
|
* One constant rather than a literal per call site, because those modules are
|
|
27
27
|
* synced byte for byte between the two builds and a literal is what a sync
|
|
28
|
-
* overwrites.
|
|
29
|
-
*
|
|
30
|
-
* builds do not copy.
|
|
28
|
+
* overwrites. Both builds use the same product name, so the host banner and
|
|
29
|
+
* these lines agree.
|
|
31
30
|
*/
|
|
32
|
-
const LOG_PREFIX = "
|
|
31
|
+
const LOG_PREFIX = "wtq-pool";
|
|
33
32
|
//#endregion
|
|
34
33
|
//#region src/loopback.ts
|
|
35
34
|
const LOOPBACK_HOSTS = /* @__PURE__ */ new Set([
|
|
@@ -55,6 +54,21 @@ function hostIsLoopback(host) {
|
|
|
55
54
|
if (host === void 0 || host.trim() === "") return false;
|
|
56
55
|
return LOOPBACK_HOSTS.has(hostnameOfHost(host));
|
|
57
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* The pathname of a request target.
|
|
59
|
+
*
|
|
60
|
+
* `req.url` keeps the query string and may be an absolute-form proxy target;
|
|
61
|
+
* routing compared it whole, so `?api-version=…` or `GET http://host/v1/…`
|
|
62
|
+
* answered 404 on every route.
|
|
63
|
+
*/
|
|
64
|
+
function requestPathname(raw) {
|
|
65
|
+
if (raw === void 0 || raw === "") return "/";
|
|
66
|
+
try {
|
|
67
|
+
return new URL(raw, "http://127.0.0.1").pathname;
|
|
68
|
+
} catch {
|
|
69
|
+
return "/";
|
|
70
|
+
}
|
|
71
|
+
}
|
|
58
72
|
/** A present Origin must be loopback; non-browser clients send none and pass. */
|
|
59
73
|
function originIsLoopback(origin) {
|
|
60
74
|
if (origin === void 0 || origin.trim() === "") return true;
|
|
@@ -169,18 +183,22 @@ function redactSecrets(text) {
|
|
|
169
183
|
/**
|
|
170
184
|
* The vendor's channel check, answered as what it is.
|
|
171
185
|
*
|
|
172
|
-
* WorkBuddy's upstream
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
186
|
+
* WorkBuddy's upstream inspects the system and assistant turns for wording that
|
|
187
|
+
* belongs to a client it does not serve, and answers business code 11128. The
|
|
188
|
+
* plugin already moves the system prompt into the first user turn and retries
|
|
189
|
+
* once (`chatStream`); a second refusal means the blocked wording is somewhere
|
|
190
|
+
* that move does not reach, or the host changed its screen again. Every account
|
|
191
|
+
* of that region answers the same way, so rotation cannot clear it, and the
|
|
192
|
+
* vendor body is a wall of mixed Chinese and English tips that names no cause a
|
|
193
|
+
* user can act on. The sentence below says what happened and what does work.
|
|
180
194
|
*/
|
|
181
195
|
function unapprovedChannelRefusal(text) {
|
|
182
|
-
if (!/"code"\s*:\s*11128\b
|
|
183
|
-
return
|
|
196
|
+
if (!/"code"\s*:\s*"?11128\b/u.test(text) && !/\bcode 11128\b/u.test(text)) return void 0;
|
|
197
|
+
return channelCheckRefusal();
|
|
198
|
+
}
|
|
199
|
+
/** The sentence a refused prompt is answered with, whatever shape the body has. */
|
|
200
|
+
function channelCheckRefusal() {
|
|
201
|
+
return "the WorkBuddy host refused this conversation at its client check (11128). That screen reads system and assistant turns for wording that belongs to a client it does not serve, so remove client-identifying text from the conversation, start a new one, or use Trae or Qoder for this prompt.";
|
|
184
202
|
}
|
|
185
203
|
/**
|
|
186
204
|
* The `finish_reason` one turn is reported with.
|
|
@@ -345,12 +363,12 @@ function readRequestBody(req, limitBytes = REQUEST_BODY_LIMIT) {
|
|
|
345
363
|
*/
|
|
346
364
|
async function answerRequest(res, handle, log, answerFailure) {
|
|
347
365
|
res.on?.("error", (error) => {
|
|
348
|
-
log?.warn?.(
|
|
366
|
+
log?.warn?.("wtq-pool: response socket closed mid-write", error);
|
|
349
367
|
});
|
|
350
368
|
try {
|
|
351
369
|
await handle();
|
|
352
370
|
} catch (error) {
|
|
353
|
-
log?.warn?.(
|
|
371
|
+
log?.warn?.("wtq-pool: request failed", error);
|
|
354
372
|
const answer = answerFailure ?? ((target, caught) => {
|
|
355
373
|
if (caught instanceof BodyTooLargeError) writeOpenAIError(target, 413, "request_too_large", String(caught.message));
|
|
356
374
|
else writeOpenAIError(target, 500, "internal", String(caught));
|
|
@@ -359,7 +377,7 @@ async function answerRequest(res, handle, log, answerFailure) {
|
|
|
359
377
|
if (!res.headersSent) answer(res, error);
|
|
360
378
|
else res.end();
|
|
361
379
|
} catch (writeError) {
|
|
362
|
-
log?.warn?.(
|
|
380
|
+
log?.warn?.("wtq-pool: could not answer a failed request", writeError);
|
|
363
381
|
}
|
|
364
382
|
}
|
|
365
383
|
}
|
|
@@ -411,6 +429,27 @@ function relayStreamOf(response) {
|
|
|
411
429
|
return response.body;
|
|
412
430
|
}
|
|
413
431
|
/**
|
|
432
|
+
* Write one relayed chunk, waiting while the client's socket is full.
|
|
433
|
+
*
|
|
434
|
+
* A relay that ignores the false return of `res.write` keeps reading the
|
|
435
|
+
* upstream at full speed and parks the whole answer inside the plugin process,
|
|
436
|
+
* competing with the gateways and the panel for one event loop.
|
|
437
|
+
*/
|
|
438
|
+
async function writeChunk(res, chunk) {
|
|
439
|
+
if (res.destroyed || res.writableEnded) return;
|
|
440
|
+
if (res.write(Buffer.from(chunk))) return;
|
|
441
|
+
if (res.destroyed) return;
|
|
442
|
+
await new Promise((resolve) => {
|
|
443
|
+
const wake = () => {
|
|
444
|
+
res.off("drain", wake);
|
|
445
|
+
res.off("close", wake);
|
|
446
|
+
resolve();
|
|
447
|
+
};
|
|
448
|
+
res.once("drain", wake);
|
|
449
|
+
res.once("close", wake);
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
414
453
|
* Relay one web stream to a Node response, honouring backpressure.
|
|
415
454
|
*
|
|
416
455
|
* Written out rather than `Readable.fromWeb` + `pipe`: that adapter stops
|
|
@@ -438,19 +477,8 @@ async function pumpToResponse(source, res) {
|
|
|
438
477
|
for (;;) {
|
|
439
478
|
const next = await reader.read();
|
|
440
479
|
if (next.done) break;
|
|
480
|
+
await writeChunk(res, next.value);
|
|
441
481
|
if (res.destroyed || res.writableEnded) break;
|
|
442
|
-
if (!res.write(Buffer.from(next.value))) {
|
|
443
|
-
if (res.destroyed) break;
|
|
444
|
-
await new Promise((resolve) => {
|
|
445
|
-
const wake = () => {
|
|
446
|
-
res.off("drain", wake);
|
|
447
|
-
res.off("close", wake);
|
|
448
|
-
resolve();
|
|
449
|
-
};
|
|
450
|
-
res.once("drain", wake);
|
|
451
|
-
res.once("close", wake);
|
|
452
|
-
});
|
|
453
|
-
}
|
|
454
482
|
}
|
|
455
483
|
} finally {
|
|
456
484
|
res.off("close", cancel);
|
|
@@ -557,7 +585,7 @@ function createLoopbackServer(options) {
|
|
|
557
585
|
});
|
|
558
586
|
server.listen(options.port ?? 0, "127.0.0.1");
|
|
559
587
|
server.on("error", (error) => {
|
|
560
|
-
options.logger?.warn?.(
|
|
588
|
+
options.logger?.warn?.("wtq-pool: " + options.label + " server error", error);
|
|
561
589
|
});
|
|
562
590
|
return {
|
|
563
591
|
ready,
|
|
@@ -758,7 +786,6 @@ const CHANNELS = /* @__PURE__ */ new Set([
|
|
|
758
786
|
"xd.distribution",
|
|
759
787
|
"xd.setAutoCheckin",
|
|
760
788
|
"xd.setComposerPoints",
|
|
761
|
-
"xd.setRewriteClientName",
|
|
762
789
|
"xd.setCreditReserve",
|
|
763
790
|
"xd.setRegionEnabled",
|
|
764
791
|
"xd.setSource",
|
|
@@ -796,63 +823,18 @@ const PANEL_CSP = [
|
|
|
796
823
|
"base-uri 'none'",
|
|
797
824
|
"form-action 'none'"
|
|
798
825
|
].join("; ");
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
* them the client's stack printed. Returns undefined for anything malformed.
|
|
805
|
-
*/
|
|
806
|
-
function ipv6Groups(address) {
|
|
807
|
-
const halves = address.toLowerCase().split("::");
|
|
808
|
-
if (halves.length > 2) return void 0;
|
|
809
|
-
const expand = (side) => {
|
|
810
|
-
if (side === void 0 || side === "") return [];
|
|
811
|
-
const groups = side.split(":");
|
|
812
|
-
const last = groups[groups.length - 1] ?? "";
|
|
813
|
-
if (!last.includes(".")) return groups;
|
|
814
|
-
const octets = last.split(".");
|
|
815
|
-
if (octets.length !== 4) return void 0;
|
|
816
|
-
if (!octets.every((octet) => /^\d{1,3}$/.test(octet) && Number(octet) <= 255)) return void 0;
|
|
817
|
-
const high = (Number(octets[0]) << 8) + Number(octets[1]);
|
|
818
|
-
const low = (Number(octets[2]) << 8) + Number(octets[3]);
|
|
819
|
-
return [
|
|
820
|
-
...groups.slice(0, -1),
|
|
821
|
-
high.toString(16),
|
|
822
|
-
low.toString(16)
|
|
823
|
-
];
|
|
824
|
-
};
|
|
825
|
-
const head = expand(halves[0]);
|
|
826
|
-
const tail = halves.length === 2 ? expand(halves[1]) : [];
|
|
827
|
-
if (head === void 0 || tail === void 0) return void 0;
|
|
828
|
-
const missing = 8 - head.length - tail.length;
|
|
829
|
-
if (missing < 0) return void 0;
|
|
830
|
-
const values = [
|
|
831
|
-
...head,
|
|
832
|
-
...Array(missing).fill("0"),
|
|
833
|
-
...tail
|
|
834
|
-
].map((group) => Number.parseInt(group, 16));
|
|
835
|
-
return values.length === 8 && values.every((value) => Number.isInteger(value)) ? values : void 0;
|
|
836
|
-
}
|
|
837
|
-
/**
|
|
838
|
-
* Whether the request's peer is this machine.
|
|
839
|
-
*
|
|
840
|
-
* Read off the address's own groups rather than off Node's serialised form of
|
|
841
|
-
* it: the regex this replaces matched `[::ffff:7f00:1]`, which is what the
|
|
842
|
-
* current Node prints for `::ffff:127.0.0.1`, and a check that depends on a
|
|
843
|
-
* serialisation detail is one Node release away from refusing a legitimate
|
|
844
|
-
* peer. Every address in 127/8, `::1`, and the `::ffff:127/8` mapping of that
|
|
845
|
-
* range is this machine; nothing else is.
|
|
846
|
-
*/
|
|
826
|
+
const LOOPBACK = new BlockList();
|
|
827
|
+
LOOPBACK.addSubnet("127.0.0.0", 8, "ipv4");
|
|
828
|
+
LOOPBACK.addAddress("::1", "ipv6");
|
|
829
|
+
LOOPBACK.addSubnet("::ffff:127.0.0.0", 104, "ipv6");
|
|
830
|
+
/** Whether the request's peer is this machine. */
|
|
847
831
|
function peerIsLoopback(req) {
|
|
848
832
|
const address = req.socket.remoteAddress;
|
|
849
833
|
if (address === void 0) return false;
|
|
850
|
-
|
|
851
|
-
if (
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
if (groups.slice(0, 6).every((value) => value === 0) && groups[6] === 0 && groups[7] === 1) return true;
|
|
855
|
-
return groups.slice(0, 5).every((value) => value === 0) && groups[5] === 65535 && (groups[6] ?? 0) >> 8 === 127;
|
|
834
|
+
const family = isIP(address);
|
|
835
|
+
if (family === 4) return LOOPBACK.check(address, "ipv4");
|
|
836
|
+
if (family === 6) return LOOPBACK.check(address, "ipv6");
|
|
837
|
+
return false;
|
|
856
838
|
}
|
|
857
839
|
/**
|
|
858
840
|
* Whether the request's own headers say the page asking is this origin.
|
|
@@ -1194,21 +1176,34 @@ const CN_CHAT_BASE = "https://copilot.tencent.com";
|
|
|
1194
1176
|
const CN_BILLING_BASE = "https://www.codebuddy.cn";
|
|
1195
1177
|
/** Global base for `workbuddy.ai` logins. */
|
|
1196
1178
|
const GLOBAL_BASE$1 = "https://www.workbuddy.ai";
|
|
1197
|
-
/** Chat UA: the official desktop client's three-segment spelling. */
|
|
1198
|
-
const CLIENT_UA = "WorkBuddy/5.5.4 WorkBuddy/5.5.4 CLI/2.137.1";
|
|
1199
1179
|
/**
|
|
1200
|
-
*
|
|
1180
|
+
* Client versions the two gateways see, and the headers that carry them.
|
|
1201
1181
|
*
|
|
1202
|
-
*
|
|
1203
|
-
*
|
|
1182
|
+
* CN desktop 5.9.3 (2026-10-27) sends `X-Agent-Runtime: local-agent` and the
|
|
1183
|
+
* same `X-Stainless-*` family as the bundled CLI. The international desktop is
|
|
1184
|
+
* still 5.8.4. Keeping one literal for both was one version behind on CN and
|
|
1185
|
+
* produced a combination no installed client sends.
|
|
1204
1186
|
*/
|
|
1205
|
-
const
|
|
1187
|
+
const CN_CLIENT_VERSION = "5.9.3";
|
|
1188
|
+
const CN_CLI_VERSION = "2.165.0";
|
|
1189
|
+
const GLOBAL_CLIENT_VERSION = "5.8.4";
|
|
1190
|
+
const GLOBAL_CLI_VERSION = "2.161.0";
|
|
1191
|
+
/** CN chat UA: the desktop client's three-segment spelling. */
|
|
1192
|
+
const CN_CHAT_UA = `WorkBuddy/${CN_CLIENT_VERSION} WorkBuddy/${CN_CLIENT_VERSION} CLI/${CN_CLI_VERSION}`;
|
|
1193
|
+
/** CN desktop UA, used by the endpoints the desktop process calls directly. */
|
|
1194
|
+
const CN_DESKTOP_UA = `WorkBuddy/${CN_CLIENT_VERSION}`;
|
|
1206
1195
|
/**
|
|
1207
|
-
*
|
|
1208
|
-
*
|
|
1209
|
-
* is why the international catalog must be read with the desktop spelling.
|
|
1196
|
+
* The international desktop client's UA. The `workbuddy-ai/` segment only
|
|
1197
|
+
* exists on that gateway, and the CN spelling gets the CLI roster there.
|
|
1210
1198
|
*/
|
|
1211
|
-
const
|
|
1199
|
+
const GLOBAL_CHAT_UA = `WorkBuddy/${GLOBAL_CLIENT_VERSION} workbuddy-ai/${GLOBAL_CLIENT_VERSION} CLI/${GLOBAL_CLI_VERSION}`;
|
|
1200
|
+
/**
|
|
1201
|
+
* Desktop app UA for the global gateway. It serves its product config only to
|
|
1202
|
+
* this client channel: the CLI UA gets a truncated roster (or an HTTP 500),
|
|
1203
|
+
* which is why the international catalog must be read with the desktop
|
|
1204
|
+
* spelling.
|
|
1205
|
+
*/
|
|
1206
|
+
const GLOBAL_DESKTOP_UA = `WorkBuddy/${GLOBAL_CLIENT_VERSION}`;
|
|
1212
1207
|
/** CN model catalog. */
|
|
1213
1208
|
const MODELS_CATALOG_PATH = "/v2/enterprises/personal/models";
|
|
1214
1209
|
/** Global product config, which carries the international model roster. */
|
|
@@ -1395,6 +1390,11 @@ const HARD_CREDIT_MARKERS = [
|
|
|
1395
1390
|
"额度用尽",
|
|
1396
1391
|
"没有积分"
|
|
1397
1392
|
];
|
|
1393
|
+
/** Whether a text names an exhausted balance rather than a rate limit. */
|
|
1394
|
+
function isHardCreditText(body) {
|
|
1395
|
+
const lower = body.toLowerCase();
|
|
1396
|
+
return HARD_CREDIT_MARKERS.some((marker) => lower.includes(marker.toLowerCase()) || body.includes(marker));
|
|
1397
|
+
}
|
|
1398
1398
|
/**
|
|
1399
1399
|
* Session-invalidation markers that mean "sign in again in the WorkBuddy app".
|
|
1400
1400
|
*
|
|
@@ -1405,6 +1405,41 @@ const HARD_CREDIT_MARKERS = [
|
|
|
1405
1405
|
*/
|
|
1406
1406
|
const SESSION_DEAD_PATTERN = /Offline user session not found|"code"\s*:\s*"?12153\b/u;
|
|
1407
1407
|
/**
|
|
1408
|
+
* The plan gate: the account is fine, the model is not included in it.
|
|
1409
|
+
*
|
|
1410
|
+
* Matched as an envelope value rather than a bare substring, for the same
|
|
1411
|
+
* reason as the session marker: `11158` turns up inside ids, and reclassifying
|
|
1412
|
+
* a request error as a plan gate would cool a model that is actually fine.
|
|
1413
|
+
*/
|
|
1414
|
+
const MODEL_TIER_PATTERN = /"code"\s*:\s*"?11158\b/u;
|
|
1415
|
+
/**
|
|
1416
|
+
* The prompt-screen refusal: `11128`.
|
|
1417
|
+
*
|
|
1418
|
+
* The host returns it for a system or assistant turn whose wording belongs to a
|
|
1419
|
+
* client it does not serve. It is a property of the request body, not the
|
|
1420
|
+
* account, so the rotation must not spend another sign-in on it; `chatStream`
|
|
1421
|
+
* moves the system prompt into the first user turn and retries once instead.
|
|
1422
|
+
*/
|
|
1423
|
+
const CHANNEL_CHECK_PATTERN = /"code"\s*:\s*"?11128\b/u;
|
|
1424
|
+
/**
|
|
1425
|
+
* The vendor's account-level restriction: `11140`, "request illegal".
|
|
1426
|
+
*
|
|
1427
|
+
* The body names the account ("your account has been restricted due to unusual
|
|
1428
|
+
* activity") rather than the request, and no retry with the same sign-in can
|
|
1429
|
+
* clear it. Reading it as a plain client error answered 400 and stopped the
|
|
1430
|
+
* rotation, so a second account that could serve the turn was never tried.
|
|
1431
|
+
*/
|
|
1432
|
+
const ACCOUNT_RESTRICTED_PATTERN = /"code"\s*:\s*"?11140\b/u;
|
|
1433
|
+
/**
|
|
1434
|
+
* The upstream's rate-limit marker.
|
|
1435
|
+
*
|
|
1436
|
+
* The same envelope is returned with HTTP 200 by JSON endpoints and by the
|
|
1437
|
+
* chat screen, so the status alone cannot decide it. The code is matched as an
|
|
1438
|
+
* envelope value rather than as a bare number for the reason above; the text
|
|
1439
|
+
* forms cover the older deployments that answer with prose only.
|
|
1440
|
+
*/
|
|
1441
|
+
const SOFT_RATE_PATTERN = /"code"\s*:\s*"?6004\b|soft_rate|频率限制/u;
|
|
1442
|
+
/**
|
|
1408
1443
|
* Hosts the international product answers on, once each has been stripped of a
|
|
1409
1444
|
* leading label. The WorkBuddy AI desktop app signs in at `workbuddy.ai` (and
|
|
1410
1445
|
* the desktop client itself lists `workbuddy.cc` alongside it); the CodeBuddy
|
|
@@ -1424,6 +1459,35 @@ function regionOf(domain) {
|
|
|
1424
1459
|
for (const host of GLOBAL_HOSTS) if (lowered === host || lowered.endsWith(`.${host}`)) return "global";
|
|
1425
1460
|
return "cn";
|
|
1426
1461
|
}
|
|
1462
|
+
/** The client version that gateway expects in `X-IDE-Version`. */
|
|
1463
|
+
function clientVersion(region) {
|
|
1464
|
+
return region === "global" ? GLOBAL_CLIENT_VERSION : CN_CLIENT_VERSION;
|
|
1465
|
+
}
|
|
1466
|
+
/** The full chat UA for the credential's gateway. */
|
|
1467
|
+
function chatUa(region) {
|
|
1468
|
+
return region === "global" ? GLOBAL_CHAT_UA : CN_CHAT_UA;
|
|
1469
|
+
}
|
|
1470
|
+
/** The desktop-process UA for endpoints outside the chat path. */
|
|
1471
|
+
function desktopUa(region) {
|
|
1472
|
+
return region === "global" ? GLOBAL_DESKTOP_UA : CN_DESKTOP_UA;
|
|
1473
|
+
}
|
|
1474
|
+
/**
|
|
1475
|
+
* The OpenAI Node SDK's fingerprint headers, as the current desktop client sends
|
|
1476
|
+
* them. OS, architecture and runtime version follow the host so a macOS or Linux
|
|
1477
|
+
* install does not claim to be a Windows client; the rest mirror the bundled CLI.
|
|
1478
|
+
*/
|
|
1479
|
+
function stainlessHeaders() {
|
|
1480
|
+
return {
|
|
1481
|
+
"x-stainless-lang": "js",
|
|
1482
|
+
"x-stainless-package-version": "6.25.0",
|
|
1483
|
+
"x-stainless-os": process.platform === "win32" ? "Windows" : process.platform === "darwin" ? "MacOS" : "Linux",
|
|
1484
|
+
"x-stainless-arch": process.arch,
|
|
1485
|
+
"x-stainless-runtime": "node",
|
|
1486
|
+
"x-stainless-runtime-version": process.version,
|
|
1487
|
+
"x-stainless-retry-count": "0",
|
|
1488
|
+
"x-stainless-timeout": "1200"
|
|
1489
|
+
};
|
|
1490
|
+
}
|
|
1427
1491
|
/**
|
|
1428
1492
|
* Gateway for a global credential.
|
|
1429
1493
|
*
|
|
@@ -1450,8 +1514,9 @@ function commonHeaders(credential) {
|
|
|
1450
1514
|
"X-Requested-With": "XMLHttpRequest",
|
|
1451
1515
|
"Origin": billingBase(credential),
|
|
1452
1516
|
"Referer": `${billingBase(credential)}/`,
|
|
1453
|
-
"User-Agent":
|
|
1454
|
-
"X-CodeBuddy-Request": "1"
|
|
1517
|
+
"User-Agent": desktopUa(regionOf(credential.domain)),
|
|
1518
|
+
"X-CodeBuddy-Request": "1",
|
|
1519
|
+
"Connection": "close"
|
|
1455
1520
|
};
|
|
1456
1521
|
}
|
|
1457
1522
|
/**
|
|
@@ -1475,7 +1540,7 @@ function commonHeaders(credential) {
|
|
|
1475
1540
|
* one - so there is nothing to gain from deriving it, and a stable value would
|
|
1476
1541
|
* be one more thing for the channel check to notice.
|
|
1477
1542
|
*/
|
|
1478
|
-
function conversationHeaders() {
|
|
1543
|
+
function conversationHeaders(region) {
|
|
1479
1544
|
const turn = randomUUID().replace(/-/g, "");
|
|
1480
1545
|
const message = randomUUID().replace(/-/g, "");
|
|
1481
1546
|
const traceId = randomUUID().replace(/-/g, "");
|
|
@@ -1489,7 +1554,7 @@ function conversationHeaders() {
|
|
|
1489
1554
|
"X-Agent-Intent": "craft",
|
|
1490
1555
|
"X-Agent-Purpose": "conversation",
|
|
1491
1556
|
"X-Agent-Type": "main",
|
|
1492
|
-
"X-IDE-Version":
|
|
1557
|
+
"X-IDE-Version": clientVersion(region),
|
|
1493
1558
|
"X-Private-Data": "true",
|
|
1494
1559
|
"X-Trace-ID": traceId,
|
|
1495
1560
|
"traceparent": `00-${traceId}-${spanId}-01`,
|
|
@@ -1501,16 +1566,19 @@ function conversationHeaders() {
|
|
|
1501
1566
|
}
|
|
1502
1567
|
/** Chat request headers, including the X-No-* conventions the official CLI uses. */
|
|
1503
1568
|
function chatHeaders(credential) {
|
|
1569
|
+
const region = regionOf(credential.domain);
|
|
1504
1570
|
return {
|
|
1505
1571
|
...commonHeaders(credential),
|
|
1506
|
-
...conversationHeaders(),
|
|
1572
|
+
...conversationHeaders(region),
|
|
1573
|
+
...stainlessHeaders(),
|
|
1507
1574
|
"Content-Type": "application/json",
|
|
1508
1575
|
"Authorization": `Bearer ${credential.accessToken}`,
|
|
1509
1576
|
...credential.uid === "" || credential.uid === void 0 ? { "X-No-User-Id": "1" } : { "X-User-Id": credential.uid },
|
|
1510
1577
|
...credential.enterpriseId === void 0 || credential.enterpriseId === "" ? { "X-No-Enterprise-Id": "1" } : { "X-Enterprise-Id": credential.enterpriseId },
|
|
1511
1578
|
...credential.domain === "" ? { "X-No-Department-Info": "1" } : { "X-Domain": credential.domain },
|
|
1512
1579
|
"X-Product": "SaaS",
|
|
1513
|
-
...
|
|
1580
|
+
...region === "cn" ? { "X-Agent-Runtime": "local-agent" } : {},
|
|
1581
|
+
"User-Agent": chatUa(region)
|
|
1514
1582
|
};
|
|
1515
1583
|
}
|
|
1516
1584
|
/** Refresh-endpoint headers; X-Refresh-Token appears here and nowhere else. */
|
|
@@ -1529,7 +1597,9 @@ function billingHeaders(credential) {
|
|
|
1529
1597
|
"Authorization": `Bearer ${credential.accessToken}`,
|
|
1530
1598
|
"Accept": "application/json",
|
|
1531
1599
|
"Content-Type": "application/json",
|
|
1532
|
-
"X-CodeBuddy-Request": "1"
|
|
1600
|
+
"X-CodeBuddy-Request": "1",
|
|
1601
|
+
"User-Agent": desktopUa(regionOf(credential.domain)),
|
|
1602
|
+
"Connection": "close"
|
|
1533
1603
|
};
|
|
1534
1604
|
if (credential.uid !== "" && credential.uid !== void 0) headers["X-User-Id"] = credential.uid;
|
|
1535
1605
|
if (credential.enterpriseId !== void 0 && credential.enterpriseId !== "") {
|
|
@@ -1579,8 +1649,11 @@ function gatewayRejectionText(status) {
|
|
|
1579
1649
|
* Never rejects: a read that fails keeps whatever arrived, which is usually the
|
|
1580
1650
|
* sentence the client needs. Callers on the error path used to let a body that
|
|
1581
1651
|
* died mid-read replace the upstream's status with a bare abort error.
|
|
1652
|
+
*
|
|
1653
|
+
* `timeoutMs` is a seam for the check: the shipped deadline is ten seconds, and
|
|
1654
|
+
* the case that proves the reader is cancelled should not sit it out.
|
|
1582
1655
|
*/
|
|
1583
|
-
async function readErrorBody(response, limitBytes, signal) {
|
|
1656
|
+
async function readErrorBody(response, limitBytes, signal, timeoutMs = ERROR_BODY_TIMEOUT_MS) {
|
|
1584
1657
|
const body = response.body;
|
|
1585
1658
|
if (body === null) return "";
|
|
1586
1659
|
const reader = body.getReader();
|
|
@@ -1591,7 +1664,7 @@ async function readErrorBody(response, limitBytes, signal) {
|
|
|
1591
1664
|
};
|
|
1592
1665
|
const deadline = setTimeout(() => {
|
|
1593
1666
|
abandon("upstream error body read timed out");
|
|
1594
|
-
},
|
|
1667
|
+
}, timeoutMs);
|
|
1595
1668
|
deadline.unref?.();
|
|
1596
1669
|
const onAbort = () => {
|
|
1597
1670
|
abandon("upstream error body read cancelled by the caller");
|
|
@@ -1619,7 +1692,7 @@ async function readErrorBody(response, limitBytes, signal) {
|
|
|
1619
1692
|
}
|
|
1620
1693
|
async function readEnvelope(response) {
|
|
1621
1694
|
const text = await response.text();
|
|
1622
|
-
if (isGatewayHtmlRejection(response.status, text)) throw new Error(gatewayRejectionText(response.status));
|
|
1695
|
+
if (isGatewayHtmlRejection(response.status, text)) throw Object.assign(new Error(gatewayRejectionText(response.status)), { kind: classifyUpstreamError(response.status, text) });
|
|
1623
1696
|
let parsed;
|
|
1624
1697
|
try {
|
|
1625
1698
|
parsed = JSON.parse(text);
|
|
@@ -1628,16 +1701,32 @@ async function readEnvelope(response) {
|
|
|
1628
1701
|
}
|
|
1629
1702
|
if (typeof parsed !== "object" || parsed === null) throw new Error(`workbuddy upstream returned an unexpected document (http ${response.status})`);
|
|
1630
1703
|
const document = parsed;
|
|
1704
|
+
const rawCode = document["code"];
|
|
1631
1705
|
return {
|
|
1632
|
-
code: typeof
|
|
1706
|
+
code: typeof rawCode === "number" && Number.isFinite(rawCode) ? rawCode : typeof rawCode === "string" && /^-?\d+$/u.test(rawCode.trim()) ? Number(rawCode.trim()) : 0,
|
|
1633
1707
|
msg: typeof document["msg"] === "string" ? document["msg"] : "",
|
|
1634
1708
|
data: "data" in document ? document["data"] : void 0
|
|
1635
1709
|
};
|
|
1636
1710
|
}
|
|
1637
1711
|
/** Fail an envelope whose business code is non-zero, classified like HTTP errors. */
|
|
1638
1712
|
function envelopeError(status, envelope) {
|
|
1639
|
-
const kind = classifyUpstreamError(status,
|
|
1640
|
-
|
|
1713
|
+
const kind = classifyUpstreamError(status, JSON.stringify({
|
|
1714
|
+
code: envelope.code,
|
|
1715
|
+
msg: envelope.msg
|
|
1716
|
+
}));
|
|
1717
|
+
return Object.assign(/* @__PURE__ */ new Error(`workbuddy upstream ${kind} (http ${status}): ${redactSecrets(envelope.msg).slice(0, 160)}`), { kind });
|
|
1718
|
+
}
|
|
1719
|
+
/**
|
|
1720
|
+
* The classified kind of an upstream failure, for a caller holding the error.
|
|
1721
|
+
*
|
|
1722
|
+
* `chatStream` answers with its kind in the result; every JSON endpoint throws,
|
|
1723
|
+
* and a caller that must tell a refused sign-in from a rate limit would
|
|
1724
|
+
* otherwise match on the message text. Anything without a kind - a local fault,
|
|
1725
|
+
* a bug - reads as undefined rather than being guessed at.
|
|
1726
|
+
*/
|
|
1727
|
+
function upstreamErrorKind(error) {
|
|
1728
|
+
const kind = error?.kind;
|
|
1729
|
+
return typeof kind === "string" ? kind : void 0;
|
|
1641
1730
|
}
|
|
1642
1731
|
/**
|
|
1643
1732
|
* Classify an upstream failure from its HTTP status and body excerpt.
|
|
@@ -1650,22 +1739,31 @@ function envelopeError(status, envelope) {
|
|
|
1650
1739
|
* fires on any other status, or a rate limit whose text happens to carry
|
|
1651
1740
|
* the code would rotate without cooling the account.
|
|
1652
1741
|
*
|
|
1653
|
-
* The credit markers are read
|
|
1654
|
-
* "this account cannot pay": 402
|
|
1655
|
-
* REQUEST, and its body routinely quotes the
|
|
1656
|
-
* matching there turned a malformed body into a
|
|
1657
|
-
* 402 that named the wrong problem.
|
|
1742
|
+
* The credit markers are read on the statuses the upstream itself uses for
|
|
1743
|
+
* "this account cannot pay": 402, 403, and an HTTP 200 business envelope. A
|
|
1744
|
+
* 400 or 422 is a verdict on the REQUEST, and its body routinely quotes the
|
|
1745
|
+
* quota it measured against, so matching there turned a malformed body into a
|
|
1746
|
+
* whole-pool rotation and a 402 that named the wrong problem.
|
|
1658
1747
|
*/
|
|
1659
1748
|
function classifyUpstreamError(status, body) {
|
|
1660
1749
|
if (status === 402) return "hard_credit";
|
|
1661
1750
|
if (status === 429) return "soft_rate";
|
|
1751
|
+
if (status === 200) {
|
|
1752
|
+
if (ACCOUNT_RESTRICTED_PATTERN.test(body)) return "account_restricted";
|
|
1753
|
+
if (MODEL_TIER_PATTERN.test(body)) return "model_tier";
|
|
1754
|
+
if (SESSION_DEAD_PATTERN.test(body)) return "session_dead";
|
|
1755
|
+
if (SOFT_RATE_PATTERN.test(body)) return "soft_rate";
|
|
1756
|
+
if (isHardCreditText(body)) return "hard_credit";
|
|
1757
|
+
return "client";
|
|
1758
|
+
}
|
|
1759
|
+
if ((status === 400 || status === 403 || status === 422) && ACCOUNT_RESTRICTED_PATTERN.test(body)) return "account_restricted";
|
|
1760
|
+
if ((status === 400 || status === 403 || status === 422) && MODEL_TIER_PATTERN.test(body)) return "model_tier";
|
|
1662
1761
|
if ((status === 401 || status === 403) && SESSION_DEAD_PATTERN.test(body)) return "session_dead";
|
|
1663
1762
|
if (status === 401) return "session_dead";
|
|
1664
1763
|
if (status === 403) {
|
|
1665
|
-
|
|
1666
|
-
for (const marker of HARD_CREDIT_MARKERS) if (lower.includes(marker.toLowerCase()) || body.includes(marker)) return "hard_credit";
|
|
1764
|
+
if (isHardCreditText(body)) return "hard_credit";
|
|
1667
1765
|
}
|
|
1668
|
-
if (status === 503 &&
|
|
1766
|
+
if (status === 503 && SOFT_RATE_PATTERN.test(body)) return "soft_rate";
|
|
1669
1767
|
if (status === 403 && !body.includes("\"code\":") && !body.includes("\"msg\":")) return "waf_block";
|
|
1670
1768
|
if (status === 404) return "not_found";
|
|
1671
1769
|
if (status >= 500) return "server";
|
|
@@ -1840,6 +1938,158 @@ function withLeadingSystemPrompt(prepared) {
|
|
|
1840
1938
|
}, ...messages];
|
|
1841
1939
|
return JSON.stringify(obj);
|
|
1842
1940
|
}
|
|
1941
|
+
/**
|
|
1942
|
+
* System prompts the host has already refused at its channel check.
|
|
1943
|
+
*
|
|
1944
|
+
* WorkBuddy's prompt screen reads system and assistant roles, not user turns, and
|
|
1945
|
+
* it matches more than one client's wording: the DSH opening line, the DSH GUI
|
|
1946
|
+
* pair, and several lines shared with other agent harnesses. A fixed rewrite list
|
|
1947
|
+
* is therefore always one upstream change behind. The first turn that is refused
|
|
1948
|
+
* pays one 400, its system prompt is folded into the first user message, and the
|
|
1949
|
+
* same prompt is folded before sending from then on. Bounded so a process that
|
|
1950
|
+
* sees many one-off prompts keeps only recent ones.
|
|
1951
|
+
*/
|
|
1952
|
+
const FOLDED_PROMPTS_MAX = 32;
|
|
1953
|
+
const foldedPrompts = /* @__PURE__ */ new Set();
|
|
1954
|
+
/** The fingerprint of every system message in a prepared body, if it has one. */
|
|
1955
|
+
function systemPromptFingerprint(prepared) {
|
|
1956
|
+
let body;
|
|
1957
|
+
try {
|
|
1958
|
+
body = JSON.parse(prepared);
|
|
1959
|
+
} catch {
|
|
1960
|
+
return;
|
|
1961
|
+
}
|
|
1962
|
+
const messages = body?.messages;
|
|
1963
|
+
if (!Array.isArray(messages)) return void 0;
|
|
1964
|
+
const texts = [];
|
|
1965
|
+
for (const value of messages) {
|
|
1966
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) continue;
|
|
1967
|
+
const message = value;
|
|
1968
|
+
if (message["role"] !== "system" && message["role"] !== "developer") continue;
|
|
1969
|
+
texts.push(contentText(message["content"]));
|
|
1970
|
+
}
|
|
1971
|
+
if (texts.length === 0) return void 0;
|
|
1972
|
+
return createHash("sha256").update(texts.join("\0")).digest("hex");
|
|
1973
|
+
}
|
|
1974
|
+
/** The text parts of a message content value, joined for the fingerprint and fold. */
|
|
1975
|
+
function contentText(content) {
|
|
1976
|
+
if (typeof content === "string") return content;
|
|
1977
|
+
if (!Array.isArray(content)) return "";
|
|
1978
|
+
const parts = [];
|
|
1979
|
+
for (const part of content) if (typeof part === "string") parts.push(part);
|
|
1980
|
+
else if (typeof part === "object" && part !== null && typeof part["text"] === "string") parts.push(part["text"]);
|
|
1981
|
+
return parts.join("\n");
|
|
1982
|
+
}
|
|
1983
|
+
/**
|
|
1984
|
+
* Move every system message into the first user message, and take the client
|
|
1985
|
+
* name out of assistant turns.
|
|
1986
|
+
*
|
|
1987
|
+
* The host accepts the same instructions in a user turn; a system turn is what
|
|
1988
|
+
* its channel check inspects. Assistant turns are inspected too, so a reply that
|
|
1989
|
+
* quoted the client name would fail the next request; that copy is renamed. A
|
|
1990
|
+
* neutral system message stays first so a gateway that insists on one still sees
|
|
1991
|
+
* it. Returns undefined when there is nothing to move or rename, which is also
|
|
1992
|
+
* what keeps an empty system message from looping.
|
|
1993
|
+
*/
|
|
1994
|
+
function foldSystemIntoUser(prepared) {
|
|
1995
|
+
let body;
|
|
1996
|
+
try {
|
|
1997
|
+
body = JSON.parse(prepared);
|
|
1998
|
+
} catch {
|
|
1999
|
+
return;
|
|
2000
|
+
}
|
|
2001
|
+
if (typeof body !== "object" || body === null || Array.isArray(body)) return void 0;
|
|
2002
|
+
const obj = body;
|
|
2003
|
+
const messages = obj["messages"];
|
|
2004
|
+
if (!Array.isArray(messages)) return void 0;
|
|
2005
|
+
const prompts = [];
|
|
2006
|
+
const rest = [];
|
|
2007
|
+
let renamed = false;
|
|
2008
|
+
for (const value of messages) {
|
|
2009
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
2010
|
+
const message = value;
|
|
2011
|
+
if (message["role"] === "system" || message["role"] === "developer") {
|
|
2012
|
+
const text = contentText(message["content"]);
|
|
2013
|
+
if (text.trim() !== "") prompts.push(text);
|
|
2014
|
+
continue;
|
|
2015
|
+
}
|
|
2016
|
+
if (message["role"] === "assistant" && "content" in message) {
|
|
2017
|
+
const content = renameInContent(message["content"]);
|
|
2018
|
+
if (content !== message["content"]) {
|
|
2019
|
+
renamed = true;
|
|
2020
|
+
rest.push({
|
|
2021
|
+
...message,
|
|
2022
|
+
content
|
|
2023
|
+
});
|
|
2024
|
+
continue;
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
}
|
|
2028
|
+
rest.push(value);
|
|
2029
|
+
}
|
|
2030
|
+
if (prompts.length === 0 && !renamed) return void 0;
|
|
2031
|
+
if (prompts.length === 0) {
|
|
2032
|
+
obj["messages"] = rest;
|
|
2033
|
+
return JSON.stringify(obj);
|
|
2034
|
+
}
|
|
2035
|
+
const instructions = prompts.join("\n\n");
|
|
2036
|
+
const firstUser = rest.findIndex((value) => typeof value === "object" && value !== null && !Array.isArray(value) && value["role"] === "user");
|
|
2037
|
+
if (firstUser === -1) rest.unshift({
|
|
2038
|
+
role: "user",
|
|
2039
|
+
content: instructions
|
|
2040
|
+
});
|
|
2041
|
+
else {
|
|
2042
|
+
const message = rest[firstUser];
|
|
2043
|
+
const content = message["content"];
|
|
2044
|
+
rest[firstUser] = {
|
|
2045
|
+
...message,
|
|
2046
|
+
content: typeof content === "string" ? `${instructions}\n\n${content}` : [{
|
|
2047
|
+
type: "text",
|
|
2048
|
+
text: instructions
|
|
2049
|
+
}, ...Array.isArray(content) ? content : []]
|
|
2050
|
+
};
|
|
2051
|
+
}
|
|
2052
|
+
obj["messages"] = [{
|
|
2053
|
+
role: "system",
|
|
2054
|
+
content: ""
|
|
2055
|
+
}, ...rest];
|
|
2056
|
+
return JSON.stringify(obj);
|
|
2057
|
+
}
|
|
2058
|
+
/**
|
|
2059
|
+
* Rename the client in one message content, preserving every other field.
|
|
2060
|
+
*
|
|
2061
|
+
* Matched case-insensitively because the host's screen is, and only the product
|
|
2062
|
+
* name is replaced: the rest of the sentence still reads naturally, and the
|
|
2063
|
+
* model sees the same instructions.
|
|
2064
|
+
*/
|
|
2065
|
+
function renameInContent(content) {
|
|
2066
|
+
const rename = (text) => text.replace(/DeepSeek Harness/gi, "DeepSeek");
|
|
2067
|
+
if (typeof content === "string") return rename(content);
|
|
2068
|
+
if (!Array.isArray(content)) return content;
|
|
2069
|
+
return content.map((part) => {
|
|
2070
|
+
if (typeof part === "string") return rename(part);
|
|
2071
|
+
if (typeof part !== "object" || part === null || Array.isArray(part)) return part;
|
|
2072
|
+
const record = part;
|
|
2073
|
+
if (typeof record["text"] !== "string") return part;
|
|
2074
|
+
const text = rename(record["text"]);
|
|
2075
|
+
return text === record["text"] ? part : {
|
|
2076
|
+
...record,
|
|
2077
|
+
text
|
|
2078
|
+
};
|
|
2079
|
+
});
|
|
2080
|
+
}
|
|
2081
|
+
function rememberFoldedPrompt(fingerprint) {
|
|
2082
|
+
if (foldedPrompts.has(fingerprint)) return;
|
|
2083
|
+
if (foldedPrompts.size >= FOLDED_PROMPTS_MAX) {
|
|
2084
|
+
const oldest = foldedPrompts.values().next().value;
|
|
2085
|
+
if (oldest !== void 0) foldedPrompts.delete(oldest);
|
|
2086
|
+
}
|
|
2087
|
+
foldedPrompts.add(fingerprint);
|
|
2088
|
+
}
|
|
2089
|
+
/** Whether a 400 body is the host's prompt screen rather than a bad request. */
|
|
2090
|
+
function isChannelCheckText(text) {
|
|
2091
|
+
return CHANNEL_CHECK_PATTERN.test(text);
|
|
2092
|
+
}
|
|
1843
2093
|
var WorkBuddyUpstreamClient = class {
|
|
1844
2094
|
fetchImpl;
|
|
1845
2095
|
constructor(options = {}) {
|
|
@@ -1866,6 +2116,10 @@ var WorkBuddyUpstreamClient = class {
|
|
|
1866
2116
|
if (typeof value !== "object" || value === null || Array.isArray(value)) continue;
|
|
1867
2117
|
const message = value;
|
|
1868
2118
|
if (message["role"] === "developer") message["role"] = "system";
|
|
2119
|
+
if (message["role"] === "assistant" && "content" in message) {
|
|
2120
|
+
const content = renameInContent(message["content"]);
|
|
2121
|
+
if (content !== message["content"]) message["content"] = content;
|
|
2122
|
+
}
|
|
1869
2123
|
}
|
|
1870
2124
|
const choice = obj["tool_choice"];
|
|
1871
2125
|
if (typeof choice === "string") {
|
|
@@ -1893,56 +2147,104 @@ var WorkBuddyUpstreamClient = class {
|
|
|
1893
2147
|
}
|
|
1894
2148
|
/** Forward one chat completion. Never throws for upstream failures. */
|
|
1895
2149
|
async chatStream(credential, prepared, signal, timeouts) {
|
|
1896
|
-
|
|
1897
|
-
const
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
}
|
|
2150
|
+
const fingerprint = systemPromptFingerprint(prepared);
|
|
2151
|
+
const region = regionOf(credential.domain);
|
|
2152
|
+
let body = region === "global" ? withLeadingSystemPrompt(prepared) : prepared;
|
|
2153
|
+
const cacheKey = fingerprint === void 0 ? void 0 : `${region}:${fingerprint}`;
|
|
2154
|
+
if (cacheKey !== void 0 && foldedPrompts.has(cacheKey)) body = foldSystemIntoUser(body) ?? body;
|
|
2155
|
+
let rememberOnSuccess;
|
|
2156
|
+
for (let attempt = 0;; attempt++) {
|
|
2157
|
+
let response;
|
|
2158
|
+
const deadline = headersDeadline(signal, timeouts?.headersMs ?? STREAM_HEADERS_TIMEOUT_MS);
|
|
2159
|
+
try {
|
|
2160
|
+
response = await this.fetchImpl(`${chatBase(credential)}/v2/chat/completions`, {
|
|
2161
|
+
redirect: "error",
|
|
2162
|
+
method: "POST",
|
|
2163
|
+
headers: chatHeaders(credential),
|
|
2164
|
+
body,
|
|
2165
|
+
signal: deadline.signal
|
|
2166
|
+
});
|
|
2167
|
+
} catch (error) {
|
|
2168
|
+
if (signal?.aborted === true) return {
|
|
2169
|
+
ok: false,
|
|
2170
|
+
kind: "aborted",
|
|
2171
|
+
status: 0,
|
|
2172
|
+
message: "cancelled by the caller"
|
|
2173
|
+
};
|
|
2174
|
+
return {
|
|
2175
|
+
ok: false,
|
|
2176
|
+
kind: "transport",
|
|
2177
|
+
status: 0,
|
|
2178
|
+
message: `transport error: ${String(error)}`
|
|
2179
|
+
};
|
|
2180
|
+
} finally {
|
|
2181
|
+
deadline.arrived();
|
|
2182
|
+
}
|
|
2183
|
+
if (response.ok) {
|
|
2184
|
+
const contentType = (response.headers.get("content-type") ?? "").toLowerCase();
|
|
2185
|
+
const declaredLength = Number(response.headers.get("content-length") ?? "");
|
|
2186
|
+
const smallDeclaredBody = Number.isFinite(declaredLength) && declaredLength > 0 && declaredLength <= ERROR_BODY_LIMIT;
|
|
2187
|
+
if (contentType.includes("application/json") || smallDeclaredBody && !contentType.includes("text/event-stream")) {
|
|
2188
|
+
const text = await readErrorBody(response, ERROR_BODY_LIMIT, signal);
|
|
2189
|
+
if (attempt === 0 && isChannelCheckText(text)) {
|
|
2190
|
+
const folded = foldSystemIntoUser(body);
|
|
2191
|
+
if (folded !== void 0) {
|
|
2192
|
+
rememberOnSuccess = cacheKey;
|
|
2193
|
+
body = folded;
|
|
2194
|
+
continue;
|
|
2195
|
+
}
|
|
2196
|
+
}
|
|
2197
|
+
if (rememberOnSuccess !== void 0 && !isChannelCheckText(text)) rememberFoldedPrompt(rememberOnSuccess);
|
|
2198
|
+
const envelopeResponse = new Response(text, {
|
|
2199
|
+
status: response.status,
|
|
2200
|
+
headers: response.headers
|
|
2201
|
+
});
|
|
2202
|
+
const envelopeBody = envelopeResponse.body;
|
|
2203
|
+
if (envelopeBody === null) return {
|
|
2204
|
+
ok: false,
|
|
2205
|
+
kind: "server",
|
|
2206
|
+
status: 502,
|
|
2207
|
+
message: "upstream returned 200 without a body"
|
|
2208
|
+
};
|
|
2209
|
+
return {
|
|
2210
|
+
ok: true,
|
|
2211
|
+
response: withRelayStream(envelopeResponse, envelopeBody)
|
|
2212
|
+
};
|
|
2213
|
+
}
|
|
2214
|
+
if (rememberOnSuccess !== void 0) rememberFoldedPrompt(rememberOnSuccess);
|
|
2215
|
+
if (response.body === null) return {
|
|
2216
|
+
ok: false,
|
|
2217
|
+
kind: "server",
|
|
2218
|
+
status: 502,
|
|
2219
|
+
message: "upstream returned 200 without a stream"
|
|
2220
|
+
};
|
|
2221
|
+
const relayed = withIdleWatchdog(response.body, timeouts?.idleMs, signal);
|
|
2222
|
+
return {
|
|
2223
|
+
ok: true,
|
|
2224
|
+
response: withRelayStream(new Response(relayed, {
|
|
2225
|
+
status: response.status,
|
|
2226
|
+
headers: response.headers
|
|
2227
|
+
}), relayed)
|
|
2228
|
+
};
|
|
2229
|
+
}
|
|
2230
|
+
const text = await readErrorBody(response, ERROR_BODY_LIMIT, signal);
|
|
2231
|
+
const retryAfter = retryAfterSeconds(response.headers);
|
|
2232
|
+
if (attempt === 0 && response.status === 400 && isChannelCheckText(text)) {
|
|
2233
|
+
const folded = foldSystemIntoUser(body);
|
|
2234
|
+
if (folded !== void 0) {
|
|
2235
|
+
rememberOnSuccess = cacheKey;
|
|
2236
|
+
body = folded;
|
|
2237
|
+
continue;
|
|
2238
|
+
}
|
|
2239
|
+
}
|
|
1914
2240
|
return {
|
|
1915
2241
|
ok: false,
|
|
1916
|
-
kind:
|
|
1917
|
-
status:
|
|
1918
|
-
message:
|
|
1919
|
-
|
|
1920
|
-
} finally {
|
|
1921
|
-
deadline.arrived();
|
|
1922
|
-
}
|
|
1923
|
-
if (response.ok) {
|
|
1924
|
-
if (response.body === null) return {
|
|
1925
|
-
ok: true,
|
|
1926
|
-
response
|
|
1927
|
-
};
|
|
1928
|
-
const relayed = withIdleWatchdog(response.body, timeouts?.idleMs, signal);
|
|
1929
|
-
return {
|
|
1930
|
-
ok: true,
|
|
1931
|
-
response: withRelayStream(new Response(relayed, {
|
|
1932
|
-
status: response.status,
|
|
1933
|
-
headers: response.headers
|
|
1934
|
-
}), relayed)
|
|
2242
|
+
kind: classifyUpstreamError(response.status, text),
|
|
2243
|
+
status: response.status,
|
|
2244
|
+
message: isGatewayHtmlRejection(response.status, text) ? gatewayRejectionText(response.status) : text,
|
|
2245
|
+
...retryAfter === void 0 ? {} : { retryAfter }
|
|
1935
2246
|
};
|
|
1936
2247
|
}
|
|
1937
|
-
const text = await readErrorBody(response, ERROR_BODY_LIMIT, signal);
|
|
1938
|
-
const retryAfter = retryAfterSeconds(response.headers);
|
|
1939
|
-
return {
|
|
1940
|
-
ok: false,
|
|
1941
|
-
kind: classifyUpstreamError(response.status, text),
|
|
1942
|
-
status: response.status,
|
|
1943
|
-
message: isGatewayHtmlRejection(response.status, text) ? gatewayRejectionText(response.status) : text,
|
|
1944
|
-
...retryAfter === void 0 ? {} : { retryAfter }
|
|
1945
|
-
};
|
|
1946
2248
|
}
|
|
1947
2249
|
/** POST the token-refresh endpoint; the caller merges the outcome. */
|
|
1948
2250
|
async refreshToken(credential) {
|
|
@@ -1968,8 +2270,9 @@ var WorkBuddyUpstreamClient = class {
|
|
|
1968
2270
|
*
|
|
1969
2271
|
* The two gateways are read differently, because they answer differently:
|
|
1970
2272
|
*
|
|
1971
|
-
* - **CN** serves the roster at `/v2/enterprises/personal/models
|
|
1972
|
-
* the
|
|
2273
|
+
* - **CN** serves the roster at `/v2/enterprises/personal/models`. Like the
|
|
2274
|
+
* desktop process that calls it directly, it expects the desktop client
|
|
2275
|
+
* spelling; this is not the chat endpoint's three-segment UA.
|
|
1973
2276
|
* - **Global** serves it as part of the product config at `/v3/config`, and
|
|
1974
2277
|
* only to the DESKTOP client channel. Asking the global host with the CLI UA
|
|
1975
2278
|
* yields a truncated roster, and the CN path answers HTTP 500 there — which
|
|
@@ -1988,14 +2291,13 @@ var WorkBuddyUpstreamClient = class {
|
|
|
1988
2291
|
...credential.domain === "" ? {} : { "X-Domain": credential.domain },
|
|
1989
2292
|
"X-Product": "SaaS",
|
|
1990
2293
|
"X-Requested-With": "XMLHttpRequest",
|
|
2294
|
+
"X-CodeBuddy-Request": "1",
|
|
1991
2295
|
"Connection": "close",
|
|
1992
|
-
"User-Agent":
|
|
2296
|
+
"User-Agent": GLOBAL_DESKTOP_UA
|
|
1993
2297
|
} : {
|
|
2298
|
+
...commonHeaders(credential),
|
|
1994
2299
|
"Authorization": `Bearer ${credential.accessToken}`,
|
|
1995
|
-
"Accept": "application/json"
|
|
1996
|
-
"Origin": billingBase(credential),
|
|
1997
|
-
"Referer": `${billingBase(credential)}/`,
|
|
1998
|
-
"User-Agent": CLIENT_UA
|
|
2300
|
+
"Accept": "application/json"
|
|
1999
2301
|
};
|
|
2000
2302
|
if (!global && credential.enterpriseId !== void 0 && credential.enterpriseId !== "") headers["X-Enterprise-Id"] = credential.enterpriseId;
|
|
2001
2303
|
const response = await this.fetchImpl(url, {
|
|
@@ -2839,6 +3141,15 @@ async function encryptedFieldOpener() {
|
|
|
2839
3141
|
*/
|
|
2840
3142
|
/** Live auth file name the WorkBuddy desktop app writes. */
|
|
2841
3143
|
const WORKBUDDY_LIVE_FILENAME = "workbuddy-desktop.info";
|
|
3144
|
+
/**
|
|
3145
|
+
* Live auth file name the international client writes.
|
|
3146
|
+
*
|
|
3147
|
+
* One file, no snapshots beside it: the client overwrites this one when the user
|
|
3148
|
+
* switches accounts. Treating it as an abandoned snapshot had the pool take over
|
|
3149
|
+
* the session the app was still signed into and renew it behind the app's back,
|
|
3150
|
+
* which spends the rotation the app needed on its next refresh.
|
|
3151
|
+
*/
|
|
3152
|
+
const WORKBUDDY_GLOBAL_LIVE_FILENAME = "workbuddy-desktop-ai.info";
|
|
2842
3153
|
/** Env override for the auth file or its directory. */
|
|
2843
3154
|
const WORKBUDDY_AUTH_FILE_ENV = "WORKBUDDY_AUTH_FILE";
|
|
2844
3155
|
/**
|
|
@@ -2852,6 +3163,15 @@ const WORKBUDDY_AUTH_FILE_ENV = "WORKBUDDY_AUTH_FILE";
|
|
|
2852
3163
|
const ARCHIVE_FILENAME = "credentials-archive.json";
|
|
2853
3164
|
/** Subdirectory of the plugin's own data root that holds the sign-ins it wrote. */
|
|
2854
3165
|
const OWN_SIGN_IN_DIRNAME = "signins";
|
|
3166
|
+
/**
|
|
3167
|
+
* Sign-ins the user deleted, kept as a tombstone so a scan cannot resurrect them.
|
|
3168
|
+
*
|
|
3169
|
+
* Deleting only the file is not enough: the account's token is still in the
|
|
3170
|
+
* archive, and the desktop client still keeps a superseded snapshot of it, so the
|
|
3171
|
+
* next scan took the snapshot over and wrote the file straight back. The row never
|
|
3172
|
+
* moved, and the delete read as a button that does nothing.
|
|
3173
|
+
*/
|
|
3174
|
+
const DELETED_SIGN_INS_FILENAME = "deleted-signins.json";
|
|
2855
3175
|
/** File-name prefix that marks one of those sign-ins as the plugin's own. */
|
|
2856
3176
|
const OWN_SIGN_IN_PREFIX = "workbuddy-panel-";
|
|
2857
3177
|
/**
|
|
@@ -2994,40 +3314,198 @@ function decryptableString(value, decrypt) {
|
|
|
2994
3314
|
return "";
|
|
2995
3315
|
}
|
|
2996
3316
|
/**
|
|
2997
|
-
*
|
|
2998
|
-
* snapshot it left behind).
|
|
2999
|
-
* app
|
|
3317
|
+
* Whether `path` is the desktop app's live sign-in (as opposed to a backup
|
|
3318
|
+
* snapshot it left behind). A copy of it outranks an abandoned snapshot of the
|
|
3319
|
+
* same token, because the app keeps it current.
|
|
3000
3320
|
*/
|
|
3001
3321
|
function isLiveAuthFile(path) {
|
|
3002
|
-
|
|
3322
|
+
const name = basename(path);
|
|
3323
|
+
return name === WORKBUDDY_LIVE_FILENAME || name === WORKBUDDY_GLOBAL_LIVE_FILENAME;
|
|
3003
3324
|
}
|
|
3004
3325
|
/**
|
|
3005
3326
|
* Which of two credentials for the same account the pool should keep.
|
|
3006
3327
|
*
|
|
3007
3328
|
* Ordering, highest first:
|
|
3008
3329
|
*
|
|
3009
|
-
* 1. the
|
|
3010
|
-
* 2. the
|
|
3330
|
+
* 1. the credential the upstream issued most recently (`lastRefreshAtMs`);
|
|
3331
|
+
* 2. the source the copy came from: the live sign-in file, then a sign-in the
|
|
3332
|
+
* plugin wrote, then a superseded snapshot the client left behind;
|
|
3011
3333
|
* 3. the longer stored expiry, as a fallback for documents that carry no issue
|
|
3012
|
-
* time
|
|
3334
|
+
* time at all.
|
|
3335
|
+
*
|
|
3336
|
+
* Issue time comes first, and source rank only breaks a tie between copies of
|
|
3337
|
+
* the same token. Rank used to come first, on the reasoning that a file the
|
|
3338
|
+
* plugin writes is the one with a future, and that ordering cost the pool a
|
|
3339
|
+
* working token outright: a renewal of an account the desktop was signed into
|
|
3340
|
+
* is written to the archive, the plugin's own copy of that account stays one
|
|
3341
|
+
* rotation behind, and the next scan served the dead copy while the live one sat
|
|
3342
|
+
* next to it. An account was marked "sign in again" with a working token in the
|
|
3343
|
+
* same directory.
|
|
3013
3344
|
*
|
|
3014
3345
|
* The stored expiry alone is NOT a freshness signal: the upstream does not
|
|
3015
3346
|
* rewrite it when it revokes a token, so a long-dead backup can claim to expire
|
|
3016
3347
|
* later than the token that actually works. Selecting on it made every upstream
|
|
3017
3348
|
* call return 401 while a perfectly good credential sat in the same directory.
|
|
3018
3349
|
*/
|
|
3019
|
-
function compareFreshness(a, b) {
|
|
3020
|
-
const aLive = isLiveAuthFile(a.sourcePath) ? 1 : 0;
|
|
3021
|
-
const bLive = isLiveAuthFile(b.sourcePath) ? 1 : 0;
|
|
3022
|
-
if (aLive !== bLive) return bLive - aLive;
|
|
3350
|
+
function compareFreshness(a, b, ownDir) {
|
|
3023
3351
|
const aIssued = a.lastRefreshAtMs ?? 0;
|
|
3024
3352
|
const bIssued = b.lastRefreshAtMs ?? 0;
|
|
3353
|
+
if (aIssued !== bIssued && aIssued > 0 && bIssued > 0) return bIssued - aIssued;
|
|
3354
|
+
const aRank = credentialRank(a, ownDir);
|
|
3355
|
+
const bRank = credentialRank(b, ownDir);
|
|
3356
|
+
if (aIssued > 0 !== bIssued > 0) {
|
|
3357
|
+
if (aIssued > 0 && aRank <= 1) return -1;
|
|
3358
|
+
if (bIssued > 0 && bRank <= 1) return 1;
|
|
3359
|
+
}
|
|
3360
|
+
if (aRank !== bRank) return aRank - bRank;
|
|
3025
3361
|
if (aIssued !== bIssued) return bIssued - aIssued;
|
|
3026
3362
|
return b.expiresAtMs - a.expiresAtMs;
|
|
3027
3363
|
}
|
|
3364
|
+
/**
|
|
3365
|
+
* Which kind of source a credential came from, lowest number first.
|
|
3366
|
+
*
|
|
3367
|
+
* A sign-in the plugin wrote outranks a snapshot the client left behind because
|
|
3368
|
+
* the plugin renews it and nothing renews the snapshot: the same token, but one
|
|
3369
|
+
* of the two copies has a future.
|
|
3370
|
+
*/
|
|
3371
|
+
function credentialRank(credential, ownDir) {
|
|
3372
|
+
if (isLiveAuthFile(credential.sourcePath)) return 0;
|
|
3373
|
+
if (ownSignInPath(credential.sourcePath, ownDir)) return 1;
|
|
3374
|
+
return 2;
|
|
3375
|
+
}
|
|
3028
3376
|
/** True when `candidate` should replace `incumbent` for the same account. */
|
|
3029
|
-
function isFresher(candidate, incumbent) {
|
|
3030
|
-
return compareFreshness(candidate, incumbent) < 0;
|
|
3377
|
+
function isFresher(candidate, incumbent, ownDir) {
|
|
3378
|
+
return compareFreshness(candidate, incumbent, ownDir) < 0;
|
|
3379
|
+
}
|
|
3380
|
+
/**
|
|
3381
|
+
* Queue order for the account list: the live sign-in first, then the copy the
|
|
3382
|
+
* upstream issued last, then the newest expiry.
|
|
3383
|
+
*
|
|
3384
|
+
* This is a different question from `compareFreshness`, which decides which copy
|
|
3385
|
+
* of *one* account serves. That rule falls back to the source only when a copy
|
|
3386
|
+
* carries no issue time, which makes it cycle across three copies (a dated
|
|
3387
|
+
* snapshot, an undated live file, a dated panel sign-in) and leaves a list sort
|
|
3388
|
+
* with the order TimSort happened to produce. A key vector cannot.
|
|
3389
|
+
*/
|
|
3390
|
+
function compareQueueOrder(a, b, ownDir) {
|
|
3391
|
+
const aRank = credentialRank(a, ownDir);
|
|
3392
|
+
const bRank = credentialRank(b, ownDir);
|
|
3393
|
+
if (aRank !== bRank) return aRank - bRank;
|
|
3394
|
+
const aIssued = a.lastRefreshAtMs ?? 0;
|
|
3395
|
+
const bIssued = b.lastRefreshAtMs ?? 0;
|
|
3396
|
+
if (aIssued !== bIssued) return bIssued - aIssued;
|
|
3397
|
+
return b.expiresAtMs - a.expiresAtMs;
|
|
3398
|
+
}
|
|
3399
|
+
function tokenDigest(accessToken) {
|
|
3400
|
+
return createHash("sha256").update(`workbuddy\0deleted\0${accessToken}`).digest("hex").slice(0, 32);
|
|
3401
|
+
}
|
|
3402
|
+
/**
|
|
3403
|
+
* A stamp for one file that changes when its content is replaced.
|
|
3404
|
+
*
|
|
3405
|
+
* Millisecond mtime alone repeats on a coarse filesystem, and two pools over one
|
|
3406
|
+
* data directory then kept a stale tombstone set; the size is in here so a rewrite
|
|
3407
|
+
* that lands in the same reported millisecond still reads as a different file.
|
|
3408
|
+
*/
|
|
3409
|
+
async function signInFileStamp(file) {
|
|
3410
|
+
try {
|
|
3411
|
+
const entry = await stat(file);
|
|
3412
|
+
return `${entry.mtimeMs}:${entry.size}`;
|
|
3413
|
+
} catch {
|
|
3414
|
+
return "";
|
|
3415
|
+
}
|
|
3416
|
+
}
|
|
3417
|
+
/**
|
|
3418
|
+
* Run `work` while holding a lock file, or without it when the lock stays taken.
|
|
3419
|
+
*
|
|
3420
|
+
* The tombstone document is the one file two pools over the same data directory
|
|
3421
|
+
* both write, and a read-union-write is only as good as its serialisation. The
|
|
3422
|
+
* budget is short on purpose: a panel call is answered within seconds, and the
|
|
3423
|
+
* work is a few kilobytes of JSON. A lock older than that is a process that died
|
|
3424
|
+
* holding it, so it is dropped and retried.
|
|
3425
|
+
*/
|
|
3426
|
+
async function withFileLock(lock, work) {
|
|
3427
|
+
let held = false;
|
|
3428
|
+
for (let attempt = 0; attempt < 25 && !held; attempt += 1) try {
|
|
3429
|
+
await writeFile(lock, "", {
|
|
3430
|
+
flag: "wx",
|
|
3431
|
+
mode: 384
|
|
3432
|
+
});
|
|
3433
|
+
held = true;
|
|
3434
|
+
break;
|
|
3435
|
+
} catch (error) {
|
|
3436
|
+
if (error.code !== "EEXIST") break;
|
|
3437
|
+
if (await stat(lock).then((entry) => Date.now() - entry.mtimeMs, () => 0) > 5e3) {
|
|
3438
|
+
await unlink(lock).catch(() => {});
|
|
3439
|
+
continue;
|
|
3440
|
+
}
|
|
3441
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
3442
|
+
}
|
|
3443
|
+
try {
|
|
3444
|
+
await work();
|
|
3445
|
+
} finally {
|
|
3446
|
+
if (held) await unlink(lock).catch(() => {});
|
|
3447
|
+
}
|
|
3448
|
+
}
|
|
3449
|
+
/**
|
|
3450
|
+
* Read the tombstone document.
|
|
3451
|
+
*
|
|
3452
|
+
* Throws when the file exists but cannot be read or parsed, so the caller can keep
|
|
3453
|
+
* what it already knows instead of treating a scanner's EBUSY as "no deleted
|
|
3454
|
+
* sign-ins".
|
|
3455
|
+
*/
|
|
3456
|
+
async function readDeletedSignInsFile(file) {
|
|
3457
|
+
const parsed = JSON.parse(await readFile(file, "utf8"));
|
|
3458
|
+
const tombstones = /* @__PURE__ */ new Map();
|
|
3459
|
+
const entries = parsed?.entries;
|
|
3460
|
+
if (entries === null || typeof entries !== "object" || Array.isArray(entries)) return tombstones;
|
|
3461
|
+
for (const [id, value] of Object.entries(entries)) {
|
|
3462
|
+
const entry = value;
|
|
3463
|
+
if (entry === null || typeof entry !== "object") continue;
|
|
3464
|
+
const digest = entry.tokenDigest;
|
|
3465
|
+
if (typeof digest !== "string" || digest === "") continue;
|
|
3466
|
+
const issued = entry.lastRefreshAtMs;
|
|
3467
|
+
const deletedAt = entry.deletedAtMs;
|
|
3468
|
+
tombstones.set(id, {
|
|
3469
|
+
tokenDigest: digest,
|
|
3470
|
+
lastRefreshAtMs: typeof issued === "number" && Number.isFinite(issued) ? issued : 0,
|
|
3471
|
+
deletedAtMs: typeof deletedAt === "number" && Number.isFinite(deletedAt) ? deletedAt : 0
|
|
3472
|
+
});
|
|
3473
|
+
}
|
|
3474
|
+
return tombstones;
|
|
3475
|
+
}
|
|
3476
|
+
/**
|
|
3477
|
+
* Whether `credential` is a sign-in the user made after deleting the account.
|
|
3478
|
+
*
|
|
3479
|
+
* A re-login is newer than the tombstone, so its issue time is the signal. Files
|
|
3480
|
+
* that carry no issue time cannot be ordered, and for those a different token is
|
|
3481
|
+
* the only evidence of a new sign-in.
|
|
3482
|
+
*
|
|
3483
|
+
* The same token never ends it, whatever stamp the copy carries: a superseded
|
|
3484
|
+
* snapshot the client still keeps is that same sign-in lying in another file, and
|
|
3485
|
+
* a tolerant comparison (same stamp within a few minutes) let one of them bring a
|
|
3486
|
+
* deleted account back the moment the build allowed it. An older-issued token is
|
|
3487
|
+
* not a re-login either, which is why the comparison stays strict: a genuine
|
|
3488
|
+
* re-login is issued after the deleted one, and the alternative — accepting a
|
|
3489
|
+
* slightly older stamp as “close enough” — cannot tell it apart from the next
|
|
3490
|
+
* snapshot down the stack.
|
|
3491
|
+
*/
|
|
3492
|
+
function endsTombstone(tombstone, credential) {
|
|
3493
|
+
if (tokenDigest(credential.accessToken) === tombstone.tokenDigest) return false;
|
|
3494
|
+
const issued = credential.lastRefreshAtMs ?? 0;
|
|
3495
|
+
if (tombstone.lastRefreshAtMs > 0 && issued > 0) return issued > tombstone.lastRefreshAtMs;
|
|
3496
|
+
if (tombstone.lastRefreshAtMs === 0 && issued > 0) return issued >= tombstone.deletedAtMs;
|
|
3497
|
+
return isLiveAuthFile(credential.sourcePath);
|
|
3498
|
+
}
|
|
3499
|
+
/**
|
|
3500
|
+
* Whether `sourcePath` sits inside the directory the plugin writes into.
|
|
3501
|
+
*
|
|
3502
|
+
* A walk that escapes the root is not inside it, and `..` is how a path gets
|
|
3503
|
+
* there. An empty walk means the path IS the directory.
|
|
3504
|
+
*/
|
|
3505
|
+
function ownSignInPath(sourcePath, ownDir) {
|
|
3506
|
+
if (ownDir === "" || sourcePath === "") return false;
|
|
3507
|
+
const walked = relative(resolve(ownDir), resolve(sourcePath));
|
|
3508
|
+
return walked !== "" && !walked.startsWith("..") && !isAbsolute(walked);
|
|
3031
3509
|
}
|
|
3032
3510
|
function workbuddyAccountId(credential) {
|
|
3033
3511
|
const stable = credential.uin ?? credential.uid ?? credential.nickname ?? `path:${credential.sourcePath}`;
|
|
@@ -3120,6 +3598,28 @@ async function readCredential(path) {
|
|
|
3120
3598
|
/** Refresh a credential this long before its expiry, so a request never rides one that dies mid-flight. */
|
|
3121
3599
|
const REFRESH_MARGIN_MS = 3e5;
|
|
3122
3600
|
/**
|
|
3601
|
+
* Oldest a sign-in the plugin itself wrote may get before it is renewed, weeks
|
|
3602
|
+
* of lifetime left or not.
|
|
3603
|
+
*
|
|
3604
|
+
* An expiry-based lead cannot do this job. The upstream revokes a session
|
|
3605
|
+
* without rewriting the token's `expiresAt`, so an account the user signed out
|
|
3606
|
+
* of still claims weeks of life: a lead of any size waits for an expiry that
|
|
3607
|
+
* never arrives, and the dead sign-in is only discovered when a request rides
|
|
3608
|
+
* it. Renewing on age instead means the renewal is visible in the panel, and a
|
|
3609
|
+
* sign-in the upstream no longer honours surfaces within this window rather
|
|
3610
|
+
* than at expiry.
|
|
3611
|
+
*
|
|
3612
|
+
* Only the plugin's own sign-ins qualify. A credential read out of the desktop
|
|
3613
|
+
* app's directory belongs to that app: it refreshes it, and a renewal here would
|
|
3614
|
+
* be discarded by the next scan while still spending an upstream call. Once a
|
|
3615
|
+
* desktop sign-in has been renewed here the copy in force is the plugin's own,
|
|
3616
|
+
* which is what keeps an account whose session ran out under this window.
|
|
3617
|
+
*
|
|
3618
|
+
* Six hours: short enough that a revoked session is noticed the same day, long
|
|
3619
|
+
* enough that the cost stays at one lightweight POST per account per window.
|
|
3620
|
+
*/
|
|
3621
|
+
const REFRESH_AFTER_MS = 216e5;
|
|
3622
|
+
/**
|
|
3123
3623
|
* Longest one penalize may cool an account: same 2h ceiling as the parse
|
|
3124
3624
|
* layer, so a raw caller-supplied timestamp cannot sleep a pool all day.
|
|
3125
3625
|
*
|
|
@@ -3130,8 +3630,28 @@ const REFRESH_MARGIN_MS = 3e5;
|
|
|
3130
3630
|
* value can reach it.
|
|
3131
3631
|
*/
|
|
3132
3632
|
const PENALIZE_CAP_MS = 72e5;
|
|
3133
|
-
/**
|
|
3134
|
-
|
|
3633
|
+
/**
|
|
3634
|
+
* How long a hold the pool decided itself keeps an account out of rotation.
|
|
3635
|
+
*
|
|
3636
|
+
* Neither condition behind a hold clears by itself: the credit balance has to
|
|
3637
|
+
* change, or the user has to sign in again. The configured cooldown window is
|
|
3638
|
+
* sized for a limit that expires, and using it here put a held account back at
|
|
3639
|
+
* the head of the queue every minute, where it failed again and burned the
|
|
3640
|
+
* attempts of the healthy accounts behind it. A hold is a fallback, not a
|
|
3641
|
+
* verdict: a manual cooldown reset, a re-scan and a balance read that finds
|
|
3642
|
+
* credits again all release it.
|
|
3643
|
+
*/
|
|
3644
|
+
const POOL_HOLD_MS = 216e5;
|
|
3645
|
+
/**
|
|
3646
|
+
* What a read on an account whose sign-in the upstream refuses is answered with
|
|
3647
|
+
* while the hold stands.
|
|
3648
|
+
*
|
|
3649
|
+
* The upstream's own sentence names a stale sign-in and sends the reader to the
|
|
3650
|
+
* connection check, which is right for the first refusal and noise on every
|
|
3651
|
+
* poll after it: the hold is the plugin's own statement that this account needs
|
|
3652
|
+
* a new sign-in, and it is the one the panel shows until one arrives.
|
|
3653
|
+
*/
|
|
3654
|
+
const REFUSED_SIGN_IN_MESSAGE = "this account's sign-in was refused by the upstream and its refresh token is spent; sign in again in the WorkBuddy desktop app";
|
|
3135
3655
|
/**
|
|
3136
3656
|
* Live per-model cooldown keys kept per account.
|
|
3137
3657
|
*
|
|
@@ -3167,9 +3687,9 @@ const RATE_LIMIT_STREAK_STEPS = 4;
|
|
|
3167
3687
|
* cycle instead of holding a persistently limited account back.
|
|
3168
3688
|
*/
|
|
3169
3689
|
const RATE_LIMIT_STREAK_WINDOW_MS = RATE_LIMIT_STREAK_CAP * 2;
|
|
3170
|
-
/** Drop expired per-model cooldowns, then cap what is left, soonest first. */
|
|
3690
|
+
/** Drop expired per-model cooldowns and blocks, then cap what is left, soonest first. */
|
|
3171
3691
|
function pruneModelCooldowns(account, now) {
|
|
3172
|
-
for (const [modelId, until] of Object.entries(
|
|
3692
|
+
for (const table of [account.modelCooldowns, account.modelBlocks]) for (const [modelId, until] of Object.entries(table)) if (until <= now) delete table[modelId];
|
|
3173
3693
|
const keys = Object.keys(account.modelCooldowns);
|
|
3174
3694
|
if (keys.length <= MAX_MODEL_COOLDOWN_KEYS) return;
|
|
3175
3695
|
keys.sort((a, b) => (account.modelCooldowns[a] ?? 0) - (account.modelCooldowns[b] ?? 0));
|
|
@@ -3188,6 +3708,28 @@ function ownSignInDir(dataPath) {
|
|
|
3188
3708
|
return dataPath === "" ? "" : join(dataPath, OWN_SIGN_IN_DIRNAME);
|
|
3189
3709
|
}
|
|
3190
3710
|
/**
|
|
3711
|
+
* The sign-in document fields one credential spells, in the vocabulary
|
|
3712
|
+
* `writeOwnSignIn` takes.
|
|
3713
|
+
*
|
|
3714
|
+
* `OwnSignIn` spells "the upstream did not say" as the empty string and a parsed
|
|
3715
|
+
* credential spells it as an absent field; coalescing here keeps that meeting
|
|
3716
|
+
* point in one place rather than in every writer.
|
|
3717
|
+
*/
|
|
3718
|
+
function ownSignInOf(credential) {
|
|
3719
|
+
return {
|
|
3720
|
+
region: regionOf(credential.domain),
|
|
3721
|
+
accessToken: credential.accessToken,
|
|
3722
|
+
refreshToken: credential.refreshToken,
|
|
3723
|
+
expiresAtMs: credential.expiresAtMs,
|
|
3724
|
+
domain: credential.domain,
|
|
3725
|
+
...credential.lastRefreshAtMs === void 0 ? {} : { lastRefreshAtMs: credential.lastRefreshAtMs },
|
|
3726
|
+
uid: credential.uid ?? "",
|
|
3727
|
+
uin: credential.uin ?? "",
|
|
3728
|
+
enterpriseId: credential.enterpriseId ?? "",
|
|
3729
|
+
nickname: credential.nickname ?? ""
|
|
3730
|
+
};
|
|
3731
|
+
}
|
|
3732
|
+
/**
|
|
3191
3733
|
* Write one panel-added sign-in as a `.info` file the scan reads back.
|
|
3192
3734
|
*
|
|
3193
3735
|
* The shape is the desktop app's own, so `parseWorkBuddyAuth` opens it with no
|
|
@@ -3206,7 +3748,7 @@ async function writeOwnSignIn(directory, signIn) {
|
|
|
3206
3748
|
refreshToken: signIn.refreshToken,
|
|
3207
3749
|
expiresAt: signIn.expiresAtMs,
|
|
3208
3750
|
domain: signIn.domain,
|
|
3209
|
-
lastRefreshTime: Date.now()
|
|
3751
|
+
lastRefreshTime: signIn.lastRefreshAtMs ?? Date.now()
|
|
3210
3752
|
},
|
|
3211
3753
|
account: {
|
|
3212
3754
|
...signIn.uid === "" ? {} : { uid: signIn.uid },
|
|
@@ -3218,6 +3760,31 @@ async function writeOwnSignIn(directory, signIn) {
|
|
|
3218
3760
|
return file;
|
|
3219
3761
|
}
|
|
3220
3762
|
/**
|
|
3763
|
+
* Rewrite one of the plugin's own sign-in files with a renewed token.
|
|
3764
|
+
*
|
|
3765
|
+
* Read-modify-write of the document that is already there, so the identity
|
|
3766
|
+
* fields the vendor's own parser looks for survive untouched and only the three
|
|
3767
|
+
* auth values a refresh replaces are written. Atomic for the same reason as the
|
|
3768
|
+
* archive: a truncated `.info` is an account the scan cannot open.
|
|
3769
|
+
*/
|
|
3770
|
+
async function renewOwnSignInFile(file, credential) {
|
|
3771
|
+
const document = JSON.parse(await readFile(file, "utf8"));
|
|
3772
|
+
const auth = document.auth ?? {};
|
|
3773
|
+
auth.accessToken = credential.accessToken;
|
|
3774
|
+
auth.refreshToken = credential.refreshToken;
|
|
3775
|
+
auth.expiresAt = credential.expiresAtMs;
|
|
3776
|
+
auth.lastRefreshTime = credential.lastRefreshAtMs ?? Date.now();
|
|
3777
|
+
document.auth = auth;
|
|
3778
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
3779
|
+
try {
|
|
3780
|
+
await writeFile(temporary, JSON.stringify(document, null, 2), { mode: 384 });
|
|
3781
|
+
await rename(temporary, file);
|
|
3782
|
+
} catch (error) {
|
|
3783
|
+
await unlink(temporary).catch(() => {});
|
|
3784
|
+
throw error;
|
|
3785
|
+
}
|
|
3786
|
+
}
|
|
3787
|
+
/**
|
|
3221
3788
|
* Soonest future one-off expiry. Missing or past sorts last. None left -> list head.
|
|
3222
3789
|
*
|
|
3223
3790
|
* Strict soonest-first, not a weighted spread: this mode exists so a pack about to
|
|
@@ -3289,16 +3856,17 @@ async function readArchivedCredentials(file) {
|
|
|
3289
3856
|
const record = entry;
|
|
3290
3857
|
if (typeof record["id"] !== "string" || record["id"] === "") continue;
|
|
3291
3858
|
if (typeof record["accessToken"] !== "string" || record["accessToken"] === "") continue;
|
|
3292
|
-
const expiresAt = typeof record["expiresAtMs"] === "number" ? record["expiresAtMs"] : 0;
|
|
3293
|
-
const refreshExpiresAt = typeof record["refreshExpiresAtMs"] === "number" ? record["refreshExpiresAtMs"] : 0;
|
|
3859
|
+
const expiresAt = typeof record["expiresAtMs"] === "number" ? expiryToMs(record["expiresAtMs"]) : 0;
|
|
3860
|
+
const refreshExpiresAt = typeof record["refreshExpiresAtMs"] === "number" ? expiryToMs(record["refreshExpiresAtMs"]) : 0;
|
|
3861
|
+
const lastRefreshAt = typeof record["lastRefreshAtMs"] === "number" ? expiryToMs(record["lastRefreshAtMs"]) : 0;
|
|
3294
3862
|
const lastUsable = Math.max(expiresAt, refreshExpiresAt);
|
|
3295
3863
|
if (lastUsable > 0 && lastUsable < horizon) continue;
|
|
3296
3864
|
stored.set(record["id"], {
|
|
3297
3865
|
accessToken: record["accessToken"],
|
|
3298
3866
|
refreshToken: typeof record["refreshToken"] === "string" ? record["refreshToken"] : "",
|
|
3299
|
-
expiresAtMs:
|
|
3300
|
-
...
|
|
3301
|
-
...
|
|
3867
|
+
expiresAtMs: expiresAt,
|
|
3868
|
+
...refreshExpiresAt === 0 ? {} : { refreshExpiresAtMs: refreshExpiresAt },
|
|
3869
|
+
...lastRefreshAt === 0 ? {} : { lastRefreshAtMs: lastRefreshAt },
|
|
3302
3870
|
...typeof record["nickname"] !== "string" || record["nickname"] === "" ? {} : { nickname: record["nickname"] },
|
|
3303
3871
|
...typeof record["uin"] !== "string" || record["uin"] === "" ? {} : { uin: record["uin"] },
|
|
3304
3872
|
...typeof record["uid"] !== "string" || record["uid"] === "" ? {} : { uid: record["uid"] },
|
|
@@ -3407,6 +3975,17 @@ var WorkBuddyAccountPool = class {
|
|
|
3407
3975
|
archiveQueue = Promise.resolve();
|
|
3408
3976
|
/** The one scan in progress; concurrent callers await it instead of duplicating it. */
|
|
3409
3977
|
scanInflight;
|
|
3978
|
+
/** Torn-down sign-ins, read on first use: a scan cannot resurrect what is here. */
|
|
3979
|
+
/**
|
|
3980
|
+
* Cached tombstones, with the file stamp they were read at.
|
|
3981
|
+
*
|
|
3982
|
+
* A second pool over the same data directory writes this file too. Serving a
|
|
3983
|
+
* cache read before that write and then writing it back verbatim dropped the
|
|
3984
|
+
* other pool's deletions, and a deleted account came back the next time the
|
|
3985
|
+
* client's snapshot was adopted. The stamp makes a write elsewhere visible.
|
|
3986
|
+
*/
|
|
3987
|
+
deletedSignIns;
|
|
3988
|
+
deletedSignInsStamp;
|
|
3410
3989
|
/** The directories the in-flight scan is reading, so a repoint supersedes it. */
|
|
3411
3990
|
scanDirs = [];
|
|
3412
3991
|
/**
|
|
@@ -3429,6 +4008,16 @@ var WorkBuddyAccountPool = class {
|
|
|
3429
4008
|
*/
|
|
3430
4009
|
heldIds = /* @__PURE__ */ new Map();
|
|
3431
4010
|
/**
|
|
4011
|
+
* Accounts the desktop client is signed into right now.
|
|
4012
|
+
*
|
|
4013
|
+
* The pool renews one of these into its own directory when the app's session
|
|
4014
|
+
* runs out, and serves that copy until the app writes a newer one. Renewing it
|
|
4015
|
+
* does not make it the pool's own account: the app still holds the session, and
|
|
4016
|
+
* a delete would hide the account from the pool while the user believed they
|
|
4017
|
+
* were removing something the plugin had added.
|
|
4018
|
+
*/
|
|
4019
|
+
appHeldIds = /* @__PURE__ */ new Set();
|
|
4020
|
+
/**
|
|
3432
4021
|
* The sign-ins each account's own files hold, as of the last scan.
|
|
3433
4022
|
*
|
|
3434
4023
|
* Only the files the client and the panel write, not the archive: the archive
|
|
@@ -3439,10 +4028,11 @@ var WorkBuddyAccountPool = class {
|
|
|
3439
4028
|
/**
|
|
3440
4029
|
* Sign-ins the upstream refused, per account.
|
|
3441
4030
|
*
|
|
3442
|
-
*
|
|
3443
|
-
*
|
|
3444
|
-
*
|
|
3445
|
-
*
|
|
4031
|
+
* A refused token must not come back through the ranking: the copies are
|
|
4032
|
+
* compared by issue time, and a token the upstream rejected can be the newest
|
|
4033
|
+
* one (a re-login that did not work, a sign-in copied from another machine).
|
|
4034
|
+
* Without this the scan would serve it again, and the hold would look released
|
|
4035
|
+
* while the requests kept failing. Kept across scans, and
|
|
3446
4036
|
* dropped once the sign-in is no longer on disk or the cooldowns are reset.
|
|
3447
4037
|
*/
|
|
3448
4038
|
refusedSignIns = /* @__PURE__ */ new Map();
|
|
@@ -3531,10 +4121,16 @@ var WorkBuddyAccountPool = class {
|
|
|
3531
4121
|
* True when this account's sign-in is one the plugin wrote, so it may be
|
|
3532
4122
|
* deleted. The panel asks before it offers the button: deleting a desktop
|
|
3533
4123
|
* app's own sign-in would sign the user out of that app.
|
|
4124
|
+
*
|
|
4125
|
+
* A refreshed desktop sign-in does not qualify. The pool holds a copy of it
|
|
4126
|
+
* after a renewal, but the app is still signed in, so the row stays out of
|
|
4127
|
+
* reach: removing it would suppress an account the user only ever added to the
|
|
4128
|
+
* app, and nothing on that row says so.
|
|
3534
4129
|
*/
|
|
3535
4130
|
ownsSignIn(accountId) {
|
|
3536
4131
|
const account = this.accounts.find((item) => item.id === accountId);
|
|
3537
|
-
|
|
4132
|
+
if (account === void 0 || this.appHeldIds.has(accountId)) return false;
|
|
4133
|
+
return this.isOwnSignInPath(account.credential.sourcePath);
|
|
3538
4134
|
}
|
|
3539
4135
|
/**
|
|
3540
4136
|
* Delete one account's sign-in, and its archive entry with it.
|
|
@@ -3553,22 +4149,233 @@ var WorkBuddyAccountPool = class {
|
|
|
3553
4149
|
async removeOwnSignIn(accountId) {
|
|
3554
4150
|
const account = this.accounts.find((item) => item.id === accountId);
|
|
3555
4151
|
if (account === void 0) return false;
|
|
3556
|
-
if (!this.
|
|
4152
|
+
if (!this.ownsSignIn(accountId)) return false;
|
|
3557
4153
|
const running = this.scanInflight;
|
|
4154
|
+
const known = (await this.loadDeletedSignIns()).has(account.id);
|
|
4155
|
+
if (!await this.rememberDeletedSignIn(account.id, account.credential)) throw new Error("could not record the deleted sign-in");
|
|
3558
4156
|
if (running !== void 0) {
|
|
3559
4157
|
if (!await raceWithTimeout(running.catch(() => {}), DELETE_SCAN_WAIT_MS)) running.catch(() => {}).then(() => this.forgetArchived(account.id));
|
|
3560
4158
|
}
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
})
|
|
4159
|
+
try {
|
|
4160
|
+
await unlink(account.credential.sourcePath);
|
|
4161
|
+
} catch (error) {
|
|
4162
|
+
if (error.code !== "ENOENT") {
|
|
4163
|
+
if (!known) {
|
|
4164
|
+
(await this.loadDeletedSignIns()).delete(account.id);
|
|
4165
|
+
await this.writeDeletedSignIns([account.id]);
|
|
4166
|
+
}
|
|
4167
|
+
throw error;
|
|
4168
|
+
}
|
|
4169
|
+
}
|
|
3564
4170
|
await this.forgetArchived(account.id);
|
|
4171
|
+
this.forgetAccount(account.id);
|
|
3565
4172
|
return true;
|
|
3566
4173
|
}
|
|
3567
|
-
/**
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
4174
|
+
/** Drop one account and everything the pool remembers about it. */
|
|
4175
|
+
forgetAccount(accountId) {
|
|
4176
|
+
this.accounts = this.accounts.filter((item) => item.id !== accountId);
|
|
4177
|
+
this.noRefreshWarned.delete(accountId);
|
|
4178
|
+
this.creditBalances.delete(accountId);
|
|
4179
|
+
this.creditExpiries.delete(accountId);
|
|
4180
|
+
this.heldIds.delete(accountId);
|
|
4181
|
+
this.signInTokens.delete(accountId);
|
|
4182
|
+
this.refusedSignIns.delete(accountId);
|
|
4183
|
+
}
|
|
4184
|
+
/**
|
|
4185
|
+
* Record one deleted sign-in, so no later scan puts the account back.
|
|
4186
|
+
*
|
|
4187
|
+
* The file is written before the delete is reported: a tombstone that only
|
|
4188
|
+
* lived in memory would be gone after a restart, and the snapshot it has to
|
|
4189
|
+
* hold off is still sitting in the client's directory.
|
|
4190
|
+
*/
|
|
4191
|
+
async rememberDeletedSignIn(id, credential) {
|
|
4192
|
+
const tombstones = await this.loadDeletedSignIns();
|
|
4193
|
+
const previous = tombstones.get(id);
|
|
4194
|
+
tombstones.set(id, {
|
|
4195
|
+
tokenDigest: tokenDigest(credential.accessToken),
|
|
4196
|
+
lastRefreshAtMs: credential.lastRefreshAtMs ?? 0,
|
|
4197
|
+
deletedAtMs: Date.now()
|
|
4198
|
+
});
|
|
4199
|
+
if (await this.writeDeletedSignIns()) return true;
|
|
4200
|
+
if (previous === void 0) tombstones.delete(id);
|
|
4201
|
+
else tombstones.set(id, previous);
|
|
4202
|
+
return false;
|
|
4203
|
+
}
|
|
4204
|
+
/**
|
|
4205
|
+
* Read the tombstones, re-reading when the file changed underneath us.
|
|
4206
|
+
*
|
|
4207
|
+
* Nothing is pruned by age. A deleted sign-in's copy is still lying in the
|
|
4208
|
+
* client's directory until the client overwrites it, so a tombstone that
|
|
4209
|
+
* expired would bring that copy straight back, refresh token included.
|
|
4210
|
+
*/
|
|
4211
|
+
async loadDeletedSignIns() {
|
|
4212
|
+
const file = this.deletedSignInsFile();
|
|
4213
|
+
const stamp = file === void 0 ? "" : await signInFileStamp(file);
|
|
4214
|
+
if (this.deletedSignIns !== void 0 && this.deletedSignInsStamp === stamp) return this.deletedSignIns;
|
|
4215
|
+
if (file === void 0) {
|
|
4216
|
+
this.deletedSignInsStamp = "";
|
|
4217
|
+
this.deletedSignIns = /* @__PURE__ */ new Map();
|
|
4218
|
+
return this.deletedSignIns;
|
|
4219
|
+
}
|
|
4220
|
+
try {
|
|
4221
|
+
const tombstones = await readDeletedSignInsFile(file);
|
|
4222
|
+
this.deletedSignInsStamp = stamp;
|
|
4223
|
+
this.deletedSignIns = tombstones;
|
|
4224
|
+
return tombstones;
|
|
4225
|
+
} catch (error) {
|
|
4226
|
+
if (error.code === "ENOENT") {
|
|
4227
|
+
this.deletedSignInsStamp = stamp;
|
|
4228
|
+
this.deletedSignIns = /* @__PURE__ */ new Map();
|
|
4229
|
+
return this.deletedSignIns;
|
|
4230
|
+
}
|
|
4231
|
+
this.logger?.warn?.(`${LOG_PREFIX}: could not read the deleted sign-ins: ${String(error)}`);
|
|
4232
|
+
return this.deletedSignIns ?? /* @__PURE__ */ new Map();
|
|
4233
|
+
}
|
|
4234
|
+
}
|
|
4235
|
+
/**
|
|
4236
|
+
* Persist the tombstone set; the caller has already updated the map.
|
|
4237
|
+
*
|
|
4238
|
+
* What is on disk is merged in first, except for the ids this call is removing:
|
|
4239
|
+
* a second pool over the same data directory writes this same file, and writing
|
|
4240
|
+
* our map verbatim dropped a delete it had already recorded. The removals are the
|
|
4241
|
+
* other half of that: a re-login ends a tombstone and a failed delete rolls one
|
|
4242
|
+
* back, and a pure union put both straight back.
|
|
4243
|
+
*
|
|
4244
|
+
* Answers false when the file could not be written, so a delete can refuse to
|
|
4245
|
+
* report success on a state it could not record.
|
|
4246
|
+
*/
|
|
4247
|
+
async writeDeletedSignIns(removed = []) {
|
|
4248
|
+
const tombstones = this.deletedSignIns;
|
|
4249
|
+
const file = this.deletedSignInsFile();
|
|
4250
|
+
if (tombstones === void 0 || file === void 0) return true;
|
|
4251
|
+
let written = false;
|
|
4252
|
+
await withFileLock(`${file}.lock`, async () => {
|
|
4253
|
+
const onDisk = await readDeletedSignInsFile(file).catch(() => /* @__PURE__ */ new Map());
|
|
4254
|
+
for (const [id, entry] of onDisk) {
|
|
4255
|
+
if (removed.includes(id)) continue;
|
|
4256
|
+
const ours = tombstones.get(id);
|
|
4257
|
+
if (ours === void 0 || entry.deletedAtMs > ours.deletedAtMs) tombstones.set(id, entry);
|
|
4258
|
+
}
|
|
4259
|
+
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
4260
|
+
try {
|
|
4261
|
+
await mkdir(dirname(file), { recursive: true });
|
|
4262
|
+
await writeFile(temporary, JSON.stringify({
|
|
4263
|
+
version: 1,
|
|
4264
|
+
entries: Object.fromEntries(tombstones)
|
|
4265
|
+
}, null, 2), { mode: 384 });
|
|
4266
|
+
await rename(temporary, file);
|
|
4267
|
+
this.deletedSignInsStamp = await signInFileStamp(file);
|
|
4268
|
+
written = true;
|
|
4269
|
+
} catch (error) {
|
|
4270
|
+
await unlink(temporary).catch(() => {});
|
|
4271
|
+
this.logger?.warn?.(`${LOG_PREFIX}: could not record the deleted sign-ins: ${String(error)}`);
|
|
4272
|
+
}
|
|
4273
|
+
});
|
|
4274
|
+
return written;
|
|
4275
|
+
}
|
|
4276
|
+
deletedSignInsFile() {
|
|
4277
|
+
const directory = this.dataPath?.();
|
|
4278
|
+
return directory === void 0 || directory === "" ? void 0 : join(directory, DELETED_SIGN_INS_FILENAME);
|
|
4279
|
+
}
|
|
4280
|
+
/** Whether the pool recorded this sign-in as refused by the upstream. */
|
|
4281
|
+
signInRefused(id, token) {
|
|
4282
|
+
if (this.refusedSignIns.get(id)?.includes(token) ?? false) return true;
|
|
4283
|
+
const hold = this.heldIds.get(id);
|
|
4284
|
+
return hold?.reason === "session" && hold.heldSignIns.includes(token);
|
|
4285
|
+
}
|
|
4286
|
+
/**
|
|
4287
|
+
* Every sign-in a refusal could have named for this account.
|
|
4288
|
+
*
|
|
4289
|
+
* The files the last walk read, plus the credential in force when it came from
|
|
4290
|
+
* somewhere else (the archive, or a token this process minted before it was
|
|
4291
|
+
* written to disk). Naming only the files left an account served from the
|
|
4292
|
+
* archive with an empty proof, and a hold with an empty proof is released by
|
|
4293
|
+
* the first scan that follows it.
|
|
4294
|
+
*/
|
|
4295
|
+
refusedSignInsFor(id) {
|
|
4296
|
+
const tokens = (this.signInTokens.get(id) ?? []).map((entry) => entry.accessToken);
|
|
4297
|
+
const inForce = this.accounts.find((account) => account.id === id)?.credential.accessToken;
|
|
4298
|
+
if (inForce !== void 0 && inForce !== "" && !tokens.includes(inForce)) tokens.push(inForce);
|
|
4299
|
+
return tokens;
|
|
4300
|
+
}
|
|
4301
|
+
/** Whether `sourcePath` sits inside the directory the plugin writes into. */
|
|
4302
|
+
isOwnSignInPath(sourcePath) {
|
|
4303
|
+
return ownSignInPath(sourcePath, ownSignInDir(this.dataPath?.() ?? ""));
|
|
4304
|
+
}
|
|
4305
|
+
/**
|
|
4306
|
+
* Take over the sign-ins the desktop client left behind.
|
|
4307
|
+
*
|
|
4308
|
+
* The client keeps one live sign-in and a stack of superseded snapshots beside
|
|
4309
|
+
* it. A snapshot's token is a session the client itself stopped using, so
|
|
4310
|
+
* nothing renews it: it serves until its access token expires and then the
|
|
4311
|
+
* account is gone, holding a refresh token the client's own rotation already
|
|
4312
|
+
* spent. Copying it into the directory the plugin writes makes it the
|
|
4313
|
+
* plugin's account, renewed by the same age rule a panel-added sign-in gets,
|
|
4314
|
+
* so the account outlives the snapshot it came from.
|
|
4315
|
+
*
|
|
4316
|
+
* Only a superseded snapshot is taken over. The live sign-in stays the
|
|
4317
|
+
* client's: it refreshes it, and a second holder of the same session
|
|
4318
|
+
* refreshing on its own schedule is how both end up with a spent refresh
|
|
4319
|
+
* token. An account the client is signed into is skipped for the same reason.
|
|
4320
|
+
*/
|
|
4321
|
+
async adoptOrphanedSignIns(found, tornDown) {
|
|
4322
|
+
const directory = ownSignInDir(this.dataPath?.() ?? "");
|
|
4323
|
+
if (directory === "") return [];
|
|
4324
|
+
const liveIds = /* @__PURE__ */ new Set();
|
|
4325
|
+
const ownedIds = /* @__PURE__ */ new Set();
|
|
4326
|
+
for (const credential of found) {
|
|
4327
|
+
const id = workbuddyAccountId(credential);
|
|
4328
|
+
if (isLiveAuthFile(credential.sourcePath)) liveIds.add(id);
|
|
4329
|
+
else if (ownSignInPath(credential.sourcePath, directory)) ownedIds.add(id);
|
|
4330
|
+
}
|
|
4331
|
+
const adopted = [];
|
|
4332
|
+
for (const credential of found) {
|
|
4333
|
+
if (isLiveAuthFile(credential.sourcePath)) continue;
|
|
4334
|
+
if (ownSignInPath(credential.sourcePath, directory)) continue;
|
|
4335
|
+
const id = workbuddyAccountId(credential);
|
|
4336
|
+
if (liveIds.has(id) || ownedIds.has(id) || tornDown.has(id)) continue;
|
|
4337
|
+
if ((credential.uin ?? credential.uid ?? credential.nickname ?? "") === "") continue;
|
|
4338
|
+
let file;
|
|
4339
|
+
try {
|
|
4340
|
+
file = await writeOwnSignIn(directory, {
|
|
4341
|
+
...ownSignInOf(credential),
|
|
4342
|
+
lastRefreshAtMs: credential.lastRefreshAtMs ?? 0
|
|
4343
|
+
});
|
|
4344
|
+
} catch (error) {
|
|
4345
|
+
this.logger?.warn?.(`${LOG_PREFIX}: could not take over ${accountLabel(credential)}: ${String(error)}`);
|
|
4346
|
+
continue;
|
|
4347
|
+
}
|
|
4348
|
+
ownedIds.add(id);
|
|
4349
|
+
adopted.push({
|
|
4350
|
+
...credential,
|
|
4351
|
+
sourcePath: file
|
|
4352
|
+
});
|
|
4353
|
+
}
|
|
4354
|
+
return adopted;
|
|
4355
|
+
}
|
|
4356
|
+
/**
|
|
4357
|
+
* Write a renewed token back into the sign-in file it was read from.
|
|
4358
|
+
*
|
|
4359
|
+
* Best effort: a failed write costs the renewal its persistence, never the
|
|
4360
|
+
* request already in flight. Anything that did not come from the plugin's own
|
|
4361
|
+
* directory is left alone - the desktop app owns its files.
|
|
4362
|
+
*/
|
|
4363
|
+
async renewOwnSignIn(credential) {
|
|
4364
|
+
if (this.isOwnSignInPath(credential.sourcePath)) {
|
|
4365
|
+
try {
|
|
4366
|
+
await renewOwnSignInFile(credential.sourcePath, credential);
|
|
4367
|
+
} catch (error) {
|
|
4368
|
+
this.logger?.warn?.(`${LOG_PREFIX}: could not write the renewed token back for ${credential.nickname ?? credential.uid}: ${String(error)}`);
|
|
4369
|
+
}
|
|
4370
|
+
return;
|
|
4371
|
+
}
|
|
4372
|
+
const directory = ownSignInDir(this.dataPath?.() ?? "");
|
|
4373
|
+
if (directory === "") return;
|
|
4374
|
+
try {
|
|
4375
|
+
credential.sourcePath = await writeOwnSignIn(directory, ownSignInOf(credential));
|
|
4376
|
+
} catch (error) {
|
|
4377
|
+
this.logger?.warn?.(`${LOG_PREFIX}: could not keep the renewed token for ${credential.nickname ?? credential.uid}: ${String(error)}`);
|
|
4378
|
+
}
|
|
3572
4379
|
}
|
|
3573
4380
|
/**
|
|
3574
4381
|
* Drop one account from the credential archive.
|
|
@@ -3595,10 +4402,11 @@ var WorkBuddyAccountPool = class {
|
|
|
3595
4402
|
/** Drop every account, e.g. when the source is switched off. A later `scan()` rebuilds from disk. */
|
|
3596
4403
|
clear() {
|
|
3597
4404
|
this.accounts = [];
|
|
4405
|
+
this.appHeldIds = /* @__PURE__ */ new Set();
|
|
3598
4406
|
}
|
|
3599
4407
|
/** Rescan the auth directories and merge newly discovered accounts. */
|
|
3600
4408
|
async scan() {
|
|
3601
|
-
const dirs =
|
|
4409
|
+
const dirs = this.scanDirsFor(this.authDirs);
|
|
3602
4410
|
if (this.scanInflight !== void 0 && sameDirs(this.scanDirs, dirs)) return this.scanInflight;
|
|
3603
4411
|
const inflight = this.scanOnce(dirs).finally(() => {
|
|
3604
4412
|
if (this.scanInflight === inflight) {
|
|
@@ -3611,6 +4419,21 @@ var WorkBuddyAccountPool = class {
|
|
|
3611
4419
|
return inflight;
|
|
3612
4420
|
}
|
|
3613
4421
|
/**
|
|
4422
|
+
* The directories a walk reads: the configured ones plus the plugin's own.
|
|
4423
|
+
*
|
|
4424
|
+
* The pool writes sign-ins into its own directory (a renewal it claims, an
|
|
4425
|
+
* account it takes over), so it has to read that directory too, whether or not
|
|
4426
|
+
* the host listed it. Read in both places through this one function: a set that
|
|
4427
|
+
* disagreed with itself read as "the auth directories were repointed mid-walk"
|
|
4428
|
+
* and threw the whole walk away.
|
|
4429
|
+
*/
|
|
4430
|
+
scanDirsFor(authDirs) {
|
|
4431
|
+
const dirs = [...authDirs];
|
|
4432
|
+
const own = ownSignInDir(this.dataPath?.() ?? "");
|
|
4433
|
+
if (own !== "" && !dirs.some((dir) => resolve(dir) === resolve(own))) dirs.push(own);
|
|
4434
|
+
return dirs;
|
|
4435
|
+
}
|
|
4436
|
+
/**
|
|
3614
4437
|
* Keep the credentials this walk saw, so an account whose only file the client
|
|
3615
4438
|
* overwrites on the next sign-in still has a copy to be read back from.
|
|
3616
4439
|
*
|
|
@@ -3619,13 +4442,14 @@ var WorkBuddyAccountPool = class {
|
|
|
3619
4442
|
async archiveCredentials(credentials) {
|
|
3620
4443
|
const file = this.archiveFile();
|
|
3621
4444
|
if (file === void 0 || credentials.length === 0) return;
|
|
4445
|
+
const ownDir = ownSignInDir(this.dataPath?.() ?? "");
|
|
3622
4446
|
const run = this.archiveQueue.then(async () => {
|
|
3623
4447
|
const stored = await readArchivedCredentials(file);
|
|
3624
4448
|
let changed = false;
|
|
3625
4449
|
for (const credential of credentials) {
|
|
3626
4450
|
const id = workbuddyAccountId(credential);
|
|
3627
4451
|
const previous = stored.get(id);
|
|
3628
|
-
if (previous !== void 0 && !isFresher(credential, previous)) continue;
|
|
4452
|
+
if (previous !== void 0 && !isFresher(credential, previous, ownDir)) continue;
|
|
3629
4453
|
stored.set(id, credential);
|
|
3630
4454
|
changed = true;
|
|
3631
4455
|
}
|
|
@@ -3671,7 +4495,31 @@ var WorkBuddyAccountPool = class {
|
|
|
3671
4495
|
else if (read.kind === "sealed") sealedDirs.add(resolve(dir));
|
|
3672
4496
|
}
|
|
3673
4497
|
}
|
|
4498
|
+
const tombstones = await this.loadDeletedSignIns();
|
|
4499
|
+
const tornDown = /* @__PURE__ */ new Set();
|
|
4500
|
+
const releasedIds = [];
|
|
4501
|
+
const held = [];
|
|
4502
|
+
for (const credential of found) {
|
|
4503
|
+
const id = workbuddyAccountId(credential);
|
|
4504
|
+
const tombstone = tombstones.get(id);
|
|
4505
|
+
if (tombstone === void 0) {
|
|
4506
|
+
held.push(credential);
|
|
4507
|
+
continue;
|
|
4508
|
+
}
|
|
4509
|
+
if (!endsTombstone(tombstone, credential)) {
|
|
4510
|
+
tornDown.add(id);
|
|
4511
|
+
continue;
|
|
4512
|
+
}
|
|
4513
|
+
tombstones.delete(id);
|
|
4514
|
+
releasedIds.push(id);
|
|
4515
|
+
held.push(credential);
|
|
4516
|
+
}
|
|
4517
|
+
found.length = 0;
|
|
4518
|
+
found.push(...held);
|
|
4519
|
+
for (const id of tombstones.keys()) tornDown.add(id);
|
|
4520
|
+
if (releasedIds.length > 0) await this.writeDeletedSignIns(releasedIds);
|
|
3674
4521
|
await this.archiveCredentials(found);
|
|
4522
|
+
found.push(...await this.adoptOrphanedSignIns(found, tornDown));
|
|
3675
4523
|
const signInTokens = /* @__PURE__ */ new Map();
|
|
3676
4524
|
for (const credential of found) {
|
|
3677
4525
|
const id = workbuddyAccountId(credential);
|
|
@@ -3680,8 +4528,11 @@ var WorkBuddyAccountPool = class {
|
|
|
3680
4528
|
else held.push(credential);
|
|
3681
4529
|
}
|
|
3682
4530
|
const liveIds = new Set(found.map((credential) => workbuddyAccountId(credential)));
|
|
4531
|
+
const appHeld = new Set(found.filter((credential) => isLiveAuthFile(credential.sourcePath)).map((credential) => workbuddyAccountId(credential)));
|
|
3683
4532
|
for (const credential of await this.archivedCredentials(readableDirs)) {
|
|
3684
|
-
|
|
4533
|
+
const id = workbuddyAccountId(credential);
|
|
4534
|
+
if (tornDown.has(id)) continue;
|
|
4535
|
+
if (found.some((entry) => workbuddyAccountId(entry) === id && entry.accessToken === credential.accessToken)) continue;
|
|
3685
4536
|
found.push(credential);
|
|
3686
4537
|
}
|
|
3687
4538
|
const byId = /* @__PURE__ */ new Map();
|
|
@@ -3695,7 +4546,8 @@ var WorkBuddyAccountPool = class {
|
|
|
3695
4546
|
label: accountLabel(credential),
|
|
3696
4547
|
credential,
|
|
3697
4548
|
cooldownUntilMs: 0,
|
|
3698
|
-
modelCooldowns:
|
|
4549
|
+
modelCooldowns: Object.create(null),
|
|
4550
|
+
modelBlocks: Object.create(null),
|
|
3699
4551
|
rateLimitHits: 0
|
|
3700
4552
|
});
|
|
3701
4553
|
continue;
|
|
@@ -3709,8 +4561,16 @@ var WorkBuddyAccountPool = class {
|
|
|
3709
4561
|
existing.credential = replaced;
|
|
3710
4562
|
this.refusedSignIns.set(id, heldSignIns);
|
|
3711
4563
|
}
|
|
3712
|
-
|
|
3713
|
-
|
|
4564
|
+
if (!this.signInRefused(id, credential.accessToken) && (this.signInRefused(id, existing.credential.accessToken) || isFresher(credential, existing.credential, ownSignInDir(this.dataPath?.() ?? "")))) existing.credential = credential;
|
|
4565
|
+
}
|
|
4566
|
+
for (const [id, account] of byId) {
|
|
4567
|
+
const hold = this.heldIds.get(id);
|
|
4568
|
+
if (hold?.reason !== "session") continue;
|
|
4569
|
+
if (hold.heldSignIns.length === 0) continue;
|
|
4570
|
+
if (this.signInRefused(id, account.credential.accessToken)) continue;
|
|
4571
|
+
this.heldIds.delete(id);
|
|
4572
|
+
if (account.cooldownUntilMs > 0 && account.cooldownUntilMs <= hold.until) account.cooldownUntilMs = 0;
|
|
4573
|
+
this.logger?.info?.(`${LOG_PREFIX}: ${account.label} has a sign-in the upstream did not refuse; releasing the hold`);
|
|
3714
4574
|
}
|
|
3715
4575
|
const foundIds = new Set(found.map((credential) => workbuddyAccountId(credential)));
|
|
3716
4576
|
for (const [id, refused] of this.refusedSignIns) {
|
|
@@ -3728,23 +4588,21 @@ var WorkBuddyAccountPool = class {
|
|
|
3728
4588
|
if (sealedDirs.has(home)) continue;
|
|
3729
4589
|
if (readableDirs.has(home) && !foundIds.has(id)) {
|
|
3730
4590
|
byId.delete(id);
|
|
3731
|
-
this.
|
|
3732
|
-
this.creditBalances.delete(id);
|
|
3733
|
-
this.creditExpiries.delete(id);
|
|
3734
|
-
this.heldIds.delete(id);
|
|
3735
|
-
this.signInTokens.delete(id);
|
|
3736
|
-
this.refusedSignIns.delete(id);
|
|
4591
|
+
this.forgetAccount(id);
|
|
3737
4592
|
}
|
|
3738
4593
|
}
|
|
4594
|
+
this.appHeldIds = appHeld;
|
|
3739
4595
|
const ordered = [...byId.values()];
|
|
3740
|
-
ordered.sort((a, b) =>
|
|
4596
|
+
ordered.sort((a, b) => compareQueueOrder(a.credential, b.credential, ownSignInDir(this.dataPath?.() ?? "")));
|
|
3741
4597
|
for (const account of ordered) {
|
|
3742
4598
|
account.label = accountLabel(account.credential);
|
|
4599
|
+
const home = resolve(dirname(account.credential.sourcePath));
|
|
4600
|
+
if (!readableDirs.has(home) || sealedDirs.has(home)) continue;
|
|
3743
4601
|
if (liveIds.has(account.id)) delete account.archived;
|
|
3744
4602
|
else account.archived = true;
|
|
3745
4603
|
}
|
|
3746
4604
|
disambiguateLabels(ordered);
|
|
3747
|
-
if (!sameDirs(this.authDirs, dirs)) return this.accounts;
|
|
4605
|
+
if (!sameDirs(this.scanDirsFor(this.authDirs), dirs)) return this.accounts;
|
|
3748
4606
|
this.accounts = ordered;
|
|
3749
4607
|
this.signInTokens = signInTokens;
|
|
3750
4608
|
this.lastScanAtMs = Date.now();
|
|
@@ -3757,6 +4615,28 @@ var WorkBuddyAccountPool = class {
|
|
|
3757
4615
|
return kept.filter((account) => regionOf(account.credential.domain) === region);
|
|
3758
4616
|
}
|
|
3759
4617
|
/**
|
|
4618
|
+
* Why this account cannot serve a request, or null when it can.
|
|
4619
|
+
*
|
|
4620
|
+
* One ladder, two readers: `available` filters with it and
|
|
4621
|
+
* `unavailableReason` prints its answer. Keeping them separate meant every
|
|
4622
|
+
* new disqualifier had to be added in two places, and forgetting one made the
|
|
4623
|
+
* panel call an account available that the rotation would never pick.
|
|
4624
|
+
*/
|
|
4625
|
+
ineligible(account, now, modelId, region) {
|
|
4626
|
+
if (this.removedIds.has(account.id)) return "removed";
|
|
4627
|
+
if (this.disabledIds.has(account.id)) return "switched off";
|
|
4628
|
+
if (this.reserveHolds(account.id)) return "under its credit reserve";
|
|
4629
|
+
if (credentialUnusable(account.credential, now)) return "sign-in expired";
|
|
4630
|
+
if (region !== void 0 && regionOf(account.credential.domain) !== region) return "other region";
|
|
4631
|
+
if (account.cooldownUntilMs > now) {
|
|
4632
|
+
const reason = this.heldIds.get(account.id)?.reason;
|
|
4633
|
+
return reason === "session" ? "sign-in refused" : reason === "restricted" ? "restricted by the upstream" : "on hold";
|
|
4634
|
+
}
|
|
4635
|
+
if (modelId !== void 0 && (account.modelBlocks[modelId] ?? 0) > now) return "plan";
|
|
4636
|
+
if (modelId !== void 0 && (account.modelCooldowns[modelId] ?? 0) > now) return "rate-limited";
|
|
4637
|
+
return null;
|
|
4638
|
+
}
|
|
4639
|
+
/**
|
|
3760
4640
|
* Accounts currently eligible to serve a request.
|
|
3761
4641
|
*
|
|
3762
4642
|
* With a `modelId`, an account is eligible when it is not account-wide cooled
|
|
@@ -3769,16 +4649,22 @@ var WorkBuddyAccountPool = class {
|
|
|
3769
4649
|
* for.
|
|
3770
4650
|
*/
|
|
3771
4651
|
available(now, modelId, region) {
|
|
3772
|
-
return this.accounts.filter((account) =>
|
|
3773
|
-
|
|
3774
|
-
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
3779
|
-
|
|
3780
|
-
|
|
3781
|
-
|
|
4652
|
+
return this.accounts.filter((account) => this.ineligible(account, now, modelId, region) === null);
|
|
4653
|
+
}
|
|
4654
|
+
/**
|
|
4655
|
+
* The pool's own hold on this account, if one is in force right now.
|
|
4656
|
+
*
|
|
4657
|
+
* A caller that is about to spend upstream calls on an account asks first: a
|
|
4658
|
+
* held sign-in cannot answer, so the read is skipped and the hold's reason is
|
|
4659
|
+
* reported instead. Null when no hold applies or the hold has run out.
|
|
4660
|
+
*/
|
|
4661
|
+
activeHold(accountId) {
|
|
4662
|
+
const hold = this.heldIds.get(accountId);
|
|
4663
|
+
if (hold === void 0 || hold.until <= Date.now()) return null;
|
|
4664
|
+
return {
|
|
4665
|
+
reason: hold.reason,
|
|
4666
|
+
untilMs: hold.until
|
|
4667
|
+
};
|
|
3782
4668
|
}
|
|
3783
4669
|
/**
|
|
3784
4670
|
* Why this account is not being served right now, for a caller that has to
|
|
@@ -3791,13 +4677,7 @@ var WorkBuddyAccountPool = class {
|
|
|
3791
4677
|
unavailableReason(accountId, now, modelId) {
|
|
3792
4678
|
const account = this.accounts.find((item) => item.id === accountId);
|
|
3793
4679
|
if (account === void 0) return null;
|
|
3794
|
-
|
|
3795
|
-
if (this.disabledIds.has(account.id)) return "switched off";
|
|
3796
|
-
if (this.reserveHolds(account.id)) return "under its credit reserve";
|
|
3797
|
-
if (credentialUnusable(account.credential, now)) return "sign-in expired";
|
|
3798
|
-
if (account.cooldownUntilMs > now) return this.heldIds.get(account.id)?.reason === "session" ? "sign-in refused" : "on hold";
|
|
3799
|
-
if (modelId !== void 0 && (account.modelCooldowns[modelId] ?? 0) > now) return "rate-limited";
|
|
3800
|
-
return null;
|
|
4680
|
+
return this.ineligible(account, now, modelId);
|
|
3801
4681
|
}
|
|
3802
4682
|
/**
|
|
3803
4683
|
* Pick the account to serve a request.
|
|
@@ -3821,19 +4701,23 @@ var WorkBuddyAccountPool = class {
|
|
|
3821
4701
|
* Scans on first use, and rescans when every known account is cooling down: a
|
|
3822
4702
|
* fresh desktop login is the usual way out of an exhausted pool.
|
|
3823
4703
|
*/
|
|
3824
|
-
async acquire(modelId, region, conversationKey) {
|
|
4704
|
+
async acquire(modelId, region, conversationKey, excludeIds) {
|
|
4705
|
+
const eligibleNow = () => {
|
|
4706
|
+
const list = this.available(Date.now(), modelId, region);
|
|
4707
|
+
return excludeIds === void 0 ? list : list.filter((item) => !excludeIds.has(item.id));
|
|
4708
|
+
};
|
|
3825
4709
|
if (this.accounts.length === 0) await this.scan();
|
|
3826
|
-
let pool =
|
|
4710
|
+
let pool = eligibleNow();
|
|
3827
4711
|
if (pool.length === 0) {
|
|
3828
4712
|
await this.scan();
|
|
3829
|
-
pool =
|
|
4713
|
+
pool = eligibleNow();
|
|
3830
4714
|
}
|
|
3831
4715
|
if (pool.length === 0) return void 0;
|
|
3832
4716
|
const preferredId = this.preferredIds.get(region);
|
|
3833
4717
|
const account = pool.find((item) => item.id === preferredId) ?? this.pick(pool, conversationKey);
|
|
3834
4718
|
if (account === void 0) return void 0;
|
|
3835
4719
|
await this.ensureFresh(account);
|
|
3836
|
-
const eligible =
|
|
4720
|
+
const eligible = eligibleNow();
|
|
3837
4721
|
if (eligible.includes(account)) return account;
|
|
3838
4722
|
const next = eligible.find((item) => item.id === preferredId) ?? this.pick(eligible, conversationKey);
|
|
3839
4723
|
if (next === void 0) return void 0;
|
|
@@ -3882,11 +4766,85 @@ var WorkBuddyAccountPool = class {
|
|
|
3882
4766
|
* token is unchanged, so the caller stops retrying the same dead token.
|
|
3883
4767
|
*/
|
|
3884
4768
|
async refreshAccount(accountId) {
|
|
4769
|
+
return await this.refreshAccountOutcome(accountId) === "refreshed";
|
|
4770
|
+
}
|
|
4771
|
+
/**
|
|
4772
|
+
* The same refresh, keeping a refusal apart from a failure.
|
|
4773
|
+
*
|
|
4774
|
+
* The caller needs the difference: only the upstream turning the sign-in down
|
|
4775
|
+
* justifies a six-hour hold, while a timeout or an unreachable host says
|
|
4776
|
+
* nothing about the token and must leave the account in rotation.
|
|
4777
|
+
*/
|
|
4778
|
+
async refreshAccountOutcome(accountId) {
|
|
4779
|
+
return await this.refreshForRetry(accountId);
|
|
4780
|
+
}
|
|
4781
|
+
/**
|
|
4782
|
+
* Refresh after a session-dead answer, keeping a refusal apart from a failure.
|
|
4783
|
+
*
|
|
4784
|
+
* Only the upstream turning the sign-in down justifies a hold. A timeout, a
|
|
4785
|
+
* rate limit or an unreachable host says nothing about the token, and parking
|
|
4786
|
+
* the account for six hours over one bad round trip took a healthy account out
|
|
4787
|
+
* of rotation until a manual reset.
|
|
4788
|
+
*/
|
|
4789
|
+
async refreshForRetry(accountId) {
|
|
3885
4790
|
const account = this.accounts.find((item) => item.id === accountId);
|
|
3886
|
-
if (account === void 0) return
|
|
4791
|
+
if (account === void 0) return "failed";
|
|
3887
4792
|
const previous = account.credential.accessToken;
|
|
3888
|
-
|
|
3889
|
-
|
|
4793
|
+
if (account.credential.refreshToken === "") return "refused";
|
|
4794
|
+
const outcome = await this.ensureFresh(account, true);
|
|
4795
|
+
if (account.credential.accessToken !== previous) return "refreshed";
|
|
4796
|
+
return outcome === "failed" ? "failed" : "refused";
|
|
4797
|
+
}
|
|
4798
|
+
/**
|
|
4799
|
+
* Hold an account whose sign-in the upstream refuses.
|
|
4800
|
+
*
|
|
4801
|
+
* The chat gateway has answered a refused sign-in with this hold since it was
|
|
4802
|
+
* written; the panel's reads and the scheduled passes had no equivalent, so a
|
|
4803
|
+
* revoked account kept spending its two upstream calls on every poll and every
|
|
4804
|
+
* pass. The hold is what stops that, and it is released the way every other
|
|
4805
|
+
* hold is: a new sign-in for the account, a cooldown reset, or its cap.
|
|
4806
|
+
*/
|
|
4807
|
+
holdRefusedSignIn(accountId) {
|
|
4808
|
+
this.penalize(accountId, Date.now() + POOL_HOLD_MS, void 0, "session");
|
|
4809
|
+
}
|
|
4810
|
+
/**
|
|
4811
|
+
* One upstream read that a refused sign-in gets a second chance at.
|
|
4812
|
+
*
|
|
4813
|
+
* The upstream revokes the session a sign-in belongs to without rewriting the
|
|
4814
|
+
* token's own expiry, so an account the user switched away from in the desktop
|
|
4815
|
+
* app keeps a credential that reads as valid for weeks and answers 401 on
|
|
4816
|
+
* every call. A refresh token that still lives recovers the account on the
|
|
4817
|
+
* spot, which is the case the desktop app's own refresh leaves behind; one
|
|
4818
|
+
* that does not leaves the account held instead of retried forever.
|
|
4819
|
+
*
|
|
4820
|
+
* A failure of any other kind is rethrown untouched: a rate limit, a spent
|
|
4821
|
+
* balance and an upstream fault each have their own path, and none of them is
|
|
4822
|
+
* helped by a token this read did not blame.
|
|
4823
|
+
*
|
|
4824
|
+
* While the hold stands the read is not attempted at all. The panel polls
|
|
4825
|
+
* every couple of minutes and the scheduled passes walk the whole pool, and
|
|
4826
|
+
* each of those would otherwise spend the same call on a sign-in that cannot
|
|
4827
|
+
* answer it.
|
|
4828
|
+
*/
|
|
4829
|
+
async readAfterRefresh(accountId, read) {
|
|
4830
|
+
const account = this.accounts.find((item) => item.id === accountId);
|
|
4831
|
+
if (account === void 0) throw new Error(`${LOG_PREFIX}: no account ${accountId} to read for`);
|
|
4832
|
+
if (this.activeHold(accountId)?.reason === "session") throw new Error(REFUSED_SIGN_IN_MESSAGE);
|
|
4833
|
+
try {
|
|
4834
|
+
return await read(account.credential);
|
|
4835
|
+
} catch (error) {
|
|
4836
|
+
if (upstreamErrorKind(error) !== "session_dead") throw error;
|
|
4837
|
+
const outcome = await this.refreshForRetry(accountId);
|
|
4838
|
+
if (outcome === "refreshed") try {
|
|
4839
|
+
return await read(account.credential);
|
|
4840
|
+
} catch (retry) {
|
|
4841
|
+
if (upstreamErrorKind(retry) !== "session_dead") throw retry;
|
|
4842
|
+
this.holdRefusedSignIn(accountId);
|
|
4843
|
+
throw retry;
|
|
4844
|
+
}
|
|
4845
|
+
if (outcome === "refused") this.holdRefusedSignIn(accountId);
|
|
4846
|
+
throw error;
|
|
4847
|
+
}
|
|
3890
4848
|
}
|
|
3891
4849
|
/**
|
|
3892
4850
|
* Refresh the account's access token when it is within the margin (or already
|
|
@@ -3902,12 +4860,11 @@ var WorkBuddyAccountPool = class {
|
|
|
3902
4860
|
async ensureFresh(account, forced = false) {
|
|
3903
4861
|
if (this.client === void 0) return "fresh";
|
|
3904
4862
|
const credential = account.credential;
|
|
3905
|
-
|
|
4863
|
+
const issuedAt = credential.lastRefreshAtMs ?? 0;
|
|
4864
|
+
const stale = this.isOwnSignInPath(credential.sourcePath) && issuedAt > 0 && Date.now() - issuedAt >= REFRESH_AFTER_MS;
|
|
4865
|
+
if (!(forced || credential.expiresAtMs <= 0 || credential.expiresAtMs <= Date.now() + REFRESH_MARGIN_MS || stale)) return "fresh";
|
|
3906
4866
|
const existing = this.refreshInflight.get(account.id);
|
|
3907
|
-
if (existing !== void 0)
|
|
3908
|
-
await existing;
|
|
3909
|
-
return "fresh";
|
|
3910
|
-
}
|
|
4867
|
+
if (existing !== void 0) return await existing;
|
|
3911
4868
|
const run = (async () => {
|
|
3912
4869
|
if (credential.refreshToken === "") {
|
|
3913
4870
|
if (credential.expiresAtMs > Date.now() + 3e4) return "fresh";
|
|
@@ -3928,14 +4885,21 @@ var WorkBuddyAccountPool = class {
|
|
|
3928
4885
|
...outcome.domain === void 0 || outcome.domain === "" ? {} : { domain: outcome.domain },
|
|
3929
4886
|
lastRefreshAtMs: Date.now()
|
|
3930
4887
|
};
|
|
4888
|
+
await this.archiveCredentials([account.credential]);
|
|
4889
|
+
await this.renewOwnSignIn(account.credential);
|
|
3931
4890
|
this.noRefreshWarned.delete(account.id);
|
|
4891
|
+
const sessionHold = this.heldIds.get(account.id);
|
|
4892
|
+
if (sessionHold !== void 0 && sessionHold.reason === "session") {
|
|
4893
|
+
this.heldIds.delete(account.id);
|
|
4894
|
+
if (account.cooldownUntilMs > Date.now() && account.cooldownUntilMs <= sessionHold.until) account.cooldownUntilMs = 0;
|
|
4895
|
+
this.logger?.info?.(`${LOG_PREFIX}: ${account.label} refreshed again; releasing the refused sign-in hold`);
|
|
4896
|
+
}
|
|
3932
4897
|
this.logger?.info?.(`${LOG_PREFIX}: refreshed token for ${account.label}`);
|
|
3933
|
-
await this.archiveCredentials([account.credential]);
|
|
3934
4898
|
return "refreshed";
|
|
3935
4899
|
} catch (error) {
|
|
3936
4900
|
if (credential.expiresAtMs > Date.now() + 3e4) this.logger?.warn?.(`${LOG_PREFIX}: token refresh failed but token still valid for ${account.label}`, error);
|
|
3937
4901
|
else this.logger?.error?.(`${LOG_PREFIX}: token refresh failed and token expired for ${account.label}`, error);
|
|
3938
|
-
return "failed";
|
|
4902
|
+
return upstreamErrorKind(error) === "session_dead" ? "refused" : "failed";
|
|
3939
4903
|
}
|
|
3940
4904
|
})();
|
|
3941
4905
|
this.refreshInflight.set(account.id, run);
|
|
@@ -3953,24 +4917,24 @@ var WorkBuddyAccountPool = class {
|
|
|
3953
4917
|
* limit ("可切换其他模型继续使用"). Without a model id the whole account is
|
|
3954
4918
|
* cooled, which callers should reserve for limits that truly span every model.
|
|
3955
4919
|
*
|
|
3956
|
-
* `hold`
|
|
3957
|
-
* an upstream answer — a spent account, a dead session. It is bounded by
|
|
3958
|
-
*
|
|
4920
|
+
* `hold` names a decision of the pool's own rather than a timestamp read
|
|
4921
|
+
* off an upstream answer — a spent account, a dead session. It is bounded by
|
|
4922
|
+
* POOL_HOLD_MS instead of PENALIZE_CAP_MS, because the two-hour ceiling
|
|
3959
4923
|
* exists to stop a mis-parsed reset from parking an account all day, and
|
|
3960
4924
|
* neither of these conditions clears by itself.
|
|
3961
4925
|
*/
|
|
3962
4926
|
penalize(accountId, resetAtMs, modelId, hold = false) {
|
|
3963
4927
|
const account = this.accounts.find((item) => item.id === accountId);
|
|
3964
4928
|
if (account === void 0) return;
|
|
3965
|
-
account.rateLimitHits += 1;
|
|
4929
|
+
if (hold === false) account.rateLimitHits += 1;
|
|
3966
4930
|
const now = Date.now();
|
|
3967
4931
|
const stated = resetAtMs !== void 0 && resetAtMs > now;
|
|
3968
4932
|
if (hold === false && !stated) {
|
|
3969
4933
|
account.rateLimitStreak = now - (account.rateLimitStreakAtMs ?? 0) > RATE_LIMIT_STREAK_WINDOW_MS ? 1 : (account.rateLimitStreak ?? 0) + 1;
|
|
3970
4934
|
account.rateLimitStreakAtMs = now;
|
|
3971
4935
|
}
|
|
3972
|
-
const holdReason = hold === false ? void 0 : hold
|
|
3973
|
-
const cap = holdReason !== void 0 ?
|
|
4936
|
+
const holdReason = hold === false ? void 0 : hold;
|
|
4937
|
+
const cap = holdReason !== void 0 ? POOL_HOLD_MS : PENALIZE_CAP_MS;
|
|
3974
4938
|
const streak = Math.min(Math.max((account.rateLimitStreak ?? 1) - 1, 0), RATE_LIMIT_STREAK_STEPS);
|
|
3975
4939
|
const backoff = Math.min(this.cooldownMs * 2 ** streak, Math.max(this.cooldownMs, RATE_LIMIT_STREAK_CAP));
|
|
3976
4940
|
const until = stated ? Math.min(resetAtMs, now + cap) : holdReason !== void 0 ? now + cap : now + backoff;
|
|
@@ -3985,20 +4949,37 @@ var WorkBuddyAccountPool = class {
|
|
|
3985
4949
|
const prior = this.heldIds.get(accountId);
|
|
3986
4950
|
const carried = prior !== void 0 && prior.until > now && prior.reason === "session" ? prior : void 0;
|
|
3987
4951
|
const reason = carried?.reason ?? holdReason;
|
|
3988
|
-
const heldSignIns = carried !== void 0 ? carried.heldSignIns :
|
|
4952
|
+
const heldSignIns = carried !== void 0 ? carried.heldSignIns : this.refusedSignInsFor(accountId);
|
|
3989
4953
|
this.heldIds.set(accountId, {
|
|
3990
4954
|
reason,
|
|
3991
4955
|
until: Math.max(until, carried?.until ?? 0),
|
|
3992
4956
|
heldSignIns
|
|
3993
4957
|
});
|
|
3994
4958
|
}
|
|
3995
|
-
this.logger?.warn(`${LOG_PREFIX}: account ${account.label} rate-limited; cooling until ${new Date(until).toISOString()}`);
|
|
4959
|
+
this.logger?.warn(holdReason !== void 0 ? `${LOG_PREFIX}: account ${account.label} held (${holdReason}); held until ${new Date(until).toISOString()}` : `${LOG_PREFIX}: account ${account.label} rate-limited; cooling until ${new Date(until).toISOString()}`);
|
|
4960
|
+
}
|
|
4961
|
+
/**
|
|
4962
|
+
* Keep one model out of one account's rotation for a stated window.
|
|
4963
|
+
*
|
|
4964
|
+
* `penalize` caps what it writes at `PENALIZE_CAP_MS`, which exists so a
|
|
4965
|
+
* mis-parsed rate-limit reset cannot park work all day. A plan refusal names
|
|
4966
|
+
* no upstream window and clears only when the subscription changes, so it
|
|
4967
|
+
* brings its own deadline; routing it through `penalize` silently shortened
|
|
4968
|
+
* it to the rate-limit ceiling and bought one wasted upstream call per two
|
|
4969
|
+
* hours. The account is left alone: every other model on it still serves.
|
|
4970
|
+
*/
|
|
4971
|
+
blockModel(accountId, modelId, untilMs) {
|
|
4972
|
+
const account = this.accounts.find((item) => item.id === accountId);
|
|
4973
|
+
if (account === void 0 || modelId === "" || untilMs <= Date.now()) return;
|
|
4974
|
+
pruneModelCooldowns(account, Date.now());
|
|
4975
|
+
account.modelBlocks[modelId] = Math.max(account.modelBlocks[modelId] ?? 0, untilMs);
|
|
3996
4976
|
}
|
|
3997
4977
|
/** Clear all cooldowns (account-wide and per-model), e.g. from a reset command. */
|
|
3998
4978
|
resetCooldowns() {
|
|
3999
4979
|
for (const account of this.accounts) {
|
|
4000
4980
|
account.cooldownUntilMs = 0;
|
|
4001
|
-
account.modelCooldowns =
|
|
4981
|
+
account.modelCooldowns = Object.create(null);
|
|
4982
|
+
account.modelBlocks = Object.create(null);
|
|
4002
4983
|
account.rateLimitHits = 0;
|
|
4003
4984
|
account.rateLimitStreak = 0;
|
|
4004
4985
|
account.rateLimitStreakAtMs = 0;
|
|
@@ -4374,18 +5355,55 @@ function decodeTraeEvent(event) {
|
|
|
4374
5355
|
* Loopback guards, the request body bound and the server shell live in
|
|
4375
5356
|
* `./loopback` — one copy, shared with the Trae and Qoder gateways.
|
|
4376
5357
|
*/
|
|
5358
|
+
/** Whether one SSE data frame carries a non-null `finish_reason`. */
|
|
5359
|
+
function carriesFinishReason(data) {
|
|
5360
|
+
try {
|
|
5361
|
+
const parsed = JSON.parse(data);
|
|
5362
|
+
return Array.isArray(parsed.choices) && parsed.choices.some((choice) => typeof choice?.finish_reason === "string" && choice.finish_reason !== "");
|
|
5363
|
+
} catch {
|
|
5364
|
+
return false;
|
|
5365
|
+
}
|
|
5366
|
+
}
|
|
4377
5367
|
/** HTTP status each upstream failure class surfaces as. */
|
|
4378
5368
|
const KIND_STATUS = {
|
|
4379
5369
|
hard_credit: 402,
|
|
4380
5370
|
soft_rate: 429,
|
|
4381
5371
|
session_dead: 401,
|
|
4382
5372
|
waf_block: 502,
|
|
4383
|
-
not_found:
|
|
5373
|
+
not_found: 404,
|
|
4384
5374
|
server: 502,
|
|
4385
5375
|
client: 400,
|
|
5376
|
+
model_tier: 400,
|
|
5377
|
+
account_restricted: 403,
|
|
4386
5378
|
transport: 502,
|
|
4387
5379
|
aborted: 499
|
|
4388
5380
|
};
|
|
5381
|
+
/**
|
|
5382
|
+
* How long one account keeps a model it is not entitled to out of rotation.
|
|
5383
|
+
*
|
|
5384
|
+
* A plan does not change inside a working day, and the upstream names no
|
|
5385
|
+
* window of its own. A day is long enough that the gate costs one refusal per
|
|
5386
|
+
* account per day, and short enough that a plan bought later comes back
|
|
5387
|
+
* without a restart.
|
|
5388
|
+
*/
|
|
5389
|
+
const MODEL_TIER_HOLD_MS = 864e5;
|
|
5390
|
+
/**
|
|
5391
|
+
* The business code an account-level 200 envelope carries.
|
|
5392
|
+
*
|
|
5393
|
+
* `classifyUpstreamError` only reads codes on the statuses the upstream uses
|
|
5394
|
+
* for failures, so a refusal wearing a 200 was classed `client` and answered as
|
|
5395
|
+
* a one-off 502. These are the codes that make it the same account property a
|
|
5396
|
+
* non-200 answer would carry.
|
|
5397
|
+
*/
|
|
5398
|
+
function classifyEnvelopeCode(code, text) {
|
|
5399
|
+
if (code === "6004") return "soft_rate";
|
|
5400
|
+
if (code === "11158") return "model_tier";
|
|
5401
|
+
if (code === "11140") return "account_restricted";
|
|
5402
|
+
if (code === "12153") return "session_dead";
|
|
5403
|
+
if (isHardCreditText(text)) return "hard_credit";
|
|
5404
|
+
const kind = classifyUpstreamError(200, text);
|
|
5405
|
+
return kind === "client" ? void 0 : kind;
|
|
5406
|
+
}
|
|
4389
5407
|
/** Stable key for one conversation: a hash of its first user message. */
|
|
4390
5408
|
function conversationKeyOf(raw) {
|
|
4391
5409
|
try {
|
|
@@ -4399,7 +5417,76 @@ function conversationKeyOf(raw) {
|
|
|
4399
5417
|
return createHash("sha256").update(content).digest("hex").slice(0, 32);
|
|
4400
5418
|
}
|
|
4401
5419
|
} catch {
|
|
4402
|
-
return;
|
|
5420
|
+
return;
|
|
5421
|
+
}
|
|
5422
|
+
}
|
|
5423
|
+
/**
|
|
5424
|
+
* Read enough of a 200 body to tell an SSE stream from a refusal wearing a 200.
|
|
5425
|
+
*
|
|
5426
|
+
* WorkBuddy answers a refusal with a JSON envelope and an upstream hiccup can
|
|
5427
|
+
* end the body before the first frame; both leave the status code claiming
|
|
5428
|
+
* success. The head is what decides: a body that opens with `{` or `[` is the
|
|
5429
|
+
* envelope, a body that already ended empty is no answer at all, and anything
|
|
5430
|
+
* else is relayed as the stream it claims to be.
|
|
5431
|
+
*
|
|
5432
|
+
* The bytes the peek took are returned so the relay can put them back in front.
|
|
5433
|
+
*/
|
|
5434
|
+
async function peekAnswerHead(reader) {
|
|
5435
|
+
const head = [];
|
|
5436
|
+
const decoder = new TextDecoder();
|
|
5437
|
+
let text = "";
|
|
5438
|
+
let bytes = 0;
|
|
5439
|
+
let ended = false;
|
|
5440
|
+
let jsonLooking = false;
|
|
5441
|
+
for (;;) {
|
|
5442
|
+
const next = await reader.read();
|
|
5443
|
+
if (next.done) {
|
|
5444
|
+
ended = true;
|
|
5445
|
+
break;
|
|
5446
|
+
}
|
|
5447
|
+
head.push(next.value);
|
|
5448
|
+
bytes += next.value.byteLength;
|
|
5449
|
+
text += decoder.decode(next.value, { stream: true });
|
|
5450
|
+
const trimmed = text.replace(/^\uFEFF/, "").trimStart();
|
|
5451
|
+
if (trimmed === "") {
|
|
5452
|
+
if (bytes >= 262144 || head.length >= 64) break;
|
|
5453
|
+
continue;
|
|
5454
|
+
}
|
|
5455
|
+
jsonLooking = trimmed.startsWith("{") || trimmed.startsWith("[");
|
|
5456
|
+
if (!jsonLooking) break;
|
|
5457
|
+
if (ended || bytes >= 32768 || head.length >= 64) break;
|
|
5458
|
+
}
|
|
5459
|
+
const trimmed = text.replace(/^\uFEFF/, "").trim();
|
|
5460
|
+
if (trimmed === "" && ended) return {
|
|
5461
|
+
head,
|
|
5462
|
+
ended,
|
|
5463
|
+
refusal: "workbuddy returned an empty answer stream"
|
|
5464
|
+
};
|
|
5465
|
+
if (!jsonLooking) return {
|
|
5466
|
+
head,
|
|
5467
|
+
ended
|
|
5468
|
+
};
|
|
5469
|
+
try {
|
|
5470
|
+
const parsed = JSON.parse(trimmed);
|
|
5471
|
+
const code = parsed["code"] ?? parsed["errCode"] ?? parsed["errorCode"];
|
|
5472
|
+
const msg = parsed["msg"] ?? parsed["message"] ?? parsed["error"];
|
|
5473
|
+
const detail = typeof msg === "string" && msg.trim() !== "" ? msg.trim() : "";
|
|
5474
|
+
const named = code === void 0 || code === null || code === 0 || code === "0" ? "" : `code ${String(code)}`;
|
|
5475
|
+
const sentence = [named, detail].filter((part) => part !== "").join(" · ");
|
|
5476
|
+
return {
|
|
5477
|
+
head,
|
|
5478
|
+
ended,
|
|
5479
|
+
body: trimmed,
|
|
5480
|
+
refusal: sentence === "" ? "workbuddy answered 200 with a JSON body where a stream was expected" : `workbuddy answered 200 with ${sentence}`,
|
|
5481
|
+
...named === "" ? {} : { code: String(code) }
|
|
5482
|
+
};
|
|
5483
|
+
} catch {
|
|
5484
|
+
return {
|
|
5485
|
+
head,
|
|
5486
|
+
ended,
|
|
5487
|
+
body: trimmed,
|
|
5488
|
+
refusal: `workbuddy answered 200 with a body that is neither a stream nor an envelope: ${safeMessage(trimmed.slice(0, 200))}`
|
|
5489
|
+
};
|
|
4403
5490
|
}
|
|
4404
5491
|
}
|
|
4405
5492
|
/**
|
|
@@ -4472,23 +5559,13 @@ function isContextTooLong(body) {
|
|
|
4472
5559
|
/**
|
|
4473
5560
|
* How long a spent or dead session is held out of rotation.
|
|
4474
5561
|
*
|
|
4475
|
-
*
|
|
4476
|
-
*
|
|
4477
|
-
*
|
|
4478
|
-
*
|
|
4479
|
-
* healthy accounts behind it. This hold is a fallback, not a verdict: a manual
|
|
4480
|
-
* cooldown reset, a re-scan and a balance read that finds credits again all
|
|
4481
|
-
* release it.
|
|
4482
|
-
*/
|
|
4483
|
-
const SPENT_ACCOUNT_HOLD_MS = 216e5;
|
|
4484
|
-
/**
|
|
4485
|
-
* The hold as the absolute reset `penalize` expects.
|
|
4486
|
-
*
|
|
4487
|
-
* `penalize` takes a timestamp, not a duration: handing it the duration put the
|
|
4488
|
-
* reset in 1970, which reads as already past and falls back to the configured
|
|
5562
|
+
* `penalize` takes an absolute reset rather than a duration, and the duration
|
|
5563
|
+
* itself lives with the pool that owns the hold: see `POOL_HOLD_MS` in
|
|
5564
|
+
* `./accounts`. Handing the duration straight to `penalize` once put the reset
|
|
5565
|
+
* in 1970, which reads as already past and falls back to the configured
|
|
4489
5566
|
* cooldown, so the hold never took effect at all.
|
|
4490
5567
|
*/
|
|
4491
|
-
const
|
|
5568
|
+
const holdUntil = () => Date.now() + POOL_HOLD_MS;
|
|
4492
5569
|
/** Turn an upstream `Retry-After` (seconds) into the absolute reset `penalize` wants. */
|
|
4493
5570
|
function resetFromRetryAfter(seconds) {
|
|
4494
5571
|
if (seconds === void 0) return void 0;
|
|
@@ -4523,6 +5600,16 @@ function unavailableAnswer(pool, live, now, modelId) {
|
|
|
4523
5600
|
detail: `every account is rate-limited for model ${modelId}`,
|
|
4524
5601
|
retryAfterSeconds
|
|
4525
5602
|
};
|
|
5603
|
+
if (only === "plan") return {
|
|
5604
|
+
status: 400,
|
|
5605
|
+
code: "model_tier",
|
|
5606
|
+
detail: `no WorkBuddy account has model ${modelId} in its plan; pick another model, or add an account whose plan includes it`
|
|
5607
|
+
};
|
|
5608
|
+
if (only === "restricted by the upstream") return {
|
|
5609
|
+
status: 403,
|
|
5610
|
+
code: "account_restricted",
|
|
5611
|
+
detail: "every WorkBuddy account was restricted by the upstream; appeal the restriction in the WorkBuddy console, or add another account"
|
|
5612
|
+
};
|
|
4526
5613
|
return {
|
|
4527
5614
|
status: 503,
|
|
4528
5615
|
code: "accounts_unavailable",
|
|
@@ -4544,7 +5631,7 @@ function createWorkBuddyShim(options) {
|
|
|
4544
5631
|
const ready = server.ready;
|
|
4545
5632
|
async function handle(req, res) {
|
|
4546
5633
|
if (!guardsPass(req, res)) return;
|
|
4547
|
-
const url = req.url
|
|
5634
|
+
const url = requestPathname(req.url);
|
|
4548
5635
|
if (req.method === "GET" && (url === "/healthz" || url === "/healthz/")) {
|
|
4549
5636
|
writeJson(res, 200, {
|
|
4550
5637
|
ok: true,
|
|
@@ -4602,20 +5689,19 @@ function createWorkBuddyShim(options) {
|
|
|
4602
5689
|
return;
|
|
4603
5690
|
}
|
|
4604
5691
|
const tried = [];
|
|
5692
|
+
const skipped = /* @__PURE__ */ new Set();
|
|
4605
5693
|
let last;
|
|
4606
5694
|
let exhaustedByRateLimit = false;
|
|
4607
|
-
|
|
4608
|
-
|
|
4609
|
-
return effort === void 0 ? void 0 : withReasoningEffort(prepared, effort);
|
|
4610
|
-
})();
|
|
5695
|
+
let effortFallback;
|
|
5696
|
+
let effortFallbackBuilt = false;
|
|
4611
5697
|
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
4612
5698
|
if (controller.signal.aborted) return;
|
|
4613
|
-
const account = await pool.acquire(modelId, region, conversationKey);
|
|
5699
|
+
const account = await pool.acquire(modelId, region, conversationKey, skipped);
|
|
4614
5700
|
if (account === void 0) {
|
|
4615
5701
|
if (exhaustedByRateLimit && last !== void 0) {
|
|
4616
5702
|
const model = ` for model ${modelId}`;
|
|
4617
|
-
const subject = last.kind === "waf_block" ? "every WorkBuddy account was blocked by the upstream gateway (WAF)" : last.kind === "hard_credit" ? `every WorkBuddy account is out of credits${model}` : last.kind === "session_dead" ? `every WorkBuddy sign-in is no longer accepted${model}` : last.kind === "transport" ? `the upstream host could not be reached on any account${model}` : `every account is rate-limited${model}`;
|
|
4618
|
-
const advice = last.kind === "soft_rate" ? "resets at the upstream window" : last.kind === "transport" ? "check the connection to the upstream host" : "sign in again in the desktop app";
|
|
5703
|
+
const subject = last.kind === "waf_block" ? "every WorkBuddy account was blocked by the upstream gateway (WAF)" : last.kind === "hard_credit" ? `every WorkBuddy account is out of credits${model}` : last.kind === "session_dead" ? `every WorkBuddy sign-in is no longer accepted${model}` : last.kind === "model_tier" ? `no WorkBuddy account can use model ${modelId}` : last.kind === "account_restricted" ? "every WorkBuddy account was restricted by the upstream" : last.kind === "transport" ? `the upstream host could not be reached on any account${model}` : `every account is rate-limited${model}`;
|
|
5704
|
+
const advice = last.kind === "soft_rate" ? "resets at the upstream window" : last.kind === "model_tier" ? "pick another model, or add an account whose plan includes it" : last.kind === "account_restricted" ? "appeal the restriction in the WorkBuddy console, or add another account" : last.kind === "transport" ? "check the connection to the upstream host" : "sign in again in the desktop app";
|
|
4619
5705
|
writeOpenAIError(res, KIND_STATUS[last.kind], last.kind, `${subject} (tried ${tried.length}: ${tried.join(", ")}); ${advice} — ${last.message.slice(0, 200)}`);
|
|
4620
5706
|
return;
|
|
4621
5707
|
}
|
|
@@ -4638,31 +5724,53 @@ function createWorkBuddyShim(options) {
|
|
|
4638
5724
|
}
|
|
4639
5725
|
tried.push(account.label);
|
|
4640
5726
|
let result = await client.chatStream(account.credential, prepared, controller.signal);
|
|
4641
|
-
|
|
5727
|
+
let refreshOutcome;
|
|
5728
|
+
if (!result.ok && result.kind === "session_dead") {
|
|
5729
|
+
const failure = result;
|
|
5730
|
+
refreshOutcome = await pool.refreshAccountOutcome(account.id);
|
|
5731
|
+
if (refreshOutcome === "failed") {
|
|
5732
|
+
logger?.warn?.(`${LOG_PREFIX}: ${account.label} could not refresh its sign-in; trying the next account without holding it`);
|
|
5733
|
+
skipped.add(account.id);
|
|
5734
|
+
last = {
|
|
5735
|
+
kind: "transport",
|
|
5736
|
+
status: 502,
|
|
5737
|
+
message: `could not refresh the sign-in: ${failure.message}`
|
|
5738
|
+
};
|
|
5739
|
+
exhaustedByRateLimit = true;
|
|
5740
|
+
continue;
|
|
5741
|
+
}
|
|
5742
|
+
}
|
|
5743
|
+
if (refreshOutcome === "refreshed") {
|
|
4642
5744
|
if (controller.signal.aborted) return;
|
|
4643
5745
|
result = await client.chatStream(account.credential, prepared, controller.signal);
|
|
4644
5746
|
}
|
|
4645
|
-
if (!result.ok && result.kind === "client" &&
|
|
4646
|
-
|
|
4647
|
-
|
|
5747
|
+
if (!result.ok && result.kind === "client" && PARAMETER_REJECTION.test(result.message) && !controller.signal.aborted) {
|
|
5748
|
+
if (!effortFallbackBuilt) {
|
|
5749
|
+
effortFallbackBuilt = true;
|
|
5750
|
+
const effort = declaredEffortOf(catalog, modelId);
|
|
5751
|
+
effortFallback = effort === void 0 ? void 0 : withReasoningEffort(prepared, effort);
|
|
5752
|
+
}
|
|
5753
|
+
if (effortFallback !== void 0) {
|
|
5754
|
+
logger?.info?.(`${LOG_PREFIX}: ${modelId} refused a request that named no effort; retrying with the model's declared default`);
|
|
5755
|
+
result = await client.chatStream(account.credential, effortFallback, controller.signal);
|
|
5756
|
+
}
|
|
4648
5757
|
}
|
|
4649
5758
|
if (result.ok) {
|
|
4650
|
-
|
|
4651
|
-
writeOpenAIError(res, 502, "upstream_error", "workbuddy returned no stream");
|
|
4652
|
-
return;
|
|
4653
|
-
}
|
|
4654
|
-
logger?.info?.(`${LOG_PREFIX}: served by ${account.label}`);
|
|
4655
|
-
res.writeHead(200, {
|
|
4656
|
-
"Content-Type": "text/event-stream",
|
|
4657
|
-
"Cache-Control": "no-cache",
|
|
4658
|
-
"Connection": "keep-alive",
|
|
4659
|
-
"X-Accel-Buffering": "no"
|
|
4660
|
-
});
|
|
4661
|
-
res.flushHeaders();
|
|
5759
|
+
const reader = relayStreamOf(result.response).getReader();
|
|
4662
5760
|
let sawDone = false;
|
|
5761
|
+
let sawFinish = false;
|
|
5762
|
+
let envelopeFailure;
|
|
4663
5763
|
const decoder = new TextDecoder();
|
|
4664
5764
|
const sse = new SseDecoder();
|
|
4665
|
-
const
|
|
5765
|
+
const startStream = () => {
|
|
5766
|
+
res.writeHead(200, {
|
|
5767
|
+
"Content-Type": "text/event-stream",
|
|
5768
|
+
"Cache-Control": "no-cache",
|
|
5769
|
+
"Connection": "keep-alive",
|
|
5770
|
+
"X-Accel-Buffering": "no"
|
|
5771
|
+
});
|
|
5772
|
+
res.flushHeaders();
|
|
5773
|
+
};
|
|
4666
5774
|
/**
|
|
4667
5775
|
* End the relayed stream in a shape the client can finish on.
|
|
4668
5776
|
*
|
|
@@ -4676,7 +5784,7 @@ function createWorkBuddyShim(options) {
|
|
|
4676
5784
|
*/
|
|
4677
5785
|
const failInBand = (error) => {
|
|
4678
5786
|
if (controller.signal.aborted || !res.writable) return;
|
|
4679
|
-
logger?.warn?.(
|
|
5787
|
+
logger?.warn?.("wtq-pool: upstream stream failed mid-flight", safeMessage(error));
|
|
4680
5788
|
try {
|
|
4681
5789
|
res.write(`data: ${JSON.stringify({ error: {
|
|
4682
5790
|
message: safeMessage(error),
|
|
@@ -4693,19 +5801,94 @@ function createWorkBuddyShim(options) {
|
|
|
4693
5801
|
/** End the response with `[DONE]` unless the upstream already sent it. */
|
|
4694
5802
|
function closeWithoutDone() {
|
|
4695
5803
|
if (sawDone || !res.writable) return;
|
|
4696
|
-
logger?.warn?.(
|
|
5804
|
+
logger?.warn?.("wtq-pool: upstream stream ended without [DONE]");
|
|
5805
|
+
if (!sawFinish) res.write(`data: ${JSON.stringify({ choices: [{
|
|
5806
|
+
index: 0,
|
|
5807
|
+
delta: {},
|
|
5808
|
+
finish_reason: "stop"
|
|
5809
|
+
}] })}\n\n`);
|
|
4697
5810
|
res.end("data: [DONE]\n\n");
|
|
4698
5811
|
}
|
|
4699
|
-
|
|
5812
|
+
await (async () => {
|
|
5813
|
+
let head = [];
|
|
5814
|
+
let ended = false;
|
|
5815
|
+
try {
|
|
5816
|
+
const peeked = await peekAnswerHead(reader);
|
|
5817
|
+
head = peeked.head;
|
|
5818
|
+
ended = peeked.ended;
|
|
5819
|
+
if (peeked.refusal !== void 0) {
|
|
5820
|
+
await reader.cancel().catch(() => {});
|
|
5821
|
+
logger?.warn?.(`${LOG_PREFIX}: ${account.label} answered 200 with no stream: ${peeked.refusal}`);
|
|
5822
|
+
if (peeked.code === "11128" || unapprovedChannelRefusal(peeked.refusal) !== void 0) {
|
|
5823
|
+
writeOpenAIError(res, 400, "upstream_channel_check", channelCheckRefusal());
|
|
5824
|
+
return;
|
|
5825
|
+
}
|
|
5826
|
+
const envelopeKind = classifyEnvelopeCode(peeked.code, peeked.refusal);
|
|
5827
|
+
if (envelopeKind === "soft_rate" || envelopeKind === "hard_credit" || envelopeKind === "session_dead" || envelopeKind === "account_restricted" || envelopeKind === "model_tier") {
|
|
5828
|
+
last = {
|
|
5829
|
+
kind: envelopeKind,
|
|
5830
|
+
status: 200,
|
|
5831
|
+
message: peeked.refusal
|
|
5832
|
+
};
|
|
5833
|
+
exhaustedByRateLimit = true;
|
|
5834
|
+
envelopeFailure = envelopeKind;
|
|
5835
|
+
if (envelopeKind === "soft_rate") {
|
|
5836
|
+
const bodyReset = parseRateLimitReset(peeked.body ?? peeked.refusal);
|
|
5837
|
+
const headerReset = resetFromRetryAfter(retryAfterSeconds(result.response.headers));
|
|
5838
|
+
const resetAtMs = bodyReset !== void 0 && bodyReset > Date.now() ? headerReset === void 0 ? bodyReset : Math.max(bodyReset, headerReset) : headerReset;
|
|
5839
|
+
pool.penalize(account.id, resetAtMs, modelId);
|
|
5840
|
+
} else if (envelopeKind === "model_tier") pool.blockModel(account.id, modelId, Date.now() + MODEL_TIER_HOLD_MS);
|
|
5841
|
+
else if (envelopeKind === "session_dead") {
|
|
5842
|
+
const refreshOutcome = await pool.refreshAccountOutcome(account.id);
|
|
5843
|
+
if (refreshOutcome === "refused") pool.penalize(account.id, holdUntil(), void 0, "session");
|
|
5844
|
+
else if (refreshOutcome === "failed") {
|
|
5845
|
+
skipped.add(account.id);
|
|
5846
|
+
last = {
|
|
5847
|
+
kind: "transport",
|
|
5848
|
+
status: 502,
|
|
5849
|
+
message: `could not refresh the sign-in: ${peeked.refusal.slice(0, 200)}`
|
|
5850
|
+
};
|
|
5851
|
+
}
|
|
5852
|
+
} else pool.penalize(account.id, holdUntil(), void 0, envelopeKind === "hard_credit" ? "spent" : "restricted");
|
|
5853
|
+
return;
|
|
5854
|
+
}
|
|
5855
|
+
writeOpenAIError(res, 502, "upstream_error", peeked.refusal);
|
|
5856
|
+
return;
|
|
5857
|
+
}
|
|
5858
|
+
} catch (error) {
|
|
5859
|
+
startStream();
|
|
5860
|
+
failInBand(error);
|
|
5861
|
+
return;
|
|
5862
|
+
}
|
|
5863
|
+
logger?.info?.(`${LOG_PREFIX}: served by ${account.label}`);
|
|
5864
|
+
startStream();
|
|
5865
|
+
let headIndex = 0;
|
|
4700
5866
|
try {
|
|
4701
5867
|
for (;;) {
|
|
4702
|
-
const
|
|
4703
|
-
|
|
5868
|
+
const replayed = headIndex < head.length ? head[headIndex++] : void 0;
|
|
5869
|
+
const { done, value } = replayed !== void 0 ? {
|
|
5870
|
+
done: false,
|
|
5871
|
+
value: replayed
|
|
5872
|
+
} : ended ? {
|
|
5873
|
+
done: true,
|
|
5874
|
+
value: void 0
|
|
5875
|
+
} : await reader.read();
|
|
5876
|
+
if (done || value === void 0) break;
|
|
4704
5877
|
if (!res.writable) return;
|
|
4705
|
-
res
|
|
5878
|
+
await writeChunk(res, value);
|
|
4706
5879
|
try {
|
|
4707
5880
|
const decoded = decoder.decode(value, { stream: true });
|
|
4708
|
-
|
|
5881
|
+
const events = sse.push(decoded);
|
|
5882
|
+
if (!sawFinish && events.some((event) => carriesFinishReason(event.data))) sawFinish = true;
|
|
5883
|
+
if (events.some((event) => event.data === "[DONE]")) {
|
|
5884
|
+
if (!sawFinish && res.writable) {
|
|
5885
|
+
res.write(`data: ${JSON.stringify({ choices: [{
|
|
5886
|
+
index: 0,
|
|
5887
|
+
delta: {},
|
|
5888
|
+
finish_reason: "stop"
|
|
5889
|
+
}] })}\n\n`);
|
|
5890
|
+
sawFinish = true;
|
|
5891
|
+
}
|
|
4709
5892
|
sawDone = true;
|
|
4710
5893
|
if (res.writable) res.end();
|
|
4711
5894
|
await reader.cancel().catch(() => {});
|
|
@@ -4715,7 +5898,9 @@ function createWorkBuddyShim(options) {
|
|
|
4715
5898
|
}
|
|
4716
5899
|
if (!sawDone) try {
|
|
4717
5900
|
const rest = decoder.decode();
|
|
4718
|
-
|
|
5901
|
+
const events = rest === "" ? sse.finish() : [...sse.push(rest), ...sse.finish()];
|
|
5902
|
+
if (events.some((event) => event.data === "[DONE]")) sawDone = true;
|
|
5903
|
+
if (events.some((event) => carriesFinishReason(event.data))) sawFinish = true;
|
|
4719
5904
|
} catch {}
|
|
4720
5905
|
closeWithoutDone();
|
|
4721
5906
|
} catch (error) {
|
|
@@ -4726,6 +5911,8 @@ function createWorkBuddyShim(options) {
|
|
|
4726
5911
|
} catch {}
|
|
4727
5912
|
}
|
|
4728
5913
|
})();
|
|
5914
|
+
if (envelopeFailure === void 0) return;
|
|
5915
|
+
continue;
|
|
4729
5916
|
}
|
|
4730
5917
|
last = {
|
|
4731
5918
|
kind: result.kind,
|
|
@@ -4753,17 +5940,30 @@ function createWorkBuddyShim(options) {
|
|
|
4753
5940
|
* for hours over a transient block.
|
|
4754
5941
|
*
|
|
4755
5942
|
* A `transport` failure is the network path's, not the account's, so the
|
|
4756
|
-
*
|
|
4757
|
-
*
|
|
5943
|
+
* rotation stops: every account here reaches the same host, and trying
|
|
5944
|
+
* each one just spends another header timeout before answering. An
|
|
5945
|
+
* `aborted` turn is the client's own doing and stops the loop too.
|
|
4758
5946
|
*/
|
|
4759
5947
|
if (result.kind === "aborted") break;
|
|
4760
|
-
if (result.kind
|
|
5948
|
+
if (result.kind === "transport") {
|
|
5949
|
+
last = {
|
|
5950
|
+
kind: result.kind,
|
|
5951
|
+
status: result.status,
|
|
5952
|
+
message: result.message
|
|
5953
|
+
};
|
|
5954
|
+
exhaustedByRateLimit = true;
|
|
5955
|
+
break;
|
|
5956
|
+
}
|
|
5957
|
+
if (result.kind !== "soft_rate" && result.kind !== "waf_block" && result.kind !== "hard_credit" && result.kind !== "session_dead" && result.kind !== "model_tier" && result.kind !== "account_restricted") break;
|
|
4761
5958
|
exhaustedByRateLimit = true;
|
|
4762
5959
|
if (result.kind === "soft_rate") {
|
|
4763
5960
|
const bodyReset = parseRateLimitReset(result.message);
|
|
4764
|
-
const
|
|
5961
|
+
const headerReset = resetFromRetryAfter(result.retryAfter);
|
|
5962
|
+
const resetAtMs = bodyReset !== void 0 && bodyReset > Date.now() ? headerReset === void 0 ? bodyReset : Math.max(bodyReset, headerReset) : headerReset;
|
|
4765
5963
|
pool.penalize(account.id, resetAtMs, modelId);
|
|
4766
|
-
} else if (result.kind === "
|
|
5964
|
+
} else if (result.kind === "model_tier") pool.blockModel(account.id, modelId, Date.now() + MODEL_TIER_HOLD_MS);
|
|
5965
|
+
else if (result.kind === "account_restricted") pool.penalize(account.id, holdUntil(), void 0, "restricted");
|
|
5966
|
+
else if (result.kind === "hard_credit" || result.kind === "session_dead") pool.penalize(account.id, holdUntil(), void 0, result.kind === "hard_credit" ? "spent" : "session");
|
|
4767
5967
|
else pool.penalize(account.id);
|
|
4768
5968
|
logger?.warn(`${LOG_PREFIX}: ${account.label} ${result.kind} on ${modelId} (attempt ${attempt + 1}/${maxAttempts}); rotating`);
|
|
4769
5969
|
}
|
|
@@ -4789,75 +5989,6 @@ function createWorkBuddyShim(options) {
|
|
|
4789
5989
|
close: server.close
|
|
4790
5990
|
};
|
|
4791
5991
|
}
|
|
4792
|
-
/**
|
|
4793
|
-
* Matched case-insensitively: the host's check is, and a lowercase spelling in a
|
|
4794
|
-
* user message tripped it just the same when probed.
|
|
4795
|
-
*/
|
|
4796
|
-
const CLIENT_NAME = /DeepSeek Harness/gi;
|
|
4797
|
-
/**
|
|
4798
|
-
* Replace the client name in every message's text, leaving everything else be.
|
|
4799
|
-
*
|
|
4800
|
-
* Only text can carry it: `tool_calls`, `tool_call_id`, tool names and ids are
|
|
4801
|
-
* the client's own vocabulary for the call it is making, and rewriting those
|
|
4802
|
-
* would break the pairing.
|
|
4803
|
-
*/
|
|
4804
|
-
function renameClientInMessages(messages, replacement) {
|
|
4805
|
-
const rename = (text) => text.replace(CLIENT_NAME, replacement);
|
|
4806
|
-
const inContent = (content) => {
|
|
4807
|
-
if (typeof content === "string") return rename(content);
|
|
4808
|
-
if (!Array.isArray(content)) return content;
|
|
4809
|
-
return content.map((part) => {
|
|
4810
|
-
if (typeof part === "string") return rename(part);
|
|
4811
|
-
if (typeof part !== "object" || part === null || Array.isArray(part)) return part;
|
|
4812
|
-
const record = part;
|
|
4813
|
-
if (typeof record["text"] !== "string") return part;
|
|
4814
|
-
return {
|
|
4815
|
-
...record,
|
|
4816
|
-
text: rename(record["text"])
|
|
4817
|
-
};
|
|
4818
|
-
});
|
|
4819
|
-
};
|
|
4820
|
-
return messages.map((message) => {
|
|
4821
|
-
if (typeof message !== "object" || message === null || Array.isArray(message)) return message;
|
|
4822
|
-
const record = message;
|
|
4823
|
-
if (!("content" in record)) return message;
|
|
4824
|
-
const content = inContent(record["content"]);
|
|
4825
|
-
return content === record["content"] ? message : {
|
|
4826
|
-
...record,
|
|
4827
|
-
content
|
|
4828
|
-
};
|
|
4829
|
-
});
|
|
4830
|
-
}
|
|
4831
|
-
/**
|
|
4832
|
-
* Wrap `fetch` so chat requests to the domestic host carry the replacement name.
|
|
4833
|
-
*
|
|
4834
|
-
* The name is read per request, so the panel's switch takes effect on the next
|
|
4835
|
-
* turn; a reader that returns nothing leaves the body exactly as the host wrote
|
|
4836
|
-
* it. Only the chat endpoint is touched. The billing and check-in calls carry no
|
|
4837
|
-
* prompt, and the international host does not check for this.
|
|
4838
|
-
*/
|
|
4839
|
-
function clientNameRewritingFetch(replacement, inner = globalThis.fetch) {
|
|
4840
|
-
const rewrite = (url, init) => {
|
|
4841
|
-
if (!url.includes("copilot.tencent.com") || typeof init?.body !== "string") return init;
|
|
4842
|
-
const name = replacement()?.trim();
|
|
4843
|
-
if (name === void 0 || name === "") return init;
|
|
4844
|
-
let parsed;
|
|
4845
|
-
try {
|
|
4846
|
-
parsed = JSON.parse(init.body);
|
|
4847
|
-
} catch {
|
|
4848
|
-
return init;
|
|
4849
|
-
}
|
|
4850
|
-
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return init;
|
|
4851
|
-
const body = parsed;
|
|
4852
|
-
if (!Array.isArray(body["messages"])) return init;
|
|
4853
|
-
body["messages"] = renameClientInMessages(body["messages"], name);
|
|
4854
|
-
return {
|
|
4855
|
-
...init,
|
|
4856
|
-
body: JSON.stringify(body)
|
|
4857
|
-
};
|
|
4858
|
-
};
|
|
4859
|
-
return ((input, init) => inner(input, rewrite(String(input), init)));
|
|
4860
|
-
}
|
|
4861
5992
|
//#endregion
|
|
4862
5993
|
//#region src/promo.ts
|
|
4863
5994
|
/**
|
|
@@ -4891,6 +6022,7 @@ function nextWindowHour(rule, hour) {
|
|
|
4891
6022
|
const candidate = (hour + step) % 24;
|
|
4892
6023
|
if (rule.hours.includes(candidate)) return candidate;
|
|
4893
6024
|
}
|
|
6025
|
+
return hour;
|
|
4894
6026
|
}
|
|
4895
6027
|
/** The hour the CURRENT window closes, searching forward from `hour`. */
|
|
4896
6028
|
function windowEndHour(rule, hour) {
|
|
@@ -4925,10 +6057,9 @@ function promoStatusFor(model, clock, region) {
|
|
|
4925
6057
|
untilHour
|
|
4926
6058
|
};
|
|
4927
6059
|
}
|
|
4928
|
-
|
|
4929
|
-
if (nextHour !== void 0) return {
|
|
6060
|
+
return {
|
|
4930
6061
|
kind: "night-later",
|
|
4931
|
-
nextHour
|
|
6062
|
+
nextHour: nextWindowHour(rule, clock.hour)
|
|
4932
6063
|
};
|
|
4933
6064
|
}
|
|
4934
6065
|
}
|
|
@@ -6243,7 +7374,7 @@ var TraeSoloUpstreamClient = class {
|
|
|
6243
7374
|
async fetchModels(signal) {
|
|
6244
7375
|
const [credential, identity] = await Promise.all([this.options.credential(), this.options.identity()]);
|
|
6245
7376
|
const region = traeRegionOfCredential(credential);
|
|
6246
|
-
const base =
|
|
7377
|
+
const base = REGION_GATEWAYS[region].chat;
|
|
6247
7378
|
const headers = {
|
|
6248
7379
|
...buildTraeHeaders(credential, identity),
|
|
6249
7380
|
Accept: "application/json"
|
|
@@ -6378,7 +7509,7 @@ var TraeSoloUpstreamClient = class {
|
|
|
6378
7509
|
const [credential, identity] = await Promise.all([this.options.credential(), this.options.identity()]);
|
|
6379
7510
|
const region = traeRegionOfCredential(credential);
|
|
6380
7511
|
if (region !== "ai") throw new Error("Trae pay status exists on the international region only");
|
|
6381
|
-
const response = await this.fetchImpl(traeEndpoint(
|
|
7512
|
+
const response = await this.fetchImpl(traeEndpoint(REGION_GATEWAYS[region].pay, TRAE_PAY_STATUS_PATH), {
|
|
6382
7513
|
redirect: "error",
|
|
6383
7514
|
method: "POST",
|
|
6384
7515
|
headers: traeCheckinHeaders(credential, identity, region),
|
|
@@ -6448,7 +7579,7 @@ var TraeSoloUpstreamClient = class {
|
|
|
6448
7579
|
};
|
|
6449
7580
|
}
|
|
6450
7581
|
const headers = buildTraeHeaders(credential, identity);
|
|
6451
|
-
const base =
|
|
7582
|
+
const base = REGION_GATEWAYS[traeRegionOfCredential(credential)].chat;
|
|
6452
7583
|
let response;
|
|
6453
7584
|
const deadline = headersDeadline(signal, timeouts?.headersMs ?? TRAE_HEADERS_TIMEOUT_MS);
|
|
6454
7585
|
try {
|
|
@@ -6738,6 +7869,13 @@ function bridgeTraeStream(source, model, options = {}) {
|
|
|
6738
7869
|
let emittedFinishReason = false;
|
|
6739
7870
|
let upstreamDone = false;
|
|
6740
7871
|
let usage;
|
|
7872
|
+
let lastKeepaliveAt = 0;
|
|
7873
|
+
const keepalive = (controller) => {
|
|
7874
|
+
const now = Date.now();
|
|
7875
|
+
if (now - lastKeepaliveAt < 1e4) return;
|
|
7876
|
+
lastKeepaliveAt = now;
|
|
7877
|
+
controller.enqueue(encoder.encode(": keepalive\n\n"));
|
|
7878
|
+
};
|
|
6741
7879
|
const chunk = (delta, finishReason = null) => encoder.encode(`data: ${JSON.stringify({
|
|
6742
7880
|
id,
|
|
6743
7881
|
object: "chat.completion.chunk",
|
|
@@ -6765,7 +7903,10 @@ function bridgeTraeStream(source, model, options = {}) {
|
|
|
6765
7903
|
if (decoded.type === "done" || event.event === "done") upstreamDone = true;
|
|
6766
7904
|
const failure = traeFailureOf(decoded);
|
|
6767
7905
|
if (failure !== void 0) throw new Error(describeTraeFailure(failure));
|
|
6768
|
-
if (decoded.type === "unknown")
|
|
7906
|
+
if (decoded.type === "unknown") {
|
|
7907
|
+
keepalive(controller);
|
|
7908
|
+
return;
|
|
7909
|
+
}
|
|
6769
7910
|
if (decoded.type === "delta") {
|
|
6770
7911
|
const delta = {};
|
|
6771
7912
|
if (decoded.text !== "") delta["content"] = decoded.text;
|
|
@@ -6779,7 +7920,10 @@ function bridgeTraeStream(source, model, options = {}) {
|
|
|
6779
7920
|
roleChunk(controller);
|
|
6780
7921
|
controller.enqueue(chunk(delta));
|
|
6781
7922
|
}
|
|
6782
|
-
if (decoded.finishReason === void 0)
|
|
7923
|
+
if (decoded.finishReason === void 0) {
|
|
7924
|
+
keepalive(controller);
|
|
7925
|
+
return;
|
|
7926
|
+
}
|
|
6783
7927
|
}
|
|
6784
7928
|
if (decoded.type === "usage") {
|
|
6785
7929
|
const details = {
|
|
@@ -6795,7 +7939,10 @@ function bridgeTraeStream(source, model, options = {}) {
|
|
|
6795
7939
|
if (emittedFinishReason) controller.enqueue(chunk(null));
|
|
6796
7940
|
return;
|
|
6797
7941
|
}
|
|
6798
|
-
if (decoded.type !== "done" && decoded.type !== "delta")
|
|
7942
|
+
if (decoded.type !== "done" && decoded.type !== "delta") {
|
|
7943
|
+
keepalive(controller);
|
|
7944
|
+
return;
|
|
7945
|
+
}
|
|
6799
7946
|
if (emittedFinishReason) return;
|
|
6800
7947
|
emittedFinishReason = true;
|
|
6801
7948
|
controller.enqueue(chunk({}, openAIFinishReason(decoded.finishReason, sawToolCalls)));
|
|
@@ -6998,7 +8145,8 @@ function capabilityMapOf(models) {
|
|
|
6998
8145
|
*/
|
|
6999
8146
|
function functionGone(status, kind, message) {
|
|
7000
8147
|
if (kind === "not_found") return true;
|
|
7001
|
-
|
|
8148
|
+
if (status !== 400) return false;
|
|
8149
|
+
return /"code"\s*:\s*"?4001\b/.test(message) || /\bcode 4001\b/.test(message);
|
|
7002
8150
|
}
|
|
7003
8151
|
function createTraeGateway(options) {
|
|
7004
8152
|
const logger = options.logger;
|
|
@@ -7148,7 +8296,7 @@ function createTraeGateway(options) {
|
|
|
7148
8296
|
let relayed;
|
|
7149
8297
|
try {
|
|
7150
8298
|
relayed = await openTraeResponse(result.response, model, { onFailure: (error) => {
|
|
7151
|
-
logger?.warn?.(
|
|
8299
|
+
logger?.warn?.("wtq-pool: trae relay stream failed mid-flight", error);
|
|
7152
8300
|
if (!(error instanceof Error && error.name === "AbortError") && !watcher.signal.aborted) options.onAttemptFailure?.(credential, {
|
|
7153
8301
|
status: 0,
|
|
7154
8302
|
kind: "server"
|
|
@@ -7244,13 +8392,13 @@ function createTraeGateway(options) {
|
|
|
7244
8392
|
});
|
|
7245
8393
|
res.flushHeaders();
|
|
7246
8394
|
pumpToResponse(relayStreamOf(relayed), res).catch((error) => {
|
|
7247
|
-
if (!(watcher.signal.aborted || error instanceof Error && error.name === "AbortError")) logger?.warn?.(
|
|
8395
|
+
if (!(watcher.signal.aborted || error instanceof Error && error.name === "AbortError")) logger?.warn?.("wtq-pool: trae relay stream failed mid-flight", error);
|
|
7248
8396
|
if (res.writable) res.end();
|
|
7249
8397
|
});
|
|
7250
8398
|
}
|
|
7251
8399
|
async function handle(req, res) {
|
|
7252
8400
|
if (!guardsPass(req, res)) return;
|
|
7253
|
-
const url = req.url
|
|
8401
|
+
const url = requestPathname(req.url);
|
|
7254
8402
|
if (req.method === "GET" && (url === "/healthz" || url === "/healthz/")) {
|
|
7255
8403
|
writeJson(res, 200, {
|
|
7256
8404
|
ok: true,
|
|
@@ -7270,7 +8418,7 @@ function createTraeGateway(options) {
|
|
|
7270
8418
|
writeOpenAIError(res, 503, "upstream_unavailable", String(error instanceof Error ? error.message : error));
|
|
7271
8419
|
return;
|
|
7272
8420
|
}
|
|
7273
|
-
logger?.warn?.(
|
|
8421
|
+
logger?.warn?.("wtq-pool: trae directory refresh failed; serving the cached roster");
|
|
7274
8422
|
}
|
|
7275
8423
|
const ids = options.listedIds?.();
|
|
7276
8424
|
writeJson(res, 200, {
|
|
@@ -7923,7 +9071,11 @@ async function readQoderCredentials(candidates) {
|
|
|
7923
9071
|
for (const group of byAppDir.values()) {
|
|
7924
9072
|
const result = await readQoderCredential(group);
|
|
7925
9073
|
if (result.credential !== void 0) {
|
|
7926
|
-
|
|
9074
|
+
const fresh = result.credential;
|
|
9075
|
+
const at = credentials.findIndex((existing) => existing.region === fresh.region && existing.userID === fresh.userID);
|
|
9076
|
+
const incumbent = at === -1 ? void 0 : credentials[at];
|
|
9077
|
+
if (incumbent === void 0) credentials.push(fresh);
|
|
9078
|
+
else if (fresh.expiresAtMs > incumbent.expiresAtMs) credentials[at] = fresh;
|
|
7927
9079
|
} else if (result.failure !== void 0 && result.failure.reason !== "no-auth") failures.push(result.failure);
|
|
7928
9080
|
}
|
|
7929
9081
|
return {
|
|
@@ -8039,9 +9191,8 @@ function aesEncryptCBCBase64(plaintext, keyString) {
|
|
|
8039
9191
|
* @param url - the absolute request URL.
|
|
8040
9192
|
* @param credential - `{ userID, token, name, email, machineID }`.
|
|
8041
9193
|
*/
|
|
8042
|
-
function buildCosyAuthHeaders(body, url, credential
|
|
8043
|
-
const
|
|
8044
|
-
const aesKey = options.aesKey ?? uuid().replace(/-/g, "").slice(0, 16);
|
|
9194
|
+
function buildCosyAuthHeaders(body, url, credential) {
|
|
9195
|
+
const aesKey = randomUUID().replace(/-/g, "").slice(0, 16);
|
|
8045
9196
|
const infoB64 = aesEncryptCBCBase64(JSON.stringify({
|
|
8046
9197
|
uid: credential.userID,
|
|
8047
9198
|
security_oauth_token: credential.token,
|
|
@@ -8053,10 +9204,11 @@ function buildCosyAuthHeaders(body, url, credential, options = {}) {
|
|
|
8053
9204
|
key: QODER_RSA_PUBLIC_KEY,
|
|
8054
9205
|
padding: constants.RSA_PKCS1_PADDING
|
|
8055
9206
|
}, Buffer.from(aesKey)).toString("base64");
|
|
8056
|
-
const timestamp = Math.floor(
|
|
9207
|
+
const timestamp = Math.floor(Date.now() / 1e3).toString();
|
|
9208
|
+
const requestId = randomUUID();
|
|
8057
9209
|
const payloadB64 = Buffer.from(JSON.stringify({
|
|
8058
9210
|
version: "v1",
|
|
8059
|
-
requestId
|
|
9211
|
+
requestId,
|
|
8060
9212
|
info: infoB64,
|
|
8061
9213
|
cosyVersion: COSY_VERSION,
|
|
8062
9214
|
ideVersion: ""
|
|
@@ -8084,7 +9236,7 @@ function buildCosyAuthHeaders(body, url, credential, options = {}) {
|
|
|
8084
9236
|
"Cosy-Organization-Id": "",
|
|
8085
9237
|
"Cosy-Organization-Tags": "",
|
|
8086
9238
|
"Login-Version": LOGIN_VERSION,
|
|
8087
|
-
"X-Request-Id":
|
|
9239
|
+
"X-Request-Id": randomUUID()
|
|
8088
9240
|
};
|
|
8089
9241
|
}
|
|
8090
9242
|
//#endregion
|
|
@@ -8445,6 +9597,8 @@ function textOf(content) {
|
|
|
8445
9597
|
function toQoderMessages(messages) {
|
|
8446
9598
|
if (!Array.isArray(messages)) return [];
|
|
8447
9599
|
const out = [];
|
|
9600
|
+
const callsByName = /* @__PURE__ */ new Map();
|
|
9601
|
+
const answered = /* @__PURE__ */ new Set();
|
|
8448
9602
|
for (const raw of messages) {
|
|
8449
9603
|
if (raw === null || typeof raw !== "object") continue;
|
|
8450
9604
|
if (raw["role"] === "system" || raw["role"] === "developer") {
|
|
@@ -8485,19 +9639,40 @@ function toQoderMessages(messages) {
|
|
|
8485
9639
|
role: "assistant",
|
|
8486
9640
|
content: textOf(raw["content"])
|
|
8487
9641
|
};
|
|
8488
|
-
if (toolCalls.length > 0)
|
|
9642
|
+
if (toolCalls.length > 0) {
|
|
9643
|
+
entry["tool_calls"] = toolCalls;
|
|
9644
|
+
for (const call of toolCalls) {
|
|
9645
|
+
const name = String(call["function"]["name"]);
|
|
9646
|
+
callsByName.set(name, [...callsByName.get(name) ?? [], String(call["id"])]);
|
|
9647
|
+
}
|
|
9648
|
+
}
|
|
8489
9649
|
out.push(entry);
|
|
8490
9650
|
continue;
|
|
8491
9651
|
}
|
|
9652
|
+
if (raw["role"] === "function") {
|
|
9653
|
+
const name = raw["name"];
|
|
9654
|
+
const callId = typeof name === "string" ? (callsByName.get(name) ?? []).find((id) => !answered.has(id)) : void 0;
|
|
9655
|
+
if (callId === void 0) throw new Error(`toQoderMessages: function message "${typeof name === "string" ? name : ""}" does not answer any assistant tool_call; the tool loop cannot match it`);
|
|
9656
|
+
answered.add(callId);
|
|
9657
|
+
out.push({
|
|
9658
|
+
role: "tool",
|
|
9659
|
+
tool_call_id: callId,
|
|
9660
|
+
content: textOf(raw["content"])
|
|
9661
|
+
});
|
|
9662
|
+
continue;
|
|
9663
|
+
}
|
|
8492
9664
|
if (raw["role"] === "tool" || raw["role"] === "toolResult") {
|
|
8493
9665
|
const toolCallId = raw["tool_call_id"] ?? raw["toolCallId"];
|
|
8494
9666
|
if (typeof toolCallId !== "string" || toolCallId.length === 0) throw new Error("toQoderMessages: tool result message is missing tool_call_id; it cannot be linked to the calling assistant turn");
|
|
9667
|
+
answered.add(toolCallId);
|
|
8495
9668
|
out.push({
|
|
8496
9669
|
role: "tool",
|
|
8497
9670
|
tool_call_id: toolCallId,
|
|
8498
9671
|
content: textOf(raw["content"])
|
|
8499
9672
|
});
|
|
9673
|
+
continue;
|
|
8500
9674
|
}
|
|
9675
|
+
throw new Error(`toQoderMessages: unsupported message role "${String(raw["role"])}"`);
|
|
8501
9676
|
}
|
|
8502
9677
|
return out;
|
|
8503
9678
|
}
|
|
@@ -8737,6 +9912,7 @@ var QoderUpstreamClient = class {
|
|
|
8737
9912
|
const model = qoderModelOf(raw);
|
|
8738
9913
|
if (model !== void 0) models.push(model);
|
|
8739
9914
|
}
|
|
9915
|
+
if (models.length === 0) throw new Error("Qoder model list contained no callable model");
|
|
8740
9916
|
return models;
|
|
8741
9917
|
}
|
|
8742
9918
|
openApiBase() {
|
|
@@ -8763,36 +9939,42 @@ var QoderUpstreamClient = class {
|
|
|
8763
9939
|
if (!response.ok) throw new Error(`Qoder usage failed: HTTP ${response.status} ${await readErrorBody(response, 300, signal)}`);
|
|
8764
9940
|
return await response.json();
|
|
8765
9941
|
};
|
|
8766
|
-
|
|
8767
|
-
|
|
9942
|
+
/**
|
|
9943
|
+
* Turn one route's document into the panel shape.
|
|
9944
|
+
*
|
|
9945
|
+
* Validation is part of parsing, not a key-presence check before it: a
|
|
9946
|
+
* document carrying only `expiresAt`, or an empty quota object, used to pass
|
|
9947
|
+
* `usable()` and then throw after the fallback had already been skipped.
|
|
9948
|
+
*/
|
|
9949
|
+
const parse = (value, source) => {
|
|
9950
|
+
const envelope = value === null || typeof value !== "object" ? {} : value;
|
|
9951
|
+
const usage = envelope["qoderUsage"] ?? envelope["data"] ?? value;
|
|
9952
|
+
if (usage === null || typeof usage !== "object") throw new Error("Qoder usage answered no usable document");
|
|
9953
|
+
const record = usage;
|
|
9954
|
+
const userQuota = qoderQuotaBucket(record["user_quota"] ?? record["userQuota"]);
|
|
9955
|
+
const addOnQuota = qoderQuotaBucket(record["add_on_quota"] ?? record["addOnQuota"]);
|
|
9956
|
+
const rawExpiry = record["expiresAt"] ?? record["expires_at"];
|
|
9957
|
+
const expiresAtMs = qoderEpochMs(rawExpiry);
|
|
9958
|
+
const neverExpires = expiresAtMs === void 0 && rawExpiry !== void 0 && rawExpiry !== null && rawExpiry !== "";
|
|
9959
|
+
const rawPackages = record["dedicated_resource_packages"] ?? record["dedicatedResourcePackages"];
|
|
9960
|
+
const now = Date.now();
|
|
9961
|
+
const dedicatedPackages = Array.isArray(rawPackages) ? rawPackages.map(qoderDedicatedPackage).filter((entry) => entry !== void 0).filter((entry) => entry.expiresAtMs === void 0 || entry.expiresAtMs > now) : [];
|
|
9962
|
+
if (userQuota === void 0 && addOnQuota === void 0 && dedicatedPackages.length === 0) throw new Error("Qoder usage contained no usable quota");
|
|
9963
|
+
return {
|
|
9964
|
+
...userQuota === void 0 ? {} : { userQuota },
|
|
9965
|
+
...addOnQuota === void 0 ? {} : { addOnQuota },
|
|
9966
|
+
dedicatedPackages,
|
|
9967
|
+
...expiresAtMs === void 0 ? {} : { expiresAtMs },
|
|
9968
|
+
...neverExpires ? { neverExpires: true } : {},
|
|
9969
|
+
source
|
|
9970
|
+
};
|
|
9971
|
+
};
|
|
8768
9972
|
try {
|
|
8769
|
-
|
|
9973
|
+
return parse(await read(QODER_USAGE_PRESENTATION_PATH), "presentation");
|
|
8770
9974
|
} catch (error) {
|
|
8771
9975
|
if (signal?.aborted) throw error;
|
|
8772
|
-
|
|
8773
|
-
|
|
8774
|
-
}
|
|
8775
|
-
const envelope = payload === null || typeof payload !== "object" ? {} : payload;
|
|
8776
|
-
const usage = envelope["qoderUsage"] ?? envelope["data"] ?? payload;
|
|
8777
|
-
if (usage === null || typeof usage !== "object") throw new Error("Qoder usage answered no usable document");
|
|
8778
|
-
const record = usage;
|
|
8779
|
-
const userQuota = qoderQuotaBucket(record["user_quota"] ?? record["userQuota"]);
|
|
8780
|
-
const addOnQuota = qoderQuotaBucket(record["add_on_quota"] ?? record["addOnQuota"]);
|
|
8781
|
-
const rawExpiry = record["expiresAt"] ?? record["expires_at"];
|
|
8782
|
-
const expiresAtMs = qoderEpochMs(rawExpiry);
|
|
8783
|
-
const neverExpires = expiresAtMs === void 0 && rawExpiry !== void 0 && rawExpiry !== null && rawExpiry !== "";
|
|
8784
|
-
const rawPackages = record["dedicated_resource_packages"] ?? record["dedicatedResourcePackages"];
|
|
8785
|
-
const now = Date.now();
|
|
8786
|
-
const dedicatedPackages = Array.isArray(rawPackages) ? rawPackages.map(qoderDedicatedPackage).filter((entry) => entry !== void 0).filter((entry) => entry.expiresAtMs === void 0 || entry.expiresAtMs > now) : [];
|
|
8787
|
-
if (userQuota === void 0 && addOnQuota === void 0 && dedicatedPackages.length === 0) throw new Error("Qoder usage contained no usable quota");
|
|
8788
|
-
return {
|
|
8789
|
-
...userQuota === void 0 ? {} : { userQuota },
|
|
8790
|
-
...addOnQuota === void 0 ? {} : { addOnQuota },
|
|
8791
|
-
dedicatedPackages,
|
|
8792
|
-
...expiresAtMs === void 0 ? {} : { expiresAtMs },
|
|
8793
|
-
...neverExpires ? { neverExpires: true } : {},
|
|
8794
|
-
source
|
|
8795
|
-
};
|
|
9976
|
+
return parse(await read(QODER_USAGE_PATH), "quota");
|
|
9977
|
+
}
|
|
8796
9978
|
}
|
|
8797
9979
|
/**
|
|
8798
9980
|
* Read the raw campaigns payload once. One round trip answers both the
|
|
@@ -8847,12 +10029,13 @@ var QoderUpstreamClient = class {
|
|
|
8847
10029
|
let plain;
|
|
8848
10030
|
try {
|
|
8849
10031
|
plain = buildQoderChatBody(openAiBody, model, options);
|
|
8850
|
-
} catch {
|
|
10032
|
+
} catch (error) {
|
|
10033
|
+
const detail = error instanceof Error ? error.message : "";
|
|
8851
10034
|
return {
|
|
8852
10035
|
ok: false,
|
|
8853
10036
|
status: 400,
|
|
8854
10037
|
kind: "client",
|
|
8855
|
-
message: "invalid JSON request"
|
|
10038
|
+
message: detail === "" ? "invalid JSON request" : detail
|
|
8856
10039
|
};
|
|
8857
10040
|
}
|
|
8858
10041
|
let credential;
|
|
@@ -9355,6 +10538,13 @@ function bridgeQoderStream(source, model, options = {}) {
|
|
|
9355
10538
|
let sawToolCalls = false;
|
|
9356
10539
|
let emittedFinishReason = false;
|
|
9357
10540
|
let usage;
|
|
10541
|
+
let lastKeepaliveAt = 0;
|
|
10542
|
+
const keepalive = (controller) => {
|
|
10543
|
+
const now = Date.now();
|
|
10544
|
+
if (now - lastKeepaliveAt < 1e4) return;
|
|
10545
|
+
lastKeepaliveAt = now;
|
|
10546
|
+
controller.enqueue(encoder.encode(": keepalive\n\n"));
|
|
10547
|
+
};
|
|
9358
10548
|
const reader = source.getReader();
|
|
9359
10549
|
let cancelled = false;
|
|
9360
10550
|
let wakeRoom;
|
|
@@ -9393,7 +10583,10 @@ function bridgeQoderStream(source, model, options = {}) {
|
|
|
9393
10583
|
const consume = (payload, controller) => {
|
|
9394
10584
|
if (finished) return;
|
|
9395
10585
|
const decoded = decodeQoderFrame(payload);
|
|
9396
|
-
if (decoded.type === "unknown")
|
|
10586
|
+
if (decoded.type === "unknown") {
|
|
10587
|
+
keepalive(controller);
|
|
10588
|
+
return;
|
|
10589
|
+
}
|
|
9397
10590
|
if (decoded.type === "failure") {
|
|
9398
10591
|
const { error } = classifyQoderFailure({
|
|
9399
10592
|
code: decoded.code,
|
|
@@ -9789,7 +10982,7 @@ function createQoderGateway(options) {
|
|
|
9789
10982
|
let relayed;
|
|
9790
10983
|
try {
|
|
9791
10984
|
relayed = await openQoderResponse(result.response, model, { onFailure: (error) => {
|
|
9792
|
-
logger?.warn?.(
|
|
10985
|
+
logger?.warn?.("wtq-pool: qoder relay stream failed mid-flight", error);
|
|
9793
10986
|
if (!(error instanceof Error && error.name === "AbortError") && !watcher.signal.aborted) options.onAttemptFailure?.(credential, {
|
|
9794
10987
|
status: 0,
|
|
9795
10988
|
kind: "server"
|
|
@@ -9879,13 +11072,13 @@ function createQoderGateway(options) {
|
|
|
9879
11072
|
});
|
|
9880
11073
|
res.flushHeaders();
|
|
9881
11074
|
pumpToResponse(relayStreamOf(relayed), res).catch((error) => {
|
|
9882
|
-
if (!(watcher.signal.aborted || error instanceof Error && error.name === "AbortError")) logger?.warn?.(
|
|
11075
|
+
if (!(watcher.signal.aborted || error instanceof Error && error.name === "AbortError")) logger?.warn?.("wtq-pool: qoder relay stream failed mid-flight", error);
|
|
9883
11076
|
if (res.writable) res.end();
|
|
9884
11077
|
});
|
|
9885
11078
|
}
|
|
9886
11079
|
async function handle(req, res) {
|
|
9887
11080
|
if (!guardsPass(req, res)) return;
|
|
9888
|
-
const url = req.url
|
|
11081
|
+
const url = requestPathname(req.url);
|
|
9889
11082
|
if (req.method === "GET" && (url === "/healthz" || url === "/healthz/")) {
|
|
9890
11083
|
writeJson(res, 200, {
|
|
9891
11084
|
ok: true,
|
|
@@ -9905,7 +11098,7 @@ function createQoderGateway(options) {
|
|
|
9905
11098
|
writeOpenAIError(res, 503, "upstream_unavailable", String(error instanceof Error ? error.message : error));
|
|
9906
11099
|
return;
|
|
9907
11100
|
}
|
|
9908
|
-
logger?.warn?.(
|
|
11101
|
+
logger?.warn?.("wtq-pool: qoder directory refresh failed; serving the cached roster");
|
|
9909
11102
|
}
|
|
9910
11103
|
const ids = options.listedIds?.();
|
|
9911
11104
|
writeJson(res, 200, {
|
|
@@ -9985,7 +11178,8 @@ function loginHeaders(origin) {
|
|
|
9985
11178
|
"Origin": origin,
|
|
9986
11179
|
"Referer": `${origin}/`,
|
|
9987
11180
|
"User-Agent": LOGIN_UA,
|
|
9988
|
-
"X-Requested-With": "XMLHttpRequest"
|
|
11181
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
11182
|
+
"Connection": "close"
|
|
9989
11183
|
};
|
|
9990
11184
|
}
|
|
9991
11185
|
async function callLogin(url, headers, fetchImpl, body) {
|
|
@@ -10197,13 +11391,14 @@ function createSourcePool(host) {
|
|
|
10197
11391
|
return healthy.concat(held, rest);
|
|
10198
11392
|
}
|
|
10199
11393
|
const live = ordered.filter((credential) => !sourceCredentialExpired(credential, now));
|
|
10200
|
-
|
|
11394
|
+
const peek = rotate === "peek";
|
|
11395
|
+
if (rotate !== true && !peek || preferred !== "" || live.length < 2 || host.sourceDistribution(source) !== "round-robin") return ordered;
|
|
10201
11396
|
const healthy = live.filter((credential) => !sourceFailureActive(source, region, labelOf(credential), now));
|
|
10202
11397
|
const held = live.filter((credential) => sourceFailureActive(source, region, labelOf(credential), now));
|
|
10203
11398
|
if (healthy.length < 2) return ordered;
|
|
10204
11399
|
const key = host.sourceRegionKey(source, region);
|
|
10205
11400
|
const step = (sourceRotation.get(key) ?? 0) % healthy.length;
|
|
10206
|
-
sourceRotation.set(key, step + 1);
|
|
11401
|
+
if (!peek) sourceRotation.set(key, step + 1);
|
|
10207
11402
|
const rest = ordered.filter((credential) => sourceCredentialExpired(credential, now));
|
|
10208
11403
|
return healthy.slice(step).concat(healthy.slice(0, step), held, rest);
|
|
10209
11404
|
}
|
|
@@ -10241,14 +11436,13 @@ function createSourcePool(host) {
|
|
|
10241
11436
|
* upstream SSE body relayed as it arrives. Nothing leaves this machine except
|
|
10242
11437
|
* to the three vendors' own endpoints.
|
|
10243
11438
|
*/
|
|
10244
|
-
/** Read from the package, so this can never drift behind the version it reports. */
|
|
10245
11439
|
/** The one background service this plugin registers. */
|
|
10246
11440
|
const SERVICE_ID = "provider-host";
|
|
10247
11441
|
/** The two gateways, matching the provider ids this plugin registers. */
|
|
10248
11442
|
const REGIONS = ["cn", "global"];
|
|
10249
11443
|
const PROVIDER_ID_BY_REGION = {
|
|
10250
|
-
cn: "
|
|
10251
|
-
global: "
|
|
11444
|
+
cn: "wtqpool-cn",
|
|
11445
|
+
global: "wtqpool-global"
|
|
10252
11446
|
};
|
|
10253
11447
|
const PROVIDER_NAME_BY_REGION = {
|
|
10254
11448
|
cn: "WTQ Pool (CN)",
|
|
@@ -10309,8 +11503,8 @@ const REGION_PORTS = {
|
|
|
10309
11503
|
*/
|
|
10310
11504
|
const TRAE_REGIONS = ["cn", "ai"];
|
|
10311
11505
|
const TRAE_PROVIDER_ID_BY_REGION = {
|
|
10312
|
-
cn: "
|
|
10313
|
-
ai: "
|
|
11506
|
+
cn: "wtqpool-trae-cn",
|
|
11507
|
+
ai: "wtqpool-trae-ai"
|
|
10314
11508
|
};
|
|
10315
11509
|
const TRAE_PROVIDER_NAME_BY_REGION = {
|
|
10316
11510
|
cn: "WTQ Pool Trae (CN)",
|
|
@@ -10335,8 +11529,8 @@ function activeTraeRegions() {
|
|
|
10335
11529
|
*/
|
|
10336
11530
|
const QODER_REGIONS = ["cn", "global"];
|
|
10337
11531
|
const QODER_PROVIDER_ID_BY_REGION = {
|
|
10338
|
-
cn: "
|
|
10339
|
-
global: "
|
|
11532
|
+
cn: "wtqpool-qoder-cn",
|
|
11533
|
+
global: "wtqpool-qoder-global"
|
|
10340
11534
|
};
|
|
10341
11535
|
const QODER_PROVIDER_NAME_BY_REGION = {
|
|
10342
11536
|
cn: "WTQ Pool Qoder (CN)",
|
|
@@ -10491,6 +11685,14 @@ let settings = {};
|
|
|
10491
11685
|
* a switch the user just flipped.
|
|
10492
11686
|
*/
|
|
10493
11687
|
let settingsRevision = 0;
|
|
11688
|
+
/**
|
|
11689
|
+
* Bumped only by writes this process made, never by a document the host pushed.
|
|
11690
|
+
*
|
|
11691
|
+
* A host announcement that was already in flight when a local save landed is
|
|
11692
|
+
* older than that save. `onHostSettingsChanged` captures this counter before it
|
|
11693
|
+
* queues, and drops the document when a local write overtook it.
|
|
11694
|
+
*/
|
|
11695
|
+
let localSettingsRevision = 0;
|
|
10494
11696
|
let dataPath = "";
|
|
10495
11697
|
/** One runtime per gateway: its shim and catalog. */
|
|
10496
11698
|
const runtimes = /* @__PURE__ */ new Map();
|
|
@@ -10525,6 +11727,19 @@ const pendingRuntimeStops = [];
|
|
|
10525
11727
|
* drift while every request to that address was refused.
|
|
10526
11728
|
*/
|
|
10527
11729
|
const gatewayStartFailures = /* @__PURE__ */ new Map();
|
|
11730
|
+
/**
|
|
11731
|
+
* Trae/Qoder regions whose gateway could not start, keyed `source:region`.
|
|
11732
|
+
*
|
|
11733
|
+
* WorkBuddy records its own start failures; without the same record for the two
|
|
11734
|
+
* source gateways a region whose bind failed kept the address the host had
|
|
11735
|
+
* already registered, so every request to it was refused while the doctor and
|
|
11736
|
+
* the panel read the region as healthy. Entries are cleared wherever the
|
|
11737
|
+
* gateway is installed or removed, so a switched-off source never reads as a
|
|
11738
|
+
* failed one.
|
|
11739
|
+
*/
|
|
11740
|
+
const sourceStartFailures = /* @__PURE__ */ new Map();
|
|
11741
|
+
/** One in-flight source-gateway start per region, shared by overlapping passes. */
|
|
11742
|
+
const sourceGatewayStarts = /* @__PURE__ */ new Map();
|
|
10528
11743
|
/** Provider ids the host has confirmed it is serving, this process's lifetime. */
|
|
10529
11744
|
let publishedIds = /* @__PURE__ */ new Set();
|
|
10530
11745
|
/**
|
|
@@ -10696,7 +11911,7 @@ const STREAK_REPORT_READ_GAP_MS = 1200;
|
|
|
10696
11911
|
const travelAdoptTried = /* @__PURE__ */ new Map();
|
|
10697
11912
|
function log(level, message) {
|
|
10698
11913
|
try {
|
|
10699
|
-
console.log("[
|
|
11914
|
+
console.log("[wtq-pool] " + level + ": " + message);
|
|
10700
11915
|
} catch {}
|
|
10701
11916
|
}
|
|
10702
11917
|
/**
|
|
@@ -10769,6 +11984,7 @@ async function saveSettings(patch) {
|
|
|
10769
11984
|
...patch
|
|
10770
11985
|
};
|
|
10771
11986
|
settingsRevision += 1;
|
|
11987
|
+
localSettingsRevision += 1;
|
|
10772
11988
|
return true;
|
|
10773
11989
|
} catch (error) {
|
|
10774
11990
|
log("warn", "could not save settings: " + safeMessage(error));
|
|
@@ -10817,6 +12033,7 @@ async function mutateSettings(key, mutate) {
|
|
|
10817
12033
|
[key]: next
|
|
10818
12034
|
};
|
|
10819
12035
|
settingsRevision += 1;
|
|
12036
|
+
localSettingsRevision += 1;
|
|
10820
12037
|
return true;
|
|
10821
12038
|
} catch (error) {
|
|
10822
12039
|
log("warn", "could not save settings: " + safeMessage(error));
|
|
@@ -10848,15 +12065,34 @@ function resetDiscoveryCaches() {
|
|
|
10848
12065
|
* reach the picker or the user still sees the models they just disabled.
|
|
10849
12066
|
*/
|
|
10850
12067
|
async function onHostSettingsChanged(next) {
|
|
10851
|
-
if (serviceStopped) return;
|
|
10852
12068
|
if (next === null || typeof next !== "object" || Array.isArray(next)) return false;
|
|
10853
|
-
const
|
|
10854
|
-
|
|
10855
|
-
|
|
10856
|
-
|
|
10857
|
-
|
|
10858
|
-
|
|
10859
|
-
|
|
12069
|
+
const localRevision = localSettingsRevision;
|
|
12070
|
+
return enqueueWrite(async () => {
|
|
12071
|
+
if (serviceStopped) return false;
|
|
12072
|
+
const before = settings;
|
|
12073
|
+
if (localSettingsRevision !== localRevision) {
|
|
12074
|
+
if (!await adoptHostSettings()) {
|
|
12075
|
+
scheduleRefresh("settings");
|
|
12076
|
+
return false;
|
|
12077
|
+
}
|
|
12078
|
+
} else {
|
|
12079
|
+
settings = next;
|
|
12080
|
+
settingsRevision += 1;
|
|
12081
|
+
}
|
|
12082
|
+
const toggled = Object.values(SOURCE_SETTING_KEYS).some((key) => Object.hasOwn(settings, key) && before[key] !== settings[key]);
|
|
12083
|
+
if (toggled) resetDiscoveryCaches();
|
|
12084
|
+
await applySettingsEverywhere(toggled);
|
|
12085
|
+
return true;
|
|
12086
|
+
});
|
|
12087
|
+
}
|
|
12088
|
+
/**
|
|
12089
|
+
* Apply the settings document already in `settings` to everything downstream.
|
|
12090
|
+
*
|
|
12091
|
+
* Shared by the host-driven change above and the panel's own switches, which
|
|
12092
|
+
* write the key first and then ask for the same reconciliation with no new
|
|
12093
|
+
* document to merge.
|
|
12094
|
+
*/
|
|
12095
|
+
async function applySettingsEverywhere(toggled) {
|
|
10860
12096
|
applySettingsToCore();
|
|
10861
12097
|
traeAccountRows = traeAccountRows.filter((row) => regionEnabled("trae", row.region));
|
|
10862
12098
|
qoderAccountRows = qoderAccountRows.filter((row) => regionEnabled("qoder", row.region));
|
|
@@ -10873,6 +12109,7 @@ async function onHostSettingsChanged(next) {
|
|
|
10873
12109
|
}
|
|
10874
12110
|
/** Subscribe to the host's settings event, tolerating a host without it. */
|
|
10875
12111
|
function subscribeToHostSettings() {
|
|
12112
|
+
unsubscribeFromHostSettings();
|
|
10876
12113
|
settingsChangedHandler = (next) => {
|
|
10877
12114
|
onHostSettingsChanged(next).then((applied) => {
|
|
10878
12115
|
if (applied) log("info", "settings changed in the host UI; selection re-applied");
|
|
@@ -10889,6 +12126,17 @@ function subscribeToHostSettings() {
|
|
|
10889
12126
|
settingsChangedHandler = null;
|
|
10890
12127
|
return false;
|
|
10891
12128
|
}
|
|
12129
|
+
/** Detach the host settings handler, if one is held. */
|
|
12130
|
+
function unsubscribeFromHostSettings() {
|
|
12131
|
+
const handler = settingsChangedHandler;
|
|
12132
|
+
settingsChangedHandler = null;
|
|
12133
|
+
if (handler === null) return;
|
|
12134
|
+
try {
|
|
12135
|
+
if (pi.events && typeof pi.events.off === "function") pi.events.off("plugin:settingsChanged", handler);
|
|
12136
|
+
} catch (error) {
|
|
12137
|
+
log("warn", "could not unsubscribe from settings changes: " + safeMessage(error));
|
|
12138
|
+
}
|
|
12139
|
+
}
|
|
10892
12140
|
function cooldownMs() {
|
|
10893
12141
|
const value = Number(settings.cooldownSeconds);
|
|
10894
12142
|
if (!Number.isFinite(value) || value <= 0) return 6e4;
|
|
@@ -10919,6 +12167,10 @@ function autoCheckinEnabled() {
|
|
|
10919
12167
|
* restart after the morning hour forgot every slot and re-ran the whole pass.
|
|
10920
12168
|
* The file holds one array per campaign day, the same shape the desktop twin
|
|
10921
12169
|
* writes, so the two engines cannot disagree about what a day's record is.
|
|
12170
|
+
* `_qoderRetryAt` is metadata beside those arrays: Qoder's deferred round can
|
|
12171
|
+
* open hours after the pass that scheduled it, and a reload before then used to
|
|
12172
|
+
* lose the day's Qoder credits. A key that is not an array is ignored by the
|
|
12173
|
+
* day reader, so the file stays compatible with the twin.
|
|
10922
12174
|
*/
|
|
10923
12175
|
const CHECKIN_SLOTS_FILE = "checkin-slots.json";
|
|
10924
12176
|
/**
|
|
@@ -10932,6 +12184,7 @@ const CHECKIN_SLOTS_FILE = "checkin-slots.json";
|
|
|
10932
12184
|
*/
|
|
10933
12185
|
function loadCheckinSlots() {
|
|
10934
12186
|
autoCheckinSlots.clear();
|
|
12187
|
+
qoderRetryAt = 0;
|
|
10935
12188
|
if (dataPath === "") return;
|
|
10936
12189
|
try {
|
|
10937
12190
|
const parsed = JSON.parse(fs$1.readFileSync(path.join(dataPath, CHECKIN_SLOTS_FILE), "utf8"));
|
|
@@ -10940,6 +12193,9 @@ function loadCheckinSlots() {
|
|
|
10940
12193
|
if (!Array.isArray(slots)) continue;
|
|
10941
12194
|
for (const slot of slots) if (typeof slot === "string" && slot !== "") autoCheckinSlots.add(slot);
|
|
10942
12195
|
}
|
|
12196
|
+
const retry = parsed._qoderRetryAt;
|
|
12197
|
+
const now = Date.now();
|
|
12198
|
+
if (typeof retry === "number" && Number.isFinite(retry) && retry > 0 && (zonedDayHour(new Date(retry)).day === zonedDayHour(new Date(now)).day || retry > now - 72e5 && retry <= now + 864e5)) qoderRetryAt = retry;
|
|
10943
12199
|
} catch {}
|
|
10944
12200
|
}
|
|
10945
12201
|
/**
|
|
@@ -10960,6 +12216,7 @@ function saveCheckinSlots() {
|
|
|
10960
12216
|
}
|
|
10961
12217
|
const keep = Object.keys(byDay).sort().slice(-3);
|
|
10962
12218
|
for (const day of Object.keys(byDay)) if (!keep.includes(day)) delete byDay[day];
|
|
12219
|
+
if (qoderRetryAt > 0) byDay._qoderRetryAt = qoderRetryAt;
|
|
10963
12220
|
const target = path.join(dataPath, CHECKIN_SLOTS_FILE);
|
|
10964
12221
|
const staging = target + ".tmp";
|
|
10965
12222
|
try {
|
|
@@ -11576,15 +12833,18 @@ function warmingGatewayIds() {
|
|
|
11576
12833
|
if (gatewayStartFailures.has(region)) continue;
|
|
11577
12834
|
if (runtime === void 0 || runtime.connecting !== null) ids.add(PROVIDER_ID_BY_REGION[region]);
|
|
11578
12835
|
}
|
|
11579
|
-
for (const [gateways, regions, idByRegion] of [[
|
|
12836
|
+
for (const [source, gateways, regions, idByRegion] of [[
|
|
12837
|
+
"trae",
|
|
11580
12838
|
traeGateways,
|
|
11581
12839
|
activeTraeRegions(),
|
|
11582
12840
|
TRAE_PROVIDER_ID_BY_REGION
|
|
11583
12841
|
], [
|
|
12842
|
+
"qoder",
|
|
11584
12843
|
qoderGateways,
|
|
11585
12844
|
activeQoderRegions(),
|
|
11586
12845
|
QODER_PROVIDER_ID_BY_REGION
|
|
11587
12846
|
]]) for (const region of regions) {
|
|
12847
|
+
if (sourceStartFailures.has(source + ":" + region)) continue;
|
|
11588
12848
|
const entry = gateways.get(region);
|
|
11589
12849
|
if (entry === void 0 || entry.gateway.models().length === 0 && entry.lastError === "") ids.add(idByRegion[region]);
|
|
11590
12850
|
}
|
|
@@ -11610,6 +12870,7 @@ async function warmSourceDirectory(entry, signal) {
|
|
|
11610
12870
|
entry.lastError = entry.gateway.models().length > 0 ? "" : "directory unavailable: " + safeMessage(error);
|
|
11611
12871
|
if (entry.lastError !== "") log("warn", "[" + entry.source + ":" + entry.region + "] " + entry.lastError);
|
|
11612
12872
|
}
|
|
12873
|
+
entry.warmSettled = true;
|
|
11613
12874
|
if (entry.lastError === "" && entry.gateway.models().length > 0) syncDeclaration();
|
|
11614
12875
|
return entry.lastError === "";
|
|
11615
12876
|
}
|
|
@@ -11711,7 +12972,8 @@ async function createTraeRuntime(region) {
|
|
|
11711
12972
|
portFallback: bound !== preferred,
|
|
11712
12973
|
preferredPort: preferred,
|
|
11713
12974
|
lastError: "",
|
|
11714
|
-
source: "trae"
|
|
12975
|
+
source: "trae",
|
|
12976
|
+
warmSettled: false
|
|
11715
12977
|
};
|
|
11716
12978
|
if (entry.portFallback) log("warn", "[trae:" + region + "] preferred port " + preferred + " is held by another process; serving this session on port " + gateway.port());
|
|
11717
12979
|
warmSourceDirectoryAtStartup(entry, warmAbort.signal).catch((error) => log("warn", "[trae:" + region + "] directory warm-up failed: " + safeMessage(error)));
|
|
@@ -11723,8 +12985,10 @@ async function reconcileTraeGateways() {
|
|
|
11723
12985
|
if (!stillLoaded(generation) || serviceStopped) return;
|
|
11724
12986
|
const active = activeTraeRegions();
|
|
11725
12987
|
for (const [region, entry] of Array.from(traeGateways)) {
|
|
12988
|
+
if (!stillLoaded(generation) || serviceStopped) return;
|
|
11726
12989
|
if (active.includes(region)) continue;
|
|
11727
12990
|
traeGateways.delete(region);
|
|
12991
|
+
sourceStartFailures.delete("trae:" + region);
|
|
11728
12992
|
try {
|
|
11729
12993
|
await entry.gateway.close();
|
|
11730
12994
|
} catch (error) {
|
|
@@ -11732,13 +12996,30 @@ async function reconcileTraeGateways() {
|
|
|
11732
12996
|
}
|
|
11733
12997
|
}
|
|
11734
12998
|
for (const region of active) {
|
|
12999
|
+
if (!stillLoaded(generation) || serviceStopped) return;
|
|
11735
13000
|
if (traeGateways.has(region)) continue;
|
|
13001
|
+
const key = "trae:" + region;
|
|
13002
|
+
const pending = sourceGatewayStarts.get(key);
|
|
13003
|
+
if (pending !== void 0) {
|
|
13004
|
+
await pending.catch(() => {});
|
|
13005
|
+
continue;
|
|
13006
|
+
}
|
|
13007
|
+
const starting = createTraeRuntime(region);
|
|
13008
|
+
sourceGatewayStarts.set(key, starting);
|
|
11736
13009
|
try {
|
|
11737
|
-
const entry = await
|
|
13010
|
+
const entry = await starting;
|
|
11738
13011
|
if (!stillLoaded(generation) || !activeTraeRegions().includes(region) || traeGateways.has(region)) await entry.gateway.close();
|
|
11739
|
-
else
|
|
13012
|
+
else {
|
|
13013
|
+
traeGateways.set(region, entry);
|
|
13014
|
+
sourceStartFailures.delete(key);
|
|
13015
|
+
}
|
|
11740
13016
|
} catch (error) {
|
|
11741
|
-
|
|
13017
|
+
if (stillLoaded(generation)) {
|
|
13018
|
+
sourceStartFailures.set(key, safeMessage(error));
|
|
13019
|
+
log("warn", "[trae:" + region + "] gateway could not start: " + safeMessage(error));
|
|
13020
|
+
}
|
|
13021
|
+
} finally {
|
|
13022
|
+
if (sourceGatewayStarts.get(key) === starting) sourceGatewayStarts.delete(key);
|
|
11742
13023
|
}
|
|
11743
13024
|
}
|
|
11744
13025
|
}
|
|
@@ -11847,7 +13128,8 @@ async function createQoderRuntime(region) {
|
|
|
11847
13128
|
portFallback: bound !== preferred,
|
|
11848
13129
|
preferredPort: preferred,
|
|
11849
13130
|
lastError: "",
|
|
11850
|
-
source: "qoder"
|
|
13131
|
+
source: "qoder",
|
|
13132
|
+
warmSettled: false
|
|
11851
13133
|
};
|
|
11852
13134
|
if (entry.portFallback) log("warn", "[qoder:" + region + "] preferred port " + preferred + " is held by another process; serving this session on port " + gateway.port());
|
|
11853
13135
|
warmSourceDirectoryAtStartup(entry, warmAbort.signal).catch((error) => log("warn", "[qoder:" + region + "] directory warm-up failed: " + safeMessage(error)));
|
|
@@ -11859,8 +13141,10 @@ async function reconcileQoderGateways() {
|
|
|
11859
13141
|
if (!stillLoaded(generation) || serviceStopped) return;
|
|
11860
13142
|
const active = activeQoderRegions();
|
|
11861
13143
|
for (const [region, entry] of Array.from(qoderGateways)) {
|
|
13144
|
+
if (!stillLoaded(generation) || serviceStopped) return;
|
|
11862
13145
|
if (active.includes(region)) continue;
|
|
11863
13146
|
qoderGateways.delete(region);
|
|
13147
|
+
sourceStartFailures.delete("qoder:" + region);
|
|
11864
13148
|
try {
|
|
11865
13149
|
await entry.gateway.close();
|
|
11866
13150
|
} catch (error) {
|
|
@@ -11868,13 +13152,30 @@ async function reconcileQoderGateways() {
|
|
|
11868
13152
|
}
|
|
11869
13153
|
}
|
|
11870
13154
|
for (const region of active) {
|
|
13155
|
+
if (!stillLoaded(generation) || serviceStopped) return;
|
|
11871
13156
|
if (qoderGateways.has(region)) continue;
|
|
13157
|
+
const key = "qoder:" + region;
|
|
13158
|
+
const pending = sourceGatewayStarts.get(key);
|
|
13159
|
+
if (pending !== void 0) {
|
|
13160
|
+
await pending.catch(() => {});
|
|
13161
|
+
continue;
|
|
13162
|
+
}
|
|
13163
|
+
const starting = createQoderRuntime(region);
|
|
13164
|
+
sourceGatewayStarts.set(key, starting);
|
|
11872
13165
|
try {
|
|
11873
|
-
const entry = await
|
|
13166
|
+
const entry = await starting;
|
|
11874
13167
|
if (!stillLoaded(generation) || !activeQoderRegions().includes(region) || qoderGateways.has(region)) await entry.gateway.close();
|
|
11875
|
-
else
|
|
13168
|
+
else {
|
|
13169
|
+
qoderGateways.set(region, entry);
|
|
13170
|
+
sourceStartFailures.delete(key);
|
|
13171
|
+
}
|
|
11876
13172
|
} catch (error) {
|
|
11877
|
-
|
|
13173
|
+
if (stillLoaded(generation)) {
|
|
13174
|
+
sourceStartFailures.set(key, safeMessage(error));
|
|
13175
|
+
log("warn", "[qoder:" + region + "] gateway could not start: " + safeMessage(error));
|
|
13176
|
+
}
|
|
13177
|
+
} finally {
|
|
13178
|
+
if (sourceGatewayStarts.get(key) === starting) sourceGatewayStarts.delete(key);
|
|
11878
13179
|
}
|
|
11879
13180
|
}
|
|
11880
13181
|
}
|
|
@@ -12018,8 +13319,9 @@ function startRuntimeOnce(region) {
|
|
|
12018
13319
|
* is every later pass's only re-entry test, so a stopped entry there would take
|
|
12019
13320
|
* the region out until the next teardown.
|
|
12020
13321
|
*
|
|
12021
|
-
* This host's `onLoad` clears both maps, so it never shares a bind across a
|
|
12022
|
-
* PI-Desktop
|
|
13322
|
+
* This host's `onLoad` clears both maps, so it never shares a bind across a
|
|
13323
|
+
* reload; PI-Desktop clears them too and relies on this same ordering within one
|
|
13324
|
+
* activation.
|
|
12023
13325
|
*/
|
|
12024
13326
|
async function settleStartedRuntime(region, runtime, unwanted) {
|
|
12025
13327
|
const held = runtimes.get(region);
|
|
@@ -12052,8 +13354,10 @@ async function startRuntimes() {
|
|
|
12052
13354
|
try {
|
|
12053
13355
|
await settleStartedRuntime(region, await startRuntimeOnce(region), !stillLoaded(generation) || !activeRegions().includes(region));
|
|
12054
13356
|
} catch (error) {
|
|
12055
|
-
|
|
12056
|
-
|
|
13357
|
+
if (stillLoaded(generation)) {
|
|
13358
|
+
gatewayStartFailures.set(region, safeMessage(error));
|
|
13359
|
+
log("warn", "[workbuddy:" + region + "] gateway could not start: " + safeMessage(error));
|
|
13360
|
+
}
|
|
12057
13361
|
}
|
|
12058
13362
|
}));
|
|
12059
13363
|
}
|
|
@@ -12105,7 +13409,8 @@ async function connect(runtime, force) {
|
|
|
12105
13409
|
if (runtime.retired) return false;
|
|
12106
13410
|
runtime.lastError = "";
|
|
12107
13411
|
runtime.lastWarning = "";
|
|
12108
|
-
const
|
|
13412
|
+
const regionAccounts = core.pool.list(runtime.region);
|
|
13413
|
+
const account = regionAccounts.find((item) => core.pool.activeHold(item.id)?.reason !== "session") ?? regionAccounts[0];
|
|
12109
13414
|
if (account === void 0) {
|
|
12110
13415
|
runtime.signedIn = false;
|
|
12111
13416
|
runtime.lastError = "no " + runtime.region + " WorkBuddy sign-in found";
|
|
@@ -12119,7 +13424,7 @@ async function connect(runtime, force) {
|
|
|
12119
13424
|
}
|
|
12120
13425
|
runtime.signedIn = true;
|
|
12121
13426
|
if (force === true || !catalogFresh(runtime)) try {
|
|
12122
|
-
const models = await core.
|
|
13427
|
+
const models = await core.pool.readAfterRefresh(account.id, (credential) => core.client.fetchModels(credential, AbortSignal.timeout(MODELS_TIMEOUT_MS)));
|
|
12123
13428
|
if (runtime.retired) return false;
|
|
12124
13429
|
runtime.catalog.update(catalogFromUpstream(models));
|
|
12125
13430
|
runtime.catalogFetchedAtMs = Date.now();
|
|
@@ -12176,6 +13481,7 @@ function probesStale() {
|
|
|
12176
13481
|
if (account.cooldownUntilMs > now || core.pool.isDisabled(account.id) || !regionEnabled("workbuddy", regionOf(account.credential.domain))) continue;
|
|
12177
13482
|
const cached = probeCache.get(account.id);
|
|
12178
13483
|
if (cached === void 0 || now - cached.atMs >= PROBE_TTL_MS) return true;
|
|
13484
|
+
if (cached.checkinOnly === true) return true;
|
|
12179
13485
|
}
|
|
12180
13486
|
return false;
|
|
12181
13487
|
}
|
|
@@ -12557,12 +13863,13 @@ function pruneSourceState() {
|
|
|
12557
13863
|
/**
|
|
12558
13864
|
* Directories the WorkBuddy pool reads, in order.
|
|
12559
13865
|
*
|
|
12560
|
-
* The desktop app's own directories come first,
|
|
12561
|
-
*
|
|
13866
|
+
* The desktop app's own directories come first, and that is only the walk
|
|
13867
|
+
* order: which copy of an account serves is decided by issue time, with the
|
|
13868
|
+
* source breaking a tie. The plugin's own directory is appended rather than
|
|
12562
13869
|
* substituted: an account added from the panel is read on exactly the same
|
|
12563
13870
|
* terms as a desktop sign-in, and removing it is a file deletion the user can
|
|
12564
|
-
* see. A repointed `authFile` narrows the walk to that one directory
|
|
12565
|
-
*
|
|
13871
|
+
* see. A repointed `authFile` narrows the walk to that one directory plus the
|
|
13872
|
+
* plugin's own, the way the pool reads whatever it writes.
|
|
12566
13873
|
*/
|
|
12567
13874
|
function poolAuthDirs(authFile) {
|
|
12568
13875
|
if (authFile !== "") return [path.dirname(authFile)];
|
|
@@ -12654,11 +13961,14 @@ function applyProbe(row, probe) {
|
|
|
12654
13961
|
async function accountProbe(account, allowNetwork, checkinOnly = false) {
|
|
12655
13962
|
const cached = probeCache.get(account.id);
|
|
12656
13963
|
if (allowNetwork !== true) return cached === void 0 ? { pending: true } : cached.value;
|
|
12657
|
-
|
|
13964
|
+
const cachedFresh = cached !== void 0 && Date.now() - cached.atMs < PROBE_TTL_MS;
|
|
13965
|
+
const cachedHasCredits = cached !== void 0 && (cached.value.credits !== void 0 || cached.value.creditsError !== void 0);
|
|
13966
|
+
if (cachedFresh && (checkinOnly || cached.checkinOnly !== true && cachedHasCredits)) return cached.value;
|
|
13967
|
+
if (signInRefused(account)) return {};
|
|
12658
13968
|
const value = {};
|
|
12659
13969
|
if (!checkinOnly) {
|
|
12660
13970
|
try {
|
|
12661
|
-
const credits = await core.
|
|
13971
|
+
const credits = await core.pool.readAfterRefresh(account.id, (credential) => core.client.fetchCredits(credential));
|
|
12662
13972
|
value.credits = {
|
|
12663
13973
|
...credits.total === void 0 ? {} : { total: credits.total },
|
|
12664
13974
|
packages: (credits.packages || []).map((pack) => ({
|
|
@@ -12677,8 +13987,9 @@ async function accountProbe(account, allowNetwork, checkinOnly = false) {
|
|
|
12677
13987
|
}
|
|
12678
13988
|
if (value.credits !== void 0 && typeof value.credits.total === "number") core.pool.setCreditBalance(account.id, value.credits.total, value.credits.nearestExpiryMs);
|
|
12679
13989
|
}
|
|
13990
|
+
const checkinStartedAt = Date.now();
|
|
12680
13991
|
try {
|
|
12681
|
-
const checkin = await core.
|
|
13992
|
+
const checkin = await core.pool.readAfterRefresh(account.id, (credential) => core.client.fetchCheckinStatus(credential));
|
|
12682
13993
|
value.checkin = {
|
|
12683
13994
|
active: checkin.active,
|
|
12684
13995
|
todayCheckedIn: checkin.todayCheckedIn,
|
|
@@ -12692,15 +14003,54 @@ async function accountProbe(account, allowNetwork, checkinOnly = false) {
|
|
|
12692
14003
|
} catch (error) {
|
|
12693
14004
|
value.checkinError = safeMessage(error);
|
|
12694
14005
|
}
|
|
14006
|
+
const latest = probeCache.get(account.id);
|
|
14007
|
+
if (latest !== cached) {
|
|
14008
|
+
if (latest === void 0) return { pending: true };
|
|
14009
|
+
if (!checkinOnly && latest.checkinOnly === true && (value.credits !== void 0 || value.creditsError !== void 0)) {
|
|
14010
|
+
const merged = { ...latest.value };
|
|
14011
|
+
if (value.credits !== void 0) {
|
|
14012
|
+
merged.credits = value.credits;
|
|
14013
|
+
delete merged.creditsError;
|
|
14014
|
+
}
|
|
14015
|
+
if (value.creditsError !== void 0) {
|
|
14016
|
+
merged.creditsError = value.creditsError;
|
|
14017
|
+
delete merged.credits;
|
|
14018
|
+
}
|
|
14019
|
+
if (latest.atMs <= checkinStartedAt) {
|
|
14020
|
+
if (value.checkin !== void 0) {
|
|
14021
|
+
merged.checkin = value.checkin;
|
|
14022
|
+
delete merged.checkinError;
|
|
14023
|
+
}
|
|
14024
|
+
if (value.checkinError !== void 0) {
|
|
14025
|
+
merged.checkinError = value.checkinError;
|
|
14026
|
+
delete merged.checkin;
|
|
14027
|
+
}
|
|
14028
|
+
}
|
|
14029
|
+
probeCache.set(account.id, {
|
|
14030
|
+
atMs: Date.now(),
|
|
14031
|
+
value: merged,
|
|
14032
|
+
checkinOnly: false
|
|
14033
|
+
});
|
|
14034
|
+
return merged;
|
|
14035
|
+
}
|
|
14036
|
+
return latest.value;
|
|
14037
|
+
}
|
|
14038
|
+
const stored = checkinOnly && cached !== void 0 ? {
|
|
14039
|
+
...cached.value,
|
|
14040
|
+
...value
|
|
14041
|
+
} : value;
|
|
14042
|
+
if (value.checkin !== void 0) delete stored.checkinError;
|
|
14043
|
+
if (value.checkinError !== void 0) delete stored.checkin;
|
|
12695
14044
|
probeCache.set(account.id, {
|
|
12696
14045
|
atMs: Date.now(),
|
|
12697
|
-
value
|
|
14046
|
+
value: stored,
|
|
14047
|
+
checkinOnly
|
|
12698
14048
|
});
|
|
12699
14049
|
if (probeCache.size > core.pool.list().length) {
|
|
12700
14050
|
const live = new Set(core.pool.list().map((item) => item.id));
|
|
12701
14051
|
for (const id of Array.from(probeCache.keys())) if (!live.has(id)) probeCache.delete(id);
|
|
12702
14052
|
}
|
|
12703
|
-
return
|
|
14053
|
+
return stored;
|
|
12704
14054
|
}
|
|
12705
14055
|
/**
|
|
12706
14056
|
* Per-gateway roster warnings, for gateways that have one right now.
|
|
@@ -12717,6 +14067,28 @@ function catalogWarnings() {
|
|
|
12717
14067
|
}
|
|
12718
14068
|
return Object.keys(warnings).length === 0 ? {} : { warnings };
|
|
12719
14069
|
}
|
|
14070
|
+
/**
|
|
14071
|
+
* Whether this account's sign-in is one the upstream has refused.
|
|
14072
|
+
*
|
|
14073
|
+
* The hold is the pool's own record that a refresh could not save the sign-in,
|
|
14074
|
+
* so every pass that walks the pool asks before it spends a call: a held
|
|
14075
|
+
* account cannot answer, and the panel row already says why.
|
|
14076
|
+
*/
|
|
14077
|
+
function signInRefused(account) {
|
|
14078
|
+
return core.pool.activeHold(account.id)?.reason === "session";
|
|
14079
|
+
}
|
|
14080
|
+
/**
|
|
14081
|
+
* The row fields that describe a hold the pool itself decided, if one is in
|
|
14082
|
+
* force for this account right now.
|
|
14083
|
+
*
|
|
14084
|
+
* Empty spread when the account is simply cooling on an upstream window: that
|
|
14085
|
+
* state already reads as one on the row, and its remedy is to wait.
|
|
14086
|
+
*/
|
|
14087
|
+
function activeHoldOf(accountId, now) {
|
|
14088
|
+
const hold = core.pool.activeHold(accountId);
|
|
14089
|
+
if (hold === null || hold.untilMs <= now) return {};
|
|
14090
|
+
return { hold: hold.reason };
|
|
14091
|
+
}
|
|
12720
14092
|
async function statusDocument(options) {
|
|
12721
14093
|
const allowNetwork = options?.allowNetwork === true;
|
|
12722
14094
|
const probeBudget = typeof options?.budgetMs === "number" ? Math.min(PROBE_BATCH_BUDGET_MS, Math.max(0, options.budgetMs)) : PROBE_BATCH_BUDGET_MS;
|
|
@@ -12755,6 +14127,7 @@ async function statusDocument(options) {
|
|
|
12755
14127
|
...cooling ? { cooldownUntil: new Date(account.cooldownUntilMs).toISOString() } : {},
|
|
12756
14128
|
...modelCooldowns.length === 0 ? {} : { modelCooldowns },
|
|
12757
14129
|
rateLimitHits: account.rateLimitHits,
|
|
14130
|
+
...activeHoldOf(account.id, now),
|
|
12758
14131
|
active: runtimeNextUpId.get(region) === account.id,
|
|
12759
14132
|
preferred: preferredAccountIds[region] === account.id,
|
|
12760
14133
|
disabled: core.pool.isDisabled(account.id),
|
|
@@ -12846,7 +14219,6 @@ async function statusDocument(options) {
|
|
|
12846
14219
|
modelCap: MAX_MODELS,
|
|
12847
14220
|
distribution: distributionSetting(),
|
|
12848
14221
|
showComposerPoints: settings.showComposerPoints === true,
|
|
12849
|
-
replacedClientName: replacedClientName() ?? null,
|
|
12850
14222
|
autoCheckin: {
|
|
12851
14223
|
enabled: autoCheckinEnabled(),
|
|
12852
14224
|
hours: checkinHours(),
|
|
@@ -12936,9 +14308,10 @@ function samePublishedAddress(a, b) {
|
|
|
12936
14308
|
/**
|
|
12937
14309
|
* True when the address that was published is not the address this run bound.
|
|
12938
14310
|
*
|
|
12939
|
-
*
|
|
12940
|
-
*
|
|
12941
|
-
*
|
|
14311
|
+
* The declaration is republished after a port fallback, so the published address
|
|
14312
|
+
* is the test: a row that follows the actual port is healthy, while one that
|
|
14313
|
+
* still names the preferred port is drift. Checking `portFallback` itself marked
|
|
14314
|
+
* a correctly republished row as broken.
|
|
12942
14315
|
*
|
|
12943
14316
|
* A gateway whose start failed counts as drift too, and for the same reason: it
|
|
12944
14317
|
* has no shim at all, so the address the host registered is refused. Skipping
|
|
@@ -12950,7 +14323,6 @@ function samePublishedAddress(a, b) {
|
|
|
12950
14323
|
*/
|
|
12951
14324
|
function addressDrift() {
|
|
12952
14325
|
for (const region of activeRegions()) {
|
|
12953
|
-
if (runtimes.get(region)?.portFallback === true) return true;
|
|
12954
14326
|
const published = declaredBaseUrl(region);
|
|
12955
14327
|
const bound = shimInfo(region).baseUrl;
|
|
12956
14328
|
if (bound === void 0) {
|
|
@@ -12960,15 +14332,21 @@ function addressDrift() {
|
|
|
12960
14332
|
if (published !== void 0 && published !== bound) return true;
|
|
12961
14333
|
}
|
|
12962
14334
|
for (const entry of Array.from(traeGateways.values())) {
|
|
12963
|
-
if (entry.portFallback === true) return true;
|
|
12964
14335
|
const published = declaredSourceBaseUrl(entry, TRAE_PROVIDER_ID_BY_REGION);
|
|
12965
14336
|
if (published !== void 0 && !samePublishedAddress(published, entry.baseUrl)) return true;
|
|
12966
14337
|
}
|
|
12967
14338
|
for (const entry of Array.from(qoderGateways.values())) {
|
|
12968
|
-
if (entry.portFallback === true) return true;
|
|
12969
14339
|
const published = declaredSourceBaseUrl(entry, QODER_PROVIDER_ID_BY_REGION);
|
|
12970
14340
|
if (published !== void 0 && !samePublishedAddress(published, entry.baseUrl)) return true;
|
|
12971
14341
|
}
|
|
14342
|
+
for (const region of activeTraeRegions()) {
|
|
14343
|
+
if (!sourceStartFailures.has("trae:" + region)) continue;
|
|
14344
|
+
if (declaredSourceBaseUrl({ region }, TRAE_PROVIDER_ID_BY_REGION) !== void 0) return true;
|
|
14345
|
+
}
|
|
14346
|
+
for (const region of activeQoderRegions()) {
|
|
14347
|
+
if (!sourceStartFailures.has("qoder:" + region)) continue;
|
|
14348
|
+
if (declaredSourceBaseUrl({ region }, QODER_PROVIDER_ID_BY_REGION) !== void 0) return true;
|
|
14349
|
+
}
|
|
12972
14350
|
return false;
|
|
12973
14351
|
}
|
|
12974
14352
|
/**
|
|
@@ -12996,10 +14374,19 @@ async function traeDoctorDetail() {
|
|
|
12996
14374
|
}
|
|
12997
14375
|
const parts = Object.entries(byRegion).map(([region, count]) => `${region} x${count}`).join(", ");
|
|
12998
14376
|
const failures = found.failures.length > 0 ? `; ${found.failures.length} unreadable file(s)` : "";
|
|
12999
|
-
const
|
|
14377
|
+
const servedRegions = activeTraeRegions();
|
|
14378
|
+
const served = servedRegions.map((region) => `${region}: ${traeDeclarationModels(region).length} model(s)`).join(", ");
|
|
14379
|
+
const stillReading = [];
|
|
14380
|
+
const problems = [];
|
|
14381
|
+
for (const region of servedRegions) {
|
|
14382
|
+
const entry = traeGateways.get(region);
|
|
14383
|
+
if (entry === void 0 && !byRegion[region]) problems.push(`${region}: no sign-in for this region`);
|
|
14384
|
+
else if (entry === void 0 || entry.warmSettled !== true) stillReading.push(region);
|
|
14385
|
+
else if (traeDeclarationModels(region).length === 0) problems.push(entry.lastError !== "" ? `${region}: ${entry.lastError}` : `${region}: no models selected`);
|
|
14386
|
+
}
|
|
13000
14387
|
return {
|
|
13001
|
-
ok:
|
|
13002
|
-
detail: `on; ${found.credentials.length} account(s) (${parts})${failures}; serving ${served}`
|
|
14388
|
+
ok: problems.length === 0,
|
|
14389
|
+
detail: `on; ${found.credentials.length} account(s) (${parts})${failures}; serving ${served}` + (stillReading.length > 0 ? `; still reading models for ${stillReading.join(", ")}` : "") + (problems.length > 0 ? `; ${problems.join("; ")}` : "")
|
|
13003
14390
|
};
|
|
13004
14391
|
} catch (error) {
|
|
13005
14392
|
return {
|
|
@@ -13024,10 +14411,19 @@ async function qoderDoctorDetail() {
|
|
|
13024
14411
|
for (const credential of found.credentials) byRegion[credential.region] = (byRegion[credential.region] || 0) + 1;
|
|
13025
14412
|
const parts = Object.entries(byRegion).map(([region, count]) => `${region} x${count}`).join(", ");
|
|
13026
14413
|
const failures = found.failures.length > 0 ? `; ${found.failures.length} unreadable app dir(s)` : "";
|
|
13027
|
-
const
|
|
14414
|
+
const servedRegions = activeQoderRegions();
|
|
14415
|
+
const served = servedRegions.map((region) => `${region}: ${qoderDeclarationModels(region).length} model(s)`).join(", ");
|
|
14416
|
+
const stillReading = [];
|
|
14417
|
+
const problems = [];
|
|
14418
|
+
for (const region of servedRegions) {
|
|
14419
|
+
const entry = qoderGateways.get(region);
|
|
14420
|
+
if (entry === void 0 && !byRegion[region]) problems.push(`${region}: no sign-in for this region`);
|
|
14421
|
+
else if (entry === void 0 || entry.warmSettled !== true) stillReading.push(region);
|
|
14422
|
+
else if (qoderDeclarationModels(region).length === 0) problems.push(entry.lastError !== "" ? `${region}: ${entry.lastError}` : `${region}: no models selected`);
|
|
14423
|
+
}
|
|
13028
14424
|
return {
|
|
13029
|
-
ok:
|
|
13030
|
-
detail: `on; ${found.credentials.length} account(s) (${parts})${failures}; serving ${served}`
|
|
14425
|
+
ok: problems.length === 0,
|
|
14426
|
+
detail: `on; ${found.credentials.length} account(s) (${parts})${failures}; serving ${served}` + (stillReading.length > 0 ? `; still reading models for ${stillReading.join(", ")}` : "") + (problems.length > 0 ? `; ${problems.join("; ")}` : "")
|
|
13031
14427
|
};
|
|
13032
14428
|
} catch (error) {
|
|
13033
14429
|
return {
|
|
@@ -13080,38 +14476,71 @@ async function doctorReport() {
|
|
|
13080
14476
|
push(label + ": models", regionModels.length > 0, regionModels.length + " model(s) in the catalog");
|
|
13081
14477
|
push(label + ": declared", declared, declared ? "registered with the running host" : runtime?.lastError || runtime?.lastWarning || "this gateway is not published yet");
|
|
13082
14478
|
}
|
|
13083
|
-
for (const [gateways, idByRegion, nameByRegion, ports] of [
|
|
14479
|
+
for (const [gateways, idByRegion, nameByRegion, ports, modelsOf] of [
|
|
13084
14480
|
[
|
|
13085
14481
|
runtimes,
|
|
13086
14482
|
PROVIDER_ID_BY_REGION,
|
|
13087
14483
|
PROVIDER_NAME_BY_REGION,
|
|
13088
|
-
REGION_PORTS
|
|
14484
|
+
REGION_PORTS,
|
|
14485
|
+
declarationModels
|
|
13089
14486
|
],
|
|
13090
14487
|
[
|
|
13091
14488
|
traeGateways,
|
|
13092
14489
|
TRAE_PROVIDER_ID_BY_REGION,
|
|
13093
14490
|
TRAE_PROVIDER_NAME_BY_REGION,
|
|
13094
|
-
TRAE_PORTS
|
|
14491
|
+
TRAE_PORTS,
|
|
14492
|
+
traeDeclarationModels
|
|
13095
14493
|
],
|
|
13096
14494
|
[
|
|
13097
14495
|
qoderGateways,
|
|
13098
14496
|
QODER_PROVIDER_ID_BY_REGION,
|
|
13099
14497
|
QODER_PROVIDER_NAME_BY_REGION,
|
|
13100
|
-
QODER_PORTS
|
|
14498
|
+
QODER_PORTS,
|
|
14499
|
+
qoderDeclarationModels
|
|
13101
14500
|
]
|
|
13102
14501
|
]) for (const entry of Array.from(gateways.values())) {
|
|
13103
14502
|
const published = declaredSourceBaseUrl(entry, idByRegion);
|
|
13104
14503
|
const bound = entry.baseUrl;
|
|
13105
14504
|
const label = nameByRegion[entry.region];
|
|
13106
|
-
if (
|
|
14505
|
+
if (bound === void 0) continue;
|
|
14506
|
+
if (published === void 0) {
|
|
14507
|
+
if (entry.warmSettled !== void 0 ? entry.warmSettled !== true : warmingGatewayIds().has(idByRegion[entry.region])) {
|
|
14508
|
+
push(label + ": published address", true, "still reading its model roster");
|
|
14509
|
+
continue;
|
|
14510
|
+
}
|
|
14511
|
+
if (entry.lastError) {
|
|
14512
|
+
push(label + ": published address", false, entry.lastError);
|
|
14513
|
+
continue;
|
|
14514
|
+
}
|
|
14515
|
+
if (modelsOf(entry.region).length === 0) {
|
|
14516
|
+
push(label + ": published address", true, "no callable models selected; nothing to publish");
|
|
14517
|
+
continue;
|
|
14518
|
+
}
|
|
14519
|
+
push(label + ": published address", false, "this gateway is running but its provider row is not published in this run's declaration - restart the plugin");
|
|
14520
|
+
continue;
|
|
14521
|
+
}
|
|
13107
14522
|
const fallback = entry.portFallback === true;
|
|
13108
|
-
if (samePublishedAddress(published, bound)
|
|
13109
|
-
|
|
14523
|
+
if (samePublishedAddress(published, bound)) {
|
|
14524
|
+
const detail = fallback ? "matches the endpoint this gateway bound (" + bound + "); preferred port " + ports[entry.region] + " is held, so this session serves the fallback" : "matches the endpoint this gateway bound (" + bound + ")";
|
|
14525
|
+
push(label + ": published address", true, detail);
|
|
13110
14526
|
continue;
|
|
13111
14527
|
}
|
|
13112
|
-
const detail =
|
|
14528
|
+
const detail = "the published address is " + published + " but this gateway is on " + bound + " - reload the plugin so the host picks the new address up";
|
|
13113
14529
|
push(label + ": published address", false, detail);
|
|
13114
14530
|
}
|
|
14531
|
+
for (const [source, nameByRegion, regions] of [[
|
|
14532
|
+
"trae",
|
|
14533
|
+
TRAE_PROVIDER_NAME_BY_REGION,
|
|
14534
|
+
activeTraeRegions()
|
|
14535
|
+
], [
|
|
14536
|
+
"qoder",
|
|
14537
|
+
QODER_PROVIDER_NAME_BY_REGION,
|
|
14538
|
+
activeQoderRegions()
|
|
14539
|
+
]]) for (const region of regions) {
|
|
14540
|
+
const reason = sourceStartFailures.get(source + ":" + region);
|
|
14541
|
+
if (reason === void 0) continue;
|
|
14542
|
+
push(nameByRegion[region] + ": endpoint", false, reason);
|
|
14543
|
+
}
|
|
13115
14544
|
push("declaration-written", runtimeState.write.ok === true, runtimeState.write.ok !== true ? safeMessage(runtimeState.write.error) : runtimeState.write.changed ? "the model list was just updated; the host discovers it from this gateway's /v1/models" : String(runtimeState.write.skipped || "up to date"));
|
|
13116
14545
|
const ok = findings.every((finding) => finding.ok);
|
|
13117
14546
|
const report = [
|
|
@@ -13222,6 +14651,41 @@ function parseSelection(body) {
|
|
|
13222
14651
|
return out;
|
|
13223
14652
|
}
|
|
13224
14653
|
/**
|
|
14654
|
+
* The model ids one save's page could display, or undefined for an older panel.
|
|
14655
|
+
*
|
|
14656
|
+
* Only used to tell "the page did not show this id" from "the user removed it",
|
|
14657
|
+
* so junk entries are skipped rather than failing the save.
|
|
14658
|
+
*/
|
|
14659
|
+
function parseKnownModelIds(value) {
|
|
14660
|
+
if (!Array.isArray(value)) return void 0;
|
|
14661
|
+
const ids = /* @__PURE__ */ new Set();
|
|
14662
|
+
for (const entry of value) if (typeof entry === "string" && entry !== "" && entry.length <= MAX_MODEL_ID_LENGTH) ids.add(entry);
|
|
14663
|
+
return ids;
|
|
14664
|
+
}
|
|
14665
|
+
/**
|
|
14666
|
+
* Keep the stored selection of models the saving page could not display.
|
|
14667
|
+
*
|
|
14668
|
+
* A roster read can be partial: the static fallback stands until the live list
|
|
14669
|
+
* arrives, and a source directory can answer with fewer rows than it will carry
|
|
14670
|
+
* later. Saving such a page used to replace the whole selection, so every model
|
|
14671
|
+
* outside the snapshot lost its tick and its per-model edits. Ids the page did
|
|
14672
|
+
* show are authoritative - an absent override there means the user cleared it -
|
|
14673
|
+
* while ids it never showed keep what was stored.
|
|
14674
|
+
*/
|
|
14675
|
+
function mergeSelectionForPartialRoster(selection, prior, known) {
|
|
14676
|
+
if (known === void 0) return selection;
|
|
14677
|
+
const carriedEnabled = (Array.isArray(prior.enabledModelIds) ? prior.enabledModelIds : []).filter((id) => !known.has(id));
|
|
14678
|
+
const priorOverrides = prior.overrides !== null && typeof prior.overrides === "object" ? prior.overrides : {};
|
|
14679
|
+
const overrides = Object.create(null);
|
|
14680
|
+
for (const [id, entry] of Object.entries(priorOverrides)) if (!known.has(id)) overrides[id] = { ...entry };
|
|
14681
|
+
const merged = { ...selection };
|
|
14682
|
+
if (Array.isArray(selection.enabledModelIds)) merged.enabledModelIds = [.../* @__PURE__ */ new Set([...selection.enabledModelIds, ...carriedEnabled])].slice(0, MAX_STORED_MODEL_IDS);
|
|
14683
|
+
if (selection.overrides !== void 0) for (const [id, entry] of Object.entries(selection.overrides)) overrides[id] = entry;
|
|
14684
|
+
if (Object.keys(overrides).length > 0) merged.overrides = overrides;
|
|
14685
|
+
else delete merged.overrides;
|
|
14686
|
+
return merged;
|
|
14687
|
+
}
|
|
14688
|
+
/**
|
|
13225
14689
|
* Validate one model's override.
|
|
13226
14690
|
*
|
|
13227
14691
|
* Every field is optional, and a value that is present must be usable: the host
|
|
@@ -13277,10 +14741,20 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13277
14741
|
* (the Refresh button) still resolves on a fully refreshed document.
|
|
13278
14742
|
*/
|
|
13279
14743
|
case "xd.state": {
|
|
13280
|
-
if (payload?.wait === true)
|
|
13281
|
-
|
|
13282
|
-
|
|
13283
|
-
|
|
14744
|
+
if (payload?.wait === true) {
|
|
14745
|
+
const refreshed = await refreshAll("panel", true);
|
|
14746
|
+
if (refreshed.stale === true) return {
|
|
14747
|
+
...await statusDocument({ allowNetwork: false }),
|
|
14748
|
+
refreshing: false,
|
|
14749
|
+
stale: true
|
|
14750
|
+
};
|
|
14751
|
+
const stillNeeded = refreshNeeded();
|
|
14752
|
+
if (stillNeeded) scheduleRefresh("panel");
|
|
14753
|
+
return {
|
|
14754
|
+
...refreshed,
|
|
14755
|
+
refreshing: stillNeeded
|
|
14756
|
+
};
|
|
14757
|
+
}
|
|
13284
14758
|
const document = await statusDocument({ allowNetwork: false });
|
|
13285
14759
|
const refreshing = refreshNeeded();
|
|
13286
14760
|
if (refreshing) scheduleRefresh("panel");
|
|
@@ -13304,7 +14778,7 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13304
14778
|
case "xd.fetchModels":
|
|
13305
14779
|
await settleWithin(Promise.all(Array.from(runtimes.values()).map(async (runtime) => {
|
|
13306
14780
|
try {
|
|
13307
|
-
await
|
|
14781
|
+
await refreshRuntime(runtime, "fetch-models", true);
|
|
13308
14782
|
} catch (error) {
|
|
13309
14783
|
runtime.lastError = safeMessage(error);
|
|
13310
14784
|
}
|
|
@@ -13326,12 +14800,21 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13326
14800
|
if (activeRegions().length > 0) await settle(core.pool.scan());
|
|
13327
14801
|
await settle(startRuntimes());
|
|
13328
14802
|
await settle(refreshSourceAccounts());
|
|
14803
|
+
await settle(reconcileTraeGateways());
|
|
14804
|
+
await settle(reconcileQoderGateways());
|
|
14805
|
+
await settle(Promise.all([...Array.from(traeGateways.values()), ...Array.from(qoderGateways.values())].filter((entry) => entry.lastError !== "" || entry.gateway.models().length === 0).map((entry) => warmSourceDirectory(entry))));
|
|
13329
14806
|
await settle(Promise.all(Array.from(runtimes.values()).map((runtime) => refreshRuntime(runtime, "rescan", true))));
|
|
13330
14807
|
syncDeclaration();
|
|
13331
|
-
|
|
14808
|
+
const document = await statusDocument({
|
|
13332
14809
|
allowNetwork: true,
|
|
13333
14810
|
budgetMs: left()
|
|
13334
14811
|
});
|
|
14812
|
+
const refreshing = refreshNeeded();
|
|
14813
|
+
if (refreshing) scheduleRefresh("panel");
|
|
14814
|
+
return {
|
|
14815
|
+
...document,
|
|
14816
|
+
refreshing
|
|
14817
|
+
};
|
|
13335
14818
|
}
|
|
13336
14819
|
case "xd.resetCooldowns":
|
|
13337
14820
|
core.pool.resetCooldowns();
|
|
@@ -13347,7 +14830,7 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13347
14830
|
const token = ++checkinPassToken;
|
|
13348
14831
|
checkinPassRunning = true;
|
|
13349
14832
|
const work = async () => {
|
|
13350
|
-
const before = await core.
|
|
14833
|
+
const before = await core.pool.readAfterRefresh(account.id, (credential) => core.client.fetchCheckinStatus(credential));
|
|
13351
14834
|
if (before.todayCheckedIn) {
|
|
13352
14835
|
probeCache.delete(accountId);
|
|
13353
14836
|
return {
|
|
@@ -13373,9 +14856,9 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13373
14856
|
result: claim
|
|
13374
14857
|
};
|
|
13375
14858
|
};
|
|
13376
|
-
const answered = await settleWithin(work()
|
|
14859
|
+
const answered = await settleWithin(work().finally(() => {
|
|
13377
14860
|
if (token === checkinPassToken) checkinPassRunning = false;
|
|
13378
|
-
});
|
|
14861
|
+
}), PANEL_STEP_BUDGET_MS);
|
|
13379
14862
|
if (answered === void 0) throw panelError("the check-in did not finish in time; try again", "TIMEOUT");
|
|
13380
14863
|
return answered;
|
|
13381
14864
|
}
|
|
@@ -13392,9 +14875,10 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13392
14875
|
if (!/^(cn|global|trae:cn|trae:ai|qoder:cn|qoder:global)$/.test(requested)) throw panelError("unknown gateway", "INVALID");
|
|
13393
14876
|
const selection = parseSelection(payload?.selection);
|
|
13394
14877
|
if (selection === void 0) throw panelError("invalid selection payload", "INVALID");
|
|
14878
|
+
const known = parseKnownModelIds(payload?.knownModelIds);
|
|
13395
14879
|
if (await mutateSettings("modelSelection", (current) => {
|
|
13396
14880
|
const all = { ...current && typeof current === "object" && !Array.isArray(current) ? current : {} };
|
|
13397
|
-
all[requested] = selection;
|
|
14881
|
+
all[requested] = mergeSelectionForPartialRoster(selection, normalizeSelection(all[requested]), known);
|
|
13398
14882
|
return all;
|
|
13399
14883
|
}) !== true) throw panelError("the selection could not be written to the plugin settings file", "SAVE_FAILED");
|
|
13400
14884
|
if (requested === "cn" || requested === "global") applyEffectiveSelection(requested);
|
|
@@ -13577,9 +15061,9 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13577
15061
|
}),
|
|
13578
15062
|
result: outcome
|
|
13579
15063
|
};
|
|
13580
|
-
})()
|
|
15064
|
+
})().finally(() => {
|
|
13581
15065
|
if (token === checkinPassToken) checkinPassRunning = false;
|
|
13582
|
-
});
|
|
15066
|
+
}), PANEL_STEP_BUDGET_MS);
|
|
13583
15067
|
if (answered === void 0) throw panelError("the check-in did not finish in time; try again", "TIMEOUT");
|
|
13584
15068
|
return answered;
|
|
13585
15069
|
}
|
|
@@ -13678,15 +15162,6 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13678
15162
|
if (await saveSettings({ showComposerPoints: payload?.enabled === true }) !== true) throw panelError("the composer credits setting could not be written to the plugin settings file", "SAVE_FAILED");
|
|
13679
15163
|
return await statusDocument({ allowNetwork: false });
|
|
13680
15164
|
/**
|
|
13681
|
-
* Rewrite the client name the domestic host's channel check refuses.
|
|
13682
|
-
*
|
|
13683
|
-
* Off by default: it changes what the prompt says about the client, which is
|
|
13684
|
-
* a decision for the user rather than something the plugin does behind them.
|
|
13685
|
-
*/
|
|
13686
|
-
case "xd.setRewriteClientName":
|
|
13687
|
-
if (await saveSettings({ rewriteClientName: payload?.enabled === true }) !== true) throw panelError("the client-name setting could not be written to the plugin settings file", "SAVE_FAILED");
|
|
13688
|
-
return await statusDocument({ allowNetwork: false });
|
|
13689
|
-
/**
|
|
13690
15165
|
* Park or unpark one edition of one source (国内版 / 国际版).
|
|
13691
15166
|
*
|
|
13692
15167
|
* Same shape as the source switch above: a parked region keeps no gateway,
|
|
@@ -13704,7 +15179,7 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13704
15179
|
if (payload?.enabled !== true) parked.push(key);
|
|
13705
15180
|
return parked;
|
|
13706
15181
|
}) !== true) throw panelError("the region switch could not be written to the plugin settings file", "SAVE_FAILED");
|
|
13707
|
-
await
|
|
15182
|
+
await applySettingsEverywhere(false);
|
|
13708
15183
|
if (payload?.enabled === true) await refreshAll("region-on", true);
|
|
13709
15184
|
else syncDeclaration();
|
|
13710
15185
|
if (source === "workbuddy") {
|
|
@@ -13726,7 +15201,7 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13726
15201
|
if (key === void 0) throw panelError("unknown source", "UNSUPPORTED");
|
|
13727
15202
|
if (await saveSettings({ [key]: payload?.enabled === true }) !== true) throw panelError("the source switch could not be written to the plugin settings file", "SAVE_FAILED");
|
|
13728
15203
|
resetDiscoveryCaches();
|
|
13729
|
-
await
|
|
15204
|
+
await applySettingsEverywhere(false);
|
|
13730
15205
|
if (payload?.enabled === true) await refreshAll("source-on", true);
|
|
13731
15206
|
else syncDeclaration();
|
|
13732
15207
|
if (payload?.source === "workbuddy") {
|
|
@@ -13837,7 +15312,15 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13837
15312
|
const generation = loadGeneration;
|
|
13838
15313
|
const counts = await settleWithin((async () => {
|
|
13839
15314
|
try {
|
|
13840
|
-
|
|
15315
|
+
const counts = await claimAllGrowth();
|
|
15316
|
+
if (stillLoaded(generation)) {
|
|
15317
|
+
growthLast = {
|
|
15318
|
+
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
15319
|
+
...counts
|
|
15320
|
+
};
|
|
15321
|
+
scheduleRefresh("growth");
|
|
15322
|
+
}
|
|
15323
|
+
return counts;
|
|
13841
15324
|
} finally {
|
|
13842
15325
|
if (token === growthPassToken) growthPassRunning = false;
|
|
13843
15326
|
}
|
|
@@ -13846,13 +15329,6 @@ async function onPanelInvoke(channel, payload) {
|
|
|
13846
15329
|
state: await statusDocument({ allowNetwork: false }),
|
|
13847
15330
|
running: true
|
|
13848
15331
|
};
|
|
13849
|
-
if (stillLoaded(generation)) {
|
|
13850
|
-
growthLast = {
|
|
13851
|
-
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
13852
|
-
...counts
|
|
13853
|
-
};
|
|
13854
|
-
scheduleRefresh("growth");
|
|
13855
|
-
}
|
|
13856
15332
|
return {
|
|
13857
15333
|
state: await statusDocument({ allowNetwork: false }),
|
|
13858
15334
|
growth: counts
|
|
@@ -13957,6 +15433,7 @@ async function runCheckinPass(options) {
|
|
|
13957
15433
|
slot,
|
|
13958
15434
|
claimed: 0,
|
|
13959
15435
|
pending: true,
|
|
15436
|
+
workbuddy: want("workbuddy"),
|
|
13960
15437
|
at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
13961
15438
|
...manual ? { manual: true } : {}
|
|
13962
15439
|
};
|
|
@@ -14000,25 +15477,30 @@ async function runCheckinPass(options) {
|
|
|
14000
15477
|
adopted: 0
|
|
14001
15478
|
}, "travel") : void 0;
|
|
14002
15479
|
if (!stillLoaded(generation)) return started;
|
|
14003
|
-
|
|
15480
|
+
if (want("qoder")) {
|
|
15481
|
+
const now = Date.now();
|
|
15482
|
+
const opening = typeof qoder?.opensAt === "number" && qoder.opensAt > now ? qoder.opensAt : 0;
|
|
15483
|
+
const backoff = Number(qoder?.failed) > 0 && (typeof qoder?.reason === "string" || qoder?.retry === true) ? now + QODER_RETRY_BACKOFF_MS : 0;
|
|
15484
|
+
qoderRetryAt = opening === 0 ? backoff : backoff === 0 ? opening : Math.min(opening, backoff);
|
|
15485
|
+
}
|
|
14004
15486
|
Object.assign(started, {
|
|
14005
15487
|
claimed: pool.claimed,
|
|
14006
15488
|
skipped: pool.skipped,
|
|
14007
15489
|
failed: pool.failed,
|
|
14008
15490
|
...pool.reason === void 0 ? {} : { reason: pool.reason },
|
|
14009
15491
|
travel,
|
|
14010
|
-
qoder,
|
|
14011
|
-
trae
|
|
15492
|
+
...want("qoder") ? { qoder } : {},
|
|
15493
|
+
...want("trae") ? { trae } : {}
|
|
14012
15494
|
});
|
|
14013
15495
|
await checkinDelay(GROWTH_AFTER_CHECKIN_MS);
|
|
14014
15496
|
if (!stillLoaded(generation)) return started;
|
|
14015
15497
|
started.growth = want("workbuddy") ? await claimAllGrowth() : void 0;
|
|
14016
15498
|
log((manual ? "manual" : "automatic") + " check-in collected " + pool.claimed + " account(s) for " + slot);
|
|
14017
|
-
if (spendsSlot && stillLoaded(generation)) saveCheckinSlots();
|
|
15499
|
+
if ((spendsSlot || want("qoder")) && stillLoaded(generation)) saveCheckinSlots();
|
|
14018
15500
|
} catch (error) {
|
|
14019
15501
|
started.error = safeMessage(error);
|
|
14020
15502
|
log("warn", (manual ? "manual" : "automatic") + " check-in failed for " + slot + ": " + started.error);
|
|
14021
|
-
if (spendsSlot && stillLoaded(generation)) saveCheckinSlots();
|
|
15503
|
+
if ((spendsSlot || want("qoder")) && stillLoaded(generation)) saveCheckinSlots();
|
|
14022
15504
|
} finally {
|
|
14023
15505
|
if (token === checkinPassToken) checkinPassRunning = false;
|
|
14024
15506
|
delete started.pending;
|
|
@@ -14174,6 +15656,7 @@ async function claimAllGrowth() {
|
|
|
14174
15656
|
for (const account of core.pool.list()) {
|
|
14175
15657
|
if (currentCheckinSignal().aborted) break;
|
|
14176
15658
|
if (core.pool.isDisabled(account.id) || !regionEnabled("workbuddy", regionOf(account.credential.domain))) continue;
|
|
15659
|
+
if (signInRefused(account)) continue;
|
|
14177
15660
|
if (seen > 0) await checkinDelay(GROWTH_ACCOUNT_DELAY_MS);
|
|
14178
15661
|
seen += 1;
|
|
14179
15662
|
counts.detail.accounts += 1;
|
|
@@ -14230,7 +15713,7 @@ function mergeGrowthDetail(total, part) {
|
|
|
14230
15713
|
async function readGrowthState() {
|
|
14231
15714
|
const rows = [];
|
|
14232
15715
|
let failed = 0;
|
|
14233
|
-
const wanted = core.pool.list().filter((account) => !core.pool.isDisabled(account.id) && regionEnabled("workbuddy", regionOf(account.credential.domain)));
|
|
15716
|
+
const wanted = core.pool.list().filter((account) => !core.pool.isDisabled(account.id) && regionEnabled("workbuddy", regionOf(account.credential.domain)) && !signInRefused(account));
|
|
14234
15717
|
const reads = await Promise.all(wanted.map(async (account) => {
|
|
14235
15718
|
try {
|
|
14236
15719
|
const [streak, chances] = await Promise.all([core.client.growthStreakFull(account.credential), core.client.lotteryChances(account.credential)]);
|
|
@@ -14300,12 +15783,16 @@ async function claimAllCheckins() {
|
|
|
14300
15783
|
let seen = 0;
|
|
14301
15784
|
for (const account of core.pool.list()) {
|
|
14302
15785
|
if (core.pool.isDisabled(account.id) || !regionEnabled("workbuddy", regionOf(account.credential.domain))) continue;
|
|
15786
|
+
if (signInRefused(account)) continue;
|
|
14303
15787
|
if (currentCheckinSignal().aborted) break;
|
|
14304
15788
|
if (seen > 0) await checkinDelay(CHECKIN_ACCOUNT_DELAY_MS);
|
|
14305
15789
|
seen += 1;
|
|
14306
15790
|
try {
|
|
14307
|
-
const before = await core.
|
|
14308
|
-
if (!before.active)
|
|
15791
|
+
const before = await core.pool.readAfterRefresh(account.id, (credential) => core.client.fetchCheckinStatus(credential));
|
|
15792
|
+
if (!before.active) {
|
|
15793
|
+
counts.skipped += 1;
|
|
15794
|
+
continue;
|
|
15795
|
+
}
|
|
14309
15796
|
if (before.todayCheckedIn) {
|
|
14310
15797
|
counts.skipped += 1;
|
|
14311
15798
|
continue;
|
|
@@ -14443,6 +15930,7 @@ async function travelAllAccounts() {
|
|
|
14443
15930
|
if (core.pool.isDisabled(account.id) || !regionEnabled("workbuddy", regionOf(account.credential.domain))) continue;
|
|
14444
15931
|
if (regionOf(account.credential.domain) !== "cn") continue;
|
|
14445
15932
|
if (!account.credential.refreshToken) continue;
|
|
15933
|
+
if (signInRefused(account)) continue;
|
|
14446
15934
|
if (currentCheckinSignal().aborted) break;
|
|
14447
15935
|
if (seen > 0) await checkinDelay(TRAVEL_ACCOUNT_DELAY_MS);
|
|
14448
15936
|
seen += 1;
|
|
@@ -14469,7 +15957,8 @@ async function qoderCheckinAll() {
|
|
|
14469
15957
|
const counts = {
|
|
14470
15958
|
claimed: 0,
|
|
14471
15959
|
skipped: 0,
|
|
14472
|
-
failed: 0
|
|
15960
|
+
failed: 0,
|
|
15961
|
+
retry: false
|
|
14473
15962
|
};
|
|
14474
15963
|
const regions = activeQoderRegions();
|
|
14475
15964
|
if (regions.length === 0) return counts;
|
|
@@ -14485,6 +15974,10 @@ async function qoderCheckinAll() {
|
|
|
14485
15974
|
reason
|
|
14486
15975
|
};
|
|
14487
15976
|
}
|
|
15977
|
+
if (found.failures.length > 0) {
|
|
15978
|
+
counts.failed += 1;
|
|
15979
|
+
counts.retry = true;
|
|
15980
|
+
}
|
|
14488
15981
|
let seen = 0;
|
|
14489
15982
|
for (const credential of found.credentials) {
|
|
14490
15983
|
if (!regions.includes(credential.region)) continue;
|
|
@@ -14493,6 +15986,7 @@ async function qoderCheckinAll() {
|
|
|
14493
15986
|
seen += 1;
|
|
14494
15987
|
const result = await qoderClaimFor(credential);
|
|
14495
15988
|
counts[result.outcome] += 1;
|
|
15989
|
+
if (result.retry === true) counts.retry = true;
|
|
14496
15990
|
if (result.opensAt !== void 0 && (counts.opensAt === void 0 || result.opensAt < counts.opensAt)) counts.opensAt = result.opensAt;
|
|
14497
15991
|
}
|
|
14498
15992
|
return counts;
|
|
@@ -14523,7 +16017,8 @@ async function qoderClaimFor(credential) {
|
|
|
14523
16017
|
log("warn", "[qoder:" + credential.region + "] check-in refused for " + qoderRowLabel(credential) + ": " + result.reason);
|
|
14524
16018
|
return {
|
|
14525
16019
|
outcome: "failed",
|
|
14526
|
-
reason: result.reason
|
|
16020
|
+
reason: result.reason,
|
|
16021
|
+
retry: false
|
|
14527
16022
|
};
|
|
14528
16023
|
}
|
|
14529
16024
|
return { outcome: result.claimed && !result.replayed ? "claimed" : "skipped" };
|
|
@@ -14532,10 +16027,17 @@ async function qoderClaimFor(credential) {
|
|
|
14532
16027
|
log("warn", "[qoder:" + credential.region + "] check-in failed for " + qoderRowLabel(credential) + ": " + reason);
|
|
14533
16028
|
return {
|
|
14534
16029
|
outcome: "failed",
|
|
14535
|
-
reason
|
|
16030
|
+
reason,
|
|
16031
|
+
retry: qoderFailureRetryable(error)
|
|
14536
16032
|
};
|
|
14537
16033
|
}
|
|
14538
16034
|
}
|
|
16035
|
+
/** A transport/5xx failure is worth another tick; a 4xx verdict is not. */
|
|
16036
|
+
function qoderFailureRetryable(error) {
|
|
16037
|
+
const status = Number(/\bHTTP\s+(\d{3})\b/iu.exec(safeMessage(error))?.[1] ?? 0);
|
|
16038
|
+
if (status === 0) return true;
|
|
16039
|
+
return status === 408 || status === 429 || status >= 500;
|
|
16040
|
+
}
|
|
14539
16041
|
/**
|
|
14540
16042
|
* Claim today's Trae credits for every signed-in CN Trae account. Each account
|
|
14541
16043
|
* is read first: one already checked in costs a single status call and no
|
|
@@ -14710,20 +16212,27 @@ async function autoCheckinTick(now) {
|
|
|
14710
16212
|
checkinAbort.abort();
|
|
14711
16213
|
checkinAbort = new AbortController();
|
|
14712
16214
|
checkinScope.enterWith(checkinAbort.signal);
|
|
16215
|
+
const retrySlot = autoCheckinLast?.slot ?? checkinSlotId(zonedDayHour(/* @__PURE__ */ new Date()).day, zonedDayHour(/* @__PURE__ */ new Date()).hour);
|
|
14713
16216
|
try {
|
|
14714
16217
|
qoderRetryAt = 0;
|
|
14715
16218
|
const retry = await qoderCheckinAll();
|
|
14716
16219
|
if (!stillLoaded(retryGeneration)) return autoCheckinLast;
|
|
14717
|
-
|
|
16220
|
+
const now = Date.now();
|
|
16221
|
+
const opening = typeof retry.opensAt === "number" && retry.opensAt > now ? retry.opensAt : 0;
|
|
16222
|
+
const backoff = Number(retry.failed) > 0 && (typeof retry.reason === "string" || retry.retry === true) ? now + QODER_RETRY_BACKOFF_MS : 0;
|
|
16223
|
+
qoderRetryAt = opening === 0 ? backoff : backoff === 0 ? opening : Math.min(opening, backoff);
|
|
14718
16224
|
const claimedBefore = Number(autoCheckinLast?.qoder?.claimed) || 0;
|
|
14719
16225
|
autoCheckinLast = {
|
|
14720
16226
|
...autoCheckinLast,
|
|
16227
|
+
slot: retrySlot,
|
|
16228
|
+
...autoCheckinLast === null ? { workbuddy: false } : {},
|
|
14721
16229
|
qoder: {
|
|
14722
16230
|
...retry,
|
|
14723
16231
|
claimed: claimedBefore + (Number(retry.claimed) || 0)
|
|
14724
16232
|
},
|
|
14725
16233
|
at: (/* @__PURE__ */ new Date()).toISOString()
|
|
14726
16234
|
};
|
|
16235
|
+
saveCheckinSlots();
|
|
14727
16236
|
log("info", "deferred Qoder check-in: " + JSON.stringify(retry));
|
|
14728
16237
|
} catch (error) {
|
|
14729
16238
|
if (stillLoaded(retryGeneration)) {
|
|
@@ -14731,6 +16240,7 @@ async function autoCheckinTick(now) {
|
|
|
14731
16240
|
const claimedBefore = Number(autoCheckinLast?.qoder?.claimed) || 0;
|
|
14732
16241
|
autoCheckinLast = {
|
|
14733
16242
|
...autoCheckinLast,
|
|
16243
|
+
slot: retrySlot,
|
|
14734
16244
|
qoder: {
|
|
14735
16245
|
claimed: claimedBefore,
|
|
14736
16246
|
skipped: 0,
|
|
@@ -14739,6 +16249,7 @@ async function autoCheckinTick(now) {
|
|
|
14739
16249
|
},
|
|
14740
16250
|
at: (/* @__PURE__ */ new Date()).toISOString()
|
|
14741
16251
|
};
|
|
16252
|
+
saveCheckinSlots();
|
|
14742
16253
|
log("warn", "deferred Qoder check-in failed: " + safeMessage(error));
|
|
14743
16254
|
}
|
|
14744
16255
|
} finally {
|
|
@@ -14799,8 +16310,10 @@ async function keepAliveTokens() {
|
|
|
14799
16310
|
if (serviceStopped) break;
|
|
14800
16311
|
if (core.pool.isDisabled(account.id)) continue;
|
|
14801
16312
|
if (!regionEnabled("workbuddy", regionOf(account.credential.domain))) continue;
|
|
16313
|
+
if (signInRefused(account)) continue;
|
|
14802
16314
|
const outcome = await core.pool.ensureFresh(account);
|
|
14803
|
-
|
|
16315
|
+
const bucket = outcome === "refused" ? "failed" : outcome;
|
|
16316
|
+
counts[bucket] = (counts[bucket] ?? 0) + 1;
|
|
14804
16317
|
}
|
|
14805
16318
|
return counts;
|
|
14806
16319
|
}
|
|
@@ -14884,10 +16397,13 @@ async function publishDeclarationEarly() {
|
|
|
14884
16397
|
else if (written.changed === true) log("info", "declaration updated at load time; " + written.providerCount + " provider(s)");
|
|
14885
16398
|
}
|
|
14886
16399
|
async function onServiceStart() {
|
|
16400
|
+
const generation = loadGeneration;
|
|
14887
16401
|
serviceStopped = false;
|
|
14888
16402
|
if (warmAbort.signal.aborted) warmAbort = new AbortController();
|
|
14889
16403
|
await adoptHostSettings();
|
|
16404
|
+
if (!stillLoaded(generation)) return;
|
|
14890
16405
|
if (settings.enabled === false) {
|
|
16406
|
+
syncDeclaration();
|
|
14891
16407
|
log("info", "plugin disabled in settings; the provider host stays down");
|
|
14892
16408
|
return;
|
|
14893
16409
|
}
|
|
@@ -14896,9 +16412,13 @@ async function onServiceStart() {
|
|
|
14896
16412
|
startAccountWatch();
|
|
14897
16413
|
const { left: bootstrapLeft } = budget(SERVICE_BOOTSTRAP_BUDGET_MS);
|
|
14898
16414
|
await settleWithin(reconcileTraeGateways(), bootstrapLeft());
|
|
16415
|
+
if (!stillLoaded(generation)) return;
|
|
14899
16416
|
await settleWithin(reconcileQoderGateways(), bootstrapLeft());
|
|
16417
|
+
if (!stillLoaded(generation)) return;
|
|
14900
16418
|
if (activeRegions().length > 0) await settleWithin(core.pool.scan(), bootstrapLeft());
|
|
16419
|
+
if (!stillLoaded(generation)) return;
|
|
14901
16420
|
await settleWithin(startRuntimes(), bootstrapLeft());
|
|
16421
|
+
if (!stillLoaded(generation)) return;
|
|
14902
16422
|
await settleWithin(Promise.all(Array.from(runtimes.values()).map(async (runtime) => {
|
|
14903
16423
|
try {
|
|
14904
16424
|
await connect(runtime, !catalogFresh(runtime));
|
|
@@ -14907,6 +16427,7 @@ async function onServiceStart() {
|
|
|
14907
16427
|
log("warn", runtime.region + " failed to start: " + runtime.lastError);
|
|
14908
16428
|
}
|
|
14909
16429
|
})), SERVICE_ROSTER_BUDGET_MS);
|
|
16430
|
+
if (!stillLoaded(generation)) return;
|
|
14910
16431
|
syncDeclaration();
|
|
14911
16432
|
scheduleRefresh("service-start");
|
|
14912
16433
|
}
|
|
@@ -14931,6 +16452,9 @@ async function onServiceStop() {
|
|
|
14931
16452
|
}));
|
|
14932
16453
|
traeGateways.clear();
|
|
14933
16454
|
qoderGateways.clear();
|
|
16455
|
+
gatewayStartFailures.clear();
|
|
16456
|
+
sourceStartFailures.clear();
|
|
16457
|
+
sourceGatewayStarts.clear();
|
|
14934
16458
|
await Promise.all(closing);
|
|
14935
16459
|
}
|
|
14936
16460
|
async function onLoad() {
|
|
@@ -14951,11 +16475,7 @@ async function onLoad() {
|
|
|
14951
16475
|
for (const runtime of runtimes.values()) pendingRuntimeStops.push(stopRuntime(runtime));
|
|
14952
16476
|
runtimes.clear();
|
|
14953
16477
|
runtimeStarts.clear();
|
|
14954
|
-
|
|
14955
|
-
log("warn", "[" + entry.source + ":" + entry.region + "] gateway did not close cleanly: " + safeMessage(error));
|
|
14956
|
-
}));
|
|
14957
|
-
traeGateways.clear();
|
|
14958
|
-
qoderGateways.clear();
|
|
16478
|
+
sourceGatewayStarts.clear();
|
|
14959
16479
|
checkinPassRunning = false;
|
|
14960
16480
|
autoCheckinSlots.clear();
|
|
14961
16481
|
autoCheckinLast = null;
|
|
@@ -14963,7 +16483,7 @@ async function onLoad() {
|
|
|
14963
16483
|
growthLast = null;
|
|
14964
16484
|
keepAliveLast = null;
|
|
14965
16485
|
qoderRetryAt = 0;
|
|
14966
|
-
core.client = new WorkBuddyUpstreamClient({
|
|
16486
|
+
core.client = new WorkBuddyUpstreamClient({});
|
|
14967
16487
|
core.pool = new WorkBuddyAccountPool({
|
|
14968
16488
|
client: core.client,
|
|
14969
16489
|
cooldownMs: cooldownMs(),
|
|
@@ -14977,7 +16497,6 @@ async function onLoad() {
|
|
|
14977
16497
|
});
|
|
14978
16498
|
await adoptHostSettings();
|
|
14979
16499
|
if (serviceStopped) return;
|
|
14980
|
-
declarationIndex = /* @__PURE__ */ new Map();
|
|
14981
16500
|
publishedIds = /* @__PURE__ */ new Set();
|
|
14982
16501
|
try {
|
|
14983
16502
|
dataPath = await pi.plugin.getDataPath();
|
|
@@ -15013,30 +16532,15 @@ async function onUnload() {
|
|
|
15013
16532
|
travelAdoptTried.clear();
|
|
15014
16533
|
traeIdentityBlockers.clear();
|
|
15015
16534
|
gatewayStartFailures.clear();
|
|
15016
|
-
|
|
15017
|
-
|
|
15018
|
-
|
|
15019
|
-
|
|
15020
|
-
}
|
|
15021
|
-
settingsChangedHandler = null;
|
|
16535
|
+
sourceStartFailures.clear();
|
|
16536
|
+
sourceGatewayStarts.clear();
|
|
16537
|
+
deviceLogins.clear();
|
|
16538
|
+
unsubscribeFromHostSettings();
|
|
15022
16539
|
}
|
|
15023
16540
|
function composerPointsEnabled() {
|
|
15024
16541
|
return settings.showComposerPoints === true;
|
|
15025
16542
|
}
|
|
15026
16543
|
/**
|
|
15027
|
-
* The name this plugin sends as the client when the user turns the rewrite on.
|
|
15028
|
-
*
|
|
15029
|
-
* WorkBuddy's domestic host refuses a request whose prompt names DeepSeek
|
|
15030
|
-
* Harness (`11128`, channel check) on every account of that region, so the panel
|
|
15031
|
-
* offers a switch that renames the client in the outgoing prompt. "DeepSeek" is
|
|
15032
|
-
* the vendor that actually serves the model, so the sentences stay true, and it
|
|
15033
|
-
* was verified against the live host: the two prompts it refuses are accepted
|
|
15034
|
-
* with the product name replaced.
|
|
15035
|
-
*/
|
|
15036
|
-
function replacedClientName() {
|
|
15037
|
-
return settings.rewriteClientName === true ? "DeepSeek" : void 0;
|
|
15038
|
-
}
|
|
15039
|
-
/**
|
|
15040
16544
|
* The source and region one selected provider route belongs to.
|
|
15041
16545
|
*
|
|
15042
16546
|
* The composer readout is handed the session's selection straight from the
|
|
@@ -15129,7 +16633,7 @@ function workbuddyComposerAccounts(region, modelId) {
|
|
|
15129
16633
|
function sourceComposerAccounts(source, region) {
|
|
15130
16634
|
const rows = (source === "trae" ? traeAccountRows : qoderAccountRows).filter((row) => row.region === region);
|
|
15131
16635
|
const live = rows.filter((row) => row.expired !== true);
|
|
15132
|
-
const active = orderSourceCredentials(source, region, live, (row) => row.label,
|
|
16636
|
+
const active = orderSourceCredentials(source, region, live, (row) => row.label, "peek")[0];
|
|
15133
16637
|
return rows.map((row) => {
|
|
15134
16638
|
const credits = row.usage?.credits;
|
|
15135
16639
|
return {
|
|
@@ -15215,9 +16719,12 @@ async function composerPrefer(args) {
|
|
|
15215
16719
|
else next[sourceRegionKey(source, region)] = accountLabel;
|
|
15216
16720
|
return next;
|
|
15217
16721
|
}) !== true) throw panelError("the account choice could not be saved", "SAVE_FAILED");
|
|
16722
|
+
const activeLabel = sourceComposerAccounts(source, region).find((row) => row.active === true)?.label ?? "";
|
|
16723
|
+
const servable = accountLabel === "" || activeLabel === accountLabel;
|
|
15218
16724
|
return {
|
|
15219
16725
|
...composerPoints(args),
|
|
15220
|
-
saved: true
|
|
16726
|
+
saved: true,
|
|
16727
|
+
...servable ? {} : { unavailable: true }
|
|
15221
16728
|
};
|
|
15222
16729
|
}
|
|
15223
16730
|
//#endregion
|
|
@@ -15361,7 +16868,6 @@ const Config = z.object({
|
|
|
15361
16868
|
preferredSourceAccounts: asVolatile(z.any().description("Picked Trae/Qoder account per region")),
|
|
15362
16869
|
removedAccounts: asVolatile(z.any().description("Accounts removed from the pool for good")),
|
|
15363
16870
|
showComposerPoints: asVolatile(z.boolean().default(false).description("Show the active account credits beside the composer")),
|
|
15364
|
-
rewriteClientName: asVolatile(z.boolean().default(false).description("Rename the client in the prompt WorkBuddy receives (domestic channel check 11128)")),
|
|
15365
16871
|
sourceDistributions: asVolatile(z.any().description("How each Trae/Qoder region spreads requests"))
|
|
15366
16872
|
});
|
|
15367
16873
|
/** Peel one volatile reference, if the running line wrapped it. */
|
|
@@ -15636,13 +17142,16 @@ function apply(ctx, config = {}) {
|
|
|
15636
17142
|
if (mutate === void 0) throw new Error("this host exposes no settings mutate()");
|
|
15637
17143
|
writesStarted += 1;
|
|
15638
17144
|
writesInFlight += 1;
|
|
17145
|
+
let echoed = false;
|
|
15639
17146
|
try {
|
|
15640
17147
|
await mutate.call(settingsService, settingsNamespace, Object.entries(patch).map(([key, value]) => ({
|
|
15641
17148
|
op: "set",
|
|
15642
17149
|
path: [key],
|
|
15643
17150
|
value
|
|
15644
17151
|
})));
|
|
17152
|
+
echoed = true;
|
|
15645
17153
|
} finally {
|
|
17154
|
+
if (!echoed) writesStarted -= 1;
|
|
15646
17155
|
writesInFlight -= 1;
|
|
15647
17156
|
if (writesInFlight === 0) {
|
|
15648
17157
|
if (droppedPushes > writesStarted) {
|