ima2-gen 1.1.21 → 1.1.23

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 (214) hide show
  1. package/README.md +44 -7
  2. package/bin/commands/video.js +14 -0
  3. package/bin/ima2.js +14 -4
  4. package/bin/lib/platform.js +34 -5
  5. package/docs/README.ko.md +43 -2
  6. package/lib/agentQueueWorker.js +6 -0
  7. package/lib/agentRuntime.js +3 -2
  8. package/lib/atomicWrite.js +14 -0
  9. package/lib/grokImageAdapter.js +6 -0
  10. package/lib/grokProxyLauncher.js +5 -3
  11. package/lib/grokVideoAdapter.js +1 -1
  12. package/lib/grokVideoPlannerPrompt.js +10 -0
  13. package/lib/inflight.js +1 -1
  14. package/lib/oauthLauncher.js +5 -0
  15. package/lib/videoFrameExtract.js +3 -3
  16. package/package.json +5 -7
  17. package/routes/capabilities.js +13 -0
  18. package/routes/edit.js +2 -1
  19. package/routes/generate.js +32 -6
  20. package/routes/health.js +4 -3
  21. package/routes/multimode.js +2 -1
  22. package/routes/video.js +35 -3
  23. package/server.js +29 -2
  24. package/skills/ima2/SKILL.md +48 -6
  25. package/ui/dist/.vite/manifest.json +12 -12
  26. package/ui/dist/assets/{AgentWorkspace-B_hq9CLg.js → AgentWorkspace-C21zqdTZ.js} +1 -1
  27. package/ui/dist/assets/{CardNewsWorkspace-wD12J7qk.js → CardNewsWorkspace-BN-ga1lG.js} +1 -1
  28. package/ui/dist/assets/{NodeCanvas-CI_wuPMf.js → NodeCanvas-BbMa4IhI.js} +1 -1
  29. package/ui/dist/assets/{PromptBuilderPanel-CUTujJUV.js → PromptBuilderPanel-DRwBJRDQ.js} +1 -1
  30. package/ui/dist/assets/{PromptImportDialog-CUi66jPK.js → PromptImportDialog-Dp85kHCq.js} +2 -2
  31. package/ui/dist/assets/{PromptImportDiscoverySection-Cm3vrjY4.js → PromptImportDiscoverySection-BE8Q8MLD.js} +1 -1
  32. package/ui/dist/assets/{PromptImportFolderSection-DOtWTD9n.js → PromptImportFolderSection-PtH5x0sc.js} +1 -1
  33. package/ui/dist/assets/{PromptLibraryPanel-BMjQegRa.js → PromptLibraryPanel-FnM9tHI9.js} +2 -2
  34. package/ui/dist/assets/SettingsWorkspace-MARPGyBL.js +1 -0
  35. package/ui/dist/assets/index-BAFI6htx.js +42 -0
  36. package/ui/dist/assets/{index-31uVIdt4.js → index-BSXxr_Bt.js} +1 -1
  37. package/ui/dist/assets/index-DS-ADE7U.css +1 -0
  38. package/ui/dist/index.html +2 -2
  39. package/bin/commands/annotate.ts +0 -119
  40. package/bin/commands/cancel.ts +0 -48
  41. package/bin/commands/canvas-versions.ts +0 -80
  42. package/bin/commands/capabilities.ts +0 -110
  43. package/bin/commands/cardnews.ts +0 -249
  44. package/bin/commands/comfy.ts +0 -54
  45. package/bin/commands/config.ts +0 -186
  46. package/bin/commands/defaults.ts +0 -192
  47. package/bin/commands/doctor.ts +0 -202
  48. package/bin/commands/edit.ts +0 -150
  49. package/bin/commands/gen.ts +0 -214
  50. package/bin/commands/grok.ts +0 -90
  51. package/bin/commands/history.ts +0 -146
  52. package/bin/commands/ls.ts +0 -64
  53. package/bin/commands/metadata.ts +0 -39
  54. package/bin/commands/multimode.ts +0 -196
  55. package/bin/commands/node.ts +0 -166
  56. package/bin/commands/observability.ts +0 -176
  57. package/bin/commands/ping.ts +0 -31
  58. package/bin/commands/prompt-sub/build.ts +0 -101
  59. package/bin/commands/prompt.ts +0 -492
  60. package/bin/commands/ps.ts +0 -81
  61. package/bin/commands/session.ts +0 -266
  62. package/bin/commands/show.ts +0 -72
  63. package/bin/commands/skill.ts +0 -70
  64. package/bin/commands/video.ts +0 -442
  65. package/bin/ima2.ts +0 -430
  66. package/bin/lib/args.ts +0 -92
  67. package/bin/lib/browser-id.ts +0 -16
  68. package/bin/lib/client.ts +0 -122
  69. package/bin/lib/config-store.ts +0 -120
  70. package/bin/lib/destructive-confirm.ts +0 -19
  71. package/bin/lib/doctor-checks.ts +0 -91
  72. package/bin/lib/error-hints.ts +0 -23
  73. package/bin/lib/files.ts +0 -39
  74. package/bin/lib/output.ts +0 -73
  75. package/bin/lib/platform.ts +0 -99
  76. package/bin/lib/recover-output.ts +0 -139
  77. package/bin/lib/sse.ts +0 -73
  78. package/bin/lib/star-prompt.ts +0 -97
  79. package/bin/lib/storage-doctor.ts +0 -39
  80. package/bin/lib/ui-build.ts +0 -85
  81. package/config.ts +0 -354
  82. package/lib/agentCommandParser.ts +0 -69
  83. package/lib/agentGenerationPlanner.ts +0 -273
  84. package/lib/agentQuestionResponder.ts +0 -266
  85. package/lib/agentQueueStore.ts +0 -270
  86. package/lib/agentQueueWorker.ts +0 -89
  87. package/lib/agentRuntime.ts +0 -604
  88. package/lib/agentSettings.ts +0 -72
  89. package/lib/agentStore.ts +0 -422
  90. package/lib/agentStoreRows.ts +0 -136
  91. package/lib/agentTypes.ts +0 -154
  92. package/lib/apiCachePolicy.ts +0 -11
  93. package/lib/assetLifecycle.ts +0 -146
  94. package/lib/canvasVersionStore.ts +0 -223
  95. package/lib/capabilities.ts +0 -126
  96. package/lib/cardNewsGenerator.ts +0 -271
  97. package/lib/cardNewsJobStore.ts +0 -142
  98. package/lib/cardNewsManifestStore.ts +0 -154
  99. package/lib/cardNewsPlanner.ts +0 -236
  100. package/lib/cardNewsPlannerClient.ts +0 -155
  101. package/lib/cardNewsPlannerPrompt.ts +0 -62
  102. package/lib/cardNewsPlannerSchema.ts +0 -321
  103. package/lib/cardNewsRoleTemplateStore.ts +0 -47
  104. package/lib/cardNewsTemplateStore.ts +0 -252
  105. package/lib/codexDetect.ts +0 -71
  106. package/lib/comfyBridge.ts +0 -235
  107. package/lib/composerSnapshot.ts +0 -33
  108. package/lib/configKeys.ts +0 -62
  109. package/lib/db.ts +0 -295
  110. package/lib/errInfo.ts +0 -43
  111. package/lib/errorClassify.ts +0 -100
  112. package/lib/generationCancel.ts +0 -28
  113. package/lib/generationErrors.ts +0 -238
  114. package/lib/grokImageAdapter.ts +0 -513
  115. package/lib/grokMultimodeAdapter.ts +0 -84
  116. package/lib/grokProxyLauncher.ts +0 -153
  117. package/lib/grokRuntime.ts +0 -23
  118. package/lib/grokSizeMapper.ts +0 -71
  119. package/lib/grokVideoAdapter.ts +0 -458
  120. package/lib/grokVideoCanvas.ts +0 -26
  121. package/lib/grokVideoDownload.ts +0 -59
  122. package/lib/grokVideoPlannerPrompt.ts +0 -67
  123. package/lib/historyIndex.ts +0 -51
  124. package/lib/historyList.ts +0 -181
  125. package/lib/imageMetadata.ts +0 -113
  126. package/lib/imageMetadataStore.ts +0 -67
  127. package/lib/imageModels.ts +0 -165
  128. package/lib/inflight.ts +0 -281
  129. package/lib/localImportStore.ts +0 -114
  130. package/lib/logger.ts +0 -161
  131. package/lib/nodeStore.ts +0 -91
  132. package/lib/oauthLauncher.ts +0 -94
  133. package/lib/oauthNormalize.ts +0 -30
  134. package/lib/oauthProxy/errors.ts +0 -128
  135. package/lib/oauthProxy/generators.ts +0 -494
  136. package/lib/oauthProxy/index.ts +0 -28
  137. package/lib/oauthProxy/prompts.ts +0 -123
  138. package/lib/oauthProxy/references.ts +0 -45
  139. package/lib/oauthProxy/runtime.ts +0 -115
  140. package/lib/oauthProxy/streams.ts +0 -232
  141. package/lib/oauthProxy/types.ts +0 -9
  142. package/lib/oauthProxy.ts +0 -3
  143. package/lib/openDirectory.ts +0 -47
  144. package/lib/pngInfo.ts +0 -26
  145. package/lib/promptBuilder/attachments.ts +0 -74
  146. package/lib/promptBuilder/client.ts +0 -130
  147. package/lib/promptBuilder/constants.ts +0 -9
  148. package/lib/promptBuilder/context.ts +0 -36
  149. package/lib/promptBuilder/errors.ts +0 -12
  150. package/lib/promptBuilder/requestSchema.ts +0 -56
  151. package/lib/promptBuilder/responseParser.ts +0 -219
  152. package/lib/promptBuilder/systemPrompt.ts +0 -135
  153. package/lib/promptBuilder/transport.ts +0 -94
  154. package/lib/promptBuilder/types.ts +0 -109
  155. package/lib/promptImport/curatedSources.ts +0 -141
  156. package/lib/promptImport/discoveryRegistry.ts +0 -329
  157. package/lib/promptImport/errors.ts +0 -18
  158. package/lib/promptImport/githubDiscovery.ts +0 -309
  159. package/lib/promptImport/githubFolder.ts +0 -397
  160. package/lib/promptImport/githubSource.ts +0 -257
  161. package/lib/promptImport/gptImageHints.ts +0 -70
  162. package/lib/promptImport/parsePromptCandidates.ts +0 -179
  163. package/lib/promptImport/promptIndex.ts +0 -326
  164. package/lib/promptImport/rankPromptCandidates.ts +0 -65
  165. package/lib/promptImport/types.ts +0 -103
  166. package/lib/promptSafetyPolicy.ts +0 -5
  167. package/lib/providerOptions.ts +0 -56
  168. package/lib/referenceImageCompress.ts +0 -84
  169. package/lib/refs.ts +0 -133
  170. package/lib/requestLogger.ts +0 -49
  171. package/lib/responsesDoctor.ts +0 -456
  172. package/lib/responsesErrors.ts +0 -83
  173. package/lib/responsesFallback.ts +0 -114
  174. package/lib/responsesImageAdapter.ts +0 -466
  175. package/lib/responsesParse.ts +0 -452
  176. package/lib/responsesTools.ts +0 -28
  177. package/lib/runtimeContext.ts +0 -146
  178. package/lib/runtimePorts.ts +0 -105
  179. package/lib/sessionStore.ts +0 -308
  180. package/lib/storageMigration.ts +0 -310
  181. package/lib/styleSheet.ts +0 -139
  182. package/lib/systemTrash.ts +0 -20
  183. package/lib/videoContinuity.ts +0 -180
  184. package/lib/videoFrameExtract.ts +0 -78
  185. package/lib/videoSeriesChain.ts +0 -29
  186. package/lib/visibleTextLanguagePolicy.ts +0 -7
  187. package/routes/agent.ts +0 -308
  188. package/routes/annotations.ts +0 -118
  189. package/routes/canvasVersions.ts +0 -69
  190. package/routes/capabilities.ts +0 -18
  191. package/routes/cardNews.ts +0 -211
  192. package/routes/comfy.ts +0 -43
  193. package/routes/edit.ts +0 -352
  194. package/routes/generate.ts +0 -492
  195. package/routes/grok.ts +0 -24
  196. package/routes/health.ts +0 -123
  197. package/routes/history.ts +0 -221
  198. package/routes/imageImport.ts +0 -37
  199. package/routes/index.ts +0 -52
  200. package/routes/metadata.ts +0 -77
  201. package/routes/multimode.ts +0 -499
  202. package/routes/nodes.ts +0 -578
  203. package/routes/promptBuilder.ts +0 -37
  204. package/routes/promptImport.ts +0 -379
  205. package/routes/prompts.ts +0 -428
  206. package/routes/quota.ts +0 -89
  207. package/routes/sessions.ts +0 -317
  208. package/routes/storage.ts +0 -47
  209. package/routes/video.ts +0 -300
  210. package/routes/videoExtended.ts +0 -284
  211. package/server.ts +0 -293
  212. package/ui/dist/assets/SettingsWorkspace-PiaVnsdA.js +0 -1
  213. package/ui/dist/assets/index-CjgnNtgt.css +0 -1
  214. package/ui/dist/assets/index-Da2s4_-5.js +0 -36
