project-graph-mcp 2.3.0 → 2.3.2

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 (66) hide show
  1. package/package.json +1 -3
  2. package/project-graph-mcp-2.3.0.tgz +0 -0
  3. package/src/network/web-server.js +1 -1
  4. package/vendor/symbiote-node/engine/AgentUICommands.js +100 -0
  5. package/vendor/symbiote-node/engine/Executor.js +371 -0
  6. package/vendor/symbiote-node/engine/Graph.js +314 -0
  7. package/vendor/symbiote-node/engine/GraphServer.js +353 -0
  8. package/vendor/symbiote-node/engine/HandlerLoader.js +145 -0
  9. package/vendor/symbiote-node/engine/History.js +83 -0
  10. package/vendor/symbiote-node/engine/Lifecycle.js +118 -0
  11. package/vendor/symbiote-node/engine/Persistence.js +84 -0
  12. package/vendor/symbiote-node/engine/Registry.js +264 -0
  13. package/vendor/symbiote-node/engine/SocketTypes.js +79 -0
  14. package/vendor/symbiote-node/engine/cli.js +404 -0
  15. package/vendor/symbiote-node/engine/index.js +56 -0
  16. package/vendor/symbiote-node/engine/nanoid.js +28 -0
  17. package/vendor/symbiote-node/engine/package.json +26 -0
  18. package/vendor/symbiote-node/engine/packs/ai/beat-detect.handler.js +215 -0
  19. package/vendor/symbiote-node/engine/packs/ai/content-adapt.handler.js +238 -0
  20. package/vendor/symbiote-node/engine/packs/ai/face-detect.handler.js +287 -0
  21. package/vendor/symbiote-node/engine/packs/ai/grok-generate.handler.js +565 -0
  22. package/vendor/symbiote-node/engine/packs/ai/kling-lipsync.handler.js +414 -0
  23. package/vendor/symbiote-node/engine/packs/ai/lesson-generate.handler.js +343 -0
  24. package/vendor/symbiote-node/engine/packs/ai/opencode.handler.js +164 -0
  25. package/vendor/symbiote-node/engine/packs/ai/replicate-lipsync.handler.js +341 -0
  26. package/vendor/symbiote-node/engine/packs/ai/tts.handler.js +241 -0
  27. package/vendor/symbiote-node/engine/packs/ai/whisper.handler.js +191 -0
  28. package/vendor/symbiote-node/engine/packs/data/db-query.handler.js +67 -0
  29. package/vendor/symbiote-node/engine/packs/data/news-accumulate.handler.js +281 -0
  30. package/vendor/symbiote-node/engine/packs/data/personas.handler.js +160 -0
  31. package/vendor/symbiote-node/engine/packs/data/prompt-loader.handler.js +193 -0
  32. package/vendor/symbiote-node/engine/packs/data/roles.handler.js +216 -0
  33. package/vendor/symbiote-node/engine/packs/data/rss-feed.handler.js +244 -0
  34. package/vendor/symbiote-node/engine/packs/debug/inject.handler.js +52 -0
  35. package/vendor/symbiote-node/engine/packs/flow/agent.handler.js +73 -0
  36. package/vendor/symbiote-node/engine/packs/flow/if.handler.js +107 -0
  37. package/vendor/symbiote-node/engine/packs/flow/loop.handler.js +58 -0
  38. package/vendor/symbiote-node/engine/packs/flow/merge.handler.js +60 -0
  39. package/vendor/symbiote-node/engine/packs/flow/retry.handler.js +65 -0
  40. package/vendor/symbiote-node/engine/packs/flow/switch.handler.js +64 -0
  41. package/vendor/symbiote-node/engine/packs/flow/wait-all.handler.js +39 -0
  42. package/vendor/symbiote-node/engine/packs/io/http-request.handler.js +82 -0
  43. package/vendor/symbiote-node/engine/packs/io/read-file.handler.js +60 -0
  44. package/vendor/symbiote-node/engine/packs/io/write-file.handler.js +63 -0
  45. package/vendor/symbiote-node/engine/packs/transform/anchor-match.handler.js +494 -0
  46. package/vendor/symbiote-node/engine/packs/transform/effects-skeleton.handler.js +417 -0
  47. package/vendor/symbiote-node/engine/packs/transform/json-parse.handler.js +43 -0
  48. package/vendor/symbiote-node/engine/packs/transform/lipsync-select.handler.js +339 -0
  49. package/vendor/symbiote-node/engine/packs/transform/riopla-adapt.handler.js +432 -0
  50. package/vendor/symbiote-node/engine/packs/transform/set.handler.js +57 -0
  51. package/vendor/symbiote-node/engine/packs/transform/template-builder.handler.js +134 -0
  52. package/vendor/symbiote-node/engine/packs/transform/template.handler.js +79 -0
  53. package/vendor/symbiote-node/engine/packs/transform/timeline-build.handler.js +399 -0
  54. package/vendor/symbiote-node/engine/packs/util/delay.handler.js +39 -0
  55. package/vendor/symbiote-node/engine/packs/util/log.handler.js +44 -0
  56. package/vendor/symbiote-node/engine/packs/video-pack.js +323 -0
  57. package/vendor/symbiote-node/package.json +2 -2
  58. package/web/app.js +6 -3
  59. package/web/components/canvas-graph.js +50 -11
  60. package/web/components/code-block.js +1 -1
  61. package/web/components/event-feed/MiniGraphWidget.js +105 -15
  62. package/web/components/follow-ribbon.js +134 -0
  63. package/web/follow-controller.js +241 -0
  64. package/web/panels/code-viewer.js +1 -1
  65. package/web/panels/dep-graph.js +21 -42
  66. package/web/style.css +6 -0
