79 Commits

Author SHA1 Message Date
stringadmin 30222cd830 Merge pull request 'Main merge work' (#21) from main-merge-work into main
Reviewed-on: #21
2026-06-16 13:13:50 +00:00
stringadmin 4ca2ab4a9c Merge origin/main into main-merge-work (resolve EcommercePage/CSS conflicts) 2026-06-16 21:13:25 +08:00
stringadmin 588da45902 refactor: optimize Topbar scroll listener; sync WIP ecommerce refactor and CSS 2026-06-16 21:09:41 +08:00
stringadmin 5466036349 refactor: extract Topbar and LocalAvatar components from App.tsx 2026-06-16 20:15:53 +08:00
stringadmin 9869c0c5e6 Merge pull request 'feat: refactor ecommerce toolbar from mode tabs to scenario-based tabs with rich template cards' (#20) from feat/ecommerce-scenario-tabs into main
Reviewed-on: #20
2026-06-16 11:16:46 +00:00
ludan 5811cbac16 feat: refactor ecommerce toolbar from mode tabs to scenario-based tabs with rich template cards
- EcommercePage.tsx:
  - Introduce CommerceScenarioKey type (popular/poster/mainImage/scene/festival/model/background/retouch/salesVideo) and CommerceScenarioTemplate interface with scenario/output/desc/badge fields
  - Add commerceScenarioOptions (9 scenario tabs with icons) replacing cloneOutputOptions as toolbar data source; each scenario maps to an output mode via commerceScenarioOutputMap
  - Add commerceScenarioTemplates (16 templates across 8 scenarios) with thumbnail, badge, title, and description; popularCommerceScenarioTemplates aggregates cross-scenario highlights for the "热门" default tab
  - Replace activeCloneTemplateCards with activeCommerceScenarioTemplates filtered by scenario; popular tab shows highlights, others show scenario-specific templates
  - handleCommerceScenarioClick: switch scenario, auto-toggle output mode, toggle template strip visibility; clicking active scenario toggles strip; popular tab preserves current output
  - handleCloneTemplateCardClick: auto-switch output mode to match template, fill prompt, focus textarea with 80ms delayed re-focus for reliability
  - Template card markup upgraded: media thumbnail (94px cover image with hover scale), body with badge pill, title, and 2-line description
  - Active scenario button shows close indicator (CloseOutlined) when strip is open
  - Template strip defaults to visible (isCloneTemplateStripVisible initial true)
  - Add "左右滑动查看更多" scroll hint for narrow viewports
- ecommerce-standalone.css (+355 lines):
  - Scenario tabs: horizontal flex scroll with hidden scrollbar, pill-shaped buttons (grid: 24px icon + fluid label), per-scenario color accent via --mode-accent custom property (pink for popular, orange for poster/festival, green for mainImage/scene/background, blue for model/retouch/salesVideo)
  - Active/open states: radial gradient glow, lifted shadow, intensified border color
  - Icon slot: 24px rounded square with tinted background and inset highlight
  - Close indicator: absolute top-right circle with hover scale
  - Template cards: 2-column grid (94px media + fluid body), badge capsule, thumbnail with hover scale(1.035), title 820 weight, 2-line description clamp
  - Responsive: ≤1024px 2-col card grid, ≤900px scroll-hint visible + left-aligned tabs, ≤640px horizontal scroll cards with snap, compact card sizing
2026-06-16 19:11:50 +08:00
stringadmin c38f056527 style: make topbar fixed transparent floating header 2026-06-16 16:39:58 +08:00
stringadmin 3469071819 Merge pull request 'Main merge work' (#19) from main-merge-work into main
Reviewed-on: #19
2026-06-16 06:38:21 +00:00
stringadmin f1be7d8d66 Merge 3b72455: PR #18 multi-turn conversation 2026-06-16 14:34:31 +08:00
stringadmin c6583d1881 Merge 526ad49: Merge branch main into record detail panel 2026-06-16 14:34:20 +08:00
stringadmin 047c66ed88 Merge 4993f6e: multi-turn conversation system 2026-06-16 14:34:00 +08:00
stringadmin d82a49d96c Merge 3321b96: 接入 husky + lint-staged 2026-06-16 14:33:17 +08:00
stringadmin 91f2f9dfe8 Merge 79f220d: add responsive layouts for template cards and hot clone 2026-06-16 14:29:13 +08:00
stringadmin 1eca1d702b Merge c1c7cb3: fix ecommerce preview and module compatibility 2026-06-16 14:28:43 +08:00
stringadmin ff4d40bcf6 Merge 003c41d: 抽出 useVideoSceneRunner hook 2026-06-16 14:27:54 +08:00
stringadmin c8e0839fc8 Merge b67f2e7: codex/main-latest branch (de3eb1d + 643595b + f056547) 2026-06-16 14:24:18 +08:00
stringadmin 20c3772cbb Merge f929be3: PR #17 优化记录详情对话面板 2026-06-16 14:04:48 +08:00
stringadmin 0543766bd6 Merge a287573: Merge branch main into chat polish 2026-06-16 14:04:37 +08:00
stringadmin 8269e32779 Merge 85adcdc: 优化记录详情对话面板布局与视觉层次 2026-06-16 14:04:11 +08:00
stringadmin 94711dc4cf Merge 66b7613: re-trigger push 2026-06-16 14:04:00 +08:00
stringadmin fdc48d2e65 Merge ab99e3b: PR #16 完善电商记录详情页 2026-06-16 14:03:50 +08:00
stringadmin 39a3edde1c Merge e3b48e2: 完善电商记录详情页 (resolved via ab99e3b) 2026-06-16 14:03:30 +08:00
stringadmin c748d1e3ba Merge 62fcf46: 抽出克隆/历史持久化模块 2026-06-16 14:01:34 +08:00
stringadmin 2e87adc957 Merge 9a9c7eb: optimize ecommerce hot clone UI (resolved conflicts + fixed unclosed block) 2026-06-16 14:00:11 +08:00
stringadmin 0958a9870e Merge 6dd2922: 收口 server/client 数据解析层 2026-06-16 13:55:41 +08:00
stringadmin bdedad0b90 Merge 8985dee: 统一 taskSubscription import 为静态 2026-06-16 13:55:25 +08:00
stringadmin a9f707525d Merge f30e585: extract platform rules and prompt builders 2026-06-16 13:55:02 +08:00
stringadmin d8cbf0d182 Merge 5b316a2: PR #14 record detail workspace 2026-06-16 13:54:51 +08:00
stringadmin 3a36174041 Merge 3f1954b: Merge branch main into record detail panel 2026-06-16 13:54:37 +08:00
stringadmin 2b69a82aea Merge 96d335d: generation record detail workspace 2026-06-16 13:54:10 +08:00
stringadmin e460901ad7 Merge 45e6534: 引入 Vitest 测试骨架 2026-06-16 13:53:40 +08:00
stringadmin b416e96e99 Merge 307537a: fix(ecommerce) clone-ai-node-label 定位样式 2026-06-16 13:53:14 +08:00
stringadmin 3b72455062 Merge pull request 'feat: implement multi-turn conversation system for generation record detail with deduplication enhancement' (#18) from feat/ecommerce-record-detail-conversation-panel into main
Reviewed-on: #18
2026-06-16 05:07:49 +00:00
stringadmin 526ad490f7 Merge branch 'main' into feat/ecommerce-record-detail-conversation-panel 2026-06-16 05:07:45 +00:00
ludan 4993f6eeec feat: implement multi-turn conversation system for generation record detail with deduplication enhancement
- generationRecordClient.ts: Enhance save deduplication with payload signature — replace simple recordId-based dedup with stableJsonStringify-based signature comparison; same recordId + same signature skips save, changed payload proceeds; add buildSaveSignature covering tool/mode/title/status/prompt/taskIds/assets/config/result/metadata; store signature alongside savedAt in recentlySavedRecords map for per-turn save accuracy
- EcommercePage.tsx: Introduce EcommerceHistoryTurn interface and multi-turn conversation architecture —
  - Add EcommerceHistoryTurn with full generation context (status/output/platform/market/language/ratio/requirement/images/results/counts/modules/scenes/replicateLevel); EcommerceHistoryRecord gains status/errorMessage/turns[] fields
  - beginEcommerceHistoryTurn() — start a new generation turn, create or append to record, persist to localStorage immediately
  - updateLocalEcommerceHistoryTurn() — real-time turn status sync (generating→done/failed) with record summary mirroring via syncRecordSummaryWithTurn()
  - restoreHistoryTurnInputs() — one-click parameter restoration from failed turns for retry
  - upsertCanvasNode() — insert or update canvas node by ID (dedup by turnId), alternating row layout (x: index*420, y: 0 or 160)
  - Generate flow wired to turns: status callbacks update turn state; cancel sets turn to failed; results written to turn.results
  - Record detail conversation panel refactored from single-message to per-turn iteration — each turn renders user message (requirement + meta + assets) and assistant message (status-aware text + progress bar during generation + result thumbnails); failed turns show "恢复参数" retry button; generating turn shows EcommerceProgressBar
  - openEcommerceHistoryRecord() loads all turns as canvas nodes with distributed positions; preserves generating turn tracking via activeHistoryTurnIdRef
  - History list items display status label (生成中/失败/time)
  - Product set preview backdrop moved to createPortal(document.body) with z-index 4000
- pages/ecommerce.css: Bump product-set-preview-backdrop z-index from 100 to 4000 for Portal rendering layer
2026-06-16 13:02:11 +08:00
stringadmin 3321b96e29 chore: 接入 husky + lint-staged,pre-commit 跑 tsc,pre-push 跑 css:audit
- pre-commit: npx lint-staged → tsc --noEmit(仅检查暂存的 TS/TSX 文件)
2026-06-16 13:01:46 +08:00
stringadmin 120fc2e70c refactor(css): #6 后续阶段——@layer 级联 + token 化 + 行尾治理
- 引入 @layer ecommerce-core,standalone 覆盖层不再依赖 !important(全站 !important 7812→967)
2026-06-16 12:23:50 +08:00
Codex 79f220dbbf feat: add responsive layouts for template cards and hot clone 2026-06-16 11:44:55 +08:00
Codex c1c7cb3cc7 fix ecommerce preview and module compatibility 2026-06-15 22:00:00 +08:00
Codex b67f2e7601 Merge branch 'main' of http://118.145.251.184:3000/OmniAI/omniai-ds-code-package into codex/main-latest-20260615-030000 2026-06-15 21:56:00 +08:00
stringadmin 003c41ddcc refactor(video): 抽出 useVideoSceneRunner hook,视频场景任务编排与 UI 分离(#3) 2026-06-15 20:18:26 +08:00
Codex f056547160 fix: align hot clone reference upload UI 2026-06-15 19:59:00 +08:00
stringadmin 643595bede refactor(css): CSS 第一阶段瘦身——止血 + 拆分 ecommerce-standalone(#6) 2026-06-15 18:32:14 +08:00
Codex de3eb1d06a merge main and adjust clone mode tabs 2026-06-15 18:25:38 +08:00
stringadmin f929be30ed Merge pull request 'feat: 优化记录详情对话面板布局与视觉层次' (#17) from feat/ecommerce-chat-polish into main
Reviewed-on: #17
2026-06-15 10:24:35 +00:00
stringadmin a2875738ce Merge branch 'main' into feat/ecommerce-chat-polish 2026-06-15 10:24:30 +00:00
ludan 85adcdceef feat: 优化记录详情对话面板布局与视觉层次
本次修改聚焦于电商记录详情页的对话面板体验打磨:

一、对话顺序优化(EcommercePage.tsx):
- 将"新需求"跟进消息从AI回复之前移至AI回复之后
- 调整后的对话时间线:用户历史需求 → AI回复 → 用户新需求,逻辑更符合真实对话流程

二、对话面板视觉升级(ecommerce-standalone.css):
- 对话面板宽度采用CSS变量动态控制(408-440px),视觉更宽敞
- 消息气泡区分明确:
  · 用户消息:左侧缩进26-36px,蓝色调渐变背景,青色边框
  · AI消息:右侧缩进26-36px,蓝调边框,中性背景
  · 跟进消息:独特高亮样式,更强边框(0.24透明度)和投影
- 排版细节打磨:
  · 消息标签字号12px/权重820
  · 正文13px/行高1.64
  · 气泡内间距15px、圆角20px、投影加深
- 元信息标签(emo)精修:28px高度、圆角胶囊样式
- 素材缩略图:46x46px、圆角14px
- 响应式适配:≤900px面板收窄至92vw,≤480px去除消息缩进

变更文件:
- src/features/ecommerce/EcommercePage.tsx (+20/-20)
- src/styles/ecommerce-standalone.css (+121)
2026-06-15 18:23:36 +08:00
Codex 66b761314b chore: re-trigger push 2026-06-15 16:52:15 +08:00
stringadmin ab99e3bf2f Merge pull request 'feat: 完善电商记录详情页,支持触摸手势交互、对话式需求面板与画布节点拖拽' (#16) from feat/ecommerce-record-detail-polish into main
Reviewed-on: #16
2026-06-15 08:38:41 +00:00
ludan e3b48e2614 feat: 完善电商记录详情页,支持触摸手势交互、对话式需求面板与画布节点拖拽
本次修改全面打磨电商图片工作台的记录详情体验,主要包含以下变更:

一、记录详情对话面板(EcommercePage.tsx):
- 将记录详情中的"需求"区域重构为聊天对话式布局:
  · 历史需求消息:展示原始需求文本、参数元信息(平台/语种/比例/设置)、已上传素材缩略图
  · 新增跟进需求消息(is-followup):若当前素材与历史记录不同,自动展示新上传素材及当前参数配置
  · AI 回复消息保持原有状态展示
- 记录详情中素材上传数量上限从 7 张提升至 20 张(maxCloneProductImages)
- 上传按钮重构:移至素材列表左侧,显示当前数量/上限,满额时禁用并提示"已满"

二、触摸与手势交互:
- 新增 PreviewTouchGesture 完整手势系统:
  · 单指平移(pan):支持触摸拖拽预览画布
  · 双指缩放(pinch):以双指中心为锚点进行缩放,范围 0.25x-2x
  · 自动排除交互元素(按钮/输入框/链接等)避免冲突
  · 智能切换:单指/双指模式无缝切换
- 画布节点触摸拖拽(canvas node drag):
  · 支持触摸拖拽移动生成结果节点
  · 考虑当前缩放级别计算位移
  · 与预览画布手势互不干扰

三、记录详情页视觉升级(ecommerce-standalone.css):
- 整体背景采用径向渐变+线性渐变,营造专业 SaaS 质感
- 对话面板与历史面板统一采用毛玻璃卡片风格
- 聊天消息气泡:圆角 18px、柔和投影、用户消息左侧缩进 18px
- 历史面板宽度固定 292px
- CSS 自定义属性体系(record-detail-*)统一管理颜色和阴影
- 面板头部加高加粗标题,优化可读性

四、其他细节优化:
- 历史刷新按钮图标从文本符号改为 ReloadOutlined 组件
- 素材缩略图移除 hover 放大镜效果(.ecom-command-asset-zoom)
- 刷新按钮禁用样式完善

变更文件:
- src/features/ecommerce/EcommercePage.tsx (+246/-11)
- src/styles/ecommerce-standalone.css (+1369)
2026-06-15 16:20:55 +08:00
stringadmin 62fcf461b6 refactor(ecommerce): 抽出克隆/历史持久化模块(#2 续) 2026-06-15 16:06:45 +08:00
Codex 9a9c7eb86d feat: optimize ecommerce hot clone UI 2026-06-15 15:26:49 +08:00
stringadmin 6dd292207f refactor(api): 收口 server/client 数据解析层,消除 aiGenerationClient 的 as T 断言 2026-06-15 15:06:41 +08:00
stringadmin 8985deea0a fix(video): 统一 taskSubscription import 为静态,消除 Vite chunk 警告 2026-06-15 14:42:37 +08:00
stringadmin f30e585cfa refactor(ecommerce): extract platform rules and prompt builders 2026-06-15 14:35:40 +08:00
stringadmin 5b316a2399 Merge pull request 'feat: add generation record detail workspace with AI conversation panel and canvas reset' (#14) from feat/ecommerce-record-detail-conversation-panel into main
Reviewed-on: #14
2026-06-15 05:41:17 +00:00
stringadmin 3f1954b38d Merge branch 'main' into feat/ecommerce-record-detail-conversation-panel 2026-06-15 05:41:12 +00:00
ludan 96d335db8a feat: add generation record detail workspace with AI conversation panel and canvas reset
- EcommercePage.tsx: Add isCloneConversationCollapsed state for toggling conversation sidebar; introduce isMainCloneWorkspace / isRecordDetailWorkspace derived flags to scope record-detail features to main clone tool only; compute currentResultCount, activeHistoryRecord, and currentResultThumbs for display; add canvas reset button (zoom=1, offset=0) in preview toolbar when viewing a history record; build AI conversation panel (clone-ai-conversation-panel) as left sidebar with:
  - Header showing record title, model/platform/language metadata, and collapse button
  - User message bubble with requirement text and uploaded asset thumbnails (up to 4 + overflow count)
  - Assistant message bubble with status-aware response text (done/generating/failed/idle), EcommerceProgressBar during generation, and clickable result thumbnails that open product set preview
  - Collapse/expand toggle button with MenuFoldOutlined / MenuUnfoldOutlined icons
- ecommerce-standalone.css (+1204 lines): Define record detail workspace layout (CSS grid: 352px chat column + fluid canvas); grid-pattern background with radial gradient accent; conversation panel styling with chat bubble cards, asset thumbnail grids, result thumbnail buttons, scrollable body; collapsed state (grid-template-columns: 0 1fr); toggle button positioning; responsive breakpoints for tablets and mobile with adjusted chat width and stacked layout
2026-06-15 13:40:14 +08:00
stringadmin 45e6534ee1 test: 引入 Vitest 测试骨架并抽出颜色/比例纯函数模块 2026-06-15 13:39:02 +08:00
stringadmin 307537a7ce fix(ecommerce): 补全 clone-ai-node-label 在 result-stack 顶部的定位样式 2026-06-15 11:33:04 +08:00
Codex 48262d6233 chore: 新增 .gitattributes 统一换行符为 LF 2026-06-15 10:52:03 +08:00
Codex 062c8b3445 feat: 临时下线智能抠图与图片翻译入口 2026-06-15 10:42:33 +08:00
stringadmin 0b2d6b901f feat: 电商工作台进度与生成记录健壮性优化 2026-06-15 10:24:31 +08:00
stringadmin e1fdbe5f9b Merge remote-tracking branch 'origin/codex/ecommerce-hot-video-responsive' into main-merge-work 2026-06-13 19:41:10 +08:00
stringadmin f51dfb17e1 Merge remote-tracking branch 'origin/fix/compact-composer-whitespace' into main-merge-work
# Conflicts:
#	src/features/ecommerce/EcommercePage.tsx
#	src/styles/ecommerce-standalone.css
2026-06-13 19:41:04 +08:00
stringadmin 76ae9ab0ac Merge pull request 'feat: 重构电商指令栏布局,模式标签外置、精简结果标签、优化生成记录交互' (#13) from feat/ecommerce-composer-redesign into main
Reviewed-on: #13
2026-06-13 11:29:36 +00:00
stringadmin 98db427ac5 Merge remote-tracking branch 'origin/main' into feat/ecommerce-composer-redesign
# Conflicts:
#	src/features/ecommerce/EcommercePage.tsx
2026-06-13 19:28:51 +08:00
stringadmin 573cbacbd3 Merge pull request 'Codex/fix project review bugs' (#12) from codex/fix-project-review-bugs into main
Reviewed-on: #12
2026-06-13 11:11:03 +00:00
stringadmin 38b513aebf Merge branch 'main' into codex/fix-project-review-bugs 2026-06-13 11:10:57 +00:00
stringadmin 4d5f487a80 fix: adjust ecommerce source thumbnail label 2026-06-12 19:32:20 +08:00
ludan 4f6e32fb10 feat: 重构电商指令栏布局,模式标签外置、精简结果标签、优化生成记录交互
本次修改对电商图片工作台的指令栏(composer)进行了全面重构,主要包含以下变更:

一、指令栏布局重构(EcommercePage.tsx):
- 新增生成模式标签页(ecom-command-mode-tabs),5种模式(套图/详情图/模特图/视频/爆款图)以标签形式外置于输入区上方,每种模式配有独立图标和配色
- 设置行(平台/语种/比例/设置)移入输入区内部,采用圆角胶囊按钮排列
- 上传按钮从输入区移到底部工具栏,改为"上传素材"紧凑样式
- 精简生成结果画布:移除所有文字标签(套图/详情图/模特图/爆款图 标签、原图素材标签、结果卡片标签),让图片成为绝对视觉焦点
- 灵感行"AI团队"更名为"作品记录",更新描述文案为"沉淀最近生成的高转化素材,随时回看与复用"

二、样式系统升级(ecommerce-standalone.css):
- 新增模式标签页完整样式:5列等宽网格、磨砂玻璃背板、各模式独立主题色
  · 套图 set:翠绿 #0f8f72
  · 详情图 detail:紫色 #7a5af8
  · 模特图 model:蓝色 #1073cc
  · 视频 video:暖橙 #cc6b14
  · 爆款图 hot:玫红 #c04468
- hover/active 状态带径向光晕和上浮微动效(translateY(-1px))
- 隐藏生成结果中的所有文字标签(display:none),减少视觉噪音
- 修复历史记录删除按钮定位:改为绝对居中定位,不受网格布局影响
- 输入区改为单列布局,增大最小高度(214-286px),增加内边距

变更文件:
- src/features/ecommerce/EcommercePage.tsx (+87/-51)
- src/styles/ecommerce-standalone.css (+456)
2026-06-12 18:41:31 +08:00
stringadmin 1f97167023 fix: polish ecommerce generation states 2026-06-12 18:15:58 +08:00
stringadmin 9ae5e1f493 Merge pull request 'Codex/fix project review bugs' (#11) from codex/fix-project-review-bugs into main
Reviewed-on: #11
2026-06-12 09:29:49 +00:00
stringadmin ad4bca31b1 fix: address project review bugs 2026-06-12 17:25:30 +08:00
stringadmin f9e55578b3 Merge remote-tracking branch 'origin/main' into fix/ecommerce-ui-polish
# Conflicts:
#	src/features/ecommerce/EcommercePage.tsx
2026-06-12 16:04:09 +08:00
stringadmin 7fdaa38504 feat: 电商快捷工具接入真实API并增强预览交互
- 图片修改接入局部重绘API,改为左右对比布局
- 去水印接入真实API,带进度条
- A+详情页预览区增加生成中/失败状态与进度条
- 新增图片翻译页面(含语言选择器)
- 快捷功能栏改为一行五列均分布局,移除白框
- 预览弹窗与A+详情页结果增加保存本地按钮
2026-06-12 16:00:43 +08:00
stringadmin e88edbe165 fix: 优化 compact 对话框、画布节点标签、删图重置及比例弹窗
- compact 模式尺寸调整,生成按钮不再溢出框外
- 生成时自动进入 compact 状态,idle 时恢复
- 删除所有样图后重置为新对话状态(清除画布、恢复标题)
- 去掉 drag handle 模式标签,原图右上角统一高级黑 tag
- 作品图不再显示重复标识
- 比例弹窗宽度自适应内容,添加 hover/active 交互样式
- 套图模式默认三种各一张
- 设置弹窗点击外部可关闭
- 历史记录删除按钮样式优化

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-12 12:37:32 +08:00
stringadmin 863f1f075e fix: 修复 compact 模式下对话框底部大量空白
高特异性 min-height 规则覆盖了 compact 模式的 max-height: 126px,
导致 .ecom-command-composer 在缩小状态下仍保持 218px+ 的最小高度。
在文件末尾添加更高特异性的 is-compact 覆盖,强制 min-height: 118px。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-12 11:33:13 +08:00
Codex aa133d0f5c style: refine ecommerce quick tool pages 2026-06-12 00:08:59 +08:00
78 changed files with 28732 additions and 1067 deletions
+43
View File
@@ -0,0 +1,43 @@
# 自动检测文本文件并统一换行符
* text=auto eol=lf
# 源码强制使用 LF(跨平台一致)
*.ts text eol=lf
*.tsx text eol=lf
*.js text eol=lf
*.jsx text eol=lf
*.mjs text eol=lf
*.cjs text eol=lf
*.json text eol=lf
*.css text eol=lf
*.html text eol=lf
*.md text eol=lf
*.svg text eol=lf
# 配置类(统一 LF
*.yml text eol=lf
*.yaml text eol=lf
*.toml text eol=lf
*.conf text eol=lf
# Windows 专用脚本保持 CRLF
*.bat text eol=crlf
*.cmd text eol=crlf
# 二进制文件,不做换行符转换
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.webp binary
*.ico binary
*.woff binary
*.woff2 binary
*.ttf binary
*.eot binary
*.otf binary
*.mp4 binary
*.mp3 binary
*.pdf binary
*.zip binary
*.gz binary
+1
View File
@@ -15,3 +15,4 @@ tmp/
*.swp
*.swo
coverage/
屏幕截图 *.png
+1
View File
@@ -0,0 +1 @@
npx lint-staged
+1
View File
@@ -0,0 +1 @@
npm run css:audit
+1 -1
View File
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="zh-CN">
<html lang="zh-CN" data-theme="dark" data-ui-theme="dark-green" style="color-scheme: dark">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
+10
View File
@@ -0,0 +1,10 @@
// lint-staged 配置 —— 配合 husky pre-commit 使用
//
// 当前只运行 tsc 全量类型检查(tsc 不接受单文件增量检查),
// 未来可扩展 ESLint / Prettier / stylelint 等按文件的检查。
//
// 函数语法返回原始命令字符串,lint-staged 不会追加文件名。
export default {
"*.{ts,tsx}": () => "tsc --noEmit",
};
+3459
View File
File diff suppressed because it is too large Load Diff
+14 -4
View File
@@ -7,20 +7,30 @@
"dev": "vite --host 127.0.0.1",
"build": "vite build",
"preview": "vite preview --host 127.0.0.1",
"type-check": "tsc -p tsconfig.json --noEmit"
"type-check": "tsc -p tsconfig.json --noEmit",
"test": "vitest",
"test:run": "vitest run",
"test:coverage": "vitest run --coverage",
"css:audit": "node scripts/css-audit.mjs",
"prepare": "husky"
},
"dependencies": {
"@ant-design/icons": "5.3.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"scheduler": "0.23.0",
"zustand": "5.0.13"
},
"devDependencies": {
"@types/react": "18.2.0",
"@types/react-dom": "18.2.0",
"@types/react": "18.2.55",
"@types/react-dom": "18.2.18",
"@vitejs/plugin-react": "4.2.1",
"@vitest/coverage-v8": "^1.6.0",
"husky": "^9.1.7",
"lint-staged": "^17.0.7",
"typescript": "5.3.3",
"vite": "5.1.0",
"vite-plugin-compression2": "2.5.3"
"vite-plugin-compression2": "2.5.3",
"vitest": "^1.6.0"
}
}
+87
View File
@@ -0,0 +1,87 @@
// CSS 健康度审计脚本。
// 用法: npm run css:audit
// 输出每个 CSS 文件的行数、选择器数、!important 数、@media 数,
// 以及 !important 密度(每 100 行的 !important 数)。
// 用于建立基线、跟踪 CSS 瘦身进度、防止 !important 回潮。
import { readFileSync, readdirSync, statSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join, relative } from "node:path";
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..", "src", "styles");
const REPORT = [];
function scanCssFile(filePath) {
const content = readFileSync(filePath, "utf-8");
const lines = content.split(/\r?\n/).length;
const selectors = (content.match(/\{/g) || []).length;
const important = (content.match(/!important/g) || []).length;
const media = (content.match(/@media/g) || []).length;
const density = lines > 0 ? ((important / lines) * 100).toFixed(1) : "0";
return { lines, selectors, important, media, density };
}
function walk(dir) {
for (const entry of readdirSync(dir)) {
const full = join(dir, entry);
const st = statSync(full);
if (st.isDirectory()) {
walk(full);
} else if (entry.endsWith(".css")) {
const rel = relative(ROOT, full).replace(/\\/g, "/");
REPORT.push({ file: rel, ...scanCssFile(full) });
}
}
}
walk(ROOT);
// Sort by !important count descending to surface the worst offenders.
REPORT.sort((a, b) => b.important - a.important);
const totals = REPORT.reduce(
(acc, r) => {
acc.lines += r.lines;
acc.selectors += r.selectors;
acc.important += r.important;
acc.media += r.media;
return acc;
},
{ lines: 0, selectors: 0, important: 0, media: 0 },
);
const pad = (s, n) => String(s).padEnd(n);
const num = (s, n) => String(s).padStart(n);
console.log("\nCSS Audit Report — src/styles/\n");
console.log(
`${pad("File", 52)} ${num("Lines", 7)} ${num("Sel", 6)} ${num("!imp", 7)} ${num("@media", 7)} imp/100ln`,
);
console.log("-".repeat(92));
for (const r of REPORT) {
console.log(
`${pad(r.file, 52)} ${num(r.lines, 7)} ${num(r.selectors, 6)} ${num(r.important, 7)} ${num(r.media, 7)} ${r.density}`,
);
}
console.log("-".repeat(92));
console.log(
`${pad("TOTAL", 52)} ${num(totals.lines, 7)} ${num(totals.selectors, 6)} ${num(totals.important, 7)} ${num(totals.media, 7)} ${((totals.important / totals.lines) * 100).toFixed(1)}`,
);
console.log("");
// Exit non-zero if total !important exceeds a budget threshold.
// Current baseline: ~7795. Set budget slightly above to allow incremental work
// while preventing uncontrolled growth.
const IMPORTANT_BUDGET = 7820;
if (totals.important > IMPORTANT_BUDGET) {
console.error(
`FAIL: !important count ${totals.important} exceeds budget ${IMPORTANT_BUDGET}. ` +
`Run with --no-important-check to bypass (not recommended).`,
);
process.exit(1);
} else {
console.log(
`OK: !important count ${totals.important} within budget ${IMPORTANT_BUDGET} ` +
`(headroom ${IMPORTANT_BUDGET - totals.important}).`,
);
}
+51 -143
View File
@@ -1,28 +1,23 @@
import { Suspense, useCallback, useEffect, useMemo, useState } from "react";
import { lazy, Suspense, useCallback, useEffect, useMemo, useState } from "react";
import {
BugOutlined,
CheckCircleFilled,
CloseOutlined,
HomeOutlined,
IdcardOutlined,
LockOutlined,
LoadingOutlined,
LoginOutlined,
LogoutOutlined,
MailOutlined,
MobileOutlined,
PictureOutlined,
SafetyOutlined,
UserOutlined,
VideoCameraOutlined,
WalletOutlined,
} from "@ant-design/icons";
import { LocalAvatar } from "./components/LocalAvatar";
import { Topbar } from "./components/Topbar";
import ErrorBoundary from "./components/ErrorBoundary";
import ToastContainer from "./components/toast/ToastContainer";
import { toast } from "./components/toast/toastStore";
import EcommercePage from "./features/ecommerce/EcommercePage";
import { flushPendingGenerationRecords } from "./api/generationRecordClient";
import { ossAssets } from "./data/ossAssets";
import { keyServerClient } from "./api/keyServerClient";
import { setUserMaxConcurrency } from "./api/generationConcurrency";
import {
@@ -38,8 +33,13 @@ import { useAppStore, useSessionStore } from "./stores";
import type { WebUserSession } from "./types";
import "./styles/ecommerce-standalone.css";
const EcommercePage = lazy(() => import("./features/ecommerce/EcommercePage"));
type AuthMode = "login" | "register";
type AuthMethod = "account" | "email" | "phone";
type WorkspaceChromeState = {
isToolPage: boolean;
};
interface LocalProfilePageProps {
session: WebUserSession;
@@ -51,33 +51,11 @@ interface LocalProfilePageProps {
onLogout: () => void;
}
const profileWorks = [
{ title: "主图套图生成", desc: "电商主图与场景图自动生成", image: ossAssets.ecommerce.templateCases[0], type: "图像", time: "6/9 18:13" },
{ title: "A+详情页设计", desc: "产品卖点与长图详情版式", image: ossAssets.ecommerce.templateCases[1], type: "图像", time: "6/9 10:11" },
{ title: "短视频广告", desc: "产品展示短视频脚本与画面", image: ossAssets.ecommerce.productSet.hosting, type: "视频", time: "6/9 10:05" },
{ title: "模特图生成", desc: "服饰商品真人上身展示", image: ossAssets.ecommerce.tryOn.tryA, type: "图像", time: "6/9 10:03" },
{ title: "商品场景图", desc: "按平台比例输出营销素材", image: ossAssets.ecommerce.detail.gridA, type: "图像", time: "6/9 10:01" },
{ title: "高度复刻", desc: "参考图结构复刻与商品替换", image: ossAssets.ecommerce.detail.gridB, type: "图像", time: "6/9 09:39" },
{ title: "详情模块", desc: "功能卖点、参数和包装模块", image: ossAssets.ecommerce.detail.gridC, type: "图像", time: "6/8 21:20" },
{ title: "平台素材", desc: "淘宝/天猫投放图批量生成", image: ossAssets.ecommerce.detail.gridD, type: "图像", time: "6/8 18:26" },
];
function LocalAvatar({ session, size = "md" }: { session: WebUserSession; size?: "sm" | "md" | "lg" }) {
const displayName = session.user.displayName || session.user.username || "用户";
const label = displayName.trim().slice(0, 1).toUpperCase() || "用";
const avatarUrl = session.user.avatarUrl;
return (
<span className={`local-user-avatar local-user-avatar--${size}`}>
{avatarUrl ? <img src={avatarUrl} alt={displayName} /> : <span>{label}</span>}
</span>
);
}
function LocalProfilePage({ session, balance, imageCount, videoCount, onBack, onBugFeedback, onLogout }: LocalProfilePageProps) {
const displayName = session.user.displayName || session.user.username || "用户";
const workCount = Math.max(imageCount + videoCount, profileWorks.length);
const projectCount = Math.max(1, Math.round(workCount / 18));
const assetCount = Math.max(1, Math.round(workCount / 20));
const workCount = Math.max(imageCount + videoCount, 0);
const projectCount = 0;
const assetCount = 0;
return (
<section className="local-profile-page">
@@ -142,22 +120,15 @@ function LocalProfilePage({ session, balance, imageCount, videoCount, onBack, on
<header>
<div>
<strong></strong>
<span></span>
<span></span>
</div>
<em>{workCount} </em>
</header>
<div className="local-profile-work-grid">
{profileWorks.map((work) => (
<article key={`${work.title}-${work.time}`} className="local-profile-work-card">
<img src={work.image} alt="" />
<div>
<span>{work.type}</span>
<strong>{work.title}</strong>
<p>{work.desc}</p>
<em> · {work.time}</em>
<div className="local-profile-work-grid local-profile-work-grid--empty">
<div className="local-profile-empty">
<strong></strong>
<span></span>
</div>
</article>
))}
</div>
</section>
</main>
@@ -184,7 +155,9 @@ function App() {
const [sessionNotice, setSessionNotice] = useState<string | null>(null);
const [profileMenuOpen, setProfileMenuOpen] = useState(false);
const [currentPage, setCurrentPage] = useState<"workspace" | "profile">("workspace");
const [workspaceKey, setWorkspaceKey] = useState(0);
const [workspaceChrome, setWorkspaceChrome] = useState<WorkspaceChromeState>({
isToolPage: false,
});
useEffect(() => {
void loadDarkGreenTheme();
@@ -337,20 +310,6 @@ function App() {
};
const balance = Math.max(usage.balanceCents, 0) / 100;
const displayName = session?.user.displayName || session?.user.username || "用户";
const actualWorkCount = Math.max(usage.imageUsed + usage.videoUsed, 0);
const shownWorkCount = Math.max(actualWorkCount, profileWorks.length);
const avatarMenuStats = useMemo(
() => [
{ icon: <IdcardOutlined />, label: "UID", value: session?.user.id ?? "-" },
{ icon: <WalletOutlined />, label: "积分", value: `${balance.toFixed(2)} 积分` },
{ icon: <PictureOutlined />, label: "图片", value: usage.imageUsed },
{ icon: <VideoCameraOutlined />, label: "视频", value: usage.videoUsed },
{ icon: <PictureOutlined />, label: "作品", value: shownWorkCount },
],
[balance, session?.user.id, shownWorkCount, usage.imageUsed, usage.videoUsed],
);
const handleOpenProfile = () => {
setProfileMenuOpen(false);
@@ -360,7 +319,6 @@ function App() {
const handleOpenWorkspace = () => {
setProfileMenuOpen(false);
setCurrentPage("workspace");
setWorkspaceKey((k) => k + 1);
};
const handleBugFeedback = () => {
@@ -369,85 +327,31 @@ function App() {
};
return (
<div className="ecommerce-standalone web-shell" data-theme="dark" data-ui-theme="dark-green" data-view="ecommerce">
<header className="ecommerce-standalone__topbar">
<button type="button" className="ecommerce-standalone__brand" onClick={handleOpenWorkspace}>
<span className="ecommerce-standalone__logo" aria-hidden="true">
<img src="https://stringtest.oss-cn-hangzhou.aliyuncs.com/logo.png" alt="" />
</span>
<strong>OmniAI </strong>
</button>
<div className="ecommerce-standalone__account">
{session ? (
<div className="ecommerce-profile-menu">
<span className="ecommerce-standalone__credits">
{(Math.max(usage.balanceCents, 0) / 100).toFixed(2)}
</span>
<button
type="button"
className="ecommerce-profile-menu__trigger"
onClick={() => setProfileMenuOpen((open) => !open)}
aria-haspopup="dialog"
aria-expanded={profileMenuOpen}
<div
className="ecommerce-standalone web-shell"
data-theme="dark"
data-ui-theme="dark-green"
data-view="ecommerce"
data-workspace-tool-page={workspaceChrome.isToolPage ? "true" : "false"}
>
<LocalAvatar session={session} size="sm" />
<span>{displayName}</span>
</button>
{profileMenuOpen ? (
<>
<button
type="button"
className="ecommerce-profile-popover__backdrop"
aria-label="关闭账户信息"
onClick={() => setProfileMenuOpen(false)}
<Topbar
session={session}
usage={usage}
profileMenuOpen={profileMenuOpen}
onProfileMenuOpenChange={setProfileMenuOpen}
onOpenWorkspace={handleOpenWorkspace}
onOpenProfile={handleOpenProfile}
onOpenAuth={openAuth}
onLogout={handleLogout}
onBugFeedback={handleBugFeedback}
/>
<section className="ecommerce-profile-popover" role="dialog" aria-label="账户信息">
<div className="ecommerce-profile-popover__head">
<LocalAvatar session={session} size="md" />
<div>
<strong>{displayName}</strong>
<span>{session.user.username}</span>
</div>
</div>
<dl className="ecommerce-profile-popover__stats">
{avatarMenuStats.map((item) => (
<div key={item.label}>
<dt>{item.icon}{item.label}</dt>
<dd>{item.value}</dd>
</div>
))}
</dl>
<div className="ecommerce-profile-popover__actions">
<button type="button" className="is-primary" onClick={handleOpenProfile}>
<UserOutlined />
</button>
<button type="button" onClick={handleBugFeedback}>
<BugOutlined />
Bug
</button>
<button type="button" className="is-danger" onClick={handleLogout}>
<LogoutOutlined />
退
</button>
</div>
</section>
</>
) : null}
</div>
) : (
<button type="button" onClick={() => openAuth("login")}>
<LoginOutlined />
<span> / </span>
</button>
)}
</div>
</header>
<main className="ecommerce-standalone__content">
{currentPage === "profile" && session ? (
{session ? (
<div
className="ecommerce-standalone__page ecommerce-standalone__page--profile"
hidden={currentPage !== "profile"}
>
<LocalProfilePage
session={session}
balance={balance}
@@ -457,7 +361,14 @@ function App() {
onBugFeedback={handleBugFeedback}
onLogout={handleLogout}
/>
) : (
</div>
) : null}
{/* 工作台常驻挂载,仅用 hidden 切换。切到个人中心时不卸载,
生成任务、进度动画、已上传图片等本地状态全部保留,切回即继续。 */}
<div
className="ecommerce-standalone__page ecommerce-standalone__page--workspace"
hidden={Boolean(session) && currentPage === "profile"}
>
<ErrorBoundary>
<Suspense
fallback={
@@ -468,9 +379,9 @@ function App() {
}
>
<EcommercePage
key={workspaceKey}
projects={[]}
isAuthenticated={Boolean(session)}
onWorkspaceChromeChange={setWorkspaceChrome}
onStartCreate={() => undefined}
onOpenProject={() => undefined}
onDeleteProject={() => undefined}
@@ -482,7 +393,7 @@ function App() {
/>
</Suspense>
</ErrorBoundary>
)}
</div>
</main>
{authOpen ? (
@@ -503,10 +414,7 @@ function App() {
<CloseOutlined />
</button>
<span className="ecommerce-auth-modal__logo" aria-hidden="true">
<i />
<i />
<i />
<i />
<img src="https://stringtest.oss-cn-hangzhou.aliyuncs.com/logo.png" alt="" />
</span>
<h2 id="ecommerce-auth-title">{authMode === "login" ? "欢迎回来" : "创建账号"}</h2>
<p className="ecommerce-auth-modal__subtitle">{authMode === "login" ? "登录后继续你的 AI 创作之旅" : "注册即可免费体验全部功能"}</p>
+56 -43
View File
@@ -1,8 +1,5 @@
import { buildApiUrl, buildAuthHeaders } from "./serverConnection";
const TEXT_MODELS = ["qwen-max", "qwen-plus", "qwen-turbo"];
const VISION_MODELS = ["qwen3.7-plus", "qwen-vl-plus", "qwen-vl-max"];
type AbortSignalConstructorWithAny = typeof AbortSignal & {
any?: (signals: AbortSignal[]) => AbortSignal;
};
@@ -110,11 +107,45 @@ export interface ComplianceCheck {
allow_video_generation: boolean;
}
function findJsonSlice(raw: string): string {
const start = raw.search(/[\[{]/);
if (start < 0) return raw;
const stack: string[] = [];
let inString = false;
let escaped = false;
for (let index = start; index < raw.length; index += 1) {
const char = raw[index];
if (inString) {
if (escaped) {
escaped = false;
} else if (char === "\\") {
escaped = true;
} else if (char === "\"") {
inString = false;
}
continue;
}
if (char === "\"") {
inString = true;
} else if (char === "{" || char === "[") {
stack.push(char === "{" ? "}" : "]");
} else if (char === "}" || char === "]") {
if (stack.pop() !== char) break;
if (stack.length === 0) return raw.slice(start, index + 1);
}
}
return raw.slice(start);
}
function extractJson(text: string): unknown {
const fenced = text.match(/```(?:json)?\s*([\s\S]*?)```/);
const raw = fenced ? fenced[1].trim() : text.trim();
const start = raw.search(/[[{]/);
const slice = start >= 0 ? raw.slice(start) : raw;
const slice = findJsonSlice(raw);
try {
return JSON.parse(slice);
} catch {
@@ -122,9 +153,16 @@ function extractJson(text: string): unknown {
}
}
type ChatContent =
| string
| Array<
| { type: "image_url"; image_url: { url: string } }
| { type: "text"; text: string }
>;
interface ChatMessage {
role: "system" | "user";
content: string;
content: ChatContent;
}
const MAX_RETRIES = 3;
@@ -171,22 +209,20 @@ async function chat(
userContent: string,
options?: { model?: string; signal?: AbortSignal },
): Promise<string> {
const candidateModels = options?.model ? [options.model] : TEXT_MODELS;
let lastError: Error | null = null;
for (const model of candidateModels) {
try {
return await retryOnTransient(async () => {
return retryOnTransient(async () => {
const messages: ChatMessage[] = [
{ role: "system", content: systemPrompt },
{ role: "user", content: userContent },
];
const timeoutSignal = AbortSignal.timeout(CHAT_TIMEOUT_MS);
const combinedSignal = combineAbortSignals(options?.signal, timeoutSignal);
const body: Record<string, unknown> = { messages, stream: false, temperature: 0.4 };
if (options?.model) body.model = options.model;
const res = await fetch(buildApiUrl("ai/chat"), {
method: "POST",
headers: buildAuthHeaders(),
body: JSON.stringify({ model, messages, stream: false, temperature: 0.4 }),
body: JSON.stringify(body),
signal: combinedSignal,
});
if (!res.ok) {
@@ -199,15 +235,6 @@ async function chat(
if (!content) throw new Error("模型未返回有效内容");
return content;
}, options?.signal);
} catch (err) {
lastError = err instanceof Error ? err : new Error(String(err));
if (options?.signal?.aborted) throw lastError;
// If user pinned a specific model, don't fall back to others
if (options?.model) throw lastError;
// Try next model in fallback chain
}
}
throw lastError ?? new Error("所有候选模型均不可用");
}
async function visionChat(
@@ -216,30 +243,28 @@ async function visionChat(
imageUrls: string[],
signal?: AbortSignal,
): Promise<string> {
const content = [
...imageUrls.map((url) => ({ type: "image_url", image_url: { url } })),
const content: ChatContent = [
...imageUrls.map((url) => ({ type: "image_url" as const, image_url: { url } })),
{ type: "text", text },
];
const messages = [
{ role: "system", content: systemPrompt },
{ role: "user", content },
];
] satisfies ChatMessage[];
let lastError: Error | null = null;
for (const model of VISION_MODELS) {
return retryOnTransient(async () => {
const timeoutSignal = AbortSignal.timeout(CHAT_TIMEOUT_MS);
const combinedSignal = combineAbortSignals(signal, timeoutSignal);
try {
const out = await retryOnTransient(async () => {
const res = await fetch(buildApiUrl("ai/chat"), {
method: "POST",
headers: buildAuthHeaders(),
body: JSON.stringify({ model, messages, stream: false, temperature: 0.3 }),
body: JSON.stringify({ messages, stream: false, temperature: 0.3 }),
signal: combinedSignal,
});
if (!res.ok) {
const errBody = await res.text().catch(() => "");
if (errBody.includes("image format")) throw new Error("IMAGE_FORMAT_FALLBACK");
if (errBody.includes("image format")) throw new Error("图片格式不受支持,请更换图片后重试");
throw new Error(`图片理解调用失败 (${res.status})${errBody ? `: ${errBody.slice(0, 120)}` : ""}`);
}
const payload = await res.json();
@@ -248,18 +273,6 @@ async function visionChat(
if (!result) throw new Error("图片理解未返回有效内容");
return result;
}, signal);
return out;
} catch (err) {
lastError = err instanceof Error ? err : new Error(String(err));
if (signal?.aborted) throw lastError;
// Continue trying next vision model on transient failures, image format errors, or upstream errors
if (lastError.message === "IMAGE_FORMAT_FALLBACK") continue;
if (lastError.message.includes("图片理解调用失败")) continue;
if (isTransientError(lastError)) continue;
throw lastError;
}
}
throw lastError ?? new Error("图片理解调用失败,所有模型均不可用");
}
const IMAGE_UNDERSTANDING_PROMPT = `你是电商产品图片分析专家。请分析用户提供的产品图片,识别产品主体、外观、颜色、材质、形状、尺寸感、品牌标识、关键部件、可视化卖点和适合展示的镜头角度。请用简洁的中文段落描述,不要编造图片中看不到的信息。`;
+1
View File
@@ -0,0 +1 @@
export * from "./aiGenerationClient.ts";
+32 -25
View File
@@ -1,18 +1,24 @@
import {
buildApiUrl,
buildAuthHeaders,
isRecord,
readJsonResponse,
serverRequest,
throwResponseError,
} from "./serverConnection";
import { isOptionalApiRouteMissing } from "./apiErrorUtils";
import {
parseAiTaskStatus,
parseAiTaskStatusList,
parseImageTaskCreateResponse,
parseSseTaskFrame,
parseTaskCreateResponse,
} from "./dtoParsers";
import type { WebGenerationPreviewTask } from "../types";
export interface ImageGenInput {
projectId?: string;
conversationId?: number;
model: string;
model?: string;
prompt: string;
ratio?: string;
quality?: string;
@@ -89,6 +95,8 @@ export interface ImageEditInput {
imageUrl: string;
function: string;
prompt?: string;
maskUrl?: string;
ratio?: string;
n?: number;
}
@@ -188,13 +196,6 @@ function parseContentDispositionFilename(value: string | null): string | undefin
return plainMatch?.[1]?.trim() || undefined;
}
function extractTaskList(payload: unknown): AiTaskStatus[] {
if (Array.isArray(payload)) return payload as AiTaskStatus[];
if (!isRecord(payload)) return [];
const rows = payload.tasks ?? payload.items;
return Array.isArray(rows) ? (rows as AiTaskStatus[]) : [];
}
function getStoredSessionRole(): string {
try {
if (typeof window === "undefined") return "";
@@ -208,8 +209,9 @@ function getStoredSessionRole(): string {
}
function emitImageRouteDebug(label: string, payload: Record<string, unknown>): void {
// Only emit console logs for admin users — hides enterprise routing details
if (getStoredSessionRole() === "admin") {
// Only emit route debug for admin users; provider routing is operational data.
if (getStoredSessionRole() !== "admin") return;
const entry: ImageRouteDebugEntry = {
at: new Date().toISOString(),
label,
@@ -220,14 +222,12 @@ function emitImageRouteDebug(label: string, payload: Record<string, unknown>): v
} catch {
console.log(label, entry);
}
}
if (typeof window === "undefined") return;
const debugWindow = window as Window & { __OMNIAI_IMAGE_ROUTE_DEBUG__?: ImageRouteDebugEntry[] };
const previousEntries = Array.isArray(debugWindow.__OMNIAI_IMAGE_ROUTE_DEBUG__)
? debugWindow.__OMNIAI_IMAGE_ROUTE_DEBUG__
: [];
const entry: ImageRouteDebugEntry = { at: new Date().toISOString(), label, ...payload };
debugWindow.__OMNIAI_IMAGE_ROUTE_DEBUG__ = [...previousEntries.slice(-19), entry];
}
@@ -250,67 +250,73 @@ export const aiGenerationClient = {
projectId: input.projectId,
conversationId: input.conversationId,
});
const payload = await serverRequest<ImageTaskCreateResponse>("ai/image", {
const payload = await serverRequest<unknown>("ai/image", {
method: "POST",
body: input,
timeoutMs: TASK_SUBMIT_TIMEOUT_MS,
maxRetries: NON_RETRYING_REQUEST.maxRetries,
fallbackMessage: "Image generation request failed",
});
if (payload.providerDebug) {
emitImageRouteDebug("[ai/image-provider-debug]", payload.providerDebug as Record<string, unknown>);
const parsed = parseImageTaskCreateResponse(payload);
if (parsed.providerDebug) {
emitImageRouteDebug("[ai/image-provider-debug]", parsed.providerDebug as Record<string, unknown>);
}
return payload;
return parsed;
},
async createVideoTask(input: VideoGenInput): Promise<{ taskId: string }> {
return serverRequest<{ taskId: string }>("ai/video", {
const payload = await serverRequest<unknown>("ai/video", {
method: "POST",
body: input,
timeoutMs: TASK_SUBMIT_TIMEOUT_MS,
maxRetries: NON_RETRYING_REQUEST.maxRetries,
fallbackMessage: "Video generation request failed",
});
return parseTaskCreateResponse(payload);
},
async createVideoSuperResolveTask(input: VideoSuperResolveInput): Promise<{ taskId: string }> {
return serverRequest<{ taskId: string }>("ai/video/super-resolve", {
const payload = await serverRequest<unknown>("ai/video/super-resolve", {
method: "POST",
body: input,
timeoutMs: TASK_SUBMIT_TIMEOUT_MS,
maxRetries: NON_RETRYING_REQUEST.maxRetries,
fallbackMessage: "Video super-resolution request failed",
});
return parseTaskCreateResponse(payload);
},
async createEraseSubtitlesTask(input: EraseSubtitlesInput): Promise<{ taskId: string }> {
return serverRequest<{ taskId: string }>("ai/video/erase-subtitles", {
const payload = await serverRequest<unknown>("ai/video/erase-subtitles", {
method: "POST",
body: input,
timeoutMs: TASK_SUBMIT_TIMEOUT_MS,
maxRetries: NON_RETRYING_REQUEST.maxRetries,
fallbackMessage: "Subtitle removal request failed",
});
return parseTaskCreateResponse(payload);
},
async createImageSuperResolveTask(input: ImageSuperResolveInput): Promise<{ taskId: string }> {
return serverRequest<{ taskId: string }>("ai/image/super-resolve", {
const payload = await serverRequest<unknown>("ai/image/super-resolve", {
method: "POST",
body: input,
timeoutMs: TASK_SUBMIT_TIMEOUT_MS,
maxRetries: NON_RETRYING_REQUEST.maxRetries,
fallbackMessage: "Image super-resolution request failed",
});
return parseTaskCreateResponse(payload);
},
async createImageEditTask(input: ImageEditInput): Promise<{ taskId: string }> {
return serverRequest<{ taskId: string }>("ai/image/edit", {
const payload = await serverRequest<unknown>("ai/image/edit", {
method: "POST",
body: input,
timeoutMs: TASK_SUBMIT_TIMEOUT_MS,
maxRetries: NON_RETRYING_REQUEST.maxRetries,
fallbackMessage: "Image edit request failed",
});
return parseTaskCreateResponse(payload);
},
async cancelTask(taskId: string): Promise<void> {
@@ -327,10 +333,11 @@ export const aiGenerationClient = {
},
async getTaskStatus(taskId: string): Promise<AiTaskStatus> {
return serverRequest<AiTaskStatus>(`ai/tasks/${taskId}`, {
const payload = await serverRequest<unknown>(`ai/tasks/${taskId}`, {
timeoutMs: TASK_STATUS_TIMEOUT_MS,
fallbackMessage: "Task status request failed",
});
return parseAiTaskStatus(payload);
},
async downloadTaskResult(taskId: string): Promise<{ blob: Blob; filename?: string; contentType?: string }> {
@@ -360,7 +367,7 @@ export const aiGenerationClient = {
const payload = await serverRequest<unknown>(`ai/tasks${search.toString() ? `?${search}` : ""}`, {
fallbackMessage: "Task history request failed",
});
return extractTaskList(payload).map(toPreviewTask);
return parseAiTaskStatusList(payload).map(toPreviewTask);
} catch (error) {
if (isOptionalApiRouteMissing(error)) {
taskHistoryRouteMissing = true;
@@ -450,7 +457,7 @@ export const aiGenerationClient = {
if (!line.startsWith("data: ")) continue;
try {
const data = JSON.parse(line.slice(6));
onUpdate(data);
onUpdate(parseSseTaskFrame(data));
} catch { /* ignore */ }
}
}
+1
View File
@@ -0,0 +1 @@
export * from "./apiErrorUtils.ts";
+184
View File
@@ -0,0 +1,184 @@
import { describe, it, expect } from "vitest";
import {
parseAiTaskStatus,
parseTaskCreateResponse,
parseImageTaskCreateResponse,
parseAiTaskStatusList,
parseSseTaskFrame,
} from "./dtoParsers";
describe("parseAiTaskStatus", () => {
it("parses a well-formed camelCase DTO", () => {
const result = parseAiTaskStatus({
taskId: "task-1",
type: "video",
status: "running",
progress: 42,
resultUrl: "https://example.com/r.mp4",
error: null,
createdAt: "2026-01-01T00:00:00Z",
updatedAt: "2026-01-01T00:01:00Z",
});
expect(result.taskId).toBe("task-1");
expect(result.type).toBe("video");
expect(result.status).toBe("running");
expect(result.progress).toBe(42);
expect(result.resultUrl).toBe("https://example.com/r.mp4");
});
it("tolerates snake_case field names", () => {
const result = parseAiTaskStatus({
task_id: "task-2",
result_url: "https://example.com/x.png",
created_at: "2026-01-01T00:00:00Z",
updated_at: "2026-01-01T00:00:00Z",
});
expect(result.taskId).toBe("task-2");
expect(result.resultUrl).toBe("https://example.com/x.png");
});
it("falls back to safe defaults for missing fields", () => {
const result = parseAiTaskStatus({});
expect(result.taskId).toBe("");
expect(result.type).toBe("image");
expect(result.status).toBe("failed");
expect(result.progress).toBe(0);
expect(result.resultUrl).toBeNull();
expect(result.error).toBeNull();
});
it("rejects unknown status/type values", () => {
const result = parseAiTaskStatus({ status: "weird", type: "audio" });
expect(result.status).toBe("failed");
expect(result.type).toBe("image");
});
it("clamps progress to [0, 100]", () => {
expect(parseAiTaskStatus({ progress: 150 }).progress).toBe(100);
expect(parseAiTaskStatus({ progress: -10 }).progress).toBe(0);
expect(parseAiTaskStatus({ progress: "not-a-number" }).progress).toBe(0);
});
it("preserves numeric conversationId and nulls others", () => {
expect(parseAiTaskStatus({ conversationId: 7 }).conversationId).toBe(7);
expect(parseAiTaskStatus({ conversation_id: 9 }).conversationId).toBe(9);
expect(parseAiTaskStatus({ conversationId: "nope" }).conversationId).toBeNull();
expect(parseAiTaskStatus({}).conversationId).toBeNull();
});
it("returns empty string for a non-record payload", () => {
const result = parseAiTaskStatus("garbage");
expect(result.taskId).toBe("");
expect(result.status).toBe("failed");
});
});
describe("parseTaskCreateResponse", () => {
it("extracts taskId from a create response", () => {
expect(parseTaskCreateResponse({ taskId: "abc" }).taskId).toBe("abc");
expect(parseTaskCreateResponse({ task_id: "def" }).taskId).toBe("def");
expect(parseTaskCreateResponse({ id: "ghi" }).taskId).toBe("ghi");
});
it("throws when taskId is missing", () => {
expect(() => parseTaskCreateResponse({})).toThrow();
expect(() => parseTaskCreateResponse({ taskId: "" })).toThrow();
expect(() => parseTaskCreateResponse({ taskId: " " })).toThrow();
});
});
describe("parseImageTaskCreateResponse", () => {
it("includes providerDebug when present", () => {
const result = parseImageTaskCreateResponse({
taskId: "img-1",
providerDebug: {
requestedModel: "gpt-image",
effectiveModel: "dall-e-3",
route: ["primary", "fallback"],
candidates: [{ provider: "openai", model: "dall-e-3" }],
},
});
expect(result.taskId).toBe("img-1");
expect(result.providerDebug?.effectiveModel).toBe("dall-e-3");
expect(result.providerDebug?.route).toEqual(["primary", "fallback"]);
expect(result.providerDebug?.candidates?.[0]?.model).toBe("dall-e-3");
});
it("omits providerDebug when absent", () => {
const result = parseImageTaskCreateResponse({ taskId: "img-2" });
expect(result.taskId).toBe("img-2");
expect(result.providerDebug).toBeUndefined();
});
it("tolerates snake_case providerDebug fields", () => {
const result = parseImageTaskCreateResponse({
taskId: "img-3",
provider_debug: { requested_model: "x", primary_provider: "openai" },
});
expect(result.providerDebug?.requestedModel).toBe("x");
expect(result.providerDebug?.primaryProvider).toBe("openai");
});
it("throws when taskId missing even if providerDebug present", () => {
expect(() => parseImageTaskCreateResponse({ providerDebug: {} })).toThrow();
});
});
describe("parseAiTaskStatusList", () => {
it("parses a bare array", () => {
const result = parseAiTaskStatusList([{ taskId: "a" }, { taskId: "b" }]);
expect(result).toHaveLength(2);
expect(result[0].taskId).toBe("a");
});
it("parses an envelope { tasks: [...] }", () => {
const result = parseAiTaskStatusList({ tasks: [{ taskId: "a" }, { task_id: "b" }] });
expect(result).toHaveLength(2);
expect(result[1].taskId).toBe("b");
});
it("parses an envelope { items: [...] }", () => {
const result = parseAiTaskStatusList({ items: [{ taskId: "a" }] });
expect(result).toHaveLength(1);
});
it("drops rows with no taskId rather than crashing", () => {
const result = parseAiTaskStatusList([{ taskId: "keep" }, { status: "running" }, {}]);
expect(result).toHaveLength(1);
expect(result[0].taskId).toBe("keep");
});
it("returns empty array for non-array non-record payload", () => {
expect(parseAiTaskStatusList(null)).toEqual([]);
expect(parseAiTaskStatusList("nope")).toEqual([]);
expect(parseAiTaskStatusList({})).toEqual([]);
});
});
describe("parseSseTaskFrame", () => {
it("parses a well-formed SSE frame", () => {
const frame = parseSseTaskFrame({
taskId: "sse-1",
status: "completed",
progress: 100,
resultUrl: "https://example.com/done.png",
});
expect(frame.taskId).toBe("sse-1");
expect(frame.status).toBe("completed");
expect(frame.progress).toBe(100);
expect(frame.resultUrl).toBe("https://example.com/done.png");
});
it("clamps progress and rejects unknown status", () => {
const frame = parseSseTaskFrame({ taskId: "sse-2", status: "oops", progress: 999 });
expect(frame.status).toBe("failed");
expect(frame.progress).toBe(100);
});
it("handles a non-object payload", () => {
const frame = parseSseTaskFrame("garbage");
expect(frame.taskId).toBe("");
expect(frame.status).toBe("failed");
expect(frame.progress).toBe(0);
});
});
+173
View File
@@ -0,0 +1,173 @@
// DTO 解析层:把后端返回的 unknown 安全地解析成强类型 view model。
// 所有从 serverRequest / SSE / localStorage 进入前端状态的 DTO 都应经过这里的 parser
// 避免 as unknown as / as T 这类静默断言在后端变形时把 undefined/错误类型传到 UI。
//
// helper 与 keyServerClient 里的 toNumber/toStringValue 同构,为避免改动 keyServerClient
// 暂在此自带一份;后续可统一到共享 dtoHelpers 模块。
import { isRecord } from "./serverConnection";
import type { AiTaskStatus, ImageTaskCreateResponse, ImageProviderDebug } from "./aiGenerationClient";
function toNumber(value: unknown, fallback = 0): number {
const numberValue = typeof value === "number" ? value : Number(value);
return Number.isFinite(numberValue) ? numberValue : fallback;
}
function toNullableString(value: unknown): string | null {
if (typeof value !== "string") return null;
const trimmed = value.trim();
return trimmed || null;
}
const TASK_STATUS_VALUES: ReadonlySet<string> = new Set(["pending", "running", "completed", "failed", "cancelled"]);
const TASK_TYPE_VALUES: ReadonlySet<string> = new Set(["image", "video"]);
function normalizeTaskStatusValue(value: unknown): AiTaskStatus["status"] {
return typeof value === "string" && TASK_STATUS_VALUES.has(value)
? (value as AiTaskStatus["status"])
: "failed";
}
function normalizeTaskTypeValue(value: unknown): AiTaskStatus["type"] {
return typeof value === "string" && TASK_TYPE_VALUES.has(value) ? (value as AiTaskStatus["type"]) : "image";
}
interface ProviderDebugCandidate {
provider?: string;
transport?: string;
model?: string;
requestedModel?: string;
billingProvider?: string;
fallbackOf?: string;
}
function normalizeProviderDebugCandidate(raw: unknown): ProviderDebugCandidate {
if (!isRecord(raw)) return {};
return {
provider: toNullableString(raw.provider) ?? undefined,
transport: toNullableString(raw.transport) ?? undefined,
model: toNullableString(raw.model) ?? undefined,
requestedModel: toNullableString(raw.requestedModel ?? raw.requested_model) ?? undefined,
billingProvider: toNullableString(raw.billingProvider ?? raw.billing_provider) ?? undefined,
fallbackOf: toNullableString(raw.fallbackOf ?? raw.fallback_of) ?? undefined,
};
}
function toStringArray(raw: unknown): string[] | undefined {
if (!Array.isArray(raw)) return undefined;
return (raw as unknown[])
.map((item) => toNullableString(item))
.filter((item): item is string => item !== null);
}
function normalizeProviderDebug(raw: unknown): ImageProviderDebug | undefined {
if (!isRecord(raw)) return undefined;
const hasAny =
(raw.requestedModel ?? raw.requested_model) !== undefined ||
(raw.effectiveModel ?? raw.effective_model) !== undefined ||
(raw.primaryProvider ?? raw.primary_provider) !== undefined ||
(raw.fallbackProviders ?? raw.fallback_providers) !== undefined ||
raw.route !== undefined ||
raw.candidates !== undefined;
if (!hasAny) return undefined;
const fallbackProviders = toStringArray(raw.fallbackProviders ?? raw.fallback_providers);
const route = toStringArray(raw.route);
const candidates = Array.isArray(raw.candidates)
? (raw.candidates as unknown[]).map(normalizeProviderDebugCandidate)
: undefined;
return {
requestedModel: toNullableString(raw.requestedModel ?? raw.requested_model) ?? undefined,
effectiveModel: toNullableString(raw.effectiveModel ?? raw.effective_model) ?? undefined,
primaryProvider: toNullableString(raw.primaryProvider ?? raw.primary_provider) ?? undefined,
fallbackProviders,
route,
candidates,
};
}
/**
* Parse a single task status DTO. Returns a well-formed AiTaskStatus with safe
* defaults for any missing/malformed field, so downstream code never sees
* undefined where it expects a value.
*/
export function parseAiTaskStatus(payload: unknown): AiTaskStatus {
const task = isRecord(payload) ? payload : {};
return {
taskId: toNullableString(task.taskId ?? task.task_id ?? task.id) ?? "",
projectId: toNullableString(task.projectId ?? task.project_id) ?? undefined,
conversationId: typeof task.conversationId === "number" || typeof task.conversation_id === "number"
? ((task.conversationId ?? task.conversation_id) as number)
: null,
clientQueueId: toNullableString(task.clientQueueId ?? task.client_queue_id),
type: normalizeTaskTypeValue(task.type),
status: normalizeTaskStatusValue(task.status),
progress: Math.max(0, Math.min(100, toNumber(task.progress))),
resultUrl: toNullableString(task.resultUrl ?? task.result_url),
error: toNullableString(task.error),
params: isRecord(task.params) ? task.params : undefined,
createdAt: toNullableString(task.createdAt ?? task.created_at) ?? "",
updatedAt: toNullableString(task.updatedAt ?? task.updated_at) ?? "",
completedAt: toNullableString(task.completedAt ?? task.completed_at),
};
}
/**
* Parse a task-create response ({ taskId }). Throws if taskId is missing,
* rather than silently returning { taskId: undefined }.
*/
export function parseTaskCreateResponse(payload: unknown): { taskId: string } {
const body = isRecord(payload) ? payload : {};
const taskId = toNullableString(body.taskId ?? body.task_id ?? body.id);
if (!taskId) {
throw new Error("任务创建失败:服务端未返回任务 ID");
}
return { taskId };
}
/**
* Parse an image task-create response, including optional provider debug info.
*/
export function parseImageTaskCreateResponse(payload: unknown): ImageTaskCreateResponse {
const base = parseTaskCreateResponse(payload);
const body = isRecord(payload) ? payload : {};
const providerDebug = normalizeProviderDebug(body.providerDebug ?? body.provider_debug);
return providerDebug ? { ...base, providerDebug } : base;
}
/**
* Parse a task list payload that may be a bare array or an envelope
* ({ tasks | items: [...] }). Malformed elements are dropped, not coerced,
* because a single bad row should not corrupt the whole history list.
*/
export function parseAiTaskStatusList(payload: unknown): AiTaskStatus[] {
let rows: unknown[];
if (Array.isArray(payload)) {
rows = payload;
} else if (isRecord(payload)) {
const nested = payload.tasks ?? payload.items;
rows = Array.isArray(nested) ? nested : [];
} else {
rows = [];
}
// Keep only rows that have a non-empty taskId — empty-id rows are useless
// to the UI and indicate a malformed DTO.
return rows.map(parseAiTaskStatus).filter((task) => task.taskId);
}
/**
* Parse an SSE task frame. SSE data is untyped JSON from the server stream;
* this validates the subset of fields that subscribeTaskStatus forwards.
*/
export function parseSseTaskFrame(payload: unknown): Pick<
AiTaskStatus,
"taskId" | "status" | "progress" | "resultUrl" | "error"
> {
const frame = isRecord(payload) ? payload : {};
return {
taskId: toNullableString(frame.taskId ?? frame.task_id) ?? "",
status: normalizeTaskStatusValue(frame.status),
progress: Math.max(0, Math.min(100, toNumber(frame.progress))),
resultUrl: toNullableString(frame.resultUrl ?? frame.result_url),
error: toNullableString(frame.error),
};
}
+1
View File
@@ -0,0 +1 @@
export * from "./generationRecordClient.ts";
+72
View File
@@ -38,6 +38,45 @@ export interface SaveGenerationRecordResult {
id: string;
}
// 同一 clientRecordId 的保存去重:套图主流程、backgroundTaskRunner、useGenerationTasks
// 三处都可能对同一条终态任务调用 saveGenerationRecordSSE 重复推送 completed 时
// 单个 poller 内也会重复触发。这里做客户端幂等:in-flight 合流 + 成功后短期拦截,
// 避免后端在缺少去重时插入重复记录。
const inFlightSaves = new Map<string, Promise<SaveGenerationRecordResult>>();
const recentlySavedRecords = new Map<string, { savedAt: number; signature: string }>();
const SAVE_DEDUPE_WINDOW_MS = 60_000;
function pruneRecentlySaved(now: number): void {
for (const [id, record] of recentlySavedRecords) {
if (now - record.savedAt > SAVE_DEDUPE_WINDOW_MS) recentlySavedRecords.delete(id);
}
}
function stableJsonStringify(value: unknown): string {
if (value === null || typeof value !== "object") return JSON.stringify(value);
if (Array.isArray(value)) return `[${value.map(stableJsonStringify).join(",")}]`;
const entries = Object.entries(value as Record<string, unknown>)
.filter(([, entryValue]) => entryValue !== undefined)
.sort(([a], [b]) => a.localeCompare(b));
return `{${entries.map(([key, entryValue]) => `${JSON.stringify(key)}:${stableJsonStringify(entryValue)}`).join(",")}}`;
}
function buildSaveSignature(input: SaveGenerationRecordInput): string {
return stableJsonStringify({
tool: input.tool,
mode: input.mode,
title: input.title,
status: input.status,
prompt: input.prompt,
taskIds: input.taskIds,
assets: input.assets,
config: input.config,
result: input.result,
metadata: input.metadata,
createdAt: input.createdAt,
});
}
function readPendingRecords(): SaveGenerationRecordInput[] {
try {
const raw = window.localStorage.getItem(PENDING_RECORDS_KEY);
@@ -60,6 +99,39 @@ function writePendingRecord(input: SaveGenerationRecordInput): void {
}
export async function saveGenerationRecord(input: SaveGenerationRecordInput): Promise<SaveGenerationRecordResult> {
const now = Date.now();
pruneRecentlySaved(now);
const recordId = input.clientRecordId;
const signature = buildSaveSignature(input);
if (recordId) {
const saveKey = `${recordId}:${signature}`;
const inFlight = inFlightSaves.get(saveKey);
if (inFlight) return inFlight;
const savedRecord = recentlySavedRecords.get(recordId);
if (savedRecord && savedRecord.signature === signature && now - savedRecord.savedAt <= SAVE_DEDUPE_WINDOW_MS) {
// 相同 clientRecordId 且 payload 完全一致时才拦截;同一记录的多轮更新需要继续保存。
return { source: "server", id: recordId };
}
}
const promise = saveGenerationRecordInternal(input);
if (recordId) {
const saveKey = `${recordId}:${signature}`;
inFlightSaves.set(saveKey, promise);
void promise
.then((result) => {
if (result.source === "server") recentlySavedRecords.set(recordId, { savedAt: Date.now(), signature });
})
.catch(() => undefined)
.finally(() => {
inFlightSaves.delete(saveKey);
});
}
return promise;
}
async function saveGenerationRecordInternal(input: SaveGenerationRecordInput): Promise<SaveGenerationRecordResult> {
try {
const response = await serverRequest<{ id?: string | number }>("ai/generation-records", {
method: "POST",
+1
View File
@@ -0,0 +1 @@
export * from "./serverConnection.ts";
+17 -3
View File
@@ -82,9 +82,19 @@ function parseStoredSession(raw: string | null): WebUserSession | null {
try {
const parsed = JSON.parse(raw) as unknown;
return isRecord(parsed) && typeof parsed.token === "string" && isRecord(parsed.user)
? (parsed as unknown as WebUserSession)
: null;
// Require token + a user object with at least an id, so a malformed/partial
// cached session does not get cast wholesale into WebUserSession and then
// crash UI code that reads user.id / user.username.
if (!isRecord(parsed) || typeof parsed.token !== "string" || !isRecord(parsed.user)) {
return null;
}
const user = parsed.user;
const userId = user.id ?? user.userId ?? user.user_id;
const username = user.username ?? user.name;
if (userId === undefined || typeof username !== "string" || !username.trim()) {
return null;
}
return parsed as unknown as WebUserSession;
} catch {
return null;
}
@@ -161,7 +171,11 @@ export function clearAllUserStorage(): void {
"omniai-web-profile-ui",
"omniai:more-recent-tools",
"omniai:generation-queue",
"omniai:generation-records.pending",
"omniai:ecommerce-video-workspace",
"omniai-canvas-saved-assets",
"omniai.clone-ai.",
"omniai.ecommerce.",
];
for (let i = window.localStorage.length - 1; i >= 0; i--) {
const key = window.localStorage.key(i);
+1
View File
@@ -0,0 +1 @@
export * from "./taskSubscription.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./webGenerationGateway.ts";
-1
View File
@@ -50,7 +50,6 @@ export const webGenerationGateway = {
const result = await aiGenerationClient.createImageTask({
projectId: params?.projectId,
conversationId: params?.conversationId,
model: "gpt-image-2",
prompt,
ratio: params?.ratio || "16:9",
quality: params?.quality || "1K",
+17
View File
@@ -0,0 +1,17 @@
import type { WebUserSession } from "../types";
interface LocalAvatarProps {
session: WebUserSession;
size?: "sm" | "md" | "lg";
}
export function LocalAvatar({ session, size = "md" }: LocalAvatarProps) {
const displayName = session.user.displayName || session.user.username || "用户";
const label = displayName.trim().slice(0, 1).toUpperCase() || "用";
const avatarUrl = session.user.avatarUrl;
return (
<span className={`local-user-avatar local-user-avatar--${size}`}>
{avatarUrl ? <img src={avatarUrl} alt={displayName} /> : <span>{label}</span>}
</span>
);
}
+195
View File
@@ -0,0 +1,195 @@
import { useEffect, useMemo, useState } from "react";
import {
BugOutlined,
IdcardOutlined,
LoginOutlined,
LogoutOutlined,
PictureOutlined,
UserOutlined,
VideoCameraOutlined,
WalletOutlined,
} from "@ant-design/icons";
import { LocalAvatar } from "./LocalAvatar";
import type { WebUserSession } from "../types";
interface TopbarProps {
session: WebUserSession | null;
usage: { balanceCents: number; imageUsed: number; videoUsed: number };
profileMenuOpen: boolean;
onProfileMenuOpenChange: (open: boolean) => void;
onOpenWorkspace: () => void;
onOpenProfile: () => void;
onOpenAuth: (mode: "login" | "register") => void;
onLogout: () => void;
onBugFeedback: () => void;
}
export function Topbar({
session,
usage,
profileMenuOpen,
onProfileMenuOpenChange,
onOpenWorkspace,
onOpenProfile,
onOpenAuth,
onLogout,
onBugFeedback,
}: TopbarProps) {
const [isTopbarHidden, setIsTopbarHidden] = useState(false);
useEffect(() => {
let restoreTimer: number | undefined;
const handleScroll = (event: Event) => {
if (profileMenuOpen) return;
const target = event.target;
const activeWorkspace = document.querySelector<HTMLElement>(".ecommerce-standalone__page--workspace:not([hidden])");
if (!activeWorkspace) return;
const isWorkspacePreviewScroll =
target instanceof HTMLElement && target.classList.contains("clone-ai-preview") && activeWorkspace.contains(target);
const isPageScroll =
target === document ||
target === document.scrollingElement ||
target === document.documentElement ||
target === document.body;
if (!isWorkspacePreviewScroll && !isPageScroll) return;
setIsTopbarHidden(true);
if (restoreTimer) window.clearTimeout(restoreTimer);
restoreTimer = window.setTimeout(() => {
setIsTopbarHidden(false);
}, 240);
};
window.addEventListener("scroll", handleScroll, { capture: true, passive: true });
return () => {
window.removeEventListener("scroll", handleScroll, { capture: true });
if (restoreTimer) window.clearTimeout(restoreTimer);
};
}, [profileMenuOpen]);
const balance = Math.max(usage.balanceCents, 0) / 100;
const displayName = session?.user.displayName || session?.user.username || "用户";
const actualWorkCount = Math.max(usage.imageUsed + usage.videoUsed, 0);
const shownWorkCount = actualWorkCount;
const avatarMenuStats = useMemo(
() => [
{ icon: <IdcardOutlined />, label: "UID", value: session?.user.id ?? "-" },
{ icon: <WalletOutlined />, label: "积分", value: `${balance.toFixed(2)} 积分` },
{ icon: <PictureOutlined />, label: "图片", value: usage.imageUsed },
{ icon: <VideoCameraOutlined />, label: "视频", value: usage.videoUsed },
{ icon: <PictureOutlined />, label: "作品", value: shownWorkCount },
],
[balance, session?.user.id, shownWorkCount, usage.imageUsed, usage.videoUsed],
);
return (
<header
className="ecommerce-standalone__topbar"
data-scroll-hidden={isTopbarHidden ? "true" : "false"}
style={{
position: "fixed",
top: 0,
left: 0,
right: 0,
zIndex: 1000,
pointerEvents: "none",
background: "transparent",
border: 0,
boxShadow: "none",
backdropFilter: "none",
WebkitBackdropFilter: "none",
}}
>
<button
type="button"
className="ecommerce-standalone__brand"
style={{ pointerEvents: "auto" }}
onClick={onOpenWorkspace}
>
<span className="ecommerce-standalone__logo" aria-hidden="true">
<img src="https://stringtest.oss-cn-hangzhou.aliyuncs.com/logo.png" alt="" />
</span>
<strong>OmniAI </strong>
</button>
<div className="ecommerce-standalone__account">
{session ? (
<div className="ecommerce-profile-menu">
<button
type="button"
className="ecommerce-profile-menu__trigger"
style={{ pointerEvents: "auto" }}
onClick={() => onProfileMenuOpenChange(!profileMenuOpen)}
aria-haspopup="dialog"
aria-expanded={profileMenuOpen}
>
<span className="ecommerce-standalone__credits">
{(Math.max(usage.balanceCents, 0) / 100).toFixed(2)}
</span>
<LocalAvatar session={session} size="sm" />
<span className="ecommerce-profile-menu__name">{displayName}</span>
</button>
{profileMenuOpen ? (
<>
<button
type="button"
className="ecommerce-profile-popover__backdrop"
aria-label="关闭账户信息"
onClick={() => onProfileMenuOpenChange(false)}
/>
<section className="ecommerce-profile-popover" role="dialog" aria-label="账户信息">
<div className="ecommerce-profile-popover__head">
<LocalAvatar session={session} size="md" />
<div>
<strong>{displayName}</strong>
<span>{session.user.username}</span>
</div>
</div>
<dl className="ecommerce-profile-popover__stats">
{avatarMenuStats.map((item) => (
<div key={item.label}>
<dt>
{item.icon}
{item.label}
</dt>
<dd>{item.value}</dd>
</div>
))}
</dl>
<div className="ecommerce-profile-popover__actions">
<button type="button" className="is-primary" onClick={onOpenProfile}>
<UserOutlined />
</button>
<button type="button" onClick={onBugFeedback}>
<BugOutlined />
Bug
</button>
<button type="button" className="is-danger" onClick={onLogout}>
<LogoutOutlined />
退
</button>
</div>
</section>
</>
) : null}
</div>
) : (
<button
type="button"
className="ecommerce-standalone__login-button"
style={{ pointerEvents: "auto" }}
onClick={() => onOpenAuth("login")}
>
<LoginOutlined />
<span> / </span>
</button>
)}
</div>
</header>
);
}
+1
View File
@@ -0,0 +1 @@
export * from "./toastStore.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./ossAssets.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./workflows.ts";
+2
View File
@@ -0,0 +1,2 @@
export { default } from "./EcommercePage.tsx";
export * from "./EcommercePage.tsx";
File diff suppressed because it is too large Load Diff
@@ -1,10 +1,11 @@
import { useSmoothedProgress } from "../../hooks/useSmoothedProgress";
import type { ReactNode } from "react";
interface EcommerceProgressBarProps {
status: "idle" | "generating" | "done" | "failed" | string;
label?: string;
onCancel?: () => void;
/** 0-100 真实进度。传入时进度条按真实值推进;省略时按状态做平滑蠕动。 */
progress?: number;
}
function mapStatus(status: string): "running" | "completed" | "failed" {
@@ -14,9 +15,13 @@ function mapStatus(status: string): "running" | "completed" | "failed" {
return "running";
}
export function EcommerceProgressBar({ status, label, onCancel }: EcommerceProgressBarProps) {
const progress = mapStatus(status) === "running" ? 50 : 100;
const smoothed = useSmoothedProgress(progress, mapStatus(status));
export function EcommerceProgressBar({ status, label, onCancel, progress }: EcommerceProgressBarProps) {
const mapped = mapStatus(status);
// running 时目标取「真实进度」与兜底值 88 的较大者:有真实进度则跟随推进,
// 后端不推中间进度时也由平滑器持续蠕动到高位,不再卡死在 75%。
const realProgress = typeof progress === "number" ? Math.max(0, Math.min(100, progress)) : 0;
const target = mapped === "running" ? Math.max(realProgress, 88) : 100;
const smoothed = useSmoothedProgress(target, mapped);
if (status === "idle") return null;
@@ -1,4 +1,4 @@
import { Fragment, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Fragment, useCallback, useEffect, useMemo, useRef, useState } from "react";
import "../../styles/pages/ecommerce-video.css";
import {
CloseOutlined,
@@ -11,7 +11,12 @@ import {
SendOutlined,
StopOutlined,
} from "@ant-design/icons";
import { runVideoPlan, renderSceneImage, renderScene, buildSceneTasks, saveVideoHistory } from "./ecommerceVideoService";
import {
runVideoPlan,
buildSceneTasks,
saveVideoHistory,
buildComplianceFailureMessage,
} from "./ecommerceVideoService";
import {
PLAN_STEP_LABELS,
PLAN_STEPS_DISPLAY,
@@ -22,7 +27,6 @@ import {
type PlanStep,
} from "./ecommerceVideoTypes";
import type { AdVideoUserConfig } from "../../api/adVideoPlanClient";
import { ServerRequestError } from "../../api/serverConnection";
import { saveToolResultToLocal, addToolResultToAssetLibrary } from "../workbench/toolResultActions";
import { useAppStore } from "../../stores";
import { useGenerationTasks } from "../../hooks/useGenerationTasks";
@@ -32,6 +36,7 @@ import {
clearEcommerceVideoState,
} from "./ecommerceVideoKeepalive";
import { saveUnifiedEcommerceGenerationRecord } from "./ecommerceGenerationPersistence";
import { useVideoSceneRunner } from "./useVideoSceneRunner";
interface EcommerceVideoWorkspaceProps {
isAuthenticated: boolean;
@@ -70,9 +75,11 @@ function buildInputFingerprint(input: {
durationSeconds: number;
resolution: string;
}): string {
const imageCount = input.productImageDataUrls.length;
const imageSignature = input.productImageDataUrls
.map((source) => `${source.length}:${hashString(source)}`)
.join("|");
return hashString([
String(imageCount),
imageSignature,
input.requirement.trim(),
input.platform,
input.aspectRatio,
@@ -81,6 +88,10 @@ function buildInputFingerprint(input: {
].join("::"));
}
function planAllowsVideoGeneration(plan: EcommerceVideoPlanResult | null): boolean {
return plan?.compliance.allow_video_generation !== false;
}
function mapResolutionToQuality(res: string): "720P" | "1080P" {
return res.includes("720") ? "720P" : "1080P";
}
@@ -124,8 +135,6 @@ export default function EcommerceVideoWorkspace({
const [actionNotice, setActionNotice] = useState<string | null>(null);
const [previewMedia, setPreviewMedia] = useState<{ url: string; type: "image" | "video" } | null>(null);
const [flowZoom, setFlowZoom] = useState(1);
const abortControllerRef = useRef<AbortController | null>(null);
const renderAbortRef = useRef({ current: false });
const actionNoticeTimerRef = useRef<number | null>(null);
const setView = useAppStore((s) => s.setView);
const keepaliveRestoredFingerprintRef = useRef<string | null>(null);
@@ -137,6 +146,28 @@ export default function EcommerceVideoWorkspace({
[productImageDataUrls, requirement, platform, aspectRatio, durationSeconds, resolution],
);
const {
abortControllerRef,
renderAbortRef,
runImagePhase,
runVideoPhase,
resumePolling,
cancel,
retryScene,
} = useVideoSceneRunner({
inputFingerprint,
planResult,
completedSteps,
sourceImageUrls,
aspectRatio,
resolution,
generation: generation as unknown as Parameters<typeof useVideoSceneRunner>[0]["generation"],
sceneStoreIdMap,
onScenesChange: setScenes,
onStageChange: setStage,
onError: setError,
});
// ── Keep-alive: restore saved state on mount ─────────────
useEffect(() => {
if (keepaliveRestoredFingerprintRef.current === inputFingerprint) return;
@@ -163,11 +194,15 @@ export default function EcommerceVideoWorkspace({
useEffect(() => {
const delay = 600;
if (stage === "planned" && planResult && scenes.length > 0) {
const timer = setTimeout(() => { void handleGenerateImages(); }, delay);
if (!planAllowsVideoGeneration(planResult)) {
setError(buildComplianceFailureMessage(planResult.compliance));
return;
}
const timer = setTimeout(() => { void runImagePhase(scenes); }, delay);
return () => clearTimeout(timer);
}
if (stage === "imaged" && scenes.every((s) => s.imageUrl)) {
const timer = setTimeout(() => { void handleRenderVideos(); }, delay);
const timer = setTimeout(() => { void runVideoPhase(scenes); }, delay);
return () => clearTimeout(timer);
}
}, [stage, scenes, planResult]);
@@ -284,82 +319,11 @@ export default function EcommerceVideoWorkspace({
useEffect(() => {
if (keepalivePollingStartedRef.current) return;
if (!scenes.length || stage === "idle" || stage === "cancelled" || stage === "completed") return;
const hasRunningScenes = scenes.some((s) => s.status === "running" || s.status === "pending");
if (!hasRunningScenes) return;
keepalivePollingStartedRef.current = true;
// Resume polling for image generation tasks
if (stage === "imaging") {
renderAbortRef.current = { current: false };
void (async () => {
for (const scene of scenes) {
if (renderAbortRef.current.current) break;
if (scene.status !== "running" && scene.status !== "pending") continue;
if (!scene.imageTaskId) continue;
try {
const { waitForTask } = await import("../../api/taskSubscription");
const resultUrl = await waitForTask(scene.imageTaskId, {
abortRef: renderAbortRef.current,
onProgress: (e) =>
setScenes((prev) => prev.map((s) => (s.sceneId === scene.sceneId ? { ...s, progress: e.progress } : s))),
});
if (resultUrl) {
setScenes((prev) =>
prev.map((s) => (s.sceneId === scene.sceneId ? { ...s, status: "idle", progress: 100, imageUrl: resultUrl } : s)),
);
}
} catch {
setScenes((prev) =>
prev.map((s) => (s.sceneId === scene.sceneId ? { ...s, status: "idle", error: "恢复任务失败" } : s)),
);
}
}
setScenes((current) => {
const allImaged = current.every((s) => s.imageUrl);
if (allImaged) setStage("imaged");
return current;
});
})();
}
// Resume polling for video rendering tasks
if (stage === "rendering") {
renderAbortRef.current = { current: false };
void (async () => {
for (const scene of scenes) {
if (renderAbortRef.current.current) break;
if (scene.status !== "running" && scene.status !== "pending") continue;
if (!scene.taskId) continue;
try {
const { waitForTask } = await import("../../api/taskSubscription");
const resultUrl = await waitForTask(scene.taskId, {
abortRef: renderAbortRef.current,
onProgress: (e) =>
setScenes((prev) => prev.map((s) => (s.sceneId === scene.sceneId ? { ...s, progress: e.progress } : s))),
});
if (resultUrl) {
setScenes((prev) =>
prev.map((s) =>
s.sceneId === scene.sceneId ? { ...s, status: "completed", progress: 100, resultUrl: resultUrl } : s,
),
);
}
} catch {
setScenes((prev) =>
prev.map((s) => (s.sceneId === scene.sceneId ? { ...s, status: "failed", error: "恢复任务失败" } : s)),
);
}
}
setScenes((current) => {
const hasFailed = current.some((s) => s.status === "failed");
const allDone = current.every((s) => s.status === "completed" || s.status === "failed");
if (allDone) setStage(hasFailed ? "partial_failed" : "completed");
return current;
});
})();
}
}, [scenes, stage]);
void resumePolling(stage, scenes);
}, [scenes, stage, resumePolling]);
// Note: keep-alive is NOT cleared on completion — results persist across page switches.
// Only cleared when user explicitly starts a new plan via handlePlan.
@@ -468,6 +432,7 @@ export default function EcommerceVideoWorkspace({
let liveCompletedSteps: PlanStep[] = resume
? ALL_STEPS.filter((s) => stepCompletedFromProgress(s, resume))
: [];
let liveCurrentStep: PlanStep | null = null;
const persist = (stageNow: EcommerceVideoStage) => {
saveEcommerceVideoState({
inputFingerprint,
@@ -484,7 +449,10 @@ export default function EcommerceVideoWorkspace({
const result = await runVideoPlan(
productImageSources, requirement, buildConfig(),
{
onStepStart: (step) => setCurrentStep(step),
onStepStart: (step) => {
liveCurrentStep = step;
setCurrentStep(step);
},
onStepDone: (step) => {
liveCompletedSteps = [...liveCompletedSteps, step];
setCompletedSteps((prev) => [...prev, step]);
@@ -517,7 +485,7 @@ export default function EcommerceVideoWorkspace({
const message = err instanceof Error ? err.message : "策划失败";
setError(message);
// Mark the step that was in-progress as failed so user can resume
setFailedStep((prev) => prev || currentStep);
setFailedStep((prev) => prev || liveCurrentStep);
setStage("idle");
// Persist partial progress so the user can resume after a page switch
persist("idle");
@@ -526,8 +494,8 @@ export default function EcommerceVideoWorkspace({
const handlePlan = async () => {
if (!isAuthenticated) { onRequestLogin?.(); return; }
if (!productImageDataUrls.length && !requirement.trim()) {
setError("请先上传品图片或填写商品说明"); return;
if (!productImageDataUrls.length) {
setError("请先上传品图片"); return;
}
await runPlanFlow(null);
};
@@ -538,140 +506,9 @@ export default function EcommerceVideoWorkspace({
await runPlanFlow(planProgress);
};
// ── Phase 2: Image generation per scene ──────────────────────
const handleGenerateImages = async () => {
if (!planResult || !scenes.length) return;
setStage("imaging"); setError(null);
renderAbortRef.current = { current: false };
const ratio = aspectRatio.includes("9:16") || aspectRatio.includes("916") ? "9:16"
: aspectRatio.includes("16:9") || aspectRatio.includes("169") ? "16:9"
: "1:1";
let currentScenes = [...scenes];
const persistScenes = (next: EcommerceVideoSceneTask[]) => {
currentScenes = next;
setScenes(next);
saveEcommerceVideoState({ inputFingerprint, stage: "imaging", completedSteps, planResult, scenes: next, sourceImageUrls });
};
// Only redo scenes missing imageUrl — preserves successfully generated images on partial retry
const scenesToProcess = currentScenes.filter((s) => !s.imageUrl);
if (!scenesToProcess.length) { setStage("imaged"); return; }
for (const scene of scenesToProcess) {
if (renderAbortRef.current.current) break;
persistScenes(currentScenes.map((s) => s.sceneId === scene.sceneId ? { ...s, status: "pending", error: undefined } : s));
try {
await renderSceneImage(
{ sceneId: scene.sceneId, prompt: scene.prompt, aspectRatio: ratio, productImageUrls: sourceImageUrls },
{
onSceneImageSubmitted: (id, taskId) => {
persistScenes(currentScenes.map((s) => s.sceneId === id ? { ...s, imageTaskId: taskId, status: "running" } : s));
const storeId = generation.submitTask({ title: `分镜${id}图片`, type: "image", status: "running", progress: 0, prompt: scene.prompt, sourceView: "ecommerce", taskId, params: { sceneId: id, phase: "imaging" } });
sceneStoreIdMap.current.set(id, storeId);
},
onSceneImageProgress: (id, progress) => persistScenes(currentScenes.map((s) => s.sceneId === id ? { ...s, progress } : s)),
onSceneImageCompleted: (id, url) => {
persistScenes(currentScenes.map((s) => s.sceneId === id ? { ...s, status: "idle", progress: 100, imageUrl: url } : s));
const sid = sceneStoreIdMap.current.get(id);
if (sid) generation.markCompleted(sid, url);
},
onSceneImageFailed: (id, err2) => {
persistScenes(currentScenes.map((s) => s.sceneId === id ? { ...s, status: "idle", error: err2 } : s));
const sid = sceneStoreIdMap.current.get(id);
if (sid) generation.markFailed(sid, err2);
},
},
renderAbortRef.current,
);
} catch (err) {
const message = err instanceof Error ? err.message : "图片生成失败";
persistScenes(currentScenes.map((s) => s.sceneId === scene.sceneId ? { ...s, status: "idle", error: message } : s));
}
}
const allHaveImages = currentScenes.every((s) => s.imageUrl);
const finalStage = allHaveImages ? "imaged" as const : "partial_failed" as const;
setStage(finalStage);
saveEcommerceVideoState({ inputFingerprint, stage: finalStage, completedSteps, planResult, scenes: currentScenes, sourceImageUrls });
};
// ── Phase 3: Video rendering from generated images ──────────
const handleRenderVideos = async () => {
if (!scenes.length) return;
if (!scenes.some((s) => s.imageUrl)) { setError("请先生成分镜图片"); return; }
setStage("rendering"); setError(null);
renderAbortRef.current = { current: false };
const quality = mapResolutionToQuality(resolution);
let currentScenes = [...scenes];
const persistScenes = (next: EcommerceVideoSceneTask[]) => {
currentScenes = next;
setScenes(next);
saveEcommerceVideoState({ inputFingerprint, stage: "rendering", completedSteps, planResult, scenes: next, sourceImageUrls });
};
// Only render scenes that haven't completed yet — preserves successful videos on partial retry
const scenesToProcess = currentScenes.filter((s) => s.imageUrl && s.status !== "completed");
if (!scenesToProcess.length) { setStage(currentScenes.every((s) => s.status === "completed") ? "completed" : "partial_failed"); return; }
for (const scene of scenesToProcess) {
if (renderAbortRef.current.current) break;
if (!scene.imageUrl) continue;
persistScenes(currentScenes.map((s) => s.sceneId === scene.sceneId ? { ...s, status: "pending", error: undefined } : s));
try {
await renderScene(
{ sceneId: scene.sceneId, prompt: scene.prompt, durationSeconds: scene.durationSeconds, imageUrl: scene.imageUrl, productImageUrls: sourceImageUrls, aspectRatio, resolution: quality },
{
onSceneSubmitted: (id, taskId) => {
persistScenes(currentScenes.map((s) => s.sceneId === id ? { ...s, taskId, status: "running" } : s));
const storeId = generation.submitTask({ title: `分镜${id}视频`, type: "video", status: "running", progress: 0, prompt: scene.prompt, sourceView: "ecommerce", taskId, params: { sceneId: id, phase: "rendering" } });
sceneStoreIdMap.current.set(id, storeId);
},
onSceneProgress: (id, progress) => persistScenes(currentScenes.map((s) => s.sceneId === id ? { ...s, progress } : s)),
onSceneCompleted: (id, url) => {
persistScenes(currentScenes.map((s) => s.sceneId === id ? { ...s, status: "completed", progress: 100, resultUrl: url } : s));
const sid = sceneStoreIdMap.current.get(id);
if (sid) generation.markCompleted(sid, url);
},
onSceneFailed: (id, err2) => {
persistScenes(currentScenes.map((s) => s.sceneId === id ? { ...s, status: "failed", error: err2 } : s));
const sid = sceneStoreIdMap.current.get(id);
if (sid) generation.markFailed(sid, err2);
},
},
renderAbortRef.current,
);
} catch (err) {
const msg = err instanceof Error ? err.message : "生成失败";
const isPayment = err instanceof ServerRequestError && err.status === 402;
persistScenes(currentScenes.map((s) => s.sceneId === scene.sceneId ? { ...s, status: "failed", error: isPayment ? "余额不足,请充值后继续" : msg } : s));
if (isPayment) { setError("余额不足,请充值后再生成视频"); renderAbortRef.current.current = true; break; }
}
}
const hasFailed = currentScenes.some((s) => s.status === "failed");
const allDone = currentScenes.every((s) => s.status === "completed" || s.status === "failed");
const finalStage = allDone ? (hasFailed ? "partial_failed" as const : "completed" as const) : "rendering" as const;
setScenes(currentScenes);
setStage(finalStage);
saveEcommerceVideoState({ inputFingerprint, stage: finalStage, completedSteps, planResult, scenes: currentScenes, sourceImageUrls });
};
const handleCancel = () => { abortControllerRef.current?.abort(); renderAbortRef.current.current = true; setStage("cancelled"); };
const handleRetryScene = async (scene: EcommerceVideoSceneTask) => {
if (!scene.imageUrl) return;
setScenes((prev) => prev.map((s) => s.sceneId === scene.sceneId ? { ...s, status: "pending", error: undefined } : s));
try {
await renderScene(
{ sceneId: scene.sceneId, prompt: scene.prompt, durationSeconds: scene.durationSeconds, imageUrl: scene.imageUrl!, productImageUrls: sourceImageUrls, aspectRatio, resolution: mapResolutionToQuality(resolution) },
{
onSceneSubmitted: (id, taskId) => setScenes((prev) => prev.map((s) => s.sceneId === id ? { ...s, taskId, status: "running" } : s)),
onSceneProgress: (id, progress) => setScenes((prev) => prev.map((s) => s.sceneId === id ? { ...s, progress } : s)),
onSceneCompleted: (id, url) => setScenes((prev) => prev.map((s) => s.sceneId === id ? { ...s, status: "completed", progress: 100, resultUrl: url } : s)),
onSceneFailed: (id, err2) => setScenes((prev) => prev.map((s) => s.sceneId === id ? { ...s, status: "failed", error: err2 } : s)),
},
renderAbortRef.current,
);
} catch (err) {
setScenes((prev) => prev.map((s) => s.sceneId === scene.sceneId ? { ...s, status: "failed", error: (err as Error).message } : s));
}
};
// ── Derived state ───────────────────────────────────────────
@@ -721,13 +558,13 @@ export default function EcommerceVideoWorkspace({
) : null}
{stage === "planned" || stage === "imaged" ? (
<button type="button" className="ecom-video-flow-action ecom-video-flow-action--ghost"
onClick={() => void handleGenerateImages()} title={stage === "imaged" ? "重新生成分镜图" : "生成图片"}>
onClick={() => void runImagePhase(scenes)} title={stage === "imaged" ? "重新生成分镜图" : "生成图片"}>
{stage === "imaged" ? <ReloadOutlined /> : <SendOutlined />}
</button>
) : null}
{stage === "imaged" || (stage === "partial_failed" && imagedScenes.length > 0) ? (
<button type="button" className="ecom-video-flow-action ecom-video-flow-action--ghost"
onClick={() => void handleRenderVideos()} title={stage === "partial_failed" ? "重新生成失败的视频" : "生成视频"}>
onClick={() => void runVideoPhase(scenes)} title={stage === "partial_failed" ? "重新生成失败的视频" : "生成视频"}>
<SendOutlined />
</button>
) : null}
@@ -741,7 +578,7 @@ export default function EcommerceVideoWorkspace({
<span className="ecom-video-flowbar__stage-label"><LoadingOutlined /> </span>
) : null}
{stage === "planning" || stage === "imaging" || stage === "rendering" ? (
<button type="button" className="ecom-video-flow-action ecom-video-flow-action--danger" onClick={handleCancel} title="终止">
<button type="button" className="ecom-video-flow-action ecom-video-flow-action--danger" onClick={cancel} title="终止">
<StopOutlined />
</button>
) : null}
@@ -830,7 +667,7 @@ export default function EcommerceVideoWorkspace({
<span className="ecom-video-tree-node__tag">{scene.sceneId}</span>
{vidFailed ? (
<button type="button" className="ecom-video-tree-node__retry"
onClick={(e) => { e.stopPropagation(); void handleRetryScene(scene); }}
onClick={(e) => { e.stopPropagation(); void retryScene(scene); }}
title="重试此镜头">
<ReloadOutlined />
</button>
@@ -0,0 +1 @@
export * from "./ecommerceGenerationPersistence.ts";
@@ -0,0 +1 @@
export * from "./ecommerceImageValidation.ts";
@@ -0,0 +1,90 @@
import { describe, it, expect } from "vitest";
import {
validateEcommerceImageFiles,
summarizeRejectedImages,
normalizeEcommerceImageMime,
ECOMMERCE_MAX_IMAGE_BYTES,
} from "./ecommerceImageValidation";
function makeFile(name: string, type: string, size: number): File {
return new File([new Uint8Array(size)], name, { type });
}
describe("validateEcommerceImageFiles", () => {
it("accepts supported types under the size limit", () => {
const result = validateEcommerceImageFiles([
makeFile("a.png", "image/png", 1024),
makeFile("b.jpg", "image/jpeg", 1024),
makeFile("c.webp", "image/webp", 1024),
makeFile("d.gif", "image/gif", 1024),
]);
expect(result.accepted).toHaveLength(4);
expect(result.rejected).toHaveLength(0);
});
it("rejects unsupported mime types", () => {
const result = validateEcommerceImageFiles([makeFile("x.bmp", "image/bmp", 1024)]);
expect(result.accepted).toHaveLength(0);
expect(result.rejected[0]).toMatchObject({ name: "x.bmp", reason: "不支持的图片格式" });
});
it("rejects files over 10MB", () => {
const result = validateEcommerceImageFiles([
makeFile("big.png", "image/png", ECOMMERCE_MAX_IMAGE_BYTES + 1),
]);
expect(result.accepted).toHaveLength(0);
expect(result.rejected[0]).toMatchObject({ name: "big.png", reason: "图片超过 10MB" });
});
it("accepts exactly 10MB (boundary)", () => {
const result = validateEcommerceImageFiles([
makeFile("edge.png", "image/png", ECOMMERCE_MAX_IMAGE_BYTES),
]);
expect(result.accepted).toHaveLength(1);
});
it("partitions a mixed batch", () => {
const result = validateEcommerceImageFiles([
makeFile("ok.png", "image/png", 100),
makeFile("bad.bmp", "image/bmp", 100),
makeFile("huge.jpg", "image/jpeg", ECOMMERCE_MAX_IMAGE_BYTES + 1),
]);
expect(result.accepted).toHaveLength(1);
expect(result.rejected).toHaveLength(2);
});
});
describe("summarizeRejectedImages", () => {
it("returns empty string for no rejections", () => {
expect(summarizeRejectedImages([])).toBe("");
});
it("summarizes a single rejection", () => {
expect(summarizeRejectedImages([{ name: "a.bmp", reason: "不支持的图片格式" }])).toBe(
"a.bmp 已跳过:不支持的图片格式",
);
});
it("appends count suffix for multiple rejections", () => {
const summary = summarizeRejectedImages([
{ name: "a.bmp", reason: "不支持的图片格式" },
{ name: "b.bmp", reason: "不支持的图片格式" },
]);
expect(summary).toBe("a.bmp 等 2 个文件 已跳过:不支持的图片格式");
});
});
describe("normalizeEcommerceImageMime", () => {
it("passes through supported types", () => {
expect(normalizeEcommerceImageMime("image/png")).toBe("image/png");
expect(normalizeEcommerceImageMime("image/jpeg")).toBe("image/jpeg");
expect(normalizeEcommerceImageMime("image/webp")).toBe("image/webp");
expect(normalizeEcommerceImageMime("image/gif")).toBe("image/gif");
});
it("falls back to image/png for unsupported or empty types", () => {
expect(normalizeEcommerceImageMime("image/bmp")).toBe("image/png");
expect(normalizeEcommerceImageMime("")).toBe("image/png");
expect(normalizeEcommerceImageMime("application/octet-stream")).toBe("image/png");
});
});
@@ -0,0 +1 @@
export * from "./ecommerceTemplates.ts";
@@ -11,6 +11,7 @@ import {
import { aiGenerationClient } from "../../api/aiGenerationClient";
import { serverRequest } from "../../api/serverConnection";
import { waitForTask } from "../../api/taskSubscription";
import { ENTERPRISE_DEFAULT_VIDEO_MODEL } from "../../utils/enterpriseVideoPolicy";
import { resolveVideoRequestModel } from "../../utils/resolveVideoModel";
import { ecommerceOssScopes } from "./ecommerceGenerationPersistence";
import { normalizeEcommerceImageMime } from "./ecommerceImageValidation";
@@ -130,6 +131,18 @@ export interface PlanCallbacks {
const LOCAL_PREVIEW_MISSING_FILE_MESSAGE = "Please re-upload the product image before generating the short video.";
export function buildComplianceFailureMessage(compliance: NonNullable<EcommerceVideoPlanProgress["compliance"]>): string {
const issues = compliance.issues
.slice(0, 3)
.map((issue) => [issue.field, issue.problem, issue.suggestion].filter(Boolean).join(""))
.filter(Boolean)
.join("");
return issues
? `合规检查未通过,已停止生成。${issues}`
: "合规检查未通过,已停止生成。请修改商品说明或广告文案后重试。";
}
function readBlobAsDataUrl(blob: Blob): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
@@ -271,6 +284,10 @@ export async function runVideoPlan(
emit();
}
if (progress.compliance.allow_video_generation === false) {
throw new Error(buildComplianceFailureMessage(progress.compliance));
}
return {
imageUrls: progress.imageUrls!,
imageDescription: progress.imageDescription,
@@ -303,7 +320,6 @@ export async function renderSceneImage(
abortRef: { current: boolean },
): Promise<void> {
const { taskId } = await aiGenerationClient.createImageTask({
model: "gpt-image-2",
prompt: input.prompt,
ratio: input.aspectRatio,
quality: "2K",
@@ -315,7 +331,6 @@ export async function renderSceneImage(
const resultUrl = await waitForTask(taskId, {
abortRef,
kind: "image",
model: "gpt-image-2",
onProgress: (e) => callbacks.onSceneImageProgress(input.sceneId, e.progress),
});
@@ -351,7 +366,7 @@ export async function renderScene(
): Promise<void> {
const allReferenceUrls = [...input.productImageUrls, input.imageUrl];
const model = resolveVideoRequestModel({
model: input.model || "happyhorse-1.0",
model: input.model || ENTERPRISE_DEFAULT_VIDEO_MODEL,
referenceUrls: allReferenceUrls,
});
@@ -381,108 +381,6 @@ export default function EcommerceClonePanel({
</section>
) : null}
{cloneOutput === "hot" ? (
<section className="clone-ai-replicate-panel" aria-label="爆款图复刻设置">
<div className="clone-ai-dynamic-head">
<strong></strong>
<span></span>
</div>
<div className="clone-ai-replicate-section">
<span className="clone-ai-replicate-title"></span>
<div className="clone-ai-replicate-tabs" role="tablist" aria-label="参考内容来源">
<button
type="button"
className={cloneReferenceMode === "upload" ? "is-active" : ""}
aria-selected={cloneReferenceMode === "upload"}
onClick={() => setCloneReferenceMode("upload")}
>
</button>
<button
type="button"
className={cloneReferenceMode === "link" ? "is-active" : ""}
aria-selected={cloneReferenceMode === "link"}
onClick={() => setCloneReferenceMode("link")}
>
</button>
</div>
{cloneReferenceMode === "upload" ? (
<button
type="button"
className={`clone-ai-replicate-upload${isCloneReferenceDragging ? " is-dragging" : ""}${cloneReferenceImages.length ? " has-files" : ""}`}
onClick={() => cloneReferenceInputRef.current?.click()}
onDragOver={handleCloneReferenceDragOver}
onDragLeave={handleCloneReferenceDragLeave}
onDrop={handleCloneReferenceDrop}
>
{cloneReferenceImages.length ? (
<>
<div className="clone-ai-replicate-files">
{cloneReferenceImages.map((item) => (
<figure
key={item.id}
className="clone-ai-replicate-file"
onMouseEnter={(e) => handleFileMouseEnter(item.src, e)}
onMouseLeave={handleFileMouseLeave}
>
<img src={item.src} alt="" />
</figure>
))}
</div>
<span className="clone-ai-replicate-add-more">
<CloudUploadOutlined />
</span>
</>
) : (
<span>
<CloudUploadOutlined />
<span className="clone-ai-replicate-upload-text"></span>
</span>
)}
<em>{cloneReferenceImages.length ? `已选 ${cloneReferenceImages.length}/${maxCloneReferenceImages}` : `最多 ${maxCloneReferenceImages}`}</em>
{isCloneReferenceDragging ? (
<div className="clone-ai-replicate-upload-overlay">
<CloudUploadOutlined />
<span></span>
</div>
) : null}
</button>
) : (
<label className="clone-ai-replicate-link">
<input placeholder="粘贴商品图或详情页链接" />
</label>
)}
<input
ref={cloneReferenceInputRef}
type="file"
accept="image/jpeg,image/png,image/webp"
multiple
onChange={handleCloneReferenceUpload}
aria-label="上传参考图片"
/>
</div>
<div className="clone-ai-replicate-section">
<span className="clone-ai-replicate-title"></span>
<div className="clone-ai-replicate-levels" role="toolbar" aria-label="复刻程度">
{cloneReplicateLevelOptions.map((option) => (
<button
key={option.key}
type="button"
className={cloneReplicateLevel === option.key ? "is-active" : ""}
aria-pressed={cloneReplicateLevel === option.key}
onClick={() => setCloneReplicateLevel(option.key)}
>
<strong>{option.title}</strong>
<span>{option.desc}</span>
</button>
))}
</div>
</div>
</section>
) : null}
{cloneOutput === "set" ? (
<section className="clone-ai-count-panel" aria-label="套图图片数量">
<div className="clone-ai-dynamic-head">
@@ -0,0 +1,480 @@
// 视频场景任务编排 hook。
// 从 EcommerceVideoWorkspace.tsx 抽出,封装"分镜图片生成 / 视频渲染 / 恢复轮询 / 取消"
// 四类场景任务的执行逻辑,消除组件内 persistScenes 闭包的重复。
//
// 运行时行为与原组件逻辑等价(setScenes/setStage/saveEcommerceVideoState 的调用顺序和参数不变);
// 抽离目的是建立逻辑边界,让 resume 与正常执行共享同一套遍历。
import { useCallback, useRef } from "react";
import type { MutableRefObject } from "react";
import {
renderSceneImage,
renderScene,
} from "./ecommerceVideoService";
import { waitForTask } from "../../api/taskSubscription";
import { ServerRequestError } from "../../api/serverConnection";
import { ENTERPRISE_DEFAULT_VIDEO_MODEL } from "../../utils/enterpriseVideoPolicy";
import { resolveVideoRequestModel } from "../../utils/resolveVideoModel";
import {
saveEcommerceVideoState,
} from "./ecommerceVideoKeepalive";
import type {
EcommerceVideoSceneTask,
EcommerceVideoStage,
EcommerceVideoPlanResult,
PlanStep,
} from "./ecommerceVideoTypes";
type SetStateAction<T> = T | ((prev: T) => T);
export interface VideoSceneRunnerContext {
inputFingerprint: string;
planResult: EcommerceVideoPlanResult | null;
completedSteps: PlanStep[];
sourceImageUrls: string[];
aspectRatio: string;
resolution: string;
/** useGenerationTasks 实例,用于 submitTask/markCompleted/markFailed */
generation: {
submitTask: (task: Record<string, unknown> & { taskId: string }) => string;
markCompleted: (id: string, resultUrl?: string) => void;
markFailed: (id: string, error?: string) => void;
};
sceneStoreIdMap: MutableRefObject<Map<number, string>>;
onScenesChange: (updater: SetStateAction<EcommerceVideoSceneTask[]>) => void;
onStageChange: (stage: EcommerceVideoStage) => void;
onError?: (message: string) => void;
}
function mapResolutionToQuality(res: string): "720P" | "1080P" {
return res.includes("720") ? "720P" : "1080P";
}
function deriveAspectRatioToken(aspectRatio: string): string {
if (aspectRatio.includes("9:16") || aspectRatio.includes("916")) return "9:16";
if (aspectRatio.includes("16:9") || aspectRatio.includes("169")) return "16:9";
return "1:1";
}
export function useVideoSceneRunner(context: VideoSceneRunnerContext) {
const {
inputFingerprint,
planResult,
completedSteps,
sourceImageUrls,
aspectRatio,
resolution,
generation,
sceneStoreIdMap,
onScenesChange,
onStageChange,
onError,
} = context;
const abortControllerRef = useRef<AbortController | null>(null);
const renderAbortRef = useRef({ current: false });
// ── Image phase: generate per-scene images ──────────────────
const runImagePhase = useCallback(
async (scenes: EcommerceVideoSceneTask[]): Promise<void> => {
if (!planResult || !scenes.length) return;
const ratio = deriveAspectRatioToken(aspectRatio);
let currentScenes = [...scenes];
const persistScenes = (next: EcommerceVideoSceneTask[]) => {
currentScenes = next;
onScenesChange(next);
saveEcommerceVideoState({
inputFingerprint,
stage: "imaging",
completedSteps,
planResult,
scenes: next,
sourceImageUrls,
});
};
const scenesToProcess = currentScenes.filter((s) => !s.imageUrl);
if (!scenesToProcess.length) {
onStageChange("imaged");
saveEcommerceVideoState({
inputFingerprint,
stage: "imaged",
completedSteps,
planResult,
scenes: currentScenes,
sourceImageUrls,
});
return;
}
for (const scene of scenesToProcess) {
if (renderAbortRef.current.current) break;
persistScenes(
currentScenes.map((s) =>
s.sceneId === scene.sceneId ? { ...s, status: "pending", error: undefined } : s,
),
);
try {
await renderSceneImage(
{
sceneId: scene.sceneId,
prompt: scene.prompt,
aspectRatio: ratio,
productImageUrls: sourceImageUrls,
},
{
onSceneImageSubmitted: (id, taskId) => {
persistScenes(
currentScenes.map((s) => (s.sceneId === id ? { ...s, imageTaskId: taskId, status: "running" } : s)),
);
const storeId = generation.submitTask({
title: `分镜${id}图片`,
type: "image",
status: "running",
progress: 0,
prompt: scene.prompt,
sourceView: "ecommerce",
taskId,
params: { sceneId: id, phase: "imaging" },
});
sceneStoreIdMap.current.set(id, storeId);
},
onSceneImageProgress: (id, progress) =>
persistScenes(currentScenes.map((s) => (s.sceneId === id ? { ...s, progress } : s))),
onSceneImageCompleted: (id, url) => {
persistScenes(
currentScenes.map((s) => (s.sceneId === id ? { ...s, status: "idle", progress: 100, imageUrl: url } : s)),
);
const sid = sceneStoreIdMap.current.get(id);
if (sid) generation.markCompleted(sid, url);
},
onSceneImageFailed: (id, err2) => {
persistScenes(
currentScenes.map((s) => (s.sceneId === id ? { ...s, status: "idle", error: err2 } : s)),
);
const sid = sceneStoreIdMap.current.get(id);
if (sid) generation.markFailed(sid, err2);
},
},
renderAbortRef.current,
);
} catch (err) {
const message = err instanceof Error ? err.message : "图片生成失败";
persistScenes(
currentScenes.map((s) => (s.sceneId === scene.sceneId ? { ...s, status: "idle", error: message } : s)),
);
}
}
const allHaveImages = currentScenes.every((s) => s.imageUrl);
const finalStage: EcommerceVideoStage = allHaveImages ? "imaged" : "partial_failed";
onStageChange(finalStage);
saveEcommerceVideoState({
inputFingerprint,
stage: finalStage,
completedSteps,
planResult,
scenes: currentScenes,
sourceImageUrls,
});
},
[
planResult,
aspectRatio,
inputFingerprint,
completedSteps,
sourceImageUrls,
generation,
sceneStoreIdMap,
onScenesChange,
onStageChange,
],
);
// ── Video phase: render per-scene videos ────────────────────
const runVideoPhase = useCallback(
async (scenes: EcommerceVideoSceneTask[]): Promise<void> => {
if (!scenes.length) return;
const quality = mapResolutionToQuality(resolution);
let currentScenes = [...scenes];
const persistScenes = (next: EcommerceVideoSceneTask[]) => {
currentScenes = next;
onScenesChange(next);
saveEcommerceVideoState({
inputFingerprint,
stage: "rendering",
completedSteps,
planResult,
scenes: next,
sourceImageUrls,
});
};
const scenesToProcess = currentScenes.filter((s) => s.imageUrl && s.status !== "completed");
if (!scenesToProcess.length) {
const finalStage: EcommerceVideoStage = currentScenes.every((s) => s.status === "completed")
? "completed"
: "partial_failed";
onStageChange(finalStage);
saveEcommerceVideoState({
inputFingerprint,
stage: finalStage,
completedSteps,
planResult,
scenes: currentScenes,
sourceImageUrls,
});
return;
}
for (const scene of scenesToProcess) {
if (renderAbortRef.current.current) break;
if (!scene.imageUrl) continue;
persistScenes(
currentScenes.map((s) =>
s.sceneId === scene.sceneId ? { ...s, status: "pending", error: undefined } : s,
),
);
try {
await renderScene(
{
sceneId: scene.sceneId,
prompt: scene.prompt,
durationSeconds: scene.durationSeconds,
imageUrl: scene.imageUrl,
productImageUrls: sourceImageUrls,
aspectRatio,
resolution: quality,
},
{
onSceneSubmitted: (id, taskId) => {
persistScenes(
currentScenes.map((s) => (s.sceneId === id ? { ...s, taskId, status: "running" } : s)),
);
const storeId = generation.submitTask({
title: `分镜${id}视频`,
type: "video",
status: "running",
progress: 0,
prompt: scene.prompt,
sourceView: "ecommerce",
taskId,
params: { sceneId: id, phase: "rendering" },
});
sceneStoreIdMap.current.set(id, storeId);
},
onSceneProgress: (id, progress) =>
persistScenes(currentScenes.map((s) => (s.sceneId === id ? { ...s, progress } : s))),
onSceneCompleted: (id, url) => {
persistScenes(
currentScenes.map((s) =>
s.sceneId === id ? { ...s, status: "completed", progress: 100, resultUrl: url } : s,
),
);
const sid = sceneStoreIdMap.current.get(id);
if (sid) generation.markCompleted(sid, url);
},
onSceneFailed: (id, err2) => {
persistScenes(
currentScenes.map((s) => (s.sceneId === id ? { ...s, status: "failed", error: err2 } : s)),
);
const sid = sceneStoreIdMap.current.get(id);
if (sid) generation.markFailed(sid, err2);
},
},
renderAbortRef.current,
);
} catch (err) {
const msg = err instanceof Error ? err.message : "生成失败";
const isPayment = err instanceof ServerRequestError && err.status === 402;
persistScenes(
currentScenes.map((s) =>
s.sceneId === scene.sceneId
? { ...s, status: "failed", error: isPayment ? "余额不足,请充值后继续" : msg }
: s,
),
);
if (isPayment) {
onError?.("余额不足,请充值后再生成视频");
renderAbortRef.current.current = true;
break;
}
}
}
const hasFailed = currentScenes.some((s) => s.status === "failed");
const allDone = currentScenes.every((s) => s.status === "completed" || s.status === "failed");
const finalStage: EcommerceVideoStage = allDone
? hasFailed
? "partial_failed"
: "completed"
: "rendering";
onScenesChange(currentScenes);
onStageChange(finalStage);
saveEcommerceVideoState({
inputFingerprint,
stage: finalStage,
completedSteps,
planResult,
scenes: currentScenes,
sourceImageUrls,
});
},
[
resolution,
inputFingerprint,
completedSteps,
planResult,
sourceImageUrls,
aspectRatio,
generation,
sceneStoreIdMap,
onScenesChange,
onStageChange,
onError,
],
);
// ── Resume polling: re-attach waitForTask to running scenes ─
// Used when the page is restored from keep-alive. Differs from runImagePhase/runVideoPhase
// in that it does NOT create new tasks — it only polls existing imageTaskId/taskId.
const resumePolling = useCallback(
async (stage: EcommerceVideoStage, scenes: EcommerceVideoSceneTask[]): Promise<void> => {
renderAbortRef.current = { current: false };
if (stage === "imaging") {
for (const scene of scenes) {
if (renderAbortRef.current.current) break;
if (scene.status !== "running" && scene.status !== "pending") continue;
if (!scene.imageTaskId) continue;
try {
const resultUrl = await waitForTask(scene.imageTaskId, {
abortRef: renderAbortRef.current,
onProgress: (e) =>
onScenesChange((prev) =>
prev.map((s) => (s.sceneId === scene.sceneId ? { ...s, progress: e.progress } : s)),
),
});
if (resultUrl) {
onScenesChange((prev) =>
prev.map((s) =>
s.sceneId === scene.sceneId ? { ...s, status: "idle", progress: 100, imageUrl: resultUrl } : s,
),
);
}
} catch {
onScenesChange((prev) =>
prev.map((s) => (s.sceneId === scene.sceneId ? { ...s, status: "idle", error: "恢复任务失败" } : s)),
);
}
}
onScenesChange((current) => {
const allImaged = current.every((s) => s.imageUrl);
if (allImaged) onStageChange("imaged");
return current;
});
}
if (stage === "rendering") {
for (const scene of scenes) {
if (renderAbortRef.current.current) break;
if (scene.status !== "running" && scene.status !== "pending") continue;
if (!scene.taskId) continue;
try {
const resultUrl = await waitForTask(scene.taskId, {
abortRef: renderAbortRef.current,
onProgress: (e) =>
onScenesChange((prev) =>
prev.map((s) => (s.sceneId === scene.sceneId ? { ...s, progress: e.progress } : s)),
),
});
if (resultUrl) {
onScenesChange((prev) =>
prev.map((s) =>
s.sceneId === scene.sceneId
? { ...s, status: "completed", progress: 100, resultUrl: resultUrl }
: s,
),
);
}
} catch {
onScenesChange((prev) =>
prev.map((s) => (s.sceneId === scene.sceneId ? { ...s, status: "failed", error: "恢复任务失败" } : s)),
);
}
}
onScenesChange((current) => {
const hasFailed = current.some((s) => s.status === "failed");
const allDone = current.every((s) => s.status === "completed" || s.status === "failed");
if (allDone) onStageChange(hasFailed ? "partial_failed" : "completed");
return current;
});
}
},
[onScenesChange, onStageChange],
);
// ── Cancel: abort planning + scene rendering ────────────────
const cancel = useCallback(() => {
abortControllerRef.current?.abort();
renderAbortRef.current.current = true;
onStageChange("cancelled");
}, [onStageChange]);
// ── Retry a single scene's video ────────────────────────────
const retryScene = useCallback(
async (scene: EcommerceVideoSceneTask): Promise<void> => {
if (!scene.imageUrl) return;
onScenesChange((prev) =>
prev.map((s) => (s.sceneId === scene.sceneId ? { ...s, status: "pending", error: undefined } : s)),
);
try {
await renderScene(
{
sceneId: scene.sceneId,
prompt: scene.prompt,
durationSeconds: scene.durationSeconds,
imageUrl: scene.imageUrl,
productImageUrls: sourceImageUrls,
aspectRatio,
resolution: mapResolutionToQuality(resolution),
},
{
onSceneSubmitted: (id, taskId) =>
onScenesChange((prev) => prev.map((s) => (s.sceneId === id ? { ...s, taskId, status: "running" } : s))),
onSceneProgress: (id, progress) =>
onScenesChange((prev) => prev.map((s) => (s.sceneId === id ? { ...s, progress } : s))),
onSceneCompleted: (id, url) =>
onScenesChange((prev) =>
prev.map((s) => (s.sceneId === id ? { ...s, status: "completed", progress: 100, resultUrl: url } : s)),
),
onSceneFailed: (id, err2) =>
onScenesChange((prev) =>
prev.map((s) => (s.sceneId === id ? { ...s, status: "failed", error: err2 } : s)),
),
},
renderAbortRef.current,
);
} catch (err) {
onScenesChange((prev) =>
prev.map((s) =>
s.sceneId === scene.sceneId ? { ...s, status: "failed", error: (err as Error).message } : s,
),
);
}
},
[sourceImageUrls, aspectRatio, resolution, onScenesChange],
);
return {
abortControllerRef,
renderAbortRef,
runImagePhase,
runVideoPhase,
resumePolling,
cancel,
retryScene,
};
}
@@ -0,0 +1,212 @@
// 克隆 / 电商历史的本地持久化模块。
// 从 EcommercePage.tsx 抽出,逻辑零改动。
// 把 localStorage 读写 + 字段校验 + 默认值收口在此,页面只调用 read/write。
//
// 领域类型(CloneImageItem / CloneResult / CloneSavedSetting / EcommerceHistoryRecord
// 及其依赖的 type alias)也定义在此并 export,因为它们本质上是"持久化数据契约";
// EcommercePage 从这里 re-import,避免循环依赖(类型 import 编译期擦除)。
import type { CloneOutputKey } from "./platformRules";
export type CloneSetCountKey = "selling" | "white" | "scene";
export type CloneModelPanelTab = "scene" | "model";
export type CloneVideoQualityKey = "standard" | "high" | "ultra";
export type CloneReplicateLevelKey = "style" | "high";
export type CloneReferenceMode = "upload" | "link";
export interface CloneImageItem {
id: string;
src: string;
name: string;
file?: File;
width?: number;
height?: number;
format?: string;
mimeType?: string;
ossKey?: string;
}
export interface CloneResult {
id: string;
src: string;
label: string;
type?: "image" | "video";
}
export interface CloneSavedSetting {
id: string;
name: string;
savedAt: string;
output: CloneOutputKey;
platform: string;
market: string;
language: string;
ratio: string;
setCounts: Record<CloneSetCountKey, number>;
detailModules: string[];
modelPanelTab: CloneModelPanelTab;
modelScenes: string[];
modelCustomScene: string;
modelGender: string;
modelAge: string;
modelEthnicity: string;
modelBody: string;
modelAppearance: string;
videoQuality: CloneVideoQualityKey;
videoDurationSeconds: number;
videoSmart: boolean;
referenceMode?: CloneReferenceMode;
replicateLevel?: CloneReplicateLevelKey;
requirement: string;
}
export interface EcommerceHistoryRecord {
id: string;
title: string;
createdAt: number;
output: CloneOutputKey;
platform: string;
market: string;
language: string;
ratio: string;
requirement: string;
productImages: CloneImageItem[];
results: CloneResult[];
setResultImages: string[];
setCounts: Record<CloneSetCountKey, number>;
detailModules: string[];
modelScenes: string[];
referenceImages: CloneImageItem[];
replicateLevel: CloneReplicateLevelKey;
}
export const cloneLatestSettingStorageKey = "omniai.clone-ai.latest-setting";
export const ecommerceHistoryStorageKey = "omniai.ecommerce.history.records";
export const defaultCloneSetCounts: Record<CloneSetCountKey, number> = {
selling: 3,
white: 1,
scene: 3,
};
export const defaultCloneDetailModuleIds = ["hero", "selling", "usage", "angle", "scene", "detail"];
export function isCloneImageItem(item: unknown): item is CloneImageItem {
const candidate = item as Partial<CloneImageItem>;
return typeof candidate.id === "string" && typeof candidate.src === "string" && typeof candidate.name === "string";
}
export function isCloneResult(item: unknown): item is CloneResult {
const candidate = item as Partial<CloneResult>;
return typeof candidate.id === "string" && typeof candidate.src === "string" && typeof candidate.label === "string";
}
export function isEcommerceHistoryRecord(item: unknown): item is EcommerceHistoryRecord {
const candidate = item as Partial<EcommerceHistoryRecord>;
return (
typeof candidate.id === "string" &&
typeof candidate.title === "string" &&
typeof candidate.createdAt === "number" &&
typeof candidate.output === "string" &&
typeof candidate.platform === "string" &&
typeof candidate.market === "string" &&
typeof candidate.language === "string" &&
typeof candidate.ratio === "string" &&
typeof candidate.requirement === "string" &&
Array.isArray(candidate.productImages) &&
candidate.productImages.every(isCloneImageItem) &&
Array.isArray(candidate.results) &&
candidate.results.every(isCloneResult)
);
}
export function isCloneSavedSetting(item: unknown): item is CloneSavedSetting {
const candidate = item as Partial<CloneSavedSetting>;
return (
typeof candidate.id === "string" &&
typeof candidate.name === "string" &&
typeof candidate.savedAt === "string" &&
typeof candidate.output === "string" &&
typeof candidate.platform === "string" &&
typeof candidate.market === "string" &&
typeof candidate.language === "string" &&
typeof candidate.ratio === "string" &&
typeof candidate.videoDurationSeconds === "number"
);
}
export function removeFilePayloadFromImages(images: CloneImageItem[]): CloneImageItem[] {
return images.map(({ id, src, name, width, height, format, mimeType, ossKey }) => ({
id,
src,
name,
width,
height,
format,
mimeType,
ossKey,
}));
}
export function normalizeEcommerceHistoryRecord(record: EcommerceHistoryRecord): EcommerceHistoryRecord {
return {
...record,
productImages: removeFilePayloadFromImages(record.productImages),
referenceImages: removeFilePayloadFromImages(record.referenceImages ?? []),
results: record.results ?? [],
setResultImages: record.setResultImages ?? [],
setCounts: record.setCounts ?? defaultCloneSetCounts,
detailModules: record.detailModules ?? defaultCloneDetailModuleIds,
modelScenes: record.modelScenes ?? [],
replicateLevel: record.replicateLevel ?? "high",
};
}
export function readCloneLatestSetting(): CloneSavedSetting | null {
if (typeof window === "undefined") return null;
try {
const rawValue = window.localStorage.getItem(cloneLatestSettingStorageKey);
if (rawValue) {
const parsedValue: unknown = JSON.parse(rawValue);
if (isCloneSavedSetting(parsedValue)) return parsedValue;
}
} catch {
return null;
}
return null;
}
export function writeCloneLatestSetting(setting: CloneSavedSetting): void {
if (typeof window === "undefined") return;
window.localStorage.setItem(cloneLatestSettingStorageKey, JSON.stringify(setting));
}
export function clearCloneLatestSetting(): void {
if (typeof window === "undefined") return;
window.localStorage.removeItem(cloneLatestSettingStorageKey);
}
export function readEcommerceHistoryRecords(): EcommerceHistoryRecord[] {
if (typeof window === "undefined") return [];
try {
const rawValue = window.localStorage.getItem(ecommerceHistoryStorageKey);
if (!rawValue) return [];
const parsedValue: unknown = JSON.parse(rawValue);
if (!Array.isArray(parsedValue)) return [];
return parsedValue
.filter(isEcommerceHistoryRecord)
.map(normalizeEcommerceHistoryRecord)
.sort((a, b) => b.createdAt - a.createdAt)
.slice(0, 30);
} catch {
return [];
}
}
export function writeEcommerceHistoryRecords(records: EcommerceHistoryRecord[]): void {
if (typeof window === "undefined") return;
window.localStorage.setItem(
ecommerceHistoryStorageKey,
JSON.stringify(records.map(normalizeEcommerceHistoryRecord).slice(0, 30)),
);
}
@@ -0,0 +1,122 @@
import { describe, it, expect } from "vitest";
import {
clampNumber,
normalizeHexColor,
hexToRgb,
rgbToHex,
parseSmartCutoutAspect,
parseSmartCutoutPercent,
hsvToRgb,
hexToHsv,
} from "./colorUtils";
describe("clampNumber", () => {
it("clamps below min", () => {
expect(clampNumber(-5, 0, 100)).toBe(0);
});
it("clamps above max", () => {
expect(clampNumber(200, 0, 100)).toBe(100);
});
it("passes through values in range", () => {
expect(clampNumber(50, 0, 100)).toBe(50);
});
});
describe("normalizeHexColor", () => {
it("normalizes a valid hex", () => {
expect(normalizeHexColor("#FF8800")).toBe("#ff8800");
});
it("accepts hex without leading #", () => {
expect(normalizeHexColor("ff8800")).toBe("#ff8800");
});
it("returns null for invalid hex", () => {
expect(normalizeHexColor("#fff")).toBeNull();
expect(normalizeHexColor("ggghhh")).toBeNull();
expect(normalizeHexColor("")).toBeNull();
});
});
describe("hex <-> rgb round-trip", () => {
const cases: Array<[string, { r: number; g: number; b: number }]> = [
["#000000", { r: 0, g: 0, b: 0 }],
["#ffffff", { r: 255, g: 255, b: 255 }],
["#ff8800", { r: 255, g: 136, b: 0 }],
["#2dd4bf", { r: 45, g: 212, b: 191 }],
];
for (const [hex, rgb] of cases) {
it(`hexToRgb(${hex}) -> rgb`, () => {
expect(hexToRgb(hex)).toEqual(rgb);
});
it(`rgbToHex(${rgb.r},${rgb.g},${rgb.b}) -> ${hex}`, () => {
expect(rgbToHex(rgb.r, rgb.g, rgb.b)).toBe(hex);
});
}
it("hexToRgb returns null for invalid", () => {
expect(hexToRgb("nope")).toBeNull();
});
it("rgbToHex clamps out-of-range channels", () => {
expect(rgbToHex(300, -5, 128)).toBe(rgbToHex(255, 0, 128));
});
});
describe("parseSmartCutoutAspect", () => {
it("parses a W / H aspect string", () => {
expect(parseSmartCutoutAspect("295 / 413")).toBeCloseTo(295 / 413, 5);
});
it("handles decimals", () => {
expect(parseSmartCutoutAspect("1.5 / 2")).toBeCloseTo(0.75, 5);
});
it("returns null when no ratio pattern is present", () => {
expect(parseSmartCutoutAspect("not-a-ratio")).toBeNull();
expect(parseSmartCutoutAspect("")).toBeNull();
});
it("returns null for zero dimensions", () => {
expect(parseSmartCutoutAspect("0 / 100")).toBeNull();
expect(parseSmartCutoutAspect("100 / 0")).toBeNull();
});
it("ignores leading sign (regex only matches digits)", () => {
// The regex \d+ does not match '-', so "-1 / 2" parses as 1/2.
expect(parseSmartCutoutAspect("-1 / 2")).toBeCloseTo(0.5, 5);
});
});
describe("parseSmartCutoutPercent", () => {
it("parses a percentage", () => {
expect(parseSmartCutoutPercent("82%", 0.5)).toBeCloseTo(0.82, 5);
});
it("clamps to [0.05, 1]", () => {
expect(parseSmartCutoutPercent("150%", 0.5)).toBe(1);
expect(parseSmartCutoutPercent("1%", 0.5)).toBe(0.05);
});
it("returns fallback for non-numeric", () => {
expect(parseSmartCutoutPercent("abc", 0.5)).toBe(0.5);
});
});
describe("hsv <-> rgb", () => {
it("hsvToRgb of pure red", () => {
expect(hsvToRgb(0, 100, 100)).toEqual({ r: 255, g: 0, b: 0 });
});
it("hsvToRgb of pure green", () => {
expect(hsvToRgb(120, 100, 100)).toEqual({ r: 0, g: 255, b: 0 });
});
it("hsvToRgb of white (saturation 0)", () => {
expect(hsvToRgb(0, 0, 100)).toEqual({ r: 255, g: 255, b: 255 });
});
it("hexToHsv then hsvToRgb round-trips within ±2 (rounding)", () => {
const hex = "#2dd4bf";
const hsv = hexToHsv(hex);
const rgb = hsvToRgb(hsv.h, hsv.s, hsv.v);
const original = hexToRgb(hex)!;
expect(Math.abs(rgb.r - original.r)).toBeLessThanOrEqual(2);
expect(Math.abs(rgb.g - original.g)).toBeLessThanOrEqual(2);
expect(Math.abs(rgb.b - original.b)).toBeLessThanOrEqual(2);
});
it("hexToHsv of white", () => {
expect(hexToHsv("#ffffff")).toEqual({ h: 0, s: 0, v: 100 });
});
});
@@ -0,0 +1,88 @@
// 智能抠图 / 调色板用到的纯数值与颜色转换工具。
// 从 EcommercePage.tsx 抽出,逻辑零改动,仅加 export 以便单测。
export const clampNumber = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value));
export const normalizeHexColor = (value: string) => {
const clean = value.trim().replace(/^#/, "");
if (!/^[0-9a-fA-F]{6}$/.test(clean)) return null;
return `#${clean.toLowerCase()}`;
};
export const hexToRgb = (value: string) => {
const normalized = normalizeHexColor(value);
if (!normalized) return null;
const numeric = Number.parseInt(normalized.slice(1), 16);
return {
r: (numeric >> 16) & 255,
g: (numeric >> 8) & 255,
b: numeric & 255,
};
};
export const rgbToHex = (r: number, g: number, b: number) =>
`#${[r, g, b].map((item) => clampNumber(Math.round(item), 0, 255).toString(16).padStart(2, "0")).join("")}`;
export const parseSmartCutoutAspect = (aspect: string) => {
const match = aspect.match(/(\d+(?:\.\d+)?)\s*\/\s*(\d+(?:\.\d+)?)/);
if (!match) return null;
const width = Number(match[1]);
const height = Number(match[2]);
if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) return null;
return width / height;
};
export const parseSmartCutoutPercent = (value: string, fallback: number) => {
const numeric = Number(value.replace("%", ""));
if (!Number.isFinite(numeric)) return fallback;
return clampNumber(numeric / 100, 0.05, 1);
};
export const hsvToRgb = (h: number, s: number, v: number) => {
const hue = ((h % 360) + 360) % 360;
const saturation = clampNumber(s, 0, 100) / 100;
const value = clampNumber(v, 0, 100) / 100;
const chroma = value * saturation;
const x = chroma * (1 - Math.abs(((hue / 60) % 2) - 1));
const match = value - chroma;
const [red, green, blue] =
hue < 60
? [chroma, x, 0]
: hue < 120
? [x, chroma, 0]
: hue < 180
? [0, chroma, x]
: hue < 240
? [0, x, chroma]
: hue < 300
? [x, 0, chroma]
: [chroma, 0, x];
return {
r: (red + match) * 255,
g: (green + match) * 255,
b: (blue + match) * 255,
};
};
export const hexToHsv = (value: string) => {
const rgb = hexToRgb(value) ?? { r: 255, g: 255, b: 255 };
const red = rgb.r / 255;
const green = rgb.g / 255;
const blue = rgb.b / 255;
const max = Math.max(red, green, blue);
const min = Math.min(red, green, blue);
const delta = max - min;
const hue =
delta === 0
? 0
: max === red
? 60 * (((green - blue) / delta) % 6)
: max === green
? 60 * ((blue - red) / delta + 2)
: 60 * ((red - green) / delta + 4);
return {
h: Math.round((hue + 360) % 360),
s: max === 0 ? 0 : Math.round((delta / max) * 100),
v: Math.round(max * 100),
};
};
@@ -0,0 +1,95 @@
import { describe, expect, it } from "vitest";
import {
defaultCloneOutput,
defaultEcommercePlatform,
defaultProductSetOutput,
formatUploadedImageRatio,
getPlatformDefaultLanguage,
getPlatformDefaultRatio,
getPlatformLanguageOptions,
getPlatformRatioOptions,
getUniqueRatioOptions,
normalizeLanguageForPlatform,
normalizeMarket,
normalizePlatform,
normalizeRatioForPlatform,
platformOptions,
} from "./platformRules";
describe("platform defaults", () => {
it("exposes the default ecommerce platform and outputs", () => {
expect(defaultEcommercePlatform).toBe("淘宝/天猫");
expect(defaultProductSetOutput).toBe("set");
expect(defaultCloneOutput).toBe("set");
});
it("lists platform labels for UI selectors", () => {
expect(platformOptions).toContain("淘宝/天猫");
expect(platformOptions).toContain("亚马逊 Amazon");
expect(platformOptions).toContain("TikTok Shop");
});
});
describe("normalizePlatform", () => {
it("normalizes legacy labels", () => {
expect(normalizePlatform("亚马逊Amazon")).toBe("亚马逊 Amazon");
expect(normalizePlatform("亚马逊")).toBe("亚马逊 Amazon");
});
it("falls back to the default platform for unknown labels", () => {
expect(normalizePlatform("unknown")).toBe("淘宝/天猫");
});
});
describe("platform ratios", () => {
it("returns mode-specific ratios", () => {
expect(getPlatformRatioOptions("淘宝/天猫", "set")).toContain("1000×1000px\u00a0\u00a0\u00a01:1");
expect(getPlatformDefaultRatio("淘宝/天猫", "video")).toBe("1080×1920px\u00a0\u00a0\u00a09:16");
});
it("normalizes an existing or partially matching ratio for a platform", () => {
expect(normalizeRatioForPlatform("淘宝/天猫", "1000×1000px\u00a0\u00a0\u00a01:1", "set")).toBe("1000×1000px\u00a0\u00a0\u00a01:1");
expect(normalizeRatioForPlatform("淘宝/天猫", "1000×1000px", "set")).toBe("1000×1000px\u00a0\u00a0\u00a01:1");
});
it("falls back to the mode default when no ratio matches", () => {
expect(normalizeRatioForPlatform("淘宝/天猫", "nope", "set")).toBe("1000×1000px\u00a0\u00a0\u00a01:1");
});
it("deduplicates ratio lists without changing order", () => {
expect(getUniqueRatioOptions(["1:1", "3:4", "1:1"])).toEqual(["1:1", "3:4"]);
});
});
describe("market and language rules", () => {
it("normalizes unknown markets to the default country", () => {
expect(normalizeMarket("火星")).toBe("中国");
});
it("uses Chinese by default for domestic platforms", () => {
expect(getPlatformDefaultLanguage("淘宝/天猫", "美国")).toBe("中文");
});
it("includes English for domestic platforms while preserving local languages", () => {
expect(getPlatformLanguageOptions("淘宝/天猫", "美国")).toEqual(["中文", "英文"]);
});
it("uses market languages for cross-border platforms", () => {
expect(getPlatformDefaultLanguage("亚马逊 Amazon", "日本")).toBe("日文");
});
it("normalizes language aliases and falls back when not available", () => {
expect(normalizeLanguageForPlatform("亚马逊 Amazon", "日本", "日语")).toBe("日文");
expect(normalizeLanguageForPlatform("亚马逊 Amazon", "日本", "德语")).toBe("日文");
});
});
describe("formatUploadedImageRatio", () => {
it("formats dimensions and aspect ratio", () => {
expect(formatUploadedImageRatio({ width: 750, height: 1000, format: "PNG" })).toBe("上传图片 750×1000px\u00a0\u00a0\u00a03:4\u00a0\u00a0\u00a0PNG");
});
it("falls back to original ratio when dimensions are missing", () => {
expect(formatUploadedImageRatio({ format: "JPG" })).toBe("上传图片\u00a0\u00a0\u00a0原图比例\u00a0\u00a0\u00a0JPG");
});
});
@@ -0,0 +1,479 @@
import { formatAspectRatio, normalizeRatioToken } from "./ratioUtils";
export type ProductSetOutputKey = "set" | "detail" | "model" | "video";
export type CloneOutputKey = ProductSetOutputKey | "hot";
export type PlatformRatioModeKey = ProductSetOutputKey | "hot";
export interface PlatformRatioGroup {
ratios: string[];
defaultRatio: string;
}
export interface EcommercePlatformSpec {
label: string;
ratios: string[];
defaultRatio: string;
ratioGroups?: Partial<Record<PlatformRatioModeKey, PlatformRatioGroup>>;
specs: string[];
tip?: string;
aliases?: string[];
}
export const platformSpecOptions: EcommercePlatformSpec[] = [
{
label: "淘宝/天猫",
ratios: ["淘宝主图 / SKU 图 800×800px", "详情页宽 750px", "详情页宽 790px"],
defaultRatio: "淘宝主图 / SKU 图 800×800px",
ratioGroups: {
set: {
ratios: ["1000×1000px\u00a0\u00a0\u00a01:1", "800×800px\u00a0\u00a0\u00a01:1"],
defaultRatio: "1000×1000px\u00a0\u00a0\u00a01:1",
},
detail: {
ratios: [
"750×1000px\u00a0\u00a0\u00a03:4",
"790×1053px\u00a0\u00a0\u00a03:4",
"750×1125px\u00a0\u00a0\u00a02:3",
"790×1185px\u00a0\u00a0\u00a02:3",
],
defaultRatio: "750×1000px\u00a0\u00a0\u00a03:4",
},
model: {
ratios: ["750×1000px\u00a0\u00a0\u00a03:4"],
defaultRatio: "750×1000px\u00a0\u00a0\u00a03:4",
},
video: {
ratios: ["1080×1920px\u00a0\u00a0\u00a09:16", "1080×1440px\u00a0\u00a0\u00a03:4", "1080×1080px\u00a0\u00a0\u00a01:1"],
defaultRatio: "1080×1920px\u00a0\u00a0\u00a09:16",
},
hot: {
ratios: ["800×800px\u00a0\u00a0\u00a01:1"],
defaultRatio: "800×800px\u00a0\u00a0\u00a01:1",
},
},
specs: ["主图 / SKU 图 800×800px,≤3MB", "详情页宽 750px 或 790px,单张高度≤1546px"],
tip: "建议主图 200-400KB JPG,超过 500KB 会影响加载速度。",
},
{
label: "京东",
ratios: ["京东主图 / SKU 图 800×800px", "详情页宽 750px", "首图主体占比 ≥80%"],
defaultRatio: "京东主图 / SKU 图 800×800px",
ratioGroups: {
set: {
ratios: ["1000×1000px\u00a0\u00a0\u00a01:1"],
defaultRatio: "1000×1000px\u00a0\u00a0\u00a01:1",
},
detail: {
ratios: [
"750×1000px\u00a0\u00a0\u00a03:4",
"990×1320px\u00a0\u00a0\u00a03:4",
"750×1125px\u00a0\u00a0\u00a02:3",
"990×1485px\u00a0\u00a0\u00a02:3",
],
defaultRatio: "750×1000px\u00a0\u00a0\u00a03:4",
},
model: {
ratios: ["750×1125px\u00a0\u00a0\u00a02:3", "990×1485px\u00a0\u00a0\u00a02:3"],
defaultRatio: "750×1125px\u00a0\u00a0\u00a02:3",
},
video: {
ratios: ["1080×1920px\u00a0\u00a0\u00a09:16", "1920×1080px\u00a0\u00a0\u00a016:9"],
defaultRatio: "1080×1920px\u00a0\u00a0\u00a09:16",
},
hot: {
ratios: ["800×800px\u00a0\u00a0\u00a01:1"],
defaultRatio: "800×800px\u00a0\u00a0\u00a01:1",
},
},
specs: ["主图 / SKU 图 800×800px,白底,≤3MB", "详情页宽 750px,首图主体占比 ≥80%"],
},
{
label: "拼多多",
ratios: ["主图 750×352px", "主图 800×800px", "详情页宽 750px"],
defaultRatio: "主图 750×352px",
ratioGroups: {
set: {
ratios: ["800×800px\u00a0\u00a0\u00a01:1", "750×1000px\u00a0\u00a0\u00a03:4"],
defaultRatio: "800×800px\u00a0\u00a0\u00a01:1",
},
detail: {
ratios: ["750×1000px\u00a0\u00a0\u00a03:4", "750×1125px\u00a0\u00a0\u00a02:3"],
defaultRatio: "750×1000px\u00a0\u00a0\u00a03:4",
},
model: {
ratios: ["750×1000px\u00a0\u00a0\u00a03:4"],
defaultRatio: "750×1000px\u00a0\u00a0\u00a03:4",
},
video: {
ratios: ["1080×1920px\u00a0\u00a0\u00a09:16"],
defaultRatio: "1080×1920px\u00a0\u00a0\u00a09:16",
},
hot: {
ratios: ["800×800px\u00a0\u00a0\u00a01:1"],
defaultRatio: "800×800px\u00a0\u00a0\u00a01:1",
},
},
specs: ["主图 750×352px 或 800×800px,≤1MB", "详情页宽 750px,要求纯白底、无水印、无拼接"],
},
{
label: "抖音电商",
ratios: ["短视频1080×1920px"],
defaultRatio: "短视频1080×1920px",
ratioGroups: {
video: {
ratios: ["1080×1920px\u00a0\u00a0\u00a09:16"],
defaultRatio: "1080×1920px\u00a0\u00a0\u00a09:16",
},
hot: {
ratios: ["800×800px\u00a0\u00a0\u00a01:1"],
defaultRatio: "800×800px\u00a0\u00a0\u00a01:1",
},
},
specs: ["短视频 1080×1920px9:16", "30s 内最佳"],
},
{
label: "亚马逊 Amazon",
ratios: ["主图 ≥1600×1600px", "建议 2000×2000px+", "最小 500×500px"],
defaultRatio: "主图 ≥1600×1600px",
ratioGroups: {
set: {
ratios: ["1600×1600px\u00a0\u00a0\u00a01:1"],
defaultRatio: "1600×1600px\u00a0\u00a0\u00a01:1",
},
detail: {
ratios: ["1600×1600px\u00a0\u00a0\u00a01:1", "1200×1800px\u00a0\u00a0\u00a02:3", "1200×1600px\u00a0\u00a0\u00a03:4"],
defaultRatio: "1200×1800px\u00a0\u00a0\u00a02:3",
},
model: {
ratios: ["1200×1800px\u00a0\u00a0\u00a02:3"],
defaultRatio: "1200×1800px\u00a0\u00a0\u00a02:3",
},
video: {
ratios: ["1920×1080px\u00a0\u00a0\u00a016:9"],
defaultRatio: "1920×1080px\u00a0\u00a0\u00a016:9",
},
hot: {
ratios: ["1600×1600px\u00a0\u00a0\u00a01:1"],
defaultRatio: "1600×1600px\u00a0\u00a0\u00a01:1",
},
},
specs: ["主图 1600×1600px+,纯白底,≤10MB", "最小 500×500px,建议 2000px+ 以支持缩放"],
aliases: ["亚马逊"],
},
{
label: "Shopee",
ratios: ["商品主图 1024×1024px", "基础主图 800×800px"],
defaultRatio: "商品主图 1024×1024px",
ratioGroups: {
set: {
ratios: ["800×800px\u00a0\u00a0\u00a01:1"],
defaultRatio: "800×800px\u00a0\u00a0\u00a01:1",
},
detail: {
ratios: ["750×1000px\u00a0\u00a0\u00a03:4", "750×1125px\u00a0\u00a0\u00a02:3"],
defaultRatio: "750×1000px\u00a0\u00a0\u00a03:4",
},
model: {
ratios: ["750×1000px\u00a0\u00a0\u00a03:4"],
defaultRatio: "750×1000px\u00a0\u00a0\u00a03:4",
},
video: {
ratios: ["1080×1920px\u00a0\u00a0\u00a09:16"],
defaultRatio: "1080×1920px\u00a0\u00a0\u00a09:16",
},
hot: {
ratios: ["800×800px\u00a0\u00a0\u00a01:1"],
defaultRatio: "800×800px\u00a0\u00a0\u00a01:1",
},
},
specs: ["商品主图推荐 1024×1024px,基础 800×800px", "≤2MB,白底或浅色底"],
aliases: ["虾皮 Shopee/Lazada", "虾皮"],
},
{
label: "Lazada",
ratios: ["商品主图 800×800px"],
defaultRatio: "商品主图 800×800px",
ratioGroups: {
set: {
ratios: ["800×800px\u00a0\u00a0\u00a01:1"],
defaultRatio: "800×800px\u00a0\u00a0\u00a01:1",
},
detail: {
ratios: ["750×1000px\u00a0\u00a0\u00a03:4", "750×1125px\u00a0\u00a0\u00a02:3"],
defaultRatio: "750×1000px\u00a0\u00a0\u00a03:4",
},
model: {
ratios: ["750×1000px\u00a0\u00a0\u00a03:4"],
defaultRatio: "750×1000px\u00a0\u00a0\u00a03:4",
},
video: {
ratios: ["1080×1920px\u00a0\u00a0\u00a09:16"],
defaultRatio: "1080×1920px\u00a0\u00a0\u00a09:16",
},
hot: {
ratios: ["800×800px\u00a0\u00a0\u00a01:1"],
defaultRatio: "800×800px\u00a0\u00a0\u00a01:1",
},
},
specs: ["商品主图 800×800px1:1"],
},
{
label: "Instagram",
ratios: ["帖子 1080×1350px", "帖子 1080×1080px", "Stories / Reels 1080×1920px", "头像 320×320px"],
defaultRatio: "帖子 1080×1350px",
ratioGroups: {
set: {
ratios: ["1080×1080px\u00a0\u00a0\u00a01:1", "1080×1350px\u00a0\u00a0\u00a04:5"],
defaultRatio: "1080×1080px\u00a0\u00a0\u00a01:1",
},
detail: {
ratios: ["1080×1350px\u00a0\u00a0\u00a04:5"],
defaultRatio: "1080×1350px\u00a0\u00a0\u00a04:5",
},
model: {
ratios: ["1080×1350px\u00a0\u00a0\u00a04:5"],
defaultRatio: "1080×1350px\u00a0\u00a0\u00a04:5",
},
video: {
ratios: ["1080×1920px\u00a0\u00a0\u00a09:16", "1080×1350px\u00a0\u00a0\u00a04:5"],
defaultRatio: "1080×1920px\u00a0\u00a0\u00a09:16",
},
},
specs: ["帖子 1080×1350px 或 1080×1080px", "Stories / Reels 封面 1080×1920px,头像 320×320px"],
tip: "建议 ≤8MB JPG。",
aliases: ["Instagram Reels"],
},
{
label: "速卖通",
ratios: ["主图 800×800px", "主图 1000×1000px+"],
defaultRatio: "主图 800×800px",
ratioGroups: {
set: {
ratios: ["1000×1000px\u00a0\u00a0\u00a01:1"],
defaultRatio: "1000×1000px\u00a0\u00a0\u00a01:1",
},
detail: {
ratios: ["750×1125px\u00a0\u00a0\u00a02:3", "750×1000px\u00a0\u00a0\u00a03:4"],
defaultRatio: "750×1125px\u00a0\u00a0\u00a02:3",
},
model: {
ratios: ["750×1125px\u00a0\u00a0\u00a02:3"],
defaultRatio: "750×1125px\u00a0\u00a0\u00a02:3",
},
video: {
ratios: ["1080×1920px\u00a0\u00a0\u00a09:16", "1920×1080px\u00a0\u00a0\u00a016:9"],
defaultRatio: "1080×1920px\u00a0\u00a0\u00a09:16",
},
hot: {
ratios: ["800×800px\u00a0\u00a0\u00a01:1"],
defaultRatio: "800×800px\u00a0\u00a0\u00a01:1",
},
},
specs: ["主图建议 800×800px 或更高,1:1", "适合跨境电商主图、SKU 图和场景图"],
},
{
label: "eBay",
ratios: ["商品图1:1", "白底多角度展示图 1:1"],
defaultRatio: "商品图1:1",
ratioGroups: {
set: {
ratios: ["1600×1600px\u00a0\u00a0\u00a01:1"],
defaultRatio: "1600×1600px\u00a0\u00a0\u00a01:1",
},
detail: {
ratios: ["1000×1500px\u00a0\u00a0\u00a02:3", "1200×1600px\u00a0\u00a0\u00a03:4"],
defaultRatio: "1000×1500px\u00a0\u00a0\u00a02:3",
},
model: {
ratios: ["1000×1500px\u00a0\u00a0\u00a02:3"],
defaultRatio: "1000×1500px\u00a0\u00a0\u00a02:3",
},
video: {
ratios: ["1920×1080px\u00a0\u00a0\u00a016:9", "1080×1920px\u00a0\u00a0\u00a09:16"],
defaultRatio: "1920×1080px\u00a0\u00a0\u00a016:9",
},
hot: {
ratios: ["1600×1600px\u00a0\u00a0\u00a01:1"],
defaultRatio: "1600×1600px\u00a0\u00a0\u00a01:1",
},
},
specs: ["商品图建议 1:1,主体清晰居中", "适合白底主图和多角度展示图"],
},
{
label: "TikTok Shop",
ratios: ["商品主图 1:1", "短视频/ 竖版封面 9:16"],
defaultRatio: "商品主图 1:1",
ratioGroups: {
set: {
ratios: ["1280×1280px\u00a0\u00a0\u00a01:1"],
defaultRatio: "1280×1280px\u00a0\u00a0\u00a01:1",
},
detail: {
ratios: ["1080×1350px\u00a0\u00a0\u00a04:5"],
defaultRatio: "1080×1350px\u00a0\u00a0\u00a04:5",
},
model: {
ratios: ["1080×1350px\u00a0\u00a0\u00a04:5"],
defaultRatio: "1080×1350px\u00a0\u00a0\u00a04:5",
},
video: {
ratios: ["1080×1920px\u00a0\u00a0\u00a09:16"],
defaultRatio: "1080×1920px\u00a0\u00a0\u00a09:16",
},
hot: {
ratios: ["800×800px\u00a0\u00a0\u00a01:1"],
defaultRatio: "800×800px\u00a0\u00a0\u00a01:1",
},
},
specs: ["商品主图建议 1:1", "短视频竖版封面建议 9:16"],
},
];
export const platformOptions = platformSpecOptions.map((option) => option.label);
const getPlatformLogoText = (value: string) => {
const normalized = value.toLowerCase();
if (value.includes("淘宝") || value.includes("天猫")) return "淘";
if (value.includes("京东")) return "京";
if (value.includes("拼多多") || value.includes("鎷煎澶")) return "拼";
if (value.includes("抖音")) return "抖";
if (normalized.includes("amazon")) return "a";
if (normalized.includes("shopee")) return "S";
if (normalized.includes("lazada")) return "L";
if (normalized.includes("instagram")) return "IG";
if (value.includes("速卖通") || value.includes("閫熷崠閫")) return "AE";
if (normalized.includes("ebay")) return "eB";
if (normalized.includes("tiktok")) return "♪";
return value.trim().slice(0, 1).toUpperCase() || "商";
};
const getPlatformLogoVariant = (value: string) => {
const normalized = value.toLowerCase();
if (value.includes("淘宝") || value.includes("天猫")) return "taobao";
if (value.includes("京东")) return "jd";
if (value.includes("拼多多") || value.includes("鎷煎澶")) return "pdd";
if (value.includes("抖音")) return "douyin";
if (normalized.includes("amazon")) return "amazon";
if (normalized.includes("shopee")) return "shopee";
if (normalized.includes("lazada")) return "lazada";
if (normalized.includes("instagram")) return "instagram";
if (value.includes("速卖通") || value.includes("閫熷崠閫")) return "aliexpress";
if (normalized.includes("ebay")) return "ebay";
if (normalized.includes("tiktok")) return "tiktok";
return "default";
};
const getPlatformLogoMarks = (value: string) => {
if (value.includes("淘宝") || value.includes("天猫")) return ["淘", "猫"];
return [getPlatformLogoText(value)];
};
export const marketLanguageOptions: Array<{ country: string; languages: string[] }> = [
{ country: "中国", languages: ["中文"] },
{ country: "美国", languages: ["英文"] },
{ country: "加拿大", languages: ["英文", "法文"] },
{ country: "英国", languages: ["英文"] },
{ country: "德国", languages: ["德文"] },
{ country: "法国", languages: ["法文"] },
{ country: "意大利", languages: ["意大利语"] },
{ country: "西班牙", languages: ["西班牙语"] },
{ country: "日本", languages: ["日文"] },
{ country: "韩国", languages: ["韩文"] },
{ country: "澳大利亚", languages: ["英文"] },
{ country: "新加坡", languages: ["英文", "中文"] },
{ country: "马来西亚", languages: ["马来语", "英文", "中文"] },
{ country: "印尼", languages: ["印度尼西亚语", "英文"] },
{ country: "越南", languages: ["越南语", "英文"] },
{ country: "泰国", languages: ["泰语", "英文"] },
{ country: "菲律宾", languages: ["菲律宾语(他加禄语)", "英文"] },
{ country: "巴西", languages: ["葡萄牙语"] },
{ country: "墨西哥", languages: ["西班牙语"] },
{ country: "智利", languages: ["西班牙语"] },
{ country: "哥伦比亚", languages: ["西班牙语"] },
{ country: "阿联酋", languages: ["阿拉伯语", "英文"] },
{ country: "沙特阿拉伯", languages: ["阿拉伯语", "英文"] },
{ country: "俄罗斯", languages: ["俄语"] },
{ country: "波兰", languages: ["波兰语"] },
];
export const marketOptions = marketLanguageOptions.map((option) => option.country);
export const languageOptions = Array.from(new Set(marketLanguageOptions.flatMap((option) => option.languages)));
export const languageAliases: Record<string, string> = {
"英文": "英文",
"中文": "中文",
"英语": "英文",
"日语": "日文",
"日文": "日文",
"德语": "德文",
"德文": "德文",
"法语": "法文",
"法文": "法文",
"韩语": "韩文",
"韩文": "韩文",
"西文": "西班牙语",
"西班牙语": "西班牙语",
"葡文": "葡萄牙语",
"葡萄牙语": "葡萄牙语",
"印尼语": "印度尼西亚语",
"印度尼西亚语": "印度尼西亚语",
"菲律宾语": "菲律宾语(他加禄语)",
"菲律宾语(他加禄语)": "菲律宾语(他加禄语)",
};
export const defaultPlatformSpec = platformSpecOptions[0]!;
export const getPlatformSpec = (value: string) =>
platformSpecOptions.find((option) => option.label === value || option.aliases?.includes(value)) ?? defaultPlatformSpec;
export const legacyPlatformAliases: Record<string, string> = {
"淘宝/天猫": "淘宝/天猫",
"京东": "京东",
"拼多多": "拼多多",
"抖音电商": "抖音电商",
"亚马逊Amazon": "亚马逊 Amazon",
"速卖通": "速卖通",
};
export const normalizePlatform = (value: string) => getPlatformSpec(legacyPlatformAliases[value] ?? value).label;
export const domesticPlatformLabels = new Set(["淘宝/天猫", "京东", "拼多多", "抖音电商"]);
export const domesticPlatformLanguages = ["中文"];
export const isDomesticPlatform = (platformValue: string) => domesticPlatformLabels.has(normalizePlatform(platformValue));
export const getPlatformRatioGroup = (value: string, mode?: PlatformRatioModeKey): PlatformRatioGroup => {
const platformSpec = getPlatformSpec(value);
return (mode ? platformSpec.ratioGroups?.[mode] : null) ?? {
ratios: platformSpec.ratios,
defaultRatio: platformSpec.defaultRatio,
};
};
export const getPlatformRatioOptions = (value: string, mode?: PlatformRatioModeKey) => getPlatformRatioGroup(value, mode).ratios;
export const getPlatformDefaultRatio = (value: string, mode?: PlatformRatioModeKey) => getPlatformRatioGroup(value, mode).defaultRatio;
export const getUniqueRatioOptions = (ratios: string[]) => Array.from(new Set(ratios));
export const normalizeRatioForPlatform = (platformValue: string, ratioValue: string, mode?: PlatformRatioModeKey) => {
const platformRatios = getPlatformRatioOptions(platformValue, mode);
if (platformRatios.includes(ratioValue)) return ratioValue;
const normalizedRatio = normalizeRatioToken(ratioValue);
const matchedRatio = platformRatios.find((option) => normalizeRatioToken(option).includes(normalizedRatio));
return matchedRatio ?? getPlatformDefaultRatio(platformValue, mode);
};
export const defaultMarketLanguageOption = marketLanguageOptions[0]!;
export const normalizeMarket = (value: string) =>
marketLanguageOptions.some((option) => option.country === value) ? value : defaultMarketLanguageOption.country;
export const normalizeLanguage = (value: string) => languageAliases[value] ?? value;
export const uniqueLanguages = (languages: string[]) => Array.from(new Set(languages));
export const appendEnglish = (languages: string[]) => Array.from(new Set([...languages, "英文"]));
export const getMarketLanguageOptions = (marketValue: string) =>
appendEnglish((marketLanguageOptions.find((option) => option.country === marketValue) ?? defaultMarketLanguageOption).languages);
export const getPlatformLanguageOptions = (platformValue: string, marketValue: string) => {
const marketLanguages = getMarketLanguageOptions(marketValue);
if (!isDomesticPlatform(platformValue)) return marketLanguages;
const localLanguages = marketLanguages.filter((item) => item !== "英文");
return uniqueLanguages([...localLanguages, ...domesticPlatformLanguages, "英文"]);
};
export const getPlatformDefaultLanguage = (platformValue: string, marketValue: string) =>
isDomesticPlatform(platformValue) ? "中文" : (getPlatformLanguageOptions(platformValue, marketValue)[0] ?? languageOptions[0] ?? "英文");
export const normalizeLanguageForPlatform = (platformValue: string, marketValue: string, languageValue: string) => {
const normalizedLanguage = normalizeLanguage(languageValue);
const platformLanguages = getPlatformLanguageOptions(platformValue, marketValue);
return platformLanguages.includes(normalizedLanguage) ? normalizedLanguage : getPlatformDefaultLanguage(platformValue, marketValue);
};
export const defaultEcommercePlatform = "淘宝/天猫";
export const defaultProductSetOutput: ProductSetOutputKey = "set";
export const defaultCloneOutput: CloneOutputKey = "set";
export const formatUploadedImageRatio = (image?: { width?: number; height?: number; format?: string }) => {
if (!image) return null;
const format = image.format ? `\u00a0\u00a0\u00a0${image.format}` : "";
if (!image.width || !image.height) return `上传图片\u00a0\u00a0\u00a0原图比例${format}`;
return `上传图片 ${image.width}×${image.height}px\u00a0\u00a0\u00a0${formatAspectRatio(image.width, image.height)}${format}`;
};
@@ -0,0 +1,83 @@
import { describe, expect, it } from "vitest";
import {
buildDetailModulePrompt,
buildEcommerceImagePrompt,
buildSetSubPrompt,
setCountLabels,
type EcommercePromptDetailModule,
} from "./promptBuilder";
const detailModules: EcommercePromptDetailModule[] = [
{ id: "hero", title: "首页焦点图", desc: "集中呈现核心利益点" },
{ id: "usage", title: "使用情境图", desc: "还原实际使用画面" },
{ id: "spec", title: "参数信息表", desc: "整理商品关键数据" },
];
describe("buildDetailModulePrompt", () => {
it("uses the complete-detail prompt when no modules are selected", () => {
expect(buildDetailModulePrompt([], detailModules)).toContain("complete A+ detail layout");
});
it("includes only selected modules", () => {
const prompt = buildDetailModulePrompt(["hero", "spec"], detailModules);
expect(prompt).toContain("首页焦点图: 集中呈现核心利益点");
expect(prompt).toContain("参数信息表: 整理商品关键数据");
expect(prompt).not.toContain("使用情境图");
});
it("returns an empty prompt for unknown selected ids", () => {
expect(buildDetailModulePrompt(["missing"], detailModules)).toBe("");
});
});
describe("buildSetSubPrompt", () => {
it("builds white-background prompts with strict background guidance", () => {
const prompt = buildSetSubPrompt("white", 0, 1, "淘宝/天猫", "1:1", "中文", "中国");
expect(prompt).toContain(setCountLabels.white.label);
expect(prompt).toContain("clean white-background product image");
expect(prompt).toContain("Platform: 淘宝/天猫. Aspect ratio: 1:1. Language/copy: 中文. Market: 中国.");
});
it("adds variant guidance when generating multiple images", () => {
expect(buildSetSubPrompt("scene", 1, 3, "Amazon", "3:4", "英文", "美国")).toContain("variant 2 of 3");
});
});
describe("buildEcommerceImagePrompt", () => {
it("builds detail prompts with selected A+ modules", () => {
const prompt = buildEcommerceImagePrompt(
"detail",
"突出轻量化",
"京东",
"3:4",
"中文",
"中国",
{ detailModules: ["usage"] },
detailModules,
);
expect(prompt).toContain("professional A+ detail page");
expect(prompt).toContain("使用情境图: 还原实际使用画面");
expect(prompt).toContain("Additional user requirements: 突出轻量化");
});
it("builds model prompts with model attributes and scenes", () => {
const prompt = buildEcommerceImagePrompt(
"model",
"",
"Shopee",
"3:4",
"英文",
"美国",
{ gender: "女", age: "青年", ethnicity: "亚洲人", body: "标准", appearance: "短发", scenes: ["都市街头"], smartScene: true },
);
expect(prompt).toContain("Model gender: 女.");
expect(prompt).toContain("Background scenes: 都市街头.");
expect(prompt).toContain("Use smart scene matching");
});
it("builds hot-replication prompts", () => {
const prompt = buildEcommerceImagePrompt("hot", "", "TikTok Shop", "9:16", "英文", "美国");
expect(prompt).toContain("closely replicates the style");
expect(prompt).toContain("TikTok Shop marketplace standards");
});
});
@@ -0,0 +1,113 @@
import type { CloneOutputKey } from "./platformRules";
export type EcommerceSetCountKey = "selling" | "white" | "scene";
export interface EcommercePromptDetailModule {
id: string;
title: string;
desc: string;
}
export interface EcommerceImagePromptOptions {
gender?: string;
age?: string;
ethnicity?: string;
body?: string;
appearance?: string;
scenes?: string[];
customScene?: string;
smartScene?: boolean;
detailModules?: string[];
}
export const setCountLabels: Record<EcommerceSetCountKey, { label: string; promptDesc: string }> = {
selling: { label: "卖点图", promptDesc: "selling-point infographic image highlighting core product advantages and detail close-ups" },
white: { label: "白底图", promptDesc: "clean white-background product photo showing the item from its best angle, studio lighting, no props" },
scene: { label: "场景图", promptDesc: "lifestyle scene image showing the product in a realistic usage environment with natural surroundings" },
};
export const buildDetailModulePrompt = (moduleIds: string[], modules: EcommercePromptDetailModule[]): string => {
if (!moduleIds.length) {
return "Generate a complete A+ detail layout with hero, selling points, usage scene, product detail, and specification modules.";
}
const selectedModules = modules.filter((module) => moduleIds.includes(module.id));
if (!selectedModules.length) return "";
const moduleList = selectedModules.map((module) => `${module.title}: ${module.desc}`).join("; ");
return `Only generate these selected A+ detail modules, no extra modules: ${moduleList}. Keep the output focused even if only one or two modules are selected.`;
};
export const buildSetSubPrompt = (
countKey: EcommerceSetCountKey,
index: number,
totalCount: number,
platform: string,
ratio: string,
language: string,
market: string,
): string => {
const info = setCountLabels[countKey];
const parts: string[] = [];
parts.push(`Generate an e-commerce ${info.label.toLowerCase()} for a product listing.`);
parts.push(info.promptDesc);
if (countKey === "white") {
parts.push("The output must be a clean white-background product image. Do not use lifestyle backgrounds, props, text overlays, or people.");
}
if (countKey === "scene") {
parts.push("The output must be a realistic usage scene image. Keep the product clearly visible and preserve its shape, color, and key details.");
}
if (countKey === "selling") {
parts.push("The output must be a selling-point graphic with clear hierarchy, concise copy, and product detail callouts.");
}
if (totalCount > 1) {
parts.push(`This is variant ${index + 1} of ${totalCount} —vary the angle, composition, or emphasis to make each distinct.`);
}
parts.push(`Platform: ${platform}. Aspect ratio: ${ratio}. Language/copy: ${language}. Market: ${market}.`);
parts.push("Must comply with platform image guidelines —proper margins, no watermark, professional quality.");
return parts.join(" ");
};
export const buildEcommerceImagePrompt = (
outputKey: CloneOutputKey,
userText: string,
platform: string,
ratio: string,
language: string,
market: string,
options?: EcommerceImagePromptOptions,
detailModules: EcommercePromptDetailModule[] = [],
): string => {
const parts: string[] = [];
if (outputKey === "detail") {
parts.push("Generate a professional A+ detail page hero image for an e-commerce product listing.");
parts.push("Create a high-impact first-screen visual that combines the product photo with key selling points, usage scenes, and detailed specifications in a cohesive layout.");
parts.push(`Platform: ${platform}. Aspect ratio: ${ratio}. Language/copy: ${language}. Market: ${market}.`);
if (options?.detailModules) parts.push(buildDetailModulePrompt(options.detailModules, detailModules));
parts.push("Follow platform A+ page best practices —clear hierarchy, professional typography, high visual impact.");
} else if (outputKey === "model") {
parts.push("Generate model/try-on lifestyle images for an e-commerce product listing.");
parts.push("Show the product being used or worn by a model in attractive lifestyle settings.");
parts.push(`Platform: ${platform}. Aspect ratio: ${ratio}. Language/copy: ${language}. Market: ${market}.`);
if (options) {
if (options.gender) parts.push(`Model gender: ${options.gender}.`);
if (options.age) parts.push(`Model age: ${options.age}.`);
if (options.ethnicity) parts.push(`Model ethnicity: ${options.ethnicity}.`);
if (options.body) parts.push(`Model body type: ${options.body}.`);
if (options.appearance) parts.push(`Model appearance details: ${options.appearance}.`);
if (options.scenes?.length) parts.push(`Background scenes: ${options.scenes.join(", ")}.`);
if (options.customScene) parts.push(`Custom background scene: ${options.customScene}.`);
if (options.smartScene) parts.push("Use smart scene matching to select the best background context.");
}
parts.push("Model should appear natural and appealing. Background should complement the product. Image must meet platform standards.");
} else if (outputKey === "hot") {
parts.push("Generate a high-conversion e-commerce product image that closely replicates the style and composition of the reference image while adapting it to the target platform.");
parts.push(`Replicate the visual style, color palette, and layout feel of the source product image, then adapt it for ${platform} marketplace standards.`);
parts.push(`Platform: ${platform}. Aspect ratio: ${ratio}. Language/copy: ${language}. Market: ${market}.`);
parts.push("The result must look professional and optimized for high click-through rate and conversion on the specified platform.");
}
if (userText.trim()) {
parts.push(`Additional user requirements: ${userText.trim()}`);
}
return parts.join(" ");
};
@@ -0,0 +1,170 @@
import { describe, it, expect } from "vitest";
import {
normalizeRatioToken,
greatestCommonDivisor,
formatAspectRatio,
getQuickSetRatioValue,
formatRatioDisplayValue,
getRatioDisplayParts,
parseRatioToAspectCss,
toSupportedImageApiRatio,
normalizeRatioForApi,
} from "./ratioUtils";
describe("normalizeRatioToken", () => {
it("normalizes non-breaking spaces", () => {
expect(normalizeRatioToken("800\u00a0\u00a0px")).toBe("800 px");
});
it("replaces plain separators with the normal multiplication sign", () => {
expect(normalizeRatioToken("800*800")).toBe("800×800");
});
it("replaces legacy mojibake multiply signs", () => {
expect(normalizeRatioToken("800\u8133800")).toBe("800×800");
});
it("replaces fullwidth and legacy mojibake colons", () => {
expect(normalizeRatioToken("11")).toBe("1:1");
expect(normalizeRatioToken("1\u951b?1")).toBe("1:1");
});
it("collapses whitespace and trims", () => {
expect(normalizeRatioToken(" 1 : 1 ")).toBe("1 : 1");
});
});
describe("greatestCommonDivisor", () => {
it("computes GCD", () => {
expect(greatestCommonDivisor(12, 8)).toBe(4);
expect(greatestCommonDivisor(1920, 1080)).toBe(120);
});
it("handles zero with fallback to 1", () => {
expect(greatestCommonDivisor(0, 5)).toBe(5);
expect(greatestCommonDivisor(0, 0)).toBe(1);
});
it("handles negatives via abs", () => {
expect(greatestCommonDivisor(-12, 8)).toBe(4);
});
});
describe("formatAspectRatio", () => {
it("reduces 1920x1080 to 16:9", () => {
expect(formatAspectRatio(1920, 1080)).toBe("16:9");
});
it("reduces 750x1000 to 3:4", () => {
expect(formatAspectRatio(750, 1000)).toBe("3:4");
});
it("reduces 800x800 to 1:1", () => {
expect(formatAspectRatio(800, 800)).toBe("1:1");
});
});
describe("getQuickSetRatioValue", () => {
it("passes through a canonical quick-set value", () => {
expect(getQuickSetRatioValue("1:1")).toBe("1:1");
});
it("derives from a WxH size string", () => {
expect(getQuickSetRatioValue("1920×1080px")).toBe("16:9");
expect(getQuickSetRatioValue("750×1000px")).toBe("3:4");
});
it("derives from a raw ratio string", () => {
expect(getQuickSetRatioValue("9:16")).toBe("9:16");
});
it("falls back to 1:1 for unparseable input", () => {
expect(getQuickSetRatioValue("unknown")).toBe("1:1");
});
});
describe("formatRatioDisplayValue", () => {
it("formats a WxHpx string with aspect suffix", () => {
expect(formatRatioDisplayValue("1000×1000px 1:1")).toBe("1000×1000px\u00a0\u00a0\u00a01:1");
});
it("reformats 800x800px without explicit aspect", () => {
expect(formatRatioDisplayValue("800x800px")).toBe("800×800px\u00a0\u00a0\u00a01:1");
});
it("replaces legacy mojibake product image labels", () => {
expect(formatRatioDisplayValue("\u935f\u55d7\u6427\u9365?")).toBe("商品图");
});
});
describe("getRatioDisplayParts", () => {
it("splits size and aspect", () => {
expect(getRatioDisplayParts("1000×1000px 1:1")).toEqual({
size: "1000×1000px",
aspect: "1:1",
});
});
it("uses 自适应 when no aspect present", () => {
const parts = getRatioDisplayParts("原图");
expect(parts.aspect).toBe("自适应");
});
});
describe("parseRatioToAspectCss", () => {
it("extracts CSS aspect-ratio", () => {
expect(parseRatioToAspectCss("1000×1000px 1:1")).toBe("1000 / 1000");
});
it("falls back to 1 / 1", () => {
expect(parseRatioToAspectCss("no numbers")).toBe("1 / 1");
});
});
describe("toSupportedImageApiRatio", () => {
it("snaps square to 1:1", () => {
expect(toSupportedImageApiRatio(800, 800)).toBe("1:1");
});
it("snaps 750x1000 to 3:4", () => {
expect(toSupportedImageApiRatio(750, 1000)).toBe("3:4");
});
it("snaps 1920x1080 to 16:9", () => {
expect(toSupportedImageApiRatio(1920, 1080)).toBe("16:9");
});
it("snaps 1080x1920 to 9:16", () => {
expect(toSupportedImageApiRatio(1080, 1920)).toBe("9:16");
});
it("snaps 800x600 to 4:3", () => {
expect(toSupportedImageApiRatio(800, 600)).toBe("4:3");
});
it("returns 1:1 for non-finite or non-positive", () => {
expect(toSupportedImageApiRatio(NaN, 100)).toBe("1:1");
expect(toSupportedImageApiRatio(0, 100)).toBe("1:1");
expect(toSupportedImageApiRatio(-1, 100)).toBe("1:1");
});
});
describe("normalizeRatioForApi", () => {
it("extracts the explicit ratio from a display string", () => {
expect(normalizeRatioForApi("1000×1000px 1:1")).toBe("1:1");
expect(normalizeRatioForApi("750×1000px 3:4")).toBe("3:4");
});
it("derives ratio from a bare size string", () => {
expect(normalizeRatioForApi("1920×1080px")).toBe("16:9");
});
it("returns 1:1 for unparseable input", () => {
expect(normalizeRatioForApi("")).toBe("1:1");
expect(normalizeRatioForApi("无尺寸信息")).toBe("1:1");
});
it("uses the last explicit ratio when multiple present", () => {
expect(normalizeRatioForApi("4:3 16:9")).toBe("16:9");
});
});
+125
View File
@@ -0,0 +1,125 @@
// Ratio and dimension formatting helpers.
// Keep compatibility with a few legacy mojibake tokens, but never emit them.
// normalizeRatioForPlatform / formatUploadedImageRatio 因依赖平台规格表与 CloneImageItem
// 暂留在 EcommercePage.tsx,后续随 platformSpec 一起整理。
const LEGACY_MULTIPLY_SIGN = "\u8133";
const LEGACY_FULLWIDTH_COLON = "\u951b?";
const LEGACY_PRODUCT_IMAGE_LABEL = "\u935f\u55d7\u6427\u9365?";
export const normalizeRatioToken = (value: string) =>
value
.replaceAll("\u00a0", " ")
.replaceAll(LEGACY_MULTIPLY_SIGN, "×")
.replaceAll("*", "×")
.replaceAll("", ":")
.replaceAll(LEGACY_FULLWIDTH_COLON, ":")
.replace(/\s+/g, " ")
.trim();
export const quickSetRatioOptions = ["1:1", "3:4", "4:3", "9:16", "16:9"];
export const greatestCommonDivisor = (left: number, right: number): number => {
let a = Math.abs(left);
let b = Math.abs(right);
while (b) {
[a, b] = [b, a % b];
}
return a || 1;
};
export const formatAspectRatio = (width: number, height: number) => {
const divisor = greatestCommonDivisor(width, height);
return `${Math.round(width / divisor)}:${Math.round(height / divisor)}`;
};
export const getQuickSetRatioValue = (value: string) => {
const normalizedValue = normalizeRatioToken(value);
if (quickSetRatioOptions.includes(normalizedValue)) return normalizedValue;
const sizeMatch = normalizedValue.match(/(\d+)\s*[×xX]\s*(\d+)/u);
if (sizeMatch) {
const width = Number(sizeMatch[1]);
const height = Number(sizeMatch[2]);
if (Number.isFinite(width) && Number.isFinite(height) && width > 0 && height > 0) {
const aspect = formatAspectRatio(width, height);
if (quickSetRatioOptions.includes(aspect)) return aspect;
}
}
const ratioMatch = normalizedValue.match(/(\d+)\s*[:]\s*(\d+)/u);
if (ratioMatch) {
const aspect = `${Number(ratioMatch[1])}:${Number(ratioMatch[2])}`;
if (quickSetRatioOptions.includes(aspect)) return aspect;
}
return quickSetRatioOptions[0]!;
};
export const formatRatioDisplayValue = (value: string) => {
const normalizedValue = normalizeRatioToken(value);
const sizeMatch = normalizedValue.match(/(\d+)\s*[×xX]\s*(\d+)\s*px?/u);
if (sizeMatch) {
const width = Number(sizeMatch[1]);
const height = Number(sizeMatch[2]);
return `${width}×${height}px\u00a0\u00a0\u00a0${formatAspectRatio(width, height)}`;
}
return normalizedValue
.replace("淘宝主图 / SKU 图 ", "淘宝主图 / SKU 图 ")
.replace("京东主图 / SKU 图 ", "京东主图 / SKU 图 ")
.replace("详情页宽", "详情页宽")
.replace("短视频", "短视频")
.replace("主图", "主图")
.replace("商品主图", "商品主图")
.replace(LEGACY_PRODUCT_IMAGE_LABEL, "商品图")
.replace(/\s+:/g, ":")
.replace(/:\s+/g, ":");
};
export const getRatioDisplayParts = (value: string) => {
const display = formatRatioDisplayValue(value).replace(/\u00a0/g, " ").replace(/\s+/g, " ").trim();
const aspectMatch = display.match(/(\d+\s*[:]\s*\d+)(?!.*\d+\s*[:]\s*\d+)/u);
const aspect = aspectMatch?.[1]?.replace(/\s+/g, "") ?? "自适应";
const size = aspectMatch ? display.replace(aspectMatch[0], "").trim() : display;
return {
size: size || "原图比例",
aspect,
};
};
/** Extract CSS aspect-ratio from a ratio string like "1000x1000px 1:1" -> "1 / 1" */
export const parseRatioToAspectCss = (ratioStr: string): string => {
const match = ratioStr.match(/(\d+)\D+(\d+)/u);
if (!match) return "1 / 1";
return `${match[1]} / ${match[2]}`;
};
export const supportedImageApiRatios = ["1:1", "3:4", "4:3", "9:16", "16:9"] as const;
export type SupportedImageApiRatio = typeof supportedImageApiRatios[number];
export const toSupportedImageApiRatio = (width: number, height: number): SupportedImageApiRatio => {
if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) return "1:1";
let bestRatio: SupportedImageApiRatio = "1:1";
let bestScore = Number.POSITIVE_INFINITY;
const target = Math.log(width / height);
for (const ratio of supportedImageApiRatios) {
const [left, right] = ratio.split(":").map(Number);
const score = Math.abs(target - Math.log(left / right));
if (score < bestScore) {
bestRatio = ratio;
bestScore = score;
}
}
return bestRatio;
};
/** Normalize ratio display string ("1000×1000px 1:1") to an image API aspect ratio ("1:1"). */
export const normalizeRatioForApi = (ratioStr: string): string => {
const normalizedValue = normalizeRatioToken(ratioStr);
const explicitRatios = Array.from(normalizedValue.matchAll(/(\d+(?:\.\d+)?)\s*:\s*(\d+(?:\.\d+)?)/g));
const explicitRatio = explicitRatios.at(-1);
if (explicitRatio) {
return toSupportedImageApiRatio(Number(explicitRatio[1]), Number(explicitRatio[2]));
}
const sizeMatch = normalizedValue.match(/(\d+(?:\.\d+)?)\s*[×xX*]\s*(\d+(?:\.\d+)?)/u);
if (!sizeMatch) return "1:1";
return toSupportedImageApiRatio(Number(sizeMatch[1]), Number(sizeMatch[2]));
};
@@ -0,0 +1 @@
export * from "./workbenchDownload.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./useGenerationTasks.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./useTypewriter.ts";
-9
View File
@@ -2,15 +2,6 @@ import React from "react";
import ReactDOM from "react-dom/client";
import "./styles/index.css";
import App from "./App";
import { reportError } from "./utils/errorReporting";
window.addEventListener("unhandledrejection", (event) => {
reportError(event.reason, "rejection");
});
window.addEventListener("error", (event) => {
if (event.error) reportError(event.error, "unhandled");
});
const root = document.getElementById("root");
+1
View File
@@ -0,0 +1 @@
export * from "./backgroundTaskRunner.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./index.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./useAppStore.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./useGenerationStore.ts";
+20 -15
View File
@@ -24,17 +24,33 @@ interface PersistedQueueSnapshot {
savedAt: number;
}
const STORAGE_KEY = "omniai:generation-queue";
const STORAGE_KEY_PREFIX = "omniai:generation-queue";
const MAX_ITEMS = 80;
const STALE_MS = 2 * 60 * 60 * 1000; // 2 hours
function hashUserId(): string {
try {
const raw = localStorage.getItem("omniai-web-session");
if (!raw) return "anon";
const parsed = JSON.parse(raw) as { user?: { id?: number | string } };
return String(parsed?.user?.id || "anon");
} catch {
return "anon";
}
}
// 队列按用户分桶持久化:不同账号读写不同 key,避免登出再登他人账号时读到上一个用户的队列。
function getStorageKey(): string {
return `${STORAGE_KEY_PREFIX}:${hashUserId()}`;
}
function loadPersistedQueue(): GenerationQueueItem[] {
try {
const raw = localStorage.getItem(STORAGE_KEY);
const raw = localStorage.getItem(getStorageKey());
if (!raw) return [];
const snapshot = JSON.parse(raw) as PersistedQueueSnapshot;
if (Date.now() - (snapshot.savedAt || 0) > STALE_MS) {
localStorage.removeItem(STORAGE_KEY);
localStorage.removeItem(getStorageKey());
return [];
}
return snapshot.items.filter(
@@ -48,7 +64,7 @@ function loadPersistedQueue(): GenerationQueueItem[] {
function persistQueue(items: GenerationQueueItem[]): void {
try {
const snapshot: PersistedQueueSnapshot = { version: 1, items, savedAt: Date.now() };
localStorage.setItem(STORAGE_KEY, JSON.stringify(snapshot));
localStorage.setItem(getStorageKey(), JSON.stringify(snapshot));
} catch { /* quota exceeded */ }
}
@@ -63,17 +79,6 @@ interface GenerationStoreState {
clearTerminal: () => void;
}
function hashUserId(): string {
try {
const raw = localStorage.getItem("omniai-web-session");
if (!raw) return "anon";
const parsed = JSON.parse(raw) as { user?: { id?: number | string } };
return String(parsed?.user?.id || "anon");
} catch {
return "anon";
}
}
const initialQueue = loadPersistedQueue();
export const useGenerationStore = create<GenerationStoreState>((set, get) => ({
+1
View File
@@ -0,0 +1 @@
export * from "./useProjectStore.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./useSessionStore.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./useTaskStore.ts";
File diff suppressed because it is too large Load Diff
+164 -1
View File
@@ -2930,12 +2930,68 @@
height: auto;
}
.product-clone-page[data-tool="clone"] .clone-ai-canvas-node .clone-ai-main-result {
width: 150px;
}
.product-clone-page[data-tool="clone"] .clone-ai-source-stack {
position: relative;
flex: 0 0 auto;
}
.product-clone-page[data-tool="clone"] .clone-ai-result-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
}
.product-clone-page[data-tool="clone"] .clone-ai-result-stack {
position: relative;
min-width: 0;
}
.product-clone-page[data-tool="clone"] .clone-ai-result-stack > .clone-ai-node-label {
position: absolute;
top: -6px;
left: 50%;
z-index: 5;
transform: translate(-50%, -100%);
white-space: nowrap;
}
.product-clone-page[data-tool="clone"] .clone-ai-source-corner-action {
position: absolute;
top: -6px;
left: 50%;
z-index: 5;
display: inline-flex;
align-items: center;
justify-content: center;
min-height: 28px;
padding: 6px 12px;
border: 1px solid rgba(0, 255, 136, 0.35);
border-radius: 999px;
background: rgba(21, 23, 28, 0.92);
box-shadow: 0 8px 22px rgba(0, 0, 0, 0.28);
color: #d8deed;
font-size: 12px;
font-weight: 900;
line-height: 1;
white-space: nowrap;
cursor: pointer;
transform: translate(-50%, -100%);
transition:
border-color 200ms ease,
transform 200ms ease,
box-shadow 200ms ease;
}
.product-clone-page[data-tool="clone"] .clone-ai-source-corner-action:hover {
border-color: #00ff88;
transform: translate(-50%, -106%);
box-shadow: 0 10px 26px rgba(0, 255, 136, 0.14), 0 8px 22px rgba(0, 0, 0, 0.28);
}
.product-clone-page[data-tool="clone"] .clone-ai-result-grid button {
width: 100%;
height: auto;
@@ -7805,7 +7861,7 @@
.product-set-preview-backdrop {
position: fixed;
inset: 0;
z-index: 100;
z-index: 4000;
display: grid;
place-items: center;
background: rgb(17 24 39 / 58%);
@@ -12046,3 +12102,110 @@ html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[d
grid-row: auto !important;
}
}
/* Composer menu anchors: place option popovers under the clicked control, not under the whole composer. */
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .clone-ai-input-wrapper.ecom-command-composer > .ecom-command-popover.ecom-command-popover {
position: absolute !important;
inset: var(--composer-popover-top, 48px) auto auto var(--composer-popover-left, 0px) !important;
right: auto !important;
bottom: auto !important;
margin: 0 !important;
transform: none !important;
translate: none !important;
z-index: 160 !important;
}
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .clone-ai-input-wrapper.ecom-command-composer > .ecom-command-popover.ecom-command-popover--platform {
grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
}
/* 平台弹窗宽度仅桌面/平板固定;≤640px 由移动端断点的全宽规则接管。 */
@media (min-width: 641px) {
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .clone-ai-input-wrapper.ecom-command-composer > .ecom-command-popover.ecom-command-popover--platform {
width: min(460px, calc(100% - 24px)) !important;
max-width: min(460px, calc(100% - 24px)) !important;
}
}
/* 平台选项:logo + 名称横排,名称过长省略,避免在窄网格里溢出弹窗。 */
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .ecom-command-popover--platform button {
display: flex !important;
align-items: center !important;
justify-content: flex-start !important;
gap: 8px !important;
min-width: 0 !important;
text-align: left !important;
}
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .ecom-command-popover--platform button .ecom-platform-name {
min-width: 0 !important;
overflow: hidden !important;
text-overflow: ellipsis !important;
white-space: nowrap !important;
}
@media (min-width: 641px) {
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .clone-ai-input-wrapper.ecom-command-composer > .ecom-command-popover.ecom-command-popover--languages,
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .clone-ai-input-wrapper.ecom-command-composer > .ecom-command-popover.ecom-command-popover--ratio-picker {
width: max-content !important;
min-width: 200px !important;
max-width: min(420px, calc(100% - var(--composer-popover-left, 0px))) !important;
}
}
/* 宽设置面板固定宽度并靠右对齐 composer避免从靠右的"设置"按钮左对齐展开时顶出右边缘被裁
仅桌面/平板生效640px 由移动端断点的全宽规则接管 */
@media (min-width: 641px) {
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .clone-ai-input-wrapper.ecom-command-composer > .ecom-command-popover.ecom-command-popover--settings {
width: min(520px, calc(100% - 24px)) !important;
max-width: min(520px, calc(100% - 24px)) !important;
left: auto !important;
inset: var(--composer-popover-top, 48px) 12px auto auto !important;
}
}
/* Uploaded assets stay as compact attachments inside the composer hierarchy. */
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .clone-ai-input-wrapper.ecom-command-composer:has(.ecom-command-asset-popover) {
min-height: 0 !important;
}
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .ecom-command-asset-popover,
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .ecom-command-composer-wrap:not(.has-generated.is-compact) .clone-ai-input-wrapper.ecom-command-composer:has(.ecom-command-asset-popover) .ecom-command-asset-popover {
position: static !important;
grid-column: 1 !important;
display: flex !important;
flex-wrap: wrap !important;
align-items: center !important;
justify-content: flex-start !important;
justify-self: start !important;
gap: 10px !important;
width: auto !important;
max-width: 100% !important;
min-height: 0 !important;
max-height: none !important;
padding: 2px 2px 0 !important;
overflow: visible !important;
border: 0 !important;
border-radius: 0 !important;
background: transparent !important;
box-shadow: none !important;
transform: none !important;
}
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .ecom-command-asset-thumb,
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .ecom-command-composer-wrap:not(.has-generated.is-compact) .clone-ai-input-wrapper.ecom-command-composer:has(.ecom-command-asset-popover) .ecom-command-asset-thumb {
flex: 0 0 64px !important;
width: 64px !important;
height: 64px !important;
border-radius: 14px !important;
}
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .ecom-command-asset-add,
html body #root .ecommerce-standalone.ecommerce-standalone .product-clone-page[data-tool="clone"][data-tool="clone"] .ecom-command-composer-wrap:not(.has-generated.is-compact) .clone-ai-input-wrapper.ecom-command-composer:has(.ecom-command-asset-popover) .ecom-command-asset-add {
flex: 0 0 44px !important;
width: 44px !important;
height: 64px !important;
min-height: 44px !important;
margin: 0 !important;
font-size: 24px !important;
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+1
View File
@@ -0,0 +1 @@
export * from "./types.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./enterpriseVideoPolicy.ts";
+5 -1
View File
@@ -15,7 +15,11 @@ let flushTimer: ReturnType<typeof setTimeout> | null = null;
function getSessionId(): string | undefined {
try {
const raw = localStorage.getItem("omniai:session") || sessionStorage.getItem("omniai:session");
const raw =
localStorage.getItem("omniai-web-session") ||
sessionStorage.getItem("omniai-web-session") ||
localStorage.getItem("omniai:session") ||
sessionStorage.getItem("omniai:session");
if (!raw) return undefined;
const parsed = JSON.parse(raw);
return parsed?.user?.sessionId ?? undefined;
+1
View File
@@ -0,0 +1 @@
export * from "./happyHorseRouting.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./pixverseRouting.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./resolveVideoModel.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./taskLifecycle.ts";
+1
View File
@@ -0,0 +1 @@
export * from "./translateTaskError.ts";
+81
View File
@@ -0,0 +1,81 @@
import { describe, it, expect } from "vitest";
import { classifyTaskError, translateTaskError, type TaskErrorCategory } from "./translateTaskError";
// 每条规则至少一个正例,按规则顺序排列(classifyTaskError 先匹配先返回)。
const RULE_CASES: Array<{ name: string; input: string; category: TaskErrorCategory }> = [
{ name: "content policy", input: "content violated our policies", category: "content_policy" },
{ name: "nsfw", input: "image flagged as nsfw", category: "content_policy" },
{ name: "auth 401", input: "401 Unauthorized", category: "auth_failure" },
{ name: "token expired", input: "token expired", category: "auth_failure" },
{ name: "insufficient balance 402", input: "402 Payment Required", category: "insufficient_balance" },
{ name: "余额不足", input: "余额不足", category: "insufficient_balance" },
{ name: "concurrency pool full", input: "concurrency pool is full", category: "concurrency_busy" },
{ name: "rate limit 429", input: "429 Too Many Requests", category: "concurrency_busy" },
{ name: "unsupported model", input: "model not found", category: "unsupported_model" },
{ name: "invalid asset", input: "invalid image format", category: "invalid_asset" },
{ name: "network ECONNREFUSED", input: "fetch failed: ECONNREFUSED", category: "network_failure" },
{ name: "timeout ETIMEDOUT", input: "ETIMEDOUT", category: "timeout" },
{ name: "quota exceeded", input: "quota exceeded", category: "insufficient_balance" },
{ name: "cancelled", input: "task was cancelled", category: "cancelled" },
{ name: "已取消", input: "任务已取消", category: "cancelled" },
{ name: "all providers failed", input: "all providers failed", category: "concurrency_busy" },
{ name: "500 server error", input: "500 Internal Server Error", category: "network_failure" },
{ name: "forbidden 403", input: "403 Forbidden", category: "auth_failure" },
{ name: "aborted", input: "request aborted", category: "timeout" },
];
describe("classifyTaskError rule coverage", () => {
for (const { name, input, category } of RULE_CASES) {
it(`classifies "${name}" as ${category}`, () => {
const result = classifyTaskError(input);
expect(result.category).toBe(category);
expect(result.message).toBeTruthy();
expect(result.action).toBeTruthy();
});
}
});
describe("classifyTaskError edge cases", () => {
it("returns unknown for empty/null/undefined", () => {
expect(classifyTaskError("").category).toBe("unknown");
expect(classifyTaskError(undefined).category).toBe("unknown");
expect(classifyTaskError(null).category).toBe("unknown");
});
it("returns the raw (truncated) message for unrecognized Chinese errors", () => {
const result = classifyTaskError("这是一条未知的中文错误信息");
expect(result.category).toBe("unknown");
expect(result.message).toContain("未知");
expect(result.message).not.toContain("服务异常");
});
it("truncates long Chinese errors to 80 chars + ellipsis", () => {
const long = "错误".repeat(50);
const result = classifyTaskError(long);
expect(result.message.endsWith("...")).toBe(true);
expect(result.message.length).toBeLessThanOrEqual(83);
});
it("returns generic service message for unrecognized English errors", () => {
const result = classifyTaskError("something completely unexpected");
expect(result.category).toBe("unknown");
expect(result.message).toBe("服务异常,请稍后重试");
});
});
describe("classifyTaskError rule ordering (first match wins)", () => {
it("content_policy beats auth_failure when both patterns present", () => {
// "nsfw" appears before "401" in rule order
const result = classifyTaskError("nsfw content with 401");
expect(result.category).toBe("content_policy");
});
});
describe("translateTaskError", () => {
it("returns the message from classifyTaskError", () => {
expect(translateTaskError("401")).toBe("登录已过期,请重新登录");
});
it("returns generic message for empty input", () => {
expect(translateTaskError("")).toBe("任务失败,请重试");
});
});
+87
View File
@@ -0,0 +1,87 @@
import { describe, it, expect } from "vitest";
import { resolveHappyHorseRequestModel, HAPPY_HORSE_UI_MODEL, HAPPY_HORSE_T2V_MODEL, HAPPY_HORSE_I2V_MODEL, HAPPY_HORSE_R2V_MODEL } from "./happyHorseRouting";
import { resolveViduRequestModel, VIDU_UI_MODEL, VIDU_T2V_MODEL, VIDU_I2V_MODEL } from "./viduRouting";
import { resolvePixverseRequestModel, PIXVERSE_UI_MODEL, PIXVERSE_T2V_MODEL, PIXVERSE_I2V_MODEL, PIXVERSE_KF2V_MODEL } from "./pixverseRouting";
type ResolveFn = (input: { model: string; referenceUrls?: string[]; imageReferenceCount?: number }) => string;
// 三家路由在参考图数量上的分支差异是回归测试重点。
// HappyHorse: 0->t2v, 1->i2v, >=2->r2v
// Vidu: 0->t2v, >=1->i2v (无 r2v)
// Pixverse: 0->t2v, 1->i2v, >=2->kf2v
describe.each([
{ name: "HappyHorse", resolve: resolveHappyHorseRequestModel, ui: HAPPY_HORSE_UI_MODEL, t2v: HAPPY_HORSE_T2V_MODEL, i2v: HAPPY_HORSE_I2V_MODEL, third: HAPPY_HORSE_R2V_MODEL },
{ name: "Vidu", resolve: resolveViduRequestModel, ui: VIDU_UI_MODEL, t2v: VIDU_T2V_MODEL, i2v: VIDU_I2V_MODEL, third: null },
{ name: "Pixverse", resolve: resolvePixverseRequestModel, ui: PIXVERSE_UI_MODEL, t2v: PIXVERSE_T2V_MODEL, i2v: PIXVERSE_I2V_MODEL, third: PIXVERSE_KF2V_MODEL },
] as Array<{ name: string; resolve: ResolveFn; ui: string; t2v: string; i2v: string; third: string | null }>)(
"$name routing by imageReferenceCount",
({ resolve, ui, t2v, i2v, third }) => {
it("returns the input model unchanged when it is not this provider", () => {
expect(resolve({ model: "some-other-model" })).toBe("some-other-model");
});
it("routes 0 reference images to t2v", () => {
expect(resolve({ model: ui, imageReferenceCount: 0 })).toBe(t2v);
});
it("routes 1 reference image to i2v", () => {
expect(resolve({ model: ui, imageReferenceCount: 1 })).toBe(i2v);
});
if (third) {
it("routes >=2 reference images to the third model", () => {
expect(resolve({ model: ui, imageReferenceCount: 2 })).toBe(third);
expect(resolve({ model: ui, imageReferenceCount: 5 })).toBe(third);
});
} else {
it("routes >=1 reference images to i2v (no third model for this provider)", () => {
expect(resolve({ model: ui, imageReferenceCount: 2 })).toBe(i2v);
expect(resolve({ model: ui, imageReferenceCount: 5 })).toBe(i2v);
});
}
},
);
describe("reference count fallback (referenceUrls when imageReferenceCount omitted)", () => {
it("HappyHorse counts non-empty urls", () => {
expect(
resolveHappyHorseRequestModel({
model: HAPPY_HORSE_UI_MODEL,
referenceUrls: ["", " ", "https://example.com/a.png"],
}),
).toBe(HAPPY_HORSE_I2V_MODEL);
});
it("Vidu falls back to 0 when all urls are empty/whitespace", () => {
expect(
resolveViduRequestModel({
model: VIDU_UI_MODEL,
referenceUrls: ["", " "],
}),
).toBe(VIDU_T2V_MODEL);
});
it("Pixverse counts two non-empty urls as kf2v", () => {
expect(
resolvePixverseRequestModel({
model: PIXVERSE_UI_MODEL,
referenceUrls: ["https://a.png", "https://b.png"],
}),
).toBe(PIXVERSE_KF2V_MODEL);
});
it("imageReferenceCount takes precedence over referenceUrls length", () => {
// Even though referenceUrls has 3 entries, explicit count of 0 wins.
expect(
resolveHappyHorseRequestModel({
model: HAPPY_HORSE_UI_MODEL,
referenceUrls: ["a", "b", "c"],
imageReferenceCount: 0,
}),
).toBe(HAPPY_HORSE_T2V_MODEL);
});
it("handles undefined referenceUrls with undefined count", () => {
expect(resolveViduRequestModel({ model: VIDU_UI_MODEL })).toBe(VIDU_T2V_MODEL);
});
});
+1
View File
@@ -0,0 +1 @@
export * from "./viduRouting.ts";
+1 -1
View File
@@ -16,5 +16,5 @@
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src", "vite.config.ts"]
"include": ["src", "vite.config.ts", "vitest.config.ts"]
}
+18 -16
View File
@@ -2,8 +2,22 @@ import react from "@vitejs/plugin-react";
import { compression } from "vite-plugin-compression2";
import { defineConfig } from "vite";
export default defineConfig(() => {
const devApiTarget = process.env.OMNIAI_DEV_API_TARGET;
export default defineConfig(({ command }) => {
// dev 模式下默认把 /api 代理到线上电商后端,本地 `npm run dev` 即可直接登录/生成。
// 想连本地或 SSH 隧道的后端时,用环境变量覆盖:
// $env:OMNIAI_DEV_API_TARGET="http://127.0.0.1:3601"; npm run dev
// 仅 dev 代理用途,不会打进生产构建产物。
const devApiTarget =
process.env.OMNIAI_DEV_API_TARGET?.trim() ||
(command === "serve" ? "https://omniai.com.cn" : "");
const apiProxy = devApiTarget
? {
"/api": {
target: devApiTarget,
changeOrigin: true,
},
}
: undefined;
return {
plugins: [
@@ -13,25 +27,13 @@ export default defineConfig(() => {
server: {
port: 5173,
host: "127.0.0.1",
proxy: devApiTarget ? {
"/api": {
target: devApiTarget,
changeOrigin: true,
},
} : {
"/api": {
target: "http://47.110.225.76:3601",
changeOrigin: true,
},
},
...(apiProxy ? { proxy: apiProxy } : {}),
},
preview: {
port: 4174,
host: "127.0.0.1",
},
esbuild: {
drop: ["console", "debugger"],
},
...(command === "build" ? { esbuild: { drop: ["console", "debugger"] } } : {}),
build: {
sourcemap: false,
rollupOptions: {
+16
View File
@@ -0,0 +1,16 @@
import { defineConfig } from "vitest/config";
// Vitest 配置独立于 vite.config.ts,避免影响 dev/build。
// 本轮只测纯函数(颜色/比例/平台/路由/错误翻译),用 node 环境即可,无需 jsdom。
// 后续要做组件测试时,再在 test.environment 切到 jsdom 并装 @testing-library/react。
export default defineConfig({
test: {
environment: "node",
include: ["src/**/*.{test,spec}.{ts,tsx}"],
coverage: {
provider: "v8",
include: ["src/**/*.{ts,tsx}"],
exclude: ["src/**/*.test.*", "src/**/*.spec.*", "src/main.tsx", "src/vite-env.d.ts"],
},
},
});