dsh-cliproxyapi-pro 1.9.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.en.md +24 -408
  3. package/README.md +24 -408
  4. package/lib/client.js +265 -27
  5. package/lib/host/accounts.js +178 -0
  6. package/lib/host/apply/agent-tool.js +406 -0
  7. package/lib/host/apply/commands.js +122 -0
  8. package/lib/host/apply/controller.js +121 -0
  9. package/lib/host/apply/http-routes.js +273 -0
  10. package/lib/host/apply/image-jobs.js +171 -0
  11. package/lib/host/apply/index.js +41 -0
  12. package/lib/host/apply/interpret.js +72 -0
  13. package/lib/host/apply/lifecycle.js +50 -0
  14. package/lib/host/apply/media-failures.js +68 -0
  15. package/lib/host/apply/media-service.js +202 -0
  16. package/lib/host/apply/quota-service.js +42 -0
  17. package/lib/host/apply/references.js +43 -0
  18. package/lib/host/apply/session-status.js +104 -0
  19. package/lib/host/apply/state.js +78 -0
  20. package/lib/host/apply/updates.js +55 -0
  21. package/lib/host/apply/video-command.js +152 -0
  22. package/lib/host/apply/video-edit.js +253 -0
  23. package/lib/host/apply/video-jobs.js +206 -0
  24. package/lib/host/apply-utils.js +39 -0
  25. package/lib/host/config.js +210 -0
  26. package/lib/host/controller.js +464 -0
  27. package/lib/host/discovery.js +301 -0
  28. package/lib/host/http-gate.js +144 -0
  29. package/lib/host/model-info.js +363 -0
  30. package/lib/host/model-names.js +152 -0
  31. package/lib/host/models.js +75 -0
  32. package/lib/host/mount-guard.js +56 -0
  33. package/lib/host/recommend.js +367 -0
  34. package/lib/host/routes.js +165 -0
  35. package/lib/host/runtime.js +471 -0
  36. package/lib/host/state-store.js +39 -0
  37. package/lib/host/status-text.js +93 -0
  38. package/lib/index.js +18 -4417
  39. package/lib/media/agent-tool.js +255 -0
  40. package/lib/media/choice.js +83 -0
  41. package/lib/media/classify.js +65 -0
  42. package/lib/media/codec/fit.js +76 -0
  43. package/lib/media/codec/jpeg.js +158 -0
  44. package/lib/media/codec/png.js +102 -0
  45. package/lib/media/commands/parsing.js +269 -0
  46. package/lib/media/constants.js +46 -0
  47. package/lib/media/diagnose.js +134 -0
  48. package/lib/media/images.js +165 -0
  49. package/lib/media/index.js +29 -0
  50. package/lib/media/interpret.js +126 -0
  51. package/lib/media/job-store.js +99 -0
  52. package/lib/media/mp4.js +44 -0
  53. package/lib/media/output-files.js +59 -0
  54. package/lib/media/session-store.js +293 -0
  55. package/lib/media/settings.js +255 -0
  56. package/lib/media/util.js +45 -0
  57. package/lib/media/video.js +326 -0
  58. package/lib/quota/auth-state.js +33 -0
  59. package/lib/quota/constants.js +86 -0
  60. package/lib/quota/controller.js +467 -0
  61. package/lib/quota/helpers.js +325 -0
  62. package/lib/quota/index.js +19 -0
  63. package/lib/quota/management.js +216 -0
  64. package/lib/quota/matching.js +143 -0
  65. package/lib/quota/parsers.js +352 -0
  66. package/lib/quota/probes/billing.js +80 -0
  67. package/lib/quota/probes/google.js +120 -0
  68. package/lib/quota/probes/oauth.js +27 -0
  69. package/lib/quota/probes/plugin.js +18 -0
  70. package/lib/updater/constants.js +23 -0
  71. package/lib/updater/errors.js +8 -0
  72. package/lib/updater/index.js +11 -0
  73. package/lib/updater/registry.js +83 -0
  74. package/lib/updater/settings.js +45 -0
  75. package/lib/updater/store.js +23 -0
  76. package/lib/updater/updater.js +148 -0
  77. package/lib/updater/verify.js +110 -0
  78. package/lib/updater/version.js +31 -0
  79. package/locale/en.json +1 -1
  80. package/locale/zh.json +1 -1
  81. package/package.json +6 -9
  82. package/lib/media.js +0 -2542
  83. package/lib/quota.js +0 -1748
  84. package/lib/updater.js +0 -443
