The open-source full-stack studio for Opus 5.5 motion video. Reverse engineering for any video. Code video with HTML: recreate, edit and render. BYOA + BYOK · Motion.md · Images, Video, Music & Voice.
TypeScript agent-skillsagentic-aiai-agentsai-videobyokclaude-codecodexdeepseek-harnessdsh-plugingenerative-ailocal-firstmcpmotionmotion-graphicsmultimodalopenmotionreverse-engineeringvideo-editingvideo-generationworkbuddy
fix(convert): rescue HTML when agent uses Write tool (#7) (#9)
Tom Huang4227732663fb
diff --git a/src/app/api/convert/route.ts b/src/app/api/convert/route.tsindex bc3bf1fa..b4e03678 100644--- a/src/app/api/convert/route.ts+++ b/src/app/api/convert/route.ts@@ -37,11 +37,12 @@ function buildEditPrompt(args: {【硬性规则】1. 仅输出完整的、修改后的 HTML。第一个字符必须是 \`<\`, 最后必须是 \`</html>\`。2. **不要**用 markdown 围栏包裹, 不要任何解释性文字。-3. 保留原 HTML 的 \`<head>\` (CDN / 字体 / 样式 / meta), 保留所有不需要变化的 DOM 结构 — 字体、配色、布局、栅格、组件结构、动画都不许改。-4. 仅根据 "旧内容 vs 新内容" 的差异, 替换或调整对应的文字 / 数据节点。-5. 如果新内容增加了条目, 沿用原有的卡片 / 行 / slide / 章节结构添加; 如果删除了条目, 移除对应的元素。-6. 如果新旧内容只差几个字, 也只改那几个字 — 不要顺手 "优化" 或 "重排"。-7. 不要捏造数据。新内容里没有的就不要写。+3. **禁止使用 Write / Edit / MultiEdit / Bash 等文件工具** — HTML 必须直接在助手回复正文里流式输出, 不要存到 \`.html\` 文件再回复"已输出至 …"。+4. 保留原 HTML 的 \`<head>\` (CDN / 字体 / 样式 / meta), 保留所有不需要变化的 DOM 结构 — 字体、配色、布局、栅格、组件结构、动画都不许改。+5. 仅根据 "旧内容 vs 新内容" 的差异, 替换或调整对应的文字 / 数据节点。+6. 如果新内容增加了条目, 沿用原有的卡片 / 行 / slide / 章节结构添加; 如果删除了条目, 移除对应的元素。+7. 如果新旧内容只差几个字, 也只改那几个字 — 不要顺手 "优化" 或 "重排"。+8. 不要捏造数据。新内容里没有的就不要写。【旧内容】${args.oldContent}diff --git a/src/lib/agents/argv.ts b/src/lib/agents/argv.tsindex ec8d8d06..2e1ea9ee 100644--- a/src/lib/agents/argv.ts+++ b/src/lib/agents/argv.ts@@ -142,6 +142,13 @@ export function envFor(agent: string): NodeJS.ProcessEnv {export type AgentParse =| { kind: "delta"; text: string }| { kind: "meta"; key: string; value: unknown }+ /**+ * Canonical HTML rescued from a file-write tool call (e.g. Claude's `Write`+ * tool). Replaces any previously streamed text — the preamble like+ * "I'll save it as output.html\n已输出至 …" is junk; the tool's input is the+ * real HTML. Downstream calls `setHtmlFor`, not `appendHtmlFor`.+ */+ | { kind: "html"; text: string }| { kind: "noise" };/**@@ -173,6 +180,51 @@ export function parseLine(agent: string, line: string): AgentParse[] {return parseLineWithState(agent, line, {});}+/**+ * Some agents (Claude + bypassPermissions, qoder, …) ignore the "stream HTML+ * inline" prompt and decide to dump the document into a file via the `Write`+ * tool, leaving the assistant text as just a confirmation ("已输出至 …").+ * Rescue the HTML from the tool_use input so the preview still gets the real+ * content. Returns an empty string if no Write/create_file tool_use was found+ * or its input has no usable content field.+ */+function rescueHtmlFromToolUse(+ content: Array<{ type?: string; name?: string; input?: unknown }> | undefined,+): string {+ if (!Array.isArray(content)) return "";+ const parts: string[] = [];+ for (const block of content) {+ if (!block || block.type !== "tool_use") continue;+ const name = (block.name ?? "").toLowerCase();+ // Match the common file-write tool names across agents.+ if (+ name !== "write" &&+ name !== "create_file" &&+ name !== "createfile" &&+ name !== "writefile" &&+ name !== "write_file" &&+ name !== "filewrite"+ )+ continue;+ const input = block.input as Record<string, unknown> | undefined;+ if (!input || typeof input !== "object") continue;+ const path = String(input.file_path ?? input.path ?? input.filename ?? "").toLowerCase();+ // Only rescue HTML-ish targets — never grab content for a .md / .txt+ // sidecar the agent might also be writing.+ if (path && !/\.(html?|htm)$/.test(path)) continue;+ const text =+ typeof input.content === "string"+ ? input.content+ : typeof input.text === "string"+ ? input.text+ : typeof input.file_content === "string"+ ? input.file_content+ : "";+ if (text) parts.push(text);+ }+ return parts.join("");+}+function parseLineWithState(agent: string, line: string, state: ParseState): AgentParse[] {const trimmed = line.trim();if (!trimmed) return [];@@ -214,10 +266,17 @@ function parseLineWithState(agent: string, line: string, state: ParseState): Age// were absent (e.g. older claude without --include-partial-messages).if (obj.type === "assistant" && obj.message && typeof obj.message === "object") {const msg = obj.message as {- content?: Array<{ type?: string; text?: string }>;+ content?: Array<{ type?: string; text?: string; name?: string; input?: unknown }>;usage?: Record<string, number>;model?: string;};+ const toolHtml = rescueHtmlFromToolUse(msg.content);+ if (toolHtml) {+ out.push({ kind: "html", text: toolHtml });+ // suppress the chatty assistant text fallback below; the Write input+ // is authoritative for this turn.+ state.sawStreamEventText = true;+ }if (!state.sawStreamEventText) {const text = (msg.content ?? []).filter((c) => c?.type === "text" && typeof c.text === "string")@@ -267,13 +326,20 @@ function parseLineWithState(agent: string, line: string, state: ParseState): Ageout.push({ kind: "delta", text: ev.delta.text });}}- if (obj.type === "assistant" && obj.message && typeof obj.message === "object" && !state.sawStreamEventText) {- const msg = obj.message as { content?: Array<{ type?: string; text?: string }> };- const text = (msg.content ?? [])- .filter((c) => c?.type === "text" && typeof c.text === "string")- .map((c) => c.text!)- .join("");- if (text) out.push({ kind: "delta", text });+ if (obj.type === "assistant" && obj.message && typeof obj.message === "object") {+ const msg = obj.message as { content?: Array<{ type?: string; text?: string; name?: string; input?: unknown }> };+ const toolHtml = rescueHtmlFromToolUse(msg.content);+ if (toolHtml) {+ out.push({ kind: "html", text: toolHtml });+ state.sawStreamEventText = true;+ }+ if (!state.sawStreamEventText) {+ const text = (msg.content ?? [])+ .filter((c) => c?.type === "text" && typeof c.text === "string")+ .map((c) => c.text!)+ .join("");+ if (text) out.push({ kind: "delta", text });+ }}// Bare `text` field — only honor it when we haven't already emitted a// streamed delta or an assistant body, otherwise it duplicates the same@@ -312,13 +378,20 @@ function parseLineWithState(agent: string, line: string, state: ParseState): Ageout.push({ kind: "delta", text: ev.delta.text });}}- if (obj.type === "assistant" && obj.message && typeof obj.message === "object" && !state.sawStreamEventText) {- const msg = obj.message as { content?: Array<{ type?: string; text?: string }> };- const text = (msg.content ?? [])- .filter((c) => c?.type === "text" && typeof c.text === "string")- .map((c) => c.text!)- .join("");- if (text) out.push({ kind: "delta", text });+ if (obj.type === "assistant" && obj.message && typeof obj.message === "object") {+ const msg = obj.message as { content?: Array<{ type?: string; text?: string; name?: string; input?: unknown }> };+ const toolHtml = rescueHtmlFromToolUse(msg.content);+ if (toolHtml) {+ out.push({ kind: "html", text: toolHtml });+ state.sawStreamEventText = true;+ }+ if (!state.sawStreamEventText) {+ const text = (msg.content ?? [])+ .filter((c) => c?.type === "text" && typeof c.text === "string")+ .map((c) => c.text!)+ .join("");+ if (text) out.push({ kind: "delta", text });+ }}if (obj.type === "result") {if (obj.usage) out.push({ kind: "meta", key: "usage", value: obj.usage });diff --git a/src/lib/agents/invoke.ts b/src/lib/agents/invoke.tsindex 78f459e6..0988c4bd 100644--- a/src/lib/agents/invoke.ts+++ b/src/lib/agents/invoke.ts@@ -13,6 +13,12 @@ export type InvokeOpts = {export type InvokeEvent =| { type: "start"; bin: string; argv: string[]; promptBytes: number }| { type: "delta"; text: string }+ /**+ * Canonical HTML rescued from a file-write tool call. The client REPLACES+ * the task's accumulated html with this payload (not appends) — see+ * [[rescueHtmlFromToolUse]] in argv.ts for why this exists.+ */+ | { type: "html"; text: string }| { type: "meta"; key: string; value: unknown }| { type: "stderr"; text: string }| { type: "raw"; text: string }@@ -146,6 +152,7 @@ export function invokeAgent(opts: InvokeOpts): ReadableStream<InvokeEvent> {if (!line) continue;for (const part of parse(line)) {if (part.kind === "delta") safeEnqueue({ type: "delta", text: part.text });+ else if (part.kind === "html") safeEnqueue({ type: "html", text: part.text });else if (part.kind === "meta") safeEnqueue({ type: "meta", key: part.key, value: part.value });else safeEnqueue({ type: "raw", text: line.slice(0, 240) });}@@ -217,6 +224,7 @@ export function invokeAgent(opts: InvokeOpts): ReadableStream<InvokeEvent> {} else if (stdoutBuf) {for (const part of parse(stdoutBuf)) {if (part.kind === "delta") safeEnqueue({ type: "delta", text: part.text });+ else if (part.kind === "html") safeEnqueue({ type: "html", text: part.text });else if (part.kind === "meta") safeEnqueue({ type: "meta", key: part.key, value: part.value });}if (opts.agent === "aider" || opts.agent === "deepseek") {diff --git a/src/lib/templates/shared.ts b/src/lib/templates/shared.tsindex fc0fdb78..165a566a 100644--- a/src/lib/templates/shared.ts+++ b/src/lib/templates/shared.ts@@ -14,6 +14,8 @@ export const SHARED_DESIGN_DIRECTIVES = `- 推荐做法: 先把【用户内容】按语义切成若干段 (章节标题 / 论点 / 数据组 / 列表项 / 步骤), 每一段 → 至少一个独立的 slide / section / card, 然后再从模板的版式池里给每一段挑最合适的版面。宁可多页也不要把多个独立要点硬塞进一页。【硬性技术要求】+- **禁止使用 Write / Edit / MultiEdit / Bash / Create / 任何文件系统工具**。不要把 HTML 写到任何 \`.html\` 文件里。前端直接捕获你的 stdout 文本, 文件落盘由前端负责。+- 直接把完整的 HTML 文档作为助手回复的正文流式输出。不要先说"我来生成"、"已输出至 …"之类的话。- 文档以 \`<!DOCTYPE html>\` 开头, 末尾以 \`</html>\` 结束。- 在 \`<head>\` 中通过 CDN 引入 Tailwind v3 Play (https://cdn.tailwindcss.com) 与所需的 Google Fonts。- 不要引用任何外部图片 URL(除非你能保证 URL 长期有效;优先使用 CSS / SVG 内联绘制)。diff --git a/src/lib/use-convert.ts b/src/lib/use-convert.tsindex 662f9c49..d911d02e 100644--- a/src/lib/use-convert.ts+++ b/src/lib/use-convert.ts@@ -188,6 +188,27 @@ function handleEvent(taskId: string, event: string, data: unknown, startedAt: nuif (typeof d.text === "string") store.appendHtmlFor(taskId, d.text);break;}+ case "html": {+ // Agent decided to write the HTML to a file via the Write tool instead+ // of streaming it. The parser rescued the file's content from the+ // tool_use input — REPLACE the accumulated text (preamble + "已输出至 …"+ // confirmation) so the preview shows the real document.+ if (typeof d.text === "string") {+ store.setHtmlFor(taskId, d.text);+ const len = d.text.length;+ const prev = useStore.getState().tasks.find((t) => t.id === taskId)?.stats;+ store.patchStatsFor(taskId, {+ outputBytes: len,+ firstByteAt: prev?.firstByteAt ?? Date.now(),+ });+ store.pushLogFor(taskId, {+ kind: "info",+ elapsed,+ text: `📄 从 Write 工具输入恢复 HTML (${len.toLocaleString()} 字节)`,+ });+ }+ break;+ }case "meta": {const key = String(d.key);const value = d.value;