CanvasAnvil is an open source AI diagram maker and flowchart generator - an infinite-canvas diagram editor and self-hosted drawio alternative that writes editable .drawio files. Text to diagram, then fix nodes, layout and labels in place. Also AI presentations, interior design CAD/BOM, posters and infographics.
TypeScript ai-agentsai-diagramai-presentationbomcadcanvasdiagramdiagram-generatordrawioflowchartinfographicspptppt-generatorpresentation-generatorreactsvgedittext-to-diagramtypescriptworkflow
release: v1.0.0
codingFeng101e577e9f91fd9
diff --git a/.vscode/extensions.json b/.vscode/extensions.jsonnew file mode 100644index 0000000..3a81428--- /dev/null+++ b/.vscode/extensions.json@@ -0,0 +1,5 @@+{+ "recommendations": [+ "openai.chatgpt"+ ]+}\ No newline at end of filediff --git a/CHANGELOG.md b/CHANGELOG.mdindex e1f3abb..4dbe19d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -4,6 +4,31 @@ All notable changes to this project will be documented in this file.The format is based on Keep a Changelog, and the project aims to follow Semantic Versioning.+## [1.0.0] - 2026-03-17++### Added++- Local PPT AI proxy route at `/api/ppt-ai` for browser-safe PPT chat, vision, and image workflows.+- Major PPT release notes and release-version upgrade to `v1.0.0`.++### Changed++- Promoted the project from `0.1.2` to the first major usable release, `1.0.0`.+- PPT chat now uses single-turn task execution instead of sending full conversation history to the agent.+- PPT image uploads no longer inject raw base64 image payloads into chat text prompts.+- PPT image generation and edit requests now use a local proxy path instead of direct browser-to-provider requests.+- PPT image-reference handling now limits reference count, compresses large image inputs, and retries with a reduced reference set on failure.+- PPT edit routing now treats uploaded images as optional materials unless the user explicitly asks to use them.+- PPT visible version labels are simplified to version number plus timestamp.+- Internal textless-background processing remains in the PPT pipeline but is no longer exposed as a visible user version.+- Improved PPT error handling for oversized requests, bad payloads, and provider/API failures.++### Fixed++- Resolved multiple garbled-text issues in the PPT workspace source.+- Fixed local storage overflow issues caused by oversized PPT chat history payloads.+- Reduced browser-side CORS failure exposure in PPT image workflows by routing through the local proxy.+## [0.1.2] - 2026-03-06### Changeddiff --git a/README.md b/README.mdindex e44681a..e320ace 100644--- a/README.md+++ b/README.md@@ -13,7 +13,23 @@<a href="README.fr-FR.md">Français</a></p>-CanvasAnvil is a multi-canvas AI creation platform that turns a single requirement into deliverables you can iterate on.+CanvasAnvil is a multi-canvas AI creation platform for flow diagrams, CAD workflows, and PPT generation/editing.++## Release++Current release: `v1.0.0`++`v1.0.0` marks the first major usable release with a more stable PPT pipeline, stronger agent routing, cleaner version handling, and better local proxy support for AI requests.++## Highlights in v1.0.0++- Unified multi-canvas workflow across `Flow`, `CAD`, and `PPT`+- PPT chat uses single-turn task execution instead of passing full chat history to the agent+- PPT image uploads no longer bloat chat input with raw base64 data+- PPT image generation and image-edit requests now go through a local proxy route for better browser compatibility+- More stable image-reference routing with image count limits, compression, and fallback retry behavior+- Cleaner PPT version display and hidden internal textless-background processing+- Fixed multiple garbled text issues in PPT workspace and repo docs## Canvas Previews@@ -32,108 +48,96 @@ CanvasAnvil is a multi-canvas AI creation platform that turns a single requireme<tr><td width="680" align="left"><img src="public/demos/ppt.gif?raw=1" alt="PPT canvas" width="680" /></td></tr></table>-## Video Tutorials--- [Watch on Bilibili](https://www.bilibili.com/video/BV1jzZ3BBEHc?vd_source=b6b031f92061ae667eba1185f4782a1c)-- [Watch on YouTube](https://youtu.be/n3Otj--aLRo)-- [Watch on Douyin](https://v.douyin.com/JwlwhmE6R40/)--## Try CanvasAnvil+## Online Demo- [Open CanvasAnvil](https://canvasanvil.codingfgd.asia)-- Note: The current server configuration is modest, so the service may occasionally feel slow or laggy. Thanks for your understanding.-## Capability Overview (User View)+## Video Tutorials-- `Flow`: flowchart generation and partial edits (draw.io XML)-- `CAD`: interior design planning, analysis boards, 2D floor plans, render tasks, and BOM-- `PPT`: presentation draft generation and iterative editing+- [Bilibili](https://www.bilibili.com/video/BV1jzZ3BBEHc?vd_source=b6b031f92061ae667eba1185f4782a1c)+- [YouTube](https://youtu.be/n3Otj--aLRo)+- [Douyin](https://v.douyin.com/JwlwhmE6R40/)-## Typical Workflow+## Capability Overview-1. Enter your requirement-2. Generate/iterate on design plan-3. Generate analysis boards and confirm strategy-4. Generate and edit 2D floor plan-5. Export outputs (diagrams / lists / slides)+- `Flow`: flowchart generation and partial edits based on draw.io XML+- `CAD`: interior workflow planning, analysis boards, 2D floor plans, render-task generation, and BOM+- `PPT`: structured slide generation, page-level editing, image-assisted iteration, and export## Quick Start1. Install dependencies+```bashnpm install```-2. Start development++2. Start local development+```bashnpm run dev```+Default URL: `http://localhost:5173`-3. Type check+3. Run type checks+```bashnpm run check```-4. Production build++4. Build for production+```bashnpm run build```-## Origins and Integrations--- Flow canvas: integrated and enhanced from [next-ai-draw-io](https://github.com/DayuanJiang/next-ai-draw-io)-- PPT canvas: integrated and enhanced from [banana-slides](https://github.com/Anionex/banana-slides.git)-- CAD canvas: implemented in-house (architecture, agent workflow, 2D SVG editing pipeline, BOM/render pipeline)--Key improvements:--- Unified UX across canvases (chat, code blocks, one-click apply to canvas)-- More stable agent routing and retry mechanisms-- CAD-specific capabilities (patch / replace / BOM / 7-slot render workflow)-- Cross-canvas state/version/export pipelines--## Core Capabilities (Developer View)--- Flow: chat-driven flowchart generation, patch/replace, one-click apply, snapshot restore-- CAD: `cad_plan` output, parallel analysis-board generation, 2D SVG partial updates with analysis-image references, concurrent render jobs, BOM export-- PPT: structured content generation, page-level incremental edits, streaming iteration--## Tech Stack--- Frontend: React 18 + TypeScript + Vite-- UI: Tailwind CSS + Radix UI + Lucide-- Diagram engines: draw.io/diagrams.net for Flow, SVG-Edit for CAD-- Model integration: configurable multi-model access (chat / image)-## Useful Scripts-- `npm run dev`: start development server-- `npm start`: production start (static site + API)-- `npm run build`: production build+- `npm run dev`: start Vite development server+- `npm run dev:full`: start web and API dev servers together+- `npm run dev:web`: start frontend dev server+- `npm run dev:api`: start API dev server- `npm run check`: TypeScript check- `npm run lint`: ESLint+- `npm run build`: production build+- `npm run preview`: preview built app+- `npm start`: run API server++## Development Notes-## Project Structure (Key Paths)+- AI configuration is read from local app settings and can be routed to custom providers+- PPT local development now depends on the local `/api/ppt-ai` proxy route+- After changing local API route wiring in `vite.config.ts`, restart the dev server++## Project Structure```text.-├─ agent/ # Prompts and sub-agent specs for CAD/Flow/PPT-├─ public/ # Static assets (including SVG-Edit)+├─ agent/ # Agent prompts and sub-agent specs+├─ public/ # Static assets├─ src/│ └─ workspaces/│ ├─ flow/ # Flow canvas-│ ├─ cad/ # CAD canvas (in-house core)+│ ├─ cad/ # CAD canvas│ └─ ppt/ # PPT canvas-├─ api/ # API logic+├─ api/ # Local API route entrypoints└─ README.md```+## Origins and Integrations++- Flow canvas: integrated and extended from [next-ai-draw-io](https://github.com/DayuanJiang/next-ai-draw-io)+- PPT canvas: integrated and extended from [banana-slides](https://github.com/Anionex/banana-slides.git)+- CAD canvas: built in-house, including agent workflow, 2D SVG editing, render orchestration, and BOM pipeline+## Docs-- Deployment guide: [Open deployment guide](deploy/README.md)+- Deployment guide: [deploy/README.md](deploy/README.md)+## Contact-Scan my WeChat QR code below to contact me.+Scan the WeChat QR code below to contact the author.<p align="left"><img src="public/wechat.jpg" alt="WeChat QR code" width="280" />diff --git a/README.zh-CN.md b/README.zh-CN.mdindex 40d3398..772e905 100644--- a/README.zh-CN.md+++ b/README.zh-CN.md@@ -13,7 +13,23 @@<a href="README.fr-FR.md">Français</a></p>-CanvasAnvil 是一个多画布 AI 创作平台,用一句需求就能完成从方案到可交付物的生成与迭代。+CanvasAnvil 是一个多画布 AI 创作平台,面向流程图、CAD 工作流和 PPT 生成/编辑。++## 版本++当前版本:`v1.0.0`++`v1.0.0` 是第一个重大可用版本,重点提升了 PPT 链路稳定性、智能体路由准确性、版本管理可用性,以及本地代理模式下的 AI 请求兼容性。++## v1.0.0 重点更新++- 统一 `Flow`、`CAD`、`PPT` 三类画布的交互模式+- PPT 交互区改为单轮任务执行,不再把完整聊天历史传给智能体+- PPT 图片上传不再把原始 base64 大文本塞进聊天输入+- PPT 图像生成与编辑统一走本地代理,减少浏览器跨域问题+- 图片参考链路增加数量限制、压缩和失败降级重试+- PPT 版本展示更清晰,内部无字底图不再作为可见版本暴露+- 修复多处 PPT 工作区与文档乱码问题## 画布预览@@ -32,108 +48,96 @@ CanvasAnvil 是一个多画布 AI 创作平台,用一句需求就能完成从<tr><td width="680" align="left"><img src="public/demos/ppt.gif?raw=1" alt="PPT 画布" width="680" /></td></tr></table>-## 视频教程--- [Bilibili 教程视频](https://www.bilibili.com/video/BV1jzZ3BBEHc?vd_source=b6b031f92061ae667eba1185f4782a1c)-- [YouTube 教程视频](https://youtu.be/n3Otj--aLRo)-- [抖音教程视频](https://v.douyin.com/JwlwhmE6R40/)--## 在线使用+## 在线体验-- [立即打开 CanvasAnvil](https://canvasanvil.codingfgd.asia)-- 说明:当前服务器配置较低,服务运行时可能会有一定延迟或卡顿,感谢理解。+- [打开 CanvasAnvil](https://canvasanvil.codingfgd.asia)-## 能力总览(用户视角)+## 教程视频-- `Flow`:流程图生成与局部编辑(draw.io XML)-- `CAD`:室内装修方案、分析图、2D 平面图、装修图与物料清单(BOM)-- `PPT`:演示文稿草稿生成与迭代编辑+- [Bilibili](https://www.bilibili.com/video/BV1jzZ3BBEHc?vd_source=b6b031f92061ae667eba1185f4782a1c)+- [YouTube](https://youtu.be/n3Otj--aLRo)+- [抖音](https://v.douyin.com/JwlwhmE6R40/)-## 典型使用路径+## 能力概览-1. 输入需求-2. 生成并迭代方案-3. 生成分析图并确认策略-4. 生成并编辑 2D 平面图-5. 导出结果(图纸 / 清单 / 演示稿)+- `Flow`:基于 draw.io XML 的流程图生成与局部编辑+- `CAD`:室内工作流规划、分析板、2D 平面图、渲染任务与 BOM+- `PPT`:结构化幻灯片生成、页面级编辑、图像辅助迭代与导出## 快速开始1. 安装依赖+```bashnpm install```-2. 启动开发环境++2. 启动本地开发+```bashnpm run dev```+默认地址:`http://localhost:5173`-3. 类型检查+3. 运行类型检查+```bashnpm run check```-4. 生产构建++4. 构建生产版本+```bashnpm run build```-## 项目来源与说明+## 常用命令-- Flow 画布:基于 [next-ai-draw-io](https://github.com/DayuanJiang/next-ai-draw-io) 集成与增强-- PPT 画布:基于 [banana-slides](https://github.com/Anionex/banana-slides.git) 集成与增强-- CAD 画布:本项目自研(架构、Agent 工作流、2D SVG 编辑链路、BOM/装修图链路)--核心优化:--- 三画布统一交互范式(聊天、代码块、一键应用到画布)-- 更稳定的 Agent 路由与重试机制-- CAD 专项能力(局部 patch / 全量 replace / BOM / 7 槽装修图)-- 跨画布状态管理、版本管理和导出链路--## 核心能力(开发视角)--- Flow:对话生成流程图,支持 patch/replace,一键应用代码块,支持历史恢复-- CAD:`cad_plan` 方案生成、分析图并发生成、参考分析图进行 2D SVG 修改、装修图并发出图、BOM 导出-- PPT:结构化内容生成、页级增量编辑、图文流式生成与迭代--## 技术栈--- 前端:React 18 + TypeScript + Vite-- UI:Tailwind CSS + Radix UI + Lucide-- 图形引擎:Flow 使用 draw.io/diagrams.net;CAD 使用 SVG-Edit-- 模型接入:可配置多模型(Chat / Image)--## 常用脚本--- `npm run dev`:开发服务器-- `npm start`:生产启动(静态站点 + API)-- `npm run build`:生产构建+- `npm run dev`:启动 Vite 开发服务+- `npm run dev:full`:同时启动 Web 和 API 开发服务+- `npm run dev:web`:启动前端开发服务+- `npm run dev:api`:启动 API 开发服务- `npm run check`:TypeScript 检查- `npm run lint`:ESLint 检查+- `npm run build`:生产构建+- `npm run preview`:预览构建结果+- `npm start`:启动 API 服务++## 开发说明-## 目录结构(关键部分)+- AI 配置从本地应用设置中读取,可接入自定义模型服务+- PPT 本地开发现在依赖本地 `/api/ppt-ai` 代理路由+- 如果修改了 `vite.config.ts` 里的本地 API 路由映射,需要重启开发服务++## 项目结构```text.-├─ agent/ # CAD/Flow/PPT 的提示词与子智能体定义-├─ public/ # 静态资源(含 SVG-Edit 等)+├─ agent/ # 智能体 Prompt 与子 Agent 规范+├─ public/ # 静态资源├─ src/│ └─ workspaces/│ ├─ flow/ # Flow 画布-│ ├─ cad/ # CAD 画布(自研核心)+│ ├─ cad/ # CAD 画布│ └─ ppt/ # PPT 画布-├─ api/ # API 相关逻辑+├─ api/ # 本地 API 路由入口└─ README.md```-## 文档导航+## 来源与集成++- Flow 画布:集成并扩展自 [next-ai-draw-io](https://github.com/DayuanJiang/next-ai-draw-io)+- PPT 画布:集成并扩展自 [banana-slides](https://github.com/Anionex/banana-slides.git)+- CAD 画布:项目自研,包含 agent 工作流、2D SVG 编辑、渲染编排和 BOM 链路++## 文档-- 部署说明:[查看部署说明](deploy/README.zh-CN.md)+- 部署说明:[deploy/README.zh-CN.md](deploy/README.zh-CN.md)-## 微信+## 联系方式-我的微信二维码如下,欢迎联系我。+可扫描下方微信二维码联系作者。<p align="left"><img src="public/wechat.jpg" alt="WeChat QR code" width="280" />diff --git a/agent/cad/images-agents/detail-drawings.md b/agent/cad/images-agents/detail-drawings.mdindex e06afe3..e49e096 100644--- a/agent/cad/images-agents/detail-drawings.md+++ b/agent/cad/images-agents/detail-drawings.md@@ -26,6 +26,9 @@ Master Renovation Scheme (SSOT, read-only):4. For each detail, specify scale (1:10 or 1:20), layers, thicknesses, dimensions, and workmanship notes.5. Output JSON strictly following the schema in “# Output Format”.+# E - End Goal+Produce one deterministic single-sheet JSON prompt for detail drawings that is directly usable by the downstream image generator.+# N - NarrowingConstraints (must follow):1. Output ONLY one JSON code block and nothing else.diff --git a/agent/cad/images-agents/elevation-index-and-interior-elevations.md b/agent/cad/images-agents/elevation-index-and-interior-elevations.mdindex f2e75ad..d612e13 100644--- a/agent/cad/images-agents/elevation-index-and-interior-elevations.md+++ b/agent/cad/images-agents/elevation-index-and-interior-elevations.md@@ -26,6 +26,9 @@ Master Renovation Scheme (SSOT, read-only):4. Provide interior elevations for major spaces/walls with materials, dimensions, and mounting heights strictly following the Master material strategy and ceiling baseline.5. Output JSON strictly following the schema in “# Output Format”.+# E - End Goal+Produce one deterministic single-sheet JSON prompt for the elevation index and interior elevations sheet that is directly usable by the downstream image generator.+# N - NarrowingConstraints (must follow):1. Output ONLY one JSON code block and nothing else.diff --git a/agent/cad/images-agents/floor-finish-plan.md b/agent/cad/images-agents/floor-finish-plan.mdindex e377a45..1f7c2e3 100644--- a/agent/cad/images-agents/floor-finish-plan.md+++ b/agent/cad/images-agents/floor-finish-plan.md@@ -25,6 +25,9 @@ Master Renovation Scheme (SSOT, read-only):3. Write a single, dense, print-ready sheet prompt that includes all mandatory elements for this sheet.4. Output JSON strictly following the schema in “# Output Format”.+# E - End Goal+Produce one deterministic single-sheet JSON prompt for the floor finish plan that is directly usable by the downstream image generator.+# N - NarrowingConstraints (must follow):1. Output ONLY one JSON code block and nothing else.diff --git a/agent/cad/images-agents/key-strategy-board.md b/agent/cad/images-agents/key-strategy-board.mdindex 8ca5c6e..bc187c3 100644--- a/agent/cad/images-agents/key-strategy-board.md+++ b/agent/cad/images-agents/key-strategy-board.md@@ -1,6 +1,9 @@-# Role+# R - RoleYou are `cad_key_strategy_board_agent`, an assistant that writes an image prompt for an interior-renovation key strategy board.+# I - Instructions+Generate one image prompt for a key strategy board used for design alignment with the user.+# InputPlan design payload:{{planDesign}}@@ -8,8 +11,10 @@ Plan design payload:Language setting:{{outputLanguage}}-# Task-Read the plan design payload, then produce one image-generation prompt for a "Key Strategy Board" used for design alignment with the user.+# S - Steps+1. Read the plan design payload.+2. Identify 3 to 7 key strategies and their relationships, priorities, or execution order.+3. Convert them into one single-paragraph image prompt for a key strategy board.The board should:- Use interior design strategy analysis / strategy derivation board style.@@ -21,7 +26,10 @@ The board should:- Be clear, professional, and presentation-ready.- Avoid CAD drawings, construction-detail drawings, and BOM outputs.-# Output Rules+# E - End Goal+Produce one concise, presentation-ready image prompt for a key strategy board.++# N - Narrowing1. Output exactly one single-paragraph image prompt in the language specified in "Language setting".2. Do not output Markdown.3. Do not output explanations, bullet points, or any extra text.diff --git a/agent/cad/images-agents/master-renovation-scheme.md b/agent/cad/images-agents/master-renovation-scheme.mdindex 5d9de0f..2fd466d 100644--- a/agent/cad/images-agents/master-renovation-scheme.md+++ b/agent/cad/images-agents/master-renovation-scheme.md@@ -22,7 +22,7 @@ Plan:{{svg2d}}```-# S - Responsibilities+# S - Steps1. Normalize and LOCK room names.2. Decide wall strategy: retain / demolish / new-build.3. Lock floor material zoning by room.@@ -34,6 +34,9 @@ Plan:9. Lock dimensioning rules and drawing consistency rules.10. Output ONE unified Renovation Scheme JSON.+# E - End Goal+Produce one deterministic renovation scheme JSON that becomes the single source of truth for downstream CAD drawing prompts.+# N - NarrowingConstraints:1. Output ONLY one JSON code block and nothing else.diff --git a/agent/cad/images-agents/mep-plan.md b/agent/cad/images-agents/mep-plan.mdindex fb391d7..76edf80 100644--- a/agent/cad/images-agents/mep-plan.md+++ b/agent/cad/images-agents/mep-plan.md@@ -26,6 +26,9 @@ Master Renovation Scheme (SSOT, read-only):4. Include mounting heights, circuit labeling, pipe diameters, and slope notes where applicable.5. Output JSON strictly following the schema in “# Output Format”.+# E - End Goal+Produce one deterministic single-sheet JSON prompt for the MEP plan that is directly usable by the downstream image generator.+# N - NarrowingConstraints (must follow):1. Output ONLY one JSON code block and nothing else.diff --git a/agent/cad/images-agents/overall-analysis-board.md b/agent/cad/images-agents/overall-analysis-board.mdindex b8635d5..0145d0e 100644--- a/agent/cad/images-agents/overall-analysis-board.md+++ b/agent/cad/images-agents/overall-analysis-board.md@@ -1,6 +1,9 @@-# Role+# R - RoleYou are `cad_overall_analysis_board_agent`, an assistant that writes an image prompt for an interior-renovation overall analysis board.+# I - Instructions+Generate one image prompt for an overall analysis board used for design alignment with the user.+# InputPlan design payload:{{planDesign}}@@ -8,8 +11,10 @@ Plan design payload:Language setting:{{outputLanguage}}-# Task-Read the plan design payload, then produce one image-generation prompt for an "Overall Analysis Board" used for design alignment with the user.+# S - Steps+1. Read the plan design payload.+2. Identify project goals, zones, constraints, and pending confirmations that matter most.+3. Convert them into one single-paragraph image prompt for an overall analysis board.The board should:- Use early-stage interior design communication / concept analysis board style.@@ -21,7 +26,10 @@ The board should:- Avoid CAD construction drawing style.- Avoid construction nodes, detailed dimensions, and material-detail drawings.-# Output Rules+# E - End Goal+Produce one concise, presentation-ready image prompt for an overall analysis board.++# N - Narrowing1. Output exactly one single-paragraph image prompt in the language specified in "Language setting".2. Do not output Markdown.3. Do not output explanations, bullet points, or any extra text.diff --git a/agent/cad/images-agents/reflected-ceiling-plan.md b/agent/cad/images-agents/reflected-ceiling-plan.mdindex 2efb19d..f71b551 100644--- a/agent/cad/images-agents/reflected-ceiling-plan.md+++ b/agent/cad/images-agents/reflected-ceiling-plan.md@@ -25,6 +25,9 @@ Master Renovation Scheme (SSOT, read-only):3. Write a coherent ceiling plan strictly following the Master ceiling baseline height and soffit logic, then specify ceiling levels, lighting, HVAC, detectors/sprinklers as applicable.4. Output JSON strictly following the schema in “# Output Format”.+# E - End Goal+Produce one deterministic single-sheet JSON prompt for the reflected ceiling plan that is directly usable by the downstream image generator.+# N - NarrowingConstraints (must follow):1. Output ONLY one JSON code block and nothing else.diff --git a/agent/cad/images-agents/renovation-plan-layout.md b/agent/cad/images-agents/renovation-plan-layout.mdindex b97342b..99eb7bb 100644--- a/agent/cad/images-agents/renovation-plan-layout.md+++ b/agent/cad/images-agents/renovation-plan-layout.md@@ -25,6 +25,9 @@ Master Renovation Scheme (SSOT, read-only):3. Write a single, dense, print-ready sheet prompt that includes all mandatory elements for this sheet.4. Output JSON strictly following the schema in “# Output Format”.+# E - End Goal+Produce one deterministic single-sheet JSON prompt for the renovation plan layout that is directly usable by the downstream image generator.+# N - NarrowingConstraints (must follow):1. Output ONLY one JSON code block and nothing else.diff --git a/agent/cad/images-agents/wall-setting-out-plan.md b/agent/cad/images-agents/wall-setting-out-plan.mdindex 64eea5e..fcfe6ff 100644--- a/agent/cad/images-agents/wall-setting-out-plan.md+++ b/agent/cad/images-agents/wall-setting-out-plan.md@@ -26,6 +26,9 @@ Master Renovation Scheme (SSOT, read-only):4. Include a demolition/new wall inset viewport as required.5. Output JSON strictly following the schema in “# Output Format”.+# E - End Goal+Produce one deterministic single-sheet JSON prompt for the wall setting-out plan that is directly usable by the downstream image generator.+# N - NarrowingConstraints (must follow):1. Output ONLY one JSON code block and nothing else.diff --git a/agent/cad/svg/patch.md b/agent/cad/svg/patch.mdindex 69e601e..b4072ca 100644--- a/agent/cad/svg/patch.md+++ b/agent/cad/svg/patch.md@@ -20,6 +20,9 @@ I will provide:5. Produce `replace` snippets that preserve valid SVG and drafting consistency.6. Output exactly one JSON code block.+# E - End Goal+Produce one minimal `cad_patch` JSON payload that safely applies the requested SVG change without disturbing unrelated content.+# N - NarrowingConstraints (CRITICAL):1. Output exactly ONE markdown ```json code block and nothing else.diff --git a/agent/cad/svg/replace.md b/agent/cad/svg/replace.mdindex 344bfe8..c3922d1 100644--- a/agent/cad/svg/replace.md+++ b/agent/cad/svg/replace.md@@ -19,6 +19,9 @@ I will provide:4. Keep geometry buildable and drafting-valid.5. Self-check SVG validity and consistency before output.+# E - End Goal+Produce one complete `cad_patch` JSON payload whose `full` field contains the final, renderable SVG for the requested floor plan.+# N - NarrowingConstraints (CRITICAL):1. Output exactly ONE markdown ```json code block and nothing else.diff --git a/agent/flow/deep-thinking-image.md b/agent/flow/deep-thinking-image.mdindex 5a1cc7b..9f54945 100644--- a/agent/flow/deep-thinking-image.md+++ b/agent/flow/deep-thinking-image.md@@ -40,7 +40,7 @@ Treat the user's requirement as the final product goal.- complete content coverage5. Produce a diagram image that looks finished and presentation-ready, not approximate.-## E: End State+## E: End GoalThe generated image must:- be a complete final-quality diagram image- be suitable as a direct visual reference for downstream XML generationdiff --git a/agent/flow/patch.md b/agent/flow/patch.mdindex 107dd01..44d0347 100644--- a/agent/flow/patch.md+++ b/agent/flow/patch.md@@ -16,6 +16,9 @@ I will provide:4. Write `replace` snippet(s) that keep draw.io XML valid and consistent.5. Output exactly one JSON code block that matches the schema.+# E - End Goal+Produce one minimal `flow_patch` JSON payload that applies the requested local change safely and exactly.+# N - NarrowingConstraints (CRITICAL):1. Output exactly ONE markdown ```json code block and nothing else.diff --git a/agent/flow/replace.md b/agent/flow/replace.mdindex 446f9b5..19a7b32 100644--- a/agent/flow/replace.md+++ b/agent/flow/replace.md@@ -15,6 +15,9 @@ I will provide:3. Use consistent layout, styles, and deterministic ids.4. Self-validate the XML and fix any violations before responding.+# E - End Goal+Produce one complete `flow_patch` JSON payload whose `full` field contains the final draw.io XML for the requested diagram.+# N - NarrowingConstraints (CRITICAL):1. Output exactly ONE markdown ```json code block and nothing else.diff --git a/agent/ppt/outline-edit.md b/agent/ppt/outline-edit.mdindex adaf44b..c7852a8 100644--- a/agent/ppt/outline-edit.md+++ b/agent/ppt/outline-edit.md@@ -27,6 +27,9 @@ Notes:- `note`5. Return one patch payload only.+# E - End Goal+Produce one machine-readable routing result that tells the external system which slides to update and how.+# N - NarrowingRules (CRITICAL):1. Text editing only. Do NOT output image URLs.diff --git a/agent/ppt/outline.md b/agent/ppt/outline.mdindex 9ca87bb..cb51571 100644--- a/agent/ppt/outline.md+++ b/agent/ppt/outline.md@@ -36,6 +36,9 @@ You may receive either one of these input sets:5. Keep language aligned with `ui_language`.6. Return one JSON payload only.+# E - End Goal+Produce one complete `PptPlan` JSON that is ready for downstream rendering and slide generation.+# N - NarrowingRules (CRITICAL):1. This planning agent only outputs plan JSON; do not output image URLs.diff --git a/agent/ppt/slides-generate.md b/agent/ppt/slides-generate.mdindex d980b2e..6a1401c 100644--- a/agent/ppt/slides-generate.md+++ b/agent/ppt/slides-generate.md@@ -17,6 +17,9 @@ Classify user feedback into per-slide edit actions and indicate whether each act- `both`4. Write concise executable `instruction` text.+# E - End Goal+Produce one machine-readable routing result that tells the external system which slides to update and how.+# N - NarrowingRules (CRITICAL):1. Output JSON array only.diff --git a/agent/ppt/system.md b/agent/ppt/system.mdindex df88a62..7d013a0 100644--- a/agent/ppt/system.md+++ b/agent/ppt/system.md@@ -1,33 +1,38 @@-# R - Role-You are `PlanEditAgent` + `SlideImageEditAgent` coordinator for existing slides.+# R - Role+You are the PPT edit router for existing slides.# I - Instructions-Use provided slide context to return plan edits. For visual change requests, include a direct image edit instruction in `instruction`.+Return only the changed slides. For each slide, decide whether the edit is:+- `text_only`+- `text_relayout`+- `background_redraw`# Input-- required: user feedback text-- required: slide context attachments (`{slides:[...]}` or single slide JSON)-- required: `ui_language`+- user feedback+- slide context JSON+- optional uploaded image tags+- `ui_language`# S - Steps-1. Identify targeted slides from context and feedback.-2. Update plan fields (`title/content/description/layout/note`) when needed.-3. If visual change is requested, include `instruction` for image editing.-4. Keep `id` stable.-5. Return changed slides only.+1. Identify which slides are affected.+2. Update `title`, `content`, `description`, `layout`, `note` when needed.+3. Set `editType` for each changed slide.+4. If the background must change, write a direct visual instruction in `instruction`.+5. If a slide should reference another slide's style, set `styleRefSlideIds` and `styleRefPolicy`.+6. Only set `materialImageUrls` when the user explicitly asks to use an uploaded image on a specific slide.++# E - End Goal+Produce one machine-readable `ppt_edit` payload that updates only the necessary slides and routes each slide to the correct edit flow.# N - Narrowing-Rules (CRITICAL):-1. Do not output full deck unless all slides changed.-2. Do not output image URL directly.-3. For visual edit requests, set `instruction` on that slide.-4. Output ONLY one markdown code block.-5. `ui_language=zh` => Simplified Chinese; `ui_language=en` => English.-6. If style should reference other slides, you may set:- - `styleRefSlideIds`: string[] (e.g. ["slide-2","slide-5"])- - `styleRefPolicy`: "style_only" | "style_and_layout"- - `styleRefImageUrls`: string[] (optional; explicit reference image URLs when slide IDs are unavailable)-7. You may edit multiple slides in one response. Each slide can have its own style reference group.+Rules:+1. Keep `id` stable.+2. Do not output unchanged slides.+3. Do not output image URLs unless they are explicit reference/material URLs.+4. Use `text_only` for wording-only edits.+5. Use `text_relayout` when text changes require text box reflow but not a new background.+6. Use `background_redraw` only when visuals, composition, layout image, or explicitly requested material usage must change.+7. Output exactly one JSON code block.# Output Format```json@@ -36,15 +41,17 @@ Rules (CRITICAL):"slides": [{"id": "slide-1",+ "editType": "text_only","title": "...","content": ["..."],"description": "...","layout": "...","note": "...",- "instruction": "optional, only for visual edit",- "styleRefSlideIds": ["slide-2", "slide-5"],+ "instruction": "optional, only for background_redraw",+ "styleRefSlideIds": ["slide-2"],"styleRefPolicy": "style_only",- "styleRefImageUrls": ["https://example.com/style-ref-1.png"]+ "styleRefImageUrls": ["https://example.com/style-ref-1.png"],+ "materialImageUrls": ["https://example.com/material-1.png"]}]}diff --git a/api/routes/ppt-ai.ts b/api/routes/ppt-ai.tsnew file mode 100644index 0000000..52f4810--- /dev/null+++ b/api/routes/ppt-ai.ts@@ -0,0 +1 @@+export { POST } from "../../src/server/routes/ppt-ai"diff --git a/package.json b/package.jsonindex 1860d8d..c6faa9f 100644--- a/package.json+++ b/package.json@@ -1,7 +1,7 @@{"name": "canvasanvil","private": true,- "version": "0.1.2",+ "version": "1.0.0","type": "module","scripts": {"dev": "vite",diff --git a/src/components/SettingsDialog.tsx b/src/components/SettingsDialog.tsxindex 0fd8742..ef47cdb 100644--- a/src/components/SettingsDialog.tsx+++ b/src/components/SettingsDialog.tsx@@ -64,14 +64,14 @@ export function SettingsDialog() {className="h-8 px-3 rounded-md"onClick={() => setUiLang("zh")}>- 中文+ {t(uiLang, "settings.language.zh")}</Button><Buttonvariant={uiLang === "en" ? "secondary" : "ghost"}className="h-8 px-3 rounded-md"onClick={() => setUiLang("en")}>- English+ {t(uiLang, "settings.language.en")}</Button></div></div>diff --git a/src/lib/ai-client.ts b/src/lib/ai-client.tsindex ce9e858..2a2881c 100644--- a/src/lib/ai-client.ts+++ b/src/lib/ai-client.ts@@ -24,6 +24,10 @@ const DEFAULT_CONFIG: AIConfig = {const STORAGE_KEY = "unified_ai_workspace_config";const MODEL_CONCURRENCY = 30;+const IMAGE_MODEL_MAX_REFERENCE_IMAGES = 3;+const IMAGE_MODEL_MAX_DIMENSION = 1536;+const IMAGE_MODEL_MAX_DATA_URL_LENGTH = 1_800_000;+const IMAGE_MODEL_JPEG_QUALITY = 0.86;type QueueItem<T> = {run: () => Promise<T>;@@ -237,6 +241,10 @@ export type GenerateChatMessageOptions = {timeoutMs?: number;};+async function normalizeVisionImageUrls(imageUrls: string[]) {+ return await normalizeImageUrlsForModel(imageUrls, IMAGE_MODEL_MAX_REFERENCE_IMAGES);+}+export async function generateChatMessage(messages: ChatMessage[], model?: string, options?: GenerateChatMessageOptions) {const timeoutMs = typeof options?.timeoutMs === "number" && options.timeoutMs > 0 ? options.timeoutMs : 0;const externalSignal = options?.signal;@@ -286,6 +294,106 @@ export async function generateChatMessage(messages: ChatMessage[], model?: strin}}+export async function generatePptProxyChatMessage(+ messages: ChatMessage[],+ model?: string,+ options?: GenerateChatMessageOptions+) {+ const config = getAIConfig();+ if (!config.apiKey) {+ throw new Error(t(getUiLanguage(), "error.missingApiKey"));+ }++ const data = await callPptProxy<{ content?: string }>(+ {+ kind: "chat",+ aiConfig: {+ apiKey: config.apiKey,+ baseUrl: config.baseUrl,+ chatModel: config.chatModel,+ imageModel: config.imageModel,+ },+ messages,+ model: model || config.chatModel,+ },+ options?.signal+ );++ return String(data?.content || "");+}++export async function generateVisionChatMessage(+ systemPrompt: string,+ userPrompt: string,+ imageUrls: string[],+ model?: string,+ options?: GenerateChatMessageOptions+) {+ const timeoutMs = typeof options?.timeoutMs === "number" && options.timeoutMs > 0 ? options.timeoutMs : 0;+ const externalSignal = options?.signal;++ const controller = typeof AbortController !== "undefined" ? new AbortController() : null;+ const mergedSignal = controller?.signal || externalSignal;+ let timeoutId: any = null;++ if (controller && externalSignal) {+ if (externalSignal.aborted) controller.abort();+ else externalSignal.addEventListener("abort", () => controller.abort(), { once: true });+ }++ if (controller && timeoutMs > 0) {+ timeoutId = setTimeout(() => controller.abort(), timeoutMs);+ }++ try {+ return await limitModelCall(async () => {+ const config = getAIConfig();++ if (!config.apiKey) {+ throw new Error(t(getUiLanguage(), "error.missingApiKey"));+ }++ const normalizedImageUrls = await normalizeVisionImageUrls(imageUrls);+ const userContent: any[] = [{ type: "text", text: userPrompt }];+ for (const url of normalizedImageUrls) {+ userContent.push({+ type: "image_url",+ image_url: { url },+ });+ }++ try {+ const data = await callPptProxy<{ content?: string }>(+ {+ kind: "chat",+ aiConfig: {+ apiKey: config.apiKey,+ baseUrl: config.baseUrl,+ chatModel: config.chatModel,+ imageModel: config.imageModel,+ },+ messages: [+ { role: "system", content: systemPrompt },+ { role: "user", content: userContent as any },+ ] as any,+ model: model || config.chatModel,+ },+ mergedSignal || undefined+ );+ return String(data?.content || "");+ } catch (error) {+ if ((error as any)?.name === "AbortError" || (error as any)?.name === "APIUserAbortError") {+ throw error;+ }+ console.error("Vision Chat Error:", error);+ throw error;+ }+ }, mergedSignal);+ } finally {+ if (timeoutId) clearTimeout(timeoutId);+ }+}+// Legacy non-stream function (kept for compatibility if needed)export async function sendChatMessage(messages: ChatMessage[]) {return streamChatMessage(messages, () => {});@@ -298,6 +406,48 @@ export interface ImageGenerationRequest {additionalReferenceImageUrls?: string[]; // Additional references}+type PptProxyChatRequest = {+ kind: "chat";+ aiConfig: Pick<AIConfig, "apiKey" | "baseUrl" | "chatModel" | "imageModel">;+ messages: ChatMessage[];+ model?: string;+};++type PptProxyImageRequest = {+ kind: "image";+ aiConfig: Pick<AIConfig, "apiKey" | "baseUrl" | "chatModel" | "imageModel">;+ prompt: string;+ referenceImageUrl?: string;+ additionalReferenceImageUrls?: string[];+ model?: string;+};++async function callPptProxy<T>(body: PptProxyChatRequest | PptProxyImageRequest, signal?: AbortSignal): Promise<T> {+ const response = await fetch("/api/ppt-ai", {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify(body),+ signal,+ });++ const text = await response.text().catch(() => "");+ let parsed: any = null;+ try {+ parsed = text ? JSON.parse(text) : null;+ } catch {+ parsed = null;+ }++ if (!response.ok) {+ const detail = String(parsed?.error || text || "").trim();+ throw new Error(detail || `Proxy request failed with status ${response.status}`);+ }++ return parsed as T;+}+// Helper to validate and clean URLfunction cleanUrl(url: string) {if (!url) return null;@@ -343,6 +493,99 @@ async function objectUrlToDataUrl(objectUrl: string) {}}+function getImageMimeFromDataUrl(dataUrl: string) {+ const match = String(dataUrl || "").match(/^data:(image\/[a-zA-Z0-9.+-]+);base64,/i);+ return match?.[1]?.toLowerCase() || "image/png";+}++async function compressDataImageUrlForModel(dataUrl: string) {+ if (typeof window === "undefined" || typeof document === "undefined") return dataUrl;+ if (!String(dataUrl || "").startsWith("data:image")) return dataUrl;++ return await new Promise<string>((resolve) => {+ const img = new Image();+ img.onload = () => {+ try {+ const width = Number(img.naturalWidth || img.width || 0);+ const height = Number(img.naturalHeight || img.height || 0);+ if (!width || !height) {+ resolve(dataUrl);+ return;+ }++ const longestSide = Math.max(width, height);+ const needsResize = longestSide > IMAGE_MODEL_MAX_DIMENSION;+ const needsReencode = dataUrl.length > IMAGE_MODEL_MAX_DATA_URL_LENGTH;++ if (!needsResize && !needsReencode) {+ resolve(dataUrl);+ return;+ }++ const scale = needsResize ? IMAGE_MODEL_MAX_DIMENSION / longestSide : 1;+ const targetWidth = Math.max(1, Math.round(width * scale));+ const targetHeight = Math.max(1, Math.round(height * scale));+ const canvas = document.createElement("canvas");+ canvas.width = targetWidth;+ canvas.height = targetHeight;+ const ctx = canvas.getContext("2d");+ if (!ctx) {+ resolve(dataUrl);+ return;+ }++ ctx.imageSmoothingEnabled = true;+ ctx.imageSmoothingQuality = "high";+ ctx.drawImage(img, 0, 0, targetWidth, targetHeight);++ const originalMime = getImageMimeFromDataUrl(dataUrl);+ const targetMime =+ originalMime === "image/jpeg" || originalMime === "image/jpg" || needsReencode+ ? "image/jpeg"+ : originalMime;+ const compressed =+ targetMime === "image/jpeg"+ ? canvas.toDataURL(targetMime, IMAGE_MODEL_JPEG_QUALITY)+ : canvas.toDataURL(targetMime);++ resolve(compressed.length < dataUrl.length ? compressed : dataUrl);+ } catch {+ resolve(dataUrl);+ }+ };+ img.onerror = () => resolve(dataUrl);+ img.src = dataUrl;+ });+}++async function normalizeImageUrlForModel(url: string) {+ const raw = String(url || "").trim();+ if (!raw) return null;++ if (raw.startsWith("blob:")) {+ const dataUrl = await objectUrlToDataUrl(raw);+ if (!dataUrl) return null;+ return await compressDataImageUrlForModel(dataUrl);+ }++ if (raw.startsWith("data:image")) {+ return await compressDataImageUrlForModel(raw);+ }++ return cleanUrl(raw);+}++async function normalizeImageUrlsForModel(imageUrls: string[], maxImages: number) {+ const normalized: string[] = [];+ for (const raw of imageUrls) {+ if (normalized.length >= maxImages) break;+ const url = await normalizeImageUrlForModel(raw);+ if (!url || normalized.includes(url)) continue;+ normalized.push(url);+ }+ return normalized;+}+function extractImageUrlFromContent(messageContent: any) {if (Array.isArray(messageContent)) {const imagePart = messageContent.find((part: any) => part.type === 'image_url');@@ -397,82 +640,32 @@ export async function generateImage(request: ImageGenerationRequest, signal?: Abthrow new Error(t(getUiLanguage(), "error.missingApiKey"));}- let referenceImageUrl = request.referenceImageUrl;- if (referenceImageUrl && referenceImageUrl.startsWith("blob:")) {- const dataUrl = await objectUrlToDataUrl(referenceImageUrl);- if (dataUrl) referenceImageUrl = dataUrl;- else referenceImageUrl = undefined;- }-- const additionalUrls: string[] = [];- if (request.additionalReferenceImageUrls && request.additionalReferenceImageUrls.length > 0) {- for (const url of request.additionalReferenceImageUrls) {- if (url.startsWith("blob:")) {- const dataUrl = await objectUrlToDataUrl(url);- if (dataUrl) additionalUrls.push(dataUrl);- } else {- additionalUrls.push(url);- }- }- }+ const normalizedReferenceImageUrl = request.referenceImageUrl+ ? await normalizeImageUrlForModel(request.referenceImageUrl)+ : null;+ const additionalUrls = await normalizeImageUrlsForModel(+ Array.isArray(request.additionalReferenceImageUrls) ? request.additionalReferenceImageUrls : [],+ Math.max(0, IMAGE_MODEL_MAX_REFERENCE_IMAGES - (normalizedReferenceImageUrl ? 1 : 0))+ );- const myHeaders = new Headers();- myHeaders.append("Authorization", `Bearer ${config.apiKey}`);- myHeaders.append("Content-Type", "application/json");-- const content: any[] = [- {- "type": "text",- "text": request.prompt- }- ];-- if (referenceImageUrl) {- content.push({- "type": "image_url",- "image_url": {- "url": referenceImageUrl- }- });- }-- for (const url of additionalUrls) {- content.push({- "type": "image_url",- "image_url": {- "url": url- }- });- }-- const raw = JSON.stringify({- "model": config.imageModel,- "messages": [- {- "role": "user",- "content": referenceImageUrl || additionalUrls.length > 0 ? content : request.prompt- }- ],- "stream": false- });-- const requestOptions = {- method: 'POST',- headers: myHeaders,- body: raw,- redirect: 'follow' as RequestRedirect,- ...(signal ? { signal } : {})- };-try {- const response = await fetch(`${config.baseUrl}/chat/completions`, requestOptions);-- if (!response.ok) {- throw new Error(`API request failed with status ${response.status}`);- }-- const result = await response.json();- const url = parseAIResponse(result);+ const result = await callPptProxy<{ url?: string }>(+ {+ kind: "image",+ aiConfig: {+ apiKey: config.apiKey,+ baseUrl: config.baseUrl,+ chatModel: config.chatModel,+ imageModel: config.imageModel,+ },+ prompt: request.prompt,+ referenceImageUrl: normalizedReferenceImageUrl || undefined,+ additionalReferenceImageUrls: additionalUrls,+ model: config.imageModel,+ },+ signal+ );+ const url = cleanUrl(String(result?.url || ""));if (url && url.startsWith("data:image")) {try {const objectUrl = dataUrlToObjectUrl(url);diff --git a/src/lib/i18n.ts b/src/lib/i18n.tsindex 44c8752..c87c908 100644--- a/src/lib/i18n.ts+++ b/src/lib/i18n.ts@@ -17,6 +17,8 @@ const dict: Dict = {"settings.title": { zh: "配置设置", en: "Settings" },"settings.subtitle": { zh: "配置 AI 模型参数与 API 密钥", en: "Configure model parameters and API keys" },"settings.language": { zh: "界面语言", en: "Language" },+ "settings.language.zh": { zh: "中文", en: "Chinese" },+ "settings.language.en": { zh: "英文", en: "English" },"settings.buttonTitle": { zh: "设置", en: "Settings" },"settings.chatModel": { zh: "对话模型 (Chat Model)", en: "Chat Model" },"settings.imageModel": { zh: "绘图模型 (Image Model)", en: "Image Model" },diff --git a/src/lib/ppt-prompts.ts b/src/lib/ppt-prompts.tsindex 6f91de9..20a9452 100644--- a/src/lib/ppt-prompts.ts+++ b/src/lib/ppt-prompts.ts@@ -1,4 +1,6 @@+import { buildRisenPrompt } from "./risen-prompt";+export interface ProjectContext {idea_prompt?: string;outline_text?: string;@@ -50,36 +52,15 @@ export function getOutlineGenerationPrompt(projectContext: ProjectContext, languconst filesXml = formatReferenceFilesXml(projectContext.reference_files_content);const ideaPrompt = projectContext.idea_prompt || "";- return `${filesXml}You are a helpful assistant that generates an outline for a ppt.--You can organize the content in two ways:--1. Simple format (for short PPTs without major sections):-[{"title": "title1", "points": ["point1", "point2"]}, {"title": "title2", "points": ["point1", "point2"]}]--2. Part-based format (for longer PPTs with major sections):-[- {- "part": "Part 1: Introduction",- "pages": [- {"title": "Welcome", "points": ["point1", "point2"]},- {"title": "Overview", "points": ["point1", "point2"]}- ]- },- {- "part": "Part 2: Main Content",- "pages": [- {"title": "Topic 1", "points": ["point1", "point2"]},- {"title": "Topic 2", "points": ["point1", "point2"]}- ]- }-]--Choose the format that best fits the content. Use parts when the PPT has clear major sections.--The user's request: ${ideaPrompt}. Now generate the outline as valid JSON only. Do not wrap in markdown code blocks. Do not include any other text.-${getLanguageInstruction(language)}-`;+ return `${filesXml}${buildRisenPrompt({+ role: "You are an outline planning agent for PPT generation.",+ instructions: `Generate an outline as valid JSON only. Choose the format that best fits the content: simple page list for short decks, or part-based outline when the deck has major sections. ${getLanguageInstruction(language)}`.trim(),+ input: `User request:\n${ideaPrompt || "(none)"}`,+ steps: `1. Understand the requested topic and audience.\n2. Choose simple format or part-based format.\n3. Keep page titles concise and points presentation-friendly.\n4. Use parts only when there are clear major sections.`,+ endGoal: "Produce a complete outline that is directly usable for downstream PPT page planning.",+ narrowing: `1. Return valid JSON only.\n2. Do not wrap the JSON in markdown code fences.\n3. Do not include any explanation outside the JSON payload.`,+ outputFormat: `Simple format:\n[{"title":"title1","points":["point1","point2"]}]\n\nPart-based format:\n[{"part":"Part 1","pages":[{"title":"Welcome","points":["point1","point2"]}]}]`,+ })}`;}export function getPageDescriptionPrompt(@@ -94,64 +75,26 @@ export function getPageDescriptionPrompt(const originalInput = projectContext.idea_prompt || "";if (language === "en") {- return `${filesXml}We are generating per-slide content descriptions for a PPT.-The user's original request is:\n${originalInput}\n-We already have the full outline:\n${JSON.stringify(outline)}\n${partInfo}-Now generate the description for slide ${pageIndex}:-${JSON.stringify(pageOutline)}--[IMPORTANT] The "Slide text" section will be rendered directly on the PPT slide. Please ensure:-1. Keep text concise; each bullet is ~8–16 words-2. Use clear list structure-3. Avoid long sentences and overly complex wording-4. Make it highly readable for live presentation-5. Do not add any extra explanatory notes or commentary--Output format example:-Slide title: Human societies: living with nature--Slide text:-- Hunter-gatherer societies: limited impact due to small scale-- High dependence: life relies on direct natural supply-- Adaptation over transformation: learn from nature to survive-- Impact pattern: local, short-term, low intensity, self-recovering--Other slide materials (add when helpful: markdown image links, formulas, tables, etc.)--[About images] If reference files include local image URLs starting with /files/ (e.g. /files/mineru/xxx/image.png), output them in markdown, e.g. . These images will be included in the slide.--${getLanguageInstruction(language)}-`;+ return `${filesXml}${buildRisenPrompt({+ role: "You are a per-slide PPT content description agent.",+ instructions: `Generate one slide description in English. The 'Slide text' will be rendered directly onto the slide, so it must stay concise and highly readable. ${getLanguageInstruction(language)}`.trim(),+ input: `Original request:\n${originalInput}\n\nFull outline:\n${JSON.stringify(outline)}\n${partInfo}\nTarget slide ${pageIndex}:\n${JSON.stringify(pageOutline)}`,+ steps: `1. Read the full outline and the target slide context.\n2. Write a concise slide title.\n3. Write slide text as clear bullets, about 8-16 words each.\n4. Add other slide materials only when helpful, including markdown image links when reference files include local image URLs.`,+ endGoal: "Produce one slide description that can be rendered directly into a readable presentation slide.",+ narrowing: "1. Avoid long sentences.\n2. Do not add commentary outside the requested sections.\n3. Keep content optimized for live presentation.",+ outputFormat: `Slide title: Human societies: living with nature\n\nSlide text:\n- Hunter-gatherer societies: limited impact due to small scale\n- High dependence: life relies on direct natural supply\n\nOther slide materials`,+ })}`;}- return `${filesXml}我们正在为PPT的每一页生成内容描述。-用户的原始需求是:\n${originalInput}\n-我们已经有了完整的大纲:\n${JSON.stringify(outline)}\n${partInfo}-现在请为第 ${pageIndex} 页生成描述:-${JSON.stringify(pageOutline)}--【重要提示】生成的"页面文字"部分会直接渲染到PPT页面上,因此请务必注意:-1. 文字内容要简洁精炼,每条要点控制在15-25字以内-2. 条理清晰,使用列表形式组织内容-3. 避免冗长的句子和复杂的表述-4. 确保内容可读性强,适合在演示时展示-5. 不要包含任何额外的说明性文字或注释--输出格式示例:-页面标题:原始社会:与自然共生--页面文字:-- 狩猎采集文明:人类活动规模小,对环境影响有限-- 依赖性强:生活完全依赖自然资源的直接供给-- 适应而非改造:通过观察学习自然,发展生存技能-- 影响特点:局部、短期、低强度,生态可自我恢复--其他页面素材(如果有请积极添加,包括markdown图片链接、公式、表格等)--【关于图片】如果参考文件中包含以 /files/ 开头的本地文件URL图片(例如 /files/mineru/xxx/image.png),请将这些图片以markdown格式输出,例如:。这些图片会被包含在PPT页面中。--${getLanguageInstruction(language)}-`;+ return `${filesXml}${buildRisenPrompt({+ role: "你是一名逐页 PPT 内容描述生成智能体。",+ instructions: `请为单页 PPT 生成内容描述。“页面文字”会直接渲染到幻灯片上,因此必须简洁、清晰、适合演示。${getLanguageInstruction(language)}`.trim(),+ input: `原始需求:\n${originalInput}\n\n完整大纲:\n${JSON.stringify(outline)}\n${partInfo}\n目标页(第 ${pageIndex} 页):\n${JSON.stringify(pageOutline)}`,+ steps: `1. 阅读完整大纲和目标页上下文。\n2. 生成一个简洁的页面标题。\n3. 生成页面文字,每条控制在 15-25 字左右。\n4. 如有帮助,可补充其他页面素材,包括 markdown 图片链接、公式或表格。`,+ endGoal: "输出一页可直接用于渲染 PPT 的内容描述。",+ narrowing: "1. 避免冗长句子和复杂表述。\n2. 不要输出额外说明或注释。\n3. 如果参考文件里有 /files/ 开头的本地图片 URL,可以用 markdown 图片格式输出。",+ outputFormat: `页面标题:原始社会:与自然共生\n\n页面文字:\n- 狩猎采集文明:人类活动规模小,对环境影响有限\n- 依赖性强:生活完全依赖自然资源的直接供给\n\n其他页面素材`,+ })}`;}export function getImageGenerationPrompt(@@ -177,57 +120,24 @@ export function getImageGenerationPrompt(: "";if (language === "en") {- return `You are an expert UI/UX presentation designer. Generate a well-designed PPT slide.-The current slide description:-<page_description>-${pageDesc}-</page_description>--<reference_information>-Overall outline:-${outlineText}--Current section: ${currentSection}-</reference_information>--<design_guidelines>-- Text must be crisp and sharp; 4K quality; 16:9 aspect ratio.-- Colors and design language must closely match the template reference image.-- Create the best composition automatically and render all text from the description accurately.-- Unless necessary, avoid markdown symbols (like #, *).-- Use the template only for style; do not include any template text.-- Fill empty areas with appropriately sized decorative shapes or illustrations.-</design_guidelines>-${getPptLanguageInstruction(language)}-${materialImagesNote}${extraReqText}-`;+ return buildRisenPrompt({+ role: "You are an expert PPT visual design agent.",+ instructions: `Generate one polished PPT slide image. ${getPptLanguageInstruction(language)}${materialImagesNote}${extraReqText}`.trim(),+ input: `Page description:\n${pageDesc}\n\nOverall outline:\n${outlineText || "(none)"}\n\nCurrent section: ${currentSection || "(none)"}`,+ steps: `1. Read the page description and outline context.\n2. Build the strongest slide composition for the content.\n3. Follow the template reference image style closely.\n4. If material images are provided, integrate only the useful ones.`,+ endGoal: "Produce one sharp, presentation-ready 16:9 slide image that renders the described content accurately.",+ narrowing: "1. Keep text crisp and readable.\n2. Avoid markdown symbols unless strictly required.\n3. Use the template for style only; do not copy template text.\n4. Fill empty regions with suitable decorative shapes or illustrations.",+ });}- return `你是一位专家级UI UX演示设计师,专注于生成设计良好的PPT页面。-当前PPT页面的页面描述如下:-<page_description>-${pageDesc}-</page_description>--<reference_information>-整个PPT的大纲为:-${outlineText}--当前位于章节:${currentSection}-</reference_information>---<design_guidelines>-- 要求文字清晰锐利, 画面为4K分辨率,16:9比例。-- 配色和设计语言和模板图片严格相似。-- 根据内容自动设计最完美的构图,不重不漏地渲染"页面描述"中的文本。-- 如非必要,禁止出现 markdown 格式符号(如 # 和 * 等)。-- 只参考风格设计,禁止出现模板中的文字。-- 使用大小恰当的装饰性图形或插画对空缺位置进行填补。-</design_guidelines>-${getPptLanguageInstruction(language)}-${materialImagesNote}${extraReqText}-`;+ return buildRisenPrompt({+ role: "你是一名专家级 PPT 视觉设计智能体。",+ instructions: `生成一张完成度很高的 PPT 页面图。${getPptLanguageInstruction(language)}${materialImagesNote}${extraReqText}`.trim(),+ input: `页面描述:\n${pageDesc}\n\n整体大纲:\n${outlineText || "(none)"}\n\n当前章节:${currentSection || "(none)"}`,+ steps: `1. 阅读页面描述和整套 PPT 上下文。\n2. 自动组织最佳构图与视觉层级。\n3. 严格参考模板图片的风格与设计语言。\n4. 如果提供了素材图,只在确实有帮助时整合进去。`,+ endGoal: "产出一张清晰、锐利、适合正式演示的 16:9 PPT 页面图。",+ narrowing: "1. 文字必须清晰可读。\n2. 非必要不要出现 markdown 符号。\n3. 只参考模板风格,不得复用模板中的文字。\n4. 用适量装饰图形或插画填补空缺区域。",+ });}export function getTemplateGenerationPrompt(args: { requirements: string; language?: string }) {@@ -235,18 +145,22 @@ export function getTemplateGenerationPrompt(args: { requirements: string; languaconst requirements = String(args.requirements || "").trim();const pageDesc =language === "en"- ? `We are generating a PPT template background image (blank slide) that will be used as a style reference for generating slides later.-User requirements:-${requirements}-Rules:-- Generate a clean template background for a 16:9 slide.-- Do NOT include any text, letters, or watermarks.`- : `我们正在生成一张PPT模板背景图(空白页面),用于后续生成整套PPT时的风格参考。-用户需求:-${requirements}-规则:-- 生成一张16:9比例的干净模板背景图。-- 禁止出现任何文字、字母或水印。`;+ ? buildRisenPrompt({+ role: "You are a PPT template background design agent.",+ instructions: "Generate one blank slide background image that will be used as a style reference later.",+ input: `User requirements:\n${requirements || "(none)"}`,+ steps: "1. Interpret the requested visual direction.\n2. Design a clean background for a 16:9 slide.\n3. Keep it reusable as a template reference.",+ endGoal: "Produce one text-free PPT template background image.",+ narrowing: "1. Do not include text, letters, or watermarks.\n2. Keep the slide clean and reusable.",+ })+ : buildRisenPrompt({+ role: "你是一名 PPT 模板背景设计智能体。",+ instructions: "生成一张空白幻灯片背景图,供后续整套 PPT 作为风格参考使用。",+ input: `用户需求:\n${requirements || "(none)"}`,+ steps: "1. 理解用户需要的视觉方向。\n2. 设计一张 16:9 比例的干净模板背景图。\n3. 保持它适合作为后续模板参考。",+ endGoal: "产出一张不含文字的 PPT 模板背景图。",+ narrowing: "1. 禁止出现任何文字、字母或水印。\n2. 保持页面干净、可复用。",+ });return getImageGenerationPrompt(pageDesc, "", "", false, "", language);}diff --git a/src/lib/ppt-service.ts b/src/lib/ppt-service.tsindex d1a8142..ac29491 100644--- a/src/lib/ppt-service.ts+++ b/src/lib/ppt-service.ts@@ -1,6 +1,7 @@import PptxGenJS from "pptxgenjs"-import { PDFDocument } from "pdf-lib"-import { generateChatMessage, generateImage } from "./ai-client"+import { PDFDocument, rgb } from "pdf-lib"+import { generateChatMessage, generateImage, generateVisionChatMessage } from "./ai-client"+import { buildRisenPrompt } from "./risen-prompt"import pptOutlineSystem from "../../agent/ppt/outline.md?raw"import pptSlidesGenerateSystem from "../../agent/ppt/slides-generate.md?raw"@@ -11,14 +12,126 @@ export interface PptPage {note?: string;layout?: string;materialLabels?: string[];+ textBlocks?: PptTextBlock[];+ backgroundImageUrl?: string;status?: string;id?: string;}+export interface PptTextStyle {+ fontFamily?: string;+ fontSize?: number;+ fontWeight?: number;+ fontStyle?: "normal" | "italic";+ color?: string;+ gradientFrom?: string;+ gradientTo?: string;+ strokeColor?: string;+ strokeWidth?: number;+ align?: "left" | "center" | "right";+ lineHeight?: number;+ letterSpacing?: number;+}++export interface PptTextBlock {+ id: string;+ role: "title" | "bullet" | "summary" | "tag";+ text: string;+ x: number;+ y: number;+ w: number;+ h: number;+ style?: PptTextStyle;+}++export const PPT_REFERENCE_SLIDE_WIDTH = 1600;+export const PPT_REFERENCE_SLIDE_HEIGHT = 900;++const countVisualLineUnits = (text: string) => {+ const line = String(text || "");+ let units = 0;+ for (const ch of line) {+ if (/\s/.test(ch)) {+ units += 0.35;+ } else if (/[\u4e00-\u9fff\u3040-\u30ff\uac00-\ud7af]/.test(ch)) {+ units += 1;+ } else if (/[A-Z]/.test(ch)) {+ units += 0.72;+ } else if (/[a-z]/.test(ch)) {+ units += 0.58;+ } else if (/[0-9]/.test(ch)) {+ units += 0.56;+ } else {+ units += 0.62;+ }+ }+ return Math.max(units, 1);+};++export const estimateTextBlockFontSize = (+ block: PptTextBlock,+ canvasWidth: number,+ canvasHeight: number,+): number => {+ const style = block.style || {};+ const rawText = String(block.text || "");+ const isSingleLine = !rawText.includes("\n");+ const isPanelHeading =+ block.role === "bullet" &&+ isSingleLine &&+ rawText.trim().length <= 24 &&+ block.h <= 0.14;+ const lineHeightRatio = Number(+ style.lineHeight || (block.role === "title" ? 1.12 : block.role === "tag" ? 1.05 : isPanelHeading ? 1.18 : 1.35)+ );+ const paddingX = block.role === "title" ? 18 : block.role === "tag" ? 16 : isPanelHeading ? 16 : 24;+ const paddingY = block.role === "title" ? 12 : block.role === "tag" ? 10 : isPanelHeading ? 12 : 16;+ const availableWidth = Math.max(24, block.w * canvasWidth - paddingX);+ const availableHeight = Math.max(18, block.h * canvasHeight - paddingY);+ const lines = rawText.split(/\r?\n/).filter(Boolean);+ const lineCount = Math.max(lines.length, 1);+ const longestLineUnits = lines.length > 0+ ? Math.max(...lines.map((line) => countVisualLineUnits(line)))+ : countVisualLineUnits(rawText);+ const sizeByHeight = availableHeight / (lineCount * lineHeightRatio);+ const widthFactor =+ block.role === "title" ? 0.9 :+ block.role === "tag" ? 1.1 :+ isPanelHeading ? 1.06 :+ 1.72;+ const sizeByWidth = availableWidth / (longestLineUnits * widthFactor);+ const baseFitted = Math.max(10, Math.min(sizeByHeight, sizeByWidth));+ const boosted =+ block.role === "title"+ ? Math.max(baseFitted, sizeByHeight * 0.78)+ : isPanelHeading+ ? Math.max(baseFitted, sizeByHeight * 0.54)+ : block.role === "tag"+ ? Math.max(baseFitted, sizeByHeight * 0.62)+ : baseFitted;+ const fitted = Math.max(10, boosted);+ const hinted = Number(style.fontSize || 0);+ if (!Number.isFinite(hinted) || hinted <= 0) {+ return Math.round(fitted);+ }+ const deltaRatio = Math.abs(hinted - fitted) / Math.max(fitted, 1);+ if (deltaRatio <= 0.18) return Math.round(hinted);+ const hintedWeight =+ block.role === "title" ? 0.34 :+ isPanelHeading ? 0.28 :+ block.role === "tag" ? 0.24 :+ 0.18;+ return Math.round(fitted * (1 - hintedWeight) + hinted * hintedWeight);+};+export interface SlideEditRoutingItem {slideId: string;- kind: "content" | "visual" | "both";+ editType: "text_only" | "text_relayout" | "background_redraw";instruction: string;+ materialImageUrls?: string[];+ styleRefSlideIds?: string[];+ styleRefPolicy?: "style_only" | "style_and_layout";+ styleRefImageUrls?: string[];}const normalizeLayoutByLanguage = (layout: string, uiLanguage: "zh" | "en") => {@@ -60,11 +173,225 @@ const normalizeSpeakerNote = (note: string, uiLanguage: "zh" | "en") => {type ReferenceFileInput = { filename: string; content: string };type ReferenceImageAssetInput = { label: string; caption: string; sourceFile: string; sourcePage?: number };-const PPT_OUTLINE_SYSTEM = String(pptOutlineSystem || "").trim() || `You are a presentation outline generator.-Return ONLY JSON. Do not wrap in markdown code blocks. Do not include any extra text.`;+const PPT_OUTLINE_SYSTEM = String(pptOutlineSystem || "").trim() || buildRisenPrompt({+ role: "You are a PPT planning agent.",+ instructions: "Generate PPT plan JSON only.",+ input: "One idea prompt or one outline prompt, plus UI language and optional references.",+ steps: "1. Understand the planning mode.\n2. Build the slide sequence.\n3. Return complete slide plan JSON only.",+ endGoal: "Produce a machine-readable PPT plan for rendering.",+ narrowing: "1. No markdown wrapper.\n2. No extra explanation.\n3. JSON only.",+});++const PPT_SLIDES_GENERATE_SYSTEM = String(pptSlidesGenerateSystem || "").trim() || buildRisenPrompt({+ role: "You are a PPT slide refinement agent.",+ instructions: "Refine one or more slide records and return JSON only.",+ input: "Current slide JSON, topic context, language, and optional references.",+ steps: "1. Improve slide text and description.\n2. Keep ids stable.\n3. Return JSON only.",+ endGoal: "Produce render-ready slide JSON.",+ narrowing: "1. No markdown wrapper unless the caller explicitly allows it.\n2. No extra commentary.",+});++const PPT_TEXT_EXTRACTION_SYSTEM = buildRisenPrompt({+ role: "You extract editable slide-layer text from a rendered PPT slide.",+ instructions: "Return JSON only.",+ input: "One full rendered slide image plus expected slide text candidates.",+ steps: "1. Find only slide-layer text and ignore text inside material images.\n2. Determine geometry first: text, role, x, y, w, h relative to the full slide.\n3. Then estimate style: fontSize relative to the full slide, fontWeight, color, align, lineHeight, optional gradient and stroke.",+ endGoal: "Produce one machine-readable textBlocks array for editable slide text.",+ narrowing: "1. JSON only.\n2. Ignore text inside embedded material images.\n3. Keep coordinates relative to the full slide.",+});++const PPT_TEXT_REVIEW_SYSTEM = buildRisenPrompt({+ role: "You review a composed slide text layer against the original rendered slide.",+ instructions: "Return JSON only.",+ input: "Image 1: original rendered slide. Image 2: textless background plus current text layer composite. Current textBlocks JSON.",+ steps: "1. Compare image 2 against image 1 only for slide-layer text.\n2. Check missing text, wrong position, wrong relative size, and obvious style mismatch.\n3. Return one corrected full textBlocks array.",+ endGoal: "Produce a corrected textBlocks array that matches the original slide-layer text closely.",+ narrowing: "1. JSON only.\n2. Review only slide-layer text.\n3. Ignore text inside material images.",+});++const clamp01 = (value: number) => {+ if (!Number.isFinite(value)) return 0;+ return Math.max(0, Math.min(1, value));+};++const normalizeTextAlign = (value: unknown): "left" | "center" | "right" => {+ if (value === "center" || value === "right") return value;+ return "left";+};++const normalizeTextBlocks = (value: any): PptTextBlock[] => {+ const asArray = Array.isArray(value) ? value : Array.isArray(value?.textBlocks) ? value.textBlocks : null;+ if (!asArray) return [];+ return asArray+ .map((item: any, idx: number) => {+ const text = typeof item?.text === "string" ? item.text.trim() : "";+ if (!text) return null;+ const role: PptTextBlock["role"] =+ item?.role === "title" || item?.role === "bullet" || item?.role === "summary" || item?.role === "tag"+ ? item.role+ : text.length <= 12 && Number(item?.w || 0) <= 0.2 && Number(item?.h || 0) <= 0.09+ ? "tag"+ : idx === 0+ ? "title"+ : "bullet";+ const style = item?.style && typeof item.style === "object"+ ? {+ fontFamily: typeof item.style.fontFamily === "string" ? item.style.fontFamily : undefined,+ fontSize: typeof item.style.fontSize === "number" && Number.isFinite(item.style.fontSize) ? item.style.fontSize : undefined,+ fontWeight: typeof item.style.fontWeight === "number" && Number.isFinite(item.style.fontWeight) ? item.style.fontWeight : undefined,+ fontStyle: item.style.fontStyle === "italic" ? "italic" : "normal",+ color: typeof item.style.color === "string" ? item.style.color : undefined,+ gradientFrom: typeof item.style.gradientFrom === "string" ? item.style.gradientFrom : undefined,+ gradientTo: typeof item.style.gradientTo === "string" ? item.style.gradientTo : undefined,+ strokeColor: typeof item.style.strokeColor === "string" ? item.style.strokeColor : undefined,+ strokeWidth: typeof item.style.strokeWidth === "number" && Number.isFinite(item.style.strokeWidth) ? item.style.strokeWidth : undefined,+ align: normalizeTextAlign(item.style.align),+ lineHeight: typeof item.style.lineHeight === "number" && Number.isFinite(item.style.lineHeight) ? item.style.lineHeight : undefined,+ letterSpacing: typeof item.style.letterSpacing === "number" && Number.isFinite(item.style.letterSpacing) ? item.style.letterSpacing : undefined,+ } satisfies PptTextStyle+ : undefined;+ return {+ id: typeof item?.id === "string" && item.id.trim() ? item.id.trim() : `text-block-${idx + 1}`,+ role,+ text,+ x: clamp01(Number(item?.x)),+ y: clamp01(Number(item?.y)),+ w: clamp01(Number(item?.w || 0.1)),+ h: clamp01(Number(item?.h || 0.05)),+ style,+ } satisfies PptTextBlock;+ })+ .filter(Boolean) as PptTextBlock[];+};++const parseColorToRgb = (value?: string) => {+ const raw = String(value || "").trim();+ if (!raw) return rgb(0.07, 0.09, 0.13);+ const hex = raw.replace(/^#/, "");+ if (/^[0-9a-fA-F]{6}$/.test(hex)) {+ const r = parseInt(hex.slice(0, 2), 16) / 255;+ const g = parseInt(hex.slice(2, 4), 16) / 255;+ const b = parseInt(hex.slice(4, 6), 16) / 255;+ return rgb(r, g, b);+ }+ const rgbMatch = raw.match(/rgb\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*\)/i);+ if (rgbMatch) {+ return rgb(+ Math.max(0, Math.min(255, Number(rgbMatch[1]))) / 255,+ Math.max(0, Math.min(255, Number(rgbMatch[2]))) / 255,+ Math.max(0, Math.min(255, Number(rgbMatch[3]))) / 255,+ );+ }+ return rgb(0.07, 0.09, 0.13);+};++const loadImageElement = async (src: string): Promise<HTMLImageElement> =>+ new Promise((resolve, reject) => {+ const img = new Image();+ img.onload = () => resolve(img);+ img.onerror = () => reject(new Error("Failed to load image for PDF export"));+ img.src = src;+ });++const renderPageToDataUri = async (+ page: PptPage,+ imageUrl?: string,+ width = PPT_REFERENCE_SLIDE_WIDTH,+ height = PPT_REFERENCE_SLIDE_HEIGHT,+): Promise<string> => {+ const canvas = document.createElement("canvas");+ canvas.width = width;+ canvas.height = height;+ const ctx = canvas.getContext("2d");+ if (!ctx) throw new Error("Canvas context unavailable for PDF export");++ ctx.fillStyle = "#ffffff";+ ctx.fillRect(0, 0, width, height);++ if (imageUrl) {+ const dataUri = await urlToDataUri(imageUrl);+ const img = await loadImageElement(dataUri);+ ctx.drawImage(img, 0, 0, width, height);+ }++ if (Array.isArray(page.textBlocks) && page.textBlocks.length > 0) {+ for (const block of page.textBlocks) {+ const style = block.style || {};+ const fontSize = estimateTextBlockFontSize(block, width, height);+ const x = block.x * width;+ const y = block.y * height;+ const w = block.w * width;+ const h = block.h * height;+ const lineHeight = fontSize * Number(style.lineHeight || (block.role === "title" ? 1.12 : block.role === "tag" ? 1.05 : 1.35));+ const lines = String(block.text || "").split(/\r?\n/).filter(Boolean);+ const fontWeight = Number(style.fontWeight || (block.role === "title" ? 900 : block.role === "tag" ? 800 : 500));+ const fontStyle = style.fontStyle === "italic" ? "italic " : "";+ const fontFamily = style.fontFamily || "Microsoft YaHei";+ const paddingX = block.role === "title" ? 18 : block.role === "tag" ? 16 : 24;+ const paddingY = block.role === "title" ? 12 : block.role === "tag" ? 10 : 16;+ const isTag = block.role === "tag";++ if (isTag) {+ ctx.save();+ ctx.fillStyle = "rgba(26,89,160,0.55)";+ ctx.strokeStyle = "rgba(116,217,255,0.55)";+ ctx.lineWidth = 2;+ const radius = Math.min(h / 2, 18);+ ctx.beginPath();+ ctx.moveTo(x + radius, y);+ ctx.lineTo(x + w - radius, y);+ ctx.quadraticCurveTo(x + w, y, x + w, y + radius);+ ctx.lineTo(x + w, y + h - radius);+ ctx.quadraticCurveTo(x + w, y + h, x + w - radius, y + h);+ ctx.lineTo(x + radius, y + h);+ ctx.quadraticCurveTo(x, y + h, x, y + h - radius);+ ctx.lineTo(x, y + radius);+ ctx.quadraticCurveTo(x, y, x + radius, y);+ ctx.closePath();+ ctx.fill();+ ctx.stroke();+ ctx.restore();+ }-const PPT_SLIDES_GENERATE_SYSTEM = String(pptSlidesGenerateSystem || "").trim() || `You are a presentation slide planning agent.-Return ONLY JSON. Do not include any extra text.`;+ ctx.save();+ ctx.font = `${fontStyle}${fontWeight} ${fontSize}px "${fontFamily}"`;+ ctx.textBaseline = "top";+ ctx.shadowColor = block.role === "title" ? "rgba(15,23,42,0.55)" : "rgba(15,23,42,0.18)";+ ctx.shadowBlur = block.role === "title" ? 10 : 4;+ ctx.shadowOffsetX = 0;+ ctx.shadowOffsetY = 1;+ ctx.lineJoin = "round";+ if (style.strokeColor) {+ ctx.strokeStyle = style.strokeColor;+ ctx.lineWidth = Number(style.strokeWidth || 1.2);+ }+ if (style.gradientFrom && style.gradientTo) {+ const gradient = ctx.createLinearGradient(x, y, x + w, y);+ gradient.addColorStop(0, style.gradientFrom);+ gradient.addColorStop(1, style.gradientTo);+ ctx.fillStyle = gradient;+ } else {+ ctx.fillStyle = style.color || (block.role === "title" ? "#ffffff" : block.role === "tag" ? "#ffd66b" : "#ffffff");+ }++ lines.forEach((line, idx) => {+ const metrics = ctx.measureText(line);+ let drawX = x + paddingX / 2;+ if (style.align === "center") {+ drawX = x + w / 2 - metrics.width / 2;+ } else if (style.align === "right") {+ drawX = x + w - metrics.width - paddingX / 2;+ }+ const drawY = y + paddingY / 2 + idx * lineHeight;+ if (style.strokeColor) ctx.strokeText(line, drawX, drawY);+ ctx.fillText(line, drawX, drawY);+ });+ ctx.restore();+ }+ }++ return canvas.toDataURL("image/png");+};const formatReferenceFiles = (referenceFiles?: ReferenceFileInput[]) => {if (!Array.isArray(referenceFiles) || referenceFiles.length === 0) return "";@@ -191,38 +518,14 @@ export const pptService = {): Promise<PptPage[]> => {const refText = formatReferenceFiles(referenceFiles);const imageAssetText = formatReferenceImageAssets(referenceImageAssets);- const prompt = `PlanFromIdeaAgent-required:-- idea_prompt: ${topic}-- ui_language: ${uiLanguage}-optional:-- reference_files_content:-${refText || "(none)"}-- reference_image_assets:-${imageAssetText || "(none)"}--Generate a complete PptPlan.-Return JSON only (markdown json block allowed).-Every slide must include:-- id-- title-- content (array of detailed bullets)-- description-- layout-- note-- materialLabels (array of labels selected from reference_image_assets, max 3; no label not in assets list)-Rules for materialLabels:-- Only assign when truly relevant to this slide.-- It is valid to return [] for slides that do not need material images.-Quality requirements:-- Each slide content should have 4-6 bullets.-- Each bullet should be specific and informative, not generic placeholders.-- description should clearly explain visual hierarchy and key visual elements (including placement hints when useful).-- note must be a coherent speaker script that can be spoken directly (3-6 connected sentences with natural transitions).-- note must be plain paragraph text, not bullet points and not numbered lists.-- All slide fields (title/content/description/note) MUST follow ui_language.-- If ui_language=zh, use Simplified Chinese (except unavoidable proper nouns/acronyms).-`;+ const prompt = buildRisenPrompt({+ role: "You are PlanFromIdeaAgent for PPT planning.",+ instructions: "Generate a complete PptPlan from the idea prompt and return JSON only.",+ input: `idea_prompt: ${topic}\nui_language: ${uiLanguage}\n\nreference_files_content:\n${refText || "(none)"}\n\nreference_image_assets:\n${imageAssetText || "(none)"}`,+ steps: "1. Convert the idea into a coherent slide sequence.\n2. Write 4-6 specific bullets per slide.\n3. Write a concrete visual description for each slide.\n4. Write a coherent speaker note paragraph for each slide.\n5. Assign materialLabels only when truly relevant.",+ endGoal: "Produce a complete render-ready PptPlan with stable slide ids and usable materialLabels.",+ narrowing: "1. Return JSON only; markdown JSON block is allowed.\n2. Every slide must include id, title, content, description, layout, note, materialLabels.\n3. materialLabels must come only from reference_image_assets and can be [].\n4. All fields must follow ui_language; zh means Simplified Chinese.",+ });const response = await generateChatMessage([{ role: "system", content: PPT_OUTLINE_SYSTEM },@@ -255,39 +558,14 @@ Quality requirements:): Promise<PptPage[]> => {const refText = formatReferenceFiles(referenceFiles);const imageAssetText = formatReferenceImageAssets(referenceImageAssets);- const prompt = `PlanFromOutlineAgent-required:-- outline_text:-${outlineText}-- ui_language: ${uiLanguage}-optional:-- reference_files_content:-${refText || "(none)"}-- reference_image_assets:-${imageAssetText || "(none)"}--Generate a complete PptPlan from this outline.-Return JSON only (markdown json block allowed).-Every slide must include:-- id-- title-- content-- description-- layout-- note-- materialLabels (array of labels selected from reference_image_assets, max 3; no label not in assets list)-Rules for materialLabels:-- Only assign when truly relevant to this slide.-- It is valid to return [] for slides that do not need material images.-Quality requirements:-- Each slide content should have 4-6 bullets.-- Each bullet should be specific and informative, not generic placeholders.-- description should clearly explain visual hierarchy and key visual elements (including placement hints when useful).-- note must be a coherent speaker script that can be spoken directly (3-6 connected sentences with natural transitions).-- note must be plain paragraph text, not bullet points and not numbered lists.-- All slide fields (title/content/description/note) MUST follow ui_language.-- If ui_language=zh, use Simplified Chinese (except unavoidable proper nouns/acronyms).-`;+ const prompt = buildRisenPrompt({+ role: "You are PlanFromOutlineAgent for PPT planning.",+ instructions: "Generate a complete PptPlan from the outline and return JSON only.",+ input: `outline_text:\n${outlineText}\n\nui_language: ${uiLanguage}\n\nreference_files_content:\n${refText || "(none)"}\n\nreference_image_assets:\n${imageAssetText || "(none)"}`,+ steps: "1. Expand the outline into a coherent slide sequence.\n2. Write 4-6 specific bullets per slide.\n3. Write a concrete visual description for each slide.\n4. Write a coherent speaker note paragraph for each slide.\n5. Assign materialLabels only when truly relevant.",+ endGoal: "Produce a complete render-ready PptPlan derived from the outline.",+ narrowing: "1. Return JSON only; markdown JSON block is allowed.\n2. Every slide must include id, title, content, description, layout, note, materialLabels.\n3. materialLabels must come only from reference_image_assets and can be [].\n4. All fields must follow ui_language; zh means Simplified Chinese.",+ });const response = await generateChatMessage([{ role: "system", content: PPT_OUTLINE_SYSTEM },{ role: "user", content: prompt }@@ -337,19 +615,14 @@ Quality requirements:2);- const prompt = `Topic: ${topic}-Slide index: ${index + 1}--Current slide:-${currentSlideJson}--${refText ? `Reference:\n${refText}\n\n` : ""}Task:-- Improve bullets (<= 12 words each), and refine description for image generation.-- Optionally add speaker note and layout hint if helpful.-- If note is provided, write it as a coherent spoken script (3-6 connected sentences), not bullets.-- Keep id stable.--Return JSON only (no extra text).`;+ const prompt = buildRisenPrompt({+ role: "You are a single-slide PPT refinement agent.",+ instructions: "Improve one slide record and return JSON only.",+ input: `Topic: ${topic}\nSlide index: ${index + 1}\n\nCurrent slide:\n${currentSlideJson}${refText ? `\n\nReference:\n${refText}` : ""}`,+ steps: "1. Improve bullets to be concise and presentation-friendly, with at most 12 words each.\n2. Refine the image description.\n3. Add or improve speaker note and layout hint only when helpful.\n4. Keep the slide id stable.",+ endGoal: "Produce one improved slide JSON record that is ready for image generation.",+ narrowing: "1. Return JSON only.\n2. If note exists, write it as a coherent spoken script of 3-6 connected sentences.\n3. No extra text outside the JSON payload.",+ });const response = await generateChatMessage([@@ -397,26 +670,14 @@ Return JSON only (no extra text).`;.map((x, i) => `${i + 1}. ${x.label || `MATERIAL_${i + 1}`}`).join("\n"): "(none)";- const prompt = `Design a presentation slide image.-Title: ${page.title}-Bullets: ${(page.content || []).join(" | ")}-Language: ${uiLanguage}-Visual style: modern, professional, clean layout, high contrast, no watermarks.-Scene/subject: ${page.description || page.title}-Format: Landscape 16:9 aspect ratio.-Rules:-- MUST follow the template reference image style consistently for this slide.-- Avoid large paragraphs of text in the image.-- Prefer diagrammatic/illustrative composition matching the bullets.-- Uploaded material labels and order (matches additional reference images order exactly):-${additionalLabelText}-- If slide description contains token {{image:Name}}, you MUST use the material image whose label is Name.-- Preserve referenced material image identity exactly: do not redraw, restyle, recolor, or replace with similar content.-- Keep referenced material aspect ratio and key details intact; avoid cropping away chart/table/flow core content.-- Follow position instructions in Scene/subject when placing referenced materials (left/right/top/bottom/center/background).-- If no token is present for a material label, do not force that material into the slide.-${extraRequirements ? `- Extra requirements: ${extraRequirements}` : ""}-`;+ const prompt = buildRisenPrompt({+ role: "You are a PPT slide image generation agent.",+ instructions: "Generate one modern, professional slide image in 16:9.",+ input: `Title: ${page.title}\nBullets: ${(page.content || []).join(" | ")}\nLanguage: ${uiLanguage}\nScene/subject: ${page.description || page.title}\nTemplate style reference: ${templateImageUrl ? "provided" : "not provided"}\nUploaded material labels:\n${additionalLabelText}${extraRequirements ? `\n\nExtra requirements:\n${extraRequirements}` : ""}`,+ steps: "1. Design the slide composition from the title, bullets, and description.\n2. Follow the template reference style consistently.\n3. Use referenced material images only when required or clearly helpful.\n4. Preserve the identity and aspect ratio of referenced material images.",+ endGoal: "Produce one sharp, readable, presentation-ready slide image with accurate content coverage.",+ narrowing: "1. Avoid large paragraphs of text.\n2. Prefer diagrammatic or illustrative composition that matches the bullets.\n3. If the description contains {{image:Name}}, you must use the material image whose label is Name.\n4. Do not redraw, restyle, recolor, or replace referenced material images.\n5. Do not add watermarks.",+ });const additionalReferenceImageUrls = Array.isArray(normalizedAdditional)? normalizedAdditional.map((x) => String(x?.url || "")).filter(Boolean): [];@@ -428,31 +689,185 @@ ${extraRequirements ? `- Extra requirements: ${extraRequirements}` : ""}});},+ extractSlideTextBlocks: async (+ page: PptPage,+ slideImageUrl: string,+ uiLanguage: "zh" | "en" = "zh",+ ): Promise<PptTextBlock[]> => {+ const expectedTexts = [+ page.title ? `TITLE: ${page.title}` : "",+ ...(Array.isArray(page.content) ? page.content.map((item, idx) => `BULLET ${idx + 1}: ${item}`) : []),+ ].filter(Boolean);++ const prompt = buildRisenPrompt({+ role: "You are a slide-layer text extraction agent for rendered PPT slides.",+ instructions: "Extract editable slide-layer text blocks and return JSON only.",+ input: `language: ${uiLanguage}\nexpected text candidates:\n${expectedTexts.length > 0 ? expectedTexts.join("\n") : "(none)"}`,+ steps: "1. Extract only slide-layer text.\n2. Determine geometry first: text, role, x, y, w, h relative to the full slide.\n3. Then estimate style, especially relative font size.\n4. Keep meaningful short labels, including chip or pill labels.",+ endGoal: "Produce one complete textBlocks array that can drive editable slide text rendering.",+ narrowing: "1. Ignore text inside photos, screenshots, charts, tables, diagrams, devices, logos, and scanned materials.\n2. Prefer expected text candidates when they match visible text.\n3. Merge wrapped lines from one text box with \\n.\n4. Use role=title, bullet, summary, or tag.\n5. Use tight text boxes and preserve visible alignment and line breaks.\n6. Include dominant color, fontWeight, lineHeight, and optional gradient or stroke when visible.",+ outputFormat: `{"textBlocks":[{"id":"title-1","role":"title","text":"Example","x":0.1,"y":0.08,"w":0.6,"h":0.12,"style":{"fontFamily":"Microsoft YaHei","fontSize":30,"fontWeight":700,"fontStyle":"normal","color":"#ffffff","gradientFrom":"#ffffff","gradientTo":"#22d3ee","strokeColor":"rgba(8,15,36,0.35)","strokeWidth":1.2,"align":"left","lineHeight":1.2,"letterSpacing":0}}]}`,+ });++ const response = await generateVisionChatMessage(+ PPT_TEXT_EXTRACTION_SYSTEM,+ prompt,+ [slideImageUrl],+ undefined,+ );++ try {+ return normalizeTextBlocks(parseJsonLoose(response));+ } catch (error) {+ console.error("Failed to parse extracted PPT text blocks", error);+ return [];+ }+ },++ generateTextlessPageImage: async (+ page: PptPage,+ slideImageUrl: string,+ textBlocks: PptTextBlock[],+ uiLanguage: "zh" | "en" = "zh",+ ): Promise<string> => {+ const textList = textBlocks.length > 0+ ? textBlocks.map((block, idx) => `${idx + 1}. ${block.role}: ${block.text}`).join("\n")+ : [page.title, ...(page.content || [])].filter(Boolean).map((text, idx) => `${idx + 1}. ${text}`).join("\n");+ const textRegions = textBlocks.length > 0+ ? textBlocks+ .map((block, idx) =>+ `${idx + 1}. role=${block.role}; text=${block.text}; box=(x=${block.x.toFixed(3)}, y=${block.y.toFixed(3)}, w=${block.w.toFixed(3)}, h=${block.h.toFixed(3)})`+ )+ .join("\n")+ : "(none)";++ const prompt = buildRisenPrompt({+ role: "You are a textless slide background editing agent.",+ instructions: "Edit the provided slide image in place and remove only true slide-layer text while preserving the slide design pixel-for-pixel everywhere else as much as possible.",+ input: `language: ${uiLanguage}\nslide-layer text to remove:\n${textList || "(none)"}\n\napproximate text regions:\n${textRegions}`,+ steps: "1. Remove only the listed slide-layer text.\n2. Reconstruct the covered background from immediate surrounding visual cues.\n3. Keep all non-text shapes, icons, charts, photos, gradients, shadows, and composition unchanged.\n4. Preserve the original 16:9 layout exactly.",+ endGoal: "Produce one 16:9 textless background slide image that preserves the original design.",+ narrowing: "1. Keep everything else unchanged.\n2. Do not redesign, move, resize, crop, recolor, restyle, sharpen, blur, or recompose anything.\n3. Do not touch text inside material images, screenshots, charts, tables, logos, diagrams, or scanned content.\n4. Do not add any new text, icons, decorative elements, or cleanup artifacts.\n5. Output must still look like the same slide, just without the removable slide-layer text.",+ });++ return await generateImage({+ prompt,+ referenceImageUrl: slideImageUrl,+ });+ },++ reviewSlideTextBlocks: async (+ page: PptPage,+ originalSlideImageUrl: string,+ textlessBackgroundImageUrl: string,+ textBlocks: PptTextBlock[],+ uiLanguage: "zh" | "en" = "zh",+ ): Promise<PptTextBlock[]> => {+ if (!Array.isArray(textBlocks) || textBlocks.length === 0) return textBlocks;+ try {+ const composedDataUri = await renderPageToDataUri(+ { ...page, textBlocks, backgroundImageUrl: textlessBackgroundImageUrl },+ textlessBackgroundImageUrl,+ PPT_REFERENCE_SLIDE_WIDTH,+ PPT_REFERENCE_SLIDE_HEIGHT,+ );+ const currentJson = JSON.stringify({ textBlocks }, null, 2);+ const prompt = buildRisenPrompt({+ role: "You are a slide text review and correction agent.",+ instructions: "Review the current text layer against the original slide and return corrected JSON only.",+ input: `language: ${uiLanguage}\nimage 1: original rendered slide\nimage 2: textless background + current text layer\ncurrent textBlocks JSON:\n${currentJson}`,+ steps: "1. Compare image 2 with image 1 only for slide-layer text.\n2. Check missing text, wrong relative size, wrong position, and obvious style mismatch.\n3. Return one corrected full textBlocks array.",+ endGoal: "Produce a corrected textBlocks array that matches the original slide-layer text closely.",+ narrowing: "1. Ignore text inside material images.\n2. Keep geometry relative to the full slide.\n3. Correct only slide-layer text.\n4. Return JSON only.",+ outputFormat: `{"textBlocks":[...]}`,+ });+ const response = await generateVisionChatMessage(+ PPT_TEXT_REVIEW_SYSTEM,+ prompt,+ [originalSlideImageUrl, composedDataUri],+ undefined,+ );+ const reviewed = normalizeTextBlocks(parseJsonLoose(response));+ return reviewed.length > 0 ? reviewed : textBlocks;+ } catch (error) {+ console.error("Failed to review PPT text blocks", error);+ return textBlocks;+ }+ },++ rewriteSlideTextBlocks: async (+ page: PptPage,+ currentTextBlocks: PptTextBlock[],+ mode: "text_only" | "text_relayout",+ uiLanguage: "zh" | "en" = "zh",+ ): Promise<PptTextBlock[]> => {+ if (!Array.isArray(currentTextBlocks) || currentTextBlocks.length === 0) return currentTextBlocks;+ const pageJson = JSON.stringify({+ id: page.id,+ title: page.title,+ content: page.content || [],+ description: page.description || "",+ note: page.note || "",+ layout: page.layout || "",+ }, null, 2);+ const blocksJson = JSON.stringify({ textBlocks: currentTextBlocks }, null, 2);+ const prompt = buildRisenPrompt({+ role: "You are an editable PPT textBlocks rewrite agent.",+ instructions: "Update existing textBlocks to match the target slide content and return JSON only.",+ input: `language: ${uiLanguage}\nmode: ${mode}\ntarget slide content:\n${pageJson}\n\ncurrent textBlocks JSON:\n${blocksJson}`,+ steps: "1. Make textBlocks match the target slide content.\n2. If mode=text_only, keep geometry and style as unchanged as possible.\n3. If mode=text_relayout, you may adjust geometry and font size to fit the new content while keeping the overall layout close.\n4. Keep slide-layer labels or tags only if they still make sense.",+ endGoal: "Produce one full corrected textBlocks array for the updated slide.",+ narrowing: "1. Return the full corrected textBlocks array.\n2. Do not invent content outside the target slide content.\n3. Keep coordinates relative to the full slide.\n4. Return JSON only.",+ outputFormat: `{"textBlocks":[...]}`,+ });+ try {+ const response = await generateChatMessage([+ { role: "system", content: PPT_TEXT_EXTRACTION_SYSTEM },+ { role: "user", content: prompt }+ ]);+ const nextBlocks = normalizeTextBlocks(parseJsonLoose(response));+ return nextBlocks.length > 0 ? nextBlocks : currentTextBlocks;+ } catch (error) {+ console.error("Failed to rewrite PPT text blocks", error);+ return currentTextBlocks;+ }+ },+editPageImage: async (page: PptPage, instruction: string, referenceImageUrl?: string, templateImageUrl?: string, additionalImages?: string[]) => {- const prompt = `Edit the slide image based on instruction.-You will be given the current slide image as the primary reference image. Treat it as the base image and modify it, not regenerate from scratch.-If additional reference images are provided, they may include the template/style reference and user-provided assets. Keep the overall visual style consistent with the template.-Slide title: ${page.title}-Bullets: ${(page.content || []).join(" | ")}-Original visual description: ${page.description || ""}-Instruction: ${instruction}-Style: keep consistent with the template, professional, clean.-Format: Landscape 16:9 aspect ratio.-Rules:-- Keep layout readable.-- Do not add watermark.-`;+ const prompt = buildRisenPrompt({+ role: "You are a slide image editing agent.",+ instructions: "Edit the current slide image as the base image rather than regenerating from scratch.",+ input: `Slide title: ${page.title}\nBullets: ${(page.content || []).join(" | ")}\nOriginal visual description: ${page.description || ""}\nInstruction: ${instruction}\nTemplate/style reference: ${templateImageUrl ? "provided" : "not provided"}\nAdditional references: ${Array.isArray(additionalImages) && additionalImages.length > 0 ? "provided" : "none"}`,+ steps: "1. Start from the current slide image.\n2. Apply the requested visual edit.\n3. Keep the overall visual style consistent with the template and references.\n4. Preserve readability.",+ endGoal: "Produce one edited 16:9 slide image that reflects the instruction while staying visually consistent.",+ narrowing: "1. Treat the current slide image as the base image.\n2. Keep layout readable.\n3. Do not add a watermark.",+ });const ref = referenceImageUrl || templateImageUrl;- const additionalReferenceImageUrls = Array.isArray(additionalImages) ? [...additionalImages] : [];+ const additionalReferenceImageUrls = Array.isArray(additionalImages)+ ? Array.from(new Set(additionalImages.map((x) => String(x || "").trim()).filter(Boolean))).slice(0, 2)+ : [];if (referenceImageUrl && templateImageUrl && templateImageUrl !== referenceImageUrl) {additionalReferenceImageUrls.push(templateImageUrl);}- return await generateImage({- prompt,- referenceImageUrl: ref,- additionalReferenceImageUrls- });+ try {+ return await generateImage({+ prompt,+ referenceImageUrl: ref,+ additionalReferenceImageUrls+ });+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error || "");+ const shouldRetryWithoutExtras =+ additionalReferenceImageUrls.length > 0 &&+ /(status 4\d\d|status 500|too large|payload|image|context|token)/i.test(message);+ if (!shouldRetryWithoutExtras) throw error;+ console.warn("Retrying slide image edit without additional reference images", error);+ return await generateImage({+ prompt,+ referenceImageUrl: ref,+ });+ }},routeSlideEdits: async (slides: Array<{ id: string; title: string; bullets: string[]; description?: string; layout?: string; note?: string }>, feedback: string): Promise<SlideEditRoutingItem[]> => {@@ -460,20 +875,15 @@ Rules:.map((s, i) => `${i + 1}. id=${s.id}; title=${s.title}; bullets=${(s.bullets || []).join(" | ")}; description=${s.description || ""}; layout=${s.layout || ""}; note=${s.note || ""}`).join("\n");- const prompt = `You are a routing agent for slide edits.-Slides:-${slideList}--User feedback:-${feedback}--Return ONLY JSON array, each item:-{ "slideId": "slide-2", "kind": "content|visual|both", "instruction": "..." }-Rules:-- If feedback mentions multiple slides, split into multiple items.-- If you are unsure, assign to the most relevant slide.-- Do not invent new slides.-`;+ const prompt = buildRisenPrompt({+ role: "You are a routing agent for slide edits.",+ instructions: "Map user feedback to per-slide edit actions and return JSON only.",+ input: `Slides:\n${slideList}\n\nUser feedback:\n${feedback}`,+ steps: "1. Find the target slides.\n2. Split multi-slide feedback into separate items.\n3. Assign editType for each target slide.\n4. Write concise executable instructions.",+ endGoal: "Produce a routing JSON array that tells the system how to process each slide edit.",+ narrowing: "1. Return JSON array only.\n2. If unsure, assign the edit to the most relevant slide.\n3. Do not invent new slides.\n4. Use text_only for simple wording-only changes.\n5. Use text_relayout when text changes need reflow but not a new background.\n6. Use background_redraw when visuals, composition, or material usage must change.",+ outputFormat: `[{"slideId":"slide-2","editType":"text_only|text_relayout|background_redraw","instruction":"...","materialImageUrls":[],"styleRefSlideIds":[],"styleRefPolicy":"style_only|style_and_layout","styleRefImageUrls":[]}]`,+ });const response = await generateChatMessage([{ role: "system", content: PPT_SLIDES_GENERATE_SYSTEM },@@ -485,12 +895,21 @@ Rules:const parsed = JSON.parse(match[0]);if (!Array.isArray(parsed)) return [];return parsed- .map((it: any) => ({+ .map((it: any): SlideEditRoutingItem => ({slideId: String(it.slideId || ""),- kind: it.kind === "content" || it.kind === "visual" || it.kind === "both" ? it.kind : "both",+ editType:+ it.editType === "text_only" || it.editType === "text_relayout" || it.editType === "background_redraw"+ ? it.editType+ : it.kind === "content"+ ? "text_relayout"+ : "background_redraw",instruction: String(it.instruction || ""),+ materialImageUrls: Array.isArray(it.materialImageUrls) ? it.materialImageUrls.map((x: any) => String(x || "").trim()).filter(Boolean) : undefined,+ styleRefSlideIds: Array.isArray(it.styleRefSlideIds) ? it.styleRefSlideIds.map((x: any) => String(x || "").trim()).filter(Boolean) : undefined,+ styleRefPolicy: (it.styleRefPolicy === "style_and_layout" ? "style_and_layout" : "style_only") as SlideEditRoutingItem["styleRefPolicy"],+ styleRefImageUrls: Array.isArray(it.styleRefImageUrls) ? it.styleRefImageUrls.map((x: any) => String(x || "").trim()).filter(Boolean) : undefined,}))- .filter((it: SlideEditRoutingItem) => it.slideId.trim().length > 0 && it.instruction.trim().length > 0);+ .filter((it) => it.slideId.trim().length > 0 && it.instruction.trim().length > 0);} catch {return [];}@@ -505,7 +924,11 @@ Rules:for (const page of pages) {const slide = pptx.addSlide();- const img = page.id ? images[page.id] : undefined;+ const hasTextLayer = Array.isArray(page.textBlocks) && page.textBlocks.length > 0;+ const editableBackground = page.backgroundImageUrl;+ const fallbackImage = page.id ? images[page.id] : undefined;+ const img = hasTextLayer ? editableBackground : (editableBackground || fallbackImage);+if (img) {const dataUri = await urlToDataUri(img);slide.addImage({@@ -515,7 +938,30 @@ Rules:w: slideW,h: slideH,});- } else {+ }++ if (hasTextLayer) {+ for (const block of page.textBlocks!) {+ const style = block.style || {};+ const fontSize = estimateTextBlockFontSize(block, PPT_REFERENCE_SLIDE_WIDTH, PPT_REFERENCE_SLIDE_HEIGHT);+ slide.addText(block.text, {+ x: block.x * slideW,+ y: block.y * slideH,+ w: block.w * slideW,+ h: block.h * slideH,+ fontFace: style.fontFamily || "Aptos",+ fontSize,+ bold: Number(style.fontWeight || (block.role === "title" ? 900 : block.role === "tag" ? 800 : 500)) >= 600,+ italic: style.fontStyle === "italic",+ color: String(style.color || (block.role === "title" ? "FFFFFF" : block.role === "tag" ? "FFD66B" : "FFFFFF")).replace(/^#/, ""),+ align: style.align || "left",+ margin: 0,+ fit: "shrink",+ breakLine: false,+ valign: "middle",+ });+ }+ } else if (!img) {slide.addText(page.title || "", {x: 0.6,y: 0.6,@@ -537,21 +983,14 @@ Rules:const pageHeight = 540;for (const page of pages) {- const img = page.id ? images[page.id] : undefined;+ const img = page.backgroundImageUrl || (page.id ? images[page.id] : undefined);const pdfPage = pdfDoc.addPage([pageWidth, pageHeight]);- if (!img) continue;- const dataUri = await urlToDataUri(img);- const base64 = dataUri.split(",")[1] || "";+ const composedDataUri = await renderPageToDataUri(page, img, pageWidth * 2, pageHeight * 2);+ const base64 = composedDataUri.split(",")[1] || "";const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));-- if (dataUri.startsWith("data:image/jpeg") || dataUri.startsWith("data:image/jpg")) {- const embedded = await pdfDoc.embedJpg(bytes);- pdfPage.drawImage(embedded, { x: 0, y: 0, width: pageWidth, height: pageHeight });- } else {- const embedded = await pdfDoc.embedPng(bytes);- pdfPage.drawImage(embedded, { x: 0, y: 0, width: pageWidth, height: pageHeight });- }+ const embedded = await pdfDoc.embedPng(bytes);+ pdfPage.drawImage(embedded, { x: 0, y: 0, width: pageWidth, height: pageHeight });}const pdfBytes = await pdfDoc.save();diff --git a/src/lib/risen-prompt.ts b/src/lib/risen-prompt.tsnew file mode 100644index 0000000..2638525--- /dev/null+++ b/src/lib/risen-prompt.ts@@ -0,0 +1,29 @@+type RisenPromptSections = {+ role: string;+ instructions?: string;+ input?: string;+ steps?: string;+ endGoal?: string;+ narrowing?: string;+ outputFormat?: string;+};++const renderSection = (title: string, body?: string) => {+ const content = String(body || "").trim();+ if (!content) return "";+ return `# ${title}\n${content}`;+};++export function buildRisenPrompt(sections: RisenPromptSections) {+ return [+ renderSection("R - Role", sections.role),+ renderSection("I - Instructions", sections.instructions),+ renderSection("Input", sections.input),+ renderSection("S - Steps", sections.steps),+ renderSection("E - End Goal", sections.endGoal),+ renderSection("N - Narrowing", sections.narrowing),+ renderSection("Output Format", sections.outputFormat),+ ]+ .filter(Boolean)+ .join("\n\n");+}diff --git a/src/server/routes/chat.ts b/src/server/routes/chat.tsindex 55c4d42..60500f5 100644--- a/src/server/routes/chat.ts+++ b/src/server/routes/chat.ts@@ -167,7 +167,7 @@ async function summarizeChunk(providerOptions,headers,system:- "You are a strict summarizer. Output only direct summary text, no questions, no instructions.",+ "R - Role\nYou are a strict summarizer.\n\nI - Instructions\nOutput only direct summary text.\n\nE - End Goal\nProduce a concise faithful summary.\n\nN - Narrowing\nNo questions. No instructions. No extra framing.",user: `任务:仅根据下述文本输出摘要(<=300字)。禁止提问、禁止让用户补充内容、禁止输出模板化客套。\n\n文本:\n${chunk}`,maxOutputTokens: 600,maxChars: 300,@@ -278,7 +278,7 @@ async function summarizeBlockMethod(args: {providerOptions: args.providerOptions,headers: args.headers,system:- "You produce concise, faithful merged summaries. Output only summary text.",+ "R - Role\nYou merge chunk summaries.\n\nI - Instructions\nOutput only concise faithful summary text.\n\nE - End Goal\nProduce one merged summary.\n\nN - Narrowing\nNo questions. No instructions. No extra framing.",user: `任务:将以下分块摘要合并为完整摘要(<=1000字)。禁止提问、禁止请求补充材料,仅输出摘要正文。\n\n${merged}`,maxOutputTokens: 1800,maxChars: 1000,@@ -314,7 +314,7 @@ async function summarizeRecursiveMethod(args: {providerOptions: args.providerOptions,headers: args.headers,system:- "You merge summaries into a higher-level summary. Output only summary text.",+ "R - Role\nYou merge summaries into a higher-level summary.\n\nI - Instructions\nOutput only summary text.\n\nE - End Goal\nProduce one higher-level merged summary.\n\nN - Narrowing\nNo questions. No instructions. No extra framing.",user: `任务:把这组摘要提炼成更高层摘要(<=350字)。禁止提问、禁止让用户补充,只输出摘要。\n\n${group}`,maxOutputTokens: 800,maxChars: 350,@@ -329,7 +329,7 @@ async function summarizeRecursiveMethod(args: {providerOptions: args.providerOptions,headers: args.headers,system:- "You produce final executive summaries with hierarchical fidelity and no hallucination. Output only summary text.",+ "R - Role\nYou produce final executive summaries.\n\nI - Instructions\nKeep hierarchical fidelity and avoid hallucination. Output only summary text.\n\nE - End Goal\nProduce one final executive summary.\n\nN - Narrowing\nNo questions. No requests for more info. No extra framing.",user: `任务:输出最终摘要(<=1000字)。禁止提问、禁止请求更多信息,仅输出摘要正文。\n\n${level.join("\n")}`,maxOutputTokens: 1800,maxChars: 1000,@@ -568,13 +568,22 @@ async function generateDeepThinkingDiagramImage(args: {error,)promptText = [+ "R - Role","You are a final-quality flowchart image generation agent.",+ "I - Instructions","Generate one polished, production-ready, directly usable final diagram image.",- "Do not generate a sketch, wireframe, draft, poster, or UI mockup.",- "Prioritize clear structure, readable labels, complete coverage, balanced spacing, and unambiguous connectors.",+ "Input",`User request:\n${String(args.userText || "").trim() || "(empty)"}`,`Global constraints:\n${String(args.globalConstraints || "").trim() || "(none)"}`,`Processed file content:\n${String(args.processedFilesContext || "").trim() || "(none)"}`,+ "S - Steps",+ "1. Understand the requested diagram and any constraints.",+ "2. Build a coherent, readable final composition.",+ "3. Generate one polished final diagram image.",+ "E - End Goal",+ "Produce one refined diagram image suitable for downstream XML generation.",+ "N - Narrowing",+ "Do not generate a sketch, wireframe, draft, poster, or UI mockup. Prioritize clear structure, readable labels, complete coverage, balanced spacing, and unambiguous connectors.",].join("\n\n")}diff --git a/src/server/routes/ppt-ai.ts b/src/server/routes/ppt-ai.tsnew file mode 100644index 0000000..cacd7eb--- /dev/null+++ b/src/server/routes/ppt-ai.ts@@ -0,0 +1,234 @@+type ProxyAIConfig = {+ apiKey?: string+ baseUrl?: string+ chatModel?: string+ imageModel?: string+}++type ProxyChatMessage = {+ role: "system" | "user" | "assistant"+ content: any+}++type PptAIRequestBody =+ | {+ kind: "chat"+ aiConfig?: ProxyAIConfig+ messages?: ProxyChatMessage[]+ model?: string+ }+ | {+ kind: "image"+ aiConfig?: ProxyAIConfig+ prompt?: string+ referenceImageUrl?: string+ additionalReferenceImageUrls?: string[]+ model?: string+ }++const jsonHeaders = { "Content-Type": "application/json" }++function getErrorMessage(error: unknown) {+ return error instanceof Error ? error.message : "Unknown error"+}++function cleanUrl(url: string) {+ if (!url) return null+ if (url.startsWith("http")) return url+ if (url.startsWith("data:image")) return url+ return null+}++function extractTextFromContent(content: any): string {+ if (typeof content === "string") return content+ if (Array.isArray(content)) {+ return content+ .map((part) => {+ if (typeof part === "string") return part+ if (part?.type === "text" && typeof part?.text === "string") return part.text+ return ""+ })+ .filter(Boolean)+ .join("\n")+ }+ return ""+}++function extractImageUrlFromContent(messageContent: any) {+ if (Array.isArray(messageContent)) {+ const imagePart = messageContent.find((part: any) => part?.type === "image_url")+ if (imagePart?.image_url?.url) {+ return cleanUrl(String(imagePart.image_url.url))+ }+ return null+ }++ if (typeof messageContent === "string") {+ const markdownMatch = messageContent.match(/!\[.*?\]\((.*?)\)/)+ if (markdownMatch?.[1]) {+ return cleanUrl(markdownMatch[1])+ }+ return cleanUrl(messageContent.trim())+ }++ return null+}++async function convertRemoteImageToDataUrl(url: string): Promise<string> {+ if (url.startsWith("data:image")) return url+ const response = await fetch(url)+ if (!response.ok) {+ throw new Error(`Failed to fetch remote image with status ${response.status}`)+ }+ const contentType = response.headers.get("content-type") || "image/png"+ const buffer = Buffer.from(await response.arrayBuffer())+ return `data:${contentType};base64,${buffer.toString("base64")}`+}++async function proxyChatCompletion(args: {+ apiKey: string+ baseUrl: string+ model: string+ messages: ProxyChatMessage[]+}) {+ const response = await fetch(`${args.baseUrl}/chat/completions`, {+ method: "POST",+ headers: {+ Authorization: `Bearer ${args.apiKey}`,+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ model: args.model,+ messages: args.messages,+ stream: false,+ }),+ })++ if (!response.ok) {+ const text = await response.text()+ throw new Error(`Upstream chat request failed with status ${response.status}: ${text}`)+ }++ const result = await response.json()+ const content = result?.choices?.[0]?.message?.content+ return extractTextFromContent(content)+}++async function proxyImageCompletion(args: {+ apiKey: string+ baseUrl: string+ model: string+ prompt: string+ referenceImageUrl?: string+ additionalReferenceImageUrls?: string[]+}) {+ const content: any[] = [{ type: "text", text: args.prompt }]+ if (args.referenceImageUrl) {+ content.push({+ type: "image_url",+ image_url: { url: args.referenceImageUrl },+ })+ }+ for (const url of args.additionalReferenceImageUrls || []) {+ if (!url) continue+ content.push({+ type: "image_url",+ image_url: { url },+ })+ }++ const response = await fetch(`${args.baseUrl}/chat/completions`, {+ method: "POST",+ headers: {+ Authorization: `Bearer ${args.apiKey}`,+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ model: args.model,+ messages: [{ role: "user", content: content.length > 1 ? content : args.prompt }],+ stream: false,+ }),+ })++ if (!response.ok) {+ const text = await response.text()+ throw new Error(`Upstream image request failed with status ${response.status}: ${text}`)+ }++ const result = await response.json()+ const messageContent = result?.choices?.[0]?.message?.content+ const url = extractImageUrlFromContent(messageContent)+ if (!url) {+ throw new Error("Upstream image request succeeded but returned no image URL")+ }+ return await convertRemoteImageToDataUrl(url)+}++export async function POST(req: Request) {+ try {+ const body = (await req.json()) as PptAIRequestBody+ const aiConfig = body?.aiConfig || {}+ const apiKey = String(aiConfig.apiKey || "").trim()+ const baseUrl = String(aiConfig.baseUrl || "").trim().replace(/\/+$/, "")++ if (!apiKey || !baseUrl) {+ return Response.json(+ { error: "Missing AI API key or base URL for PPT proxy request." },+ { status: 400, headers: jsonHeaders },+ )+ }++ if (body.kind === "chat") {+ const model = String(body.model || aiConfig.chatModel || "").trim()+ const messages = Array.isArray(body.messages) ? body.messages : []+ if (!model || messages.length === 0) {+ return Response.json(+ { error: "Missing chat model or messages." },+ { status: 400, headers: jsonHeaders },+ )+ }++ const content = await proxyChatCompletion({+ apiKey,+ baseUrl,+ model,+ messages,+ })+ return Response.json({ content }, { headers: jsonHeaders })+ }++ if (body.kind === "image") {+ const model = String(body.model || aiConfig.imageModel || "").trim()+ const prompt = String(body.prompt || "").trim()+ if (!model || !prompt) {+ return Response.json(+ { error: "Missing image model or prompt." },+ { status: 400, headers: jsonHeaders },+ )+ }++ const url = await proxyImageCompletion({+ apiKey,+ baseUrl,+ model,+ prompt,+ referenceImageUrl:+ typeof body.referenceImageUrl === "string" ? body.referenceImageUrl : undefined,+ additionalReferenceImageUrls: Array.isArray(body.additionalReferenceImageUrls)+ ? body.additionalReferenceImageUrls.map((x) => String(x || "")).filter(Boolean)+ : [],+ })+ return Response.json({ url }, { headers: jsonHeaders })+ }++ return Response.json(+ { error: "Unsupported PPT AI request kind." },+ { status: 400, headers: jsonHeaders },+ )+ } catch (error) {+ return Response.json(+ { error: getErrorMessage(error) },+ { status: 500, headers: jsonHeaders },+ )+ }+}diff --git a/src/workspaces/flow/next/lib/system-prompts.ts b/src/workspaces/flow/next/lib/system-prompts.tsindex a26eaeb..54c68c8 100644--- a/src/workspaces/flow/next/lib/system-prompts.ts+++ b/src/workspaces/flow/next/lib/system-prompts.ts@@ -8,13 +8,30 @@// Default system prompt (~1900 tokens) - works with all modelsexport const DEFAULT_SYSTEM_PROMPT = `+# R - RoleYou are an expert diagram creation assistant specializing in draw.io XML generation.-Your primary function is chat with user and crafting clear, well-organized visual diagrams through precise XML specifications.++# I - Instructions+Your primary function is chatting with the user and crafting clear, well-organized visual diagrams through precise XML specifications.You can see images that users upload, and you can read the text content extracted from PDF documents they upload.You may also receive an optional "Reference image" generated by an upstream deep-thinking stage. When present, use it as a visual guide for structure, composition, grouping, and layout. When absent, ignore this input completely.+When you are asked to create a diagram, briefly describe your plan about the layout and structure to avoid object overlap or edge crossings (2-3 sentences max), then use display_diagram to generate the XML.+After generating or editing a diagram, you do not need to say anything else. The user can already see the diagram.++# Input+You may receive user requests, uploaded images, extracted PDF text, optional reference images, and optional current diagram XML.++# S - Steps+1. Understand the requested diagram and the available context.+2. Decide whether to create a new diagram or edit the existing one.+3. Plan a readable layout before generating XML.+4. Use the appropriate tool to create or modify the diagram.++# E - End Goal+Produce a clean, valid, professional diagram that communicates the requested information clearly.-When you are asked to create a diagram, briefly describe your plan about the layout and structure to avoid object overlapping or edge cross the objects. (2-3 sentences max), then use display_diagram tool to generate the XML.-After generating or editing a diagram, you don't need to say anything. The user can see the diagram - no need to describe it.+# N - Narrowing+Follow the tool rules, XML validity rules, layout limits, and editing constraints below without adding unnecessary chat output.## App ContextYou are an AI agent (powered by {{MODEL_NAME}}) inside a web app. The interface has:@@ -252,9 +269,9 @@ When creating edges/connectors, you MUST follow these rules to avoid overlapping- If two edges connect the same pair of nodes, they MUST exit/enter at DIFFERENT positions- Use exitY=0.3 for first edge, exitY=0.7 for second edge (NOT both 0.5)-**Rule 2: For bidirectional connections (A鈫擝), use OPPOSITE sides**-- A鈫払: exit from RIGHT side of A (exitX=1), enter LEFT side of B (entryX=0)-- B鈫扐: exit from LEFT side of B (exitX=0), enter RIGHT side of A (entryX=1)+**Rule 2: For bidirectional connections (A↔B), use OPPOSITE sides**+- A→B: exit from RIGHT side of A (exitX=1), enter LEFT side of B (entryX=0)+- B→A: exit from LEFT side of B (exitX=0), enter RIGHT side of A (entryX=1)**Rule 3: Always specify exitX, exitY, entryX, entryY explicitly**- Every edge MUST have these 4 attributes set in the style@@ -285,13 +302,13 @@ When creating edges/connectors, you MUST follow these rules to avoid overlapping- For TOP-TO-BOTTOM flow: exit from bottom (exitY=1), enter from top (entryY=0)- For LEFT-TO-RIGHT flow: exit from right (exitX=1), enter from left (entryX=0)- For DIAGONAL connections: use the side closest to the target, not corners-- Example: Node below-right of source 鈫?exit from bottom (exitY=1) OR right (exitX=1), not corner+- Example: Node below-right of source → exit from bottom (exitY=1) OR right (exitX=1), not corner**Before generating XML, mentally verify:**-1. "Do any edges cross over shapes that aren't their source/target?" 鈫?If yes, add waypoints-2. "Do any two edges share the same path?" 鈫?If yes, adjust exit/entry points-3. "Are any connection points at corners (both X and Y are 0 or 1)?" 鈫?If yes, use edge centers instead-4. "Could I rearrange shapes to reduce edge crossings?" 鈫?If yes, revise layout+1. "Do any edges cross over shapes that aren't their source/target?" → If yes, add waypoints+2. "Do any two edges share the same path?" → If yes, adjust exit/entry points+3. "Are any connection points at corners (both X and Y are 0 or 1)?" → If yes, use edge centers instead+4. "Could I rearrange shapes to reduce edge crossings?" → If yes, revise layout## Edge Examples@@ -317,7 +334,7 @@ When creating edges/connectors, you MUST follow these rules to avoid overlapping\`\`\`### Edge with waypoints (routing AROUND obstacles) - CRITICAL PATTERN:-**Scenario:** Hotfix(right,bottom) 鈫?Main(center,top), but Develop(center,middle) is in between.+**Scenario:** Hotfix(right,bottom) → Main(center,top), but Develop(center,middle) is in between.**WRONG:** Direct diagonal line crosses over Develop**CORRECT:** Route around the OUTSIDE (go right first, then up)\`\`\`xmldiff --git a/src/workspaces/ppt/chat/ChatPanel.tsx b/src/workspaces/ppt/chat/ChatPanel.tsxindex 768e980..b0edbdf 100644--- a/src/workspaces/ppt/chat/ChatPanel.tsx+++ b/src/workspaces/ppt/chat/ChatPanel.tsx@@ -1,7 +1,7 @@import React, { useState, useRef, useEffect } from 'react';import { PanelRightClose, PanelRightOpen } from 'lucide-react';import { cn } from '@/lib/utils';-import { streamChatMessage, generateChatMessage, ChatMessage, getAIConfig } from '@/lib/ai-client';+import { streamChatMessage, generateChatMessage, generatePptProxyChatMessage, ChatMessage, getAIConfig } from '@/lib/ai-client';import { DRAWIO_SYSTEM_PROMPT } from '@/lib/system-prompts';import { ButtonWithTooltip } from '@/workspaces/ppt/chat/components/button-with-tooltip';import { ChatInput } from '@/workspaces/ppt/chat/ChatInput';@@ -64,6 +64,8 @@ interface ChatPanelProps {const STORAGE_KEY_PREFIX = 'chat_history_v2_';const PPT_WORKSPACE_STORAGE_KEY = "CanvasAnvil-ppt-state-v1";+const STORAGE_MESSAGE_LIMIT = 40;+const STORAGE_CONTENT_LIMIT = 4000;// Convert internal ChatMessage to UIMessageconst toUIMessage = (msg: ChatMessage, index: number): UIMessage => ({@@ -73,6 +75,53 @@ const toUIMessage = (msg: ChatMessage, index: number): UIMessage => ({parts: [{ type: 'text', text: msg.content }]});+const compactMessageContent = (value: unknown) => {+ const text = String(value || "");+ const withoutImageTags = text.replace(/\[\[IMAGE\|([^|\]]*)\|data:image\/[\s\S]*?\]\]/gi, "[[IMAGE|$1|[image-data]]]");+ const withoutDataUrls = withoutImageTags.replace(/data:image\/[^)\s]+/gi, "[image-data]");+ if (withoutDataUrls.length <= STORAGE_CONTENT_LIMIT) return withoutDataUrls;+ return `${withoutDataUrls.slice(0, STORAGE_CONTENT_LIMIT)}\n...[truncated]`;+};++const getMessagesForStorage = (messages: ChatMessage[]) =>+ messages.slice(-STORAGE_MESSAGE_LIMIT).map((message) => ({+ role: message.role,+ content: compactMessageContent(message.content),+ }));++const getChatErrorText = (+ error: unknown,+ trText: (zhText: string, enText: string) => string+) => {+ const raw = String((error as any)?.message || error || "");++ if (/input token count exceeds|maximum number of tokens allowed|too many tokens|context length/i.test(raw)) {+ return trText(+ "本次请求内容过长,通常是图片或附件内容过大。请重试;如果仍失败,请减少附件数量或缩小单个附件内容。",+ "This request is too large, usually because an image or attachment expanded the input too much. Retry once; if it still fails, reduce the number or size of attachments."+ );+ }++ if (/api key|invalid api key|incorrect api key|unauthorized|401/i.test(raw)) {+ return trText(+ "API 配置无效,请检查 API key 或服务配置。",+ "The API configuration is invalid. Check the API key or provider settings."+ );+ }++ if (/400|bad request/i.test(raw)) {+ return trText(+ "请求格式无效,请检查本次输入或附件内容。",+ "The request payload is invalid. Check this input or its attachments."+ );+ }++ return trText(+ "抱歉,请求失败,请稍后重试。",+ "Sorry, the request failed. Please try again."+ );+};+export function ChatPanel({className,attachments = [],@@ -91,7 +140,7 @@ export function ChatPanel({collapsed = false,onToggleCollapse,collapseLocked = false,- title = "AI 助手",+ title,inputPlaceholder,history = [],onRestore,@@ -103,6 +152,7 @@ export function ChatPanel({}: ChatPanelProps) {const uiLang = useUiLanguage();const trText = (zhText: string, enText: string) => (uiLang === "zh" ? zhText : enText);+ const resolvedTitle = title || t(uiLang, "workspace.default.title");// Persistence keyconst storageKey = `${STORAGE_KEY_PREFIX}${workspaceId}`;@@ -326,7 +376,15 @@ export function ChatPanel({// Persist messagesuseEffect(() => {- localStorage.setItem(storageKey, JSON.stringify(messages));+ try {+ localStorage.setItem(storageKey, JSON.stringify(getMessagesForStorage(messages)));+ } catch (error) {+ console.warn("Failed to persist chat history", error);+ try {+ localStorage.removeItem(storageKey);+ } catch {+ }+ }onMessagesChange?.(messages);}, [messages, storageKey, onMessagesChange]);@@ -562,13 +620,14 @@ export function ChatPanel({if (file.type.startsWith('image/')) {try {const dataUrl = await fileToDataUrl(file);- // We embed the image directly in the prompt using Markdown syntax.- // This allows the Agent (if it's a VLM) to see it, or at least we provide the Data URL string- // which the Agent can echo back in the JSON for the frontend to use.- fileTexts.push(``);- fileTexts.push(`[Image Attachment: ${file.name}]`);currentUploadedImages.push(dataUrl);currentUploadedImageItems.push({ name: file.name, url: dataUrl });+ const imageIndex = currentUploadedImageItems.length;+ fileTexts.push(+ workspaceId === "ppt"+ ? `[Uploaded Image ${imageIndex}: ${file.name}]\nThis image is available as an optional material/reference for this turn. Do not use it or route to image editing unless the user explicitly asks to place, reference, replace, or visually use this uploaded image.`+ : `[Image Attachment ${imageIndex}: ${file.name}]`+ );} catch (e) {console.error("Failed to read image", file.name, e);fileTexts.push(`[Image: ${file.name}] (Failed to read)`);@@ -867,20 +926,31 @@ export function ChatPanel({const systemContent = [systemPrompt, globalSystemPrompt, globalConstraints].filter(Boolean).join('\n\n');- const apiMessages: ChatMessage[] = [- { role: 'system', content: systemContent },- ...messages,- { role: 'user', content: promptContent }- ];+ const apiMessages: ChatMessage[] =+ workspaceId === "ppt"+ ? [+ { role: 'system', content: systemContent },+ { role: 'user', content: promptContent }+ ]+ : [+ { role: 'system', content: systemContent },+ ...messages,+ { role: 'user', content: promptContent }+ ];setMessages(prev => [...prev, { role: 'assistant', content: '' }]);let fullResponse = '';const updater = createThrottledAssistantUpdater();- await streamChatMessage(apiMessages, (chunk) => {- fullResponse = chunk;- updater.push(chunk);- }, chatModel, controller.signal);+ if (workspaceId === "ppt") {+ fullResponse = await generatePptProxyChatMessage(apiMessages, chatModel, { signal: controller.signal });+ updater.push(fullResponse);+ } else {+ await streamChatMessage(apiMessages, (chunk) => {+ fullResponse = chunk;+ updater.push(chunk);+ }, chatModel, controller.signal);+ }updater.flush();let flowRoutedBaseMessages: ChatMessage[] | null = null;@@ -1318,12 +1388,9 @@ export function ChatPanel({});return;}+ const errorText = getChatErrorText(error, trText);setMessages(prev => {const last = prev[prev.length - 1];- const errorText = trText(- "Sorry, an error occurred. Please check API key settings.",- "Sorry, an error occurred. Please check API key settings."- );if (last.role === 'assistant' && !last.content) {return [...prev.slice(0, -1), { role: 'assistant', content: errorText }];}@@ -1353,11 +1420,17 @@ export function ChatPanel({: "";const promptContent = [lastUserText, flowContextText].filter(Boolean).join("\n\n");- const apiMessages: ChatMessage[] = [- { role: 'system', content: systemContent },- ...baseMessages.slice(0, -1),- { role: "user", content: promptContent }- ];+ const apiMessages: ChatMessage[] =+ workspaceId === "ppt"+ ? [+ { role: 'system', content: systemContent },+ { role: "user", content: promptContent }+ ]+ : [+ { role: 'system', content: systemContent },+ ...baseMessages.slice(0, -1),+ { role: "user", content: promptContent }+ ];setIsLoading(true);abortControllerRef.current?.abort();@@ -1369,10 +1442,15 @@ export function ChatPanel({let fullResponse = '';const updater = createThrottledAssistantUpdater();try {- await streamChatMessage(apiMessages, (chunk) => {- fullResponse = chunk;- updater.push(chunk);- }, chatModel, controller.signal);+ if (workspaceId === "ppt") {+ fullResponse = await generatePptProxyChatMessage(apiMessages, chatModel, { signal: controller.signal });+ updater.push(fullResponse);+ } else {+ await streamChatMessage(apiMessages, (chunk) => {+ fullResponse = chunk;+ updater.push(chunk);+ }, chatModel, controller.signal);+ }updater.flush();if (fullResponse) {@@ -1489,13 +1567,14 @@ export function ChatPanel({});return;}+ const errorText = getChatErrorText(error, trText);setMessages(prev => {const last = prev[prev.length - 1];if (last?.role === 'assistant' && !last.content) {- return [...prev.slice(0, -1), { role: 'assistant', content: "Sorry, an error occurred. Please check API key settings." }];+ return [...prev.slice(0, -1), { role: 'assistant', content: errorText }];}- return [...prev, { role: 'assistant', content: "Sorry, an error occurred. Please check API key settings." }];+ return [...prev, { role: 'assistant', content: errorText }];});} finally {setIsLoading(false);@@ -1571,7 +1650,7 @@ export function ChatPanel({<div className="px-5 py-4 border-b border-border/50"><div className="flex items-center justify-between"><div className="flex items-center gap-2">- <span className="text-base font-semibold tracking-tight whitespace-nowrap">{title}</span>+ <span className="text-base font-semibold tracking-tight whitespace-nowrap">{resolvedTitle}</span></div><div className="flex items-center gap-1">{onToggleCollapse && (diff --git a/src/workspaces/ppt/workspace/PptWorkspace.tsx b/src/workspaces/ppt/workspace/PptWorkspace.tsxindex 7558e16..5ac366f 100644--- a/src/workspaces/ppt/workspace/PptWorkspace.tsx+++ b/src/workspaces/ppt/workspace/PptWorkspace.tsx@@ -1,7 +1,7 @@-import React, { useState, useEffect, useRef } from 'react';+import React, { useState, useEffect, useLayoutEffect, useRef } from 'react';import { motion } from "framer-motion";import { generateImage, generateChatMessage } from '@/lib/ai-client';-import { pptService, PptPage } from '@/lib/ppt-service';+import { pptService, PptPage, type PptTextBlock, type SlideEditRoutingItem, estimateTextBlockFontSize, PPT_REFERENCE_SLIDE_HEIGHT, PPT_REFERENCE_SLIDE_WIDTH } from '@/lib/ppt-service';import { getTemplateGenerationPrompt } from '@/lib/ppt-prompts';import { Loader2, Plus, Image as ImageIcon, MessageSquarePlus, Upload, Presentation, Sparkles, Check, Play, FileText, Download, Lightbulb, X, ArrowLeft, ArrowRight, Eye, Trash2, Maximize2, Minimize2 } from 'lucide-react';import {@@ -25,6 +25,96 @@ interface SlideData {description?: string; // Add description support}+type SlideRenderLayer = {+ backgroundImageUrl: string;+ textBlocks: PptTextBlock[];+ status: "pending" | "ready" | "failed";+ error?: string;+};++type SlideImageVersionType = "generated" | "edited" | "derived_textless";++type SlideImageVersion = {+ id: string;+ url: string;+ timestamp: number;+ type: SlideImageVersionType;+ instruction?: string;+ sourceVersionId?: string;+};++const hasRenderableTextBlocks = (layer?: SlideRenderLayer) =>+ Array.isArray(layer?.textBlocks) && layer.textBlocks.length > 0;++const migrateLegacyTextlessVersions = (rawState: any) => {+ if (!rawState || typeof rawState !== "object") return rawState;++ const imageVersions =+ rawState.imageVersions && typeof rawState.imageVersions === "object" && !Array.isArray(rawState.imageVersions)+ ? { ...rawState.imageVersions }+ : {};+ const renderLayers =+ rawState.renderLayers && typeof rawState.renderLayers === "object" && !Array.isArray(rawState.renderLayers)+ ? { ...rawState.renderLayers }+ : {};+ const currentImageVersionId =+ rawState.currentImageVersionId &&+ typeof rawState.currentImageVersionId === "object" &&+ !Array.isArray(rawState.currentImageVersionId)+ ? { ...rawState.currentImageVersionId }+ : {};++ for (const [slideId, rawVersions] of Object.entries(imageVersions)) {+ if (!Array.isArray(rawVersions)) continue;+ const versions = rawVersions as SlideImageVersion[];+ const layerMap =+ renderLayers[slideId] && typeof renderLayers[slideId] === "object" && !Array.isArray(renderLayers[slideId])+ ? { ...renderLayers[slideId] }+ : {};+ let changed = false;++ for (const version of versions) {+ if (version?.type !== "derived_textless" || !version.sourceVersionId) continue;+ const sourceLayer = layerMap[version.sourceVersionId];+ const derivedLayer = layerMap[version.id];+ if (!hasRenderableTextBlocks(sourceLayer) && hasRenderableTextBlocks(derivedLayer)) {+ layerMap[version.sourceVersionId] = derivedLayer;+ changed = true;+ }+ if (currentImageVersionId[slideId] === version.id) {+ currentImageVersionId[slideId] = version.sourceVersionId;+ }+ }++ const filtered = versions.filter((version) => version?.type !== "derived_textless");+ if (filtered.length !== versions.length) {+ imageVersions[slideId] = filtered;+ changed = true;+ }++ if (changed) {+ renderLayers[slideId] = layerMap;+ }+ }++ return {+ ...rawState,+ imageVersions,+ renderLayers,+ currentImageVersionId,+ };+};++const stripLeadingBullet = (value: string) =>+ value.replace(/^[•●▪◦·]\s*/, "").trim();++const textToLines = (value: string) =>+ value+ .replace(/\r\n/g, "\n")+ .split("\n")+ .map((item) => item.trim())+ .filter(Boolean);+interface PptData {theme?: string;slides: SlideData[];@@ -97,6 +187,252 @@ interface PptWorkspaceProps {onResetWorkspace?: () => void;}+type TextBlockOverlayProps = {+ block: PptTextBlock;+ slideId: string;+ editable: boolean;+ uiLang: "zh" | "en";+ canvasWidth: number;+ canvasHeight: number;+ isDragging: boolean;+ isEditing: boolean;+ onFocusBlock: (blockId: string) => void;+ onBlurBlock: (blockId: string, nextText: string) => void;+ onDragStart: (event: React.PointerEvent<HTMLButtonElement>, block: PptTextBlock, slideId: string) => void;+ tr: (zh: string, en: string) => string;+};++const PPT_POINT_TO_CSS_PX = 96 / 72;++function TextBlockOverlay({+ block,+ slideId,+ editable,+ uiLang,+ canvasWidth,+ canvasHeight,+ isDragging,+ isEditing,+ onFocusBlock,+ onBlurBlock,+ onDragStart,+ tr,+}: TextBlockOverlayProps) {+ const style = block.style || {};+ const rawFontSize = Number(style.fontSize || 0);+ const isSingleLine = !String(block.text || "").includes("\n");+ const isTitle = block.role === "title";+ const isTag =+ block.role === "tag" ||+ (block.role === "summary" && block.text.trim().length <= 12 && block.w <= 0.2 && block.h <= 0.09);+ const isPanelHeading =+ !isTitle &&+ !isTag &&+ isSingleLine &&+ block.text.trim().length <= 22 &&+ block.h <= 0.13 &&+ (rawFontSize >= 16 || Number(style.fontWeight || 0) >= 620 || block.role === "bullet");+ const scale = Math.max(0.45, Math.min(1.8, Math.min(canvasWidth / 1100, canvasHeight / 619)));+ const basePaddingX = Math.max(2, Math.round((isTitle ? 12 : isTag ? 9 : 10) * scale));+ const basePaddingY = Math.max(1, Math.round((isTitle ? 8 : isTag ? 4 : 6) * scale));+ const dragHandleSize = Math.max(14, Math.round(20 * scale));+ const dragHandleOffset = Math.max(2, Math.round(4 * scale));+ const paddingLeft = basePaddingX;+ const paddingRight = basePaddingX;+ const paddingTop = basePaddingY;+ const paddingBottom = basePaddingY;+ const lineHeightRatio = Number(style.lineHeight || (isTitle ? 1.12 : isTag ? 1.04 : 1.35));+ const slideScale = Math.min(canvasWidth / PPT_REFERENCE_SLIDE_WIDTH, canvasHeight / PPT_REFERENCE_SLIDE_HEIGHT);+ const referenceFontSize = Math.max(+ 10,+ estimateTextBlockFontSize(block, PPT_REFERENCE_SLIDE_WIDTH, PPT_REFERENCE_SLIDE_HEIGHT)+ );+ const estimatedFontSize = Math.max(10, referenceFontSize * slideScale);+ const [resolvedFontSize, setResolvedFontSize] = useState(estimatedFontSize);+ const outerRef = useRef<HTMLDivElement | null>(null);+ const textRef = useRef<HTMLDivElement | null>(null);+ const baseColor = style.color || (isTitle ? "#ffffff" : isTag || isPanelHeading ? "#ffd66b" : "#ffffff");+ const gradientFrom = style.gradientFrom || (isTitle ? "#ffffff" : undefined);+ const gradientTo = style.gradientTo || (isTitle ? "#22d3ee" : undefined);+ const hasGradient = Boolean(gradientFrom && gradientTo && isTitle);+ const relaxedVerticalFit = isTitle || isPanelHeading || isTag;+ const outerBackground = isTag+ ? "linear-gradient(180deg, rgba(26,89,160,0.55) 0%, rgba(32,159,230,0.24) 100%)"+ : "transparent";+ const outerBorder = isTag ? "1px solid rgba(116,217,255,0.55)" : "none";+ const outerRadius = isTag ? `${Math.max(12, Math.round(18 * scale))}px` : "0px";+ const outerShadow = isTag ? "0 0 0 1px rgba(255,255,255,0.08) inset, 0 8px 18px rgba(15,23,42,0.18)" : "none";+ const effectiveFontWeight = Number(+ style.fontWeight || (isTitle ? 900 : isTag || isPanelHeading ? 800 : 500)+ );+ const effectiveFontFamily = style.fontFamily || (uiLang === "zh" ? "Microsoft YaHei" : "Aptos");+ const effectiveTextShadow = isTitle+ ? "0 2px 10px rgba(15,23,42,0.55), 0 0 16px rgba(34,211,238,0.22)"+ : isTag || isPanelHeading+ ? "0 1px 6px rgba(15,23,42,0.45), 0 0 10px rgba(250,204,21,0.18)"+ : "0 1px 4px rgba(15,23,42,0.18)";+ const effectiveStroke = style.strokeColor+ ? `${Number(style.strokeWidth || Math.max(0.6, 1.1 * scale))}px ${style.strokeColor}`+ : isTitle+ ? `${Math.max(0.7, 1.25 * scale)}px rgba(8,15,36,0.42)`+ : isPanelHeading+ ? `${Math.max(0.45, 0.8 * scale)}px rgba(8,15,36,0.28)`+ : "0px transparent";++ useEffect(() => {+ setResolvedFontSize(estimatedFontSize);+ }, [estimatedFontSize, block.id, block.text]);++ useEffect(() => {+ if (!editable || !isEditing) return;+ const el = textRef.current;+ if (!el) return;+ el.focus();+ const selection = window.getSelection();+ if (!selection) return;+ const range = document.createRange();+ range.selectNodeContents(el);+ range.collapse(false);+ selection.removeAllRanges();+ selection.addRange(range);+ }, [editable, isEditing]);++ useLayoutEffect(() => {+ const outer = outerRef.current;+ const text = textRef.current;+ if (!outer || !text) return;++ const minSize = Math.max(5, Math.round(estimatedFontSize * 0.72), Math.round(6 * scale));+ const availableWidth = Math.max(8, outer.clientWidth - paddingLeft - paddingRight);+ const targetWidth = availableWidth * (isTitle ? 0.99 : isTag ? 0.97 : 0.985);++ const apply = (fontSize: number) => {+ text.style.fontSize = `${fontSize}px`;+ text.style.lineHeight = String(lineHeightRatio);+ text.style.wordBreak = "break-word";+ text.style.overflowWrap = "anywhere";+ };++ let size = estimatedFontSize;+ apply(size);+ for (let i = 0; i < 24 && text.scrollWidth > targetWidth + 1 && size > minSize; i += 1) {+ size = Math.max(minSize, size - 0.5);+ apply(size);+ }+ apply(size);++ if (Math.abs(size - resolvedFontSize) > 0.25) {+ setResolvedFontSize(size);+ }+ }, [+ block.id,+ block.text,+ block.x,+ block.y,+ block.w,+ block.h,+ canvasWidth,+ canvasHeight,+ estimatedFontSize,+ paddingLeft,+ paddingRight,+ paddingTop,+ paddingBottom,+ lineHeightRatio,+ resolvedFontSize,+ scale,+ block.text,+ isTitle,+ isTag,+ isPanelHeading,+ relaxedVerticalFit,+ ]);++ return (+ <div+ ref={outerRef}+ className={relaxedVerticalFit ? "absolute overflow-visible" : "absolute overflow-hidden"}+ style={{+ left: `${block.x * 100}%`,+ top: `${block.y * 100}%`,+ width: `${block.w * 100}%`,+ height: `${block.h * 100}%`,+ padding: `${paddingTop}px ${paddingRight}px ${paddingBottom}px ${paddingLeft}px`,+ boxSizing: "border-box",+ pointerEvents: editable ? "auto" : "none",+ color: baseColor,+ fontFamily: effectiveFontFamily,+ fontWeight: effectiveFontWeight,+ fontStyle: style.fontStyle || "normal",+ letterSpacing: `${style.letterSpacing || 0}px`,+ textAlign: style.align || "left",+ textShadow: effectiveTextShadow,+ background: outerBackground,+ border: outerBorder,+ borderRadius: outerRadius,+ boxShadow: outerShadow,+ cursor: editable ? "text" : "default",+ outline: editable && isDragging ? "2px solid rgba(59,130,246,0.45)" : "none",+ }}+ >+ {editable ? (+ <button+ type="button"+ className="absolute z-20 rounded bg-black/55 text-white leading-none shadow-sm cursor-grab active:cursor-grabbing"+ title={tr("拖动文本框", "Drag text block")}+ style={{+ right: `${-Math.round(dragHandleSize * 0.45)}px`,+ top: `${-Math.round(dragHandleSize * 0.3)}px`,+ width: `${dragHandleSize}px`,+ height: `${dragHandleSize}px`,+ fontSize: `${Math.max(8, Math.round(10 * scale))}px`,+ }}+ onPointerDown={(event) => onDragStart(event, block, slideId)}+ >+ ++ </button>+ ) : null}+ <div+ ref={textRef}+ contentEditable={editable && isEditing}+ suppressContentEditableWarning+ spellCheck={false}+ className={relaxedVerticalFit+ ? "h-full w-full whitespace-pre-wrap break-words bg-transparent outline-none overflow-visible"+ : "h-full w-full whitespace-pre-wrap break-words bg-transparent outline-none overflow-hidden"}+ onClick={() => {+ if (editable && !isEditing) onFocusBlock(block.id);+ }}+ onFocus={() => {+ if (editable && isEditing) onFocusBlock(block.id);+ }}+ onBlur={(event) => {+ if (!editable || !isEditing) return;+ const nextText = textToLines(event.currentTarget.textContent || "").join("\n");+ onBlurBlock(block.id, nextText);+ }}+ style={{+ fontSize: `${resolvedFontSize}px`,+ lineHeight: String(lineHeightRatio),+ wordBreak: "break-word",+ overflowWrap: "anywhere",+ color: hasGradient ? "transparent" : baseColor,+ fontWeight: effectiveFontWeight,+ textShadow: effectiveTextShadow,+ backgroundImage: hasGradient ? `linear-gradient(90deg, ${gradientFrom}, ${gradientTo})` : undefined,+ backgroundClip: hasGradient ? "text" : undefined,+ WebkitBackgroundClip: hasGradient ? "text" : undefined,+ WebkitTextFillColor: hasGradient ? "transparent" : undefined,+ WebkitTextStroke: effectiveStroke,+ outline: editable && isEditing ? "2px solid rgba(59,130,246,0.35)" : "none",+ }}+ >+ {block.text}+ </div>+ </div>+ );+}+type CreationStep = 'idle' | 'input' | 'outline' | 'generating_content' | 'generating_images' | 'done';type CreationMode = 'idea' | 'outline' | 'beautify';type ReferenceFile = { id: string; filename: string; content: string; charCount: number };@@ -153,7 +489,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChconst raw = localStorage.getItem(PPT_WORKSPACE_STORAGE_KEY);const parsed = raw ? JSON.parse(raw) : null;if (!parsed || typeof parsed !== "object") return null;- return parsed as any;+ return migrateLegacyTextlessVersions(parsed as any);} catch {return null;}@@ -219,20 +555,21 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh}return out;});- const [imageVersions, setImageVersions] = useState<Record<string, Array<{ id: string; url: string; timestamp: number; type: 'generated' | 'edited'; instruction?: string }>>>(() => {+ const [imageVersions, setImageVersions] = useState<Record<string, SlideImageVersion[]>>(() => {const v = initialPptState?.imageVersions;if (!v || typeof v !== "object" || Array.isArray(v)) return {};- const out: Record<string, Array<{ id: string; url: string; timestamp: number; type: 'generated' | 'edited'; instruction?: string }>> = {};+ const out: Record<string, SlideImageVersion[]> = {};for (const [k, val] of Object.entries(v)) {if (typeof k !== "string" || !Array.isArray(val)) continue;out[k] = val- .filter((x: any) => x && typeof x.id === "string" && typeof x.url === "string" && typeof x.timestamp === "number" && (x.type === "generated" || x.type === "edited"))+ .filter((x: any) => x && typeof x.id === "string" && typeof x.url === "string" && typeof x.timestamp === "number" && (x.type === "generated" || x.type === "edited" || x.type === "derived_textless")).map((x: any) => ({id: x.id,url: x.url,timestamp: x.timestamp,type: x.type,instruction: typeof x.instruction === "string" ? x.instruction : undefined,+ sourceVersionId: typeof x.sourceVersionId === "string" ? x.sourceVersionId : undefined,}));}return out;@@ -246,6 +583,32 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh}return out;});+ const [renderLayers, setRenderLayers] = useState<Record<string, Record<string, SlideRenderLayer>>>(() => {+ const v = initialPptState?.renderLayers;+ if (!v || typeof v !== "object" || Array.isArray(v)) return {};+ const out: Record<string, Record<string, SlideRenderLayer>> = {};+ for (const [slideId, versions] of Object.entries(v)) {+ if (typeof slideId !== "string" || !versions || typeof versions !== "object" || Array.isArray(versions)) continue;+ const nextVersions: Record<string, SlideRenderLayer> = {};+ for (const [versionId, layer] of Object.entries(versions)) {+ if (typeof versionId !== "string" || !layer || typeof layer !== "object" || Array.isArray(layer)) continue;+ const backgroundImageUrl = typeof (layer as any).backgroundImageUrl === "string" ? (layer as any).backgroundImageUrl : "";+ const textBlocks = Array.isArray((layer as any).textBlocks) ? (layer as any).textBlocks : [];+ const status = (layer as any).status === "pending" || (layer as any).status === "failed" ? (layer as any).status : "ready";+ nextVersions[versionId] = {+ backgroundImageUrl,+ textBlocks,+ status,+ error: typeof (layer as any).error === "string" ? (layer as any).error : undefined,+ };+ }+ out[slideId] = nextVersions;+ }+ return out;+ });+ const [editingTextBlockId, setEditingTextBlockId] = useState<string | null>(null);+ const [draggingTextBlockId, setDraggingTextBlockId] = useState<string | null>(null);+ const [resizingTextBlockId, setResizingTextBlockId] = useState<string | null>(null);const [isGeneratingImage, setIsGeneratingImage] = useState(false);const [isExporting, setIsExporting] = useState<null | "pptx" | "pdf">(null);@@ -324,6 +687,34 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChconst descriptionEditorAppliedRef = useRef<Record<string, string>>({});const descriptionEditorFocusedRef = useRef<string | null>(null);const assetCaptionCacheRef = useRef<Record<string, string>>({});+ const derivedProcessingRef = useRef<Set<string>>(new Set());+ const previewCanvasRef = useRef<HTMLDivElement | null>(null);+ const textBlockDragRef = useRef<null | {+ slideId: string;+ versionId: string;+ blockId: string;+ startClientX: number;+ startClientY: number;+ startX: number;+ startY: number;+ blockW: number;+ blockH: number;+ canvasWidth: number;+ canvasHeight: number;+ }>(null);+ const textBlockResizeRef = useRef<null | {+ slideId: string;+ versionId: string;+ blockId: string;+ startClientX: number;+ startClientY: number;+ startW: number;+ startH: number;+ startX: number;+ startY: number;+ canvasWidth: number;+ canvasHeight: number;+ }>(null);const [slideshowOpen, setSlideshowOpen] = useState(false);const [slideshowIndex, setSlideshowIndex] = useState(0);const [slideshowFullscreen, setSlideshowFullscreen] = useState(false);@@ -334,6 +725,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChitem: null});const [windowDimensions, setWindowDimensions] = useState({ width: 0, height: 0 });+ const [previewCanvasSize, setPreviewCanvasSize] = useState({ width: 1100, height: 619 });const isParsingReferenceFiles = Array.from(referencePdfData.values()).some((x) => x.isExtracting);const referenceFiles: ReferenceFile[] = referenceUploadFiles.map((file) => {@@ -384,6 +776,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChgeneratedImages,imageVersions,currentImageVersionId,+ renderLayers,creationStep,creationMode,ideaInput,@@ -404,6 +797,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChgeneratedImages,imageVersions,currentImageVersionId,+ renderLayers,creationStep,creationMode,ideaInput,@@ -428,6 +822,21 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChreturn () => window.removeEventListener('resize', handleResize);}, []);+ useEffect(() => {+ if (typeof ResizeObserver === "undefined") return;+ const target = previewCanvasRef.current;+ if (!target) return;+ const observer = new ResizeObserver((entries) => {+ const entry = entries[0];+ if (!entry) return;+ const width = Math.max(1, Math.round(entry.contentRect.width));+ const height = Math.max(1, Math.round(entry.contentRect.height));+ setPreviewCanvasSize({ width, height });+ });+ observer.observe(target);+ return () => observer.disconnect();+ }, [currentSlideIndex, imageVersions, currentImageVersionId, generatedImages, creationStep]);+const getSlideshowDimensions = () => {if (!windowDimensions.width) return { width: '90vw', height: '50.625vw' }; // Fallbackconst maxWidth = windowDimensions.width * 0.9;@@ -632,7 +1041,9 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChif (incomingSlidesRaw.length === 0) return;- const uploadedImages: string[] = Array.isArray(parsed?.uploadedImages) ? parsed.uploadedImages : [];+ const uploadedImages: string[] = Array.isArray(parsed?.uploadedImages)+ ? Array.from(new Set((parsed.uploadedImages as any[]).map((x: any) => String(x || "").trim()).filter(Boolean))).slice(0, 2)+ : [];const existingById = new Map(localSlides.map((s) => [s.id, s] as const));const getCurrentSlideImageUrlById = (slideId: string) => {const versions = imageVersions[slideId] || [];@@ -699,6 +1110,183 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChconst allowImageEdits = creationStep === "done";if (!allowImageEdits) return;+ const routedEditTasks: Array<() => Promise<void>> = [];+ for (const inc of mergedIncomingSlides) {+ const id = String(inc?.id || "");+ if (!id) continue;+ const slide = mergedSlides.find((s) => s.id === id);+ if (!slide) continue;++ const incomingImageUrl = typeof inc?.imageUrl === "string" ? inc.imageUrl : "";+ const instruction = typeof inc?.imageEditInstruction === "string"+ ? inc.imageEditInstruction+ : typeof inc?.instruction === "string"+ ? inc.instruction+ : "";+ const before = existingById.get(id);+ const changedByPatch =+ !!before &&+ (+ before.title !== slide.title ||+ before.description !== slide.description ||+ before.layout !== slide.layout ||+ before.note !== slide.note ||+ JSON.stringify(before.content || []) !== JSON.stringify(slide.content || [])+ );+ const isNewSlide = !before;+ const editType = normalizeSlideEditType(inc, before, slide);+ const styleRefSlideIds = Array.isArray(inc?.styleRefSlideIds)+ ? inc.styleRefSlideIds.map((x: any) => String(x || "").trim()).filter((x: string) => !!x && x !== id)+ : [];+ const styleRefPolicy: "style_only" | "style_and_layout" =+ inc?.styleRefPolicy === "style_and_layout" ? "style_and_layout" : "style_only";+ const explicitStyleRefImageUrls = Array.isArray(inc?.styleRefImageUrls)+ ? inc.styleRefImageUrls.map((x: any) => String(x || "").trim()).filter((x: string) => !!x)+ : [];+ const explicitMaterialImageUrls = Array.isArray(inc?.materialImageUrls)+ ? inc.materialImageUrls.map((x: any) => String(x || "").trim()).filter((x: string) => !!x)+ : [];+ const styleRefRefsFromSlides = styleRefSlideIds+ .map((sid: string) => {+ const url = getCurrentSlideImageUrlById(sid);+ if (!url) return null;+ const safeSid = sid.replace(/[^a-zA-Z0-9_-]/g, "_");+ return { url, label: `STYLE_REF_SLIDE_${safeSid}`, source: sid };+ })+ .filter(Boolean) as Array<{ url: string; label: string; source: string }>;+ const explicitStyleRefRefs = explicitStyleRefImageUrls.map((url: string, idx: number) => ({+ url,+ label: `STYLE_REF_EXTERNAL_${idx + 1}`,+ source: `external-${idx + 1}`,+ }));+ const styleRefRefs = Array.from(+ new Map([...styleRefRefsFromSlides, ...explicitStyleRefRefs].map((x) => [x.url, x] as const)).values()+ );+ const styleRefImageUrls = styleRefRefs.map((x) => x.url);+ const styleRefMappingText = styleRefRefs.length > 0+ ? styleRefRefs.map((x, i) => `${i + 1}. ${x.label} => ${x.source}`).join("\n")+ : "";+ /*+ const styleReferenceInstruction = styleRefImageUrls.length > 0+ ? (+ styleRefPolicy === "style_and_layout"+ ? tr(+ `风格参考映射如下(标签 => 来源):\n${styleRefMappingText}\n可参考其视觉风格与版式结构,但禁止复用其文字内容。`,+ `Style reference mapping (label => source):\n${styleRefMappingText}\nYou may follow both style and layout, but must not copy their text content.`+ )+ : tr(+ `风格参考映射如下(标签 => 来源):\n${styleRefMappingText}\n仅参考其视觉风格(配色、质感、氛围),不要复制其版式与文字内容。`,+ `Style reference mapping (label => source):\n${styleRefMappingText}\nFollow style only (palette/texture/mood), do not copy their layout or text content.`+ )+ )+ : "";+ */+ const styleReferenceInstruction = styleRefImageUrls.length > 0+ ? (+ styleRefPolicy === "style_and_layout"+ ? tr(+ `参考映射如下(标签 => 来源):\n${styleRefMappingText}\n可参考其风格和版式,但不要复制其中的文字内容。`,+ `Style reference mapping (label => source):\n${styleRefMappingText}\nYou may follow both style and layout, but must not copy their text content.`+ )+ : tr(+ `参考映射如下(标签 => 来源):\n${styleRefMappingText}\n仅参考风格,不要复制其版式和文字内容。`,+ `Style reference mapping (label => source):\n${styleRefMappingText}\nFollow style only (palette/texture/mood), do not copy their layout or text content.`+ )+ )+ : "";+ const page: PptPage = {+ id,+ title: slide.title,+ content: slide.content || [],+ description: slide.description,+ note: slide.note,+ layout: slide.layout,+ };++ if (incomingImageUrl.trim()) {+ const versionId = pushImageVersion(id, incomingImageUrl, "edited", instruction.trim() ? instruction : undefined);+ routedEditTasks.push(async () => {+ await processRenderedSlideVersion(slide, incomingImageUrl, versionId);+ });+ continue;+ }++ if (editType === "text_only" || editType === "text_relayout") {+ routedEditTasks.push(async () => {+ const currentVersionMeta = getSlideVersionMeta(id);+ const currentLayer = currentVersionMeta.versionId ? renderLayers[id]?.[currentVersionMeta.versionId] : undefined;+ if (!isNewSlide && currentVersionMeta.versionId && currentLayer?.textBlocks?.length) {+ await applySlideTextLayerEdit(slide, currentVersionMeta.versionId, editType);+ return;+ }+ const rendered = await pptService.generatePageImage(+ page,+ uiLang as "zh" | "en",+ templateImage || undefined,+ [+ ...styleRefRefs.map((x) => ({ url: x.url, label: x.label })),+ ...getSlideMaterialImageRefs(id),+ ],+ [instruction.trim(), styleReferenceInstruction].filter(Boolean).join("\n")+ );+ if (rendered) {+ await pushImageVersionAndProcess(slide, rendered, "generated", instruction.trim() || undefined);+ }+ });+ continue;+ }++ routedEditTasks.push(async () => {+ const versions = imageVersions[id] || [];+ const currentVersion = currentImageVersionId[id];+ const currentUrl = currentVersion ? versions.find((v) => v.id === currentVersion)?.url : generatedImages[id];+ const shouldRegenerate = isNewSlide || !currentUrl || changedByPatch;+ if (shouldRegenerate) {+ const rendered = await pptService.generatePageImage(+ page,+ uiLang as "zh" | "en",+ templateImage || undefined,+ [+ ...styleRefRefs.map((x) => ({ url: x.url, label: x.label })),+ ...getSlideMaterialImageRefs(id),+ ],+ [instruction.trim(), styleReferenceInstruction].filter(Boolean).join("\n")+ );+ if (rendered) {+ await pushImageVersionAndProcess(slide, rendered, "generated", instruction.trim() || undefined);+ }+ return;+ }+ const editedUrl = await pptService.editPageImage(+ page,+ [instruction.trim(), styleReferenceInstruction].filter(Boolean).join("\n"),+ currentUrl || undefined,+ templateImage || undefined,+ Array.from(new Set([+ ...styleRefImageUrls,+ ...explicitMaterialImageUrls,+ ...uploadedImages,+ ...getSlideMaterialImageUrls(id),+ ]))+ );+ if (editedUrl) {+ await pushImageVersionAndProcess(slide, editedUrl, "edited", instruction.trim() || undefined);+ }+ });+ }++ if (routedEditTasks.length > 0) {+ setIsApplyingEdits(true);+ try {+ await runInParallel(routedEditTasks, MODEL_CONCURRENCY);+ } catch (e) {+ console.error("Failed to apply image edits", e);+ } finally {+ setIsApplyingEdits(false);+ }+ return;+ }+const editTasks: Array<() => Promise<void>> = [];for (const inc of mergedIncomingSlides) {const id = String(inc?.id || "");@@ -753,7 +1341,10 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChif (!slide) continue;if (incomingImageUrl.trim()) {- pushImageVersion(id, incomingImageUrl, "edited", instruction.trim() ? instruction : undefined);+ const versionId = pushImageVersion(id, incomingImageUrl, "edited", instruction.trim() ? instruction : undefined);+ editTasks.push(async () => {+ await processRenderedSlideVersion(slide, incomingImageUrl, versionId);+ });continue;}@@ -805,7 +1396,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh].filter(Boolean).join("\n"));if (rendered) {- pushImageVersion(id, rendered, "generated", kind === "both" ? instruction : undefined);+ await pushImageVersionAndProcess(slide, rendered, "generated", kind === "both" ? instruction : undefined);}});if (kind === "content" || kind === "both") continue;@@ -847,7 +1438,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChArray.from(new Set([...styleRefImageUrls, ...uploadedImages, ...getSlideMaterialImageUrls(id)])));if (editedUrl) {- pushImageVersion(id, editedUrl, "edited", instruction);+ await pushImageVersionAndProcess(slide, editedUrl, "edited", instruction);}});}@@ -876,18 +1467,750 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh});}, [incomingEdit?.id]);- const activeSlides = localSlides.length > 0 ? localSlides : [];- const currentSlide = activeSlides[currentSlideIndex];- const getSlideImageUrl = (slideId: string) => {+ const getSlideVersionMeta = (slideId: string) => {const versions = imageVersions[slideId] || [];- const currentVersion = currentImageVersionId[slideId];- const v = currentVersion ? versions.find((x) => x.id === currentVersion) : undefined;- return v?.url || generatedImages[slideId] || "";+ const visibleVersions = versions.filter((version) => {+ const layer = renderLayers[slideId]?.[version.id];+ return hasRenderableTextBlocks(layer);+ });+ const requestedVersionId = currentImageVersionId[slideId] || "";+ const resolvedVersionId =+ (requestedVersionId && visibleVersions.some((item) => item.id === requestedVersionId)+ ? requestedVersionId+ : visibleVersions[visibleVersions.length - 1]?.id) ||+ requestedVersionId ||+ versions[versions.length - 1]?.id ||+ "";+ const version = resolvedVersionId ? versions.find((x) => x.id === resolvedVersionId) : undefined;+ return {+ versionId: resolvedVersionId,+ version,+ imageUrl: version?.url || generatedImages[slideId] || "",+ };+ };+ const getVisibleSlideVersions = (slideId: string) => {+ const versions = imageVersions[slideId] || [];+ const visible = versions.filter((version) => {+ const layer = renderLayers[slideId]?.[version.id];+ return hasRenderableTextBlocks(layer);+ });+ return visible;+ };+ const getSlideImageUrl = (slideId: string) => getSlideVersionMeta(slideId).imageUrl;+ const getSlideRenderLayer = (slideId: string) => {+ const { versionId } = getSlideVersionMeta(slideId);+ if (!versionId) return undefined;+ return renderLayers[slideId]?.[versionId];+ };+ const getSlideBackgroundUrl = (slideId: string) => {+ const layer = getSlideRenderLayer(slideId);+ return layer?.backgroundImageUrl || getSlideImageUrl(slideId) || "";+ };++ const normalizeSlideEditType = (+ incoming: any,+ before?: SlideData,+ after?: SlideData+ ): SlideEditRoutingItem["editType"] => {+ if (incoming?.editType === "text_only" || incoming?.editType === "text_relayout" || incoming?.editType === "background_redraw") {+ return incoming.editType;+ }+ const hasImageUrl = typeof incoming?.imageUrl === "string" && incoming.imageUrl.trim().length > 0;+ const hasImageEditInstruction =+ typeof incoming?.imageEditInstruction === "string" && incoming.imageEditInstruction.trim().length > 0;+ const hasInstruction = typeof incoming?.instruction === "string" && incoming.instruction.trim().length > 0;+ const hasMaterialImages =+ Array.isArray(incoming?.materialImageUrls) &&+ incoming.materialImageUrls.some((x: any) => String(x || "").trim().length > 0);+ const hasStyleRefImages =+ Array.isArray(incoming?.styleRefImageUrls) &&+ incoming.styleRefImageUrls.some((x: any) => String(x || "").trim().length > 0);+ const hasStyleRefSlides =+ Array.isArray(incoming?.styleRefSlideIds) &&+ incoming.styleRefSlideIds.some((x: any) => String(x || "").trim().length > 0);+ const styleNeedsLayout =+ (incoming?.styleRefPolicy === "style_and_layout" && (hasStyleRefImages || hasStyleRefSlides));+ const legacyKind = incoming?.kind === "content" || incoming?.kind === "visual" || incoming?.kind === "both" ? incoming.kind : null;+ if (legacyKind === "visual" || legacyKind === "both") return "background_redraw";+ if (legacyKind === "content") return "text_relayout";+ if (hasImageUrl || hasImageEditInstruction || hasMaterialImages || styleNeedsLayout) return "background_redraw";+ const layoutChanged = !!before && !!after && before.layout !== after.layout;+ if (layoutChanged) return "background_redraw";+ if (!before) return hasInstruction ? "text_relayout" : "text_only";+ const textChanged =+ !!after &&+ (+ before.title !== after.title ||+ before.description !== after.description ||+ before.note !== after.note ||+ JSON.stringify(before.content || []) !== JSON.stringify(after.content || [])+ );+ if (textChanged || hasInstruction || hasStyleRefImages || hasStyleRefSlides) return "text_relayout";+ return "text_only";+ };++ const updateSlideRenderLayerBlocks = (slideId: string, versionId: string, textBlocks: PptTextBlock[]) => {+ setRenderLayers((prev) => {+ const layer = prev[slideId]?.[versionId];+ if (!layer) return prev;+ return {+ ...prev,+ [slideId]: {+ ...(prev[slideId] || {}),+ [versionId]: {+ ...layer,+ textBlocks,+ },+ },+ };+ });};- const currentSlideImage = currentSlide ? getSlideImageUrl(currentSlide.id) : "";++ const createRenderLayerSnapshotVersion = (+ slideId: string,+ sourceVersionId: string,+ nextLayer: SlideRenderLayer,+ instruction: string,+ ) => {+ const versions = imageVersions[slideId] || [];+ const sourceVersion = versions.find((item) => item.id === sourceVersionId);+ if (!sourceVersion) return sourceVersionId;+ const nextVersionType: SlideImageVersionType = "edited";+ const nextVersionId = pushImageVersion(+ slideId,+ sourceVersion.url,+ nextVersionType,+ instruction,+ {+ sourceVersionId: sourceVersion.sourceVersionId || sourceVersionId,+ }+ );+ setRenderLayerState(slideId, nextVersionId, nextLayer);+ return nextVersionId;+ };++ const applySlideTextLayerEdit = async (+ slide: SlideData,+ versionId: string,+ editType: "text_only" | "text_relayout",+ ) => {+ const layer = renderLayers[slide.id]?.[versionId];+ if (!layer || !Array.isArray(layer.textBlocks) || layer.textBlocks.length === 0) return;+ const nextBlocks = await pptService.rewriteSlideTextBlocks(+ {+ id: slide.id,+ title: slide.title,+ content: slide.content || [],+ description: slide.description,+ note: slide.note,+ layout: slide.layout,+ },+ layer.textBlocks,+ editType,+ uiLang as "zh" | "en",+ );+ if (!Array.isArray(nextBlocks) || nextBlocks.length === 0) return;+ createRenderLayerSnapshotVersion(+ slide.id,+ versionId,+ {+ ...layer,+ textBlocks: nextBlocks,+ },+ tr("文字层调整", "Text layer update"),+ );+ };++ const updateSlideTextBlock = (slideId: string, blockId: string, nextText: string) => {+ const { versionId } = getSlideVersionMeta(slideId);+ if (!versionId) return;+ const currentLayer = renderLayers[slideId]?.[versionId];+ const nextBlocks = (currentLayer?.textBlocks || []).map((block) =>+ block.id === blockId ? { ...block, text: nextText } : block+ );+ setRenderLayers((prev) => {+ const layer = prev[slideId]?.[versionId];+ if (!layer) return prev;+ return {+ ...prev,+ [slideId]: {+ ...(prev[slideId] || {}),+ [versionId]: {+ ...layer,+ textBlocks: nextBlocks,+ },+ },+ };+ });+ setLocalSlides((prev) =>+ prev.map((slide) => {+ if (slide.id !== slideId) return slide;+ const titleBlock = nextBlocks.find((block) => block.role === "title");+ const bulletBlocks = nextBlocks.filter((block) => block.role === "bullet");+ const summaryBlocks = nextBlocks.filter((block) => block.role === "summary");+ return {+ ...slide,+ title: titleBlock ? titleBlock.text.trim() : slide.title,+ content: bulletBlocks.length > 0 ? bulletBlocks.map((block) => stripLeadingBullet(block.text)).filter(Boolean) : slide.content,+ description: summaryBlocks.length > 0 ? summaryBlocks.map((block) => block.text.trim()).filter(Boolean).join("\n") : slide.description,+ };+ })+ );+ };++ const updateSlideTextBlockPosition = (slideId: string, blockId: string, nextX: number, nextY: number, targetVersionId?: string) => {+ const { versionId: currentVersionId } = getSlideVersionMeta(slideId);+ const versionId = targetVersionId || currentVersionId;+ if (!versionId) return;+ setRenderLayers((prev) => {+ const layer = prev[slideId]?.[versionId];+ if (!layer) return prev;+ return {+ ...prev,+ [slideId]: {+ ...(prev[slideId] || {}),+ [versionId]: {+ ...layer,+ textBlocks: layer.textBlocks.map((block) =>+ block.id === blockId+ ? {+ ...block,+ x: Math.max(0, Math.min(1 - block.w, nextX)),+ y: Math.max(0, Math.min(1 - block.h, nextY)),+ }+ : block+ ),+ },+ },+ };+ });+ };++ const updateSlideTextBlockSize = (+ slideId: string,+ blockId: string,+ nextW: number,+ nextH: number,+ targetVersionId?: string,+ ) => {+ const { versionId: currentVersionId } = getSlideVersionMeta(slideId);+ const versionId = targetVersionId || currentVersionId;+ if (!versionId) return;+ setRenderLayers((prev) => {+ const layer = prev[slideId]?.[versionId];+ if (!layer) return prev;+ return {+ ...prev,+ [slideId]: {+ ...(prev[slideId] || {}),+ [versionId]: {+ ...layer,+ textBlocks: layer.textBlocks.map((block) => {+ if (block.id !== blockId) return block;+ const minW = Math.max(0.05, block.role === "title" ? 0.18 : block.role === "tag" ? 0.07 : 0.1);+ const minH = Math.max(0.04, block.role === "title" ? 0.08 : block.role === "tag" ? 0.045 : 0.06);+ return {+ ...block,+ w: Math.max(minW, Math.min(1 - block.x, nextW)),+ h: Math.max(minH, Math.min(1 - block.y, nextH)),+ };+ }),+ },+ },+ };+ });+ };++ const beginTextBlockDrag = (slideId: string, sourceVersionId: string) => {+ return sourceVersionId;+ };++ const beginTextBlockResize = (slideId: string, sourceVersionId: string) => {+ return sourceVersionId;+ };++ useEffect(() => {+ if (!draggingTextBlockId && !resizingTextBlockId) return;+ const handlePointerMove = (event: PointerEvent) => {+ const drag = textBlockDragRef.current;+ if (drag) {+ const dx = (event.clientX - drag.startClientX) / Math.max(drag.canvasWidth, 1);+ const dy = (event.clientY - drag.startClientY) / Math.max(drag.canvasHeight, 1);+ updateSlideTextBlockPosition(+ drag.slideId,+ drag.blockId,+ drag.startX + dx,+ drag.startY + dy,+ drag.versionId,+ );+ }+ const resize = textBlockResizeRef.current;+ if (resize) {+ const dw = (event.clientX - resize.startClientX) / Math.max(resize.canvasWidth, 1);+ const dh = (event.clientY - resize.startClientY) / Math.max(resize.canvasHeight, 1);+ updateSlideTextBlockSize(+ resize.slideId,+ resize.blockId,+ resize.startW + dw,+ resize.startH + dh,+ resize.versionId,+ );+ }+ };+ const stopDrag = () => {+ textBlockDragRef.current = null;+ textBlockResizeRef.current = null;+ setDraggingTextBlockId(null);+ setResizingTextBlockId(null);+ };+ window.addEventListener("pointermove", handlePointerMove);+ window.addEventListener("pointerup", stopDrag);+ window.addEventListener("pointercancel", stopDrag);+ return () => {+ window.removeEventListener("pointermove", handlePointerMove);+ window.removeEventListener("pointerup", stopDrag);+ window.removeEventListener("pointercancel", stopDrag);+ };+ }, [draggingTextBlockId, resizingTextBlockId]);++ const renderSlideTextBlocks = (+ slide: SlideData,+ editable = false,+ canvasWidth = 1100,+ canvasHeight = 619+ ) => {+ const layer = getSlideRenderLayer(slide.id);+ if (!layer || layer.textBlocks.length === 0) return null;+ const scale = Math.max(0.45, Math.min(1.8, Math.min(canvasWidth / 1100, canvasHeight / 619)));+ return layer.textBlocks.map((block) => {+ const style = block.style || {};+ const fontSize = Math.max(10, estimateTextBlockFontSize(block, canvasWidth, canvasHeight));+ const isDragging = draggingTextBlockId === block.id;+ const paddingX = Math.max(6, Math.round((block.role === "title" ? 14 : 12) * scale));+ const paddingY = Math.max(4, Math.round((block.role === "title" ? 10 : 8) * scale));+ const dragHandleSize = Math.max(14, Math.round(20 * scale));+ const dragHandleOffset = Math.max(2, Math.round(4 * scale));+ return (+ <div+ key={block.id}+ className="absolute overflow-hidden"+ style={{+ left: `${block.x * 100}%`,+ top: `${block.y * 100}%`,+ width: `${block.w * 100}%`,+ height: `${block.h * 100}%`,+ padding: `${paddingY}px ${paddingX}px`,+ pointerEvents: editable ? "auto" : "none",+ color: style.color || "#111827",+ fontFamily: style.fontFamily || (uiLang === "zh" ? "Microsoft YaHei" : "Aptos"),+ fontSize: `${fontSize}px`,+ fontWeight: style.fontWeight || (block.role === "title" ? 700 : 500),+ fontStyle: style.fontStyle || "normal",+ lineHeight: String(style.lineHeight || (block.role === "title" ? 1.18 : 1.35)),+ letterSpacing: `${style.letterSpacing || 0}px`,+ textAlign: style.align || "left",+ textShadow: "0 1px 4px rgba(15,23,42,0.18)",+ cursor: editable ? "text" : "default",+ outline: editable && isDragging ? "2px solid rgba(59,130,246,0.45)" : "none",+ }}+ >+ {editable ? (+ <button+ type="button"+ className="absolute z-20 rounded bg-black/55 text-white leading-none shadow-sm cursor-grab active:cursor-grabbing"+ title={tr("拖动文本框", "Drag text block")}+ style={{+ right: `${dragHandleOffset}px`,+ top: `${dragHandleOffset}px`,+ width: `${dragHandleSize}px`,+ height: `${dragHandleSize}px`,+ fontSize: `${Math.max(8, Math.round(10 * scale))}px`,+ }}+ onPointerDown={(event) => {+ event.preventDefault();+ event.stopPropagation();+ const rect = previewCanvasRef.current?.getBoundingClientRect();+ if (!rect) return;+ const { versionId: sourceVersionId } = getSlideVersionMeta(slide.id);+ if (!sourceVersionId) return;+ const editableVersionId = beginTextBlockDrag(slide.id, sourceVersionId);+ textBlockDragRef.current = {+ slideId: slide.id,+ versionId: editableVersionId,+ blockId: block.id,+ startClientX: event.clientX,+ startClientY: event.clientY,+ startX: block.x,+ startY: block.y,+ blockW: block.w,+ blockH: block.h,+ canvasWidth: rect.width,+ canvasHeight: rect.height,+ };+ setDraggingTextBlockId(block.id);+ }}+ >+ ++ </button>+ ) : null}+ <div+ contentEditable={editable}+ suppressContentEditableWarning+ spellCheck={false}+ className="h-full w-full whitespace-pre-wrap break-words bg-transparent outline-none"+ onFocus={() => {+ if (editable) setEditingTextBlockId(block.id);+ }}+ onBlur={(event) => {+ if (!editable) return;+ setEditingTextBlockId((current) => (current === block.id ? null : current));+ const nextText = textToLines(event.currentTarget.textContent || "").join("\n");+ if (!nextText || nextText === block.text) return;+ updateSlideTextBlock(slide.id, block.id, nextText);+ }}+ style={{+ outline: editable && editingTextBlockId === block.id ? "2px solid rgba(59,130,246,0.35)" : "none",+ }}+ >+ {block.text}+ </div>+ </div>+ );+ });+ };++ const renderResponsiveSlideTextBlocks = (+ slide: SlideData,+ editable = false,+ canvasWidth = 1100,+ canvasHeight = 619+ ) => {+ const layer = getSlideRenderLayer(slide.id);+ if (!layer || layer.textBlocks.length === 0) return null;+ return layer.textBlocks.map((block) => (+ <TextBlockOverlay+ key={block.id}+ block={block}+ slideId={slide.id}+ editable={editable}+ uiLang={uiLang as "zh" | "en"}+ canvasWidth={canvasWidth}+ canvasHeight={canvasHeight}+ isDragging={draggingTextBlockId === block.id}+ isEditing={editingTextBlockId === block.id}+ onFocusBlock={(blockId) => {+ if (editable) setEditingTextBlockId(blockId);+ }}+ onBlurBlock={(blockId, nextText) => {+ if (!editable) return;+ setEditingTextBlockId((current) => (current === blockId ? null : current));+ if (!nextText || nextText === block.text) return;+ updateSlideTextBlock(slide.id, blockId, nextText);+ }}+ onDragStart={(event, activeBlock, activeSlideId) => {+ event.preventDefault();+ event.stopPropagation();+ const rect = previewCanvasRef.current?.getBoundingClientRect();+ if (!rect) return;+ const { versionId: sourceVersionId } = getSlideVersionMeta(activeSlideId);+ if (!sourceVersionId) return;+ const editableVersionId = beginTextBlockDrag(activeSlideId, sourceVersionId);+ textBlockDragRef.current = {+ slideId: activeSlideId,+ versionId: editableVersionId,+ blockId: activeBlock.id,+ startClientX: event.clientX,+ startClientY: event.clientY,+ startX: activeBlock.x,+ startY: activeBlock.y,+ blockW: activeBlock.w,+ blockH: activeBlock.h,+ canvasWidth: rect.width,+ canvasHeight: rect.height,+ };+ setDraggingTextBlockId(activeBlock.id);+ }}+ tr={tr}+ />+ ));+ };++ const renderFixedSlideTextSvg = (slide: SlideData) => {+ const layer = getSlideRenderLayer(slide.id);+ if (!layer || layer.textBlocks.length === 0) return null;++ return (+ <div className="absolute inset-0 z-10 pointer-events-none">+ {layer.textBlocks.map((block) => {+ if (editingTextBlockId === block.id) return null;+ const style = block.style || {};+ const refFontSize = Math.max(+ 10,+ estimateTextBlockFontSize(block, PPT_REFERENCE_SLIDE_WIDTH, PPT_REFERENCE_SLIDE_HEIGHT)+ );+ const previewFontSize = refFontSize * PPT_POINT_TO_CSS_PX;+ const lineHeightRatio = Number(style.lineHeight || (block.role === "title" ? 1.12 : block.role === "tag" ? 1.05 : 1.35));+ const x = block.x * PPT_REFERENCE_SLIDE_WIDTH;+ const y = block.y * PPT_REFERENCE_SLIDE_HEIGHT;+ const w = block.w * PPT_REFERENCE_SLIDE_WIDTH;+ const h = block.h * PPT_REFERENCE_SLIDE_HEIGHT;+ const isTag = block.role === "tag";+ const justifyContent = style.align === "center" ? "center" : style.align === "right" ? "flex-end" : "flex-start";+ const textColor = style.color || (block.role === "title" ? "#ffffff" : isTag ? "#ffd66b" : "#ffffff");+ const fontFamily = style.fontFamily || "Aptos";++ return (+ <div+ key={block.id}+ className="absolute overflow-visible"+ style={{+ left: `${x}px`,+ top: `${y}px`,+ width: `${w}px`,+ height: `${h}px`,+ display: "flex",+ alignItems: "center",+ justifyContent,+ background: "transparent",+ border: "none",+ boxShadow: "none",+ }}+ >+ <div+ style={{+ width: "100%",+ whiteSpace: "pre-wrap",+ wordBreak: "break-word",+ overflowWrap: "anywhere",+ textAlign: style.align || "left",+ fontFamily,+ fontSize: `${previewFontSize}px`,+ fontWeight: Number(style.fontWeight || (block.role === "title" ? 900 : isTag ? 800 : 500)),+ fontStyle: style.fontStyle || "normal",+ lineHeight: String(lineHeightRatio),+ letterSpacing: `${Number(style.letterSpacing || 0)}px`,+ color: textColor,+ }}+ >+ {block.text}+ </div>+ </div>+ );+ })}+ </div>+ );+ };++ const renderSlideInteractionLayer = (slide: SlideData) => {+ const layer = getSlideRenderLayer(slide.id);+ if (!layer || layer.textBlocks.length === 0) return null;++ return layer.textBlocks.map((block) => {+ const style = block.style || {};+ const refFontSize = Math.max(+ 10,+ estimateTextBlockFontSize(block, PPT_REFERENCE_SLIDE_WIDTH, PPT_REFERENCE_SLIDE_HEIGHT)+ );+ const previewFontSize = refFontSize * PPT_POINT_TO_CSS_PX;+ const isEditing = editingTextBlockId === block.id;+ const dragHandleSize = 20;+ const resizeHandleSize = 18;+ return (+ <div+ key={`interactive-${block.id}`}+ className="absolute"+ style={{+ left: `${block.x * 100}%`,+ top: `${block.y * 100}%`,+ width: `${block.w * 100}%`,+ height: `${block.h * 100}%`,+ pointerEvents: "auto",+ outline:+ draggingTextBlockId === block.id || resizingTextBlockId === block.id+ ? "2px solid rgba(59,130,246,0.45)"+ : "none",+ }}+ >+ <button+ type="button"+ className="absolute z-20 rounded bg-black/55 text-white leading-none shadow-sm cursor-grab active:cursor-grabbing"+ title={tr("拖动文本框", "Drag text block")}+ style={{+ right: "-8px",+ top: "-6px",+ width: `${dragHandleSize}px`,+ height: `${dragHandleSize}px`,+ fontSize: "10px",+ }}+ onPointerDown={(event) => {+ event.preventDefault();+ event.stopPropagation();+ const rect = previewCanvasRef.current?.getBoundingClientRect();+ if (!rect) return;+ const { versionId: sourceVersionId } = getSlideVersionMeta(slide.id);+ if (!sourceVersionId) return;+ const editableVersionId = beginTextBlockDrag(slide.id, sourceVersionId);+ textBlockDragRef.current = {+ slideId: slide.id,+ versionId: editableVersionId,+ blockId: block.id,+ startClientX: event.clientX,+ startClientY: event.clientY,+ startX: block.x,+ startY: block.y,+ blockW: block.w,+ blockH: block.h,+ canvasWidth: rect.width,+ canvasHeight: rect.height,+ };+ setDraggingTextBlockId(block.id);+ }}+ >+ ++ </button>+ <button+ type="button"+ className="absolute z-20 rounded bg-blue-600/75 text-white leading-none shadow-sm cursor-nwse-resize active:cursor-nwse-resize"+ title={tr("缩放文本框", "Resize text box")}+ style={{+ right: "-7px",+ bottom: "-7px",+ width: `${resizeHandleSize}px`,+ height: `${resizeHandleSize}px`,+ fontSize: "10px",+ }}+ onPointerDown={(event) => {+ event.preventDefault();+ event.stopPropagation();+ const rect = previewCanvasRef.current?.getBoundingClientRect();+ if (!rect) return;+ const { versionId: sourceVersionId } = getSlideVersionMeta(slide.id);+ if (!sourceVersionId) return;+ const editableVersionId = beginTextBlockResize(slide.id, sourceVersionId);+ textBlockResizeRef.current = {+ slideId: slide.id,+ versionId: editableVersionId,+ blockId: block.id,+ startClientX: event.clientX,+ startClientY: event.clientY,+ startW: block.w,+ startH: block.h,+ startX: block.x,+ startY: block.y,+ canvasWidth: rect.width,+ canvasHeight: rect.height,+ };+ setResizingTextBlockId(block.id);+ }}+ >+ {uiLang === "zh" ? "缩" : "↘"}+ </button>+ {isEditing ? (+ <div+ className="absolute inset-0 flex"+ style={{+ alignItems: "center",+ justifyContent: style.align === "center" ? "center" : style.align === "right" ? "flex-end" : "flex-start",+ }}+ >+ <div+ contentEditable+ suppressContentEditableWarning+ spellCheck={false}+ className="w-full whitespace-pre-wrap break-words bg-transparent outline-none"+ onBlur={(event) => {+ const nextText = textToLines(event.currentTarget.textContent || "").join("\n");+ setEditingTextBlockId((current) => (current === block.id ? null : current));+ if (!nextText || nextText === block.text) return;+ updateSlideTextBlock(slide.id, block.id, nextText);+ }}+ ref={(node) => {+ if (!node || editingTextBlockId !== block.id) return;+ node.focus();+ const selection = window.getSelection();+ if (!selection) return;+ const range = document.createRange();+ range.selectNodeContents(node);+ range.collapse(false);+ selection.removeAllRanges();+ selection.addRange(range);+ }}+ style={{+ textAlign: style.align || "left",+ fontFamily: style.fontFamily || "Aptos",+ fontSize: `${previewFontSize}px`,+ fontWeight: Number(style.fontWeight || (block.role === "title" ? 900 : block.role === "tag" ? 800 : 500)),+ fontStyle: style.fontStyle || "normal",+ lineHeight: String(style.lineHeight || (block.role === "title" ? 1.12 : block.role === "tag" ? 1.05 : 1.35)),+ color: style.color || (block.role === "title" ? "#ffffff" : block.role === "tag" ? "#ffd66b" : "#ffffff"),+ outline: "2px solid rgba(59,130,246,0.35)",+ }}+ >+ {block.text}+ </div>+ </div>+ ) : (+ <button+ type="button"+ className="absolute inset-0 bg-transparent"+ onClick={() => setEditingTextBlockId(block.id)}+ aria-label={`Edit ${block.id}`}+ />+ )}+ </div>+ );+ });+ };++ const renderScaledSlideScene = (+ slide: SlideData,+ editable = false,+ outerWidth = PPT_REFERENCE_SLIDE_WIDTH,+ outerHeight = PPT_REFERENCE_SLIDE_HEIGHT,+ ) => {+ const backgroundUrl = getSlideBackgroundUrl(slide.id);+ const hasTextLayer = (getSlideRenderLayer(slide.id)?.textBlocks || []).length > 0;+ const scale = Math.min(+ outerWidth / PPT_REFERENCE_SLIDE_WIDTH,+ outerHeight / PPT_REFERENCE_SLIDE_HEIGHT+ ) || 1;++ if (!backgroundUrl) return null;++ return (+ <div className="absolute inset-0 overflow-hidden">+ <div+ className="absolute left-0 top-0"+ style={{+ width: `${PPT_REFERENCE_SLIDE_WIDTH}px`,+ height: `${PPT_REFERENCE_SLIDE_HEIGHT}px`,+ transform: `scale(${scale})`,+ transformOrigin: "top left",+ }}+ >+ <img+ src={backgroundUrl}+ className="absolute inset-0 w-full h-full object-cover bg-white"+ alt={`Slide ${slide.id}`}+ />+ {hasTextLayer ? renderFixedSlideTextSvg(slide) : null}+ {editable && hasTextLayer ? <div className="absolute inset-0 z-20">{renderSlideInteractionLayer(slide)}</div> : null}+ </div>+ </div>+ );+ };++ const activeSlides = localSlides.length > 0 ? localSlides : [];+ const currentSlide = activeSlides[currentSlideIndex];+ const currentSlideImage = currentSlide ? getSlideBackgroundUrl(currentSlide.id) : "";+ const currentSlideHasTextLayer = currentSlide ? ((getSlideRenderLayer(currentSlide.id)?.textBlocks || []).length > 0) : false;const failedBeautifyCount = activeSlides.reduce((n, s) => n + (beautifyFailures[s.id] ? 1 : 0), 0);const currentSlideFailure = currentSlide ? beautifyFailures[currentSlide.id] : "";+ const formatImageVersionLabel = (version: SlideImageVersion, index: number) => {+ return tr(`第 ${index + 1} 版`, `Version ${index + 1}`);+ };+const enterSlideshowFullscreen = async () => {if (typeof document === "undefined") return;const root = slideshowRootRef.current;@@ -983,19 +2306,160 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChreturn () => window.removeEventListener("keydown", onKeyDown);}, [slideshowOpen, activeSlides.length, slideshowFullscreen]);- const pushImageVersion = (slideId: string, url: string, type: 'generated' | 'edited', instruction?: string) => {+ const setRenderLayerState = (slideId: string, versionId: string, layer: SlideRenderLayer) => {+ setRenderLayers((prev) => ({+ ...prev,+ [slideId]: {+ ...(prev[slideId] || {}),+ [versionId]: layer,+ },+ }));+ };++ const getDerivedProcessingKey = (slideId: string, sourceVersionId: string) => `${slideId}::${sourceVersionId}`;++ const processRenderedSlideVersion = async (slide: SlideData, slideImageUrl: string, versionId: string) => {+ try {+ const page: PptPage = {+ id: slide.id,+ title: slide.title,+ content: slide.content,+ description: slide.description,+ note: slide.note,+ layout: slide.layout,+ };+ const [textBlocks, backgroundImageUrl] = await Promise.all([+ pptService.extractSlideTextBlocks(page, slideImageUrl, uiLang as "zh" | "en"),+ pptService.generateTextlessPageImage(page, slideImageUrl, [], uiLang as "zh" | "en"),+ ]);+ const reviewedTextBlocks = await pptService.reviewSlideTextBlocks(+ page,+ slideImageUrl,+ backgroundImageUrl || slideImageUrl,+ textBlocks,+ uiLang as "zh" | "en"+ );+ return {+ backgroundImageUrl: backgroundImageUrl || slideImageUrl,+ textBlocks: reviewedTextBlocks,+ status: "ready",+ } satisfies SlideRenderLayer;+ } catch (error) {+ console.error("Failed to build PPT render layer", error);+ const failedLayer = {+ backgroundImageUrl: slideImageUrl,+ textBlocks: [],+ status: "failed",+ error: error instanceof Error ? error.message : "Failed to process slide",+ } satisfies SlideRenderLayer;+ setRenderLayerState(slide.id, versionId, failedLayer);+ return failedLayer;+ }+ };++ const pushImageVersion = (+ slideId: string,+ url: string,+ type: SlideImageVersionType,+ instruction?: string,+ options?: { setCurrent?: boolean; sourceVersionId?: string }+ ) => {const versionId = `v-${Date.now()}-${Math.random().toString(16).slice(2)}`;setImageVersions(prev => ({...prev,[slideId]: [...(prev[slideId] || []),- { id: versionId, url, timestamp: Date.now(), type, instruction }+ { id: versionId, url, timestamp: Date.now(), type, instruction, sourceVersionId: options?.sourceVersionId }]}));- setCurrentImageVersionId(prev => ({ ...prev, [slideId]: versionId }));- setGeneratedImages(prev => ({ ...prev, [slideId]: url }));+ if (options?.setCurrent !== false) {+ setCurrentImageVersionId(prev => ({ ...prev, [slideId]: versionId }));+ setGeneratedImages(prev => ({ ...prev, [slideId]: url }));+ }+ return versionId;};+ const pushImageVersionAndProcess = async (slide: SlideData, url: string, type: "generated" | "edited", instruction?: string) => {+ const originalVersionId = pushImageVersion(slide.id, url, type, instruction);+ const processingKey = getDerivedProcessingKey(slide.id, originalVersionId);+ derivedProcessingRef.current.add(processingKey);+ setRenderLayerState(slide.id, originalVersionId, {+ backgroundImageUrl: url,+ textBlocks: [],+ status: "ready",+ });+ try {+ const derived = await processRenderedSlideVersion(slide, url, originalVersionId);+ if (derived?.backgroundImageUrl) {+ setRenderLayerState(slide.id, originalVersionId, derived);+ }+ return originalVersionId;+ } finally {+ derivedProcessingRef.current.delete(processingKey);+ }+ };++ const deriveTextlessVersionFromExisting = async (slide: SlideData, sourceVersion: SlideImageVersion) => {+ const processingKey = getDerivedProcessingKey(slide.id, sourceVersion.id);+ if (derivedProcessingRef.current.has(processingKey)) return sourceVersion.id;+ derivedProcessingRef.current.add(processingKey);+ try {+ const derived = await processRenderedSlideVersion(slide, sourceVersion.url, sourceVersion.id);+ if (derived?.backgroundImageUrl) {+ setRenderLayerState(slide.id, sourceVersion.id, derived);+ }+ return sourceVersion.id;+ } finally {+ derivedProcessingRef.current.delete(processingKey);+ }+ };++ const createTwoStageSlideProgressTracker = (+ slideCount: number,+ initialZh: string,+ initialEn: string,+ processingZh: string,+ processingEn: string,+ ) => {+ const safeTotal = Math.max(1, slideCount);+ const counter = { doneUnits: 0 };+ const setStage = (current: number, zhLabel: string, enLabel: string) => {+ setProgress({+ current,+ total: safeTotal,+ message: tr(zhLabel, enLabel),+ });+ };++ return {+ start() {+ setStage(0, initialZh, initialEn);+ },+ markBaseReady() {+ counter.doneUnits = Math.min(safeTotal * 2, counter.doneUnits + 1);+ setStage(counter.doneUnits / 2, processingZh, processingEn);+ },+ markSlideFinished(baseReady: boolean) {+ counter.doneUnits = Math.min(safeTotal * 2, counter.doneUnits + (baseReady ? 1 : 2));+ setStage(counter.doneUnits / 2, processingZh, processingEn);+ },+ };+ };++ useEffect(() => {+ activeSlides.forEach((slide) => {+ const { versionId, version, imageUrl } = getSlideVersionMeta(slide.id);+ if (!versionId || !version || !imageUrl) return;+ if (version.type === "derived_textless") return;+ if (derivedProcessingRef.current.has(getDerivedProcessingKey(slide.id, versionId))) return;+ const hasDerivedVersion = (imageVersions[slide.id] || []).some((item) => item.sourceVersionId === versionId && item.type === "derived_textless");+ if (hasDerivedVersion) return;+ const layer = renderLayers[slide.id]?.[versionId];+ if (layer?.status === "pending" || hasRenderableTextBlocks(layer)) return;+ void deriveTextlessVersionFromExisting(slide, version);+ });+ }, [activeSlides, imageVersions, currentImageVersionId, renderLayers]);+const handleReferenceFileInputChange = async (e: React.ChangeEvent<HTMLInputElement>) => {const files = Array.from(e.target.files || []);e.target.value = "";@@ -1547,6 +3011,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChsetGeneratedImages({});setImageVersions({});setCurrentImageVersionId({});+ setRenderLayers({});setCurrentSlideIndex(0);};@@ -1788,7 +3253,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChconst initialGenerated: Record<string, string> = {};const initialCurrent: Record<string, string> = {};- const initialVersions: Record<string, Array<{ id: string; url: string; timestamp: number; type: "generated" | "edited"; instruction?: string }>> = {};+ const initialVersions: Record<string, SlideImageVersion[]> = {};for (let i = 0; i < slides.length; i += 1) {const slideId = slides[i].id;@@ -1796,7 +3261,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChconst versionId = `v-${Date.now()}-${Math.random().toString(16).slice(2)}`;initialGenerated[slideId] = url;initialCurrent[slideId] = versionId;- initialVersions[slideId] = [{ id: versionId, url, timestamp: Date.now(), type: "generated", instruction: tr("原始页面", "Original") }];+ initialVersions[slideId] = [{ id: versionId, url, timestamp: Date.now(), type: "generated", instruction: tr("原始页面", "Original full slide") }];}setLocalSlides(slides);@@ -1805,17 +3270,26 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChsetImageVersions(initialVersions);setCreationStep("generating_images");- setProgress({ current: 0, total: slides.length, message: tr("正在美化渲染...", "Beautifying slides...") });+ const progressTracker = createTwoStageSlideProgressTracker(+ slides.length,+ "正在生成美化页图...",+ "Generating beautified slides...",+ "正在处理美化页文字层...",+ "Processing beautified slide text layers...",+ );+ progressTracker.start();const instruction = buildBeautifyInstruction(beautifyRequirement);const beautifyTemplate = beautifyUseTemplate ? (templateImage || undefined) : undefined;- const counter = { done: 0 };const tasks = slides.map((s, i) => async () => {+ let baseReady = false;try {const page: PptPage = { id: s.id, title: s.title, content: s.content || [], description: s.description || "" };const edited = await editPageImageWithRetry(page, instruction, pageImages[i], beautifyTemplate);+ progressTracker.markBaseReady();+ baseReady = true;if (edited) {- pushImageVersion(s.id, edited, "edited", instruction);+ await pushImageVersionAndProcess(s, edited, "edited", instruction);setBeautifyFailures((prev) => {if (!prev[s.id]) return prev;const next = { ...prev };@@ -1829,13 +3303,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChconsole.error(`Beautify failed for slide ${i + 1}`, e);setBeautifyFailures((prev) => ({ ...prev, [s.id]: getErrorMessage(e) }));} finally {- counter.done += 1;- setProgress((prev) => ({- ...prev,- current: counter.done,- total: slides.length,- message: tr(`正在美化渲染... (${counter.done}/${slides.length})`, `Beautifying slides... (${counter.done}/${slides.length})`),- }));+ progressTracker.markSlideFinished(baseReady);}});@@ -1859,37 +3327,36 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChif (failedSlideIds.length === 0) return;setCreationStep("generating_images");- setProgress({- current: 0,- total: failedSlideIds.length,- message: tr(`正在重试失败页... (0/${failedSlideIds.length})`, `Retrying failed slides... (0/${failedSlideIds.length})`),- });+ const progressTracker = createTwoStageSlideProgressTracker(+ failedSlideIds.length,+ "正在重试生成页图...",+ "Retrying slide generation...",+ "正在处理重试页文字层...",+ "Processing retried slide text layers...",+ );+ progressTracker.start();try {const instruction = buildBeautifyInstruction(beautifyRequirement);const beautifyTemplate = beautifyUseTemplate ? (templateImage || undefined) : undefined;- const counter = { done: 0 };const tasks = failedSlideIds.map((slideId) => async () => {+ let baseReady = false;const slide = activeSlides.find((x) => x.id === slideId);const baseImageUrl = generatedImages[slideId] || getSlideImageUrl(slideId);if (!slide || !baseImageUrl) {setBeautifyFailures((prev) => ({ ...prev, [slideId]: tr("缺少可重试的原始图片", "Missing base image for retry") }));- counter.done += 1;- setProgress((prev) => ({- ...prev,- current: counter.done,- total: failedSlideIds.length,- message: tr(`正在重试失败页... (${counter.done}/${failedSlideIds.length})`, `Retrying failed slides... (${counter.done}/${failedSlideIds.length})`),- }));+ progressTracker.markSlideFinished(false);return;}try {const page: PptPage = { id: slide.id, title: slide.title, content: slide.content || [], description: slide.description || "" };const edited = await editPageImageWithRetry(page, instruction, baseImageUrl, beautifyTemplate);+ progressTracker.markBaseReady();+ baseReady = true;if (!edited) {setBeautifyFailures((prev) => ({ ...prev, [slideId]: tr("模型返回空结果", "Empty model result") }));} else {- pushImageVersion(slideId, edited, "edited", instruction);+ await pushImageVersionAndProcess(slide, edited, "edited", instruction);setBeautifyFailures((prev) => {if (!prev[slideId]) return prev;const next = { ...prev };@@ -1900,13 +3367,9 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh} catch (e) {setBeautifyFailures((prev) => ({ ...prev, [slideId]: getErrorMessage(e) }));} finally {- counter.done += 1;- setProgress((prev) => ({- ...prev,- current: counter.done,- total: failedSlideIds.length,- message: tr(`正在重试失败页... (${counter.done}/${failedSlideIds.length})`, `Retrying failed slides... (${counter.done}/${failedSlideIds.length})`),- }));+ if (slide && baseImageUrl) {+ progressTracker.markSlideFinished(baseReady);+ }}});@@ -2030,30 +3493,35 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh}));try {- setProgress({ current: 0, total: pages.length, message: tr("正在生成幻灯片...", "Generating slides...") });-- const imageCounter = { done: 0 };+ const progressTracker = createTwoStageSlideProgressTracker(+ pages.length,+ "正在生成页图...",+ "Generating slide images...",+ "正在处理页文字层...",+ "Processing slide text layers...",+ );+ progressTracker.start();const imageTasks = pages.map((_, i) => async () => {+ let baseReady = false;try {const imageUrl = await pptService.generatePageImage(pages[i],uiLang as "zh" | "en",templateImage || undefined,- getSlideMaterialImageRefs(pages[i].id || `slide-${i + 1}`)+ getSlideMaterialImageRefs(pages[i].id || `slide-${i + 1}`));+ progressTracker.markBaseReady();+ baseReady = true;if (imageUrl) {- const slideId = localSlides[i]?.id || `slide-${i}`;- pushImageVersion(slideId, imageUrl, 'generated');+ const slide = localSlides[i];+ if (slide) {+ await pushImageVersionAndProcess(slide, imageUrl, 'generated');+ }}} catch (e) {console.error(`Failed to generate image for slide ${i}`, e);} finally {- imageCounter.done += 1;- setProgress(prev => ({- ...prev,- current: imageCounter.done,- message: tr(`正在生成幻灯片... (${imageCounter.done}/${pages.length})`, `Generating slides... (${imageCounter.done}/${pages.length})`)- }));+ progressTracker.markSlideFinished(baseReady);}});@@ -2073,7 +3541,6 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChconst handleGenerateImagesOnly = async () => {if (localSlides.length === 0) return;setCreationStep('generating_images');- setProgress({ current: 0, total: localSlides.length, message: tr("正在生成幻灯片...", "Generating slides...") });const pages: PptPage[] = localSlides.map((s) => ({id: s.id,@@ -2083,8 +3550,16 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChstatus: "description_generated"}));- const imageCounter = { done: 0 };+ const progressTracker = createTwoStageSlideProgressTracker(+ pages.length,+ "正在生成页图...",+ "Generating slide images...",+ "正在处理页文字层...",+ "Processing slide text layers...",+ );+ progressTracker.start();const imageTasks = pages.map((_, i) => async () => {+ let baseReady = false;try {const imageUrl = await pptService.generatePageImage(pages[i],@@ -2092,18 +3567,18 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChtemplateImage || undefined,getSlideMaterialImageRefs(pages[i].id || `slide-${i + 1}`));+ progressTracker.markBaseReady();+ baseReady = true;if (imageUrl) {- pushImageVersion(pages[i].id || `slide-${i}`, imageUrl, 'generated');+ const slide = localSlides[i];+ if (slide) {+ await pushImageVersionAndProcess(slide, imageUrl, 'generated');+ }}} catch (e) {console.error(`Failed to generate image for slide ${i}`, e);} finally {- imageCounter.done += 1;- setProgress(prev => ({- ...prev,- current: imageCounter.done,- message: tr(`正在生成幻灯片... (${imageCounter.done}/${pages.length})`, `Generating slides... (${imageCounter.done}/${pages.length})`)- }));+ progressTracker.markSlideFinished(baseReady);}});@@ -2123,38 +3598,26 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChsetIsGeneratingImage(true);try {- // Use the service logic if description exists, otherwise fallbacklet imageUrl: string | null = null;-- if (currentSlide.description) {- const pages: PptPage[] = localSlides.map(s => ({- id: s.id,- title: s.title,- content: s.content,- status: 'description_generated',- description: s.description,- note: s.note,- layout: s.layout- }));- const pageIndex = localSlides.findIndex(s => s === currentSlide);- imageUrl = await pptService.generatePageImage(- pages[pageIndex],- uiLang as "zh" | "en",- templateImage || undefined,- getSlideMaterialImageRefs(currentSlide.id || `slide-${pageIndex + 1}`)- );- } else {- // Fallback to simple prompt- const prompt = `Design a presentation slide. Title: "${currentSlide.title}". Content: ${(currentSlide.content || []).join('; ')}. Style: Professional, Modern.`;- imageUrl = await generateImage({- prompt: prompt,- referenceImageUrl: templateImage || undefined- });- }+ const pages: PptPage[] = localSlides.map(s => ({+ id: s.id,+ title: s.title,+ content: s.content,+ status: 'description_generated',+ description: s.description,+ note: s.note,+ layout: s.layout+ }));+ const pageIndex = localSlides.findIndex(s => s === currentSlide);+ imageUrl = await pptService.generatePageImage(+ pages[pageIndex],+ uiLang as "zh" | "en",+ templateImage || undefined,+ getSlideMaterialImageRefs(currentSlide.id || `slide-${pageIndex + 1}`)+ );if (imageUrl) {- const slideId = currentSlide.id || `slide-${currentSlideIndex + 1}`;- pushImageVersion(slideId, imageUrl, 'generated');+ await pushImageVersionAndProcess(currentSlide, imageUrl, 'generated');}} catch (e) {console.error("Failed to generate slide image", e);@@ -2163,12 +3626,28 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh}};+ const toRenderablePage = (slide: SlideData): PptPage => {+ const layer = getSlideRenderLayer(slide.id);+ return {+ id: slide.id,+ title: slide.title,+ content: slide.content,+ description: slide.description,+ note: slide.note,+ layout: slide.layout,+ textBlocks: layer?.textBlocks || [],+ backgroundImageUrl: layer?.backgroundImageUrl || getSlideImageUrl(slide.id) || undefined,+ status: "completed",+ };+ };+const handleAddSlideToChat = (slide: SlideData) => {if (onAddToChat) {const slideId = slide.id || `slide-${currentSlideIndex + 1}`;const currentVersion = currentImageVersionId[slideId];const versions = imageVersions[slideId] || [];const imageUrl = currentVersion ? versions.find(v => v.id === currentVersion)?.url : generatedImages[slideId];+ const layer = getSlideRenderLayer(slideId);const materialImages = (slideMaterials[slideId] || []).map((x) => ({name: x.name,url: x.dataUrl,@@ -2177,7 +3656,13 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChsourcePage: typeof x.sourcePage === "number" ? x.sourcePage : undefined,refLabel: x.refLabel || "",}));- onAddToChat(JSON.stringify({ ...slide, imageUrl, materialImages }, null, 2), `${slideId}.json`);+ onAddToChat(JSON.stringify({+ ...slide,+ imageUrl,+ backgroundImageUrl: layer?.backgroundImageUrl || imageUrl,+ textBlocks: layer?.textBlocks || [],+ materialImages,+ }, null, 2), `${slideId}.json`);}};@@ -2185,12 +3670,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChif (isExporting) return;setIsExporting("pptx");try {- const pages: PptPage[] = activeSlides.map(s => ({- id: s.id,- title: s.title,- content: s.content,- status: 'completed'- }));+ const pages: PptPage[] = activeSlides.map((s) => toRenderablePage(s));const images: Record<string, string> = {};for (const s of activeSlides) {const versions = imageVersions[s.id] || [];@@ -2211,12 +3691,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageChif (isExporting) return;setIsExporting("pdf");try {- const pages: PptPage[] = activeSlides.map(s => ({- id: s.id,- title: s.title,- content: s.content,- status: 'completed'- }));+ const pages: PptPage[] = activeSlides.map((s) => toRenderablePage(s));const images: Record<string, string> = {};for (const s of activeSlides) {const versions = imageVersions[s.id] || [];@@ -3129,11 +4604,12 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh{/* Thumbnails */}<div className="w-64 bg-zinc-50 dark:bg-zinc-900 border-r border-border overflow-y-auto p-4 space-y-4">{activeSlides.map((slide, index) => {- const hasGeneratedImage = generatedImages[slide.id || index];+ const hasGeneratedImage = getSlideBackgroundUrl(slide.id);+ const hasTextLayer = (getSlideRenderLayer(slide.id)?.textBlocks || []).length > 0;const slideFailure = beautifyFailures[slide.id];return (<ContextMenu key={slide.id || index}>- <ContextMenuTrigger>+ <ContextMenuTrigger asChild><divonClick={() => setCurrentSlideIndex(index)}className={`cursor-pointer border-2 rounded-lg overflow-hidden relative aspect-[16/9] group transition-all duration-200 ${@@ -3158,7 +4634,7 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh</div>{/* Thumbnail Preview */}{hasGeneratedImage ? (- <img src={hasGeneratedImage} className="w-full h-full object-cover" alt={`Slide ${index + 1}`} />+ renderScaledSlideScene(slide, false, 240, 135)) : (<div className="w-full h-full p-2 flex flex-col bg-white overflow-hidden text-[6px]">{templateImage && (@@ -3221,14 +4697,22 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh{isGeneratingImage ? <Loader2 className="w-3 h-3 mr-1 animate-spin" /> : null}{tr("重新渲染本页", "Regenerate this slide")}</Button>- {currentSlide && (imageVersions[currentSlide.id] || []).length > 0 && (+ {currentSlide && getVisibleSlideVersions(currentSlide.id).length > 0 && (<selectclassName="h-7 text-xs rounded-md border border-input bg-background px-2"- value={currentImageVersionId[currentSlide.id] || (imageVersions[currentSlide.id] || [])[0]?.id}+ value={+ (() => {+ const visibleVersions = getVisibleSlideVersions(currentSlide.id);+ const currentVersionId = currentImageVersionId[currentSlide.id];+ return visibleVersions.some((item) => item.id === currentVersionId)+ ? currentVersionId+ : visibleVersions[visibleVersions.length - 1]?.id || "";+ })()+ }onChange={(e) => {const slideId = currentSlide.id;const versionId = e.target.value;- const versions = imageVersions[slideId] || [];+ const versions = getVisibleSlideVersions(slideId);const v = versions.find(x => x.id === versionId);if (v) {setCurrentImageVersionId(prev => ({ ...prev, [slideId]: versionId }));@@ -3236,9 +4720,9 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh}}}>- {(imageVersions[currentSlide.id] || []).map((v, idx) => (+ {getVisibleSlideVersions(currentSlide.id).map((v, idx) => (<option key={v.id} value={v.id}>- {idx + 1} 路 {new Date(v.timestamp).toLocaleString()}+ {formatImageVersionLabel(v, idx)} · {new Date(v.timestamp).toLocaleString()}</option>))}</select>@@ -3252,14 +4736,15 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh) : null}{currentSlide ? (<ContextMenu>- <ContextMenuTrigger className="outline-none">+ <ContextMenuTrigger asChild><div- className="relative w-full max-w-[1100px] bg-white shadow-2xl rounded-sm overflow-hidden flex flex-col transition-transform duration-300"+ ref={previewCanvasRef}+ className="relative w-full max-w-[1100px] bg-white shadow-2xl rounded-sm overflow-hidden flex flex-col transition-transform duration-300 outline-none"style={{ aspectRatio: "16/9" }}>{/* Display either generated image or DOM layout */}{currentSlideImage ? (- <img src={currentSlideImage} className="w-full h-full object-contain bg-white" alt="AI Generated Slide" />+ renderScaledSlideScene(currentSlide, true, previewCanvasSize.width, previewCanvasSize.height)) : (<div className="w-full h-full p-12 flex flex-col relative">{/* Background Template */}@@ -3355,20 +4840,21 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh<div className="flex-1 flex items-center justify-center p-8 overflow-hidden bg-black/90">{activeSlides[slideshowIndex] ? (+ (() => {+ const slideDims = getSlideshowDimensions();+ const slideWidth = typeof slideDims.width === "number" ? slideDims.width : 1100;+ const slideHeight = typeof slideDims.height === "number" ? slideDims.height : 619;+ return (<div className="relative w-full h-full flex items-center justify-center"><divclassName="relative bg-white shadow-2xl overflow-hidden rounded-lg mx-auto"style={{- width: getSlideshowDimensions().width,- height: getSlideshowDimensions().height+ width: slideDims.width,+ height: slideDims.height}}>- {getSlideImageUrl(activeSlides[slideshowIndex].id) ? (- <img- src={getSlideImageUrl(activeSlides[slideshowIndex].id)}- className="w-full h-full object-contain bg-black"- alt="Slide"- />+ {getSlideBackgroundUrl(activeSlides[slideshowIndex].id) ? (+ renderScaledSlideScene(activeSlides[slideshowIndex], false, slideWidth, slideHeight)) : (<div className="w-full h-full p-16 flex flex-col"><h1 className="text-5xl font-bold mb-12 text-zinc-900 border-b-4 border-blue-600 pb-6 w-fit pr-16">@@ -3390,6 +4876,8 @@ export function PptWorkspace({ data, onAddToChat, onPptReadyChange, onPptStageCh)}</div></div>+ );+ })()) : null}</div>diff --git a/vite.config.ts b/vite.config.tsindex cbb7ae7..c222648 100644--- a/vite.config.ts+++ b/vite.config.ts@@ -10,6 +10,7 @@ import { POST as postLogSave } from "./api/routes/log-save";import { POST as postLogFileParser } from "./api/routes/log-file-parser";import { POST as postThirdPartyParser } from "./api/routes/third-party-parser";import { POST as postChat } from "./api/routes/chat";+import { POST as postPptAi } from "./api/routes/ppt-ai";type RouteHandler = (request: Request) => Promise<Response>;@@ -21,6 +22,7 @@ const API_ROUTES: Record<string, RouteHandler> = {"POST /api/log-file-parser": postLogFileParser,"POST /api/third-party-parser": postThirdPartyParser,"POST /api/chat": postChat,+ "POST /api/ppt-ai": postPptAi,};async function toWebRequest(req: IncomingMessage): Promise<Request> {