ima2-gen 1.1.20 → 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 (239) hide show
  1. package/README.md +42 -26
  2. package/bin/commands/capabilities.js +2 -2
  3. package/bin/commands/defaults.js +2 -2
  4. package/bin/commands/doctor.js +3 -3
  5. package/bin/commands/edit.js +1 -1
  6. package/bin/commands/gen.js +1 -1
  7. package/bin/commands/grok.js +16 -11
  8. package/bin/commands/multimode.js +1 -1
  9. package/bin/commands/observability.js +2 -2
  10. package/bin/commands/video.js +335 -13
  11. package/bin/ima2.js +23 -13
  12. package/bin/lib/error-hints.js +2 -2
  13. package/bin/lib/platform.js +34 -5
  14. package/docs/API.md +112 -3
  15. package/docs/CLI.md +61 -7
  16. package/docs/FAQ.ko.md +15 -20
  17. package/docs/FAQ.md +14 -19
  18. package/docs/NPX_QUICKSTART.md +40 -0
  19. package/docs/PROMPT_STUDIO.ko.md +1 -1
  20. package/docs/PROMPT_STUDIO.md +1 -1
  21. package/docs/README.ja.md +6 -16
  22. package/docs/README.ko.md +35 -14
  23. package/docs/README.zh-CN.md +7 -17
  24. package/docs/migration/runtime-test-inventory.md +8 -1
  25. package/lib/agentQueueWorker.js +6 -0
  26. package/lib/agentRuntime.js +20 -5
  27. package/lib/atomicWrite.js +14 -0
  28. package/lib/capabilities.js +1 -1
  29. package/lib/generationErrors.js +1 -1
  30. package/lib/grokProxyLauncher.js +31 -6
  31. package/lib/grokVideoAdapter.js +18 -89
  32. package/lib/grokVideoCanvas.js +25 -0
  33. package/lib/grokVideoDownload.js +58 -0
  34. package/lib/grokVideoPlannerPrompt.js +64 -0
  35. package/lib/historyList.js +7 -1
  36. package/lib/inflight.js +1 -1
  37. package/lib/oauthLauncher.js +26 -6
  38. package/lib/videoContinuity.js +149 -0
  39. package/lib/videoFrameExtract.js +80 -0
  40. package/node_modules/progrok/dist/index.js +187 -88
  41. package/node_modules/progrok/dist/index.js.map +1 -1
  42. package/node_modules/progrok/package.json +1 -1
  43. package/node_modules/progrok/skills/progrok/SKILL.md +33 -4
  44. package/package.json +6 -8
  45. package/routes/edit.js +2 -1
  46. package/routes/generate.js +4 -3
  47. package/routes/health.js +4 -3
  48. package/routes/index.js +4 -0
  49. package/routes/multimode.js +2 -1
  50. package/routes/quota.js +66 -0
  51. package/routes/video.js +80 -16
  52. package/routes/videoExtended.js +293 -0
  53. package/server.js +35 -4
  54. package/skills/ima2/SKILL.md +320 -7
  55. package/ui/dist/.vite/manifest.json +12 -12
  56. package/ui/dist/assets/{AgentWorkspace-DS8uvoLI.js → AgentWorkspace-COxQ5TjU.js} +2 -2
  57. package/ui/dist/assets/{CardNewsWorkspace-CYxMsE67.js → CardNewsWorkspace-B0OkcuVz.js} +1 -1
  58. package/ui/dist/assets/{NodeCanvas-DccIc347.js → NodeCanvas-BSsclEBh.js} +1 -1
  59. package/ui/dist/assets/{PromptBuilderPanel-BvxxwSJp.js → PromptBuilderPanel-DpC9A5Rz.js} +1 -1
  60. package/ui/dist/assets/{PromptImportDialog-u1_BFDRd.js → PromptImportDialog-CVwT0rLd.js} +2 -2
  61. package/ui/dist/assets/{PromptImportDiscoverySection-C5uvkVSz.js → PromptImportDiscoverySection-BDCkRCRs.js} +1 -1
  62. package/ui/dist/assets/{PromptImportFolderSection-D3E_O1SD.js → PromptImportFolderSection-QoKbZD83.js} +1 -1
  63. package/ui/dist/assets/{PromptLibraryPanel-4gyf9CB9.js → PromptLibraryPanel-BhFgeKnY.js} +2 -2
  64. package/ui/dist/assets/SettingsWorkspace-CfjrlH5R.js +1 -0
  65. package/ui/dist/assets/index-C-mur7pa.css +1 -0
  66. package/ui/dist/assets/index-CCP5nUOj.js +42 -0
  67. package/ui/dist/assets/{index-DoKtXbod.js → index-Cxhzi3bs.js} +1 -1
  68. package/ui/dist/index.html +2 -2
  69. package/vendor/progrok-0.2.0.tgz +0 -0
  70. package/bin/commands/annotate.ts +0 -119
  71. package/bin/commands/cancel.ts +0 -48
  72. package/bin/commands/canvas-versions.ts +0 -80
  73. package/bin/commands/capabilities.ts +0 -110
  74. package/bin/commands/cardnews.ts +0 -249
  75. package/bin/commands/comfy.ts +0 -54
  76. package/bin/commands/config.ts +0 -186
  77. package/bin/commands/defaults.ts +0 -192
  78. package/bin/commands/doctor.ts +0 -202
  79. package/bin/commands/edit.ts +0 -150
  80. package/bin/commands/gen.ts +0 -214
  81. package/bin/commands/grok.ts +0 -85
  82. package/bin/commands/history.ts +0 -146
  83. package/bin/commands/ls.ts +0 -64
  84. package/bin/commands/metadata.ts +0 -39
  85. package/bin/commands/multimode.ts +0 -196
  86. package/bin/commands/node.ts +0 -166
  87. package/bin/commands/observability.ts +0 -176
  88. package/bin/commands/ping.ts +0 -31
  89. package/bin/commands/prompt-sub/build.ts +0 -101
  90. package/bin/commands/prompt.ts +0 -492
  91. package/bin/commands/ps.ts +0 -81
  92. package/bin/commands/session.ts +0 -266
  93. package/bin/commands/show.ts +0 -72
  94. package/bin/commands/skill.ts +0 -70
  95. package/bin/commands/video.ts +0 -205
  96. package/bin/ima2.ts +0 -430
  97. package/bin/lib/args.ts +0 -92
  98. package/bin/lib/browser-id.ts +0 -16
  99. package/bin/lib/client.ts +0 -122
  100. package/bin/lib/config-store.ts +0 -120
  101. package/bin/lib/destructive-confirm.ts +0 -19
  102. package/bin/lib/doctor-checks.ts +0 -91
  103. package/bin/lib/error-hints.ts +0 -23
  104. package/bin/lib/files.ts +0 -39
  105. package/bin/lib/output.ts +0 -73
  106. package/bin/lib/platform.ts +0 -99
  107. package/bin/lib/recover-output.ts +0 -139
  108. package/bin/lib/sse.ts +0 -73
  109. package/bin/lib/star-prompt.ts +0 -97
  110. package/bin/lib/storage-doctor.ts +0 -39
  111. package/bin/lib/ui-build.ts +0 -85
  112. package/config.ts +0 -354
  113. package/lib/agentCommandParser.ts +0 -69
  114. package/lib/agentGenerationPlanner.ts +0 -273
  115. package/lib/agentQuestionResponder.ts +0 -266
  116. package/lib/agentQueueStore.ts +0 -270
  117. package/lib/agentQueueWorker.ts +0 -89
  118. package/lib/agentRuntime.ts +0 -592
  119. package/lib/agentSettings.ts +0 -72
  120. package/lib/agentStore.ts +0 -422
  121. package/lib/agentStoreRows.ts +0 -136
  122. package/lib/agentTypes.ts +0 -154
  123. package/lib/apiCachePolicy.ts +0 -11
  124. package/lib/assetLifecycle.ts +0 -146
  125. package/lib/canvasVersionStore.ts +0 -223
  126. package/lib/capabilities.ts +0 -126
  127. package/lib/cardNewsGenerator.ts +0 -271
  128. package/lib/cardNewsJobStore.ts +0 -142
  129. package/lib/cardNewsManifestStore.ts +0 -154
  130. package/lib/cardNewsPlanner.ts +0 -236
  131. package/lib/cardNewsPlannerClient.ts +0 -155
  132. package/lib/cardNewsPlannerPrompt.ts +0 -62
  133. package/lib/cardNewsPlannerSchema.ts +0 -321
  134. package/lib/cardNewsRoleTemplateStore.ts +0 -47
  135. package/lib/cardNewsTemplateStore.ts +0 -252
  136. package/lib/codexDetect.ts +0 -71
  137. package/lib/comfyBridge.ts +0 -235
  138. package/lib/composerSnapshot.ts +0 -33
  139. package/lib/configKeys.ts +0 -62
  140. package/lib/db.ts +0 -295
  141. package/lib/errInfo.ts +0 -43
  142. package/lib/errorClassify.ts +0 -100
  143. package/lib/generationCancel.ts +0 -28
  144. package/lib/generationErrors.ts +0 -238
  145. package/lib/grokImageAdapter.ts +0 -513
  146. package/lib/grokMultimodeAdapter.ts +0 -84
  147. package/lib/grokProxyLauncher.ts +0 -129
  148. package/lib/grokRuntime.ts +0 -23
  149. package/lib/grokSizeMapper.ts +0 -71
  150. package/lib/grokVideoAdapter.ts +0 -519
  151. package/lib/historyIndex.ts +0 -51
  152. package/lib/historyList.ts +0 -177
  153. package/lib/imageMetadata.ts +0 -113
  154. package/lib/imageMetadataStore.ts +0 -67
  155. package/lib/imageModels.ts +0 -165
  156. package/lib/inflight.ts +0 -281
  157. package/lib/localImportStore.ts +0 -114
  158. package/lib/logger.ts +0 -161
  159. package/lib/nodeStore.ts +0 -91
  160. package/lib/oauthLauncher.ts +0 -78
  161. package/lib/oauthNormalize.ts +0 -30
  162. package/lib/oauthProxy/errors.ts +0 -128
  163. package/lib/oauthProxy/generators.ts +0 -494
  164. package/lib/oauthProxy/index.ts +0 -28
  165. package/lib/oauthProxy/prompts.ts +0 -123
  166. package/lib/oauthProxy/references.ts +0 -45
  167. package/lib/oauthProxy/runtime.ts +0 -115
  168. package/lib/oauthProxy/streams.ts +0 -232
  169. package/lib/oauthProxy/types.ts +0 -9
  170. package/lib/oauthProxy.ts +0 -3
  171. package/lib/openDirectory.ts +0 -47
  172. package/lib/pngInfo.ts +0 -26
  173. package/lib/promptBuilder/attachments.ts +0 -74
  174. package/lib/promptBuilder/client.ts +0 -130
  175. package/lib/promptBuilder/constants.ts +0 -9
  176. package/lib/promptBuilder/context.ts +0 -36
  177. package/lib/promptBuilder/errors.ts +0 -12
  178. package/lib/promptBuilder/requestSchema.ts +0 -56
  179. package/lib/promptBuilder/responseParser.ts +0 -219
  180. package/lib/promptBuilder/systemPrompt.ts +0 -135
  181. package/lib/promptBuilder/transport.ts +0 -94
  182. package/lib/promptBuilder/types.ts +0 -109
  183. package/lib/promptImport/curatedSources.ts +0 -141
  184. package/lib/promptImport/discoveryRegistry.ts +0 -329
  185. package/lib/promptImport/errors.ts +0 -18
  186. package/lib/promptImport/githubDiscovery.ts +0 -309
  187. package/lib/promptImport/githubFolder.ts +0 -397
  188. package/lib/promptImport/githubSource.ts +0 -257
  189. package/lib/promptImport/gptImageHints.ts +0 -70
  190. package/lib/promptImport/parsePromptCandidates.ts +0 -179
  191. package/lib/promptImport/promptIndex.ts +0 -326
  192. package/lib/promptImport/rankPromptCandidates.ts +0 -65
  193. package/lib/promptImport/types.ts +0 -103
  194. package/lib/promptSafetyPolicy.ts +0 -5
  195. package/lib/providerOptions.ts +0 -56
  196. package/lib/referenceImageCompress.ts +0 -84
  197. package/lib/refs.ts +0 -133
  198. package/lib/requestLogger.ts +0 -49
  199. package/lib/responsesDoctor.ts +0 -456
  200. package/lib/responsesErrors.ts +0 -83
  201. package/lib/responsesFallback.ts +0 -114
  202. package/lib/responsesImageAdapter.ts +0 -466
  203. package/lib/responsesParse.ts +0 -452
  204. package/lib/responsesTools.ts +0 -28
  205. package/lib/runtimeContext.ts +0 -146
  206. package/lib/runtimePorts.ts +0 -105
  207. package/lib/sessionStore.ts +0 -308
  208. package/lib/storageMigration.ts +0 -310
  209. package/lib/styleSheet.ts +0 -139
  210. package/lib/systemTrash.ts +0 -20
  211. package/lib/videoSeriesChain.ts +0 -29
  212. package/lib/visibleTextLanguagePolicy.ts +0 -7
  213. package/routes/agent.ts +0 -308
  214. package/routes/annotations.ts +0 -118
  215. package/routes/canvasVersions.ts +0 -69
  216. package/routes/capabilities.ts +0 -18
  217. package/routes/cardNews.ts +0 -211
  218. package/routes/comfy.ts +0 -43
  219. package/routes/edit.ts +0 -352
  220. package/routes/generate.ts +0 -492
  221. package/routes/grok.ts +0 -24
  222. package/routes/health.ts +0 -123
  223. package/routes/history.ts +0 -221
  224. package/routes/imageImport.ts +0 -37
  225. package/routes/index.ts +0 -48
  226. package/routes/metadata.ts +0 -77
  227. package/routes/multimode.ts +0 -499
  228. package/routes/nodes.ts +0 -578
  229. package/routes/promptBuilder.ts +0 -37
  230. package/routes/promptImport.ts +0 -379
  231. package/routes/prompts.ts +0 -428
  232. package/routes/sessions.ts +0 -317
  233. package/routes/storage.ts +0 -47
  234. package/routes/video.ts +0 -232
  235. package/server.ts +0 -290
  236. package/ui/dist/assets/SettingsWorkspace-F3eNu3mJ.js +0 -1
  237. package/ui/dist/assets/index-B6tcw_UF.css +0 -1
  238. package/ui/dist/assets/index-DYOh6gQD.js +0 -32
  239. package/vendor/progrok-0.1.1.tgz +0 -0
