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,414 @@
1
+ /**
2
+ * ai/kling-lipsync — Kling API lip-sync animation
3
+ *
4
+ * Adds lip-sync mouth animation to videos via Kling API.
5
+ * JWT authentication with HMAC-SHA256 signing.
6
+ *
7
+ * 3-step pipeline:
8
+ * 1. identify-face: detect faces in video → session_id + face_id
9
+ * 2. advanced-lip-sync: create task with audio + face → task_id
10
+ * 3. poll: wait for completion → download result video
11
+ *
12
+ * Ported from Mr-Computer/modules/ai-music-video/src/services/kling-lipsync.js
13
+ *
14
+ * @module agi-graph/packs/ai/kling-lipsync
15
+ */
16
+
17
+ import { createHmac } from 'crypto';
18
+ import { readFile, writeFile, mkdir } from 'fs/promises';
19
+ import { existsSync } from 'fs';
20
+ import { execSync } from 'child_process';
21
+ import path from 'path';
22
+
23
+ export default {
24
+ type: 'ai/kling-lipsync',
25
+ category: 'ai',
26
+ icon: 'record_voice_over',
27
+
28
+ driver: {
29
+ description: 'Kling API lip-sync: detect face → animate mouth → download result',
30
+ inputs: [
31
+ { name: 'videoUrl', type: 'string' },
32
+ { name: 'audioPath', type: 'string' },
33
+ ],
34
+ outputs: [
35
+ { name: 'result', type: 'any' },
36
+ { name: 'error', type: 'string' },
37
+ ],
38
+ params: {
39
+ operation: { type: 'string', default: 'lipsync', description: 'Operation: identify-face | lipsync | poll | batch' },
40
+ accessKey: { type: 'string', default: '', description: 'Kling API access key' },
41
+ secretKey: { type: 'string', default: '', description: 'Kling API secret key' },
42
+ baseUrl: { type: 'string', default: 'https://api.klingai.com', description: 'Kling API base URL' },
43
+ publicBaseUrl: { type: 'string', default: '', description: 'Public URL for file server (ngrok/cloudflared)' },
44
+ outputDir: { type: 'string', default: '/tmp/kling-lipsync', description: 'Output directory for results' },
45
+ // For poll operation
46
+ taskId: { type: 'string', default: '', description: 'Task ID to poll' },
47
+ maxWaitMs: { type: 'int', default: 300000, description: 'Max poll wait time (ms)' },
48
+ // For lipsync operation
49
+ startTime: { type: 'number', default: 0, description: 'Audio start time (seconds)' },
50
+ endTime: { type: 'number', default: 0, description: 'Audio end time (seconds)' },
51
+ segmentId: { type: 'string', default: '', description: 'Segment identifier' },
52
+ // For batch operation
53
+ segments: { type: 'any', default: null, description: 'Segments array with start/end/promptId' },
54
+ videoMap: { type: 'any', default: null, description: 'Map of promptId → videoPath' },
55
+ concurrency: { type: 'int', default: 2, description: 'Max concurrent batch tasks' },
56
+ },
57
+ },
58
+
59
+ lifecycle: {
60
+ validate: (inputs, params) => {
61
+ if (!params.accessKey || !params.secretKey) return false;
62
+
63
+ const op = params.operation;
64
+ if (op === 'identify-face' && !inputs.videoUrl) return false;
65
+ if (op === 'poll' && !params.taskId) return false;
66
+ if (op === 'lipsync' && (!inputs.videoUrl || !inputs.audioPath)) return false;
67
+ if (op === 'batch' && (!params.segments || !inputs.audioPath)) return false;
68
+
69
+ return true;
70
+ },
71
+
72
+ cacheKey: (inputs, params) => {
73
+ return `kling:${params.operation}:${params.segmentId || params.taskId || inputs.videoUrl || ''}`;
74
+ },
75
+
76
+ execute: async (inputs, params) => {
77
+ try {
78
+ const op = params.operation;
79
+ const token = generateJWT(params.accessKey, params.secretKey);
80
+
81
+ if (op === 'identify-face') {
82
+ const data = await identifyFace(inputs.videoUrl, token, params.baseUrl);
83
+ return { result: data, error: null };
84
+ }
85
+
86
+ if (op === 'poll') {
87
+ const data = await pollTaskCompletion(params.taskId, token, params);
88
+ return { result: data, error: null };
89
+ }
90
+
91
+ if (op === 'lipsync') {
92
+ const result = await processSegment(inputs, params);
93
+ return { result, error: null };
94
+ }
95
+
96
+ if (op === 'batch') {
97
+ const results = await processBatch(inputs, params);
98
+ return { result: { processed: results.size, results: Object.fromEntries(results) }, error: null };
99
+ }
100
+
101
+ return { result: null, error: `Unknown operation: ${op}` };
102
+ } catch (err) {
103
+ return { result: null, error: err.message };
104
+ }
105
+ },
106
+ },
107
+ };
108
+
109
+ // --- JWT Authentication ---
110
+
111
+ /**
112
+ * Generate JWT token for Kling API authentication
113
+ * @param {string} accessKey
114
+ * @param {string} secretKey
115
+ * @returns {string}
116
+ */
117
+ function generateJWT(accessKey, secretKey) {
118
+ const header = { alg: 'HS256', typ: 'JWT' };
119
+ const now = Math.floor(Date.now() / 1000);
120
+ const payload = {
121
+ iss: accessKey,
122
+ exp: now + 1800,
123
+ nbf: now - 5,
124
+ };
125
+
126
+ const base64Header = Buffer.from(JSON.stringify(header)).toString('base64url');
127
+ const base64Payload = Buffer.from(JSON.stringify(payload)).toString('base64url');
128
+
129
+ const signature = createHmac('sha256', secretKey)
130
+ .update(`${base64Header}.${base64Payload}`)
131
+ .digest('base64url');
132
+
133
+ return `${base64Header}.${base64Payload}.${signature}`;
134
+ }
135
+
136
+ // --- API Operations ---
137
+
138
+ /**
139
+ * Step 1: Identify face in video
140
+ * @param {string} videoUrl
141
+ * @param {string} token
142
+ * @param {string} baseUrl
143
+ * @returns {Promise<Object>}
144
+ */
145
+ async function identifyFace(videoUrl, token, baseUrl) {
146
+ const response = await fetch(`${baseUrl}/v1/videos/identify-face`, {
147
+ method: 'POST',
148
+ headers: {
149
+ 'Content-Type': 'application/json',
150
+ 'Authorization': `Bearer ${token}`,
151
+ },
152
+ body: JSON.stringify({ video_url: videoUrl }),
153
+ });
154
+
155
+ if (!response.ok) {
156
+ const errorText = await response.text();
157
+ throw new Error(`Kling identify-face error: ${response.status} - ${errorText}`);
158
+ }
159
+
160
+ const result = await response.json();
161
+ if (result.code !== 0) {
162
+ throw new Error(`Kling API error: ${result.code} - ${result.message}`);
163
+ }
164
+
165
+ return result.data;
166
+ }
167
+
168
+ /**
169
+ * Step 2: Create lip-sync task
170
+ * @param {string} sessionId
171
+ * @param {string} faceId
172
+ * @param {string} soundFile - Base64 audio
173
+ * @param {number} soundDurationMs
174
+ * @param {number} faceStartMs
175
+ * @param {string} token
176
+ * @param {string} baseUrl
177
+ * @returns {Promise<Object>}
178
+ */
179
+ async function createLipsyncTask(sessionId, faceId, soundFile, soundDurationMs, faceStartMs, token, baseUrl) {
180
+ const response = await fetch(`${baseUrl}/v1/videos/advanced-lip-sync`, {
181
+ method: 'POST',
182
+ headers: {
183
+ 'Content-Type': 'application/json',
184
+ 'Authorization': `Bearer ${token}`,
185
+ },
186
+ body: JSON.stringify({
187
+ session_id: sessionId,
188
+ face_choose: [{
189
+ face_id: faceId,
190
+ sound_file: soundFile,
191
+ sound_start_time: 0,
192
+ sound_end_time: soundDurationMs,
193
+ sound_insert_time: faceStartMs,
194
+ sound_volume: 1,
195
+ original_audio_volume: 0,
196
+ }],
197
+ }),
198
+ });
199
+
200
+ if (!response.ok) {
201
+ const errorText = await response.text();
202
+ throw new Error(`Kling advanced-lip-sync error: ${response.status} - ${errorText}`);
203
+ }
204
+
205
+ const result = await response.json();
206
+ if (result.code !== 0) {
207
+ throw new Error(`Kling API error: ${result.code} - ${result.message}`);
208
+ }
209
+
210
+ return result.data;
211
+ }
212
+
213
+ /**
214
+ * Step 3: Poll for task completion
215
+ * @param {string} taskId
216
+ * @param {string} token
217
+ * @param {Object} params
218
+ * @returns {Promise<Object>}
219
+ */
220
+ async function pollTaskCompletion(taskId, token, params) {
221
+ const startTime = Date.now();
222
+ const maxWaitMs = params.maxWaitMs;
223
+ const pollInterval = 5000;
224
+
225
+ while (Date.now() - startTime < maxWaitMs) {
226
+ // Refresh token for each poll
227
+ const freshToken = generateJWT(params.accessKey, params.secretKey);
228
+
229
+ const response = await fetch(`${params.baseUrl}/v1/videos/advanced-lip-sync/${taskId}`, {
230
+ method: 'GET',
231
+ headers: { 'Authorization': `Bearer ${freshToken}` },
232
+ });
233
+
234
+ if (!response.ok) {
235
+ const errorText = await response.text();
236
+ throw new Error(`Kling poll error: ${response.status} - ${errorText}`);
237
+ }
238
+
239
+ const result = await response.json();
240
+ if (result.code !== 0) {
241
+ throw new Error(`Kling API error: ${result.code} - ${result.message}`);
242
+ }
243
+
244
+ const status = result.data?.task_status;
245
+
246
+ if (status === 'succeed') {
247
+ return result.data;
248
+ }
249
+ if (status === 'failed') {
250
+ throw new Error(`Lipsync task failed: ${result.data?.task_status_msg || 'Unknown error'}`);
251
+ }
252
+
253
+ await new Promise(resolve => setTimeout(resolve, pollInterval));
254
+ }
255
+
256
+ throw new Error(`Lipsync task timed out after ${maxWaitMs / 1000}s`);
257
+ }
258
+
259
+ /**
260
+ * Download result video
261
+ * @param {string} videoUrl
262
+ * @param {string} outputPath
263
+ * @returns {Promise<string>}
264
+ */
265
+ async function downloadResult(videoUrl, outputPath) {
266
+ const response = await fetch(videoUrl);
267
+ if (!response.ok) {
268
+ throw new Error(`Failed to download video: ${response.status}`);
269
+ }
270
+
271
+ const buffer = await response.arrayBuffer();
272
+ await writeFile(outputPath, Buffer.from(buffer));
273
+ return outputPath;
274
+ }
275
+
276
+ // --- Audio Utilities ---
277
+
278
+ /**
279
+ * Extract audio clip using FFmpeg
280
+ * @param {string} audioPath
281
+ * @param {number} startTime
282
+ * @param {number} endTime
283
+ * @param {string} outputPath
284
+ * @returns {string}
285
+ */
286
+ function extractAudioClip(audioPath, startTime, endTime, outputPath) {
287
+ if (existsSync(outputPath)) return outputPath;
288
+
289
+ const duration = endTime - startTime;
290
+ const cmd = `ffmpeg -y -i "${path.resolve(audioPath)}" -ss ${startTime.toFixed(3)} -t ${duration.toFixed(3)} ` +
291
+ `-c:a libmp3lame -q:a 2 "${outputPath}" 2>/dev/null`;
292
+
293
+ execSync(cmd, { stdio: 'pipe' });
294
+ return outputPath;
295
+ }
296
+
297
+ /**
298
+ * Convert audio file to base64 data URI
299
+ * @param {string} audioPath
300
+ * @returns {Promise<string>}
301
+ */
302
+ async function audioToBase64(audioPath) {
303
+ const buffer = await readFile(path.resolve(audioPath));
304
+ return `data:audio/mpeg;base64,${buffer.toString('base64')}`;
305
+ }
306
+
307
+ // --- High-level pipeline ---
308
+
309
+ /**
310
+ * Process single segment: extract audio → identify face → create task → poll → download
311
+ * @param {Object} inputs
312
+ * @param {Object} params
313
+ * @returns {Promise<Object>}
314
+ */
315
+ async function processSegment(inputs, params) {
316
+ const { startTime, endTime, segmentId, outputDir, accessKey, secretKey, baseUrl } = params;
317
+
318
+ const lipsyncDir = path.join(outputDir, 'lipsync-videos');
319
+ const clipsDir = path.join(outputDir, 'audio-clips');
320
+ await mkdir(lipsyncDir, { recursive: true });
321
+ await mkdir(clipsDir, { recursive: true });
322
+
323
+ const outputPath = path.join(lipsyncDir, `${segmentId}.mp4`);
324
+ if (existsSync(outputPath)) {
325
+ return { videoPath: outputPath, cached: true };
326
+ }
327
+
328
+ // 1. Extract audio clip
329
+ const clipPath = path.join(clipsDir, `${segmentId}.mp3`);
330
+ extractAudioClip(inputs.audioPath, startTime, endTime, clipPath);
331
+ const audioDurationMs = Math.round((endTime - startTime) * 1000);
332
+
333
+ // 2. Convert to base64
334
+ const audioBase64 = await audioToBase64(clipPath);
335
+
336
+ // 3. Identify face
337
+ let token = generateJWT(accessKey, secretKey);
338
+ const faceData = await identifyFace(inputs.videoUrl, token, baseUrl);
339
+
340
+ if (!faceData.face_data || faceData.face_data.length === 0) {
341
+ throw new Error('No face detected in video');
342
+ }
343
+
344
+ const face = faceData.face_data[0];
345
+
346
+ // 4. Create task
347
+ token = generateJWT(accessKey, secretKey);
348
+ const task = await createLipsyncTask(
349
+ faceData.session_id,
350
+ face.face_id,
351
+ audioBase64,
352
+ audioDurationMs,
353
+ face.start_time || 0,
354
+ token,
355
+ baseUrl,
356
+ );
357
+
358
+ // 5. Poll
359
+ const result = await pollTaskCompletion(task.task_id, token, params);
360
+
361
+ // 6. Download
362
+ const resultVideoUrl = result.task_result?.videos?.[0]?.url;
363
+ if (!resultVideoUrl) {
364
+ throw new Error('No video URL in task result');
365
+ }
366
+
367
+ await downloadResult(resultVideoUrl, outputPath);
368
+ return { videoPath: outputPath, taskId: task.task_id, cached: false };
369
+ }
370
+
371
+ /**
372
+ * Process batch of segments with concurrency
373
+ * @param {Object} inputs
374
+ * @param {Object} params
375
+ * @returns {Promise<Map>}
376
+ */
377
+ async function processBatch(inputs, params) {
378
+ const segments = params.segments;
379
+ const videoMap = params.videoMap || {};
380
+ const concurrency = params.concurrency;
381
+ const results = new Map();
382
+
383
+ for (let i = 0; i < segments.length; i += concurrency) {
384
+ const batch = segments.slice(i, i + concurrency);
385
+
386
+ const batchResults = await Promise.allSettled(
387
+ batch.map(async (segment) => {
388
+ const videoUrl = videoMap[segment.promptId];
389
+ if (!videoUrl) return null;
390
+
391
+ const segParams = {
392
+ ...params,
393
+ segmentId: segment.promptId,
394
+ startTime: segment.start,
395
+ endTime: segment.end,
396
+ };
397
+
398
+ const result = await processSegment(
399
+ { videoUrl, audioPath: inputs.audioPath },
400
+ segParams,
401
+ );
402
+ return { promptId: segment.promptId, ...result };
403
+ })
404
+ );
405
+
406
+ for (const result of batchResults) {
407
+ if (result.status === 'fulfilled' && result.value) {
408
+ results.set(result.value.promptId, result.value.videoPath);
409
+ }
410
+ }
411
+ }
412
+
413
+ return results;
414
+ }