Skip to content

中文版 AI 姿势参考图库 · 1439 组 / 2878 张图片 / 完整汉化 · 零依赖静态画廊

fix(comfyui): 默认改用本机可用的 Qwen Image Edit 2509 三图工作流

San12582ca381ca
diff --git a/README.md b/README.md
index 06f1a1e..e94dc0b 100644
--- a/README.md
+++ b/README.md
@@ -58,6 +58,8 @@
- **本地 ComfyUI 工作流引擎**:支持自动将三合一拼图及单张人像/服装/姿势图上传至本地 ComfyUI(`/upload/image`),注入自定义 API 工作流占位符或自动识别节点提交 `/prompt` 队列并轮询取回成品图;支持导入/导出通用 `.api.json` 工作流模板。
- **跨页面后台生图 & IndexedDB 历史记录**:切换页面不会中断正在进行的生图请求;生成结果自动存入浏览器 IndexedDB(避免 `localStorage` 容量限制),支持复用提示词与原图下载。
+> 本地 ComfyUI 三图合成(Qwen Image Edit 2509)的模型清单、工作流说明和本地运行方法见 [docs/comfyui-local.md](docs/comfyui-local.md)。
+
### 3. ComfyUI 原生自定义节点插件(`comfyui_plugin/`)
将本仓库克隆到 `ComfyUI/custom_nodes/ai-pose-gallery-zh` 后,ComfyUI 画布右键菜单 **「AI角色三合一参考图集」** 下自动提供 4 个节点:
- **AI 姿势参考图 (`PoseGalleryNode`)**:输出 `姿势图 (IMAGE)`、`图片ID`、`姿势提示词`
diff --git a/docs/comfyui-local.md b/docs/comfyui-local.md
new file mode 100644
index 0000000..9b4e1c9
--- /dev/null
+++ b/docs/comfyui-local.md
@@ -0,0 +1,56 @@
+# 本地 ComfyUI 三图合成(Qwen Image Edit 2509)
+
+人像(图1:脸与身份)+ 服装(图2:款式、材质、颜色)+ 姿势(图3:动作与构图)→ 一张合成人物图。
+
+## 所需模型(ComfyUI 官方 Comfy-Org/Qwen-Image_ComfyUI 仓库)
+
+| 用途 | 文件 | 目录 | 大小 |
+| --- | --- | --- | --- |
+| 扩散模型 | `qwen_image_edit_2509_fp8_e4m3fn.safetensors` | `models/diffusion_models` 或 `models/unet` | 20.4 GB |
+| 文本/视觉编码器 | `qwen_2.5_vl_7b_fp8_scaled.safetensors` | `models/text_encoders` | 9.4 GB |
+| VAE | `qwen_image_vae.safetensors` | `models/vae` | 254 MB |
+| 4 步加速 LoRA(2509 专用) | `Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors` | `models/loras` | 850 MB |
+
+不需要任何自定义节点,ComfyUI 0.3.60 以上的内置节点即可(实测 0.43.0)。
+
+## 工作流
+
+`src/workflows/qwen_image_edit_2509_3img.api.json`(API 格式,可直接 `POST /prompt`,也可在合成台「导出 .json」)。
+
+- `UNETLoader → LoraLoaderModelOnly(Lightning) → ModelSamplingAuraFlow(shift=3) → CFGNorm → KSampler(4 步, cfg=1, euler/simple)`,与官方模板一致。
+- 三张图都送入 `TextEncodeQwenImageEditPlus`(视觉语言理解,按「图1/图2/图3」指代)。
+- **只有人像和服装**通过 `ReferenceLatent` 作为像素级参考;姿势图只用于语义理解。
+ 实测如果把线稿人偶姿势图也作为像素参考,生成结果会照搬人偶的关节球和分块线条。
+- 输出尺寸按姿势图比例缩放到约 1 MP(节点 10 的 `megapixels`)。
+- cfg=1 时反向提示词不生效,「不要文字/水印」等要求写在正向提示词里。
+
+旧的 Qwen-Image 2.1 预设仍保留,但需要另行安装 2.1 模型;合成台在提交前会检测并列出缺失的模型文件。
+
+## 本地运行
+
+```bash
+corepack pnpm install
+corepack pnpm dev
+```
+
+打开 http://localhost:5173/studio,接口协议选「本地 ComfyUI」。
+本地运行时 ComfyUI 地址填 `/comfy`(默认值):Vite 把 `/comfy/*` 同源代理到 `http://127.0.0.1:8000`,
+因此不需要修改 ComfyUI 的 `--enable-cors-header`。ComfyUI 在其他端口时:
+
+```bash
+COMFYUI_URL=http://127.0.0.1:8188 corepack pnpm dev
+```
+
+直连(不经代理)时,ComfyUI 启动参数需要 `--enable-cors-header "<网页的来源>"`,该参数只能填一个来源。
+
+## 显存与速度(RTX 4070 SUPER 12 GB / 32 GB 内存实测)
+
+- 首次生成约 50 秒(含加载模型),之后每张约 25–30 秒。
+- 20 GB 的 fp8 模型超过 12 GB 显存,ComfyUI 会自动部分卸载到内存,无需额外设置。
+- 8 GB 显卡:把节点 10 的 `megapixels` 调到 0.6,关闭其他占用显卡的程序;显存不足时合成台会提示并调用 `/free` 释放显存。
+
+## 已知限制
+
+- 个别随机种子会在角落生成无意义的“标志”文字,换一个种子重新生成即可。
+- 服装名称与图片不一致时(如名称写“牛仔”但图中是粉色),以图片为准,但不能保证每次都正确。
+- 线上站点 https://ai-pose-gallery.anhao.app 由原作者部署,本仓库的修改不会自动同步到该站点。
diff --git a/src/routes/settings.tsx b/src/routes/settings.tsx
index bc23358..c4a7ceb 100644
--- a/src/routes/settings.tsx
+++ b/src/routes/settings.tsx
@@ -6,6 +6,7 @@ import {
EyeIcon,
EyeOffIcon,
RotateCcwIcon,
+ ScanSearchIcon,
SparklesIcon,
} from 'lucide-react'
import { useState } from 'react'
@@ -16,7 +17,7 @@ import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
import { Switch } from '@/components/ui/switch'
-import { PROVIDER_META, testComfyConnection } from '@/services/aiGenerator'
+import { detectComfyUi, PROVIDER_META, testComfyConnection } from '@/services/aiGenerator'
import { useStudio } from '@/store/studioStore'
import type { ImagePassMode, ImageProviderId } from '@/types/gallery'
@@ -41,8 +42,25 @@ function SettingsPage() {
try {
const info = await testComfyConnection(pCfg.baseUrl, pCfg.apiKey)
toast.success(`连接成功:${info}`)
- } catch {
- toast.error('连接失败,请确认 ComfyUI 已启动且添加了 --enable-cors-header *')
+ } catch (e) {
+ toast.error(e instanceof Error ? e.message : '连接失败', { duration: 10000 })
+ } finally {
+ setTestingComfy(false)
+ }
+ }
+
+ const handleDetectComfy = async () => {
+ setTestingComfy(true)
+ try {
+ const found = await detectComfyUi(pCfg.baseUrl, pCfg.apiKey)
+ if (!found) {
+ toast.error('未找到可用的 ComfyUI(已尝试当前地址、127.0.0.1:8000、127.0.0.1:8188),请确认已启动并检查跨域设置', {
+ duration: 10000,
+ })
+ return
+ }
+ updateProviderConfig('comfyui', { baseUrl: found.baseUrl })
+ toast.success(`已识别:${found.info}(${found.baseUrl})`)
} finally {
setTestingComfy(false)
}
@@ -100,10 +118,16 @@ function SettingsPage() {
actions={
<div className="flex items-center gap-1.5">
{pid === 'comfyui' && (
- <Button variant="outline" size="sm" onClick={handleTestComfy} disabled={testingComfy}>
- <ActivityIcon className="size-3.5" />
- {testingComfy ? '检测中…' : '测试连接'}
- </Button>
+ <>
+ <Button variant="outline" size="sm" onClick={handleDetectComfy} disabled={testingComfy}>
+ <ScanSearchIcon className="size-3.5" />
+ 自动识别
+ </Button>
+ <Button variant="outline" size="sm" onClick={handleTestComfy} disabled={testingComfy}>
+ <ActivityIcon className="size-3.5" />
+ {testingComfy ? '检测中…' : '测试连接'}
+ </Button>
+ </>
)}
<Button variant="ghost" size="sm" asChild>
<a href={meta.docUrl} target="_blank" rel="noreferrer">
@@ -132,12 +156,12 @@ function SettingsPage() {
label={pid === 'comfyui' ? 'ComfyUI 地址 (Base URL)' : 'Base URL'}
hint={
pid === 'comfyui'
- ? '启动 ComfyUI 需加参数 --enable-cors-header *(秋叶整合包勾选「允许跨域」)'
+ ? `本地运行本站时填 /comfy(同源代理,无需改跨域设置);直连时 ComfyUI 需加启动参数 --enable-cors-header "${typeof window !== 'undefined' ? window.location.origin : '*'}"。Desktop 版默认端口 8000,便携版 8188`
: undefined
}
>
<Input
- type="url"
+ type={pid === 'comfyui' ? 'text' : 'url'}
value={pCfg.baseUrl}
onChange={(e) => updateProviderConfig(pid, { baseUrl: e.target.value })}
placeholder={meta.defaultBaseUrl}
diff --git a/src/routes/studio.tsx b/src/routes/studio.tsx
index e45a60a..a60bdd9 100644
--- a/src/routes/studio.tsx
+++ b/src/routes/studio.tsx
@@ -2,6 +2,8 @@ import { useQueryClient } from '@tanstack/react-query'
import { createFileRoute, Link } from '@tanstack/react-router'
import {
ChevronDownIcon,
+ CircleAlertIcon,
+ CircleCheckIcon,
ChevronUpIcon,
CodeXmlIcon,
CopyIcon,
@@ -14,6 +16,7 @@ import {
LinkIcon,
LoaderCircleIcon,
PersonStandingIcon,
+ ScanSearchIcon,
RotateCcwIcon,
ShirtIcon,
SlidersHorizontalIcon,
@@ -37,7 +40,11 @@ import { Textarea } from '@/components/ui/textarea'
import {
autoDetectComfyMappings,
buildRecipePrompt,
+ type ComfyCheckResult,
+ type ComfyPresetId,
+ checkComfyWorkflow,
COMFYUI_WORKFLOW_TEMPLATES,
+ detectComfyUi,
fileToDataUrl,
parseComfyWorkflowNodes,
PROVIDER_META,
@@ -73,6 +80,8 @@ function StudioPage() {
setPreviewUrl,
isGenerating,
generatingModel,
+ generationProgress,
+ generationError,
startGeneration,
setPortrait,
setCostume,
@@ -108,6 +117,8 @@ function StudioPage() {
const [zoomImageUrl, setZoomImageUrl] = useState<string | null>(null)
const [showComfyJsonModal, setShowComfyJsonModal] = useState(false)
const [isDragging, setIsDragging] = useState(false)
+ const [comfyCheck, setComfyCheck] = useState<(ComfyCheckResult & { info?: string }) | null>(null)
+ const [checkingComfy, setCheckingComfy] = useState(false)
const parsedComfyNodes = useMemo(
() => parseComfyWorkflowNodes(apiConfig.comfyuiParams.workflowJson),
@@ -115,6 +126,7 @@ function StudioPage() {
)
const handleSelectComfyPreset = (preset: ComfyUiParams['workflowPreset']) => {
+ setComfyCheck(null)
if (preset === 'custom') {
updateComfyUiParams({ workflowPreset: 'custom' })
return
@@ -128,6 +140,44 @@ function StudioPage() {
toast.success(`已切换为:${tpl.label}`)
}
+ // 自动识别 ComfyUI 地址 + 检测工作流所需节点/模型是否在本机存在
+ const handleCheckComfy = async () => {
+ setCheckingComfy(true)
+ setComfyCheck(null)
+ try {
+ const cfg = apiConfig.providers.comfyui
+ const found = await detectComfyUi(cfg.baseUrl, cfg.apiKey)
+ if (!found) {
+ setComfyCheck({
+ ok: false,
+ errors: [
+ `未找到可用的 ComfyUI 服务(已尝试:${cfg.baseUrl || '默认地址'}、127.0.0.1:8000、127.0.0.1:8188)。请确认 ComfyUI 已启动,并检查跨域 (CORS) 设置。`,
+ ],
+ warnings: [],
+ fixes: [],
+ })
+ return
+ }
+ if (found.baseUrl !== cfg.baseUrl.trim().replace(/\/+$/, '')) {
+ updateProviderConfig('comfyui', { baseUrl: found.baseUrl })
+ toast.success(`已自动识别 ComfyUI 地址:${found.baseUrl}`)
+ }
+ const cp = apiConfig.comfyuiParams
+ const result = await checkComfyWorkflow(found.baseUrl, cfg.apiKey, cp.workflowJson, [
+ cp.portraitNodeId,
+ cp.costumeNodeId,
+ cp.poseNodeId,
+ cp.stitchedNodeId,
+ ])
+ if (result.fixedJson) updateComfyUiParams({ workflowJson: result.fixedJson })
+ setComfyCheck({ ...result, info: `${found.info}(${found.baseUrl})` })
+ } catch (e) {
+ setComfyCheck({ ok: false, errors: [e instanceof Error ? e.message : '检测失败'], warnings: [], fixes: [] })
+ } finally {
+ setCheckingComfy(false)
+ }
+ }
+
const handleComfyFileUpload = async (e: ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0]
e.target.value = ''
@@ -140,6 +190,7 @@ function StudioPage() {
return
}
const detected = autoDetectComfyMappings(text)
+ setComfyCheck(null)
updateComfyUiParams({
workflowPreset: 'custom',
workflowJson: text,
@@ -566,12 +617,11 @@ function StudioPage() {
<SelectValue />
</SelectTrigger>
<SelectContent>
- <SelectItem value="qwen21_3img" className="text-xs">
- {COMFYUI_WORKFLOW_TEMPLATES.qwen21_3img.label}
- </SelectItem>
- <SelectItem value="qwen21_2img" className="text-xs">
- {COMFYUI_WORKFLOW_TEMPLATES.qwen21_2img.label}
- </SelectItem>
+ {(Object.keys(COMFYUI_WORKFLOW_TEMPLATES) as ComfyPresetId[]).map((key) => (
+ <SelectItem key={key} value={key} className="text-xs">
+ {COMFYUI_WORKFLOW_TEMPLATES[key].label}
+ </SelectItem>
+ ))}
<SelectItem value="custom" className="text-xs">
自定义工作流 (已导入 JSON)
</SelectItem>
@@ -615,7 +665,56 @@ function StudioPage() {
<DownloadIcon className="size-3.5" />
导出 .json
</Button>
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ className="h-7 px-2 text-xs"
+ onClick={handleCheckComfy}
+ disabled={checkingComfy}
+ >
+ {checkingComfy ? (
+ <LoaderCircleIcon className="size-3.5 animate-spin" />
+ ) : (
+ <ScanSearchIcon className="size-3.5" />
+ )}
+ 检测本机模型
+ </Button>
</div>
+ {comfyCheck && (
+ <div
+ className={`space-y-1 rounded-xl border px-3 py-2 text-[11px] leading-relaxed ${
+ comfyCheck.ok
+ ? 'border-emerald-500/40 bg-emerald-500/8 text-emerald-700 dark:text-emerald-300'
+ : 'border-destructive/40 bg-destructive/10 text-destructive'
+ }`}
+ >
+ <div className="flex items-center gap-1.5 font-semibold">
+ {comfyCheck.ok ? (
+ <CircleCheckIcon className="size-3.5" />
+ ) : (
+ <CircleAlertIcon className="size-3.5" />
+ )}
+ {comfyCheck.ok ? '工作流与本机模型/节点匹配,可以生成' : '检测未通过'}
+ </div>
+ {comfyCheck.info && <div className="opacity-80">{comfyCheck.info}</div>}
+ {comfyCheck.errors.map((m, i) => (
+ <div key={`e${i}`} className="whitespace-pre-wrap break-all">
+ • {m}
+ </div>
+ ))}
+ {comfyCheck.fixes.map((m, i) => (
+ <div key={`f${i}`} className="break-all">
+ ✓ {m}
+ </div>
+ ))}
+ {comfyCheck.warnings.map((m, i) => (
+ <div key={`w${i}`} className="break-all opacity-80">
+ ! {m}
+ </div>
+ ))}
+ </div>
+ )}
</div>
</Field>
) : (
@@ -1046,6 +1145,18 @@ function StudioPage() {
</SelectContent>
</Select>
</Field>
+ <Field label="最长等待时间(秒)">
+ <Input
+ type="number"
+ min={60}
+ step={60}
+ value={apiConfig.comfyuiParams.timeoutSec}
+ onChange={(e) =>
+ updateComfyUiParams({ timeoutSec: Math.max(60, Number(e.target.value) || 600) })
+ }
+ className="bg-background text-xs"
+ />
+ </Field>
<Field label="随机种子 (seed)">
<div className="flex h-9 items-center justify-between rounded-xl border bg-background px-3">
<span className="text-xs text-muted-foreground">
@@ -1174,11 +1285,37 @@ function StudioPage() {
{generatingModel}
</Badge>
)}
+ {generationProgress && (
+ <div className="w-56 space-y-1.5 pt-1">
+ <div className="text-xs text-foreground/80">{generationProgress.message}</div>
+ {generationProgress.percent !== undefined && (
+ <div className="h-1.5 overflow-hidden rounded-full bg-muted">
+ <div
+ className="h-full rounded-full bg-primary transition-all"
+ style={{ width: `${generationProgress.percent}%` }}
+ />
+ </div>
+ )}
+ </div>
+ )}
<p className="pt-1 text-xs text-muted-foreground">
切换页面不会中断当前任务
</p>
</div>
</div>
+ ) : generationError ? (
+ <div className="flex max-w-md flex-col items-center gap-2 text-center">
+ <CircleAlertIcon className="size-7 text-destructive" />
+ <div className="text-sm font-semibold text-destructive">生成失败</div>
+ <p className="whitespace-pre-wrap break-all text-left text-xs leading-relaxed text-muted-foreground">
+ {generationError}
+ </p>
+ {previewUrl && (
+ <Button variant="ghost" size="sm" onClick={() => setZoomImageUrl(previewUrl)}>
+ 查看上一张结果
+ </Button>
+ )}
+ </div>
) : previewUrl ? (
<img
src={previewUrl}
diff --git a/src/services/aiGenerator.ts b/src/services/aiGenerator.ts
index cb8e6b6..7386d74 100644
--- a/src/services/aiGenerator.ts
+++ b/src/services/aiGenerator.ts
@@ -1,4 +1,5 @@
import type { ApiConfig, ImageProviderId, RecipeState } from '@/types/gallery'
+import qwen2509ThreeImageWorkflow from '@/workflows/qwen_image_edit_2509_3img.api.json'
export interface ProviderModelOption {
value: string
@@ -80,13 +81,12 @@ export const PROVIDER_META: Record<
id: 'comfyui',
name: '本地 ComfyUI',
shortName: 'ComfyUI',
- defaultBaseUrl: 'http://127.0.0.1:8188',
+ // 本地开发时走 /comfy 同源代理;线上站点直连 ComfyUI Desktop 默认端口 8000
+ defaultBaseUrl: defaultComfyBaseUrl(),
docUrl: 'https://docs.comfy.org/essentials/comfyui_server',
models: [
- { value: 'qwen_image_2.1', label: 'qwen_image_2.1' },
- { value: 'flux_redux_controlnet', label: 'flux_redux_controlnet' },
- { value: 'sdxl_ipadapter_openpose', label: 'sdxl_ipadapter_openpose' },
- { value: 'custom', label: '自定义工作流' },
+ { value: 'qwen_image_edit_2509', label: 'qwen_image_edit_2509(由工作流决定)' },
+ { value: 'custom', label: '自定义名称(仅用于记录显示)' },
],
},
}
@@ -143,36 +143,44 @@ export const STYLE_PRESETS: Record<string, { label: string; prompt: string }> =
}
// 按实际传入模型的图片顺序动态生成「图1 / 图2 / 图3...」明确指代,支持附加上传图片
+// 用自然语言逐项描述(人物 / 服装 / 姿势),实测比堆砌要点更能让多图编辑模型还原服装、避免照搬人体模型线条
+const PHOTO_STYLES = new Set(['none', 'realistic', 'fashion', 'film'])
+
export function buildRecipePrompt(recipe: RecipeState, stylePreset = 'realistic'): string {
const styleInfo = STYLE_PRESETS[stylePreset] ?? STYLE_PRESETS.realistic
+ const isPhoto = PHOTO_STYLES.has(stylePreset)
const clauses: string[] = []
let idx = 1
+ let costumeIdx = 0
if (recipe.portrait) {
const hairDesc =
recipe.portrait.发型名称 && recipe.portrait.发型名称 !== '标准基础款'
? recipe.portrait.标题
: recipe.portrait.发长
- clauses.push(
- `• 【图${idx}(人像参考)】:保持图${idx}人物的五官脸型、发色与「${hairDesc}」发型气质一致。`
- )
+ clauses.push(`• 人物:图${idx}中的人物,保持其脸型、五官、发型(${hairDesc})和发色一致。`)
idx++
}
if (recipe.costume) {
+ costumeIdx = idx
clauses.push(
- `• 【图${idx}(服装参考)】:穿上图${idx}中的完整服饰「${recipe.costume.服装名称}」,还原面料质感与剪裁。`
+ `• 服装:让人物穿上图${idx}中展示的整套服装和配饰(参考名称:${recipe.costume.服装名称}),每件单品的颜色、款式、面料与配饰都以图${idx}为准,名称仅作辅助说明。`
)
idx++
}
if (recipe.pose) {
+ const wear = costumeIdx ? `,穿着图${costumeIdx}的服装` : ''
+ const body = isPhoto
+ ? `最终画面中的人物是真人,皮肤真实自然${wear}`
+ : `人物的肤色与质感按风格要求呈现${wear}`
clauses.push(
- `• 【图${idx}(姿势参考)】:身体姿态与镜头透视遵循图${idx}「${recipe.pose.姿势名称}」【${recipe.pose.角度}】(仅参考骨架动作,忽略素体白模材质)。`
+ `• 姿势:按照图${idx}中人体模型的动作和镜头角度摆姿势(${recipe.pose.姿势名称},${recipe.pose.角度});图${idx}只是动作示意,${body}。`
)
idx++
}
for (const extra of recipe.extraImages || []) {
clauses.push(
- `• 【图${idx}(附加参考:${extra.name || '补充元素'})】:融合图${idx}中的元素、配饰、背景或光影细节。`
+ `• 附加参考:融合图${idx}(${extra.name || '补充元素'})中的元素、配饰、背景或光影细节。`
)
idx++
}
@@ -182,12 +190,13 @@ export function buildRecipePrompt(recipe: RecipeState, stylePreset = 'realistic'
}
const lines = [
- `请结合提供的 ${clauses.length} 张参考图,融合成一张完整的人物图像:`,
+ `请根据提供的 ${clauses.length} 张参考图,生成一张完整的${isPhoto ? '真实人物照片' : '人物图像'}:`,
...clauses,
]
if (styleInfo.prompt) {
- lines.push(`• 【画风要求】:${styleInfo.prompt} 人物比例协调,手部与五官自然,光影统一。`)
+ lines.push(`• 风格:${styleInfo.prompt} 人物比例协调,手部与五官自然,光影统一。`)
}
+ lines.push('画面中没有任何文字、标志或水印。')
return lines.join('\n')
}
@@ -421,219 +430,177 @@ function extractUniversalImage(json: any): string | null {
// 本地 ComfyUI API 工作流模板与节点解析工具
// ============================================================================
-export const COMFYUI_WORKFLOW_TEMPLATES: Record<
- 'qwen21_3img' | 'qwen21_2img',
- {
- label: string
- workflow: Record<string, any>
- defaults: {
- promptNodeId: string
- portraitNodeId: string
- costumeNodeId: string
- poseNodeId: string
- stitchedNodeId: string
- }
+type ComfyNode = { class_type: string; inputs: Record<string, any>; _meta?: { title?: string } }
+type ComfyWorkflow = Record<string, ComfyNode>
+
+export type ComfyPresetId = 'qwen2509_3img' | 'qwen2509_2img' | 'qwen21_3img' | 'qwen21_2img'
+
+export interface ComfyNodeMappings {
+ promptNodeId: string
+ portraitNodeId: string
+ costumeNodeId: string
+ poseNodeId: string
+ stitchedNodeId: string
+}
+
+/** 本地开发 / 本地预览时使用的同源代理前缀(见 vite.config.ts),可绕开 ComfyUI 的 CORS 白名单限制 */
+export const COMFY_DEV_PROXY = '/comfy'
+
+export function isLocalDevHost(): boolean {
+ return (
+ typeof window !== 'undefined' &&
+ ['localhost', '127.0.0.1', '[::1]'].includes(window.location.hostname)
+ )
+}
+
+/** ComfyUI Desktop 默认端口 8000;便携版 / 源码版默认 8188 */
+// 注意:PROVIDER_META 在模块初始化时就会调用本函数,这里不能引用后面定义的 const
+export function defaultComfyBaseUrl(): string {
+ return isLocalDevHost() ? '/comfy' : 'http://127.0.0.1:8000'
+}
+
+export function comfyCandidateUrls(current = ''): string[] {
+ const list = [
+ current.trim(),
+ isLocalDevHost() ? defaultComfyBaseUrl() : '',
+ 'http://127.0.0.1:8000',
+ 'http://127.0.0.1:8188',
+ ]
+ .map((x) => x.replace(/\/+$/, ''))
+ .filter(Boolean)
+ return Array.from(new Set(list))
+}
+
+function cloneWorkflow<T>(wf: T): T {
+ return JSON.parse(JSON.stringify(wf))
+}
+
+// 双图版:图1 = 人物(含服装),图2 = 姿势(姿势图只送入视觉语言编码器,不作为像素参考)
+function buildQwen2509TwoImage(): ComfyWorkflow {
+ const wf = cloneWorkflow(qwen2509ThreeImageWorkflow) as ComfyWorkflow
+ for (const id of ['2', '22', '23', '25', '27']) delete wf[id]
+ wf['1']._meta = { title: '图1:人物/服装参考 (Person)' }
+ wf['3']._meta = { title: '图2:姿势参考 (Pose)' }
+ for (const id of ['12', '13']) {
+ wf[id].inputs.image2 = ['3', 0]
+ delete wf[id].inputs.image3
}
-> = {
- qwen21_3img: {
- label: 'Qwen-Image 2.1 三图合成(人像+服装+姿势)',
- defaults: {
- promptNodeId: '9',
- portraitNodeId: '1',
- costumeNodeId: '2',
- poseNodeId: '3',
- stitchedNodeId: 'none',
+ wf['14'].inputs.positive = ['24', 0]
+ wf['14'].inputs.negative = ['26', 0]
+ return wf
+}
+
+// 旧版 Qwen-Image 2.1 预设(需要另行安装 2.1 模型;默认已改用本机可用的 Edit 2509)
+function buildQwen21Workflow(threeImages: boolean): ComfyWorkflow {
+ const wf: ComfyWorkflow = {
+ '1': {
+ inputs: { image: 'portrait_ref.png' },
+ class_type: 'LoadImage',
+ _meta: { title: threeImages ? '图1:人像参考 (Portrait)' : '图2:人物/服装参考图 (Person)' },
},
- workflow: {
- '1': {
- inputs: { image: 'portrait_ref.png' },
- class_type: 'LoadImage',
- _meta: { title: '图1:人像参考 (Portrait)' },
- },
- '2': {
- inputs: { image: 'costume_ref.png' },
- class_type: 'LoadImage',
- _meta: { title: '图2:服装参考 (Costume)' },
- },
- '3': {
- inputs: { image: 'pose_ref.png' },
- class_type: 'LoadImage',
- _meta: { title: '图3:姿势参考 (Pose)' },
- },
- '4': {
- inputs: {
- unet_name: 'image2image/qwen_image_2.1_bf16.safetensors',
- weight_dtype: 'default',
- },
- class_type: 'UNETLoader',
- _meta: { title: '加载 Qwen Image 2.1 基础模型' },
- },
- '5': {
- inputs: {
- model: ['4', 0],
- lora_name: 'qwen2.1/动作编辑VNCCS_QI2_PoseStudioV2_3294845.safetensors',
- strength_model: 1.0,
- },
- class_type: 'LoraLoaderModelOnly',
- _meta: { title: '加载姿态编辑 LoRA 适配器' },
- },
- '6': {
- inputs: { device: 'auto', dtype: 'default', model: ['5', 0] },
- class_type: 'QwenImage21Cache',
- _meta: { title: 'Qwen Image 2.1 条件缓存' },
- },
- '7': {
- inputs: {
- clip_name: 'text_encoders/qwen3vl_8b_bf16.safetensors',
- type: 'qwen_image',
- device: 'default',
- },
- class_type: 'CLIPLoader',
- _meta: { title: '加载 Qwen3-VL 8B 编码器' },
- },
- '8': {
- inputs: { vae_name: 'vae/qwen_image_2.1_vae_bf16.safetensors' },
- class_type: 'VAELoader',
- _meta: { title: '加载 Qwen Image 2.1 VAE' },
- },
- '9': {
- inputs: {
- prompt: '',
- negative_prompt: '',
- resolution: 1024,
- clip: ['7', 0],
- 'images.image_1': ['1', 0],
- 'images.image_2': ['2', 0],
- 'images.image_3': ['3', 0],
- vae: ['8', 0],
- },
- class_type: 'TextEncodeQwenImage21',
- _meta: { title: '正向提示词与多图编码 (Qwen 2.1)' },
- },
- '10': {
- inputs: {
- seed: 20261031,
- steps: 25,
- cfg: 1.0,
- sampler_name: 'euler',
- scheduler: 'simple',
- denoise: 1.0,
- model: ['6', 0],
- positive: ['9', 0],
- negative: ['9', 1],
- latent_image: ['9', 2],
- },
- class_type: 'KSampler',
- _meta: { title: 'KSampler 采样器' },
- },
- '11': {
- inputs: { samples: ['10', 0], vae: ['8', 0] },
- class_type: 'VAEDecode',
- _meta: { title: 'VAE 解码' },
- },
- '12': {
- inputs: { filename_prefix: 'pose_studio/result', images: ['11', 0] },
- class_type: 'SaveImage',
- _meta: { title: '保存生成结果' },
+ '4': {
+ inputs: { unet_name: 'image2image/qwen_image_2.1_bf16.safetensors', weight_dtype: 'default' },
+ class_type: 'UNETLoader',
+ _meta: { title: '加载 Qwen Image 2.1 基础模型' },
+ },
+ '5': {
+ inputs: {
+ model: ['4', 0],
+ lora_name: 'qwen2.1/动作编辑VNCCS_QI2_PoseStudioV2_3294845.safetensors',
+ strength_model: 1.0,
},
+ class_type: 'LoraLoaderModelOnly',
+ _meta: { title: '加载姿态编辑 LoRA 适配器 (Qwen 2.1 专用)' },
},
- },
- qwen21_2img: {
- label: 'Qwen-Image 2.1 双图姿态迁移(姿势+人物/拼图)',
- defaults: {
- promptNodeId: '9',
- portraitNodeId: '1',
- costumeNodeId: 'none',
- poseNodeId: '2',
- stitchedNodeId: 'none',
+ '6': {
+ inputs: { device: 'auto', dtype: 'default', model: ['5', 0] },
+ class_type: 'QwenImage21Cache',
+ _meta: { title: 'Qwen Image 2.1 条件缓存' },
},
- workflow: {
- '1': {
- inputs: { image: 'person_reference.png' },
- class_type: 'LoadImage',
- _meta: { title: '图2:人物/服装参考图 (LoadImage)' },
- },
- '2': {
- inputs: { image: 'pose_reference.png' },
- class_type: 'LoadImage',
- _meta: { title: '图1:姿势参考图 (LoadImage)' },
- },
- '4': {
- inputs: {
- unet_name: 'image2image/qwen_image_2.1_bf16.safetensors',
- weight_dtype: 'default',
- },
- class_type: 'UNETLoader',
- _meta: { title: '加载 Qwen Image 2.1 基础模型' },
- },
- '5': {
- inputs: {
- model: ['4', 0],
- lora_name: 'qwen2.1/动作编辑VNCCS_QI2_PoseStudioV2_3294845.safetensors',
- strength_model: 1.0,
- },
- class_type: 'LoraLoaderModelOnly',
- _meta: { title: '加载姿态编辑 LoRA 适配器' },
- },
- '6': {
- inputs: { device: 'auto', dtype: 'default', model: ['5', 0] },
- class_type: 'QwenImage21Cache',
- _meta: { title: 'Qwen Image 2.1 条件缓存' },
- },
- '7': {
- inputs: {
- clip_name: 'text_encoders/qwen3vl_8b_bf16.safetensors',
- type: 'qwen_image',
- device: 'default',
- },
- class_type: 'CLIPLoader',
- _meta: { title: '加载 Qwen3-VL 8B 编码器' },
- },
- '8': {
- inputs: { vae_name: 'vae/qwen_image_2.1_vae_bf16.safetensors' },
- class_type: 'VAELoader',
- _meta: { title: '加载 Qwen Image 2.1 VAE' },
- },
- '9': {
- inputs: {
- prompt: '',
- negative_prompt: '',
- resolution: 1024,
- clip: ['7', 0],
- 'images.image_1': ['2', 0],
- 'images.image_2': ['1', 0],
- vae: ['8', 0],
- },
- class_type: 'TextEncodeQwenImage21',
- _meta: { title: '正向提示词与双图编码 (Qwen 2.1)' },
- },
- '10': {
- inputs: {
- seed: 20261031,
- steps: 25,
- cfg: 1.0,
- sampler_name: 'euler',
- scheduler: 'simple',
- denoise: 1.0,
- model: ['6', 0],
- positive: ['9', 0],
- negative: ['9', 1],
- latent_image: ['9', 2],
- },
- class_type: 'KSampler',
- _meta: { title: 'KSampler 采样器' },
- },
- '11': {
- inputs: { samples: ['10', 0], vae: ['8', 0] },
- class_type: 'VAEDecode',
- _meta: { title: 'VAE 解码' },
+ '7': {
+ inputs: { clip_name: 'text_encoders/qwen3vl_8b_bf16.safetensors', type: 'qwen_image', device: 'default' },
+ class_type: 'CLIPLoader',
+ _meta: { title: '加载 Qwen3-VL 8B 编码器' },
+ },
+ '8': {
+ inputs: { vae_name: 'vae/qwen_image_2.1_vae_bf16.safetensors' },
+ class_type: 'VAELoader',
+ _meta: { title: '加载 Qwen Image 2.1 VAE' },
+ },
+ '9': {
+ inputs: {
+ prompt: '',
+ negative_prompt: '',
+ resolution: 1024,
+ clip: ['7', 0],
+ vae: ['8', 0],
},
- '12': {
- inputs: { filename_prefix: 'pose_studio/result', images: ['11', 0] },
- class_type: 'SaveImage',
- _meta: { title: '保存生成结果' },
+ class_type: 'TextEncodeQwenImage21',
+ _meta: { title: '正向提示词与多图编码 (Qwen 2.1)' },
+ },
+ '10': {
+ inputs: {
+ seed: 20261031,
+ steps: 25,
+ cfg: 1.0,
+ sampler_name: 'euler',
+ scheduler: 'simple',
+ denoise: 1.0,
+ model: ['6', 0],
+ positive: ['9', 0],
+ negative: ['9', 1],
+ latent_image: ['9', 2],
},
+ class_type: 'KSampler',
+ _meta: { title: 'KSampler 采样器' },
},
+ '11': {
+ inputs: { samples: ['10', 0], vae: ['8', 0] },
+ class_type: 'VAEDecode',
+ _meta: { title: 'VAE 解码' },
+ },
+ '12': {
+ inputs: { filename_prefix: 'pose_studio/result', images: ['11', 0] },
+ class_type: 'SaveImage',
+ _meta: { title: '保存生成结果' },
+ },
+ }
+ if (threeImages) {
+ wf['2'] = { inputs: { image: 'costume_ref.png' }, class_type: 'LoadImage', _meta: { title: '图2:服装参考 (Costume)' } }
+ wf['3'] = { inputs: { image: 'pose_ref.png' }, class_type: 'LoadImage', _meta: { title: '图3:姿势参考 (Pose)' } }
+ Object.assign(wf['9'].inputs, { 'images.image_1': ['1', 0], 'images.image_2': ['2', 0], 'images.image_3': ['3', 0] })
+ } else {
+ wf['2'] = { inputs: { image: 'pose_reference.png' }, class_type: 'LoadImage', _meta: { title: '图1:姿势参考图 (Pose)' } }
+ Object.assign(wf['9'].inputs, { 'images.image_1': ['2', 0], 'images.image_2': ['1', 0] })
+ }
+ return wf
+}
+
+export const DEFAULT_COMFY_PRESET: ComfyPresetId = 'qwen2509_3img'
+
+export const COMFYUI_WORKFLOW_TEMPLATES: Record<
+ ComfyPresetId,
+ { label: string; workflow: ComfyWorkflow; defaults: ComfyNodeMappings }
+> = {
+ qwen2509_3img: {
+ label: 'Qwen Image Edit 2509 三图合成(人像+服装+姿势)· 推荐',
+ workflow: qwen2509ThreeImageWorkflow as ComfyWorkflow,
+ defaults: { promptNodeId: '12', portraitNodeId: '1', costumeNodeId: '2', poseNodeId: '3', stitchedNodeId: 'none' },
+ },
+ qwen2509_2img: {
+ label: 'Qwen Image Edit 2509 双图姿态迁移(人物+姿势)',
+ workflow: buildQwen2509TwoImage(),
+ defaults: { promptNodeId: '12', portraitNodeId: '1', costumeNodeId: 'none', poseNodeId: '3', stitchedNodeId: 'none' },
+ },
+ qwen21_3img: {
+ label: 'Qwen-Image 2.1 三图合成(需另装 2.1 模型)',
+ workflow: buildQwen21Workflow(true),
+ defaults: { promptNodeId: '9', portraitNodeId: '1', costumeNodeId: '2', poseNodeId: '3', stitchedNodeId: 'none' },
+ },
+ qwen21_2img: {
+ label: 'Qwen-Image 2.1 双图姿态迁移(需另装 2.1 模型)',
+ workflow: buildQwen21Workflow(false),
+ defaults: { promptNodeId: '9', portraitNodeId: '1', costumeNodeId: 'none', poseNodeId: '2', stitchedNodeId: 'none' },
},
}
@@ -658,7 +625,7 @@ export function parseComfyWorkflowNodes(workflowJson: string): ComfyParsedNodes
return {
valid: false,
isUiFormat: true,
- error: '检测到普通画布格式 JSON,请在 ComfyUI 设置中开启「开发者模式」并使用「导出 (API)」',
+ error: '检测到普通画布格式 JSON,请在 ComfyUI 中使用「工作流 → 导出 (API)」得到 API 格式 JSON',
imageNodes: [],
promptNodes: [],
}
@@ -674,27 +641,20 @@ export function parseComfyWorkflowNodes(workflowJson: string): ComfyParsedNodes
const inputs = node.inputs || {}
if (classType === 'LoadImage' || typeof inputs.image === 'string') {
- imageNodes.push({
- id,
- title,
- classType,
- currentImage: String(inputs.image || ''),
- })
+ imageNodes.push({ id, title, classType, currentImage: String(inputs.image || '') })
}
for (const field of ['prompt', 'text', 'string', 'positive']) {
if (typeof inputs[field] === 'string') {
- promptNodes.push({
- id,
- title,
- classType,
- field,
- })
+ promptNodes.push({ id, title, classType, field })
break
}
}
}
+ if (imageNodes.length === 0 && promptNodes.length === 0) {
+ return { valid: false, isUiFormat: false, error: '未在 JSON 中找到任何 ComfyUI 节点', imageNodes, promptNodes }
+ }
return { valid: true, isUiFormat: false, imageNodes, promptNodes }
} catch (err: any) {
return {
@@ -707,13 +667,7 @@ export function parseComfyWorkflowNodes(workflowJson: string): ComfyParsedNodes
}
}
-export function autoDetectComfyMappings(workflowJson: string): {
- promptNodeId: string
- portraitNodeId: string
- costumeNodeId: string
- poseNodeId: string
- stitchedNodeId: string
-} {
+export function autoDetectComfyMappings(workflowJson: string): ComfyNodeMappings {
const parsed = parseComfyWorkflowNodes(workflowJson)
let promptNodeId = 'none'
let portraitNodeId = 'none'
@@ -743,31 +697,307 @@ export function autoDetectComfyMappings(workflowJson: string): {
}
const remaining = parsed.imageNodes.filter((n) => !usedIds.has(n.id))
- if (portraitNodeId === 'none' && remaining[0]) {
- portraitNodeId = remaining.shift()!.id
+ if (portraitNodeId === 'none' && remaining[0]) portraitNodeId = remaining.shift()!.id
+ if (costumeNodeId === 'none' && remaining[0]) costumeNodeId = remaining.shift()!.id
+ if (poseNodeId === 'none' && remaining[0]) poseNodeId = remaining.shift()!.id
+
+ return { promptNodeId, portraitNodeId, costumeNodeId, poseNodeId, stitchedNodeId }
+}
+
+// ---------------------------------------------------------------------------
+// 网络请求与中文错误
+// ---------------------------------------------------------------------------
+
+function normalizeComfyBase(baseUrl: string): string {
+ return (baseUrl.trim() || defaultComfyBaseUrl()).replace(/\/+$/, '')
+}
+
+function describeComfyNetworkError(baseUrl: string, timedOut: boolean): string {
+ if (timedOut) {
+ return `[ComfyUI] 请求 ${baseUrl} 超时:ComfyUI 可能正忙或无响应,请查看 ComfyUI 终端日志。`
}
- if (costumeNodeId === 'none' && remaining[0]) {
- costumeNodeId = remaining.shift()!.id
+ if (baseUrl.startsWith('/')) {
+ return `[ComfyUI] 本地代理 ${baseUrl} 无法连通 ComfyUI:请确认 ComfyUI 已启动,且端口与启动网页时的 COMFYUI_URL 一致(默认 http://127.0.0.1:8000)。`
}
- if (poseNodeId === 'none' && remaining[0]) {
- poseNodeId = remaining.shift()!.id
+ const origin = typeof window !== 'undefined' ? window.location.origin : ''
+ const reasons = [
+ `① ComfyUI 未启动,或地址/端口不对(Desktop 版默认 8000,便携版默认 8188)`,
+ `② 跨域 (CORS) 被拦截:ComfyUI 启动参数需包含 --enable-cors-header "${origin}"(该参数只能填一个来源)`,
+ ]
+ if (typeof window !== 'undefined' && window.location.protocol === 'https:') {
+ reasons.push('③ 浏览器「本地网络访问」权限被拒绝:请点击地址栏左侧图标允许访问本地设备')
+ if (/^http:/i.test(baseUrl) && !/\/\/(127\.0\.0\.1|localhost|\[::1\])/i.test(baseUrl)) {
+ reasons.push('④ HTTPS 页面不能访问局域网 HTTP 地址(混合内容限制),请改用 127.0.0.1 或本地运行本站')
+ }
}
+ return `[ComfyUI] 无法连接 ${baseUrl}。可能原因:\n${reasons.join('\n')}`
+}
- return { promptNodeId, portraitNodeId, costumeNodeId, poseNodeId, stitchedNodeId }
+async function comfyFetch(
+ baseUrl: string,
+ path: string,
+ init: RequestInit = {},
+ apiKey = '',
+ timeoutMs = 30000
+): Promise<Response> {
+ const headers = new Headers(init.headers)
+ if (apiKey.trim()) headers.set('Authorization', `Bearer ${apiKey.trim()}`)
+ const ctrl = new AbortController()
+ const timer = setTimeout(() => ctrl.abort(), timeoutMs)
+ let res: Response
+ try {
+ res = await fetch(`${baseUrl}${path}`, { ...init, headers, signal: ctrl.signal })
+ } catch (e: any) {
+ throw new Error(describeComfyNetworkError(baseUrl, e?.name === 'AbortError'))
+ } finally {
+ clearTimeout(timer)
+ }
+ // 开发代理连不上后端时会返回非 JSON 的 5xx
+ if (
+ baseUrl.startsWith('/') &&
+ res.status >= 500 &&
+ !(res.headers.get('content-type') || '').includes('json')
+ ) {
+ throw new Error(describeComfyNetworkError(baseUrl, false))
+ }
+ return res
+}
+
+async function readJsonSafe(res: Response): Promise<any> {
+ const text = await res.text()
+ try {
+ return JSON.parse(text)
+ } catch {
+ return { _raw: text.slice(0, 300) }
+ }
}
export async function testComfyConnection(baseUrl: string, apiKey = ''): Promise<string> {
- const cleanUrl = (baseUrl.trim() || PROVIDER_META.comfyui.defaultBaseUrl).replace(/\/+$/, '')
- const headers: Record<string, string> = {}
- if (apiKey.trim()) headers.Authorization = `Bearer ${apiKey.trim()}`
- const res = await fetch(`${cleanUrl}/system_stats`, { headers })
- if (!res.ok) {
- throw new Error(`HTTP ${res.status}`)
+ const cleanUrl = normalizeComfyBase(baseUrl)
+ const res = await comfyFetch(cleanUrl, '/system_stats', {}, apiKey, 8000)
+ if (res.status === 401 || res.status === 403) {
+ throw new Error(`[ComfyUI] ${cleanUrl} 拒绝访问 (HTTP ${res.status}),请检查 API Key / 鉴权设置`)
+ }
+ if (!res.ok) throw new Error(`[ComfyUI] ${cleanUrl}/system_stats 返回 HTTP ${res.status},该地址可能不是 ComfyUI`)
+ const data = await readJsonSafe(res)
+ if (!data?.system) throw new Error(`[ComfyUI] ${cleanUrl} 的响应不是 ComfyUI 格式,请检查地址`)
+ const ver = data.system.comfyui_version || '未知版本'
+ const dev = data.devices?.[0]
+ const gpu = dev?.name ? String(dev.name).replace(/^cuda:\d+\s*/, '').replace(/\s*:\s*\w+$/, '') : '本地设备'
+ const vram = dev?.vram_total ? ` · 显存 ${(dev.vram_total / 1024 ** 3).toFixed(1)} GB` : ''
+ return `ComfyUI ${ver} · ${gpu}${vram}`
+}
+
+/** 依次尝试候选地址,返回第一个可用的 ComfyUI 地址 */
+export async function detectComfyUi(
+ current = '',
+ apiKey = ''
+): Promise<{ baseUrl: string; info: string } | null> {
+ for (const url of comfyCandidateUrls(current)) {
+ try {
+ const info = await testComfyConnection(url, apiKey)
+ return { baseUrl: url, info }
+ } catch {
+ /* try next */
+ }
+ }
+ return null
+}
+
+// ---------------------------------------------------------------------------
+// 工作流与本机模型/节点匹配检测(基于 /object_info)
+// ---------------------------------------------------------------------------
+
+export interface ComfyCheckResult {
+ ok: boolean
+ errors: string[]
+ warnings: string[]
+ fixes: string[]
+ /** 自动修正模型路径后的工作流 JSON(无修正时为 undefined) */
+ fixedJson?: string
+}
+
+function comboOptions(spec: any): string[] | null {
+ if (!Array.isArray(spec)) return null
+ if (Array.isArray(spec[0])) return spec[0].map(String)
+ if (spec[0] === 'COMBO' && Array.isArray(spec[1]?.options)) return spec[1].options.map(String)
+ return null
+}
+
+const baseName = (s: string) => s.split(/[\\/]/).pop()!.toLowerCase()
+
+export async function checkComfyWorkflow(
+ baseUrl: string,
+ apiKey: string,
+ workflowJson: string,
+ mappedImageNodeIds: string[] = []
+): Promise<ComfyCheckResult> {
+ const base = normalizeComfyBase(baseUrl)
+ const parsedInfo = parseComfyWorkflowNodes(workflowJson)
+ if (!parsedInfo.valid) {
+ return { ok: false, errors: [parsedInfo.error || '工作流 JSON 无效'], warnings: [], fixes: [] }
+ }
+ const wf: ComfyWorkflow = JSON.parse(workflowJson)
+ const classes = Array.from(new Set(Object.values(wf).map((n) => n?.class_type).filter(Boolean)))
+
+ const infoEntries = await Promise.all(
+ classes.map(async (cls) => {
+ const res = await comfyFetch(base, `/object_info/${encodeURIComponent(cls)}`, {}, apiKey, 15000)
+ const json = res.ok ? await readJsonSafe(res) : {}
+ return [cls, json?.[cls]] as const
+ })
+ )
+ const info = Object.fromEntries(infoEntries)
+
+ const errors: string[] = []
+ const warnings: string[] = []
+ const fixes: string[] = []
+ const mapped = new Set(mappedImageNodeIds.filter((x) => x && x !== 'none'))
+
+ for (const [id, node] of Object.entries(wf)) {
+ if (!node?.class_type) continue
+ const title = node._meta?.title || node.class_type
+ const def = info[node.class_type]
+ if (!def) {
+ errors.push(
+ `缺少节点「${node.class_type}」(节点 ${id}:${title})。请在 ComfyUI Manager 安装对应的自定义节点,或升级 ComfyUI。`
+ )
+ continue
+ }
+ const required = def.input?.required || {}
+ const optional = def.input?.optional || {}
+ const inputs = node.inputs || {}
+
+ for (const name of Object.keys(required)) {
+ if (!(name in inputs)) errors.push(`节点 ${id}「${title}」缺少必填输入 ${name}`)
+ }
+
+ for (const [name, value] of Object.entries(inputs)) {
+ if (Array.isArray(value)) continue // 节点连线
+ const spec = required[name] ?? optional[name]
+ const options = comboOptions(spec)
+ if (!options || typeof value !== 'string') continue
+ if (node.class_type === 'LoadImage' && name === 'image') {
+ if (!mapped.has(id) && !options.includes(value)) {
+ warnings.push(`节点 ${id}「${title}」引用的图片 ${value} 不在 ComfyUI input 目录中,且未映射到人像/服装/姿势槽位`)
+ }
+ continue
+ }
+ if (options.includes(value)) continue
+ const sameFile = options.find((o) => baseName(o) === baseName(value))
+ if (sameFile) {
+ inputs[name] = sameFile
+ fixes.push(`节点 ${id}「${title}」${name}:${value} → ${sameFile}(自动修正路径)`)
+ continue
+ }
+ const sample = options.slice(0, 12).join('、')
+ errors.push(
+ `节点 ${id}「${title}」的 ${name} = ${value} 在本机不存在(通常是模型文件未安装或路径不同)。本机可选:${sample || '(空)'}${options.length > 12 ? ' …' : ''}`
+ )
+ }
+ }
+
+ return {
+ ok: errors.length === 0,
+ errors,
+ warnings,
+ fixes,
+ fixedJson: fixes.length ? JSON.stringify(wf, null, 2) : undefined,
+ }
+}
+
+function translateNodeError(er: any): string {
+ const detail = er?.details ? `(${er.details})` : ''
+ switch (er?.type) {
+ case 'value_not_in_list':
+ return `${er.extra_info?.input_name ?? '输入'} 的值不在本机可选列表中${detail}——通常是模型文件不存在或路径不同`
+ case 'required_input_missing':
+ return `缺少必填输入 ${er.extra_info?.input_name ?? ''}`
+ case 'invalid_input_type':
+ case 'return_type_mismatch':
+ return `节点连线类型不匹配${detail}`
+ case 'value_bigger_than_max':
+ case 'value_smaller_than_min':
+ return `参数超出范围${detail}`
+ case 'image_not_found':
+ case 'custom_validation_failed':
+ return `${er.message || '校验失败'}${detail}`
+ default:
+ return `${er?.message || '校验失败'}${detail}`
+ }
+}
+
+function formatComfyPromptError(json: any, wf: ComfyWorkflow, status: number): string {
+ const parts: string[] = []
+ const e = json?.error
+ if (e) {
+ if (e.type === 'missing_node_type' || /does not exist|not found/i.test(String(e.message))) {
+ parts.push(`缺少节点类型:${e.details || e.message}。请在 ComfyUI Manager 安装缺失的自定义节点或升级 ComfyUI`)
+ } else if (e.type === 'prompt_no_outputs') {
+ parts.push('工作流没有输出节点(SaveImage / PreviewImage)')
+ } else if (e.type !== 'prompt_outputs_failed_validation') {
+ parts.push(`${e.message || e.type}${e.details ? `:${e.details}` : ''}`)
+ }
+ }
+ for (const [id, ne] of Object.entries<any>(json?.node_errors || {})) {
+ const title = wf[id]?._meta?.title || ne?.class_type || id
+ for (const er of ne?.errors || []) {
+ parts.push(`节点 ${id}「${title}」:${translateNodeError(er)}`)
+ }
}
- const data = await res.json()
- const ver = data?.system?.comfyui_version || '未知版本'
- const gpu = data?.devices?.[0]?.name || '本地设备'
- return `ComfyUI ${ver} · ${gpu}`
+ if (!parts.length) parts.push(json?._raw || `HTTP ${status}`)
+ return `[ComfyUI] 提交工作流失败:\n${parts.join('\n')}`
+}
+
+async function freeComfyMemory(baseUrl: string, apiKey: string): Promise<void> {
+ try {
+ await comfyFetch(
+ baseUrl,
+ '/free',
+ {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ unload_models: true, free_memory: true }),
+ },
+ apiKey,
+ 10000
+ )
+ } catch {
+ /* ignore */
+ }
+}
+
+function formatExecutionError(messages: any[], wf: ComfyWorkflow): { text: string; oom: boolean } {
+ const errMsg = (messages || []).find((m) => m?.[0] === 'execution_error')?.[1]
+ if (errMsg) {
+ const title = wf[errMsg.node_id]?._meta?.title || errMsg.node_type || errMsg.node_id
+ const raw = `${errMsg.exception_type || ''} ${errMsg.exception_message || ''}`
+ if (/out of memory|OutOfMemory|CUDA error: out of memory|allocat/i.test(raw)) {
+ return {
+ oom: true,
+ text:
+ `[ComfyUI] 显存不足(节点 ${errMsg.node_id}「${title}」)。已请求 ComfyUI 释放显存。\n` +
+ '建议:关闭其他占用显卡的程序;在工作流中把「按姿势图比例确定输出尺寸」节点的 megapixels 从 1.0 调低到 0.6;然后重试。',
+ }
+ }
+ return {
+ oom: false,
+ text: `[ComfyUI] 执行出错(节点 ${errMsg.node_id}「${title}」):${String(errMsg.exception_message || raw).trim().slice(0, 400)}`,
+ }
+ }
+ if ((messages || []).some((m) => m?.[0] === 'execution_interrupted')) {
+ return { oom: false, text: '[ComfyUI] 任务已被中断(可能在 ComfyUI 界面中点击了取消)' }
+ }
+ return { oom: false, text: `[ComfyUI] 工作流执行出错:${JSON.stringify(messages || '').slice(0, 300)}` }
+}
+
+function blobToDataUrl(blob: Blob): Promise<string> {
+ return new Promise((resolve, reject) => {
+ const reader = new FileReader()
+ reader.onerror = () => reject(new Error('读取生成结果失败'))
+ reader.onload = () => resolve(reader.result as string)
+ reader.readAsDataURL(blob)
+ })
}
async function uploadImageToComfy(
@@ -776,27 +1006,58 @@ async function uploadImageToComfy(
filenamePrefix: string,
apiKey: string
): Promise<string> {
- const dataUrl = await srcToPngDataUrl(src)
- const blob = dataUrlToBlob(dataUrl)
+ let dataUrl: string
+ try {
+ dataUrl = await srcToPngDataUrl(src)
+ } catch {
+ throw new Error(`读取参考图失败:${src.slice(0, 80)}`)
+ }
const form = new FormData()
- form.append('image', blob, `${filenamePrefix}_${Date.now()}.png`)
+ form.append('image', dataUrlToBlob(dataUrl), `${filenamePrefix}_${Date.now()}.png`)
+ form.append('subfolder', 'pose_studio')
form.append('overwrite', 'true')
- const headers: Record<string, string> = {}
- if (apiKey) headers.Authorization = `Bearer ${apiKey}`
-
- const res = await fetch(`${baseUrl}/upload/image`, {
- method: 'POST',
- headers,
- body: form,
- })
+ const res = await comfyFetch(baseUrl, '/upload/image', { method: 'POST', body: form }, apiKey, 60000)
if (!res.ok) {
- throw new Error(`上传参考图到 ComfyUI 失败 (HTTP ${res.status}),请确认已开启 --enable-cors-header *`)
+ throw new Error(`[ComfyUI] 上传参考图失败 (HTTP ${res.status}):${(await res.text()).slice(0, 200)}`)
}
const json = await res.json()
return json.subfolder ? `${json.subfolder}/${json.name}` : json.name
}
+function openComfySocket(baseUrl: string, clientId: string, onMessage: (msg: any) => void): WebSocket | null {
+ try {
+ const u = new URL(baseUrl, window.location.href)
+ u.protocol = u.protocol === 'https:' ? 'wss:' : 'ws:'
+ u.pathname = `${u.pathname.replace(/\/+$/, '')}/ws`
+ u.search = `clientId=${encodeURIComponent(clientId)}`
+ const ws = new WebSocket(u.toString())
+ ws.onmessage = (e) => {
+ if (typeof e.data !== 'string') return
+ try {
+ onMessage(JSON.parse(e.data))
+ } catch {
+ /* ignore */
+ }
+ }
+ ws.onerror = () => {
+ /* 进度推送失败不影响结果轮询 */
+ }
+ return ws
+ } catch {
+ return null
+ }
+}
+
+export interface GenerationProgress {
+ stage: 'check' | 'upload' | 'queue' | 'running' | 'download'
+ message: string
+ /** 0-100,未知时为 undefined */
+ percent?: number
+}
+
+const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
+
async function generateWithComfyUi(params: {
recipe: RecipeState
prompt: string
@@ -804,46 +1065,62 @@ async function generateWithComfyUi(params: {
baseUrl: string
apiKey: string
model: string
+ onProgress?: (p: GenerationProgress) => void
}): Promise<{ imageUrl: string; modelUsed: string; providerUsed: ImageProviderId }> {
const { recipe, prompt, config, baseUrl, apiKey, model } = params
+ const report = params.onProgress ?? (() => {})
const cp = config.comfyuiParams
- const parsedInfo = parseComfyWorkflowNodes(cp.workflowJson)
- if (!parsedInfo.valid) {
- throw new Error(`[ComfyUI] ${parsedInfo.error || '工作流 JSON 无效'}`)
+ const timeoutSec = Math.max(60, cp.timeoutSec || 600)
+
+ // 0. 预检:服务在线 + 节点/模型与本机匹配
+ report({ stage: 'check', message: '正在检查 ComfyUI 服务与模型…' })
+ await testComfyConnection(baseUrl, apiKey)
+ const check = await checkComfyWorkflow(baseUrl, apiKey, cp.workflowJson, [
+ cp.portraitNodeId,
+ cp.costumeNodeId,
+ cp.poseNodeId,
+ cp.stitchedNodeId,
+ ])
+ if (!check.ok) {
+ throw new Error(`[ComfyUI] 工作流与本机不匹配:\n${check.errors.join('\n')}`)
}
-
- const workflow = JSON.parse(cp.workflowJson)
+ const workflow: ComfyWorkflow = JSON.parse(check.fixedJson ?? cp.workflowJson)
// 1. 按映射槽位上传参考图并注入节点
- const slotUploads: Array<{ nodeId: string; src: string | null; prefix: string }> = [
+ const slotUploads: Array<{ nodeId: string; src: string | null; prefix: string; label: string }> = [
{
nodeId: cp.portraitNodeId,
src: recipe.portrait ? recipe.portrait.原图 || recipe.portrait.缩略图 : null,
prefix: 'studio_portrait',
+ label: '人像',
},
{
nodeId: cp.costumeNodeId,
src: recipe.costume ? recipe.costume.预览图 || recipe.costume.原图 || recipe.costume.缩略图 : null,
prefix: 'studio_costume',
+ label: '服装',
},
{
nodeId: cp.poseNodeId,
src: recipe.pose ? recipe.pose.原图 || recipe.pose.缩略图 : null,
prefix: 'studio_pose',
+ label: '姿势',
},
]
for (const slot of slotUploads) {
- if (slot.nodeId && slot.nodeId !== 'none' && slot.src && workflow[slot.nodeId]?.inputs) {
- const uploadedName = await uploadImageToComfy(baseUrl, slot.src, slot.prefix, apiKey)
- workflow[slot.nodeId].inputs.image = uploadedName
+ if (!slot.nodeId || slot.nodeId === 'none' || !workflow[slot.nodeId]?.inputs) continue
+ if (!slot.src) {
+ throw new Error(`当前工作流需要「${slot.label}」参考图(节点 ${slot.nodeId}),请先在图库中选择`)
}
+ report({ stage: 'upload', message: `正在上传${slot.label}参考图…` })
+ workflow[slot.nodeId].inputs.image = await uploadImageToComfy(baseUrl, slot.src, slot.prefix, apiKey)
}
if (cp.stitchedNodeId && cp.stitchedNodeId !== 'none' && workflow[cp.stitchedNodeId]?.inputs) {
+ report({ stage: 'upload', message: '正在上传参考拼图…' })
const stitchedDataUrl = await stitchRecipeCanvas(recipe)
- const uploadedName = await uploadImageToComfy(baseUrl, stitchedDataUrl, 'studio_stitched', apiKey)
- workflow[cp.stitchedNodeId].inputs.image = uploadedName
+ workflow[cp.stitchedNodeId].inputs.image = await uploadImageToComfy(baseUrl, stitchedDataUrl, 'studio_stitched', apiKey)
}
// 2. 注入提示词节点
@@ -867,85 +1144,140 @@ async function generateWithComfyUi(params: {
}
}
- // 4. 提交任务到 ComfyUI /prompt
- const headers: Record<string, string> = { 'Content-Type': 'application/json' }
- if (apiKey) headers.Authorization = `Bearer ${apiKey}`
-
+ // 4. 提交任务到 ComfyUI /prompt,并通过 WebSocket 接收实时进度
const clientId = `pose_studio_${Math.random().toString(36).slice(2, 10)}`
- let promptRes: Response
+ let promptId = ''
+ let lastNodeTitle = ''
+ const ws = openComfySocket(baseUrl, clientId, (msg) => {
+ const d = msg?.data
+ if (!d || (promptId && d.prompt_id && d.prompt_id !== promptId)) return
+ if (msg.type === 'executing' && d.node) {
+ lastNodeTitle = workflow[d.node]?._meta?.title || workflow[d.node]?.class_type || d.node
+ report({ stage: 'running', message: `正在执行:${lastNodeTitle}` })
+ } else if (msg.type === 'progress' && d.max) {
+ const percent = Math.round((d.value / d.max) * 100)
+ report({ stage: 'running', message: `${lastNodeTitle || '采样中'}(${d.value}/${d.max})`, percent })
+ }
+ })
+
try {
- promptRes = await fetch(`${baseUrl}/prompt`, {
- method: 'POST',
- headers,
- body: JSON.stringify({ prompt: workflow, client_id: clientId }),
- })
- } catch {
- throw new Error(
- `无法连接本地 ComfyUI (${baseUrl})。请确认 ComfyUI 已启动,且启动参数包含 --enable-cors-header *`
+ report({ stage: 'queue', message: '正在提交工作流…' })
+ const promptRes = await comfyFetch(
+ baseUrl,
+ '/prompt',
+ {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ prompt: workflow, client_id: clientId }),
+ },
+ apiKey,
+ 60000
)
- }
-
- const promptJson = await promptRes.json()
- if (!promptRes.ok || !promptJson?.prompt_id) {
- const nodeErr = promptJson?.node_errors
- ? JSON.stringify(promptJson.node_errors).slice(0, 220)
- : promptJson?.error?.message || `HTTP ${promptRes.status}`
- throw new Error(`[ComfyUI] 提交工作流失败:${nodeErr}`)
- }
-
- const promptId: string = promptJson.prompt_id
-
- // 5. 轮询 /history/{prompt_id} 获取生成结果图片
- const authHeaders: Record<string, string> = {}
- if (apiKey) authHeaders.Authorization = `Bearer ${apiKey}`
-
- for (let attempt = 0; attempt < 250; attempt++) {
- await new Promise((r) => setTimeout(r, 1200))
- const histRes = await fetch(`${baseUrl}/history/${promptId}`, { headers: authHeaders })
- if (!histRes.ok) continue
- const histJson = await histRes.json()
- const item = histJson?.[promptId]
- if (!item) continue
-
- if (item.status?.status_str === 'error') {
- const errDetail = JSON.stringify(item.status?.messages || '执行出错').slice(0, 220)
- throw new Error(`[ComfyUI] 工作流执行出错:${errDetail}`)
+ const promptJson = await readJsonSafe(promptRes)
+ if (!promptRes.ok || !promptJson?.prompt_id) {
+ throw new Error(formatComfyPromptError(promptJson, workflow, promptRes.status))
}
+ promptId = promptJson.prompt_id
+ report({ stage: 'queue', message: '已提交,等待 ComfyUI 执行…' })
+
+ // 5. 轮询 /history/{prompt_id} 获取最终结果
+ const startedAt = Date.now()
+ for (let attempt = 0; ; attempt++) {
+ await sleep(1500)
+ if (Date.now() - startedAt > timeoutSec * 1000) {
+ await cancelComfyPrompt(baseUrl, apiKey, promptId)
+ throw new Error(
+ `[ComfyUI] 等待生成结果超时(超过 ${Math.round(timeoutSec / 60)} 分钟),已取消该任务。首次加载模型较慢,可在合成台调大超时时间后重试。`
+ )
+ }
+ let histRes: Response
+ try {
+ histRes = await comfyFetch(baseUrl, `/history/${promptId}`, {}, apiKey, 15000)
+ } catch (e) {
+ if (attempt % 4 === 3) report({ stage: 'running', message: '暂时无法连接 ComfyUI,正在重试…' })
+ if (Date.now() - startedAt > 60000 && attempt % 20 === 19) throw e
+ continue
+ }
+ if (!histRes.ok) continue
+ const item = (await readJsonSafe(histRes))?.[promptId]
+ if (!item) {
+ if (attempt % 3 === 0) {
+ try {
+ const q = await (await comfyFetch(baseUrl, '/queue', {}, apiKey, 10000)).json()
+ const pendingIdx = (q?.queue_pending || []).findIndex((x: any) => x?.[1] === promptId)
+ if (pendingIdx >= 0) {
+ report({ stage: 'queue', message: `排队中,前方还有 ${pendingIdx + (q.queue_running?.length || 0)} 个任务` })
+ }
+ } catch {
+ /* ignore */
+ }
+ }
+ continue
+ }
- const outputs = item.outputs || {}
- let outImg: { filename: string; subfolder?: string; type?: string } | null = null
- for (const nodeOut of Object.values<any>(outputs)) {
- if (Array.isArray(nodeOut?.images) && nodeOut.images.length > 0) {
- outImg = nodeOut.images[0]
- if (outImg?.type === 'output') break
+ if (item.status?.status_str === 'error') {
+ const { text, oom } = formatExecutionError(item.status?.messages, workflow)
+ if (oom) await freeComfyMemory(baseUrl, apiKey)
+ throw new Error(text)
}
- }
+ if (item.status && item.status.completed === false && item.status.status_str !== 'success') continue
- if (outImg) {
+ let outImg: { filename: string; subfolder?: string; type?: string } | null = null
+ for (const nodeOut of Object.values<any>(item.outputs || {})) {
+ for (const im of nodeOut?.images || []) {
+ if (!outImg || (im?.type === 'output' && outImg.type !== 'output')) outImg = im
+ }
+ }
+ if (!outImg) {
+ if (item.status?.completed) throw new Error('[ComfyUI] 工作流已执行完成,但没有输出图片(请检查是否有 SaveImage 节点)')
+ continue
+ }
+
+ report({ stage: 'download', message: '正在获取生成结果…', percent: 100 })
const qs = new URLSearchParams({
filename: outImg.filename,
subfolder: outImg.subfolder || '',
type: outImg.type || 'output',
})
- const viewUrl = `${baseUrl}/view?${qs.toString()}`
- try {
- const imgDataUrl = await srcToPngDataUrl(viewUrl)
- return { imageUrl: imgDataUrl, modelUsed: model || 'comfyui', providerUsed: 'comfyui' }
- } catch {
- return { imageUrl: viewUrl, modelUsed: model || 'comfyui', providerUsed: 'comfyui' }
+ const viewRes = await comfyFetch(baseUrl, `/view?${qs.toString()}`, {}, apiKey, 60000)
+ if (!viewRes.ok) throw new Error(`[ComfyUI] 获取生成图片失败 (HTTP ${viewRes.status})`)
+ const blob = await viewRes.blob()
+ if (!blob.size || !blob.type.startsWith('image/')) {
+ throw new Error('[ComfyUI] 返回的生成结果不是有效图片')
}
+ return { imageUrl: await blobToDataUrl(blob), modelUsed: model || 'comfyui', providerUsed: 'comfyui' }
}
+ } finally {
+ ws?.close()
}
+}
- throw new Error('[ComfyUI] 等待生图结果超时(超过 5 分钟),请在 ComfyUI 终端查看进度')
+async function cancelComfyPrompt(baseUrl: string, apiKey: string, promptId: string): Promise<void> {
+ try {
+ const q = await (await comfyFetch(baseUrl, '/queue', {}, apiKey, 10000)).json()
+ if ((q?.queue_running || []).some((x: any) => x?.[1] === promptId)) {
+ await comfyFetch(baseUrl, '/interrupt', { method: 'POST' }, apiKey, 10000)
+ } else {
+ await comfyFetch(
+ baseUrl,
+ '/queue',
+ { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ delete: [promptId] }) },
+ apiKey,
+ 10000
+ )
+ }
+ } catch {
+ /* ignore */
+ }
}
export async function generateCharacterImage(params: {
recipe: RecipeState
prompt: string
config: ApiConfig
+ onProgress?: (p: GenerationProgress) => void
}): Promise<{ imageUrl: string; modelUsed: string; providerUsed: ImageProviderId }> {
- const { recipe, prompt, config } = params
+ const { recipe, prompt, config, onProgress } = params
const provider = config.activeProvider
const pCfg = config.providers[provider]
const apiKey = pCfg.apiKey.trim()
@@ -973,7 +1305,7 @@ export async function generateCharacterImage(params: {
const baseUrl = (pCfg.baseUrl.trim() || PROVIDER_META[provider].defaultBaseUrl).replace(/\/+$/, '')
if (provider === 'comfyui') {
- return generateWithComfyUi({ recipe, prompt, config, baseUrl, apiKey, model })
+ return generateWithComfyUi({ recipe, prompt, config, baseUrl, apiKey, model, onProgress })
}
// 判断当前请求是否需要转换为本地 Base64:
diff --git a/src/store/studioStore.tsx b/src/store/studioStore.tsx
index 185d6e3..4aa83f5 100644
--- a/src/store/studioStore.tsx
+++ b/src/store/studioStore.tsx
@@ -15,7 +15,9 @@ import { idbGet, idbSet } from '@/lib/idbStorage'
import {
buildRecipePrompt,
COMFYUI_WORKFLOW_TEMPLATES,
+ DEFAULT_COMFY_PRESET,
generateCharacterImage,
+ type GenerationProgress,
PROVIDER_META,
stitchRecipeCanvas,
} from '@/services/aiGenerator'
@@ -41,6 +43,8 @@ const RECIPE_KEY = 'pose_atlas_recipe_v3'
const HISTORY_KEY = 'pose_atlas_history_v3'
const SAFE_MODE_KEY = 'pose_atlas_safe_mode_v1'
+const COMFY_CONFIG_VERSION = 2
+
const DEFAULT_API_CONFIG: ApiConfig = {
activeProvider: 'gemini',
imagePassMode: 'url',
@@ -107,16 +111,48 @@ const DEFAULT_API_CONFIG: ApiConfig = {
responseFormat: 'b64_json',
},
comfyuiParams: {
- workflowPreset: 'qwen21_3img',
- workflowJson: JSON.stringify(COMFYUI_WORKFLOW_TEMPLATES.qwen21_3img.workflow, null, 2),
- ...COMFYUI_WORKFLOW_TEMPLATES.qwen21_3img.defaults,
+ workflowPreset: DEFAULT_COMFY_PRESET,
+ workflowJson: JSON.stringify(COMFYUI_WORKFLOW_TEMPLATES[DEFAULT_COMFY_PRESET].workflow, null, 2),
+ ...COMFYUI_WORKFLOW_TEMPLATES[DEFAULT_COMFY_PRESET].defaults,
randomizeSeed: true,
+ timeoutSec: 600,
+ configVersion: COMFY_CONFIG_VERSION,
},
}
+// v1 → v2:旧版默认的 Qwen 2.1 预设引用了本机通常不存在的模型,自动切换为 Qwen Image Edit 2509
+const LEGACY_COMFY_BASE_URLS = ['http://127.0.0.1:8188', 'http://localhost:8188']
+
+function migrateComfy(
+ provider: ApiConfig['providers']['comfyui'],
+ params: ApiConfig['comfyuiParams']
+): { provider: ApiConfig['providers']['comfyui']; params: ApiConfig['comfyuiParams'] } {
+ if ((params.configVersion ?? 1) >= COMFY_CONFIG_VERSION) return { provider, params }
+ const nextParams = { ...params, configVersion: COMFY_CONFIG_VERSION }
+ if (params.workflowPreset === 'qwen21_3img' || params.workflowPreset === 'qwen21_2img') {
+ Object.assign(nextParams, {
+ workflowPreset: DEFAULT_COMFY_PRESET,
+ workflowJson: JSON.stringify(COMFYUI_WORKFLOW_TEMPLATES[DEFAULT_COMFY_PRESET].workflow, null, 2),
+ ...COMFYUI_WORKFLOW_TEMPLATES[DEFAULT_COMFY_PRESET].defaults,
+ })
+ }
+ const nextProvider = { ...provider }
+ if (!nextProvider.baseUrl.trim() || LEGACY_COMFY_BASE_URLS.includes(nextProvider.baseUrl.trim())) {
+ nextProvider.baseUrl = PROVIDER_META.comfyui.defaultBaseUrl
+ }
+ if (!PROVIDER_META.comfyui.models.some((m) => m.value === nextProvider.model)) {
+ nextProvider.model = PROVIDER_META.comfyui.models[0].value
+ }
+ return { provider: nextProvider, params: nextParams }
+}
+
function mergeStoredConfig(raw: unknown): ApiConfig {
if (!raw || typeof raw !== 'object') return DEFAULT_API_CONFIG
const obj = raw as Partial<ApiConfig>
+ const comfy = migrateComfy(
+ { ...DEFAULT_API_CONFIG.providers.comfyui, ...obj.providers?.comfyui },
+ { ...DEFAULT_API_CONFIG.comfyuiParams, configVersion: 1, ...obj.comfyuiParams }
+ )
return {
...DEFAULT_API_CONFIG,
...obj,
@@ -125,13 +161,13 @@ function mergeStoredConfig(raw: unknown): ApiConfig {
gpt: { ...DEFAULT_API_CONFIG.providers.gpt, ...obj.providers?.gpt },
qwen: { ...DEFAULT_API_CONFIG.providers.qwen, ...obj.providers?.qwen },
doubao: { ...DEFAULT_API_CONFIG.providers.doubao, ...obj.providers?.doubao },
- comfyui: { ...DEFAULT_API_CONFIG.providers.comfyui, ...obj.providers?.comfyui },
+ comfyui: comfy.provider,
},
gptParams: { ...DEFAULT_API_CONFIG.gptParams, ...obj.gptParams },
geminiParams: { ...DEFAULT_API_CONFIG.geminiParams, ...obj.geminiParams },
qwenParams: { ...DEFAULT_API_CONFIG.qwenParams, ...obj.qwenParams },
doubaoParams: { ...DEFAULT_API_CONFIG.doubaoParams, ...obj.doubaoParams },
- comfyuiParams: { ...DEFAULT_API_CONFIG.comfyuiParams, ...obj.comfyuiParams },
+ comfyuiParams: comfy.params,
}
}
@@ -149,6 +185,8 @@ interface StudioContextValue {
setPreviewUrl: (url: string) => void
isGenerating: boolean
generatingModel: string
+ generationProgress: GenerationProgress | null
+ generationError: string
startGeneration: (overridePrompt?: string) => Promise<void>
refreshStitchPreview: () => Promise<void>
setPortrait: (item: PortraitItem | null) => void
@@ -235,6 +273,8 @@ export function StudioProvider({ children }: { children: ReactNode }) {
})
const [isGenerating, setIsGenerating] = useState(false)
const [generatingModel, setGeneratingModel] = useState('')
+ const [generationProgress, setGenerationProgress] = useState<GenerationProgress | null>(null)
+ const [generationError, setGenerationError] = useState('')
const isGeneratingRef = useRef(false)
const idbHydratedRef = useRef(false)
@@ -478,12 +518,15 @@ export function StudioProvider({ children }: { children: ReactNode }) {
isGeneratingRef.current = true
setIsGenerating(true)
setGeneratingModel(modelName)
+ setGenerationError('')
+ setGenerationProgress(null)
try {
const { imageUrl, modelUsed, providerUsed } = await generateCharacterImage({
recipe: recipeSnapshot,
prompt: finalPrompt,
config: configSnapshot,
+ onProgress: setGenerationProgress,
})
setPreviewUrl(imageUrl)
const extraCount = (recipeSnapshot.extraImages || []).length
@@ -504,10 +547,13 @@ export function StudioProvider({ children }: { children: ReactNode }) {
})
toast.success(`角色图生成完成 (${modelUsed})`)
} catch (e) {
- toast.error(e instanceof Error ? e.message : '调用失败')
+ const msg = e instanceof Error ? e.message : '调用失败'
+ setGenerationError(msg)
+ toast.error(msg.split('\n')[0])
} finally {
isGeneratingRef.current = false
setIsGenerating(false)
+ setGenerationProgress(null)
}
}
@@ -526,6 +572,8 @@ export function StudioProvider({ children }: { children: ReactNode }) {
setPreviewUrl,
isGenerating,
generatingModel,
+ generationProgress,
+ generationError,
startGeneration,
refreshStitchPreview,
setPortrait,
diff --git a/src/types/gallery.ts b/src/types/gallery.ts
index 5088efa..67fd3cc 100644
--- a/src/types/gallery.ts
+++ b/src/types/gallery.ts
@@ -74,7 +74,7 @@ export interface ProviderEndpointConfig {
export type ImagePassMode = 'url' | 'base64'
export interface ComfyUiParams {
- workflowPreset: 'qwen21_3img' | 'qwen21_2img' | 'custom'
+ workflowPreset: 'qwen2509_3img' | 'qwen2509_2img' | 'qwen21_3img' | 'qwen21_2img' | 'custom'
workflowJson: string
promptNodeId: string
portraitNodeId: string
@@ -82,6 +82,10 @@ export interface ComfyUiParams {
poseNodeId: string
stitchedNodeId: string
randomizeSeed: boolean
+ /** 单次生成最长等待秒数 */
+ timeoutSec: number
+ /** 配置结构版本,用于迁移旧版默认值 */
+ configVersion?: number
}
export interface GptParams {
diff --git a/src/workflows/qwen_image_edit_2509_3img.api.json b/src/workflows/qwen_image_edit_2509_3img.api.json
new file mode 100644
index 0000000..0b2b762
--- /dev/null
+++ b/src/workflows/qwen_image_edit_2509_3img.api.json
@@ -0,0 +1,365 @@
+{
+ "1": {
+ "class_type": "LoadImage",
+ "inputs": {
+ "image": "portrait_ref.png"
+ },
+ "_meta": {
+ "title": "图1:人像参考 (Portrait)"
+ }
+ },
+ "2": {
+ "class_type": "LoadImage",
+ "inputs": {
+ "image": "costume_ref.png"
+ },
+ "_meta": {
+ "title": "图2:服装参考 (Costume)"
+ }
+ },
+ "3": {
+ "class_type": "LoadImage",
+ "inputs": {
+ "image": "pose_ref.png"
+ },
+ "_meta": {
+ "title": "图3:姿势参考 (Pose)"
+ }
+ },
+ "4": {
+ "class_type": "UNETLoader",
+ "inputs": {
+ "unet_name": "qwen_image_edit_2509_fp8_e4m3fn.safetensors",
+ "weight_dtype": "default"
+ },
+ "_meta": {
+ "title": "加载 Qwen Image Edit 2509 模型"
+ }
+ },
+ "5": {
+ "class_type": "LoraLoaderModelOnly",
+ "inputs": {
+ "model": [
+ "4",
+ 0
+ ],
+ "lora_name": "Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors",
+ "strength_model": 1.0
+ },
+ "_meta": {
+ "title": "Lightning 4 步加速 LoRA (2509 专用)"
+ }
+ },
+ "6": {
+ "class_type": "ModelSamplingAuraFlow",
+ "inputs": {
+ "model": [
+ "5",
+ 0
+ ],
+ "shift": 3.0
+ },
+ "_meta": {
+ "title": "ModelSamplingAuraFlow"
+ }
+ },
+ "7": {
+ "class_type": "CFGNorm",
+ "inputs": {
+ "model": [
+ "6",
+ 0
+ ],
+ "strength": 1.0
+ },
+ "_meta": {
+ "title": "CFGNorm"
+ }
+ },
+ "8": {
+ "class_type": "CLIPLoader",
+ "inputs": {
+ "clip_name": "qwen_2.5_vl_7b_fp8_scaled.safetensors",
+ "type": "qwen_image",
+ "device": "default"
+ },
+ "_meta": {
+ "title": "加载 Qwen2.5-VL 7B 文本编码器"
+ }
+ },
+ "9": {
+ "class_type": "VAELoader",
+ "inputs": {
+ "vae_name": "qwen_image_vae.safetensors"
+ },
+ "_meta": {
+ "title": "加载 Qwen Image VAE"
+ }
+ },
+ "10": {
+ "class_type": "ImageScaleToTotalPixels",
+ "inputs": {
+ "image": [
+ "3",
+ 0
+ ],
+ "upscale_method": "lanczos",
+ "megapixels": 1.0,
+ "resolution_steps": 16
+ },
+ "_meta": {
+ "title": "按姿势图比例确定输出尺寸 (megapixels 越小越省显存)"
+ }
+ },
+ "11": {
+ "class_type": "VAEEncode",
+ "inputs": {
+ "pixels": [
+ "10",
+ 0
+ ],
+ "vae": [
+ "9",
+ 0
+ ]
+ },
+ "_meta": {
+ "title": "输出画布 Latent"
+ }
+ },
+ "12": {
+ "class_type": "TextEncodeQwenImageEditPlus",
+ "inputs": {
+ "clip": [
+ "8",
+ 0
+ ],
+ "image1": [
+ "1",
+ 0
+ ],
+ "image2": [
+ "2",
+ 0
+ ],
+ "image3": [
+ "3",
+ 0
+ ],
+ "prompt": ""
+ },
+ "_meta": {
+ "title": "正向提示词 (Positive Prompt,三图均送入视觉语言编码器)"
+ }
+ },
+ "13": {
+ "class_type": "TextEncodeQwenImageEditPlus",
+ "inputs": {
+ "clip": [
+ "8",
+ 0
+ ],
+ "image1": [
+ "1",
+ 0
+ ],
+ "image2": [
+ "2",
+ 0
+ ],
+ "image3": [
+ "3",
+ 0
+ ],
+ "prompt": ""
+ },
+ "_meta": {
+ "title": "反向提示词 (Negative Prompt,cfg=1 时不生效)"
+ }
+ },
+ "14": {
+ "class_type": "KSampler",
+ "inputs": {
+ "model": [
+ "7",
+ 0
+ ],
+ "positive": [
+ "25",
+ 0
+ ],
+ "negative": [
+ "27",
+ 0
+ ],
+ "latent_image": [
+ "11",
+ 0
+ ],
+ "seed": 20261031,
+ "steps": 4,
+ "cfg": 1.0,
+ "sampler_name": "euler",
+ "scheduler": "simple",
+ "denoise": 1.0
+ },
+ "_meta": {
+ "title": "KSampler (4 步 Lightning)"
+ }
+ },
+ "15": {
+ "class_type": "VAEDecode",
+ "inputs": {
+ "samples": [
+ "14",
+ 0
+ ],
+ "vae": [
+ "9",
+ 0
+ ]
+ },
+ "_meta": {
+ "title": "VAE 解码"
+ }
+ },
+ "16": {
+ "class_type": "SaveImage",
+ "inputs": {
+ "images": [
+ "15",
+ 0
+ ],
+ "filename_prefix": "pose_studio/result"
+ },
+ "_meta": {
+ "title": "保存生成结果"
+ }
+ },
+ "20": {
+ "class_type": "ImageScaleToTotalPixels",
+ "inputs": {
+ "image": [
+ "1",
+ 0
+ ],
+ "upscale_method": "lanczos",
+ "megapixels": 1.0,
+ "resolution_steps": 16
+ },
+ "_meta": {
+ "title": "人像参考缩放"
+ }
+ },
+ "21": {
+ "class_type": "VAEEncode",
+ "inputs": {
+ "pixels": [
+ "20",
+ 0
+ ],
+ "vae": [
+ "9",
+ 0
+ ]
+ },
+ "_meta": {
+ "title": "人像参考 Latent"
+ }
+ },
+ "22": {
+ "class_type": "ImageScaleToTotalPixels",
+ "inputs": {
+ "image": [
+ "2",
+ 0
+ ],
+ "upscale_method": "lanczos",
+ "megapixels": 1.0,
+ "resolution_steps": 16
+ },
+ "_meta": {
+ "title": "服装参考缩放"
+ }
+ },
+ "23": {
+ "class_type": "VAEEncode",
+ "inputs": {
+ "pixels": [
+ "22",
+ 0
+ ],
+ "vae": [
+ "9",
+ 0
+ ]
+ },
+ "_meta": {
+ "title": "服装参考 Latent"
+ }
+ },
+ "24": {
+ "class_type": "ReferenceLatent",
+ "inputs": {
+ "conditioning": [
+ "12",
+ 0
+ ],
+ "latent": [
+ "21",
+ 0
+ ]
+ },
+ "_meta": {
+ "title": "正向:附加人像参考"
+ }
+ },
+ "25": {
+ "class_type": "ReferenceLatent",
+ "inputs": {
+ "conditioning": [
+ "24",
+ 0
+ ],
+ "latent": [
+ "23",
+ 0
+ ]
+ },
+ "_meta": {
+ "title": "正向:附加服装参考"
+ }
+ },
+ "26": {
+ "class_type": "ReferenceLatent",
+ "inputs": {
+ "conditioning": [
+ "13",
+ 0
+ ],
+ "latent": [
+ "21",
+ 0
+ ]
+ },
+ "_meta": {
+ "title": "反向:附加人像参考"
+ }
+ },
+ "27": {
+ "class_type": "ReferenceLatent",
+ "inputs": {
+ "conditioning": [
+ "26",
+ 0
+ ],
+ "latent": [
+ "23",
+ 0
+ ]
+ },
+ "_meta": {
+ "title": "反向:附加服装参考"
+ }
+ }
+}
diff --git a/tsconfig.app.json b/tsconfig.app.json
index ab86dd6..91bb3f9 100644
--- a/tsconfig.app.json
+++ b/tsconfig.app.json
@@ -14,6 +14,7 @@
"verbatimModuleSyntax": true,
"moduleDetection": "force",
"noEmit": true,
+ "resolveJsonModule": true,
"paths": { "@/*": ["./src/*"] },
"jsx": "react-jsx",
diff --git a/vite.config.ts b/vite.config.ts
index a20f2d2..3d74e8d 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -98,6 +98,23 @@ function localAssetsAndProxyPlugin(): Plugin {
}
}
+// 本地 ComfyUI 同源代理:浏览器请求 /comfy/* → ComfyUI(默认 Desktop 端口 8000)
+// 这样无需修改 ComfyUI 的 --enable-cors-header 白名单。可用环境变量 COMFYUI_URL 指定其他地址。
+const COMFYUI_URL = process.env.COMFYUI_URL || 'http://127.0.0.1:8000'
+const comfyProxy = {
+ '/comfy': {
+ target: COMFYUI_URL,
+ changeOrigin: true,
+ ws: true,
+ rewrite: (p: string) => p.replace(/^\/comfy/, '') || '/',
+ configure: (proxy: any) => {
+ // ComfyUI 对带 Origin 的跨源请求做校验,代理转发时去掉浏览器 Origin,按同源请求处理
+ proxy.on('proxyReq', (proxyReq: any) => proxyReq.removeHeader('origin'))
+ proxy.on('proxyReqWs', (proxyReq: any) => proxyReq.removeHeader('origin'))
+ },
+ },
+}
+
export default defineConfig({
publicDir: false,
plugins: [
@@ -124,5 +141,9 @@ export default defineConfig({
},
server: {
port: 5173,
+ proxy: comfyProxy,
+ },
+ preview: {
+ proxy: comfyProxy,
},
})