dsh-wtq-pool 1.2.0 → 1.2.2
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 +23 -0
- package/README.en.md +20 -15
- package/README.md +20 -15
- package/lib/index.js +682 -281
- package/package.json +12 -9
- package/renderer/index.html +1 -1
- package/renderer/panel.css +2 -1
- package/renderer/panel.js +168 -85
package/lib/index.js
CHANGED
|
@@ -12,9 +12,9 @@ import { resolveImageAttachmentAccess, resolveRetryPolicy } from "@deepseek-ai/d
|
|
|
12
12
|
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
|
+
import { constants, createCipheriv, createDecipheriv, createHash, publicEncrypt, randomBytes, randomUUID } from "node:crypto";
|
|
15
16
|
import * as net from "node:net";
|
|
16
17
|
import { BlockList, isIP } from "node:net";
|
|
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";
|
|
20
20
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
@@ -29,6 +29,17 @@ import { execFile, execFileSync, spawn } from "node:child_process";
|
|
|
29
29
|
* these lines agree.
|
|
30
30
|
*/
|
|
31
31
|
const LOG_PREFIX = "wtq-pool";
|
|
32
|
+
/**
|
|
33
|
+
* A stable, non-identifying tag for one account in a log line.
|
|
34
|
+
*
|
|
35
|
+
* The panel shows the label because the user needs to tell accounts apart. A
|
|
36
|
+
* log is copied into bug reports far more often than the panel is, and an email
|
|
37
|
+
* address or nickname there is personal data; a short digest still lets two
|
|
38
|
+
* lines about the same account be correlated without carrying the label.
|
|
39
|
+
*/
|
|
40
|
+
function accountLogTag(label) {
|
|
41
|
+
return "acct-" + createHash("sha256").update(String(label ?? "")).digest("hex").slice(0, 8);
|
|
42
|
+
}
|
|
32
43
|
//#endregion
|
|
33
44
|
//#region src/loopback.ts
|
|
34
45
|
const LOOPBACK_HOSTS = /* @__PURE__ */ new Set([
|
|
@@ -164,6 +175,7 @@ const REDACTIONS = [
|
|
|
164
175
|
[/\b(qoder-oauth-token-)[A-Za-z0-9_+/.=-]+/g, "$1[redacted]"],
|
|
165
176
|
[/\b(rt_)[A-Za-z0-9_+/.=-]+/g, "$1[redacted]"],
|
|
166
177
|
[/((?:\\{0,2}["']?)(?:access_?token|refresh_?token|id_?token|auth_?token|oauth_?token|api_?key|secret|token)(?:\\{0,2}["']?\s*[:=]\s*\\{0,2}["']?))(?=[A-Za-z0-9_+/.=-]{6,})(?=[A-Za-z0-9_+/.=-]*[A-Za-z_-])[A-Za-z0-9_+/.=-]+/gi, "$1[redacted]"],
|
|
178
|
+
[/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g, "[account]"],
|
|
167
179
|
[/\b[A-Za-z]:[\\/][^\r\n"'<>|]*/g, "[redacted path]"],
|
|
168
180
|
[/\\\\[^\r\n"'<>|]+/g, "[redacted path]"],
|
|
169
181
|
[/(['"])(\/(?!\/)[^'"\r\n]+)\1/g, "$1[redacted path]$1"],
|
|
@@ -585,7 +597,7 @@ function createLoopbackServer(options) {
|
|
|
585
597
|
});
|
|
586
598
|
server.listen(options.port ?? 0, "127.0.0.1");
|
|
587
599
|
server.on("error", (error) => {
|
|
588
|
-
options.logger?.warn?.("wtq-pool: " + options.label + " server error", error);
|
|
600
|
+
options.logger?.warn?.("wtq-pool: " + options.label + " server error", safeMessage(error));
|
|
589
601
|
});
|
|
590
602
|
return {
|
|
591
603
|
ready,
|
|
@@ -986,7 +998,7 @@ function registerPanelRoutes(ctx, deps) {
|
|
|
986
998
|
for (const dispose of disposers) try {
|
|
987
999
|
dispose();
|
|
988
1000
|
} catch (error) {
|
|
989
|
-
deps.logger.warn("dsh-wtq-pool: a panel route did not release cleanly", error);
|
|
1001
|
+
deps.logger.warn("dsh-wtq-pool: a panel route did not release cleanly", safeMessage(error));
|
|
990
1002
|
}
|
|
991
1003
|
};
|
|
992
1004
|
const mount = (route) => {
|
|
@@ -1047,7 +1059,7 @@ function registerPanelRoutes(ctx, deps) {
|
|
|
1047
1059
|
return release;
|
|
1048
1060
|
});
|
|
1049
1061
|
} catch (error) {
|
|
1050
|
-
deps.logger.warn("dsh-wtq-pool: the panel routes could not be mounted", error);
|
|
1062
|
+
deps.logger.warn("dsh-wtq-pool: the panel routes could not be mounted", safeMessage(error));
|
|
1051
1063
|
}
|
|
1052
1064
|
}
|
|
1053
1065
|
//#endregion
|
|
@@ -1764,7 +1776,7 @@ function classifyUpstreamError(status, body) {
|
|
|
1764
1776
|
if (isHardCreditText(body)) return "hard_credit";
|
|
1765
1777
|
}
|
|
1766
1778
|
if (status === 503 && SOFT_RATE_PATTERN.test(body)) return "soft_rate";
|
|
1767
|
-
if (status === 403 &&
|
|
1779
|
+
if (status === 403 && !/"code"\s*:|"msg"\s*:/.test(body)) return "waf_block";
|
|
1768
1780
|
if (status === 404) return "not_found";
|
|
1769
1781
|
if (status >= 500) return "server";
|
|
1770
1782
|
return "client";
|
|
@@ -1908,17 +1920,8 @@ function parseUpstreamModel(value) {
|
|
|
1908
1920
|
...supportsImages === void 0 ? {} : { supportsImages }
|
|
1909
1921
|
};
|
|
1910
1922
|
}
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
* with a user turn.
|
|
1914
|
-
*
|
|
1915
|
-
* The international gateway refuses a request whose first message is not a
|
|
1916
|
-
* system prompt (code 11128, "first message is not system prompt"), and a host
|
|
1917
|
-
* that starts with the user's turn sends none. The content is empty on purpose:
|
|
1918
|
-
* it satisfies the check without adding an instruction the model would follow,
|
|
1919
|
-
* so the model still sees exactly what the host sent. The CN gateway asks for
|
|
1920
|
-
* neither, so this is the one part of the shape that is region-specific.
|
|
1921
|
-
*/
|
|
1923
|
+
const NEUTRAL_SYSTEM_PROMPT = "You are a helpful assistant.";
|
|
1924
|
+
/** Global requires a leading system turn; Kimi rejects an empty placeholder. */
|
|
1922
1925
|
function withLeadingSystemPrompt(prepared) {
|
|
1923
1926
|
let body;
|
|
1924
1927
|
try {
|
|
@@ -1934,7 +1937,7 @@ function withLeadingSystemPrompt(prepared) {
|
|
|
1934
1937
|
if (typeof first === "object" && first !== null && first["role"] === "system") return prepared;
|
|
1935
1938
|
obj["messages"] = [{
|
|
1936
1939
|
role: "system",
|
|
1937
|
-
content:
|
|
1940
|
+
content: NEUTRAL_SYSTEM_PROMPT
|
|
1938
1941
|
}, ...messages];
|
|
1939
1942
|
return JSON.stringify(obj);
|
|
1940
1943
|
}
|
|
@@ -1989,7 +1992,7 @@ function contentText(content) {
|
|
|
1989
1992
|
* quoted the client name would fail the next request; that copy is renamed. A
|
|
1990
1993
|
* neutral system message stays first so a gateway that insists on one still sees
|
|
1991
1994
|
* it. Returns undefined when there is nothing to move or rename, which is also
|
|
1992
|
-
* what keeps
|
|
1995
|
+
* what keeps the neutral placeholder from folding into the user turn again.
|
|
1993
1996
|
*/
|
|
1994
1997
|
function foldSystemIntoUser(prepared) {
|
|
1995
1998
|
let body;
|
|
@@ -2005,12 +2008,14 @@ function foldSystemIntoUser(prepared) {
|
|
|
2005
2008
|
const prompts = [];
|
|
2006
2009
|
const rest = [];
|
|
2007
2010
|
let renamed = false;
|
|
2011
|
+
let hadSystem = false;
|
|
2008
2012
|
for (const value of messages) {
|
|
2009
2013
|
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
2010
2014
|
const message = value;
|
|
2011
2015
|
if (message["role"] === "system" || message["role"] === "developer") {
|
|
2016
|
+
hadSystem = true;
|
|
2012
2017
|
const text = contentText(message["content"]);
|
|
2013
|
-
if (text.trim() !== "") prompts.push(text);
|
|
2018
|
+
if (text.trim() !== "" && text !== NEUTRAL_SYSTEM_PROMPT) prompts.push(text);
|
|
2014
2019
|
continue;
|
|
2015
2020
|
}
|
|
2016
2021
|
if (message["role"] === "assistant" && "content" in message) {
|
|
@@ -2029,7 +2034,10 @@ function foldSystemIntoUser(prepared) {
|
|
|
2029
2034
|
}
|
|
2030
2035
|
if (prompts.length === 0 && !renamed) return void 0;
|
|
2031
2036
|
if (prompts.length === 0) {
|
|
2032
|
-
obj["messages"] =
|
|
2037
|
+
obj["messages"] = hadSystem ? [{
|
|
2038
|
+
role: "system",
|
|
2039
|
+
content: NEUTRAL_SYSTEM_PROMPT
|
|
2040
|
+
}, ...rest] : rest;
|
|
2033
2041
|
return JSON.stringify(obj);
|
|
2034
2042
|
}
|
|
2035
2043
|
const instructions = prompts.join("\n\n");
|
|
@@ -2051,7 +2059,7 @@ function foldSystemIntoUser(prepared) {
|
|
|
2051
2059
|
}
|
|
2052
2060
|
obj["messages"] = [{
|
|
2053
2061
|
role: "system",
|
|
2054
|
-
content:
|
|
2062
|
+
content: NEUTRAL_SYSTEM_PROMPT
|
|
2055
2063
|
}, ...rest];
|
|
2056
2064
|
return JSON.stringify(obj);
|
|
2057
2065
|
}
|
|
@@ -2822,6 +2830,18 @@ function openEncryptedField(field, key) {
|
|
|
2822
2830
|
function cleanIconPath(raw) {
|
|
2823
2831
|
return raw.replace(/^"/u, "").replace(/",-?\d+$/u, "").replace(/,-?\d+$/u, "").trim();
|
|
2824
2832
|
}
|
|
2833
|
+
/**
|
|
2834
|
+
* Whether a Windows path names a local file rather than a network share.
|
|
2835
|
+
*
|
|
2836
|
+
* The registry scan reads values any process running as the user can write. A
|
|
2837
|
+
* `DisplayIcon` naming `\\host\share\x.exe` would make PowerShell's own path
|
|
2838
|
+
* check authenticate to that host before any signature was read, leaking the
|
|
2839
|
+
* user's NTLM response. Only absolute drive paths are considered, and every
|
|
2840
|
+
* stat and spawn goes through the same filter.
|
|
2841
|
+
*/
|
|
2842
|
+
function localWindowsPath(candidate) {
|
|
2843
|
+
return /^[A-Za-z]:[\\/]/u.test(candidate);
|
|
2844
|
+
}
|
|
2825
2845
|
function windowsRegistryAppPaths() {
|
|
2826
2846
|
if (process.platform !== "win32") return [];
|
|
2827
2847
|
const cached = registryAppPaths;
|
|
@@ -2892,21 +2912,20 @@ function scanInterpreter(env) {
|
|
|
2892
2912
|
return join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
2893
2913
|
}
|
|
2894
2914
|
/**
|
|
2895
|
-
* Every path an uninstall key names for a WorkBuddy-family install
|
|
2896
|
-
* the signature PowerShell found on it.
|
|
2915
|
+
* Every path an uninstall key names for a WorkBuddy-family install.
|
|
2897
2916
|
*
|
|
2898
2917
|
* The scan reads `DisplayName`, `DisplayIcon` and `InstallLocation` out of the
|
|
2899
2918
|
* user's own uninstall keys, which means any process running as the user can add
|
|
2900
|
-
* one.
|
|
2901
|
-
*
|
|
2902
|
-
*
|
|
2903
|
-
*
|
|
2919
|
+
* one. It only enumerates here: a path is filtered to a local drive, stat'ed,
|
|
2920
|
+
* and signature-checked in Node before anything is spawned. Doing the stat or
|
|
2921
|
+
* signature read in PowerShell would touch a planted UNC path first and leak an
|
|
2922
|
+
* NTLM response to the host it names.
|
|
2904
2923
|
*
|
|
2905
|
-
* PowerShell does the part only it can do: enumerate
|
|
2906
|
-
* and quotes off `DisplayIcon
|
|
2907
|
-
*
|
|
2908
|
-
*
|
|
2909
|
-
*
|
|
2924
|
+
* PowerShell does the part only it can do: enumerate the keys and clean the
|
|
2925
|
+
* resource index and quotes off `DisplayIcon`. The decision is
|
|
2926
|
+
* {@link signatureAccepts}'s, in Node, because that is where it can be tested
|
|
2927
|
+
* without a registry to plant a key in, and it is handed a module path 5.1 can
|
|
2928
|
+
* actually load, because the host's is not always one.
|
|
2910
2929
|
*/
|
|
2911
2930
|
function readRegistryAppPaths() {
|
|
2912
2931
|
const script = [
|
|
@@ -2921,23 +2940,19 @@ function readRegistryAppPaths() {
|
|
|
2921
2940
|
" return $v",
|
|
2922
2941
|
"}",
|
|
2923
2942
|
"$roots=@('HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*','HKLM:\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*','HKCU:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*')",
|
|
2924
|
-
"foreach($r in $roots){",
|
|
2943
|
+
"$out = foreach($r in $roots){",
|
|
2925
2944
|
" Get-ItemProperty -Path $r | ForEach-Object {",
|
|
2926
2945
|
" $dn=[string]$_.DisplayName; $di=[string]$_.DisplayIcon; $il=[string]$_.InstallLocation",
|
|
2927
2946
|
" if($dn -match 'WorkBuddy|CodeBuddy' -or $di -match 'WorkBuddy|CodeBuddy'){",
|
|
2928
2947
|
" foreach($raw in @($di,$il)){",
|
|
2929
2948
|
" $p = Clean $raw",
|
|
2930
2949
|
" if($p -eq ''){ continue }",
|
|
2931
|
-
"
|
|
2932
|
-
" $s = $null",
|
|
2933
|
-
" try { $s = Get-AuthenticodeSignature -LiteralPath $p } catch { $s = $null }",
|
|
2934
|
-
" $st = if($s -ne $null){ [string]$s.Status } else { \"\" }",
|
|
2935
|
-
" $sj = if($s -ne $null -and $s.SignerCertificate -ne $null){ [string]$s.SignerCertificate.Subject } else { \"\" }",
|
|
2936
|
-
" Write-Output ($p + [char]9 + $st + [char]9 + $sj)",
|
|
2950
|
+
" Write-Output $p",
|
|
2937
2951
|
" }",
|
|
2938
2952
|
" }",
|
|
2939
2953
|
" }",
|
|
2940
|
-
"}"
|
|
2954
|
+
"}",
|
|
2955
|
+
"ConvertTo-Json -InputObject @($out) -Compress"
|
|
2941
2956
|
].join("\n");
|
|
2942
2957
|
let stdout;
|
|
2943
2958
|
const interpreter = scanInterpreter(process.env);
|
|
@@ -2960,12 +2975,18 @@ function readRegistryAppPaths() {
|
|
|
2960
2975
|
} catch {
|
|
2961
2976
|
return [];
|
|
2962
2977
|
}
|
|
2978
|
+
let parsed;
|
|
2979
|
+
try {
|
|
2980
|
+
parsed = JSON.parse(stdout);
|
|
2981
|
+
} catch {
|
|
2982
|
+
return [];
|
|
2983
|
+
}
|
|
2984
|
+
const entries = Array.isArray(parsed) ? parsed : [parsed];
|
|
2963
2985
|
const out = [];
|
|
2964
|
-
for (const
|
|
2965
|
-
|
|
2966
|
-
const cleaned =
|
|
2967
|
-
if (cleaned === "") continue;
|
|
2968
|
-
if (!signatureAccepts(status, signer)) continue;
|
|
2986
|
+
for (const entry of entries) {
|
|
2987
|
+
if (typeof entry !== "string") continue;
|
|
2988
|
+
const cleaned = cleanIconPath(entry);
|
|
2989
|
+
if (cleaned === "" || !localWindowsPath(cleaned)) continue;
|
|
2969
2990
|
out.push(cleaned);
|
|
2970
2991
|
}
|
|
2971
2992
|
return out;
|
|
@@ -2977,11 +2998,12 @@ function readRegistryAppPaths() {
|
|
|
2977
2998
|
* PowerShell half is not unit-testable, and this half is where the decision
|
|
2978
2999
|
* actually lives.
|
|
2979
3000
|
*
|
|
2980
|
-
* `Valid` means the chain resolved to a trusted root
|
|
2981
|
-
* the
|
|
2982
|
-
*
|
|
2983
|
-
*
|
|
2984
|
-
*
|
|
3001
|
+
* `Valid` means the chain resolved to a trusted root. A user process cannot
|
|
3002
|
+
* forge the vendor's signature, but it can trust a certificate of its own in the
|
|
3003
|
+
* current user's root store without elevation, so this check raises the bar
|
|
3004
|
+
* rather than proving the file is the vendor's. It still rejects an unsigned
|
|
3005
|
+
* planted executable, and every cached registry path is checked again before it
|
|
3006
|
+
* is run.
|
|
2985
3007
|
*
|
|
2986
3008
|
* The publisher is deliberately not pinned. The plugin is a third-party build and
|
|
2987
3009
|
* the vendor can change its signing certificate; a pinned name would turn that
|
|
@@ -2995,17 +3017,100 @@ function readRegistryAppPaths() {
|
|
|
2995
3017
|
function signatureAccepts(status, signer) {
|
|
2996
3018
|
return status === "Valid" && signer !== void 0 && signer.trim() !== "";
|
|
2997
3019
|
}
|
|
3020
|
+
/** Why the last signature scan could not produce an answer, if it could not. */
|
|
3021
|
+
let signatureScanFailure = "";
|
|
3022
|
+
/**
|
|
3023
|
+
* The candidates whose Authenticode signature is valid.
|
|
3024
|
+
*
|
|
3025
|
+
* The standard install roots and the environment override were trusted by path
|
|
3026
|
+
* alone, while only the registry fallback checked a signature. A process running
|
|
3027
|
+
* as the user can write `%LOCALAPPDATA%\\Programs\\WorkBuddy\\WorkBuddy.exe`; when
|
|
3028
|
+
* the host runs elevated, spawning that file would hand it the plugin's whole
|
|
3029
|
+
* environment and every WorkBuddy token. Every path is checked here, and the
|
|
3030
|
+
* registry result is passed through the same check before it is spawned.
|
|
3031
|
+
*/
|
|
3032
|
+
async function signedExecutables(candidates) {
|
|
3033
|
+
if (process.platform !== "win32" || candidates.length === 0) return candidates;
|
|
3034
|
+
signatureScanFailure = "";
|
|
3035
|
+
const interpreter = scanInterpreter(process.env);
|
|
3036
|
+
if (interpreter === void 0) {
|
|
3037
|
+
signatureScanFailure = "the signature check could not run: Windows PowerShell was not found";
|
|
3038
|
+
return [];
|
|
3039
|
+
}
|
|
3040
|
+
const script = [
|
|
3041
|
+
"$ErrorActionPreference='SilentlyContinue'",
|
|
3042
|
+
"try { [Console]::OutputEncoding=[System.Text.Encoding]::UTF8 } catch {}",
|
|
3043
|
+
`$paths = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('${Buffer.from(JSON.stringify(candidates), "utf8").toString("base64")}')) | ConvertFrom-Json`,
|
|
3044
|
+
"$out = foreach ($p in @($paths)) {",
|
|
3045
|
+
" if ([string]::IsNullOrWhiteSpace([string]$p)) { continue }",
|
|
3046
|
+
" $s = $null",
|
|
3047
|
+
" try { $s = Get-AuthenticodeSignature -LiteralPath ([string]$p) } catch { $s = $null }",
|
|
3048
|
+
" [pscustomobject]@{",
|
|
3049
|
+
" path = [string]$p",
|
|
3050
|
+
" status = if ($s -ne $null) { [string]$s.Status } else { \"\" }",
|
|
3051
|
+
" signer = if ($s -ne $null -and $s.SignerCertificate -ne $null) { [string]$s.SignerCertificate.Subject } else { \"\" }",
|
|
3052
|
+
" }",
|
|
3053
|
+
"}",
|
|
3054
|
+
"ConvertTo-Json -InputObject @($out) -Compress"
|
|
3055
|
+
].join("\n");
|
|
3056
|
+
let stdout;
|
|
3057
|
+
try {
|
|
3058
|
+
stdout = await new Promise((resolve, reject) => {
|
|
3059
|
+
execFile(interpreter, [
|
|
3060
|
+
"-NoProfile",
|
|
3061
|
+
"-NonInteractive",
|
|
3062
|
+
"-ExecutionPolicy",
|
|
3063
|
+
"Bypass",
|
|
3064
|
+
"-Command",
|
|
3065
|
+
script
|
|
3066
|
+
], {
|
|
3067
|
+
encoding: "utf8",
|
|
3068
|
+
timeout: 2e4,
|
|
3069
|
+
windowsHide: true,
|
|
3070
|
+
maxBuffer: 8388608,
|
|
3071
|
+
env: signatureScanEnv()
|
|
3072
|
+
}, (error, output) => {
|
|
3073
|
+
if (error !== null) reject(error);
|
|
3074
|
+
else resolve(output);
|
|
3075
|
+
});
|
|
3076
|
+
});
|
|
3077
|
+
} catch {
|
|
3078
|
+
signatureScanFailure = "the signature check could not run";
|
|
3079
|
+
return [];
|
|
3080
|
+
}
|
|
3081
|
+
let parsed;
|
|
3082
|
+
try {
|
|
3083
|
+
parsed = JSON.parse(stdout);
|
|
3084
|
+
} catch {
|
|
3085
|
+
signatureScanFailure = "the signature check returned no readable answer";
|
|
3086
|
+
return [];
|
|
3087
|
+
}
|
|
3088
|
+
const entries = Array.isArray(parsed) ? parsed : [parsed];
|
|
3089
|
+
const accepted = [];
|
|
3090
|
+
for (const entry of entries) {
|
|
3091
|
+
if (entry === null || typeof entry !== "object") continue;
|
|
3092
|
+
const record = entry;
|
|
3093
|
+
const file = record["path"];
|
|
3094
|
+
const status = record["status"];
|
|
3095
|
+
const signer = record["signer"];
|
|
3096
|
+
if (typeof file !== "string" || file === "") continue;
|
|
3097
|
+
if (!signatureAccepts(typeof status === "string" ? status : void 0, typeof signer === "string" ? signer : void 0)) continue;
|
|
3098
|
+
accepted.push(file);
|
|
3099
|
+
}
|
|
3100
|
+
return accepted;
|
|
3101
|
+
}
|
|
2998
3102
|
function statExecutables(candidates) {
|
|
2999
3103
|
const files = [];
|
|
3000
3104
|
for (const candidate of candidates) {
|
|
3001
3105
|
if (candidate === "") continue;
|
|
3106
|
+
if (process.platform === "win32" && !localWindowsPath(candidate)) continue;
|
|
3002
3107
|
try {
|
|
3003
3108
|
if (statSync(candidate, { throwIfNoEntry: false })?.isFile() === true) files.push(candidate);
|
|
3004
3109
|
} catch {}
|
|
3005
3110
|
}
|
|
3006
3111
|
return files;
|
|
3007
3112
|
}
|
|
3008
|
-
function executableCandidates(env = process.env) {
|
|
3113
|
+
async function executableCandidates(env = process.env) {
|
|
3009
3114
|
const override = env[WORKBUDDY_APP_EXECUTABLE_ENV]?.trim();
|
|
3010
3115
|
const known = override === void 0 ? [] : [override];
|
|
3011
3116
|
if (process.platform === "win32") {
|
|
@@ -3026,9 +3131,9 @@ function executableCandidates(env = process.env) {
|
|
|
3026
3131
|
const home = homedir();
|
|
3027
3132
|
for (const bundle of ["WorkBuddy.app", "WorkBuddy AI.app"]) for (const base of ["/Applications", join(home, "Applications")]) known.push(join(base, bundle, "Contents", "MacOS", "Electron"));
|
|
3028
3133
|
}
|
|
3029
|
-
const standard = statExecutables(known);
|
|
3134
|
+
const standard = await signedExecutables(statExecutables(known));
|
|
3030
3135
|
if (standard.length > 0 || process.platform !== "win32") return standard;
|
|
3031
|
-
return statExecutables(windowsRegistryAppPaths());
|
|
3136
|
+
return signedExecutables(statExecutables(windowsRegistryAppPaths()));
|
|
3032
3137
|
}
|
|
3033
3138
|
function fetchAtRestKeyPayload(executable) {
|
|
3034
3139
|
return new Promise((resolve, reject) => {
|
|
@@ -3087,10 +3192,10 @@ function ensureAtRestKeys() {
|
|
|
3087
3192
|
if (atRestKeyById.size > 0) return Promise.resolve();
|
|
3088
3193
|
if (Date.now() - lastKeyFailureAtMs < KEY_FAILURE_BACKOFF_MS) return Promise.resolve();
|
|
3089
3194
|
inflightKeys ??= (async () => {
|
|
3090
|
-
const candidates = executableCandidates();
|
|
3195
|
+
const candidates = await executableCandidates();
|
|
3091
3196
|
if (candidates.length === 0) {
|
|
3092
3197
|
lastKeyFailureAtMs = Date.now();
|
|
3093
|
-
lastKeyFailure = "no WorkBuddy executable found in the standard install roots";
|
|
3198
|
+
lastKeyFailure = process.platform === "win32" ? signatureScanFailure !== "" ? signatureScanFailure : "no WorkBuddy executable with a valid signature was found" : "no WorkBuddy executable found in the standard install roots";
|
|
3094
3199
|
return;
|
|
3095
3200
|
}
|
|
3096
3201
|
let anySuccess = false;
|
|
@@ -3737,11 +3842,12 @@ function ownSignInOf(credential) {
|
|
|
3737
3842
|
* account rewrites its own file instead of leaving a second copy behind, and
|
|
3738
3843
|
* two accounts never collide on a name.
|
|
3739
3844
|
*/
|
|
3740
|
-
async function writeOwnSignIn(directory, signIn) {
|
|
3845
|
+
async function writeOwnSignIn(directory, signIn, signal) {
|
|
3741
3846
|
const stable = signIn.uin === "" ? signIn.uid : signIn.uin;
|
|
3742
3847
|
const suffix = stable === "" ? randomUUID() : stable.replace(/[^A-Za-z0-9_-]/g, "").slice(0, 32);
|
|
3743
3848
|
const file = join(directory, `${OWN_SIGN_IN_PREFIX}${suffix === "" ? randomUUID() : suffix}.info`);
|
|
3744
3849
|
await mkdir(directory, { recursive: true });
|
|
3850
|
+
signal?.throwIfAborted();
|
|
3745
3851
|
await writeFile(file, JSON.stringify({
|
|
3746
3852
|
auth: {
|
|
3747
3853
|
accessToken: signIn.accessToken,
|
|
@@ -3884,8 +3990,9 @@ async function readArchivedCredentials(file) {
|
|
|
3884
3990
|
* leave a truncated file behind if it is interrupted: temp file, then rename.
|
|
3885
3991
|
* Only the archived credential is stored, not the raw sign-in document.
|
|
3886
3992
|
*/
|
|
3887
|
-
async function writeArchivedCredentials(file, entries) {
|
|
3993
|
+
async function writeArchivedCredentials(file, entries, signal) {
|
|
3888
3994
|
await mkdir(dirname(file), { recursive: true });
|
|
3995
|
+
signal?.throwIfAborted();
|
|
3889
3996
|
const body = JSON.stringify({
|
|
3890
3997
|
version: 1,
|
|
3891
3998
|
entries: [...entries].map(([id, credential]) => ({
|
|
@@ -3909,6 +4016,7 @@ async function writeArchivedCredentials(file, entries) {
|
|
|
3909
4016
|
encoding: "utf8",
|
|
3910
4017
|
mode: 384
|
|
3911
4018
|
});
|
|
4019
|
+
signal?.throwIfAborted();
|
|
3912
4020
|
await rename(temporary, file);
|
|
3913
4021
|
} catch (error) {
|
|
3914
4022
|
await unlink(temporary).catch(() => {});
|
|
@@ -3988,6 +4096,7 @@ var WorkBuddyAccountPool = class {
|
|
|
3988
4096
|
deletedSignInsStamp;
|
|
3989
4097
|
/** The directories the in-flight scan is reading, so a repoint supersedes it. */
|
|
3990
4098
|
scanDirs = [];
|
|
4099
|
+
scanAbort = new AbortController();
|
|
3991
4100
|
/**
|
|
3992
4101
|
* Accounts whose "expired with no refresh token" warning has been logged.
|
|
3993
4102
|
*
|
|
@@ -4059,7 +4168,7 @@ var WorkBuddyAccountPool = class {
|
|
|
4059
4168
|
let emptied = false;
|
|
4060
4169
|
if (options.authDirs !== void 0 && options.authDirs.length > 0) {
|
|
4061
4170
|
if (!sameDirs(this.authDirs, options.authDirs)) {
|
|
4062
|
-
this.
|
|
4171
|
+
this.clear();
|
|
4063
4172
|
emptied = true;
|
|
4064
4173
|
}
|
|
4065
4174
|
this.authDirs = options.authDirs;
|
|
@@ -4094,7 +4203,7 @@ var WorkBuddyAccountPool = class {
|
|
|
4094
4203
|
if (hold.reason !== "spent" || total <= 0) return;
|
|
4095
4204
|
this.heldIds.delete(accountId);
|
|
4096
4205
|
if (account.cooldownUntilMs > Date.now() && account.cooldownUntilMs <= hold.until) account.cooldownUntilMs = 0;
|
|
4097
|
-
this.logger?.info?.(`${LOG_PREFIX}: ${account.label} has credits again; releasing the hold`);
|
|
4206
|
+
this.logger?.info?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} has credits again; releasing the hold`);
|
|
4098
4207
|
}
|
|
4099
4208
|
/** True when a known balance has reached the floor set for this account. */
|
|
4100
4209
|
reserveHolds(accountId) {
|
|
@@ -4208,9 +4317,10 @@ var WorkBuddyAccountPool = class {
|
|
|
4208
4317
|
* client's directory until the client overwrites it, so a tombstone that
|
|
4209
4318
|
* expired would bring that copy straight back, refresh token included.
|
|
4210
4319
|
*/
|
|
4211
|
-
async loadDeletedSignIns() {
|
|
4320
|
+
async loadDeletedSignIns(signal) {
|
|
4212
4321
|
const file = this.deletedSignInsFile();
|
|
4213
4322
|
const stamp = file === void 0 ? "" : await signInFileStamp(file);
|
|
4323
|
+
if (signal?.aborted) return this.deletedSignIns ?? /* @__PURE__ */ new Map();
|
|
4214
4324
|
if (this.deletedSignIns !== void 0 && this.deletedSignInsStamp === stamp) return this.deletedSignIns;
|
|
4215
4325
|
if (file === void 0) {
|
|
4216
4326
|
this.deletedSignInsStamp = "";
|
|
@@ -4219,10 +4329,12 @@ var WorkBuddyAccountPool = class {
|
|
|
4219
4329
|
}
|
|
4220
4330
|
try {
|
|
4221
4331
|
const tombstones = await readDeletedSignInsFile(file);
|
|
4332
|
+
if (signal?.aborted) return this.deletedSignIns ?? /* @__PURE__ */ new Map();
|
|
4222
4333
|
this.deletedSignInsStamp = stamp;
|
|
4223
4334
|
this.deletedSignIns = tombstones;
|
|
4224
4335
|
return tombstones;
|
|
4225
4336
|
} catch (error) {
|
|
4337
|
+
if (signal?.aborted) return this.deletedSignIns ?? /* @__PURE__ */ new Map();
|
|
4226
4338
|
if (error.code === "ENOENT") {
|
|
4227
4339
|
this.deletedSignInsStamp = stamp;
|
|
4228
4340
|
this.deletedSignIns = /* @__PURE__ */ new Map();
|
|
@@ -4244,13 +4356,15 @@ var WorkBuddyAccountPool = class {
|
|
|
4244
4356
|
* Answers false when the file could not be written, so a delete can refuse to
|
|
4245
4357
|
* report success on a state it could not record.
|
|
4246
4358
|
*/
|
|
4247
|
-
async writeDeletedSignIns(removed = []) {
|
|
4359
|
+
async writeDeletedSignIns(removed = [], signal) {
|
|
4248
4360
|
const tombstones = this.deletedSignIns;
|
|
4249
4361
|
const file = this.deletedSignInsFile();
|
|
4250
4362
|
if (tombstones === void 0 || file === void 0) return true;
|
|
4251
4363
|
let written = false;
|
|
4252
4364
|
await withFileLock(`${file}.lock`, async () => {
|
|
4365
|
+
if (signal?.aborted) return;
|
|
4253
4366
|
const onDisk = await readDeletedSignInsFile(file).catch(() => /* @__PURE__ */ new Map());
|
|
4367
|
+
if (signal?.aborted) return;
|
|
4254
4368
|
for (const [id, entry] of onDisk) {
|
|
4255
4369
|
if (removed.includes(id)) continue;
|
|
4256
4370
|
const ours = tombstones.get(id);
|
|
@@ -4259,16 +4373,19 @@ var WorkBuddyAccountPool = class {
|
|
|
4259
4373
|
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
4260
4374
|
try {
|
|
4261
4375
|
await mkdir(dirname(file), { recursive: true });
|
|
4376
|
+
signal?.throwIfAborted();
|
|
4262
4377
|
await writeFile(temporary, JSON.stringify({
|
|
4263
4378
|
version: 1,
|
|
4264
4379
|
entries: Object.fromEntries(tombstones)
|
|
4265
4380
|
}, null, 2), { mode: 384 });
|
|
4381
|
+
signal?.throwIfAborted();
|
|
4266
4382
|
await rename(temporary, file);
|
|
4267
|
-
|
|
4383
|
+
const stamp = await signInFileStamp(file);
|
|
4384
|
+
if (!signal?.aborted && this.deletedSignIns === tombstones) this.deletedSignInsStamp = stamp;
|
|
4268
4385
|
written = true;
|
|
4269
4386
|
} catch (error) {
|
|
4270
4387
|
await unlink(temporary).catch(() => {});
|
|
4271
|
-
this.logger?.warn?.(`${LOG_PREFIX}: could not record the deleted sign-ins: ${String(error)}`);
|
|
4388
|
+
if (!signal?.aborted) this.logger?.warn?.(`${LOG_PREFIX}: could not record the deleted sign-ins: ${String(error)}`);
|
|
4272
4389
|
}
|
|
4273
4390
|
});
|
|
4274
4391
|
return written;
|
|
@@ -4318,7 +4435,7 @@ var WorkBuddyAccountPool = class {
|
|
|
4318
4435
|
* refreshing on its own schedule is how both end up with a spent refresh
|
|
4319
4436
|
* token. An account the client is signed into is skipped for the same reason.
|
|
4320
4437
|
*/
|
|
4321
|
-
async adoptOrphanedSignIns(found, tornDown) {
|
|
4438
|
+
async adoptOrphanedSignIns(found, tornDown, signal) {
|
|
4322
4439
|
const directory = ownSignInDir(this.dataPath?.() ?? "");
|
|
4323
4440
|
if (directory === "") return [];
|
|
4324
4441
|
const liveIds = /* @__PURE__ */ new Set();
|
|
@@ -4330,6 +4447,7 @@ var WorkBuddyAccountPool = class {
|
|
|
4330
4447
|
}
|
|
4331
4448
|
const adopted = [];
|
|
4332
4449
|
for (const credential of found) {
|
|
4450
|
+
if (signal.aborted) break;
|
|
4333
4451
|
if (isLiveAuthFile(credential.sourcePath)) continue;
|
|
4334
4452
|
if (ownSignInPath(credential.sourcePath, directory)) continue;
|
|
4335
4453
|
const id = workbuddyAccountId(credential);
|
|
@@ -4340,9 +4458,10 @@ var WorkBuddyAccountPool = class {
|
|
|
4340
4458
|
file = await writeOwnSignIn(directory, {
|
|
4341
4459
|
...ownSignInOf(credential),
|
|
4342
4460
|
lastRefreshAtMs: credential.lastRefreshAtMs ?? 0
|
|
4343
|
-
});
|
|
4461
|
+
}, signal);
|
|
4344
4462
|
} catch (error) {
|
|
4345
|
-
|
|
4463
|
+
if (signal.aborted) break;
|
|
4464
|
+
this.logger?.warn?.(`${LOG_PREFIX}: could not take over ${accountLogTag(accountLabel(credential))}: ${String(error)}`);
|
|
4346
4465
|
continue;
|
|
4347
4466
|
}
|
|
4348
4467
|
ownedIds.add(id);
|
|
@@ -4365,7 +4484,7 @@ var WorkBuddyAccountPool = class {
|
|
|
4365
4484
|
try {
|
|
4366
4485
|
await renewOwnSignInFile(credential.sourcePath, credential);
|
|
4367
4486
|
} catch (error) {
|
|
4368
|
-
this.logger?.warn?.(`${LOG_PREFIX}: could not write the renewed token back for ${credential.nickname ?? credential.uid}: ${String(error)}`);
|
|
4487
|
+
this.logger?.warn?.(`${LOG_PREFIX}: could not write the renewed token back for ${accountLogTag(credential.nickname ?? credential.uid)}: ${String(error)}`);
|
|
4369
4488
|
}
|
|
4370
4489
|
return;
|
|
4371
4490
|
}
|
|
@@ -4374,7 +4493,7 @@ var WorkBuddyAccountPool = class {
|
|
|
4374
4493
|
try {
|
|
4375
4494
|
credential.sourcePath = await writeOwnSignIn(directory, ownSignInOf(credential));
|
|
4376
4495
|
} catch (error) {
|
|
4377
|
-
this.logger?.warn?.(`${LOG_PREFIX}: could not keep the renewed token for ${credential.nickname ?? credential.uid}: ${String(error)}`);
|
|
4496
|
+
this.logger?.warn?.(`${LOG_PREFIX}: could not keep the renewed token for ${accountLogTag(credential.nickname ?? credential.uid)}: ${String(error)}`);
|
|
4378
4497
|
}
|
|
4379
4498
|
}
|
|
4380
4499
|
/**
|
|
@@ -4401,14 +4520,25 @@ var WorkBuddyAccountPool = class {
|
|
|
4401
4520
|
}
|
|
4402
4521
|
/** Drop every account, e.g. when the source is switched off. A later `scan()` rebuilds from disk. */
|
|
4403
4522
|
clear() {
|
|
4523
|
+
this.scanAbort.abort();
|
|
4524
|
+
this.scanAbort = new AbortController();
|
|
4525
|
+
this.scanInflight = void 0;
|
|
4526
|
+
this.scanDirs = [];
|
|
4404
4527
|
this.accounts = [];
|
|
4405
4528
|
this.appHeldIds = /* @__PURE__ */ new Set();
|
|
4406
4529
|
}
|
|
4407
|
-
/**
|
|
4530
|
+
/** Read after any scan already running, so a manual request gets a fresh snapshot. */
|
|
4531
|
+
async rescan() {
|
|
4532
|
+
const signal = this.scanAbort.signal;
|
|
4533
|
+
await this.scanInflight?.catch(() => {});
|
|
4534
|
+
if (signal.aborted) return this.accounts;
|
|
4535
|
+
return this.scan();
|
|
4536
|
+
}
|
|
4537
|
+
/** Scan the auth directories and merge newly discovered accounts. */
|
|
4408
4538
|
async scan() {
|
|
4409
4539
|
const dirs = this.scanDirsFor(this.authDirs);
|
|
4410
4540
|
if (this.scanInflight !== void 0 && sameDirs(this.scanDirs, dirs)) return this.scanInflight;
|
|
4411
|
-
const inflight = this.scanOnce(dirs).finally(() => {
|
|
4541
|
+
const inflight = this.scanOnce(dirs, this.scanAbort.signal).finally(() => {
|
|
4412
4542
|
if (this.scanInflight === inflight) {
|
|
4413
4543
|
this.scanInflight = void 0;
|
|
4414
4544
|
this.scanDirs = [];
|
|
@@ -4439,12 +4569,14 @@ var WorkBuddyAccountPool = class {
|
|
|
4439
4569
|
*
|
|
4440
4570
|
* Best effort by design: a failed write costs the archive, never the scan.
|
|
4441
4571
|
*/
|
|
4442
|
-
async archiveCredentials(credentials) {
|
|
4572
|
+
async archiveCredentials(credentials, signal) {
|
|
4443
4573
|
const file = this.archiveFile();
|
|
4444
4574
|
if (file === void 0 || credentials.length === 0) return;
|
|
4445
4575
|
const ownDir = ownSignInDir(this.dataPath?.() ?? "");
|
|
4446
4576
|
const run = this.archiveQueue.then(async () => {
|
|
4577
|
+
if (signal?.aborted) return;
|
|
4447
4578
|
const stored = await readArchivedCredentials(file);
|
|
4579
|
+
if (signal?.aborted) return;
|
|
4448
4580
|
let changed = false;
|
|
4449
4581
|
for (const credential of credentials) {
|
|
4450
4582
|
const id = workbuddyAccountId(credential);
|
|
@@ -4453,13 +4585,13 @@ var WorkBuddyAccountPool = class {
|
|
|
4453
4585
|
stored.set(id, credential);
|
|
4454
4586
|
changed = true;
|
|
4455
4587
|
}
|
|
4456
|
-
if (changed) await writeArchivedCredentials(file, stored);
|
|
4588
|
+
if (changed) await writeArchivedCredentials(file, stored, signal);
|
|
4457
4589
|
});
|
|
4458
4590
|
this.archiveQueue = run.catch(() => {});
|
|
4459
4591
|
try {
|
|
4460
4592
|
await run;
|
|
4461
4593
|
} catch (error) {
|
|
4462
|
-
this.logger?.warn?.(`${LOG_PREFIX}: could not update the credential archive: ${String(error)}`);
|
|
4594
|
+
if (!signal?.aborted) this.logger?.warn?.(`${LOG_PREFIX}: could not update the credential archive: ${String(error)}`);
|
|
4463
4595
|
}
|
|
4464
4596
|
}
|
|
4465
4597
|
/** Credentials the archive kept for a directory this walk can read. */
|
|
@@ -4481,21 +4613,25 @@ var WorkBuddyAccountPool = class {
|
|
|
4481
4613
|
const directory = this.dataPath?.();
|
|
4482
4614
|
return directory === void 0 || directory === "" ? void 0 : join(directory, ARCHIVE_FILENAME);
|
|
4483
4615
|
}
|
|
4484
|
-
async scanOnce(dirs) {
|
|
4616
|
+
async scanOnce(dirs, signal = this.scanAbort.signal) {
|
|
4485
4617
|
const found = [];
|
|
4486
4618
|
const readableDirs = /* @__PURE__ */ new Set();
|
|
4487
4619
|
const sealedDirs = /* @__PURE__ */ new Set();
|
|
4488
4620
|
for (const dir of dirs) {
|
|
4621
|
+
if (signal.aborted) return this.accounts;
|
|
4489
4622
|
const files = await authFilesIn(dir);
|
|
4623
|
+
if (signal.aborted) return this.accounts;
|
|
4490
4624
|
if (files === void 0) continue;
|
|
4491
4625
|
readableDirs.add(resolve(dir));
|
|
4492
4626
|
for (const file of files) {
|
|
4493
4627
|
const read = await readCredential(file);
|
|
4628
|
+
if (signal.aborted) return this.accounts;
|
|
4494
4629
|
if (read.kind === "credential") found.push(read.credential);
|
|
4495
4630
|
else if (read.kind === "sealed") sealedDirs.add(resolve(dir));
|
|
4496
4631
|
}
|
|
4497
4632
|
}
|
|
4498
|
-
const tombstones = await this.loadDeletedSignIns();
|
|
4633
|
+
const tombstones = await this.loadDeletedSignIns(signal);
|
|
4634
|
+
if (signal.aborted) return this.accounts;
|
|
4499
4635
|
const tornDown = /* @__PURE__ */ new Set();
|
|
4500
4636
|
const releasedIds = [];
|
|
4501
4637
|
const held = [];
|
|
@@ -4517,9 +4653,12 @@ var WorkBuddyAccountPool = class {
|
|
|
4517
4653
|
found.length = 0;
|
|
4518
4654
|
found.push(...held);
|
|
4519
4655
|
for (const id of tombstones.keys()) tornDown.add(id);
|
|
4520
|
-
if (releasedIds.length > 0) await this.writeDeletedSignIns(releasedIds);
|
|
4521
|
-
|
|
4522
|
-
|
|
4656
|
+
if (releasedIds.length > 0) await this.writeDeletedSignIns(releasedIds, signal);
|
|
4657
|
+
if (signal.aborted) return this.accounts;
|
|
4658
|
+
await this.archiveCredentials(found, signal);
|
|
4659
|
+
if (signal.aborted) return this.accounts;
|
|
4660
|
+
found.push(...await this.adoptOrphanedSignIns(found, tornDown, signal));
|
|
4661
|
+
if (signal.aborted) return this.accounts;
|
|
4523
4662
|
const signInTokens = /* @__PURE__ */ new Map();
|
|
4524
4663
|
for (const credential of found) {
|
|
4525
4664
|
const id = workbuddyAccountId(credential);
|
|
@@ -4529,7 +4668,9 @@ var WorkBuddyAccountPool = class {
|
|
|
4529
4668
|
}
|
|
4530
4669
|
const liveIds = new Set(found.map((credential) => workbuddyAccountId(credential)));
|
|
4531
4670
|
const appHeld = new Set(found.filter((credential) => isLiveAuthFile(credential.sourcePath)).map((credential) => workbuddyAccountId(credential)));
|
|
4532
|
-
|
|
4671
|
+
const archived = await this.archivedCredentials(readableDirs);
|
|
4672
|
+
if (signal.aborted) return this.accounts;
|
|
4673
|
+
for (const credential of archived) {
|
|
4533
4674
|
const id = workbuddyAccountId(credential);
|
|
4534
4675
|
if (tornDown.has(id)) continue;
|
|
4535
4676
|
if (found.some((entry) => workbuddyAccountId(entry) === id && entry.accessToken === credential.accessToken)) continue;
|
|
@@ -4570,7 +4711,7 @@ var WorkBuddyAccountPool = class {
|
|
|
4570
4711
|
if (this.signInRefused(id, account.credential.accessToken)) continue;
|
|
4571
4712
|
this.heldIds.delete(id);
|
|
4572
4713
|
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`);
|
|
4714
|
+
this.logger?.info?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} has a sign-in the upstream did not refuse; releasing the hold`);
|
|
4574
4715
|
}
|
|
4575
4716
|
const foundIds = new Set(found.map((credential) => workbuddyAccountId(credential)));
|
|
4576
4717
|
for (const [id, refused] of this.refusedSignIns) {
|
|
@@ -4702,27 +4843,32 @@ var WorkBuddyAccountPool = class {
|
|
|
4702
4843
|
* fresh desktop login is the usual way out of an exhausted pool.
|
|
4703
4844
|
*/
|
|
4704
4845
|
async acquire(modelId, region, conversationKey, excludeIds) {
|
|
4846
|
+
const signal = this.scanAbort.signal;
|
|
4705
4847
|
const eligibleNow = () => {
|
|
4706
4848
|
const list = this.available(Date.now(), modelId, region);
|
|
4707
4849
|
return excludeIds === void 0 ? list : list.filter((item) => !excludeIds.has(item.id));
|
|
4708
4850
|
};
|
|
4709
4851
|
if (this.accounts.length === 0) await this.scan();
|
|
4852
|
+
if (signal.aborted) return void 0;
|
|
4710
4853
|
let pool = eligibleNow();
|
|
4711
4854
|
if (pool.length === 0) {
|
|
4712
4855
|
await this.scan();
|
|
4856
|
+
if (signal.aborted) return void 0;
|
|
4713
4857
|
pool = eligibleNow();
|
|
4714
4858
|
}
|
|
4715
4859
|
if (pool.length === 0) return void 0;
|
|
4716
4860
|
const preferredId = this.preferredIds.get(region);
|
|
4717
|
-
const
|
|
4718
|
-
|
|
4719
|
-
|
|
4720
|
-
|
|
4721
|
-
|
|
4722
|
-
|
|
4723
|
-
|
|
4724
|
-
|
|
4725
|
-
|
|
4861
|
+
const remaining = new Set(pool.map((account) => account.id));
|
|
4862
|
+
while (remaining.size > 0) {
|
|
4863
|
+
const account = pool.find((item) => item.id === preferredId) ?? this.pick(pool, conversationKey);
|
|
4864
|
+
if (account === void 0) return void 0;
|
|
4865
|
+
remaining.delete(account.id);
|
|
4866
|
+
await this.ensureFresh(account);
|
|
4867
|
+
if (signal.aborted) return void 0;
|
|
4868
|
+
const eligible = eligibleNow();
|
|
4869
|
+
if (eligible.includes(account)) return account;
|
|
4870
|
+
pool = eligible.filter((item) => remaining.has(item.id));
|
|
4871
|
+
}
|
|
4726
4872
|
}
|
|
4727
4873
|
pick(pool, conversationKey) {
|
|
4728
4874
|
if (this.distribution === "expiry") return this.pickByExpiry(pool);
|
|
@@ -4870,7 +5016,7 @@ var WorkBuddyAccountPool = class {
|
|
|
4870
5016
|
if (credential.expiresAtMs > Date.now() + 3e4) return "fresh";
|
|
4871
5017
|
if (!this.noRefreshWarned.has(account.id)) {
|
|
4872
5018
|
this.noRefreshWarned.add(account.id);
|
|
4873
|
-
this.logger?.warn(`${LOG_PREFIX}: ${account.label} token expired with no refresh token; sign in again`);
|
|
5019
|
+
this.logger?.warn(`${LOG_PREFIX}: ${accountLogTag(account.label)} token expired with no refresh token; sign in again`);
|
|
4874
5020
|
}
|
|
4875
5021
|
return "noToken";
|
|
4876
5022
|
}
|
|
@@ -4892,13 +5038,13 @@ var WorkBuddyAccountPool = class {
|
|
|
4892
5038
|
if (sessionHold !== void 0 && sessionHold.reason === "session") {
|
|
4893
5039
|
this.heldIds.delete(account.id);
|
|
4894
5040
|
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`);
|
|
5041
|
+
this.logger?.info?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} refreshed again; releasing the refused sign-in hold`);
|
|
4896
5042
|
}
|
|
4897
|
-
this.logger?.info?.(`${LOG_PREFIX}: refreshed token for ${account.label}`);
|
|
5043
|
+
this.logger?.info?.(`${LOG_PREFIX}: refreshed token for ${accountLogTag(account.label)}`);
|
|
4898
5044
|
return "refreshed";
|
|
4899
5045
|
} catch (error) {
|
|
4900
|
-
if (credential.expiresAtMs > Date.now() + 3e4) this.logger?.warn?.(`${LOG_PREFIX}: token refresh failed but token still valid for ${account.label}`, error);
|
|
4901
|
-
else this.logger?.error?.(`${LOG_PREFIX}: token refresh failed and token expired for ${account.label}`, error);
|
|
5046
|
+
if (credential.expiresAtMs > Date.now() + 3e4) this.logger?.warn?.(`${LOG_PREFIX}: token refresh failed but token still valid for ${accountLogTag(account.label)}`, error);
|
|
5047
|
+
else this.logger?.error?.(`${LOG_PREFIX}: token refresh failed and token expired for ${accountLogTag(account.label)}`, error);
|
|
4902
5048
|
return upstreamErrorKind(error) === "session_dead" ? "refused" : "failed";
|
|
4903
5049
|
}
|
|
4904
5050
|
})();
|
|
@@ -4941,7 +5087,7 @@ var WorkBuddyAccountPool = class {
|
|
|
4941
5087
|
if (modelId !== void 0 && modelId !== "") {
|
|
4942
5088
|
pruneModelCooldowns(account, now);
|
|
4943
5089
|
account.modelCooldowns[modelId] = Math.max(account.modelCooldowns[modelId] ?? 0, until);
|
|
4944
|
-
this.logger?.warn(`${LOG_PREFIX}: ${account.label} rate-limited on model ${modelId}; cooling that model until ${new Date(until).toISOString()}`);
|
|
5090
|
+
this.logger?.warn(`${LOG_PREFIX}: ${accountLogTag(account.label)} rate-limited on model ${modelId}; cooling that model until ${new Date(until).toISOString()}`);
|
|
4945
5091
|
return;
|
|
4946
5092
|
}
|
|
4947
5093
|
account.cooldownUntilMs = Math.max(account.cooldownUntilMs, until);
|
|
@@ -4956,7 +5102,7 @@ var WorkBuddyAccountPool = class {
|
|
|
4956
5102
|
heldSignIns
|
|
4957
5103
|
});
|
|
4958
5104
|
}
|
|
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()}`);
|
|
5105
|
+
this.logger?.warn(holdReason !== void 0 ? `${LOG_PREFIX}: account ${accountLogTag(account.label)} held (${holdReason}); held until ${new Date(until).toISOString()}` : `${LOG_PREFIX}: account ${accountLogTag(account.label)} rate-limited; cooling until ${new Date(until).toISOString()}`);
|
|
4960
5106
|
}
|
|
4961
5107
|
/**
|
|
4962
5108
|
* Keep one model out of one account's rotation for a stated window.
|
|
@@ -5201,12 +5347,17 @@ const MAX_LINE_CHARS$1 = 67108864;
|
|
|
5201
5347
|
*/
|
|
5202
5348
|
const MAX_DATA_CHARS = 4194304;
|
|
5203
5349
|
var SseDecoder = class {
|
|
5350
|
+
onFrame;
|
|
5204
5351
|
buffer = "";
|
|
5205
5352
|
event;
|
|
5206
5353
|
id;
|
|
5207
5354
|
retry;
|
|
5208
5355
|
data = [];
|
|
5209
5356
|
dataChars = 0;
|
|
5357
|
+
rawFrame = "";
|
|
5358
|
+
constructor(onFrame) {
|
|
5359
|
+
this.onFrame = onFrame;
|
|
5360
|
+
}
|
|
5210
5361
|
push(chunk) {
|
|
5211
5362
|
this.buffer += chunk;
|
|
5212
5363
|
const events = [];
|
|
@@ -5215,9 +5366,14 @@ var SseDecoder = class {
|
|
|
5215
5366
|
if (match === null || match.index === void 0) break;
|
|
5216
5367
|
if (match[0] === "\r" && match.index + 1 === this.buffer.length) break;
|
|
5217
5368
|
const line = this.buffer.slice(0, match.index);
|
|
5369
|
+
if (this.onFrame !== void 0) this.appendRaw(this.buffer.slice(0, match.index + match[0].length));
|
|
5218
5370
|
this.buffer = this.buffer.slice(match.index + match[0].length);
|
|
5219
5371
|
const emitted = this.consumeLine(line);
|
|
5220
5372
|
if (emitted !== void 0) events.push(emitted);
|
|
5373
|
+
if (line === "" && this.onFrame !== void 0) {
|
|
5374
|
+
this.onFrame(this.rawFrame, emitted);
|
|
5375
|
+
this.rawFrame = "";
|
|
5376
|
+
}
|
|
5221
5377
|
}
|
|
5222
5378
|
if (this.buffer.length > MAX_LINE_CHARS$1) {
|
|
5223
5379
|
this.buffer = "";
|
|
@@ -5234,6 +5390,7 @@ var SseDecoder = class {
|
|
|
5234
5390
|
finish() {
|
|
5235
5391
|
const events = [];
|
|
5236
5392
|
if (this.buffer !== "") {
|
|
5393
|
+
if (this.onFrame !== void 0) this.appendRaw(this.buffer);
|
|
5237
5394
|
const tail = this.buffer.endsWith("\r") ? this.buffer.slice(0, -1) : this.buffer;
|
|
5238
5395
|
const emitted = this.consumeLine(tail);
|
|
5239
5396
|
this.buffer = "";
|
|
@@ -5241,8 +5398,19 @@ var SseDecoder = class {
|
|
|
5241
5398
|
}
|
|
5242
5399
|
const final = this.dispatch();
|
|
5243
5400
|
if (final !== void 0) events.push(final);
|
|
5401
|
+
if (this.rawFrame !== "" && this.onFrame !== void 0) {
|
|
5402
|
+
this.onFrame(this.rawFrame, events[0]);
|
|
5403
|
+
this.rawFrame = "";
|
|
5404
|
+
}
|
|
5244
5405
|
return events;
|
|
5245
5406
|
}
|
|
5407
|
+
appendRaw(value) {
|
|
5408
|
+
if (this.rawFrame.length + value.length > MAX_DATA_CHARS) {
|
|
5409
|
+
this.rawFrame = "";
|
|
5410
|
+
throw new Error("sse event exceeded 4194304 characters of framing without a separator");
|
|
5411
|
+
}
|
|
5412
|
+
this.rawFrame += value;
|
|
5413
|
+
}
|
|
5246
5414
|
consumeLine(line) {
|
|
5247
5415
|
if (line === "") return this.dispatch();
|
|
5248
5416
|
if (line.startsWith(":")) return void 0;
|
|
@@ -5355,13 +5523,19 @@ function decodeTraeEvent(event) {
|
|
|
5355
5523
|
* Loopback guards, the request body bound and the server shell live in
|
|
5356
5524
|
* `./loopback` — one copy, shared with the Trae and Qoder gateways.
|
|
5357
5525
|
*/
|
|
5358
|
-
|
|
5359
|
-
function carriesFinishReason(data) {
|
|
5526
|
+
function completionState(data) {
|
|
5360
5527
|
try {
|
|
5361
5528
|
const parsed = JSON.parse(data);
|
|
5362
|
-
|
|
5529
|
+
const choices = Array.isArray(parsed.choices) ? parsed.choices : [];
|
|
5530
|
+
return {
|
|
5531
|
+
finished: choices.some((choice) => typeof choice?.finish_reason === "string" && choice.finish_reason !== ""),
|
|
5532
|
+
toolCalls: choices.some((choice) => Array.isArray(choice?.delta?.tool_calls) && choice.delta.tool_calls.length > 0)
|
|
5533
|
+
};
|
|
5363
5534
|
} catch {
|
|
5364
|
-
return
|
|
5535
|
+
return {
|
|
5536
|
+
finished: false,
|
|
5537
|
+
toolCalls: false
|
|
5538
|
+
};
|
|
5365
5539
|
}
|
|
5366
5540
|
}
|
|
5367
5541
|
/** HTTP status each upstream failure class surfaces as. */
|
|
@@ -5388,6 +5562,16 @@ const KIND_STATUS = {
|
|
|
5388
5562
|
*/
|
|
5389
5563
|
const MODEL_TIER_HOLD_MS = 864e5;
|
|
5390
5564
|
/**
|
|
5565
|
+
* How many accounts one request may lose to a transport failure.
|
|
5566
|
+
*
|
|
5567
|
+
* Rotation is still worth one retry: a global pool can hold sign-ins served by
|
|
5568
|
+
* different gateways, so one unreachable host says nothing about the next
|
|
5569
|
+
* account. A host-wide outage, however, would otherwise cost one headers
|
|
5570
|
+
* timeout per sign-in before the client saw an answer, so the second failure
|
|
5571
|
+
* ends the turn.
|
|
5572
|
+
*/
|
|
5573
|
+
const MAX_TRANSPORT_ATTEMPTS = 2;
|
|
5574
|
+
/**
|
|
5391
5575
|
* The business code an account-level 200 envelope carries.
|
|
5392
5576
|
*
|
|
5393
5577
|
* `classifyUpstreamError` only reads codes on the statuses the upstream uses
|
|
@@ -5420,6 +5604,7 @@ function conversationKeyOf(raw) {
|
|
|
5420
5604
|
return;
|
|
5421
5605
|
}
|
|
5422
5606
|
}
|
|
5607
|
+
const EMPTY_STREAM_REFUSAL = "workbuddy returned an empty answer stream";
|
|
5423
5608
|
/**
|
|
5424
5609
|
* Read enough of a 200 body to tell an SSE stream from a refusal wearing a 200.
|
|
5425
5610
|
*
|
|
@@ -5460,7 +5645,7 @@ async function peekAnswerHead(reader) {
|
|
|
5460
5645
|
if (trimmed === "" && ended) return {
|
|
5461
5646
|
head,
|
|
5462
5647
|
ended,
|
|
5463
|
-
refusal:
|
|
5648
|
+
refusal: EMPTY_STREAM_REFUSAL
|
|
5464
5649
|
};
|
|
5465
5650
|
if (!jsonLooking) return {
|
|
5466
5651
|
head,
|
|
@@ -5621,7 +5806,6 @@ function createWorkBuddyShim(options) {
|
|
|
5621
5806
|
const { pool, client, catalog } = options;
|
|
5622
5807
|
const region = options.region;
|
|
5623
5808
|
const logger = options.logger;
|
|
5624
|
-
const maxAttempts = 8;
|
|
5625
5809
|
const server = createLoopbackServer({
|
|
5626
5810
|
port: options.port,
|
|
5627
5811
|
label: "workbuddy shim",
|
|
@@ -5688,12 +5872,14 @@ function createWorkBuddyShim(options) {
|
|
|
5688
5872
|
writeOpenAIError(res, 400, "invalid_request_error", "model is required");
|
|
5689
5873
|
return;
|
|
5690
5874
|
}
|
|
5691
|
-
|
|
5875
|
+
let tried = 0;
|
|
5692
5876
|
const skipped = /* @__PURE__ */ new Set();
|
|
5693
5877
|
let last;
|
|
5694
5878
|
let exhaustedByRateLimit = false;
|
|
5879
|
+
let transportAttempts = 0;
|
|
5695
5880
|
let effortFallback;
|
|
5696
5881
|
let effortFallbackBuilt = false;
|
|
5882
|
+
const maxAttempts = Math.max(8, pool.list(region).length + 2);
|
|
5697
5883
|
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
5698
5884
|
if (controller.signal.aborted) return;
|
|
5699
5885
|
const account = await pool.acquire(modelId, region, conversationKey, skipped);
|
|
@@ -5701,14 +5887,19 @@ function createWorkBuddyShim(options) {
|
|
|
5701
5887
|
if (exhaustedByRateLimit && last !== void 0) {
|
|
5702
5888
|
const model = ` for model ${modelId}`;
|
|
5703
5889
|
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";
|
|
5705
|
-
|
|
5890
|
+
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 === "waf_block" ? "wait for the upstream gateway block to clear, or change network" : last.kind === "transport" ? "check the connection to the upstream host" : last.kind === "hard_credit" ? "top up the account, or add another account" : "sign in again in the desktop app";
|
|
5891
|
+
if (last.kind === "soft_rate") {
|
|
5892
|
+
const live = pool.list(region).filter((candidate) => !pool.isDisabled(candidate.id));
|
|
5893
|
+
const wait = unavailableAnswer(pool, live, Date.now(), modelId).retryAfterSeconds;
|
|
5894
|
+
if (wait !== void 0) res.setHeader("Retry-After", String(wait));
|
|
5895
|
+
}
|
|
5896
|
+
writeOpenAIError(res, KIND_STATUS[last.kind], last.kind, `${subject} (tried ${tried} account(s)); ${advice} — ${last.message.slice(0, 200)}`);
|
|
5706
5897
|
return;
|
|
5707
5898
|
}
|
|
5708
5899
|
const regionAccounts = pool.list(region);
|
|
5709
5900
|
const switchedOff = regionAccounts.filter((account) => pool.isDisabled(account.id));
|
|
5710
5901
|
if (regionAccounts.length > 0 && switchedOff.length === regionAccounts.length) {
|
|
5711
|
-
writeOpenAIError(res, 403, "accounts_switched_off", `every account for this gateway is switched off in the plugin panel
|
|
5902
|
+
writeOpenAIError(res, 403, "accounts_switched_off", `every account for this gateway is switched off in the plugin panel; switch one back on`);
|
|
5712
5903
|
return;
|
|
5713
5904
|
}
|
|
5714
5905
|
if (regionAccounts.length > 0) {
|
|
@@ -5722,14 +5913,14 @@ function createWorkBuddyShim(options) {
|
|
|
5722
5913
|
writeOpenAIError(res, 401, "not_signed_in", "no WorkBuddy credential found; sign in on the desktop app (or set WORKBUDDY_AUTH_FILE)");
|
|
5723
5914
|
return;
|
|
5724
5915
|
}
|
|
5725
|
-
tried
|
|
5916
|
+
tried += 1;
|
|
5726
5917
|
let result = await client.chatStream(account.credential, prepared, controller.signal);
|
|
5727
5918
|
let refreshOutcome;
|
|
5728
5919
|
if (!result.ok && result.kind === "session_dead") {
|
|
5729
5920
|
const failure = result;
|
|
5730
5921
|
refreshOutcome = await pool.refreshAccountOutcome(account.id);
|
|
5731
5922
|
if (refreshOutcome === "failed") {
|
|
5732
|
-
logger?.warn?.(`${LOG_PREFIX}: ${account.label} could not refresh its sign-in; trying the next account without holding it`);
|
|
5923
|
+
logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} could not refresh its sign-in; trying the next account without holding it`);
|
|
5733
5924
|
skipped.add(account.id);
|
|
5734
5925
|
last = {
|
|
5735
5926
|
kind: "transport",
|
|
@@ -5759,9 +5950,20 @@ function createWorkBuddyShim(options) {
|
|
|
5759
5950
|
const reader = relayStreamOf(result.response).getReader();
|
|
5760
5951
|
let sawDone = false;
|
|
5761
5952
|
let sawFinish = false;
|
|
5953
|
+
let sawToolCalls = false;
|
|
5762
5954
|
let envelopeFailure;
|
|
5955
|
+
let headFailure;
|
|
5763
5956
|
const decoder = new TextDecoder();
|
|
5764
|
-
const
|
|
5957
|
+
const frames = [];
|
|
5958
|
+
const sse = new SseDecoder((raw, event) => frames.push({
|
|
5959
|
+
raw,
|
|
5960
|
+
event
|
|
5961
|
+
}));
|
|
5962
|
+
const finishChunk = () => Buffer.from(`data: ${JSON.stringify({ choices: [{
|
|
5963
|
+
index: 0,
|
|
5964
|
+
delta: {},
|
|
5965
|
+
finish_reason: openAIFinishReason(void 0, sawToolCalls)
|
|
5966
|
+
}] })}\n\n`);
|
|
5765
5967
|
const startStream = () => {
|
|
5766
5968
|
res.writeHead(200, {
|
|
5767
5969
|
"Content-Type": "text/event-stream",
|
|
@@ -5802,13 +6004,32 @@ function createWorkBuddyShim(options) {
|
|
|
5802
6004
|
function closeWithoutDone() {
|
|
5803
6005
|
if (sawDone || !res.writable) return;
|
|
5804
6006
|
logger?.warn?.("wtq-pool: upstream stream ended without [DONE]");
|
|
5805
|
-
if (!sawFinish) res.write(
|
|
5806
|
-
index: 0,
|
|
5807
|
-
delta: {},
|
|
5808
|
-
finish_reason: "stop"
|
|
5809
|
-
}] })}\n\n`);
|
|
6007
|
+
if (!sawFinish) res.write(finishChunk());
|
|
5810
6008
|
res.end("data: [DONE]\n\n");
|
|
5811
6009
|
}
|
|
6010
|
+
const relayFrames = async (finishTail = false) => {
|
|
6011
|
+
for (const { raw, event } of frames.splice(0)) {
|
|
6012
|
+
const chunk = Buffer.from(finishTail ? raw + "\n\n" : raw);
|
|
6013
|
+
if (event?.data === "[DONE]") {
|
|
6014
|
+
if (!sawFinish) {
|
|
6015
|
+
await writeChunk(res, finishChunk());
|
|
6016
|
+
sawFinish = true;
|
|
6017
|
+
}
|
|
6018
|
+
await writeChunk(res, chunk);
|
|
6019
|
+
sawDone = true;
|
|
6020
|
+
res.end();
|
|
6021
|
+
await reader.cancel().catch(() => {});
|
|
6022
|
+
return true;
|
|
6023
|
+
}
|
|
6024
|
+
if (event !== void 0) {
|
|
6025
|
+
const state = completionState(event.data);
|
|
6026
|
+
sawFinish ||= state.finished;
|
|
6027
|
+
sawToolCalls ||= state.toolCalls;
|
|
6028
|
+
}
|
|
6029
|
+
await writeChunk(res, chunk);
|
|
6030
|
+
}
|
|
6031
|
+
return false;
|
|
6032
|
+
};
|
|
5812
6033
|
await (async () => {
|
|
5813
6034
|
let head = [];
|
|
5814
6035
|
let ended = false;
|
|
@@ -5818,7 +6039,12 @@ function createWorkBuddyShim(options) {
|
|
|
5818
6039
|
ended = peeked.ended;
|
|
5819
6040
|
if (peeked.refusal !== void 0) {
|
|
5820
6041
|
await reader.cancel().catch(() => {});
|
|
5821
|
-
|
|
6042
|
+
if (peeked.ended && peeked.refusal === EMPTY_STREAM_REFUSAL) {
|
|
6043
|
+
headFailure = peeked.refusal;
|
|
6044
|
+
logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} answered 200 with an empty stream; rotating`);
|
|
6045
|
+
return;
|
|
6046
|
+
}
|
|
6047
|
+
logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} answered 200 with no stream: ${peeked.refusal}`);
|
|
5822
6048
|
if (peeked.code === "11128" || unapprovedChannelRefusal(peeked.refusal) !== void 0) {
|
|
5823
6049
|
writeOpenAIError(res, 400, "upstream_channel_check", channelCheckRefusal());
|
|
5824
6050
|
return;
|
|
@@ -5856,11 +6082,12 @@ function createWorkBuddyShim(options) {
|
|
|
5856
6082
|
return;
|
|
5857
6083
|
}
|
|
5858
6084
|
} catch (error) {
|
|
5859
|
-
|
|
5860
|
-
|
|
6085
|
+
await reader.cancel().catch(() => {});
|
|
6086
|
+
headFailure = safeMessage(error);
|
|
6087
|
+
logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} failed before its first byte; rotating`);
|
|
5861
6088
|
return;
|
|
5862
6089
|
}
|
|
5863
|
-
logger?.info?.(`${LOG_PREFIX}: served by ${account.label}`);
|
|
6090
|
+
logger?.info?.(`${LOG_PREFIX}: served by ${accountLogTag(account.label)}`);
|
|
5864
6091
|
startStream();
|
|
5865
6092
|
let headIndex = 0;
|
|
5866
6093
|
try {
|
|
@@ -5875,33 +6102,15 @@ function createWorkBuddyShim(options) {
|
|
|
5875
6102
|
} : await reader.read();
|
|
5876
6103
|
if (done || value === void 0) break;
|
|
5877
6104
|
if (!res.writable) return;
|
|
5878
|
-
|
|
5879
|
-
|
|
5880
|
-
const decoded = decoder.decode(value, { stream: true });
|
|
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
|
-
}
|
|
5892
|
-
sawDone = true;
|
|
5893
|
-
if (res.writable) res.end();
|
|
5894
|
-
await reader.cancel().catch(() => {});
|
|
5895
|
-
return;
|
|
5896
|
-
}
|
|
5897
|
-
} catch {}
|
|
6105
|
+
sse.push(decoder.decode(value, { stream: true }));
|
|
6106
|
+
if (await relayFrames()) return;
|
|
5898
6107
|
}
|
|
5899
|
-
if (!sawDone)
|
|
6108
|
+
if (!sawDone) {
|
|
5900
6109
|
const rest = decoder.decode();
|
|
5901
|
-
|
|
5902
|
-
|
|
5903
|
-
if (
|
|
5904
|
-
}
|
|
6110
|
+
if (rest !== "") sse.push(rest);
|
|
6111
|
+
sse.finish();
|
|
6112
|
+
if (await relayFrames(true)) return;
|
|
6113
|
+
}
|
|
5905
6114
|
closeWithoutDone();
|
|
5906
6115
|
} catch (error) {
|
|
5907
6116
|
failInBand(error);
|
|
@@ -5911,6 +6120,18 @@ function createWorkBuddyShim(options) {
|
|
|
5911
6120
|
} catch {}
|
|
5912
6121
|
}
|
|
5913
6122
|
})();
|
|
6123
|
+
if (headFailure !== void 0) {
|
|
6124
|
+
last = {
|
|
6125
|
+
kind: "transport",
|
|
6126
|
+
status: 502,
|
|
6127
|
+
message: headFailure
|
|
6128
|
+
};
|
|
6129
|
+
exhaustedByRateLimit = true;
|
|
6130
|
+
skipped.add(account.id);
|
|
6131
|
+
transportAttempts += 1;
|
|
6132
|
+
if (transportAttempts >= MAX_TRANSPORT_ATTEMPTS) break;
|
|
6133
|
+
continue;
|
|
6134
|
+
}
|
|
5914
6135
|
if (envelopeFailure === void 0) return;
|
|
5915
6136
|
continue;
|
|
5916
6137
|
}
|
|
@@ -5939,10 +6160,13 @@ function createWorkBuddyShim(options) {
|
|
|
5939
6160
|
* configured window rather than a hold that would park a healthy account
|
|
5940
6161
|
* for hours over a transient block.
|
|
5941
6162
|
*
|
|
5942
|
-
* A `transport` failure is the network path's
|
|
5943
|
-
*
|
|
5944
|
-
*
|
|
5945
|
-
*
|
|
6163
|
+
* A `transport` failure is the network path's. A global pool need not
|
|
6164
|
+
* share a host: `codebuddy.ai` and `workbuddy.ai` logins can be served by
|
|
6165
|
+
* different gateways, so one unreachable host says nothing about the next
|
|
6166
|
+
* account. It is skipped for this request and rotation continues, up to
|
|
6167
|
+
* `MAX_TRANSPORT_ATTEMPTS`: past that the host itself is down, and one
|
|
6168
|
+
* headers timeout per sign-in is not a better answer.
|
|
6169
|
+
* An `aborted` turn is the client's own doing and stops the loop.
|
|
5946
6170
|
*/
|
|
5947
6171
|
if (result.kind === "aborted") break;
|
|
5948
6172
|
if (result.kind === "transport") {
|
|
@@ -5952,7 +6176,11 @@ function createWorkBuddyShim(options) {
|
|
|
5952
6176
|
message: result.message
|
|
5953
6177
|
};
|
|
5954
6178
|
exhaustedByRateLimit = true;
|
|
5955
|
-
|
|
6179
|
+
skipped.add(account.id);
|
|
6180
|
+
transportAttempts += 1;
|
|
6181
|
+
if (transportAttempts >= MAX_TRANSPORT_ATTEMPTS) break;
|
|
6182
|
+
logger?.warn?.(`${LOG_PREFIX}: ${accountLogTag(account.label)} transport failure; rotating`);
|
|
6183
|
+
continue;
|
|
5956
6184
|
}
|
|
5957
6185
|
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;
|
|
5958
6186
|
exhaustedByRateLimit = true;
|
|
@@ -5965,7 +6193,7 @@ function createWorkBuddyShim(options) {
|
|
|
5965
6193
|
else if (result.kind === "account_restricted") pool.penalize(account.id, holdUntil(), void 0, "restricted");
|
|
5966
6194
|
else if (result.kind === "hard_credit" || result.kind === "session_dead") pool.penalize(account.id, holdUntil(), void 0, result.kind === "hard_credit" ? "spent" : "session");
|
|
5967
6195
|
else pool.penalize(account.id);
|
|
5968
|
-
logger?.warn(`${LOG_PREFIX}: ${account.label} ${result.kind} on ${modelId} (attempt ${attempt + 1}/${maxAttempts}); rotating`);
|
|
6196
|
+
logger?.warn(`${LOG_PREFIX}: ${accountLogTag(account.label)} ${result.kind} on ${modelId} (attempt ${attempt + 1}/${maxAttempts}); rotating`);
|
|
5969
6197
|
}
|
|
5970
6198
|
if (last === void 0) {
|
|
5971
6199
|
writeOpenAIError(res, 500, "internal", "chat request exhausted without a result");
|
|
@@ -5980,7 +6208,7 @@ function createWorkBuddyShim(options) {
|
|
|
5980
6208
|
writeOpenAIError(res, 400, "upstream_channel_check", unapproved);
|
|
5981
6209
|
return;
|
|
5982
6210
|
}
|
|
5983
|
-
writeOpenAIError(res, KIND_STATUS[last.kind], last.kind, `workbuddy upstream ${last.kind} (http ${last.status}) after ${tried
|
|
6211
|
+
writeOpenAIError(res, KIND_STATUS[last.kind], last.kind, `workbuddy upstream ${last.kind} (http ${last.status}) after ${tried} account(s): ${last.message.slice(0, 400)}`);
|
|
5984
6212
|
}
|
|
5985
6213
|
return {
|
|
5986
6214
|
ready,
|
|
@@ -7902,7 +8130,11 @@ function bridgeTraeStream(source, model, options = {}) {
|
|
|
7902
8130
|
const decoded = decodeTraeEvent(event);
|
|
7903
8131
|
if (decoded.type === "done" || event.event === "done") upstreamDone = true;
|
|
7904
8132
|
const failure = traeFailureOf(decoded);
|
|
7905
|
-
if (failure !== void 0)
|
|
8133
|
+
if (failure !== void 0) {
|
|
8134
|
+
const refusal = new Error(describeTraeFailure(failure));
|
|
8135
|
+
refusal.traeFailure = failure;
|
|
8136
|
+
throw refusal;
|
|
8137
|
+
}
|
|
7906
8138
|
if (decoded.type === "unknown") {
|
|
7907
8139
|
keepalive(controller);
|
|
7908
8140
|
return;
|
|
@@ -8297,10 +8529,13 @@ function createTraeGateway(options) {
|
|
|
8297
8529
|
try {
|
|
8298
8530
|
relayed = await openTraeResponse(result.response, model, { onFailure: (error) => {
|
|
8299
8531
|
logger?.warn?.("wtq-pool: trae relay stream failed mid-flight", error);
|
|
8300
|
-
if (!(error instanceof Error && error.name === "AbortError") && !watcher.signal.aborted)
|
|
8301
|
-
|
|
8302
|
-
|
|
8303
|
-
|
|
8532
|
+
if (!(error instanceof Error && error.name === "AbortError") && !watcher.signal.aborted) {
|
|
8533
|
+
const failure = error.traeFailure;
|
|
8534
|
+
if (failure !== void 0) {
|
|
8535
|
+
const classified = classifyTraeFailure(failure);
|
|
8536
|
+
options.onAttemptFailure?.(credential, { ...classified.retryAfterSeconds === void 0 ? {} : { retryAfter: String(classified.retryAfterSeconds) } });
|
|
8537
|
+
} else options.onAttemptFailure?.(credential, {});
|
|
8538
|
+
}
|
|
8304
8539
|
} });
|
|
8305
8540
|
} catch (error) {
|
|
8306
8541
|
if (watcher.signal.aborted || error instanceof Error && error.name === "AbortError") return {
|
|
@@ -8359,11 +8594,7 @@ function createTraeGateway(options) {
|
|
|
8359
8594
|
}
|
|
8360
8595
|
opened = attempt;
|
|
8361
8596
|
if (!rotates(attempt.status, attempt.kind)) break;
|
|
8362
|
-
options.onAttemptFailure?.(credential, {
|
|
8363
|
-
status: attempt.status,
|
|
8364
|
-
...attempt.kind === void 0 ? {} : { kind: attempt.kind },
|
|
8365
|
-
...attempt.retryAfter === void 0 ? {} : { retryAfter: attempt.retryAfter }
|
|
8366
|
-
});
|
|
8597
|
+
options.onAttemptFailure?.(credential, { ...attempt.retryAfter === void 0 ? {} : { retryAfter: attempt.retryAfter } });
|
|
8367
8598
|
if (index < credentials.length - 1) logger?.warn?.(`${LOG_PREFIX}: trae sign-in failed on ${model} (http ${attempt.status}); rotating`);
|
|
8368
8599
|
}
|
|
8369
8600
|
if (opened === void 0 || !opened.ok) {
|
|
@@ -9413,13 +9644,16 @@ function qoderWindowOpenAt(campaign, nowMs) {
|
|
|
9413
9644
|
function qoderClaimableCampaignOf(payload, nowMs) {
|
|
9414
9645
|
const list = payload?.campaigns;
|
|
9415
9646
|
if (!Array.isArray(list)) return void 0;
|
|
9647
|
+
let collected;
|
|
9416
9648
|
for (const entry of list) {
|
|
9417
9649
|
if (entry === null || typeof entry !== "object") continue;
|
|
9418
9650
|
const campaign = entry;
|
|
9419
9651
|
if (campaign["actionType"] !== QODER_CLAIM_BENEFIT_ACTION) continue;
|
|
9420
9652
|
if (!qoderWindowOpenAt(campaign, nowMs)) continue;
|
|
9421
|
-
return campaign;
|
|
9653
|
+
if (campaign["claimStatus"] !== QODER_CLAIMED_STATUS) return campaign;
|
|
9654
|
+
if (collected === void 0) collected = campaign;
|
|
9422
9655
|
}
|
|
9656
|
+
return collected;
|
|
9423
9657
|
}
|
|
9424
9658
|
/**
|
|
9425
9659
|
* Id of today's round when it can be claimed right now, else undefined. Kept
|
|
@@ -10134,6 +10368,7 @@ function isAcknowledgementCode(value) {
|
|
|
10134
10368
|
function unwrapFailure(frame) {
|
|
10135
10369
|
let code = "";
|
|
10136
10370
|
let detail = "";
|
|
10371
|
+
const signals = [];
|
|
10137
10372
|
let node = frame;
|
|
10138
10373
|
let descended = false;
|
|
10139
10374
|
let codeFromPlainKey = false;
|
|
@@ -10156,8 +10391,10 @@ function unwrapFailure(frame) {
|
|
|
10156
10391
|
}
|
|
10157
10392
|
}
|
|
10158
10393
|
const message = node["message"] ?? node["errorMessage"];
|
|
10159
|
-
if (typeof message === "string" && message.
|
|
10394
|
+
if (typeof message === "string" && message.trim() !== "" && !signals.includes(message)) signals.push(message);
|
|
10395
|
+
if ((detail === "" || tryJsonObject(detail) !== void 0) && typeof message === "string" && message.length > 0) detail = message;
|
|
10160
10396
|
const flat = typeof node["error"] === "string" ? node["error"] : typeof node["details"] === "string" ? node["details"] : void 0;
|
|
10397
|
+
if (flat !== void 0 && flat.trim() !== "" && !signals.includes(flat)) signals.push(flat);
|
|
10161
10398
|
if (detail === "" && flat !== void 0 && flat.length > 0) detail = flat;
|
|
10162
10399
|
const nested = isPlainObject(node["details"]) ? node["details"] : isPlainObject(node["error"]) ? node["error"] : void 0;
|
|
10163
10400
|
const next = nested ?? (typeof message === "string" ? tryJsonObject(message) : void 0);
|
|
@@ -10170,25 +10407,32 @@ function unwrapFailure(frame) {
|
|
|
10170
10407
|
if (codeFromPlainKey && isAcknowledgementCode(code)) code = "";
|
|
10171
10408
|
return {
|
|
10172
10409
|
code,
|
|
10173
|
-
detail
|
|
10410
|
+
detail,
|
|
10411
|
+
signals: signals.join("\n")
|
|
10174
10412
|
};
|
|
10175
10413
|
}
|
|
10176
10414
|
/**
|
|
10177
10415
|
* The gateway's retry hint, when the frame carries one.
|
|
10178
10416
|
*
|
|
10179
|
-
* A carried `0` is the decoder's "no hint" placeholder, not a real wait
|
|
10180
|
-
*
|
|
10181
|
-
*
|
|
10182
|
-
*
|
|
10417
|
+
* A carried `0` is the decoder's "no hint" placeholder, not a real wait. The
|
|
10418
|
+
* hint may sit on the envelope, in a nested container, or inside a message that
|
|
10419
|
+
* is itself a JSON document; the walk reads all three so a queue descriptor is
|
|
10420
|
+
* not lost when the outer sentence is the one worth showing.
|
|
10183
10421
|
*/
|
|
10184
10422
|
function retryAfterSecondsOf(frame) {
|
|
10185
|
-
|
|
10186
|
-
|
|
10187
|
-
|
|
10188
|
-
|
|
10189
|
-
|
|
10190
|
-
|
|
10191
|
-
|
|
10423
|
+
const seen = /* @__PURE__ */ new Set();
|
|
10424
|
+
let node = frame;
|
|
10425
|
+
for (let depth = 0; depth < 8; depth++) {
|
|
10426
|
+
if (!isPlainObject(node) || seen.has(node)) break;
|
|
10427
|
+
seen.add(node);
|
|
10428
|
+
const direct = node["retryAfterSeconds"];
|
|
10429
|
+
if (typeof direct === "number" && Number.isFinite(direct) && direct > 0) return direct;
|
|
10430
|
+
const message = node["message"] ?? node["errorMessage"];
|
|
10431
|
+
const next = (isPlainObject(node["details"]) ? node["details"] : isPlainObject(node["error"]) ? node["error"] : void 0) ?? (typeof message === "string" ? tryJsonObject(message) : void 0);
|
|
10432
|
+
if (next === void 0) break;
|
|
10433
|
+
node = next;
|
|
10434
|
+
}
|
|
10435
|
+
return 0;
|
|
10192
10436
|
}
|
|
10193
10437
|
/**
|
|
10194
10438
|
* The inner chunk of one data payload: the envelope's `body` when it is a JSON
|
|
@@ -10226,7 +10470,7 @@ function decodeQoderFrame(payload) {
|
|
|
10226
10470
|
type: "chunk",
|
|
10227
10471
|
chunk
|
|
10228
10472
|
};
|
|
10229
|
-
const { code, detail } = unwrapFailure(chunk);
|
|
10473
|
+
const { code, detail, signals } = unwrapFailure(chunk);
|
|
10230
10474
|
if (code === "" && detail === "") return {
|
|
10231
10475
|
type: "unknown",
|
|
10232
10476
|
reason: "unnamed",
|
|
@@ -10236,6 +10480,7 @@ function decodeQoderFrame(payload) {
|
|
|
10236
10480
|
type: "failure",
|
|
10237
10481
|
code,
|
|
10238
10482
|
detail,
|
|
10483
|
+
signals,
|
|
10239
10484
|
retryAfterSeconds: retryAfterSecondsOf(chunk)
|
|
10240
10485
|
};
|
|
10241
10486
|
}
|
|
@@ -10315,6 +10560,16 @@ var QoderFrameDecoder = class {
|
|
|
10315
10560
|
* frame. A vendor that does close its stream wins this race and pays nothing.
|
|
10316
10561
|
*/
|
|
10317
10562
|
const TAIL_GRACE_MS = 1e3;
|
|
10563
|
+
/**
|
|
10564
|
+
* How long a peek that has seen a frame carrying no output waits for a refusal
|
|
10565
|
+
* that may follow it.
|
|
10566
|
+
*
|
|
10567
|
+
* Qoder opens a turn with a role-only delta. Committing on that frame hid a
|
|
10568
|
+
* refusal in the very next one; waiting for output instead kept the 200 from the
|
|
10569
|
+
* client for the whole thinking phase. The grace is enough for a refusal that
|
|
10570
|
+
* arrives promptly, and short enough that a slow model still gets its headers.
|
|
10571
|
+
*/
|
|
10572
|
+
const PEEK_OUTPUT_GRACE_MS = 1e3;
|
|
10318
10573
|
/** Retryable queue wait the gateway answers with 429 + Retry-After. */
|
|
10319
10574
|
var QoderQueueRejection = class extends Error {
|
|
10320
10575
|
retryAfterSeconds;
|
|
@@ -10394,14 +10649,16 @@ function queueSeconds(frame) {
|
|
|
10394
10649
|
*/
|
|
10395
10650
|
function classifyQoderFailure(failure) {
|
|
10396
10651
|
const code = failure.code.trim();
|
|
10397
|
-
const text = `${code} ${failure.message}`;
|
|
10652
|
+
const text = `${code} ${failure.message} ${failure.signals ?? ""}`;
|
|
10653
|
+
const frame = {
|
|
10654
|
+
type: "failure",
|
|
10655
|
+
code,
|
|
10656
|
+
detail: failure.message,
|
|
10657
|
+
signals: failure.signals ?? "",
|
|
10658
|
+
retryAfterSeconds: failure.retryAfterSeconds ?? 0
|
|
10659
|
+
};
|
|
10398
10660
|
if (code === "110" || /daily.*(limit|quota)|quota.*daily|allowance.*spent/i.test(text)) {
|
|
10399
|
-
const retryAfter = queueSeconds(
|
|
10400
|
-
type: "failure",
|
|
10401
|
-
code,
|
|
10402
|
-
detail: failure.message,
|
|
10403
|
-
retryAfterSeconds: failure.retryAfterSeconds ?? 0
|
|
10404
|
-
});
|
|
10661
|
+
const retryAfter = queueSeconds(frame);
|
|
10405
10662
|
return {
|
|
10406
10663
|
status: 429,
|
|
10407
10664
|
kind: "hard_credit",
|
|
@@ -10411,12 +10668,7 @@ function classifyQoderFailure(failure) {
|
|
|
10411
10668
|
if (code === "10605") return {
|
|
10412
10669
|
status: 429,
|
|
10413
10670
|
kind: "soft_rate",
|
|
10414
|
-
error: new QoderQueueRejection(queueSeconds(
|
|
10415
|
-
type: "failure",
|
|
10416
|
-
code,
|
|
10417
|
-
detail: failure.message,
|
|
10418
|
-
retryAfterSeconds: failure.retryAfterSeconds ?? 0
|
|
10419
|
-
}), failure.message)
|
|
10671
|
+
error: new QoderQueueRejection(queueSeconds(frame), failure.message)
|
|
10420
10672
|
};
|
|
10421
10673
|
if (/Login expired|TOKEN_EXPIRE|token is not active/i.test(text) || code === "105") return {
|
|
10422
10674
|
status: 401,
|
|
@@ -10428,15 +10680,10 @@ function classifyQoderFailure(failure) {
|
|
|
10428
10680
|
kind: "client",
|
|
10429
10681
|
error: new Error(failure.message)
|
|
10430
10682
|
};
|
|
10431
|
-
if (queueMarkerCount(
|
|
10683
|
+
if (queueMarkerCount(text) >= 2) return {
|
|
10432
10684
|
status: 429,
|
|
10433
10685
|
kind: "soft_rate",
|
|
10434
|
-
error: new QoderQueueRejection(queueSeconds(
|
|
10435
|
-
type: "failure",
|
|
10436
|
-
code,
|
|
10437
|
-
detail: failure.message,
|
|
10438
|
-
retryAfterSeconds: failure.retryAfterSeconds ?? 0
|
|
10439
|
-
}), failure.message)
|
|
10686
|
+
error: new QoderQueueRejection(queueSeconds(frame), failure.message)
|
|
10440
10687
|
};
|
|
10441
10688
|
if (code === "429") return {
|
|
10442
10689
|
status: 429,
|
|
@@ -10591,6 +10838,7 @@ function bridgeQoderStream(source, model, options = {}) {
|
|
|
10591
10838
|
const { error } = classifyQoderFailure({
|
|
10592
10839
|
code: decoded.code,
|
|
10593
10840
|
message: decoded.detail,
|
|
10841
|
+
signals: decoded.signals,
|
|
10594
10842
|
retryAfterSeconds: decoded.retryAfterSeconds
|
|
10595
10843
|
});
|
|
10596
10844
|
throw error;
|
|
@@ -10753,6 +11001,45 @@ function bridgeQoderStream(source, model, options = {}) {
|
|
|
10753
11001
|
});
|
|
10754
11002
|
}
|
|
10755
11003
|
/**
|
|
11004
|
+
* Whether a Qoder chunk carries output a client would actually see.
|
|
11005
|
+
*
|
|
11006
|
+
* A role-only delta and a usage-only frame both have `choices`, but neither is
|
|
11007
|
+
* output: a refusal that follows them can still become a real status code and
|
|
11008
|
+
* let the next account try. Counting any non-empty `choices` as output committed
|
|
11009
|
+
* the stream at 200 and skipped failover.
|
|
11010
|
+
*/
|
|
11011
|
+
function qoderChunkHasOutput(chunk) {
|
|
11012
|
+
const choices = chunk["choices"];
|
|
11013
|
+
if (!Array.isArray(choices)) return false;
|
|
11014
|
+
for (const raw of choices) {
|
|
11015
|
+
if (raw === null || typeof raw !== "object") continue;
|
|
11016
|
+
const delta = raw["delta"];
|
|
11017
|
+
if (delta === null || typeof delta !== "object") continue;
|
|
11018
|
+
const fields = delta;
|
|
11019
|
+
if (typeof fields["content"] === "string" && fields["content"] !== "") return true;
|
|
11020
|
+
if (typeof fields["reasoning_content"] === "string" && fields["reasoning_content"] !== "") return true;
|
|
11021
|
+
const calls = fields["tool_calls"];
|
|
11022
|
+
if (Array.isArray(calls) && calls.length > 0) return true;
|
|
11023
|
+
}
|
|
11024
|
+
return false;
|
|
11025
|
+
}
|
|
11026
|
+
/**
|
|
11027
|
+
* Whether a Qoder chunk closes the turn, even if the socket stays open.
|
|
11028
|
+
*
|
|
11029
|
+
* Qoder reports a completed turn with a `finish_reason` and often a trailing
|
|
11030
|
+
* usage frame, then leaves the connection open. The peek must stop there or it
|
|
11031
|
+
* waits for the upstream idle watchdog on a turn that has already finished.
|
|
11032
|
+
*/
|
|
11033
|
+
function qoderChunkIsTerminal(chunk) {
|
|
11034
|
+
const choices = chunk["choices"];
|
|
11035
|
+
if (Array.isArray(choices)) for (const raw of choices) {
|
|
11036
|
+
if (raw === null || typeof raw !== "object") continue;
|
|
11037
|
+
const finish = raw["finish_reason"];
|
|
11038
|
+
if (finish !== void 0 && finish !== null) return true;
|
|
11039
|
+
}
|
|
11040
|
+
return chunk["usage"] !== void 0 && chunk["usage"] !== null;
|
|
11041
|
+
}
|
|
11042
|
+
/**
|
|
10756
11043
|
* Turn one gateway response into something an OpenAI client can consume.
|
|
10757
11044
|
*
|
|
10758
11045
|
* A non-2xx answer becomes a JSON error, and so does a 2xx with no body at all.
|
|
@@ -10763,7 +11050,7 @@ function bridgeQoderStream(source, model, options = {}) {
|
|
|
10763
11050
|
*/
|
|
10764
11051
|
async function openQoderResponse(response, model, options = {}) {
|
|
10765
11052
|
if (!response.ok) {
|
|
10766
|
-
const detail =
|
|
11053
|
+
const detail = await readErrorBody(response, 1024);
|
|
10767
11054
|
return Response.json({ error: {
|
|
10768
11055
|
message: detail === "" ? `Qoder returned HTTP ${response.status}` : detail,
|
|
10769
11056
|
type: "upstream_error",
|
|
@@ -10781,26 +11068,52 @@ async function openQoderResponse(response, model, options = {}) {
|
|
|
10781
11068
|
const buffered = [];
|
|
10782
11069
|
let bufferedBytes = 0;
|
|
10783
11070
|
let sawOutput = false;
|
|
11071
|
+
let sawTerminal = false;
|
|
11072
|
+
let sawQuiet = false;
|
|
11073
|
+
let graceDeadline = 0;
|
|
11074
|
+
let pendingRead;
|
|
10784
11075
|
let failure;
|
|
10785
11076
|
let ended = false;
|
|
10786
11077
|
const inspect = (payloads) => {
|
|
10787
11078
|
for (const payload of payloads) {
|
|
10788
11079
|
const decoded = decodeQoderFrame(payload);
|
|
10789
11080
|
if (decoded.type === "chunk") {
|
|
10790
|
-
const
|
|
10791
|
-
if (
|
|
10792
|
-
|
|
11081
|
+
const chunk = decoded.chunk;
|
|
11082
|
+
if (qoderChunkHasOutput(chunk)) sawOutput = true;
|
|
11083
|
+
if (qoderChunkIsTerminal(chunk)) sawTerminal = true;
|
|
11084
|
+
if (!sawOutput && !sawTerminal && Array.isArray(chunk["choices"]) && chunk["choices"].length > 0) sawQuiet = true;
|
|
10793
11085
|
} else if (decoded.type === "failure") failure ??= {
|
|
10794
11086
|
code: decoded.code,
|
|
10795
11087
|
message: decoded.detail,
|
|
11088
|
+
signals: decoded.signals,
|
|
10796
11089
|
retryAfterSeconds: decoded.retryAfterSeconds
|
|
10797
11090
|
};
|
|
10798
|
-
if (sawOutput || failure !== void 0) break;
|
|
11091
|
+
if (sawOutput || sawTerminal || failure !== void 0) break;
|
|
10799
11092
|
}
|
|
10800
11093
|
};
|
|
10801
11094
|
try {
|
|
10802
11095
|
while (buffered.length < 64 && bufferedBytes < 262144) {
|
|
10803
|
-
|
|
11096
|
+
let next;
|
|
11097
|
+
if (graceDeadline === 0) next = await reader.read();
|
|
11098
|
+
else {
|
|
11099
|
+
const read = reader.read();
|
|
11100
|
+
pendingRead = read;
|
|
11101
|
+
read.catch(() => {});
|
|
11102
|
+
const remaining = Math.max(0, graceDeadline - Date.now());
|
|
11103
|
+
let timer;
|
|
11104
|
+
const timedOut = new Promise((resolve) => {
|
|
11105
|
+
timer = setTimeout(() => resolve(void 0), remaining);
|
|
11106
|
+
});
|
|
11107
|
+
let winner;
|
|
11108
|
+
try {
|
|
11109
|
+
winner = await Promise.race([read.then((result) => ({ result })), timedOut]);
|
|
11110
|
+
} finally {
|
|
11111
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
11112
|
+
}
|
|
11113
|
+
if (winner === void 0) break;
|
|
11114
|
+
next = winner.result;
|
|
11115
|
+
pendingRead = void 0;
|
|
11116
|
+
}
|
|
10804
11117
|
if (next.done) {
|
|
10805
11118
|
ended = true;
|
|
10806
11119
|
const rest = decoder.decode();
|
|
@@ -10810,9 +11123,11 @@ async function openQoderResponse(response, model, options = {}) {
|
|
|
10810
11123
|
buffered.push(next.value);
|
|
10811
11124
|
bufferedBytes += next.value.byteLength;
|
|
10812
11125
|
inspect(frames.push(decoder.decode(next.value, { stream: true })));
|
|
10813
|
-
if (sawOutput || failure !== void 0 || frames.ended) break;
|
|
11126
|
+
if (sawOutput || sawTerminal || failure !== void 0 || frames.ended) break;
|
|
11127
|
+
if (sawQuiet && graceDeadline === 0) graceDeadline = Date.now() + PEEK_OUTPUT_GRACE_MS;
|
|
10814
11128
|
}
|
|
10815
11129
|
} catch (error) {
|
|
11130
|
+
pendingRead = void 0;
|
|
10816
11131
|
await reader.cancel().catch(() => {});
|
|
10817
11132
|
throw error;
|
|
10818
11133
|
}
|
|
@@ -10831,7 +11146,19 @@ async function openQoderResponse(response, model, options = {}) {
|
|
|
10831
11146
|
...wait === void 0 ? {} : { headers: { "Retry-After": wait } }
|
|
10832
11147
|
});
|
|
10833
11148
|
}
|
|
10834
|
-
const relay = bridgeQoderStream(replaySource(buffered,
|
|
11149
|
+
const relay = bridgeQoderStream(replaySource(buffered, {
|
|
11150
|
+
async read() {
|
|
11151
|
+
const queued = pendingRead;
|
|
11152
|
+
if (queued !== void 0) {
|
|
11153
|
+
pendingRead = void 0;
|
|
11154
|
+
return queued;
|
|
11155
|
+
}
|
|
11156
|
+
return reader.read();
|
|
11157
|
+
},
|
|
11158
|
+
cancel(reason) {
|
|
11159
|
+
return reader.cancel(reason);
|
|
11160
|
+
}
|
|
11161
|
+
}, ended), model, options);
|
|
10835
11162
|
return withRelayStream(new Response(relay, {
|
|
10836
11163
|
status: 200,
|
|
10837
11164
|
headers: { "Content-Type": "text/event-stream" }
|
|
@@ -10983,10 +11310,10 @@ function createQoderGateway(options) {
|
|
|
10983
11310
|
try {
|
|
10984
11311
|
relayed = await openQoderResponse(result.response, model, { onFailure: (error) => {
|
|
10985
11312
|
logger?.warn?.("wtq-pool: qoder relay stream failed mid-flight", error);
|
|
10986
|
-
if (!(error instanceof Error && error.name === "AbortError") && !watcher.signal.aborted)
|
|
10987
|
-
|
|
10988
|
-
|
|
10989
|
-
}
|
|
11313
|
+
if (!(error instanceof Error && error.name === "AbortError") && !watcher.signal.aborted) {
|
|
11314
|
+
const seconds = error.retryAfterSeconds;
|
|
11315
|
+
options.onAttemptFailure?.(credential, { ...typeof seconds === "number" && seconds > 0 ? { retryAfter: String(seconds) } : {} });
|
|
11316
|
+
}
|
|
10990
11317
|
} });
|
|
10991
11318
|
} catch (error) {
|
|
10992
11319
|
if (watcher.signal.aborted || error instanceof Error && error.name === "AbortError") return {
|
|
@@ -11039,11 +11366,7 @@ function createQoderGateway(options) {
|
|
|
11039
11366
|
}
|
|
11040
11367
|
opened = attempt;
|
|
11041
11368
|
if (!rotates(attempt.status, attempt.kind)) break;
|
|
11042
|
-
options.onAttemptFailure?.(credential, {
|
|
11043
|
-
status: attempt.status,
|
|
11044
|
-
...attempt.kind === void 0 ? {} : { kind: attempt.kind },
|
|
11045
|
-
...attempt.retryAfter === void 0 ? {} : { retryAfter: attempt.retryAfter }
|
|
11046
|
-
});
|
|
11369
|
+
options.onAttemptFailure?.(credential, { ...attempt.retryAfter === void 0 ? {} : { retryAfter: attempt.retryAfter } });
|
|
11047
11370
|
if (index < credentials.length - 1) logger?.warn?.(`${LOG_PREFIX}: qoder sign-in failed on ${model} (http ${attempt.status}); rotating`);
|
|
11048
11371
|
}
|
|
11049
11372
|
if (opened === void 0 || !opened.ok) {
|
|
@@ -11170,6 +11493,23 @@ function baseFor(realm) {
|
|
|
11170
11493
|
function originFor(realm) {
|
|
11171
11494
|
return realm === "global" ? GLOBAL_ORIGIN : CN_ORIGIN;
|
|
11172
11495
|
}
|
|
11496
|
+
/**
|
|
11497
|
+
* The browser URL a login start hands the panel.
|
|
11498
|
+
*
|
|
11499
|
+
* The panel opens it without inspecting it, so the scheme is checked here: an
|
|
11500
|
+
* upstream answer that named `javascript:` or `file:` would otherwise be handed
|
|
11501
|
+
* straight to the desktop shell.
|
|
11502
|
+
*/
|
|
11503
|
+
function checkedAuthUrl(value) {
|
|
11504
|
+
let parsed;
|
|
11505
|
+
try {
|
|
11506
|
+
parsed = new URL(value);
|
|
11507
|
+
} catch {
|
|
11508
|
+
throw new Error("workbuddy login start returned an invalid url");
|
|
11509
|
+
}
|
|
11510
|
+
if (parsed.protocol !== "https:") throw new Error("workbuddy login start returned a non-https url");
|
|
11511
|
+
return value;
|
|
11512
|
+
}
|
|
11173
11513
|
/** Browser-shaped headers; `Origin` is the one the endpoints check. */
|
|
11174
11514
|
function loginHeaders(origin) {
|
|
11175
11515
|
return {
|
|
@@ -11224,7 +11564,7 @@ async function startDeviceLogin(realm, fetchImpl = fetch) {
|
|
|
11224
11564
|
if (state === "" || authUrl === "") throw new Error(`workbuddy login start gave no state or url: ${envelope.msg}`);
|
|
11225
11565
|
return {
|
|
11226
11566
|
state,
|
|
11227
|
-
authUrl
|
|
11567
|
+
authUrl: checkedAuthUrl(authUrl)
|
|
11228
11568
|
};
|
|
11229
11569
|
}
|
|
11230
11570
|
/**
|
|
@@ -11911,7 +12251,8 @@ const STREAK_REPORT_READ_GAP_MS = 1200;
|
|
|
11911
12251
|
const travelAdoptTried = /* @__PURE__ */ new Map();
|
|
11912
12252
|
function log(level, message) {
|
|
11913
12253
|
try {
|
|
11914
|
-
|
|
12254
|
+
const text = redactSecrets(String(message));
|
|
12255
|
+
console.log("[wtq-pool] " + level + ": " + text);
|
|
11915
12256
|
} catch {}
|
|
11916
12257
|
}
|
|
11917
12258
|
/**
|
|
@@ -13471,7 +13812,11 @@ function refreshNeeded() {
|
|
|
13471
13812
|
*/
|
|
13472
13813
|
function cooldownPending() {
|
|
13473
13814
|
const now = Date.now();
|
|
13474
|
-
for (const account of core.pool.list())
|
|
13815
|
+
for (const account of core.pool.list()) {
|
|
13816
|
+
if (account.cooldownUntilMs <= now) continue;
|
|
13817
|
+
if (core.pool.isDisabled(account.id) || !regionEnabled("workbuddy", regionOf(account.credential.domain))) continue;
|
|
13818
|
+
return true;
|
|
13819
|
+
}
|
|
13475
13820
|
return false;
|
|
13476
13821
|
}
|
|
13477
13822
|
/** True while any pool account's cached billing answers are missing or old. */
|
|
@@ -13562,7 +13907,7 @@ async function refreshAll(reason, force) {
|
|
|
13562
13907
|
await adoptHostSettings();
|
|
13563
13908
|
if (!stillLoaded(generation)) return stalePass();
|
|
13564
13909
|
applySettingsToCore();
|
|
13565
|
-
if (poolNeedsScan) {
|
|
13910
|
+
if (poolNeedsScan && activeRegions().length > 0) {
|
|
13566
13911
|
poolNeedsScan = false;
|
|
13567
13912
|
await settle(core.pool.scan());
|
|
13568
13913
|
if (!stillLoaded(generation)) return stalePass();
|
|
@@ -14797,7 +15142,7 @@ async function onPanelInvoke(channel, payload) {
|
|
|
14797
15142
|
resetDiscoveryCaches();
|
|
14798
15143
|
growthRead = null;
|
|
14799
15144
|
growthReadEpoch += 1;
|
|
14800
|
-
if (activeRegions().length > 0) await settle(core.pool.
|
|
15145
|
+
if (activeRegions().length > 0) await settle(core.pool.rescan());
|
|
14801
15146
|
await settle(startRuntimes());
|
|
14802
15147
|
await settle(refreshSourceAccounts());
|
|
14803
15148
|
await settle(reconcileTraeGateways());
|
|
@@ -15054,6 +15399,7 @@ async function onPanelInvoke(channel, payload) {
|
|
|
15054
15399
|
checkinPassRunning = true;
|
|
15055
15400
|
const answered = await settleWithin((async () => {
|
|
15056
15401
|
const outcome = source === "trae" ? await traeClaimOne(label, region) : await qoderClaimOne(label, region);
|
|
15402
|
+
markSourceCheckinRow(source, label, region, outcome);
|
|
15057
15403
|
return {
|
|
15058
15404
|
state: await statusDocument({
|
|
15059
15405
|
allowNetwork: true,
|
|
@@ -15248,6 +15594,7 @@ async function onPanelInvoke(channel, payload) {
|
|
|
15248
15594
|
throw panelError("the login link has expired; start again", "LOGIN_EXPIRED");
|
|
15249
15595
|
}
|
|
15250
15596
|
const answer = await pollDeviceLogin(realm, pending.state);
|
|
15597
|
+
if (deviceLogins.get(realm) !== pending) throw panelError("this login is no longer waiting; start again", "NO_LOGIN");
|
|
15251
15598
|
if (answer.status !== "ok") return { status: "pending" };
|
|
15252
15599
|
deviceLogins.delete(realm);
|
|
15253
15600
|
const directory = ownSignInDir(dataPath);
|
|
@@ -15480,7 +15827,7 @@ async function runCheckinPass(options) {
|
|
|
15480
15827
|
if (want("qoder")) {
|
|
15481
15828
|
const now = Date.now();
|
|
15482
15829
|
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;
|
|
15830
|
+
const backoff = Number(qoder?.failed) > 0 && qoder?.retry !== false && (typeof qoder?.reason === "string" || qoder?.retry === true) ? now + QODER_RETRY_BACKOFF_MS : 0;
|
|
15484
15831
|
qoderRetryAt = opening === 0 ? backoff : backoff === 0 ? opening : Math.min(opening, backoff);
|
|
15485
15832
|
}
|
|
15486
15833
|
Object.assign(started, {
|
|
@@ -15575,68 +15922,79 @@ async function redeemAccountStreak(account) {
|
|
|
15575
15922
|
lottery: 0,
|
|
15576
15923
|
failed: 0
|
|
15577
15924
|
};
|
|
15925
|
+
const result = {
|
|
15926
|
+
claimed: 0,
|
|
15927
|
+
credit: 0,
|
|
15928
|
+
energy: 0,
|
|
15929
|
+
detail
|
|
15930
|
+
};
|
|
15931
|
+
const signal = currentCheckinSignal();
|
|
15932
|
+
if (signal.aborted) return result;
|
|
15578
15933
|
const conversationId = "wb2api-streak-" + Date.now();
|
|
15579
15934
|
try {
|
|
15580
|
-
|
|
15935
|
+
const cards = await core.client.makeupCards(account.credential);
|
|
15936
|
+
if (signal.aborted) return result;
|
|
15937
|
+
if (cards > 0 && await core.client.missedYesterday(account.credential)) {
|
|
15938
|
+
if (signal.aborted) return result;
|
|
15581
15939
|
const yesterday = zonedDay(-1);
|
|
15582
15940
|
await core.client.useMakeupCard(account.credential, yesterday);
|
|
15583
15941
|
detail.makeup += 1;
|
|
15584
|
-
log("info", "streak " + account.label + ": make-up card spent on " + yesterday);
|
|
15942
|
+
log("info", "streak " + accountLogTag(account.label) + ": make-up card spent on " + yesterday);
|
|
15585
15943
|
}
|
|
15586
15944
|
} catch (error) {
|
|
15587
15945
|
detail.failed += 1;
|
|
15588
|
-
log("warn", "streak " + account.label + ": make-up: " + safeMessage(error));
|
|
15946
|
+
log("warn", "streak " + accountLogTag(account.label) + ": make-up: " + safeMessage(error));
|
|
15589
15947
|
}
|
|
15590
|
-
for (const [label, collect] of [["gift", () => core.client.claimGift(account.credential)], ["compensation", () => core.client.claimCompensation(account.credential)]])
|
|
15591
|
-
|
|
15592
|
-
|
|
15593
|
-
|
|
15594
|
-
|
|
15595
|
-
|
|
15596
|
-
|
|
15948
|
+
for (const [label, collect] of [["gift", () => core.client.claimGift(account.credential)], ["compensation", () => core.client.claimCompensation(account.credential)]]) {
|
|
15949
|
+
if (signal.aborted) return result;
|
|
15950
|
+
try {
|
|
15951
|
+
const paid = await collect();
|
|
15952
|
+
if (paid > 0) {
|
|
15953
|
+
detail[label] += paid;
|
|
15954
|
+
log("info", "streak " + accountLogTag(account.label) + ": " + label + " +" + paid + "c");
|
|
15955
|
+
}
|
|
15956
|
+
} catch {}
|
|
15957
|
+
}
|
|
15958
|
+
if (signal.aborted) return result;
|
|
15597
15959
|
try {
|
|
15598
15960
|
await core.client.reportChatActivity(account.credential, conversationId);
|
|
15599
15961
|
detail.activity += 1;
|
|
15600
15962
|
} catch (error) {
|
|
15601
15963
|
detail.failed += 1;
|
|
15602
|
-
log("warn", "streak " + account.label + ": activity report failed: " + safeMessage(error));
|
|
15964
|
+
log("warn", "streak " + accountLogTag(account.label) + ": activity report failed: " + safeMessage(error));
|
|
15603
15965
|
}
|
|
15604
15966
|
await checkinDelay(STREAK_REPORT_READ_GAP_MS);
|
|
15967
|
+
if (signal.aborted) return result;
|
|
15605
15968
|
const status = await core.client.growthStreakFull(account.credential);
|
|
15606
|
-
let claimed = 0;
|
|
15607
|
-
let credit = 0;
|
|
15608
|
-
let energy = 0;
|
|
15609
15969
|
for (const tier of status.tiers) {
|
|
15610
15970
|
if (tier.status !== "unlocked") continue;
|
|
15971
|
+
if (signal.aborted) return result;
|
|
15611
15972
|
try {
|
|
15612
15973
|
await core.client.redeemStreakTier(account.credential, tier.tier);
|
|
15613
|
-
claimed += 1;
|
|
15614
|
-
credit += tier.credit;
|
|
15615
|
-
energy += tier.energy;
|
|
15974
|
+
result.claimed += 1;
|
|
15975
|
+
result.credit += tier.credit;
|
|
15976
|
+
result.energy += tier.energy;
|
|
15616
15977
|
detail.streak += 1;
|
|
15617
|
-
log("info", "streak " + account.label + ": redeemed " + tier.tier + " (+" + tier.credit + "c +" + tier.energy + "e)");
|
|
15978
|
+
log("info", "streak " + accountLogTag(account.label) + ": redeemed " + tier.tier + " (+" + tier.credit + "c +" + tier.energy + "e)");
|
|
15618
15979
|
} catch (error) {
|
|
15619
15980
|
detail.failed += 1;
|
|
15620
|
-
log("warn", "streak " + account.label + ": redeem " + tier.tier + " failed: " + safeMessage(error));
|
|
15981
|
+
log("warn", "streak " + accountLogTag(account.label) + ": redeem " + tier.tier + " failed: " + safeMessage(error));
|
|
15621
15982
|
}
|
|
15622
15983
|
}
|
|
15984
|
+
if (signal.aborted) return result;
|
|
15623
15985
|
try {
|
|
15624
15986
|
const chances = await core.client.lotteryChances(account.credential);
|
|
15625
15987
|
for (let draw = 0; draw < chances; draw += 1) {
|
|
15988
|
+
if (signal.aborted) return result;
|
|
15626
15989
|
const prize = await core.client.drawLottery(account.credential);
|
|
15627
15990
|
detail.lottery += 1;
|
|
15628
|
-
log("info", "streak " + account.label + ": draw " + (draw + 1) + "/" + chances + " " + JSON.stringify(prize).slice(0, 160));
|
|
15991
|
+
log("info", "streak " + accountLogTag(account.label) + ": draw " + (draw + 1) + "/" + chances + " " + JSON.stringify(prize).slice(0, 160));
|
|
15629
15992
|
}
|
|
15630
15993
|
} catch (error) {
|
|
15631
15994
|
detail.failed += 1;
|
|
15632
|
-
log("warn", "streak " + account.label + ": lottery: " + safeMessage(error));
|
|
15995
|
+
log("warn", "streak " + accountLogTag(account.label) + ": lottery: " + safeMessage(error));
|
|
15633
15996
|
}
|
|
15634
|
-
return
|
|
15635
|
-
claimed,
|
|
15636
|
-
credit,
|
|
15637
|
-
energy,
|
|
15638
|
-
detail
|
|
15639
|
-
};
|
|
15997
|
+
return result;
|
|
15640
15998
|
}
|
|
15641
15999
|
/**
|
|
15642
16000
|
* Collect the streak bonus of every account.
|
|
@@ -15670,7 +16028,7 @@ async function claimAllGrowth() {
|
|
|
15670
16028
|
} catch (error) {
|
|
15671
16029
|
counts.failed += 1;
|
|
15672
16030
|
counts.detail.failed += 1;
|
|
15673
|
-
log("warn", "streak failed for " + account.label + ": " + safeMessage(error));
|
|
16031
|
+
log("warn", "streak failed for " + accountLogTag(account.label) + ": " + safeMessage(error));
|
|
15674
16032
|
}
|
|
15675
16033
|
if (collected) probeCache.delete(account.id);
|
|
15676
16034
|
}
|
|
@@ -15723,7 +16081,7 @@ async function readGrowthState() {
|
|
|
15723
16081
|
chances
|
|
15724
16082
|
};
|
|
15725
16083
|
} catch (error) {
|
|
15726
|
-
log("warn", "growth state failed for " + account.label + ": " + safeMessage(error));
|
|
16084
|
+
log("warn", "growth state failed for " + accountLogTag(account.label) + ": " + safeMessage(error));
|
|
15727
16085
|
return {
|
|
15728
16086
|
account,
|
|
15729
16087
|
error
|
|
@@ -15806,7 +16164,7 @@ async function claimAllCheckins() {
|
|
|
15806
16164
|
continue;
|
|
15807
16165
|
}
|
|
15808
16166
|
counts.failed += 1;
|
|
15809
|
-
log("warn", "check-in failed for " + account.label + ": " + safeMessage(error));
|
|
16167
|
+
log("warn", "check-in failed for " + accountLogTag(account.label) + ": " + safeMessage(error));
|
|
15810
16168
|
}
|
|
15811
16169
|
}
|
|
15812
16170
|
return counts;
|
|
@@ -15833,13 +16191,13 @@ async function travelAdopt(account) {
|
|
|
15833
16191
|
try {
|
|
15834
16192
|
await core.client.reportChatActivity(account.credential, conversationId);
|
|
15835
16193
|
} catch (error) {
|
|
15836
|
-
log("warn", "travel " + account.label + ": adopt preflight report: " + safeMessage(error));
|
|
16194
|
+
log("warn", "travel " + accountLogTag(account.label) + ": adopt preflight report: " + safeMessage(error));
|
|
15837
16195
|
}
|
|
15838
16196
|
await checkinDelay(TRAVEL_ADOPT_GAP_MS);
|
|
15839
16197
|
try {
|
|
15840
16198
|
await core.client.agreeBuddy(account.credential);
|
|
15841
16199
|
} catch (error) {
|
|
15842
|
-
log("warn", "travel " + account.label + ": agreement: " + safeMessage(error));
|
|
16200
|
+
log("warn", "travel " + accountLogTag(account.label) + ": agreement: " + safeMessage(error));
|
|
15843
16201
|
return "failed";
|
|
15844
16202
|
}
|
|
15845
16203
|
try {
|
|
@@ -15847,13 +16205,13 @@ async function travelAdopt(account) {
|
|
|
15847
16205
|
} catch (error) {
|
|
15848
16206
|
if (isBuddyThresholdIncomplete(safeMessage(error))) {
|
|
15849
16207
|
travelAdoptTried.set(account.id, day);
|
|
15850
|
-
log("info", "travel " + account.label + ": adopt skipped (conversation threshold not reached, retry tomorrow)");
|
|
16208
|
+
log("info", "travel " + accountLogTag(account.label) + ": adopt skipped (conversation threshold not reached, retry tomorrow)");
|
|
15851
16209
|
return "skipped";
|
|
15852
16210
|
}
|
|
15853
|
-
log("warn", "travel " + account.label + ": adopt: " + safeMessage(error));
|
|
16211
|
+
log("warn", "travel " + accountLogTag(account.label) + ": adopt: " + safeMessage(error));
|
|
15854
16212
|
return "failed";
|
|
15855
16213
|
}
|
|
15856
|
-
log("info", "travel " + account.label + ": adopt ok");
|
|
16214
|
+
log("info", "travel " + accountLogTag(account.label) + ": adopt ok");
|
|
15857
16215
|
return "adopted";
|
|
15858
16216
|
}
|
|
15859
16217
|
/**
|
|
@@ -15867,7 +16225,7 @@ async function travelOne(account) {
|
|
|
15867
16225
|
try {
|
|
15868
16226
|
buddy = await core.client.fetchBuddyInfo(account.credential);
|
|
15869
16227
|
} catch (error) {
|
|
15870
|
-
log("warn", "travel " + account.label + ": buddy-info: " + safeMessage(error));
|
|
16228
|
+
log("warn", "travel " + accountLogTag(account.label) + ": buddy-info: " + safeMessage(error));
|
|
15871
16229
|
return "failed";
|
|
15872
16230
|
}
|
|
15873
16231
|
if (buddy === null) return await travelAdopt(account);
|
|
@@ -15875,42 +16233,42 @@ async function travelOne(account) {
|
|
|
15875
16233
|
try {
|
|
15876
16234
|
status = await core.client.fetchTravelStatus(account.credential);
|
|
15877
16235
|
} catch (error) {
|
|
15878
|
-
log("warn", "travel " + account.label + ": status: " + safeMessage(error));
|
|
16236
|
+
log("warn", "travel " + accountLogTag(account.label) + ": status: " + safeMessage(error));
|
|
15879
16237
|
return "failed";
|
|
15880
16238
|
}
|
|
15881
16239
|
if (status.state === "arrived") {
|
|
15882
16240
|
if (status.recordId === 0) {
|
|
15883
|
-
log("info", "travel " + account.label + ": claim skipped (arrived but no record_id)");
|
|
16241
|
+
log("info", "travel " + accountLogTag(account.label) + ": claim skipped (arrived but no record_id)");
|
|
15884
16242
|
return "skipped";
|
|
15885
16243
|
}
|
|
15886
16244
|
try {
|
|
15887
16245
|
const reward = await core.client.claimTravelReward(account.credential, status.recordId);
|
|
15888
|
-
log("info", "travel " + account.label + ": claim ok record=" + status.recordId + " reward=" + reward);
|
|
16246
|
+
log("info", "travel " + accountLogTag(account.label) + ": claim ok record=" + status.recordId + " reward=" + reward);
|
|
15889
16247
|
return "claimed";
|
|
15890
16248
|
} catch (error) {
|
|
15891
|
-
log("warn", "travel " + account.label + ": claim record=" + status.recordId + ": " + safeMessage(error));
|
|
16249
|
+
log("warn", "travel " + accountLogTag(account.label) + ": claim record=" + status.recordId + ": " + safeMessage(error));
|
|
15892
16250
|
return "failed";
|
|
15893
16251
|
}
|
|
15894
16252
|
}
|
|
15895
16253
|
if (status.state === "idle") {
|
|
15896
16254
|
if (status.dailyLimitReached) {
|
|
15897
|
-
log("info", "travel " + account.label + ": skip (daily limit reached)");
|
|
16255
|
+
log("info", "travel " + accountLogTag(account.label) + ": skip (daily limit reached)");
|
|
15898
16256
|
return "skipped";
|
|
15899
16257
|
}
|
|
15900
16258
|
try {
|
|
15901
16259
|
await core.client.departTravel(account.credential, 4);
|
|
15902
|
-
log("info", "travel " + account.label + ": depart ok location=4");
|
|
16260
|
+
log("info", "travel " + accountLogTag(account.label) + ": depart ok location=4");
|
|
15903
16261
|
return "departed";
|
|
15904
16262
|
} catch (error) {
|
|
15905
|
-
log("warn", "travel " + account.label + ": depart: " + safeMessage(error));
|
|
16263
|
+
log("warn", "travel " + accountLogTag(account.label) + ": depart: " + safeMessage(error));
|
|
15906
16264
|
return "failed";
|
|
15907
16265
|
}
|
|
15908
16266
|
}
|
|
15909
16267
|
if (status.state === "traveling") {
|
|
15910
|
-
log("info", "travel " + account.label + ": skip (traveling record=" + status.recordId + ")");
|
|
16268
|
+
log("info", "travel " + accountLogTag(account.label) + ": skip (traveling record=" + status.recordId + ")");
|
|
15911
16269
|
return "skipped";
|
|
15912
16270
|
}
|
|
15913
|
-
log("info", "travel " + account.label + ": skip (unknown state " + JSON.stringify(status.state) + ")");
|
|
16271
|
+
log("info", "travel " + accountLogTag(account.label) + ": skip (unknown state " + JSON.stringify(status.state) + ")");
|
|
15914
16272
|
return "skipped";
|
|
15915
16273
|
}
|
|
15916
16274
|
/**
|
|
@@ -15942,7 +16300,7 @@ async function travelAllAccounts() {
|
|
|
15942
16300
|
} else if (outcome === "failed") counts.failed += 1;
|
|
15943
16301
|
} catch (error) {
|
|
15944
16302
|
counts.failed += 1;
|
|
15945
|
-
log("warn", "travel failed for " + account.label + ": " + safeMessage(error));
|
|
16303
|
+
log("warn", "travel failed for " + accountLogTag(account.label) + ": " + safeMessage(error));
|
|
15946
16304
|
}
|
|
15947
16305
|
}
|
|
15948
16306
|
return counts;
|
|
@@ -15971,7 +16329,8 @@ async function qoderCheckinAll() {
|
|
|
15971
16329
|
return {
|
|
15972
16330
|
...counts,
|
|
15973
16331
|
failed: 1,
|
|
15974
|
-
reason
|
|
16332
|
+
reason,
|
|
16333
|
+
retry: true
|
|
15975
16334
|
};
|
|
15976
16335
|
}
|
|
15977
16336
|
if (found.failures.length > 0) {
|
|
@@ -15986,6 +16345,8 @@ async function qoderCheckinAll() {
|
|
|
15986
16345
|
seen += 1;
|
|
15987
16346
|
const result = await qoderClaimFor(credential);
|
|
15988
16347
|
counts[result.outcome] += 1;
|
|
16348
|
+
if (result.outcome === "failed" && counts.reason === void 0 && result.reason) counts.reason = safeMessage(result.reason);
|
|
16349
|
+
markSourceCheckinRow("qoder", qoderRowLabel(credential), credential.region, result);
|
|
15989
16350
|
if (result.retry === true) counts.retry = true;
|
|
15990
16351
|
if (result.opensAt !== void 0 && (counts.opensAt === void 0 || result.opensAt < counts.opensAt)) counts.opensAt = result.opensAt;
|
|
15991
16352
|
}
|
|
@@ -15998,6 +16359,14 @@ async function qoderCheckinAll() {
|
|
|
15998
16359
|
* that already collected) costs a single GET and no mutation.
|
|
15999
16360
|
*/
|
|
16000
16361
|
async function qoderClaimFor(credential) {
|
|
16362
|
+
if (sourceCredentialExpired(credential, Date.now())) {
|
|
16363
|
+
const reason = "token expired (open Qoder to refresh it)";
|
|
16364
|
+
log("warn", "[qoder:" + credential.region + "] check-in skipped for " + accountLogTag(qoderRowLabel(credential)) + ": token expired (open Qoder to refresh it)");
|
|
16365
|
+
return {
|
|
16366
|
+
outcome: "skipped",
|
|
16367
|
+
reason
|
|
16368
|
+
};
|
|
16369
|
+
}
|
|
16001
16370
|
const client = new QoderUpstreamClient({
|
|
16002
16371
|
region: credential.region,
|
|
16003
16372
|
credential: async () => qoderClientCredential(credential)
|
|
@@ -16009,22 +16378,24 @@ async function qoderClaimFor(credential) {
|
|
|
16009
16378
|
const state = qoderCheckinStateFrom(campaigns);
|
|
16010
16379
|
return {
|
|
16011
16380
|
outcome: "skipped",
|
|
16381
|
+
...state.todayCheckedIn === true ? { todayCheckedIn: true } : {},
|
|
16012
16382
|
...state.opensAt === void 0 ? {} : { opensAt: state.opensAt }
|
|
16013
16383
|
};
|
|
16014
16384
|
}
|
|
16015
16385
|
const result = qoderNormalizeClaimResult(await client.claimCampaign(id));
|
|
16016
16386
|
if (result.reason !== void 0) {
|
|
16017
|
-
|
|
16387
|
+
const reason = safeMessage(result.reason);
|
|
16388
|
+
log("warn", "[qoder:" + credential.region + "] check-in refused for " + accountLogTag(qoderRowLabel(credential)) + ": " + reason);
|
|
16018
16389
|
return {
|
|
16019
16390
|
outcome: "failed",
|
|
16020
|
-
reason
|
|
16391
|
+
reason,
|
|
16021
16392
|
retry: false
|
|
16022
16393
|
};
|
|
16023
16394
|
}
|
|
16024
|
-
return { outcome: result.claimed
|
|
16395
|
+
return { outcome: result.claimed ? "claimed" : "skipped" };
|
|
16025
16396
|
} catch (error) {
|
|
16026
16397
|
const reason = safeMessage(error);
|
|
16027
|
-
log("warn", "[qoder:" + credential.region + "] check-in failed for " + qoderRowLabel(credential) + ": " + reason);
|
|
16398
|
+
log("warn", "[qoder:" + credential.region + "] check-in failed for " + accountLogTag(qoderRowLabel(credential)) + ": " + reason);
|
|
16028
16399
|
return {
|
|
16029
16400
|
outcome: "failed",
|
|
16030
16401
|
reason,
|
|
@@ -16066,16 +16437,25 @@ async function traeCheckinAll() {
|
|
|
16066
16437
|
};
|
|
16067
16438
|
}
|
|
16068
16439
|
const cnCredentials = found.credentials.filter((credential) => traeRegionOfCredential(credential) === "cn");
|
|
16440
|
+
const spentEditions = /* @__PURE__ */ new Set();
|
|
16069
16441
|
let index = 0;
|
|
16070
16442
|
for (; index < cnCredentials.length; index += 1) {
|
|
16071
16443
|
if (currentCheckinSignal().aborted) break;
|
|
16444
|
+
const credential = cnCredentials[index];
|
|
16445
|
+
if (spentEditions.has(credential.edition)) {
|
|
16446
|
+
markSourceCheckinRow("trae", traeRowLabel(credential), "cn", {
|
|
16447
|
+
outcome: "skipped",
|
|
16448
|
+
deviceCheckedIn: true
|
|
16449
|
+
});
|
|
16450
|
+
counts.skipped += 1;
|
|
16451
|
+
continue;
|
|
16452
|
+
}
|
|
16072
16453
|
if (index > 0) await checkinDelay(Math.max(1e3, TRAE_ACCOUNT_DELAY_MS + Math.round((Math.random() * 2 - 1) * TRAE_ACCOUNT_JITTER_MS)));
|
|
16073
|
-
const result = await traeClaimFor(
|
|
16454
|
+
const result = await traeClaimFor(credential, desktopCandidates);
|
|
16074
16455
|
counts[result.outcome] += 1;
|
|
16075
|
-
if (result.
|
|
16076
|
-
|
|
16077
|
-
|
|
16078
|
-
}
|
|
16456
|
+
if (result.outcome === "failed" && counts.reason === void 0 && result.reason) counts.reason = safeMessage(result.reason);
|
|
16457
|
+
markSourceCheckinRow("trae", traeRowLabel(credential), "cn", result);
|
|
16458
|
+
if (result.deviceCheckedIn === true) spentEditions.add(credential.edition);
|
|
16079
16459
|
}
|
|
16080
16460
|
return counts;
|
|
16081
16461
|
}
|
|
@@ -16132,7 +16512,7 @@ async function traeClaimFor(credential, desktopCandidates) {
|
|
|
16132
16512
|
const label = traeRowLabel(credential);
|
|
16133
16513
|
if (sourceCredentialExpired(credential, Date.now())) {
|
|
16134
16514
|
const reason = "token expired (open Trae to refresh it)";
|
|
16135
|
-
log("warn", "[trae:cn] check-in skipped for " + label + ": token expired (open Trae to refresh it)");
|
|
16515
|
+
log("warn", "[trae:cn] check-in skipped for " + accountLogTag(label) + ": token expired (open Trae to refresh it)");
|
|
16136
16516
|
return {
|
|
16137
16517
|
outcome: "skipped",
|
|
16138
16518
|
reason
|
|
@@ -16145,7 +16525,10 @@ async function traeClaimFor(credential, desktopCandidates) {
|
|
|
16145
16525
|
try {
|
|
16146
16526
|
const status = await client.fetchCheckinStatus();
|
|
16147
16527
|
if (status.code === 0 && status.enable === false) return { outcome: "skipped" };
|
|
16148
|
-
if (status.code === 0 && status.checkedIn) return {
|
|
16528
|
+
if (status.code === 0 && status.checkedIn) return {
|
|
16529
|
+
outcome: "skipped",
|
|
16530
|
+
todayCheckedIn: true
|
|
16531
|
+
};
|
|
16149
16532
|
if (status.code === 0 && status.didCheckedIn) return {
|
|
16150
16533
|
outcome: "skipped",
|
|
16151
16534
|
deviceCheckedIn: true
|
|
@@ -16153,27 +16536,45 @@ async function traeClaimFor(credential, desktopCandidates) {
|
|
|
16153
16536
|
const claim = status.code === 0 ? await client.claimCheckin() : status;
|
|
16154
16537
|
if (claim.code === 0) return { outcome: "claimed" };
|
|
16155
16538
|
if (claim.code === 9095) {
|
|
16156
|
-
log("info", "[trae:cn] check-in already used by this device for " + label + ": " + safeMessage(claim.message ?? ""));
|
|
16539
|
+
log("info", "[trae:cn] check-in already used by this device for " + accountLogTag(label) + ": " + safeMessage(claim.message ?? ""));
|
|
16157
16540
|
return {
|
|
16158
16541
|
outcome: "skipped",
|
|
16159
16542
|
deviceCheckedIn: true
|
|
16160
16543
|
};
|
|
16161
16544
|
}
|
|
16162
16545
|
const reason = await traeCheckinReason("code " + claim.code + (claim.message === void 0 ? "" : " " + safeMessage(claim.message)), credential, desktopCandidates);
|
|
16163
|
-
log("warn", "[trae:cn] check-in refused for " + label + ": " + reason);
|
|
16546
|
+
log("warn", "[trae:cn] check-in refused for " + accountLogTag(label) + ": " + reason);
|
|
16164
16547
|
return {
|
|
16165
16548
|
outcome: "failed",
|
|
16166
16549
|
reason
|
|
16167
16550
|
};
|
|
16168
16551
|
} catch (error) {
|
|
16169
16552
|
const reason = await traeCheckinReason(safeMessage(error), credential, desktopCandidates);
|
|
16170
|
-
log("warn", "[trae:cn] check-in failed for " + label + ": " + reason);
|
|
16553
|
+
log("warn", "[trae:cn] check-in failed for " + accountLogTag(label) + ": " + reason);
|
|
16171
16554
|
return {
|
|
16172
16555
|
outcome: "failed",
|
|
16173
16556
|
reason
|
|
16174
16557
|
};
|
|
16175
16558
|
}
|
|
16176
16559
|
}
|
|
16560
|
+
/** Record a single claim in the cached source row the panel reads back. */
|
|
16561
|
+
function markSourceCheckinRow(source, label, region, outcome) {
|
|
16562
|
+
const checkedToday = outcome?.outcome === "claimed" || outcome?.todayCheckedIn === true;
|
|
16563
|
+
if (!checkedToday && outcome?.deviceCheckedIn !== true) return;
|
|
16564
|
+
const row = (source === "trae" ? traeAccountRows : qoderAccountRows).find((entry) => entry.label === label && entry.region === region);
|
|
16565
|
+
if (row === void 0) return;
|
|
16566
|
+
row.checkin = {
|
|
16567
|
+
...row.checkin ?? {
|
|
16568
|
+
active: true,
|
|
16569
|
+
todayCheckedIn: false
|
|
16570
|
+
},
|
|
16571
|
+
...checkedToday ? {
|
|
16572
|
+
active: true,
|
|
16573
|
+
todayCheckedIn: true
|
|
16574
|
+
} : {},
|
|
16575
|
+
...outcome.deviceCheckedIn === true ? { deviceCheckedIn: true } : {}
|
|
16576
|
+
};
|
|
16577
|
+
}
|
|
16177
16578
|
/** The one Trae account a panel row names, or a refusal that says why. */
|
|
16178
16579
|
async function traeClaimOne(label, region) {
|
|
16179
16580
|
if (region !== "cn") throw panelError("Trae check-in is CN-only", "INVALID");
|
|
@@ -16219,7 +16620,7 @@ async function autoCheckinTick(now) {
|
|
|
16219
16620
|
if (!stillLoaded(retryGeneration)) return autoCheckinLast;
|
|
16220
16621
|
const now = Date.now();
|
|
16221
16622
|
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;
|
|
16623
|
+
const backoff = Number(retry.failed) > 0 && retry.retry !== false && (typeof retry.reason === "string" || retry.retry === true) ? now + QODER_RETRY_BACKOFF_MS : 0;
|
|
16223
16624
|
qoderRetryAt = opening === 0 ? backoff : backoff === 0 ? opening : Math.min(opening, backoff);
|
|
16224
16625
|
const claimedBefore = Number(autoCheckinLast?.qoder?.claimed) || 0;
|
|
16225
16626
|
autoCheckinLast = {
|
|
@@ -17262,7 +17663,7 @@ function apply(ctx, config = {}) {
|
|
|
17262
17663
|
try {
|
|
17263
17664
|
if (typeof settingsService?.configure === "function") ctx.effect(() => settingsService.configure?.({ auto: true }, ctx.fiber) ?? (() => {}));
|
|
17264
17665
|
} catch (error) {
|
|
17265
|
-
ctx.logger.warn?.("dsh-wtq-pool: settings section unavailable", error);
|
|
17666
|
+
ctx.logger.warn?.("dsh-wtq-pool: settings section unavailable", safeMessage(error));
|
|
17266
17667
|
}
|
|
17267
17668
|
ctx.effect(() => {
|
|
17268
17669
|
disposeDiscovery?.();
|