@kevlns/art-workshop 0.1.3 → 0.3.0
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 +33 -12
- package/README.md +33 -135
- package/art-workshop.mjs +95 -25
- package/docs/commands.md +915 -0
- package/docs/configuration.md +60 -0
- package/docs/core-guidance.md +80 -13
- package/docs/data-formats.md +29 -0
- package/docs/deployment.md +40 -0
- package/docs/model-architecture.md +52 -0
- package/docs/modes.md +39 -0
- package/docs/remote-models.md +42 -0
- package/docs/troubleshooting.md +46 -0
- package/lib/adapters/dashscope.mjs +234 -0
- package/lib/adapters/index.mjs +28 -0
- package/lib/agent.mjs +3 -2
- package/lib/analysis-worker.mjs +25 -0
- package/lib/analyze.mjs +8 -8
- package/lib/backends.mjs +365 -0
- package/lib/bl.mjs +3 -18
- package/lib/candidates.mjs +82 -0
- package/lib/comfyui.mjs +64 -0
- package/lib/contracts.d.ts +67 -0
- package/lib/deployment-worker.mjs +97 -0
- package/lib/deployment.mjs +72 -0
- package/lib/design.mjs +5 -5
- package/lib/diagnostics.mjs +83 -0
- package/lib/dimensions.mjs +22 -0
- package/lib/edit.mjs +6 -3
- package/lib/environments.mjs +67 -0
- package/lib/execution/remote.mjs +156 -0
- package/lib/executor.mjs +10 -24
- package/lib/generate.mjs +9 -5
- package/lib/generation-policy.mjs +198 -0
- package/lib/images.mjs +10 -0
- package/lib/lifecycle.mjs +40 -0
- package/lib/migration.mjs +154 -0
- package/lib/model-catalog.mjs +288 -0
- package/lib/model-registry.mjs +234 -0
- package/lib/plan.mjs +98 -28
- package/lib/project.mjs +13 -4
- package/lib/providers.mjs +30 -0
- package/lib/refs.mjs +12 -16
- package/lib/runtime.mjs +25 -0
- package/lib/service.mjs +137 -0
- package/lib/settings.mjs +5 -6
- package/lib/shared.mjs +12 -2
- package/lib/style-lifecycle.mjs +72 -0
- package/lib/task-references.mjs +35 -0
- package/lib/task-worker.mjs +233 -0
- package/lib/tasks.mjs +458 -0
- package/lib/templates.mjs +69 -0
- package/lib/transport/cli.mjs +69 -0
- package/lib/transport/http.mjs +82 -0
- package/lib/vision.mjs +46 -0
- package/lib/workflows.mjs +28 -0
- package/lib/workspace.mjs +190 -0
- package/package.json +14 -6
- package/skills/art-workshop/SKILL.md +27 -27
- package/v-cli.plugin.json +1484 -135
- 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
package/lib/backends.mjs
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import { resolveDimensions, DIMENSION_CAPABILITIES } from "./dimensions.mjs";
|
|
2
|
+
import { designPrompt } from "./design.mjs";
|
|
3
|
+
import { finishPlan } from "./plan.mjs";
|
|
4
|
+
import { keys, id, integer, hash, text } from "./shared.mjs";
|
|
5
|
+
import { execution } from "./plan.mjs";
|
|
6
|
+
import { BUILTIN_WORKFLOW, loadBackend } from "./workflows.mjs";
|
|
7
|
+
import { templateIssues } from "./diagnostics.mjs";
|
|
8
|
+
import { resolveModel } from "./model-registry.mjs";
|
|
9
|
+
import { validateRemotePlan, freezeRemotePlan } from "./generation-policy.mjs";
|
|
10
|
+
|
|
11
|
+
export function generationConfig(value = {}) {
|
|
12
|
+
keys(value, ["defaultBackend", "defaults", "backends"], "generation");
|
|
13
|
+
execution({}, value.defaults ?? {});
|
|
14
|
+
for (const [name, b] of Object.entries(value.backends ?? {})) {
|
|
15
|
+
id(name, "backend");
|
|
16
|
+
keys(
|
|
17
|
+
b,
|
|
18
|
+
[
|
|
19
|
+
"provider",
|
|
20
|
+
"model",
|
|
21
|
+
"execution",
|
|
22
|
+
"endpoints",
|
|
23
|
+
"workflow",
|
|
24
|
+
"bindings",
|
|
25
|
+
"options",
|
|
26
|
+
"launch",
|
|
27
|
+
"credentialReference",
|
|
28
|
+
],
|
|
29
|
+
"backend",
|
|
30
|
+
);
|
|
31
|
+
if (b.credentialReference) {
|
|
32
|
+
keys(
|
|
33
|
+
b.credentialReference,
|
|
34
|
+
["providerId", "revision", "baseUrl", "api"],
|
|
35
|
+
"backend.credentialReference",
|
|
36
|
+
);
|
|
37
|
+
for (const [key, value] of Object.entries(b.credentialReference))
|
|
38
|
+
text(value, "credentialReference." + key);
|
|
39
|
+
}
|
|
40
|
+
text(b.provider, "backend.provider");
|
|
41
|
+
execution({}, b.execution ?? {});
|
|
42
|
+
text(b.model, "backend.model");
|
|
43
|
+
if (b.provider !== "comfyui") continue;
|
|
44
|
+
if (!Array.isArray(b.endpoints) || !b.endpoints.length)
|
|
45
|
+
throw new Error("ComfyUI 需要 endpoints");
|
|
46
|
+
for (const endpoint of b.endpoints) {
|
|
47
|
+
const u = new URL(endpoint);
|
|
48
|
+
if (
|
|
49
|
+
u.protocol !== "http:" ||
|
|
50
|
+
!["127.0.0.1", "localhost", "[::1]"].includes(u.hostname) ||
|
|
51
|
+
u.username ||
|
|
52
|
+
u.password ||
|
|
53
|
+
u.pathname !== "/" ||
|
|
54
|
+
u.search ||
|
|
55
|
+
u.hash
|
|
56
|
+
)
|
|
57
|
+
throw new Error("ComfyUI endpoint 必须是本机 HTTP 根地址");
|
|
58
|
+
}
|
|
59
|
+
const builtin = b.workflow === undefined || b.workflow === BUILTIN_WORKFLOW;
|
|
60
|
+
if (!builtin && (!b.workflow || !b.bindings))
|
|
61
|
+
throw new Error("自定义 ComfyUI 需要 API workflow 和 bindings");
|
|
62
|
+
if (b.workflow !== undefined) text(b.workflow, "workflow");
|
|
63
|
+
keys(
|
|
64
|
+
b.bindings ?? {},
|
|
65
|
+
[
|
|
66
|
+
"prompt",
|
|
67
|
+
"negativePrompt",
|
|
68
|
+
"width",
|
|
69
|
+
"height",
|
|
70
|
+
"seed",
|
|
71
|
+
"count",
|
|
72
|
+
"model",
|
|
73
|
+
"image",
|
|
74
|
+
"vae",
|
|
75
|
+
],
|
|
76
|
+
"bindings",
|
|
77
|
+
);
|
|
78
|
+
for (const field of ["prompt", "width", "height", "seed"])
|
|
79
|
+
if (!builtin && !b.bindings[field])
|
|
80
|
+
throw new Error("缺少 binding " + field);
|
|
81
|
+
for (const binding of Object.values(b.bindings ?? {})) {
|
|
82
|
+
keys(binding, ["node", "input"], "binding");
|
|
83
|
+
if (!binding.node || !binding.input)
|
|
84
|
+
throw new Error("binding 需要 node/input");
|
|
85
|
+
}
|
|
86
|
+
keys(
|
|
87
|
+
b.options ?? {},
|
|
88
|
+
[
|
|
89
|
+
"steps",
|
|
90
|
+
"cfg",
|
|
91
|
+
"sampler",
|
|
92
|
+
"scheduler",
|
|
93
|
+
"timeoutSeconds",
|
|
94
|
+
"pollMilliseconds",
|
|
95
|
+
],
|
|
96
|
+
"backend.options",
|
|
97
|
+
);
|
|
98
|
+
integer(b.options?.timeoutSeconds ?? 1800, "timeoutSeconds", 1, 86400);
|
|
99
|
+
integer(b.options?.pollMilliseconds ?? 500, "pollMilliseconds", 50, 10000);
|
|
100
|
+
if (b.options?.steps !== undefined)
|
|
101
|
+
integer(b.options.steps, "steps", 1, 10000);
|
|
102
|
+
if (
|
|
103
|
+
b.options?.cfg !== undefined &&
|
|
104
|
+
(!Number.isFinite(b.options.cfg) ||
|
|
105
|
+
b.options.cfg < 0 ||
|
|
106
|
+
b.options.cfg > 100)
|
|
107
|
+
)
|
|
108
|
+
throw new Error("cfg 必须为 0~100 的数值");
|
|
109
|
+
if (b.launch) {
|
|
110
|
+
keys(
|
|
111
|
+
b.launch,
|
|
112
|
+
["executable", "args", "cwd", "startupTimeoutSeconds"],
|
|
113
|
+
"launch",
|
|
114
|
+
);
|
|
115
|
+
if (
|
|
116
|
+
!b.launch.executable ||
|
|
117
|
+
!b.launch.cwd ||
|
|
118
|
+
!Array.isArray(b.launch.args) ||
|
|
119
|
+
b.launch.args.some((x) => typeof x !== "string")
|
|
120
|
+
)
|
|
121
|
+
throw new Error("launch 需要 executable/cwd/args");
|
|
122
|
+
integer(
|
|
123
|
+
b.launch.startupTimeoutSeconds ?? 120,
|
|
124
|
+
"startupTimeoutSeconds",
|
|
125
|
+
1,
|
|
126
|
+
600,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (value.defaultBackend && !value.backends?.[value.defaultBackend])
|
|
131
|
+
throw new Error("defaultBackend 未定义");
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function backendDefaults(settings, selection) {
|
|
136
|
+
const name = selection ?? settings.generation?.defaultBackend;
|
|
137
|
+
if (!name) return settings.execution;
|
|
138
|
+
const b = settings.generation.backends[name];
|
|
139
|
+
if (!b) throw new Error("未知 backend " + name);
|
|
140
|
+
const builtin =
|
|
141
|
+
b.provider === "comfyui" &&
|
|
142
|
+
(b.workflow === undefined || b.workflow === BUILTIN_WORKFLOW);
|
|
143
|
+
return {
|
|
144
|
+
...(builtin ? { size: "512*512" } : {}),
|
|
145
|
+
...settings.generation.defaults,
|
|
146
|
+
...b.execution,
|
|
147
|
+
...(b.model ? { model: b.model } : {}),
|
|
148
|
+
...settings.execution,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function resolvePlanDimensions(plan, capability) {
|
|
153
|
+
const items = plan.items.map((item) => {
|
|
154
|
+
const old = item.design,
|
|
155
|
+
e = item.execution ?? plan.execution;
|
|
156
|
+
const requested =
|
|
157
|
+
old?.requestedSize ??
|
|
158
|
+
old?.outputSize ??
|
|
159
|
+
item.dimensionAdjustment?.requested ??
|
|
160
|
+
Object.fromEntries(
|
|
161
|
+
e.size
|
|
162
|
+
.split("*")
|
|
163
|
+
.map((value, index) => [index ? "height" : "width", Number(value)]),
|
|
164
|
+
);
|
|
165
|
+
const dimensions = resolveDimensions(
|
|
166
|
+
requested,
|
|
167
|
+
typeof capability === "function" ? capability(item) : capability,
|
|
168
|
+
);
|
|
169
|
+
const { inputHash, ...body } = item;
|
|
170
|
+
if (!old)
|
|
171
|
+
return {
|
|
172
|
+
...body,
|
|
173
|
+
execution: {
|
|
174
|
+
...e,
|
|
175
|
+
size: `${dimensions.actual.width}*${dimensions.actual.height}`,
|
|
176
|
+
},
|
|
177
|
+
...(dimensions.clamped
|
|
178
|
+
? { dimensionAdjustment: { requested, actual: dimensions.actual } }
|
|
179
|
+
: {}),
|
|
180
|
+
};
|
|
181
|
+
const design = { ...old, outputSize: dimensions.actual, dimensions };
|
|
182
|
+
return {
|
|
183
|
+
...body,
|
|
184
|
+
design,
|
|
185
|
+
execution: {
|
|
186
|
+
...e,
|
|
187
|
+
size: `${dimensions.actual.width}*${dimensions.actual.height}`,
|
|
188
|
+
},
|
|
189
|
+
prompt: item.prompt.replace(designPrompt(old), designPrompt(design)),
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
const { planHash, ...body } = plan;
|
|
193
|
+
return finishPlan({ ...body, items });
|
|
194
|
+
}
|
|
195
|
+
function bindRemotePlan(plan, backend = plan.backend) {
|
|
196
|
+
const current = { ...plan, ...(backend ? { backend } : {}) };
|
|
197
|
+
const { requests, contract } = freezeRemotePlan(current);
|
|
198
|
+
const { planHash, ...body } = current;
|
|
199
|
+
return finishPlan({
|
|
200
|
+
...body,
|
|
201
|
+
executionContract: contract,
|
|
202
|
+
items: current.items.map(({ inputHash, ...item }, index) => ({
|
|
203
|
+
...item,
|
|
204
|
+
request: requests[index],
|
|
205
|
+
})),
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
export function bindBackend(plan, settings, selection) {
|
|
209
|
+
const name = selection ?? settings.generation?.defaultBackend;
|
|
210
|
+
if (!name) {
|
|
211
|
+
const compatibility = resolveModel(
|
|
212
|
+
"dashscope",
|
|
213
|
+
plan.execution.model,
|
|
214
|
+
plan.module,
|
|
215
|
+
);
|
|
216
|
+
if (!compatibility.supported)
|
|
217
|
+
throw new Error(plan.execution.model + ":" + compatibility.reason);
|
|
218
|
+
plan = resolvePlanDimensions(
|
|
219
|
+
plan,
|
|
220
|
+
(item) =>
|
|
221
|
+
resolveModel(
|
|
222
|
+
compatibility.api,
|
|
223
|
+
plan.execution.model,
|
|
224
|
+
plan.module,
|
|
225
|
+
!!item.imageArgs?.length,
|
|
226
|
+
).dimensions,
|
|
227
|
+
);
|
|
228
|
+
return bindRemotePlan(plan);
|
|
229
|
+
}
|
|
230
|
+
const backend = loadBackend(settings, name, plan.module);
|
|
231
|
+
const compatibility =
|
|
232
|
+
backend.provider !== "comfyui"
|
|
233
|
+
? resolveModel(
|
|
234
|
+
backend.credentialReference?.api ?? "dashscope",
|
|
235
|
+
backend.model,
|
|
236
|
+
plan.module,
|
|
237
|
+
)
|
|
238
|
+
: null;
|
|
239
|
+
if (compatibility && !compatibility.supported)
|
|
240
|
+
throw new Error(backend.model + ":" + compatibility.reason);
|
|
241
|
+
plan = resolvePlanDimensions(
|
|
242
|
+
plan,
|
|
243
|
+
compatibility
|
|
244
|
+
? (item) =>
|
|
245
|
+
resolveModel(
|
|
246
|
+
compatibility.api,
|
|
247
|
+
backend.model,
|
|
248
|
+
plan.module,
|
|
249
|
+
!!item.imageArgs?.length,
|
|
250
|
+
).dimensions
|
|
251
|
+
: DIMENSION_CAPABILITIES[backend.provider],
|
|
252
|
+
);
|
|
253
|
+
if (compatibility) {
|
|
254
|
+
validateRemotePlan({ ...plan, backend });
|
|
255
|
+
return bindRemotePlan(plan, { id: name, ...backend });
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (backend.provider === "comfyui") {
|
|
259
|
+
const workflow = backend.template;
|
|
260
|
+
const issues = templateIssues(backend);
|
|
261
|
+
if (issues.length)
|
|
262
|
+
throw new Error("工作流校验失败:" + JSON.stringify(issues));
|
|
263
|
+
if (
|
|
264
|
+
workflow.nodes ||
|
|
265
|
+
!Object.keys(workflow).length ||
|
|
266
|
+
Object.values(workflow).some((n) => !n.class_type || !n.inputs)
|
|
267
|
+
)
|
|
268
|
+
throw new Error("workflow 必须为 ComfyUI API 格式");
|
|
269
|
+
backend.workflowHash = hash(workflow);
|
|
270
|
+
for (const binding of Object.values(backend.bindings))
|
|
271
|
+
if (
|
|
272
|
+
!workflow[binding.node]?.inputs ||
|
|
273
|
+
!(binding.input in workflow[binding.node].inputs)
|
|
274
|
+
)
|
|
275
|
+
throw new Error("binding 指向不存在的节点输入");
|
|
276
|
+
for (const item of plan.items)
|
|
277
|
+
buildComfyRequest({ ...plan, backend }, item);
|
|
278
|
+
}
|
|
279
|
+
const { planHash, ...body } = plan;
|
|
280
|
+
return {
|
|
281
|
+
...body,
|
|
282
|
+
backend: { id: name, ...backend },
|
|
283
|
+
planHash: hash({ ...body, backend: { id: name, ...backend } }),
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function buildComfyRequest(plan, item, { index = 0, image } = {}) {
|
|
288
|
+
const b = plan.backend,
|
|
289
|
+
e = item.execution ?? plan.execution;
|
|
290
|
+
const [width, height] = e.size.split("*").map(Number);
|
|
291
|
+
if (width % 32 || height % 32)
|
|
292
|
+
throw new Error("本地宽高必须为 32 的倍数;不自动改变设计尺寸");
|
|
293
|
+
if (!Number.isInteger(e.perValue) || e.perValue < 1 || e.perValue > 4)
|
|
294
|
+
throw new Error("本地生成数量必须为 1–4");
|
|
295
|
+
if (e.promptExtend || e.watermark)
|
|
296
|
+
throw new Error("ComfyUI 后端不支持 promptExtend/watermark");
|
|
297
|
+
if (item.negativeBlock && !b.bindings.negativePrompt)
|
|
298
|
+
throw new Error("负向词非空但未配置 negativePrompt binding");
|
|
299
|
+
if (
|
|
300
|
+
!b.bindings.count &&
|
|
301
|
+
Object.values(b.template).some(
|
|
302
|
+
(n) => n.inputs.batch_size !== undefined && n.inputs.batch_size !== 1,
|
|
303
|
+
)
|
|
304
|
+
)
|
|
305
|
+
throw new Error("串行生图必须配置 count binding 或将 batch_size 固定为 1");
|
|
306
|
+
if (
|
|
307
|
+
!b.bindings.model &&
|
|
308
|
+
Object.values(b.template).some(
|
|
309
|
+
(n) =>
|
|
310
|
+
n.class_type === "UnetLoaderGGUF" && n.inputs.unet_name !== e.model,
|
|
311
|
+
)
|
|
312
|
+
)
|
|
313
|
+
throw new Error("执行模型与 workflow 模型不一致,需要 model binding");
|
|
314
|
+
if (plan.module === "edit" && (!b.bindings.image || !b.bindings.vae))
|
|
315
|
+
throw new Error("图编辑需要 image 和 vae bindings");
|
|
316
|
+
const prompt = structuredClone(b.template);
|
|
317
|
+
const values = {
|
|
318
|
+
prompt: item.prompt,
|
|
319
|
+
negativePrompt: item.negativeBlock ?? "",
|
|
320
|
+
width,
|
|
321
|
+
height,
|
|
322
|
+
seed: item.seed + index,
|
|
323
|
+
count: 1,
|
|
324
|
+
model: e.model,
|
|
325
|
+
...(image ? { image } : {}),
|
|
326
|
+
};
|
|
327
|
+
for (const [field, binding] of Object.entries(b.bindings)) {
|
|
328
|
+
if (field === "vae") continue;
|
|
329
|
+
if (field === "image" && !image) continue;
|
|
330
|
+
if (field in values)
|
|
331
|
+
prompt[binding.node].inputs[binding.input] = values[field];
|
|
332
|
+
}
|
|
333
|
+
for (const [field, value] of Object.entries(b.options ?? {})) {
|
|
334
|
+
const sampler = prompt[b.bindings.seed.node];
|
|
335
|
+
const input = {
|
|
336
|
+
steps: "steps",
|
|
337
|
+
cfg: "cfg",
|
|
338
|
+
sampler: "sampler_name",
|
|
339
|
+
scheduler: "scheduler",
|
|
340
|
+
}[field];
|
|
341
|
+
if (input) {
|
|
342
|
+
if (!(input in sampler.inputs))
|
|
343
|
+
throw new Error("采样节点缺少参数 " + input);
|
|
344
|
+
sampler.inputs[input] = value;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (plan.module === "edit") {
|
|
348
|
+
const imageBinding = b.bindings.image,
|
|
349
|
+
vaeBinding = b.bindings.vae;
|
|
350
|
+
const encoder = prompt[b.bindings.prompt.node];
|
|
351
|
+
delete encoder.inputs.images;
|
|
352
|
+
encoder.inputs["images.image_1"] = [
|
|
353
|
+
b.workflow === BUILTIN_WORKFLOW ? "5" : imageBinding.node,
|
|
354
|
+
0,
|
|
355
|
+
];
|
|
356
|
+
encoder.inputs.vae = [vaeBinding.node, 0];
|
|
357
|
+
}
|
|
358
|
+
return {
|
|
359
|
+
provider: "comfyui",
|
|
360
|
+
backend: b.id,
|
|
361
|
+
prompt,
|
|
362
|
+
seed: item.seed + index,
|
|
363
|
+
size: e.size,
|
|
364
|
+
};
|
|
365
|
+
}
|
package/lib/bl.mjs
CHANGED
|
@@ -7,27 +7,12 @@ import { execFileSync } from "node:child_process";
|
|
|
7
7
|
import { createHash } from "node:crypto";
|
|
8
8
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
9
9
|
import { join } from "node:path";
|
|
10
|
-
|
|
11
|
-
let BL_ENTRY = null;
|
|
10
|
+
import {createRequire} from 'node:module';
|
|
12
11
|
|
|
13
12
|
/** 定位 bailian-cli 入口脚本(绕开 .ps1/.cmd shim 与 shell) */
|
|
14
13
|
export function resolveBlEntry() {
|
|
15
|
-
if
|
|
16
|
-
|
|
17
|
-
// Windows 上先看 APPDATA 标准位置,避免为了找入口去 spawn npm(会触发 DEP0190 噪音)
|
|
18
|
-
const cands = [];
|
|
19
|
-
if (process.env.APPDATA) cands.push(join(process.env.APPDATA, "npm", "node_modules", "bailian-cli", "dist", "bailian.mjs"));
|
|
20
|
-
for (const p of cands) if (existsSync(p)) return (BL_ENTRY = p);
|
|
21
|
-
// 兜底:非标准安装位置才去问 npm
|
|
22
|
-
try {
|
|
23
|
-
const root = execFileSync("npm", ["root", "-g"], {
|
|
24
|
-
encoding: "utf8", shell: process.platform === "win32", stdio: ["ignore", "pipe", "ignore"],
|
|
25
|
-
}).trim();
|
|
26
|
-
const p = join(root, "bailian-cli", "dist", "bailian.mjs");
|
|
27
|
-
if (existsSync(p)) return (BL_ENTRY = p);
|
|
28
|
-
} catch { /* npm 不可用 */ }
|
|
29
|
-
for (const p of cands) if (existsSync(p)) return (BL_ENTRY = p);
|
|
30
|
-
throw new Error("找不到 bailian-cli 入口;请设置 BL_BIN 指向 bailian-cli/dist/bailian.mjs");
|
|
14
|
+
if(process.env.BL_BIN)return process.env.BL_BIN;
|
|
15
|
+
return createRequire(import.meta.url).resolve('bailian-cli');
|
|
31
16
|
}
|
|
32
17
|
|
|
33
18
|
/**
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {readFileSync,rmSync} from 'node:fs';
|
|
2
|
+
import {join,resolve} from 'node:path';
|
|
3
|
+
import {hash,readJson,writeJson,text} from './shared.mjs';
|
|
4
|
+
import {ownedPath,readObject,objectDirectory,mutateObject,listObjects,createObject} from './workspace.mjs';
|
|
5
|
+
import {validateImage} from './images.mjs';
|
|
6
|
+
import {sha256File} from './bl.mjs';
|
|
7
|
+
import {objectFile} from './workspace.mjs';
|
|
8
|
+
import {checkRun} from './executor.mjs';
|
|
9
|
+
import {tasks,runtimeRoot,locked} from './runtime.mjs';
|
|
10
|
+
import {jobTemplate} from './templates.mjs';
|
|
11
|
+
|
|
12
|
+
export async function editFromCandidate(settings,projectId,taskId,{runId,candidateId}={}){
|
|
13
|
+
return locked(runtimeRoot(settings),'control',async()=>{
|
|
14
|
+
const source=readObject(settings.paths.workspace,projectId,'task',taskId),candidate=candidates(settings,projectId,taskId,runId).find(row=>row.candidateId===candidateId&&!row.deleted&&row.accepted);
|
|
15
|
+
if(!candidate)throw new Error('候选未完成、已删除或完整性验证失败');
|
|
16
|
+
const config=jobTemplate(settings.paths.workspace,projectId,{kind:'edit'});config.items[0].source=candidate.path;
|
|
17
|
+
return createObject(settings,projectId,'task',{name:source.name+' · 编辑',config,source:{kind:'candidate',taskId,runId,candidateId,hash:sha256File(candidate.path)}});
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function runManifest(settings,projectId,taskId,runTaskId){
|
|
22
|
+
const run=tasks(runtimeRoot(settings)).find(t=>t.taskId===runTaskId);
|
|
23
|
+
if(!run||run.plan?.project!==projectId||run.plan?.job!==taskId||!run.manifestPath)throw new Error('执行批次不属于该任务');
|
|
24
|
+
const path=ownedPath(settings.paths.output,run.manifestPath),manifest=readJson(path);
|
|
25
|
+
if(manifest.planHash!==run.plan.planHash)throw new Error('批次计划身份不匹配');
|
|
26
|
+
return {run,manifest,path};
|
|
27
|
+
}
|
|
28
|
+
export function candidates(settings,projectId,taskId,runTaskId){
|
|
29
|
+
const {manifest,path}=runManifest(settings,projectId,taskId,runTaskId);
|
|
30
|
+
const tombstones=readObject(settings.paths.workspace,projectId,'task',taskId).deletedCandidates??[];
|
|
31
|
+
return checkRun(manifest,path).filter(c=>c.file).map(c=>({...c,runId:runTaskId,candidateId:hash({runId:runTaskId,assetId:c.itemId,variantId:c.variantId,file:c.file}),deleted:tombstones.some(d=>d.candidateId===hash({runId:runTaskId,assetId:c.itemId,variantId:c.variantId,file:c.file}))}));
|
|
32
|
+
}
|
|
33
|
+
export function finalizations(settings,projectId,taskId){return readObject(settings.paths.workspace,projectId,'task',taskId).finalizations??[];}
|
|
34
|
+
export async function finalize(settings,projectId,taskId,revision,{runId,candidateId,replace=false,actor}={}){
|
|
35
|
+
text(actor,'操作者');
|
|
36
|
+
// Mutation lock covers integrity checks, current selection and commit together.
|
|
37
|
+
return mutateObject(settings,projectId,'task',taskId,revision,async record=>{
|
|
38
|
+
const candidate=candidates(settings,projectId,taskId,runId).find(c=>c.candidateId===candidateId);
|
|
39
|
+
if(!candidate?.accepted||candidate.deleted)throw new Error('候选未完成、已删除或完整性验证失败');
|
|
40
|
+
const info=await validateImage(readFileSync(candidate.path));
|
|
41
|
+
const {run}=runManifest(settings,projectId,taskId,runId);
|
|
42
|
+
const item=run.plan.items.find(item=>item.itemId===candidate.itemId&&item.variantId===candidate.variantId);
|
|
43
|
+
const expected=(item?.execution??run.plan.execution).size.split('*').map(Number);
|
|
44
|
+
if(info.width!==expected[0]||info.height!==expected[1])throw new Error('候选尺寸与冻结计划不符');
|
|
45
|
+
const previous=(record.finalizations??[]).find(f=>f.assetId===candidate.itemId);
|
|
46
|
+
if(previous?.candidateId===candidateId)return record;
|
|
47
|
+
if(previous&&!replace)throw new Error('该资产已有定版,请明确确认替换');
|
|
48
|
+
record.finalizationHistory??=[];if(previous)record.finalizationHistory.push({...previous,replacedAt:new Date().toISOString()});
|
|
49
|
+
if(run.plan.sources.taskConfigHash!==hash(record.config))throw new Error('候选对应的需求配置已变化,请选择匹配当前配置的批次');
|
|
50
|
+
const selection={taskConfigHash:run.plan.sources.taskConfigHash,assetId:candidate.itemId,runId,candidateId,path:candidate.path,hash:sha256File(candidate.path),actor,createdAt:new Date().toISOString(),revision:record.revision+1};
|
|
51
|
+
record.finalizations=[...(record.finalizations??[]).filter(f=>f.assetId!==candidate.itemId),selection];
|
|
52
|
+
return record;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
export async function deleteCandidate(settings,projectId,taskId,revision,{runId,candidateId,confirm=false,actor}={}){
|
|
56
|
+
if(!confirm)throw new Error('删除候选需要明确确认影响');text(actor,'操作者');
|
|
57
|
+
return mutateObject(settings,projectId,'task',taskId,revision,record=>{
|
|
58
|
+
const candidate=candidates(settings,projectId,taskId,runId).find(c=>c.candidateId===candidateId);
|
|
59
|
+
if(!candidate)throw new Error('候选不存在');
|
|
60
|
+
let tombstone=record.deletedCandidates?.find(d=>d.candidateId===candidateId);
|
|
61
|
+
if(tombstone?.cleanup==='completed')return record;
|
|
62
|
+
if(!tombstone){
|
|
63
|
+
for(const other of listObjects(settings.paths.workspace,projectId,'task')){
|
|
64
|
+
if(other.status==='discarded')continue;
|
|
65
|
+
if((other.config.items??[]).some(i=>i.source&&resolve(settings.paths.workspace,i.source)===candidate.path))throw new Error('候选被编辑任务底图引用,不能删除');
|
|
66
|
+
}
|
|
67
|
+
for(const run of tasks(runtimeRoot(settings)))if((run.plan?.refs??[]).some(r=>resolve(r.path)===candidate.path))throw new Error('候选被历史计划引用,不能删除');
|
|
68
|
+
const selected=(record.finalizations??[]).find(f=>f.candidateId===candidateId);
|
|
69
|
+
record.finalizationHistory??=[];if(selected)record.finalizationHistory.push({...selected,deletedAt:new Date().toISOString()});
|
|
70
|
+
record.finalizations=(record.finalizations??[]).filter(f=>f.candidateId!==candidateId);
|
|
71
|
+
record.deletedCandidates??=[];
|
|
72
|
+
tombstone={candidateId,runId,path:candidate.path,actor,deletedAt:new Date().toISOString(),cleanup:'pending'};
|
|
73
|
+
record.deletedCandidates.push(tombstone);
|
|
74
|
+
}
|
|
75
|
+
// Write-ahead tombstone and physical cleanup share the mutation guard. A crash
|
|
76
|
+
// leaves a resumable pending record; it cannot expose a deleted selection.
|
|
77
|
+
writeJson(objectFile(settings.paths.workspace,projectId,'task',taskId),{...record,revision:record.revision+1,updatedAt:new Date().toISOString()});
|
|
78
|
+
try{ownedPath(settings.paths.output,tombstone.path);rmSync(tombstone.path,{force:true});tombstone.cleanup='completed';delete tombstone.error;}
|
|
79
|
+
catch(error){tombstone.cleanup='failed';tombstone.error=error.message;}
|
|
80
|
+
return record;
|
|
81
|
+
});
|
|
82
|
+
}
|
package/lib/comfyui.mjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {serviceRoot} from './service.mjs';
|
|
2
|
+
import {readFileSync,writeFileSync} from 'node:fs';
|
|
3
|
+
import {join,basename} from 'node:path';
|
|
4
|
+
import {randomUUID} from 'node:crypto';
|
|
5
|
+
import {buildComfyRequest} from './backends.mjs';
|
|
6
|
+
import {http,sleep,locked} from './runtime.mjs';
|
|
7
|
+
import {workflowError} from './diagnostics.mjs';
|
|
8
|
+
|
|
9
|
+
export async function cancelPrompt(endpoint,promptId) {
|
|
10
|
+
if(typeof promptId!=='string'||!promptId)throw new Error('缺少可核验的后端任务身份');
|
|
11
|
+
await http(endpoint,'/queue',{method:'POST',body:{delete:[promptId]}});
|
|
12
|
+
const queue=await http(endpoint,'/queue');
|
|
13
|
+
if(queue.queue_running?.some(row=>row[1]===promptId)) {
|
|
14
|
+
if(queue.queue_running.some(row=>row[1]!==promptId))throw new Error('其他任务正在运行,拒绝全局中断');
|
|
15
|
+
// ComfyUI interrupt supports a prompt_id guard, preventing an unrelated next task from being interrupted.
|
|
16
|
+
await http(endpoint,'/interrupt',{method:'POST',body:{prompt_id:promptId}});
|
|
17
|
+
}
|
|
18
|
+
const deadline=Date.now()+30000;
|
|
19
|
+
while(true){const q=await http(endpoint,'/queue');const active=[...(q.queue_running??[]),...(q.queue_pending??[])];if(!active.some(row=>row[1]===promptId))return;if(Date.now()>deadline)throw new Error('ComfyUI 任务停止超时');await sleep(200);}
|
|
20
|
+
}
|
|
21
|
+
export async function generateComfy(plan,item,{endpoint,root,outputDir,cancelled=()=>false,onPrompt=()=>{}}) {
|
|
22
|
+
return locked(serviceRoot(),'queue-'+new URL(endpoint).port,async()=>{
|
|
23
|
+
const b=plan.backend,e=item.execution??plan.execution,files=[];
|
|
24
|
+
if(item.imageArgs.length>1)throw new Error('当前编辑适配器只接受一张原图');
|
|
25
|
+
let image;
|
|
26
|
+
if(plan.module==='edit') {
|
|
27
|
+
const path=item.imageArgs[0],form=new FormData();form.append('image',new Blob([readFileSync(path)]),randomUUID()+'-'+basename(path));form.append('type','input');
|
|
28
|
+
const response=await fetch(new URL('/upload/image',endpoint),{method:'POST',body:form,signal:AbortSignal.timeout(30000)});
|
|
29
|
+
if(!response.ok)throw new Error('上传编辑原图失败');const upload=await response.json();image=upload.subfolder?upload.subfolder+'/'+upload.name:upload.name;
|
|
30
|
+
}
|
|
31
|
+
for(let index=0;index<e.perValue;index++) {
|
|
32
|
+
if(cancelled())throw new Error('任务已取消');
|
|
33
|
+
const request=buildComfyRequest(plan,item,{index,image});
|
|
34
|
+
let result;
|
|
35
|
+
try{result=await http(endpoint,'/prompt',{method:'POST',body:{prompt:request.prompt,client_id:randomUUID()}});}
|
|
36
|
+
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});error.unsettled=true;throw error;}
|
|
37
|
+
if(!result.prompt_id||Object.keys(result.node_errors??{}).length){
|
|
38
|
+
const error=workflowError('ComfyUI 拒绝工作流',b,{stage:'submission',promptId:result.prompt_id,nodeErrors:result.node_errors});
|
|
39
|
+
if(result.prompt_id){onPrompt(result.prompt_id);try{await cancelPrompt(endpoint,result.prompt_id);}catch{error.unsettled=true;}}
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
const promptId=result.prompt_id;onPrompt(promptId);
|
|
43
|
+
const deadline=Date.now()+(b.options?.timeoutSeconds??1800)*1000;
|
|
44
|
+
while(true) {
|
|
45
|
+
if(cancelled()){try{await cancelPrompt(endpoint,promptId);}catch(error){error.unsettled=true;throw error;}throw new Error('任务已取消');}
|
|
46
|
+
const history=(await http(endpoint,'/history/'+encodeURIComponent(promptId)))[promptId];
|
|
47
|
+
if(history?.status?.completed) {
|
|
48
|
+
if(history.status.status_str==='error')throw workflowError('ComfyUI 执行失败',b,{promptId,messages:history.status.messages});
|
|
49
|
+
const images=Object.values(history.outputs??{}).flatMap(value=>value.images??[]).filter(x=>x.type==='output');
|
|
50
|
+
if(images.length!==1)throw new Error('每次串行采样应输出恰好一张图片');
|
|
51
|
+
const params=new URLSearchParams(images[0]);const response=await fetch(new URL('/view?'+params,endpoint),{signal:AbortSignal.timeout(30000)});
|
|
52
|
+
if(!response.ok)throw new Error('获取生成图片失败');
|
|
53
|
+
const file=`${item.itemId}--${item.variantId}${index?'--'+(index+1):''}.png`;
|
|
54
|
+
writeFileSync(join(outputDir,file),Buffer.from(await response.arrayBuffer()),{flag:'wx'});files.push(file);break;
|
|
55
|
+
}
|
|
56
|
+
if(history?.status?.status_str==='error')throw workflowError('ComfyUI 执行失败',b,{promptId,messages:history.status.messages});
|
|
57
|
+
if(Date.now()>deadline){try{await cancelPrompt(endpoint,promptId);}catch(error){error.unsettled=true;throw error;}throw new Error('生图超时,已停止任务');}
|
|
58
|
+
await sleep(b.options?.pollMilliseconds??500);
|
|
59
|
+
}
|
|
60
|
+
onPrompt(null);
|
|
61
|
+
}
|
|
62
|
+
return {files};
|
|
63
|
+
});
|
|
64
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** Stable image extension contracts. Runtime validation lives in model-registry. */
|
|
2
|
+
export type ImageMode = "generate" | "edit";
|
|
3
|
+
export type ImageParameterRule = {
|
|
4
|
+
type: "boolean" | "integer" | "string";
|
|
5
|
+
wire: string;
|
|
6
|
+
cli?: string;
|
|
7
|
+
default?: boolean | number | string;
|
|
8
|
+
min?: number;
|
|
9
|
+
max?: number;
|
|
10
|
+
enum?: (boolean | number | string)[];
|
|
11
|
+
};
|
|
12
|
+
export interface ImageRequest {
|
|
13
|
+
schema: "art/image-request-v1";
|
|
14
|
+
api: string;
|
|
15
|
+
adapter: string;
|
|
16
|
+
contractHash: string;
|
|
17
|
+
module: ImageMode;
|
|
18
|
+
model: string;
|
|
19
|
+
prompt: string;
|
|
20
|
+
negativePrompt: string;
|
|
21
|
+
images: string[];
|
|
22
|
+
transport: {
|
|
23
|
+
adapter: string;
|
|
24
|
+
version: number;
|
|
25
|
+
mode: string;
|
|
26
|
+
path?: string;
|
|
27
|
+
timeoutMilliseconds?: number;
|
|
28
|
+
};
|
|
29
|
+
inputPolicy?: {
|
|
30
|
+
maxBytes: number;
|
|
31
|
+
formats: string[];
|
|
32
|
+
minAxis?: number;
|
|
33
|
+
maxAxis?: number;
|
|
34
|
+
minRatio?: number;
|
|
35
|
+
maxRatio?: number;
|
|
36
|
+
alpha?: boolean;
|
|
37
|
+
};
|
|
38
|
+
parameters: Record<string, boolean | number | string>;
|
|
39
|
+
size: string;
|
|
40
|
+
seed: number;
|
|
41
|
+
count: number;
|
|
42
|
+
adjustments: { parameter: string; action: string; message: string }[];
|
|
43
|
+
}
|
|
44
|
+
export interface RemoteReceipt {
|
|
45
|
+
schema: "art/remote-receipt-v1";
|
|
46
|
+
adapter: string;
|
|
47
|
+
adapterVersion: number;
|
|
48
|
+
requestHash: string;
|
|
49
|
+
baseUrl: string;
|
|
50
|
+
submittedAt: string;
|
|
51
|
+
state: "dispatching" | "accepted" | "completed" | "failed" | "cancelled";
|
|
52
|
+
taskId?: string;
|
|
53
|
+
urls?: string[];
|
|
54
|
+
message?: string;
|
|
55
|
+
}
|
|
56
|
+
export type RemoteTransition = Pick<RemoteReceipt, "state"> &
|
|
57
|
+
Partial<Pick<RemoteReceipt, "taskId" | "urls" | "message">>;
|
|
58
|
+
export interface ImageAdapter {
|
|
59
|
+
id: string;
|
|
60
|
+
label?: string;
|
|
61
|
+
version: number;
|
|
62
|
+
cancellation: "unsupported" | "supported";
|
|
63
|
+
prepare(request: ImageRequest, context: object): Promise<object>;
|
|
64
|
+
submit(request: ImageRequest, context: object): Promise<RemoteTransition>;
|
|
65
|
+
query(receipt: RemoteReceipt, context: object): Promise<RemoteTransition>;
|
|
66
|
+
preview?(request: ImageRequest): object;
|
|
67
|
+
}
|