Files
video-create/web/server/agent/tools/update-manifest-items.ts

46 lines
1.8 KiB
TypeScript
Raw Normal View History

import path from 'path';
import fs from 'fs';
import { PROJECT_ROOT, loadJSON } from './shared';
import type { ToolDefinition } from './types';
export const updateManifestItems: ToolDefinition = {
name: 'update_manifest_items',
description: '更新 manifest.json 中指定 items 的字段(如 imagePrompt、videoPrompt。只更新提供的字段不覆盖其他字段。',
input_schema: {
type: 'object',
properties: {
manifestPath: { type: 'string', description: 'manifest.json 路径' },
updates: { type: 'string', description: 'JSON 数组,每个元素需包含 idshot 序号)和要更新的字段,如 [{id:1,imagePrompt:"..."},{id:2,imagePrompt:"..."}]' },
},
required: ['manifestPath', 'updates'],
},
execute: async (params) => {
const { manifestPath, updates } = params as { manifestPath: string; updates: string };
const resolved = path.isAbsolute(manifestPath)
? manifestPath
: path.resolve(PROJECT_ROOT, manifestPath);
if (!fs.existsSync(resolved)) return `manifest 不存在: ${resolved}`;
let updateList: any[];
try { updateList = JSON.parse(updates); } catch { return '错误: updates 不是合法 JSON'; }
if (!Array.isArray(updateList)) return '错误: updates 必须是数组';
const manifest = loadJSON(resolved) as { items: any[] };
if (!manifest.items) return '错误: manifest 无 items 数组';
let updated = 0;
for (const upd of updateList) {
const idx = manifest.items.findIndex((item: any) => item.id === upd.id);
if (idx === -1) return `错误: 找不到 id=${upd.id} 的 item`;
const { id, ...fields } = upd;
Object.assign(manifest.items[idx], fields);
updated++;
}
fs.writeFileSync(resolved, JSON.stringify(manifest, null, 2), 'utf-8');
return `已更新 ${updated}/${manifest.items.length} 个 item`;
},
};