ima2-gen 1.1.21 → 1.1.22

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 (208) hide show
  1. package/README.md +30 -4
  2. package/bin/ima2.js +14 -4
  3. package/bin/lib/platform.js +34 -5
  4. package/docs/README.ko.md +31 -0
  5. package/lib/agentQueueWorker.js +6 -0
  6. package/lib/agentRuntime.js +3 -2
  7. package/lib/atomicWrite.js +14 -0
  8. package/lib/grokProxyLauncher.js +5 -3
  9. package/lib/inflight.js +1 -1
  10. package/lib/oauthLauncher.js +5 -0
  11. package/lib/videoFrameExtract.js +3 -3
  12. package/package.json +5 -7
  13. package/routes/edit.js +2 -1
  14. package/routes/generate.js +4 -3
  15. package/routes/health.js +4 -3
  16. package/routes/multimode.js +2 -1
  17. package/routes/video.js +4 -2
  18. package/server.js +29 -2
  19. package/ui/dist/.vite/manifest.json +12 -12
  20. package/ui/dist/assets/{AgentWorkspace-B_hq9CLg.js → AgentWorkspace-COxQ5TjU.js} +1 -1
  21. package/ui/dist/assets/{CardNewsWorkspace-wD12J7qk.js → CardNewsWorkspace-B0OkcuVz.js} +1 -1
  22. package/ui/dist/assets/{NodeCanvas-CI_wuPMf.js → NodeCanvas-BSsclEBh.js} +1 -1
  23. package/ui/dist/assets/{PromptBuilderPanel-CUTujJUV.js → PromptBuilderPanel-DpC9A5Rz.js} +1 -1
  24. package/ui/dist/assets/{PromptImportDialog-CUi66jPK.js → PromptImportDialog-CVwT0rLd.js} +2 -2
  25. package/ui/dist/assets/{PromptImportDiscoverySection-Cm3vrjY4.js → PromptImportDiscoverySection-BDCkRCRs.js} +1 -1
  26. package/ui/dist/assets/{PromptImportFolderSection-DOtWTD9n.js → PromptImportFolderSection-QoKbZD83.js} +1 -1
  27. package/ui/dist/assets/{PromptLibraryPanel-BMjQegRa.js → PromptLibraryPanel-BhFgeKnY.js} +2 -2
  28. package/ui/dist/assets/SettingsWorkspace-CfjrlH5R.js +1 -0
  29. package/ui/dist/assets/index-C-mur7pa.css +1 -0
  30. package/ui/dist/assets/index-CCP5nUOj.js +42 -0
  31. package/ui/dist/assets/{index-31uVIdt4.js → index-Cxhzi3bs.js} +1 -1
  32. package/ui/dist/index.html +2 -2
  33. package/bin/commands/annotate.ts +0 -119
  34. package/bin/commands/cancel.ts +0 -48
  35. package/bin/commands/canvas-versions.ts +0 -80
  36. package/bin/commands/capabilities.ts +0 -110
  37. package/bin/commands/cardnews.ts +0 -249
  38. package/bin/commands/comfy.ts +0 -54
  39. package/bin/commands/config.ts +0 -186
  40. package/bin/commands/defaults.ts +0 -192
  41. package/bin/commands/doctor.ts +0 -202
  42. package/bin/commands/edit.ts +0 -150
  43. package/bin/commands/gen.ts +0 -214
  44. package/bin/commands/grok.ts +0 -90
  45. package/bin/commands/history.ts +0 -146
  46. package/bin/commands/ls.ts +0 -64
  47. package/bin/commands/metadata.ts +0 -39
  48. package/bin/commands/multimode.ts +0 -196
  49. package/bin/commands/node.ts +0 -166
  50. package/bin/commands/observability.ts +0 -176
  51. package/bin/commands/ping.ts +0 -31
  52. package/bin/commands/prompt-sub/build.ts +0 -101
  53. package/bin/commands/prompt.ts +0 -492
  54. package/bin/commands/ps.ts +0 -81
  55. package/bin/commands/session.ts +0 -266
  56. package/bin/commands/show.ts +0 -72
  57. package/bin/commands/skill.ts +0 -70
  58. package/bin/commands/video.ts +0 -442
  59. package/bin/ima2.ts +0 -430
  60. package/bin/lib/args.ts +0 -92
  61. package/bin/lib/browser-id.ts +0 -16
  62. package/bin/lib/client.ts +0 -122
  63. package/bin/lib/config-store.ts +0 -120
  64. package/bin/lib/destructive-confirm.ts +0 -19
  65. package/bin/lib/doctor-checks.ts +0 -91
  66. package/bin/lib/error-hints.ts +0 -23
  67. package/bin/lib/files.ts +0 -39
  68. package/bin/lib/output.ts +0 -73
  69. package/bin/lib/platform.ts +0 -99
  70. package/bin/lib/recover-output.ts +0 -139
  71. package/bin/lib/sse.ts +0 -73
  72. package/bin/lib/star-prompt.ts +0 -97
  73. package/bin/lib/storage-doctor.ts +0 -39
  74. package/bin/lib/ui-build.ts +0 -85
  75. package/config.ts +0 -354
  76. package/lib/agentCommandParser.ts +0 -69
  77. package/lib/agentGenerationPlanner.ts +0 -273
  78. package/lib/agentQuestionResponder.ts +0 -266
  79. package/lib/agentQueueStore.ts +0 -270
  80. package/lib/agentQueueWorker.ts +0 -89
  81. package/lib/agentRuntime.ts +0 -604
  82. package/lib/agentSettings.ts +0 -72
  83. package/lib/agentStore.ts +0 -422
  84. package/lib/agentStoreRows.ts +0 -136
  85. package/lib/agentTypes.ts +0 -154
  86. package/lib/apiCachePolicy.ts +0 -11
  87. package/lib/assetLifecycle.ts +0 -146
  88. package/lib/canvasVersionStore.ts +0 -223
  89. package/lib/capabilities.ts +0 -126
  90. package/lib/cardNewsGenerator.ts +0 -271
  91. package/lib/cardNewsJobStore.ts +0 -142
  92. package/lib/cardNewsManifestStore.ts +0 -154
  93. package/lib/cardNewsPlanner.ts +0 -236
  94. package/lib/cardNewsPlannerClient.ts +0 -155
  95. package/lib/cardNewsPlannerPrompt.ts +0 -62
  96. package/lib/cardNewsPlannerSchema.ts +0 -321
  97. package/lib/cardNewsRoleTemplateStore.ts +0 -47
  98. package/lib/cardNewsTemplateStore.ts +0 -252
  99. package/lib/codexDetect.ts +0 -71
  100. package/lib/comfyBridge.ts +0 -235
  101. package/lib/composerSnapshot.ts +0 -33
  102. package/lib/configKeys.ts +0 -62
  103. package/lib/db.ts +0 -295
  104. package/lib/errInfo.ts +0 -43
  105. package/lib/errorClassify.ts +0 -100
  106. package/lib/generationCancel.ts +0 -28
  107. package/lib/generationErrors.ts +0 -238
  108. package/lib/grokImageAdapter.ts +0 -513
  109. package/lib/grokMultimodeAdapter.ts +0 -84
  110. package/lib/grokProxyLauncher.ts +0 -153
  111. package/lib/grokRuntime.ts +0 -23
  112. package/lib/grokSizeMapper.ts +0 -71
  113. package/lib/grokVideoAdapter.ts +0 -458
  114. package/lib/grokVideoCanvas.ts +0 -26
  115. package/lib/grokVideoDownload.ts +0 -59
  116. package/lib/grokVideoPlannerPrompt.ts +0 -67
  117. package/lib/historyIndex.ts +0 -51
  118. package/lib/historyList.ts +0 -181
  119. package/lib/imageMetadata.ts +0 -113
  120. package/lib/imageMetadataStore.ts +0 -67
  121. package/lib/imageModels.ts +0 -165
  122. package/lib/inflight.ts +0 -281
  123. package/lib/localImportStore.ts +0 -114
  124. package/lib/logger.ts +0 -161
  125. package/lib/nodeStore.ts +0 -91
  126. package/lib/oauthLauncher.ts +0 -94
  127. package/lib/oauthNormalize.ts +0 -30
  128. package/lib/oauthProxy/errors.ts +0 -128
  129. package/lib/oauthProxy/generators.ts +0 -494
  130. package/lib/oauthProxy/index.ts +0 -28
  131. package/lib/oauthProxy/prompts.ts +0 -123
  132. package/lib/oauthProxy/references.ts +0 -45
  133. package/lib/oauthProxy/runtime.ts +0 -115
  134. package/lib/oauthProxy/streams.ts +0 -232
  135. package/lib/oauthProxy/types.ts +0 -9
  136. package/lib/oauthProxy.ts +0 -3
  137. package/lib/openDirectory.ts +0 -47
  138. package/lib/pngInfo.ts +0 -26
  139. package/lib/promptBuilder/attachments.ts +0 -74
  140. package/lib/promptBuilder/client.ts +0 -130
  141. package/lib/promptBuilder/constants.ts +0 -9
  142. package/lib/promptBuilder/context.ts +0 -36
  143. package/lib/promptBuilder/errors.ts +0 -12
  144. package/lib/promptBuilder/requestSchema.ts +0 -56
  145. package/lib/promptBuilder/responseParser.ts +0 -219
  146. package/lib/promptBuilder/systemPrompt.ts +0 -135
  147. package/lib/promptBuilder/transport.ts +0 -94
  148. package/lib/promptBuilder/types.ts +0 -109
  149. package/lib/promptImport/curatedSources.ts +0 -141
  150. package/lib/promptImport/discoveryRegistry.ts +0 -329
  151. package/lib/promptImport/errors.ts +0 -18
  152. package/lib/promptImport/githubDiscovery.ts +0 -309
  153. package/lib/promptImport/githubFolder.ts +0 -397
  154. package/lib/promptImport/githubSource.ts +0 -257
  155. package/lib/promptImport/gptImageHints.ts +0 -70
  156. package/lib/promptImport/parsePromptCandidates.ts +0 -179
  157. package/lib/promptImport/promptIndex.ts +0 -326
  158. package/lib/promptImport/rankPromptCandidates.ts +0 -65
  159. package/lib/promptImport/types.ts +0 -103
  160. package/lib/promptSafetyPolicy.ts +0 -5
  161. package/lib/providerOptions.ts +0 -56
  162. package/lib/referenceImageCompress.ts +0 -84
  163. package/lib/refs.ts +0 -133
  164. package/lib/requestLogger.ts +0 -49
  165. package/lib/responsesDoctor.ts +0 -456
  166. package/lib/responsesErrors.ts +0 -83
  167. package/lib/responsesFallback.ts +0 -114
  168. package/lib/responsesImageAdapter.ts +0 -466
  169. package/lib/responsesParse.ts +0 -452
  170. package/lib/responsesTools.ts +0 -28
  171. package/lib/runtimeContext.ts +0 -146
  172. package/lib/runtimePorts.ts +0 -105
  173. package/lib/sessionStore.ts +0 -308
  174. package/lib/storageMigration.ts +0 -310
  175. package/lib/styleSheet.ts +0 -139
  176. package/lib/systemTrash.ts +0 -20
  177. package/lib/videoContinuity.ts +0 -180
  178. package/lib/videoFrameExtract.ts +0 -78
  179. package/lib/videoSeriesChain.ts +0 -29
  180. package/lib/visibleTextLanguagePolicy.ts +0 -7
  181. package/routes/agent.ts +0 -308
  182. package/routes/annotations.ts +0 -118
  183. package/routes/canvasVersions.ts +0 -69
  184. package/routes/capabilities.ts +0 -18
  185. package/routes/cardNews.ts +0 -211
  186. package/routes/comfy.ts +0 -43
  187. package/routes/edit.ts +0 -352
  188. package/routes/generate.ts +0 -492
  189. package/routes/grok.ts +0 -24
  190. package/routes/health.ts +0 -123
  191. package/routes/history.ts +0 -221
  192. package/routes/imageImport.ts +0 -37
  193. package/routes/index.ts +0 -52
  194. package/routes/metadata.ts +0 -77
  195. package/routes/multimode.ts +0 -499
  196. package/routes/nodes.ts +0 -578
  197. package/routes/promptBuilder.ts +0 -37
  198. package/routes/promptImport.ts +0 -379
  199. package/routes/prompts.ts +0 -428
  200. package/routes/quota.ts +0 -89
  201. package/routes/sessions.ts +0 -317
  202. package/routes/storage.ts +0 -47
  203. package/routes/video.ts +0 -300
  204. package/routes/videoExtended.ts +0 -284
  205. package/server.ts +0 -293
  206. package/ui/dist/assets/SettingsWorkspace-PiaVnsdA.js +0 -1
  207. package/ui/dist/assets/index-CjgnNtgt.css +0 -1
  208. 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
- }