@@ -1,442 +0,0 @@
1
- import { parseArgs } from "../lib/args.js";
2
- import { resolveServer } from "../lib/client.js";
3
- import { streamSse } from "../lib/sse.js";
4
- import { out, die, color, json, exitCodeForError } from "../lib/output.js";
5
- import { config } from "../../config.js";
6
- import { readFile, writeFile, mkdir } from "node:fs/promises";
7
- import { basename, dirname, join } from "node:path";
8
-
9
- const VALID_RESOLUTIONS = new Set(["480p", "720p"]);
10
- const VALID_ASPECT_RATIOS = new Set(["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3", "auto"]);
11
- const VALID_MODELS = new Set(["grok-imagine-video", "grok-imagine-video-1.5-preview"]);
12
- const ACTIVE_VIDEO_PROMPT_GUIDANCE = "Active video prompt required: describe visual flow, motion flow, sound/no-music intent, dialogue/no-dialogue intent, and the desired ending frame. Pace the scene to naturally fill the selected duration with an opening composition, connected motion/emotion change, and stable ending frame.";
13
-
14
- function parseIntegerFlag(value: unknown, fallback: number, label: string): number {
15
- const raw = value === undefined ? String(fallback) : String(value);
16
- if (!/^\d+$/.test(raw)) die(2, `${label} must be an integer`);
17
- return Number(raw);
18
- }
19
-
20
- function rejectUnknownFlags(args: { _unknown?: string[] }): void {
21
- if (args._unknown?.length) die(2, `unknown option: ${args._unknown[0]}`);
22
- }
23
-
24
- async function readJsonResponse(res: Response, label: string): Promise<Record<string, unknown>> {
25
- const text = await res.text();
26
- try {
27
- return text ? JSON.parse(text) as Record<string, unknown> : {};
28
- } catch {
29
- die(1, `${label} failed: expected JSON response, got ${text.slice(0, 120) || `HTTP ${res.status}`}`);
30
- }
31
- }
32
-
33
- function parseTimeoutSeconds(seconds: unknown): number {
34
- const sec = parseIntegerFlag(seconds, 600, "--timeout");
35
- if (sec < 1) die(2, "--timeout must be at least 1");
36
- return sec;
37
- }
38
-
39
- function timeoutSignal(seconds: unknown): AbortSignal {
40
- const sec = parseTimeoutSeconds(seconds);
41
- return AbortSignal.timeout(sec * 1000);
42
- }
43
-
44
- async function writeBuffer(path: string, buf: Buffer): Promise<void> {
45
- await mkdir(dirname(path), { recursive: true }).catch(() => {});
46
- await writeFile(path, buf);
47
- }
48
-
49
- async function downloadReturnedVideo(serverBase: string, data: Record<string, unknown>, outPath: string, signal: AbortSignal): Promise<void> {
50
- const rawUrl = typeof data.url === "string" ? data.url : "";
51
- const url = rawUrl.startsWith("/") ? `${serverBase}${rawUrl}` : rawUrl;
52
- if (!url) die(1, "server did not return a video url");
53
- const res = await fetch(url, { signal });
54
- if (!res.ok) die(1, `failed to download video: HTTP ${res.status}`);
55
- await writeBuffer(outPath, Buffer.from(await res.arrayBuffer()));
56
- }
57
-
58
- const SPEC = {
59
- flags: {
60
- duration: { type: "string", default: "5" },
61
- resolution: { type: "string", default: "480p" },
62
- "aspect-ratio": { type: "string", default: "auto" },
63
- model: { type: "string" },
64
- topic: { type: "string" },
65
- ref: { type: "string", repeatable: true },
66
- out: { short: "o", type: "string" },
67
- "out-dir": { short: "d", type: "string" },
68
- json: { type: "boolean" },
69
- timeout: { type: "string", default: "600" },
70
- server: { type: "string" },
71
- session: { type: "string" },
72
- help: { short: "h", type: "boolean" },
73
- },
74
- };
75
-
76
- const HELP = `
77
- ima2 video <prompt...> [options]
78
- ima2 video edit <prompt> --video <url|file_id|generated-file>
79
- ima2 video extend <prompt> --video <url|file_id|generated-file> [--duration 6]
80
- ima2 video continue <prompt> --video <generated-file>
81
- ima2 video frame <file> [--last] [-o output.png]
82
- ima2 video analyze <generated-file>
83
-
84
- Generate, edit, extend, or analyze video via Grok.
85
-
86
- Subcommands:
87
- (default) Generate video (T2V / I2V / Ref2V)
88
- edit Edit existing video with text prompt (V2V)
89
- extend Continue video from last frame
90
- continue Generate a new I2V clip from a generated video's last frame with lineage
91
- frame Extract a frame from video (requires ffmpeg on server)
92
- analyze Analyze video with Grok 4.3 vision
93
-
94
- Options (generate mode):
95
- --duration <1..15> Duration in seconds. Default: 5. Prompt motion should naturally fill this length
96
- --resolution <480p|720p> Default: 480p
97
- --aspect-ratio <ratio|auto> 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3, auto. Default: auto
98
- --model <name> grok-imagine-video, grok-imagine-video-1.5-preview
99
- --topic <text> Series topic for prompt chain continuity
100
- --ref <file> Attach source/reference image (repeatable, max 7)
101
- -o, --out <file> Output file path
102
- -d, --out-dir <dir> Output directory
103
- --json Print JSON result to stdout
104
- --timeout <sec> Default: 600
105
- --server <url> Override server URL
106
- --session <id> Session ID
107
-
108
- Edit/extend subcommands:
109
- --video <value> HTTPS URL, xAI file_id, data URL, or generated filename
110
- --duration <2..10> Extension duration only. Default: 6
111
-
112
- Modes (auto-detected from --ref count):
113
- 0 refs → text-to-video
114
- 1 ref → image-to-video
115
- 2-7 refs → reference-to-video (max 10s duration)
116
-
117
- Examples:
118
- ima2 video "a cat playing piano"
119
- ima2 video "animate this" --ref photo.png --duration 10
120
- ima2 video edit "make it sunset" --video https://vidgen.x.ai/.../clip.mp4
121
- ima2 video extend "camera pulls back" --video https://vidgen.x.ai/.../clip.mp4 --duration 5
122
- ima2 video continue "she turns back as the music cuts to room tone" --video 1780226256355_50252101.mp4
123
- ima2 video frame 1780226256355_50252101.mp4 --last -o lastframe.png
124
- ima2 video analyze 1780226256355_50252101.mp4
125
- `;
126
-
127
- export default async function videoCmd(argv: string[]) {
128
- const sub = argv[0];
129
- if (sub === "edit") return videoEditCmd(argv.slice(1));
130
- if (sub === "extend") return videoExtendCmd(argv.slice(1));
131
- if (sub === "continue") return videoContinueCmd(argv.slice(1));
132
- if (sub === "frame") return videoFrameCmd(argv.slice(1));
133
- if (sub === "analyze") return videoAnalyzeCmd(argv.slice(1));
134
-
135
- const args = parseArgs(argv, SPEC);
136
- rejectUnknownFlags(args);
137
- if (args.help) { out(HELP); return; }
138
-
139
- const prompt = args.positional.join(" ");
140
- if (!prompt.trim()) die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
141
-
142
- const duration = parseIntegerFlag(args.duration, 5, "--duration");
143
- if (duration < 1 || duration > 15) die(2, "--duration must be between 1 and 15");
144
-
145
- const resolution = String(args.resolution);
146
- if (!VALID_RESOLUTIONS.has(resolution)) die(2, "--resolution must be one of: 480p, 720p");
147
-
148
- const aspectRatio = String(args["aspect-ratio"]);
149
- if (!VALID_ASPECT_RATIOS.has(aspectRatio)) die(2, "--aspect-ratio must be one of: 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3, auto");
150
-
151
- if (args.model && !VALID_MODELS.has(String(args.model))) {
152
- die(2, "--model must be one of: grok-imagine-video, grok-imagine-video-1.5-preview");
153
- }
154
-
155
- const refs = (Array.isArray(args.ref) ? args.ref : []) as string[];
156
- if (refs.length > 7) die(2, "max 7 --ref attachments for video");
157
- if (refs.length >= 2 && duration > 10) die(2, "--duration must be between 1 and 10 when using 2 or more --ref attachments");
158
-
159
- const timeoutSeconds = parseIntegerFlag(args.timeout, 600, "--timeout");
160
- if (timeoutSeconds < 1) die(2, "--timeout must be at least 1");
161
- const timeoutMs = timeoutSeconds * 1000;
162
-
163
- let server;
164
- try { server = await resolveServer({ serverFlag: args.server }); }
165
- catch (e: unknown) { die(exitCodeForError(e), (e as Error).message); throw e; }
166
-
167
- const referenceImages = await Promise.all(refs.map(async (p: string) => {
168
- const buf = await readFile(p);
169
- return buf.toString("base64");
170
- }));
171
-
172
- const requestId = `req_cli_video_${Date.now().toString(36)}`;
173
-
174
- const body: Record<string, unknown> = {
175
- prompt,
176
- provider: "grok",
177
- duration,
178
- resolution,
179
- aspectRatio,
180
- requestId,
181
- };
182
- if (args.model) body.model = args.model;
183
- if (args.session) body.sessionId = args.session;
184
- if (args.topic) body.topic = args.topic;
185
- if (referenceImages.length === 1) {
186
- body.sourceImage = referenceImages[0];
187
- } else if (referenceImages.length > 1) {
188
- body.referenceImages = referenceImages;
189
- }
190
-
191
- const ac = new AbortController();
192
- let timedOut = false;
193
- const timeoutTimer = setTimeout(() => { timedOut = true; ac.abort(); }, timeoutMs);
194
- const onSig = () => { ac.abort(); process.exit(130); };
195
- process.once("SIGINT", onSig);
196
- process.once("SIGTERM", onSig);
197
-
198
- const url = `${server.base}/api/video/generate`;
199
- let doneData: Record<string, unknown> | null = null;
200
- let lastProgress = -1;
201
-
202
- try {
203
- for await (const ev of streamSse(url, { body, signal: ac.signal, headers: { "X-Request-Id": requestId } })) {
204
- switch (ev.event) {
205
- case "planning":
206
- if (!args.json) out(color.dim("[planning] preparing video generation..."));
207
- break;
208
- case "submitted":
209
- if (!args.json) out(color.dim(`[submitted] xai request: ${ev.data.xaiVideoRequestId || "..."}`));
210
- break;
211
- case "progress": {
212
- const pct = typeof ev.data.progress === "number" ? Math.round(ev.data.progress * 100) : null;
213
- if (pct !== null && pct !== lastProgress && !args.json) {
214
- const bar = renderBar(pct);
215
- process.stdout.write(`\r ${bar} ${pct}%`);
216
- lastProgress = pct;
217
- }
218
- break;
219
- }
220
- case "done":
221
- if (!args.json && lastProgress >= 0) process.stdout.write("\n");
222
- doneData = ev.data;
223
- break;
224
- case "error":
225
- if (!args.json && lastProgress >= 0) process.stdout.write("\n");
226
- die(1, `video error: ${ev.data.error || ev.data}${ev.data.guidance ? `\n${ev.data.guidance}` : ""}${ev.data.code ? ` (${ev.data.code})` : ""}`);
227
- }
228
- }
229
- } catch (e: unknown) {
230
- if ((e as Error).name === "AbortError" && !timedOut) return;
231
- if (!args.json && lastProgress >= 0) process.stdout.write("\n");
232
- die(exitCodeForError(e), (e as Error).message);
233
- } finally {
234
- clearTimeout(timeoutTimer);
235
- process.off("SIGINT", onSig);
236
- process.off("SIGTERM", onSig);
237
- }
238
-
239
- if (!doneData?.filename) die(1, "server did not return a video filename");
240
-
241
- // Determine output path
242
- const filename = String(doneData.filename);
243
- const explicitOut = args.out ? String(args.out) : null;
244
- const outDir = args["out-dir"] ? String(args["out-dir"]) : null;
245
- let target: string;
246
- if (explicitOut) {
247
- target = explicitOut;
248
- } else if (outDir) {
249
- target = join(outDir, filename);
250
- } else {
251
- target = join(config.storage.generatedDir, filename);
252
- }
253
-
254
- // Download the video file from server
255
- const videoUrl = `${server.base}${doneData.url || `/generated/${encodeURIComponent(filename)}`}`;
256
- const dlRes = await fetch(videoUrl, { signal: timeoutSignal(args.timeout) });
257
- if (!dlRes.ok) die(1, `failed to download video: HTTP ${dlRes.status}`);
258
- const videoBuf = Buffer.from(await dlRes.arrayBuffer());
259
- await writeBuffer(target, videoBuf);
260
-
261
- if (args.json) {
262
- json({
263
- ok: true,
264
- requestId: doneData.requestId,
265
- path: target,
266
- filename,
267
- elapsed: doneData.elapsed,
268
- video: doneData.video,
269
- revisedPrompt: doneData.revisedPrompt,
270
- });
271
- } else {
272
- out(color.green("✓ ") + target);
273
- if (doneData.elapsed) out(color.dim(`elapsed ${doneData.elapsed}s`));
274
- if (doneData.revisedPrompt) out(color.dim(`revised: ${String(doneData.revisedPrompt).slice(0, 80)}`));
275
- }
276
- }
277
-
278
- function renderBar(pct: number): string {
279
- const width = 20;
280
- const filled = Math.round((pct / 100) * width);
281
- return color.green("█".repeat(filled)) + color.dim("░".repeat(width - filled));
282
- }
283
-
284
- async function runVideoGenerateRequest(serverBase: string, body: Record<string, unknown>, timeout: unknown, silent: boolean): Promise<Record<string, unknown>> {
285
- let doneData: Record<string, unknown> | null = null;
286
- let lastProgress = -1;
287
- for await (const ev of streamSse(`${serverBase}/api/video/generate`, {
288
- body: { provider: "grok", ...body },
289
- signal: timeoutSignal(timeout),
290
- headers: typeof body.requestId === "string" ? { "X-Request-Id": body.requestId } : undefined,
291
- })) {
292
- if (ev.event === "progress") {
293
- const pct = typeof ev.data.progress === "number" ? Math.round(ev.data.progress * 100) : null;
294
- if (pct !== null && pct !== lastProgress && !silent) {
295
- process.stdout.write(`\r ${renderBar(pct)} ${pct}%`);
296
- lastProgress = pct;
297
- }
298
- } else if (ev.event === "done") {
299
- if (!silent && lastProgress >= 0) process.stdout.write("\n");
300
- doneData = ev.data;
301
- } else if (ev.event === "error") {
302
- if (!silent && lastProgress >= 0) process.stdout.write("\n");
303
- die(1, `video error: ${ev.data.error || ev.data}${ev.data.guidance ? `\n${ev.data.guidance}` : ""}${ev.data.code ? ` (${ev.data.code})` : ""}`);
304
- }
305
- }
306
- if (!doneData) die(1, "server did not return a video result");
307
- return doneData;
308
- }
309
-
310
- // --- Subcommands ---
311
-
312
- async function videoEditCmd(argv: string[]) {
313
- const spec = { flags: { video: { type: "string" }, out: { short: "o", type: "string" }, output: { type: "string" }, json: { type: "boolean" }, timeout: { type: "string", default: "600" }, server: { type: "string" }, help: { short: "h", type: "boolean" } } };
314
- const args = parseArgs(argv, spec);
315
- rejectUnknownFlags(args);
316
- if (args.help) { out(` ima2 video edit <prompt> --video <url|file_id|generated-file>\n\n Edit existing video with text prompt (real V2V).\n Model: grok-imagine-video only. Input: mp4, max 8.7s.\n\n Options:\n --video <value> Source video HTTPS URL, xAI file_id, data URL, or generated filename (required)\n -o, --out <file> Download edited video to file\n --output <file> Alias for --out\n --json Print JSON result\n --timeout <sec> Default: 600\n --server <url> Override server URL`); return; }
317
- const prompt = args.positional.join(" ");
318
- if (!prompt.trim()) die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
319
- if (!args.video) die(2, "--video <url> is required");
320
- parseTimeoutSeconds(args.timeout);
321
- let server;
322
- try { server = await resolveServer({ serverFlag: args.server }); } catch (e: unknown) { die(exitCodeForError(e), (e as Error).message); throw e; }
323
- const res = await fetch(`${server.base}/api/video/edit`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ prompt, videoUrl: args.video }), signal: timeoutSignal(args.timeout) });
324
- const data = await readJsonResponse(res, "edit");
325
- if (!res.ok) die(1, `edit failed: ${data.error ?? res.status}`);
326
- const outPath = (args.out || args.output) as string | undefined;
327
- if (outPath) await downloadReturnedVideo(server.base, data, outPath, timeoutSignal(args.timeout));
328
- if (args.json) { out(JSON.stringify(data, null, 2)); } else { out(color.green("✓ ") + `Edited video: ${data.url}`); }
329
- }
330
-
331
- async function videoExtendCmd(argv: string[]) {
332
- const spec = { flags: { video: { type: "string" }, duration: { type: "string", default: "6" }, out: { short: "o", type: "string" }, output: { type: "string" }, json: { type: "boolean" }, timeout: { type: "string", default: "600" }, server: { type: "string" }, help: { short: "h", type: "boolean" } } };
333
- const args = parseArgs(argv, spec);
334
- rejectUnknownFlags(args);
335
- if (args.help) { out(` ima2 video extend <prompt> --video <url|file_id|generated-file> [--duration 6]\n\n Extend video from its last frame.\n Model: grok-imagine-video only. Extension: 2-10s.\n\n Options:\n --video <value> Source video HTTPS URL, xAI file_id, data URL, or generated filename (required)\n --duration <2-10> Extension duration (default: 6)\n -o, --out <file> Download extended video to file\n --output <file> Alias for --out\n --json Print JSON result\n --timeout <sec> Default: 600\n --server <url> Override server URL`); return; }
336
- const prompt = args.positional.join(" ");
337
- if (!prompt.trim()) die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
338
- if (!args.video) die(2, "--video <url> is required");
339
- const duration = parseIntegerFlag(args.duration, 6, "--duration");
340
- if (duration < 2 || duration > 10) die(2, "--duration must be between 2 and 10");
341
- parseTimeoutSeconds(args.timeout);
342
- let server;
343
- try { server = await resolveServer({ serverFlag: args.server }); } catch (e: unknown) { die(exitCodeForError(e), (e as Error).message); throw e; }
344
- const res = await fetch(`${server.base}/api/video/extend`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ prompt, videoUrl: args.video, duration }), signal: timeoutSignal(args.timeout) });
345
- const data = await readJsonResponse(res, "extend");
346
- if (!res.ok) die(1, `extend failed: ${data.error ?? res.status}`);
347
- const outPath = (args.out || args.output) as string | undefined;
348
- if (outPath) await downloadReturnedVideo(server.base, data, outPath, timeoutSignal(args.timeout));
349
- if (args.json) { out(JSON.stringify(data, null, 2)); } else { out(color.green("✓ ") + `Extended video (${data.duration}s): ${data.url}`); }
350
- }
351
-
352
- async function videoContinueCmd(argv: string[]) {
353
- const spec = {
354
- flags: {
355
- video: { type: "string" },
356
- duration: { type: "string", default: "5" },
357
- resolution: { type: "string", default: "720p" },
358
- "aspect-ratio": { type: "string", default: "auto" },
359
- model: { type: "string" },
360
- out: { short: "o", type: "string" },
361
- output: { type: "string" },
362
- json: { type: "boolean" },
363
- timeout: { type: "string", default: "600" },
364
- server: { type: "string" },
365
- help: { short: "h", type: "boolean" },
366
- },
367
- };
368
- const args = parseArgs(argv, spec);
369
- rejectUnknownFlags(args);
370
- if (args.help) {
371
- out(` ima2 video continue <prompt> --video <generated-file>\n\n Generate a new clip from a generated video's last frame and carry branch-local revisedPrompt lineage.\n\n Prompt must describe visual flow, motion, sound/music/no-music, dialogue/no-dialogue, ending frame, and how the selected duration should feel naturally filled.\n\n Options:\n --video <file> Generated .mp4 filename (required)\n --duration <1..15> Default: 5. Prompt motion should naturally fill this length\n --resolution <480p|720p> Default: 720p\n --aspect-ratio <ratio|auto> Default: auto\n --model <name> grok-imagine-video, grok-imagine-video-1.5-preview\n -o, --out <file> Download continued video to file\n --output <file> Alias for --out\n --json Print JSON result\n --timeout <sec> Default: 600\n --server <url> Override server URL`);
372
- return;
373
- }
374
- const prompt = args.positional.join(" ");
375
- if (!prompt.trim()) die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
376
- if (!args.video) die(2, "--video <generated-file> is required");
377
- const duration = parseIntegerFlag(args.duration, 5, "--duration");
378
- if (duration < 1 || duration > 15) die(2, "--duration must be between 1 and 15");
379
- const resolution = String(args.resolution);
380
- if (!VALID_RESOLUTIONS.has(resolution)) die(2, "--resolution must be one of: 480p, 720p");
381
- const aspectRatio = String(args["aspect-ratio"]);
382
- if (!VALID_ASPECT_RATIOS.has(aspectRatio)) die(2, "--aspect-ratio must be one of: 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3, auto");
383
- if (args.model && !VALID_MODELS.has(String(args.model))) {
384
- die(2, "--model must be one of: grok-imagine-video, grok-imagine-video-1.5-preview");
385
- }
386
- parseTimeoutSeconds(args.timeout);
387
- let server;
388
- try { server = await resolveServer({ serverFlag: args.server }); } catch (e: unknown) { die(exitCodeForError(e), (e as Error).message); throw e; }
389
- const requestId = `req_cli_video_continue_${Date.now().toString(36)}`;
390
- const body: Record<string, unknown> = {
391
- prompt,
392
- requestId,
393
- duration,
394
- resolution,
395
- aspectRatio,
396
- continueFromVideo: args.video,
397
- };
398
- if (args.model) body.model = args.model;
399
- const data = await runVideoGenerateRequest(server.base, body, args.timeout, Boolean(args.json));
400
- const outPath = (args.out || args.output) as string | undefined;
401
- if (outPath) await downloadReturnedVideo(server.base, data, outPath, timeoutSignal(args.timeout));
402
- if (args.json) out(JSON.stringify(data, null, 2));
403
- else out(color.green("✓ ") + `Continued video: ${data.url}`);
404
- }
405
-
406
- async function videoFrameCmd(argv: string[]) {
407
- const spec = { flags: { last: { type: "boolean" }, position: { type: "string" }, out: { type: "string" }, output: { short: "o", type: "string" }, timeout: { type: "string", default: "60" }, server: { type: "string" }, help: { short: "h", type: "boolean" } } };
408
- const args = parseArgs(argv, spec);
409
- rejectUnknownFlags(args);
410
- if (args.help) { out(` ima2 video frame <generated-file> [--last] [--position <sec>] [-o output.png]\n\n Extract a frame from a generated video file.\n\n Options:\n --last Extract last frame (default)\n --position <sec> Extract frame at specific second\n -o, --output <path> Output file path\n --out <path> Alias for --output\n --timeout <sec> Default: 60\n --server <url> Override server URL`); return; }
411
- const file = args.positional[0];
412
- if (!file) die(2, "file argument required");
413
- if (args.last && args.position) die(2, "use either --last or --position, not both");
414
- const position = args.last ? "last" : (String(args.position || "last"));
415
- if (position !== "last" && !/^\d+(\.\d+)?$/.test(position)) die(2, "--position must be a non-negative number");
416
- parseTimeoutSeconds(args.timeout);
417
- let server;
418
- try { server = await resolveServer({ serverFlag: args.server }); } catch (e: unknown) { die(exitCodeForError(e), (e as Error).message); throw e; }
419
- const url = `${server.base}/api/video/frame?file=${encodeURIComponent(file)}&position=${encodeURIComponent(position)}`;
420
- const res = await fetch(url, { signal: timeoutSignal(args.timeout) });
421
- if (!res.ok) { const d = await readJsonResponse(res, "frame extraction"); die(1, `frame extraction failed: ${(d as any).error || res.status}`); }
422
- const buf = Buffer.from(await res.arrayBuffer());
423
- const outPath = (args.output || args.out) as string || `frame-${basename(file).replace(/\.[^.]+$/, "")}.png`;
424
- await writeBuffer(outPath, buf);
425
- out(color.green("✓ ") + `Frame saved: ${outPath} (${buf.length} bytes)`);
426
- }
427
-
428
- async function videoAnalyzeCmd(argv: string[]) {
429
- const spec = { flags: { json: { type: "boolean" }, timeout: { type: "string", default: "180" }, server: { type: "string" }, help: { short: "h", type: "boolean" } } };
430
- const args = parseArgs(argv, spec);
431
- rejectUnknownFlags(args);
432
- if (args.help) { out(` ima2 video analyze <generated-file>\n\n Analyze first/last frames from a generated .mp4 with Grok 4.3 image understanding. Outputs structured recreation prompt.\n\n Options:\n --json Print JSON result\n --timeout <sec> Default: 180\n --server <url> Override server URL`); return; }
433
- const videoUrl = args.positional[0];
434
- if (!videoUrl) die(2, "generated video filename required");
435
- parseTimeoutSeconds(args.timeout);
436
- let server;
437
- try { server = await resolveServer({ serverFlag: args.server }); } catch (e: unknown) { die(exitCodeForError(e), (e as Error).message); throw e; }
438
- const res = await fetch(`${server.base}/api/video/analyze`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ videoUrl }), signal: timeoutSignal(args.timeout) });
439
- const data = await readJsonResponse(res, "analyze");
440
- if (!res.ok) die(1, `analyze failed: ${(data as any).error || res.status}`);
441
- if (args.json) { out(JSON.stringify(data, null, 2)); } else { out((data as any).analysis); }
442
- }