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
@@ -0,0 +1,43 @@
1
+ // Bounded attachment references and optional request-image resizing.
2
+
3
+ import { MediaError, fitReferenceImage, REFERENCE_MAX_EDGE } from "../../media/index.js";
4
+
5
+ export function createReferences(shared) {
6
+ // Request bodies carry references base64-encoded and upstreams cap the body
7
+ // (xAI: "Failed to read request body: length limit exceeded"). Video
8
+ // requests use the measured `videoImageBudget()`; image edits keep this
9
+ // unmeasured share.
10
+ const FIRST_FRAME_MAX_BYTES = 1_000_000;
11
+ const REFERENCES_MAX_BYTES = 1_400_000;
12
+
13
+ /**
14
+ * One attached image within `maxBytes`: as stored when it fits; a PNG is
15
+ * re-encoded here (JPEG on white, long edge ≤ 1280); another format asks
16
+ * DSH's attachment store for a smaller request version.
17
+ * @throws MediaError `reference-too-large`.
18
+ */
19
+ const readReference = async (store, block, maxBytes, signal) => {
20
+ const stored = await store.readImage(block.attachment, signal);
21
+ const image = { mediaType: stored.ref?.mediaType ?? block.attachment?.mediaType, data: stored.data };
22
+ const fitted = fitReferenceImage(image, maxBytes);
23
+ if (fitted !== undefined) return { mediaType: fitted.mediaType, data: fitted.data };
24
+ const width = Number(stored.ref?.width ?? block.attachment?.width);
25
+ const height = Number(stored.ref?.height ?? block.attachment?.height);
26
+ if (typeof store.readImageRequest === "function" && width > 0 && height > 0) {
27
+ const scale = Math.min(1, REFERENCE_MAX_EDGE / Math.max(width, height));
28
+ try {
29
+ const smaller = await store.readImageRequest(
30
+ block.attachment,
31
+ { width: Math.max(1, Math.round(width * scale)), height: Math.max(1, Math.round(height * scale)), maxBytes },
32
+ signal
33
+ );
34
+ if (smaller?.data?.byteLength <= maxBytes) return { mediaType: smaller.mediaType, data: smaller.data };
35
+ } catch (error) {
36
+ if (signal?.aborted) throw error;
37
+ }
38
+ }
39
+ throw new MediaError("reference-too-large", String(Math.round(maxBytes / 1000)));
40
+ };
41
+
42
+ return { FIRST_FRAME_MAX_BYTES, REFERENCES_MAX_BYTES, readReference };
43
+ }
@@ -0,0 +1,104 @@
1
+ // Activation job tracking, session-status heartbeat and generation run recording.
2
+
3
+ import { basename, isAbsolute, relative, sep } from "node:path";
4
+ import { MediaError } from "../../media/index.js";
5
+
6
+ export function createSessionStatus(shared) {
7
+ const { ctx, mediaSessions } = shared;
8
+ // #region Background generation jobs and the session's "running" state
9
+ //
10
+ // `/cpa-image` returns as soon as the request is admitted, so the composer
11
+ // clears the draft at once; the generation continues here and its chat row
12
+ // follows it through `media/command`. DSH counts only agent turns as
13
+ // running, so while a job runs this plugin publishes the host's own
14
+ // `api-session/status` event for that session (the workspace list shows it
15
+ // as running, then completed). It is republished every few seconds and
16
+ // after the agent itself goes idle, because those reset it to idle.
17
+
18
+ const mediaJobs = new Map(); // job key → { sessionId, abort }
19
+ // job key → the promise of a job of this activation, until it settles (the agent tool waits on it).
20
+ const jobPromises = new Map();
21
+ const settleWith = (key, promise) => {
22
+ jobPromises.set(key, promise);
23
+ void promise.finally(() => {
24
+ if (jobPromises.get(key) === promise) jobPromises.delete(key);
25
+ });
26
+ return promise;
27
+ };
28
+ shared.mediaJobSeq = 0;
29
+ let mediaHeartbeat;
30
+ const MEDIA_HEARTBEAT_MS = 5000;
31
+ const jobSessions = () => new Set([...mediaJobs.values()].map((job) => job.sessionId).filter(Boolean));
32
+ const agentRunning = (sessionId) => {
33
+ try {
34
+ return ctx.get?.("agents")?.get?.(sessionId)?.status === "running";
35
+ } catch {
36
+ return false;
37
+ }
38
+ };
39
+ const publishSessionStatus = (sessionId, running) => {
40
+ if (typeof ctx.emit !== "function") return;
41
+ try {
42
+ ctx.emit("api-session/status", sessionId, running);
43
+ } catch (error) {
44
+ ctx.logger?.warn?.(`cpa: could not publish the session status: ${error instanceof Error ? error.message : String(error)}`);
45
+ }
46
+ };
47
+ const settleSessionStatus = (sessionId) => {
48
+ if (sessionId === undefined || jobSessions().has(sessionId) || agentRunning(sessionId)) return;
49
+ publishSessionStatus(sessionId, false);
50
+ };
51
+ const syncHeartbeat = () => {
52
+ if (mediaJobs.size > 0 && mediaHeartbeat === undefined) {
53
+ mediaHeartbeat = setInterval(() => {
54
+ for (const sessionId of jobSessions()) publishSessionStatus(sessionId, true);
55
+ }, MEDIA_HEARTBEAT_MS);
56
+ mediaHeartbeat.unref?.();
57
+ } else if (mediaJobs.size === 0 && mediaHeartbeat !== undefined) {
58
+ clearInterval(mediaHeartbeat);
59
+ mediaHeartbeat = undefined;
60
+ }
61
+ };
62
+ ctx.on?.("agent/status", (event) => {
63
+ const sessionId = event?.agent?.id;
64
+ if (event?.status === "running" || !jobSessions().has(sessionId)) return;
65
+ // After the host's own idle notice for this transition.
66
+ setTimeout(() => {
67
+ if (jobSessions().has(sessionId)) publishSessionStatus(sessionId, true);
68
+ }, 0);
69
+ });
70
+ ctx.effect(
71
+ () => () => {
72
+ const sessions = jobSessions();
73
+ for (const job of mediaJobs.values()) job.abort.abort(new MediaError("interrupted"));
74
+ mediaJobs.clear();
75
+ syncHeartbeat();
76
+ for (const sessionId of sessions) settleSessionStatus(sessionId);
77
+ },
78
+ "cpa: generation jobs"
79
+ );
80
+
81
+ /** Update one run record (and prepend new recent outputs) in the session store; never throws. */
82
+ const recordRun = (sessionId, commandId, patch, recent = []) => {
83
+ if (sessionId === undefined) return;
84
+ try {
85
+ mediaSessions.update(sessionId, (entry) => {
86
+ if (recent.length > 0) entry.recent = [...recent, ...entry.recent];
87
+ if (commandId === undefined) return;
88
+ const index = entry.runs.findIndex((run) => run.commandId === commandId);
89
+ if (index >= 0) entry.runs[index] = { ...entry.runs[index], ...patch };
90
+ else entry.runs = [{ commandId, ...patch }, ...entry.runs];
91
+ });
92
+ } catch (error) {
93
+ ctx.logger?.warn?.(`cpa: could not record a generation result: ${error instanceof Error ? error.message : String(error)}`);
94
+ }
95
+ };
96
+
97
+ /** How the chat row names the save folder: relative to the working directory when inside it. */
98
+ const folderLabel = (cwd, dir) => {
99
+ const rel = relative(cwd, dir);
100
+ return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel) ? rel.split(sep).join("/") : basename(dir);
101
+ };
102
+
103
+ return { mediaJobs, jobPromises, settleWith, publishSessionStatus, settleSessionStatus, syncHeartbeat, recordRun, folderLabel };
104
+ }
@@ -0,0 +1,78 @@
1
+ // Account state migration, curated-route seeding and fresh state access.
2
+
3
+ import { normalizeState, mergeHostConfig } from "../accounts.js";
4
+ import { PI_AI_NS, DEFAULT_MODEL_NS } from "../config.js";
5
+ import { managedRouteKeys } from "../routes.js";
6
+ import { resolveStateFilePath, loadPersistedState, savePersistedState } from "../state-store.js";
7
+
8
+ export function createState(shared) {
9
+ const { ctx, config } = shared;
10
+ const stateFilePath = resolveStateFilePath(ctx);
11
+ const persisted = loadPersistedState(stateFilePath);
12
+
13
+ const readExistingPiAiRoute = (provider) => {
14
+ try {
15
+ const descriptors = ctx.settings.describe();
16
+ const piAi = descriptors.find((d) => d.ns === PI_AI_NS);
17
+ return piAi?.value?.providers?.[provider];
18
+ } catch {
19
+ return undefined;
20
+ }
21
+ };
22
+
23
+ const readDefaultModel = () => {
24
+ try {
25
+ const value = ctx.settings.describe().find((d) => d.ns === DEFAULT_MODEL_NS)?.value;
26
+ return typeof value?.provider === "string" && typeof value?.model === "string"
27
+ ? { provider: value.provider, model: value.model }
28
+ : undefined;
29
+ } catch {
30
+ return undefined;
31
+ }
32
+ };
33
+
34
+ // Seed a selection from every managed route, not just the base one, so a
35
+ // restart after protocol routing keeps Messages / Responses models checked.
36
+ const existingManagedIds = (provider) =>
37
+ managedRouteKeys(provider).flatMap((key) => {
38
+ const route = readExistingPiAiRoute(key);
39
+ return Array.isArray(route?.models)
40
+ ? route.models.map((m) => m?.id).filter((id) => typeof id === "string" && id.trim() !== "")
41
+ : [];
42
+ });
43
+
44
+ // Migrate a legacy single-account file into `accounts[]`, keeping a copy.
45
+ if (Object.keys(persisted).length > 0 && !Array.isArray(persisted.accounts)) {
46
+ savePersistedState(stateFilePath.replace(/\.json$/, ".v1.json"), persisted);
47
+ }
48
+
49
+ // An account without a saved selection keeps the models already on its
50
+ // pi-ai routes when that is a curated list (<= 35). Otherwise it stays
51
+ // unset and its first successful probe seeds recommendModels().
52
+ let memState = normalizeState(mergeHostConfig(persisted, config));
53
+ memState = {
54
+ ...memState,
55
+ accounts: memState.accounts.map((account) => {
56
+ if (account.selectedModels !== undefined) return account;
57
+ const existingIds = existingManagedIds(account.provider);
58
+ return existingIds.length > 0 && existingIds.length <= 35
59
+ ? { ...account, selectedModels: existingIds }
60
+ : account;
61
+ }),
62
+ };
63
+ savePersistedState(stateFilePath, memState);
64
+
65
+ // The state file is re-read on every access so hand edits take effect.
66
+ const readState = () => {
67
+ const fresh = loadPersistedState(stateFilePath);
68
+ if (Object.keys(fresh).length > 0) memState = normalizeState(fresh);
69
+ return memState;
70
+ };
71
+ const writeState = (state) => {
72
+ memState = state;
73
+ savePersistedState(stateFilePath, state);
74
+ };
75
+
76
+
77
+ return { stateFilePath, readExistingPiAiRoute, readDefaultModel, readState, writeState };
78
+ }
@@ -0,0 +1,55 @@
1
+ // Independent update preferences, updater wiring and disposed startup/periodic checks.
2
+
3
+ import { readFileSync } from "node:fs";
4
+ import { dirname, join } from "node:path";
5
+ import { createPluginUpdater, loadUpdateSettings, saveUpdateSettings, storeUpdatePackage, UPDATE_INTERVAL_MS } from "../../updater/index.js";
6
+ import { outsideHmrTransaction } from "../apply-utils.js";
7
+ import { PACKAGE_NAME, getEnvValue } from "../config.js";
8
+ import { loadPersistedState } from "../state-store.js";
9
+
10
+ export function createUpdates(shared) {
11
+ const { ctx, stateFilePath } = shared;
12
+ // Update preferences and packages never share the account state or credentials.
13
+ const updateDirectory = dirname(stateFilePath);
14
+ const updateSettingsPath = join(updateDirectory, "update-settings.json");
15
+ let profile;
16
+ try { profile = ctx.get?.("profileContext"); } catch {}
17
+ const source = typeof profile?.dir === "string"
18
+ ? loadPersistedState(join(profile.dir, "package.json")).dependencies?.[PACKAGE_NAME]
19
+ : undefined;
20
+ const linkedInstall = typeof source === "string" &&
21
+ (/^(?: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));
22
+ const currentVersion = JSON.parse(readFileSync(new URL("../../../package.json", import.meta.url), "utf8")).version;
23
+ let updateTimer;
24
+ let updateStartup;
25
+ const scheduleUpdates = (preferences) => {
26
+ clearInterval(updateTimer);
27
+ clearTimeout(updateStartup);
28
+ if (!preferences.autoCheck) return;
29
+ outsideHmrTransaction(ctx, () => {
30
+ const run = () => { void Promise.resolve().then(() => updater.check({ automatic: true })).catch(() => {}); };
31
+ updateStartup = setTimeout(run, 3000);
32
+ updateTimer = setInterval(run, UPDATE_INTERVAL_MS);
33
+ });
34
+ };
35
+ const updater = createPluginUpdater({
36
+ currentVersion,
37
+ linkedInstall,
38
+ profileName: profile?.name ?? profile?.profile ?? getEnvValue(ctx, "DSH_PROFILE"),
39
+ readSettings: () => loadUpdateSettings(updateSettingsPath),
40
+ writeSettings: (preferences) => saveUpdateSettings(updateSettingsPath, preferences),
41
+ storePackage: (version, bytes) => storeUpdatePackage(join(updateDirectory, "updates"), version, bytes),
42
+ onSettingsChanged: scheduleUpdates,
43
+ logger: ctx.logger,
44
+ });
45
+ ctx.effect(() => {
46
+ scheduleUpdates(updater.status());
47
+ return () => {
48
+ clearInterval(updateTimer);
49
+ clearTimeout(updateStartup);
50
+ updater.dispose();
51
+ };
52
+ }, "cpa: plugin update checks");
53
+
54
+ return { updateDirectory, updater };
55
+ }
@@ -0,0 +1,152 @@
1
+ // Video generation admission, frame modes and interpreted submission parameters.
2
+
3
+ import { existsSync } from "node:fs";
4
+ import { isAbsolute } from "node:path";
5
+ import { chooseMediaModel, MediaError, outputDirectory, validCommandId, validSessionId, parseVideoCommand, submitVideo, videoImageBudget } from "../../media/index.js";
6
+
7
+ export function createVideoCommand(shared) {
8
+ const { ctx, controller, mediaSessions, mediaChoicesFor, mediaAccountsSummary, FIRST_FRAME_MAX_BYTES, readReference, interpretParams, interpretedRecord, mediaFailureText, recordRun, videoLive, startVideoJob } = shared;
9
+ /** Grok's 1.5 video family takes more references and pinned first / last frames. */
10
+ const isVideo15 = (modelId) => /(^|\/)grok-imagine-video-1\.5/i.test(String(modelId ?? ""));
11
+
12
+ /**
13
+ * How attached images are used: none = text only; one = first frame
14
+ * (`--ref`: a reference, `--last`: the last frame); two = references
15
+ * (`--first-last` / `--last`: first and last frame); more = references.
16
+ * @throws MediaError `frames-need-images` / `too-many-references` / `needs-video-15`.
17
+ */
18
+ const videoMode = (count, frames, modelId) => {
19
+ if (count === 0) {
20
+ if (frames !== undefined) throw new MediaError("frames-need-images");
21
+ return "text";
22
+ }
23
+ let mode;
24
+ if (count === 1) mode = frames === "reference" ? "reference" : frames === "last" ? "last-frame" : frames === "first-last" ? undefined : "first-frame";
25
+ else if (count === 2) mode = frames === "first-last" || frames === "last" ? "first-last" : "reference";
26
+ else mode = frames === undefined || frames === "reference" ? "reference" : undefined;
27
+ if (mode === undefined) throw new MediaError(count === 1 ? "frames-need-images" : "too-many-references", count === 1 ? "2" : "2");
28
+ const maxRefs = isVideo15(modelId) ? 14 : 7;
29
+ if (mode === "reference" && count > maxRefs) throw new MediaError("too-many-references", String(maxRefs));
30
+ if ((mode === "first-last" || mode === "last-frame") && !isVideo15(modelId)) throw new MediaError("needs-video-15");
31
+ return mode;
32
+ };
33
+
34
+ /**
35
+ * `/cpa-video`: submit a video — text only, from a first frame, from
36
+ * reference images, or between pinned first / last frames — and return at
37
+ * once; the chat row follows the background job.
38
+ */
39
+ const runVideoCommand = async ({ agent, rawInput, attachments = [], signal, commandId, literal = false, pickSession, gate }) => {
40
+ try {
41
+ const parsed = parseVideoCommand(rawInput, { literal });
42
+ const blocks = Array.isArray(attachments) ? attachments : [];
43
+ if (blocks.some((block) => block?.type !== "image")) throw new MediaError("unsupported-attachment");
44
+ if (blocks.length > 14) throw new MediaError("too-many-references", "14");
45
+ const { settings, options, accounts } = await mediaChoicesFor("video");
46
+ const sessionId = validSessionId(agent?.id);
47
+ const pickId = validSessionId(pickSession) ?? sessionId;
48
+ const entry = pickId === undefined ? {} : mediaSessions.get(pickId);
49
+ let option;
50
+ try {
51
+ ({ option } = chooseMediaModel({ options: options.video, requested: parsed.model, sessionRef: entry.video, lastRef: settings.lastSelection.video }));
52
+ } catch (error) {
53
+ if (error instanceof MediaError && error.code === "no-media-model") throw new MediaError("no-media-model", mediaAccountsSummary(accounts, "video"));
54
+ throw error;
55
+ }
56
+ const mode = videoMode(blocks.length, parsed.frames, option.modelId);
57
+ const workingDirectory = ctx.get?.("workingDirectory");
58
+ const cwd = agent !== undefined && workingDirectory !== undefined ? await workingDirectory.ensure(agent, signal) : undefined;
59
+ if (typeof cwd !== "string" || !isAbsolute(cwd) || !existsSync(cwd)) throw new MediaError("workspace-not-local");
60
+ const images = [];
61
+ if (blocks.length > 0) {
62
+ const store = ctx.get?.("attachments");
63
+ if (store === undefined) throw new MediaError("unsupported-attachment");
64
+ // The generation route reads about 1 MiB of body (measured), so all images share that.
65
+ const each = Math.min(FIRST_FRAME_MAX_BYTES, videoImageBudget(parsed.prompt, blocks.length));
66
+ for (const block of blocks) images.push(await readReference(store, block, each, signal));
67
+ }
68
+ const framesFor = (m) =>
69
+ m === "first-frame" ? { image: images[0] }
70
+ : m === "last-frame" ? { lastFrame: images[0] }
71
+ : m === "first-last" ? { image: images[0], lastFrame: images[1] }
72
+ : m === "reference" ? { references: images }
73
+ : {};
74
+ const endpoint = await controller.mediaEndpoint(option.accountId);
75
+ const args = { ...parsed, seconds: parsed.seconds ?? settings.defaultVideoSeconds, resolution: parsed.resolution ?? settings.defaultVideoResolution };
76
+ const id = validCommandId(commandId);
77
+ if (gate !== undefined) {
78
+ 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 });
79
+ }
80
+ recordRun(sessionId, id, {
81
+ kind: "video",
82
+ status: "running",
83
+ mode,
84
+ model: option.ref,
85
+ modelName: option.name || option.modelId,
86
+ accountName: option.accountName,
87
+ prompt: args.prompt,
88
+ ...(args.aspect === undefined ? {} : { aspect: args.aspect }),
89
+ seconds: args.seconds,
90
+ resolution: args.resolution,
91
+ count: 1,
92
+ references: images.length,
93
+ files: 0,
94
+ startedAt: new Date().toISOString(),
95
+ });
96
+ void startVideoJob({
97
+ sessionId,
98
+ commandId: id,
99
+ accountId: option.accountId,
100
+ modelId: option.modelId,
101
+ ref: option.ref,
102
+ prompt: args.prompt,
103
+ seconds: args.seconds,
104
+ cwd,
105
+ outputDir: settings.outputDir,
106
+ label: "/cpa-video",
107
+ submit: async (jobSignal) => {
108
+ // The session's chat model reads what the rules did not (explicit options and recognized words win).
109
+ if (id !== undefined && !literal) videoLive.set(id, { phase: "interpreting" });
110
+ const fixed = ["seconds", "resolution", "aspect", "frames"].filter((k) => parsed[k] !== undefined);
111
+ const result = literal ? undefined : await interpretParams({ agent, command: "video", text: parsed.prompt, images: images.length, fixed, signal: jobSignal });
112
+ let final = args;
113
+ let finalMode = mode;
114
+ if (result !== undefined) {
115
+ final = { ...args, ...result.values, prompt: result.prompt };
116
+ if (result.values.frames !== undefined) finalMode = videoMode(images.length, result.values.frames, option.modelId);
117
+ recordRun(sessionId, id, {
118
+ mode: finalMode,
119
+ prompt: final.prompt,
120
+ ...(final.aspect === undefined ? {} : { aspect: final.aspect }),
121
+ seconds: final.seconds,
122
+ resolution: final.resolution,
123
+ ...interpretedRecord(result, parsed.prompt),
124
+ });
125
+ }
126
+ if (id !== undefined) videoLive.set(id, { phase: "submitting" });
127
+ const submitted = await submitVideo({
128
+ rootURL: endpoint.rootURL,
129
+ apiKey: endpoint.apiKey,
130
+ model: option.modelId,
131
+ prompt: final.prompt,
132
+ seconds: final.seconds,
133
+ ...(final.aspect === undefined ? {} : { aspect: final.aspect }),
134
+ resolution: final.resolution,
135
+ ...framesFor(finalMode),
136
+ signal: jobSignal,
137
+ });
138
+ return { ...submitted, prompt: final.prompt, seconds: final.seconds };
139
+ },
140
+ });
141
+ return {
142
+ kind: "success",
143
+ 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)}`,
144
+ };
145
+ } catch (error) {
146
+ if (signal?.aborted || error?.gateError === true) throw error;
147
+ return { kind: "error", text: `/cpa-video: ${mediaFailureText(error, "video")}` };
148
+ }
149
+ };
150
+
151
+ return { runVideoCommand };
152
+ }