Files
openmotion/shared/platform-export.ts
Tom Huang 89d80cbc40 Add configurable platform export handoffs for external Agents (#535)
* feat(export): add platform-specific Agent handoffs and guidance

Add configurable platform briefs, branded destination choices and a bounded export panel with fixed actions. Share scenario guidance with the MCP handbook and retain project identity, revision protection and manual upload boundaries.

Cover prompt contracts, clipboard recovery, responsive scrolling, packaging, and the real Studio/MCP/export/playback workflow.

* Fix platform export regression fixtures and shared translations
2026-11-17 18:20:37 +08:00

109 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { guideUrl, setupGuideUrl, type IntegrationInfo } from './integration.ts';
import { projectAgentContext } from './project-agent-prompt.ts';
/** Editorial starting points, not platform upload limits or encoder presets. */
export const platformExportTargets = [
{ id: 'douyin', label: 'Douyin', aspect: '9:16', width: 1080, height: 1920 },
{ id: 'xiaohongshu', label: 'Xiaohongshu', aspect: '9:16', width: 1080, height: 1920 },
{ id: 'wechat', label: 'WeChat Channels', aspect: '9:16', width: 1080, height: 1920 },
{ id: 'bilibili', label: 'Bilibili', aspect: '16:9', width: 1920, height: 1080 },
{ id: 'tiktok', label: 'TikTok', aspect: '9:16', width: 1080, height: 1920 },
{ id: 'reels', label: 'Instagram Reels', aspect: '9:16', width: 1080, height: 1920 },
{ id: 'shorts', label: 'YouTube Shorts', aspect: '9:16', width: 1080, height: 1920 },
] as const;
export const platformExportGoals = [
{ id: 'shorten', label: 'Make a short version', description: 'Keep the main idea in one concise video.' },
{ id: 'highlights', label: 'Extract highlights', description: 'Choose moments that each tell a complete story.' },
{ id: 'adapt', label: 'Adapt the full video', description: 'Keep the full story and adapt its framing and layout.' },
] as const;
export type PlatformExportTarget = typeof platformExportTargets[number]['id'];
export type PlatformExportGoal = typeof platformExportGoals[number]['id'];
export const defaultPlatformExportTarget = (locale: string): PlatformExportTarget => locale === 'zh-CN' ? 'douyin' : 'shorts';
export const platformExportCanvases = [
{ id: '9:16', width: 1080, height: 1920, label: 'Vertical' },
{ id: '16:9', width: 1920, height: 1080, label: 'Landscape' },
{ id: '1:1', width: 1080, height: 1080, label: 'Square' },
{ id: '4:5', width: 1080, height: 1350, label: 'Portrait' },
] as const;
export const platformExportDurations = ['15–30', '30–60', '60–90'] as const;
export const platformExportCaptions = [
{ id: 'preserve', label: 'Keep existing captions' },
{ id: 'add', label: 'Add editable captions' },
{ id: 'none', label: 'No captions' },
] as const;
export type PlatformExportOptions = {
canvas?: 'platform' | typeof platformExportCanvases[number]['id'];
duration?: typeof platformExportDurations[number];
clipCount?: 1 | 2 | 3;
captions?: typeof platformExportCaptions[number]['id'];
};
export function platformExportSettings(target: PlatformExportTarget, options: PlatformExportOptions = {}) {
const platform = platformExportTargets.find(item => item.id === target);
const canvas = platformExportCanvases.find(item => item.id === (!options.canvas || options.canvas === 'platform' ? platform?.aspect : options.canvas));
const duration = options.duration ?? '30–60', clipCount = options.clipCount ?? 3, captions = options.captions ?? 'preserve';
if (!platform || !canvas || !platformExportDurations.includes(duration) || ![1, 2, 3].includes(clipCount) || !platformExportCaptions.some(item => item.id === captions)) throw new Error(`Unknown platform export settings: ${JSON.stringify({ canvas: options.canvas, duration, clipCount, captions })}`);
return { canvas, duration, clipCount, captions };
}
export function platformExportPrompt({ target, goal, locale = 'en', notes = '', context, workspace, options }: {
target: PlatformExportTarget;
goal: PlatformExportGoal;
locale?: string;
notes?: string;
options?: PlatformExportOptions;
context?: { info: Pick<IntegrationInfo, 'previewUrl' | 'dataDirectory'>; project: { id: string; title: string } };
workspace?: Pick<IntegrationInfo, 'previewUrl' | 'dataDirectory'>;
}): string {
const platform = platformExportTargets.find(item => item.id === target);
if (!platform || !platformExportGoals.some(item => item.id === goal)) throw new Error('Unknown platform export scenario');
const { canvas, duration, clipCount, captions } = platformExportSettings(target, options);
const zh = locale === 'zh-CN';
const selection = zh ? '先用 openmotion_get_project 列出工程,让我指定源工程;不要猜测或修改其他工程。'
: 'First list projects with openmotion_get_project and ask me to identify the source project; do not guess or modify another project.';
const connection = workspace ? (zh ? `工作区:${workspace.dataDirectory}\nStudio 地址:${workspace.previewUrl}\n接入指引:${guideUrl(workspace, 'mcp')}`
: `Workspace: ${workspace.dataDirectory}\nStudio URL: ${workspace.previewUrl}\nConnection guide: ${guideUrl(workspace, 'mcp')}`)
: `${zh ? '接入指引' : 'Connection guide'}: ${setupGuideUrl(null)}`;
const identity = context ? projectAgentContext(context.info, context.project, locale) : `${connection}\n\n${selection}`;
const intentions = zh ? {
shorten: `将长视频精简为一条内容完整的 ${duration} 秒短片,保留核心观点和必要上下文。`,
highlights: `从长视频中选择最多 ${clipCount} 段可独立理解的精彩内容,每条以 ${duration} 秒为目标,并分别交付。宁可少选,也不要凑数。`,
adapt: '保留完整内容、顺序和原有时长,只适配画幅、构图、文字与字幕布局。',
} : {
shorten: `Condense the long video into one self-contained ${duration} second short, preserving its main point and necessary context.`,
highlights: `Select up to ${['zero', 'one', 'two', 'three'][clipCount]} standalone highlights, targeting ${duration} seconds each, and deliver them separately. Prefer fewer strong clips to filling a quota.`,
adapt: 'Preserve the complete content, sequence and original duration; adapt only the aspect ratio, framing, text and subtitle layout.',
};
const brief = zh ? `请通过 OpenMotion MCP 为 ${platform.label} 制作平台版本。
创作目标:${intentions[goal]}
建议画幅:${canvas.id},${canvas.width}×${canvas.height}。这是创作目标,不是平台上传限制或编码预设。原片较短时不要为了凑时长补内容。`
: `Use OpenMotion MCP to prepare a version for ${platform.label}.
Creative goal: ${intentions[goal]}
Suggested canvas: ${canvas.id}, ${canvas.width}×${canvas.height}. These are creative targets, not platform upload limits or encoding presets. Do not pad a shorter source to meet the duration target.
Respond in the user's language (${locale}).`;
const captionBrief = zh ? {
preserve: '字幕:保留现有字幕并适配布局,不自动添加新字幕。',
add: '字幕:根据真实转录、脚本或已确认字幕添加可编辑字幕。缺少内容时先索取,不编造。',
none: '字幕:在副本中移除可编辑字幕,保留原始声音与其他文字。若字幕已烧录进素材,说明无法无损移除,不承诺去除。',
} : {
preserve: 'Captions: preserve existing captions and adapt their layout; do not add new captions automatically.',
add: 'Captions: add editable captions from a real transcript, script or confirmed captions. Ask for missing content; never invent it.',
none: 'Captions: remove editable caption layers in the copy, keeping original audio and other text. Disclose burned-in captions that cannot be removed losslessly; do not promise their removal.',
};
const steps = zh ? `1. 连接上面指定的工作区,读取 openmotion_get_project 的最新内容、revision 和 currentVersionId。根据叙事改编任务读取 openmotion-director 及相关共享工程指引。
2. 根据真实素材、字幕、脚本或已确认时间码制定剪辑计划。需要理解语音或画面时,使用宿主已有的分析/转录工具;若不可用,说明缺少什么并索取字幕或时间码,不编造台词、选段或人脸追踪结果。
3. 用 openmotion_manage_project(action=duplicate) 和 expectedRevision 创建独立适配工程,每条成片一份;保留源工程不变。为本次操作使用稳定 requestId,重试复用已有副本,不重复创建。
4. 对副本读取最新 revision 和 currentVersionId,再用 openmotion_edit_project 携带 expectedRevision、expectedVersionId 修改原生工程。保持品牌、事实和声音含义,重排布局,避免拉伸主体或裁掉文字;将保留或新增的字幕保持为可编辑内容。冲突后重读并核对变化,不盲目覆盖。
5. 用 openmotion_open_preview 打开副本检查画面、字幕和声音,保留可编辑工程。预览初始化可能更新版本,导出前再次读取最新身份。
6. 用 openmotion_export_request 提交该副本的 projectId、versionId、expectedRevision 和稳定 requestId。按素材决定 audio 为 required 或 silent,跟踪同一任务,用 openmotion_get_project(exportJobId) 等待完成及技术验证通过;失败时说明原因,不把旧文件作为本次结果。
7. 实际播放最终 MP4,检查声音、定位与重播;如无法播放,明确未验证的环节。交付每条视频的文件和对应工程。仅记录真实执行的审阅,不自动上传或发布平台。`
: `1. Connect to the identified workspace and read the latest content, revision and currentVersionId with openmotion_get_project. Read openmotion-director and the applicable shared native editing guidance for this editorial adaptation.
2. Plan from real media, captions, scripts or confirmed timecodes. Use the host's available analysis/transcription tools when speech or visual understanding is needed. If unavailable, explain the missing input and ask for captions or timecodes; never invent speech, selections or face-tracking results.
3. Use openmotion_manage_project(action=duplicate) with expectedRevision to create an independent adaptation, one project per output, leaving the source unchanged. Use stable requestIds and reuse the same copies on retries.
4. Read each copy's latest revision and currentVersionId, then edit its native files with openmotion_edit_project using expectedRevision and expectedVersionId. Preserve the brand, facts and audio meaning; recompose layouts without stretching subjects or clipping text, and keep retained or added captions editable. Reread and reconcile conflicts rather than overwriting blindly.
5. Open each copy with openmotion_open_preview to inspect picture, captions and sound, keeping the editable project. Preview initialization can update its version; read the latest identity again before export.
6. Call openmotion_export_request with the copy's projectId, versionId, expectedRevision and a stable requestId. Set audio to required or silent according to the source. Track the same job with openmotion_get_project(exportJobId) until completed and technical verification passes. Report failures; never substitute an old file.
7. Play the actual final MP4, check sound, seeking and replay; disclose any playback checks you cannot perform. Deliver each video file and its project. Record only reviews actually performed. Do not upload or publish to platforms automatically.`;
return `${identity}\n\n${brief}\n${captionBrief[captions]}\n\n${steps}${notes.trim() ? `\n\n${zh ? '我的补充要求' : 'My additional requirements'}:\n${notes.trim()}` : ''}`;
}