dsh-cliproxyapi-pro 1.8.0 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.en.md +38 -409
  3. package/README.md +38 -409
  4. package/lib/client.js +131 -28
  5. package/lib/host/accounts.js +176 -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 +105 -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 +34 -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 +205 -0
  26. package/lib/host/controller.js +435 -0
  27. package/lib/host/discovery.js +295 -0
  28. package/lib/host/http-gate.js +144 -0
  29. package/lib/host/models.js +102 -0
  30. package/lib/host/mount-guard.js +56 -0
  31. package/lib/host/routes.js +165 -0
  32. package/lib/host/runtime.js +445 -0
  33. package/lib/host/state-store.js +39 -0
  34. package/lib/host/status-text.js +93 -0
  35. package/lib/index.js +17 -4330
  36. package/lib/media/agent-tool.js +255 -0
  37. package/lib/media/choice.js +83 -0
  38. package/lib/media/classify.js +65 -0
  39. package/lib/media/codec/fit.js +76 -0
  40. package/lib/media/codec/jpeg.js +158 -0
  41. package/lib/media/codec/png.js +102 -0
  42. package/lib/media/commands/parsing.js +269 -0
  43. package/lib/media/constants.js +46 -0
  44. package/lib/media/diagnose.js +134 -0
  45. package/lib/media/images.js +165 -0
  46. package/lib/media/index.js +29 -0
  47. package/lib/media/interpret.js +126 -0
  48. package/lib/media/job-store.js +99 -0
  49. package/lib/media/mp4.js +44 -0
  50. package/lib/media/output-files.js +59 -0
  51. package/lib/media/session-store.js +293 -0
  52. package/lib/media/settings.js +255 -0
  53. package/lib/media/util.js +45 -0
  54. package/lib/media/video.js +326 -0
  55. package/lib/quota/auth-state.js +33 -0
  56. package/lib/quota/constants.js +86 -0
  57. package/lib/quota/controller.js +467 -0
  58. package/lib/quota/helpers.js +325 -0
  59. package/lib/quota/index.js +19 -0
  60. package/lib/quota/management.js +216 -0
  61. package/lib/quota/matching.js +143 -0
  62. package/lib/quota/parsers.js +352 -0
  63. package/lib/quota/probes/billing.js +80 -0
  64. package/lib/quota/probes/google.js +120 -0
  65. package/lib/quota/probes/oauth.js +27 -0
  66. package/lib/quota/probes/plugin.js +18 -0
  67. package/lib/updater/constants.js +23 -0
  68. package/lib/updater/errors.js +8 -0
  69. package/lib/updater/index.js +11 -0
  70. package/lib/updater/registry.js +83 -0
  71. package/lib/updater/settings.js +45 -0
  72. package/lib/updater/store.js +23 -0
  73. package/lib/updater/updater.js +148 -0
  74. package/lib/updater/verify.js +110 -0
  75. package/lib/updater/version.js +31 -0
  76. package/locale/en.json +1 -1
  77. package/locale/zh.json +1 -1
  78. package/package.json +6 -9
  79. package/lib/media.js +0 -2528
  80. package/lib/quota.js +0 -1646
  81. package/lib/updater.js +0 -443
