@kevlns/art-workshop 0.1.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +61 -11
- package/README.md +72 -93
- package/art-workshop.mjs +35 -9
- package/docs/commands.md +82 -0
- package/docs/configuration.md +166 -0
- package/docs/core-guidance.md +80 -13
- package/docs/data-formats.md +167 -0
- package/docs/modes.md +140 -0
- package/docs/troubleshooting.md +84 -0
- package/docs//351/227/256/351/242/230/346/212/245/345/221/212-2026-10-08.md +29 -0
- package/lib/agent.mjs +3 -2
- package/lib/backends.mjs +89 -0
- package/lib/comfyui.mjs +62 -0
- package/lib/design.mjs +29 -0
- package/lib/diagnostics.mjs +83 -0
- package/lib/executor.mjs +10 -6
- package/lib/generate.mjs +6 -4
- package/lib/plan.mjs +6 -3
- package/lib/presentation.mjs +2 -2
- package/lib/providers.mjs +25 -0
- package/lib/runtime.mjs +23 -0
- package/lib/service.mjs +96 -0
- package/lib/settings.mjs +3 -2
- package/lib/shared.mjs +12 -2
- package/lib/task-worker.mjs +58 -0
- package/lib/tasks.mjs +81 -0
- package/lib/workflows.mjs +19 -0
- package/package.json +7 -5
- package/skills/art-workshop/SKILL.md +59 -13
- package/v-cli.plugin.json +250 -15
- package/workflows/vant-builtin-qwen-image-2.1-Q4-8GB.api.json +116 -0
- package/workflows/vant-builtin-qwen-image-2.1-Q4-8GB.json +648 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# art 诊断与任务控制
|
|
2
|
+
|
|
3
|
+
检查分为全局配置、环境、任务编译、请求预演和真实执行五层。前一层通过不能代替后一层;HTTP 服务可达也不能证明 GPU 权重能加载或生成图片。
|
|
4
|
+
|
|
5
|
+
## 配置和环境
|
|
6
|
+
|
|
7
|
+
```powershell
|
|
8
|
+
v-cli art config show
|
|
9
|
+
v-cli art config validate
|
|
10
|
+
v-cli art config validate --backend local-qwen
|
|
11
|
+
v-cli art doctor --backend local-qwen
|
|
12
|
+
v-cli art doctor --backend local-qwen --start
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
config validate 检查字段、模板格式、连接、bindings 和共享执行限制;不遍历所有工程。doctor 检查启动路径、ComfyUI 版本、节点、枚举中的模型和选项及输出索引;不执行全部自定义节点的任意校验逻辑。使用当前配置检查时,冻结任务可能有不同快照,须同时查看其 plan/manifest。
|
|
16
|
+
|
|
17
|
+
仅指定本机 endpoints,不扫描局域网。doctor 不可达时默认退出 1 并提示 --start。--start 可能懒启动;启动失败看返回 issues、嵌套 diagnostic 和 .runtime 中服务日志,检查完可 shutdown。编辑图的 LoadImage 占位值跳过已有图片枚举检查,因为实际输入会上传。
|
|
18
|
+
|
|
19
|
+
远程 doctor 只检查配置。检查百炼安装用 bl --help,认证用 bl auth status,生效区域/URL 用 bl config show;当前安装没有 bl doctor。只读检查不能保证云端联网、额度和模型实际可用,需在任务授权范围内验证真实调用。
|
|
20
|
+
|
|
21
|
+
## 工作流错误结构
|
|
22
|
+
|
|
23
|
+
```powershell
|
|
24
|
+
v-cli art task status TASK
|
|
25
|
+
v-cli art task wait TASK --timeout 60
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
本地环境不兼容、工作流提交拒绝和采样异常分别记录 diagnostic.stage=environment/submission/execution。task 状态、前台等待结果和 manifest 提供错误;单体失败记录也保留 diagnostic。
|
|
29
|
+
|
|
30
|
+
| 字段 | 读取方式 |
|
|
31
|
+
|---|---|
|
|
32
|
+
| kind/stage | 是否工作流相关,失败发生在哪一层 |
|
|
33
|
+
| backend/workflow | 任务实际冻结的后端与模板身份 |
|
|
34
|
+
| promptId | ComfyUI 已接受的执行 ID;提交前失败可能没有 |
|
|
35
|
+
| nodes | 提交校验节点 ID/类型/title/errors,或执行异常节点、异常类型、信息和 traceback |
|
|
36
|
+
| environments | 环境不兼容时的 endpoint、版本和缺失节点/模型 issues |
|
|
37
|
+
| messages | ComfyUI 返回的历史执行消息 |
|
|
38
|
+
| hint | 辅助修复建议,以原始错误为依据 |
|
|
39
|
+
| unsettled | 无法确认后端已经停止;不能宣称回收完成 |
|
|
40
|
+
|
|
41
|
+
启动入口、网络、上传或输出数量错误不一定有节点 diagnostic,应读取 error、logPath 和 manifestPath。ComfyUI 接受 promptId 同时报告节点错误时,art 先请求停止该 prompt,再记录失败,避免遗留执行。
|
|
42
|
+
|
|
43
|
+
## 按原因处理
|
|
44
|
+
|
|
45
|
+
| 现象 | 检查与处理 |
|
|
46
|
+
|---|---|
|
|
47
|
+
| configuration/workflow_format | 修复字段;导出 API JSON,而非画布 nodes/links |
|
|
48
|
+
| workflow_link/workflow_binding | 对照节点 ID、input 名和输出索引;不要只看中文标题 |
|
|
49
|
+
| missing_node | 安装或修复相关 ComfyUI 插件,重启服务后 doctor |
|
|
50
|
+
| missing_model/invalid_node_value | 核对模型文件名、目录及节点实际可选值;检查执行 model 覆盖 |
|
|
51
|
+
| comfyui_version | 升级到支持按 prompt_id 中断的版本,至少 0.39.0 |
|
|
52
|
+
| launch_path/launch_entry/startup | 核对 executable/cwd/args、端口和服务日志;不能用端口占用证明服务兼容 |
|
|
53
|
+
| CUDA OutOfMemory/内存不足 | 降低尺寸或候选工作负载,确认 lowvram 与 CPU 编码配置;重新 plan |
|
|
54
|
+
| 只输出零张或多张图片 | 模板每次须恰好一张最终 output;检查 SaveImage 和 batch_size |
|
|
55
|
+
| 原图或计划哈希变化 | 从当前任务和原图重新 plan,不手改哈希或 frozen plan |
|
|
56
|
+
| 远程调用失败 | 检查百炼入口、凭证、区域、模型限制和任务日志;不自动换模型或后端 |
|
|
57
|
+
|
|
58
|
+
修复模板或配置后重新 plan、run --dry,再在授权范围内重试。检查结果不自动选择替代模型、不改设计尺寸、不下载权重。
|
|
59
|
+
|
|
60
|
+
## 单次任务生命周期
|
|
61
|
+
|
|
62
|
+
| 状态 | 含义与后续 |
|
|
63
|
+
|---|---|
|
|
64
|
+
| queued | 工作进程已提交,等待运行 |
|
|
65
|
+
| running | 可能正在启动服务、等串行锁、加载或采样;查看 currentItem/promptId |
|
|
66
|
+
| succeeded | 所选任务完成;进入人工审查 |
|
|
67
|
+
| failed | 执行失败;读取 error/diagnostic/manifest;可能已有部分图片 |
|
|
68
|
+
| cancelled | 当前任务已取消;查看已完成产物与远程取消说明 |
|
|
69
|
+
| lost | worker 死亡且状态过期;后端可能仍在执行,需要 cancel/shutdown |
|
|
70
|
+
|
|
71
|
+
后台任务不会因终端窗口关闭自动取消。task wait 只等待;--timeout 超时后仍可 status/wait/cancel。task cancel 请求指定任务停止,保留服务;已完成任务会返回原终态。部分图片保留,不自动删除。
|
|
72
|
+
|
|
73
|
+
## shutdown 的完整语义
|
|
74
|
+
|
|
75
|
+
```powershell
|
|
76
|
+
v-cli art task cancel TASK
|
|
77
|
+
v-cli art shutdown
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
cancel 针对一个 art 任务;shutdown 针对当前 --config 识别的全部 art 生图/编辑任务,以及配置和历史冻结任务引用的本地后端:阻止新提交、请求取消、清空队列、全局中断、等待停止、关闭核验后的服务进程。后端上浏览器或其他客户端的任务也会被清空/中断。同步 style analyze 不属于该队列。
|
|
81
|
+
|
|
82
|
+
关闭前核对 PID、创建时间和命令身份。art 启动的服务保存身份;Windows 可定位与 launch.executable 和入口匹配的已有实例。其他平台无法核验的外部进程不能宣称已回收。未确认停止或关闭失败则返回错误;读取失败信息再处理,不广泛终止所有 Python 进程。
|
|
83
|
+
|
|
84
|
+
远程取消终止本地 bl 调用,不保证云端请求停止或费用撤销。remoteCancellation 字段与 shutdown.remoteCancellationUnconfirmed 提供这个边界;不能把本地 cancelled 当作云端已取消。
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# art-workshop 历史试验记录:2026-10-08
|
|
2
|
+
|
|
3
|
+
本文件记录早期远程图标/单位试验的观察,现已按当前文档体系整理为历史档案。它不规定 0.2.1 的接口、当前价格、通用模型规律或生产资产验收。当前调用以 [README](../README.md)、[模式规范](modes.md) 和实时命令元数据为准。
|
|
4
|
+
|
|
5
|
+
## 原试验范围
|
|
6
|
+
|
|
7
|
+
原记录涉及细胞题材技能图标、单位概念图,以 ATP 工厂原画为风格参照,尝试赛璐璐处理与无框图案。原记录写明使用 qwen-image-2.1-pro、迭代 8 轮、出图 25 张、累计约 6.3 元;这些是当时记录,不是本轮重新核对的账单,也不用于估算当前任务费用。
|
|
8
|
+
|
|
9
|
+
## 保留的观察与当前设计归属
|
|
10
|
+
|
|
11
|
+
| 历史观察 | 当时采取的办法 | 当前结构中的位置 |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| 将风格参考作为编辑底图时,输出复刻参考主体,需求主体丢失 | 分析参考风格后走文生图 | style refs 用于分析;edit source 是真正底图 |
|
|
14
|
+
| 多张候选构图相似 | 调整 seed 进行对照 | item/variant seed;不将相邻 seed 相似推广为必然规律 |
|
|
15
|
+
| 只有效果词时主体不可读 | 明确主体、动作和效果 | entities/effects/relations/mustRead |
|
|
16
|
+
| 出现未要求的边框或环形外壳 | 澄清正向构图及表面作用范围 | profile 资产约束、core.domains、item 内容 |
|
|
17
|
+
| 全局冷色规则覆盖红细胞身份色 | 保留主体色,统一渲染而非同色 | item.color 和 core.color 的职责分离 |
|
|
18
|
+
| 枚举多种细胞被理解为画面元素清单 | 从全局风格删除具体内容枚举 | 核心只保留可复用规则,实体清单属于任务 |
|
|
19
|
+
| 风格变更影响其他图片 | 固定可控参数做少量 A/B | 新核心与新计划、compare、逐图人工审查 |
|
|
20
|
+
|
|
21
|
+
这些观察来自特定版本和样本。负向词的构图作用、相邻 seed 的关系、参考图尺寸限制等不能据此写成适用于所有模型的硬规则。
|
|
22
|
+
|
|
23
|
+
## 遗留项的边界
|
|
24
|
+
|
|
25
|
+
原报告列出配色去枚举化、部分技能重跑和新主体扩展等后续建议。本轮只更新工具与文档,没有重新评估或代为完成这些历史美术任务;其状态需以相应工程需求、产物和人工审查为准。
|
|
26
|
+
|
|
27
|
+
原先的 styleBlock、renderBlock、values、seedStride、旧风格包目录与早期 anchor/batch 入口不再构成当前协议。当前结构为 project/core/profile/job,命令为 style、generate、edit、task、check/select。当前 check 已实现记录完整性与人工审查汇总,仍不自动生成视觉一致性评分。
|
|
28
|
+
|
|
29
|
+
不要复制历史配置作为新工程输入。按当前模式规范重新发现任务、编译计划、预演,在授权范围内执行,再做 content/style/asset 验收;费用与模型限制从当前提供方配置和实际结果确认。
|
package/lib/agent.mjs
CHANGED
|
@@ -39,6 +39,7 @@ export function autoSync({root=ROOT,cwd=process.cwd(),home=homedir(),env=process
|
|
|
39
39
|
return syncSkill({root,skillDirs:dirs});
|
|
40
40
|
}
|
|
41
41
|
export function agentIndex(root=ROOT,workspace=loadSettings().paths.workspace) {
|
|
42
|
-
return {tool:'art-workshop',displayName:'美术工坊',root,workspace,config:loadSettings().file,entry:join(root,'art-workshop.mjs'),skill:{name:'art-workshop',source:join(root,'skills','art-workshop','SKILL.md'),automaticDiscovery:true},modules:[{name:'style',actions:['analyze','validate','freeze','show']},{name:'generate',actions:['show','plan','run']},{name:'edit',actions:['show','plan','run']},{name:'refs',actions:['guide']},{name:'agent',actions:['index','docs','init']}],utilities:['list','check','select','compare'],projects:(directory(join(workspace,'projects'))?readdirSync(join(workspace,'projects')):[]).filter(id=>directory(join(workspace,'projects',id))).map(id=>{const p=loadProject(id,workspace);return {id:p.id,cores:p.cores,defaultCore:p.defaultCore,jobs:readdirSync(join(workspace,'projects',id,'jobs')).filter(x=>x.endsWith('.json')).map(x=>x.slice(0,-5))};})};
|
|
42
|
+
return {tool:'art-workshop',displayName:'美术工坊',root,workspace,config:loadSettings().file,entry:join(root,'art-workshop.mjs'),skill:{name:'art-workshop',source:join(root,'skills','art-workshop','SKILL.md'),automaticDiscovery:true},modules:[{name:'style',actions:['analyze','validate','freeze','show']},{name:'generate',actions:['show','plan','run']},{name:'edit',actions:['show','plan','run']},{name:'refs',actions:['guide']},{name:'agent',actions:['index','docs','init']}],utilities:['list','check','select','compare','task','shutdown','doctor','workflow','config'],projects:(directory(join(workspace,'projects'))?readdirSync(join(workspace,'projects')):[]).filter(id=>directory(join(workspace,'projects',id))).map(id=>{const p=loadProject(id,workspace);return {id:p.id,cores:p.cores,defaultCore:p.defaultCore,jobs:readdirSync(join(workspace,'projects',id,'jobs')).filter(x=>x.endsWith('.json')).map(x=>x.slice(0,-5))};})};
|
|
43
43
|
}
|
|
44
|
-
export
|
|
44
|
+
export const GUIDE_FILES=['README.md','docs/modes.md','docs/configuration.md','docs/data-formats.md','docs/commands.md','docs/troubleshooting.md','docs/core-guidance.md'];
|
|
45
|
+
export function agentDocs(root=ROOT) {return GUIDE_FILES.map(file=>readFileSync(join(root,file),'utf8')).join('\n\n');}
|
package/lib/backends.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import {keys,id,integer,hash,text} from './shared.mjs';
|
|
2
|
+
import {execution} from './plan.mjs';
|
|
3
|
+
import {BUILTIN_WORKFLOW,loadBackend} from './workflows.mjs';
|
|
4
|
+
import {templateIssues} from './diagnostics.mjs';
|
|
5
|
+
|
|
6
|
+
export function generationConfig(value={}) {
|
|
7
|
+
keys(value,['defaultBackend','defaults','backends'],'generation');
|
|
8
|
+
keys(value.defaults??{},['size','perValue','promptExtend','watermark'],'generation.defaults');
|
|
9
|
+
execution({},value.defaults??{});
|
|
10
|
+
for(const [name,b] of Object.entries(value.backends??{})) {
|
|
11
|
+
id(name,'backend'); keys(b,['provider','model','execution','endpoints','workflow','bindings','options','launch'],'backend');
|
|
12
|
+
if(!['bailian','comfyui'].includes(b.provider)) throw new Error('provider 必须为 bailian/comfyui');
|
|
13
|
+
keys(b.execution??{},['size','perValue','promptExtend','watermark'],'backend.execution');
|
|
14
|
+
execution({},b.execution??{});
|
|
15
|
+
text(b.model,'backend.model');
|
|
16
|
+
if(b.provider==='bailian')continue;
|
|
17
|
+
if(!Array.isArray(b.endpoints)||!b.endpoints.length)throw new Error('ComfyUI 需要 endpoints');
|
|
18
|
+
for(const endpoint of b.endpoints) {const u=new URL(endpoint);if(u.protocol!=='http:'||!['127.0.0.1','localhost','[::1]'].includes(u.hostname)||u.username||u.password||u.pathname!=='/'||u.search||u.hash)throw new Error('ComfyUI endpoint 必须是本机 HTTP 根地址');}
|
|
19
|
+
const builtin=b.workflow===undefined||b.workflow===BUILTIN_WORKFLOW;
|
|
20
|
+
if(!builtin&&(!b.workflow||!b.bindings))throw new Error('自定义 ComfyUI 需要 API workflow 和 bindings');
|
|
21
|
+
if(b.workflow!==undefined)text(b.workflow,'workflow');
|
|
22
|
+
keys(b.bindings??{},['prompt','negativePrompt','width','height','seed','count','model','image','vae'],'bindings');
|
|
23
|
+
for(const field of ['prompt','width','height','seed'])if(!builtin&&!b.bindings[field])throw new Error('缺少 binding '+field);
|
|
24
|
+
for(const binding of Object.values(b.bindings??{})){keys(binding,['node','input'],'binding');if(!binding.node||!binding.input)throw new Error('binding 需要 node/input');}
|
|
25
|
+
keys(b.options??{},['steps','cfg','sampler','scheduler','timeoutSeconds','pollMilliseconds'],'backend.options');
|
|
26
|
+
integer(b.options?.timeoutSeconds??1800,'timeoutSeconds',1,86400);
|
|
27
|
+
integer(b.options?.pollMilliseconds??500,'pollMilliseconds',50,10000);
|
|
28
|
+
if(b.options?.steps!==undefined)integer(b.options.steps,'steps',1,10000);
|
|
29
|
+
if(b.options?.cfg!==undefined&&(!Number.isFinite(b.options.cfg)||b.options.cfg<0||b.options.cfg>100))throw new Error('cfg 必须为 0~100 的数值');
|
|
30
|
+
if(b.launch) {keys(b.launch,['executable','args','cwd','startupTimeoutSeconds'],'launch');if(!b.launch.executable||!b.launch.cwd||!Array.isArray(b.launch.args)||b.launch.args.some(x=>typeof x!=='string'))throw new Error('launch 需要 executable/cwd/args');integer(b.launch.startupTimeoutSeconds??120,'startupTimeoutSeconds',1,600);}
|
|
31
|
+
}
|
|
32
|
+
if(value.defaultBackend&&!value.backends?.[value.defaultBackend])throw new Error('defaultBackend 未定义');
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function backendDefaults(settings,selection) {
|
|
37
|
+
const name=selection??settings.generation?.defaultBackend;
|
|
38
|
+
if(!name)return settings.execution;
|
|
39
|
+
const b=settings.generation.backends[name];if(!b)throw new Error('未知 backend '+name);
|
|
40
|
+
const builtin=b.provider==='comfyui'&&(b.workflow===undefined||b.workflow===BUILTIN_WORKFLOW);
|
|
41
|
+
return {...(builtin?{size:'512*512'}:{}),...settings.generation.defaults,...b.execution,...(b.model?{model:b.model}:{}),...settings.execution};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function bindBackend(plan,settings,selection) {
|
|
45
|
+
const name=selection??settings.generation?.defaultBackend;
|
|
46
|
+
if(!name)return plan;
|
|
47
|
+
const backend=loadBackend(settings,name);
|
|
48
|
+
if(backend.provider==='comfyui') {
|
|
49
|
+
const workflow=backend.template;
|
|
50
|
+
const issues=templateIssues(backend);if(issues.length)throw new Error('工作流校验失败:'+JSON.stringify(issues));
|
|
51
|
+
if(workflow.nodes||!Object.keys(workflow).length||Object.values(workflow).some(n=>!n.class_type||!n.inputs))throw new Error('workflow 必须为 ComfyUI API 格式');
|
|
52
|
+
backend.workflowHash=hash(workflow);
|
|
53
|
+
for(const binding of Object.values(backend.bindings))if(!workflow[binding.node]?.inputs||!(binding.input in workflow[binding.node].inputs))throw new Error('binding 指向不存在的节点输入');
|
|
54
|
+
for(const item of plan.items)buildComfyRequest({...plan,backend},item);
|
|
55
|
+
}
|
|
56
|
+
const {planHash,...body}=plan;
|
|
57
|
+
return {...body,backend:{id:name,...backend},planHash:hash({...body,backend:{id:name,...backend}})};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function buildComfyRequest(plan,item,{index=0,image}={}) {
|
|
61
|
+
const b=plan.backend,e=item.execution??plan.execution;
|
|
62
|
+
const [width,height]=e.size.split('*').map(Number);
|
|
63
|
+
if(width%32||height%32)throw new Error('本地宽高必须为 32 的倍数;不自动改变设计尺寸');
|
|
64
|
+
if(e.promptExtend||e.watermark)throw new Error('ComfyUI 后端不支持 promptExtend/watermark');
|
|
65
|
+
if(item.negativeBlock&&!b.bindings.negativePrompt)throw new Error('负向词非空但未配置 negativePrompt binding');
|
|
66
|
+
if(!b.bindings.count&&Object.values(b.template).some(n=>n.inputs.batch_size!==undefined&&n.inputs.batch_size!==1))throw new Error('串行生图必须配置 count binding 或将 batch_size 固定为 1');
|
|
67
|
+
if(!b.bindings.model&&Object.values(b.template).some(n=>n.class_type==='UnetLoaderGGUF'&&n.inputs.unet_name!==e.model))throw new Error('执行模型与 workflow 模型不一致,需要 model binding');
|
|
68
|
+
if(plan.module==='edit'&&(!b.bindings.image||!b.bindings.vae))throw new Error('图编辑需要 image 和 vae bindings');
|
|
69
|
+
const prompt=structuredClone(b.template);
|
|
70
|
+
const values={prompt:item.prompt,negativePrompt:item.negativeBlock??'',width,height,seed:item.seed+index,count:1,model:e.model,...(image?{image}:{})};
|
|
71
|
+
for(const [field,binding] of Object.entries(b.bindings)) {
|
|
72
|
+
if(field==='vae')continue;
|
|
73
|
+
if(field==='image'&&!image)continue;
|
|
74
|
+
if(field in values)prompt[binding.node].inputs[binding.input]=values[field];
|
|
75
|
+
}
|
|
76
|
+
for(const [field,value] of Object.entries(b.options??{})) {
|
|
77
|
+
const sampler=prompt[b.bindings.seed.node];
|
|
78
|
+
const input={steps:'steps',cfg:'cfg',sampler:'sampler_name',scheduler:'scheduler'}[field];
|
|
79
|
+
if(input){if(!(input in sampler.inputs))throw new Error('采样节点缺少参数 '+input);sampler.inputs[input]=value;}
|
|
80
|
+
}
|
|
81
|
+
if(plan.module==='edit') {
|
|
82
|
+
const imageBinding=b.bindings.image,vaeBinding=b.bindings.vae;
|
|
83
|
+
const encoder=prompt[b.bindings.prompt.node];
|
|
84
|
+
delete encoder.inputs.images;
|
|
85
|
+
encoder.inputs['images.image_1']=[imageBinding.node,0];
|
|
86
|
+
encoder.inputs.vae=[vaeBinding.node,0];
|
|
87
|
+
}
|
|
88
|
+
return {provider:'comfyui',backend:b.id,prompt,seed:item.seed+index,size:e.size};
|
|
89
|
+
}
|
package/lib/comfyui.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {readFileSync,writeFileSync} from 'node:fs';
|
|
2
|
+
import {join,basename} from 'node:path';
|
|
3
|
+
import {randomUUID} from 'node:crypto';
|
|
4
|
+
import {buildComfyRequest} from './backends.mjs';
|
|
5
|
+
import {http,sleep,locked} from './runtime.mjs';
|
|
6
|
+
import {workflowError} from './diagnostics.mjs';
|
|
7
|
+
|
|
8
|
+
export async function cancelPrompt(endpoint,promptId,{all=false}={}) {
|
|
9
|
+
await http(endpoint,'/queue',{method:'POST',body:all?{clear:true}:{delete:[promptId]}});
|
|
10
|
+
const queue=await http(endpoint,'/queue');
|
|
11
|
+
if(all||queue.queue_running?.some(row=>row[1]===promptId)) {
|
|
12
|
+
if(!all&&queue.queue_running.some(row=>row[1]!==promptId))throw new Error('其他任务正在运行,拒绝全局中断');
|
|
13
|
+
// ComfyUI interrupt supports a prompt_id guard, preventing an unrelated next task from being interrupted.
|
|
14
|
+
await http(endpoint,'/interrupt',{method:'POST',body:all?{}:{prompt_id:promptId}});
|
|
15
|
+
}
|
|
16
|
+
const deadline=Date.now()+30000;
|
|
17
|
+
while(true){const q=await http(endpoint,'/queue');const active=[...(q.queue_running??[]),...(q.queue_pending??[])];if(!active.some(row=>all||row[1]===promptId))return;if(Date.now()>deadline)throw new Error('ComfyUI 任务停止超时');await sleep(200);}
|
|
18
|
+
}
|
|
19
|
+
export async function generateComfy(plan,item,{endpoint,root,outputDir,cancelled=()=>false,onPrompt=()=>{}}) {
|
|
20
|
+
return locked(root,'queue-'+new URL(endpoint).port,async()=>{
|
|
21
|
+
const b=plan.backend,e=item.execution??plan.execution,files=[];
|
|
22
|
+
if(item.imageArgs.length>1)throw new Error('当前编辑适配器只接受一张原图');
|
|
23
|
+
let image;
|
|
24
|
+
if(plan.module==='edit') {
|
|
25
|
+
const path=item.imageArgs[0],form=new FormData();form.append('image',new Blob([readFileSync(path)]),randomUUID()+'-'+basename(path));form.append('type','input');
|
|
26
|
+
const response=await fetch(new URL('/upload/image',endpoint),{method:'POST',body:form,signal:AbortSignal.timeout(30000)});
|
|
27
|
+
if(!response.ok)throw new Error('上传编辑原图失败');const upload=await response.json();image=upload.subfolder?upload.subfolder+'/'+upload.name:upload.name;
|
|
28
|
+
}
|
|
29
|
+
for(let index=0;index<e.perValue;index++) {
|
|
30
|
+
if(cancelled())throw new Error('任务已取消');
|
|
31
|
+
const request=buildComfyRequest(plan,item,{index,image});
|
|
32
|
+
let result;
|
|
33
|
+
try{result=await http(endpoint,'/prompt',{method:'POST',body:{prompt:request.prompt,client_id:randomUUID()}});}
|
|
34
|
+
catch(error){if(error.response)throw workflowError('ComfyUI 拒绝工作流:'+JSON.stringify(error.response.error??error.response),b,{stage:'submission',nodeErrors:error.response.node_errors,cause:error});throw error;}
|
|
35
|
+
if(!result.prompt_id||Object.keys(result.node_errors??{}).length){
|
|
36
|
+
const error=workflowError('ComfyUI 拒绝工作流',b,{stage:'submission',promptId:result.prompt_id,nodeErrors:result.node_errors});
|
|
37
|
+
if(result.prompt_id){onPrompt(result.prompt_id);try{await cancelPrompt(endpoint,result.prompt_id);}catch{error.unsettled=true;}}
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
const promptId=result.prompt_id;onPrompt(promptId);
|
|
41
|
+
const deadline=Date.now()+(b.options?.timeoutSeconds??1800)*1000;
|
|
42
|
+
while(true) {
|
|
43
|
+
if(cancelled()){try{await cancelPrompt(endpoint,promptId);}catch(error){error.unsettled=true;throw error;}throw new Error('任务已取消');}
|
|
44
|
+
const history=(await http(endpoint,'/history/'+encodeURIComponent(promptId)))[promptId];
|
|
45
|
+
if(history?.status?.completed) {
|
|
46
|
+
if(history.status.status_str==='error')throw workflowError('ComfyUI 执行失败',b,{promptId,messages:history.status.messages});
|
|
47
|
+
const images=Object.values(history.outputs??{}).flatMap(value=>value.images??[]).filter(x=>x.type==='output');
|
|
48
|
+
if(images.length!==1)throw new Error('每次串行采样应输出恰好一张图片');
|
|
49
|
+
const params=new URLSearchParams(images[0]);const response=await fetch(new URL('/view?'+params,endpoint),{signal:AbortSignal.timeout(30000)});
|
|
50
|
+
if(!response.ok)throw new Error('获取生成图片失败');
|
|
51
|
+
const file=`${item.itemId}--${item.variantId}${index?'--'+(index+1):''}.png`;
|
|
52
|
+
writeFileSync(join(outputDir,file),Buffer.from(await response.arrayBuffer()),{flag:'wx'});files.push(file);break;
|
|
53
|
+
}
|
|
54
|
+
if(history?.status?.status_str==='error')throw workflowError('ComfyUI 执行失败',b,{promptId,messages:history.status.messages});
|
|
55
|
+
if(Date.now()>deadline){try{await cancelPrompt(endpoint,promptId);}catch(error){error.unsettled=true;throw error;}throw new Error('生图超时,已停止任务');}
|
|
56
|
+
await sleep(b.options?.pollMilliseconds??500);
|
|
57
|
+
}
|
|
58
|
+
onPrompt(null);
|
|
59
|
+
}
|
|
60
|
+
return {files};
|
|
61
|
+
});
|
|
62
|
+
}
|
package/lib/design.mjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { keys, integer } from './shared.mjs';
|
|
2
|
+
|
|
3
|
+
// 设计基准归资产规格,单体尺寸与倍率归任务;出图尺寸由它们确定。
|
|
4
|
+
export function design(profile, override, explicitExecution = {}) {
|
|
5
|
+
if (profile.design === undefined) {
|
|
6
|
+
if (override !== undefined) throw new Error('单体 design 需要资产规格声明 design');
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const base = profile.design;
|
|
10
|
+
keys(base, ['resolution', 'outputScale'], 'profile.design');
|
|
11
|
+
keys(base.resolution, ['width', 'height'], 'design.resolution');
|
|
12
|
+
integer(base.resolution.width, 'design.resolution.width', 1, 16384);
|
|
13
|
+
integer(base.resolution.height, 'design.resolution.height', 1, 16384);
|
|
14
|
+
integer(base.outputScale, 'design.outputScale', 1, 4);
|
|
15
|
+
if (override !== undefined) keys(override, ['width', 'height', 'outputScale'], 'item.design');
|
|
16
|
+
const size = override === undefined ? base.resolution : override;
|
|
17
|
+
integer(size.width, 'design.width', 1, base.resolution.width);
|
|
18
|
+
integer(size.height, 'design.height', 1, base.resolution.height);
|
|
19
|
+
const outputScale = override?.outputScale ?? base.outputScale;
|
|
20
|
+
integer(outputScale, 'design.outputScale', 1, 4);
|
|
21
|
+
if (explicitExecution.size !== undefined) throw new Error('设计尺寸与任务 execution.size 不能同时指定');
|
|
22
|
+
const outputSize = { width: size.width * outputScale, height: size.height * outputScale };
|
|
23
|
+
for (const axis of ['width', 'height']) integer(outputSize[axis], `设计换算出图 ${axis}`, 256, 4096);
|
|
24
|
+
return { resolution: { ...base.resolution }, size: { width: size.width, height: size.height }, outputScale, outputSize };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function designPrompt(value) {
|
|
28
|
+
return `UI 设计基准 ${value.resolution.width}×${value.resolution.height};本单体设计尺寸 ${value.size.width}×${value.size.height};出图倍率 ${value.outputScale},画布 ${value.outputSize.width}×${value.outputSize.height} 像素。保持设计宽高比,完整展示并在画布内居中;描边、圆角与细节随同一倍率缩放。`;
|
|
29
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {existsSync,statSync} from 'node:fs';
|
|
2
|
+
import {resolve} from 'node:path';
|
|
3
|
+
import {loadBackend} from './workflows.mjs';
|
|
4
|
+
import {http,runtimeRoot} from './runtime.mjs';
|
|
5
|
+
const issue=(code,message,extra={})=>({code,message,...extra});
|
|
6
|
+
export function templateIssues(backend) {
|
|
7
|
+
const graph=backend.template,issues=[];
|
|
8
|
+
if(!graph||graph.nodes||!Object.keys(graph).length||Object.values(graph).some(n=>!n?.class_type||!n.inputs||typeof n.inputs!=='object'))return [issue('workflow_format','需要非空 ComfyUI API 格式,而非 UI 导出的 nodes/links 格式')];
|
|
9
|
+
for(const [nodeId,node] of Object.entries(graph))for(const [input,value] of Object.entries(node.inputs))if(Array.isArray(value)&&value.length===2&&typeof value[0]==='string'&&Number.isInteger(value[1])) {
|
|
10
|
+
if(!graph[value[0]])issues.push(issue('workflow_link','连接指向不存在的节点',{nodeId,nodeType:node.class_type,input,sourceNode:value[0]}));
|
|
11
|
+
if(value[1]<0)issues.push(issue('workflow_link','输出索引不能为负数',{nodeId,input}));
|
|
12
|
+
}
|
|
13
|
+
for(const [field,b] of Object.entries(backend.bindings??{}))if(!(b.input in (graph[b.node]?.inputs??{})))issues.push(issue('workflow_binding','参数绑定指向不存在的节点输入',{field,nodeId:b.node,input:b.input}));
|
|
14
|
+
return issues;
|
|
15
|
+
}
|
|
16
|
+
export function environmentIssues(backend,info) {
|
|
17
|
+
const issues=[];
|
|
18
|
+
for(const [nodeId,node] of Object.entries(backend.template)) {
|
|
19
|
+
const schema=info[node.class_type];
|
|
20
|
+
if(!schema){issues.push(issue('missing_node','ComfyUI 未注册此节点,请安装或修复对应自定义节点',{nodeId,nodeType:node.class_type}));continue;}
|
|
21
|
+
const inputs={...schema.input?.required,...schema.input?.optional};
|
|
22
|
+
for(const [input,templateValue] of Object.entries(node.inputs)) {
|
|
23
|
+
const value=backend.bindings?.model?.node===nodeId&&backend.bindings.model.input===input?backend.model:templateValue;
|
|
24
|
+
const spec=inputs[input];
|
|
25
|
+
const uploadedImage=backend.bindings?.image?.node===nodeId&&backend.bindings.image.input===input;
|
|
26
|
+
if(!uploadedImage&&Array.isArray(spec?.[0])&&!Array.isArray(value)&&!spec[0].includes(value))issues.push(issue(input.endsWith('_name')?'missing_model':'invalid_node_value','节点输入值不在当前环境可选范围内',{nodeId,nodeType:node.class_type,input,value,available:spec[0]}));
|
|
27
|
+
if(Array.isArray(value)&&value.length===2&&typeof value[0]==='string'&&Number.isInteger(value[1])) {
|
|
28
|
+
const source=info[backend.template[value[0]]?.class_type];
|
|
29
|
+
if(source?.output&&value[1]>=source.output.length)issues.push(issue('workflow_link','连接输出索引超出节点输出范围',{nodeId,input,sourceNode:value[0],output:value[1]}));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return issues;
|
|
34
|
+
}
|
|
35
|
+
export async function inspectEnvironment(endpoint,backend) {
|
|
36
|
+
const stats=await http(endpoint,'/system_stats',{timeout:2000});
|
|
37
|
+
const version=stats.system?.comfyui_version,parts=version?.split('.').map(Number),issues=[];
|
|
38
|
+
if(!version||!Number.isFinite(parts[0])||!Number.isFinite(parts[1])||(parts[0]===0&&parts[1]<39))issues.push(issue('comfyui_version','需要 ComfyUI 0.39.0 或更新版本,以支持按任务中断',{version}));
|
|
39
|
+
const nodes=await http(endpoint,'/object_info',{timeout:10000});
|
|
40
|
+
issues.push(...environmentIssues(backend,nodes));
|
|
41
|
+
return {endpoint,reachable:true,ok:!issues.length,version,devices:stats.devices??[],issues};
|
|
42
|
+
}
|
|
43
|
+
export function validateConfiguration(settings,{backend:selection}={}) {
|
|
44
|
+
const names=selection?[selection]:Object.keys(settings.generation.backends??{}),backends=[];
|
|
45
|
+
for(const name of names) {
|
|
46
|
+
try {
|
|
47
|
+
const b=loadBackend(settings,name),issues=b.provider==='comfyui'?templateIssues(b):[];
|
|
48
|
+
if(b.provider==='comfyui') {
|
|
49
|
+
const execution={...settings.generation.defaults,...b.execution,...settings.execution};
|
|
50
|
+
if(execution.promptExtend||execution.watermark)issues.push(issue('unsupported_option','本地后端不支持 promptExtend/watermark'));
|
|
51
|
+
if(execution.size&&execution.size.split('*').some(x=>Number(x)%32))issues.push(issue('local_dimensions','本地宽高必须为 32 的倍数',{size:execution.size}));
|
|
52
|
+
}
|
|
53
|
+
backends.push({backend:name,provider:b.provider,workflow:b.workflow,issues});
|
|
54
|
+
}
|
|
55
|
+
catch(error){backends.push({backend:name,issues:[issue('configuration',error.message)]});}
|
|
56
|
+
}
|
|
57
|
+
return {ok:backends.every(b=>!b.issues.length),config:settings.file,defaultBackend:settings.generation.defaultBackend??'bailian',backends};
|
|
58
|
+
}
|
|
59
|
+
export async function doctor(settings,{backend:selection,start=false}={}) {
|
|
60
|
+
const validation=validateConfiguration(settings,{backend:selection});if(!validation.ok)return {...validation,stage:'configuration'};
|
|
61
|
+
const reports=[];
|
|
62
|
+
for(const row of validation.backends) {
|
|
63
|
+
if(row.provider!=='comfyui'){reports.push({...row,ok:true,scope:'configuration-only',notes:'远程凭证与配置请核对 bl auth status 和 bl config show;本命令不验证云端连通性,也不发起付费调用'});continue;}
|
|
64
|
+
const b=loadBackend(settings,row.backend),issues=[];b.model=settings.execution.model??b.model;
|
|
65
|
+
if(b.launch) {
|
|
66
|
+
for(const [label,path,type] of [['launch.executable',b.launch.executable,'file'],['launch.cwd',b.launch.cwd,'directory']])if(!existsSync(path)||(type==='file'?!statSync(path).isFile():!statSync(path).isDirectory()))issues.push(issue('launch_path',label+' 不存在或类型不符',{path}));
|
|
67
|
+
const entry=b.launch.args.find(x=>/\.py$|\.mjs$|\.js$/.test(x));if(entry&&!existsSync(resolve(b.launch.cwd,entry)))issues.push(issue('launch_entry','启动入口文件不存在',{path:resolve(b.launch.cwd,entry)}));
|
|
68
|
+
}
|
|
69
|
+
if(start&&!issues.length)try{await (await import('./service.mjs')).ensureService(b,runtimeRoot(settings));}catch(error){issues.push(issue('startup',error.message,{diagnostic:error.diagnostic}));}
|
|
70
|
+
const endpoints=[];
|
|
71
|
+
for(const endpoint of b.endpoints)try{endpoints.push(await inspectEnvironment(endpoint,b));}catch(error){endpoints.push({endpoint,reachable:false,ok:false,issues:[issue('unreachable','无法读取 ComfyUI 环境:'+error.message)]});}
|
|
72
|
+
const available=endpoints.some(e=>e.ok);
|
|
73
|
+
reports.push({...row,ok:available&&!issues.length,issues,endpoints,notes:available?'未执行采样,显存容量和图像效果仍需实际任务验证':start||endpoints.some(e=>e.reachable)?'查看 issues:服务可达但环境不兼容,或启动配置有误':'服务未启动时不能确认插件和模型;使用 doctor --start 启动并检查'});
|
|
74
|
+
}
|
|
75
|
+
return {ok:reports.every(r=>r.ok),config:settings.file,startedOnDemand:start,backends:reports};
|
|
76
|
+
}
|
|
77
|
+
export function workflowError(message,backend,{stage='execution',promptId,nodeErrors={},messages=[],cause}={}) {
|
|
78
|
+
const nodes=Object.entries(nodeErrors).map(([nodeId,data])=>({nodeId,nodeType:data.class_type??backend.template[nodeId]?.class_type,title:backend.template[nodeId]?._meta?.title,errors:data.errors??[data]}));
|
|
79
|
+
for(const entry of messages)if(Array.isArray(entry)&&entry[0]==='execution_error') {const data=entry[1];nodes.push({nodeId:String(data.node_id),nodeType:data.node_type,exceptionType:data.exception_type,message:data.exception_message,traceback:data.traceback});}
|
|
80
|
+
const details=JSON.stringify({nodes,message});
|
|
81
|
+
const hint=/out of memory|OutOfMemory|allocation/i.test(details)?'显存或内存不足:降低出图尺寸,确认 lowvram 和文本编码器 CPU 配置':/not in list|not found|missing/i.test(details)?'运行 art doctor 检查模型文件、插件节点与输入选项':'检查指定节点的输入与连接;运行 art config validate 和 art doctor';
|
|
82
|
+
const error=new Error(message,{cause});error.diagnostic={kind:'workflow',stage,backend:backend.id,workflow:backend.workflow,promptId,nodes,messages,hint};return error;
|
|
83
|
+
}
|
package/lib/executor.mjs
CHANGED
|
@@ -15,11 +15,14 @@ function runtimeVersion() {
|
|
|
15
15
|
export function executePlan(plan, items, { outputRoot=loadSettings().paths.output, dry = false, runner = runBl, log = console.log } = {}) {
|
|
16
16
|
validatePlan(plan);
|
|
17
17
|
if (!items.length) throw new Error('没有选中任何计划项');
|
|
18
|
-
for (const item of items)
|
|
18
|
+
for (const item of items) {
|
|
19
|
+
const expected = plan.items.find(x => x.inputHash === item.inputHash);
|
|
20
|
+
if (!expected || hash(item) !== hash(expected)) throw new Error('执行项不属于该计划或内容已改变');
|
|
21
|
+
}
|
|
19
22
|
for (const ref of plan.refs) { externalPath(ref.path,'原图/参考图'); if (!existsSync(ref.path) || sha256File(ref.path) !== ref.hash) throw new Error(`参考图已变化:${ref.path}`); }
|
|
20
23
|
externalPath(outputRoot,'输出目录');
|
|
21
24
|
if (dry) {
|
|
22
|
-
const requests = items.map(item => ({ itemId: item.itemId, variantId: item.variantId, seed: item.seed,
|
|
25
|
+
const requests = items.map(item => ({ ...(item.design?{design:item.design,execution:item.execution}:{}),itemId: item.itemId, variantId: item.variantId, seed: item.seed,
|
|
23
26
|
inputHash: item.inputHash, args: buildArgs(plan, item, join(outputRoot,'<runId>'), `${item.itemId}--${item.variantId}`) }));
|
|
24
27
|
log(JSON.stringify({ dry: true, remoteCalls: 0, planHash: plan.planHash, requests }, null, 2));
|
|
25
28
|
return { dry: true, requests };
|
|
@@ -39,7 +42,7 @@ export function executePlan(plan, items, { outputRoot=loadSettings().paths.outpu
|
|
|
39
42
|
for (const item of items) {
|
|
40
43
|
const prefix = `${item.itemId}--${item.variantId}`;
|
|
41
44
|
const args = buildArgs(plan, item, runDir, prefix);
|
|
42
|
-
const record = { ...item, args, ok: false, status: 'running', files: [], urls: [], outputHashes: {} };
|
|
45
|
+
const record = { ...structuredClone(item), args, ok: false, status: 'running', files: [], urls: [], outputHashes: {} };
|
|
43
46
|
manifest.items.push(record); persist();
|
|
44
47
|
const before = new Set(readdirSync(runDir));
|
|
45
48
|
try {
|
|
@@ -54,7 +57,8 @@ export function executePlan(plan, items, { outputRoot=loadSettings().paths.outpu
|
|
|
54
57
|
record.files.push(target); record.outputHashes[target] = sha256File(join(runDir, target));
|
|
55
58
|
});
|
|
56
59
|
record.urls = response.urls ?? [];
|
|
57
|
-
|
|
60
|
+
const expectedCount = (item.execution??plan.execution).perValue;
|
|
61
|
+
if (record.files.length !== expectedCount) throw new Error(`期望 ${expectedCount} 张,实际 ${record.files.length} 张`);
|
|
58
62
|
record.ok = true; record.status = 'generated';
|
|
59
63
|
log(`✔ ${item.itemId}/${item.variantId} seed=${item.seed} → ${record.files.join(', ')}`);
|
|
60
64
|
} catch (e) {
|
|
@@ -81,8 +85,8 @@ export function checkRun(manifest, manifestPath, review = manifest.review ?? [])
|
|
|
81
85
|
for (const item of manifest.items) {
|
|
82
86
|
const expected = manifest.plan.items.find(x => x.itemId === item.itemId && x.variantId === item.variantId);
|
|
83
87
|
if (!expected || expected.inputHash !== item.inputHash) throw new Error('执行记录不属于原始计划');
|
|
84
|
-
for (const key of ['prompt', 'seed', 'checks', 'negativeBlock', 'imageArgs']) {
|
|
85
|
-
if (hash(item[key]) !== hash(expected[key])) throw new Error(`执行记录已改变:${item.itemId}.${key}`);
|
|
88
|
+
for (const key of ['prompt', 'seed', 'checks', 'negativeBlock', 'imageArgs', 'execution', 'design']) {
|
|
89
|
+
if (hash({value:item[key]}) !== hash({value:expected[key]})) throw new Error(`执行记录已改变:${item.itemId}.${key}`);
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
92
|
if (!Array.isArray(review)) throw new Error('review 必须为数组');
|
package/lib/generate.mjs
CHANGED
|
@@ -3,12 +3,13 @@ import { ROOT,loadProject,loadJob,projectPath } from './project.mjs';
|
|
|
3
3
|
import {selectCore,corePrompt,coreNegative} from './core.mjs';
|
|
4
4
|
import {execution,finishPlan} from './plan.mjs';
|
|
5
5
|
import {presentation,assetPrompt} from './presentation.mjs';
|
|
6
|
+
import {design,designPrompt} from './design.mjs';
|
|
6
7
|
export function compileGenerate({projectId,jobId,coreId,root=ROOT,localExecution={}}) {
|
|
7
8
|
const project=loadProject(projectId,root),job=loadJob(projectId,jobId,'generate',root);
|
|
8
9
|
keys(job,['project','kind','core','profile','execution','items'],'job');
|
|
9
10
|
const selected=selectCore(project,job,coreId,root),core=selected.core;
|
|
10
11
|
const profile=readJson(projectPath(root,projectId,'profiles',job.profile));
|
|
11
|
-
keys(profile,['project','id','name','constraints','defaults','allowedOverrides','presets','checks'],'profile');
|
|
12
|
+
keys(profile,['project','id','name','constraints','defaults','allowedOverrides','presets','checks','design'],'profile');
|
|
12
13
|
if(profile.project!==projectId||profile.id!==job.profile) throw new Error('资产配置工程身份不匹配');
|
|
13
14
|
keys(profile.constraints,['text','frame','scene','crop','shadow'],'constraints');
|
|
14
15
|
for(const v of Object.values(profile.constraints)) if(typeof v!=='boolean') throw new Error('资产约束必须为布尔值');
|
|
@@ -16,7 +17,8 @@ export function compileGenerate({projectId,jobId,coreId,root=ROOT,localExecution
|
|
|
16
17
|
const items=[],ids=new Set();
|
|
17
18
|
if(!Array.isArray(job.items)||!job.items.length) throw new Error('任务 items 必须非空');
|
|
18
19
|
for(const source of job.items) {
|
|
19
|
-
keys(source,['id','seed','intent','entities','effects','relations','composition','mustRead','variants'],'item');
|
|
20
|
+
keys(source,['id','seed','intent','entities','effects','relations','composition','mustRead','variants','design'],'item');
|
|
21
|
+
const d=design(profile,source.design,job.execution);
|
|
20
22
|
id(source.id); if(ids.has(source.id)) throw new Error('item ID 重复'); ids.add(source.id);
|
|
21
23
|
text(source.intent,'intent'); texts(source.mustRead,'mustRead');
|
|
22
24
|
if(!Array.isArray(source.entities)||!source.entities.length) throw new Error('entities 必须非空');
|
|
@@ -39,10 +41,10 @@ export function compileGenerate({projectId,jobId,coreId,root=ROOT,localExecution
|
|
|
39
41
|
const variants=source.variants?.length?source.variants:[{id:'base'}],variantIds=new Set();
|
|
40
42
|
for(const variant of variants) {
|
|
41
43
|
keys(variant,['id','composition','seed'],'variant');id(variant.id); if(variantIds.has(variant.id)) throw new Error('variant ID 重复');variantIds.add(variant.id);
|
|
42
|
-
const p=presentation(profile,{...source.composition,...variant.composition});
|
|
44
|
+
const p=presentation(profile,{...source.composition,...variant.composition},d);
|
|
43
45
|
if(!objectIds.has(p.value.focalTarget)) throw new Error('视觉焦点不存在');
|
|
44
46
|
const seed=variant.seed??source.seed??parseInt(hash([projectId,jobId,source.id]).slice(0,7),16); integer(seed,'seed');
|
|
45
|
-
items.push({itemId:source.id,variantId:variant.id,seed,brief:{...source,composition:p.value},prompt:`【风格核心】\n${corePrompt(core,source.entities.map(e=>e.kind))}\n【真实需求】\n${source.intent}。\n${lines.join('\n')}\n视觉焦点:${p.value.focalTarget}。\n【构图与资产】\n${p.prompt}\n${assetPrompt(profile)}`,negativeBlock:[coreNegative(core),profile.constraints.text?'':'文字, 字母, 水印',profile.constraints.frame?'':'装饰外框'].filter(Boolean).join(', '),imageArgs:[],checks:{content:source.mustRead,style:[corePrompt(core,source.entities.map(e=>e.kind))],asset:[...profile.checks,p.background]}});
|
|
47
|
+
items.push({...(d?{design:d,execution:execution(project,{...job.execution,size:`${d.outputSize.width}*${d.outputSize.height}`},localExecution)}:{}),itemId:source.id,variantId:variant.id,seed,brief:{...source,composition:p.value},prompt:`【风格核心】\n${corePrompt(core,source.entities.map(e=>e.kind))}\n【真实需求】\n${source.intent}。\n${lines.join('\n')}\n视觉焦点:${p.value.focalTarget}。\n【构图与资产】\n${p.prompt}\n${d ? designPrompt(d) : ""}\n${assetPrompt(profile)}`,negativeBlock:[coreNegative(core),profile.constraints.text?'':'文字, 字母, 水印',profile.constraints.frame?'':'装饰外框'].filter(Boolean).join(', '),imageArgs:[],checks:{content:source.mustRead,style:[corePrompt(core,source.entities.map(e=>e.kind))],asset:[...profile.checks,p.background]}});
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
50
|
return finishPlan({module:'generate',project:projectId,job:jobId,execution:execution(project,job.execution,localExecution),sources:{core:core.id,coreHash:core.coreHash,selection:selected.source},snapshots:{project,core,profile,job},refs:[],items});
|
package/lib/plan.mjs
CHANGED
|
@@ -10,13 +10,16 @@ export function execution(project, overrides = {},local = {}) {
|
|
|
10
10
|
return e;
|
|
11
11
|
}
|
|
12
12
|
export function finishPlan(body) {
|
|
13
|
-
const items = body.items.map(x => ({...x,inputHash:hash({...x,execution:body.execution})}));
|
|
13
|
+
const items = body.items.map(x => ({...x,inputHash:hash({...x,execution:x.execution??body.execution})}));
|
|
14
14
|
const plan = {schema:'art-workshop/plan',...body,items}; return {...plan,planHash:hash(plan)};
|
|
15
15
|
}
|
|
16
16
|
export function validatePlan(plan) {
|
|
17
17
|
if(plan.schema !== 'art-workshop/plan' || !['generate','edit'].includes(plan.module) || !plan.items?.length) throw new Error('不是有效的当前计划');
|
|
18
18
|
const {planHash,...body} = plan; if(hash(body)!==planHash) throw new Error('计划已改变;请修改任务后重新预演');
|
|
19
|
-
for(const {inputHash,...item} of plan.items)
|
|
19
|
+
for(const {inputHash,...item} of plan.items) {
|
|
20
|
+
execution({},item.execution??plan.execution);
|
|
21
|
+
if(hash({...item,execution:item.execution??plan.execution})!==inputHash) throw new Error('计划项校验失败');
|
|
22
|
+
}
|
|
20
23
|
}
|
|
21
24
|
export function selectItems(plan,{item,variant,offset=0,limit}={}) {
|
|
22
25
|
integer(offset,'offset'); if(limit!==undefined) integer(limit,'limit',1);
|
|
@@ -26,7 +29,7 @@ export function selectItems(plan,{item,variant,offset=0,limit}={}) {
|
|
|
26
29
|
return result.filter(x=>ids.includes(x.itemId));
|
|
27
30
|
}
|
|
28
31
|
export function buildArgs(plan,item,outDir,prefix) {
|
|
29
|
-
const e=plan.execution, args=['image',plan.module==='edit'?'edit':'generate'];
|
|
32
|
+
const e=item.execution??plan.execution, args=['image',plan.module==='edit'?'edit':'generate'];
|
|
30
33
|
for(const path of item.imageArgs) args.push('--image',path);
|
|
31
34
|
args.push('--prompt',item.prompt,'--model',e.model,'--size',e.size,'--n',String(e.perValue));
|
|
32
35
|
if(item.negativeBlock) args.push('--negative-prompt',item.negativeBlock);
|
package/lib/presentation.mjs
CHANGED
|
@@ -6,14 +6,14 @@ export function background(value,allowScene=false) {
|
|
|
6
6
|
if(value.type==='scene'&&allowScene) { keys(value,['type','description'],'background'); return `场景背景:${text(value.description,'scene')}。`; }
|
|
7
7
|
throw new Error('背景类型不支持或资产不允许场景');
|
|
8
8
|
}
|
|
9
|
-
export function presentation(profile,overrides={}) {
|
|
9
|
+
export function presentation(profile,overrides={},design=null) {
|
|
10
10
|
keys(overrides,['preset','view','direction','occupancy','effectIntensity','background','focalTarget'],'composition');
|
|
11
11
|
const c={...profile.defaults,...overrides};
|
|
12
12
|
for(const k of ['preset','view','direction','effectIntensity']) choice(c[k],profile.allowedOverrides[k],k);
|
|
13
13
|
const range=profile.allowedOverrides.occupancy;
|
|
14
14
|
if(!Number.isFinite(c.occupancy)||c.occupancy<range.min||c.occupancy>range.max) throw new Error('主体占比越界');
|
|
15
15
|
const bg=background(c.background,profile.constraints.scene);
|
|
16
|
-
return {value:c,prompt:`${profile.presets[c.preset]}。视角:${c.view};方向:${c.direction}
|
|
16
|
+
return {value:c,prompt:`${profile.presets[c.preset]}。视角:${c.view};方向:${c.direction};${design?'保持设计宽高比与四周安全留白':'整体占画幅约 '+c.occupancy*100+'%'};效果强度:${c.effectIntensity}。\n${bg}`,background:bg};
|
|
17
17
|
}
|
|
18
18
|
export function assetPrompt(profile) {
|
|
19
19
|
const c=profile.constraints;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {spawn} from 'node:child_process';
|
|
2
|
+
import {resolveBlEntry} from './bl.mjs';
|
|
3
|
+
import {generateComfy} from './comfyui.mjs';
|
|
4
|
+
|
|
5
|
+
export function generationRequest(plan,item) {
|
|
6
|
+
const execution=item.execution??plan.execution;
|
|
7
|
+
return {module:plan.module,prompt:item.prompt,negativePrompt:item.negativeBlock??'',model:execution.model,size:execution.size,seed:item.seed,count:execution.perValue,images:item.imageArgs,promptExtend:execution.promptExtend,watermark:execution.watermark};
|
|
8
|
+
}
|
|
9
|
+
function generateBailian(request,{outputDir,prefix,cancelled,onRemoteCancel=()=>{}}) {
|
|
10
|
+
const args=['image',request.module==='edit'?'edit':'generate'];
|
|
11
|
+
for(const image of request.images)args.push('--image',image);
|
|
12
|
+
args.push('--prompt',request.prompt,'--model',request.model,'--size',request.size,'--n',String(request.count),'--seed',String(request.seed),'--prompt-extend',String(request.promptExtend),'--watermark',String(request.watermark),'--out-dir',outputDir,'--out-prefix',prefix,'--output','json');
|
|
13
|
+
if(request.negativePrompt)args.push('--negative-prompt',request.negativePrompt);
|
|
14
|
+
return new Promise((resolve,reject)=>{
|
|
15
|
+
const child=spawn(process.execPath,[resolveBlEntry(),...args],{windowsHide:true,stdio:['ignore','pipe','pipe']});
|
|
16
|
+
let out='',err='',overflow=false;
|
|
17
|
+
child.stdout.on('data',chunk=>{out+=chunk;if(out.length>64*1024*1024){overflow=true;child.kill();}});child.stderr.on('data',chunk=>{err=(err+chunk).slice(-4000);});
|
|
18
|
+
const timer=setInterval(()=>{if(cancelled()){onRemoteCancel();child.kill();}},100);
|
|
19
|
+
child.once('error',e=>{clearInterval(timer);reject(e);});
|
|
20
|
+
child.once('exit',code=>{clearInterval(timer);if(cancelled())return reject(new Error('任务已取消'));if(overflow)return reject(new Error('远程响应过大'));if(code!==0)return reject(new Error(err||'bl 调用失败'));try{resolve(JSON.parse(out));}catch{resolve({});}});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export function generate(plan,item,context) {
|
|
24
|
+
return plan.backend?.provider==='comfyui'?generateComfy(plan,item,context):generateBailian(generationRequest(plan,item),context);
|
|
25
|
+
}
|
package/lib/runtime.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {existsSync,mkdirSync,rmSync,readdirSync,lstatSync} from 'node:fs';
|
|
2
|
+
import {join,dirname,resolve,relative,isAbsolute} from 'node:path';
|
|
3
|
+
import {randomUUID} from 'node:crypto';
|
|
4
|
+
import {readJson,writeJson,hash,id} from './shared.mjs';
|
|
5
|
+
export const sleep=ms=>new Promise(r=>setTimeout(r,ms));
|
|
6
|
+
export const terminal=s=>['succeeded','failed','cancelled','lost'].includes(s);
|
|
7
|
+
export const runtimeRoot=settings=>join(dirname(settings.file),'.runtime',hash(settings.file).slice(0,12));
|
|
8
|
+
export function alive(pid){try{process.kill(pid,0);return true;}catch{return false;}}
|
|
9
|
+
export function stateRead(path){return existsSync(path)?readJson(path):null;}
|
|
10
|
+
export async function locked(root,name,fn,{timeout=150000}={}) {
|
|
11
|
+
if(!/^[a-z0-9-]+$/.test(name))throw new Error('非法锁名称');
|
|
12
|
+
mkdirSync(root,{recursive:true});const path=join(root,name+'.lock'),token=randomUUID(),deadline=Date.now()+timeout;
|
|
13
|
+
const rel=relative(resolve(root),resolve(path));if(isAbsolute(rel)||rel.startsWith('..'))throw new Error('锁路径越界');
|
|
14
|
+
while(true){try{mkdirSync(path);writeJson(join(path,'owner.json'),{pid:process.pid,token});break;}catch(e){if(e.code!=='EEXIST')throw e;if(lstatSync(path).isSymbolicLink())throw new Error('锁目录不能是链接');const owner=stateRead(join(path,'owner.json'));if(owner&&!alive(owner.pid)){rmSync(path,{recursive:true});continue;}if(Date.now()>deadline)throw new Error('等待锁超时 '+name);await sleep(100);}}
|
|
15
|
+
try{return await fn();}finally{if(stateRead(join(path,'owner.json'))?.token===token)rmSync(path,{recursive:true});}
|
|
16
|
+
}
|
|
17
|
+
export function taskFile(root,taskId){id(taskId,'task');return join(root,'tasks',taskId+'.json');}
|
|
18
|
+
export function tasks(root){const dir=join(root,'tasks');return existsSync(dir)?readdirSync(dir).filter(x=>x.endsWith('.json')).map(x=>stateRead(join(dir,x))).filter(x=>x?.schema==='art-workshop/task'):[];}
|
|
19
|
+
export async function http(endpoint,path,{method='GET',body,timeout=10000}={}) {
|
|
20
|
+
const response=await fetch(new URL(path,endpoint),{method,signal:AbortSignal.timeout(timeout),...(body?{headers:{'Content-Type':'application/json'},body:JSON.stringify(body)}:{})});
|
|
21
|
+
if(!response.ok){const content=await response.text(),error=new Error(`ComfyUI HTTP ${response.status}: ${content.slice(0,1000)}`);error.httpStatus=response.status;try{error.response=JSON.parse(content);}catch{};throw error;}
|
|
22
|
+
const content=await response.text();return content?JSON.parse(content):{};
|
|
23
|
+
}
|