@@ -0,0 +1,565 @@
1
+ /**
2
+ * ai/grok-generate — Image/Video generation via Grok browser automation
3
+ *
4
+ * Uses Chrome extension bridge to automate grok.com for:
5
+ * - Text-to-image generation (via WebSocket injection)
6
+ * - Image editing (reference-based generation)
7
+ * - Image-to-video conversion
8
+ * - Batch processing with worker pool
9
+ *
10
+ * Architecture:
11
+ * - Bridge server (localhost:3333) ↔ Chrome extension (grok-bridge)
12
+ * - SELECTORS: stable DOM selectors (aria-label preferred)
13
+ * - ACTIONS: atomic bridge commands
14
+ * - WORKFLOWS: composed sequences
15
+ *
16
+ * Ported from Mr-Computer/modules/ai-music-video/src/services/grok-*.js
17
+ *
18
+ * @module agi-graph/packs/ai/grok-generate
19
+ */
20
+
21
+ import { readFile, writeFile, mkdir, rename } from 'fs/promises';
22
+ import { existsSync } from 'fs';
23
+ import path from 'path';
24
+
25
+ export default {
26
+ type: 'ai/grok-generate',
27
+ category: 'ai',
28
+ icon: 'auto_awesome',
29
+
30
+ driver: {
31
+ description: 'Generate images/videos via Grok browser automation',
32
+ inputs: [
33
+ { name: 'prompt', type: 'string' },
34
+ { name: 'referencePath', type: 'string' },
35
+ ],
36
+ outputs: [
37
+ { name: 'result', type: 'any' },
38
+ { name: 'error', type: 'string' },
39
+ ],
40
+ params: {
41
+ operation: { type: 'string', default: 'image', description: 'Operation: image | image-edit | video | batch-images | batch-videos | check' },
42
+ bridgeUrl: { type: 'string', default: 'http://localhost:3333', description: 'Bridge server URL' },
43
+ outputDir: { type: 'string', default: '/tmp/grok-output', description: 'Output directory' },
44
+ globalStyle: { type: 'string', default: '', description: 'Global style prefix for prompts' },
45
+ filename: { type: 'string', default: '', description: 'Output filename (without extension)' },
46
+ enableUpscale: { type: 'boolean', default: false, description: 'HD upscale for videos' },
47
+ workerId: { type: 'string', default: '', description: 'Worker ID for multi-tab' },
48
+ // Batch params
49
+ segments: { type: 'any', default: null, description: 'Segments for batch operations' },
50
+ workers: { type: 'int', default: 1, description: 'Parallel workers for batch' },
51
+ // Image params
52
+ imagePath: { type: 'string', default: '', description: 'Source image for video gen' },
53
+ videoPrompt: { type: 'string', default: '', description: 'Camera movement prompt for video' },
54
+ },
55
+ },
56
+
57
+ lifecycle: {
58
+ validate: (inputs, params) => {
59
+ if (params.operation === 'check') return true;
60
+ if (params.operation === 'image' && !inputs.prompt) return false;
61
+ if (params.operation === 'image-edit' && (!inputs.prompt || !inputs.referencePath)) return false;
62
+ if (params.operation === 'video' && !params.imagePath) return false;
63
+ return true;
64
+ },
65
+
66
+ cacheKey: (inputs, params) => {
67
+ return `grok:${params.operation}:${params.filename || inputs.prompt?.substring(0, 30) || ''}`;
68
+ },
69
+
70
+ execute: async (inputs, params) => {
71
+ try {
72
+ const op = params.operation;
73
+ const bridge = createBridgeClient(params.bridgeUrl);
74
+
75
+ if (op === 'check') {
76
+ const ok = await bridge.checkHealth();
77
+ return { result: { healthy: ok }, error: null };
78
+ }
79
+
80
+ if (op === 'image') {
81
+ const result = await generateImage(bridge, inputs.prompt, params);
82
+ return { result, error: null };
83
+ }
84
+
85
+ if (op === 'image-edit') {
86
+ const result = await editImage(bridge, inputs.prompt, inputs.referencePath, params);
87
+ return { result, error: null };
88
+ }
89
+
90
+ if (op === 'video') {
91
+ const result = await generateVideo(bridge, params);
92
+ return { result, error: null };
93
+ }
94
+
95
+ if (op === 'batch-images') {
96
+ const results = await batchImages(bridge, params);
97
+ return { result: results, error: null };
98
+ }
99
+
100
+ if (op === 'batch-videos') {
101
+ const results = await batchVideos(bridge, params);
102
+ return { result: results, error: null };
103
+ }
104
+
105
+ return { result: null, error: `Unknown operation: ${op}` };
106
+ } catch (err) {
107
+ return { result: null, error: err.message };
108
+ }
109
+ },
110
+ },
111
+ };
112
+
113
+ // --- Bridge Client ---
114
+
115
+ /**
116
+ * Create bridge client for communication with grok-bridge Chrome extension
117
+ * @param {string} baseUrl
118
+ * @returns {Object}
119
+ */
120
+ function createBridgeClient(baseUrl) {
121
+ const sleep = (ms) => new Promise(r => setTimeout(r, ms));
122
+
123
+ async function sendCommand(action, cmdParams = {}, timeout = 30000, workerId = null) {
124
+ const payload = { action, params: cmdParams };
125
+ if (workerId) payload.workerId = workerId;
126
+
127
+ const sendRes = await fetch(`${baseUrl}/command`, {
128
+ method: 'POST',
129
+ headers: { 'Content-Type': 'application/json' },
130
+ body: JSON.stringify(payload),
131
+ signal: AbortSignal.timeout(10000),
132
+ });
133
+
134
+ if (!sendRes.ok) throw new Error('Failed to send command');
135
+ const { id } = await sendRes.json();
136
+
137
+ const start = Date.now();
138
+ while (Date.now() - start < timeout) {
139
+ await sleep(500);
140
+ try {
141
+ const res = await fetch(`${baseUrl}/result/${id}`, {
142
+ signal: AbortSignal.timeout(5000),
143
+ });
144
+ if (!res.ok) continue;
145
+ const data = await res.json();
146
+ if (data.error && data.error !== 'Result not found or not ready') {
147
+ throw new Error(data.error);
148
+ }
149
+ if (data.result !== undefined) return data.result;
150
+ } catch (e) {
151
+ if (e.message !== 'Result not found or not ready') {
152
+ // Connection issue — retry poll
153
+ }
154
+ }
155
+ }
156
+ throw new Error(`Timeout: ${action}`);
157
+ }
158
+
159
+ return {
160
+ sendCommand,
161
+ sleep,
162
+
163
+ async checkHealth() {
164
+ try {
165
+ const res = await fetch(`${baseUrl}/health`, { signal: AbortSignal.timeout(2000) });
166
+ return res.ok;
167
+ } catch {
168
+ return false;
169
+ }
170
+ },
171
+
172
+ // Atomic actions
173
+ async navigate(url, workerId) { return sendCommand('navigate', { url }, 30000, workerId); },
174
+ async waitFor(selector, timeout = 15000, workerId = null) { return sendCommand('waitForSelector', { selector, timeout }, timeout + 5000, workerId); },
175
+ async click(selector, workerId) { return sendCommand('click', { selector }, 30000, workerId); },
176
+ async type(selector, text, workerId) { return sendCommand('type', { selector, text }, 30000, workerId); },
177
+ async uploadFile(base64, mimeType, filename, workerId) { return sendCommand('uploadFile', { base64, mimeType, filename }, 30000, workerId); },
178
+ async queryAll(selector, workerId) { return sendCommand('querySelectorAll', { selector }, 30000, workerId); },
179
+ async getAttribute(selector, attribute, workerId) { return sendCommand('getAttribute', { selector, attribute }, 30000, workerId); },
180
+ async getPageInfo(workerId) { return sendCommand('getPageInfo', {}, 30000, workerId); },
181
+ async refresh(workerId) { return sendCommand('refresh', {}, 30000, workerId); },
182
+
183
+ // WebSocket-based direct generation
184
+ async generateImageWS(prompt, options, workerId) { return sendCommand('generateImage', { prompt, options }, 90000, workerId); },
185
+ async fetchImage(url, workerId) { return sendCommand('fetchImage', { url }, 30000, workerId); },
186
+ async waitForImageComplete(timeout = 120000, workerId) { return sendCommand('waitForImageComplete', { timeout }, timeout + 5000, workerId); },
187
+
188
+ // Zone-based interaction
189
+ async showZones(layer = 'all', workerId) { return sendCommand('showClickableZones', { layer }, 30000, workerId); },
190
+ async clickZone(zone, workerId) { return sendCommand('clickZone', { zone }, 30000, workerId); },
191
+ async hideZones(workerId) { return sendCommand('hideZones', {}, 30000, workerId); },
192
+ };
193
+ }
194
+
195
+ // --- Selectors ---
196
+
197
+ const SEL = {
198
+ promptEditor: '.tiptap.ProseMirror',
199
+ editPrompt: '[aria-label="Введите для изменения изображения..."]',
200
+ videoPrompt: 'textarea[aria-label="Сделать видео"]',
201
+ imageCard: 'div.group\\/media-post-masonry-card img',
202
+ firstImage: 'div.group\\/media-post-masonry-card:first-child img',
203
+ video: 'video',
204
+ downloadBtn: '[aria-label="Скачать"]',
205
+ sendBtn: '[aria-label="Отправить"]',
206
+ preferenceBtn: 'button:has(svg.lucide-thumbs-up)',
207
+ moderatedContent: 'img[alt="Moderated"], svg.lucide-eye-off',
208
+ errorToast: '[data-sonner-toast][data-type="error"]',
209
+ hdButton: 'button .text-\\[10px\\].font-bold',
210
+ };
211
+
212
+ // --- Workflows ---
213
+
214
+ /**
215
+ * Navigate to /imagine page (skip if already there)
216
+ * @param {Object} bridge
217
+ * @param {string} workerId
218
+ */
219
+ async function ensureOnImagine(bridge, workerId) {
220
+ try {
221
+ const pageInfo = await bridge.getPageInfo(workerId);
222
+ const url = pageInfo?.url || '';
223
+
224
+ if (url.includes('grok.com/imagine') && !url.includes('/imagine/post/')) {
225
+ try {
226
+ await bridge.waitFor(SEL.promptEditor, 5000, workerId);
227
+ return;
228
+ } catch {
229
+ await bridge.refresh(workerId);
230
+ await bridge.sleep(3000);
231
+ }
232
+ }
233
+ } catch { /* not on page */ }
234
+
235
+ await bridge.navigate('https://grok.com/imagine', workerId);
236
+ await bridge.sleep(3000);
237
+ await bridge.waitFor(SEL.promptEditor, 15000, workerId);
238
+ }
239
+
240
+ /**
241
+ * Generate image via WebSocket (text-to-image)
242
+ * @param {Object} bridge
243
+ * @param {string} prompt
244
+ * @param {Object} params
245
+ * @returns {Promise<Object>}
246
+ */
247
+ async function generateImage(bridge, prompt, params) {
248
+ const { outputDir, filename, globalStyle, workerId } = params;
249
+ const fullPrompt = globalStyle ? `${globalStyle}, ${prompt}` : prompt;
250
+
251
+ await mkdir(outputDir, { recursive: true });
252
+
253
+ // Generate via WebSocket
254
+ const wsResult = await bridge.generateImageWS(fullPrompt, {}, workerId || null);
255
+
256
+ if (!wsResult?.imageUrl) {
257
+ throw new Error('No image URL from WebSocket generation');
258
+ }
259
+
260
+ // Download image via bridge (authenticated)
261
+ const imageData = await bridge.fetchImage(wsResult.imageUrl, workerId || null);
262
+
263
+ // Save to file
264
+ const outputName = filename || `grok-${Date.now()}`;
265
+ const outputPath = path.join(outputDir, `${outputName}.png`);
266
+
267
+ const base64Data = imageData.dataUrl.split(',')[1];
268
+ await writeFile(outputPath, Buffer.from(base64Data, 'base64'));
269
+
270
+ return {
271
+ imagePath: outputPath,
272
+ imageUrl: wsResult.imageUrl,
273
+ prompt: fullPrompt,
274
+ };
275
+ }
276
+
277
+ /**
278
+ * Edit image with reference (upload + prompt)
279
+ * @param {Object} bridge
280
+ * @param {string} prompt
281
+ * @param {string} referencePath
282
+ * @param {Object} params
283
+ * @returns {Promise<Object>}
284
+ */
285
+ async function editImage(bridge, prompt, referencePath, params) {
286
+ const { outputDir, filename, workerId } = params;
287
+ await mkdir(outputDir, { recursive: true });
288
+
289
+ // Navigate to /imagine
290
+ await ensureOnImagine(bridge, workerId || null);
291
+
292
+ // Upload reference image
293
+ const imageBuffer = await readFile(path.resolve(referencePath));
294
+ const base64 = imageBuffer.toString('base64');
295
+ const ext = path.extname(referencePath).toLowerCase();
296
+ const mimeType = ext === '.png' ? 'image/png' : 'image/jpeg';
297
+
298
+ await bridge.uploadFile(base64, mimeType, `image${ext}`, workerId || null);
299
+ await bridge.sleep(2000);
300
+
301
+ // Enter edit prompt
302
+ await bridge.waitFor(SEL.editPrompt, 15000, workerId || null);
303
+ await bridge.type(SEL.editPrompt, prompt, workerId || null);
304
+
305
+ // Submit
306
+ await bridge.click(SEL.sendBtn, workerId || null);
307
+
308
+ // Wait for result via WebSocket
309
+ const wsResult = await bridge.waitForImageComplete(120000, workerId || null);
310
+
311
+ if (!wsResult?.firstUrl) {
312
+ throw new Error('No image from edit generation');
313
+ }
314
+
315
+ // Download
316
+ const imageData = await bridge.fetchImage(wsResult.firstUrl, workerId || null);
317
+ const outputName = filename || `grok-edit-${Date.now()}`;
318
+ const outputPath = path.join(outputDir, `${outputName}.png`);
319
+
320
+ const base64Data = imageData.dataUrl.split(',')[1];
321
+ await writeFile(outputPath, Buffer.from(base64Data, 'base64'));
322
+
323
+ return { imagePath: outputPath, imageUrl: wsResult.firstUrl };
324
+ }
325
+
326
+ /**
327
+ * Generate video from image (image-to-video)
328
+ * @param {Object} bridge
329
+ * @param {Object} params
330
+ * @returns {Promise<Object>}
331
+ */
332
+ async function generateVideo(bridge, params) {
333
+ const { imagePath, videoPrompt, outputDir, filename, enableUpscale, workerId } = params;
334
+ await mkdir(outputDir, { recursive: true });
335
+
336
+ // Navigate to /imagine
337
+ await ensureOnImagine(bridge, workerId || null);
338
+
339
+ // Upload image
340
+ const imageBuffer = await readFile(path.resolve(imagePath));
341
+ const base64 = imageBuffer.toString('base64');
342
+ const ext = path.extname(imagePath).toLowerCase();
343
+ const mimeType = ext === '.png' ? 'image/png' : 'image/jpeg';
344
+
345
+ await bridge.uploadFile(base64, mimeType, `image${ext}`, workerId || null);
346
+ await bridge.sleep(3000);
347
+
348
+ // Enter video prompt
349
+ if (videoPrompt) {
350
+ await bridge.waitFor(SEL.videoPrompt, 15000, workerId || null);
351
+ await bridge.type(SEL.videoPrompt, videoPrompt, workerId || null);
352
+ await bridge.sleep(500);
353
+ }
354
+
355
+ // Show zones and submit
356
+ await bridge.showZones('all', workerId || null);
357
+ await bridge.sleep(500);
358
+
359
+ // Find and click submit button (zone-based)
360
+ await bridge.click(SEL.sendBtn, workerId || null);
361
+ try { await bridge.hideZones(workerId || null); } catch { /* ignore */ }
362
+
363
+ // Wait for video
364
+ const videoUrl = await waitForVideo(bridge, 90000, workerId || null);
365
+
366
+ // Download video
367
+ const outputName = filename || `grok-video-${Date.now()}`;
368
+ const outputPath = path.join(outputDir, `${outputName}.mp4`);
369
+
370
+ const videoResponse = await fetch(videoUrl);
371
+ if (!videoResponse.ok) throw new Error(`Failed to download video: ${videoResponse.status}`);
372
+ const buffer = await videoResponse.arrayBuffer();
373
+ await writeFile(outputPath, Buffer.from(buffer));
374
+
375
+ const result = { videoPath: outputPath, videoUrl };
376
+
377
+ // HD upscale if requested
378
+ if (enableUpscale) {
379
+ try {
380
+ await triggerUpscale(bridge, workerId || null);
381
+ const hdUrl = await waitForHD(bridge, 120000, workerId || null, videoUrl);
382
+
383
+ const hdPath = path.join(outputDir, `${outputName}_hd.mp4`);
384
+ const hdResponse = await fetch(hdUrl);
385
+ if (hdResponse.ok) {
386
+ const hdBuffer = await hdResponse.arrayBuffer();
387
+ await writeFile(hdPath, Buffer.from(hdBuffer));
388
+ result.hdVideoPath = hdPath;
389
+ result.hdVideoUrl = hdUrl;
390
+ }
391
+ } catch (e) {
392
+ result.upscaleError = e.message;
393
+ }
394
+ }
395
+
396
+ return result;
397
+ }
398
+
399
+ // --- Helper workflows ---
400
+
401
+ /**
402
+ * Wait for video element with mp4 src
403
+ * @param {Object} bridge
404
+ * @param {number} timeout
405
+ * @param {string} workerId
406
+ * @param {string} prevUrl
407
+ * @returns {Promise<string>}
408
+ */
409
+ async function waitForVideo(bridge, timeout = 90000, workerId = null, prevUrl = null) {
410
+ const start = Date.now();
411
+
412
+ while (Date.now() - start < timeout) {
413
+ await bridge.sleep(3000);
414
+
415
+ // Check rate limit
416
+ try {
417
+ const errors = await bridge.queryAll(SEL.errorToast, workerId);
418
+ if (errors.count > 0) throw new Error('RATE_LIMIT_REACHED');
419
+ } catch (e) {
420
+ if (e.message === 'RATE_LIMIT_REACHED') throw e;
421
+ }
422
+
423
+ // Check content moderation
424
+ try {
425
+ const moderated = await bridge.queryAll(SEL.moderatedContent, workerId);
426
+ if (moderated.count > 0) throw new Error('CONTENT_MODERATED');
427
+ } catch (e) {
428
+ if (e.message === 'CONTENT_MODERATED') throw e;
429
+ }
430
+
431
+ // Check preference selection (A/B test) — refresh to skip
432
+ try {
433
+ const prefs = await bridge.queryAll(SEL.preferenceBtn, workerId);
434
+ if (prefs.count >= 2) {
435
+ await bridge.refresh(workerId);
436
+ await bridge.sleep(3000);
437
+ continue;
438
+ }
439
+ } catch { /* ignore */ }
440
+
441
+ // Check for video
442
+ try {
443
+ const result = await bridge.getAttribute(SEL.video, 'src', workerId);
444
+ const videoUrl = result.value;
445
+ if (videoUrl?.includes('.mp4')) {
446
+ if (prevUrl && videoUrl === prevUrl) continue;
447
+ return videoUrl;
448
+ }
449
+ } catch { /* not yet */ }
450
+ }
451
+
452
+ throw new Error('Timeout waiting for video');
453
+ }
454
+
455
+ /**
456
+ * Trigger HD upscale via menu
457
+ * @param {Object} bridge
458
+ * @param {string} workerId
459
+ */
460
+ async function triggerUpscale(bridge, workerId) {
461
+ await bridge.showZones('all', workerId);
462
+ await bridge.sleep(500);
463
+ // Menu button→upscale is zone-dependent, use click by text as fallback
464
+ try {
465
+ await bridge.click('[aria-label="Больше опций"]', workerId);
466
+ await bridge.sleep(1000);
467
+ // Click 5th menu item (upscale)
468
+ await bridge.click('[role="menuitem"]:nth-child(5)', workerId);
469
+ } catch {
470
+ // Fallback
471
+ await bridge.sendCommand('clickByText', { text: 'Улучшить' }, 30000, workerId);
472
+ }
473
+ try { await bridge.hideZones(workerId); } catch { /* ignore */ }
474
+ }
475
+
476
+ /**
477
+ * Wait for HD video after upscale
478
+ * @param {Object} bridge
479
+ * @param {number} timeout
480
+ * @param {string} workerId
481
+ * @param {string} sdUrl
482
+ * @returns {Promise<string>}
483
+ */
484
+ async function waitForHD(bridge, timeout = 120000, workerId = null, sdUrl = null) {
485
+ const start = Date.now();
486
+
487
+ while (Date.now() - start < timeout) {
488
+ await bridge.sleep(3000);
489
+
490
+ try {
491
+ const hdExists = await bridge.waitFor(SEL.hdButton, 5000, workerId).catch(() => null);
492
+ if (hdExists) {
493
+ const result = await bridge.getAttribute(SEL.video, 'src', workerId);
494
+ if (result.value?.includes('.mp4')) return result.value;
495
+ }
496
+
497
+ if (sdUrl) {
498
+ const result = await bridge.getAttribute(SEL.video, 'src', workerId);
499
+ if (result.value?.includes('.mp4') && result.value !== sdUrl) return result.value;
500
+ }
501
+ } catch { /* not yet */ }
502
+ }
503
+
504
+ throw new Error('Timeout waiting for HD video');
505
+ }
506
+
507
+ // --- Batch processing ---
508
+
509
+ /**
510
+ * Batch image generation
511
+ * @param {Object} bridge
512
+ * @param {Object} params
513
+ * @returns {Promise<Object>}
514
+ */
515
+ async function batchImages(bridge, params) {
516
+ const { segments, outputDir, globalStyle } = params;
517
+ const results = {};
518
+ await mkdir(outputDir, { recursive: true });
519
+
520
+ for (const seg of segments) {
521
+ try {
522
+ const result = await generateImage(bridge, seg.prompt || seg.text, {
523
+ ...params,
524
+ filename: seg.promptId || seg.id,
525
+ });
526
+ results[seg.promptId || seg.id] = result.imagePath;
527
+ } catch (e) {
528
+ console.error(`[GrokBatch] Image failed: ${seg.promptId} - ${e.message}`);
529
+ results[seg.promptId || seg.id] = null;
530
+ }
531
+ }
532
+
533
+ return { total: segments.length, success: Object.values(results).filter(Boolean).length, results };
534
+ }
535
+
536
+ /**
537
+ * Batch video generation
538
+ * @param {Object} bridge
539
+ * @param {Object} params
540
+ * @returns {Promise<Object>}
541
+ */
542
+ async function batchVideos(bridge, params) {
543
+ const { segments, outputDir } = params;
544
+ const results = {};
545
+ await mkdir(outputDir, { recursive: true });
546
+
547
+ for (const seg of segments) {
548
+ if (!seg.imagePath) continue;
549
+
550
+ try {
551
+ const result = await generateVideo(bridge, {
552
+ ...params,
553
+ imagePath: seg.imagePath,
554
+ videoPrompt: seg.videoPrompt || seg.cameraPrompt || '',
555
+ filename: seg.promptId || seg.id,
556
+ });
557
+ results[seg.promptId || seg.id] = result.videoPath;
558
+ } catch (e) {
559
+ console.error(`[GrokBatch] Video failed: ${seg.promptId} - ${e.message}`);
560
+ results[seg.promptId || seg.id] = null;
561
+ }
562
+ }
563
+
564
+ return { total: segments.length, success: Object.values(results).filter(Boolean).length, results };
565
+ }