@@ -0,0 +1,273 @@
1
+ // Ordered account/update/quota/media HTTP route registration and media file serving.
2
+
3
+ import { createReadStream, existsSync, readFileSync, statSync } from "node:fs";
4
+ import { basename, dirname, extname } from "node:path";
5
+ import { UpdateError } from "../../updater/index.js";
6
+ import { MediaError, validCommandId, validSessionId, EDIT_SOURCE_MAX_SECONDS, EXTEND_SOURCE_SECONDS, VIDEO_UPLOAD_MAX_BYTES } from "../../media/index.js";
7
+ import { plainObject } from "../accounts.js";
8
+ import { accountPatchFromInput } from "../apply-utils.js";
9
+ import { GLOBAL_FIELDS } from "../config.js";
10
+ import { ROUTES, sendJson, admitRequest, admitUpdateRequest, readJsonBody } from "../http-gate.js";
11
+
12
+ export function createHttpRoutes(shared) {
13
+ const { ctx, updater, controller, quota, mediaStatus, saveMedia, mediaSessions, mediaChoicesFor, sessionView, saveSessionPick, MEDIA_FILE_TYPES, byteRange, mediaFailureText, mediaJobs, videoLive, VIDEO_SOURCE_TTL_MS, usableSource, unsupportedRoutes, pendingConfirms, syncAgentTool } = shared;
14
+ // Register HTTP API Routes on webServer for the Web UI
15
+ ctx.inject(["webServer"], (webCtx) => {
16
+ webCtx.effect(() => {
17
+ const route = (path, method, run, errorStatus = 500) =>
18
+ webCtx.webServer.register({
19
+ kind: "exact",
20
+ path,
21
+ handler: async (req, res) => {
22
+ if (!admitRequest(ctx, req, res, method)) return;
23
+ try {
24
+ const body = method === "POST" ? (await readJsonBody(req)) ?? {} : {};
25
+ const result = await run(body, req);
26
+ // Account, model and Generation-tab changes may switch the agent tool on or off.
27
+ if (method === "POST") syncAgentTool();
28
+ sendJson(res, 200, result);
29
+ } catch (error) {
30
+ sendJson(res, errorStatus, {
31
+ ok: false,
32
+ error: error instanceof Error ? error.message : String(error),
33
+ });
34
+ }
35
+ },
36
+ });
37
+ const updateRoute = (path, method, run) => webCtx.webServer.register({
38
+ kind: "exact", path,
39
+ handler: async (req, res) => {
40
+ if (!admitUpdateRequest(ctx, req, res, method)) return;
41
+ try {
42
+ const body = method === "POST" ? await readJsonBody(req, 8192) : {};
43
+ if (!plainObject(body)) throw new UpdateError("invalid-settings");
44
+ if (path === ROUTES.updateDownload && (Object.keys(body).some((key) => key !== "version") || typeof body.version !== "string")) {
45
+ throw new UpdateError("stale-version");
46
+ }
47
+ if (path === ROUTES.updateCheck && Object.keys(body).length) throw new UpdateError("invalid-settings");
48
+ const update = await run(body);
49
+ sendJson(res, 200, { ok: true, update });
50
+ } catch (error) {
51
+ const errorCode = error instanceof UpdateError ? error.code : "invalid-settings";
52
+ sendJson(res, errorCode === "busy" ? 409 : 400, { ok: false, errorCode, error: errorCode, update: updater.status() });
53
+ }
54
+ },
55
+ });
56
+ const disposers = [
57
+ updateRoute(ROUTES.updateStatus, "GET", () => updater.status()),
58
+ updateRoute(ROUTES.updateCheck, "POST", () => updater.check()),
59
+ updateRoute(ROUTES.updateSave, "POST", (body) => updater.save(body)),
60
+ updateRoute(ROUTES.updateDownload, "POST", (body) => updater.download(body.version)),
61
+ route(ROUTES.status, "GET", () => controller.statusPayload()),
62
+ route(ROUTES.refresh, "POST", (body) => controller.refresh(body.accountId)),
63
+ route(
64
+ ROUTES.save,
65
+ "POST",
66
+ (body) => {
67
+ const patch = accountPatchFromInput(body);
68
+ const hasAccountFields = Object.keys(patch).some((k) => !GLOBAL_FIELDS.includes(k));
69
+ return body.accountId === undefined && !hasAccountFields
70
+ ? controller.updateGlobals(patch)
71
+ : controller.updateAccount(body.accountId, patch);
72
+ },
73
+ 400
74
+ ),
75
+ route(ROUTES.addAccount, "POST", (body) => controller.addAccount(accountPatchFromInput(body)), 400),
76
+ route(ROUTES.removeAccount, "POST", (body) => controller.removeAccount(body.accountId), 400),
77
+ route(ROUTES.testAccount, "POST", (body) => controller.testConnection(body.accountId, { baseURL: body.baseURL, apiKey: body.apiKey }), 400),
78
+ route(ROUTES.quotaStatus, "GET", async () => ({ ok: true, quota: await quota.snapshot() })),
79
+ route(ROUTES.quotaRefresh, "POST", async (body) => ({
80
+ ok: true,
81
+ quota: await quota.refresh(typeof body.accountId === "string" && body.accountId !== "" ? body.accountId : undefined, { wait: false }),
82
+ })),
83
+ route(
84
+ ROUTES.quotaSave,
85
+ "POST",
86
+ async (body) => {
87
+ if (typeof body.accountId !== "string" || body.accountId === "") {
88
+ throw new Error("dsh-cliproxyapi: saving a management key needs accountId");
89
+ }
90
+ const managementKey = typeof body.managementKey === "string" ? body.managementKey.trim() : "";
91
+ return {
92
+ ok: true,
93
+ quota: await quota.saveKey(body.accountId, {
94
+ ...(managementKey === "" ? {} : { managementKey }),
95
+ ...(body.clear === true ? { clear: true } : {}),
96
+ }, { wait: false }),
97
+ };
98
+ },
99
+ 400
100
+ ),
101
+ route(ROUTES.mediaStatus, "GET", async () => ({ ok: true, media: mediaStatus() })),
102
+ route(ROUTES.mediaSave, "POST", saveMedia, 400),
103
+ route(
104
+ ROUTES.mediaDiagnose,
105
+ "POST",
106
+ async (body) => {
107
+ if (typeof body.accountId !== "string" || body.accountId === "") throw new MediaError("invalid-account");
108
+ return { ok: true, diagnostic: await controller.diagnoseMedia(body.accountId) };
109
+ },
110
+ 400
111
+ ),
112
+ route(
113
+ ROUTES.mediaSession,
114
+ "GET",
115
+ async (_body, req) => {
116
+ const raw = new URL(req.url ?? "/", "http://host").searchParams.get("sessionId");
117
+ const sessionId = raw === null || raw === "" ? undefined : validSessionId(raw);
118
+ if (raw !== null && raw !== "" && sessionId === undefined) throw new MediaError("invalid-session");
119
+ // Right after a start the catalogs may not be read yet; sync once
120
+ // instead of letting the composer button wait for its next retry.
121
+ await mediaChoicesFor("any");
122
+ return { ok: true, session: sessionView(sessionId) };
123
+ },
124
+ 400
125
+ ),
126
+ route(ROUTES.mediaSessionSave, "POST", async (body) => saveSessionPick(body), 400),
127
+ // The user's answer to a generate_media confirmation shown in its chat row.
128
+ route(
129
+ ROUTES.mediaConfirm,
130
+ "POST",
131
+ async (body) => {
132
+ const sessionId = validSessionId(body.sessionId);
133
+ const commandId = validCommandId(body.commandId);
134
+ if (sessionId === undefined || commandId === undefined || typeof body.allow !== "boolean") throw new MediaError("invalid-request");
135
+ const waiting = pendingConfirms.get(commandId);
136
+ if (waiting === undefined || waiting.sessionId !== sessionId) throw new MediaError("confirm-expired");
137
+ waiting.settle(body.allow ? "allowed" : "rejected");
138
+ return { ok: true };
139
+ },
140
+ 400
141
+ ),
142
+ // One slash-command run's record and files (its chat row shows them).
143
+ // The absolute paths let the page open or reveal a file through DSH's
144
+ // own Session Remote (`session.openWorkspacePath`), which re-verifies
145
+ // them on the Host.
146
+ route(
147
+ ROUTES.mediaCommand,
148
+ "GET",
149
+ async (_body, req) => {
150
+ const params = new URL(req.url ?? "/", "http://host").searchParams;
151
+ const sessionId = validSessionId(params.get("sessionId") ?? "");
152
+ const commandId = validCommandId(params.get("commandId") ?? "");
153
+ if (sessionId === undefined || commandId === undefined) throw new MediaError("invalid-request");
154
+ const entry = mediaSessions.get(sessionId);
155
+ const files = entry.recent
156
+ .filter((r) => r.commandId === commandId && existsSync(r.file))
157
+ .map((r) => ({ id: r.id, kind: r.kind, name: basename(r.file), path: r.file }));
158
+ let run = entry.runs.find((r) => r.commandId === commandId) ?? null;
159
+ // A run recorded as running that no job of this activation owns was
160
+ // cut off by a reload or a restart.
161
+ if (run?.status === "running" && !mediaJobs.has(commandId)) {
162
+ run = { ...run, status: "error", errorCode: "interrupted", error: mediaFailureText(new MediaError("interrupted"), run.kind) };
163
+ } else if (run?.status === "running" && videoLive.has(commandId)) {
164
+ // A video's phase (submitting / queued / running / downloading) and CPA's progress, if it reports one.
165
+ run = { ...run, ...videoLive.get(commandId) };
166
+ }
167
+ // Which file manager "show in folder" opens, to name the menu item.
168
+ const desktop = process.platform === "win32" ? "explorer" : process.platform === "darwin" ? "finder" : "filemanager";
169
+ // Edit / extend this video: by xAI's short-lived link while it is
170
+ // remembered, else by sending its file inline when small enough.
171
+ let actions;
172
+ // Past results stay visible with generation off, without the edit / extend actions.
173
+ if (run?.kind === "video" && run.status === "done" && shared.readMedia().settings.enabled === true) {
174
+ const source = usableSource(commandId);
175
+ const video = files.find((f) => f.kind === "video");
176
+ let size;
177
+ try {
178
+ size = video === undefined ? undefined : statSync(video.path).size;
179
+ } catch {
180
+ size = undefined;
181
+ }
182
+ if (source !== undefined || (size !== undefined && size <= VIDEO_UPLOAD_MAX_BYTES)) {
183
+ const seconds = source?.seconds ?? run.seconds;
184
+ const accountId = source?.accountId ?? String(run.model).split("/", 1)[0];
185
+ actions = {
186
+ edit: seconds === undefined || seconds <= EDIT_SOURCE_MAX_SECONDS,
187
+ extend:
188
+ (seconds === undefined || (seconds >= EXTEND_SOURCE_SECONDS.min && seconds <= EXTEND_SOURCE_SECONDS.max)) &&
189
+ !unsupportedRoutes.has(`${accountId}/extensions`),
190
+ ...(source === undefined ? {} : { expiresAt: new Date(source.at + VIDEO_SOURCE_TTL_MS).toISOString() }),
191
+ };
192
+ }
193
+ }
194
+ // A generate_media call waiting for the user's confirmation in this row.
195
+ const waiting = pendingConfirms.get(commandId);
196
+ const confirm = waiting !== undefined && waiting.sessionId === sessionId ? { kind: waiting.kind, en: waiting.en, zh: waiting.zh, expiresAt: waiting.expiresAt } : undefined;
197
+ return {
198
+ ok: true,
199
+ run,
200
+ files,
201
+ desktop,
202
+ ...(files.length > 0 ? { folderPath: dirname(files[0].path) } : {}),
203
+ ...(actions === undefined ? {} : { actions }),
204
+ ...(confirm === undefined ? {} : { confirm }),
205
+ };
206
+ },
207
+ 400
208
+ ),
209
+ // Serves only files this plugin wrote and recorded for that session.
210
+ webCtx.webServer.register({
211
+ kind: "exact",
212
+ path: ROUTES.mediaFile,
213
+ handler: async (req, res) => {
214
+ if (!admitRequest(ctx, req, res, "GET")) return;
215
+ const params = new URL(req.url ?? "/", "http://host").searchParams;
216
+ const sessionId = validSessionId(params.get("sessionId") ?? "");
217
+ const record = sessionId === undefined ? undefined : mediaSessions.get(sessionId).recent.find((r) => r.id === params.get("id"));
218
+ const type = record === undefined ? undefined : MEDIA_FILE_TYPES[extname(record.file).toLowerCase()];
219
+ if (record === undefined || type === undefined || !existsSync(record.file)) {
220
+ sendJson(res, 404, { ok: false, error: "not-found" });
221
+ return;
222
+ }
223
+ try {
224
+ if (!type.startsWith("video/")) {
225
+ const bytes = readFileSync(record.file);
226
+ res.writeHead(200, {
227
+ "cache-control": "no-store",
228
+ "content-type": type,
229
+ "content-length": String(bytes.byteLength),
230
+ "x-content-type-options": "nosniff",
231
+ });
232
+ res.end(bytes);
233
+ return;
234
+ }
235
+ // Videos are streamed, with single byte ranges so <video> can seek.
236
+ const size = statSync(record.file).size;
237
+ const range = byteRange(req.headers?.range, size);
238
+ if (range === null) {
239
+ res.writeHead(416, { "content-range": `bytes */${size}` });
240
+ res.end();
241
+ return;
242
+ }
243
+ const [start, end] = range ?? [0, size - 1];
244
+ res.writeHead(range === undefined ? 200 : 206, {
245
+ "cache-control": "no-store",
246
+ "content-type": type,
247
+ "accept-ranges": "bytes",
248
+ "content-length": String(size === 0 ? 0 : end - start + 1),
249
+ ...(range === undefined ? {} : { "content-range": `bytes ${start}-${end}/${size}` }),
250
+ "x-content-type-options": "nosniff",
251
+ });
252
+ if (size === 0) {
253
+ res.end();
254
+ return;
255
+ }
256
+ const stream = createReadStream(record.file, { start, end });
257
+ stream.on("error", () => res.destroy?.());
258
+ res.on?.("close", () => stream.destroy());
259
+ stream.pipe(res);
260
+ } catch {
261
+ sendJson(res, 404, { ok: false, error: "not-found" });
262
+ }
263
+ },
264
+ }),
265
+ ];
266
+ return () => {
267
+ for (const dispose of disposers) dispose?.();
268
+ };
269
+ }, "cpa: webserver routes");
270
+ });
271
+
272
+ return {};
273
+ }
@@ -0,0 +1,171 @@
1
+ // Image admission and background generation, output persistence and settlement.
2
+
3
+ import { existsSync } from "node:fs";
4
+ import { isAbsolute } from "node:path";
5
+ import { chooseMediaModel, generateImages, MAX_REFERENCE_IMAGES, MediaError, outputDirectory, outputStem, parseImageCommand, recentEntry, validCommandId, validSessionId, writeOutputFiles } from "../../media/index.js";
6
+
7
+ export function createImageJobs(shared) {
8
+ const { ctx, controller, mediaSessions, mediaChoicesFor, mediaAccountsSummary, REFERENCES_MAX_BYTES, readReference, interpretParams, interpretedRecord, mediaFailureText, mediaJobs, settleWith, publishSessionStatus, settleSessionStatus, syncHeartbeat, recordRun, folderLabel } = shared;
9
+ /** Run one admitted image request in the background; resolves when it has settled and been recorded. */
10
+ const startImageJob = ({ sessionId, commandId, option, args: admitted, references, cwd, outputDir, endpoint, interpret }) => {
11
+ const key = commandId ?? `job-${++shared.mediaJobSeq}`;
12
+ const abort = new AbortController();
13
+ mediaJobs.set(key, { sessionId, abort });
14
+ syncHeartbeat();
15
+ if (sessionId !== undefined) publishSessionStatus(sessionId, true);
16
+ const started = Date.now();
17
+ return settleWith(key, (async () => {
18
+ try {
19
+ let args = admitted;
20
+ if (interpret !== undefined) {
21
+ if (commandId !== undefined) shared.videoLive.set(commandId, { phase: "interpreting" });
22
+ const result = await interpret(abort.signal);
23
+ if (commandId !== undefined) shared.videoLive.delete(commandId);
24
+ if (result !== undefined) {
25
+ args = { ...admitted, ...result.values, prompt: result.prompt };
26
+ recordRun(sessionId, commandId, {
27
+ prompt: args.prompt,
28
+ ...(args.aspect === undefined ? {} : { aspect: args.aspect }),
29
+ count: args.count ?? 1,
30
+ ...interpretedRecord(result, admitted.prompt),
31
+ });
32
+ }
33
+ }
34
+ const images = await generateImages({
35
+ rootURL: endpoint.rootURL,
36
+ apiKey: endpoint.apiKey,
37
+ model: option.modelId,
38
+ prompt: args.prompt,
39
+ ...(args.aspect === undefined ? {} : { aspect: args.aspect }),
40
+ count: args.count ?? 1,
41
+ references,
42
+ signal: abort.signal,
43
+ });
44
+ const dir = outputDirectory(cwd, outputDir);
45
+ const paths = writeOutputFiles(dir, outputStem(option.modelId, args.prompt), images);
46
+ const revised = images.find((image) => image.revisedPrompt !== undefined)?.revisedPrompt;
47
+ recordRun(
48
+ sessionId,
49
+ commandId,
50
+ {
51
+ status: "done",
52
+ files: paths.length,
53
+ folder: folderLabel(cwd, dir),
54
+ finishedAt: new Date().toISOString(),
55
+ ...(revised === undefined ? {} : { revisedPrompt: revised }),
56
+ },
57
+ paths.map((p) => recentEntry("image", p, option.ref, new Date(), commandId))
58
+ );
59
+ ctx.logger?.info?.(`cpa: /cpa-image wrote ${paths.length} image(s) with ${option.modelId} in ${Math.round((Date.now() - started) / 1000)} s`);
60
+ } catch (thrown) {
61
+ const error = abort.signal.aborted ? new MediaError("interrupted") : thrown;
62
+ recordRun(sessionId, commandId, {
63
+ status: "error",
64
+ errorCode: error instanceof MediaError ? error.code : "failed",
65
+ error: mediaFailureText(error),
66
+ finishedAt: new Date().toISOString(),
67
+ });
68
+ ctx.logger?.warn?.(`cpa: /cpa-image failed: ${mediaFailureText(error)}`);
69
+ } finally {
70
+ if (commandId !== undefined) shared.videoLive.delete(commandId);
71
+ mediaJobs.delete(key);
72
+ syncHeartbeat();
73
+ settleSessionStatus(sessionId);
74
+ }
75
+ })());
76
+ };
77
+
78
+ // #endregion
79
+
80
+ /**
81
+ * `/cpa-image`: generate or (with attached images) edit, save into the
82
+ * session's folder. Checks everything it can up front (options, model,
83
+ * folder, references, endpoint), then returns while the generation runs as
84
+ * a background job; the chat row shows its progress and result.
85
+ */
86
+ // Internal callers (the agent tool) add: `literal` (no plain-language
87
+ // parsing or interpretation), `pickSession` (whose model pick applies) and
88
+ // `gate(plan)` (confirmation / budget, run after every check and before
89
+ // anything is recorded or sent; a thrown `gateError` passes through).
90
+ const runImageCommand = async ({ agent, rawInput, attachments = [], signal, commandId, literal = false, pickSession, gate }) => {
91
+ try {
92
+ const args = parseImageCommand(rawInput, { literal });
93
+ const blocks = Array.isArray(attachments) ? attachments : [];
94
+ if (blocks.some((block) => block?.type !== "image")) throw new MediaError("unsupported-attachment");
95
+ if (blocks.length > MAX_REFERENCE_IMAGES) throw new MediaError("too-many-references");
96
+ const { settings, options, accounts } = await mediaChoicesFor("image");
97
+ const sessionId = validSessionId(agent?.id);
98
+ const pickId = validSessionId(pickSession) ?? sessionId;
99
+ const entry = pickId === undefined ? {} : mediaSessions.get(pickId);
100
+ let option;
101
+ try {
102
+ ({ option } = chooseMediaModel({
103
+ options: options.image,
104
+ requested: args.model,
105
+ sessionRef: entry.image,
106
+ lastRef: settings.lastSelection.image,
107
+ }));
108
+ } catch (error) {
109
+ if (error instanceof MediaError && error.code === "no-media-model") {
110
+ throw new MediaError("no-media-model", mediaAccountsSummary(accounts, "image"));
111
+ }
112
+ throw error;
113
+ }
114
+ const workingDirectory = ctx.get?.("workingDirectory");
115
+ const cwd = agent !== undefined && workingDirectory !== undefined ? await workingDirectory.ensure(agent, signal) : undefined;
116
+ if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new MediaError("workspace-not-local");
117
+ const references = [];
118
+ if (blocks.length > 0) {
119
+ const store = ctx.get?.("attachments");
120
+ if (store === undefined) throw new MediaError("unsupported-attachment");
121
+ const each = Math.floor(REFERENCES_MAX_BYTES / blocks.length);
122
+ for (const block of blocks) references.push(await readReference(store, block, each, signal));
123
+ }
124
+ const endpoint = await controller.mediaEndpoint(option.accountId);
125
+ const id = validCommandId(commandId);
126
+ const count = args.count ?? 1;
127
+ if (gate !== undefined) {
128
+ 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 });
129
+ }
130
+ recordRun(sessionId, id, {
131
+ kind: "image",
132
+ status: "running",
133
+ model: option.ref,
134
+ modelName: option.name || option.modelId,
135
+ accountName: option.accountName,
136
+ prompt: args.prompt,
137
+ ...(args.aspect === undefined ? {} : { aspect: args.aspect }),
138
+ count,
139
+ references: references.length,
140
+ files: 0,
141
+ startedAt: new Date().toISOString(),
142
+ });
143
+ void startImageJob({
144
+ sessionId,
145
+ commandId: id,
146
+ option,
147
+ args,
148
+ references,
149
+ cwd,
150
+ outputDir: settings.outputDir,
151
+ endpoint,
152
+ ...(literal
153
+ ? {}
154
+ : {
155
+ interpret: (jobSignal) =>
156
+ interpretParams({ agent, command: "image", text: args.prompt, images: references.length, fixed: ["aspect", "count"].filter((k) => args[k] !== undefined), signal: jobSignal }),
157
+ }),
158
+ });
159
+ const dir = outputDirectory(cwd, settings.outputDir);
160
+ return {
161
+ kind: "success",
162
+ text: `${references.length > 0 ? "Editing" : "Generating"} ${count} image(s) with ${option.modelId} (${option.accountName}); files go to ${dir}`,
163
+ };
164
+ } catch (error) {
165
+ if (signal?.aborted || error?.gateError === true) throw error;
166
+ return { kind: "error", text: `/cpa-image: ${mediaFailureText(error)}` };
167
+ }
168
+ };
169
+
170
+ return { runImageCommand };
171
+ }
@@ -0,0 +1,41 @@
1
+ // Host apply orchestration: build an explicit activation context and wire factories in original order.
2
+
3
+ import { createState } from "./state.js";
4
+ import { createUpdates } from "./updates.js";
5
+ import { createController } from "./controller.js";
6
+ import { createQuotaService } from "./quota-service.js";
7
+ import { createMediaService } from "./media-service.js";
8
+ import { createReferences } from "./references.js";
9
+ import { createInterpret } from "./interpret.js";
10
+ import { createMediaFailures } from "./media-failures.js";
11
+ import { createSessionStatus } from "./session-status.js";
12
+ import { createImageJobs } from "./image-jobs.js";
13
+ import { createVideoJobs } from "./video-jobs.js";
14
+ import { createVideoCommand } from "./video-command.js";
15
+ import { createVideoEdit } from "./video-edit.js";
16
+ import { createAgentTool } from "./agent-tool.js";
17
+ import { createLifecycle } from "./lifecycle.js";
18
+ import { createCommands } from "./commands.js";
19
+ import { createHttpRoutes } from "./http-routes.js";
20
+
21
+ export function applyImpl(ctx, config) {
22
+ const shared = { ctx, config };
23
+ Object.assign(shared, createState(shared));
24
+ Object.assign(shared, createUpdates(shared));
25
+ Object.assign(shared, createController(shared));
26
+ Object.assign(shared, createQuotaService(shared));
27
+ Object.assign(shared, createMediaService(shared));
28
+ Object.assign(shared, createReferences(shared));
29
+ Object.assign(shared, createInterpret(shared));
30
+ Object.assign(shared, createMediaFailures(shared));
31
+ Object.assign(shared, createSessionStatus(shared));
32
+ Object.assign(shared, createImageJobs(shared));
33
+ Object.assign(shared, createVideoJobs(shared));
34
+ Object.assign(shared, createVideoCommand(shared));
35
+ Object.assign(shared, createVideoEdit(shared));
36
+ Object.assign(shared, createAgentTool(shared));
37
+ Object.assign(shared, createLifecycle(shared));
38
+ Object.assign(shared, createCommands(shared));
39
+ shared.registerAgentTool();
40
+ Object.assign(shared, createHttpRoutes(shared));
41
+ }
@@ -0,0 +1,72 @@
1
+ // Session chat-target lookup and non-blocking generation-parameter interpretation.
2
+
3
+ import { interpretationLabels, interpretationRequest, parseInterpretation } from "../../media/index.js";
4
+
5
+ export function createInterpret(shared) {
6
+ const { ctx } = shared;
7
+ // #region Parameter interpretation (the session's chat model)
8
+ const INTERPRET_TIMEOUT_MS = 30_000;
9
+ const INTERPRET_MAX_TOKENS = 2000;
10
+
11
+ /** The provider / model the session's agent last used (else its options, else the default model). */
12
+ const sessionChatTarget = (agent) => {
13
+ const valid = (t) => (t && typeof t.provider === "string" && t.provider !== "" && typeof t.model === "string" && t.model !== "" ? { provider: t.provider, model: t.model } : undefined);
14
+ try {
15
+ const header = valid(agent?.session?.requestHeader?.()?.config);
16
+ if (header !== undefined) return header;
17
+ } catch {
18
+ // fall through
19
+ }
20
+ return valid(agent?.options) ?? valid(ctx.get?.("agentDefaultModel")?.currentSelection?.());
21
+ };
22
+
23
+ /**
24
+ * Ask the session's chat model which parameters the prompt states.
25
+ * Never throws: without an `llm` service or a model it returns undefined;
26
+ * a failed call returns `{ failed: true, by }`.
27
+ * @param fixed - fields the rule parser already set (they win).
28
+ * @returns `{ by, values, prompt, items, failed? }` or undefined.
29
+ */
30
+ const interpretParams = async ({ agent, command, text, images = 0, fixed = [], signal }) => {
31
+ const llm = ctx.get?.("llm");
32
+ const target = sessionChatTarget(agent);
33
+ if (typeof llm?.stream !== "function" || target === undefined) return undefined;
34
+ const by = `${target.provider}/${target.model}`;
35
+ const request = interpretationRequest({ command, text, images, fixed });
36
+ const timeout = AbortSignal.timeout(INTERPRET_TIMEOUT_MS);
37
+ const combined = signal === undefined ? timeout : AbortSignal.any([signal, timeout]);
38
+ try {
39
+ const parts = new Map();
40
+ for await (const chunk of llm.stream({
41
+ provider: target.provider,
42
+ model: target.model,
43
+ system: request.system,
44
+ messages: [{ role: "user", content: [{ type: "text", text: request.user }] }],
45
+ maxTokens: INTERPRET_MAX_TOKENS,
46
+ ...(agent?.session?.id === undefined ? {} : { sessionId: agent.session.id }),
47
+ purpose: "cpa-media-params",
48
+ signal: combined,
49
+ })) {
50
+ if (chunk?.type === "text-delta") parts.set(chunk.index, (parts.get(chunk.index) ?? "") + chunk.text);
51
+ else if (chunk?.type === "block-end" && chunk.block?.type === "text") parts.set(chunk.index, chunk.block.text);
52
+ }
53
+ const answer = [...parts.values()].join("");
54
+ const parsed = parseInterpretation(answer, { command, text, fields: request.fields, images });
55
+ if (parsed === undefined) throw new Error("no JSON in the answer");
56
+ return { by, ...parsed, items: interpretationLabels(parsed.values) };
57
+ } catch (error) {
58
+ if (signal?.aborted) throw signal.reason;
59
+ ctx.logger?.warn?.(`cpa: parameter interpretation with ${by} failed: ${error instanceof Error ? error.message : String(error)}`);
60
+ return { by, failed: true, values: {}, prompt: text, items: [] };
61
+ }
62
+ };
63
+
64
+ /** The run-record field describing an interpretation. */
65
+ const interpretedRecord = (result, original) =>
66
+ result === undefined
67
+ ? {}
68
+ : { interpreted: { by: result.by, items: result.items, ...(result.failed ? { failed: true } : {}), ...(result.prompt !== original ? { prompt: true } : {}) } };
69
+ // #endregion
70
+
71
+ return { sessionChatTarget, interpretParams, interpretedRecord };
72
+ }
@@ -0,0 +1,34 @@
1
+ // Refresh startup, video resume, credential listener and refresh-loop disposal.
2
+
3
+ import { outsideHmrTransaction } from "../apply-utils.js";
4
+
5
+ export function createLifecycle(shared) {
6
+ const { ctx, reschedule, controller, quota, resumeVideoJobs, syncAgentTool } = shared;
7
+ reschedule();
8
+ resumeVideoJobs();
9
+ shared.startupTimeout = outsideHmrTransaction(ctx, () =>
10
+ setTimeout(() => {
11
+ shared.startupTimeout = undefined;
12
+ void controller.refreshAll().finally(() => syncAgentTool());
13
+ }, 600)
14
+ );
15
+
16
+ ctx.on("credentials/reference-updated", (ref) => {
17
+ // A management key changed (also from the DSH credentials page): start a new failure count.
18
+ quota.keyChanged(ref);
19
+ for (const id of controller.accountsUsingKey(ref)) {
20
+ void controller.refreshAccount(id);
21
+ }
22
+ });
23
+
24
+ ctx.effect(
25
+ () => () => {
26
+ if (shared.timer !== undefined) clearInterval(shared.timer);
27
+ if (shared.startupTimeout !== undefined) clearTimeout(shared.startupTimeout);
28
+ controller.dispose();
29
+ },
30
+ "cpa: refresh loop"
31
+ );
32
+
33
+ return {};
34
+ }
@@ -0,0 +1,68 @@
1
+ // Plain-English media failure summaries for commands and persisted run records.
2
+
3
+ import { IMAGE_ASPECTS, MAX_REFERENCE_IMAGES, MediaError, MAX_VIDEO_BYTES, VIDEO_ASPECTS, EDIT_SOURCE_MAX_SECONDS, EXTEND_SOURCE_SECONDS, VIDEO_UPLOAD_MAX_BYTES } from "../../media/index.js";
4
+
5
+ export function createMediaFailures(shared) {
6
+ /** Plain-English host text for a media failure (slash command output and run records). */
7
+ const mediaFailureText = (error, kind = "image") => {
8
+ if (!(error instanceof MediaError)) return error instanceof Error ? error.message : String(error);
9
+ const detail = error.detail;
10
+ const video = kind === "video";
11
+ const noun = video ? "video" : "image";
12
+ switch (error.code) {
13
+ 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";
14
+ case "invalid-option": return video
15
+ ? `unknown option ${detail}; use -m <model>, --aspect <ratio>, -s <1-15>, -r <480p|720p>`
16
+ : `unknown option ${detail}; use -m <model>, --aspect <ratio>, -n <1-4>`;
17
+ case "invalid-aspect": return `--aspect must be one of ${(video ? VIDEO_ASPECTS : IMAGE_ASPECTS).join(", ")}`;
18
+ case "invalid-count": return "-n must be 1 to 4";
19
+ case "invalid-video-seconds": return "-s / --seconds must be a whole number from 1 to 15";
20
+ case "invalid-video-resolution": return "-r / --resolution must be 480p or 720p";
21
+ 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`;
22
+ case "choose-model": return `several ${noun} models are available (${detail}); pick one with the button next to the composer or pass -m <model>`;
23
+ case "model-not-found": return `no enabled ${noun} model matches; available: ${detail || "none"}`;
24
+ case "model-ambiguous": return `several ${noun} models match (${detail}); be more specific`;
25
+ case "workspace-not-local": return `the session's working directory is not a local folder, so the ${noun} cannot be saved`;
26
+ case "unsupported-attachment": return detail === "edit"
27
+ ? "attach one MP4 video as the source (images cannot be used here)"
28
+ : video ? "only image attachments can be used for /cpa-video (first frame, references, first / last frames)" : "only image attachments can be used as references";
29
+ 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`;
30
+ 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";
31
+ case "needs-video-15": return "pinned first / last frames need grok-imagine-video-1.5; pick it with -m or the model button";
32
+ case "invalid-extend-seconds": return "an extension adds 2 to 10 seconds (-s 2-10)";
33
+ case "no-video-source": return "no source video: attach an MP4, or pass --video <path>, --url <public URL> or --from <command id>";
34
+ 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>";
35
+ case "one-video-source": return "use one source video: an attachment, --video, --url or --from";
36
+ case "invalid-video-url": return "--url must be a public http(s) address without a user name or password";
37
+ case "video-not-found": return `no video file at ${detail}`;
38
+ case "video-not-mp4": return "the source video must be an MP4 file (H.264, H.265 or AV1)";
39
+ 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`;
40
+ case "request-too-large": return video
41
+ ? `the request is too large for this server (${detail ? `${Math.round(Number(detail) / 1024)} KB` : "body limit"}); attach smaller or fewer images`
42
+ : "the request is too large for this server";
43
+ 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`;
44
+ 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`;
45
+ case "extend-source-length": return `xAI extends videos of ${EXTEND_SOURCE_SECONDS.min} to ${EXTEND_SOURCE_SECONDS.max} seconds (this one has ${detail} s)`;
46
+ case "reference-too-large": return `an attached image could not be made smaller than ${detail ?? "1000"} KB; attach a smaller image`;
47
+ case "upstream-error": return `CPA rejected the request (${detail})`;
48
+ case "timeout": return video ? "CPA did not answer in time" : "CPA did not answer within 3 minutes";
49
+ case "network": return "could not reach CPA";
50
+ case "no-image": return "CPA returned no image";
51
+ case "invalid-image":
52
+ 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";
53
+ case "image-too-large": return "an image is larger than 32 MiB";
54
+ case "video-too-large": return `the video is larger than ${Math.round(MAX_VIDEO_BYTES / 1024 / 1024)} MiB`;
55
+ case "invalid-video": return "CPA returned data that is not an MP4, MOV or WebM video";
56
+ case "video-failed": return `the video could not be generated${detail ? ` (${detail})` : ""}`;
57
+ case "expired": return "CPA no longer knows this video: more than 3 hours have passed since it was submitted";
58
+ case "download-failed": return `could not download the generated ${noun}`;
59
+ case "write-failed": return `could not write the ${noun} file${detail ? ` (${detail})` : ""}`;
60
+ case "no-endpoint": return "this account has no usable server address yet";
61
+ case "invalid-account": return "the model's account no longer exists";
62
+ case "interrupted": return "stopped because the plugin was reloaded or DSH closed";
63
+ default: return error.code;
64
+ }
65
+ };
66
+
67
+ return { mediaFailureText };
68
+ }