@@ -4,10 +4,54 @@ import { streamSse } from "../lib/sse.js";
4
4
  import { out, die, color, json, exitCodeForError } from "../lib/output.js";
5
5
  import { config } from "../../config.js";
6
6
  import { readFile, writeFile, mkdir } from "node:fs/promises";
7
- import { dirname, join } from "node:path";
7
+ import { basename, dirname, join } from "node:path";
8
8
  const VALID_RESOLUTIONS = new Set(["480p", "720p"]);
9
9
  const VALID_ASPECT_RATIOS = new Set(["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3", "auto"]);
10
10
  const VALID_MODELS = new Set(["grok-imagine-video", "grok-imagine-video-1.5-preview"]);
11
+ 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.";
12
+ function parseIntegerFlag(value, fallback, label) {
13
+ const raw = value === undefined ? String(fallback) : String(value);
14
+ if (!/^\d+$/.test(raw))
15
+ die(2, `${label} must be an integer`);
16
+ return Number(raw);
17
+ }
18
+ function rejectUnknownFlags(args) {
19
+ if (args._unknown?.length)
20
+ die(2, `unknown option: ${args._unknown[0]}`);
21
+ }
22
+ async function readJsonResponse(res, label) {
23
+ const text = await res.text();
24
+ try {
25
+ return text ? JSON.parse(text) : {};
26
+ }
27
+ catch {
28
+ die(1, `${label} failed: expected JSON response, got ${text.slice(0, 120) || `HTTP ${res.status}`}`);
29
+ }
30
+ }
31
+ function parseTimeoutSeconds(seconds) {
32
+ const sec = parseIntegerFlag(seconds, 600, "--timeout");
33
+ if (sec < 1)
34
+ die(2, "--timeout must be at least 1");
35
+ return sec;
36
+ }
37
+ function timeoutSignal(seconds) {
38
+ const sec = parseTimeoutSeconds(seconds);
39
+ return AbortSignal.timeout(sec * 1000);
40
+ }
41
+ async function writeBuffer(path, buf) {
42
+ await mkdir(dirname(path), { recursive: true }).catch(() => { });
43
+ await writeFile(path, buf);
44
+ }
45
+ async function downloadReturnedVideo(serverBase, data, outPath, signal) {
46
+ const rawUrl = typeof data.url === "string" ? data.url : "";
47
+ const url = rawUrl.startsWith("/") ? `${serverBase}${rawUrl}` : rawUrl;
48
+ if (!url)
49
+ die(1, "server did not return a video url");
50
+ const res = await fetch(url, { signal });
51
+ if (!res.ok)
52
+ die(1, `failed to download video: HTTP ${res.status}`);
53
+ await writeBuffer(outPath, Buffer.from(await res.arrayBuffer()));
54
+ }
11
55
  const SPEC = {
12
56
  flags: {
13
57
  duration: { type: "string", default: "5" },
@@ -27,11 +71,24 @@ const SPEC = {
27
71
  };
28
72
  const HELP = `
29
73
  ima2 video <prompt...> [options]
74
+ ima2 video edit <prompt> --video <url|file_id|generated-file>
75
+ ima2 video extend <prompt> --video <url|file_id|generated-file> [--duration 6]
76
+ ima2 video continue <prompt> --video <generated-file>
77
+ ima2 video frame <file> [--last] [-o output.png]
78
+ ima2 video analyze <generated-file>
30
79
 
31
- Generate a video via the Grok video provider (SSE streaming).
80
+ Generate, edit, extend, or analyze video via Grok.
32
81
 
33
- Options:
34
- --duration <1..15> Duration in seconds. Default: 5
82
+ Subcommands:
83
+ (default) Generate video (T2V / I2V / Ref2V)
84
+ edit Edit existing video with text prompt (V2V)
85
+ extend Continue video from last frame
86
+ continue Generate a new I2V clip from a generated video's last frame with lineage
87
+ frame Extract a frame from video (requires ffmpeg on server)
88
+ analyze Analyze video with Grok 4.3 vision
89
+
90
+ Options (generate mode):
91
+ --duration <1..15> Duration in seconds. Default: 5. Prompt motion should naturally fill this length
35
92
  --resolution <480p|720p> Default: 480p
36
93
  --aspect-ratio <ratio|auto> 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3, auto. Default: auto
37
94
  --model <name> grok-imagine-video, grok-imagine-video-1.5-preview
@@ -44,6 +101,10 @@ const HELP = `
44
101
  --server <url> Override server URL
45
102
  --session <id> Session ID
46
103
 
104
+ Edit/extend subcommands:
105
+ --video <value> HTTPS URL, xAI file_id, data URL, or generated filename
106
+ --duration <2..10> Extension duration only. Default: 6
107
+
47
108
  Modes (auto-detected from --ref count):
48
109
  0 refs → text-to-video
49
110
  1 ref → image-to-video
@@ -52,18 +113,34 @@ const HELP = `
52
113
  Examples:
53
114
  ima2 video "a cat playing piano"
54
115
  ima2 video "animate this" --ref photo.png --duration 10
55
- ima2 video "cinematic" --resolution 720p --aspect-ratio 16:9 -o out.mp4
116
+ ima2 video edit "make it sunset" --video https://vidgen.x.ai/.../clip.mp4
117
+ ima2 video extend "camera pulls back" --video https://vidgen.x.ai/.../clip.mp4 --duration 5
118
+ ima2 video continue "she turns back as the music cuts to room tone" --video 1780226256355_50252101.mp4
119
+ ima2 video frame 1780226256355_50252101.mp4 --last -o lastframe.png
120
+ ima2 video analyze 1780226256355_50252101.mp4
56
121
  `;
57
122
  export default async function videoCmd(argv) {
123
+ const sub = argv[0];
124
+ if (sub === "edit")
125
+ return videoEditCmd(argv.slice(1));
126
+ if (sub === "extend")
127
+ return videoExtendCmd(argv.slice(1));
128
+ if (sub === "continue")
129
+ return videoContinueCmd(argv.slice(1));
130
+ if (sub === "frame")
131
+ return videoFrameCmd(argv.slice(1));
132
+ if (sub === "analyze")
133
+ return videoAnalyzeCmd(argv.slice(1));
58
134
  const args = parseArgs(argv, SPEC);
135
+ rejectUnknownFlags(args);
59
136
  if (args.help) {
60
137
  out(HELP);
61
138
  return;
62
139
  }
63
140
  const prompt = args.positional.join(" ");
64
- if (!prompt)
65
- die(2, "prompt is required");
66
- const duration = parseInt(String(args.duration)) || 5;
141
+ if (!prompt.trim())
142
+ die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
143
+ const duration = parseIntegerFlag(args.duration, 5, "--duration");
67
144
  if (duration < 1 || duration > 15)
68
145
  die(2, "--duration must be between 1 and 15");
69
146
  const resolution = String(args.resolution);
@@ -78,6 +155,12 @@ export default async function videoCmd(argv) {
78
155
  const refs = (Array.isArray(args.ref) ? args.ref : []);
79
156
  if (refs.length > 7)
80
157
  die(2, "max 7 --ref attachments for video");
158
+ if (refs.length >= 2 && duration > 10)
159
+ die(2, "--duration must be between 1 and 10 when using 2 or more --ref attachments");
160
+ const timeoutSeconds = parseIntegerFlag(args.timeout, 600, "--timeout");
161
+ if (timeoutSeconds < 1)
162
+ die(2, "--timeout must be at least 1");
163
+ const timeoutMs = timeoutSeconds * 1000;
81
164
  let server;
82
165
  try {
83
166
  server = await resolveServer({ serverFlag: args.server });
@@ -90,7 +173,6 @@ export default async function videoCmd(argv) {
90
173
  const buf = await readFile(p);
91
174
  return buf.toString("base64");
92
175
  }));
93
- const timeoutMs = (parseInt(String(args.timeout)) || 600) * 1000;
94
176
  const requestId = `req_cli_video_${Date.now().toString(36)}`;
95
177
  const body = {
96
178
  prompt,
@@ -149,7 +231,7 @@ export default async function videoCmd(argv) {
149
231
  case "error":
150
232
  if (!args.json && lastProgress >= 0)
151
233
  process.stdout.write("\n");
152
- die(1, `video error: ${ev.data.error || ev.data}${ev.data.code ? ` (${ev.data.code})` : ""}`);
234
+ die(1, `video error: ${ev.data.error || ev.data}${ev.data.guidance ? `\n${ev.data.guidance}` : ""}${ev.data.code ? ` (${ev.data.code})` : ""}`);
153
235
  }
154
236
  }
155
237
  }
@@ -183,12 +265,11 @@ export default async function videoCmd(argv) {
183
265
  }
184
266
  // Download the video file from server
185
267
  const videoUrl = `${server.base}${doneData.url || `/generated/${encodeURIComponent(filename)}`}`;
186
- const dlRes = await fetch(videoUrl, { signal: AbortSignal.timeout(30_000) });
268
+ const dlRes = await fetch(videoUrl, { signal: timeoutSignal(args.timeout) });
187
269
  if (!dlRes.ok)
188
270
  die(1, `failed to download video: HTTP ${dlRes.status}`);
189
271
  const videoBuf = Buffer.from(await dlRes.arrayBuffer());
190
- await mkdir(dirname(target), { recursive: true }).catch(() => { });
191
- await writeFile(target, videoBuf);
272
+ await writeBuffer(target, videoBuf);
192
273
  if (args.json) {
193
274
  json({
194
275
  ok: true,
@@ -213,3 +294,244 @@ function renderBar(pct) {
213
294
  const filled = Math.round((pct / 100) * width);
214
295
  return color.green("█".repeat(filled)) + color.dim("░".repeat(width - filled));
215
296
  }
297
+ async function runVideoGenerateRequest(serverBase, body, timeout, silent) {
298
+ let doneData = null;
299
+ let lastProgress = -1;
300
+ for await (const ev of streamSse(`${serverBase}/api/video/generate`, {
301
+ body: { provider: "grok", ...body },
302
+ signal: timeoutSignal(timeout),
303
+ headers: typeof body.requestId === "string" ? { "X-Request-Id": body.requestId } : undefined,
304
+ })) {
305
+ if (ev.event === "progress") {
306
+ const pct = typeof ev.data.progress === "number" ? Math.round(ev.data.progress * 100) : null;
307
+ if (pct !== null && pct !== lastProgress && !silent) {
308
+ process.stdout.write(`\r ${renderBar(pct)} ${pct}%`);
309
+ lastProgress = pct;
310
+ }
311
+ }
312
+ else if (ev.event === "done") {
313
+ if (!silent && lastProgress >= 0)
314
+ process.stdout.write("\n");
315
+ doneData = ev.data;
316
+ }
317
+ else if (ev.event === "error") {
318
+ if (!silent && lastProgress >= 0)
319
+ process.stdout.write("\n");
320
+ die(1, `video error: ${ev.data.error || ev.data}${ev.data.guidance ? `\n${ev.data.guidance}` : ""}${ev.data.code ? ` (${ev.data.code})` : ""}`);
321
+ }
322
+ }
323
+ if (!doneData)
324
+ die(1, "server did not return a video result");
325
+ return doneData;
326
+ }
327
+ // --- Subcommands ---
328
+ async function videoEditCmd(argv) {
329
+ 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" } } };
330
+ const args = parseArgs(argv, spec);
331
+ rejectUnknownFlags(args);
332
+ if (args.help) {
333
+ 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`);
334
+ return;
335
+ }
336
+ const prompt = args.positional.join(" ");
337
+ if (!prompt.trim())
338
+ die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
339
+ if (!args.video)
340
+ die(2, "--video <url> is required");
341
+ parseTimeoutSeconds(args.timeout);
342
+ let server;
343
+ try {
344
+ server = await resolveServer({ serverFlag: args.server });
345
+ }
346
+ catch (e) {
347
+ die(exitCodeForError(e), e.message);
348
+ throw e;
349
+ }
350
+ 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) });
351
+ const data = await readJsonResponse(res, "edit");
352
+ if (!res.ok)
353
+ die(1, `edit failed: ${data.error ?? res.status}`);
354
+ const outPath = (args.out || args.output);
355
+ if (outPath)
356
+ await downloadReturnedVideo(server.base, data, outPath, timeoutSignal(args.timeout));
357
+ if (args.json) {
358
+ out(JSON.stringify(data, null, 2));
359
+ }
360
+ else {
361
+ out(color.green("✓ ") + `Edited video: ${data.url}`);
362
+ }
363
+ }
364
+ async function videoExtendCmd(argv) {
365
+ 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" } } };
366
+ const args = parseArgs(argv, spec);
367
+ rejectUnknownFlags(args);
368
+ if (args.help) {
369
+ 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`);
370
+ return;
371
+ }
372
+ const prompt = args.positional.join(" ");
373
+ if (!prompt.trim())
374
+ die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
375
+ if (!args.video)
376
+ die(2, "--video <url> is required");
377
+ const duration = parseIntegerFlag(args.duration, 6, "--duration");
378
+ if (duration < 2 || duration > 10)
379
+ die(2, "--duration must be between 2 and 10");
380
+ parseTimeoutSeconds(args.timeout);
381
+ let server;
382
+ try {
383
+ server = await resolveServer({ serverFlag: args.server });
384
+ }
385
+ catch (e) {
386
+ die(exitCodeForError(e), e.message);
387
+ throw e;
388
+ }
389
+ 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) });
390
+ const data = await readJsonResponse(res, "extend");
391
+ if (!res.ok)
392
+ die(1, `extend failed: ${data.error ?? res.status}`);
393
+ const outPath = (args.out || args.output);
394
+ if (outPath)
395
+ await downloadReturnedVideo(server.base, data, outPath, timeoutSignal(args.timeout));
396
+ if (args.json) {
397
+ out(JSON.stringify(data, null, 2));
398
+ }
399
+ else {
400
+ out(color.green("✓ ") + `Extended video (${data.duration}s): ${data.url}`);
401
+ }
402
+ }
403
+ async function videoContinueCmd(argv) {
404
+ const spec = {
405
+ flags: {
406
+ video: { type: "string" },
407
+ duration: { type: "string", default: "5" },
408
+ resolution: { type: "string", default: "720p" },
409
+ "aspect-ratio": { type: "string", default: "auto" },
410
+ model: { type: "string" },
411
+ out: { short: "o", type: "string" },
412
+ output: { type: "string" },
413
+ json: { type: "boolean" },
414
+ timeout: { type: "string", default: "600" },
415
+ server: { type: "string" },
416
+ help: { short: "h", type: "boolean" },
417
+ },
418
+ };
419
+ const args = parseArgs(argv, spec);
420
+ rejectUnknownFlags(args);
421
+ if (args.help) {
422
+ 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`);
423
+ return;
424
+ }
425
+ const prompt = args.positional.join(" ");
426
+ if (!prompt.trim())
427
+ die(2, ACTIVE_VIDEO_PROMPT_GUIDANCE);
428
+ if (!args.video)
429
+ die(2, "--video <generated-file> is required");
430
+ const duration = parseIntegerFlag(args.duration, 5, "--duration");
431
+ if (duration < 1 || duration > 15)
432
+ die(2, "--duration must be between 1 and 15");
433
+ const resolution = String(args.resolution);
434
+ if (!VALID_RESOLUTIONS.has(resolution))
435
+ die(2, "--resolution must be one of: 480p, 720p");
436
+ const aspectRatio = String(args["aspect-ratio"]);
437
+ if (!VALID_ASPECT_RATIOS.has(aspectRatio))
438
+ die(2, "--aspect-ratio must be one of: 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3, auto");
439
+ if (args.model && !VALID_MODELS.has(String(args.model))) {
440
+ die(2, "--model must be one of: grok-imagine-video, grok-imagine-video-1.5-preview");
441
+ }
442
+ parseTimeoutSeconds(args.timeout);
443
+ let server;
444
+ try {
445
+ server = await resolveServer({ serverFlag: args.server });
446
+ }
447
+ catch (e) {
448
+ die(exitCodeForError(e), e.message);
449
+ throw e;
450
+ }
451
+ const requestId = `req_cli_video_continue_${Date.now().toString(36)}`;
452
+ const body = {
453
+ prompt,
454
+ requestId,
455
+ duration,
456
+ resolution,
457
+ aspectRatio,
458
+ continueFromVideo: args.video,
459
+ };
460
+ if (args.model)
461
+ body.model = args.model;
462
+ const data = await runVideoGenerateRequest(server.base, body, args.timeout, Boolean(args.json));
463
+ const outPath = (args.out || args.output);
464
+ if (outPath)
465
+ await downloadReturnedVideo(server.base, data, outPath, timeoutSignal(args.timeout));
466
+ if (args.json)
467
+ out(JSON.stringify(data, null, 2));
468
+ else
469
+ out(color.green("✓ ") + `Continued video: ${data.url}`);
470
+ }
471
+ async function videoFrameCmd(argv) {
472
+ 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" } } };
473
+ const args = parseArgs(argv, spec);
474
+ rejectUnknownFlags(args);
475
+ if (args.help) {
476
+ 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`);
477
+ return;
478
+ }
479
+ const file = args.positional[0];
480
+ if (!file)
481
+ die(2, "file argument required");
482
+ if (args.last && args.position)
483
+ die(2, "use either --last or --position, not both");
484
+ const position = args.last ? "last" : (String(args.position || "last"));
485
+ if (position !== "last" && !/^\d+(\.\d+)?$/.test(position))
486
+ die(2, "--position must be a non-negative number");
487
+ parseTimeoutSeconds(args.timeout);
488
+ let server;
489
+ try {
490
+ server = await resolveServer({ serverFlag: args.server });
491
+ }
492
+ catch (e) {
493
+ die(exitCodeForError(e), e.message);
494
+ throw e;
495
+ }
496
+ const url = `${server.base}/api/video/frame?file=${encodeURIComponent(file)}&position=${encodeURIComponent(position)}`;
497
+ const res = await fetch(url, { signal: timeoutSignal(args.timeout) });
498
+ if (!res.ok) {
499
+ const d = await readJsonResponse(res, "frame extraction");
500
+ die(1, `frame extraction failed: ${d.error || res.status}`);
501
+ }
502
+ const buf = Buffer.from(await res.arrayBuffer());
503
+ const outPath = (args.output || args.out) || `frame-${basename(file).replace(/\.[^.]+$/, "")}.png`;
504
+ await writeBuffer(outPath, buf);
505
+ out(color.green("✓ ") + `Frame saved: ${outPath} (${buf.length} bytes)`);
506
+ }
507
+ async function videoAnalyzeCmd(argv) {
508
+ const spec = { flags: { json: { type: "boolean" }, timeout: { type: "string", default: "180" }, server: { type: "string" }, help: { short: "h", type: "boolean" } } };
509
+ const args = parseArgs(argv, spec);
510
+ rejectUnknownFlags(args);
511
+ if (args.help) {
512
+ 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`);
513
+ return;
514
+ }
515
+ const videoUrl = args.positional[0];
516
+ if (!videoUrl)
517
+ die(2, "generated video filename required");
518
+ parseTimeoutSeconds(args.timeout);
519
+ let server;
520
+ try {
521
+ server = await resolveServer({ serverFlag: args.server });
522
+ }
523
+ catch (e) {
524
+ die(exitCodeForError(e), e.message);
525
+ throw e;
526
+ }
527
+ const res = await fetch(`${server.base}/api/video/analyze`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ videoUrl }), signal: timeoutSignal(args.timeout) });
528
+ const data = await readJsonResponse(res, "analyze");
529
+ if (!res.ok)
530
+ die(1, `analyze failed: ${data.error || res.status}`);
531
+ if (args.json) {
532
+ out(JSON.stringify(data, null, 2));
533
+ }
534
+ else {
535
+ out(data.analysis);
536
+ }
537
+ }
package/bin/ima2.js CHANGED
@@ -6,7 +6,7 @@ import { fileURLToPath } from "url";
6
6
  import { spawn, execSync } from "child_process";
7
7
  import { confirmDestructiveAction } from "./lib/destructive-confirm.js";
8
8
  import { doctor } from "./commands/doctor.js";
9
- import { openUrl, resolveBin } from "./lib/platform.js";
9
+ import { openUrl, resolveBin, killProcessTree } from "./lib/platform.js";
10
10
  import { maybePromptGithubStar } from "./lib/star-prompt.js";
11
11
  import { ensureFreshUiDist } from "./lib/ui-build.js";
12
12
  import { detectCodexAuth } from "../lib/codexDetect.js";
@@ -88,7 +88,7 @@ async function setup() {
88
88
  saveConfig(config);
89
89
  console.log("\n Starting Grok OAuth login...\n");
90
90
  try {
91
- execSync(`node ${JSON.stringify(join(ROOT, "bin", "ima2.js"))} grok login`, { stdio: "inherit" });
91
+ execSync(`node ${JSON.stringify(join(ROOT, "bin", "ima2.js"))} grok login --manual-paste`, { stdio: "inherit" });
92
92
  }
93
93
  catch {
94
94
  console.log("\n Grok login failed or cancelled. You can retry with 'ima2 grok login'.\n");
@@ -121,7 +121,7 @@ async function setup() {
121
121
  // Grok OAuth
122
122
  console.log(" Running Grok OAuth login...\n");
123
123
  try {
124
- execSync(`node ${JSON.stringify(join(ROOT, "bin", "ima2.js"))} grok login`, { stdio: "inherit" });
124
+ execSync(`node ${JSON.stringify(join(ROOT, "bin", "ima2.js"))} grok login --manual-paste`, { stdio: "inherit" });
125
125
  }
126
126
  catch {
127
127
  console.log("\n Grok login failed. You can retry with 'ima2 grok login'.\n");
@@ -135,7 +135,7 @@ async function setup() {
135
135
  config.oauth.disableAutoStart = false;
136
136
  delete config.apiKey;
137
137
  saveConfig(config);
138
- console.log("\n Starting OAuth login...\n");
138
+ console.log("\n Starting GPT OAuth login...\n");
139
139
  const auth = detectCodexAuth();
140
140
  const hasAuth = auth.authed;
141
141
  if (!hasAuth) {
@@ -154,10 +154,10 @@ async function setup() {
154
154
  }
155
155
  else {
156
156
  const how = auth.probe === "authed" ? "codex CLI" : "auth file";
157
- console.log(` Existing OAuth session found (${how}).\n`);
157
+ console.log(` Existing GPT OAuth session found (${how}).\n`);
158
158
  }
159
159
  saveConfig(config);
160
- console.log(" OAuth configured. Starting server...\n");
160
+ console.log(" GPT OAuth configured. Starting server...\n");
161
161
  }
162
162
  rl.close();
163
163
  return config;
@@ -197,9 +197,16 @@ async function serve(serveArgs = []) {
197
197
  env,
198
198
  cwd: ROOT,
199
199
  });
200
+ child.on("error", (err) => {
201
+ console.error(`[ima2] Failed to start server: ${err.message}`);
202
+ process.exit(1);
203
+ });
200
204
  child.on("exit", (code) => process.exit(code));
201
- process.on("SIGINT", () => child.kill("SIGINT"));
202
- process.on("SIGTERM", () => child.kill("SIGTERM"));
205
+ process.on("SIGINT", () => killProcessTree(child.pid));
206
+ process.on("SIGTERM", () => killProcessTree(child.pid));
207
+ if (process.platform === "win32") {
208
+ process.on("SIGBREAK", () => killProcessTree(child.pid));
209
+ }
203
210
  }
204
211
  async function showStatus() {
205
212
  const config = loadConfig();
@@ -222,7 +229,7 @@ async function showStatus() {
222
229
  }
223
230
  // Check OAuth auth files + codex CLI probe
224
231
  const auth = detectCodexAuth();
225
- console.log(` OAuth sessions:`);
232
+ console.log(` GPT OAuth sessions:`);
226
233
  console.log(` ${auth.files.codex} ${auth.fileHits.codex ? "✓" : "✗"}`);
227
234
  console.log(` ${auth.files.chatgpt} ${auth.fileHits.chatgpt ? "✓" : "✗"}`);
228
235
  if (auth.fileHits.xdgCodex) {
@@ -255,7 +262,7 @@ function showHelp() {
255
262
 
256
263
  Server commands:
257
264
  serve [--dev] Start the image generation server
258
- setup, login Configure API key or OAuth (interactive)
265
+ setup, login Configure API key or GPT OAuth (interactive)
259
266
  status Show current configuration status
260
267
  doctor Diagnose environment and setup
261
268
  open Open web UI in browser
@@ -283,8 +290,8 @@ function showHelp() {
283
290
  storage <sub> Storage status / open-dir (ima2 storage --help)
284
291
  billing API usage / quota
285
292
  providers Configured providers
286
- oauth <sub> OAuth proxy status (ima2 oauth --help)
287
- grok <sub> Bundled progrok login/status (ima2 grok --help)
293
+ oauth <sub> GPT OAuth proxy status (ima2 oauth --help)
294
+ grok <sub> Bundled Grok auth/status (ima2 grok --help)
288
295
  config <sub> Config get/set/ls/path/rm (ima2 config --help)
289
296
  defaults <sub> Inspect/change model defaults (ima2 defaults --help)
290
297
  capabilities Agent capability metadata (ima2 capabilities --help)
@@ -335,7 +342,10 @@ switch (command) {
335
342
  break;
336
343
  case "setup":
337
344
  case "login":
338
- setup().then(() => console.log(" Done. Run 'ima2 serve' to start."));
345
+ setup().then(() => console.log(" Done. Run 'ima2 serve' to start.")).catch((e) => {
346
+ console.error(`Setup failed: ${e?.message || e}`);
347
+ process.exit(1);
348
+ });
339
349
  break;
340
350
  case "status":
341
351
  showStatus();
@@ -3,11 +3,11 @@ const HINTS = {
3
3
  APIKEY_DISABLED: "API-key generation is supported in current builds; switch providers or update the configured API key.",
4
4
  IMAGE_MODEL_UNSUPPORTED: "This model is visible but cannot generate images here. Use gpt-5.4 or gpt-5.4-mini.",
5
5
  INVALID_IMAGE_MODEL: "Use one of: gpt-5.5, gpt-5.4, gpt-5.4-mini.",
6
- OAUTH_UNAVAILABLE: "OAuth proxy is unavailable. Check `ima2 doctor` and restart `ima2 serve`.",
6
+ OAUTH_UNAVAILABLE: "GPT OAuth proxy is unavailable. Check `ima2 doctor` and restart `ima2 serve`.",
7
7
  NETWORK_FAILED: "Network/proxy failed. This is not a moderation refusal.",
8
8
  SAFETY_REFUSAL: "The image backend refused this generation.",
9
9
  MODERATION_REFUSED: "The prompt or image was rejected by moderation.",
10
- AUTH_CHATGPT_EXPIRED: "Run `npx @openai/codex login`, then restart `ima2 serve`.",
10
+ AUTH_CHATGPT_EXPIRED: "Re-run `ima2 setup` (option 1), then restart `ima2 serve`.",
11
11
  REF_TOO_LARGE: "Reference image is too large. Resize/compress it and retry.",
12
12
  REF_NOT_BASE64: "Reference payload is invalid. Use a normal PNG/JPEG/WebP file.",
13
13
  };
@@ -80,20 +80,29 @@ export function openUrl(url) {
80
80
  * Windows does NOT raise SIGTERM from the OS — SIGINT (Ctrl+C) and SIGBREAK
81
81
  * (Ctrl+Break) are the observable signals. We still register SIGTERM so that
82
82
  * Node-internal `child.kill("SIGTERM")` calls work in tests.
83
+ *
84
+ * Handlers may return a Promise — they run with a grace period (default 3s)
85
+ * before forceful exit, giving file handles and sockets time to close cleanly.
83
86
  */
87
+ const SHUTDOWN_GRACE_MS = 3_000;
88
+ let shutdownStarted = false;
84
89
  export function onShutdown(handler) {
85
90
  const signals = isWin
86
91
  ? ["SIGINT", "SIGTERM", "SIGBREAK"]
87
92
  : ["SIGINT", "SIGTERM", "SIGHUP"];
88
93
  for (const sig of signals) {
89
94
  try {
90
- process.on(sig, () => {
95
+ process.on(sig, async () => {
96
+ if (shutdownStarted)
97
+ return;
98
+ shutdownStarted = true;
99
+ const forceExit = setTimeout(() => process.exit(0), SHUTDOWN_GRACE_MS);
100
+ forceExit.unref?.();
91
101
  try {
92
- handler(sig);
93
- }
94
- finally {
95
- process.exit(0);
102
+ await handler(sig);
96
103
  }
104
+ catch { }
105
+ process.exit(0);
97
106
  });
98
107
  }
99
108
  catch {
@@ -101,3 +110,23 @@ export function onShutdown(handler) {
101
110
  }
102
111
  }
103
112
  }
113
+ /**
114
+ * Kill an entire process tree. On Windows, child.kill() only kills the
115
+ * immediate process, leaving grandchildren alive and holding file locks.
116
+ * taskkill /T /F kills the whole tree.
117
+ */
118
+ export function killProcessTree(pid) {
119
+ if (!pid)
120
+ return;
121
+ try {
122
+ if (isWin) {
123
+ execSync(`taskkill /T /F /PID ${pid}`, { stdio: "ignore" });
124
+ }
125
+ else {
126
+ process.kill(pid, "SIGTERM");
127
+ }
128
+ }
129
+ catch {
130
+ // Process already exited
131
+ }
132
+ }