dsh-mo-image 1.0.2 → 1.0.3
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/CHANGELOG.md +5 -0
- package/LICENSE +34 -0
- package/README.md +11 -9
- package/lib/adapters.js +50 -26
- package/lib/client.js +78 -199
- package/lib/comfy-workflow.js +265 -58
- package/lib/config-store.js +14 -2
- package/lib/image-jobs.js +2 -2
- package/lib/image-queue.js +1 -1
- package/lib/image-worker.js +1 -1
- package/lib/index.js +14 -4
- package/lib/prompt-profiles.js +8 -0
- package/lib/status.js +31 -8
- package/lib/update-service.js +11 -13
- package/lib/update.js +17 -34
- package/locale/messages.csv +4 -9
- package/package.json +1 -2
- package/skills/mo-image-editing/SKILL.md +2 -1
- package/THIRD-PARTY-NOTICES.md +0 -32
package/lib/comfy-workflow.js
CHANGED
|
@@ -2,17 +2,21 @@
|
|
|
2
2
|
* ComfyUI 工作流解析:把用户贴进来的任何一种工作流,变成「参数该写进哪个节点」的映射。
|
|
3
3
|
*
|
|
4
4
|
* 纯函数,不碰网络和文件:/object_info 由调用方取好传进来。
|
|
5
|
-
*
|
|
5
|
+
* 提示词、尺寸与质量由解析器绑定。其它参数保留工作流自己的配置入口。
|
|
6
6
|
*
|
|
7
7
|
* 定位策略:先找到主采样器,再把**它整条上游闭包**摊开,按角色(positive/negative)、
|
|
8
8
|
* 名字(seed/ckpt_name)和类型去匹配。不逐输入查表——FLUX.2 的 SamplerCustomAdvanced
|
|
9
9
|
* 自己没有 positive/seed/model,这些散落在 CFGGuider 和 RandomNoise 上。
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
export const MAPPING_KEYS = ['prompt', 'negative_prompt', 'width', 'height', 'seed', 'model', 'image'];
|
|
12
|
+
export const MAPPING_KEYS = ['prompt', 'negative_prompt', 'width', 'height', 'quality', 'seed', 'model', 'image'];
|
|
13
|
+
export const WORKFLOW_REFERENCE_LIMIT = 16;
|
|
13
14
|
|
|
14
15
|
/** 输入名优先级:先认这些名字,再退而求其次找其它字符串输入。 */
|
|
15
|
-
const TEXTISH = /^(text|
|
|
16
|
+
const TEXTISH = /^(text|string|value)$/i;
|
|
17
|
+
const POSITIVE_PROMPT = /^(prompt|positive_prompt|positivePrompt)$/i;
|
|
18
|
+
const NEGATIVE_PROMPT = /^(negative_prompt|negativePrompt)$/i;
|
|
19
|
+
const QUALITYISH = /^(quality|image_quality|quality_preset|rendering_quality)$/i;
|
|
16
20
|
const SEEDISH = /^(seed|noise_seed)$/i;
|
|
17
21
|
const MODELISH = /^(ckpt_name|unet_name|model_name)$/i;
|
|
18
22
|
/** UI 格式里,种子后面那个「fixed/randomize」下拉不是节点输入,但占了一个 widgets_values 位。 */
|
|
@@ -20,6 +24,32 @@ const PHANTOM_WIDGETS = new Set(['fixed', 'increment', 'decrement', 'randomize']
|
|
|
20
24
|
|
|
21
25
|
const SUMMARY_MAX = 80;
|
|
22
26
|
|
|
27
|
+
export function workflowPlaceholders(text) {
|
|
28
|
+
return new Set(Array.from(text.matchAll(/\{\{\s*([A-Za-z_][A-Za-z0-9_]*)\s*\}\}/g), (match) =>
|
|
29
|
+
match[1] === 'negativePrompt' ? 'negative_prompt' : match[1]));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function placeholdersIn(value) {
|
|
33
|
+
const found = new Set();
|
|
34
|
+
const visit = (part) => {
|
|
35
|
+
if (typeof part === 'string' && part.includes('{{')) {
|
|
36
|
+
for (const name of workflowPlaceholders(part)) found.add(name);
|
|
37
|
+
} else if (part && typeof part === 'object') {
|
|
38
|
+
for (const item of Object.values(part)) visit(item);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
visit(value);
|
|
42
|
+
return found;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function workflowInputPlaceholders(graph) {
|
|
46
|
+
const found = new Set();
|
|
47
|
+
for (const node of Object.values(graph)) {
|
|
48
|
+
for (const name of placeholdersIn(node.inputs)) found.add(name);
|
|
49
|
+
}
|
|
50
|
+
return found;
|
|
51
|
+
}
|
|
52
|
+
|
|
23
53
|
/** 只在字符串外剥离 // 行注释,避免把提示词里的网址吃掉。 */
|
|
24
54
|
function stripLineComments(text) {
|
|
25
55
|
let out = '';
|
|
@@ -125,15 +155,16 @@ export function uiToApi(ui, objectInfo) {
|
|
|
125
155
|
const widgets = Array.isArray(node.widgets_values) ? [...node.widgets_values] : [];
|
|
126
156
|
let cursor = 0;
|
|
127
157
|
for (const name of [...required, ...optional]) {
|
|
128
|
-
const spec = (schema.input.required && schema.input.required[name]) || (schema.input.optional && schema.input.optional[name]) || [];
|
|
129
|
-
if (spec[1] && typeof spec[1] === 'object' && spec[1].forceInput) continue;
|
|
130
158
|
if (linked.has(name)) { inputs[name] = linked.get(name); continue; }
|
|
159
|
+
const spec = (schema.input.required && schema.input.required[name]) || (schema.input.optional && schema.input.optional[name]) || [];
|
|
160
|
+
if (spec[1]?.forceInput) continue;
|
|
131
161
|
if (cursor >= widgets.length) continue;
|
|
132
162
|
inputs[name] = widgets[cursor];
|
|
133
163
|
cursor += 1;
|
|
134
164
|
if (SEEDISH.test(name) && typeof widgets[cursor] === 'string' && PHANTOM_WIDGETS.has(widgets[cursor])) cursor += 1;
|
|
135
165
|
}
|
|
136
166
|
graph[id] = { class_type: type, inputs };
|
|
167
|
+
if (typeof node.title === 'string' && node.title.trim()) graph[id]._meta = { title: node.title };
|
|
137
168
|
}
|
|
138
169
|
for (const type of missing) warnings.push('工作流里的节点 ' + type + ' 没有安装,涉及它的连线会被忽略');
|
|
139
170
|
return { graph, warnings, missing };
|
|
@@ -162,8 +193,9 @@ function makeCandidate(id, node, input, value) {
|
|
|
162
193
|
|
|
163
194
|
function findOutputNodes(graph, objectInfo) {
|
|
164
195
|
return Object.keys(graph).filter((id) => {
|
|
165
|
-
const
|
|
166
|
-
|
|
196
|
+
const type = graph[id].class_type;
|
|
197
|
+
const schema = objectInfo[type];
|
|
198
|
+
return schema ? Boolean(schema.output_node) : type === 'SaveImage' || type === 'PreviewImage';
|
|
167
199
|
}).sort();
|
|
168
200
|
}
|
|
169
201
|
|
|
@@ -201,10 +233,10 @@ function findSamplers(graph, objectInfo, outputIds) {
|
|
|
201
233
|
return found.map((item) => item.id);
|
|
202
234
|
}
|
|
203
235
|
|
|
204
|
-
/**
|
|
205
|
-
function upstreamClosure(graph,
|
|
206
|
-
const depth = new Map([
|
|
207
|
-
let frontier =
|
|
236
|
+
/** 从各起点向上游查找,记录到最近起点的距离。 */
|
|
237
|
+
function upstreamClosure(graph, roots) {
|
|
238
|
+
const depth = new Map(roots.map((id) => [id, 0]));
|
|
239
|
+
let frontier = roots;
|
|
208
240
|
while (frontier.length) {
|
|
209
241
|
const next = [];
|
|
210
242
|
for (const id of frontier) {
|
|
@@ -224,24 +256,24 @@ function upstreamClosure(graph, samplerId) {
|
|
|
224
256
|
}
|
|
225
257
|
|
|
226
258
|
/** 在上游闭包里按角色找输入(positive/negative),最近的优先。 */
|
|
227
|
-
function findRoleLink(graph,
|
|
228
|
-
const sampler = graph[samplerId];
|
|
229
|
-
if (sampler && isLink(sampler.inputs && sampler.inputs[role])) return sampler.inputs[role];
|
|
230
|
-
const ids = [...closure.keys()].filter((id) => id !== samplerId)
|
|
231
|
-
.sort((a, b) => closure.get(a) - closure.get(b) || a.localeCompare(b));
|
|
259
|
+
function findRoleLink(graph, ids, role, matcher) {
|
|
232
260
|
for (const id of ids) {
|
|
233
|
-
const hit = inputEntries(graph[id]).find(([name, value]) => name === role && isLink(value));
|
|
261
|
+
const hit = inputEntries(graph[id]).find(([name, value]) => (name === role || matcher.test(name)) && isLink(value));
|
|
234
262
|
if (hit) return hit[1];
|
|
235
263
|
}
|
|
236
264
|
return null;
|
|
237
265
|
}
|
|
238
266
|
|
|
239
267
|
/** 从某个输入出发,按优先级向上找字面量字符串输入;沿途收集候选。 */
|
|
240
|
-
function walkText(graph, startLink, pool) {
|
|
268
|
+
function walkText(graph, startLink, pool, role, matcher) {
|
|
241
269
|
const visited = new Set();
|
|
242
270
|
let primary = null;
|
|
243
271
|
|
|
244
|
-
const
|
|
272
|
+
const matches = (name) => matcher.test(name) || name === role || TEXTISH.test(name);
|
|
273
|
+
const excluded = (name) => name === 'system_prompt'
|
|
274
|
+
|| (role === 'positive' ? name === 'negative' || NEGATIVE_PROMPT.test(name)
|
|
275
|
+
: name === 'positive' || POSITIVE_PROMPT.test(name));
|
|
276
|
+
const rank = (name) => (matches(name) ? 0 : excluded(name) ? 2 : 1);
|
|
245
277
|
const visit = (link) => {
|
|
246
278
|
if (!isLink(link)) return;
|
|
247
279
|
const id = String(link[0]);
|
|
@@ -254,7 +286,7 @@ function walkText(graph, startLink, pool) {
|
|
|
254
286
|
// 空字符串也是字面量:导出和模板里的文本框默认就是空的,跳过它等于让整条角色参数消失。
|
|
255
287
|
if (typeof value !== 'string') continue;
|
|
256
288
|
pool.push(makeCandidate(id, node, name, value));
|
|
257
|
-
if (!primary &&
|
|
289
|
+
if (!primary && matches(name)) primary = { node: id, input: name };
|
|
258
290
|
}
|
|
259
291
|
for (const [name, value] of ordered) {
|
|
260
292
|
if (!isLink(value)) continue;
|
|
@@ -263,23 +295,194 @@ function walkText(graph, startLink, pool) {
|
|
|
263
295
|
}
|
|
264
296
|
};
|
|
265
297
|
visit(startLink);
|
|
266
|
-
|
|
298
|
+
const fallback = pool.find((item) => !excluded(item.input));
|
|
299
|
+
if (!primary && fallback) primary = { node: fallback.node, input: fallback.input };
|
|
267
300
|
return primary;
|
|
268
301
|
}
|
|
269
302
|
|
|
303
|
+
function upstreamIds(graph, roots) {
|
|
304
|
+
const closure = upstreamClosure(graph, roots);
|
|
305
|
+
return [...closure.keys()].sort((a, b) => closure.get(a) - closure.get(b) || a.localeCompare(b));
|
|
306
|
+
}
|
|
307
|
+
|
|
270
308
|
/** 在上游闭包里按名字找某个输入,最近的优先。 */
|
|
271
|
-
function findInClosure(graph,
|
|
272
|
-
const ids = [...closure.keys()].sort((a, b) => closure.get(a) - closure.get(b) || a.localeCompare(b));
|
|
309
|
+
function findInClosure(graph, ids, matcher, accepts = () => true) {
|
|
273
310
|
for (const id of ids) {
|
|
274
311
|
for (const [name, value] of inputEntries(graph[id]).sort(([a], [b]) => a.localeCompare(b))) {
|
|
275
|
-
if (
|
|
276
|
-
if (literalOnly && isLink(value)) continue;
|
|
277
|
-
return { node: id, input: name };
|
|
312
|
+
if (matcher.test(name) && accepts(value)) return { node: id, input: name };
|
|
278
313
|
}
|
|
279
314
|
}
|
|
280
315
|
return null;
|
|
281
316
|
}
|
|
282
317
|
|
|
318
|
+
function findQuality(graph, ids, objectInfo) {
|
|
319
|
+
for (const id of ids) {
|
|
320
|
+
const node = graph[id];
|
|
321
|
+
const schema = objectInfo[node.class_type];
|
|
322
|
+
if (!schema || schema.output_node) continue;
|
|
323
|
+
for (const [name, value] of inputEntries(node)) {
|
|
324
|
+
if (!QUALITYISH.test(name) || typeof value !== 'string') continue;
|
|
325
|
+
const [type, metadata] = schema.input?.required?.[name] || schema.input?.optional?.[name] || [];
|
|
326
|
+
const values = Array.isArray(type) ? type : type === 'COMBO' && metadata?.options;
|
|
327
|
+
if (!Array.isArray(values)) continue;
|
|
328
|
+
const options = new Map();
|
|
329
|
+
for (const label of values) {
|
|
330
|
+
if (typeof label !== 'string') continue;
|
|
331
|
+
const key = label.trim().toLowerCase().replace(/\s+/g, '-');
|
|
332
|
+
if (!/^[a-z0-9][a-z0-9._-]{0,31}$/.test(key) || key === 'off' || key === 'auto') continue;
|
|
333
|
+
if (!options.has(key) || label === value) options.set(key, label);
|
|
334
|
+
}
|
|
335
|
+
if (!options.size) continue;
|
|
336
|
+
return {
|
|
337
|
+
target: { node: id, input: name },
|
|
338
|
+
spec: {
|
|
339
|
+
supported: true,
|
|
340
|
+
options: [...options].map(([key, label]) => ({ value: key, label, default: label === value })),
|
|
341
|
+
map: Object.fromEntries(options)
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** 定位生成链路上的输入,跳过与输出无关的节点。 */
|
|
350
|
+
function workflowBindings(graph, objectInfo, outputIds) {
|
|
351
|
+
const samplers = findSamplers(graph, objectInfo, outputIds);
|
|
352
|
+
const samplerId = samplers[0];
|
|
353
|
+
const roots = samplerId ? [samplerId] : outputIds;
|
|
354
|
+
const ids = upstreamIds(graph, roots);
|
|
355
|
+
const promptPool = [];
|
|
356
|
+
const negativePool = [];
|
|
357
|
+
const findPrompt = (role, matcher, pool) => (
|
|
358
|
+
walkText(graph, findRoleLink(graph, ids, role, matcher), pool, role, matcher)
|
|
359
|
+
|| findInClosure(graph, ids, matcher, (value) => typeof value === 'string')
|
|
360
|
+
);
|
|
361
|
+
const prompt = findPrompt('positive', POSITIVE_PROMPT, promptPool);
|
|
362
|
+
const negative = findPrompt('negative', NEGATIVE_PROMPT, negativePool);
|
|
363
|
+
const samePrompt = prompt && negative && prompt.node === negative.node && prompt.input === negative.input;
|
|
364
|
+
const latent = graph[samplerId]?.inputs?.latent_image;
|
|
365
|
+
const sizeIds = isLink(latent) ? [...new Set([...upstreamIds(graph, [latent[0]]), ...ids])] : ids;
|
|
366
|
+
const quality = findQuality(graph, ids, objectInfo);
|
|
367
|
+
const mapping = {
|
|
368
|
+
prompt, negative_prompt: samePrompt ? null : negative,
|
|
369
|
+
width: findInClosure(graph, sizeIds, /^width$/, isDimensionInput),
|
|
370
|
+
height: findInClosure(graph, sizeIds, /^height$/, isDimensionInput),
|
|
371
|
+
seed: findInClosure(graph, ids, SEEDISH),
|
|
372
|
+
model: findInClosure(graph, ids, MODELISH, (value) => !isLink(value)),
|
|
373
|
+
image: null
|
|
374
|
+
};
|
|
375
|
+
if (quality) mapping.quality = quality.target;
|
|
376
|
+
return { mapping, quality: quality?.spec, samplers, promptPool, negativePool };
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function isDimensionInput(value) {
|
|
380
|
+
return Number.isFinite(value) || isLink(value)
|
|
381
|
+
|| (typeof value === 'string' && /^\{\{\s*(width|height)\s*\}\}$/.test(value));
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export function findWorkflowPrompt(graph, objectInfo = {}) {
|
|
385
|
+
return workflowBindings(graph, objectInfo, findOutputNodes(graph, objectInfo)).mapping.prompt;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** 只绑定输出链路上的图像上传控件。文件名来自 ComfyUI 的 input 目录。 */
|
|
389
|
+
function referenceInputs(graph, objectInfo, outputIds) {
|
|
390
|
+
const sources = new Set();
|
|
391
|
+
for (const id of upstreamIds(graph, outputIds)) {
|
|
392
|
+
for (const [, value] of inputEntries(graph[id])) {
|
|
393
|
+
if (!isLink(value)) continue;
|
|
394
|
+
const source = graph[value[0]];
|
|
395
|
+
const types = outputTypes(source || {}, objectInfo);
|
|
396
|
+
const imageOutput = types.length ? types[value[1]] === 'IMAGE'
|
|
397
|
+
: source?.class_type === 'LoadImage' && value[1] === 0;
|
|
398
|
+
if (imageOutput) sources.add(value[0]);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
const candidates = [];
|
|
402
|
+
for (const id of sources) {
|
|
403
|
+
const node = graph[id];
|
|
404
|
+
const signature = objectInfo[node.class_type]?.input;
|
|
405
|
+
for (const [name, value] of inputEntries(node)) {
|
|
406
|
+
if (typeof value !== 'string' || /mask/i.test(name)) continue;
|
|
407
|
+
const metadata = (signature?.required?.[name] || signature?.optional?.[name])?.[1];
|
|
408
|
+
const loader = node.class_type === 'LoadImage' && name === 'image';
|
|
409
|
+
if (!loader && !(metadata?.image_upload === true && (!metadata.image_folder || metadata.image_folder === 'input'))) continue;
|
|
410
|
+
candidates.push(makeCandidate(id, node, name, value));
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return candidates;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
export function workflowReferenceTarget(graph, saved, objectInfo = {}) {
|
|
417
|
+
const outputIds = findOutputNodes(graph, objectInfo);
|
|
418
|
+
const images = referenceInputs(graph, objectInfo, outputIds);
|
|
419
|
+
const node = saved && graph[saved.node];
|
|
420
|
+
if (typeof node?.inputs?.[saved.input] === 'string') {
|
|
421
|
+
if (!outputIds.length) return saved; // API 导出可能使用客户端无法识别的自定义输出节点。
|
|
422
|
+
if (images.some((target) => target.node === saved.node && target.input === saved.input)) return saved;
|
|
423
|
+
if (node.class_type !== 'LoadImage' && upstreamClosure(graph, outputIds).has(saved.node)) return saved;
|
|
424
|
+
}
|
|
425
|
+
return uniqueReference(images);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
export function isWorkflowImagePlaceholder(name) {
|
|
429
|
+
return name === 'image' || name.startsWith('image_');
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** Numbered placeholders opt inputs into editing. Other image loaders keep their own assets. */
|
|
433
|
+
export function workflowImageSlots(graph) {
|
|
434
|
+
const slots = new Map();
|
|
435
|
+
for (const [id, node] of Object.entries(graph)) {
|
|
436
|
+
const title = typeof node._meta?.title === 'string' ? node._meta.title.replace(/\s+/g, ' ').trim() : '';
|
|
437
|
+
for (const [input, value] of inputEntries(node)) {
|
|
438
|
+
for (const name of placeholdersIn(value)) {
|
|
439
|
+
if (!isWorkflowImagePlaceholder(name)) continue;
|
|
440
|
+
const index = name === 'image' ? 1 : Number(name.slice(6));
|
|
441
|
+
if (!Number.isInteger(index) || index < 1 || index > WORKFLOW_REFERENCE_LIMIT
|
|
442
|
+
|| (name !== 'image' && name !== 'image_' + index)) {
|
|
443
|
+
throw new Error('参考图占位符必须从 {{image_1}} 到 {{image_' + WORKFLOW_REFERENCE_LIMIT + '}}:' + name);
|
|
444
|
+
}
|
|
445
|
+
const slot = slots.get(index) || { index, keys: new Set(), targets: [], purpose: '' };
|
|
446
|
+
slot.keys.add(name);
|
|
447
|
+
slot.targets.push({ node: id, input });
|
|
448
|
+
if (!slot.purpose && title && title !== node.class_type) slot.purpose = title.slice(0, 96);
|
|
449
|
+
slots.set(index, slot);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
const list = Array.from(slots.values()).sort((a, b) => a.index - b.index);
|
|
454
|
+
if (list.some((slot, index) => slot.index !== index + 1)) {
|
|
455
|
+
throw new Error('参考图占位符须从 {{image_1}} 开始连续编号;{{image}} 等同于 {{image_1}}。');
|
|
456
|
+
}
|
|
457
|
+
if (!list.length || list.every((slot) => slot.purpose)) return list;
|
|
458
|
+
const uses = new Map();
|
|
459
|
+
for (const node of Object.values(graph)) {
|
|
460
|
+
for (const [input, value] of inputEntries(node)) {
|
|
461
|
+
if (!isLink(value) || /^(image|images|pixels|input|value|source)$/i.test(input)) continue;
|
|
462
|
+
const source = value[0];
|
|
463
|
+
if (!uses.has(source)) uses.set(source, input);
|
|
464
|
+
else if (uses.get(source) !== input) uses.set(source, null);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
for (const slot of list) {
|
|
468
|
+
const target = slot.targets[0];
|
|
469
|
+
const purpose = uses.get(target.node) || target.input;
|
|
470
|
+
if (!slot.purpose && !/^(widgets|image|images|pixels|input|value|source)$/i.test(purpose)) {
|
|
471
|
+
slot.purpose = purpose.slice(0, 96);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return list;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export function assertComfyReferenceCount(actual, expected) {
|
|
478
|
+
if (actual !== expected) throw new Error('当前 ComfyUI 工作流需要 ' + expected
|
|
479
|
+
+ ' 张参考图,实际收到 ' + actual + ' 张。请按配置中的参考图入口顺序传入 images。');
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function uniqueReference(images) {
|
|
483
|
+
return images.length === 1 ? { node: images[0].node, input: images[0].input } : null;
|
|
484
|
+
}
|
|
485
|
+
|
|
283
486
|
/**
|
|
284
487
|
* 分析整张图。返回的 mapping 里每一项要么是 {node, input},要么是 null。
|
|
285
488
|
* @throws 图里没有输出节点时抛错——ComfyUI 会直接拒绝执行这种图。
|
|
@@ -289,46 +492,28 @@ export function analyzeWorkflow(graph, objectInfo) {
|
|
|
289
492
|
const outputIds = findOutputNodes(graph, objectInfo);
|
|
290
493
|
if (!outputIds.length) throw new Error('这个工作流没有输出节点,ComfyUI 不会执行它。');
|
|
291
494
|
|
|
292
|
-
const samplers =
|
|
293
|
-
const
|
|
294
|
-
if (!
|
|
295
|
-
|
|
296
|
-
const mapping = { prompt: null, negative_prompt: null, width: null, height: null, seed: null, model: null, image: null };
|
|
297
|
-
const promptPool = [];
|
|
298
|
-
const negativePool = [];
|
|
299
|
-
|
|
300
|
-
if (samplerId) {
|
|
301
|
-
const closure = upstreamClosure(graph, samplerId);
|
|
302
|
-
mapping.prompt = walkText(graph, findRoleLink(graph, closure, samplerId, 'positive'), promptPool);
|
|
303
|
-
mapping.negative_prompt = walkText(graph, findRoleLink(graph, closure, samplerId, 'negative'), negativePool);
|
|
304
|
-
if (mapping.prompt && mapping.negative_prompt
|
|
305
|
-
&& mapping.prompt.node === mapping.negative_prompt.node
|
|
306
|
-
&& mapping.prompt.input === mapping.negative_prompt.input) {
|
|
307
|
-
mapping.negative_prompt = null;
|
|
308
|
-
}
|
|
309
|
-
mapping.seed = findInClosure(graph, closure, samplerId, SEEDISH);
|
|
310
|
-
mapping.model = findInClosure(graph, closure, samplerId, MODELISH, { literalOnly: true });
|
|
311
|
-
mapping.width = findInClosure(graph, closure, samplerId, /^width$/);
|
|
312
|
-
mapping.height = findInClosure(graph, closure, samplerId, /^height$/);
|
|
495
|
+
const { mapping, quality, samplers, promptPool, negativePool } = workflowBindings(graph, objectInfo, outputIds);
|
|
496
|
+
const placeholders = workflowInputPlaceholders(graph);
|
|
497
|
+
if (!mapping.prompt && !placeholders.has('prompt')) {
|
|
498
|
+
warnings.push('未识别到提示词输入。请将工作流的提示词输入设为 {{prompt}} 后重新解析。');
|
|
313
499
|
}
|
|
314
500
|
|
|
315
|
-
const
|
|
316
|
-
|
|
501
|
+
const images = referenceInputs(graph, objectInfo, outputIds);
|
|
502
|
+
const slots = [...placeholders].some(isWorkflowImagePlaceholder) ? workflowImageSlots(graph) : [];
|
|
503
|
+
mapping.image = uniqueReference(images);
|
|
504
|
+
const ambiguousImage = images.length > 1 && !slots.length;
|
|
317
505
|
|
|
318
506
|
const candidates = {
|
|
319
507
|
prompt: promptPool,
|
|
320
508
|
negative_prompt: negativePool,
|
|
321
|
-
width: collectInputs(graph, /^width$/),
|
|
322
|
-
height: collectInputs(graph, /^height$/),
|
|
323
509
|
seed: collectInputs(graph, SEEDISH),
|
|
324
510
|
model: collectInputs(graph, MODELISH),
|
|
325
|
-
image:
|
|
326
|
-
.map((id) => makeCandidate(id, graph[id], 'image', graph[id].inputs.image)),
|
|
511
|
+
image: images,
|
|
327
512
|
sampler: samplers.map((id) => makeCandidate(id, graph[id], 'latent_image', id))
|
|
328
513
|
};
|
|
329
514
|
|
|
330
515
|
return {
|
|
331
|
-
mapping, candidates, warnings, samplerId, outputIds,
|
|
516
|
+
mapping, quality, candidates, warnings, ambiguousImage, samplerId: samplers[0] || null, outputIds,
|
|
332
517
|
params: [
|
|
333
518
|
...collectRoleParams(graph, mapping, objectInfo).map((param) => (
|
|
334
519
|
Object.assign({}, param, { candidates: paramCandidates(graph, param.name) })
|
|
@@ -359,6 +544,7 @@ const VALUE_KEYS = {
|
|
|
359
544
|
negative_prompt: 'negativePrompt',
|
|
360
545
|
width: 'width',
|
|
361
546
|
height: 'height',
|
|
547
|
+
quality: 'quality',
|
|
362
548
|
seed: 'seed',
|
|
363
549
|
model: 'model',
|
|
364
550
|
image: 'image'
|
|
@@ -437,14 +623,26 @@ export function collectParams(graph, mapping, objectInfo) {
|
|
|
437
623
|
*/
|
|
438
624
|
export const PROMPT_ROLE_NAMES = new Set(['prompt', 'negative_prompt']);
|
|
439
625
|
|
|
626
|
+
const MANAGED_WORKFLOW_ROLES = ['prompt', 'negative_prompt', 'width', 'height', 'quality', 'image'];
|
|
627
|
+
const SELECTOR_INPUTS = /^(prompt|positive_prompt|positivePrompt|negative_prompt|negativePrompt|width|height)$/i;
|
|
628
|
+
|
|
629
|
+
/** 核心参数既可按名称识别,也可能在旧配置里以其他名称绑定同一输入。 */
|
|
630
|
+
export function isManagedWorkflowInput(row, mapping) {
|
|
631
|
+
return SELECTOR_INPUTS.test(row?.name) || SELECTOR_INPUTS.test(row?.input)
|
|
632
|
+
|| MANAGED_WORKFLOW_ROLES.some((role) => {
|
|
633
|
+
const target = mapping?.[role];
|
|
634
|
+
return target && String(target.node) === String(row?.node) && target.input === row?.input;
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
|
|
440
638
|
/**
|
|
441
|
-
*
|
|
639
|
+
* 只有种子和模型作为额外的参数行。
|
|
442
640
|
* 它们在工作流里已经有确定位置,和可调参数放进同一张表,但默认只有能调的那几项需要显式打开。
|
|
443
641
|
* 类型来自 object_info;值是连线时按输入名推断,避免把 ["19",0] 当成字符串。
|
|
444
642
|
*/
|
|
445
643
|
export function collectRoleParams(graph, mapping, objectInfo) {
|
|
446
644
|
const out = [];
|
|
447
|
-
for (const key of ['
|
|
645
|
+
for (const key of ['seed', 'model']) {
|
|
448
646
|
const target = mapping && mapping[key];
|
|
449
647
|
if (!target) continue;
|
|
450
648
|
const node = graph[target.node];
|
|
@@ -459,7 +657,7 @@ export function collectRoleParams(graph, mapping, objectInfo) {
|
|
|
459
657
|
if (Array.isArray(spec) && spec.length) {
|
|
460
658
|
type = Array.isArray(spec[0]) ? 'enum' : String(spec[0]);
|
|
461
659
|
} else if (isLink(value)) {
|
|
462
|
-
type = key === '
|
|
660
|
+
type = key === 'seed' ? 'INT' : 'STRING';
|
|
463
661
|
} else if (typeof value === 'number') {
|
|
464
662
|
type = 'INT';
|
|
465
663
|
}
|
|
@@ -519,7 +717,11 @@ function scalarOf(value) {
|
|
|
519
717
|
* 每一项的形状就是离散参数表要的那一行(node / input / type / default / name),
|
|
520
718
|
* 勾中后直接进 `modelOptions[m].params`。
|
|
521
719
|
*/
|
|
522
|
-
export function listWorkflowInputs(graph) {
|
|
720
|
+
export function listWorkflowInputs(graph, objectInfo = {}) {
|
|
721
|
+
const outputIds = findOutputNodes(graph, objectInfo);
|
|
722
|
+
const { mapping } = workflowBindings(graph, objectInfo, outputIds);
|
|
723
|
+
mapping.image = uniqueReference(referenceInputs(graph, objectInfo, outputIds));
|
|
724
|
+
const outputSet = new Set(outputIds);
|
|
523
725
|
const out = [];
|
|
524
726
|
for (const id of Object.keys(graph || {}).sort(compareNodeIds)) {
|
|
525
727
|
const node = graph[id];
|
|
@@ -528,6 +730,11 @@ export function listWorkflowInputs(graph) {
|
|
|
528
730
|
for (const [input, value] of inputEntries(node).slice().sort(([a], [b]) => a.localeCompare(b))) {
|
|
529
731
|
if (isLink(value)) continue;
|
|
530
732
|
if (value === undefined) continue;
|
|
733
|
+
if (isManagedWorkflowInput({ node: id, input }, mapping)) continue;
|
|
734
|
+
if (typeof value === 'string' && value.includes('{{') && [...workflowPlaceholders(value)].some((name) =>
|
|
735
|
+
MANAGED_WORKFLOW_ROLES.includes(name) || isWorkflowImagePlaceholder(name))) continue;
|
|
736
|
+
// API 导出没有枚举元数据。把可能由选择器接管的档位留给完整的工作流解析。
|
|
737
|
+
if (!objectInfo[classType] && !outputSet.has(id) && typeof value === 'string' && QUALITYISH.test(input)) continue;
|
|
531
738
|
const raw = scalarOf(value);
|
|
532
739
|
out.push({
|
|
533
740
|
node: id,
|
package/lib/config-store.js
CHANGED
|
@@ -16,10 +16,10 @@ import {
|
|
|
16
16
|
} from './adapters.js';
|
|
17
17
|
import { normalizeAuthSpec, resolveHttpUrl } from './adapter-kit.js';
|
|
18
18
|
import { normalizeOperations, normalizeResponseOverrides } from './provider-operations.js';
|
|
19
|
-
import { MAPPING_KEYS, PROMPT_ROLE_NAMES } from './comfy-workflow.js';
|
|
19
|
+
import { MAPPING_KEYS, PROMPT_ROLE_NAMES, isManagedWorkflowInput } from './comfy-workflow.js';
|
|
20
20
|
import { applyFields } from './field-templates.js';
|
|
21
21
|
import { GEMINI_25_IMAGE } from './model-rules.js';
|
|
22
|
-
import { buildImageStatus, editCapableModels, normalizeKeySource } from './status.js';
|
|
22
|
+
import { buildImageStatus, comfyReferencePlan, editCapableModels, normalizeKeySource } from './status.js';
|
|
23
23
|
import { localizedError } from './localized-error.js';
|
|
24
24
|
|
|
25
25
|
export function emptyConfig() {
|
|
@@ -1109,6 +1109,8 @@ export function modelImageState(provider, model, defaults, canEdit) {
|
|
|
1109
1109
|
provider, model, adapter, canEdit,
|
|
1110
1110
|
editModel: model, editAdapter: adapter,
|
|
1111
1111
|
workflowParams: workflowParamsOf(provider, model, adapter),
|
|
1112
|
+
referenceImages: adapter?.id === 'comfyui' && model !== 'auto'
|
|
1113
|
+
? comfyReferencePlan(provider.modelOptions?.[model])?.slots : undefined,
|
|
1112
1114
|
qualitySpec: adapter ? qualityOptionsFor(provider, model, adapter) : null,
|
|
1113
1115
|
quality: adapter ? qualityForAdapter(provider, model, resolveQuality(defaults), adapter) : undefined,
|
|
1114
1116
|
sizeMode: fixedSize ? 'fixed' : 'auto',
|
|
@@ -1136,6 +1138,12 @@ export function activeImageState(store, snapshot) {
|
|
|
1136
1138
|
const state = modelImageState(provider, model, config.defaults, model === 'auto' ? editModels.length > 0 : editModels.includes(model));
|
|
1137
1139
|
state.models = models;
|
|
1138
1140
|
state.editModels = editModels;
|
|
1141
|
+
if (model === 'auto' && state.adapter?.id === 'comfyui') {
|
|
1142
|
+
state.referenceWorkflows = models.flatMap((id, index) => {
|
|
1143
|
+
const slots = comfyReferencePlan(provider.modelOptions?.[id])?.slots;
|
|
1144
|
+
return slots?.length ? [{ index, purposes: slots.map((slot) => slot.purpose) }] : [];
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1139
1147
|
cache?.set(config, state);
|
|
1140
1148
|
return state;
|
|
1141
1149
|
}
|
|
@@ -1230,6 +1238,7 @@ export function workflowParamsOf(provider, model, adapter) {
|
|
|
1230
1238
|
}
|
|
1231
1239
|
const expose = source && source.expose && typeof source.expose === 'object' ? source.expose : {};
|
|
1232
1240
|
const rows = source && Array.isArray(source.params) ? source.params : [];
|
|
1241
|
+
const references = adapter.id === 'comfyui' && rows.length ? comfyReferencePlan(source)?.slots : undefined;
|
|
1233
1242
|
const takesHttpRows = adapterTakesParamRows(adapter);
|
|
1234
1243
|
const exposed = [];
|
|
1235
1244
|
let sawModelRow = false;
|
|
@@ -1248,6 +1257,9 @@ export function workflowParamsOf(provider, model, adapter) {
|
|
|
1248
1257
|
if (isWorkflowParamShape(row)) {
|
|
1249
1258
|
// 工作流行只对 ComfyUI 有意义;落点不全的行写不进去,别报给模型。
|
|
1250
1259
|
if (adapter.id !== 'comfyui' || !row.node || !row.input) continue;
|
|
1260
|
+
if (isManagedWorkflowInput(row, source?.mapping)) continue;
|
|
1261
|
+
if (references?.some((slot) => slot.targets.some((target) =>
|
|
1262
|
+
target.node === row.node && target.input === row.input))) continue;
|
|
1251
1263
|
exposed.push(out);
|
|
1252
1264
|
continue;
|
|
1253
1265
|
}
|
package/lib/image-jobs.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 使用工具调用身份和请求指纹的内存注册表。
|
|
3
3
|
*
|
|
4
4
|
* 为什么需要它:一次生成/编辑是一笔要花钱的远端任务。宿主会重放工具调用
|
|
5
5
|
* (用户重试、客户端重发、Agent 在同一个 callId 上再调一次),这些重放不能
|
|
@@ -232,7 +232,7 @@ function validJobId(id) {
|
|
|
232
232
|
}
|
|
233
233
|
|
|
234
234
|
/**
|
|
235
|
-
*
|
|
235
|
+
* 创建持久化任务日志。
|
|
236
236
|
*
|
|
237
237
|
* 一次生图在提交后进程重启,内存里的 Promise 已经不存在了;只有把
|
|
238
238
|
* 「远端任务 id / 轮询地址 / 恢复所需的参数」写进磁盘,恢复入口才能继续查。
|
package/lib/image-queue.js
CHANGED
package/lib/image-worker.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -47,8 +47,8 @@ export {
|
|
|
47
47
|
capabilityManifest, normalizeConfig, normalizeDefaults, normalizeModelOptions,
|
|
48
48
|
paramNoticeOf, resolveSizeRequest, workflowParamsOf
|
|
49
49
|
} from './config-store.js';
|
|
50
|
-
import { MAPPING_KEYS } from './comfy-workflow.js';
|
|
51
|
-
import { STATUS_CONTRACT, editCapableModels, pickAutoEditModel } from './status.js';
|
|
50
|
+
import { MAPPING_KEYS, assertComfyReferenceCount } from './comfy-workflow.js';
|
|
51
|
+
import { STATUS_CONTRACT, comfyReferencePlan, editCapableModels, pickAutoEditModel } from './status.js';
|
|
52
52
|
import { detectQualityRule, detectFieldRule } from './model-rules.js';
|
|
53
53
|
|
|
54
54
|
const PLUGIN = 'dsh-mo-image';
|
|
@@ -647,6 +647,9 @@ async function edit(args, exec, store, snapshot, queue, journal, cwd) {
|
|
|
647
647
|
const provider = await resolveProviderCredential(store, picked.provider);
|
|
648
648
|
const adapter = resolveAdapter(provider, model);
|
|
649
649
|
assertReferenceCount(adapter.operations?.edit, sources.length);
|
|
650
|
+
if (adapter.id === 'comfyui') {
|
|
651
|
+
assertComfyReferenceCount(sources.length, comfyReferencePlan(provider.modelOptions?.[model])?.count || 1);
|
|
652
|
+
}
|
|
650
653
|
const profile = resolvePromptProfile(model, adapter);
|
|
651
654
|
const quality = resolveToolQuality(provider, model, defaults, args, adapter);
|
|
652
655
|
const { requested, fitted } = imageSizePlan(provider, model, defaults, args, adapter);
|
|
@@ -714,6 +717,9 @@ function settingsView(state) {
|
|
|
714
717
|
const cached = settingsViews.get(state);
|
|
715
718
|
if (cached) return cached;
|
|
716
719
|
const payload = settingsPayload(state);
|
|
720
|
+
if (state.referenceWorkflows?.length) {
|
|
721
|
+
payload.referenceRevision = createHash('sha256').update(stableStringify(state.referenceWorkflows)).digest('hex').slice(0, 16);
|
|
722
|
+
}
|
|
717
723
|
const { provider, defaults } = state;
|
|
718
724
|
// Details are served on demand. Changes to those settings must still invalidate past reads.
|
|
719
725
|
const models = state.model === 'auto' ? state.models : [state.model];
|
|
@@ -1124,7 +1130,6 @@ export function apply(ctx, options = {}) {
|
|
|
1124
1130
|
log: logHost,
|
|
1125
1131
|
installDelayMs: Number.isFinite(updateOptions.installDelayMs) ? updateOptions.installDelayMs : 800,
|
|
1126
1132
|
registryBase: updateOptions.registryBase,
|
|
1127
|
-
mirrorBase: updateOptions.mirrorBase,
|
|
1128
1133
|
cdnSources: updateOptions.cdnSources
|
|
1129
1134
|
});
|
|
1130
1135
|
// 上次更新成没成,先看标记文件——这一步不能等网络,否则重启后的
|
|
@@ -1558,6 +1563,11 @@ export function apply(ctx, options = {}) {
|
|
|
1558
1563
|
unchanged: { type: 'boolean' },
|
|
1559
1564
|
models: { type: 'array', items: { type: 'string' } },
|
|
1560
1565
|
editModels: { type: 'array', items: { type: 'string' } },
|
|
1566
|
+
referenceModelIndexes: { type: 'array', items: { type: 'integer' } },
|
|
1567
|
+
referenceRevision: { type: 'string' },
|
|
1568
|
+
referenceImages: {
|
|
1569
|
+
type: 'array', items: { type: 'object', properties: { index: { type: 'integer' }, purpose: { type: 'string' } } }
|
|
1570
|
+
},
|
|
1561
1571
|
params: { type: 'array', items: { type: 'object' } }
|
|
1562
1572
|
}
|
|
1563
1573
|
},
|
|
@@ -1771,7 +1781,7 @@ export function apply(ctx, options = {}) {
|
|
|
1771
1781
|
type: 'object', additionalProperties: false,
|
|
1772
1782
|
properties: {
|
|
1773
1783
|
prompt: { type: 'string', description: 'Final edit prompt: name each reference (图1/图2); state exactly what changes and that everything else stays.' },
|
|
1774
|
-
images: { type: 'array', items: { type: 'string' }, description: '1–16 absolute
|
|
1784
|
+
images: { type: 'array', items: { type: 'string' }, description: '1–16 absolute paths or http(s) URLs. Match the order, purposes and count in referenceImages when present. For candidates in referenceModelIndexes, query mo_image_settings with the model if details are missing or referenceRevision changed. Ask if an image’s role is unclear.' },
|
|
1775
1785
|
negative_prompt: { type: 'string', description: 'Optional; only for models that support it. Artifacts/elements to avoid.' },
|
|
1776
1786
|
size: { type: 'string', description: 'Optional shape like "1024x1536" or an accepted aspect or tier; only honored if sizeMode is "auto".' },
|
|
1777
1787
|
quality: { type: 'string', description: 'Optional level from qualityOptions. Only honored if qualityMode is "auto".' },
|
package/lib/prompt-profiles.js
CHANGED
|
@@ -277,6 +277,9 @@ export function settingsPayload(state, { includeParamDetails = false } = {}) {
|
|
|
277
277
|
payload.models = (Array.isArray(state && state.models) ? state.models : []).filter(Boolean).map(String);
|
|
278
278
|
// 改图只在能图生图的模型里选;没传 model 时插件会自动取第一个。
|
|
279
279
|
payload.editModels = (Array.isArray(state && state.editModels) ? state.editModels : []).filter(Boolean).map(String);
|
|
280
|
+
if (state?.referenceWorkflows?.length) {
|
|
281
|
+
payload.referenceModelIndexes = state.referenceWorkflows.map((item) => item.index);
|
|
282
|
+
}
|
|
280
283
|
return payload;
|
|
281
284
|
}
|
|
282
285
|
const profile = resolvePromptProfile(model, (state && state.adapter) || null);
|
|
@@ -298,6 +301,11 @@ export function settingsPayload(state, { includeParamDetails = false } = {}) {
|
|
|
298
301
|
if (payload.strict) {
|
|
299
302
|
payload.hint += ';严格模式尝试将成品缩放到明确的像素宽高;比例或档位没有精确像素目标';
|
|
300
303
|
}
|
|
304
|
+
if (state?.referenceImages?.length) {
|
|
305
|
+
payload.referenceImages = state.referenceImages.map(({ index, purpose }) => ({
|
|
306
|
+
index, ...(purpose ? { purpose } : {})
|
|
307
|
+
}));
|
|
308
|
+
}
|
|
301
309
|
const workflowParams = Array.isArray(state && state.workflowParams)
|
|
302
310
|
? state.workflowParams.filter((param) => param && param.expose !== false)
|
|
303
311
|
: [];
|