dsh-cliproxyapi-pro 1.8.0 → 1.9.1
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 +38 -409
- package/README.md +38 -409
- package/lib/client.js +131 -28
- package/lib/host/accounts.js +176 -0
- package/lib/host/apply/agent-tool.js +406 -0
- package/lib/host/apply/commands.js +122 -0
- package/lib/host/apply/controller.js +105 -0
- package/lib/host/apply/http-routes.js +273 -0
- package/lib/host/apply/image-jobs.js +171 -0
- package/lib/host/apply/index.js +41 -0
- package/lib/host/apply/interpret.js +72 -0
- package/lib/host/apply/lifecycle.js +34 -0
- package/lib/host/apply/media-failures.js +68 -0
- package/lib/host/apply/media-service.js +202 -0
- package/lib/host/apply/quota-service.js +42 -0
- package/lib/host/apply/references.js +43 -0
- package/lib/host/apply/session-status.js +104 -0
- package/lib/host/apply/state.js +78 -0
- package/lib/host/apply/updates.js +55 -0
- package/lib/host/apply/video-command.js +152 -0
- package/lib/host/apply/video-edit.js +253 -0
- package/lib/host/apply/video-jobs.js +206 -0
- package/lib/host/apply-utils.js +39 -0
- package/lib/host/config.js +205 -0
- package/lib/host/controller.js +435 -0
- package/lib/host/discovery.js +295 -0
- package/lib/host/http-gate.js +144 -0
- package/lib/host/models.js +102 -0
- package/lib/host/mount-guard.js +56 -0
- package/lib/host/routes.js +165 -0
- package/lib/host/runtime.js +445 -0
- package/lib/host/state-store.js +39 -0
- package/lib/host/status-text.js +93 -0
- package/lib/index.js +17 -4330
- package/lib/media/agent-tool.js +255 -0
- package/lib/media/choice.js +83 -0
- package/lib/media/classify.js +65 -0
- package/lib/media/codec/fit.js +76 -0
- package/lib/media/codec/jpeg.js +158 -0
- package/lib/media/codec/png.js +102 -0
- package/lib/media/commands/parsing.js +269 -0
- package/lib/media/constants.js +46 -0
- package/lib/media/diagnose.js +134 -0
- package/lib/media/images.js +165 -0
- package/lib/media/index.js +29 -0
- package/lib/media/interpret.js +126 -0
- package/lib/media/job-store.js +99 -0
- package/lib/media/mp4.js +44 -0
- package/lib/media/output-files.js +59 -0
- package/lib/media/session-store.js +293 -0
- package/lib/media/settings.js +255 -0
- package/lib/media/util.js +45 -0
- package/lib/media/video.js +326 -0
- package/lib/quota/auth-state.js +33 -0
- package/lib/quota/constants.js +86 -0
- package/lib/quota/controller.js +467 -0
- package/lib/quota/helpers.js +325 -0
- package/lib/quota/index.js +19 -0
- package/lib/quota/management.js +216 -0
- package/lib/quota/matching.js +143 -0
- package/lib/quota/parsers.js +352 -0
- package/lib/quota/probes/billing.js +80 -0
- package/lib/quota/probes/google.js +120 -0
- package/lib/quota/probes/oauth.js +27 -0
- package/lib/quota/probes/plugin.js +18 -0
- package/lib/updater/constants.js +23 -0
- package/lib/updater/errors.js +8 -0
- package/lib/updater/index.js +11 -0
- package/lib/updater/registry.js +83 -0
- package/lib/updater/settings.js +45 -0
- package/lib/updater/store.js +23 -0
- package/lib/updater/updater.js +148 -0
- package/lib/updater/verify.js +110 -0
- package/lib/updater/version.js +31 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +6 -9
- package/lib/media.js +0 -2528
- package/lib/quota.js +0 -1646
- package/lib/updater.js +0 -443
package/lib/index.js
CHANGED
|
@@ -1,4336 +1,23 @@
|
|
|
1
1
|
// Zero-external-dependency host plugin for DSH CLIProxyAPI auto-discovery, model selection & sync.
|
|
2
2
|
// Uses only Node.js built-ins and Cordis context services (`ctx.settings`, `ctx.credentials`, `ctx.commands`, `ctx.webServer`).
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// Image / video models (lib/media
|
|
3
|
+
// Accounts are managed in the Settings page, HTTP routes and slash commands;
|
|
4
|
+
// the only agent tool, generate_media, is off by default (lib/host/apply/agent-tool.js).
|
|
5
|
+
// Image / video models (lib/media/) are classified and kept out of the
|
|
6
6
|
// chat routes; their preferences live in media-settings.json.
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from "./
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
createMediaJobStore, downloadVideo, MAX_VIDEO_BYTES, parseVideoCommand, retrieveVideo, submitVideo, VIDEO_ASPECTS, VIDEO_JOB_TTL_MS,
|
|
23
|
-
fitReferenceImage, REFERENCE_MAX_EDGE,
|
|
24
|
-
EDIT_SOURCE_MAX_SECONDS, EXTEND_SOURCE_SECONDS, parseVideoEditCommand, submitVideoEdit, VIDEO_EDIT_MODEL,
|
|
25
|
-
isMp4, mp4Duration, videoImageBudget, VIDEO_UPLOAD_MAX_BYTES,
|
|
26
|
-
interpretationLabels, interpretationRequest, parseInterpretation,
|
|
27
|
-
agentPolicy, applySessionPolicy, approvalText, chargeSeconds, GENERATE_MEDIA, GENERATE_MEDIA_OUTPUT, GENERATE_MEDIA_TOOL, parseToolArgs,
|
|
28
|
-
sniffImageType, toolCommandLine, toolRunId,
|
|
29
|
-
} from "./media.js";
|
|
30
|
-
|
|
31
|
-
// #region Constants & Config Resolution
|
|
32
|
-
|
|
33
|
-
export const PACKAGE_NAME = "dsh-cliproxyapi-pro";
|
|
34
|
-
export const name = "dsh-cliproxyapi-pro";
|
|
35
|
-
export const inject = ["settings"];
|
|
36
|
-
|
|
37
|
-
export const DEFAULT_ACCOUNT_ID = "default";
|
|
38
|
-
export const DEFAULT_PROVIDER = "cli-proxy";
|
|
39
|
-
export const DEFAULT_DISPLAY_NAME = "CLI Proxy API";
|
|
40
|
-
export const DEFAULT_API_KEY_ENV = "CLI_PROXY_API_KEY";
|
|
41
|
-
export const STATE_VERSION = 2;
|
|
42
|
-
const ACCOUNT_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
43
|
-
/** Per-account settings; everything else in the state file is global. */
|
|
44
|
-
export const ACCOUNT_FIELDS = [
|
|
45
|
-
"enabled",
|
|
46
|
-
"provider",
|
|
47
|
-
"displayName",
|
|
48
|
-
"baseURL",
|
|
49
|
-
"apiKeyEnv",
|
|
50
|
-
"protocol",
|
|
51
|
-
"protocolMode",
|
|
52
|
-
"includePrefix",
|
|
53
|
-
"excludePrefix",
|
|
54
|
-
"selectedModels",
|
|
55
|
-
"modelOverrides",
|
|
56
|
-
];
|
|
57
|
-
const GLOBAL_FIELDS = ["refreshIntervalSeconds", "probeTimeoutMs"];
|
|
58
|
-
export const DEFAULT_REFRESH_INTERVAL_SECONDS = 300;
|
|
59
|
-
export const DEFAULT_PROBE_TIMEOUT_MS = 5000;
|
|
60
|
-
|
|
61
|
-
export const LOCAL_CANDIDATES = [
|
|
62
|
-
"http://127.0.0.1:8317",
|
|
63
|
-
"http://localhost:8317",
|
|
64
|
-
"http://[::1]:8317",
|
|
65
|
-
];
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Most models {@link recommendModels} picks for an account that has no saved
|
|
69
|
-
* selection yet: one per model family, so the picker starts small.
|
|
70
|
-
*/
|
|
71
|
-
export const MAX_RECOMMENDED_MODELS = 12;
|
|
72
|
-
|
|
73
|
-
const MAX_CATALOG_BYTES = 8 * 1024 * 1024;
|
|
74
|
-
const PI_AI_NS = "llm-pi-ai";
|
|
75
|
-
const REASONING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"];
|
|
76
|
-
/** Wire protocols the DSH llm-pi-ai adapter accepts on a hand-declared route. */
|
|
77
|
-
export const PROTOCOLS = ["openai-completions", "openai-responses", "anthropic-messages"];
|
|
78
|
-
/** Route-key suffix and display-name label of each protocol's route. */
|
|
79
|
-
const PROTOCOL_ROUTES = {
|
|
80
|
-
"openai-completions": { suffix: "", label: "" },
|
|
81
|
-
"openai-responses": { suffix: "-responses", label: " (OpenAI Responses)" },
|
|
82
|
-
"anthropic-messages": { suffix: "-messages", label: " (Claude Messages)" },
|
|
83
|
-
};
|
|
84
|
-
const DEFAULT_MODEL_NS = "agent-default-model";
|
|
85
|
-
const REF_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
86
|
-
|
|
87
|
-
function toCredentialRef(value) {
|
|
88
|
-
const trimmed = String(value ?? "").trim();
|
|
89
|
-
if (!REF_PATTERN.test(trimmed)) {
|
|
90
|
-
throw new TypeError(`credential ref "${trimmed}" must match ${String(REF_PATTERN)}`);
|
|
91
|
-
}
|
|
92
|
-
return trimmed;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function getEnvValue(ctx, varName) {
|
|
96
|
-
try {
|
|
97
|
-
const launchEnv = ctx.get?.("launchEnvironment");
|
|
98
|
-
if (launchEnv && typeof launchEnv.get === "function") {
|
|
99
|
-
const hit = launchEnv.get(varName);
|
|
100
|
-
if (hit && typeof hit.value === "string" && hit.value.length > 0) {
|
|
101
|
-
return hit.value;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
} catch {}
|
|
105
|
-
const raw = process.env[varName];
|
|
106
|
-
return typeof raw === "string" && raw.length > 0 ? raw : undefined;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function readVolatile(field, fallback) {
|
|
110
|
-
if (field !== undefined && field !== null && typeof field.get === "function") {
|
|
111
|
-
const val = field.get();
|
|
112
|
-
return val === undefined || val === null ? fallback : val;
|
|
113
|
-
}
|
|
114
|
-
return field === undefined || field === null ? fallback : field;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function normalizeStringList(value) {
|
|
118
|
-
if (!Array.isArray(value)) return undefined;
|
|
119
|
-
const seen = new Set();
|
|
120
|
-
const out = [];
|
|
121
|
-
for (const item of value) {
|
|
122
|
-
if (typeof item !== "string") continue;
|
|
123
|
-
const trimmed = item.trim();
|
|
124
|
-
if (trimmed !== "" && !seen.has(trimmed)) {
|
|
125
|
-
seen.add(trimmed);
|
|
126
|
-
out.push(trimmed);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return out;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const OVERRIDE_LIMITS = {
|
|
133
|
-
contextWindow: 10_000_000,
|
|
134
|
-
maxTokens: 2_000_000,
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
function normalizePositiveInteger(value, max) {
|
|
138
|
-
if (typeof value === "string" && value.trim() !== "") {
|
|
139
|
-
if (!/^\d+$/.test(value.trim())) return undefined;
|
|
140
|
-
value = Number(value.trim());
|
|
141
|
-
}
|
|
142
|
-
return typeof value === "number" &&
|
|
143
|
-
Number.isInteger(value) &&
|
|
144
|
-
value > 0 &&
|
|
145
|
-
value <= max
|
|
146
|
-
? value
|
|
147
|
-
: undefined;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function normalizeModelOverrides(value) {
|
|
151
|
-
const raw = readVolatile(value, undefined);
|
|
152
|
-
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return {};
|
|
153
|
-
const out = {};
|
|
154
|
-
for (const [id, entry] of Object.entries(raw)) {
|
|
155
|
-
if (typeof id !== "string" || id.trim() === "") continue;
|
|
156
|
-
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
|
|
157
|
-
const normalized = {};
|
|
158
|
-
const contextWindow = normalizePositiveInteger(entry.contextWindow, OVERRIDE_LIMITS.contextWindow);
|
|
159
|
-
const maxTokens = normalizePositiveInteger(entry.maxTokens, OVERRIDE_LIMITS.maxTokens);
|
|
160
|
-
if (contextWindow !== undefined) normalized.contextWindow = contextWindow;
|
|
161
|
-
if (maxTokens !== undefined) normalized.maxTokens = maxTokens;
|
|
162
|
-
if (entry.image === true || entry.image === false) normalized.image = entry.image;
|
|
163
|
-
if (typeof entry.protocol === "string" && PROTOCOLS.includes(entry.protocol)) {
|
|
164
|
-
normalized.protocol = entry.protocol;
|
|
165
|
-
}
|
|
166
|
-
if (Array.isArray(entry.reasoningLevels)) {
|
|
167
|
-
const levels = [];
|
|
168
|
-
for (const level of entry.reasoningLevels) {
|
|
169
|
-
if (typeof level !== "string") continue;
|
|
170
|
-
const mapped = level.trim().toLowerCase();
|
|
171
|
-
if (REASONING_LEVELS.includes(mapped) && !levels.includes(mapped)) levels.push(mapped);
|
|
172
|
-
}
|
|
173
|
-
normalized.reasoningLevels = levels;
|
|
174
|
-
}
|
|
175
|
-
if (Object.keys(normalized).length > 0) out[id.trim()] = normalized;
|
|
176
|
-
}
|
|
177
|
-
return out;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export function resolveConfig(rawConfig = {}) {
|
|
181
|
-
const providerRaw = String(readVolatile(rawConfig?.provider, DEFAULT_PROVIDER)).trim();
|
|
182
|
-
const provider = providerRaw || DEFAULT_PROVIDER;
|
|
183
|
-
const displayNameRaw = String(readVolatile(rawConfig?.displayName, DEFAULT_DISPLAY_NAME)).trim();
|
|
184
|
-
const displayName = displayNameRaw || DEFAULT_DISPLAY_NAME;
|
|
185
|
-
const apiKeyEnvRaw = String(readVolatile(rawConfig?.apiKeyEnv, DEFAULT_API_KEY_ENV)).trim();
|
|
186
|
-
const apiKeyEnv = apiKeyEnvRaw || DEFAULT_API_KEY_ENV;
|
|
187
|
-
const baseURL = String(readVolatile(rawConfig?.baseURL, "")).trim();
|
|
188
|
-
const protocolRaw = String(readVolatile(rawConfig?.protocol, "openai-completions")).trim();
|
|
189
|
-
const protocol =
|
|
190
|
-
protocolRaw === "openai-responses" || protocolRaw === "anthropic-messages"
|
|
191
|
-
? protocolRaw
|
|
192
|
-
: "openai-completions";
|
|
193
|
-
const protocolMode =
|
|
194
|
-
String(readVolatile(rawConfig?.protocolMode, "auto")).trim() === "fixed" ? "fixed" : "auto";
|
|
195
|
-
const refreshRaw = Number(
|
|
196
|
-
readVolatile(rawConfig?.refreshIntervalSeconds, DEFAULT_REFRESH_INTERVAL_SECONDS)
|
|
197
|
-
);
|
|
198
|
-
const refreshIntervalSeconds =
|
|
199
|
-
Number.isFinite(refreshRaw) && refreshRaw >= 15 && refreshRaw <= 86400
|
|
200
|
-
? Math.floor(refreshRaw)
|
|
201
|
-
: DEFAULT_REFRESH_INTERVAL_SECONDS;
|
|
202
|
-
const timeoutRaw = Number(readVolatile(rawConfig?.probeTimeoutMs, DEFAULT_PROBE_TIMEOUT_MS));
|
|
203
|
-
const probeTimeoutMs =
|
|
204
|
-
Number.isFinite(timeoutRaw) && timeoutRaw >= 250 && timeoutRaw <= 60000
|
|
205
|
-
? Math.floor(timeoutRaw)
|
|
206
|
-
: DEFAULT_PROBE_TIMEOUT_MS;
|
|
207
|
-
const includePrefix = String(readVolatile(rawConfig?.includePrefix, "")).trim();
|
|
208
|
-
const excludePrefix = String(readVolatile(rawConfig?.excludePrefix, "")).trim();
|
|
209
|
-
const selectedModels = normalizeStringList(readVolatile(rawConfig?.selectedModels, undefined));
|
|
210
|
-
const modelOverrides = normalizeModelOverrides(rawConfig?.modelOverrides);
|
|
211
|
-
|
|
212
|
-
return {
|
|
213
|
-
id: normalizeAccountId(readVolatile(rawConfig?.id, DEFAULT_ACCOUNT_ID)) ?? DEFAULT_ACCOUNT_ID,
|
|
214
|
-
enabled: readVolatile(rawConfig?.enabled, true) !== false,
|
|
215
|
-
provider,
|
|
216
|
-
displayName,
|
|
217
|
-
baseURL,
|
|
218
|
-
apiKeyEnv,
|
|
219
|
-
protocol,
|
|
220
|
-
protocolMode,
|
|
221
|
-
refreshIntervalSeconds,
|
|
222
|
-
probeTimeoutMs,
|
|
223
|
-
includePrefix,
|
|
224
|
-
excludePrefix,
|
|
225
|
-
...(selectedModels === undefined ? {} : { selectedModels }),
|
|
226
|
-
modelOverrides,
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// #endregion
|
|
231
|
-
|
|
232
|
-
// #region Accounts
|
|
233
|
-
|
|
234
|
-
export function normalizeAccountId(value) {
|
|
235
|
-
const trimmed = typeof value === "string" ? value.trim() : "";
|
|
236
|
-
return ACCOUNT_ID_PATTERN.test(trimmed) ? trimmed : undefined;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/** The persisted shape of one account: its settings without the globals. */
|
|
240
|
-
export function pickAccount(resolved) {
|
|
241
|
-
return {
|
|
242
|
-
id: resolved.id,
|
|
243
|
-
enabled: resolved.enabled,
|
|
244
|
-
provider: resolved.provider,
|
|
245
|
-
displayName: resolved.displayName,
|
|
246
|
-
baseURL: resolved.baseURL,
|
|
247
|
-
apiKeyEnv: resolved.apiKeyEnv,
|
|
248
|
-
protocol: resolved.protocol,
|
|
249
|
-
protocolMode: resolved.protocolMode,
|
|
250
|
-
includePrefix: resolved.includePrefix,
|
|
251
|
-
excludePrefix: resolved.excludePrefix,
|
|
252
|
-
...(resolved.selectedModels === undefined ? {} : { selectedModels: resolved.selectedModels }),
|
|
253
|
-
modelOverrides: resolved.modelOverrides,
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function pickFields(source, fields) {
|
|
258
|
-
const out = {};
|
|
259
|
-
if (typeof source !== "object" || source === null) return out;
|
|
260
|
-
for (const field of fields) if (source[field] !== undefined) out[field] = source[field];
|
|
261
|
-
return out;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
function plainObject(value) {
|
|
265
|
-
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : undefined;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Normalize the state file into `{ version, refreshIntervalSeconds,
|
|
270
|
-
* probeTimeoutMs, accounts[] }`. A legacy single-account file (flat fields,
|
|
271
|
-
* no `accounts` array) becomes one account with id `default`, so its
|
|
272
|
-
* provider route, key reference and model selection carry over unchanged.
|
|
273
|
-
* Invalid or duplicate account ids are replaced with generated ones.
|
|
274
|
-
*/
|
|
275
|
-
export function normalizeState(raw = {}) {
|
|
276
|
-
const source = plainObject(raw) ?? {};
|
|
277
|
-
const globals = resolveConfig(pickFields(source, GLOBAL_FIELDS));
|
|
278
|
-
const rawAccounts = Array.isArray(source.accounts)
|
|
279
|
-
? source.accounts
|
|
280
|
-
: [{ ...pickFields(source, ACCOUNT_FIELDS), id: DEFAULT_ACCOUNT_ID }];
|
|
281
|
-
const accounts = [];
|
|
282
|
-
const ids = new Set();
|
|
283
|
-
for (const entry of rawAccounts) {
|
|
284
|
-
if (plainObject(entry) === undefined) continue;
|
|
285
|
-
let id = normalizeAccountId(entry.id);
|
|
286
|
-
if (id === undefined || ids.has(id)) {
|
|
287
|
-
let n = accounts.length + 1;
|
|
288
|
-
while (ids.has(`account-${n}`)) n += 1;
|
|
289
|
-
id = `account-${n}`;
|
|
290
|
-
}
|
|
291
|
-
ids.add(id);
|
|
292
|
-
accounts.push(pickAccount(resolveConfig({ ...pickFields(entry, ACCOUNT_FIELDS), id })));
|
|
293
|
-
}
|
|
294
|
-
return {
|
|
295
|
-
version: STATE_VERSION,
|
|
296
|
-
refreshIntervalSeconds: globals.refreshIntervalSeconds,
|
|
297
|
-
probeTimeoutMs: globals.probeTimeoutMs,
|
|
298
|
-
accounts,
|
|
299
|
-
};
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Overlay the host-supplied plugin config on the persisted state. A config
|
|
304
|
-
* with `accounts` replaces the account list; flat legacy fields apply to the
|
|
305
|
-
* first account, as they did when only one account existed.
|
|
306
|
-
*/
|
|
307
|
-
export function mergeHostConfig(persisted, config) {
|
|
308
|
-
const host = plainObject(config) ?? {};
|
|
309
|
-
const base = plainObject(persisted) ?? {};
|
|
310
|
-
if (!Array.isArray(base.accounts) || Array.isArray(host.accounts)) return { ...base, ...host };
|
|
311
|
-
const flat = pickFields(host, ACCOUNT_FIELDS);
|
|
312
|
-
const accounts = [...base.accounts];
|
|
313
|
-
if (Object.keys(flat).length > 0 && accounts.length > 0) accounts[0] = { ...accounts[0], ...flat };
|
|
314
|
-
return { ...base, ...pickFields(host, GLOBAL_FIELDS), accounts };
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Accounts whose managed route keys overlap an earlier account's. Each one
|
|
319
|
-
* maps to the id of the account that already owns the keys.
|
|
320
|
-
*/
|
|
321
|
-
export function accountConflicts(accounts) {
|
|
322
|
-
const owners = new Map();
|
|
323
|
-
const conflicts = new Map();
|
|
324
|
-
for (const account of accounts) {
|
|
325
|
-
const keys = managedRouteKeys(account.provider);
|
|
326
|
-
const clash = keys.map((key) => owners.get(key)).find((owner) => owner !== undefined);
|
|
327
|
-
if (clash !== undefined) {
|
|
328
|
-
conflicts.set(account.id, clash);
|
|
329
|
-
continue;
|
|
330
|
-
}
|
|
331
|
-
for (const key of keys) owners.set(key, account.id);
|
|
332
|
-
}
|
|
333
|
-
return conflicts;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
export function assertNoConflicts(accounts) {
|
|
337
|
-
const conflicts = accountConflicts(accounts);
|
|
338
|
-
for (const [id, ownerId] of conflicts) {
|
|
339
|
-
const account = accounts.find((a) => a.id === id);
|
|
340
|
-
const owner = accounts.find((a) => a.id === ownerId);
|
|
341
|
-
throw new Error(
|
|
342
|
-
`dsh-cliproxyapi: provider route "${account?.provider}" of account "${account?.displayName ?? id}" overlaps the routes of account "${owner?.displayName ?? ownerId}". Every account needs its own Provider Route ID.`
|
|
343
|
-
);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
* Defaults for a newly added account: the first free `cli-proxy-N` route,
|
|
349
|
-
* its own credential reference, and disabled until the user fills it in.
|
|
350
|
-
* When no account owns `cli-proxy`, the new account takes the classic names.
|
|
351
|
-
*/
|
|
352
|
-
export function newAccountDefaults(accounts) {
|
|
353
|
-
const usedIds = new Set(accounts.map((a) => a.id));
|
|
354
|
-
const usedKeys = new Set(accounts.flatMap((a) => managedRouteKeys(a.provider)));
|
|
355
|
-
const usedRefs = new Set(accounts.map((a) => a.apiKeyEnv));
|
|
356
|
-
const free = (provider) => managedRouteKeys(provider).every((key) => !usedKeys.has(key));
|
|
357
|
-
const base = {
|
|
358
|
-
enabled: false,
|
|
359
|
-
baseURL: "",
|
|
360
|
-
protocol: "openai-completions",
|
|
361
|
-
protocolMode: "auto",
|
|
362
|
-
includePrefix: "",
|
|
363
|
-
excludePrefix: "",
|
|
364
|
-
// No selectedModels: the first successful probe seeds recommendModels().
|
|
365
|
-
modelOverrides: {},
|
|
366
|
-
};
|
|
367
|
-
if (!usedIds.has(DEFAULT_ACCOUNT_ID) && free(DEFAULT_PROVIDER) && !usedRefs.has(DEFAULT_API_KEY_ENV)) {
|
|
368
|
-
return {
|
|
369
|
-
...base,
|
|
370
|
-
id: DEFAULT_ACCOUNT_ID,
|
|
371
|
-
provider: DEFAULT_PROVIDER,
|
|
372
|
-
displayName: DEFAULT_DISPLAY_NAME,
|
|
373
|
-
apiKeyEnv: DEFAULT_API_KEY_ENV,
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
let n = Math.max(2, accounts.length + 1);
|
|
377
|
-
while (
|
|
378
|
-
usedIds.has(`account-${n}`) ||
|
|
379
|
-
!free(`${DEFAULT_PROVIDER}-${n}`) ||
|
|
380
|
-
usedRefs.has(`${DEFAULT_API_KEY_ENV}_${n}`)
|
|
381
|
-
) {
|
|
382
|
-
n += 1;
|
|
383
|
-
}
|
|
384
|
-
return {
|
|
385
|
-
...base,
|
|
386
|
-
id: `account-${n}`,
|
|
387
|
-
provider: `${DEFAULT_PROVIDER}-${n}`,
|
|
388
|
-
displayName: `${DEFAULT_DISPLAY_NAME} ${n}`,
|
|
389
|
-
apiKeyEnv: `${DEFAULT_API_KEY_ENV}_${n}`,
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* Apply an account patch from the UI. `selectedModels` and
|
|
395
|
-
* `modelOverrides` replace the stored value when present; the id never changes.
|
|
396
|
-
*/
|
|
397
|
-
export function patchAccount(account, patch) {
|
|
398
|
-
const fields = pickFields(patch, ACCOUNT_FIELDS);
|
|
399
|
-
const next = { ...account, ...fields, id: account.id };
|
|
400
|
-
if (patch !== null && typeof patch === "object" && "selectedModels" in patch) {
|
|
401
|
-
next.selectedModels = Array.isArray(patch.selectedModels)
|
|
402
|
-
? normalizeStringList(patch.selectedModels) ?? []
|
|
403
|
-
: [];
|
|
404
|
-
}
|
|
405
|
-
if (patch !== null && typeof patch === "object" && "modelOverrides" in patch) {
|
|
406
|
-
next.modelOverrides = normalizeModelOverrides(patch.modelOverrides);
|
|
407
|
-
}
|
|
408
|
-
return pickAccount(resolveConfig(next));
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
// #endregion
|
|
412
|
-
|
|
413
|
-
// #region Persistent State Store
|
|
414
|
-
|
|
415
|
-
export function resolveStateFilePath(ctx) {
|
|
416
|
-
try {
|
|
417
|
-
const profileCtx = ctx?.get?.("profileContext");
|
|
418
|
-
if (profileCtx && typeof profileCtx.dir === "string" && profileCtx.dir.trim() !== "") {
|
|
419
|
-
return join(profileCtx.dir.trim(), ".dsh-cliproxyapi", "state.json");
|
|
420
|
-
}
|
|
421
|
-
} catch {}
|
|
422
|
-
const dshHome =
|
|
423
|
-
process.env.DSH_HOME && process.env.DSH_HOME.trim() !== ""
|
|
424
|
-
? process.env.DSH_HOME.trim()
|
|
425
|
-
: join(homedir(), ".dsh");
|
|
426
|
-
return join(dshHome, "dsh-cliproxyapi", "state.json");
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
export function loadPersistedState(filePath) {
|
|
430
|
-
try {
|
|
431
|
-
if (!existsSync(filePath)) return {};
|
|
432
|
-
const raw = JSON.parse(readFileSync(filePath, "utf8"));
|
|
433
|
-
return typeof raw === "object" && raw !== null && !Array.isArray(raw) ? raw : {};
|
|
434
|
-
} catch {
|
|
435
|
-
return {};
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
export function savePersistedState(filePath, state) {
|
|
440
|
-
try {
|
|
441
|
-
const dir = dirname(filePath);
|
|
442
|
-
mkdirSync(dir, { recursive: true });
|
|
443
|
-
const tmp = `${filePath}.${Date.now().toString(36)}.tmp`;
|
|
444
|
-
writeFileSync(tmp, JSON.stringify(state, null, 2), "utf8");
|
|
445
|
-
renameSync(tmp, filePath);
|
|
446
|
-
} catch {}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
// #endregion
|
|
450
|
-
|
|
451
|
-
// #region Discovery & Catalog Parsing
|
|
452
|
-
|
|
453
|
-
function positiveInteger(value) {
|
|
454
|
-
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : undefined;
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
function firstPositiveInteger(source, fields) {
|
|
458
|
-
for (const field of fields) {
|
|
459
|
-
const value = positiveInteger(source[field]);
|
|
460
|
-
if (value !== undefined) return value;
|
|
461
|
-
}
|
|
462
|
-
return undefined;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
function stringArray(value) {
|
|
466
|
-
if (!Array.isArray(value)) return [];
|
|
467
|
-
return [
|
|
468
|
-
...new Set(
|
|
469
|
-
value
|
|
470
|
-
.filter((item) => typeof item === "string")
|
|
471
|
-
.map((item) => item.trim())
|
|
472
|
-
.filter(Boolean)
|
|
473
|
-
),
|
|
474
|
-
];
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
function catalogArray(payload) {
|
|
478
|
-
if (typeof payload !== "object" || payload === null) return undefined;
|
|
479
|
-
if (Array.isArray(payload.models)) return payload.models;
|
|
480
|
-
if (Array.isArray(payload.data)) return payload.data;
|
|
481
|
-
return undefined;
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
function rawReasoningEfforts(value) {
|
|
485
|
-
if (!Array.isArray(value)) return [];
|
|
486
|
-
return value
|
|
487
|
-
.map((item) =>
|
|
488
|
-
typeof item === "string"
|
|
489
|
-
? item
|
|
490
|
-
: typeof item === "object" && item !== null && typeof item.effort === "string"
|
|
491
|
-
? item.effort
|
|
492
|
-
: ""
|
|
493
|
-
)
|
|
494
|
-
.map((raw) => raw.trim().toLowerCase())
|
|
495
|
-
.filter(Boolean);
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
function parseReasoningLevels(value) {
|
|
499
|
-
const levels = [];
|
|
500
|
-
for (const normalized of rawReasoningEfforts(value)) {
|
|
501
|
-
const mapped =
|
|
502
|
-
normalized === "none" ? "off" : normalized === "ultra" ? "max" : normalized;
|
|
503
|
-
if (REASONING_LEVELS.includes(mapped) && !levels.includes(mapped)) {
|
|
504
|
-
levels.push(mapped);
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
return levels;
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
/**
|
|
511
|
-
* Wire spellings that differ from the DSH level name: a catalog offering only
|
|
512
|
-
* `ultra` sends `ultra` for DSH `max`; one also offering `max` keeps `max`.
|
|
513
|
-
*/
|
|
514
|
-
function parseReasoningWire(value) {
|
|
515
|
-
const raw = new Set(rawReasoningEfforts(value));
|
|
516
|
-
return raw.has("ultra") && !raw.has("max") ? { max: "ultra" } : {};
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
export function normalizeCpaRoot(raw) {
|
|
520
|
-
const trimmed = String(raw ?? "").trim();
|
|
521
|
-
if (trimmed === "") throw new Error("empty CPA baseURL");
|
|
522
|
-
const withScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed) ? trimmed : `http://${trimmed}`;
|
|
523
|
-
const url = new URL(withScheme);
|
|
524
|
-
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
525
|
-
throw new Error(`unsupported protocol ${url.protocol}`);
|
|
526
|
-
}
|
|
527
|
-
url.search = "";
|
|
528
|
-
url.hash = "";
|
|
529
|
-
let path = url.pathname.replace(/\/+$/, "");
|
|
530
|
-
path = path.replace(/\/(?:v1(?:\/messages|\/chat\/completions|\/responses)?|backend-api)$/i, "");
|
|
531
|
-
url.pathname = path === "" ? "/" : `${path}/`;
|
|
532
|
-
return url.toString().replace(/\/$/, "");
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* Probe order: the configured URL, then (only with `autoDiscover`, the
|
|
537
|
-
* default) the account's existing route, `CLIPROXYAPI_BASE_URL` and the local
|
|
538
|
-
* ports. Accounts with an explicit URL never fall back to another server, so
|
|
539
|
-
* one account can't silently mirror another.
|
|
540
|
-
*/
|
|
541
|
-
export function discoveryCandidates(options) {
|
|
542
|
-
const raw =
|
|
543
|
-
options.autoDiscover === false
|
|
544
|
-
? [options.baseURL]
|
|
545
|
-
: [options.baseURL, options.fallbackBaseURL, options.environmentBaseURL, ...LOCAL_CANDIDATES];
|
|
546
|
-
const result = [];
|
|
547
|
-
for (const candidate of raw) {
|
|
548
|
-
if (candidate === undefined || String(candidate).trim() === "") continue;
|
|
549
|
-
try {
|
|
550
|
-
const normalized = normalizeCpaRoot(candidate);
|
|
551
|
-
if (!result.includes(normalized)) result.push(normalized);
|
|
552
|
-
} catch {
|
|
553
|
-
if (candidate === options.baseURL && String(options.baseURL).trim() !== "") {
|
|
554
|
-
throw new Error(`dsh-cliproxyapi: invalid baseURL "${candidate}"`);
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
return result;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
export function parseCatalog(payload) {
|
|
562
|
-
const catalog = catalogArray(payload);
|
|
563
|
-
if (catalog === undefined) {
|
|
564
|
-
throw new Error("response does not contain a models or data array");
|
|
565
|
-
}
|
|
566
|
-
const models = new Map();
|
|
567
|
-
for (const item of catalog) {
|
|
568
|
-
if (typeof item !== "object" || item === null) continue;
|
|
569
|
-
if (String(item.visibility ?? "").toLowerCase() === "hide") continue;
|
|
570
|
-
const rawId = item.slug ?? item.id;
|
|
571
|
-
const id = typeof rawId === "string" ? rawId.trim() : "";
|
|
572
|
-
if (id === "" || models.has(id)) continue;
|
|
573
|
-
const inputModalities = stringArray(item.input_modalities ?? item.inputModalities).map((v) =>
|
|
574
|
-
v.toLowerCase()
|
|
575
|
-
);
|
|
576
|
-
const outputModalities = stringArray(
|
|
577
|
-
item.supportedOutputModalities ?? item.output_modalities ?? item.outputModalities
|
|
578
|
-
).map((v) => v.toLowerCase());
|
|
579
|
-
const catalogType = typeof item.type === "string" && item.type.trim() !== "" ? item.type.trim() : undefined;
|
|
580
|
-
const rawLevels = item.supported_reasoning_levels ?? item.reasoning_levels;
|
|
581
|
-
const levels = parseReasoningLevels(rawLevels);
|
|
582
|
-
const reasoningWire = parseReasoningWire(rawLevels);
|
|
583
|
-
const contextWindow = firstPositiveInteger(item, [
|
|
584
|
-
"context_window",
|
|
585
|
-
"max_context_window",
|
|
586
|
-
"context_length",
|
|
587
|
-
"contextWindow",
|
|
588
|
-
]);
|
|
589
|
-
const maxTokens = firstPositiveInteger(item, [
|
|
590
|
-
"max_output_tokens",
|
|
591
|
-
"max_tokens",
|
|
592
|
-
"maxTokens",
|
|
593
|
-
]);
|
|
594
|
-
const rawName = item.display_name ?? item.name;
|
|
595
|
-
models.set(id, {
|
|
596
|
-
id,
|
|
597
|
-
...(typeof rawName === "string" && rawName.trim() !== "" ? { name: rawName.trim() } : {}),
|
|
598
|
-
...(typeof item.owned_by === "string" && item.owned_by.trim() !== ""
|
|
599
|
-
? { ownedBy: item.owned_by.trim() }
|
|
600
|
-
: {}),
|
|
601
|
-
...(typeof item.provider === "string" && item.provider.trim() !== ""
|
|
602
|
-
? { provider: item.provider.trim() }
|
|
603
|
-
: {}),
|
|
604
|
-
...(contextWindow === undefined ? {} : { contextWindow }),
|
|
605
|
-
...(maxTokens === undefined ? {} : { maxTokens }),
|
|
606
|
-
inputModalities: inputModalities.length > 0 ? inputModalities : ["text"],
|
|
607
|
-
...(outputModalities.length === 0 ? {} : { outputModalities }),
|
|
608
|
-
...(catalogType === undefined ? {} : { catalogType }),
|
|
609
|
-
reasoning: item.reasoning === true || levels.some((level) => level !== "off"),
|
|
610
|
-
reasoningLevels: levels,
|
|
611
|
-
...(Object.keys(reasoningWire).length === 0 ? {} : { reasoningWire }),
|
|
612
|
-
// chat | image | video, before user overrides (see lib/media.js).
|
|
613
|
-
detectedKind: detectMediaKind({ id, catalogType, outputModalities }),
|
|
614
|
-
});
|
|
615
|
-
}
|
|
616
|
-
return [...models.values()];
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
async function readBoundedJson(response) {
|
|
620
|
-
const declared = Number(response.headers.get("content-length"));
|
|
621
|
-
if (Number.isFinite(declared) && declared > MAX_CATALOG_BYTES) {
|
|
622
|
-
throw new Error("catalog is larger than 8 MiB");
|
|
623
|
-
}
|
|
624
|
-
const text = await response.text();
|
|
625
|
-
if (new TextEncoder().encode(text).byteLength > MAX_CATALOG_BYTES) {
|
|
626
|
-
throw new Error("catalog is larger than 8 MiB");
|
|
627
|
-
}
|
|
628
|
-
try {
|
|
629
|
-
return JSON.parse(text);
|
|
630
|
-
} catch {
|
|
631
|
-
throw new Error("catalog is not valid JSON");
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
/**
|
|
636
|
-
* The plain `/v1/models` listing, parsed, by id. It carries models the
|
|
637
|
-
* enriched catalog omits (the registry `type` that marks image models, and
|
|
638
|
-
* media models only this listing knows), so it gets its own deadline instead
|
|
639
|
-
* of the enriched request's remaining time and one retry: a slow first
|
|
640
|
-
* response must never silently hide every image model until the next
|
|
641
|
-
* five-minute refresh.
|
|
642
|
-
* @returns `{ models, ok }`; `ok` is false when the listing could not be read.
|
|
643
|
-
*/
|
|
644
|
-
async function fetchListing(fetchImpl, rootURL, options) {
|
|
645
|
-
const timeoutMs = options.timeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS;
|
|
646
|
-
const attempt = async () => {
|
|
647
|
-
const timeout = AbortSignal.timeout(timeoutMs);
|
|
648
|
-
const signal = options.signal === undefined ? timeout : AbortSignal.any([options.signal, timeout]);
|
|
649
|
-
const response = await fetchImpl(`${rootURL}/v1/models`, {
|
|
650
|
-
method: "GET",
|
|
651
|
-
headers: {
|
|
652
|
-
Accept: "application/json",
|
|
653
|
-
...(options.apiKey === undefined || options.apiKey === ""
|
|
654
|
-
? {}
|
|
655
|
-
: { Authorization: `Bearer ${options.apiKey}` }),
|
|
656
|
-
},
|
|
657
|
-
signal,
|
|
658
|
-
});
|
|
659
|
-
if (!response.ok) return { models: new Map(), ok: false };
|
|
660
|
-
return { models: new Map(parseCatalog(await readBoundedJson(response)).map((m) => [m.id, m])), ok: true };
|
|
661
|
-
};
|
|
662
|
-
try {
|
|
663
|
-
return await attempt();
|
|
664
|
-
} catch (error) {
|
|
665
|
-
if (options.signal?.aborted) throw options.signal.reason;
|
|
666
|
-
if (error instanceof Error && /catalog is larger|not valid JSON|does not contain/.test(error.message)) {
|
|
667
|
-
return { models: new Map(), ok: false };
|
|
668
|
-
}
|
|
669
|
-
try {
|
|
670
|
-
return await attempt();
|
|
671
|
-
} catch (retryError) {
|
|
672
|
-
if (options.signal?.aborted) throw options.signal.reason;
|
|
673
|
-
return { models: new Map(), ok: false };
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
/**
|
|
679
|
-
* Fill catalog gaps from the plain listing: `owned_by` (native protocol),
|
|
680
|
-
* the registry `type` and output modalities (media kind). A media model that
|
|
681
|
-
* only the plain listing carries is added; plain-only chat models are not.
|
|
682
|
-
*/
|
|
683
|
-
function mergeListing(models, listing) {
|
|
684
|
-
const merged = models.map((m) => {
|
|
685
|
-
const plainEntry = listing.get(m.id);
|
|
686
|
-
if (plainEntry === undefined) return m;
|
|
687
|
-
const next = { ...m };
|
|
688
|
-
for (const field of ["ownedBy", "catalogType", "outputModalities"]) {
|
|
689
|
-
if (next[field] === undefined && plainEntry[field] !== undefined) next[field] = plainEntry[field];
|
|
690
|
-
}
|
|
691
|
-
next.detectedKind = detectMediaKind(next);
|
|
692
|
-
return next;
|
|
693
|
-
});
|
|
694
|
-
const known = new Set(merged.map((m) => m.id));
|
|
695
|
-
for (const entry of listing.values()) {
|
|
696
|
-
if (!known.has(entry.id) && entry.detectedKind !== "chat") merged.push(entry);
|
|
697
|
-
}
|
|
698
|
-
return merged;
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
export async function discoverCpa(options) {
|
|
702
|
-
const fetchImpl = options.fetch ?? fetch;
|
|
703
|
-
const failures = [];
|
|
704
|
-
const candidates = discoveryCandidates(options);
|
|
705
|
-
for (const rootURL of candidates) {
|
|
706
|
-
const modelsURL = `${rootURL}/v1/models?client_version=pi`;
|
|
707
|
-
const timeout = AbortSignal.timeout(options.timeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS);
|
|
708
|
-
const signal =
|
|
709
|
-
options.signal === undefined ? timeout : AbortSignal.any([options.signal, timeout]);
|
|
710
|
-
// Start the plain listing together with the enriched catalog; both carry
|
|
711
|
-
// different fields, and the listing alone knows some image models.
|
|
712
|
-
const listingPromise = fetchListing(fetchImpl, rootURL, options);
|
|
713
|
-
try {
|
|
714
|
-
const response = await fetchImpl(modelsURL, {
|
|
715
|
-
method: "GET",
|
|
716
|
-
headers: {
|
|
717
|
-
Accept: "application/json",
|
|
718
|
-
...(options.apiKey === undefined || options.apiKey === ""
|
|
719
|
-
? {}
|
|
720
|
-
: { Authorization: `Bearer ${options.apiKey}` }),
|
|
721
|
-
},
|
|
722
|
-
signal,
|
|
723
|
-
});
|
|
724
|
-
if (!response.ok) {
|
|
725
|
-
void listingPromise.catch(() => {});
|
|
726
|
-
failures.push(`${rootURL}: HTTP ${response.status}`);
|
|
727
|
-
continue;
|
|
728
|
-
}
|
|
729
|
-
const models = parseCatalog(await readBoundedJson(response));
|
|
730
|
-
// The enriched catalog omits `owned_by`; the plain listing carries the
|
|
731
|
-
// model family each upstream reports, which decides its native protocol,
|
|
732
|
-
// and the registry type that marks image models.
|
|
733
|
-
const listing = await listingPromise;
|
|
734
|
-
return { rootURL, modelsURL, models: mergeListing(models, listing.models), listingOk: listing.ok };
|
|
735
|
-
} catch (error) {
|
|
736
|
-
void listingPromise.catch(() => {});
|
|
737
|
-
if (options.signal?.aborted) throw options.signal.reason;
|
|
738
|
-
failures.push(`${rootURL}: ${error instanceof Error ? error.message : String(error)}`);
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
throw new Error(`dsh-cliproxyapi: no usable CPA service found (${failures.join("; ")})`);
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
// #endregion
|
|
745
|
-
|
|
746
|
-
// #region Profile & Model Builder
|
|
747
|
-
|
|
748
|
-
function splitPrefixes(csv) {
|
|
749
|
-
if (typeof csv !== "string" || csv.trim() === "") return [];
|
|
750
|
-
return csv
|
|
751
|
-
.split(",")
|
|
752
|
-
.map((s) => s.trim().toLowerCase())
|
|
753
|
-
.filter(Boolean);
|
|
754
|
-
}
|
|
755
|
-
|
|
756
|
-
export function filterModels(models, config) {
|
|
757
|
-
const includes = splitPrefixes(config.includePrefix);
|
|
758
|
-
const excludes = splitPrefixes(config.excludePrefix);
|
|
759
|
-
const selectedSet =
|
|
760
|
-
Array.isArray(config.selectedModels) && config.selectedModels.length > 0
|
|
761
|
-
? new Set(config.selectedModels)
|
|
762
|
-
: null;
|
|
763
|
-
|
|
764
|
-
return models.filter((m) => {
|
|
765
|
-
if (selectedSet !== null && !selectedSet.has(m.id)) {
|
|
766
|
-
return false;
|
|
767
|
-
}
|
|
768
|
-
const idLower = m.id.toLowerCase();
|
|
769
|
-
if (includes.length > 0 && !includes.some((p) => idLower.startsWith(p) || idLower.includes(p))) {
|
|
770
|
-
return false;
|
|
771
|
-
}
|
|
772
|
-
if (excludes.length > 0 && excludes.some((p) => idLower.startsWith(p) || idLower.includes(p))) {
|
|
773
|
-
return false;
|
|
774
|
-
}
|
|
775
|
-
return true;
|
|
776
|
-
});
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
export function modelFamily(model) {
|
|
780
|
-
const label = (model.name ?? model.id).trim().toLowerCase();
|
|
781
|
-
const known = label.match(
|
|
782
|
-
/^(gpt-oss|gemini|claude|gpt|grok|deepseek|qwen|glm|kimi|minimax|mistral|llama|swe|o(?=\d))/
|
|
783
|
-
)?.[1];
|
|
784
|
-
if (known !== undefined) return known;
|
|
785
|
-
return label.split(/[\s_/-]*\d/, 1)[0]?.replace(/[\s_/-]+$/, "") || label;
|
|
786
|
-
}
|
|
787
|
-
|
|
788
|
-
function modelVersion(model) {
|
|
789
|
-
const labels = [model.name, model.id];
|
|
790
|
-
for (const label of labels) {
|
|
791
|
-
if (label === undefined) continue;
|
|
792
|
-
const match = label.match(/(?:^|[^\d])(\d+(?:[.-]\d+)*)/);
|
|
793
|
-
if (match?.[1] !== undefined) return match[1].split(/[.-]/).map(Number);
|
|
794
|
-
}
|
|
795
|
-
return [];
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
function compareVersionDescending(left, right) {
|
|
799
|
-
const a = modelVersion(left);
|
|
800
|
-
const b = modelVersion(right);
|
|
801
|
-
const length = Math.max(a.length, b.length);
|
|
802
|
-
for (let i = 0; i < length; i += 1) {
|
|
803
|
-
const diff = (b[i] ?? 0) - (a[i] ?? 0);
|
|
804
|
-
if (diff !== 0) return diff;
|
|
805
|
-
}
|
|
806
|
-
return 0;
|
|
807
|
-
}
|
|
808
|
-
|
|
809
|
-
export function sortModelsNewestFirst(models) {
|
|
810
|
-
const groups = new Map();
|
|
811
|
-
for (const model of models) {
|
|
812
|
-
const family = modelFamily(model);
|
|
813
|
-
const group = groups.get(family);
|
|
814
|
-
if (group === undefined) groups.set(family, [model]);
|
|
815
|
-
else group.push(model);
|
|
816
|
-
}
|
|
817
|
-
return [...groups.values()].flatMap((group) => [...group].sort(compareVersionDescending));
|
|
818
|
-
}
|
|
819
|
-
|
|
820
|
-
/**
|
|
821
|
-
* A starting selection derived from the server's own catalog: the newest
|
|
822
|
-
* model of each family, in catalog order, at most {@link MAX_RECOMMENDED_MODELS}.
|
|
823
|
-
* Among equally new models a plain id is preferred over a prefixed one
|
|
824
|
-
* (`gpt-6` over `vendor/gpt-6`).
|
|
825
|
-
* @param models - parsed catalog models.
|
|
826
|
-
* @returns model ids.
|
|
827
|
-
*/
|
|
828
|
-
export function recommendModels(models) {
|
|
829
|
-
const groups = new Map();
|
|
830
|
-
for (const model of models) {
|
|
831
|
-
const family = modelFamily(model);
|
|
832
|
-
const group = groups.get(family);
|
|
833
|
-
if (group === undefined) groups.set(family, [model]);
|
|
834
|
-
else group.push(model);
|
|
835
|
-
}
|
|
836
|
-
const picks = [];
|
|
837
|
-
for (const group of groups.values()) {
|
|
838
|
-
const sorted = [...group].sort(
|
|
839
|
-
(a, b) => compareVersionDescending(a, b) || Number(a.id.includes("/")) - Number(b.id.includes("/"))
|
|
840
|
-
);
|
|
841
|
-
picks.push(sorted[0].id);
|
|
842
|
-
if (picks.length >= MAX_RECOMMENDED_MODELS) break;
|
|
843
|
-
}
|
|
844
|
-
return picks;
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
/**
|
|
848
|
-
* Pick the wire protocol that reaches a model with the fewest translations.
|
|
849
|
-
*
|
|
850
|
-
* CLIProxyAPI serves every model on OpenAI Chat, OpenAI Responses and
|
|
851
|
-
* Anthropic Messages and translates between them and the upstream channel.
|
|
852
|
-
* Speaking the upstream's own protocol skips that translation:
|
|
853
|
-
* - Claude family -> Anthropic Messages (`/v1/messages`)
|
|
854
|
-
* - OpenAI / Codex / Grok -> OpenAI Responses (`/v1/responses`)
|
|
855
|
-
* - everything else, including Gemini -> OpenAI Chat Completions; DSH
|
|
856
|
-
* cannot speak Gemini generateContent on a custom route.
|
|
857
|
-
* `owned_by` from the CPA listing names the family; the id is the fallback.
|
|
858
|
-
* @param model - parsed catalog model.
|
|
859
|
-
* @returns one of {@link PROTOCOLS}.
|
|
860
|
-
*/
|
|
861
|
-
export function nativeProtocol(model) {
|
|
862
|
-
const owner = String(model.ownedBy ?? "").toLowerCase();
|
|
863
|
-
const bare = String(model.id ?? "").toLowerCase().split("/").pop() ?? "";
|
|
864
|
-
if (owner === "anthropic" || /^claude/.test(bare)) return "anthropic-messages";
|
|
865
|
-
if (owner === "openai" || owner === "xai") return "openai-responses";
|
|
866
|
-
if (/^(gpt|o\d|codex|grok)/.test(bare) && !/^gpt-oss/.test(bare)) return "openai-responses";
|
|
867
|
-
return "openai-completions";
|
|
868
|
-
}
|
|
869
|
-
|
|
870
|
-
/** The protocol a model is synchronized with under the given config. */
|
|
871
|
-
export function modelProtocol(model, config) {
|
|
872
|
-
const override = config.modelOverrides?.[model.id]?.protocol;
|
|
873
|
-
if (override !== undefined) return override;
|
|
874
|
-
return config.protocolMode === "fixed" ? config.protocol : nativeProtocol(model);
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
/** The DSH route key carrying one protocol. */
|
|
878
|
-
export function routeKeyFor(provider, protocol) {
|
|
879
|
-
return `${provider}${PROTOCOL_ROUTES[protocol].suffix}`;
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
function routeBaseURL(rootURL, protocol) {
|
|
883
|
-
return protocol === "anthropic-messages" ? rootURL : `${rootURL}/v1`;
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
function supportedReasoning(model) {
|
|
887
|
-
if (!model.reasoning) return undefined;
|
|
888
|
-
const declared = new Set(model.reasoningLevels);
|
|
889
|
-
const levels =
|
|
890
|
-
declared.size === 0
|
|
891
|
-
? REASONING_LEVELS
|
|
892
|
-
: REASONING_LEVELS.filter((level) => declared.has(level));
|
|
893
|
-
const result = {};
|
|
894
|
-
for (const level of levels) {
|
|
895
|
-
result[level] = level === "off" ? null : model.reasoningWire?.[level] ?? level;
|
|
896
|
-
}
|
|
897
|
-
return Object.keys(result).length === 0 ? undefined : result;
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
/**
|
|
901
|
-
* Claude models that take adaptive thinking with an `effort` value rather
|
|
902
|
-
* than a token budget: Opus/Sonnet 4.6+, Haiku 5+, Fable and every 5.x.
|
|
903
|
-
* Older Claude models keep budget-based thinking.
|
|
904
|
-
*/
|
|
905
|
-
function claudeUsesAdaptiveThinking(model) {
|
|
906
|
-
const bare = String(model.id).toLowerCase().split("/").pop() ?? "";
|
|
907
|
-
if (/^claude-fable/.test(bare)) return true;
|
|
908
|
-
const match = bare.match(/^claude-(?:opus|sonnet|haiku)-(\d+)(?:-(\d+))?/);
|
|
909
|
-
if (match === null) return /^claude-\d+-fable/.test(bare);
|
|
910
|
-
const major = Number(match[1]);
|
|
911
|
-
const minor = match[2] !== undefined && match[2].length <= 2 ? Number(match[2]) : 0;
|
|
912
|
-
return major > 4 || (major === 4 && minor >= 6);
|
|
913
|
-
}
|
|
914
|
-
|
|
915
|
-
export function applyModelOverride(model, override) {
|
|
916
|
-
if (override === undefined) return model;
|
|
917
|
-
const next = { ...model };
|
|
918
|
-
if (override.contextWindow !== undefined) next.contextWindow = override.contextWindow;
|
|
919
|
-
if (override.maxTokens !== undefined) next.maxTokens = override.maxTokens;
|
|
920
|
-
if (override.image === true || override.image === false) {
|
|
921
|
-
const rest = model.inputModalities.filter((item) => item !== "image");
|
|
922
|
-
next.inputModalities = override.image ? [...new Set(["text", "image", ...rest])] : rest.length > 0 ? rest : ["text"];
|
|
923
|
-
}
|
|
924
|
-
if (Array.isArray(override.reasoningLevels)) {
|
|
925
|
-
next.reasoningLevels = [...override.reasoningLevels];
|
|
926
|
-
next.reasoning = override.reasoningLevels.some((level) => level !== "off");
|
|
927
|
-
}
|
|
928
|
-
return next;
|
|
929
|
-
}
|
|
930
|
-
|
|
931
|
-
function protocolCompat(model, protocol, hasReasoning) {
|
|
932
|
-
if (protocol === "openai-completions") {
|
|
933
|
-
return hasReasoning ? { supportsReasoningEffort: true, thinkingFormat: "openai" } : undefined;
|
|
934
|
-
}
|
|
935
|
-
if (protocol === "anthropic-messages") {
|
|
936
|
-
return hasReasoning && claudeUsesAdaptiveThinking(model)
|
|
937
|
-
? { forceAdaptiveThinking: true }
|
|
938
|
-
: undefined;
|
|
939
|
-
}
|
|
940
|
-
return undefined;
|
|
941
|
-
}
|
|
942
|
-
|
|
943
|
-
function toPiAiModelProfile(model, protocol) {
|
|
944
|
-
const reasoningEfforts = supportedReasoning(model);
|
|
945
|
-
const compat = protocolCompat(model, protocol, reasoningEfforts !== undefined);
|
|
946
|
-
return {
|
|
947
|
-
id: model.id,
|
|
948
|
-
...(model.name === undefined ? {} : { name: model.name }),
|
|
949
|
-
contextWindow: model.contextWindow ?? 262144,
|
|
950
|
-
maxTokens: model.maxTokens ?? 64000,
|
|
951
|
-
input: model.inputModalities.includes("image") ? ["text", "image"] : ["text"],
|
|
952
|
-
...(reasoningEfforts === undefined ? {} : { reasoningEfforts }),
|
|
953
|
-
...(compat === undefined ? {} : { compat }),
|
|
954
|
-
};
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
/**
|
|
958
|
-
* Build one llm-pi-ai route per protocol the selected models need.
|
|
959
|
-
* The base route key (e.g. `cli-proxy`) always carries OpenAI Chat so that
|
|
960
|
-
* existing sessions and the default-model setting keep resolving; Responses
|
|
961
|
-
* and Messages models get `<key>-responses` / `<key>-messages` siblings.
|
|
962
|
-
* @returns `{ allModels, orderedModels, routes }` where `routes` maps each
|
|
963
|
-
* route key to its profile, in protocol order, and every ordered model
|
|
964
|
-
* carries its `protocol` and `route`.
|
|
965
|
-
*/
|
|
966
|
-
/** Whether a model belongs in chat routes: `kind` (with overrides) or detection says chat. */
|
|
967
|
-
export function isChatModel(model) {
|
|
968
|
-
return (model.kind ?? model.detectedKind ?? "chat") === "chat";
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
export function buildPiAiRoutes(config, discovery, hasApiKey) {
|
|
972
|
-
const allOrdered = sortModelsNewestFirst(discovery.models);
|
|
973
|
-
// Image / video models are served by CPA's media endpoints, never by chat routes.
|
|
974
|
-
const chatOrdered = allOrdered.filter(isChatModel);
|
|
975
|
-
const filtered = filterModels(chatOrdered, config).map((model) => {
|
|
976
|
-
const effective = applyModelOverride(model, config.modelOverrides?.[model.id]);
|
|
977
|
-
const protocol = modelProtocol(model, config);
|
|
978
|
-
return { ...effective, protocol, route: routeKeyFor(config.provider, protocol) };
|
|
979
|
-
});
|
|
980
|
-
if (filtered.length === 0) {
|
|
981
|
-
throw new Error(
|
|
982
|
-
`dsh-cliproxyapi: none of the selected models matched the ${chatOrdered.length} chat model(s) offered by ${discovery.rootURL}. Select models in Settings → CLIProxyAPI → Models.`
|
|
983
|
-
);
|
|
984
|
-
}
|
|
985
|
-
const auth = hasApiKey
|
|
986
|
-
? { apiKeyEnv: config.apiKeyEnv }
|
|
987
|
-
: { headers: { Authorization: "Bearer cpa-local" } };
|
|
988
|
-
const routes = {};
|
|
989
|
-
for (const protocol of PROTOCOLS) {
|
|
990
|
-
const members = filtered.filter((m) => m.protocol === protocol);
|
|
991
|
-
if (members.length === 0) continue;
|
|
992
|
-
routes[routeKeyFor(config.provider, protocol)] = {
|
|
993
|
-
displayName: `${config.displayName}${PROTOCOL_ROUTES[protocol].label}`,
|
|
994
|
-
api: protocol,
|
|
995
|
-
baseURL: routeBaseURL(discovery.rootURL, protocol),
|
|
996
|
-
models: members.map((m) => toPiAiModelProfile(m, protocol)),
|
|
997
|
-
...auth,
|
|
998
|
-
};
|
|
999
|
-
}
|
|
1000
|
-
return { allModels: allOrdered, orderedModels: filtered, routes };
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
|
-
/** Every route key this plugin may own under a provider prefix. */
|
|
1004
|
-
export function managedRouteKeys(provider) {
|
|
1005
|
-
return PROTOCOLS.map((protocol) => routeKeyFor(provider, protocol));
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
// #endregion
|
|
1009
|
-
|
|
1010
|
-
// #region Runtime Synchronizer
|
|
1011
|
-
|
|
1012
|
-
function sleep(ms) {
|
|
1013
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
function sameJson(a, b) {
|
|
1017
|
-
return JSON.stringify(a) === JSON.stringify(b);
|
|
1018
|
-
}
|
|
1019
|
-
|
|
1020
|
-
/** Fields this plugin writes; any stale copy is replaced, never merged. */
|
|
1021
|
-
const MANAGED_ROUTE_FIELDS = ["displayName", "api", "baseURL", "models", "apiKeyEnv", "headers"];
|
|
1022
|
-
|
|
1023
|
-
function stripManagedFields(route) {
|
|
1024
|
-
const out = { ...route };
|
|
1025
|
-
for (const field of MANAGED_ROUTE_FIELDS) delete out[field];
|
|
1026
|
-
// An empty dict materialized by the schema is not a user choice.
|
|
1027
|
-
if (out.compat !== undefined && sameJson(out.compat, { chatTemplateKwargs: {}, chatTemplateArgs: {} })) {
|
|
1028
|
-
delete out.compat;
|
|
1029
|
-
}
|
|
1030
|
-
return out;
|
|
1031
|
-
}
|
|
1032
|
-
|
|
1033
|
-
async function mutateSettingsSafe(settings, ns, ops) {
|
|
1034
|
-
let lastError;
|
|
1035
|
-
for (let attempt = 0; attempt < 6; attempt += 1) {
|
|
1036
|
-
try {
|
|
1037
|
-
await settings.mutate(ns, ops);
|
|
1038
|
-
return;
|
|
1039
|
-
} catch (error) {
|
|
1040
|
-
lastError = error;
|
|
1041
|
-
const msg = error instanceof Error ? error.message : String(error);
|
|
1042
|
-
if (msg.includes("HMR transactions cannot be nested")) {
|
|
1043
|
-
await sleep(300 * (attempt + 1));
|
|
1044
|
-
continue;
|
|
1045
|
-
}
|
|
1046
|
-
throw error;
|
|
1047
|
-
}
|
|
1048
|
-
}
|
|
1049
|
-
throw lastError;
|
|
1050
|
-
}
|
|
1051
|
-
|
|
1052
|
-
function formatModelView(m, selectedSet, config) {
|
|
1053
|
-
const overrides = config?.modelOverrides ?? {};
|
|
1054
|
-
const override = overrides[m.id];
|
|
1055
|
-
const effective = applyModelOverride(m, override);
|
|
1056
|
-
const protocol = config === undefined ? nativeProtocol(m) : modelProtocol(m, config);
|
|
1057
|
-
return {
|
|
1058
|
-
kind: m.kind ?? m.detectedKind ?? "chat",
|
|
1059
|
-
protocol,
|
|
1060
|
-
nativeProtocol: nativeProtocol(m),
|
|
1061
|
-
...(m.ownedBy === undefined ? {} : { ownedBy: m.ownedBy }),
|
|
1062
|
-
id: effective.id,
|
|
1063
|
-
name: effective.name ?? effective.id,
|
|
1064
|
-
family: modelFamily(effective),
|
|
1065
|
-
contextWindow: effective.contextWindow ?? 262144,
|
|
1066
|
-
maxTokens: effective.maxTokens ?? 64000,
|
|
1067
|
-
image: effective.inputModalities.includes("image"),
|
|
1068
|
-
reasoning: effective.reasoning,
|
|
1069
|
-
reasoningLevels: effective.reasoningLevels,
|
|
1070
|
-
selected: selectedSet.has(effective.id),
|
|
1071
|
-
...(override === undefined ? {} : { override }),
|
|
1072
|
-
discovered: {
|
|
1073
|
-
contextWindow: m.contextWindow ?? 262144,
|
|
1074
|
-
maxTokens: m.maxTokens ?? 64000,
|
|
1075
|
-
image: m.inputModalities.includes("image"),
|
|
1076
|
-
reasoningLevels: m.reasoningLevels,
|
|
1077
|
-
},
|
|
1078
|
-
};
|
|
1079
|
-
}
|
|
1080
|
-
|
|
1081
|
-
export class CpaRuntime {
|
|
1082
|
-
constructor(options) {
|
|
1083
|
-
this.options = options;
|
|
1084
|
-
this.activeProvider = undefined;
|
|
1085
|
-
this.activeRoutes = undefined;
|
|
1086
|
-
this.running = undefined;
|
|
1087
|
-
this.rerun = false;
|
|
1088
|
-
this.controller = new AbortController();
|
|
1089
|
-
this.allDiscoveredModels = [];
|
|
1090
|
-
this.syncedModels = [];
|
|
1091
|
-
// Whether the last successful discovery could read the plain `/v1/models`
|
|
1092
|
-
// listing, which carries models the enriched catalog omits (image models
|
|
1093
|
-
// for some servers). A failed read is retried shortly, not in five minutes.
|
|
1094
|
-
this.listingOk = true;
|
|
1095
|
-
this.listingRetries = 0;
|
|
1096
|
-
this.listingRetryTimer = undefined;
|
|
1097
|
-
this.currentStatus = {
|
|
1098
|
-
state: "idle",
|
|
1099
|
-
modelCount: 0,
|
|
1100
|
-
totalDiscovered: 0,
|
|
1101
|
-
};
|
|
1102
|
-
}
|
|
1103
|
-
|
|
1104
|
-
/**
|
|
1105
|
-
* Models with their effective `kind` under the account's current kind
|
|
1106
|
-
* overrides (read fresh, so an override applies without a new probe).
|
|
1107
|
-
*/
|
|
1108
|
-
withKinds(models = this.allDiscoveredModels) {
|
|
1109
|
-
let overrides = {};
|
|
1110
|
-
try {
|
|
1111
|
-
overrides = this.options.kindOverrides?.() ?? {};
|
|
1112
|
-
} catch {}
|
|
1113
|
-
return models.map((m) => ({ ...m, kind: effectiveKind(m, overrides) }));
|
|
1114
|
-
}
|
|
1115
|
-
|
|
1116
|
-
/** Discovered chat models (what the picker, recommendations and seeding use). */
|
|
1117
|
-
chatModels(models = this.allDiscoveredModels) {
|
|
1118
|
-
return this.withKinds(models).filter(isChatModel);
|
|
1119
|
-
}
|
|
1120
|
-
|
|
1121
|
-
status() {
|
|
1122
|
-
const syncedSet = new Set(this.syncedModels.map((m) => m.id));
|
|
1123
|
-
const config = this.options.config();
|
|
1124
|
-
const kinded = this.withKinds();
|
|
1125
|
-
const byId = new Map(kinded.map((m) => [m.id, m]));
|
|
1126
|
-
return {
|
|
1127
|
-
...this.currentStatus,
|
|
1128
|
-
listingOk: this.listingOk,
|
|
1129
|
-
models: this.syncedModels.map((m) => ({
|
|
1130
|
-
...formatModelView(byId.get(m.id) ?? m, syncedSet, config),
|
|
1131
|
-
route: m.route,
|
|
1132
|
-
})),
|
|
1133
|
-
allModels: kinded.map((m) => formatModelView(m, syncedSet, config)),
|
|
1134
|
-
};
|
|
1135
|
-
}
|
|
1136
|
-
|
|
1137
|
-
/**
|
|
1138
|
-
* Run one sync. `probe: true` also reads the catalog of a disabled account
|
|
1139
|
-
* so its models can be picked before it is enabled; periodic refreshes of a
|
|
1140
|
-
* disabled account stay offline.
|
|
1141
|
-
*/
|
|
1142
|
-
refresh(options = {}) {
|
|
1143
|
-
if (options.probe === true) this.probeRequested = true;
|
|
1144
|
-
if (this.running !== undefined) {
|
|
1145
|
-
this.rerun = true;
|
|
1146
|
-
return this.running;
|
|
1147
|
-
}
|
|
1148
|
-
this.running = this.runLoop().finally(() => {
|
|
1149
|
-
this.running = undefined;
|
|
1150
|
-
});
|
|
1151
|
-
return this.running;
|
|
1152
|
-
}
|
|
1153
|
-
|
|
1154
|
-
dispose() {
|
|
1155
|
-
this.controller.abort(new Error("dsh-cliproxyapi disposed"));
|
|
1156
|
-
if (this.hmrRetryTimer !== undefined) clearTimeout(this.hmrRetryTimer);
|
|
1157
|
-
if (this.listingRetryTimer !== undefined) clearTimeout(this.listingRetryTimer);
|
|
1158
|
-
}
|
|
1159
|
-
|
|
1160
|
-
/** Stop syncing and remove every route this account wrote. */
|
|
1161
|
-
async teardown(provider) {
|
|
1162
|
-
this.dispose();
|
|
1163
|
-
try {
|
|
1164
|
-
await this.running;
|
|
1165
|
-
} catch {}
|
|
1166
|
-
await this.releaseRoutes([provider, this.activeProvider]);
|
|
1167
|
-
this.activeProvider = undefined;
|
|
1168
|
-
this.activeRoutes = undefined;
|
|
1169
|
-
}
|
|
1170
|
-
|
|
1171
|
-
/** Unset the managed route keys of the given providers that still exist. */
|
|
1172
|
-
async releaseRoutes(providers) {
|
|
1173
|
-
const reserved = this.reservedKeys();
|
|
1174
|
-
const keys = new Set(
|
|
1175
|
-
providers
|
|
1176
|
-
.filter((p) => typeof p === "string" && p !== "")
|
|
1177
|
-
.flatMap((p) => managedRouteKeys(p))
|
|
1178
|
-
);
|
|
1179
|
-
const ops = [];
|
|
1180
|
-
for (const key of keys) {
|
|
1181
|
-
if (reserved.has(key)) continue;
|
|
1182
|
-
if (this.options.readExistingPiAiRoute?.(key) !== undefined) {
|
|
1183
|
-
ops.push({ op: "unset", path: ["providers", key] });
|
|
1184
|
-
}
|
|
1185
|
-
}
|
|
1186
|
-
if (ops.length > 0) await mutateSettingsSafe(this.options.settings, PI_AI_NS, ops);
|
|
1187
|
-
}
|
|
1188
|
-
|
|
1189
|
-
/**
|
|
1190
|
-
* An account that has never saved a selection gets the catalog's
|
|
1191
|
-
* recommendation once, persisted through `options.seedSelection`, so the
|
|
1192
|
-
* picker starts with one model per family instead of the whole catalog.
|
|
1193
|
-
* @returns the seeded ids, or undefined when a selection already exists.
|
|
1194
|
-
*/
|
|
1195
|
-
seedSelection(config, models) {
|
|
1196
|
-
if (config.selectedModels !== undefined || models.length === 0) return undefined;
|
|
1197
|
-
const ids = recommendModels(models);
|
|
1198
|
-
try {
|
|
1199
|
-
this.options.seedSelection?.(ids);
|
|
1200
|
-
} catch (error) {
|
|
1201
|
-
this.options.logger?.warn?.(
|
|
1202
|
-
`dsh-cliproxyapi: could not save the initial model selection: ${error instanceof Error ? error.message : String(error)}`
|
|
1203
|
-
);
|
|
1204
|
-
}
|
|
1205
|
-
return ids;
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
/** Route keys owned by other accounts, from the latest config. */
|
|
1209
|
-
reservedKeys() {
|
|
1210
|
-
try {
|
|
1211
|
-
const keys = this.options.config?.()?.reservedKeys;
|
|
1212
|
-
return new Set(Array.isArray(keys) ? keys : []);
|
|
1213
|
-
} catch {
|
|
1214
|
-
return new Set();
|
|
1215
|
-
}
|
|
1216
|
-
}
|
|
1217
|
-
|
|
1218
|
-
/** Resolve the key and probe the account's CPA service. */
|
|
1219
|
-
async discover(config) {
|
|
1220
|
-
const existingRoute = this.options.readExistingPiAiRoute?.(config.provider);
|
|
1221
|
-
const effectiveApiKeyEnv =
|
|
1222
|
-
config.apiKeyEnv === DEFAULT_API_KEY_ENV &&
|
|
1223
|
-
existingRoute?.apiKeyEnv &&
|
|
1224
|
-
typeof existingRoute.apiKeyEnv === "string"
|
|
1225
|
-
? existingRoute.apiKeyEnv
|
|
1226
|
-
: config.apiKeyEnv;
|
|
1227
|
-
|
|
1228
|
-
const apiKey = await this.options.resolveApiKey(effectiveApiKeyEnv);
|
|
1229
|
-
const autoDiscover = config.baseURL === "";
|
|
1230
|
-
const fallbackBaseURL =
|
|
1231
|
-
autoDiscover && typeof existingRoute?.baseURL === "string" ? existingRoute.baseURL : undefined;
|
|
1232
|
-
const environmentBaseURL = autoDiscover ? this.options.environmentBaseURL?.() : undefined;
|
|
1233
|
-
|
|
1234
|
-
const result = await discoverCpa({
|
|
1235
|
-
baseURL: config.baseURL,
|
|
1236
|
-
autoDiscover,
|
|
1237
|
-
...(fallbackBaseURL === undefined ? {} : { fallbackBaseURL }),
|
|
1238
|
-
...(environmentBaseURL === undefined ? {} : { environmentBaseURL }),
|
|
1239
|
-
...(apiKey === undefined ? {} : { apiKey }),
|
|
1240
|
-
timeoutMs: config.probeTimeoutMs,
|
|
1241
|
-
signal: this.controller.signal,
|
|
1242
|
-
...(this.options.fetch === undefined ? {} : { fetch: this.options.fetch }),
|
|
1243
|
-
});
|
|
1244
|
-
return { result, apiKey, effectiveApiKeyEnv };
|
|
1245
|
-
}
|
|
1246
|
-
|
|
1247
|
-
async runLoop() {
|
|
1248
|
-
do {
|
|
1249
|
-
this.rerun = false;
|
|
1250
|
-
try {
|
|
1251
|
-
await this.syncOnce();
|
|
1252
|
-
this.hmrRetries = 0;
|
|
1253
|
-
} catch (error) {
|
|
1254
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
1255
|
-
this.currentStatus = {
|
|
1256
|
-
...this.currentStatus,
|
|
1257
|
-
state: "error",
|
|
1258
|
-
error: message,
|
|
1259
|
-
};
|
|
1260
|
-
this.options.logger?.warn?.(message);
|
|
1261
|
-
this.scheduleHmrRetry(message);
|
|
1262
|
-
}
|
|
1263
|
-
} while (this.rerun && !this.controller.signal.aborted);
|
|
1264
|
-
}
|
|
1265
|
-
|
|
1266
|
-
/**
|
|
1267
|
-
* A plugin reload holds DSH's HMR transaction for longer than the in-call
|
|
1268
|
-
* backoff; retry the whole sync a few times once that transaction ends.
|
|
1269
|
-
*/
|
|
1270
|
-
scheduleHmrRetry(message) {
|
|
1271
|
-
if (!message.includes("HMR transactions cannot be nested")) return;
|
|
1272
|
-
this.hmrRetries = (this.hmrRetries ?? 0) + 1;
|
|
1273
|
-
if (this.hmrRetries > 5 || this.controller.signal.aborted) return;
|
|
1274
|
-
if (this.hmrRetryTimer !== undefined) clearTimeout(this.hmrRetryTimer);
|
|
1275
|
-
const schedule = this.options.setTimeout ?? setTimeout;
|
|
1276
|
-
this.hmrRetryTimer = schedule(() => {
|
|
1277
|
-
this.hmrRetryTimer = undefined;
|
|
1278
|
-
void this.refresh();
|
|
1279
|
-
}, 3000 * this.hmrRetries);
|
|
1280
|
-
}
|
|
1281
|
-
|
|
1282
|
-
/**
|
|
1283
|
-
* A failed plain listing must not leave image models hidden until the next
|
|
1284
|
-
* five-minute refresh. Re-read soon, at most twice per failure streak.
|
|
1285
|
-
*/
|
|
1286
|
-
scheduleListingRetry() {
|
|
1287
|
-
if (this.listingOk !== false) {
|
|
1288
|
-
this.listingRetries = 0;
|
|
1289
|
-
if (this.listingRetryTimer !== undefined) {
|
|
1290
|
-
clearTimeout(this.listingRetryTimer);
|
|
1291
|
-
this.listingRetryTimer = undefined;
|
|
1292
|
-
}
|
|
1293
|
-
return;
|
|
1294
|
-
}
|
|
1295
|
-
if (this.listingRetries >= 2 || this.controller.signal.aborted || this.listingRetryTimer !== undefined) return;
|
|
1296
|
-
this.listingRetries += 1;
|
|
1297
|
-
const schedule = this.options.setTimeout ?? setTimeout;
|
|
1298
|
-
this.listingRetryTimer = schedule(() => {
|
|
1299
|
-
this.listingRetryTimer = undefined;
|
|
1300
|
-
if (!this.controller.signal.aborted) void this.refresh();
|
|
1301
|
-
}, 4000);
|
|
1302
|
-
}
|
|
1303
|
-
|
|
1304
|
-
async syncOnce() {
|
|
1305
|
-
const config = this.options.config();
|
|
1306
|
-
const probe = this.probeRequested === true;
|
|
1307
|
-
this.probeRequested = false;
|
|
1308
|
-
|
|
1309
|
-
// An account whose route keys belong to another account must not write
|
|
1310
|
-
// or remove them.
|
|
1311
|
-
if (config.conflictWith !== undefined) {
|
|
1312
|
-
this.syncedModels = [];
|
|
1313
|
-
throw new Error(
|
|
1314
|
-
`dsh-cliproxyapi: provider route "${config.provider}" overlaps account "${config.conflictWith}"; choose a different Provider Route ID`
|
|
1315
|
-
);
|
|
1316
|
-
}
|
|
1317
|
-
|
|
1318
|
-
if (!config.enabled) {
|
|
1319
|
-
// A disabled account keeps no models in the picker.
|
|
1320
|
-
await this.releaseRoutes([config.provider, this.activeProvider]);
|
|
1321
|
-
this.activeProvider = undefined;
|
|
1322
|
-
this.activeRoutes = undefined;
|
|
1323
|
-
this.syncedModels = [];
|
|
1324
|
-
let endpoint;
|
|
1325
|
-
let probeError;
|
|
1326
|
-
if (probe) {
|
|
1327
|
-
try {
|
|
1328
|
-
const { result } = await this.discover(config);
|
|
1329
|
-
this.allDiscoveredModels = result.models;
|
|
1330
|
-
this.listingOk = result.listingOk !== false;
|
|
1331
|
-
this.seedSelection(config, this.chatModels(result.models));
|
|
1332
|
-
endpoint = result.rootURL;
|
|
1333
|
-
} catch (error) {
|
|
1334
|
-
if (this.controller.signal.aborted) throw error;
|
|
1335
|
-
probeError = error instanceof Error ? error.message : String(error);
|
|
1336
|
-
}
|
|
1337
|
-
}
|
|
1338
|
-
this.currentStatus = {
|
|
1339
|
-
state: "disabled",
|
|
1340
|
-
provider: config.provider,
|
|
1341
|
-
displayName: config.displayName,
|
|
1342
|
-
...(endpoint === undefined ? {} : { endpoint }),
|
|
1343
|
-
modelCount: 0,
|
|
1344
|
-
totalDiscovered: this.allDiscoveredModels.length,
|
|
1345
|
-
...(probe ? { refreshedAt: new Date().toISOString() } : {}),
|
|
1346
|
-
...(probeError === undefined ? {} : { error: probeError }),
|
|
1347
|
-
};
|
|
1348
|
-
return;
|
|
1349
|
-
}
|
|
1350
|
-
|
|
1351
|
-
const { error: _prevErr, ...prevStatus } = this.currentStatus;
|
|
1352
|
-
this.currentStatus = { ...prevStatus, state: "probing" };
|
|
1353
|
-
|
|
1354
|
-
const { result, apiKey, effectiveApiKeyEnv } = await this.discover(config);
|
|
1355
|
-
// Keep the catalog visible in the panel even if the selection matches nothing.
|
|
1356
|
-
this.allDiscoveredModels = result.models;
|
|
1357
|
-
this.listingOk = result.listingOk !== false;
|
|
1358
|
-
this.scheduleListingRetry();
|
|
1359
|
-
const seeded = this.seedSelection(config, this.chatModels(result.models));
|
|
1360
|
-
|
|
1361
|
-
const effectiveConfig = {
|
|
1362
|
-
...config,
|
|
1363
|
-
...(seeded === undefined ? {} : { selectedModels: seeded }),
|
|
1364
|
-
apiKeyEnv: effectiveApiKeyEnv,
|
|
1365
|
-
};
|
|
1366
|
-
|
|
1367
|
-
const built = buildPiAiRoutes(
|
|
1368
|
-
effectiveConfig,
|
|
1369
|
-
{ ...result, models: this.withKinds(result.models) },
|
|
1370
|
-
apiKey !== undefined
|
|
1371
|
-
);
|
|
1372
|
-
this.syncedModels = built.orderedModels;
|
|
1373
|
-
|
|
1374
|
-
// Each managed route keeps unrelated fields a user set by hand (retry
|
|
1375
|
-
// policy, image budgets) and takes this plugin's fields on top. A route
|
|
1376
|
-
// key that no longer carries models is removed.
|
|
1377
|
-
const reserved = new Set(config.reservedKeys ?? []);
|
|
1378
|
-
const ownedKeys = new Set(
|
|
1379
|
-
[
|
|
1380
|
-
...managedRouteKeys(config.provider),
|
|
1381
|
-
...(this.activeProvider !== undefined && this.activeProvider !== config.provider
|
|
1382
|
-
? managedRouteKeys(this.activeProvider)
|
|
1383
|
-
: []),
|
|
1384
|
-
].filter((key) => !reserved.has(key))
|
|
1385
|
-
);
|
|
1386
|
-
const ops = [];
|
|
1387
|
-
const nextRoutes = {};
|
|
1388
|
-
for (const key of ownedKeys) {
|
|
1389
|
-
const existing = this.options.readExistingPiAiRoute?.(key);
|
|
1390
|
-
const wanted = built.routes[key];
|
|
1391
|
-
if (wanted === undefined) {
|
|
1392
|
-
if (existing !== undefined) ops.push({ op: "unset", path: ["providers", key] });
|
|
1393
|
-
continue;
|
|
1394
|
-
}
|
|
1395
|
-
const merged = {
|
|
1396
|
-
...(existing && typeof existing === "object" ? stripManagedFields(existing) : {}),
|
|
1397
|
-
...wanted,
|
|
1398
|
-
};
|
|
1399
|
-
nextRoutes[key] = merged;
|
|
1400
|
-
if (!sameJson(existing, merged)) {
|
|
1401
|
-
ops.push({ op: "set", path: ["providers", key], value: merged });
|
|
1402
|
-
}
|
|
1403
|
-
}
|
|
1404
|
-
if (ops.length > 0) {
|
|
1405
|
-
await mutateSettingsSafe(this.options.settings, PI_AI_NS, ops);
|
|
1406
|
-
}
|
|
1407
|
-
this.activeProvider = config.provider;
|
|
1408
|
-
this.activeRoutes = nextRoutes;
|
|
1409
|
-
await this.followDefaultModel(built.orderedModels, ownedKeys);
|
|
1410
|
-
|
|
1411
|
-
const protocolCounts = {};
|
|
1412
|
-
for (const m of built.orderedModels) protocolCounts[m.protocol] = (protocolCounts[m.protocol] ?? 0) + 1;
|
|
1413
|
-
this.currentStatus = {
|
|
1414
|
-
state: "connected",
|
|
1415
|
-
endpoint: result.rootURL,
|
|
1416
|
-
provider: config.provider,
|
|
1417
|
-
displayName: config.displayName,
|
|
1418
|
-
routes: Object.keys(built.routes),
|
|
1419
|
-
protocolCounts,
|
|
1420
|
-
modelCount: built.orderedModels.length,
|
|
1421
|
-
totalDiscovered: result.models.length,
|
|
1422
|
-
refreshedAt: new Date().toISOString(),
|
|
1423
|
-
};
|
|
1424
|
-
this.options.logger?.info?.(
|
|
1425
|
-
`dsh-cliproxyapi: connected to ${result.rootURL}; synchronized ${built.orderedModels.length}/${result.models.length} model(s) into ${Object.keys(built.routes).join(", ")}`
|
|
1426
|
-
);
|
|
1427
|
-
}
|
|
1428
|
-
|
|
1429
|
-
/**
|
|
1430
|
-
* Keep the agent default model reachable when protocol routing moves it to
|
|
1431
|
-
* a sibling route (e.g. `cli-proxy` -> `cli-proxy-messages`).
|
|
1432
|
-
*/
|
|
1433
|
-
async followDefaultModel(models, ownedKeys) {
|
|
1434
|
-
const current = this.options.readDefaultModel?.();
|
|
1435
|
-
if (current === undefined || !ownedKeys.has(current.provider)) return;
|
|
1436
|
-
const target = models.find((m) => m.id === current.model);
|
|
1437
|
-
if (target === undefined || target.route === current.provider) return;
|
|
1438
|
-
try {
|
|
1439
|
-
await mutateSettingsSafe(this.options.settings, DEFAULT_MODEL_NS, [
|
|
1440
|
-
{ op: "set", path: ["provider"], value: target.route },
|
|
1441
|
-
]);
|
|
1442
|
-
} catch (error) {
|
|
1443
|
-
this.options.logger?.warn?.(
|
|
1444
|
-
`dsh-cliproxyapi: could not move default model to ${target.route}: ${error instanceof Error ? error.message : String(error)}`
|
|
1445
|
-
);
|
|
1446
|
-
}
|
|
1447
|
-
}
|
|
1448
|
-
}
|
|
1449
|
-
|
|
1450
|
-
/**
|
|
1451
|
-
* Queue settings writes so concurrent account syncs never interleave their
|
|
1452
|
-
* transactions on the shared `llm-pi-ai` namespace.
|
|
1453
|
-
*/
|
|
1454
|
-
export function serializeSettings(settings) {
|
|
1455
|
-
let tail = Promise.resolve();
|
|
1456
|
-
return {
|
|
1457
|
-
mutate(ns, ops) {
|
|
1458
|
-
const run = tail.then(() => settings.mutate(ns, ops));
|
|
1459
|
-
tail = run.catch(() => {});
|
|
1460
|
-
return run;
|
|
1461
|
-
},
|
|
1462
|
-
};
|
|
1463
|
-
}
|
|
1464
|
-
|
|
1465
|
-
/**
|
|
1466
|
-
* Owns one {@link CpaRuntime} per account and the state file they share.
|
|
1467
|
-
*
|
|
1468
|
-
* @param deps.readState - returns the normalized state (see {@link normalizeState}).
|
|
1469
|
-
* @param deps.writeState - persists a normalized state.
|
|
1470
|
-
* @param deps.resolveApiKey / deps.describeApiKey - credential lookups by ref.
|
|
1471
|
-
* @param deps.setCredential / deps.unsetCredential - credential writes by ref.
|
|
1472
|
-
* @param deps.onGlobalsChanged - called after the refresh interval changes.
|
|
1473
|
-
*/
|
|
1474
|
-
export function createCpaController(deps) {
|
|
1475
|
-
const settings = serializeSettings(deps.settings);
|
|
1476
|
-
const runtimes = new Map();
|
|
1477
|
-
|
|
1478
|
-
const accountConfig = (state, id) => {
|
|
1479
|
-
const account = state.accounts.find((a) => a.id === id);
|
|
1480
|
-
if (account === undefined) return undefined;
|
|
1481
|
-
const conflicts = accountConflicts(state.accounts);
|
|
1482
|
-
const conflictWith = conflicts.get(id);
|
|
1483
|
-
return {
|
|
1484
|
-
...resolveConfig({
|
|
1485
|
-
...account,
|
|
1486
|
-
refreshIntervalSeconds: state.refreshIntervalSeconds,
|
|
1487
|
-
probeTimeoutMs: state.probeTimeoutMs,
|
|
1488
|
-
}),
|
|
1489
|
-
// Route keys of every other (non-conflicting) account: never written or
|
|
1490
|
-
// removed here. A conflicting account owns no keys.
|
|
1491
|
-
reservedKeys: state.accounts
|
|
1492
|
-
.filter((a) => a.id !== id && !conflicts.has(a.id))
|
|
1493
|
-
.flatMap((a) => managedRouteKeys(a.provider)),
|
|
1494
|
-
...(conflictWith === undefined ? {} : { conflictWith }),
|
|
1495
|
-
};
|
|
1496
|
-
};
|
|
1497
|
-
|
|
1498
|
-
const ensureRuntime = (id) => {
|
|
1499
|
-
const existing = runtimes.get(id);
|
|
1500
|
-
if (existing !== undefined) return existing;
|
|
1501
|
-
let last = accountConfig(deps.readState(), id) ?? resolveConfig({ id, enabled: false });
|
|
1502
|
-
const runtime = new CpaRuntime({
|
|
1503
|
-
settings,
|
|
1504
|
-
config: () => {
|
|
1505
|
-
const config = accountConfig(deps.readState(), id);
|
|
1506
|
-
if (config !== undefined) last = config;
|
|
1507
|
-
return last;
|
|
1508
|
-
},
|
|
1509
|
-
resolveApiKey: deps.resolveApiKey,
|
|
1510
|
-
readExistingPiAiRoute: deps.readExistingPiAiRoute,
|
|
1511
|
-
readDefaultModel: deps.readDefaultModel,
|
|
1512
|
-
// User reclassifications (chat / image / video) from media-settings.json.
|
|
1513
|
-
kindOverrides: () => deps.mediaKindOverrides?.(id) ?? {},
|
|
1514
|
-
// Persist a first-run selection unless one was saved in the meantime.
|
|
1515
|
-
seedSelection: (ids) => {
|
|
1516
|
-
const state = deps.readState();
|
|
1517
|
-
const account = state.accounts.find((a) => a.id === id);
|
|
1518
|
-
if (account === undefined || account.selectedModels !== undefined) return;
|
|
1519
|
-
deps.writeState(
|
|
1520
|
-
normalizeState({
|
|
1521
|
-
...state,
|
|
1522
|
-
accounts: state.accounts.map((a) => (a.id === id ? { ...a, selectedModels: ids } : a)),
|
|
1523
|
-
})
|
|
1524
|
-
);
|
|
1525
|
-
},
|
|
1526
|
-
...(deps.setTimeout === undefined ? {} : { setTimeout: deps.setTimeout }),
|
|
1527
|
-
...(deps.environmentBaseURL === undefined ? {} : { environmentBaseURL: deps.environmentBaseURL }),
|
|
1528
|
-
...(deps.fetch === undefined ? {} : { fetch: deps.fetch }),
|
|
1529
|
-
logger: deps.logger,
|
|
1530
|
-
});
|
|
1531
|
-
runtime.lastProvider = () => last.provider;
|
|
1532
|
-
runtimes.set(id, runtime);
|
|
1533
|
-
return runtime;
|
|
1534
|
-
};
|
|
1535
|
-
|
|
1536
|
-
/** Create runtimes for new accounts and tear down removed ones. */
|
|
1537
|
-
const reconcile = async (state) => {
|
|
1538
|
-
for (const account of state.accounts) ensureRuntime(account.id);
|
|
1539
|
-
const live = new Set(state.accounts.map((a) => a.id));
|
|
1540
|
-
const removed = [...runtimes.entries()].filter(([id]) => !live.has(id));
|
|
1541
|
-
for (const [id, runtime] of removed) {
|
|
1542
|
-
runtimes.delete(id);
|
|
1543
|
-
try {
|
|
1544
|
-
await runtime.teardown(runtime.lastProvider());
|
|
1545
|
-
} catch (error) {
|
|
1546
|
-
deps.logger?.warn?.(
|
|
1547
|
-
`dsh-cliproxyapi: could not remove routes of account ${id}: ${error instanceof Error ? error.message : String(error)}`
|
|
1548
|
-
);
|
|
1549
|
-
}
|
|
1550
|
-
}
|
|
1551
|
-
};
|
|
1552
|
-
|
|
1553
|
-
const findAccount = (state, ref) => {
|
|
1554
|
-
if (ref === undefined || ref === null || String(ref).trim() === "") {
|
|
1555
|
-
if (state.accounts.length === 0) {
|
|
1556
|
-
throw new Error("dsh-cliproxyapi: no CLIProxyAPI account is configured; add one first");
|
|
1557
|
-
}
|
|
1558
|
-
return state.accounts[0];
|
|
1559
|
-
}
|
|
1560
|
-
const wanted = String(ref).trim();
|
|
1561
|
-
const hit =
|
|
1562
|
-
state.accounts.find((a) => a.id === wanted) ??
|
|
1563
|
-
state.accounts.find((a) => a.provider === wanted) ??
|
|
1564
|
-
state.accounts.find((a) => a.displayName === wanted);
|
|
1565
|
-
if (hit === undefined) {
|
|
1566
|
-
throw new Error(
|
|
1567
|
-
`dsh-cliproxyapi: unknown account "${wanted}" (known: ${state.accounts.map((a) => a.id).join(", ") || "none"})`
|
|
1568
|
-
);
|
|
1569
|
-
}
|
|
1570
|
-
return hit;
|
|
1571
|
-
};
|
|
1572
|
-
|
|
1573
|
-
const effectiveKeyEnv = (config) => {
|
|
1574
|
-
const route = deps.readExistingPiAiRoute?.(config.provider);
|
|
1575
|
-
return config.apiKeyEnv === DEFAULT_API_KEY_ENV && typeof route?.apiKeyEnv === "string"
|
|
1576
|
-
? route.apiKeyEnv
|
|
1577
|
-
: config.apiKeyEnv;
|
|
1578
|
-
};
|
|
1579
|
-
|
|
1580
|
-
const describeAccount = async (state, account) => {
|
|
1581
|
-
const config = accountConfig(state, account.id);
|
|
1582
|
-
const runtime = ensureRuntime(account.id);
|
|
1583
|
-
const st = runtime.status();
|
|
1584
|
-
const existingRoute = deps.readExistingPiAiRoute?.(config.provider);
|
|
1585
|
-
const keyEnv = effectiveKeyEnv(config);
|
|
1586
|
-
const keyInfo = (await deps.describeApiKey?.(keyEnv)) ?? { configured: false };
|
|
1587
|
-
return {
|
|
1588
|
-
id: account.id,
|
|
1589
|
-
config: {
|
|
1590
|
-
...config,
|
|
1591
|
-
selectedModels: config.selectedModels ?? [],
|
|
1592
|
-
// false until a selection is saved or seeded by the first probe.
|
|
1593
|
-
selectionSaved: config.selectedModels !== undefined,
|
|
1594
|
-
modelOverrides: config.modelOverrides ?? {},
|
|
1595
|
-
effectiveBaseURL: config.baseURL || st.endpoint || existingRoute?.baseURL || "",
|
|
1596
|
-
effectiveApiKeyEnv: keyEnv,
|
|
1597
|
-
},
|
|
1598
|
-
credential: {
|
|
1599
|
-
ref: keyEnv,
|
|
1600
|
-
configured: keyInfo.configured === true,
|
|
1601
|
-
writable: keyInfo.writable !== false,
|
|
1602
|
-
source: keyInfo.source ?? null,
|
|
1603
|
-
},
|
|
1604
|
-
// One model per family from this account's catalog (the panel's reset button).
|
|
1605
|
-
recommendedModels: recommendModels(runtime.chatModels()),
|
|
1606
|
-
status: st,
|
|
1607
|
-
};
|
|
1608
|
-
};
|
|
1609
|
-
|
|
1610
|
-
const statusPayload = async (extra = {}) => {
|
|
1611
|
-
const state = deps.readState();
|
|
1612
|
-
const accounts = [];
|
|
1613
|
-
for (const account of state.accounts) accounts.push(await describeAccount(state, account));
|
|
1614
|
-
return {
|
|
1615
|
-
ok: true,
|
|
1616
|
-
global: {
|
|
1617
|
-
refreshIntervalSeconds: state.refreshIntervalSeconds,
|
|
1618
|
-
probeTimeoutMs: state.probeTimeoutMs,
|
|
1619
|
-
},
|
|
1620
|
-
accounts,
|
|
1621
|
-
...extra,
|
|
1622
|
-
};
|
|
1623
|
-
};
|
|
1624
|
-
|
|
1625
|
-
const applyKeyPatch = async (account, patch) => {
|
|
1626
|
-
const ref = toCredentialRef(account.apiKeyEnv);
|
|
1627
|
-
if (typeof patch.apiKey === "string" && patch.apiKey.trim() !== "") {
|
|
1628
|
-
if (deps.setCredential === undefined) {
|
|
1629
|
-
throw new Error("Credentials service is not available on this host");
|
|
1630
|
-
}
|
|
1631
|
-
await deps.setCredential(ref, patch.apiKey.trim());
|
|
1632
|
-
} else if (patch.clearApiKey === true) {
|
|
1633
|
-
await deps.unsetCredential?.(ref);
|
|
1634
|
-
}
|
|
1635
|
-
};
|
|
1636
|
-
|
|
1637
|
-
const writeAccounts = (state, accounts, patch = {}) => {
|
|
1638
|
-
assertNoConflicts(accounts);
|
|
1639
|
-
for (const account of accounts) toCredentialRef(account.apiKeyEnv);
|
|
1640
|
-
const next = normalizeState({ ...state, ...pickFields(patch, GLOBAL_FIELDS), accounts });
|
|
1641
|
-
deps.writeState(next);
|
|
1642
|
-
if (next.refreshIntervalSeconds !== state.refreshIntervalSeconds) deps.onGlobalsChanged?.(next);
|
|
1643
|
-
return next;
|
|
1644
|
-
};
|
|
1645
|
-
|
|
1646
|
-
const refreshAccount = (id, options) => ensureRuntime(id).refresh(options);
|
|
1647
|
-
|
|
1648
|
-
return {
|
|
1649
|
-
statusPayload,
|
|
1650
|
-
findAccount: (ref) => findAccount(deps.readState(), ref),
|
|
1651
|
-
|
|
1652
|
-
/** Refresh every account (no catalog probe for disabled ones). */
|
|
1653
|
-
async refreshAll(options = {}) {
|
|
1654
|
-
const state = deps.readState();
|
|
1655
|
-
await reconcile(state);
|
|
1656
|
-
await Promise.all(state.accounts.map((a) => refreshAccount(a.id, options)));
|
|
1657
|
-
},
|
|
1658
|
-
|
|
1659
|
-
/** Refresh one account, or all of them when `ref` is omitted. */
|
|
1660
|
-
async refresh(ref, options = { probe: true }) {
|
|
1661
|
-
const state = deps.readState();
|
|
1662
|
-
await reconcile(state);
|
|
1663
|
-
if (ref === undefined || ref === null || String(ref).trim() === "") {
|
|
1664
|
-
await Promise.all(state.accounts.map((a) => refreshAccount(a.id, options)));
|
|
1665
|
-
return statusPayload();
|
|
1666
|
-
}
|
|
1667
|
-
const account = findAccount(state, ref);
|
|
1668
|
-
await refreshAccount(account.id, options);
|
|
1669
|
-
return statusPayload({ account: account.id });
|
|
1670
|
-
},
|
|
1671
|
-
|
|
1672
|
-
/** Update one account's settings (and globals in the same patch). */
|
|
1673
|
-
async updateAccount(ref, patch = {}) {
|
|
1674
|
-
const state = deps.readState();
|
|
1675
|
-
const account = findAccount(state, ref);
|
|
1676
|
-
const updated = patchAccount(account, patch);
|
|
1677
|
-
const accounts = state.accounts.map((a) => (a.id === account.id ? updated : a));
|
|
1678
|
-
writeAccounts(state, accounts, patch);
|
|
1679
|
-
await applyKeyPatch(updated, patch);
|
|
1680
|
-
await refreshAccount(account.id, { probe: true });
|
|
1681
|
-
return statusPayload({ account: account.id });
|
|
1682
|
-
},
|
|
1683
|
-
|
|
1684
|
-
/** Update only the global settings. */
|
|
1685
|
-
async updateGlobals(patch = {}) {
|
|
1686
|
-
const state = deps.readState();
|
|
1687
|
-
writeAccounts(state, state.accounts, patch);
|
|
1688
|
-
return statusPayload();
|
|
1689
|
-
},
|
|
1690
|
-
|
|
1691
|
-
/** Add an account; it starts disabled unless `patch.enabled` is true. */
|
|
1692
|
-
async addAccount(patch = {}) {
|
|
1693
|
-
const state = deps.readState();
|
|
1694
|
-
const defaults = newAccountDefaults(state.accounts);
|
|
1695
|
-
const created = patchAccount(defaults, {
|
|
1696
|
-
...patch,
|
|
1697
|
-
enabled: patch.enabled === true,
|
|
1698
|
-
});
|
|
1699
|
-
const accounts = [...state.accounts, created];
|
|
1700
|
-
const next = writeAccounts(state, accounts, patch);
|
|
1701
|
-
await applyKeyPatch(created, patch);
|
|
1702
|
-
await reconcile(next);
|
|
1703
|
-
await refreshAccount(created.id, { probe: true });
|
|
1704
|
-
return statusPayload({ account: created.id, createdId: created.id });
|
|
1705
|
-
},
|
|
1706
|
-
|
|
1707
|
-
/** Remove an account and every route it synchronized. */
|
|
1708
|
-
async removeAccount(ref) {
|
|
1709
|
-
if (ref === undefined || ref === null || String(ref).trim() === "") {
|
|
1710
|
-
throw new Error("dsh-cliproxyapi: removing an account needs its id");
|
|
1711
|
-
}
|
|
1712
|
-
const state = deps.readState();
|
|
1713
|
-
const account = findAccount(state, ref);
|
|
1714
|
-
// Capture the account's provider (and reserved keys) before it leaves the state.
|
|
1715
|
-
ensureRuntime(account.id).options.config();
|
|
1716
|
-
const next = writeAccounts(
|
|
1717
|
-
state,
|
|
1718
|
-
state.accounts.filter((a) => a.id !== account.id)
|
|
1719
|
-
);
|
|
1720
|
-
await reconcile(next);
|
|
1721
|
-
return statusPayload({ removedId: account.id });
|
|
1722
|
-
},
|
|
1723
|
-
|
|
1724
|
-
/**
|
|
1725
|
-
* Probe a CPA address with unsaved form values, without saving or writing
|
|
1726
|
-
* routes. A blank `apiKey` uses the account's stored key; a blank
|
|
1727
|
-
* `baseURL` follows the usual fallbacks. Never returns the key.
|
|
1728
|
-
*/
|
|
1729
|
-
async testConnection(ref, input = {}) {
|
|
1730
|
-
const state = deps.readState();
|
|
1731
|
-
const account = ref === undefined || ref === null || String(ref).trim() === "" ? undefined : findAccount(state, ref);
|
|
1732
|
-
const config = account === undefined ? undefined : accountConfig(state, account.id);
|
|
1733
|
-
const draftKey = typeof input.apiKey === "string" ? input.apiKey.trim() : "";
|
|
1734
|
-
const apiKey = draftKey !== "" ? draftKey : config === undefined ? undefined : await deps.resolveApiKey(effectiveKeyEnv(config));
|
|
1735
|
-
const baseURL = typeof input.baseURL === "string" ? input.baseURL.trim() : "";
|
|
1736
|
-
try {
|
|
1737
|
-
const result = await discoverCpa({
|
|
1738
|
-
baseURL,
|
|
1739
|
-
autoDiscover: baseURL === "",
|
|
1740
|
-
fallbackBaseURL: config === undefined ? undefined : deps.readExistingPiAiRoute?.(config.provider)?.baseURL,
|
|
1741
|
-
environmentBaseURL: deps.environmentBaseURL?.(),
|
|
1742
|
-
apiKey,
|
|
1743
|
-
timeoutMs: state.probeTimeoutMs,
|
|
1744
|
-
...(deps.fetch === undefined ? {} : { fetch: deps.fetch }),
|
|
1745
|
-
});
|
|
1746
|
-
return { ok: true, test: { ok: true, endpoint: result.rootURL, modelCount: result.models.length } };
|
|
1747
|
-
} catch (error) {
|
|
1748
|
-
return { ok: true, test: { ok: false, error: error instanceof Error ? error.message : String(error) } };
|
|
1749
|
-
}
|
|
1750
|
-
},
|
|
1751
|
-
|
|
1752
|
-
/** Accounts whose credential reference is `ref`. */
|
|
1753
|
-
accountsUsingKey(ref) {
|
|
1754
|
-
const state = deps.readState();
|
|
1755
|
-
return state.accounts
|
|
1756
|
-
.map((a) => accountConfig(state, a.id))
|
|
1757
|
-
.filter((config) => effectiveKeyEnv(config) === ref)
|
|
1758
|
-
.map((config) => config.id);
|
|
1759
|
-
},
|
|
1760
|
-
|
|
1761
|
-
refreshAccount,
|
|
1762
|
-
|
|
1763
|
-
/**
|
|
1764
|
-
* Account facts the quota engine needs: the CPA address it talks to and
|
|
1765
|
-
* the DSH route keys whose models belong to this account. A conflicting
|
|
1766
|
-
* account owns no routes, so none of its models map to quota.
|
|
1767
|
-
*/
|
|
1768
|
-
quotaAccounts() {
|
|
1769
|
-
const state = deps.readState();
|
|
1770
|
-
const conflicts = accountConflicts(state.accounts);
|
|
1771
|
-
return state.accounts.map((account) => {
|
|
1772
|
-
const config = accountConfig(state, account.id);
|
|
1773
|
-
const runtime = runtimes.get(account.id);
|
|
1774
|
-
const st = runtime?.currentStatus ?? {};
|
|
1775
|
-
const existingRoute = deps.readExistingPiAiRoute?.(config.provider);
|
|
1776
|
-
const owners = new Map((runtime?.allDiscoveredModels ?? []).map((m) => [m.id, m.ownedBy]));
|
|
1777
|
-
return {
|
|
1778
|
-
id: account.id,
|
|
1779
|
-
displayName: config.displayName,
|
|
1780
|
-
provider: config.provider,
|
|
1781
|
-
enabled: config.enabled === true,
|
|
1782
|
-
baseURL: config.baseURL || "",
|
|
1783
|
-
endpoint: st.endpoint || existingRoute?.baseURL || "",
|
|
1784
|
-
routes: conflicts.has(account.id) ? [] : managedRouteKeys(config.provider),
|
|
1785
|
-
// Models this account currently puts in the DSH picker.
|
|
1786
|
-
pickerModels: (runtime?.syncedModels ?? []).map((m) => ({
|
|
1787
|
-
id: m.id,
|
|
1788
|
-
route: m.route,
|
|
1789
|
-
...(typeof owners.get(m.id) === "string" ? { ownedBy: owners.get(m.id) } : {}),
|
|
1790
|
-
})),
|
|
1791
|
-
};
|
|
1792
|
-
});
|
|
1793
|
-
},
|
|
1794
|
-
|
|
1795
|
-
/**
|
|
1796
|
-
* Media models per account, from the last catalog each runtime saw, with
|
|
1797
|
-
* their effective and detected kind. `chatModelIds` lets the panel mark a
|
|
1798
|
-
* chat model as media by hand. No network access.
|
|
1799
|
-
*/
|
|
1800
|
-
mediaAccounts() {
|
|
1801
|
-
const state = deps.readState();
|
|
1802
|
-
return state.accounts.map((account) => {
|
|
1803
|
-
const config = accountConfig(state, account.id);
|
|
1804
|
-
const runtime = ensureRuntime(account.id);
|
|
1805
|
-
const st = runtime.currentStatus ?? {};
|
|
1806
|
-
const kinded = runtime.withKinds();
|
|
1807
|
-
return {
|
|
1808
|
-
id: account.id,
|
|
1809
|
-
displayName: config.displayName,
|
|
1810
|
-
enabled: config.enabled === true,
|
|
1811
|
-
state: config.enabled === true ? st.state ?? "idle" : "disabled",
|
|
1812
|
-
endpoint: st.endpoint || "",
|
|
1813
|
-
discovered: kinded.length,
|
|
1814
|
-
listingOk: runtime.listingOk !== false,
|
|
1815
|
-
catalogIncomplete: runtime.listingOk === false,
|
|
1816
|
-
models: kinded
|
|
1817
|
-
.filter((m) => !isChatModel(m))
|
|
1818
|
-
.map((m) => ({
|
|
1819
|
-
id: m.id,
|
|
1820
|
-
...(m.name === undefined ? {} : { name: m.name }),
|
|
1821
|
-
kind: m.kind,
|
|
1822
|
-
detectedKind: m.detectedKind ?? "chat",
|
|
1823
|
-
...(mediaAliasTarget(m.id) === undefined ? {} : { aliasOf: mediaAliasTarget(m.id) }),
|
|
1824
|
-
})),
|
|
1825
|
-
chatModelIds: kinded.filter(isChatModel).map((m) => m.id),
|
|
1826
|
-
};
|
|
1827
|
-
});
|
|
1828
|
-
},
|
|
1829
|
-
|
|
1830
|
-
/**
|
|
1831
|
-
* Read-only media diagnostic for one account (see diagnoseMedia in
|
|
1832
|
-
* lib/media.js). Uses the account's key on the host; never returns it.
|
|
1833
|
-
*/
|
|
1834
|
-
/**
|
|
1835
|
-
* The CPA root and key a media request of this account uses: the address
|
|
1836
|
-
* the last sync connected to, else the configured or route address.
|
|
1837
|
-
* Host-only; the key is never returned to a page.
|
|
1838
|
-
* @throws MediaError `no-endpoint`.
|
|
1839
|
-
*/
|
|
1840
|
-
async mediaEndpoint(accountId) {
|
|
1841
|
-
const state = deps.readState();
|
|
1842
|
-
const account = state.accounts.find((a) => a.id === accountId);
|
|
1843
|
-
if (account === undefined) throw new MediaError("invalid-account");
|
|
1844
|
-
const config = accountConfig(state, account.id);
|
|
1845
|
-
const runtime = ensureRuntime(account.id);
|
|
1846
|
-
const known = runtime.currentStatus?.endpoint || config.baseURL || deps.readExistingPiAiRoute?.(config.provider)?.baseURL || "";
|
|
1847
|
-
let rootURL;
|
|
1848
|
-
try {
|
|
1849
|
-
rootURL = normalizeCpaRoot(known);
|
|
1850
|
-
} catch {
|
|
1851
|
-
throw new MediaError("no-endpoint");
|
|
1852
|
-
}
|
|
1853
|
-
return { rootURL, apiKey: await deps.resolveApiKey(effectiveKeyEnv(config)), probeTimeoutMs: state.probeTimeoutMs };
|
|
1854
|
-
},
|
|
1855
|
-
|
|
1856
|
-
async diagnoseMedia(ref) {
|
|
1857
|
-
const state = deps.readState();
|
|
1858
|
-
const account = findAccount(state, ref);
|
|
1859
|
-
const { rootURL, apiKey } = await this.mediaEndpoint(account.id);
|
|
1860
|
-
const diagnostic = await diagnoseMedia({
|
|
1861
|
-
rootURL,
|
|
1862
|
-
apiKey,
|
|
1863
|
-
timeoutMs: Math.max(state.probeTimeoutMs, 8000),
|
|
1864
|
-
...(deps.fetch === undefined ? {} : { fetch: deps.fetch }),
|
|
1865
|
-
});
|
|
1866
|
-
return { accountId: account.id, keyConfigured: apiKey !== undefined, ...diagnostic };
|
|
1867
|
-
},
|
|
1868
|
-
|
|
1869
|
-
dispose() {
|
|
1870
|
-
for (const runtime of runtimes.values()) runtime.dispose();
|
|
1871
|
-
runtimes.clear();
|
|
1872
|
-
},
|
|
1873
|
-
};
|
|
1874
|
-
}
|
|
1875
|
-
|
|
1876
|
-
// #endregion
|
|
1877
|
-
|
|
1878
|
-
// #region HTTP Gate & WebServer Routes
|
|
1879
|
-
|
|
1880
|
-
export const ROUTES = {
|
|
1881
|
-
status: "/api/dsh-cliproxyapi/status",
|
|
1882
|
-
refresh: "/api/dsh-cliproxyapi/refresh",
|
|
1883
|
-
save: "/api/dsh-cliproxyapi/save",
|
|
1884
|
-
addAccount: "/api/dsh-cliproxyapi/accounts/add",
|
|
1885
|
-
removeAccount: "/api/dsh-cliproxyapi/accounts/remove",
|
|
1886
|
-
testAccount: "/api/dsh-cliproxyapi/accounts/test",
|
|
1887
|
-
updateStatus: "/api/dsh-cliproxyapi/updates/status",
|
|
1888
|
-
updateCheck: "/api/dsh-cliproxyapi/updates/check",
|
|
1889
|
-
updateSave: "/api/dsh-cliproxyapi/updates/save",
|
|
1890
|
-
updateDownload: "/api/dsh-cliproxyapi/updates/download",
|
|
1891
|
-
quotaStatus: "/api/dsh-cliproxyapi/quota/status",
|
|
1892
|
-
quotaRefresh: "/api/dsh-cliproxyapi/quota/refresh",
|
|
1893
|
-
quotaSave: "/api/dsh-cliproxyapi/quota/save",
|
|
1894
|
-
mediaStatus: "/api/dsh-cliproxyapi/media/status",
|
|
1895
|
-
mediaSave: "/api/dsh-cliproxyapi/media/save",
|
|
1896
|
-
mediaDiagnose: "/api/dsh-cliproxyapi/media/diagnose",
|
|
1897
|
-
mediaSession: "/api/dsh-cliproxyapi/media/session",
|
|
1898
|
-
mediaSessionSave: "/api/dsh-cliproxyapi/media/session/save",
|
|
1899
|
-
mediaFile: "/api/dsh-cliproxyapi/media/file",
|
|
1900
|
-
mediaCommand: "/api/dsh-cliproxyapi/media/command",
|
|
1901
|
-
mediaConfirm: "/api/dsh-cliproxyapi/media/confirm",
|
|
1902
|
-
};
|
|
1903
|
-
|
|
1904
|
-
function sendJson(res, status, payload) {
|
|
1905
|
-
res.writeHead(status, {
|
|
1906
|
-
"cache-control": "no-store",
|
|
1907
|
-
"content-type": "application/json; charset=utf-8",
|
|
1908
|
-
});
|
|
1909
|
-
res.end(JSON.stringify(payload));
|
|
1910
|
-
}
|
|
1911
|
-
|
|
1912
|
-
function loopbackAuthority(host) {
|
|
1913
|
-
if (typeof host !== "string") return false;
|
|
1914
|
-
const lower = host.toLowerCase();
|
|
1915
|
-
const name = lower.startsWith("[")
|
|
1916
|
-
? lower.slice(0, lower.indexOf("]") + 1)
|
|
1917
|
-
: lower.split(":")[0];
|
|
1918
|
-
return (
|
|
1919
|
-
name === "127.0.0.1" ||
|
|
1920
|
-
name === "localhost" ||
|
|
1921
|
-
name === "[::1]" ||
|
|
1922
|
-
/^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(name)
|
|
1923
|
-
);
|
|
1924
|
-
}
|
|
1925
|
-
|
|
1926
|
-
function admitRequest(ctx, req, res, expectedMethod) {
|
|
1927
|
-
if (req.method !== expectedMethod) {
|
|
1928
|
-
sendJson(res, 405, { error: "Method Not Allowed" });
|
|
1929
|
-
return false;
|
|
1930
|
-
}
|
|
1931
|
-
const connection = ctx.get?.("connection");
|
|
1932
|
-
if (typeof connection?.requestRejection === "function") {
|
|
1933
|
-
let rejection;
|
|
1934
|
-
try {
|
|
1935
|
-
rejection = connection.requestRejection(req);
|
|
1936
|
-
} catch {
|
|
1937
|
-
rejection = 403;
|
|
1938
|
-
}
|
|
1939
|
-
if (rejection !== undefined) {
|
|
1940
|
-
const code = typeof rejection === "number" ? rejection : rejection?.status ?? 403;
|
|
1941
|
-
sendJson(res, code, { error: "Request rejected by host connection security policy" });
|
|
1942
|
-
return false;
|
|
1943
|
-
}
|
|
1944
|
-
}
|
|
1945
|
-
if (expectedMethod === "POST") {
|
|
1946
|
-
const host = req.headers.host;
|
|
1947
|
-
const trusted = Array.isArray(connection?.trustedHosts) ? connection.trustedHosts : [];
|
|
1948
|
-
if (
|
|
1949
|
-
host !== undefined &&
|
|
1950
|
-
!loopbackAuthority(host) &&
|
|
1951
|
-
!trusted.some((t) => String(t).toLowerCase() === host.toLowerCase())
|
|
1952
|
-
) {
|
|
1953
|
-
sendJson(res, 403, { error: "Forbidden host" });
|
|
1954
|
-
return false;
|
|
1955
|
-
}
|
|
1956
|
-
if (req.headers["sec-fetch-site"] === "cross-site") {
|
|
1957
|
-
sendJson(res, 403, { error: "Cross-site request forbidden" });
|
|
1958
|
-
return false;
|
|
1959
|
-
}
|
|
1960
|
-
const origin = req.headers.origin;
|
|
1961
|
-
if (origin !== undefined) {
|
|
1962
|
-
try {
|
|
1963
|
-
if (new URL(origin).host !== host) {
|
|
1964
|
-
sendJson(res, 403, { error: "Origin mismatch" });
|
|
1965
|
-
return false;
|
|
1966
|
-
}
|
|
1967
|
-
} catch {
|
|
1968
|
-
sendJson(res, 403, { error: "Invalid Origin" });
|
|
1969
|
-
return false;
|
|
1970
|
-
}
|
|
1971
|
-
}
|
|
1972
|
-
}
|
|
1973
|
-
return true;
|
|
1974
|
-
}
|
|
1975
|
-
|
|
1976
|
-
/** Same authority, using the scheme the Origin itself declares (host comparison only). */
|
|
1977
|
-
function sameAuthorityOrigin(origin, host) {
|
|
1978
|
-
let originUrl;
|
|
1979
|
-
try { originUrl = new URL(origin); } catch { return false; }
|
|
1980
|
-
if (originUrl.protocol !== "http:" && originUrl.protocol !== "https:") return false;
|
|
1981
|
-
if (originUrl.username || originUrl.password) return false;
|
|
1982
|
-
try {
|
|
1983
|
-
return originUrl.host === new URL(`${originUrl.protocol}//${host}`).host;
|
|
1984
|
-
} catch {
|
|
1985
|
-
return false;
|
|
1986
|
-
}
|
|
1987
|
-
}
|
|
1988
|
-
|
|
1989
|
-
/**
|
|
1990
|
-
* Update writes need an authenticated browser request. The host connection
|
|
1991
|
-
* service already enforces the Host/Origin fence and its session cookie, so
|
|
1992
|
-
* this only narrows it: when a browser sends an Origin it must belong to this
|
|
1993
|
-
* listener. A carrier that omits Origin (the desktop shell proxies API calls
|
|
1994
|
-
* without it) is allowed, because the session cookie is still required and an
|
|
1995
|
-
* unauthenticated caller is rejected before reaching the route.
|
|
1996
|
-
*/
|
|
1997
|
-
function admitUpdateRequest(ctx, req, res, method) {
|
|
1998
|
-
if (!admitRequest(ctx, req, res, method)) return false;
|
|
1999
|
-
if (method === "GET") return true;
|
|
2000
|
-
const host = req.headers.host;
|
|
2001
|
-
const origin = req.headers.origin;
|
|
2002
|
-
if (host === undefined || origin !== undefined && !sameAuthorityOrigin(origin, host)) {
|
|
2003
|
-
sendJson(res, 403, { ok: false, errorCode: "forbidden-origin", error: "Same-origin browser request required" });
|
|
2004
|
-
return false;
|
|
2005
|
-
}
|
|
2006
|
-
return true;
|
|
2007
|
-
}
|
|
2008
|
-
|
|
2009
|
-
async function readJsonBody(req, maxBytes = 256 * 1024) {
|
|
2010
|
-
const chunks = [];
|
|
2011
|
-
let size = 0;
|
|
2012
|
-
for await (const chunk of req) {
|
|
2013
|
-
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
2014
|
-
size += buf.length;
|
|
2015
|
-
if (size > maxBytes) throw new Error("Request body too large");
|
|
2016
|
-
chunks.push(buf);
|
|
2017
|
-
}
|
|
2018
|
-
const raw = Buffer.concat(chunks).toString("utf8");
|
|
2019
|
-
return raw.trim() === "" ? {} : JSON.parse(raw);
|
|
2020
|
-
}
|
|
2021
|
-
|
|
2022
|
-
// #endregion
|
|
2023
|
-
|
|
2024
|
-
// #region Mount Once Guard
|
|
2025
|
-
|
|
2026
|
-
const MOUNTED = Symbol.for("dsh-web.mounted-plugins");
|
|
2027
|
-
const WAITERS = Symbol.for("dsh-web.mounted-plugins.waiters");
|
|
2028
|
-
|
|
2029
|
-
function mountedSet() {
|
|
2030
|
-
const existing = globalThis[MOUNTED];
|
|
2031
|
-
if (existing instanceof Set) return existing;
|
|
2032
|
-
const created = new Set();
|
|
2033
|
-
globalThis[MOUNTED] = created;
|
|
2034
|
-
return created;
|
|
2035
|
-
}
|
|
2036
|
-
|
|
2037
|
-
function mountWaiters() {
|
|
2038
|
-
return (globalThis[WAITERS] ??= new Map());
|
|
2039
|
-
}
|
|
2040
|
-
|
|
2041
|
-
function mountOnce(packageName, fn) {
|
|
2042
|
-
const mount = (...args) => {
|
|
2043
|
-
const mounted = mountedSet();
|
|
2044
|
-
const ctx = args[0];
|
|
2045
|
-
if (mounted.has(packageName)) {
|
|
2046
|
-
const waiters = mountWaiters();
|
|
2047
|
-
const queue = waiters.get(packageName) ?? [];
|
|
2048
|
-
let alive = true;
|
|
2049
|
-
const pending = {
|
|
2050
|
-
run: () => {
|
|
2051
|
-
if (alive) mount(...args);
|
|
2052
|
-
},
|
|
2053
|
-
};
|
|
2054
|
-
ctx?.effect?.(() => () => {
|
|
2055
|
-
alive = false;
|
|
2056
|
-
const idx = queue.indexOf(pending);
|
|
2057
|
-
if (idx >= 0) queue.splice(idx, 1);
|
|
2058
|
-
});
|
|
2059
|
-
queue.push(pending);
|
|
2060
|
-
waiters.set(packageName, queue);
|
|
2061
|
-
return;
|
|
2062
|
-
}
|
|
2063
|
-
mounted.add(packageName);
|
|
2064
|
-
ctx?.effect?.(() => () => {
|
|
2065
|
-
mounted.delete(packageName);
|
|
2066
|
-
const waiters = mountWaiters();
|
|
2067
|
-
const queue = waiters.get(packageName);
|
|
2068
|
-
if (queue === undefined) return;
|
|
2069
|
-
waiters.delete(packageName);
|
|
2070
|
-
for (const waiter of queue.splice(0)) {
|
|
2071
|
-
queueMicrotask(() => waiter.run());
|
|
2072
|
-
}
|
|
2073
|
-
});
|
|
2074
|
-
return fn(...args);
|
|
2075
|
-
};
|
|
2076
|
-
return mount;
|
|
2077
|
-
}
|
|
2078
|
-
|
|
2079
|
-
// #endregion
|
|
2080
|
-
|
|
2081
|
-
// #region Plugin Apply Implementation
|
|
2082
|
-
|
|
2083
|
-
/**
|
|
2084
|
-
* Run `fn` outside the HMR transaction's async context.
|
|
2085
|
-
*
|
|
2086
|
-
* dsh-hmr marks a running transaction with an AsyncLocalStorage store, and
|
|
2087
|
-
* this plugin's apply() runs inside the install/enable transaction. Timers
|
|
2088
|
-
* created there inherit that store forever, so every later settings write
|
|
2089
|
-
* from them fails with "HMR transactions cannot be nested". Creating the
|
|
2090
|
-
* timers under `executing.exit()` — what dsh-hmr does for its own watchers —
|
|
2091
|
-
* gives them a clean context.
|
|
2092
|
-
*/
|
|
2093
|
-
function outsideHmrTransaction(ctx, fn) {
|
|
2094
|
-
try {
|
|
2095
|
-
const executing = ctx.get?.("hmr")?.executing;
|
|
2096
|
-
if (typeof executing?.exit === "function") return executing.exit(fn);
|
|
2097
|
-
} catch {}
|
|
2098
|
-
return fn();
|
|
2099
|
-
}
|
|
2100
|
-
|
|
2101
|
-
function renderAccountStatus(account) {
|
|
2102
|
-
const { config: cfg, credential: keyInfo, status } = account;
|
|
2103
|
-
const selectedMode =
|
|
2104
|
-
cfg.selectionSaved === false
|
|
2105
|
-
? "Selected Filter: not chosen yet (the first successful probe checks the newest model of each family)"
|
|
2106
|
-
: Array.isArray(cfg.selectedModels) && cfg.selectedModels.length > 0
|
|
2107
|
-
? `Selected Filter: ${cfg.selectedModels.length} model(s) checked`
|
|
2108
|
-
: "Selected Filter: All models (no selection filter active)";
|
|
2109
|
-
const modelListPreview =
|
|
2110
|
-
Array.isArray(status.models) && status.models.length > 0
|
|
2111
|
-
? `Active Models in Picker: ${status.models
|
|
2112
|
-
.slice(0, 12)
|
|
2113
|
-
.map((m) => m.id)
|
|
2114
|
-
.join(", ")}${status.models.length > 12 ? ` ... (+${status.models.length - 12} more)` : ""}`
|
|
2115
|
-
: undefined;
|
|
2116
|
-
const media = (status.allModels ?? []).filter((m) => m.kind === "image" || m.kind === "video");
|
|
2117
|
-
const mediaLine =
|
|
2118
|
-
media.length > 0
|
|
2119
|
-
? `Media Models (not in the picker): ${media.filter((m) => m.kind === "image").length} image, ${media.filter((m) => m.kind === "video").length} video`
|
|
2120
|
-
: undefined;
|
|
2121
|
-
const counts = status.protocolCounts ?? {};
|
|
2122
|
-
const protocolLine =
|
|
2123
|
-
cfg.protocolMode === "fixed"
|
|
2124
|
-
? `Protocol: ${cfg.protocol} (fixed for every model)`
|
|
2125
|
-
: `Protocol: native per model (${PROTOCOLS.filter((p) => counts[p])
|
|
2126
|
-
.map((p) => `${p}: ${counts[p]}`)
|
|
2127
|
-
.join(", ") || "none synced yet"})`;
|
|
2128
|
-
const lines = [
|
|
2129
|
-
`Account "${cfg.displayName}" [id: ${account.id}] — ${cfg.enabled ? "enabled" : "disabled"}`,
|
|
2130
|
-
`CLIProxyAPI Status: ${status.state}`,
|
|
2131
|
-
`Provider Routes: ${(status.state === "connected" && status.routes ? status.routes : [cfg.provider]).join(", ")}`,
|
|
2132
|
-
protocolLine,
|
|
2133
|
-
`Configured Base URL: ${cfg.baseURL || "(auto-discover / inherited)"}`,
|
|
2134
|
-
...(status.endpoint ? [`Active Endpoint: ${status.endpoint}`] : []),
|
|
2135
|
-
`API Key Reference: ${keyInfo.ref} (${keyInfo.configured ? "configured" : "not set"})`,
|
|
2136
|
-
selectedMode,
|
|
2137
|
-
`Synchronized Models: ${status.modelCount}${
|
|
2138
|
-
status.totalDiscovered ? ` (of ${status.totalDiscovered} discovered)` : ""
|
|
2139
|
-
}`,
|
|
2140
|
-
...(modelListPreview ? [modelListPreview] : []),
|
|
2141
|
-
...(mediaLine ? [mediaLine] : []),
|
|
2142
|
-
...(status.refreshedAt ? [`Last Refreshed: ${status.refreshedAt}`] : []),
|
|
2143
|
-
...(status.error ? [`Error: ${status.error}`] : []),
|
|
2144
|
-
];
|
|
2145
|
-
return lines.join("\n");
|
|
2146
|
-
}
|
|
2147
|
-
|
|
2148
|
-
/** One line per account: management key, quota state and what the picker models map to. Never includes keys. */
|
|
2149
|
-
export function renderQuotaText(snapshot, onlyAccountId) {
|
|
2150
|
-
const accounts = (snapshot?.accounts ?? []).filter((a) => onlyAccountId === undefined || a.accountId === onlyAccountId);
|
|
2151
|
-
if (accounts.length === 0) return "";
|
|
2152
|
-
const lines = ["Quota (CPA management API):"];
|
|
2153
|
-
for (const a of accounts) {
|
|
2154
|
-
if (!a.keyConfigured) {
|
|
2155
|
-
lines.push(`- ${a.displayName} [${a.accountId}]: no management key (${a.keyRef})`);
|
|
2156
|
-
continue;
|
|
2157
|
-
}
|
|
2158
|
-
const creds = Array.isArray(a.credentials) ? a.credentials : [];
|
|
2159
|
-
const state = a.state + (a.errorCode ? ` (${a.errorCode}${a.errorStatus ? ` ${a.errorStatus}` : ""})` : "") + (a.refreshing ? ", refreshing" : "");
|
|
2160
|
-
lines.push(`- ${a.displayName} [${a.accountId}]: ${state}; ${creds.length} credential(s)${a.fetchedAt ? `; fetched ${a.fetchedAt}` : ""}; routes ${(a.routes ?? []).join(", ") || "none"}`);
|
|
2161
|
-
for (const m of a.pickerModels ?? []) {
|
|
2162
|
-
const ids = a.modelIndex?.[m.id] ?? [];
|
|
2163
|
-
const names = ids.map((id) => {
|
|
2164
|
-
const c = creds.find((x) => x.id === id);
|
|
2165
|
-
if (!c) return id;
|
|
2166
|
-
const pct = (c.windows ?? []).map((w) => w.remainingPercent).filter((v) => typeof v === "number");
|
|
2167
|
-
return `${c.provider}:${c.label || c.id}${pct.length ? ` ${Math.round(Math.min(...pct))}%` : ""}${c.status === "active" ? "" : ` (${c.status})`}`;
|
|
2168
|
-
});
|
|
2169
|
-
lines.push(` ${m.route}/${m.id} -> ${names.length ? names.join(", ") : "no credential"} [${a.modelMatch?.[m.id] ?? "none"}]`);
|
|
2170
|
-
}
|
|
2171
|
-
}
|
|
2172
|
-
return lines.join("\n");
|
|
2173
|
-
}
|
|
2174
|
-
|
|
2175
|
-
export function renderStatusText(payload, onlyAccountId) {
|
|
2176
|
-
const accounts = payload.accounts ?? [];
|
|
2177
|
-
if (accounts.length === 0) {
|
|
2178
|
-
return "No CLIProxyAPI account is configured. Add one in Settings → CLIProxyAPI.";
|
|
2179
|
-
}
|
|
2180
|
-
const shown =
|
|
2181
|
-
onlyAccountId === undefined ? accounts : accounts.filter((a) => a.id === onlyAccountId);
|
|
2182
|
-
const enabled = accounts.filter((a) => a.config.enabled).length;
|
|
2183
|
-
const header = `CLIProxyAPI accounts: ${accounts.length} (${enabled} enabled); auto refresh every ${payload.global?.refreshIntervalSeconds ?? DEFAULT_REFRESH_INTERVAL_SECONDS}s`;
|
|
2184
|
-
return [header, ...shown.map(renderAccountStatus)].join("\n\n");
|
|
2185
|
-
}
|
|
2186
|
-
|
|
2187
|
-
/** Account fields the save and add routes accept, besides key handling. */
|
|
2188
|
-
function accountPatchFromInput(input) {
|
|
2189
|
-
const patch = {};
|
|
2190
|
-
for (const field of ["baseURL", "provider", "displayName", "protocol", "protocolMode", "includePrefix", "excludePrefix", "apiKeyEnv"]) {
|
|
2191
|
-
if (typeof input[field] === "string") patch[field] = input[field];
|
|
2192
|
-
}
|
|
2193
|
-
if (typeof input.enabled === "boolean") patch.enabled = input.enabled;
|
|
2194
|
-
if (Array.isArray(input.selectedModels)) patch.selectedModels = input.selectedModels;
|
|
2195
|
-
if (plainObject(input.modelOverrides) !== undefined) patch.modelOverrides = input.modelOverrides;
|
|
2196
|
-
if (typeof input.apiKey === "string" && input.apiKey.trim() !== "") patch.apiKey = input.apiKey.trim();
|
|
2197
|
-
if (input.clearApiKey === true) patch.clearApiKey = true;
|
|
2198
|
-
for (const field of GLOBAL_FIELDS) {
|
|
2199
|
-
if (input[field] !== undefined) patch[field] = input[field];
|
|
2200
|
-
}
|
|
2201
|
-
return patch;
|
|
2202
|
-
}
|
|
2203
|
-
|
|
2204
|
-
function applyImpl(ctx, config) {
|
|
2205
|
-
const stateFilePath = resolveStateFilePath(ctx);
|
|
2206
|
-
const persisted = loadPersistedState(stateFilePath);
|
|
2207
|
-
|
|
2208
|
-
const readExistingPiAiRoute = (provider) => {
|
|
2209
|
-
try {
|
|
2210
|
-
const descriptors = ctx.settings.describe();
|
|
2211
|
-
const piAi = descriptors.find((d) => d.ns === PI_AI_NS);
|
|
2212
|
-
return piAi?.value?.providers?.[provider];
|
|
2213
|
-
} catch {
|
|
2214
|
-
return undefined;
|
|
2215
|
-
}
|
|
2216
|
-
};
|
|
2217
|
-
|
|
2218
|
-
const readDefaultModel = () => {
|
|
2219
|
-
try {
|
|
2220
|
-
const value = ctx.settings.describe().find((d) => d.ns === DEFAULT_MODEL_NS)?.value;
|
|
2221
|
-
return typeof value?.provider === "string" && typeof value?.model === "string"
|
|
2222
|
-
? { provider: value.provider, model: value.model }
|
|
2223
|
-
: undefined;
|
|
2224
|
-
} catch {
|
|
2225
|
-
return undefined;
|
|
2226
|
-
}
|
|
2227
|
-
};
|
|
2228
|
-
|
|
2229
|
-
// Seed a selection from every managed route, not just the base one, so a
|
|
2230
|
-
// restart after protocol routing keeps Messages / Responses models checked.
|
|
2231
|
-
const existingManagedIds = (provider) =>
|
|
2232
|
-
managedRouteKeys(provider).flatMap((key) => {
|
|
2233
|
-
const route = readExistingPiAiRoute(key);
|
|
2234
|
-
return Array.isArray(route?.models)
|
|
2235
|
-
? route.models.map((m) => m?.id).filter((id) => typeof id === "string" && id.trim() !== "")
|
|
2236
|
-
: [];
|
|
2237
|
-
});
|
|
2238
|
-
|
|
2239
|
-
// Migrate a legacy single-account file into `accounts[]`, keeping a copy.
|
|
2240
|
-
if (Object.keys(persisted).length > 0 && !Array.isArray(persisted.accounts)) {
|
|
2241
|
-
savePersistedState(stateFilePath.replace(/\.json$/, ".v1.json"), persisted);
|
|
2242
|
-
}
|
|
2243
|
-
|
|
2244
|
-
// An account without a saved selection keeps the models already on its
|
|
2245
|
-
// pi-ai routes when that is a curated list (<= 35). Otherwise it stays
|
|
2246
|
-
// unset and its first successful probe seeds recommendModels().
|
|
2247
|
-
let memState = normalizeState(mergeHostConfig(persisted, config));
|
|
2248
|
-
memState = {
|
|
2249
|
-
...memState,
|
|
2250
|
-
accounts: memState.accounts.map((account) => {
|
|
2251
|
-
if (account.selectedModels !== undefined) return account;
|
|
2252
|
-
const existingIds = existingManagedIds(account.provider);
|
|
2253
|
-
return existingIds.length > 0 && existingIds.length <= 35
|
|
2254
|
-
? { ...account, selectedModels: existingIds }
|
|
2255
|
-
: account;
|
|
2256
|
-
}),
|
|
2257
|
-
};
|
|
2258
|
-
savePersistedState(stateFilePath, memState);
|
|
2259
|
-
|
|
2260
|
-
// The state file is re-read on every access so hand edits take effect.
|
|
2261
|
-
const readState = () => {
|
|
2262
|
-
const fresh = loadPersistedState(stateFilePath);
|
|
2263
|
-
if (Object.keys(fresh).length > 0) memState = normalizeState(fresh);
|
|
2264
|
-
return memState;
|
|
2265
|
-
};
|
|
2266
|
-
const writeState = (state) => {
|
|
2267
|
-
memState = state;
|
|
2268
|
-
savePersistedState(stateFilePath, state);
|
|
2269
|
-
};
|
|
2270
|
-
|
|
2271
|
-
// Update preferences and packages never share the account state or credentials.
|
|
2272
|
-
const updateDirectory = dirname(stateFilePath);
|
|
2273
|
-
const updateSettingsPath = join(updateDirectory, "update-settings.json");
|
|
2274
|
-
let profile;
|
|
2275
|
-
try { profile = ctx.get?.("profileContext"); } catch {}
|
|
2276
|
-
const source = typeof profile?.dir === "string"
|
|
2277
|
-
? loadPersistedState(join(profile.dir, "package.json")).dependencies?.[PACKAGE_NAME]
|
|
2278
|
-
: undefined;
|
|
2279
|
-
const linkedInstall = typeof source === "string" &&
|
|
2280
|
-
(/^(?:link:|workspace:)/.test(source) || /^(?:file:)?(?:[A-Za-z]:[\\/]|\/|\.{1,2}[\\/])/.test(source) && !/\.(?:tgz|tar\.gz)$/i.test(source) || source.startsWith("file:") && !/\.(?:tgz|tar\.gz)$/i.test(source));
|
|
2281
|
-
const currentVersion = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
2282
|
-
let updateTimer;
|
|
2283
|
-
let updateStartup;
|
|
2284
|
-
const scheduleUpdates = (preferences) => {
|
|
2285
|
-
clearInterval(updateTimer);
|
|
2286
|
-
clearTimeout(updateStartup);
|
|
2287
|
-
if (!preferences.autoCheck) return;
|
|
2288
|
-
outsideHmrTransaction(ctx, () => {
|
|
2289
|
-
const run = () => { void Promise.resolve().then(() => updater.check({ automatic: true })).catch(() => {}); };
|
|
2290
|
-
updateStartup = setTimeout(run, 3000);
|
|
2291
|
-
updateTimer = setInterval(run, UPDATE_INTERVAL_MS);
|
|
2292
|
-
});
|
|
2293
|
-
};
|
|
2294
|
-
const updater = createPluginUpdater({
|
|
2295
|
-
currentVersion,
|
|
2296
|
-
linkedInstall,
|
|
2297
|
-
profileName: profile?.name ?? profile?.profile ?? getEnvValue(ctx, "DSH_PROFILE"),
|
|
2298
|
-
readSettings: () => loadUpdateSettings(updateSettingsPath),
|
|
2299
|
-
writeSettings: (preferences) => saveUpdateSettings(updateSettingsPath, preferences),
|
|
2300
|
-
storePackage: (version, bytes) => storeUpdatePackage(join(updateDirectory, "updates"), version, bytes),
|
|
2301
|
-
onSettingsChanged: scheduleUpdates,
|
|
2302
|
-
logger: ctx.logger,
|
|
2303
|
-
});
|
|
2304
|
-
ctx.effect(() => {
|
|
2305
|
-
scheduleUpdates(updater.status());
|
|
2306
|
-
return () => {
|
|
2307
|
-
clearInterval(updateTimer);
|
|
2308
|
-
clearTimeout(updateStartup);
|
|
2309
|
-
updater.dispose();
|
|
2310
|
-
};
|
|
2311
|
-
}, "cpa: plugin update checks");
|
|
2312
|
-
|
|
2313
|
-
let timer;
|
|
2314
|
-
let startupTimeout;
|
|
2315
|
-
|
|
2316
|
-
const resolveApiKey = async (refName) => {
|
|
2317
|
-
try {
|
|
2318
|
-
const ref = toCredentialRef(refName);
|
|
2319
|
-
const credentials = ctx.get("credentials");
|
|
2320
|
-
const stored = credentials === undefined ? undefined : await credentials.resolve(ref);
|
|
2321
|
-
const raw = stored?.value ?? getEnvValue(ctx, ref);
|
|
2322
|
-
const value = raw?.trim();
|
|
2323
|
-
return value === undefined || value === "" ? undefined : value;
|
|
2324
|
-
} catch {
|
|
2325
|
-
return undefined;
|
|
2326
|
-
}
|
|
2327
|
-
};
|
|
2328
|
-
|
|
2329
|
-
const describeApiKey = async (refName) => {
|
|
2330
|
-
try {
|
|
2331
|
-
const ref = toCredentialRef(refName);
|
|
2332
|
-
const credentials = ctx.get("credentials");
|
|
2333
|
-
if (credentials !== undefined) {
|
|
2334
|
-
return await credentials.describe(ref);
|
|
2335
|
-
}
|
|
2336
|
-
const envVal = getEnvValue(ctx, ref);
|
|
2337
|
-
return {
|
|
2338
|
-
configured: envVal !== undefined && envVal.length > 0,
|
|
2339
|
-
writable: false,
|
|
2340
|
-
};
|
|
2341
|
-
} catch {
|
|
2342
|
-
return { configured: false, writable: true };
|
|
2343
|
-
}
|
|
2344
|
-
};
|
|
2345
|
-
|
|
2346
|
-
const reschedule = () => {
|
|
2347
|
-
if (timer !== undefined) clearInterval(timer);
|
|
2348
|
-
timer = outsideHmrTransaction(ctx, () =>
|
|
2349
|
-
setInterval(() => {
|
|
2350
|
-
void controller.refreshAll().finally(() => syncAgentTool());
|
|
2351
|
-
}, readState().refreshIntervalSeconds * 1000)
|
|
2352
|
-
);
|
|
2353
|
-
};
|
|
2354
|
-
|
|
2355
|
-
// Media preferences (lib/media.js) live in their own file next to the
|
|
2356
|
-
// account state, never inside it, so no state migration is involved.
|
|
2357
|
-
const mediaSettingsPath = join(updateDirectory, "media-settings.json");
|
|
2358
|
-
let mediaWarned = false;
|
|
2359
|
-
const readMedia = () => {
|
|
2360
|
-
const loaded = loadMediaSettings(mediaSettingsPath);
|
|
2361
|
-
if (loaded.error === undefined) {
|
|
2362
|
-
mediaWarned = false;
|
|
2363
|
-
} else if (!mediaWarned) {
|
|
2364
|
-
mediaWarned = true;
|
|
2365
|
-
ctx.logger?.warn?.(
|
|
2366
|
-
`dsh-cliproxyapi: ${mediaSettingsPath} is unreadable; default media settings apply until it is saved again (the damaged file is then kept as *.corrupt-*.json)`
|
|
2367
|
-
);
|
|
2368
|
-
}
|
|
2369
|
-
return loaded;
|
|
2370
|
-
};
|
|
2371
|
-
|
|
2372
|
-
const controller = createCpaController({
|
|
2373
|
-
settings: ctx.settings,
|
|
2374
|
-
readState,
|
|
2375
|
-
writeState,
|
|
2376
|
-
mediaKindOverrides: (id) => accountKindOverrides(readMedia().settings, id),
|
|
2377
|
-
resolveApiKey,
|
|
2378
|
-
describeApiKey,
|
|
2379
|
-
setCredential: async (ref, value) => {
|
|
2380
|
-
const credentials = ctx.get("credentials");
|
|
2381
|
-
if (credentials === undefined) {
|
|
2382
|
-
throw new Error("Credentials service is not available on this host");
|
|
2383
|
-
}
|
|
2384
|
-
await credentials.set(ref, value);
|
|
2385
|
-
},
|
|
2386
|
-
unsetCredential: async (ref) => {
|
|
2387
|
-
await ctx.get("credentials")?.unset(ref);
|
|
2388
|
-
},
|
|
2389
|
-
readExistingPiAiRoute,
|
|
2390
|
-
readDefaultModel,
|
|
2391
|
-
setTimeout: (fn, ms) => outsideHmrTransaction(ctx, () => setTimeout(fn, ms)),
|
|
2392
|
-
environmentBaseURL: () => getEnvValue(ctx, "CLIPROXYAPI_BASE_URL"),
|
|
2393
|
-
logger: ctx.logger,
|
|
2394
|
-
onGlobalsChanged: () => reschedule(),
|
|
2395
|
-
});
|
|
2396
|
-
|
|
2397
|
-
// Quota ("额度" tab + composer badge): management keys live in the
|
|
2398
|
-
// credentials service under their own refs, never in account state.
|
|
2399
|
-
const quota = createQuotaController({
|
|
2400
|
-
listAccounts: () => controller.quotaAccounts(),
|
|
2401
|
-
resolveKey: (ref) => resolveApiKey(ref),
|
|
2402
|
-
describeKey: (ref) => describeApiKey(ref),
|
|
2403
|
-
setKey: async (ref, value) => {
|
|
2404
|
-
const credentials = ctx.get("credentials");
|
|
2405
|
-
if (credentials === undefined) {
|
|
2406
|
-
throw new Error("Credentials service is not available on this host");
|
|
2407
|
-
}
|
|
2408
|
-
await credentials.set(ref, value);
|
|
2409
|
-
},
|
|
2410
|
-
unsetKey: async (ref) => {
|
|
2411
|
-
await ctx.get("credentials")?.unset(ref);
|
|
2412
|
-
},
|
|
2413
|
-
logger: ctx.logger,
|
|
2414
|
-
});
|
|
2415
|
-
ctx.effect(() => () => quota.dispose(), "cpa: quota");
|
|
2416
|
-
|
|
2417
|
-
/** Media page payload: preferences (without per-account maps) and each account's media models. */
|
|
2418
|
-
const mediaStatus = () => {
|
|
2419
|
-
const { settings, error } = readMedia();
|
|
2420
|
-
const { accounts: perAccount, ...global } = settings;
|
|
2421
|
-
return {
|
|
2422
|
-
settings: global,
|
|
2423
|
-
...(error === undefined ? {} : { settingsError: error }),
|
|
2424
|
-
accounts: controller.mediaAccounts().map((account) => {
|
|
2425
|
-
const overrides = accountKindOverrides(settings, account.id);
|
|
2426
|
-
return {
|
|
2427
|
-
...account,
|
|
2428
|
-
disabledModels: perAccount[account.id]?.disabledModels ?? [],
|
|
2429
|
-
kindOverrides: overrides,
|
|
2430
|
-
models: account.models.map((m) => ({
|
|
2431
|
-
...m,
|
|
2432
|
-
enabled: mediaModelEnabled(settings, account.id, m.id),
|
|
2433
|
-
overridden: overrides[m.id] !== undefined,
|
|
2434
|
-
})),
|
|
2435
|
-
};
|
|
2436
|
-
}),
|
|
2437
|
-
};
|
|
2438
|
-
};
|
|
2439
|
-
|
|
2440
|
-
const saveMedia = async (body) => {
|
|
2441
|
-
const accountId = body?.account?.accountId;
|
|
2442
|
-
if (accountId !== undefined && !controller.mediaAccounts().some((a) => a.id === accountId)) {
|
|
2443
|
-
throw new MediaError("invalid-account");
|
|
2444
|
-
}
|
|
2445
|
-
const { settings, kindsChanged } = applyMediaPatch(readMedia().settings, body);
|
|
2446
|
-
saveMediaSettings(mediaSettingsPath, settings);
|
|
2447
|
-
// A reclassified model enters or leaves the chat routes on the next sync.
|
|
2448
|
-
if (kindsChanged === undefined) return { ok: true, media: mediaStatus() };
|
|
2449
|
-
await controller.refreshAccount(kindsChanged);
|
|
2450
|
-
return { ok: true, media: mediaStatus(), status: await controller.statusPayload() };
|
|
2451
|
-
};
|
|
2452
|
-
|
|
2453
|
-
// Per-session model picks and recent outputs, in their own file.
|
|
2454
|
-
const mediaSessions = createMediaSessionStore(join(updateDirectory, "media-sessions.json"));
|
|
2455
|
-
|
|
2456
|
-
const mediaChoices = () => {
|
|
2457
|
-
const { settings } = readMedia();
|
|
2458
|
-
const accounts = controller.mediaAccounts();
|
|
2459
|
-
return {
|
|
2460
|
-
settings,
|
|
2461
|
-
accounts,
|
|
2462
|
-
options: { image: mediaOptions(accounts, settings, "image"), video: mediaOptions(accounts, settings, "video") },
|
|
2463
|
-
};
|
|
2464
|
-
};
|
|
2465
|
-
|
|
2466
|
-
/**
|
|
2467
|
-
* Choices for a generation command. When a needed kind is not offered and an
|
|
2468
|
-
* enabled account either has not read its catalog yet (just after a start
|
|
2469
|
-
* or reload) or reported a failed plain listing, sync once and look again
|
|
2470
|
-
* instead of failing on a timing race. An incomplete-catalog retry is
|
|
2471
|
-
* limited to once per 30 s so a permanently missing listing is not fetched
|
|
2472
|
-
* on every request.
|
|
2473
|
-
*/
|
|
2474
|
-
let catalogRetryAt = 0;
|
|
2475
|
-
const mediaChoicesFor = async (kind) => {
|
|
2476
|
-
let choices = mediaChoices();
|
|
2477
|
-
const missing = (k) => choices.options[k].length === 0;
|
|
2478
|
-
const someMissing = kind === "any" ? missing("image") || missing("video") : missing(kind);
|
|
2479
|
-
const cold = choices.accounts.some((a) => a.enabled && a.discovered === 0);
|
|
2480
|
-
const incomplete = choices.accounts.some((a) => a.enabled && a.catalogIncomplete === true);
|
|
2481
|
-
const retryIncomplete = incomplete && Date.now() - catalogRetryAt >= 30_000;
|
|
2482
|
-
if (someMissing && (cold || retryIncomplete)) {
|
|
2483
|
-
catalogRetryAt = Date.now();
|
|
2484
|
-
await controller.refreshAll();
|
|
2485
|
-
choices = mediaChoices();
|
|
2486
|
-
}
|
|
2487
|
-
return choices;
|
|
2488
|
-
};
|
|
2489
|
-
|
|
2490
|
-
/** One line per account for an empty choice: state and catalog counts, never keys or URLs. */
|
|
2491
|
-
const mediaAccountsSummary = (accounts, kind) =>
|
|
2492
|
-
accounts
|
|
2493
|
-
.map((a) => `${a.displayName || a.id}: ${a.enabled ? a.state : "disabled"}, ${a.discovered} model(s), ${a.models.filter((m) => m.kind === kind).length} ${kind}`)
|
|
2494
|
-
.join("; ") || "no account";
|
|
2495
|
-
|
|
2496
|
-
/** What the composer button shows for one session (no network access). */
|
|
2497
|
-
const sessionView = (sessionId) => {
|
|
2498
|
-
const { settings, options, accounts } = mediaChoices();
|
|
2499
|
-
const entry = sessionId === undefined ? { recent: [] } : mediaSessions.get(sessionId);
|
|
2500
|
-
const current = {};
|
|
2501
|
-
for (const kind of ["image", "video"]) {
|
|
2502
|
-
try {
|
|
2503
|
-
const { option, source } = chooseMediaModel({
|
|
2504
|
-
options: options[kind],
|
|
2505
|
-
sessionRef: entry[kind],
|
|
2506
|
-
lastRef: settings.lastSelection[kind],
|
|
2507
|
-
});
|
|
2508
|
-
current[kind] = { ref: option.ref, source };
|
|
2509
|
-
} catch {
|
|
2510
|
-
current[kind] = null;
|
|
2511
|
-
}
|
|
2512
|
-
}
|
|
2513
|
-
const policy = agentPolicy(settings, entry);
|
|
2514
|
-
return {
|
|
2515
|
-
sessionId: sessionId ?? null,
|
|
2516
|
-
options,
|
|
2517
|
-
current,
|
|
2518
|
-
// Compatibility composer fallback; native questions never enter this map.
|
|
2519
|
-
pendingConfirmations: sessionId === undefined ? [] : [...pendingConfirms.entries()]
|
|
2520
|
-
.filter(([, waiting]) => waiting.sessionId === sessionId)
|
|
2521
|
-
.map(([commandId, { kind, en, zh, expiresAt }]) => ({ commandId, kind, en, zh, expiresAt })),
|
|
2522
|
-
// True while an enabled account's last catalog read missed the plain
|
|
2523
|
-
// listing; the composer button retries instead of keeping an old,
|
|
2524
|
-
// image-less list.
|
|
2525
|
-
catalogIncomplete: accounts.some((a) => a.enabled === true && a.catalogIncomplete === true),
|
|
2526
|
-
// The agent tool's rules for this session (shown while the tool is switched on).
|
|
2527
|
-
agent: {
|
|
2528
|
-
enabled: settings.agentTool === true,
|
|
2529
|
-
imageConfirm: policy.imageConfirm,
|
|
2530
|
-
videoConfirm: policy.videoConfirm,
|
|
2531
|
-
videoBudget: policy.budget,
|
|
2532
|
-
used: policy.used,
|
|
2533
|
-
sources: policy.sources,
|
|
2534
|
-
global: { imageConfirm: settings.imageConfirm, videoConfirm: settings.videoConfirm, videoBudget: settings.videoBudget },
|
|
2535
|
-
},
|
|
2536
|
-
recent: entry.recent
|
|
2537
|
-
.filter((r) => existsSync(r.file))
|
|
2538
|
-
.slice(0, 12)
|
|
2539
|
-
.map((r) => ({ id: r.id, kind: r.kind, name: basename(r.file), model: r.model, createdAt: r.createdAt })),
|
|
2540
|
-
};
|
|
2541
|
-
};
|
|
2542
|
-
|
|
2543
|
-
/** Pick a model for one session (and remember it as the last pick); `ref: null` clears the session's pick. */
|
|
2544
|
-
const saveSessionPick = (body) => {
|
|
2545
|
-
// Agent-tool overrides for one session (no model pick in the same request).
|
|
2546
|
-
if (body?.kind === undefined && ["imageConfirm", "videoConfirm", "videoBudget"].some((k) => k in (body ?? {}))) {
|
|
2547
|
-
const sessionId = validSessionId(body.sessionId);
|
|
2548
|
-
if (sessionId === undefined) throw new MediaError("invalid-session");
|
|
2549
|
-
const { settings } = mediaChoices();
|
|
2550
|
-
mediaSessions.update(sessionId, (entry) => applySessionPolicy(entry, body, settings));
|
|
2551
|
-
return { ok: true, session: sessionView(sessionId) };
|
|
2552
|
-
}
|
|
2553
|
-
const kind = body?.kind;
|
|
2554
|
-
if (kind !== "image" && kind !== "video") throw new MediaError("invalid-request");
|
|
2555
|
-
const sessionId = body.sessionId === undefined || body.sessionId === null ? undefined : validSessionId(body.sessionId);
|
|
2556
|
-
if (body.sessionId !== undefined && body.sessionId !== null && sessionId === undefined) throw new MediaError("invalid-session");
|
|
2557
|
-
const ref = body.ref === null ? null : typeof body.ref === "string" ? body.ref : undefined;
|
|
2558
|
-
if (ref === undefined) throw new MediaError("invalid-model");
|
|
2559
|
-
const { settings, options } = mediaChoices();
|
|
2560
|
-
if (ref !== null && !options[kind].some((o) => o.ref === ref)) throw new MediaError("invalid-model");
|
|
2561
|
-
if (sessionId !== undefined) {
|
|
2562
|
-
mediaSessions.update(sessionId, (entry) => {
|
|
2563
|
-
if (ref === null) delete entry[kind];
|
|
2564
|
-
else entry[kind] = ref;
|
|
2565
|
-
});
|
|
2566
|
-
}
|
|
2567
|
-
if (ref !== null && settings.lastSelection[kind] !== ref) {
|
|
2568
|
-
saveMediaSettings(mediaSettingsPath, normalizeMediaSettings({ ...settings, lastSelection: { ...settings.lastSelection, [kind]: ref } }));
|
|
2569
|
-
}
|
|
2570
|
-
return { ok: true, session: sessionView(sessionId) };
|
|
2571
|
-
};
|
|
2572
|
-
|
|
2573
|
-
const MEDIA_FILE_TYPES = {
|
|
2574
|
-
".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".webp": "image/webp", ".gif": "image/gif",
|
|
2575
|
-
".mp4": "video/mp4", ".webm": "video/webm", ".mov": "video/quicktime",
|
|
2576
|
-
};
|
|
2577
|
-
|
|
2578
|
-
/**
|
|
2579
|
-
* One `Range: bytes=a-b` request against a file of `size` bytes: `[start, end]`,
|
|
2580
|
-
* `undefined` without a (usable single) range, `null` when unsatisfiable.
|
|
2581
|
-
*/
|
|
2582
|
-
const byteRange = (header, size) => {
|
|
2583
|
-
if (typeof header !== "string" || header === "") return undefined;
|
|
2584
|
-
const match = /^bytes=(\d*)-(\d*)$/.exec(header.trim());
|
|
2585
|
-
if (match === null || (match[1] === "" && match[2] === "")) return undefined;
|
|
2586
|
-
let start;
|
|
2587
|
-
let end;
|
|
2588
|
-
if (match[1] === "") {
|
|
2589
|
-
const suffix = Number(match[2]);
|
|
2590
|
-
if (suffix === 0) return null;
|
|
2591
|
-
start = Math.max(0, size - suffix);
|
|
2592
|
-
end = size - 1;
|
|
2593
|
-
} else {
|
|
2594
|
-
start = Number(match[1]);
|
|
2595
|
-
end = match[2] === "" ? size - 1 : Math.min(Number(match[2]), size - 1);
|
|
2596
|
-
}
|
|
2597
|
-
if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start > end || start >= size) return null;
|
|
2598
|
-
return [start, end];
|
|
2599
|
-
};
|
|
2600
|
-
// Request bodies carry references base64-encoded and upstreams cap the body
|
|
2601
|
-
// (xAI: "Failed to read request body: length limit exceeded"). Video
|
|
2602
|
-
// requests use the measured `videoImageBudget()`; image edits keep this
|
|
2603
|
-
// unmeasured share.
|
|
2604
|
-
const FIRST_FRAME_MAX_BYTES = 1_000_000;
|
|
2605
|
-
const REFERENCES_MAX_BYTES = 1_400_000;
|
|
2606
|
-
|
|
2607
|
-
/**
|
|
2608
|
-
* One attached image within `maxBytes`: as stored when it fits; a PNG is
|
|
2609
|
-
* re-encoded here (JPEG on white, long edge ≤ 1280); another format asks
|
|
2610
|
-
* DSH's attachment store for a smaller request version.
|
|
2611
|
-
* @throws MediaError `reference-too-large`.
|
|
2612
|
-
*/
|
|
2613
|
-
const readReference = async (store, block, maxBytes, signal) => {
|
|
2614
|
-
const stored = await store.readImage(block.attachment, signal);
|
|
2615
|
-
const image = { mediaType: stored.ref?.mediaType ?? block.attachment?.mediaType, data: stored.data };
|
|
2616
|
-
const fitted = fitReferenceImage(image, maxBytes);
|
|
2617
|
-
if (fitted !== undefined) return { mediaType: fitted.mediaType, data: fitted.data };
|
|
2618
|
-
const width = Number(stored.ref?.width ?? block.attachment?.width);
|
|
2619
|
-
const height = Number(stored.ref?.height ?? block.attachment?.height);
|
|
2620
|
-
if (typeof store.readImageRequest === "function" && width > 0 && height > 0) {
|
|
2621
|
-
const scale = Math.min(1, REFERENCE_MAX_EDGE / Math.max(width, height));
|
|
2622
|
-
try {
|
|
2623
|
-
const smaller = await store.readImageRequest(
|
|
2624
|
-
block.attachment,
|
|
2625
|
-
{ width: Math.max(1, Math.round(width * scale)), height: Math.max(1, Math.round(height * scale)), maxBytes },
|
|
2626
|
-
signal
|
|
2627
|
-
);
|
|
2628
|
-
if (smaller?.data?.byteLength <= maxBytes) return { mediaType: smaller.mediaType, data: smaller.data };
|
|
2629
|
-
} catch (error) {
|
|
2630
|
-
if (signal?.aborted) throw error;
|
|
2631
|
-
}
|
|
2632
|
-
}
|
|
2633
|
-
throw new MediaError("reference-too-large", String(Math.round(maxBytes / 1000)));
|
|
2634
|
-
};
|
|
2635
|
-
|
|
2636
|
-
// #region Parameter interpretation (the session's chat model)
|
|
2637
|
-
const INTERPRET_TIMEOUT_MS = 30_000;
|
|
2638
|
-
const INTERPRET_MAX_TOKENS = 2000;
|
|
2639
|
-
|
|
2640
|
-
/** The provider / model the session's agent last used (else its options, else the default model). */
|
|
2641
|
-
const sessionChatTarget = (agent) => {
|
|
2642
|
-
const valid = (t) => (t && typeof t.provider === "string" && t.provider !== "" && typeof t.model === "string" && t.model !== "" ? { provider: t.provider, model: t.model } : undefined);
|
|
2643
|
-
try {
|
|
2644
|
-
const header = valid(agent?.session?.requestHeader?.()?.config);
|
|
2645
|
-
if (header !== undefined) return header;
|
|
2646
|
-
} catch {
|
|
2647
|
-
// fall through
|
|
2648
|
-
}
|
|
2649
|
-
return valid(agent?.options) ?? valid(ctx.get?.("agentDefaultModel")?.currentSelection?.());
|
|
2650
|
-
};
|
|
2651
|
-
|
|
2652
|
-
/**
|
|
2653
|
-
* Ask the session's chat model which parameters the prompt states.
|
|
2654
|
-
* Never throws: without an `llm` service or a model it returns undefined;
|
|
2655
|
-
* a failed call returns `{ failed: true, by }`.
|
|
2656
|
-
* @param fixed - fields the rule parser already set (they win).
|
|
2657
|
-
* @returns `{ by, values, prompt, items, failed? }` or undefined.
|
|
2658
|
-
*/
|
|
2659
|
-
const interpretParams = async ({ agent, command, text, images = 0, fixed = [], signal }) => {
|
|
2660
|
-
const llm = ctx.get?.("llm");
|
|
2661
|
-
const target = sessionChatTarget(agent);
|
|
2662
|
-
if (typeof llm?.stream !== "function" || target === undefined) return undefined;
|
|
2663
|
-
const by = `${target.provider}/${target.model}`;
|
|
2664
|
-
const request = interpretationRequest({ command, text, images, fixed });
|
|
2665
|
-
const timeout = AbortSignal.timeout(INTERPRET_TIMEOUT_MS);
|
|
2666
|
-
const combined = signal === undefined ? timeout : AbortSignal.any([signal, timeout]);
|
|
2667
|
-
try {
|
|
2668
|
-
const parts = new Map();
|
|
2669
|
-
for await (const chunk of llm.stream({
|
|
2670
|
-
provider: target.provider,
|
|
2671
|
-
model: target.model,
|
|
2672
|
-
system: request.system,
|
|
2673
|
-
messages: [{ role: "user", content: [{ type: "text", text: request.user }] }],
|
|
2674
|
-
maxTokens: INTERPRET_MAX_TOKENS,
|
|
2675
|
-
...(agent?.session?.id === undefined ? {} : { sessionId: agent.session.id }),
|
|
2676
|
-
purpose: "cpa-media-params",
|
|
2677
|
-
signal: combined,
|
|
2678
|
-
})) {
|
|
2679
|
-
if (chunk?.type === "text-delta") parts.set(chunk.index, (parts.get(chunk.index) ?? "") + chunk.text);
|
|
2680
|
-
else if (chunk?.type === "block-end" && chunk.block?.type === "text") parts.set(chunk.index, chunk.block.text);
|
|
2681
|
-
}
|
|
2682
|
-
const answer = [...parts.values()].join("");
|
|
2683
|
-
const parsed = parseInterpretation(answer, { command, text, fields: request.fields, images });
|
|
2684
|
-
if (parsed === undefined) throw new Error("no JSON in the answer");
|
|
2685
|
-
return { by, ...parsed, items: interpretationLabels(parsed.values) };
|
|
2686
|
-
} catch (error) {
|
|
2687
|
-
if (signal?.aborted) throw signal.reason;
|
|
2688
|
-
ctx.logger?.warn?.(`cpa: parameter interpretation with ${by} failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
2689
|
-
return { by, failed: true, values: {}, prompt: text, items: [] };
|
|
2690
|
-
}
|
|
2691
|
-
};
|
|
2692
|
-
|
|
2693
|
-
/** The run-record field describing an interpretation. */
|
|
2694
|
-
const interpretedRecord = (result, original) =>
|
|
2695
|
-
result === undefined
|
|
2696
|
-
? {}
|
|
2697
|
-
: { interpreted: { by: result.by, items: result.items, ...(result.failed ? { failed: true } : {}), ...(result.prompt !== original ? { prompt: true } : {}) } };
|
|
2698
|
-
// #endregion
|
|
2699
|
-
|
|
2700
|
-
/** Plain-English host text for a media failure (slash command output and run records). */
|
|
2701
|
-
const mediaFailureText = (error, kind = "image") => {
|
|
2702
|
-
if (!(error instanceof MediaError)) return error instanceof Error ? error.message : String(error);
|
|
2703
|
-
const detail = error.detail;
|
|
2704
|
-
const video = kind === "video";
|
|
2705
|
-
const noun = video ? "video" : "image";
|
|
2706
|
-
switch (error.code) {
|
|
2707
|
-
case "missing-prompt": return video ? "add a prompt, for example: /cpa-video waves rolling onto a beach at sunset" : "add a prompt, for example: /cpa-image a red fox in the snow";
|
|
2708
|
-
case "invalid-option": return video
|
|
2709
|
-
? `unknown option ${detail}; use -m <model>, --aspect <ratio>, -s <1-15>, -r <480p|720p>`
|
|
2710
|
-
: `unknown option ${detail}; use -m <model>, --aspect <ratio>, -n <1-4>`;
|
|
2711
|
-
case "invalid-aspect": return `--aspect must be one of ${(video ? VIDEO_ASPECTS : IMAGE_ASPECTS).join(", ")}`;
|
|
2712
|
-
case "invalid-count": return "-n must be 1 to 4";
|
|
2713
|
-
case "invalid-video-seconds": return "-s / --seconds must be a whole number from 1 to 15";
|
|
2714
|
-
case "invalid-video-resolution": return "-r / --resolution must be 480p or 720p";
|
|
2715
|
-
case "no-media-model": return `no ${noun} model is available (${detail || "no account"}); check Settings → CLIProxyAPI → Generation, or run /cpa-refresh if a catalog has not been read yet`;
|
|
2716
|
-
case "choose-model": return `several ${noun} models are available (${detail}); pick one with the button next to the composer or pass -m <model>`;
|
|
2717
|
-
case "model-not-found": return `no enabled ${noun} model matches; available: ${detail || "none"}`;
|
|
2718
|
-
case "model-ambiguous": return `several ${noun} models match (${detail}); be more specific`;
|
|
2719
|
-
case "workspace-not-local": return `the session's working directory is not a local folder, so the ${noun} cannot be saved`;
|
|
2720
|
-
case "unsupported-attachment": return detail === "edit"
|
|
2721
|
-
? "attach one MP4 video as the source (images cannot be used here)"
|
|
2722
|
-
: video ? "only image attachments can be used for /cpa-video (first frame, references, first / last frames)" : "only image attachments can be used as references";
|
|
2723
|
-
case "too-many-references": return video ? `attach at most ${detail ?? "14"} images for this mode (more than 2 images are references)` : `attach at most ${MAX_REFERENCE_IMAGES} reference images`;
|
|
2724
|
-
case "frames-need-images": return "--ref / --last / --first-last need attached images: one for a reference or a last frame, two for first and last frame";
|
|
2725
|
-
case "needs-video-15": return "pinned first / last frames need grok-imagine-video-1.5; pick it with -m or the model button";
|
|
2726
|
-
case "invalid-extend-seconds": return "an extension adds 2 to 10 seconds (-s 2-10)";
|
|
2727
|
-
case "no-video-source": return "no source video: attach an MP4, or pass --video <path>, --url <public URL> or --from <command id>";
|
|
2728
|
-
case "video-source-expired": return "that video's file is gone and its short-lived xAI link has expired; attach the MP4 or pass --video <path>";
|
|
2729
|
-
case "one-video-source": return "use one source video: an attachment, --video, --url or --from";
|
|
2730
|
-
case "invalid-video-url": return "--url must be a public http(s) address without a user name or password";
|
|
2731
|
-
case "video-not-found": return `no video file at ${detail}`;
|
|
2732
|
-
case "video-not-mp4": return "the source video must be an MP4 file (H.264, H.265 or AV1)";
|
|
2733
|
-
case "source-too-large": return `the source video is ${detail} MiB; videos up to ${Math.round(VIDEO_UPLOAD_MAX_BYTES / 1024 / 1024)} MiB can be sent inline — shorten or compress it, or pass a public link with --url`;
|
|
2734
|
-
case "request-too-large": return video
|
|
2735
|
-
? `the request is too large for this server (${detail ? `${Math.round(Number(detail) / 1024)} KB` : "body limit"}); attach smaller or fewer images`
|
|
2736
|
-
: "the request is too large for this server";
|
|
2737
|
-
case "route-unsupported": return `this server's upstream does not offer ${detail === "extensions" ? "video extension" : detail === "edits" ? "video editing" : detail} (HTTP 405); CPA may pause the credential for this model for about a minute`;
|
|
2738
|
-
case "edit-source-too-long": return `xAI edits videos of at most ${EDIT_SOURCE_MAX_SECONDS} seconds (this one has ${detail} s); extend it instead`;
|
|
2739
|
-
case "extend-source-length": return `xAI extends videos of ${EXTEND_SOURCE_SECONDS.min} to ${EXTEND_SOURCE_SECONDS.max} seconds (this one has ${detail} s)`;
|
|
2740
|
-
case "reference-too-large": return `an attached image could not be made smaller than ${detail ?? "1000"} KB; attach a smaller image`;
|
|
2741
|
-
case "upstream-error": return `CPA rejected the request (${detail})`;
|
|
2742
|
-
case "timeout": return video ? "CPA did not answer in time" : "CPA did not answer within 3 minutes";
|
|
2743
|
-
case "network": return "could not reach CPA";
|
|
2744
|
-
case "no-image": return "CPA returned no image";
|
|
2745
|
-
case "invalid-image":
|
|
2746
|
-
case "invalid-response": return video ? "CPA returned an answer this plugin does not understand" : "CPA returned data that is not a PNG, JPEG, WebP or GIF image";
|
|
2747
|
-
case "image-too-large": return "an image is larger than 32 MiB";
|
|
2748
|
-
case "video-too-large": return `the video is larger than ${Math.round(MAX_VIDEO_BYTES / 1024 / 1024)} MiB`;
|
|
2749
|
-
case "invalid-video": return "CPA returned data that is not an MP4, MOV or WebM video";
|
|
2750
|
-
case "video-failed": return `the video could not be generated${detail ? ` (${detail})` : ""}`;
|
|
2751
|
-
case "expired": return "CPA no longer knows this video: more than 3 hours have passed since it was submitted";
|
|
2752
|
-
case "download-failed": return `could not download the generated ${noun}`;
|
|
2753
|
-
case "write-failed": return `could not write the ${noun} file${detail ? ` (${detail})` : ""}`;
|
|
2754
|
-
case "no-endpoint": return "this account has no usable server address yet";
|
|
2755
|
-
case "invalid-account": return "the model's account no longer exists";
|
|
2756
|
-
case "interrupted": return "stopped because the plugin was reloaded or DSH closed";
|
|
2757
|
-
default: return error.code;
|
|
2758
|
-
}
|
|
2759
|
-
};
|
|
2760
|
-
// #region Background generation jobs and the session's "running" state
|
|
2761
|
-
//
|
|
2762
|
-
// `/cpa-image` returns as soon as the request is admitted, so the composer
|
|
2763
|
-
// clears the draft at once; the generation continues here and its chat row
|
|
2764
|
-
// follows it through `media/command`. DSH counts only agent turns as
|
|
2765
|
-
// running, so while a job runs this plugin publishes the host's own
|
|
2766
|
-
// `api-session/status` event for that session (the workspace list shows it
|
|
2767
|
-
// as running, then completed). It is republished every few seconds and
|
|
2768
|
-
// after the agent itself goes idle, because those reset it to idle.
|
|
2769
|
-
|
|
2770
|
-
const mediaJobs = new Map(); // job key → { sessionId, abort }
|
|
2771
|
-
// job key → the promise of a job of this activation, until it settles (the agent tool waits on it).
|
|
2772
|
-
const jobPromises = new Map();
|
|
2773
|
-
const settleWith = (key, promise) => {
|
|
2774
|
-
jobPromises.set(key, promise);
|
|
2775
|
-
void promise.finally(() => {
|
|
2776
|
-
if (jobPromises.get(key) === promise) jobPromises.delete(key);
|
|
2777
|
-
});
|
|
2778
|
-
return promise;
|
|
2779
|
-
};
|
|
2780
|
-
let mediaJobSeq = 0;
|
|
2781
|
-
let mediaHeartbeat;
|
|
2782
|
-
const MEDIA_HEARTBEAT_MS = 5000;
|
|
2783
|
-
const jobSessions = () => new Set([...mediaJobs.values()].map((job) => job.sessionId).filter(Boolean));
|
|
2784
|
-
const agentRunning = (sessionId) => {
|
|
2785
|
-
try {
|
|
2786
|
-
return ctx.get?.("agents")?.get?.(sessionId)?.status === "running";
|
|
2787
|
-
} catch {
|
|
2788
|
-
return false;
|
|
2789
|
-
}
|
|
2790
|
-
};
|
|
2791
|
-
const publishSessionStatus = (sessionId, running) => {
|
|
2792
|
-
if (typeof ctx.emit !== "function") return;
|
|
2793
|
-
try {
|
|
2794
|
-
ctx.emit("api-session/status", sessionId, running);
|
|
2795
|
-
} catch (error) {
|
|
2796
|
-
ctx.logger?.warn?.(`cpa: could not publish the session status: ${error instanceof Error ? error.message : String(error)}`);
|
|
2797
|
-
}
|
|
2798
|
-
};
|
|
2799
|
-
const settleSessionStatus = (sessionId) => {
|
|
2800
|
-
if (sessionId === undefined || jobSessions().has(sessionId) || agentRunning(sessionId)) return;
|
|
2801
|
-
publishSessionStatus(sessionId, false);
|
|
2802
|
-
};
|
|
2803
|
-
const syncHeartbeat = () => {
|
|
2804
|
-
if (mediaJobs.size > 0 && mediaHeartbeat === undefined) {
|
|
2805
|
-
mediaHeartbeat = setInterval(() => {
|
|
2806
|
-
for (const sessionId of jobSessions()) publishSessionStatus(sessionId, true);
|
|
2807
|
-
}, MEDIA_HEARTBEAT_MS);
|
|
2808
|
-
mediaHeartbeat.unref?.();
|
|
2809
|
-
} else if (mediaJobs.size === 0 && mediaHeartbeat !== undefined) {
|
|
2810
|
-
clearInterval(mediaHeartbeat);
|
|
2811
|
-
mediaHeartbeat = undefined;
|
|
2812
|
-
}
|
|
2813
|
-
};
|
|
2814
|
-
ctx.on?.("agent/status", (event) => {
|
|
2815
|
-
const sessionId = event?.agent?.id;
|
|
2816
|
-
if (event?.status === "running" || !jobSessions().has(sessionId)) return;
|
|
2817
|
-
// After the host's own idle notice for this transition.
|
|
2818
|
-
setTimeout(() => {
|
|
2819
|
-
if (jobSessions().has(sessionId)) publishSessionStatus(sessionId, true);
|
|
2820
|
-
}, 0);
|
|
2821
|
-
});
|
|
2822
|
-
ctx.effect(
|
|
2823
|
-
() => () => {
|
|
2824
|
-
const sessions = jobSessions();
|
|
2825
|
-
for (const job of mediaJobs.values()) job.abort.abort(new MediaError("interrupted"));
|
|
2826
|
-
mediaJobs.clear();
|
|
2827
|
-
syncHeartbeat();
|
|
2828
|
-
for (const sessionId of sessions) settleSessionStatus(sessionId);
|
|
2829
|
-
},
|
|
2830
|
-
"cpa: generation jobs"
|
|
2831
|
-
);
|
|
2832
|
-
|
|
2833
|
-
/** Update one run record (and prepend new recent outputs) in the session store; never throws. */
|
|
2834
|
-
const recordRun = (sessionId, commandId, patch, recent = []) => {
|
|
2835
|
-
if (sessionId === undefined) return;
|
|
2836
|
-
try {
|
|
2837
|
-
mediaSessions.update(sessionId, (entry) => {
|
|
2838
|
-
if (recent.length > 0) entry.recent = [...recent, ...entry.recent];
|
|
2839
|
-
if (commandId === undefined) return;
|
|
2840
|
-
const index = entry.runs.findIndex((run) => run.commandId === commandId);
|
|
2841
|
-
if (index >= 0) entry.runs[index] = { ...entry.runs[index], ...patch };
|
|
2842
|
-
else entry.runs = [{ commandId, ...patch }, ...entry.runs];
|
|
2843
|
-
});
|
|
2844
|
-
} catch (error) {
|
|
2845
|
-
ctx.logger?.warn?.(`cpa: could not record a generation result: ${error instanceof Error ? error.message : String(error)}`);
|
|
2846
|
-
}
|
|
2847
|
-
};
|
|
2848
|
-
|
|
2849
|
-
/** How the chat row names the save folder: relative to the working directory when inside it. */
|
|
2850
|
-
const folderLabel = (cwd, dir) => {
|
|
2851
|
-
const rel = relative(cwd, dir);
|
|
2852
|
-
return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel) ? rel.split(sep).join("/") : basename(dir);
|
|
2853
|
-
};
|
|
2854
|
-
|
|
2855
|
-
/** Run one admitted image request in the background; resolves when it has settled and been recorded. */
|
|
2856
|
-
const startImageJob = ({ sessionId, commandId, option, args: admitted, references, cwd, outputDir, endpoint, interpret }) => {
|
|
2857
|
-
const key = commandId ?? `job-${++mediaJobSeq}`;
|
|
2858
|
-
const abort = new AbortController();
|
|
2859
|
-
mediaJobs.set(key, { sessionId, abort });
|
|
2860
|
-
syncHeartbeat();
|
|
2861
|
-
if (sessionId !== undefined) publishSessionStatus(sessionId, true);
|
|
2862
|
-
const started = Date.now();
|
|
2863
|
-
return settleWith(key, (async () => {
|
|
2864
|
-
try {
|
|
2865
|
-
let args = admitted;
|
|
2866
|
-
if (interpret !== undefined) {
|
|
2867
|
-
if (commandId !== undefined) videoLive.set(commandId, { phase: "interpreting" });
|
|
2868
|
-
const result = await interpret(abort.signal);
|
|
2869
|
-
if (commandId !== undefined) videoLive.delete(commandId);
|
|
2870
|
-
if (result !== undefined) {
|
|
2871
|
-
args = { ...admitted, ...result.values, prompt: result.prompt };
|
|
2872
|
-
recordRun(sessionId, commandId, {
|
|
2873
|
-
prompt: args.prompt,
|
|
2874
|
-
...(args.aspect === undefined ? {} : { aspect: args.aspect }),
|
|
2875
|
-
count: args.count ?? 1,
|
|
2876
|
-
...interpretedRecord(result, admitted.prompt),
|
|
2877
|
-
});
|
|
2878
|
-
}
|
|
2879
|
-
}
|
|
2880
|
-
const images = await generateImages({
|
|
2881
|
-
rootURL: endpoint.rootURL,
|
|
2882
|
-
apiKey: endpoint.apiKey,
|
|
2883
|
-
model: option.modelId,
|
|
2884
|
-
prompt: args.prompt,
|
|
2885
|
-
...(args.aspect === undefined ? {} : { aspect: args.aspect }),
|
|
2886
|
-
count: args.count ?? 1,
|
|
2887
|
-
references,
|
|
2888
|
-
signal: abort.signal,
|
|
2889
|
-
});
|
|
2890
|
-
const dir = outputDirectory(cwd, outputDir);
|
|
2891
|
-
const paths = writeOutputFiles(dir, outputStem(option.modelId, args.prompt), images);
|
|
2892
|
-
const revised = images.find((image) => image.revisedPrompt !== undefined)?.revisedPrompt;
|
|
2893
|
-
recordRun(
|
|
2894
|
-
sessionId,
|
|
2895
|
-
commandId,
|
|
2896
|
-
{
|
|
2897
|
-
status: "done",
|
|
2898
|
-
files: paths.length,
|
|
2899
|
-
folder: folderLabel(cwd, dir),
|
|
2900
|
-
finishedAt: new Date().toISOString(),
|
|
2901
|
-
...(revised === undefined ? {} : { revisedPrompt: revised }),
|
|
2902
|
-
},
|
|
2903
|
-
paths.map((p) => recentEntry("image", p, option.ref, new Date(), commandId))
|
|
2904
|
-
);
|
|
2905
|
-
ctx.logger?.info?.(`cpa: /cpa-image wrote ${paths.length} image(s) with ${option.modelId} in ${Math.round((Date.now() - started) / 1000)} s`);
|
|
2906
|
-
} catch (thrown) {
|
|
2907
|
-
const error = abort.signal.aborted ? new MediaError("interrupted") : thrown;
|
|
2908
|
-
recordRun(sessionId, commandId, {
|
|
2909
|
-
status: "error",
|
|
2910
|
-
errorCode: error instanceof MediaError ? error.code : "failed",
|
|
2911
|
-
error: mediaFailureText(error),
|
|
2912
|
-
finishedAt: new Date().toISOString(),
|
|
2913
|
-
});
|
|
2914
|
-
ctx.logger?.warn?.(`cpa: /cpa-image failed: ${mediaFailureText(error)}`);
|
|
2915
|
-
} finally {
|
|
2916
|
-
if (commandId !== undefined) videoLive.delete(commandId);
|
|
2917
|
-
mediaJobs.delete(key);
|
|
2918
|
-
syncHeartbeat();
|
|
2919
|
-
settleSessionStatus(sessionId);
|
|
2920
|
-
}
|
|
2921
|
-
})());
|
|
2922
|
-
};
|
|
2923
|
-
|
|
2924
|
-
// #endregion
|
|
2925
|
-
|
|
2926
|
-
/**
|
|
2927
|
-
* `/cpa-image`: generate or (with attached images) edit, save into the
|
|
2928
|
-
* session's folder. Checks everything it can up front (options, model,
|
|
2929
|
-
* folder, references, endpoint), then returns while the generation runs as
|
|
2930
|
-
* a background job; the chat row shows its progress and result.
|
|
2931
|
-
*/
|
|
2932
|
-
// Internal callers (the agent tool) add: `literal` (no plain-language
|
|
2933
|
-
// parsing or interpretation), `pickSession` (whose model pick applies) and
|
|
2934
|
-
// `gate(plan)` (confirmation / budget, run after every check and before
|
|
2935
|
-
// anything is recorded or sent; a thrown `gateError` passes through).
|
|
2936
|
-
const runImageCommand = async ({ agent, rawInput, attachments = [], signal, commandId, literal = false, pickSession, gate }) => {
|
|
2937
|
-
try {
|
|
2938
|
-
const args = parseImageCommand(rawInput, { literal });
|
|
2939
|
-
const blocks = Array.isArray(attachments) ? attachments : [];
|
|
2940
|
-
if (blocks.some((block) => block?.type !== "image")) throw new MediaError("unsupported-attachment");
|
|
2941
|
-
if (blocks.length > MAX_REFERENCE_IMAGES) throw new MediaError("too-many-references");
|
|
2942
|
-
const { settings, options, accounts } = await mediaChoicesFor("image");
|
|
2943
|
-
const sessionId = validSessionId(agent?.id);
|
|
2944
|
-
const pickId = validSessionId(pickSession) ?? sessionId;
|
|
2945
|
-
const entry = pickId === undefined ? {} : mediaSessions.get(pickId);
|
|
2946
|
-
let option;
|
|
2947
|
-
try {
|
|
2948
|
-
({ option } = chooseMediaModel({
|
|
2949
|
-
options: options.image,
|
|
2950
|
-
requested: args.model,
|
|
2951
|
-
sessionRef: entry.image,
|
|
2952
|
-
lastRef: settings.lastSelection.image,
|
|
2953
|
-
}));
|
|
2954
|
-
} catch (error) {
|
|
2955
|
-
if (error instanceof MediaError && error.code === "no-media-model") {
|
|
2956
|
-
throw new MediaError("no-media-model", mediaAccountsSummary(accounts, "image"));
|
|
2957
|
-
}
|
|
2958
|
-
throw error;
|
|
2959
|
-
}
|
|
2960
|
-
const workingDirectory = ctx.get?.("workingDirectory");
|
|
2961
|
-
const cwd = agent !== undefined && workingDirectory !== undefined ? await workingDirectory.ensure(agent, signal) : undefined;
|
|
2962
|
-
if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new MediaError("workspace-not-local");
|
|
2963
|
-
const references = [];
|
|
2964
|
-
if (blocks.length > 0) {
|
|
2965
|
-
const store = ctx.get?.("attachments");
|
|
2966
|
-
if (store === undefined) throw new MediaError("unsupported-attachment");
|
|
2967
|
-
const each = Math.floor(REFERENCES_MAX_BYTES / blocks.length);
|
|
2968
|
-
for (const block of blocks) references.push(await readReference(store, block, each, signal));
|
|
2969
|
-
}
|
|
2970
|
-
const endpoint = await controller.mediaEndpoint(option.accountId);
|
|
2971
|
-
const id = validCommandId(commandId);
|
|
2972
|
-
const count = args.count ?? 1;
|
|
2973
|
-
if (gate !== undefined) {
|
|
2974
|
-
await gate({ kind: "image", mode: references.length > 0 ? "image-edit" : "image-text", option, prompt: args.prompt, count, references: references.length, aspect: args.aspect, cwd, outputDir: settings.outputDir });
|
|
2975
|
-
}
|
|
2976
|
-
recordRun(sessionId, id, {
|
|
2977
|
-
kind: "image",
|
|
2978
|
-
status: "running",
|
|
2979
|
-
model: option.ref,
|
|
2980
|
-
modelName: option.name || option.modelId,
|
|
2981
|
-
accountName: option.accountName,
|
|
2982
|
-
prompt: args.prompt,
|
|
2983
|
-
...(args.aspect === undefined ? {} : { aspect: args.aspect }),
|
|
2984
|
-
count,
|
|
2985
|
-
references: references.length,
|
|
2986
|
-
files: 0,
|
|
2987
|
-
startedAt: new Date().toISOString(),
|
|
2988
|
-
});
|
|
2989
|
-
void startImageJob({
|
|
2990
|
-
sessionId,
|
|
2991
|
-
commandId: id,
|
|
2992
|
-
option,
|
|
2993
|
-
args,
|
|
2994
|
-
references,
|
|
2995
|
-
cwd,
|
|
2996
|
-
outputDir: settings.outputDir,
|
|
2997
|
-
endpoint,
|
|
2998
|
-
...(literal
|
|
2999
|
-
? {}
|
|
3000
|
-
: {
|
|
3001
|
-
interpret: (jobSignal) =>
|
|
3002
|
-
interpretParams({ agent, command: "image", text: args.prompt, images: references.length, fixed: ["aspect", "count"].filter((k) => args[k] !== undefined), signal: jobSignal }),
|
|
3003
|
-
}),
|
|
3004
|
-
});
|
|
3005
|
-
const dir = outputDirectory(cwd, settings.outputDir);
|
|
3006
|
-
return {
|
|
3007
|
-
kind: "success",
|
|
3008
|
-
text: `${references.length > 0 ? "Editing" : "Generating"} ${count} image(s) with ${option.modelId} (${option.accountName}); files go to ${dir}`,
|
|
3009
|
-
};
|
|
3010
|
-
} catch (error) {
|
|
3011
|
-
if (signal?.aborted || error?.gateError === true) throw error;
|
|
3012
|
-
return { kind: "error", text: `/cpa-image: ${mediaFailureText(error)}` };
|
|
3013
|
-
}
|
|
3014
|
-
};
|
|
3015
|
-
|
|
3016
|
-
// #region Video jobs (/cpa-video)
|
|
3017
|
-
//
|
|
3018
|
-
// A video is submitted once and cannot be cancelled (CPA and xAI have no
|
|
3019
|
-
// cancel route). The job is stored in media-jobs.json as soon as CPA
|
|
3020
|
-
// returns its request id, polled (5 s, then 10 s, then 15 s) and downloaded
|
|
3021
|
-
// through CPA into the session's folder. A reload or restart only pauses
|
|
3022
|
-
// the polling: the next activation resumes every stored job younger than
|
|
3023
|
-
// CPA's 3-hour request binding, and marks older ones expired.
|
|
3024
|
-
|
|
3025
|
-
const mediaJobStore = createMediaJobStore(join(updateDirectory, "media-jobs.json"));
|
|
3026
|
-
// commandId → { phase: "submitting" | "queued" | "running" | "downloading", progress? } for the chat row.
|
|
3027
|
-
const videoLive = new Map();
|
|
3028
|
-
// commandId → { url, accountId, accountName, seconds, sessionId, commandId, at }: the short-lived
|
|
3029
|
-
// result URL of a finished video, for /cpa-video-edit and /cpa-video-extend.
|
|
3030
|
-
// In memory only (never stored, logged or sent to the page); a reload forgets it.
|
|
3031
|
-
const videoSources = new Map();
|
|
3032
|
-
// xAI calls its result URLs ephemeral without a number; offer them for an hour.
|
|
3033
|
-
const VIDEO_SOURCE_TTL_MS = 60 * 60 * 1000;
|
|
3034
|
-
const MAX_VIDEO_SOURCES = 100;
|
|
3035
|
-
const rememberSource = (job, status) => {
|
|
3036
|
-
if (job.commandId === undefined || status.sourceUrl === undefined) return;
|
|
3037
|
-
videoSources.delete(job.commandId);
|
|
3038
|
-
videoSources.set(job.commandId, {
|
|
3039
|
-
url: status.sourceUrl,
|
|
3040
|
-
accountId: job.accountId,
|
|
3041
|
-
seconds: status.seconds ?? job.seconds,
|
|
3042
|
-
sessionId: job.sessionId,
|
|
3043
|
-
commandId: job.commandId,
|
|
3044
|
-
at: Date.now(),
|
|
3045
|
-
});
|
|
3046
|
-
while (videoSources.size > MAX_VIDEO_SOURCES) videoSources.delete(videoSources.keys().next().value);
|
|
3047
|
-
};
|
|
3048
|
-
const usableSource = (commandId) => {
|
|
3049
|
-
const source = videoSources.get(commandId);
|
|
3050
|
-
return source !== undefined && Date.now() - source.at <= VIDEO_SOURCE_TTL_MS ? source : undefined;
|
|
3051
|
-
};
|
|
3052
|
-
|
|
3053
|
-
const sleep = (ms, signal) =>
|
|
3054
|
-
new Promise((resolve, reject) => {
|
|
3055
|
-
if (signal.aborted) return reject(signal.reason);
|
|
3056
|
-
const timer = setTimeout(() => {
|
|
3057
|
-
signal.removeEventListener("abort", onAbort);
|
|
3058
|
-
resolve();
|
|
3059
|
-
}, ms);
|
|
3060
|
-
timer.unref?.();
|
|
3061
|
-
const onAbort = () => {
|
|
3062
|
-
clearTimeout(timer);
|
|
3063
|
-
reject(signal.reason);
|
|
3064
|
-
};
|
|
3065
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
3066
|
-
});
|
|
3067
|
-
|
|
3068
|
-
const pollDelay = (elapsedMs) => (elapsedMs < 60_000 ? 5000 : elapsedMs < 180_000 ? 10_000 : 15_000);
|
|
3069
|
-
const MAX_POLL_FAILURES = 12;
|
|
3070
|
-
|
|
3071
|
-
/** Poll, download and record one submitted video; a dispose abort leaves it stored for the next activation. */
|
|
3072
|
-
const followVideoJob = async (job, abort) => {
|
|
3073
|
-
const live = (patch) => {
|
|
3074
|
-
if (job.commandId !== undefined) videoLive.set(job.commandId, { ...(videoLive.get(job.commandId) ?? {}), ...patch });
|
|
3075
|
-
};
|
|
3076
|
-
const finish = (state, runPatch, recent = []) => {
|
|
3077
|
-
mediaJobStore.put({ ...job, state, finishedAt: new Date().toISOString() });
|
|
3078
|
-
recordRun(job.sessionId, job.commandId, { ...runPatch, finishedAt: new Date().toISOString() }, recent);
|
|
3079
|
-
};
|
|
3080
|
-
const fail = (error) => {
|
|
3081
|
-
const code = error instanceof MediaError ? error.code : "failed";
|
|
3082
|
-
finish(code === "expired" ? "expired" : "failed", { status: "error", errorCode: code, error: mediaFailureText(error, "video") });
|
|
3083
|
-
ctx.logger?.warn?.(`cpa: /cpa-video ${job.id} failed: ${mediaFailureText(error, "video")}`);
|
|
3084
|
-
};
|
|
3085
|
-
const submitted = Date.parse(job.submittedAt);
|
|
3086
|
-
let failures = 0;
|
|
3087
|
-
live({ phase: "queued" });
|
|
3088
|
-
try {
|
|
3089
|
-
for (;;) {
|
|
3090
|
-
if (Date.now() - submitted > VIDEO_JOB_TTL_MS) throw new MediaError("expired");
|
|
3091
|
-
let endpoint;
|
|
3092
|
-
let status;
|
|
3093
|
-
try {
|
|
3094
|
-
endpoint = await controller.mediaEndpoint(job.accountId);
|
|
3095
|
-
status = await retrieveVideo({ rootURL: endpoint.rootURL, apiKey: endpoint.apiKey, requestId: job.id, signal: abort.signal });
|
|
3096
|
-
failures = 0;
|
|
3097
|
-
} catch (error) {
|
|
3098
|
-
if (abort.signal.aborted) throw error;
|
|
3099
|
-
// A missing account or a refusal that keeps coming back ends the job;
|
|
3100
|
-
// network trouble and short CPA hiccups are retried.
|
|
3101
|
-
if (error instanceof MediaError && error.code === "invalid-account") throw error;
|
|
3102
|
-
failures += 1;
|
|
3103
|
-
if (failures >= MAX_POLL_FAILURES) throw error;
|
|
3104
|
-
await sleep(pollDelay(Date.now() - submitted), abort.signal);
|
|
3105
|
-
continue;
|
|
3106
|
-
}
|
|
3107
|
-
if (status.state === "failed") throw new MediaError("video-failed", status.error);
|
|
3108
|
-
if (status.state === "done") {
|
|
3109
|
-
live({ phase: "downloading", progress: 100 });
|
|
3110
|
-
const dir = outputDirectory(job.cwd, job.outputDir);
|
|
3111
|
-
const path = await downloadVideo({
|
|
3112
|
-
rootURL: endpoint.rootURL,
|
|
3113
|
-
apiKey: endpoint.apiKey,
|
|
3114
|
-
requestId: job.id,
|
|
3115
|
-
dir,
|
|
3116
|
-
stem: outputStem(job.modelId, job.prompt),
|
|
3117
|
-
signal: abort.signal,
|
|
3118
|
-
});
|
|
3119
|
-
finish("done", { status: "done", files: 1, folder: folderLabel(job.cwd, dir) }, [recentEntry("video", path, job.ref, new Date(), job.commandId)]);
|
|
3120
|
-
rememberSource(job, status);
|
|
3121
|
-
ctx.logger?.info?.(`cpa: /cpa-video ${job.id} saved after ${Math.round((Date.now() - submitted) / 1000)} s`);
|
|
3122
|
-
return;
|
|
3123
|
-
}
|
|
3124
|
-
live({ phase: status.state, ...(status.progress === undefined ? {} : { progress: status.progress }) });
|
|
3125
|
-
await sleep(pollDelay(Date.now() - submitted), abort.signal);
|
|
3126
|
-
}
|
|
3127
|
-
} catch (error) {
|
|
3128
|
-
if (abort.signal.aborted) return; // paused by a reload; resumed by the next activation
|
|
3129
|
-
fail(error);
|
|
3130
|
-
}
|
|
3131
|
-
};
|
|
3132
|
-
|
|
3133
|
-
/** Track one job in the session's running state until it settles. */
|
|
3134
|
-
const trackVideoJob = (key, sessionId, run) => {
|
|
3135
|
-
const abort = new AbortController();
|
|
3136
|
-
mediaJobs.set(key, { sessionId, abort });
|
|
3137
|
-
syncHeartbeat();
|
|
3138
|
-
if (sessionId !== undefined) publishSessionStatus(sessionId, true);
|
|
3139
|
-
return settleWith(key, (async () => {
|
|
3140
|
-
try {
|
|
3141
|
-
await run(abort);
|
|
3142
|
-
} finally {
|
|
3143
|
-
if (mediaJobs.get(key)?.abort === abort) mediaJobs.delete(key);
|
|
3144
|
-
videoLive.delete(key);
|
|
3145
|
-
syncHeartbeat();
|
|
3146
|
-
settleSessionStatus(sessionId);
|
|
3147
|
-
}
|
|
3148
|
-
})());
|
|
3149
|
-
};
|
|
3150
|
-
|
|
3151
|
-
/**
|
|
3152
|
-
* Submit one admitted video through `submit(signal)` (generation, edit or
|
|
3153
|
-
* extension), store the job, then follow it.
|
|
3154
|
-
*/
|
|
3155
|
-
const startVideoJob = ({ sessionId, commandId, accountId, modelId, ref, prompt, seconds, cwd, outputDir, submit, label }) => {
|
|
3156
|
-
const key = commandId ?? `job-${++mediaJobSeq}`;
|
|
3157
|
-
if (commandId !== undefined) videoLive.set(commandId, { phase: "submitting" });
|
|
3158
|
-
return trackVideoJob(key, sessionId, async (abort) => {
|
|
3159
|
-
let requestId;
|
|
3160
|
-
try {
|
|
3161
|
-
// `submit` may settle the final prompt / length first (parameter interpretation).
|
|
3162
|
-
const submitted = await submit(abort.signal);
|
|
3163
|
-
requestId = submitted.requestId;
|
|
3164
|
-
if (typeof submitted.prompt === "string") prompt = submitted.prompt;
|
|
3165
|
-
if (Number.isInteger(submitted.seconds)) seconds = submitted.seconds;
|
|
3166
|
-
} catch (thrown) {
|
|
3167
|
-
// Cut off before CPA answered: whether it was submitted is unknown.
|
|
3168
|
-
const error = abort.signal.aborted ? new MediaError("interrupted") : thrown;
|
|
3169
|
-
recordRun(sessionId, commandId, {
|
|
3170
|
-
status: "error",
|
|
3171
|
-
errorCode: error instanceof MediaError ? error.code : "failed",
|
|
3172
|
-
error: mediaFailureText(error, "video"),
|
|
3173
|
-
finishedAt: new Date().toISOString(),
|
|
3174
|
-
});
|
|
3175
|
-
ctx.logger?.warn?.(`cpa: ${label} was not submitted: ${mediaFailureText(error, "video")}`);
|
|
3176
|
-
return;
|
|
3177
|
-
}
|
|
3178
|
-
const job = mediaJobStore.put({
|
|
3179
|
-
id: requestId,
|
|
3180
|
-
state: "submitted",
|
|
3181
|
-
accountId,
|
|
3182
|
-
modelId,
|
|
3183
|
-
ref,
|
|
3184
|
-
...(sessionId === undefined ? {} : { sessionId }),
|
|
3185
|
-
...(commandId === undefined ? {} : { commandId }),
|
|
3186
|
-
cwd,
|
|
3187
|
-
outputDir,
|
|
3188
|
-
prompt,
|
|
3189
|
-
seconds,
|
|
3190
|
-
submittedAt: new Date().toISOString(),
|
|
3191
|
-
});
|
|
3192
|
-
recordRun(sessionId, commandId, { videoId: requestId });
|
|
3193
|
-
ctx.logger?.info?.(`cpa: ${label} submitted ${requestId} (${modelId}, ${seconds} s)`);
|
|
3194
|
-
await followVideoJob(job, abort);
|
|
3195
|
-
});
|
|
3196
|
-
};
|
|
3197
|
-
/** Resume every stored job that is still running (after a reload or restart). */
|
|
3198
|
-
const resumeVideoJobs = () => {
|
|
3199
|
-
let jobs;
|
|
3200
|
-
try {
|
|
3201
|
-
jobs = mediaJobStore.list().filter((job) => job.state === "submitted");
|
|
3202
|
-
} catch (error) {
|
|
3203
|
-
ctx.logger?.warn?.(`cpa: could not read media-jobs.json: ${error instanceof Error ? error.message : String(error)}`);
|
|
3204
|
-
return;
|
|
3205
|
-
}
|
|
3206
|
-
for (const job of jobs) {
|
|
3207
|
-
const key = job.commandId ?? `video-${job.id}`;
|
|
3208
|
-
if (mediaJobs.has(key)) continue;
|
|
3209
|
-
void trackVideoJob(key, job.sessionId, (abort) => followVideoJob(job, abort));
|
|
3210
|
-
}
|
|
3211
|
-
};
|
|
3212
|
-
|
|
3213
|
-
/** Grok's 1.5 video family takes more references and pinned first / last frames. */
|
|
3214
|
-
const isVideo15 = (modelId) => /(^|\/)grok-imagine-video-1\.5/i.test(String(modelId ?? ""));
|
|
3215
|
-
|
|
3216
|
-
/**
|
|
3217
|
-
* How attached images are used: none = text only; one = first frame
|
|
3218
|
-
* (`--ref`: a reference, `--last`: the last frame); two = references
|
|
3219
|
-
* (`--first-last` / `--last`: first and last frame); more = references.
|
|
3220
|
-
* @throws MediaError `frames-need-images` / `too-many-references` / `needs-video-15`.
|
|
3221
|
-
*/
|
|
3222
|
-
const videoMode = (count, frames, modelId) => {
|
|
3223
|
-
if (count === 0) {
|
|
3224
|
-
if (frames !== undefined) throw new MediaError("frames-need-images");
|
|
3225
|
-
return "text";
|
|
3226
|
-
}
|
|
3227
|
-
let mode;
|
|
3228
|
-
if (count === 1) mode = frames === "reference" ? "reference" : frames === "last" ? "last-frame" : frames === "first-last" ? undefined : "first-frame";
|
|
3229
|
-
else if (count === 2) mode = frames === "first-last" || frames === "last" ? "first-last" : "reference";
|
|
3230
|
-
else mode = frames === undefined || frames === "reference" ? "reference" : undefined;
|
|
3231
|
-
if (mode === undefined) throw new MediaError(count === 1 ? "frames-need-images" : "too-many-references", count === 1 ? "2" : "2");
|
|
3232
|
-
const maxRefs = isVideo15(modelId) ? 14 : 7;
|
|
3233
|
-
if (mode === "reference" && count > maxRefs) throw new MediaError("too-many-references", String(maxRefs));
|
|
3234
|
-
if ((mode === "first-last" || mode === "last-frame") && !isVideo15(modelId)) throw new MediaError("needs-video-15");
|
|
3235
|
-
return mode;
|
|
3236
|
-
};
|
|
3237
|
-
|
|
3238
|
-
/**
|
|
3239
|
-
* `/cpa-video`: submit a video — text only, from a first frame, from
|
|
3240
|
-
* reference images, or between pinned first / last frames — and return at
|
|
3241
|
-
* once; the chat row follows the background job.
|
|
3242
|
-
*/
|
|
3243
|
-
const runVideoCommand = async ({ agent, rawInput, attachments = [], signal, commandId, literal = false, pickSession, gate }) => {
|
|
3244
|
-
try {
|
|
3245
|
-
const parsed = parseVideoCommand(rawInput, { literal });
|
|
3246
|
-
const blocks = Array.isArray(attachments) ? attachments : [];
|
|
3247
|
-
if (blocks.some((block) => block?.type !== "image")) throw new MediaError("unsupported-attachment");
|
|
3248
|
-
if (blocks.length > 14) throw new MediaError("too-many-references", "14");
|
|
3249
|
-
const { settings, options, accounts } = await mediaChoicesFor("video");
|
|
3250
|
-
const sessionId = validSessionId(agent?.id);
|
|
3251
|
-
const pickId = validSessionId(pickSession) ?? sessionId;
|
|
3252
|
-
const entry = pickId === undefined ? {} : mediaSessions.get(pickId);
|
|
3253
|
-
let option;
|
|
3254
|
-
try {
|
|
3255
|
-
({ option } = chooseMediaModel({ options: options.video, requested: parsed.model, sessionRef: entry.video, lastRef: settings.lastSelection.video }));
|
|
3256
|
-
} catch (error) {
|
|
3257
|
-
if (error instanceof MediaError && error.code === "no-media-model") throw new MediaError("no-media-model", mediaAccountsSummary(accounts, "video"));
|
|
3258
|
-
throw error;
|
|
3259
|
-
}
|
|
3260
|
-
const mode = videoMode(blocks.length, parsed.frames, option.modelId);
|
|
3261
|
-
const workingDirectory = ctx.get?.("workingDirectory");
|
|
3262
|
-
const cwd = agent !== undefined && workingDirectory !== undefined ? await workingDirectory.ensure(agent, signal) : undefined;
|
|
3263
|
-
if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new MediaError("workspace-not-local");
|
|
3264
|
-
const images = [];
|
|
3265
|
-
if (blocks.length > 0) {
|
|
3266
|
-
const store = ctx.get?.("attachments");
|
|
3267
|
-
if (store === undefined) throw new MediaError("unsupported-attachment");
|
|
3268
|
-
// The generation route reads about 1 MiB of body (measured), so all images share that.
|
|
3269
|
-
const each = Math.min(FIRST_FRAME_MAX_BYTES, videoImageBudget(parsed.prompt, blocks.length));
|
|
3270
|
-
for (const block of blocks) images.push(await readReference(store, block, each, signal));
|
|
3271
|
-
}
|
|
3272
|
-
const framesFor = (m) =>
|
|
3273
|
-
m === "first-frame" ? { image: images[0] }
|
|
3274
|
-
: m === "last-frame" ? { lastFrame: images[0] }
|
|
3275
|
-
: m === "first-last" ? { image: images[0], lastFrame: images[1] }
|
|
3276
|
-
: m === "reference" ? { references: images }
|
|
3277
|
-
: {};
|
|
3278
|
-
const endpoint = await controller.mediaEndpoint(option.accountId);
|
|
3279
|
-
const args = { ...parsed, seconds: parsed.seconds ?? settings.defaultVideoSeconds, resolution: parsed.resolution ?? settings.defaultVideoResolution };
|
|
3280
|
-
const id = validCommandId(commandId);
|
|
3281
|
-
if (gate !== undefined) {
|
|
3282
|
-
await gate({ kind: "video", mode, option, prompt: args.prompt, seconds: args.seconds, resolution: args.resolution, aspect: args.aspect, references: images.length, cwd, outputDir: settings.outputDir });
|
|
3283
|
-
}
|
|
3284
|
-
recordRun(sessionId, id, {
|
|
3285
|
-
kind: "video",
|
|
3286
|
-
status: "running",
|
|
3287
|
-
mode,
|
|
3288
|
-
model: option.ref,
|
|
3289
|
-
modelName: option.name || option.modelId,
|
|
3290
|
-
accountName: option.accountName,
|
|
3291
|
-
prompt: args.prompt,
|
|
3292
|
-
...(args.aspect === undefined ? {} : { aspect: args.aspect }),
|
|
3293
|
-
seconds: args.seconds,
|
|
3294
|
-
resolution: args.resolution,
|
|
3295
|
-
count: 1,
|
|
3296
|
-
references: images.length,
|
|
3297
|
-
files: 0,
|
|
3298
|
-
startedAt: new Date().toISOString(),
|
|
3299
|
-
});
|
|
3300
|
-
void startVideoJob({
|
|
3301
|
-
sessionId,
|
|
3302
|
-
commandId: id,
|
|
3303
|
-
accountId: option.accountId,
|
|
3304
|
-
modelId: option.modelId,
|
|
3305
|
-
ref: option.ref,
|
|
3306
|
-
prompt: args.prompt,
|
|
3307
|
-
seconds: args.seconds,
|
|
3308
|
-
cwd,
|
|
3309
|
-
outputDir: settings.outputDir,
|
|
3310
|
-
label: "/cpa-video",
|
|
3311
|
-
submit: async (jobSignal) => {
|
|
3312
|
-
// The session's chat model reads what the rules did not (explicit options and recognized words win).
|
|
3313
|
-
if (id !== undefined && !literal) videoLive.set(id, { phase: "interpreting" });
|
|
3314
|
-
const fixed = ["seconds", "resolution", "aspect", "frames"].filter((k) => parsed[k] !== undefined);
|
|
3315
|
-
const result = literal ? undefined : await interpretParams({ agent, command: "video", text: parsed.prompt, images: images.length, fixed, signal: jobSignal });
|
|
3316
|
-
let final = args;
|
|
3317
|
-
let finalMode = mode;
|
|
3318
|
-
if (result !== undefined) {
|
|
3319
|
-
final = { ...args, ...result.values, prompt: result.prompt };
|
|
3320
|
-
if (result.values.frames !== undefined) finalMode = videoMode(images.length, result.values.frames, option.modelId);
|
|
3321
|
-
recordRun(sessionId, id, {
|
|
3322
|
-
mode: finalMode,
|
|
3323
|
-
prompt: final.prompt,
|
|
3324
|
-
...(final.aspect === undefined ? {} : { aspect: final.aspect }),
|
|
3325
|
-
seconds: final.seconds,
|
|
3326
|
-
resolution: final.resolution,
|
|
3327
|
-
...interpretedRecord(result, parsed.prompt),
|
|
3328
|
-
});
|
|
3329
|
-
}
|
|
3330
|
-
if (id !== undefined) videoLive.set(id, { phase: "submitting" });
|
|
3331
|
-
const submitted = await submitVideo({
|
|
3332
|
-
rootURL: endpoint.rootURL,
|
|
3333
|
-
apiKey: endpoint.apiKey,
|
|
3334
|
-
model: option.modelId,
|
|
3335
|
-
prompt: final.prompt,
|
|
3336
|
-
seconds: final.seconds,
|
|
3337
|
-
...(final.aspect === undefined ? {} : { aspect: final.aspect }),
|
|
3338
|
-
resolution: final.resolution,
|
|
3339
|
-
...framesFor(finalMode),
|
|
3340
|
-
signal: jobSignal,
|
|
3341
|
-
});
|
|
3342
|
-
return { ...submitted, prompt: final.prompt, seconds: final.seconds };
|
|
3343
|
-
},
|
|
3344
|
-
});
|
|
3345
|
-
return {
|
|
3346
|
-
kind: "success",
|
|
3347
|
-
text: `Submitting a ${args.seconds} s ${args.resolution} video (${mode}) to ${option.modelId} (${option.accountName}); a submitted video cannot be cancelled. Files go to ${outputDirectory(cwd, settings.outputDir)}`,
|
|
3348
|
-
};
|
|
3349
|
-
} catch (error) {
|
|
3350
|
-
if (signal?.aborted || error?.gateError === true) throw error;
|
|
3351
|
-
return { kind: "error", text: `/cpa-video: ${mediaFailureText(error, "video")}` };
|
|
3352
|
-
}
|
|
3353
|
-
};
|
|
3354
|
-
|
|
3355
|
-
// `${accountId}/${route}` the upstream answered 405 for in this activation:
|
|
3356
|
-
// asked again only after a reload (CPA pauses the credential after a 405).
|
|
3357
|
-
const unsupportedRoutes = new Set();
|
|
3358
|
-
const editRoute = (mode) => (mode === "extend" ? "extensions" : "edits");
|
|
3359
|
-
|
|
3360
|
-
/** The newest existing video file the session recorded (for one command, or any). */
|
|
3361
|
-
const sessionVideoFile = (sessionId, commandId) => {
|
|
3362
|
-
if (sessionId === undefined) return undefined;
|
|
3363
|
-
return mediaSessions.get(sessionId).recent.find((r) => r.kind === "video" && (commandId === undefined || r.commandId === commandId) && existsSync(r.file));
|
|
3364
|
-
};
|
|
3365
|
-
/** The account a recorded run used (`accountId/modelId`), if it still exists. */
|
|
3366
|
-
const runAccountId = (sessionId, commandId, accounts) => {
|
|
3367
|
-
if (sessionId === undefined || commandId === undefined) return undefined;
|
|
3368
|
-
const run = mediaSessions.get(sessionId).runs.find((r) => r.commandId === commandId);
|
|
3369
|
-
const id = typeof run?.model === "string" ? run.model.split("/", 1)[0] : undefined;
|
|
3370
|
-
return accounts.some((a) => a.id === id) ? id : undefined;
|
|
3371
|
-
};
|
|
3372
|
-
const checkVideoBytes = (data) => {
|
|
3373
|
-
if (!isMp4(data)) throw new MediaError("video-not-mp4");
|
|
3374
|
-
return mp4Duration(data);
|
|
3375
|
-
};
|
|
3376
|
-
const tooLarge = (bytes) => new MediaError("source-too-large", (bytes / 1024 / 1024).toFixed(1));
|
|
3377
|
-
/** A local MP4 read whole (at most {@link VIDEO_UPLOAD_MAX_BYTES}) for an inline upload. */
|
|
3378
|
-
const readLocalVideo = (path, name = basename(path)) => {
|
|
3379
|
-
let info;
|
|
3380
|
-
try {
|
|
3381
|
-
info = statSync(path);
|
|
3382
|
-
} catch {
|
|
3383
|
-
info = undefined;
|
|
3384
|
-
}
|
|
3385
|
-
if (info === undefined || !info.isFile()) throw new MediaError("video-not-found", name);
|
|
3386
|
-
if (info.size > VIDEO_UPLOAD_MAX_BYTES) throw tooLarge(info.size);
|
|
3387
|
-
const data = new Uint8Array(readFileSync(path));
|
|
3388
|
-
return { data, seconds: checkVideoBytes(data), name };
|
|
3389
|
-
};
|
|
3390
|
-
/** An MP4 attached to the command (a DSH file attachment `{ attachmentId, name, bytes }`). */
|
|
3391
|
-
const readAttachedVideo = async (store, ref, signal) => {
|
|
3392
|
-
const name = basename(String(ref?.name ?? "video.mp4"));
|
|
3393
|
-
if (Number(ref?.bytes) > VIDEO_UPLOAD_MAX_BYTES) throw tooLarge(Number(ref.bytes));
|
|
3394
|
-
let hostPath;
|
|
3395
|
-
try {
|
|
3396
|
-
hostPath = store.fileHostPath?.(ref);
|
|
3397
|
-
} catch {
|
|
3398
|
-
hostPath = undefined;
|
|
3399
|
-
}
|
|
3400
|
-
if (typeof hostPath === "string" && existsSync(hostPath)) return readLocalVideo(hostPath, name);
|
|
3401
|
-
if (typeof store.readFileStream !== "function") throw new MediaError("unsupported-attachment", "edit");
|
|
3402
|
-
const chunks = [];
|
|
3403
|
-
let size = 0;
|
|
3404
|
-
for await (const chunk of store.readFileStream(ref, signal)) {
|
|
3405
|
-
size += chunk.byteLength;
|
|
3406
|
-
if (size > VIDEO_UPLOAD_MAX_BYTES) throw tooLarge(size);
|
|
3407
|
-
chunks.push(chunk);
|
|
3408
|
-
}
|
|
3409
|
-
const data = new Uint8Array(Buffer.concat(chunks.map((c) => Buffer.from(c.buffer, c.byteOffset, c.byteLength))));
|
|
3410
|
-
return { data, seconds: checkVideoBytes(data), name };
|
|
3411
|
-
};
|
|
3412
|
-
|
|
3413
|
-
/**
|
|
3414
|
-
* `/cpa-video-edit` and `/cpa-video-extend`: change or continue any MP4.
|
|
3415
|
-
* Sources: an attached MP4, `--video <path>`, `--url <public URL>`,
|
|
3416
|
-
* `--from <command id>`, or by default the session's newest video. A video
|
|
3417
|
-
* this plugin made goes by xAI's own short-lived link while it is
|
|
3418
|
-
* remembered (about an hour, this activation), otherwise its file is sent
|
|
3419
|
-
* inline; local files go inline as a base64 data URI (CPA has no file
|
|
3420
|
-
* upload route; the edit routes read bodies of 20 MiB, measured), a URL is
|
|
3421
|
-
* downloaded by xAI itself.
|
|
3422
|
-
*/
|
|
3423
|
-
const runVideoEditCommand = (mode) => async ({ agent, rawInput, attachments = [], signal, commandId, literal = false, pickSession, gate }) => {
|
|
3424
|
-
const name = mode === "extend" ? "/cpa-video-extend" : "/cpa-video-edit";
|
|
3425
|
-
try {
|
|
3426
|
-
const parsed = parseVideoEditCommand(rawInput, mode, { literal });
|
|
3427
|
-
const blocks = Array.isArray(attachments) ? attachments : [];
|
|
3428
|
-
if (blocks.some((block) => block?.type !== "file")) throw new MediaError("unsupported-attachment", "edit");
|
|
3429
|
-
const named = [parsed.from, parsed.video, parsed.url].filter((v) => v !== undefined).length;
|
|
3430
|
-
if (blocks.length > 1 || (blocks.length === 1 && named > 0)) throw new MediaError("one-video-source");
|
|
3431
|
-
const sessionId = validSessionId(agent?.id);
|
|
3432
|
-
const workingDirectory = ctx.get?.("workingDirectory");
|
|
3433
|
-
const cwd = agent !== undefined && workingDirectory !== undefined ? await workingDirectory.ensure(agent, signal) : undefined;
|
|
3434
|
-
if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new MediaError("workspace-not-local");
|
|
3435
|
-
const { settings, options, accounts } = await mediaChoicesFor("video");
|
|
3436
|
-
|
|
3437
|
-
// source: { url? | data?, seconds?, accountId?, commandId?, name? }
|
|
3438
|
-
let source;
|
|
3439
|
-
const linkOf = (id) => {
|
|
3440
|
-
const link = usableSource(id);
|
|
3441
|
-
return link !== undefined && link.sessionId === sessionId ? link : undefined;
|
|
3442
|
-
};
|
|
3443
|
-
const fromLink = (link) => ({ url: link.url, seconds: link.seconds, accountId: link.accountId, commandId: link.commandId });
|
|
3444
|
-
const fromFile = (record) => ({ ...readLocalVideo(record.file), commandId: record.commandId, accountId: runAccountId(sessionId, record.commandId, accounts) });
|
|
3445
|
-
if (blocks.length === 1) {
|
|
3446
|
-
const store = ctx.get?.("attachments");
|
|
3447
|
-
if (store === undefined) throw new MediaError("unsupported-attachment", "edit");
|
|
3448
|
-
source = await readAttachedVideo(store, blocks[0].attachment, signal);
|
|
3449
|
-
} else if (parsed.video !== undefined) {
|
|
3450
|
-
source = readLocalVideo(isAbsolute(parsed.video) ? parsed.video : join(cwd, parsed.video));
|
|
3451
|
-
} else if (parsed.url !== undefined) {
|
|
3452
|
-
source = { url: parsed.url, name: new URL(parsed.url).hostname };
|
|
3453
|
-
} else if (parsed.from !== undefined) {
|
|
3454
|
-
const link = linkOf(parsed.from);
|
|
3455
|
-
const record = link === undefined ? sessionVideoFile(sessionId, parsed.from) : undefined;
|
|
3456
|
-
if (link !== undefined) source = fromLink(link);
|
|
3457
|
-
else if (record !== undefined) source = fromFile(record);
|
|
3458
|
-
else throw new MediaError(videoSources.has(parsed.from) ? "video-source-expired" : "no-video-source");
|
|
3459
|
-
} else {
|
|
3460
|
-
const record = sessionVideoFile(sessionId);
|
|
3461
|
-
const link = record !== undefined ? linkOf(record.commandId) : [...videoSources.values()].reverse().map((s) => linkOf(s.commandId)).find(Boolean);
|
|
3462
|
-
if (link !== undefined) source = fromLink(link);
|
|
3463
|
-
else if (record !== undefined) source = fromFile(record);
|
|
3464
|
-
else throw new MediaError("no-video-source");
|
|
3465
|
-
}
|
|
3466
|
-
if (source.seconds !== undefined) {
|
|
3467
|
-
if (mode === "edit" && source.seconds > EDIT_SOURCE_MAX_SECONDS) throw new MediaError("edit-source-too-long", String(source.seconds));
|
|
3468
|
-
if (mode === "extend" && (source.seconds < EXTEND_SOURCE_SECONDS.min || source.seconds > EXTEND_SOURCE_SECONDS.max)) {
|
|
3469
|
-
throw new MediaError("extend-source-length", String(source.seconds));
|
|
3470
|
-
}
|
|
3471
|
-
}
|
|
3472
|
-
|
|
3473
|
-
// The account: the source video's own, unless -m names a model; else the session's video model.
|
|
3474
|
-
let accountId = parsed.model === undefined ? source.accountId : undefined;
|
|
3475
|
-
if (accountId === undefined) {
|
|
3476
|
-
const pickId = validSessionId(pickSession) ?? sessionId;
|
|
3477
|
-
const entry = pickId === undefined ? {} : mediaSessions.get(pickId);
|
|
3478
|
-
try {
|
|
3479
|
-
({ option: { accountId } } = chooseMediaModel({ options: options.video, requested: parsed.model, sessionRef: entry.video, lastRef: settings.lastSelection.video }));
|
|
3480
|
-
} catch (error) {
|
|
3481
|
-
if (error instanceof MediaError && error.code === "no-media-model") throw new MediaError("no-media-model", mediaAccountsSummary(accounts, "video"));
|
|
3482
|
-
throw error;
|
|
3483
|
-
}
|
|
3484
|
-
}
|
|
3485
|
-
const account = accounts.find((a) => a.id === accountId);
|
|
3486
|
-
if (account === undefined) throw new MediaError("invalid-account");
|
|
3487
|
-
const route = editRoute(mode);
|
|
3488
|
-
if (unsupportedRoutes.has(`${accountId}/${route}`)) throw new MediaError("route-unsupported", route);
|
|
3489
|
-
const endpoint = await controller.mediaEndpoint(accountId);
|
|
3490
|
-
const known = source.seconds === undefined ? undefined : Math.max(1, Math.min(15, Math.round(source.seconds)));
|
|
3491
|
-
const seconds = mode === "extend" ? parsed.seconds ?? 6 : known;
|
|
3492
|
-
const id = validCommandId(commandId);
|
|
3493
|
-
const accountName = account.displayName || account.id;
|
|
3494
|
-
if (gate !== undefined) {
|
|
3495
|
-
await gate({
|
|
3496
|
-
kind: "video",
|
|
3497
|
-
mode,
|
|
3498
|
-
option: { ref: `${accountId}/${VIDEO_EDIT_MODEL}`, modelId: VIDEO_EDIT_MODEL, accountId, accountName },
|
|
3499
|
-
prompt: parsed.prompt,
|
|
3500
|
-
// Billed as the new footage: an edit re-renders the source, an extension adds `seconds`.
|
|
3501
|
-
seconds: seconds ?? 6,
|
|
3502
|
-
sourceName: source.name ?? (source.commandId !== undefined ? source.commandId : undefined),
|
|
3503
|
-
references: 0,
|
|
3504
|
-
cwd,
|
|
3505
|
-
outputDir: settings.outputDir,
|
|
3506
|
-
});
|
|
3507
|
-
}
|
|
3508
|
-
recordRun(sessionId, id, {
|
|
3509
|
-
kind: "video",
|
|
3510
|
-
status: "running",
|
|
3511
|
-
mode,
|
|
3512
|
-
...(source.commandId === undefined ? {} : { sourceCommandId: source.commandId }),
|
|
3513
|
-
...(source.name === undefined ? {} : { sourceName: source.name }),
|
|
3514
|
-
sourceKind: source.data !== undefined ? "upload" : source.commandId !== undefined ? "link" : "url",
|
|
3515
|
-
...(Array.isArray(parsed.ignored) ? { ignored: parsed.ignored } : {}),
|
|
3516
|
-
model: `${accountId}/${VIDEO_EDIT_MODEL}`,
|
|
3517
|
-
modelName: VIDEO_EDIT_MODEL,
|
|
3518
|
-
accountName,
|
|
3519
|
-
prompt: parsed.prompt,
|
|
3520
|
-
...(seconds === undefined ? {} : { seconds }),
|
|
3521
|
-
count: 1,
|
|
3522
|
-
references: 0,
|
|
3523
|
-
files: 0,
|
|
3524
|
-
startedAt: new Date().toISOString(),
|
|
3525
|
-
});
|
|
3526
|
-
const { url: sourceUrl, data: sourceData } = source;
|
|
3527
|
-
void startVideoJob({
|
|
3528
|
-
sessionId,
|
|
3529
|
-
commandId: id,
|
|
3530
|
-
accountId,
|
|
3531
|
-
modelId: VIDEO_EDIT_MODEL,
|
|
3532
|
-
ref: `${accountId}/${VIDEO_EDIT_MODEL}`,
|
|
3533
|
-
prompt: parsed.prompt,
|
|
3534
|
-
// An extension returns the source plus the new part.
|
|
3535
|
-
seconds: mode === "extend" ? Math.min(15, (known ?? 0) + seconds) : seconds ?? 6,
|
|
3536
|
-
cwd,
|
|
3537
|
-
outputDir: settings.outputDir,
|
|
3538
|
-
label: name,
|
|
3539
|
-
submit: async (jobSignal) => {
|
|
3540
|
-
// The session's chat model reads an extension length or other wording the rules missed.
|
|
3541
|
-
if (id !== undefined && !literal) videoLive.set(id, { phase: "interpreting" });
|
|
3542
|
-
const fixed = mode === "extend" && parsed.seconds !== undefined ? ["seconds"] : [];
|
|
3543
|
-
const result = literal ? undefined : await interpretParams({ agent, command: mode, text: parsed.prompt, fixed, signal: jobSignal });
|
|
3544
|
-
let prompt = parsed.prompt;
|
|
3545
|
-
let added = seconds;
|
|
3546
|
-
if (result !== undefined) {
|
|
3547
|
-
prompt = result.prompt;
|
|
3548
|
-
if (mode === "extend" && result.values.seconds !== undefined) added = result.values.seconds;
|
|
3549
|
-
recordRun(sessionId, id, { prompt, ...(added === undefined ? {} : { seconds: added }), ...interpretedRecord(result, parsed.prompt) });
|
|
3550
|
-
}
|
|
3551
|
-
if (id !== undefined) videoLive.set(id, { phase: "submitting" });
|
|
3552
|
-
try {
|
|
3553
|
-
const submitted = await submitVideoEdit({
|
|
3554
|
-
rootURL: endpoint.rootURL,
|
|
3555
|
-
apiKey: endpoint.apiKey,
|
|
3556
|
-
mode,
|
|
3557
|
-
...(sourceData !== undefined ? { sourceData } : { sourceUrl }),
|
|
3558
|
-
prompt,
|
|
3559
|
-
...(mode === "extend" ? { seconds: added } : {}),
|
|
3560
|
-
signal: jobSignal,
|
|
3561
|
-
});
|
|
3562
|
-
return { ...submitted, prompt, ...(mode === "extend" ? { seconds: Math.min(15, (known ?? 0) + added) } : {}) };
|
|
3563
|
-
} catch (error) {
|
|
3564
|
-
if (error instanceof MediaError && error.code === "route-unsupported") unsupportedRoutes.add(`${accountId}/${route}`);
|
|
3565
|
-
throw error;
|
|
3566
|
-
}
|
|
3567
|
-
},
|
|
3568
|
-
});
|
|
3569
|
-
const skipped = Array.isArray(parsed.ignored) && parsed.ignored.length > 0
|
|
3570
|
-
? ` Ignored ${parsed.ignored.join(", ")}: ${mode === "extend" ? "an extension keeps the source's resolution and ratio" : "an edit keeps the source's length, resolution and ratio"}.`
|
|
3571
|
-
: "";
|
|
3572
|
-
const how = sourceData !== undefined ? `uploading ${source.name} (${(sourceData.byteLength / 1024 / 1024).toFixed(1)} MiB)` : source.name !== undefined ? `from ${source.name}` : "from xAI's link";
|
|
3573
|
-
return {
|
|
3574
|
-
kind: "success",
|
|
3575
|
-
text: `${mode === "extend" ? `Extending the video by ${seconds} s` : "Editing the video"} with ${VIDEO_EDIT_MODEL} (${accountName}), ${how}; a submitted video cannot be cancelled. Files go to ${outputDirectory(cwd, settings.outputDir)}.${skipped}`,
|
|
3576
|
-
};
|
|
3577
|
-
} catch (error) {
|
|
3578
|
-
if (signal?.aborted || error?.gateError === true) throw error;
|
|
3579
|
-
return { kind: "error", text: `${name}: ${mediaFailureText(error, "video")}` };
|
|
3580
|
-
}
|
|
3581
|
-
};
|
|
3582
|
-
/** `/cpa-video-status`: this session's video jobs, newest first (text). */
|
|
3583
|
-
const runVideoStatusCommand = async ({ agent }) => {
|
|
3584
|
-
const sessionId = validSessionId(agent?.id);
|
|
3585
|
-
const jobs = mediaJobStore.list().filter((job) => job.sessionId !== undefined && job.sessionId === sessionId).slice(0, 20);
|
|
3586
|
-
if (jobs.length === 0) return { kind: "success", text: "No video jobs in this session." };
|
|
3587
|
-
const lines = jobs.map((job) => {
|
|
3588
|
-
const live = job.commandId === undefined ? undefined : videoLive.get(job.commandId);
|
|
3589
|
-
const state = job.state === "submitted" ? (live?.phase ?? "waiting") + (live?.progress === undefined ? "" : ` ${live.progress}%`) : job.state;
|
|
3590
|
-
const prompt = job.prompt.length > 40 ? `${job.prompt.slice(0, 40)}…` : job.prompt;
|
|
3591
|
-
return `${job.submittedAt.slice(0, 19).replace("T", " ")} ${state.padEnd(14)} ${job.modelId}, ${job.seconds} s ${prompt} (${job.id})`;
|
|
3592
|
-
});
|
|
3593
|
-
return { kind: "success", text: lines.join("\n") };
|
|
3594
|
-
};
|
|
3595
|
-
|
|
3596
|
-
// #endregion
|
|
3597
|
-
|
|
3598
|
-
// #region Agent tool generate_media (off by default; design §5, §7, §8)
|
|
3599
|
-
//
|
|
3600
|
-
// One optional tool, registered only while the Generation tab's switch is
|
|
3601
|
-
// on and some enabled account offers a generation model. A call runs the
|
|
3602
|
-
// same admission as the slash commands (literally: no plain-language
|
|
3603
|
-
// parsing or interpretation), with a gate between admission and the
|
|
3604
|
-
// request that asks for approval and enforces the video seconds budget of
|
|
3605
|
-
// the root session, then waits up to 10 minutes for the background job.
|
|
3606
|
-
const TOOL_WAIT_MS = 10 * 60_000;
|
|
3607
|
-
const TOOL_IMAGE_MAX_BYTES = 32 * 1024 * 1024;
|
|
3608
|
-
const TOOL_PREVIEWS = 4;
|
|
3609
|
-
let toolsCtx;
|
|
3610
|
-
let toolOff;
|
|
3611
|
-
|
|
3612
|
-
const gateError = (message) => Object.assign(new Error(message), { gateError: true });
|
|
3613
|
-
|
|
3614
|
-
/** The top-level agent a (sub-)agent was created under: its session holds picks and the budget. */
|
|
3615
|
-
const rootAgentOf = (agent) => {
|
|
3616
|
-
const agents = ctx.get?.("agents");
|
|
3617
|
-
let current = agent;
|
|
3618
|
-
if (typeof agents?.list !== "function" || typeof agents.isOwnedBy !== "function") return current;
|
|
3619
|
-
for (let depth = 0; depth < 16; depth += 1) {
|
|
3620
|
-
const owner = agents.list().find((candidate) => candidate !== current && agents.isOwnedBy(current.id, candidate));
|
|
3621
|
-
if (owner === undefined) break;
|
|
3622
|
-
current = owner;
|
|
3623
|
-
}
|
|
3624
|
-
return current;
|
|
3625
|
-
};
|
|
3626
|
-
|
|
3627
|
-
// Legacy fallback only when the native userQuestions service is absent. Cost
|
|
3628
|
-
// confirmation is independent of DSH's execution-approval permission policy.
|
|
3629
|
-
// runId → { sessionId, kind, en, zh, expiresAt, settle(outcome) }, in memory only.
|
|
3630
|
-
const pendingConfirms = new Map();
|
|
3631
|
-
const pendingNativeConfirms = new Set();
|
|
3632
|
-
const CONFIRM_WAIT_MS = 10 * 60_000;
|
|
3633
|
-
const CONFIRM_ALLOW = "Allow / 允许";
|
|
3634
|
-
const CONFIRM_DECLINE = "Decline / 拒绝";
|
|
3635
|
-
const cancelConfirms = () => {
|
|
3636
|
-
for (const cancel of [...pendingNativeConfirms]) cancel();
|
|
3637
|
-
for (const waiting of [...pendingConfirms.values()]) waiting.settle("cancelled");
|
|
3638
|
-
};
|
|
3639
|
-
|
|
3640
|
-
/** Compatibility composer/row consent without userQuestions (`allowed` / `rejected` / `timeout` / `cancelled`). */
|
|
3641
|
-
const askInRow = (exec, runId, sessionId, text, kind) =>
|
|
3642
|
-
new Promise((resolve) => {
|
|
3643
|
-
if (exec.signal?.aborted || toolOff === undefined || toolsCtx === undefined) return resolve("cancelled");
|
|
3644
|
-
const timer = setTimeout(() => settle("timeout"), CONFIRM_WAIT_MS);
|
|
3645
|
-
timer.unref?.();
|
|
3646
|
-
const onAbort = () => settle("cancelled");
|
|
3647
|
-
const settle = (outcome) => {
|
|
3648
|
-
if (pendingConfirms.get(runId)?.settle !== settle) return;
|
|
3649
|
-
pendingConfirms.delete(runId);
|
|
3650
|
-
clearTimeout(timer);
|
|
3651
|
-
exec.signal?.removeEventListener("abort", onAbort);
|
|
3652
|
-
resolve(outcome);
|
|
3653
|
-
};
|
|
3654
|
-
exec.signal?.addEventListener("abort", onAbort, { once: true });
|
|
3655
|
-
pendingConfirms.set(runId, { sessionId, kind, en: text.en, zh: text.zh, expiresAt: new Date(Date.now() + CONFIRM_WAIT_MS).toISOString(), settle });
|
|
3656
|
-
});
|
|
3657
|
-
|
|
3658
|
-
/** Anonymous questions block in the native composer, not in a folded tool call. */
|
|
3659
|
-
const askNativeConfirm = (questions, exec, text, kind, runId) =>
|
|
3660
|
-
new Promise((resolve) => {
|
|
3661
|
-
if (exec.signal?.aborted || toolOff === undefined || toolsCtx === undefined) return resolve("cancelled");
|
|
3662
|
-
const controller = new AbortController();
|
|
3663
|
-
const questionId = `media-confirm-${runId}`;
|
|
3664
|
-
let settled = false;
|
|
3665
|
-
const onAbort = () => finish("cancelled");
|
|
3666
|
-
const cancel = () => finish("cancelled");
|
|
3667
|
-
const timer = setTimeout(() => finish("timeout"), CONFIRM_WAIT_MS);
|
|
3668
|
-
timer.unref?.();
|
|
3669
|
-
const finish = (outcome) => {
|
|
3670
|
-
if (settled) return;
|
|
3671
|
-
settled = true;
|
|
3672
|
-
clearTimeout(timer);
|
|
3673
|
-
exec.signal?.removeEventListener("abort", onAbort);
|
|
3674
|
-
pendingNativeConfirms.delete(cancel);
|
|
3675
|
-
// Remove the composer's pending request on every settlement, even if
|
|
3676
|
-
// an answerer ignores cancellation or rejects after the local deadline.
|
|
3677
|
-
controller.abort();
|
|
3678
|
-
resolve(outcome);
|
|
3679
|
-
};
|
|
3680
|
-
pendingNativeConfirms.add(cancel);
|
|
3681
|
-
exec.signal?.addEventListener("abort", onAbort, { once: true });
|
|
3682
|
-
Promise.resolve().then(() => {
|
|
3683
|
-
if (settled) return;
|
|
3684
|
-
return questions.ask({
|
|
3685
|
-
agent: rootAgentOf(exec.agent),
|
|
3686
|
-
signal: controller.signal,
|
|
3687
|
-
questions: [{
|
|
3688
|
-
id: questionId,
|
|
3689
|
-
header: kind === "video" ? "Video confirmation / 视频确认" : "Image confirmation / 图像确认",
|
|
3690
|
-
question: `${text.zh}\n\n${text.en}`,
|
|
3691
|
-
options: [{ label: CONFIRM_ALLOW }, { label: CONFIRM_DECLINE }],
|
|
3692
|
-
multiSelect: false,
|
|
3693
|
-
}],
|
|
3694
|
-
// Do not supply wait/callId: anonymous requests publish the native
|
|
3695
|
-
// blocking PendingQuestion instead of a continued tool-call card.
|
|
3696
|
-
});
|
|
3697
|
-
}).then((answer) => {
|
|
3698
|
-
const item = Array.isArray(answer?.answers) && answer.answers.length === 1 ? answer.answers[0] : undefined;
|
|
3699
|
-
const allowed = item?.id === questionId && item.custom === undefined &&
|
|
3700
|
-
Array.isArray(item.selected) && item.selected.length === 1 && item.selected[0] === CONFIRM_ALLOW;
|
|
3701
|
-
finish(allowed ? "allowed" : "rejected");
|
|
3702
|
-
}, () => finish("unavailable"));
|
|
3703
|
-
});
|
|
3704
|
-
|
|
3705
|
-
/** Cost consent is not an execution approval and never falls back after a native error. */
|
|
3706
|
-
const askApproval = async (exec, text, kind, runId, sessionId) => {
|
|
3707
|
-
const questions = ctx.get?.("userQuestions");
|
|
3708
|
-
const what = kind === "video" ? "this video" : "these images";
|
|
3709
|
-
let answer;
|
|
3710
|
-
if (questions === undefined || questions === null) {
|
|
3711
|
-
const ownerId = validSessionId(rootAgentOf(exec.agent)?.id) ?? sessionId;
|
|
3712
|
-
if (ownerId === undefined) throw gateError(`${what} needs the user's confirmation, but this call has no session to ask in`);
|
|
3713
|
-
answer = await askInRow(exec, runId, ownerId, text, kind);
|
|
3714
|
-
} else {
|
|
3715
|
-
answer = await askNativeConfirm(questions, exec, text, kind, runId);
|
|
3716
|
-
}
|
|
3717
|
-
if (answer === "allowed") return;
|
|
3718
|
-
if (answer === "rejected") throw gateError(`the user declined ${what}; do not retry unless the user asks`);
|
|
3719
|
-
if (answer === "timeout") throw gateError(`the user did not answer the confirmation for ${what} within 10 minutes; ask the user before trying again`);
|
|
3720
|
-
if (answer === "cancelled") throw gateError(`the confirmation for ${what} was cancelled`);
|
|
3721
|
-
throw gateError(`the confirmation for ${what} could not be shown; ask the user before trying again`);
|
|
3722
|
-
};
|
|
3723
|
-
|
|
3724
|
-
/** Attached-image blocks for local image paths (saved in DSH's attachment store, like composer images). */
|
|
3725
|
-
const toolImageBlocks = async (paths, cwd, signal) => {
|
|
3726
|
-
if (paths === undefined) return [];
|
|
3727
|
-
const store = ctx.get?.("attachments");
|
|
3728
|
-
if (typeof store?.saveImage !== "function") throw new Error("images cannot be read: DSH's attachment store is not available");
|
|
3729
|
-
const blocks = [];
|
|
3730
|
-
for (const raw of paths) {
|
|
3731
|
-
const path = isAbsolute(raw) ? raw : join(cwd, raw);
|
|
3732
|
-
let info;
|
|
3733
|
-
try {
|
|
3734
|
-
info = statSync(path);
|
|
3735
|
-
} catch {
|
|
3736
|
-
info = undefined;
|
|
3737
|
-
}
|
|
3738
|
-
if (info === undefined || !info.isFile()) throw new Error(`image not found: ${raw}`);
|
|
3739
|
-
if (info.size > TOOL_IMAGE_MAX_BYTES) throw new Error(`image is larger than 32 MiB: ${raw}`);
|
|
3740
|
-
const data = new Uint8Array(readFileSync(path));
|
|
3741
|
-
const mediaType = sniffImageType(data);
|
|
3742
|
-
if (mediaType === undefined) throw new Error(`not a PNG, JPEG, WebP or GIF image: ${raw}`);
|
|
3743
|
-
signal?.throwIfAborted();
|
|
3744
|
-
const ref = await store.saveImage({ data, mediaType, name: basename(path) });
|
|
3745
|
-
blocks.push({ type: "image", attachment: ref });
|
|
3746
|
-
}
|
|
3747
|
-
return blocks;
|
|
3748
|
-
};
|
|
3749
|
-
|
|
3750
|
-
/** Whether the calling route declares image input (as DSH's own read_image checks). */
|
|
3751
|
-
const routeSeesImages = async (agent, signal) => {
|
|
3752
|
-
const target = sessionChatTarget(agent);
|
|
3753
|
-
const llm = ctx.get?.("llm");
|
|
3754
|
-
if (target === undefined || typeof llm?.resolveModelInfo !== "function") return false;
|
|
3755
|
-
try {
|
|
3756
|
-
const info = await llm.resolveModelInfo(target.provider, target.model, signal);
|
|
3757
|
-
return Array.isArray(info?.inputModalities) && info.inputModalities.includes("image");
|
|
3758
|
-
} catch {
|
|
3759
|
-
return false;
|
|
3760
|
-
}
|
|
3761
|
-
};
|
|
3762
|
-
|
|
3763
|
-
/** Smaller copies of generated images for the model (DSH's attachment store normalizes them). */
|
|
3764
|
-
const imagePreviews = async (paths, agent, signal) => {
|
|
3765
|
-
const store = ctx.get?.("attachments");
|
|
3766
|
-
if (paths.length === 0 || typeof store?.saveImage !== "function" || !(await routeSeesImages(agent, signal))) return [];
|
|
3767
|
-
const previews = [];
|
|
3768
|
-
for (const path of paths.slice(0, TOOL_PREVIEWS)) {
|
|
3769
|
-
try {
|
|
3770
|
-
const data = new Uint8Array(readFileSync(path));
|
|
3771
|
-
const mediaType = sniffImageType(data);
|
|
3772
|
-
if (mediaType === undefined) continue;
|
|
3773
|
-
const ref = await store.saveImage({ data, mediaType, name: basename(path) });
|
|
3774
|
-
previews.push({
|
|
3775
|
-
attachmentId: String(ref.attachmentId),
|
|
3776
|
-
mediaType: ref.mediaType,
|
|
3777
|
-
bytes: ref.bytes,
|
|
3778
|
-
width: ref.width,
|
|
3779
|
-
height: ref.height,
|
|
3780
|
-
...(ref.name === undefined ? {} : { name: ref.name }),
|
|
3781
|
-
...(ref.originalDimensions === undefined ? {} : { originalDimensions: { width: ref.originalDimensions.width, height: ref.originalDimensions.height } }),
|
|
3782
|
-
});
|
|
3783
|
-
} catch (error) {
|
|
3784
|
-
if (signal?.aborted) throw error;
|
|
3785
|
-
ctx.logger?.warn?.(`cpa: could not attach a preview of ${basename(path)}: ${error instanceof Error ? error.message : String(error)}`);
|
|
3786
|
-
}
|
|
3787
|
-
}
|
|
3788
|
-
return previews;
|
|
3789
|
-
};
|
|
3790
|
-
|
|
3791
|
-
/** Wait for a job of this activation: "settled", "timeout" or "aborted" (the job keeps running). */
|
|
3792
|
-
const waitForJob = (key, signal, ms) => {
|
|
3793
|
-
const promise = jobPromises.get(key);
|
|
3794
|
-
if (promise === undefined) return Promise.resolve("settled");
|
|
3795
|
-
return new Promise((resolve) => {
|
|
3796
|
-
const timer = setTimeout(() => finish("timeout"), ms);
|
|
3797
|
-
timer.unref?.();
|
|
3798
|
-
const onAbort = () => finish("aborted");
|
|
3799
|
-
const finish = (how) => {
|
|
3800
|
-
clearTimeout(timer);
|
|
3801
|
-
signal?.removeEventListener("abort", onAbort);
|
|
3802
|
-
resolve(how);
|
|
3803
|
-
};
|
|
3804
|
-
if (signal?.aborted) return finish("aborted");
|
|
3805
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
3806
|
-
promise.then(() => finish("settled"), () => finish("settled"));
|
|
3807
|
-
});
|
|
3808
|
-
};
|
|
3809
|
-
|
|
3810
|
-
// Not submitted (CPA refused it at once): the seconds go back to the budget.
|
|
3811
|
-
const NOT_SUBMITTED = new Set(["upstream-error", "request-too-large", "route-unsupported", "no-endpoint", "invalid-account"]);
|
|
3812
|
-
|
|
3813
|
-
const executeGenerateMedia = async (args, exec) => {
|
|
3814
|
-
const t = parseToolArgs(args);
|
|
3815
|
-
if (exec.agent === undefined) throw new Error("generate_media needs an agent session");
|
|
3816
|
-
const agent = exec.agent;
|
|
3817
|
-
const sessionId = validSessionId(agent.id);
|
|
3818
|
-
const root = rootAgentOf(agent);
|
|
3819
|
-
const rootId = validSessionId(root?.id) ?? sessionId;
|
|
3820
|
-
const runId = toolRunId(exec.callId) ?? `tool-${randomUUID()}`;
|
|
3821
|
-
const workingDirectory = ctx.get?.("workingDirectory");
|
|
3822
|
-
const cwd = workingDirectory === undefined ? undefined : await workingDirectory.ensure(agent, exec.signal);
|
|
3823
|
-
if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new Error(mediaFailureText(new MediaError("workspace-not-local"), t.kind));
|
|
3824
|
-
const blocks = await toolImageBlocks(t.images, cwd, exec.signal);
|
|
3825
|
-
const line = toolCommandLine(t);
|
|
3826
|
-
const handler = t.kind === "image" ? runImageCommand : t.mode === "generate" ? runVideoCommand : runVideoEditCommand(t.mode);
|
|
3827
|
-
// The client groups native tool events at turn level; never impersonate
|
|
3828
|
-
// a user slash command. Retain the deprecated output flag for old clients.
|
|
3829
|
-
const commandRow = false;
|
|
3830
|
-
let charged = 0;
|
|
3831
|
-
let planned;
|
|
3832
|
-
const gate = async (plan) => {
|
|
3833
|
-
planned = plan;
|
|
3834
|
-
const { settings } = mediaChoices();
|
|
3835
|
-
const policy = agentPolicy(settings, rootId === undefined ? {} : mediaSessions.get(rootId));
|
|
3836
|
-
if (plan.kind === "video") {
|
|
3837
|
-
if (policy.videoConfirm === "auto" && policy.limit !== undefined && policy.used + plan.seconds > policy.limit) {
|
|
3838
|
-
throw gateError(`this session's agent video budget is ${policy.limit} s and ${policy.used} s are used; this video needs ${plan.seconds} s. Ask the user to raise the limit (the media button next to the composer) or to confirm videos one by one`);
|
|
3839
|
-
}
|
|
3840
|
-
if (policy.videoConfirm === "ask") await askApproval(exec, approvalText(plan, policy), "video", runId, sessionId);
|
|
3841
|
-
if (rootId !== undefined) {
|
|
3842
|
-
mediaSessions.update(rootId, (entry) => chargeSeconds(entry, agentPolicy(settings, entry), plan.seconds));
|
|
3843
|
-
charged = plan.seconds;
|
|
3844
|
-
}
|
|
3845
|
-
} else if (policy.imageConfirm === "ask") {
|
|
3846
|
-
await askApproval(exec, approvalText(plan, policy), "image", runId, sessionId);
|
|
3847
|
-
}
|
|
3848
|
-
};
|
|
3849
|
-
const refund = () => {
|
|
3850
|
-
if (charged === 0 || rootId === undefined) return;
|
|
3851
|
-
const { settings } = mediaChoices();
|
|
3852
|
-
mediaSessions.update(rootId, (entry) => chargeSeconds(entry, agentPolicy(settings, entry), -charged));
|
|
3853
|
-
charged = 0;
|
|
3854
|
-
};
|
|
3855
|
-
const outcome = await handler({ agent, rawInput: line, attachments: blocks, signal: exec.signal, commandId: runId, literal: true, pickSession: rootId, gate });
|
|
3856
|
-
if (outcome?.kind !== "success") {
|
|
3857
|
-
refund();
|
|
3858
|
-
throw new Error(String(outcome?.text ?? "the request was refused").replace(/^\/cpa-[a-z-]+:\s*/, ""));
|
|
3859
|
-
}
|
|
3860
|
-
const waited = await waitForJob(runId, exec.signal, TOOL_WAIT_MS);
|
|
3861
|
-
const entry = sessionId === undefined ? { runs: [], recent: [] } : mediaSessions.get(sessionId);
|
|
3862
|
-
const run = entry.runs.find((r) => r.commandId === runId);
|
|
3863
|
-
const files = entry.recent.filter((r) => r.commandId === runId && existsSync(r.file)).map((r) => r.file);
|
|
3864
|
-
const option = planned?.option;
|
|
3865
|
-
const base = {
|
|
3866
|
-
kind: t.kind,
|
|
3867
|
-
...(t.kind === "video" ? { mode: t.mode } : {}),
|
|
3868
|
-
model: option?.modelId ?? run?.modelName ?? "",
|
|
3869
|
-
...(option?.accountName ? { account: option.accountName } : {}),
|
|
3870
|
-
run: runId,
|
|
3871
|
-
commandRow,
|
|
3872
|
-
files,
|
|
3873
|
-
folder: outputDirectory(cwd, planned?.outputDir ?? mediaChoices().settings.outputDir),
|
|
3874
|
-
...(Number.isInteger(run?.seconds) ? { seconds: run.seconds } : {}),
|
|
3875
|
-
...(typeof run?.resolution === "string" ? { resolution: run.resolution } : {}),
|
|
3876
|
-
...(typeof run?.aspect === "string" ? { aspect: run.aspect } : {}),
|
|
3877
|
-
};
|
|
3878
|
-
if (waited !== "settled" || run?.status === "running") {
|
|
3879
|
-
return {
|
|
3880
|
-
status: "running",
|
|
3881
|
-
...base,
|
|
3882
|
-
note:
|
|
3883
|
-
waited === "aborted"
|
|
3884
|
-
? "Stopped waiting. The generation keeps running in the background (and is billed); the file will be saved in the folder above and shown in this call's row."
|
|
3885
|
-
: "Still generating after 10 minutes. It keeps running in the background; the file will be saved in the folder above and shown in this call's row.",
|
|
3886
|
-
};
|
|
3887
|
-
}
|
|
3888
|
-
if (run === undefined || run.status === "error") {
|
|
3889
|
-
if (run === undefined || (run.videoId === undefined && NOT_SUBMITTED.has(run.errorCode))) refund();
|
|
3890
|
-
throw new Error(run?.error ?? "the generation failed");
|
|
3891
|
-
}
|
|
3892
|
-
const previews = t.kind === "image" ? await imagePreviews(files, agent, exec.signal) : [];
|
|
3893
|
-
return {
|
|
3894
|
-
status: "done",
|
|
3895
|
-
...base,
|
|
3896
|
-
...(typeof run.revisedPrompt === "string" ? { revisedPrompt: run.revisedPrompt } : {}),
|
|
3897
|
-
...(previews.length > 0 ? { previews } : {}),
|
|
3898
|
-
};
|
|
3899
|
-
};
|
|
3900
|
-
|
|
3901
|
-
/** The model-facing content: a short summary, then any image previews. */
|
|
3902
|
-
const renderGenerateMedia = (_args, value) => {
|
|
3903
|
-
const lines = [
|
|
3904
|
-
`${value.status === "done" ? "Saved" : "Running"}: ${value.kind}${value.mode && value.mode !== "generate" ? ` (${value.mode})` : ""} with ${value.model}${value.account ? ` (${value.account})` : ""}${value.seconds ? `, ${value.seconds} s` : ""}${value.resolution ? `, ${value.resolution}` : ""}${value.aspect ? `, ${value.aspect}` : ""}`,
|
|
3905
|
-
...value.files.map((file) => file),
|
|
3906
|
-
...(value.files.length === 0 ? [`Folder: ${value.folder}`] : []),
|
|
3907
|
-
...(value.revisedPrompt ? [`Revised prompt: ${value.revisedPrompt}`] : []),
|
|
3908
|
-
...(value.note ? [value.note] : []),
|
|
3909
|
-
];
|
|
3910
|
-
return [
|
|
3911
|
-
{ type: "text", text: lines.join("\n") },
|
|
3912
|
-
...(value.previews ?? []).map((p) => ({
|
|
3913
|
-
type: "image",
|
|
3914
|
-
attachment: {
|
|
3915
|
-
attachmentId: p.attachmentId,
|
|
3916
|
-
mediaType: p.mediaType,
|
|
3917
|
-
bytes: p.bytes,
|
|
3918
|
-
width: p.width,
|
|
3919
|
-
height: p.height,
|
|
3920
|
-
...(p.name === undefined ? {} : { name: p.name }),
|
|
3921
|
-
...(p.originalDimensions === undefined ? {} : { originalDimensions: { ...p.originalDimensions } }),
|
|
3922
|
-
},
|
|
3923
|
-
})),
|
|
3924
|
-
];
|
|
3925
|
-
};
|
|
3926
|
-
|
|
3927
|
-
const generateMediaTool = {
|
|
3928
|
-
name: GENERATE_MEDIA,
|
|
3929
|
-
description: GENERATE_MEDIA_TOOL.description,
|
|
3930
|
-
parameters: GENERATE_MEDIA_TOOL.parameters,
|
|
3931
|
-
output: {
|
|
3932
|
-
schema: GENERATE_MEDIA_OUTPUT,
|
|
3933
|
-
render: renderGenerateMedia,
|
|
3934
|
-
presentationMeta: (_args, value) => ({ run: value.run, status: value.status, commandRow: value.commandRow === true }),
|
|
3935
|
-
},
|
|
3936
|
-
execute: executeGenerateMedia,
|
|
3937
|
-
};
|
|
3938
|
-
|
|
3939
|
-
/** Register or unregister the tool to match the switch and the offered models. */
|
|
3940
|
-
const syncAgentTool = () => {
|
|
3941
|
-
let wanted = false;
|
|
3942
|
-
try {
|
|
3943
|
-
const { settings, options } = mediaChoices();
|
|
3944
|
-
wanted = settings.agentTool === true && options.image.length + options.video.length > 0;
|
|
3945
|
-
} catch {
|
|
3946
|
-
wanted = false;
|
|
3947
|
-
}
|
|
3948
|
-
if (wanted && toolOff === undefined && toolsCtx !== undefined) {
|
|
3949
|
-
try {
|
|
3950
|
-
const off = toolsCtx.tools.register(generateMediaTool);
|
|
3951
|
-
toolOff = typeof off === "function" ? off : () => {};
|
|
3952
|
-
ctx.logger?.info?.("cpa: generate_media tool registered");
|
|
3953
|
-
} catch (error) {
|
|
3954
|
-
ctx.logger?.warn?.(`cpa: could not register generate_media: ${error instanceof Error ? error.message : String(error)}`);
|
|
3955
|
-
}
|
|
3956
|
-
} else if (!wanted && toolOff !== undefined) {
|
|
3957
|
-
cancelConfirms();
|
|
3958
|
-
const off = toolOff;
|
|
3959
|
-
toolOff = undefined;
|
|
3960
|
-
try {
|
|
3961
|
-
off();
|
|
3962
|
-
} catch {
|
|
3963
|
-
// already gone with its scope
|
|
3964
|
-
}
|
|
3965
|
-
ctx.logger?.info?.("cpa: generate_media tool unregistered");
|
|
3966
|
-
}
|
|
3967
|
-
};
|
|
3968
|
-
// #endregion
|
|
3969
|
-
|
|
3970
|
-
reschedule();
|
|
3971
|
-
resumeVideoJobs();
|
|
3972
|
-
startupTimeout = outsideHmrTransaction(ctx, () =>
|
|
3973
|
-
setTimeout(() => {
|
|
3974
|
-
startupTimeout = undefined;
|
|
3975
|
-
void controller.refreshAll().finally(() => syncAgentTool());
|
|
3976
|
-
}, 600)
|
|
3977
|
-
);
|
|
3978
|
-
|
|
3979
|
-
ctx.on("credentials/reference-updated", (ref) => {
|
|
3980
|
-
for (const id of controller.accountsUsingKey(ref)) {
|
|
3981
|
-
void controller.refreshAccount(id);
|
|
3982
|
-
}
|
|
3983
|
-
});
|
|
3984
|
-
|
|
3985
|
-
ctx.effect(
|
|
3986
|
-
() => () => {
|
|
3987
|
-
if (timer !== undefined) clearInterval(timer);
|
|
3988
|
-
if (startupTimeout !== undefined) clearTimeout(startupTimeout);
|
|
3989
|
-
controller.dispose();
|
|
3990
|
-
},
|
|
3991
|
-
"cpa: refresh loop"
|
|
3992
|
-
);
|
|
3993
|
-
|
|
3994
|
-
// Register slash commands (/cpa-status, /cpa-refresh)
|
|
3995
|
-
ctx.inject(["commands"], (cmdCtx) => {
|
|
3996
|
-
cmdCtx.effect(() => {
|
|
3997
|
-
const offStatus = cmdCtx.commands.register({
|
|
3998
|
-
name: "cpa-status",
|
|
3999
|
-
description: "Show CLIProxyAPI discovery and model synchronization status of every account.",
|
|
4000
|
-
handler: async () => {
|
|
4001
|
-
const payload = await controller.statusPayload();
|
|
4002
|
-
const quotaText = renderQuotaText(await quota.snapshot());
|
|
4003
|
-
return { kind: "success", text: [renderStatusText(payload), quotaText].filter(Boolean).join("\n\n") };
|
|
4004
|
-
},
|
|
4005
|
-
});
|
|
4006
|
-
const offRefresh = cmdCtx.commands.register({
|
|
4007
|
-
name: "cpa-refresh",
|
|
4008
|
-
description: "Probe every CLIProxyAPI account now and refresh the synchronized DSH model catalog.",
|
|
4009
|
-
handler: async () => {
|
|
4010
|
-
const payload = await controller.refresh(undefined);
|
|
4011
|
-
syncAgentTool();
|
|
4012
|
-
const failed = payload.accounts.some((a) => a.status.state === "error");
|
|
4013
|
-
return { kind: failed ? "error" : "success", text: renderStatusText(payload) };
|
|
4014
|
-
},
|
|
4015
|
-
});
|
|
4016
|
-
const offVideo = cmdCtx.commands.register({
|
|
4017
|
-
name: "cpa-video",
|
|
4018
|
-
description: "Generate a video with a CLIProxyAPI video model: text only, one image as the first frame, reference images (--ref, or 2+ images), or pinned first / last frames (--first-last, --last). A submitted video cannot be cancelled.",
|
|
4019
|
-
input: { hint: "[-m <model>] [--aspect 16:9] [-s 1-15] [-r 480p|720p] [--ref|--first-last|--last] <prompt>", attachments: true },
|
|
4020
|
-
handler: runVideoCommand,
|
|
4021
|
-
});
|
|
4022
|
-
const offVideoEdit = cmdCtx.commands.register({
|
|
4023
|
-
name: "cpa-video-edit",
|
|
4024
|
-
description: "Edit any MP4 of up to 8.7 s with grok-imagine-video: attach it, or pass --video <path>, --url <public URL> or --from <command id> (default: this session's newest video). A submitted video cannot be cancelled.",
|
|
4025
|
-
input: { hint: "[--video <path> | --url <URL> | --from <command id>] [-m <model>] <what to change>", attachments: true },
|
|
4026
|
-
handler: runVideoEditCommand("edit"),
|
|
4027
|
-
});
|
|
4028
|
-
const offVideoExtend = cmdCtx.commands.register({
|
|
4029
|
-
name: "cpa-video-extend",
|
|
4030
|
-
description: "Continue any MP4 of 2-15 s by 2-10 seconds: attach it, or pass --video <path>, --url <public URL> or --from <command id> (default: this session's newest video). A submitted video cannot be cancelled.",
|
|
4031
|
-
input: { hint: "[--video <path> | --url <URL> | --from <command id>] [-m <model>] [-s 2-10] <what happens next>", attachments: true },
|
|
4032
|
-
handler: runVideoEditCommand("extend"),
|
|
4033
|
-
});
|
|
4034
|
-
const offVideoStatus = cmdCtx.commands.register({
|
|
4035
|
-
name: "cpa-video-status",
|
|
4036
|
-
description: "List this session's CLIProxyAPI video jobs and their state.",
|
|
4037
|
-
handler: runVideoStatusCommand,
|
|
4038
|
-
});
|
|
4039
|
-
const offImage = cmdCtx.commands.register({
|
|
4040
|
-
name: "cpa-image",
|
|
4041
|
-
description: "Generate an image with a CLIProxyAPI image model; attach images to edit them. Files go to the session's working directory.",
|
|
4042
|
-
input: { hint: "[-m <model>] [--aspect 16:9] [-n 1-4] <prompt>", attachments: true },
|
|
4043
|
-
handler: runImageCommand,
|
|
4044
|
-
});
|
|
4045
|
-
return () => {
|
|
4046
|
-
offStatus?.();
|
|
4047
|
-
offRefresh?.();
|
|
4048
|
-
offImage?.();
|
|
4049
|
-
offVideo?.();
|
|
4050
|
-
offVideoStatus?.();
|
|
4051
|
-
offVideoEdit?.();
|
|
4052
|
-
offVideoExtend?.();
|
|
4053
|
-
};
|
|
4054
|
-
}, "cpa: commands");
|
|
4055
|
-
});
|
|
4056
|
-
|
|
4057
|
-
// The optional agent tool (registered only while switched on; see syncAgentTool).
|
|
4058
|
-
ctx.inject(["tools"], (scope) => {
|
|
4059
|
-
scope.effect(() => {
|
|
4060
|
-
toolsCtx = scope;
|
|
4061
|
-
syncAgentTool();
|
|
4062
|
-
return () => {
|
|
4063
|
-
cancelConfirms();
|
|
4064
|
-
const off = toolOff;
|
|
4065
|
-
toolOff = undefined;
|
|
4066
|
-
toolsCtx = undefined;
|
|
4067
|
-
try {
|
|
4068
|
-
off?.();
|
|
4069
|
-
} catch {
|
|
4070
|
-
// disposed with its scope
|
|
4071
|
-
}
|
|
4072
|
-
};
|
|
4073
|
-
}, "cpa: agent tool");
|
|
4074
|
-
});
|
|
4075
|
-
|
|
4076
|
-
// Register HTTP API Routes on webServer for the Web UI
|
|
4077
|
-
ctx.inject(["webServer"], (webCtx) => {
|
|
4078
|
-
webCtx.effect(() => {
|
|
4079
|
-
const route = (path, method, run, errorStatus = 500) =>
|
|
4080
|
-
webCtx.webServer.register({
|
|
4081
|
-
kind: "exact",
|
|
4082
|
-
path,
|
|
4083
|
-
handler: async (req, res) => {
|
|
4084
|
-
if (!admitRequest(ctx, req, res, method)) return;
|
|
4085
|
-
try {
|
|
4086
|
-
const body = method === "POST" ? (await readJsonBody(req)) ?? {} : {};
|
|
4087
|
-
const result = await run(body, req);
|
|
4088
|
-
// Account, model and Generation-tab changes may switch the agent tool on or off.
|
|
4089
|
-
if (method === "POST") syncAgentTool();
|
|
4090
|
-
sendJson(res, 200, result);
|
|
4091
|
-
} catch (error) {
|
|
4092
|
-
sendJson(res, errorStatus, {
|
|
4093
|
-
ok: false,
|
|
4094
|
-
error: error instanceof Error ? error.message : String(error),
|
|
4095
|
-
});
|
|
4096
|
-
}
|
|
4097
|
-
},
|
|
4098
|
-
});
|
|
4099
|
-
const updateRoute = (path, method, run) => webCtx.webServer.register({
|
|
4100
|
-
kind: "exact", path,
|
|
4101
|
-
handler: async (req, res) => {
|
|
4102
|
-
if (!admitUpdateRequest(ctx, req, res, method)) return;
|
|
4103
|
-
try {
|
|
4104
|
-
const body = method === "POST" ? await readJsonBody(req, 8192) : {};
|
|
4105
|
-
if (!plainObject(body)) throw new UpdateError("invalid-settings");
|
|
4106
|
-
if (path === ROUTES.updateDownload && (Object.keys(body).some((key) => key !== "version") || typeof body.version !== "string")) {
|
|
4107
|
-
throw new UpdateError("stale-version");
|
|
4108
|
-
}
|
|
4109
|
-
if (path === ROUTES.updateCheck && Object.keys(body).length) throw new UpdateError("invalid-settings");
|
|
4110
|
-
const update = await run(body);
|
|
4111
|
-
sendJson(res, 200, { ok: true, update });
|
|
4112
|
-
} catch (error) {
|
|
4113
|
-
const errorCode = error instanceof UpdateError ? error.code : "invalid-settings";
|
|
4114
|
-
sendJson(res, errorCode === "busy" ? 409 : 400, { ok: false, errorCode, error: errorCode, update: updater.status() });
|
|
4115
|
-
}
|
|
4116
|
-
},
|
|
4117
|
-
});
|
|
4118
|
-
const disposers = [
|
|
4119
|
-
updateRoute(ROUTES.updateStatus, "GET", () => updater.status()),
|
|
4120
|
-
updateRoute(ROUTES.updateCheck, "POST", () => updater.check()),
|
|
4121
|
-
updateRoute(ROUTES.updateSave, "POST", (body) => updater.save(body)),
|
|
4122
|
-
updateRoute(ROUTES.updateDownload, "POST", (body) => updater.download(body.version)),
|
|
4123
|
-
route(ROUTES.status, "GET", () => controller.statusPayload()),
|
|
4124
|
-
route(ROUTES.refresh, "POST", (body) => controller.refresh(body.accountId)),
|
|
4125
|
-
route(
|
|
4126
|
-
ROUTES.save,
|
|
4127
|
-
"POST",
|
|
4128
|
-
(body) => {
|
|
4129
|
-
const patch = accountPatchFromInput(body);
|
|
4130
|
-
const hasAccountFields = Object.keys(patch).some((k) => !GLOBAL_FIELDS.includes(k));
|
|
4131
|
-
return body.accountId === undefined && !hasAccountFields
|
|
4132
|
-
? controller.updateGlobals(patch)
|
|
4133
|
-
: controller.updateAccount(body.accountId, patch);
|
|
4134
|
-
},
|
|
4135
|
-
400
|
|
4136
|
-
),
|
|
4137
|
-
route(ROUTES.addAccount, "POST", (body) => controller.addAccount(accountPatchFromInput(body)), 400),
|
|
4138
|
-
route(ROUTES.removeAccount, "POST", (body) => controller.removeAccount(body.accountId), 400),
|
|
4139
|
-
route(ROUTES.testAccount, "POST", (body) => controller.testConnection(body.accountId, { baseURL: body.baseURL, apiKey: body.apiKey }), 400),
|
|
4140
|
-
route(ROUTES.quotaStatus, "GET", async () => ({ ok: true, quota: await quota.snapshot() })),
|
|
4141
|
-
route(ROUTES.quotaRefresh, "POST", async (body) => ({
|
|
4142
|
-
ok: true,
|
|
4143
|
-
quota: await quota.refresh(typeof body.accountId === "string" && body.accountId !== "" ? body.accountId : undefined, { wait: false }),
|
|
4144
|
-
})),
|
|
4145
|
-
route(
|
|
4146
|
-
ROUTES.quotaSave,
|
|
4147
|
-
"POST",
|
|
4148
|
-
async (body) => {
|
|
4149
|
-
if (typeof body.accountId !== "string" || body.accountId === "") {
|
|
4150
|
-
throw new Error("dsh-cliproxyapi: saving a management key needs accountId");
|
|
4151
|
-
}
|
|
4152
|
-
const managementKey = typeof body.managementKey === "string" ? body.managementKey.trim() : "";
|
|
4153
|
-
return {
|
|
4154
|
-
ok: true,
|
|
4155
|
-
quota: await quota.saveKey(body.accountId, {
|
|
4156
|
-
...(managementKey === "" ? {} : { managementKey }),
|
|
4157
|
-
...(body.clear === true ? { clear: true } : {}),
|
|
4158
|
-
}, { wait: false }),
|
|
4159
|
-
};
|
|
4160
|
-
},
|
|
4161
|
-
400
|
|
4162
|
-
),
|
|
4163
|
-
route(ROUTES.mediaStatus, "GET", async () => ({ ok: true, media: mediaStatus() })),
|
|
4164
|
-
route(ROUTES.mediaSave, "POST", saveMedia, 400),
|
|
4165
|
-
route(
|
|
4166
|
-
ROUTES.mediaDiagnose,
|
|
4167
|
-
"POST",
|
|
4168
|
-
async (body) => {
|
|
4169
|
-
if (typeof body.accountId !== "string" || body.accountId === "") throw new MediaError("invalid-account");
|
|
4170
|
-
return { ok: true, diagnostic: await controller.diagnoseMedia(body.accountId) };
|
|
4171
|
-
},
|
|
4172
|
-
400
|
|
4173
|
-
),
|
|
4174
|
-
route(
|
|
4175
|
-
ROUTES.mediaSession,
|
|
4176
|
-
"GET",
|
|
4177
|
-
async (_body, req) => {
|
|
4178
|
-
const raw = new URL(req.url ?? "/", "http://host").searchParams.get("sessionId");
|
|
4179
|
-
const sessionId = raw === null || raw === "" ? undefined : validSessionId(raw);
|
|
4180
|
-
if (raw !== null && raw !== "" && sessionId === undefined) throw new MediaError("invalid-session");
|
|
4181
|
-
// Right after a start the catalogs may not be read yet; sync once
|
|
4182
|
-
// instead of letting the composer button wait for its next retry.
|
|
4183
|
-
await mediaChoicesFor("any");
|
|
4184
|
-
return { ok: true, session: sessionView(sessionId) };
|
|
4185
|
-
},
|
|
4186
|
-
400
|
|
4187
|
-
),
|
|
4188
|
-
route(ROUTES.mediaSessionSave, "POST", async (body) => saveSessionPick(body), 400),
|
|
4189
|
-
// The user's answer to a generate_media confirmation shown in its chat row.
|
|
4190
|
-
route(
|
|
4191
|
-
ROUTES.mediaConfirm,
|
|
4192
|
-
"POST",
|
|
4193
|
-
async (body) => {
|
|
4194
|
-
const sessionId = validSessionId(body.sessionId);
|
|
4195
|
-
const commandId = validCommandId(body.commandId);
|
|
4196
|
-
if (sessionId === undefined || commandId === undefined || typeof body.allow !== "boolean") throw new MediaError("invalid-request");
|
|
4197
|
-
const waiting = pendingConfirms.get(commandId);
|
|
4198
|
-
if (waiting === undefined || waiting.sessionId !== sessionId) throw new MediaError("confirm-expired");
|
|
4199
|
-
waiting.settle(body.allow ? "allowed" : "rejected");
|
|
4200
|
-
return { ok: true };
|
|
4201
|
-
},
|
|
4202
|
-
400
|
|
4203
|
-
),
|
|
4204
|
-
// One slash-command run's record and files (its chat row shows them).
|
|
4205
|
-
// The absolute paths let the page open or reveal a file through DSH's
|
|
4206
|
-
// own Session Remote (`session.openWorkspacePath`), which re-verifies
|
|
4207
|
-
// them on the Host.
|
|
4208
|
-
route(
|
|
4209
|
-
ROUTES.mediaCommand,
|
|
4210
|
-
"GET",
|
|
4211
|
-
async (_body, req) => {
|
|
4212
|
-
const params = new URL(req.url ?? "/", "http://host").searchParams;
|
|
4213
|
-
const sessionId = validSessionId(params.get("sessionId") ?? "");
|
|
4214
|
-
const commandId = validCommandId(params.get("commandId") ?? "");
|
|
4215
|
-
if (sessionId === undefined || commandId === undefined) throw new MediaError("invalid-request");
|
|
4216
|
-
const entry = mediaSessions.get(sessionId);
|
|
4217
|
-
const files = entry.recent
|
|
4218
|
-
.filter((r) => r.commandId === commandId && existsSync(r.file))
|
|
4219
|
-
.map((r) => ({ id: r.id, kind: r.kind, name: basename(r.file), path: r.file }));
|
|
4220
|
-
let run = entry.runs.find((r) => r.commandId === commandId) ?? null;
|
|
4221
|
-
// A run recorded as running that no job of this activation owns was
|
|
4222
|
-
// cut off by a reload or a restart.
|
|
4223
|
-
if (run?.status === "running" && !mediaJobs.has(commandId)) {
|
|
4224
|
-
run = { ...run, status: "error", errorCode: "interrupted", error: mediaFailureText(new MediaError("interrupted"), run.kind) };
|
|
4225
|
-
} else if (run?.status === "running" && videoLive.has(commandId)) {
|
|
4226
|
-
// A video's phase (submitting / queued / running / downloading) and CPA's progress, if it reports one.
|
|
4227
|
-
run = { ...run, ...videoLive.get(commandId) };
|
|
4228
|
-
}
|
|
4229
|
-
// Which file manager "show in folder" opens, to name the menu item.
|
|
4230
|
-
const desktop = process.platform === "win32" ? "explorer" : process.platform === "darwin" ? "finder" : "filemanager";
|
|
4231
|
-
// Edit / extend this video: by xAI's short-lived link while it is
|
|
4232
|
-
// remembered, else by sending its file inline when small enough.
|
|
4233
|
-
let actions;
|
|
4234
|
-
if (run?.kind === "video" && run.status === "done") {
|
|
4235
|
-
const source = usableSource(commandId);
|
|
4236
|
-
const video = files.find((f) => f.kind === "video");
|
|
4237
|
-
let size;
|
|
4238
|
-
try {
|
|
4239
|
-
size = video === undefined ? undefined : statSync(video.path).size;
|
|
4240
|
-
} catch {
|
|
4241
|
-
size = undefined;
|
|
4242
|
-
}
|
|
4243
|
-
if (source !== undefined || (size !== undefined && size <= VIDEO_UPLOAD_MAX_BYTES)) {
|
|
4244
|
-
const seconds = source?.seconds ?? run.seconds;
|
|
4245
|
-
const accountId = source?.accountId ?? String(run.model).split("/", 1)[0];
|
|
4246
|
-
actions = {
|
|
4247
|
-
edit: seconds === undefined || seconds <= EDIT_SOURCE_MAX_SECONDS,
|
|
4248
|
-
extend:
|
|
4249
|
-
(seconds === undefined || (seconds >= EXTEND_SOURCE_SECONDS.min && seconds <= EXTEND_SOURCE_SECONDS.max)) &&
|
|
4250
|
-
!unsupportedRoutes.has(`${accountId}/extensions`),
|
|
4251
|
-
...(source === undefined ? {} : { expiresAt: new Date(source.at + VIDEO_SOURCE_TTL_MS).toISOString() }),
|
|
4252
|
-
};
|
|
4253
|
-
}
|
|
4254
|
-
}
|
|
4255
|
-
// A generate_media call waiting for the user's confirmation in this row.
|
|
4256
|
-
const waiting = pendingConfirms.get(commandId);
|
|
4257
|
-
const confirm = waiting !== undefined && waiting.sessionId === sessionId ? { kind: waiting.kind, en: waiting.en, zh: waiting.zh, expiresAt: waiting.expiresAt } : undefined;
|
|
4258
|
-
return {
|
|
4259
|
-
ok: true,
|
|
4260
|
-
run,
|
|
4261
|
-
files,
|
|
4262
|
-
desktop,
|
|
4263
|
-
...(files.length > 0 ? { folderPath: dirname(files[0].path) } : {}),
|
|
4264
|
-
...(actions === undefined ? {} : { actions }),
|
|
4265
|
-
...(confirm === undefined ? {} : { confirm }),
|
|
4266
|
-
};
|
|
4267
|
-
},
|
|
4268
|
-
400
|
|
4269
|
-
),
|
|
4270
|
-
// Serves only files this plugin wrote and recorded for that session.
|
|
4271
|
-
webCtx.webServer.register({
|
|
4272
|
-
kind: "exact",
|
|
4273
|
-
path: ROUTES.mediaFile,
|
|
4274
|
-
handler: async (req, res) => {
|
|
4275
|
-
if (!admitRequest(ctx, req, res, "GET")) return;
|
|
4276
|
-
const params = new URL(req.url ?? "/", "http://host").searchParams;
|
|
4277
|
-
const sessionId = validSessionId(params.get("sessionId") ?? "");
|
|
4278
|
-
const record = sessionId === undefined ? undefined : mediaSessions.get(sessionId).recent.find((r) => r.id === params.get("id"));
|
|
4279
|
-
const type = record === undefined ? undefined : MEDIA_FILE_TYPES[extname(record.file).toLowerCase()];
|
|
4280
|
-
if (record === undefined || type === undefined || !existsSync(record.file)) {
|
|
4281
|
-
sendJson(res, 404, { ok: false, error: "not-found" });
|
|
4282
|
-
return;
|
|
4283
|
-
}
|
|
4284
|
-
try {
|
|
4285
|
-
if (!type.startsWith("video/")) {
|
|
4286
|
-
const bytes = readFileSync(record.file);
|
|
4287
|
-
res.writeHead(200, {
|
|
4288
|
-
"cache-control": "no-store",
|
|
4289
|
-
"content-type": type,
|
|
4290
|
-
"content-length": String(bytes.byteLength),
|
|
4291
|
-
"x-content-type-options": "nosniff",
|
|
4292
|
-
});
|
|
4293
|
-
res.end(bytes);
|
|
4294
|
-
return;
|
|
4295
|
-
}
|
|
4296
|
-
// Videos are streamed, with single byte ranges so <video> can seek.
|
|
4297
|
-
const size = statSync(record.file).size;
|
|
4298
|
-
const range = byteRange(req.headers?.range, size);
|
|
4299
|
-
if (range === null) {
|
|
4300
|
-
res.writeHead(416, { "content-range": `bytes */${size}` });
|
|
4301
|
-
res.end();
|
|
4302
|
-
return;
|
|
4303
|
-
}
|
|
4304
|
-
const [start, end] = range ?? [0, size - 1];
|
|
4305
|
-
res.writeHead(range === undefined ? 200 : 206, {
|
|
4306
|
-
"cache-control": "no-store",
|
|
4307
|
-
"content-type": type,
|
|
4308
|
-
"accept-ranges": "bytes",
|
|
4309
|
-
"content-length": String(size === 0 ? 0 : end - start + 1),
|
|
4310
|
-
...(range === undefined ? {} : { "content-range": `bytes ${start}-${end}/${size}` }),
|
|
4311
|
-
"x-content-type-options": "nosniff",
|
|
4312
|
-
});
|
|
4313
|
-
if (size === 0) {
|
|
4314
|
-
res.end();
|
|
4315
|
-
return;
|
|
4316
|
-
}
|
|
4317
|
-
const stream = createReadStream(record.file, { start, end });
|
|
4318
|
-
stream.on("error", () => res.destroy?.());
|
|
4319
|
-
res.on?.("close", () => stream.destroy());
|
|
4320
|
-
stream.pipe(res);
|
|
4321
|
-
} catch {
|
|
4322
|
-
sendJson(res, 404, { ok: false, error: "not-found" });
|
|
4323
|
-
}
|
|
4324
|
-
},
|
|
4325
|
-
}),
|
|
4326
|
-
];
|
|
4327
|
-
return () => {
|
|
4328
|
-
for (const dispose of disposers) dispose?.();
|
|
4329
|
-
};
|
|
4330
|
-
}, "cpa: webserver routes");
|
|
4331
|
-
});
|
|
4332
|
-
}
|
|
8
|
+
import { PACKAGE_NAME } from "./host/config.js";
|
|
9
|
+
import { mountOnce } from "./host/mount-guard.js";
|
|
10
|
+
import { applyImpl } from "./host/apply/index.js";
|
|
11
|
+
|
|
12
|
+
export { pickAccount, normalizeState, mergeHostConfig, accountConflicts, assertNoConflicts, newAccountDefaults, patchAccount } from "./host/accounts.js";
|
|
13
|
+
export { PACKAGE_NAME, name, inject, DEFAULT_ACCOUNT_ID, DEFAULT_PROVIDER, DEFAULT_DISPLAY_NAME, DEFAULT_API_KEY_ENV, STATE_VERSION, ACCOUNT_FIELDS, DEFAULT_REFRESH_INTERVAL_SECONDS, DEFAULT_PROBE_TIMEOUT_MS, LOCAL_CANDIDATES, MAX_RECOMMENDED_MODELS, PROTOCOLS, normalizeModelOverrides, resolveConfig, normalizeAccountId } from "./host/config.js";
|
|
14
|
+
export { serializeSettings, createCpaController } from "./host/controller.js";
|
|
15
|
+
export { normalizeCpaRoot, discoveryCandidates, parseCatalog, discoverCpa } from "./host/discovery.js";
|
|
16
|
+
export { ROUTES } from "./host/http-gate.js";
|
|
17
|
+
export { filterModels, modelFamily, sortModelsNewestFirst, recommendModels } from "./host/models.js";
|
|
18
|
+
export { nativeProtocol, modelProtocol, routeKeyFor, applyModelOverride, isChatModel, buildPiAiRoutes, managedRouteKeys } from "./host/routes.js";
|
|
19
|
+
export { CpaRuntime } from "./host/runtime.js";
|
|
20
|
+
export { resolveStateFilePath, loadPersistedState, savePersistedState } from "./host/state-store.js";
|
|
21
|
+
export { renderQuotaText, renderStatusText } from "./host/status-text.js";
|
|
4333
22
|
|
|
4334
23
|
export const apply = mountOnce(PACKAGE_NAME, applyImpl);
|
|
4335
|
-
|
|
4336
|
-
// #endregion
|