package/lib/index.js CHANGED
@@ -1,4423 +1,24 @@
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
- // It deliberately registers no agent tool: accounts are managed in the Settings
4
- // page, HTTP routes and slash commands, so nothing adds to model context.
5
- // Image / video models (lib/media.js) are classified and kept out of the
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 { createReadStream, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
9
- import { randomUUID } from "node:crypto";
10
- import { homedir } from "node:os";
11
- import { basename, dirname, extname, isAbsolute, join, relative, sep } from "node:path";
12
- import {
13
- createPluginUpdater, loadUpdateSettings, saveUpdateSettings, storeUpdatePackage,
14
- UPDATE_INTERVAL_MS, UpdateError,
15
- } from "./updater.js";
16
- import { createQuotaController } from "./quota.js";
17
- import {
18
- accountKindOverrides, applyMediaPatch, chooseMediaModel, createMediaSessionStore, detectMediaKind,
19
- diagnoseMedia, effectiveKind, generateImages, IMAGE_ASPECTS, loadMediaSettings, MAX_REFERENCE_IMAGES,
20
- mediaAliasTarget, MediaError, mediaModelEnabled, mediaOptions, normalizeMediaSettings, outputDirectory,
21
- outputStem, parseImageCommand, recentEntry, saveMediaSettings, validCommandId, validSessionId, writeOutputFiles,
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}` : ""})` : "") +
2160
- (Number.isInteger(a.authFailures) && a.authFailures > 0
2161
- ? `, key rejected ${a.authFailures}/${a.authFailureLimit}${a.authPaused ? ", automatic checks stopped until a new management key is saved" : ""}`
2162
- : "") +
2163
- (a.refreshing ? ", refreshing" : "");
2164
- lines.push(`- ${a.displayName} [${a.accountId}]: ${state}; ${creds.length} credential(s)${a.fetchedAt ? `; fetched ${a.fetchedAt}` : ""}; routes ${(a.routes ?? []).join(", ") || "none"}`);
2165
- for (const m of a.pickerModels ?? []) {
2166
- const ids = a.modelIndex?.[m.id] ?? [];
2167
- const names = ids.map((id) => {
2168
- const c = creds.find((x) => x.id === id);
2169
- if (!c) return id;
2170
- const pct = (c.windows ?? []).map((w) => w.remainingPercent).filter((v) => typeof v === "number");
2171
- return `${c.provider}:${c.label || c.id}${pct.length ? ` ${Math.round(Math.min(...pct))}%` : ""}${c.status === "active" ? "" : ` (${c.status})`}`;
2172
- });
2173
- lines.push(` ${m.route}/${m.id} -> ${names.length ? names.join(", ") : "no credential"} [${a.modelMatch?.[m.id] ?? "none"}]`);
2174
- }
2175
- }
2176
- return lines.join("\n");
2177
- }
2178
-
2179
- export function renderStatusText(payload, onlyAccountId) {
2180
- const accounts = payload.accounts ?? [];
2181
- if (accounts.length === 0) {
2182
- return "No CLIProxyAPI account is configured. Add one in Settings → CLIProxyAPI.";
2183
- }
2184
- const shown =
2185
- onlyAccountId === undefined ? accounts : accounts.filter((a) => a.id === onlyAccountId);
2186
- const enabled = accounts.filter((a) => a.config.enabled).length;
2187
- const header = `CLIProxyAPI accounts: ${accounts.length} (${enabled} enabled); auto refresh every ${payload.global?.refreshIntervalSeconds ?? DEFAULT_REFRESH_INTERVAL_SECONDS}s`;
2188
- return [header, ...shown.map(renderAccountStatus)].join("\n\n");
2189
- }
2190
-
2191
- /** Account fields the save and add routes accept, besides key handling. */
2192
- function accountPatchFromInput(input) {
2193
- const patch = {};
2194
- for (const field of ["baseURL", "provider", "displayName", "protocol", "protocolMode", "includePrefix", "excludePrefix", "apiKeyEnv"]) {
2195
- if (typeof input[field] === "string") patch[field] = input[field];
2196
- }
2197
- if (typeof input.enabled === "boolean") patch.enabled = input.enabled;
2198
- if (Array.isArray(input.selectedModels)) patch.selectedModels = input.selectedModels;
2199
- if (plainObject(input.modelOverrides) !== undefined) patch.modelOverrides = input.modelOverrides;
2200
- if (typeof input.apiKey === "string" && input.apiKey.trim() !== "") patch.apiKey = input.apiKey.trim();
2201
- if (input.clearApiKey === true) patch.clearApiKey = true;
2202
- for (const field of GLOBAL_FIELDS) {
2203
- if (input[field] !== undefined) patch[field] = input[field];
2204
- }
2205
- return patch;
2206
- }
2207
-
2208
- function applyImpl(ctx, config) {
2209
- const stateFilePath = resolveStateFilePath(ctx);
2210
- const persisted = loadPersistedState(stateFilePath);
2211
-
2212
- const readExistingPiAiRoute = (provider) => {
2213
- try {
2214
- const descriptors = ctx.settings.describe();
2215
- const piAi = descriptors.find((d) => d.ns === PI_AI_NS);
2216
- return piAi?.value?.providers?.[provider];
2217
- } catch {
2218
- return undefined;
2219
- }
2220
- };
2221
-
2222
- const readDefaultModel = () => {
2223
- try {
2224
- const value = ctx.settings.describe().find((d) => d.ns === DEFAULT_MODEL_NS)?.value;
2225
- return typeof value?.provider === "string" && typeof value?.model === "string"
2226
- ? { provider: value.provider, model: value.model }
2227
- : undefined;
2228
- } catch {
2229
- return undefined;
2230
- }
2231
- };
2232
-
2233
- // Seed a selection from every managed route, not just the base one, so a
2234
- // restart after protocol routing keeps Messages / Responses models checked.
2235
- const existingManagedIds = (provider) =>
2236
- managedRouteKeys(provider).flatMap((key) => {
2237
- const route = readExistingPiAiRoute(key);
2238
- return Array.isArray(route?.models)
2239
- ? route.models.map((m) => m?.id).filter((id) => typeof id === "string" && id.trim() !== "")
2240
- : [];
2241
- });
2242
-
2243
- // Migrate a legacy single-account file into `accounts[]`, keeping a copy.
2244
- if (Object.keys(persisted).length > 0 && !Array.isArray(persisted.accounts)) {
2245
- savePersistedState(stateFilePath.replace(/\.json$/, ".v1.json"), persisted);
2246
- }
2247
-
2248
- // An account without a saved selection keeps the models already on its
2249
- // pi-ai routes when that is a curated list (<= 35). Otherwise it stays
2250
- // unset and its first successful probe seeds recommendModels().
2251
- let memState = normalizeState(mergeHostConfig(persisted, config));
2252
- memState = {
2253
- ...memState,
2254
- accounts: memState.accounts.map((account) => {
2255
- if (account.selectedModels !== undefined) return account;
2256
- const existingIds = existingManagedIds(account.provider);
2257
- return existingIds.length > 0 && existingIds.length <= 35
2258
- ? { ...account, selectedModels: existingIds }
2259
- : account;
2260
- }),
2261
- };
2262
- savePersistedState(stateFilePath, memState);
2263
-
2264
- // The state file is re-read on every access so hand edits take effect.
2265
- const readState = () => {
2266
- const fresh = loadPersistedState(stateFilePath);
2267
- if (Object.keys(fresh).length > 0) memState = normalizeState(fresh);
2268
- return memState;
2269
- };
2270
- const writeState = (state) => {
2271
- memState = state;
2272
- savePersistedState(stateFilePath, state);
2273
- };
2274
-
2275
- // Update preferences and packages never share the account state or credentials.
2276
- const updateDirectory = dirname(stateFilePath);
2277
- const updateSettingsPath = join(updateDirectory, "update-settings.json");
2278
- let profile;
2279
- try { profile = ctx.get?.("profileContext"); } catch {}
2280
- const source = typeof profile?.dir === "string"
2281
- ? loadPersistedState(join(profile.dir, "package.json")).dependencies?.[PACKAGE_NAME]
2282
- : undefined;
2283
- const linkedInstall = typeof source === "string" &&
2284
- (/^(?: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));
2285
- const currentVersion = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
2286
- let updateTimer;
2287
- let updateStartup;
2288
- const scheduleUpdates = (preferences) => {
2289
- clearInterval(updateTimer);
2290
- clearTimeout(updateStartup);
2291
- if (!preferences.autoCheck) return;
2292
- outsideHmrTransaction(ctx, () => {
2293
- const run = () => { void Promise.resolve().then(() => updater.check({ automatic: true })).catch(() => {}); };
2294
- updateStartup = setTimeout(run, 3000);
2295
- updateTimer = setInterval(run, UPDATE_INTERVAL_MS);
2296
- });
2297
- };
2298
- const updater = createPluginUpdater({
2299
- currentVersion,
2300
- linkedInstall,
2301
- profileName: profile?.name ?? profile?.profile ?? getEnvValue(ctx, "DSH_PROFILE"),
2302
- readSettings: () => loadUpdateSettings(updateSettingsPath),
2303
- writeSettings: (preferences) => saveUpdateSettings(updateSettingsPath, preferences),
2304
- storePackage: (version, bytes) => storeUpdatePackage(join(updateDirectory, "updates"), version, bytes),
2305
- onSettingsChanged: scheduleUpdates,
2306
- logger: ctx.logger,
2307
- });
2308
- ctx.effect(() => {
2309
- scheduleUpdates(updater.status());
2310
- return () => {
2311
- clearInterval(updateTimer);
2312
- clearTimeout(updateStartup);
2313
- updater.dispose();
2314
- };
2315
- }, "cpa: plugin update checks");
2316
-
2317
- let timer;
2318
- let startupTimeout;
2319
-
2320
- const resolveApiKey = async (refName) => {
2321
- try {
2322
- const ref = toCredentialRef(refName);
2323
- const credentials = ctx.get("credentials");
2324
- const stored = credentials === undefined ? undefined : await credentials.resolve(ref);
2325
- const raw = stored?.value ?? getEnvValue(ctx, ref);
2326
- const value = raw?.trim();
2327
- return value === undefined || value === "" ? undefined : value;
2328
- } catch {
2329
- return undefined;
2330
- }
2331
- };
2332
-
2333
- const describeApiKey = async (refName) => {
2334
- try {
2335
- const ref = toCredentialRef(refName);
2336
- const credentials = ctx.get("credentials");
2337
- if (credentials !== undefined) {
2338
- return await credentials.describe(ref);
2339
- }
2340
- const envVal = getEnvValue(ctx, ref);
2341
- return {
2342
- configured: envVal !== undefined && envVal.length > 0,
2343
- writable: false,
2344
- };
2345
- } catch {
2346
- return { configured: false, writable: true };
2347
- }
2348
- };
2349
-
2350
- const reschedule = () => {
2351
- if (timer !== undefined) clearInterval(timer);
2352
- timer = outsideHmrTransaction(ctx, () =>
2353
- setInterval(() => {
2354
- void controller.refreshAll().finally(() => syncAgentTool());
2355
- }, readState().refreshIntervalSeconds * 1000)
2356
- );
2357
- };
2358
-
2359
- // Media preferences (lib/media.js) live in their own file next to the
2360
- // account state, never inside it, so no state migration is involved.
2361
- const mediaSettingsPath = join(updateDirectory, "media-settings.json");
2362
- let mediaWarned = false;
2363
- // Generation is off for a new profile; one that already picked a model or
2364
- // generated something before the switch existed (session or job file, no
2365
- // readable settings file) keeps it on.
2366
- const mediaUsedBefore = () =>
2367
- existsSync(join(updateDirectory, "media-sessions.json")) || existsSync(join(updateDirectory, "media-jobs.json"));
2368
- const readMedia = () => {
2369
- const loaded = loadMediaSettings(mediaSettingsPath);
2370
- if ((loaded.error !== undefined || !existsSync(mediaSettingsPath)) && mediaUsedBefore()) {
2371
- loaded.settings = { ...loaded.settings, enabled: true };
2372
- }
2373
- if (loaded.error === undefined) {
2374
- mediaWarned = false;
2375
- } else if (!mediaWarned) {
2376
- mediaWarned = true;
2377
- ctx.logger?.warn?.(
2378
- `dsh-cliproxyapi: ${mediaSettingsPath} is unreadable; default media settings apply until it is saved again (the damaged file is then kept as *.corrupt-*.json)`
2379
- );
2380
- }
2381
- return loaded;
2382
- };
2383
-
2384
- const controller = createCpaController({
2385
- settings: ctx.settings,
2386
- readState,
2387
- writeState,
2388
- mediaKindOverrides: (id) => accountKindOverrides(readMedia().settings, id),
2389
- resolveApiKey,
2390
- describeApiKey,
2391
- setCredential: async (ref, value) => {
2392
- const credentials = ctx.get("credentials");
2393
- if (credentials === undefined) {
2394
- throw new Error("Credentials service is not available on this host");
2395
- }
2396
- await credentials.set(ref, value);
2397
- },
2398
- unsetCredential: async (ref) => {
2399
- await ctx.get("credentials")?.unset(ref);
2400
- },
2401
- readExistingPiAiRoute,
2402
- readDefaultModel,
2403
- setTimeout: (fn, ms) => outsideHmrTransaction(ctx, () => setTimeout(fn, ms)),
2404
- environmentBaseURL: () => getEnvValue(ctx, "CLIPROXYAPI_BASE_URL"),
2405
- logger: ctx.logger,
2406
- onGlobalsChanged: () => reschedule(),
2407
- });
2408
-
2409
- // Quota ("额度" tab + composer badge): management keys live in the
2410
- // credentials service under their own refs, never in account state.
2411
- // Consecutive management-key rejections per account (never the key itself),
2412
- // so a wrong key stays stopped across reloads and restarts.
2413
- const quotaStatePath = join(updateDirectory, "quota-state.json");
2414
- const quota = createQuotaController({
2415
- authStore: {
2416
- read: () => (existsSync(quotaStatePath) ? JSON.parse(readFileSync(quotaStatePath, "utf8")) : undefined),
2417
- write: (value) => {
2418
- mkdirSync(dirname(quotaStatePath), { recursive: true });
2419
- const tmp = `${quotaStatePath}.${Date.now().toString(36)}.tmp`;
2420
- writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`, "utf8");
2421
- renameSync(tmp, quotaStatePath);
2422
- },
2423
- },
2424
- listAccounts: () => controller.quotaAccounts(),
2425
- resolveKey: (ref) => resolveApiKey(ref),
2426
- describeKey: (ref) => describeApiKey(ref),
2427
- setKey: async (ref, value) => {
2428
- const credentials = ctx.get("credentials");
2429
- if (credentials === undefined) {
2430
- throw new Error("Credentials service is not available on this host");
2431
- }
2432
- await credentials.set(ref, value);
2433
- },
2434
- unsetKey: async (ref) => {
2435
- await ctx.get("credentials")?.unset(ref);
2436
- },
2437
- logger: ctx.logger,
2438
- });
2439
- ctx.effect(() => () => quota.dispose(), "cpa: quota");
2440
-
2441
- /** Media page payload: preferences (without per-account maps) and each account's media models. */
2442
- const mediaStatus = () => {
2443
- const { settings, error } = readMedia();
2444
- const { accounts: perAccount, ...global } = settings;
2445
- return {
2446
- settings: global,
2447
- ...(error === undefined ? {} : { settingsError: error }),
2448
- accounts: controller.mediaAccounts().map((account) => {
2449
- const overrides = accountKindOverrides(settings, account.id);
2450
- return {
2451
- ...account,
2452
- disabledModels: perAccount[account.id]?.disabledModels ?? [],
2453
- kindOverrides: overrides,
2454
- models: account.models.map((m) => ({
2455
- ...m,
2456
- enabled: mediaModelEnabled(settings, account.id, m.id),
2457
- overridden: overrides[m.id] !== undefined,
2458
- })),
2459
- };
2460
- }),
2461
- };
2462
- };
2463
-
2464
- const saveMedia = async (body) => {
2465
- const accountId = body?.account?.accountId;
2466
- if (accountId !== undefined && !controller.mediaAccounts().some((a) => a.id === accountId)) {
2467
- throw new MediaError("invalid-account");
2468
- }
2469
- const { settings, kindsChanged } = applyMediaPatch(readMedia().settings, body);
2470
- saveMediaSettings(mediaSettingsPath, settings);
2471
- // A reclassified model enters or leaves the chat routes on the next sync.
2472
- if (kindsChanged === undefined) return { ok: true, media: mediaStatus() };
2473
- await controller.refreshAccount(kindsChanged);
2474
- return { ok: true, media: mediaStatus(), status: await controller.statusPayload() };
2475
- };
2476
-
2477
- // Per-session model picks and recent outputs, in their own file.
2478
- const mediaSessions = createMediaSessionStore(join(updateDirectory, "media-sessions.json"));
2479
-
2480
- // With generation switched off nothing is offered: no composer buttons, no
2481
- // agent tool, and no command can pick a model.
2482
- const mediaChoices = () => {
2483
- const { settings } = readMedia();
2484
- const accounts = controller.mediaAccounts();
2485
- const on = settings.enabled === true;
2486
- return {
2487
- settings,
2488
- accounts,
2489
- options: on
2490
- ? { image: mediaOptions(accounts, settings, "image"), video: mediaOptions(accounts, settings, "video") }
2491
- : { image: [], video: [] },
2492
- };
2493
- };
2494
-
2495
- /**
2496
- * Choices for a generation command. When a needed kind is not offered and an
2497
- * enabled account either has not read its catalog yet (just after a start
2498
- * or reload) or reported a failed plain listing, sync once and look again
2499
- * instead of failing on a timing race. An incomplete-catalog retry is
2500
- * limited to once per 30 s so a permanently missing listing is not fetched
2501
- * on every request.
2502
- */
2503
- let catalogRetryAt = 0;
2504
- const mediaChoicesFor = async (kind) => {
2505
- let choices = mediaChoices();
2506
- if (choices.settings.enabled !== true) return choices;
2507
- const missing = (k) => choices.options[k].length === 0;
2508
- const someMissing = kind === "any" ? missing("image") || missing("video") : missing(kind);
2509
- const cold = choices.accounts.some((a) => a.enabled && a.discovered === 0);
2510
- const incomplete = choices.accounts.some((a) => a.enabled && a.catalogIncomplete === true);
2511
- const retryIncomplete = incomplete && Date.now() - catalogRetryAt >= 30_000;
2512
- if (someMissing && (cold || retryIncomplete)) {
2513
- catalogRetryAt = Date.now();
2514
- await controller.refreshAll();
2515
- choices = mediaChoices();
2516
- }
2517
- return choices;
2518
- };
2519
-
2520
- /** One line per account for an empty choice: state and catalog counts, never keys or URLs. */
2521
- const mediaAccountsSummary = (accounts, kind) =>
2522
- accounts
2523
- .map((a) => `${a.displayName || a.id}: ${a.enabled ? a.state : "disabled"}, ${a.discovered} model(s), ${a.models.filter((m) => m.kind === kind).length} ${kind}`)
2524
- .join("; ") || "no account";
2525
-
2526
- /** What the composer button shows for one session (no network access). */
2527
- const sessionView = (sessionId) => {
2528
- const { settings, options, accounts } = mediaChoices();
2529
- const entry = sessionId === undefined ? { recent: [] } : mediaSessions.get(sessionId);
2530
- const current = {};
2531
- for (const kind of ["image", "video"]) {
2532
- try {
2533
- const { option, source } = chooseMediaModel({
2534
- options: options[kind],
2535
- sessionRef: entry[kind],
2536
- lastRef: settings.lastSelection[kind],
2537
- });
2538
- current[kind] = { ref: option.ref, source };
2539
- } catch {
2540
- current[kind] = null;
2541
- }
2542
- }
2543
- const policy = agentPolicy(settings, entry);
2544
- return {
2545
- sessionId: sessionId ?? null,
2546
- // The Generation tab's master switch; off = the composer shows nothing.
2547
- generation: settings.enabled === true,
2548
- options,
2549
- current,
2550
- // Compatibility composer fallback; native questions never enter this map.
2551
- pendingConfirmations: sessionId === undefined ? [] : [...pendingConfirms.entries()]
2552
- .filter(([, waiting]) => waiting.sessionId === sessionId)
2553
- .map(([commandId, { kind, en, zh, expiresAt }]) => ({ commandId, kind, en, zh, expiresAt })),
2554
- // True while an enabled account's last catalog read missed the plain
2555
- // listing; the composer button retries instead of keeping an old,
2556
- // image-less list.
2557
- catalogIncomplete: settings.enabled === true && accounts.some((a) => a.enabled === true && a.catalogIncomplete === true),
2558
- // The agent tool's rules for this session (shown while the tool is switched on).
2559
- agent: {
2560
- enabled: settings.agentTool === true,
2561
- imageConfirm: policy.imageConfirm,
2562
- videoConfirm: policy.videoConfirm,
2563
- videoBudget: policy.budget,
2564
- used: policy.used,
2565
- sources: policy.sources,
2566
- global: { imageConfirm: settings.imageConfirm, videoConfirm: settings.videoConfirm, videoBudget: settings.videoBudget },
2567
- },
2568
- recent: entry.recent
2569
- .filter((r) => existsSync(r.file))
2570
- .slice(0, 12)
2571
- .map((r) => ({ id: r.id, kind: r.kind, name: basename(r.file), model: r.model, createdAt: r.createdAt })),
2572
- };
2573
- };
2574
-
2575
- /** Pick a model for one session (and remember it as the last pick); `ref: null` clears the session's pick. */
2576
- const saveSessionPick = (body) => {
2577
- // Agent-tool overrides for one session (no model pick in the same request).
2578
- if (body?.kind === undefined && ["imageConfirm", "videoConfirm", "videoBudget"].some((k) => k in (body ?? {}))) {
2579
- const sessionId = validSessionId(body.sessionId);
2580
- if (sessionId === undefined) throw new MediaError("invalid-session");
2581
- const { settings } = mediaChoices();
2582
- mediaSessions.update(sessionId, (entry) => applySessionPolicy(entry, body, settings));
2583
- return { ok: true, session: sessionView(sessionId) };
2584
- }
2585
- const kind = body?.kind;
2586
- if (kind !== "image" && kind !== "video") throw new MediaError("invalid-request");
2587
- const sessionId = body.sessionId === undefined || body.sessionId === null ? undefined : validSessionId(body.sessionId);
2588
- if (body.sessionId !== undefined && body.sessionId !== null && sessionId === undefined) throw new MediaError("invalid-session");
2589
- const ref = body.ref === null ? null : typeof body.ref === "string" ? body.ref : undefined;
2590
- if (ref === undefined) throw new MediaError("invalid-model");
2591
- const { settings, options } = mediaChoices();
2592
- if (ref !== null && !options[kind].some((o) => o.ref === ref)) throw new MediaError("invalid-model");
2593
- if (sessionId !== undefined) {
2594
- mediaSessions.update(sessionId, (entry) => {
2595
- if (ref === null) delete entry[kind];
2596
- else entry[kind] = ref;
2597
- });
2598
- }
2599
- if (ref !== null && settings.lastSelection[kind] !== ref) {
2600
- saveMediaSettings(mediaSettingsPath, normalizeMediaSettings({ ...settings, lastSelection: { ...settings.lastSelection, [kind]: ref } }));
2601
- }
2602
- return { ok: true, session: sessionView(sessionId) };
2603
- };
2604
-
2605
- const MEDIA_FILE_TYPES = {
2606
- ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".webp": "image/webp", ".gif": "image/gif",
2607
- ".mp4": "video/mp4", ".webm": "video/webm", ".mov": "video/quicktime",
2608
- };
2609
-
2610
- /**
2611
- * One `Range: bytes=a-b` request against a file of `size` bytes: `[start, end]`,
2612
- * `undefined` without a (usable single) range, `null` when unsatisfiable.
2613
- */
2614
- const byteRange = (header, size) => {
2615
- if (typeof header !== "string" || header === "") return undefined;
2616
- const match = /^bytes=(\d*)-(\d*)$/.exec(header.trim());
2617
- if (match === null || (match[1] === "" && match[2] === "")) return undefined;
2618
- let start;
2619
- let end;
2620
- if (match[1] === "") {
2621
- const suffix = Number(match[2]);
2622
- if (suffix === 0) return null;
2623
- start = Math.max(0, size - suffix);
2624
- end = size - 1;
2625
- } else {
2626
- start = Number(match[1]);
2627
- end = match[2] === "" ? size - 1 : Math.min(Number(match[2]), size - 1);
2628
- }
2629
- if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start > end || start >= size) return null;
2630
- return [start, end];
2631
- };
2632
- // Request bodies carry references base64-encoded and upstreams cap the body
2633
- // (xAI: "Failed to read request body: length limit exceeded"). Video
2634
- // requests use the measured `videoImageBudget()`; image edits keep this
2635
- // unmeasured share.
2636
- const FIRST_FRAME_MAX_BYTES = 1_000_000;
2637
- const REFERENCES_MAX_BYTES = 1_400_000;
2638
-
2639
- /**
2640
- * One attached image within `maxBytes`: as stored when it fits; a PNG is
2641
- * re-encoded here (JPEG on white, long edge ≤ 1280); another format asks
2642
- * DSH's attachment store for a smaller request version.
2643
- * @throws MediaError `reference-too-large`.
2644
- */
2645
- const readReference = async (store, block, maxBytes, signal) => {
2646
- const stored = await store.readImage(block.attachment, signal);
2647
- const image = { mediaType: stored.ref?.mediaType ?? block.attachment?.mediaType, data: stored.data };
2648
- const fitted = fitReferenceImage(image, maxBytes);
2649
- if (fitted !== undefined) return { mediaType: fitted.mediaType, data: fitted.data };
2650
- const width = Number(stored.ref?.width ?? block.attachment?.width);
2651
- const height = Number(stored.ref?.height ?? block.attachment?.height);
2652
- if (typeof store.readImageRequest === "function" && width > 0 && height > 0) {
2653
- const scale = Math.min(1, REFERENCE_MAX_EDGE / Math.max(width, height));
2654
- try {
2655
- const smaller = await store.readImageRequest(
2656
- block.attachment,
2657
- { width: Math.max(1, Math.round(width * scale)), height: Math.max(1, Math.round(height * scale)), maxBytes },
2658
- signal
2659
- );
2660
- if (smaller?.data?.byteLength <= maxBytes) return { mediaType: smaller.mediaType, data: smaller.data };
2661
- } catch (error) {
2662
- if (signal?.aborted) throw error;
2663
- }
2664
- }
2665
- throw new MediaError("reference-too-large", String(Math.round(maxBytes / 1000)));
2666
- };
2667
-
2668
- // #region Parameter interpretation (the session's chat model)
2669
- const INTERPRET_TIMEOUT_MS = 30_000;
2670
- const INTERPRET_MAX_TOKENS = 2000;
2671
-
2672
- /** The provider / model the session's agent last used (else its options, else the default model). */
2673
- const sessionChatTarget = (agent) => {
2674
- const valid = (t) => (t && typeof t.provider === "string" && t.provider !== "" && typeof t.model === "string" && t.model !== "" ? { provider: t.provider, model: t.model } : undefined);
2675
- try {
2676
- const header = valid(agent?.session?.requestHeader?.()?.config);
2677
- if (header !== undefined) return header;
2678
- } catch {
2679
- // fall through
2680
- }
2681
- return valid(agent?.options) ?? valid(ctx.get?.("agentDefaultModel")?.currentSelection?.());
2682
- };
2683
-
2684
- /**
2685
- * Ask the session's chat model which parameters the prompt states.
2686
- * Never throws: without an `llm` service or a model it returns undefined;
2687
- * a failed call returns `{ failed: true, by }`.
2688
- * @param fixed - fields the rule parser already set (they win).
2689
- * @returns `{ by, values, prompt, items, failed? }` or undefined.
2690
- */
2691
- const interpretParams = async ({ agent, command, text, images = 0, fixed = [], signal }) => {
2692
- const llm = ctx.get?.("llm");
2693
- const target = sessionChatTarget(agent);
2694
- if (typeof llm?.stream !== "function" || target === undefined) return undefined;
2695
- const by = `${target.provider}/${target.model}`;
2696
- const request = interpretationRequest({ command, text, images, fixed });
2697
- const timeout = AbortSignal.timeout(INTERPRET_TIMEOUT_MS);
2698
- const combined = signal === undefined ? timeout : AbortSignal.any([signal, timeout]);
2699
- try {
2700
- const parts = new Map();
2701
- for await (const chunk of llm.stream({
2702
- provider: target.provider,
2703
- model: target.model,
2704
- system: request.system,
2705
- messages: [{ role: "user", content: [{ type: "text", text: request.user }] }],
2706
- maxTokens: INTERPRET_MAX_TOKENS,
2707
- ...(agent?.session?.id === undefined ? {} : { sessionId: agent.session.id }),
2708
- purpose: "cpa-media-params",
2709
- signal: combined,
2710
- })) {
2711
- if (chunk?.type === "text-delta") parts.set(chunk.index, (parts.get(chunk.index) ?? "") + chunk.text);
2712
- else if (chunk?.type === "block-end" && chunk.block?.type === "text") parts.set(chunk.index, chunk.block.text);
2713
- }
2714
- const answer = [...parts.values()].join("");
2715
- const parsed = parseInterpretation(answer, { command, text, fields: request.fields, images });
2716
- if (parsed === undefined) throw new Error("no JSON in the answer");
2717
- return { by, ...parsed, items: interpretationLabels(parsed.values) };
2718
- } catch (error) {
2719
- if (signal?.aborted) throw signal.reason;
2720
- ctx.logger?.warn?.(`cpa: parameter interpretation with ${by} failed: ${error instanceof Error ? error.message : String(error)}`);
2721
- return { by, failed: true, values: {}, prompt: text, items: [] };
2722
- }
2723
- };
2724
-
2725
- /** The run-record field describing an interpretation. */
2726
- const interpretedRecord = (result, original) =>
2727
- result === undefined
2728
- ? {}
2729
- : { interpreted: { by: result.by, items: result.items, ...(result.failed ? { failed: true } : {}), ...(result.prompt !== original ? { prompt: true } : {}) } };
2730
- // #endregion
2731
-
2732
- /** Plain-English host text for a media failure (slash command output and run records). */
2733
- const mediaFailureText = (error, kind = "image") => {
2734
- if (!(error instanceof MediaError)) return error instanceof Error ? error.message : String(error);
2735
- const detail = error.detail;
2736
- const video = kind === "video";
2737
- const noun = video ? "video" : "image";
2738
- switch (error.code) {
2739
- 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";
2740
- case "invalid-option": return video
2741
- ? `unknown option ${detail}; use -m <model>, --aspect <ratio>, -s <1-15>, -r <480p|720p>`
2742
- : `unknown option ${detail}; use -m <model>, --aspect <ratio>, -n <1-4>`;
2743
- case "invalid-aspect": return `--aspect must be one of ${(video ? VIDEO_ASPECTS : IMAGE_ASPECTS).join(", ")}`;
2744
- case "invalid-count": return "-n must be 1 to 4";
2745
- case "invalid-video-seconds": return "-s / --seconds must be a whole number from 1 to 15";
2746
- case "invalid-video-resolution": return "-r / --resolution must be 480p or 720p";
2747
- 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`;
2748
- case "choose-model": return `several ${noun} models are available (${detail}); pick one with the button next to the composer or pass -m <model>`;
2749
- case "model-not-found": return `no enabled ${noun} model matches; available: ${detail || "none"}`;
2750
- case "model-ambiguous": return `several ${noun} models match (${detail}); be more specific`;
2751
- case "workspace-not-local": return `the session's working directory is not a local folder, so the ${noun} cannot be saved`;
2752
- case "unsupported-attachment": return detail === "edit"
2753
- ? "attach one MP4 video as the source (images cannot be used here)"
2754
- : video ? "only image attachments can be used for /cpa-video (first frame, references, first / last frames)" : "only image attachments can be used as references";
2755
- 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`;
2756
- 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";
2757
- case "needs-video-15": return "pinned first / last frames need grok-imagine-video-1.5; pick it with -m or the model button";
2758
- case "invalid-extend-seconds": return "an extension adds 2 to 10 seconds (-s 2-10)";
2759
- case "no-video-source": return "no source video: attach an MP4, or pass --video <path>, --url <public URL> or --from <command id>";
2760
- 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>";
2761
- case "one-video-source": return "use one source video: an attachment, --video, --url or --from";
2762
- case "invalid-video-url": return "--url must be a public http(s) address without a user name or password";
2763
- case "video-not-found": return `no video file at ${detail}`;
2764
- case "video-not-mp4": return "the source video must be an MP4 file (H.264, H.265 or AV1)";
2765
- 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`;
2766
- case "request-too-large": return video
2767
- ? `the request is too large for this server (${detail ? `${Math.round(Number(detail) / 1024)} KB` : "body limit"}); attach smaller or fewer images`
2768
- : "the request is too large for this server";
2769
- 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`;
2770
- 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`;
2771
- case "extend-source-length": return `xAI extends videos of ${EXTEND_SOURCE_SECONDS.min} to ${EXTEND_SOURCE_SECONDS.max} seconds (this one has ${detail} s)`;
2772
- case "reference-too-large": return `an attached image could not be made smaller than ${detail ?? "1000"} KB; attach a smaller image`;
2773
- case "upstream-error": return `CPA rejected the request (${detail})`;
2774
- case "timeout": return video ? "CPA did not answer in time" : "CPA did not answer within 3 minutes";
2775
- case "network": return "could not reach CPA";
2776
- case "no-image": return "CPA returned no image";
2777
- case "invalid-image":
2778
- 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";
2779
- case "image-too-large": return "an image is larger than 32 MiB";
2780
- case "video-too-large": return `the video is larger than ${Math.round(MAX_VIDEO_BYTES / 1024 / 1024)} MiB`;
2781
- case "invalid-video": return "CPA returned data that is not an MP4, MOV or WebM video";
2782
- case "video-failed": return `the video could not be generated${detail ? ` (${detail})` : ""}`;
2783
- case "expired": return "CPA no longer knows this video: more than 3 hours have passed since it was submitted";
2784
- case "download-failed": return `could not download the generated ${noun}`;
2785
- case "write-failed": return `could not write the ${noun} file${detail ? ` (${detail})` : ""}`;
2786
- case "no-endpoint": return "this account has no usable server address yet";
2787
- case "invalid-account": return "the model's account no longer exists";
2788
- case "interrupted": return "stopped because the plugin was reloaded or DSH closed";
2789
- default: return error.code;
2790
- }
2791
- };
2792
- // #region Background generation jobs and the session's "running" state
2793
- //
2794
- // `/cpa-image` returns as soon as the request is admitted, so the composer
2795
- // clears the draft at once; the generation continues here and its chat row
2796
- // follows it through `media/command`. DSH counts only agent turns as
2797
- // running, so while a job runs this plugin publishes the host's own
2798
- // `api-session/status` event for that session (the workspace list shows it
2799
- // as running, then completed). It is republished every few seconds and
2800
- // after the agent itself goes idle, because those reset it to idle.
2801
-
2802
- const mediaJobs = new Map(); // job key → { sessionId, abort }
2803
- // job key → the promise of a job of this activation, until it settles (the agent tool waits on it).
2804
- const jobPromises = new Map();
2805
- const settleWith = (key, promise) => {
2806
- jobPromises.set(key, promise);
2807
- void promise.finally(() => {
2808
- if (jobPromises.get(key) === promise) jobPromises.delete(key);
2809
- });
2810
- return promise;
2811
- };
2812
- let mediaJobSeq = 0;
2813
- let mediaHeartbeat;
2814
- const MEDIA_HEARTBEAT_MS = 5000;
2815
- const jobSessions = () => new Set([...mediaJobs.values()].map((job) => job.sessionId).filter(Boolean));
2816
- const agentRunning = (sessionId) => {
2817
- try {
2818
- return ctx.get?.("agents")?.get?.(sessionId)?.status === "running";
2819
- } catch {
2820
- return false;
2821
- }
2822
- };
2823
- const publishSessionStatus = (sessionId, running) => {
2824
- if (typeof ctx.emit !== "function") return;
2825
- try {
2826
- ctx.emit("api-session/status", sessionId, running);
2827
- } catch (error) {
2828
- ctx.logger?.warn?.(`cpa: could not publish the session status: ${error instanceof Error ? error.message : String(error)}`);
2829
- }
2830
- };
2831
- const settleSessionStatus = (sessionId) => {
2832
- if (sessionId === undefined || jobSessions().has(sessionId) || agentRunning(sessionId)) return;
2833
- publishSessionStatus(sessionId, false);
2834
- };
2835
- const syncHeartbeat = () => {
2836
- if (mediaJobs.size > 0 && mediaHeartbeat === undefined) {
2837
- mediaHeartbeat = setInterval(() => {
2838
- for (const sessionId of jobSessions()) publishSessionStatus(sessionId, true);
2839
- }, MEDIA_HEARTBEAT_MS);
2840
- mediaHeartbeat.unref?.();
2841
- } else if (mediaJobs.size === 0 && mediaHeartbeat !== undefined) {
2842
- clearInterval(mediaHeartbeat);
2843
- mediaHeartbeat = undefined;
2844
- }
2845
- };
2846
- ctx.on?.("agent/status", (event) => {
2847
- const sessionId = event?.agent?.id;
2848
- if (event?.status === "running" || !jobSessions().has(sessionId)) return;
2849
- // After the host's own idle notice for this transition.
2850
- setTimeout(() => {
2851
- if (jobSessions().has(sessionId)) publishSessionStatus(sessionId, true);
2852
- }, 0);
2853
- });
2854
- ctx.effect(
2855
- () => () => {
2856
- const sessions = jobSessions();
2857
- for (const job of mediaJobs.values()) job.abort.abort(new MediaError("interrupted"));
2858
- mediaJobs.clear();
2859
- syncHeartbeat();
2860
- for (const sessionId of sessions) settleSessionStatus(sessionId);
2861
- },
2862
- "cpa: generation jobs"
2863
- );
2864
-
2865
- /** Update one run record (and prepend new recent outputs) in the session store; never throws. */
2866
- const recordRun = (sessionId, commandId, patch, recent = []) => {
2867
- if (sessionId === undefined) return;
2868
- try {
2869
- mediaSessions.update(sessionId, (entry) => {
2870
- if (recent.length > 0) entry.recent = [...recent, ...entry.recent];
2871
- if (commandId === undefined) return;
2872
- const index = entry.runs.findIndex((run) => run.commandId === commandId);
2873
- if (index >= 0) entry.runs[index] = { ...entry.runs[index], ...patch };
2874
- else entry.runs = [{ commandId, ...patch }, ...entry.runs];
2875
- });
2876
- } catch (error) {
2877
- ctx.logger?.warn?.(`cpa: could not record a generation result: ${error instanceof Error ? error.message : String(error)}`);
2878
- }
2879
- };
2880
-
2881
- /** How the chat row names the save folder: relative to the working directory when inside it. */
2882
- const folderLabel = (cwd, dir) => {
2883
- const rel = relative(cwd, dir);
2884
- return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel) ? rel.split(sep).join("/") : basename(dir);
2885
- };
2886
-
2887
- /** Run one admitted image request in the background; resolves when it has settled and been recorded. */
2888
- const startImageJob = ({ sessionId, commandId, option, args: admitted, references, cwd, outputDir, endpoint, interpret }) => {
2889
- const key = commandId ?? `job-${++mediaJobSeq}`;
2890
- const abort = new AbortController();
2891
- mediaJobs.set(key, { sessionId, abort });
2892
- syncHeartbeat();
2893
- if (sessionId !== undefined) publishSessionStatus(sessionId, true);
2894
- const started = Date.now();
2895
- return settleWith(key, (async () => {
2896
- try {
2897
- let args = admitted;
2898
- if (interpret !== undefined) {
2899
- if (commandId !== undefined) videoLive.set(commandId, { phase: "interpreting" });
2900
- const result = await interpret(abort.signal);
2901
- if (commandId !== undefined) videoLive.delete(commandId);
2902
- if (result !== undefined) {
2903
- args = { ...admitted, ...result.values, prompt: result.prompt };
2904
- recordRun(sessionId, commandId, {
2905
- prompt: args.prompt,
2906
- ...(args.aspect === undefined ? {} : { aspect: args.aspect }),
2907
- count: args.count ?? 1,
2908
- ...interpretedRecord(result, admitted.prompt),
2909
- });
2910
- }
2911
- }
2912
- const images = await generateImages({
2913
- rootURL: endpoint.rootURL,
2914
- apiKey: endpoint.apiKey,
2915
- model: option.modelId,
2916
- prompt: args.prompt,
2917
- ...(args.aspect === undefined ? {} : { aspect: args.aspect }),
2918
- count: args.count ?? 1,
2919
- references,
2920
- signal: abort.signal,
2921
- });
2922
- const dir = outputDirectory(cwd, outputDir);
2923
- const paths = writeOutputFiles(dir, outputStem(option.modelId, args.prompt), images);
2924
- const revised = images.find((image) => image.revisedPrompt !== undefined)?.revisedPrompt;
2925
- recordRun(
2926
- sessionId,
2927
- commandId,
2928
- {
2929
- status: "done",
2930
- files: paths.length,
2931
- folder: folderLabel(cwd, dir),
2932
- finishedAt: new Date().toISOString(),
2933
- ...(revised === undefined ? {} : { revisedPrompt: revised }),
2934
- },
2935
- paths.map((p) => recentEntry("image", p, option.ref, new Date(), commandId))
2936
- );
2937
- ctx.logger?.info?.(`cpa: /cpa-image wrote ${paths.length} image(s) with ${option.modelId} in ${Math.round((Date.now() - started) / 1000)} s`);
2938
- } catch (thrown) {
2939
- const error = abort.signal.aborted ? new MediaError("interrupted") : thrown;
2940
- recordRun(sessionId, commandId, {
2941
- status: "error",
2942
- errorCode: error instanceof MediaError ? error.code : "failed",
2943
- error: mediaFailureText(error),
2944
- finishedAt: new Date().toISOString(),
2945
- });
2946
- ctx.logger?.warn?.(`cpa: /cpa-image failed: ${mediaFailureText(error)}`);
2947
- } finally {
2948
- if (commandId !== undefined) videoLive.delete(commandId);
2949
- mediaJobs.delete(key);
2950
- syncHeartbeat();
2951
- settleSessionStatus(sessionId);
2952
- }
2953
- })());
2954
- };
2955
-
2956
- // #endregion
2957
-
2958
- /**
2959
- * `/cpa-image`: generate or (with attached images) edit, save into the
2960
- * session's folder. Checks everything it can up front (options, model,
2961
- * folder, references, endpoint), then returns while the generation runs as
2962
- * a background job; the chat row shows its progress and result.
2963
- */
2964
- // Internal callers (the agent tool) add: `literal` (no plain-language
2965
- // parsing or interpretation), `pickSession` (whose model pick applies) and
2966
- // `gate(plan)` (confirmation / budget, run after every check and before
2967
- // anything is recorded or sent; a thrown `gateError` passes through).
2968
- const runImageCommand = async ({ agent, rawInput, attachments = [], signal, commandId, literal = false, pickSession, gate }) => {
2969
- try {
2970
- const args = parseImageCommand(rawInput, { literal });
2971
- const blocks = Array.isArray(attachments) ? attachments : [];
2972
- if (blocks.some((block) => block?.type !== "image")) throw new MediaError("unsupported-attachment");
2973
- if (blocks.length > MAX_REFERENCE_IMAGES) throw new MediaError("too-many-references");
2974
- const { settings, options, accounts } = await mediaChoicesFor("image");
2975
- const sessionId = validSessionId(agent?.id);
2976
- const pickId = validSessionId(pickSession) ?? sessionId;
2977
- const entry = pickId === undefined ? {} : mediaSessions.get(pickId);
2978
- let option;
2979
- try {
2980
- ({ option } = chooseMediaModel({
2981
- options: options.image,
2982
- requested: args.model,
2983
- sessionRef: entry.image,
2984
- lastRef: settings.lastSelection.image,
2985
- }));
2986
- } catch (error) {
2987
- if (error instanceof MediaError && error.code === "no-media-model") {
2988
- throw new MediaError("no-media-model", mediaAccountsSummary(accounts, "image"));
2989
- }
2990
- throw error;
2991
- }
2992
- const workingDirectory = ctx.get?.("workingDirectory");
2993
- const cwd = agent !== undefined && workingDirectory !== undefined ? await workingDirectory.ensure(agent, signal) : undefined;
2994
- if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new MediaError("workspace-not-local");
2995
- const references = [];
2996
- if (blocks.length > 0) {
2997
- const store = ctx.get?.("attachments");
2998
- if (store === undefined) throw new MediaError("unsupported-attachment");
2999
- const each = Math.floor(REFERENCES_MAX_BYTES / blocks.length);
3000
- for (const block of blocks) references.push(await readReference(store, block, each, signal));
3001
- }
3002
- const endpoint = await controller.mediaEndpoint(option.accountId);
3003
- const id = validCommandId(commandId);
3004
- const count = args.count ?? 1;
3005
- if (gate !== undefined) {
3006
- 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 });
3007
- }
3008
- recordRun(sessionId, id, {
3009
- kind: "image",
3010
- status: "running",
3011
- model: option.ref,
3012
- modelName: option.name || option.modelId,
3013
- accountName: option.accountName,
3014
- prompt: args.prompt,
3015
- ...(args.aspect === undefined ? {} : { aspect: args.aspect }),
3016
- count,
3017
- references: references.length,
3018
- files: 0,
3019
- startedAt: new Date().toISOString(),
3020
- });
3021
- void startImageJob({
3022
- sessionId,
3023
- commandId: id,
3024
- option,
3025
- args,
3026
- references,
3027
- cwd,
3028
- outputDir: settings.outputDir,
3029
- endpoint,
3030
- ...(literal
3031
- ? {}
3032
- : {
3033
- interpret: (jobSignal) =>
3034
- interpretParams({ agent, command: "image", text: args.prompt, images: references.length, fixed: ["aspect", "count"].filter((k) => args[k] !== undefined), signal: jobSignal }),
3035
- }),
3036
- });
3037
- const dir = outputDirectory(cwd, settings.outputDir);
3038
- return {
3039
- kind: "success",
3040
- text: `${references.length > 0 ? "Editing" : "Generating"} ${count} image(s) with ${option.modelId} (${option.accountName}); files go to ${dir}`,
3041
- };
3042
- } catch (error) {
3043
- if (signal?.aborted || error?.gateError === true) throw error;
3044
- return { kind: "error", text: `/cpa-image: ${mediaFailureText(error)}` };
3045
- }
3046
- };
3047
-
3048
- // #region Video jobs (/cpa-video)
3049
- //
3050
- // A video is submitted once and cannot be cancelled (CPA and xAI have no
3051
- // cancel route). The job is stored in media-jobs.json as soon as CPA
3052
- // returns its request id, polled (5 s, then 10 s, then 15 s) and downloaded
3053
- // through CPA into the session's folder. A reload or restart only pauses
3054
- // the polling: the next activation resumes every stored job younger than
3055
- // CPA's 3-hour request binding, and marks older ones expired.
3056
-
3057
- const mediaJobStore = createMediaJobStore(join(updateDirectory, "media-jobs.json"));
3058
- // commandId → { phase: "submitting" | "queued" | "running" | "downloading", progress? } for the chat row.
3059
- const videoLive = new Map();
3060
- // commandId → { url, accountId, accountName, seconds, sessionId, commandId, at }: the short-lived
3061
- // result URL of a finished video, for /cpa-video-edit and /cpa-video-extend.
3062
- // In memory only (never stored, logged or sent to the page); a reload forgets it.
3063
- const videoSources = new Map();
3064
- // xAI calls its result URLs ephemeral without a number; offer them for an hour.
3065
- const VIDEO_SOURCE_TTL_MS = 60 * 60 * 1000;
3066
- const MAX_VIDEO_SOURCES = 100;
3067
- const rememberSource = (job, status) => {
3068
- if (job.commandId === undefined || status.sourceUrl === undefined) return;
3069
- videoSources.delete(job.commandId);
3070
- videoSources.set(job.commandId, {
3071
- url: status.sourceUrl,
3072
- accountId: job.accountId,
3073
- seconds: status.seconds ?? job.seconds,
3074
- sessionId: job.sessionId,
3075
- commandId: job.commandId,
3076
- at: Date.now(),
3077
- });
3078
- while (videoSources.size > MAX_VIDEO_SOURCES) videoSources.delete(videoSources.keys().next().value);
3079
- };
3080
- const usableSource = (commandId) => {
3081
- const source = videoSources.get(commandId);
3082
- return source !== undefined && Date.now() - source.at <= VIDEO_SOURCE_TTL_MS ? source : undefined;
3083
- };
3084
-
3085
- const sleep = (ms, signal) =>
3086
- new Promise((resolve, reject) => {
3087
- if (signal.aborted) return reject(signal.reason);
3088
- const timer = setTimeout(() => {
3089
- signal.removeEventListener("abort", onAbort);
3090
- resolve();
3091
- }, ms);
3092
- timer.unref?.();
3093
- const onAbort = () => {
3094
- clearTimeout(timer);
3095
- reject(signal.reason);
3096
- };
3097
- signal.addEventListener("abort", onAbort, { once: true });
3098
- });
3099
-
3100
- const pollDelay = (elapsedMs) => (elapsedMs < 60_000 ? 5000 : elapsedMs < 180_000 ? 10_000 : 15_000);
3101
- const MAX_POLL_FAILURES = 12;
3102
-
3103
- /** Poll, download and record one submitted video; a dispose abort leaves it stored for the next activation. */
3104
- const followVideoJob = async (job, abort) => {
3105
- const live = (patch) => {
3106
- if (job.commandId !== undefined) videoLive.set(job.commandId, { ...(videoLive.get(job.commandId) ?? {}), ...patch });
3107
- };
3108
- const finish = (state, runPatch, recent = []) => {
3109
- mediaJobStore.put({ ...job, state, finishedAt: new Date().toISOString() });
3110
- recordRun(job.sessionId, job.commandId, { ...runPatch, finishedAt: new Date().toISOString() }, recent);
3111
- };
3112
- const fail = (error) => {
3113
- const code = error instanceof MediaError ? error.code : "failed";
3114
- finish(code === "expired" ? "expired" : "failed", { status: "error", errorCode: code, error: mediaFailureText(error, "video") });
3115
- ctx.logger?.warn?.(`cpa: /cpa-video ${job.id} failed: ${mediaFailureText(error, "video")}`);
3116
- };
3117
- const submitted = Date.parse(job.submittedAt);
3118
- let failures = 0;
3119
- live({ phase: "queued" });
3120
- try {
3121
- for (;;) {
3122
- if (Date.now() - submitted > VIDEO_JOB_TTL_MS) throw new MediaError("expired");
3123
- let endpoint;
3124
- let status;
3125
- try {
3126
- endpoint = await controller.mediaEndpoint(job.accountId);
3127
- status = await retrieveVideo({ rootURL: endpoint.rootURL, apiKey: endpoint.apiKey, requestId: job.id, signal: abort.signal });
3128
- failures = 0;
3129
- } catch (error) {
3130
- if (abort.signal.aborted) throw error;
3131
- // A missing account or a refusal that keeps coming back ends the job;
3132
- // network trouble and short CPA hiccups are retried.
3133
- if (error instanceof MediaError && error.code === "invalid-account") throw error;
3134
- failures += 1;
3135
- if (failures >= MAX_POLL_FAILURES) throw error;
3136
- await sleep(pollDelay(Date.now() - submitted), abort.signal);
3137
- continue;
3138
- }
3139
- if (status.state === "failed") throw new MediaError("video-failed", status.error);
3140
- if (status.state === "done") {
3141
- live({ phase: "downloading", progress: 100 });
3142
- const dir = outputDirectory(job.cwd, job.outputDir);
3143
- const path = await downloadVideo({
3144
- rootURL: endpoint.rootURL,
3145
- apiKey: endpoint.apiKey,
3146
- requestId: job.id,
3147
- dir,
3148
- stem: outputStem(job.modelId, job.prompt),
3149
- signal: abort.signal,
3150
- });
3151
- finish("done", { status: "done", files: 1, folder: folderLabel(job.cwd, dir) }, [recentEntry("video", path, job.ref, new Date(), job.commandId)]);
3152
- rememberSource(job, status);
3153
- ctx.logger?.info?.(`cpa: /cpa-video ${job.id} saved after ${Math.round((Date.now() - submitted) / 1000)} s`);
3154
- return;
3155
- }
3156
- live({ phase: status.state, ...(status.progress === undefined ? {} : { progress: status.progress }) });
3157
- await sleep(pollDelay(Date.now() - submitted), abort.signal);
3158
- }
3159
- } catch (error) {
3160
- if (abort.signal.aborted) return; // paused by a reload; resumed by the next activation
3161
- fail(error);
3162
- }
3163
- };
3164
-
3165
- /** Track one job in the session's running state until it settles. */
3166
- const trackVideoJob = (key, sessionId, run) => {
3167
- const abort = new AbortController();
3168
- mediaJobs.set(key, { sessionId, abort });
3169
- syncHeartbeat();
3170
- if (sessionId !== undefined) publishSessionStatus(sessionId, true);
3171
- return settleWith(key, (async () => {
3172
- try {
3173
- await run(abort);
3174
- } finally {
3175
- if (mediaJobs.get(key)?.abort === abort) mediaJobs.delete(key);
3176
- videoLive.delete(key);
3177
- syncHeartbeat();
3178
- settleSessionStatus(sessionId);
3179
- }
3180
- })());
3181
- };
3182
-
3183
- /**
3184
- * Submit one admitted video through `submit(signal)` (generation, edit or
3185
- * extension), store the job, then follow it.
3186
- */
3187
- const startVideoJob = ({ sessionId, commandId, accountId, modelId, ref, prompt, seconds, cwd, outputDir, submit, label }) => {
3188
- const key = commandId ?? `job-${++mediaJobSeq}`;
3189
- if (commandId !== undefined) videoLive.set(commandId, { phase: "submitting" });
3190
- return trackVideoJob(key, sessionId, async (abort) => {
3191
- let requestId;
3192
- try {
3193
- // `submit` may settle the final prompt / length first (parameter interpretation).
3194
- const submitted = await submit(abort.signal);
3195
- requestId = submitted.requestId;
3196
- if (typeof submitted.prompt === "string") prompt = submitted.prompt;
3197
- if (Number.isInteger(submitted.seconds)) seconds = submitted.seconds;
3198
- } catch (thrown) {
3199
- // Cut off before CPA answered: whether it was submitted is unknown.
3200
- const error = abort.signal.aborted ? new MediaError("interrupted") : thrown;
3201
- recordRun(sessionId, commandId, {
3202
- status: "error",
3203
- errorCode: error instanceof MediaError ? error.code : "failed",
3204
- error: mediaFailureText(error, "video"),
3205
- finishedAt: new Date().toISOString(),
3206
- });
3207
- ctx.logger?.warn?.(`cpa: ${label} was not submitted: ${mediaFailureText(error, "video")}`);
3208
- return;
3209
- }
3210
- const job = mediaJobStore.put({
3211
- id: requestId,
3212
- state: "submitted",
3213
- accountId,
3214
- modelId,
3215
- ref,
3216
- ...(sessionId === undefined ? {} : { sessionId }),
3217
- ...(commandId === undefined ? {} : { commandId }),
3218
- cwd,
3219
- outputDir,
3220
- prompt,
3221
- seconds,
3222
- submittedAt: new Date().toISOString(),
3223
- });
3224
- recordRun(sessionId, commandId, { videoId: requestId });
3225
- ctx.logger?.info?.(`cpa: ${label} submitted ${requestId} (${modelId}, ${seconds} s)`);
3226
- await followVideoJob(job, abort);
3227
- });
3228
- };
3229
- /** Resume every stored job that is still running (after a reload or restart). */
3230
- const resumeVideoJobs = () => {
3231
- let jobs;
3232
- try {
3233
- jobs = mediaJobStore.list().filter((job) => job.state === "submitted");
3234
- } catch (error) {
3235
- ctx.logger?.warn?.(`cpa: could not read media-jobs.json: ${error instanceof Error ? error.message : String(error)}`);
3236
- return;
3237
- }
3238
- for (const job of jobs) {
3239
- const key = job.commandId ?? `video-${job.id}`;
3240
- if (mediaJobs.has(key)) continue;
3241
- void trackVideoJob(key, job.sessionId, (abort) => followVideoJob(job, abort));
3242
- }
3243
- };
3244
-
3245
- /** Grok's 1.5 video family takes more references and pinned first / last frames. */
3246
- const isVideo15 = (modelId) => /(^|\/)grok-imagine-video-1\.5/i.test(String(modelId ?? ""));
3247
-
3248
- /**
3249
- * How attached images are used: none = text only; one = first frame
3250
- * (`--ref`: a reference, `--last`: the last frame); two = references
3251
- * (`--first-last` / `--last`: first and last frame); more = references.
3252
- * @throws MediaError `frames-need-images` / `too-many-references` / `needs-video-15`.
3253
- */
3254
- const videoMode = (count, frames, modelId) => {
3255
- if (count === 0) {
3256
- if (frames !== undefined) throw new MediaError("frames-need-images");
3257
- return "text";
3258
- }
3259
- let mode;
3260
- if (count === 1) mode = frames === "reference" ? "reference" : frames === "last" ? "last-frame" : frames === "first-last" ? undefined : "first-frame";
3261
- else if (count === 2) mode = frames === "first-last" || frames === "last" ? "first-last" : "reference";
3262
- else mode = frames === undefined || frames === "reference" ? "reference" : undefined;
3263
- if (mode === undefined) throw new MediaError(count === 1 ? "frames-need-images" : "too-many-references", count === 1 ? "2" : "2");
3264
- const maxRefs = isVideo15(modelId) ? 14 : 7;
3265
- if (mode === "reference" && count > maxRefs) throw new MediaError("too-many-references", String(maxRefs));
3266
- if ((mode === "first-last" || mode === "last-frame") && !isVideo15(modelId)) throw new MediaError("needs-video-15");
3267
- return mode;
3268
- };
3269
-
3270
- /**
3271
- * `/cpa-video`: submit a video — text only, from a first frame, from
3272
- * reference images, or between pinned first / last frames — and return at
3273
- * once; the chat row follows the background job.
3274
- */
3275
- const runVideoCommand = async ({ agent, rawInput, attachments = [], signal, commandId, literal = false, pickSession, gate }) => {
3276
- try {
3277
- const parsed = parseVideoCommand(rawInput, { literal });
3278
- const blocks = Array.isArray(attachments) ? attachments : [];
3279
- if (blocks.some((block) => block?.type !== "image")) throw new MediaError("unsupported-attachment");
3280
- if (blocks.length > 14) throw new MediaError("too-many-references", "14");
3281
- const { settings, options, accounts } = await mediaChoicesFor("video");
3282
- const sessionId = validSessionId(agent?.id);
3283
- const pickId = validSessionId(pickSession) ?? sessionId;
3284
- const entry = pickId === undefined ? {} : mediaSessions.get(pickId);
3285
- let option;
3286
- try {
3287
- ({ option } = chooseMediaModel({ options: options.video, requested: parsed.model, sessionRef: entry.video, lastRef: settings.lastSelection.video }));
3288
- } catch (error) {
3289
- if (error instanceof MediaError && error.code === "no-media-model") throw new MediaError("no-media-model", mediaAccountsSummary(accounts, "video"));
3290
- throw error;
3291
- }
3292
- const mode = videoMode(blocks.length, parsed.frames, option.modelId);
3293
- const workingDirectory = ctx.get?.("workingDirectory");
3294
- const cwd = agent !== undefined && workingDirectory !== undefined ? await workingDirectory.ensure(agent, signal) : undefined;
3295
- if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new MediaError("workspace-not-local");
3296
- const images = [];
3297
- if (blocks.length > 0) {
3298
- const store = ctx.get?.("attachments");
3299
- if (store === undefined) throw new MediaError("unsupported-attachment");
3300
- // The generation route reads about 1 MiB of body (measured), so all images share that.
3301
- const each = Math.min(FIRST_FRAME_MAX_BYTES, videoImageBudget(parsed.prompt, blocks.length));
3302
- for (const block of blocks) images.push(await readReference(store, block, each, signal));
3303
- }
3304
- const framesFor = (m) =>
3305
- m === "first-frame" ? { image: images[0] }
3306
- : m === "last-frame" ? { lastFrame: images[0] }
3307
- : m === "first-last" ? { image: images[0], lastFrame: images[1] }
3308
- : m === "reference" ? { references: images }
3309
- : {};
3310
- const endpoint = await controller.mediaEndpoint(option.accountId);
3311
- const args = { ...parsed, seconds: parsed.seconds ?? settings.defaultVideoSeconds, resolution: parsed.resolution ?? settings.defaultVideoResolution };
3312
- const id = validCommandId(commandId);
3313
- if (gate !== undefined) {
3314
- 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 });
3315
- }
3316
- recordRun(sessionId, id, {
3317
- kind: "video",
3318
- status: "running",
3319
- mode,
3320
- model: option.ref,
3321
- modelName: option.name || option.modelId,
3322
- accountName: option.accountName,
3323
- prompt: args.prompt,
3324
- ...(args.aspect === undefined ? {} : { aspect: args.aspect }),
3325
- seconds: args.seconds,
3326
- resolution: args.resolution,
3327
- count: 1,
3328
- references: images.length,
3329
- files: 0,
3330
- startedAt: new Date().toISOString(),
3331
- });
3332
- void startVideoJob({
3333
- sessionId,
3334
- commandId: id,
3335
- accountId: option.accountId,
3336
- modelId: option.modelId,
3337
- ref: option.ref,
3338
- prompt: args.prompt,
3339
- seconds: args.seconds,
3340
- cwd,
3341
- outputDir: settings.outputDir,
3342
- label: "/cpa-video",
3343
- submit: async (jobSignal) => {
3344
- // The session's chat model reads what the rules did not (explicit options and recognized words win).
3345
- if (id !== undefined && !literal) videoLive.set(id, { phase: "interpreting" });
3346
- const fixed = ["seconds", "resolution", "aspect", "frames"].filter((k) => parsed[k] !== undefined);
3347
- const result = literal ? undefined : await interpretParams({ agent, command: "video", text: parsed.prompt, images: images.length, fixed, signal: jobSignal });
3348
- let final = args;
3349
- let finalMode = mode;
3350
- if (result !== undefined) {
3351
- final = { ...args, ...result.values, prompt: result.prompt };
3352
- if (result.values.frames !== undefined) finalMode = videoMode(images.length, result.values.frames, option.modelId);
3353
- recordRun(sessionId, id, {
3354
- mode: finalMode,
3355
- prompt: final.prompt,
3356
- ...(final.aspect === undefined ? {} : { aspect: final.aspect }),
3357
- seconds: final.seconds,
3358
- resolution: final.resolution,
3359
- ...interpretedRecord(result, parsed.prompt),
3360
- });
3361
- }
3362
- if (id !== undefined) videoLive.set(id, { phase: "submitting" });
3363
- const submitted = await submitVideo({
3364
- rootURL: endpoint.rootURL,
3365
- apiKey: endpoint.apiKey,
3366
- model: option.modelId,
3367
- prompt: final.prompt,
3368
- seconds: final.seconds,
3369
- ...(final.aspect === undefined ? {} : { aspect: final.aspect }),
3370
- resolution: final.resolution,
3371
- ...framesFor(finalMode),
3372
- signal: jobSignal,
3373
- });
3374
- return { ...submitted, prompt: final.prompt, seconds: final.seconds };
3375
- },
3376
- });
3377
- return {
3378
- kind: "success",
3379
- 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)}`,
3380
- };
3381
- } catch (error) {
3382
- if (signal?.aborted || error?.gateError === true) throw error;
3383
- return { kind: "error", text: `/cpa-video: ${mediaFailureText(error, "video")}` };
3384
- }
3385
- };
3386
-
3387
- // `${accountId}/${route}` the upstream answered 405 for in this activation:
3388
- // asked again only after a reload (CPA pauses the credential after a 405).
3389
- const unsupportedRoutes = new Set();
3390
- const editRoute = (mode) => (mode === "extend" ? "extensions" : "edits");
3391
-
3392
- /** The newest existing video file the session recorded (for one command, or any). */
3393
- const sessionVideoFile = (sessionId, commandId) => {
3394
- if (sessionId === undefined) return undefined;
3395
- return mediaSessions.get(sessionId).recent.find((r) => r.kind === "video" && (commandId === undefined || r.commandId === commandId) && existsSync(r.file));
3396
- };
3397
- /** The account a recorded run used (`accountId/modelId`), if it still exists. */
3398
- const runAccountId = (sessionId, commandId, accounts) => {
3399
- if (sessionId === undefined || commandId === undefined) return undefined;
3400
- const run = mediaSessions.get(sessionId).runs.find((r) => r.commandId === commandId);
3401
- const id = typeof run?.model === "string" ? run.model.split("/", 1)[0] : undefined;
3402
- return accounts.some((a) => a.id === id) ? id : undefined;
3403
- };
3404
- const checkVideoBytes = (data) => {
3405
- if (!isMp4(data)) throw new MediaError("video-not-mp4");
3406
- return mp4Duration(data);
3407
- };
3408
- const tooLarge = (bytes) => new MediaError("source-too-large", (bytes / 1024 / 1024).toFixed(1));
3409
- /** A local MP4 read whole (at most {@link VIDEO_UPLOAD_MAX_BYTES}) for an inline upload. */
3410
- const readLocalVideo = (path, name = basename(path)) => {
3411
- let info;
3412
- try {
3413
- info = statSync(path);
3414
- } catch {
3415
- info = undefined;
3416
- }
3417
- if (info === undefined || !info.isFile()) throw new MediaError("video-not-found", name);
3418
- if (info.size > VIDEO_UPLOAD_MAX_BYTES) throw tooLarge(info.size);
3419
- const data = new Uint8Array(readFileSync(path));
3420
- return { data, seconds: checkVideoBytes(data), name };
3421
- };
3422
- /** An MP4 attached to the command (a DSH file attachment `{ attachmentId, name, bytes }`). */
3423
- const readAttachedVideo = async (store, ref, signal) => {
3424
- const name = basename(String(ref?.name ?? "video.mp4"));
3425
- if (Number(ref?.bytes) > VIDEO_UPLOAD_MAX_BYTES) throw tooLarge(Number(ref.bytes));
3426
- let hostPath;
3427
- try {
3428
- hostPath = store.fileHostPath?.(ref);
3429
- } catch {
3430
- hostPath = undefined;
3431
- }
3432
- if (typeof hostPath === "string" && existsSync(hostPath)) return readLocalVideo(hostPath, name);
3433
- if (typeof store.readFileStream !== "function") throw new MediaError("unsupported-attachment", "edit");
3434
- const chunks = [];
3435
- let size = 0;
3436
- for await (const chunk of store.readFileStream(ref, signal)) {
3437
- size += chunk.byteLength;
3438
- if (size > VIDEO_UPLOAD_MAX_BYTES) throw tooLarge(size);
3439
- chunks.push(chunk);
3440
- }
3441
- const data = new Uint8Array(Buffer.concat(chunks.map((c) => Buffer.from(c.buffer, c.byteOffset, c.byteLength))));
3442
- return { data, seconds: checkVideoBytes(data), name };
3443
- };
3444
-
3445
- /**
3446
- * `/cpa-video-edit` and `/cpa-video-extend`: change or continue any MP4.
3447
- * Sources: an attached MP4, `--video <path>`, `--url <public URL>`,
3448
- * `--from <command id>`, or by default the session's newest video. A video
3449
- * this plugin made goes by xAI's own short-lived link while it is
3450
- * remembered (about an hour, this activation), otherwise its file is sent
3451
- * inline; local files go inline as a base64 data URI (CPA has no file
3452
- * upload route; the edit routes read bodies of 20 MiB, measured), a URL is
3453
- * downloaded by xAI itself.
3454
- */
3455
- const runVideoEditCommand = (mode) => async ({ agent, rawInput, attachments = [], signal, commandId, literal = false, pickSession, gate }) => {
3456
- const name = mode === "extend" ? "/cpa-video-extend" : "/cpa-video-edit";
3457
- try {
3458
- const parsed = parseVideoEditCommand(rawInput, mode, { literal });
3459
- const blocks = Array.isArray(attachments) ? attachments : [];
3460
- if (blocks.some((block) => block?.type !== "file")) throw new MediaError("unsupported-attachment", "edit");
3461
- const named = [parsed.from, parsed.video, parsed.url].filter((v) => v !== undefined).length;
3462
- if (blocks.length > 1 || (blocks.length === 1 && named > 0)) throw new MediaError("one-video-source");
3463
- const sessionId = validSessionId(agent?.id);
3464
- const workingDirectory = ctx.get?.("workingDirectory");
3465
- const cwd = agent !== undefined && workingDirectory !== undefined ? await workingDirectory.ensure(agent, signal) : undefined;
3466
- if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new MediaError("workspace-not-local");
3467
- const { settings, options, accounts } = await mediaChoicesFor("video");
3468
-
3469
- // source: { url? | data?, seconds?, accountId?, commandId?, name? }
3470
- let source;
3471
- const linkOf = (id) => {
3472
- const link = usableSource(id);
3473
- return link !== undefined && link.sessionId === sessionId ? link : undefined;
3474
- };
3475
- const fromLink = (link) => ({ url: link.url, seconds: link.seconds, accountId: link.accountId, commandId: link.commandId });
3476
- const fromFile = (record) => ({ ...readLocalVideo(record.file), commandId: record.commandId, accountId: runAccountId(sessionId, record.commandId, accounts) });
3477
- if (blocks.length === 1) {
3478
- const store = ctx.get?.("attachments");
3479
- if (store === undefined) throw new MediaError("unsupported-attachment", "edit");
3480
- source = await readAttachedVideo(store, blocks[0].attachment, signal);
3481
- } else if (parsed.video !== undefined) {
3482
- source = readLocalVideo(isAbsolute(parsed.video) ? parsed.video : join(cwd, parsed.video));
3483
- } else if (parsed.url !== undefined) {
3484
- source = { url: parsed.url, name: new URL(parsed.url).hostname };
3485
- } else if (parsed.from !== undefined) {
3486
- const link = linkOf(parsed.from);
3487
- const record = link === undefined ? sessionVideoFile(sessionId, parsed.from) : undefined;
3488
- if (link !== undefined) source = fromLink(link);
3489
- else if (record !== undefined) source = fromFile(record);
3490
- else throw new MediaError(videoSources.has(parsed.from) ? "video-source-expired" : "no-video-source");
3491
- } else {
3492
- const record = sessionVideoFile(sessionId);
3493
- const link = record !== undefined ? linkOf(record.commandId) : [...videoSources.values()].reverse().map((s) => linkOf(s.commandId)).find(Boolean);
3494
- if (link !== undefined) source = fromLink(link);
3495
- else if (record !== undefined) source = fromFile(record);
3496
- else throw new MediaError("no-video-source");
3497
- }
3498
- if (source.seconds !== undefined) {
3499
- if (mode === "edit" && source.seconds > EDIT_SOURCE_MAX_SECONDS) throw new MediaError("edit-source-too-long", String(source.seconds));
3500
- if (mode === "extend" && (source.seconds < EXTEND_SOURCE_SECONDS.min || source.seconds > EXTEND_SOURCE_SECONDS.max)) {
3501
- throw new MediaError("extend-source-length", String(source.seconds));
3502
- }
3503
- }
3504
-
3505
- // The account: the source video's own, unless -m names a model; else the session's video model.
3506
- let accountId = parsed.model === undefined ? source.accountId : undefined;
3507
- if (accountId === undefined) {
3508
- const pickId = validSessionId(pickSession) ?? sessionId;
3509
- const entry = pickId === undefined ? {} : mediaSessions.get(pickId);
3510
- try {
3511
- ({ option: { accountId } } = chooseMediaModel({ options: options.video, requested: parsed.model, sessionRef: entry.video, lastRef: settings.lastSelection.video }));
3512
- } catch (error) {
3513
- if (error instanceof MediaError && error.code === "no-media-model") throw new MediaError("no-media-model", mediaAccountsSummary(accounts, "video"));
3514
- throw error;
3515
- }
3516
- }
3517
- const account = accounts.find((a) => a.id === accountId);
3518
- if (account === undefined) throw new MediaError("invalid-account");
3519
- const route = editRoute(mode);
3520
- if (unsupportedRoutes.has(`${accountId}/${route}`)) throw new MediaError("route-unsupported", route);
3521
- const endpoint = await controller.mediaEndpoint(accountId);
3522
- const known = source.seconds === undefined ? undefined : Math.max(1, Math.min(15, Math.round(source.seconds)));
3523
- const seconds = mode === "extend" ? parsed.seconds ?? 6 : known;
3524
- const id = validCommandId(commandId);
3525
- const accountName = account.displayName || account.id;
3526
- if (gate !== undefined) {
3527
- await gate({
3528
- kind: "video",
3529
- mode,
3530
- option: { ref: `${accountId}/${VIDEO_EDIT_MODEL}`, modelId: VIDEO_EDIT_MODEL, accountId, accountName },
3531
- prompt: parsed.prompt,
3532
- // Billed as the new footage: an edit re-renders the source, an extension adds `seconds`.
3533
- seconds: seconds ?? 6,
3534
- sourceName: source.name ?? (source.commandId !== undefined ? source.commandId : undefined),
3535
- references: 0,
3536
- cwd,
3537
- outputDir: settings.outputDir,
3538
- });
3539
- }
3540
- recordRun(sessionId, id, {
3541
- kind: "video",
3542
- status: "running",
3543
- mode,
3544
- ...(source.commandId === undefined ? {} : { sourceCommandId: source.commandId }),
3545
- ...(source.name === undefined ? {} : { sourceName: source.name }),
3546
- sourceKind: source.data !== undefined ? "upload" : source.commandId !== undefined ? "link" : "url",
3547
- ...(Array.isArray(parsed.ignored) ? { ignored: parsed.ignored } : {}),
3548
- model: `${accountId}/${VIDEO_EDIT_MODEL}`,
3549
- modelName: VIDEO_EDIT_MODEL,
3550
- accountName,
3551
- prompt: parsed.prompt,
3552
- ...(seconds === undefined ? {} : { seconds }),
3553
- count: 1,
3554
- references: 0,
3555
- files: 0,
3556
- startedAt: new Date().toISOString(),
3557
- });
3558
- const { url: sourceUrl, data: sourceData } = source;
3559
- void startVideoJob({
3560
- sessionId,
3561
- commandId: id,
3562
- accountId,
3563
- modelId: VIDEO_EDIT_MODEL,
3564
- ref: `${accountId}/${VIDEO_EDIT_MODEL}`,
3565
- prompt: parsed.prompt,
3566
- // An extension returns the source plus the new part.
3567
- seconds: mode === "extend" ? Math.min(15, (known ?? 0) + seconds) : seconds ?? 6,
3568
- cwd,
3569
- outputDir: settings.outputDir,
3570
- label: name,
3571
- submit: async (jobSignal) => {
3572
- // The session's chat model reads an extension length or other wording the rules missed.
3573
- if (id !== undefined && !literal) videoLive.set(id, { phase: "interpreting" });
3574
- const fixed = mode === "extend" && parsed.seconds !== undefined ? ["seconds"] : [];
3575
- const result = literal ? undefined : await interpretParams({ agent, command: mode, text: parsed.prompt, fixed, signal: jobSignal });
3576
- let prompt = parsed.prompt;
3577
- let added = seconds;
3578
- if (result !== undefined) {
3579
- prompt = result.prompt;
3580
- if (mode === "extend" && result.values.seconds !== undefined) added = result.values.seconds;
3581
- recordRun(sessionId, id, { prompt, ...(added === undefined ? {} : { seconds: added }), ...interpretedRecord(result, parsed.prompt) });
3582
- }
3583
- if (id !== undefined) videoLive.set(id, { phase: "submitting" });
3584
- try {
3585
- const submitted = await submitVideoEdit({
3586
- rootURL: endpoint.rootURL,
3587
- apiKey: endpoint.apiKey,
3588
- mode,
3589
- ...(sourceData !== undefined ? { sourceData } : { sourceUrl }),
3590
- prompt,
3591
- ...(mode === "extend" ? { seconds: added } : {}),
3592
- signal: jobSignal,
3593
- });
3594
- return { ...submitted, prompt, ...(mode === "extend" ? { seconds: Math.min(15, (known ?? 0) + added) } : {}) };
3595
- } catch (error) {
3596
- if (error instanceof MediaError && error.code === "route-unsupported") unsupportedRoutes.add(`${accountId}/${route}`);
3597
- throw error;
3598
- }
3599
- },
3600
- });
3601
- const skipped = Array.isArray(parsed.ignored) && parsed.ignored.length > 0
3602
- ? ` 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"}.`
3603
- : "";
3604
- const how = sourceData !== undefined ? `uploading ${source.name} (${(sourceData.byteLength / 1024 / 1024).toFixed(1)} MiB)` : source.name !== undefined ? `from ${source.name}` : "from xAI's link";
3605
- return {
3606
- kind: "success",
3607
- 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}`,
3608
- };
3609
- } catch (error) {
3610
- if (signal?.aborted || error?.gateError === true) throw error;
3611
- return { kind: "error", text: `${name}: ${mediaFailureText(error, "video")}` };
3612
- }
3613
- };
3614
- /** `/cpa-video-status`: this session's video jobs, newest first (text). */
3615
- const runVideoStatusCommand = async ({ agent }) => {
3616
- const sessionId = validSessionId(agent?.id);
3617
- const jobs = mediaJobStore.list().filter((job) => job.sessionId !== undefined && job.sessionId === sessionId).slice(0, 20);
3618
- if (jobs.length === 0) return { kind: "success", text: "No video jobs in this session." };
3619
- const lines = jobs.map((job) => {
3620
- const live = job.commandId === undefined ? undefined : videoLive.get(job.commandId);
3621
- const state = job.state === "submitted" ? (live?.phase ?? "waiting") + (live?.progress === undefined ? "" : ` ${live.progress}%`) : job.state;
3622
- const prompt = job.prompt.length > 40 ? `${job.prompt.slice(0, 40)}…` : job.prompt;
3623
- return `${job.submittedAt.slice(0, 19).replace("T", " ")} ${state.padEnd(14)} ${job.modelId}, ${job.seconds} s ${prompt} (${job.id})`;
3624
- });
3625
- return { kind: "success", text: lines.join("\n") };
3626
- };
3627
-
3628
- // #endregion
3629
-
3630
- // #region Agent tool generate_media (off by default; design §5, §7, §8)
3631
- //
3632
- // One optional tool, registered only while the Generation tab's switch is
3633
- // on and some enabled account offers a generation model. A call runs the
3634
- // same admission as the slash commands (literally: no plain-language
3635
- // parsing or interpretation), with a gate between admission and the
3636
- // request that asks for approval and enforces the video seconds budget of
3637
- // the root session, then waits up to 10 minutes for the background job.
3638
- const TOOL_WAIT_MS = 10 * 60_000;
3639
- const TOOL_IMAGE_MAX_BYTES = 32 * 1024 * 1024;
3640
- const TOOL_PREVIEWS = 4;
3641
- let toolsCtx;
3642
- let toolOff;
3643
- // The generation slash commands (registered only while generation is on).
3644
- let commandsCtx;
3645
- let mediaCommandsOff;
3646
-
3647
- const gateError = (message) => Object.assign(new Error(message), { gateError: true });
3648
-
3649
- /** The top-level agent a (sub-)agent was created under: its session holds picks and the budget. */
3650
- const rootAgentOf = (agent) => {
3651
- const agents = ctx.get?.("agents");
3652
- let current = agent;
3653
- if (typeof agents?.list !== "function" || typeof agents.isOwnedBy !== "function") return current;
3654
- for (let depth = 0; depth < 16; depth += 1) {
3655
- const owner = agents.list().find((candidate) => candidate !== current && agents.isOwnedBy(current.id, candidate));
3656
- if (owner === undefined) break;
3657
- current = owner;
3658
- }
3659
- return current;
3660
- };
3661
-
3662
- // Legacy fallback only when the native userQuestions service is absent. Cost
3663
- // confirmation is independent of DSH's execution-approval permission policy.
3664
- // runId → { sessionId, kind, en, zh, expiresAt, settle(outcome) }, in memory only.
3665
- const pendingConfirms = new Map();
3666
- const pendingNativeConfirms = new Set();
3667
- const CONFIRM_WAIT_MS = 10 * 60_000;
3668
- const CONFIRM_ALLOW = "Allow / 允许";
3669
- const CONFIRM_DECLINE = "Decline / 拒绝";
3670
- const cancelConfirms = () => {
3671
- for (const cancel of [...pendingNativeConfirms]) cancel();
3672
- for (const waiting of [...pendingConfirms.values()]) waiting.settle("cancelled");
3673
- };
3674
-
3675
- /** Compatibility composer/row consent without userQuestions (`allowed` / `rejected` / `timeout` / `cancelled`). */
3676
- const askInRow = (exec, runId, sessionId, text, kind) =>
3677
- new Promise((resolve) => {
3678
- if (exec.signal?.aborted || toolOff === undefined || toolsCtx === undefined) return resolve("cancelled");
3679
- const timer = setTimeout(() => settle("timeout"), CONFIRM_WAIT_MS);
3680
- timer.unref?.();
3681
- const onAbort = () => settle("cancelled");
3682
- const settle = (outcome) => {
3683
- if (pendingConfirms.get(runId)?.settle !== settle) return;
3684
- pendingConfirms.delete(runId);
3685
- clearTimeout(timer);
3686
- exec.signal?.removeEventListener("abort", onAbort);
3687
- resolve(outcome);
3688
- };
3689
- exec.signal?.addEventListener("abort", onAbort, { once: true });
3690
- pendingConfirms.set(runId, { sessionId, kind, en: text.en, zh: text.zh, expiresAt: new Date(Date.now() + CONFIRM_WAIT_MS).toISOString(), settle });
3691
- });
3692
-
3693
- /** Anonymous questions block in the native composer, not in a folded tool call. */
3694
- const askNativeConfirm = (questions, exec, text, kind, runId) =>
3695
- new Promise((resolve) => {
3696
- if (exec.signal?.aborted || toolOff === undefined || toolsCtx === undefined) return resolve("cancelled");
3697
- const controller = new AbortController();
3698
- const questionId = `media-confirm-${runId}`;
3699
- let settled = false;
3700
- const onAbort = () => finish("cancelled");
3701
- const cancel = () => finish("cancelled");
3702
- const timer = setTimeout(() => finish("timeout"), CONFIRM_WAIT_MS);
3703
- timer.unref?.();
3704
- const finish = (outcome) => {
3705
- if (settled) return;
3706
- settled = true;
3707
- clearTimeout(timer);
3708
- exec.signal?.removeEventListener("abort", onAbort);
3709
- pendingNativeConfirms.delete(cancel);
3710
- // Remove the composer's pending request on every settlement, even if
3711
- // an answerer ignores cancellation or rejects after the local deadline.
3712
- controller.abort();
3713
- resolve(outcome);
3714
- };
3715
- pendingNativeConfirms.add(cancel);
3716
- exec.signal?.addEventListener("abort", onAbort, { once: true });
3717
- Promise.resolve().then(() => {
3718
- if (settled) return;
3719
- return questions.ask({
3720
- agent: rootAgentOf(exec.agent),
3721
- signal: controller.signal,
3722
- questions: [{
3723
- id: questionId,
3724
- header: kind === "video" ? "Video confirmation / 视频确认" : "Image confirmation / 图像确认",
3725
- question: `${text.zh}\n\n${text.en}`,
3726
- options: [{ label: CONFIRM_ALLOW }, { label: CONFIRM_DECLINE }],
3727
- multiSelect: false,
3728
- }],
3729
- // Do not supply wait/callId: anonymous requests publish the native
3730
- // blocking PendingQuestion instead of a continued tool-call card.
3731
- });
3732
- }).then((answer) => {
3733
- const item = Array.isArray(answer?.answers) && answer.answers.length === 1 ? answer.answers[0] : undefined;
3734
- const allowed = item?.id === questionId && item.custom === undefined &&
3735
- Array.isArray(item.selected) && item.selected.length === 1 && item.selected[0] === CONFIRM_ALLOW;
3736
- finish(allowed ? "allowed" : "rejected");
3737
- }, () => finish("unavailable"));
3738
- });
3739
-
3740
- /** Cost consent is not an execution approval and never falls back after a native error. */
3741
- const askApproval = async (exec, text, kind, runId, sessionId) => {
3742
- const questions = ctx.get?.("userQuestions");
3743
- const what = kind === "video" ? "this video" : "these images";
3744
- let answer;
3745
- if (questions === undefined || questions === null) {
3746
- const ownerId = validSessionId(rootAgentOf(exec.agent)?.id) ?? sessionId;
3747
- if (ownerId === undefined) throw gateError(`${what} needs the user's confirmation, but this call has no session to ask in`);
3748
- answer = await askInRow(exec, runId, ownerId, text, kind);
3749
- } else {
3750
- answer = await askNativeConfirm(questions, exec, text, kind, runId);
3751
- }
3752
- if (answer === "allowed") return;
3753
- if (answer === "rejected") throw gateError(`the user declined ${what}; do not retry unless the user asks`);
3754
- if (answer === "timeout") throw gateError(`the user did not answer the confirmation for ${what} within 10 minutes; ask the user before trying again`);
3755
- if (answer === "cancelled") throw gateError(`the confirmation for ${what} was cancelled`);
3756
- throw gateError(`the confirmation for ${what} could not be shown; ask the user before trying again`);
3757
- };
3758
-
3759
- /** Attached-image blocks for local image paths (saved in DSH's attachment store, like composer images). */
3760
- const toolImageBlocks = async (paths, cwd, signal) => {
3761
- if (paths === undefined) return [];
3762
- const store = ctx.get?.("attachments");
3763
- if (typeof store?.saveImage !== "function") throw new Error("images cannot be read: DSH's attachment store is not available");
3764
- const blocks = [];
3765
- for (const raw of paths) {
3766
- const path = isAbsolute(raw) ? raw : join(cwd, raw);
3767
- let info;
3768
- try {
3769
- info = statSync(path);
3770
- } catch {
3771
- info = undefined;
3772
- }
3773
- if (info === undefined || !info.isFile()) throw new Error(`image not found: ${raw}`);
3774
- if (info.size > TOOL_IMAGE_MAX_BYTES) throw new Error(`image is larger than 32 MiB: ${raw}`);
3775
- const data = new Uint8Array(readFileSync(path));
3776
- const mediaType = sniffImageType(data);
3777
- if (mediaType === undefined) throw new Error(`not a PNG, JPEG, WebP or GIF image: ${raw}`);
3778
- signal?.throwIfAborted();
3779
- const ref = await store.saveImage({ data, mediaType, name: basename(path) });
3780
- blocks.push({ type: "image", attachment: ref });
3781
- }
3782
- return blocks;
3783
- };
3784
-
3785
- /** Whether the calling route declares image input (as DSH's own read_image checks). */
3786
- const routeSeesImages = async (agent, signal) => {
3787
- const target = sessionChatTarget(agent);
3788
- const llm = ctx.get?.("llm");
3789
- if (target === undefined || typeof llm?.resolveModelInfo !== "function") return false;
3790
- try {
3791
- const info = await llm.resolveModelInfo(target.provider, target.model, signal);
3792
- return Array.isArray(info?.inputModalities) && info.inputModalities.includes("image");
3793
- } catch {
3794
- return false;
3795
- }
3796
- };
3797
-
3798
- /** Smaller copies of generated images for the model (DSH's attachment store normalizes them). */
3799
- const imagePreviews = async (paths, agent, signal) => {
3800
- const store = ctx.get?.("attachments");
3801
- if (paths.length === 0 || typeof store?.saveImage !== "function" || !(await routeSeesImages(agent, signal))) return [];
3802
- const previews = [];
3803
- for (const path of paths.slice(0, TOOL_PREVIEWS)) {
3804
- try {
3805
- const data = new Uint8Array(readFileSync(path));
3806
- const mediaType = sniffImageType(data);
3807
- if (mediaType === undefined) continue;
3808
- const ref = await store.saveImage({ data, mediaType, name: basename(path) });
3809
- previews.push({
3810
- attachmentId: String(ref.attachmentId),
3811
- mediaType: ref.mediaType,
3812
- bytes: ref.bytes,
3813
- width: ref.width,
3814
- height: ref.height,
3815
- ...(ref.name === undefined ? {} : { name: ref.name }),
3816
- ...(ref.originalDimensions === undefined ? {} : { originalDimensions: { width: ref.originalDimensions.width, height: ref.originalDimensions.height } }),
3817
- });
3818
- } catch (error) {
3819
- if (signal?.aborted) throw error;
3820
- ctx.logger?.warn?.(`cpa: could not attach a preview of ${basename(path)}: ${error instanceof Error ? error.message : String(error)}`);
3821
- }
3822
- }
3823
- return previews;
3824
- };
3825
-
3826
- /** Wait for a job of this activation: "settled", "timeout" or "aborted" (the job keeps running). */
3827
- const waitForJob = (key, signal, ms) => {
3828
- const promise = jobPromises.get(key);
3829
- if (promise === undefined) return Promise.resolve("settled");
3830
- return new Promise((resolve) => {
3831
- const timer = setTimeout(() => finish("timeout"), ms);
3832
- timer.unref?.();
3833
- const onAbort = () => finish("aborted");
3834
- const finish = (how) => {
3835
- clearTimeout(timer);
3836
- signal?.removeEventListener("abort", onAbort);
3837
- resolve(how);
3838
- };
3839
- if (signal?.aborted) return finish("aborted");
3840
- signal?.addEventListener("abort", onAbort, { once: true });
3841
- promise.then(() => finish("settled"), () => finish("settled"));
3842
- });
3843
- };
3844
-
3845
- // Not submitted (CPA refused it at once): the seconds go back to the budget.
3846
- const NOT_SUBMITTED = new Set(["upstream-error", "request-too-large", "route-unsupported", "no-endpoint", "invalid-account"]);
3847
-
3848
- const executeGenerateMedia = async (args, exec) => {
3849
- const t = parseToolArgs(args);
3850
- if (exec.agent === undefined) throw new Error("generate_media needs an agent session");
3851
- const agent = exec.agent;
3852
- const sessionId = validSessionId(agent.id);
3853
- const root = rootAgentOf(agent);
3854
- const rootId = validSessionId(root?.id) ?? sessionId;
3855
- const runId = toolRunId(exec.callId) ?? `tool-${randomUUID()}`;
3856
- const workingDirectory = ctx.get?.("workingDirectory");
3857
- const cwd = workingDirectory === undefined ? undefined : await workingDirectory.ensure(agent, exec.signal);
3858
- if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new Error(mediaFailureText(new MediaError("workspace-not-local"), t.kind));
3859
- const blocks = await toolImageBlocks(t.images, cwd, exec.signal);
3860
- const line = toolCommandLine(t);
3861
- const handler = t.kind === "image" ? runImageCommand : t.mode === "generate" ? runVideoCommand : runVideoEditCommand(t.mode);
3862
- // The client groups native tool events at turn level; never impersonate
3863
- // a user slash command. Retain the deprecated output flag for old clients.
3864
- const commandRow = false;
3865
- let charged = 0;
3866
- let planned;
3867
- const gate = async (plan) => {
3868
- planned = plan;
3869
- const { settings } = mediaChoices();
3870
- const policy = agentPolicy(settings, rootId === undefined ? {} : mediaSessions.get(rootId));
3871
- if (plan.kind === "video") {
3872
- if (policy.videoConfirm === "auto" && policy.limit !== undefined && policy.used + plan.seconds > policy.limit) {
3873
- 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`);
3874
- }
3875
- if (policy.videoConfirm === "ask") await askApproval(exec, approvalText(plan, policy), "video", runId, sessionId);
3876
- if (rootId !== undefined) {
3877
- mediaSessions.update(rootId, (entry) => chargeSeconds(entry, agentPolicy(settings, entry), plan.seconds));
3878
- charged = plan.seconds;
3879
- }
3880
- } else if (policy.imageConfirm === "ask") {
3881
- await askApproval(exec, approvalText(plan, policy), "image", runId, sessionId);
3882
- }
3883
- };
3884
- const refund = () => {
3885
- if (charged === 0 || rootId === undefined) return;
3886
- const { settings } = mediaChoices();
3887
- mediaSessions.update(rootId, (entry) => chargeSeconds(entry, agentPolicy(settings, entry), -charged));
3888
- charged = 0;
3889
- };
3890
- const outcome = await handler({ agent, rawInput: line, attachments: blocks, signal: exec.signal, commandId: runId, literal: true, pickSession: rootId, gate });
3891
- if (outcome?.kind !== "success") {
3892
- refund();
3893
- throw new Error(String(outcome?.text ?? "the request was refused").replace(/^\/cpa-[a-z-]+:\s*/, ""));
3894
- }
3895
- const waited = await waitForJob(runId, exec.signal, TOOL_WAIT_MS);
3896
- const entry = sessionId === undefined ? { runs: [], recent: [] } : mediaSessions.get(sessionId);
3897
- const run = entry.runs.find((r) => r.commandId === runId);
3898
- const files = entry.recent.filter((r) => r.commandId === runId && existsSync(r.file)).map((r) => r.file);
3899
- const option = planned?.option;
3900
- const base = {
3901
- kind: t.kind,
3902
- ...(t.kind === "video" ? { mode: t.mode } : {}),
3903
- model: option?.modelId ?? run?.modelName ?? "",
3904
- ...(option?.accountName ? { account: option.accountName } : {}),
3905
- run: runId,
3906
- commandRow,
3907
- files,
3908
- folder: outputDirectory(cwd, planned?.outputDir ?? mediaChoices().settings.outputDir),
3909
- ...(Number.isInteger(run?.seconds) ? { seconds: run.seconds } : {}),
3910
- ...(typeof run?.resolution === "string" ? { resolution: run.resolution } : {}),
3911
- ...(typeof run?.aspect === "string" ? { aspect: run.aspect } : {}),
3912
- };
3913
- if (waited !== "settled" || run?.status === "running") {
3914
- return {
3915
- status: "running",
3916
- ...base,
3917
- note:
3918
- waited === "aborted"
3919
- ? "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."
3920
- : "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.",
3921
- };
3922
- }
3923
- if (run === undefined || run.status === "error") {
3924
- if (run === undefined || (run.videoId === undefined && NOT_SUBMITTED.has(run.errorCode))) refund();
3925
- throw new Error(run?.error ?? "the generation failed");
3926
- }
3927
- const previews = t.kind === "image" ? await imagePreviews(files, agent, exec.signal) : [];
3928
- return {
3929
- status: "done",
3930
- ...base,
3931
- ...(typeof run.revisedPrompt === "string" ? { revisedPrompt: run.revisedPrompt } : {}),
3932
- ...(previews.length > 0 ? { previews } : {}),
3933
- };
3934
- };
3935
-
3936
- /** The model-facing content: a short summary, then any image previews. */
3937
- const renderGenerateMedia = (_args, value) => {
3938
- const lines = [
3939
- `${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}` : ""}`,
3940
- ...value.files.map((file) => file),
3941
- ...(value.files.length === 0 ? [`Folder: ${value.folder}`] : []),
3942
- ...(value.revisedPrompt ? [`Revised prompt: ${value.revisedPrompt}`] : []),
3943
- ...(value.note ? [value.note] : []),
3944
- ];
3945
- return [
3946
- { type: "text", text: lines.join("\n") },
3947
- ...(value.previews ?? []).map((p) => ({
3948
- type: "image",
3949
- attachment: {
3950
- attachmentId: p.attachmentId,
3951
- mediaType: p.mediaType,
3952
- bytes: p.bytes,
3953
- width: p.width,
3954
- height: p.height,
3955
- ...(p.name === undefined ? {} : { name: p.name }),
3956
- ...(p.originalDimensions === undefined ? {} : { originalDimensions: { ...p.originalDimensions } }),
3957
- },
3958
- })),
3959
- ];
3960
- };
3961
-
3962
- const generateMediaTool = {
3963
- name: GENERATE_MEDIA,
3964
- description: GENERATE_MEDIA_TOOL.description,
3965
- parameters: GENERATE_MEDIA_TOOL.parameters,
3966
- output: {
3967
- schema: GENERATE_MEDIA_OUTPUT,
3968
- render: renderGenerateMedia,
3969
- presentationMeta: (_args, value) => ({ run: value.run, status: value.status, commandRow: value.commandRow === true }),
3970
- },
3971
- execute: executeGenerateMedia,
3972
- };
3973
-
3974
- /** Register or unregister the tool to match the switch and the offered models. */
3975
- const syncAgentTool = () => {
3976
- let wanted = false;
3977
- try {
3978
- const { settings, options } = mediaChoices();
3979
- wanted = settings.agentTool === true && options.image.length + options.video.length > 0;
3980
- } catch {
3981
- wanted = false;
3982
- }
3983
- if (wanted && toolOff === undefined && toolsCtx !== undefined) {
3984
- try {
3985
- const off = toolsCtx.tools.register(generateMediaTool);
3986
- toolOff = typeof off === "function" ? off : () => {};
3987
- ctx.logger?.info?.("cpa: generate_media tool registered");
3988
- } catch (error) {
3989
- ctx.logger?.warn?.(`cpa: could not register generate_media: ${error instanceof Error ? error.message : String(error)}`);
3990
- }
3991
- } else if (!wanted && toolOff !== undefined) {
3992
- cancelConfirms();
3993
- const off = toolOff;
3994
- toolOff = undefined;
3995
- try {
3996
- off();
3997
- } catch {
3998
- // already gone with its scope
3999
- }
4000
- ctx.logger?.info?.("cpa: generate_media tool unregistered");
4001
- }
4002
- syncMediaCommands();
4003
- };
4004
-
4005
- /**
4006
- * Register or unregister /cpa-image and the /cpa-video* commands to match
4007
- * the Generation tab's master switch. Jobs already submitted keep running.
4008
- */
4009
- const syncMediaCommands = () => {
4010
- let wanted = false;
4011
- try {
4012
- wanted = readMedia().settings.enabled === true;
4013
- } catch {
4014
- wanted = false;
4015
- }
4016
- if (wanted && mediaCommandsOff === undefined && commandsCtx !== undefined) {
4017
- try {
4018
- mediaCommandsOff = registerMediaCommands(commandsCtx);
4019
- ctx.logger?.info?.("cpa: generation commands registered");
4020
- } catch (error) {
4021
- ctx.logger?.warn?.(`cpa: could not register the generation commands: ${error instanceof Error ? error.message : String(error)}`);
4022
- }
4023
- } else if (!wanted && mediaCommandsOff !== undefined) {
4024
- const off = mediaCommandsOff;
4025
- mediaCommandsOff = undefined;
4026
- try {
4027
- off();
4028
- } catch {
4029
- // already gone with its scope
4030
- }
4031
- ctx.logger?.info?.("cpa: generation commands unregistered");
4032
- }
4033
- };
4034
- // #endregion
4035
-
4036
- reschedule();
4037
- resumeVideoJobs();
4038
- startupTimeout = outsideHmrTransaction(ctx, () =>
4039
- setTimeout(() => {
4040
- startupTimeout = undefined;
4041
- void controller.refreshAll().finally(() => syncAgentTool());
4042
- }, 600)
4043
- );
4044
-
4045
- ctx.on("credentials/reference-updated", (ref) => {
4046
- // A management key changed (also from the DSH credentials page): start a new failure count.
4047
- quota.keyChanged(ref);
4048
- for (const id of controller.accountsUsingKey(ref)) {
4049
- void controller.refreshAccount(id);
4050
- }
4051
- });
4052
-
4053
- ctx.effect(
4054
- () => () => {
4055
- if (timer !== undefined) clearInterval(timer);
4056
- if (startupTimeout !== undefined) clearTimeout(startupTimeout);
4057
- controller.dispose();
4058
- },
4059
- "cpa: refresh loop"
4060
- );
4061
-
4062
- // Register slash commands (/cpa-status, /cpa-refresh)
4063
- ctx.inject(["commands"], (cmdCtx) => {
4064
- cmdCtx.effect(() => {
4065
- const offStatus = cmdCtx.commands.register({
4066
- name: "cpa-status",
4067
- description: "Show CLIProxyAPI discovery and model synchronization status of every account.",
4068
- handler: async () => {
4069
- const payload = await controller.statusPayload();
4070
- const quotaText = renderQuotaText(await quota.snapshot());
4071
- return { kind: "success", text: [renderStatusText(payload), quotaText].filter(Boolean).join("\n\n") };
4072
- },
4073
- });
4074
- const offRefresh = cmdCtx.commands.register({
4075
- name: "cpa-refresh",
4076
- description: "Probe every CLIProxyAPI account now and refresh the synchronized DSH model catalog.",
4077
- handler: async () => {
4078
- const payload = await controller.refresh(undefined);
4079
- syncAgentTool();
4080
- const failed = payload.accounts.some((a) => a.status.state === "error");
4081
- return { kind: failed ? "error" : "success", text: renderStatusText(payload) };
4082
- },
4083
- });
4084
- commandsCtx = cmdCtx;
4085
- syncMediaCommands();
4086
- return () => {
4087
- offStatus?.();
4088
- offRefresh?.();
4089
- commandsCtx = undefined;
4090
- const off = mediaCommandsOff;
4091
- mediaCommandsOff = undefined;
4092
- try {
4093
- off?.();
4094
- } catch {
4095
- // disposed with its scope
4096
- }
4097
- };
4098
- }, "cpa: commands");
4099
- });
4100
-
4101
- /** The generation commands, registered only while generation is switched on (see syncMediaCommands). */
4102
- function registerMediaCommands(cmdCtx) {
4103
- {
4104
- const offVideo = cmdCtx.commands.register({
4105
- name: "cpa-video",
4106
- 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.",
4107
- input: { hint: "[-m <model>] [--aspect 16:9] [-s 1-15] [-r 480p|720p] [--ref|--first-last|--last] <prompt>", attachments: true },
4108
- handler: runVideoCommand,
4109
- });
4110
- const offVideoEdit = cmdCtx.commands.register({
4111
- name: "cpa-video-edit",
4112
- 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.",
4113
- input: { hint: "[--video <path> | --url <URL> | --from <command id>] [-m <model>] <what to change>", attachments: true },
4114
- handler: runVideoEditCommand("edit"),
4115
- });
4116
- const offVideoExtend = cmdCtx.commands.register({
4117
- name: "cpa-video-extend",
4118
- 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.",
4119
- input: { hint: "[--video <path> | --url <URL> | --from <command id>] [-m <model>] [-s 2-10] <what happens next>", attachments: true },
4120
- handler: runVideoEditCommand("extend"),
4121
- });
4122
- const offVideoStatus = cmdCtx.commands.register({
4123
- name: "cpa-video-status",
4124
- description: "List this session's CLIProxyAPI video jobs and their state.",
4125
- handler: runVideoStatusCommand,
4126
- });
4127
- const offImage = cmdCtx.commands.register({
4128
- name: "cpa-image",
4129
- description: "Generate an image with a CLIProxyAPI image model; attach images to edit them. Files go to the session's working directory.",
4130
- input: { hint: "[-m <model>] [--aspect 16:9] [-n 1-4] <prompt>", attachments: true },
4131
- handler: runImageCommand,
4132
- });
4133
- return () => {
4134
- offImage?.();
4135
- offVideo?.();
4136
- offVideoStatus?.();
4137
- offVideoEdit?.();
4138
- offVideoExtend?.();
4139
- };
4140
- }
4141
- }
4142
-
4143
- // The optional agent tool (registered only while switched on; see syncAgentTool).
4144
- ctx.inject(["tools"], (scope) => {
4145
- scope.effect(() => {
4146
- toolsCtx = scope;
4147
- syncAgentTool();
4148
- return () => {
4149
- cancelConfirms();
4150
- const off = toolOff;
4151
- toolOff = undefined;
4152
- toolsCtx = undefined;
4153
- try {
4154
- off?.();
4155
- } catch {
4156
- // disposed with its scope
4157
- }
4158
- };
4159
- }, "cpa: agent tool");
4160
- });
4161
-
4162
- // Register HTTP API Routes on webServer for the Web UI
4163
- ctx.inject(["webServer"], (webCtx) => {
4164
- webCtx.effect(() => {
4165
- const route = (path, method, run, errorStatus = 500) =>
4166
- webCtx.webServer.register({
4167
- kind: "exact",
4168
- path,
4169
- handler: async (req, res) => {
4170
- if (!admitRequest(ctx, req, res, method)) return;
4171
- try {
4172
- const body = method === "POST" ? (await readJsonBody(req)) ?? {} : {};
4173
- const result = await run(body, req);
4174
- // Account, model and Generation-tab changes may switch the agent tool on or off.
4175
- if (method === "POST") syncAgentTool();
4176
- sendJson(res, 200, result);
4177
- } catch (error) {
4178
- sendJson(res, errorStatus, {
4179
- ok: false,
4180
- error: error instanceof Error ? error.message : String(error),
4181
- });
4182
- }
4183
- },
4184
- });
4185
- const updateRoute = (path, method, run) => webCtx.webServer.register({
4186
- kind: "exact", path,
4187
- handler: async (req, res) => {
4188
- if (!admitUpdateRequest(ctx, req, res, method)) return;
4189
- try {
4190
- const body = method === "POST" ? await readJsonBody(req, 8192) : {};
4191
- if (!plainObject(body)) throw new UpdateError("invalid-settings");
4192
- if (path === ROUTES.updateDownload && (Object.keys(body).some((key) => key !== "version") || typeof body.version !== "string")) {
4193
- throw new UpdateError("stale-version");
4194
- }
4195
- if (path === ROUTES.updateCheck && Object.keys(body).length) throw new UpdateError("invalid-settings");
4196
- const update = await run(body);
4197
- sendJson(res, 200, { ok: true, update });
4198
- } catch (error) {
4199
- const errorCode = error instanceof UpdateError ? error.code : "invalid-settings";
4200
- sendJson(res, errorCode === "busy" ? 409 : 400, { ok: false, errorCode, error: errorCode, update: updater.status() });
4201
- }
4202
- },
4203
- });
4204
- const disposers = [
4205
- updateRoute(ROUTES.updateStatus, "GET", () => updater.status()),
4206
- updateRoute(ROUTES.updateCheck, "POST", () => updater.check()),
4207
- updateRoute(ROUTES.updateSave, "POST", (body) => updater.save(body)),
4208
- updateRoute(ROUTES.updateDownload, "POST", (body) => updater.download(body.version)),
4209
- route(ROUTES.status, "GET", () => controller.statusPayload()),
4210
- route(ROUTES.refresh, "POST", (body) => controller.refresh(body.accountId)),
4211
- route(
4212
- ROUTES.save,
4213
- "POST",
4214
- (body) => {
4215
- const patch = accountPatchFromInput(body);
4216
- const hasAccountFields = Object.keys(patch).some((k) => !GLOBAL_FIELDS.includes(k));
4217
- return body.accountId === undefined && !hasAccountFields
4218
- ? controller.updateGlobals(patch)
4219
- : controller.updateAccount(body.accountId, patch);
4220
- },
4221
- 400
4222
- ),
4223
- route(ROUTES.addAccount, "POST", (body) => controller.addAccount(accountPatchFromInput(body)), 400),
4224
- route(ROUTES.removeAccount, "POST", (body) => controller.removeAccount(body.accountId), 400),
4225
- route(ROUTES.testAccount, "POST", (body) => controller.testConnection(body.accountId, { baseURL: body.baseURL, apiKey: body.apiKey }), 400),
4226
- route(ROUTES.quotaStatus, "GET", async () => ({ ok: true, quota: await quota.snapshot() })),
4227
- route(ROUTES.quotaRefresh, "POST", async (body) => ({
4228
- ok: true,
4229
- quota: await quota.refresh(typeof body.accountId === "string" && body.accountId !== "" ? body.accountId : undefined, { wait: false }),
4230
- })),
4231
- route(
4232
- ROUTES.quotaSave,
4233
- "POST",
4234
- async (body) => {
4235
- if (typeof body.accountId !== "string" || body.accountId === "") {
4236
- throw new Error("dsh-cliproxyapi: saving a management key needs accountId");
4237
- }
4238
- const managementKey = typeof body.managementKey === "string" ? body.managementKey.trim() : "";
4239
- return {
4240
- ok: true,
4241
- quota: await quota.saveKey(body.accountId, {
4242
- ...(managementKey === "" ? {} : { managementKey }),
4243
- ...(body.clear === true ? { clear: true } : {}),
4244
- }, { wait: false }),
4245
- };
4246
- },
4247
- 400
4248
- ),
4249
- route(ROUTES.mediaStatus, "GET", async () => ({ ok: true, media: mediaStatus() })),
4250
- route(ROUTES.mediaSave, "POST", saveMedia, 400),
4251
- route(
4252
- ROUTES.mediaDiagnose,
4253
- "POST",
4254
- async (body) => {
4255
- if (typeof body.accountId !== "string" || body.accountId === "") throw new MediaError("invalid-account");
4256
- return { ok: true, diagnostic: await controller.diagnoseMedia(body.accountId) };
4257
- },
4258
- 400
4259
- ),
4260
- route(
4261
- ROUTES.mediaSession,
4262
- "GET",
4263
- async (_body, req) => {
4264
- const raw = new URL(req.url ?? "/", "http://host").searchParams.get("sessionId");
4265
- const sessionId = raw === null || raw === "" ? undefined : validSessionId(raw);
4266
- if (raw !== null && raw !== "" && sessionId === undefined) throw new MediaError("invalid-session");
4267
- // Right after a start the catalogs may not be read yet; sync once
4268
- // instead of letting the composer button wait for its next retry.
4269
- await mediaChoicesFor("any");
4270
- return { ok: true, session: sessionView(sessionId) };
4271
- },
4272
- 400
4273
- ),
4274
- route(ROUTES.mediaSessionSave, "POST", async (body) => saveSessionPick(body), 400),
4275
- // The user's answer to a generate_media confirmation shown in its chat row.
4276
- route(
4277
- ROUTES.mediaConfirm,
4278
- "POST",
4279
- async (body) => {
4280
- const sessionId = validSessionId(body.sessionId);
4281
- const commandId = validCommandId(body.commandId);
4282
- if (sessionId === undefined || commandId === undefined || typeof body.allow !== "boolean") throw new MediaError("invalid-request");
4283
- const waiting = pendingConfirms.get(commandId);
4284
- if (waiting === undefined || waiting.sessionId !== sessionId) throw new MediaError("confirm-expired");
4285
- waiting.settle(body.allow ? "allowed" : "rejected");
4286
- return { ok: true };
4287
- },
4288
- 400
4289
- ),
4290
- // One slash-command run's record and files (its chat row shows them).
4291
- // The absolute paths let the page open or reveal a file through DSH's
4292
- // own Session Remote (`session.openWorkspacePath`), which re-verifies
4293
- // them on the Host.
4294
- route(
4295
- ROUTES.mediaCommand,
4296
- "GET",
4297
- async (_body, req) => {
4298
- const params = new URL(req.url ?? "/", "http://host").searchParams;
4299
- const sessionId = validSessionId(params.get("sessionId") ?? "");
4300
- const commandId = validCommandId(params.get("commandId") ?? "");
4301
- if (sessionId === undefined || commandId === undefined) throw new MediaError("invalid-request");
4302
- const entry = mediaSessions.get(sessionId);
4303
- const files = entry.recent
4304
- .filter((r) => r.commandId === commandId && existsSync(r.file))
4305
- .map((r) => ({ id: r.id, kind: r.kind, name: basename(r.file), path: r.file }));
4306
- let run = entry.runs.find((r) => r.commandId === commandId) ?? null;
4307
- // A run recorded as running that no job of this activation owns was
4308
- // cut off by a reload or a restart.
4309
- if (run?.status === "running" && !mediaJobs.has(commandId)) {
4310
- run = { ...run, status: "error", errorCode: "interrupted", error: mediaFailureText(new MediaError("interrupted"), run.kind) };
4311
- } else if (run?.status === "running" && videoLive.has(commandId)) {
4312
- // A video's phase (submitting / queued / running / downloading) and CPA's progress, if it reports one.
4313
- run = { ...run, ...videoLive.get(commandId) };
4314
- }
4315
- // Which file manager "show in folder" opens, to name the menu item.
4316
- const desktop = process.platform === "win32" ? "explorer" : process.platform === "darwin" ? "finder" : "filemanager";
4317
- // Edit / extend this video: by xAI's short-lived link while it is
4318
- // remembered, else by sending its file inline when small enough.
4319
- // Past results stay visible with generation off, without the edit / extend actions.
4320
- let actions;
4321
- if (run?.kind === "video" && run.status === "done" && readMedia().settings.enabled === true) {
4322
- const source = usableSource(commandId);
4323
- const video = files.find((f) => f.kind === "video");
4324
- let size;
4325
- try {
4326
- size = video === undefined ? undefined : statSync(video.path).size;
4327
- } catch {
4328
- size = undefined;
4329
- }
4330
- if (source !== undefined || (size !== undefined && size <= VIDEO_UPLOAD_MAX_BYTES)) {
4331
- const seconds = source?.seconds ?? run.seconds;
4332
- const accountId = source?.accountId ?? String(run.model).split("/", 1)[0];
4333
- actions = {
4334
- edit: seconds === undefined || seconds <= EDIT_SOURCE_MAX_SECONDS,
4335
- extend:
4336
- (seconds === undefined || (seconds >= EXTEND_SOURCE_SECONDS.min && seconds <= EXTEND_SOURCE_SECONDS.max)) &&
4337
- !unsupportedRoutes.has(`${accountId}/extensions`),
4338
- ...(source === undefined ? {} : { expiresAt: new Date(source.at + VIDEO_SOURCE_TTL_MS).toISOString() }),
4339
- };
4340
- }
4341
- }
4342
- // A generate_media call waiting for the user's confirmation in this row.
4343
- const waiting = pendingConfirms.get(commandId);
4344
- const confirm = waiting !== undefined && waiting.sessionId === sessionId ? { kind: waiting.kind, en: waiting.en, zh: waiting.zh, expiresAt: waiting.expiresAt } : undefined;
4345
- return {
4346
- ok: true,
4347
- run,
4348
- files,
4349
- desktop,
4350
- ...(files.length > 0 ? { folderPath: dirname(files[0].path) } : {}),
4351
- ...(actions === undefined ? {} : { actions }),
4352
- ...(confirm === undefined ? {} : { confirm }),
4353
- };
4354
- },
4355
- 400
4356
- ),
4357
- // Serves only files this plugin wrote and recorded for that session.
4358
- webCtx.webServer.register({
4359
- kind: "exact",
4360
- path: ROUTES.mediaFile,
4361
- handler: async (req, res) => {
4362
- if (!admitRequest(ctx, req, res, "GET")) return;
4363
- const params = new URL(req.url ?? "/", "http://host").searchParams;
4364
- const sessionId = validSessionId(params.get("sessionId") ?? "");
4365
- const record = sessionId === undefined ? undefined : mediaSessions.get(sessionId).recent.find((r) => r.id === params.get("id"));
4366
- const type = record === undefined ? undefined : MEDIA_FILE_TYPES[extname(record.file).toLowerCase()];
4367
- if (record === undefined || type === undefined || !existsSync(record.file)) {
4368
- sendJson(res, 404, { ok: false, error: "not-found" });
4369
- return;
4370
- }
4371
- try {
4372
- if (!type.startsWith("video/")) {
4373
- const bytes = readFileSync(record.file);
4374
- res.writeHead(200, {
4375
- "cache-control": "no-store",
4376
- "content-type": type,
4377
- "content-length": String(bytes.byteLength),
4378
- "x-content-type-options": "nosniff",
4379
- });
4380
- res.end(bytes);
4381
- return;
4382
- }
4383
- // Videos are streamed, with single byte ranges so <video> can seek.
4384
- const size = statSync(record.file).size;
4385
- const range = byteRange(req.headers?.range, size);
4386
- if (range === null) {
4387
- res.writeHead(416, { "content-range": `bytes */${size}` });
4388
- res.end();
4389
- return;
4390
- }
4391
- const [start, end] = range ?? [0, size - 1];
4392
- res.writeHead(range === undefined ? 200 : 206, {
4393
- "cache-control": "no-store",
4394
- "content-type": type,
4395
- "accept-ranges": "bytes",
4396
- "content-length": String(size === 0 ? 0 : end - start + 1),
4397
- ...(range === undefined ? {} : { "content-range": `bytes ${start}-${end}/${size}` }),
4398
- "x-content-type-options": "nosniff",
4399
- });
4400
- if (size === 0) {
4401
- res.end();
4402
- return;
4403
- }
4404
- const stream = createReadStream(record.file, { start, end });
4405
- stream.on("error", () => res.destroy?.());
4406
- res.on?.("close", () => stream.destroy());
4407
- stream.pipe(res);
4408
- } catch {
4409
- sendJson(res, 404, { ok: false, error: "not-found" });
4410
- }
4411
- },
4412
- }),
4413
- ];
4414
- return () => {
4415
- for (const dispose of disposers) dispose?.();
4416
- };
4417
- }, "cpa: webserver routes");
4418
- });
4419
- }
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 } from "./host/models.js";
18
+ export { recommendModels, planRecommendations, selectionSuggestions } from "./host/recommend.js";
19
+ export { nativeProtocol, modelProtocol, routeKeyFor, applyModelOverride, isChatModel, buildPiAiRoutes, managedRouteKeys } from "./host/routes.js";
20
+ export { CpaRuntime } from "./host/runtime.js";
21
+ export { resolveStateFilePath, loadPersistedState, savePersistedState } from "./host/state-store.js";
22
+ export { renderQuotaText, renderStatusText } from "./host/status-text.js";
4420
23
 
4421
24
  export const apply = mountOnce(PACKAGE_NAME, applyImpl);
4422
-
4423
- // #endregion