Files
video-create/web/server/agent/tools/generate-images.ts

47 lines
2.0 KiB
TypeScript
Raw Normal View History

import { execSync } from 'child_process';
import { runInit, loadJSON, PIPELINE_SCRIPT, PROJECT_ROOT } from './shared';
import type { ToolDefinition } from './types';
export const generateImages: ToolDefinition = {
name: 'generate_images',
description: '文生图:根据文本提示词生成图片。内部创建临时 manifest 并调用 pipeline images 阶段,支持批量生成。生成结果写入 output 目录,返回图片文件路径列表。',
input_schema: {
type: 'object',
properties: {
accountId: { type: 'string', description: '账号ID用于继承模型、参考图等配置' },
prompt: { type: 'string', description: '图片提示词imagePrompt描述要生成的图片内容' },
count: { type: 'number', description: '生成图片数量,默认 1' },
imageModel: { type: 'string', description: '生图模型(可选,默认继承账号配置): gemini, mj, gpt-image, kling' },
},
required: ['accountId', 'prompt'],
},
execute: async (params) => {
const { accountId, prompt, count, imageModel } = params as Record<string, unknown>;
const num = Math.max(1, Number(count) || 1);
const items = Array.from({ length: num }, (_, i) => ({
id: i + 1,
shotDesc: prompt as string,
script: '',
imagePrompt: prompt as string,
keyword: 'generated',
}));
const manifestPath = runInit({
account: accountId as string,
mode: 'single',
items,
imageModel: imageModel as string | undefined,
});
execSync(`node "${PIPELINE_SCRIPT}" run --manifest "${manifestPath}" --phase images`, {
cwd: PROJECT_ROOT, encoding: 'utf-8',
});
const manifest = loadJSON(manifestPath) as { items?: Array<{ id: number; file?: string; candidates?: string[]; status?: string }> };
const results = (manifest.items || []).map((item) => ({
id: item.id,
file: item.file || null,
candidates: item.candidates || [],
status: item.status,
}));
return JSON.stringify({ manifestPath, images: results }, null, 2);
},
};