Skip to content

Text to vertical video in 100+ styles: AI codes each scene (HTML/SVG/WebGL), renders a 9:16 MP4 with music and English/Vietnamese voice. MCP, CLI and REST for AI agents.

4.8 KB
Raw Blame
// Thử MCP server bằng client stdio thật (SDK). Dùng: node scripts/mcp-smoke.mjs [jobIdNguồn] [--quick]
// Luồng: list_templates → get_template → create_video(agent) → submit_scene (cảnh của 1 job cũ) → preview_job → render_job.
// --quick: dừng sau submit_scene (không preview, không render). Không có job nguồn thì bỏ qua submit_scene.
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';

const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const QUICK = process.argv.includes('--quick');
const SRC_JOB = process.argv.slice(2).find((a) => !a.startsWith('--')) || '261127064317-009-64b1';

// Không bao giờ treo vô hạn: quá 300 s thì in bước đang chờ rồi thoát mã 1. Khởi động MCP server lâu nhất là nạp zod +
// MCP SDK từ node_modules (đo 2–26 s trên ổ F: khi máy bận), không phải do tool nào.
let phase = 'khởi động MCP server';
const T0 = Date.now();
setTimeout(() => { console.error(`smoke quá 300 s, đang chờ: ${phase}`); process.exit(1); }, 300_000).unref();
const transport = new StdioClientTransport({ command: process.execPath, args: [path.join(ROOT, 'mcp', 'server.mjs')], cwd: ROOT, env: { ...process.env }, stderr: 'ignore' });
const client = new Client({ name: 'smoke', version: '1.0.0' });
await client.connect(transport);
console.log(`kết nối MCP server: ${((Date.now() - T0) / 1000).toFixed(1)} s`);
const short = (r) => r.content.map((c) => (c.type === 'text' ? c.text.slice(0, 400) : `[${c.type} ${c.mimeType} ${Math.round(c.data.length * 0.75 / 1024)} KB]`)).join('\n');
const call = async (name, args, opts) => {
  phase = name;
  const t = Date.now();
  const r = await client.callTool({ name, arguments: args }, undefined, opts);
  console.log(`\n=== ${name} (${((Date.now() - t) / 1000).toFixed(1)}s)${r.isError ? ' LỖI' : ''}\n${short(r)}`);
  return r;
};

const tools = await client.listTools();
console.log('tools:', tools.tools.map((t) => t.name).join(', '));
for (const t of ['edit_video', 'revise_edit', 'get_edit']) if (!tools.tools.some((x) => x.name === t)) { console.error('thiếu tool ' + t); process.exitCode = 1; }
await call('get_edit', {});
await call('edit_video', { path: path.join(ROOT, 'khong-co-file.mp4') }); // phải báo lỗi rõ ràng
await call('revise_edit', { editId: '../../etc', instruction: 'x' }); // phải bị chặn
await call('list_templates', { query: 'tài chính' });
const gt = await call('get_template', { id: '009' });
if (gt.isError || !gt.content[0].text.includes('Ví dụ chỉ để hiểu định dạng ô')) { console.error('get_template thiếu guidance'); process.exitCode = 1; }
const created = await call('create_video', {
  templateId: '009', mode: 'agent',
  content: 'Mỗi tháng bỏ 3 triệu vào quỹ chỉ số, lãi giả định 10%/năm, sau 20 năm có bao nhiêu? Kênh: Lumfin. CTA: Theo dõi để biết thêm mẹo tiền',
});
const jobId = JSON.parse(created.content[0].text).jobId;
const srcHtml = path.join(ROOT, 'jobs', SRC_JOB, 'scene', 'index.html');
if (!fs.existsSync(srcHtml)) { console.log(`
(không có job nguồn ${SRC_JOB}: bỏ qua submit_scene/preview/render)`); await client.close(); process.exit(); }
const html = fs.readFileSync(srcHtml, 'utf8');
const brief = JSON.parse(fs.readFileSync(path.join(ROOT, 'jobs', SRC_JOB, 'brief.json'), 'utf8'));
await call('submit_scene', { jobId, html: html.replace('fetch', 'fetch'), brief });
await call('submit_scene', { jobId: '../../etc', html }); // phải bị chặn
await call('submit_scene', { jobId, html: html.replace('</body>', '<script src="https://cdn.example.com/x.js"></script></body>') }); // phải bị chặn
if (QUICK) { await call('get_job', { jobId }); await client.close(); process.exit(); }
for (let i = 0; i < 8; i++) { // máy bận thì preview_job trả pending: gọi lại
  const r = await call('preview_job', { jobId }, { timeout: 120000 });
  if (!(r.content[0].type === 'text' && r.content[0].text.includes('"pending": true'))) break;
}
await call('render_job', { jobId }, { timeout: 180000 });
// render chạy ở tiến trình nền: hỏi get_job tới khi xong (tối đa 6 phút)
for (let i = 0; i < 36; i++) {
  const r = await client.callTool({ name: 'get_job', arguments: { jobId } });
  const j = JSON.parse(r.content[0].text);
  if (['done', 'error'].includes(j.status)) {
    console.log(`\n=== get_job: ${j.status} ${j.output ? `${j.output.width}x${j.output.height} ${j.output.fps} ${j.output.duration}s` : ''}\nmp4: ${j.files.mp4}\nexported: ${j.output?.exported || '-'}${j.error ? '\nlỗi: ' + j.error : ''}`);
    break;
  }
  await new Promise((res) => setTimeout(res, 10000));
}
await client.close();