dsh-mo-image 0.6.0 → 0.7.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/CHANGELOG.md +11 -0
- package/README.md +57 -17
- package/icon.png +0 -0
- package/lib/adapter-kit.js +363 -11
- package/lib/adapters/async.js +258 -31
- package/lib/adapters/cn.js +13 -0
- package/lib/adapters/openai.js +21 -2
- package/lib/adapters.js +880 -139
- package/lib/client.js +459 -131
- package/lib/comfy-workflow.js +7 -3
- package/lib/config-store.js +692 -0
- package/lib/image-execution.js +835 -0
- package/lib/image-jobs.js +333 -0
- package/lib/image-queue.js +195 -0
- package/lib/image-service.js +226 -0
- package/lib/image-worker.js +195 -0
- package/lib/index.js +439 -858
- package/lib/png-scale.js +103 -28
- package/lib/prompt-profiles.js +139 -68
- package/lib/request-auth.js +143 -0
- package/lib/retry.js +162 -0
- package/lib/settings-memory.js +33 -0
- package/lib/status.js +187 -0
- package/locale/messages.csv +226 -214
- package/package.json +3 -1
- package/skills/mo-image-editing/SKILL.md +34 -72
- package/skills/mo-image-generation/SKILL.md +59 -152
package/lib/adapters.js
CHANGED
|
@@ -21,7 +21,8 @@ import {
|
|
|
21
21
|
ASPECT_CHOICES, aspectLabel, nearestAspect, resolutionFor, baseUrlOf, headersFor,
|
|
22
22
|
keyedHeaders, sleep, readJsonResponse, jsonRequest, getPath, sniffType, looksLikeImage,
|
|
23
23
|
base64ToImage, fetchImageUrl, decodeImagePayload, classifyFailure, extractMessage,
|
|
24
|
-
readResponseBytes, MAX_IMAGE_BYTES
|
|
24
|
+
readResponseBytes, MAX_IMAGE_BYTES, imageSizeOf,
|
|
25
|
+
readTaskEnvelope, taskPollUrls, taskStatusOf, taskImageOf, looseImageOf, taskMessageOf
|
|
25
26
|
} from './adapter-kit.js';
|
|
26
27
|
import { OPENAI_ADAPTERS } from './adapters/openai.js';
|
|
27
28
|
import { ASYNC_ADAPTERS } from './adapters/async.js';
|
|
@@ -29,7 +30,8 @@ import { CN_ADAPTERS } from './adapters/cn.js';
|
|
|
29
30
|
|
|
30
31
|
export {
|
|
31
32
|
ASPECT_CHOICES, aspectLabel, nearestAspect, resolutionFor, sniffType, looksLikeImage,
|
|
32
|
-
fetchImageUrl, decodeImagePayload, classifyFailure, extractMessage, readResponseBytes, MAX_IMAGE_BYTES
|
|
33
|
+
fetchImageUrl, decodeImagePayload, classifyFailure, extractMessage, readResponseBytes, MAX_IMAGE_BYTES, imageSizeOf,
|
|
34
|
+
readTaskEnvelope, taskPollUrls, taskStatusOf, taskImageOf, looseImageOf, taskMessageOf
|
|
33
35
|
};
|
|
34
36
|
|
|
35
37
|
const require = createRequire(import.meta.url);
|
|
@@ -48,17 +50,107 @@ export function parseSize(raw) {
|
|
|
48
50
|
return { width: width, height: height, text: width + 'x' + height };
|
|
49
51
|
}
|
|
50
52
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
/**
|
|
54
|
+
* 合并出该模型真正适用的尺寸策略:基础策略 + sizeByModel 里第一个命中的覆盖。
|
|
55
|
+
* 同一平台内部规则按模型分叉(Ark 5.5 只有 1K/1.5K/2K,魔搭 Qwen-Image 上限 1664),
|
|
56
|
+
* 所以能力必须按模型算,不能只看适配器。
|
|
57
|
+
*/
|
|
58
|
+
export function sizePolicyFor(adapter, model) {
|
|
59
|
+
const base = adapter && adapter.size && typeof adapter.size === 'object' ? adapter.size : {};
|
|
60
|
+
const overrides = adapter && Array.isArray(adapter.sizeByModel) ? adapter.sizeByModel : [];
|
|
61
|
+
const id = String(model || '');
|
|
62
|
+
for (const entry of overrides) {
|
|
63
|
+
if (!entry || !entry.size || !(entry.match instanceof RegExp)) continue;
|
|
64
|
+
entry.match.lastIndex = 0;
|
|
65
|
+
if (id && entry.match.test(id)) return Object.assign({}, base, entry.size);
|
|
66
|
+
}
|
|
67
|
+
return base;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 下发给选择器的尺寸能力:控件形态 + 候选 + hint 代码。
|
|
72
|
+
* 客户端只按这个渲染,不再自己判断"这个模型收不收宽高"。
|
|
73
|
+
*/
|
|
74
|
+
export function sizeSpecFor(adapter, model) {
|
|
75
|
+
if (!adapter) return { control: 'wh', aspects: [], tiers: [], custom: true, wire: [], stitch: false, hint: '' };
|
|
76
|
+
const policy = sizePolicyFor(adapter, model);
|
|
77
|
+
const allowed = Array.isArray(policy.allowed) ? policy.allowed.filter(Boolean).map(String) : [];
|
|
78
|
+
const control = String(policy.control || (allowed.length ? 'preset' : (policy.style === 'aspect' ? 'aspect' : 'wh')));
|
|
79
|
+
const tiers = (Array.isArray(policy.tiers) ? policy.tiers : [])
|
|
80
|
+
.filter((item) => item && item.value)
|
|
81
|
+
.map((item) => ({ value: String(item.value), pixels: Number(item.pixels) || 0 }));
|
|
54
82
|
return {
|
|
83
|
+
control: control,
|
|
84
|
+
aspects: Array.isArray(policy.aspects) ? policy.aspects.map(String) : [],
|
|
85
|
+
tiers: tiers.length ? tiers : allowed.map((value) => ({ value: value, pixels: 0 })),
|
|
86
|
+
custom: policy.custom === true,
|
|
87
|
+
wire: Array.isArray(policy.wire) ? policy.wire.map(String) : [],
|
|
88
|
+
stitch: policy.stitch === true,
|
|
89
|
+
hint: String(policy.hint || '')
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** 档位名("2K")解析成 {value, pixels};不是档位就退成 WxH 像素。 */export function parseTier(adapter, raw, model) {
|
|
94
|
+
const policy = sizePolicyFor(adapter, model);
|
|
95
|
+
const tiers = Array.isArray(policy.tiers) ? policy.tiers : [];
|
|
96
|
+
const text = String(raw || '').trim();
|
|
97
|
+
if (text) {
|
|
98
|
+
const wanted = text.toLowerCase();
|
|
99
|
+
const hit = tiers.find((item) => item && String(item.value || '').toLowerCase() === wanted);
|
|
100
|
+
const pixels = hit ? Number(hit.pixels) : 0;
|
|
101
|
+
if (hit && pixels > 0) return { value: String(hit.value), pixels: pixels };
|
|
102
|
+
}
|
|
103
|
+
const parsed = parseSize(text);
|
|
104
|
+
return parsed
|
|
105
|
+
? { value: parsed.text, pixels: parsed.width * parsed.height, width: parsed.width, height: parsed.height }
|
|
106
|
+
: null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 按像素预算挑最近的档位:只挑最近的可用档,不做阈值猜测。 */
|
|
110
|
+
export function nearestTier(tiers, pixels) {
|
|
111
|
+
const list = Array.isArray(tiers) ? tiers : [];
|
|
112
|
+
const target = Math.log(Math.max(1, Number(pixels) || 0));
|
|
113
|
+
let best = null;
|
|
114
|
+
let bestScore = Infinity;
|
|
115
|
+
for (const item of list) {
|
|
116
|
+
const value = Number(item && item.pixels);
|
|
117
|
+
if (!(value > 0)) continue;
|
|
118
|
+
const score = Math.abs(Math.log(value) - target);
|
|
119
|
+
if (score < bestScore) { bestScore = score; best = item; }
|
|
120
|
+
}
|
|
121
|
+
return best;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function round8(value) {
|
|
125
|
+
return Math.max(8, Math.round(Number(value) / 8) * 8);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** 比例 + 像素预算 → 具体宽高(两侧对齐 8,会直接当成严格模式的落盘目标)。 */
|
|
129
|
+
export function sizeForAspect(aspect, budget) {
|
|
130
|
+
const parts = String(aspect || '1:1').split(':');
|
|
131
|
+
const a = Number(parts[0]) || 1;
|
|
132
|
+
const b = Number(parts[1]) || 1;
|
|
133
|
+
const total = Number(budget) > 0 ? Number(budget) : 1024 * 1024;
|
|
134
|
+
return { width: round8(Math.sqrt((total * a) / b)), height: round8(Math.sqrt((total * b) / a)) };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function settle(size, adjusted, reason, requested, extra) {
|
|
138
|
+
const text = size && size.text ? size.text : (size && size.size ? size.size : String(size || DEFAULT_SIZE));
|
|
139
|
+
const width = size && Number(size.width) ? Math.round(Number(size.width)) : 0;
|
|
140
|
+
const height = size && Number(size.height) ? Math.round(Number(size.height)) : 0;
|
|
141
|
+
const parsed = width && height ? { width: width, height: height } : parseSize(text);
|
|
142
|
+
return Object.assign({
|
|
55
143
|
size: text,
|
|
56
144
|
width: parsed ? parsed.width : 0,
|
|
57
145
|
height: parsed ? parsed.height : 0,
|
|
58
146
|
adjusted: !!adjusted,
|
|
59
147
|
reason: reason || '',
|
|
60
|
-
requested: requested || null
|
|
61
|
-
|
|
148
|
+
requested: requested || null,
|
|
149
|
+
// 比例/档位/是否把尺寸写进提示词:编码器与严格模式都要用。
|
|
150
|
+
aspect: '',
|
|
151
|
+
tier: '',
|
|
152
|
+
stitch: false
|
|
153
|
+
}, extra || {});
|
|
62
154
|
}
|
|
63
155
|
|
|
64
156
|
function nearestAllowed(allowed, width, height) {
|
|
@@ -80,7 +172,7 @@ function nearestAllowed(allowed, width, height) {
|
|
|
80
172
|
return best;
|
|
81
173
|
}
|
|
82
174
|
|
|
83
|
-
function clampPixels(width, height, minPixels, maxPixels, multiple) {
|
|
175
|
+
function clampPixels(width, height, minPixels, maxPixels, multiple, minSide, maxSide) {
|
|
84
176
|
let w = width;
|
|
85
177
|
let h = height;
|
|
86
178
|
if (minPixels && w * h < minPixels) {
|
|
@@ -93,6 +185,18 @@ function clampPixels(width, height, minPixels, maxPixels, multiple) {
|
|
|
93
185
|
w = Math.floor(w * k);
|
|
94
186
|
h = Math.floor(h * k);
|
|
95
187
|
}
|
|
188
|
+
// 有的网关只限制单边(魔搭是每边 [64, 2048]),按比例缩放到区间内,别把比例压扁。
|
|
189
|
+
// 先补下限再收上限:两个方向打架时(极端长宽比)宁可不超上限,否则一定被网关拒。
|
|
190
|
+
if (minSide && Math.min(w, h) < minSide) {
|
|
191
|
+
const k = minSide / Math.min(w, h);
|
|
192
|
+
w = Math.ceil(w * k);
|
|
193
|
+
h = Math.ceil(h * k);
|
|
194
|
+
}
|
|
195
|
+
if (maxSide && Math.max(w, h) > maxSide) {
|
|
196
|
+
const k = maxSide / Math.max(w, h);
|
|
197
|
+
w = Math.floor(w * k);
|
|
198
|
+
h = Math.floor(h * k);
|
|
199
|
+
}
|
|
96
200
|
w = Math.max(multiple, Math.round(w / multiple) * multiple);
|
|
97
201
|
h = Math.max(multiple, Math.round(h / multiple) * multiple);
|
|
98
202
|
return { width: w, height: h };
|
|
@@ -102,86 +206,132 @@ function clampPixels(width, height, minPixels, maxPixels, multiple) {
|
|
|
102
206
|
* 把用户要的尺寸落成该模型真正接受的尺寸。
|
|
103
207
|
* 返回 { size, width, height, adjusted, reason, requested, rejected? }
|
|
104
208
|
*/
|
|
105
|
-
export function fitSize(adapter,
|
|
106
|
-
const policy = adapter
|
|
209
|
+
export function fitSize(adapter, request, model) {
|
|
210
|
+
const policy = sizePolicyFor(adapter, model);
|
|
107
211
|
const allowed = Array.isArray(policy.allowed) ? policy.allowed : null;
|
|
212
|
+
// allowCustom:allowed 只是推荐值,白名单外照发(只受像素/倍数约束)。
|
|
213
|
+
const allowCustom = policy.allowCustom === true;
|
|
108
214
|
const fallback = parseSize(policy.default || DEFAULT_SIZE) || parseSize(DEFAULT_SIZE);
|
|
109
|
-
const
|
|
215
|
+
const objectRequest = request && typeof request === 'object' ? request : null;
|
|
216
|
+
const requestedRaw = String(objectRequest ? (objectRequest.text || '') : (request === undefined || request === null ? '' : request)).trim();
|
|
110
217
|
const requestedText = requestedRaw.toLowerCase().replace(/[×*]/g, 'x').replace(/\s+/g, '');
|
|
111
218
|
const requested = parseSize(requestedText);
|
|
219
|
+
// 档位与比例在这里补齐:编码器只要读 fitted.tier / fitted.aspect,不必自己查策略。
|
|
220
|
+
const done = (value) => withTierAspect(policy, value);
|
|
112
221
|
|
|
113
222
|
if (!requestedText || requestedText === 'none' || requestedText === 'auto') {
|
|
114
223
|
// 用户选了「自动」:请求里干脆不带尺寸,让模型自己决定
|
|
115
|
-
return { size: 'none', width: 0, height: 0,
|
|
224
|
+
return Object.assign(settle('none', false, '', null), { size: 'none', width: 0, height: 0, omit: true });
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// 档位名("2K"):size 保留档位名(编码要原样发上去),宽高取该档像素。
|
|
228
|
+
const tier = parseTier(adapter, requestedText, model);
|
|
229
|
+
if (tier && !requested && tier.value) {
|
|
230
|
+
const side = Math.round(Math.sqrt(tier.pixels)) || 0;
|
|
231
|
+
return done(settle({ text: tier.value, width: side, height: side }, false, '', null, { tier: tier.value }));
|
|
116
232
|
}
|
|
117
233
|
|
|
118
234
|
if (!requested) {
|
|
119
|
-
return settle(fallback, !!requestedRaw, requestedRaw ? '尺寸无法识别,改用 ' + fallback.text : '', null);
|
|
235
|
+
return done(settle(fallback, !!requestedRaw, requestedRaw ? '尺寸无法识别,改用 ' + fallback.text : '', null));
|
|
120
236
|
}
|
|
121
237
|
|
|
122
|
-
if (allowed && allowed.length) {
|
|
238
|
+
if (allowed && allowed.length && !allowCustom) {
|
|
123
239
|
if (allowed.includes(requested.text)) {
|
|
124
|
-
return settle(requested, false, '', requested);
|
|
240
|
+
return done(settle(requested, false, '', requested));
|
|
125
241
|
}
|
|
126
242
|
if (policy.onUnsupported === 'reject') {
|
|
127
|
-
return Object.assign(settle(requested, false, '', requested), {
|
|
243
|
+
return done(Object.assign(settle(requested, false, '', requested), {
|
|
128
244
|
rejected: true,
|
|
129
245
|
reason: '该模型不支持 ' + requested.text + ',可用尺寸:' + allowed.join(' / ')
|
|
130
|
-
});
|
|
246
|
+
}));
|
|
131
247
|
}
|
|
132
248
|
const chosen = nearestAllowed(allowed, requested.width, requested.height) || fallback;
|
|
133
|
-
return settle(chosen, true, requested.text + ' → ' + chosen.text, requested);
|
|
249
|
+
return done(settle(chosen, true, requested.text + ' → ' + chosen.text, requested));
|
|
134
250
|
}
|
|
135
251
|
|
|
136
252
|
const multiple = Number(policy.multiple) || 1;
|
|
137
253
|
const minPixels = Number(policy.minPixels) || 0;
|
|
138
254
|
const maxPixels = Number(policy.maxPixels) || 0;
|
|
139
|
-
const
|
|
255
|
+
const minSide = Number(policy.minSide) || 0;
|
|
256
|
+
const maxSide = Number(policy.maxSide) || 0;
|
|
257
|
+
const clamped = clampPixels(requested.width, requested.height, minPixels, maxPixels, multiple, minSide, maxSide);
|
|
140
258
|
const text = clamped.width + 'x' + clamped.height;
|
|
141
|
-
return settle({ width: clamped.width, height: clamped.height, text: text }, text !== requested.text,
|
|
142
|
-
text !== requested.text ? requested.text + ' → ' + text : '', requested);
|
|
259
|
+
return done(settle({ width: clamped.width, height: clamped.height, text: text }, text !== requested.text,
|
|
260
|
+
text !== requested.text ? requested.text + ' → ' + text : '', requested));
|
|
143
261
|
}
|
|
144
262
|
|
|
145
|
-
/**
|
|
146
|
-
|
|
147
|
-
if (fitted
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
for (const item of allowed) {
|
|
154
|
-
const parsed = parseSize(item);
|
|
155
|
-
if (!parsed) continue;
|
|
156
|
-
if (parsed.width * parsed.height > currentPixels && (!best || parsed.width * parsed.height < best.width * best.height)) best = parsed;
|
|
263
|
+
/** 给落位后的尺寸补上档位与比例:编码器就不必自己再查一遍策略。 */
|
|
264
|
+
function withTierAspect(policy, fitted) {
|
|
265
|
+
if (!fitted || fitted.omit || !fitted.width || !fitted.height) return fitted;
|
|
266
|
+
if (!fitted.tier) {
|
|
267
|
+
const tiers = Array.isArray(policy.tiers) ? policy.tiers : [];
|
|
268
|
+
if (tiers.length) {
|
|
269
|
+
const tier = nearestTier(tiers, fitted.width * fitted.height);
|
|
270
|
+
if (tier) fitted.tier = String(tier.value);
|
|
157
271
|
}
|
|
158
|
-
if (best) return settle(best, true, '上游拒绝 ' + fitted.size + ',抬到 ' + best.text, fitted.requested);
|
|
159
272
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const h = Math.round((fitted.height || 1024) * k);
|
|
167
|
-
return settle({ width: w, height: h, text: w + 'x' + h }, true, '上游拒绝 ' + fitted.size + ',抬到 ' + w + 'x' + h, fitted.requested);
|
|
273
|
+
if (!fitted.aspect) {
|
|
274
|
+
const aspects = Array.isArray(policy.aspects) ? policy.aspects : null;
|
|
275
|
+
// 只处理冒号格式的比例表;Ideogram 这种 16x9 的由它自己的编码器转换。
|
|
276
|
+
if (aspects && aspects.length && String(aspects[0]).includes(':')) {
|
|
277
|
+
fitted.aspect = nearestAspect(fitted.width, fitted.height, aspects);
|
|
278
|
+
}
|
|
168
279
|
}
|
|
169
|
-
return
|
|
280
|
+
return fitted;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function megapixels(value) {
|
|
284
|
+
return (Number(value) / 1e6).toFixed(2);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* 上游拒绝了尺寸以后,告诉用户这个模型到底能吃什么。
|
|
289
|
+
* 顺序:白名单 → 档位名 → 像素区间 → 单边区间 → 倍数 → 根本不收尺寸。
|
|
290
|
+
*/
|
|
291
|
+
export function sizeHintText(adapter, model) {
|
|
292
|
+
const policy = sizePolicyFor(adapter, model);
|
|
293
|
+
const allowed = Array.isArray(policy.allowed) ? policy.allowed.filter(Boolean) : [];
|
|
294
|
+
if (allowed.length) return '支持的尺寸:' + allowed.join(' / ');
|
|
295
|
+
const tiers = Array.isArray(policy.tiers) ? policy.tiers.filter((item) => item && item.value) : [];
|
|
296
|
+
if (tiers.length) return '支持的档位:' + tiers.map((item) => item.value).join(' / ');
|
|
297
|
+
const parts = [];
|
|
298
|
+
const minPixels = Number(policy.minPixels) || 0;
|
|
299
|
+
const maxPixels = Number(policy.maxPixels) || 0;
|
|
300
|
+
const minSide = Number(policy.minSide) || 0;
|
|
301
|
+
const maxSide = Number(policy.maxSide) || 0;
|
|
302
|
+
const multiple = Number(policy.multiple) || 1;
|
|
303
|
+
if (minPixels && maxPixels) parts.push('总像素 ' + megapixels(minPixels) + '–' + megapixels(maxPixels) + 'MP');
|
|
304
|
+
else if (minPixels) parts.push('总像素不少于 ' + megapixels(minPixels) + 'MP');
|
|
305
|
+
else if (maxPixels) parts.push('总像素不超过 ' + megapixels(maxPixels) + 'MP');
|
|
306
|
+
if (minSide && maxSide) parts.push('每边 ' + minSide + '–' + maxSide + 'px');
|
|
307
|
+
else if (minSide) parts.push('每边不少于 ' + minSide + 'px');
|
|
308
|
+
else if (maxSide) parts.push('每边不超过 ' + maxSide + 'px');
|
|
309
|
+
if (multiple > 1) parts.push('宽高需为 ' + multiple + ' 的倍数');
|
|
310
|
+
const wire = Array.isArray(policy.wire) ? policy.wire : null;
|
|
311
|
+
if (!parts.length) return wire && !wire.length ? '该模型不接受尺寸参数' : '该模型没有声明尺寸范围';
|
|
312
|
+
return '支持的尺寸:' + parts.join(',');
|
|
170
313
|
}
|
|
171
314
|
|
|
172
315
|
/* -------------------------------------------------------------- adapters */
|
|
173
316
|
|
|
174
|
-
function encodeSize(adapter, fitted) {
|
|
175
|
-
const policy = adapter
|
|
317
|
+
function encodeSize(adapter, fitted, model) {
|
|
318
|
+
const policy = sizePolicyFor(adapter, model);
|
|
176
319
|
const field = policy.field || 'size';
|
|
177
320
|
const style = policy.style || 'string';
|
|
178
321
|
if (fitted.omit || fitted.size === 'none') return {};
|
|
179
|
-
//
|
|
180
|
-
|
|
322
|
+
// 少数平台只认档位名(火山方舟的 1K/1.5K/2K);已经就是档位就原样发,
|
|
323
|
+
// 是像素且贴近某一档才换成档位名,其余照发像素。
|
|
324
|
+
const tiers = Array.isArray(policy.tiers) && policy.tiers.length
|
|
325
|
+
? policy.tiers
|
|
326
|
+
: (Array.isArray(policy.presets) ? policy.presets : null);
|
|
327
|
+
if (tiers) {
|
|
328
|
+
const parsed = parseSize(fitted.size);
|
|
329
|
+
const isLabel = !parsed && String(fitted.size || '').trim() !== '';
|
|
330
|
+
if (isLabel) return { [field]: fitted.size };
|
|
181
331
|
const pixels = (fitted.width || 1024) * (fitted.height || 1024);
|
|
182
|
-
for (const
|
|
183
|
-
if (!
|
|
184
|
-
if (Math.abs(pixels -
|
|
332
|
+
for (const tier of tiers) {
|
|
333
|
+
if (!tier || !tier.pixels) continue;
|
|
334
|
+
if (Math.abs(pixels - tier.pixels) / tier.pixels <= 0.12) return { [field]: tier.value };
|
|
185
335
|
}
|
|
186
336
|
return { [field]: fitted.size };
|
|
187
337
|
}
|
|
@@ -297,41 +447,147 @@ export function filterImageModels(models) {
|
|
|
297
447
|
import { parseWorkflow, uiToApi, analyzeWorkflow, injectByMapping, applyWorkflowParams } from './comfy-workflow.js';
|
|
298
448
|
|
|
299
449
|
const OBJECT_INFO_TTL = 5 * 60 * 1000;
|
|
450
|
+
// 装了很多自定义节点的 ComfyUI,一次 /object_info 就能到 20MB+。
|
|
451
|
+
// 缓存必须有条数与字节上限,否则在设置页来回切换就会把整份签名长期钉在内存里。
|
|
452
|
+
const OBJECT_INFO_LIMIT = 50;
|
|
453
|
+
const OBJECT_INFO_BUDGET = 4 * 1024 * 1024;
|
|
454
|
+
|
|
455
|
+
/** 已完成的签名:key -> { at, bytes, data }。 */
|
|
300
456
|
const objectInfoCache = new Map();
|
|
457
|
+
/** 进行中的签名:key -> Promise。并发的设置页解析、模型列表和工作流执行共用一次请求。 */
|
|
458
|
+
const objectInfoInflight = new Map();
|
|
459
|
+
let objectInfoBytes = 0;
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* 凭据版本引用:换了 Key 就不能复用旧权限下取回的签名,所以凭据必须参与缓存键;
|
|
463
|
+
* 但键会长期留在 Map 里,不能出现明文 Key。这里只用长度 + FNV-1a 哈希做版本引用。
|
|
464
|
+
*/
|
|
465
|
+
function credentialRef(provider) {
|
|
466
|
+
const key = String((provider && provider.key) || '');
|
|
467
|
+
if (!key) return 'anon';
|
|
468
|
+
let hash = 0x811c9dc5;
|
|
469
|
+
for (let i = 0; i < key.length; i++) {
|
|
470
|
+
hash ^= key.charCodeAt(i);
|
|
471
|
+
hash = Math.imul(hash, 0x01000193) >>> 0;
|
|
472
|
+
}
|
|
473
|
+
return key.length + ':' + hash.toString(16);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** 缓存键:baseUrl + 节点 + 字段 + 凭据版本。全量读没有节点/字段,两段留空。 */
|
|
477
|
+
function objectInfoKey(base, provider, node, field) {
|
|
478
|
+
return [String(base || ''), String(node || ''), String(field || ''), credentialRef(provider)].join('\u0000');
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function evictObjectInfo(key) {
|
|
482
|
+
const entry = objectInfoCache.get(key);
|
|
483
|
+
if (!entry) return;
|
|
484
|
+
objectInfoBytes -= entry.bytes;
|
|
485
|
+
objectInfoCache.delete(key);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** 过期项在每次读写时清掉;超容量时按写入顺序淘汰最旧的。 */
|
|
489
|
+
function pruneObjectInfo(now) {
|
|
490
|
+
for (const [key, entry] of objectInfoCache) {
|
|
491
|
+
if (now - entry.at >= OBJECT_INFO_TTL) evictObjectInfo(key);
|
|
492
|
+
}
|
|
493
|
+
// 留一格给马上要写入的条目,避免刚放进来的缓存立刻被自己挤掉。
|
|
494
|
+
while (objectInfoCache.size >= OBJECT_INFO_LIMIT) {
|
|
495
|
+
const oldest = objectInfoCache.keys().next().value;
|
|
496
|
+
if (oldest === undefined) break;
|
|
497
|
+
evictObjectInfo(oldest);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function rememberObjectInfo(key, data) {
|
|
502
|
+
let bytes = 0;
|
|
503
|
+
try {
|
|
504
|
+
// 只需要量级判断:按 JSON 字符长度估算,不做逐字节统计。
|
|
505
|
+
bytes = JSON.stringify(data)?.length || 0;
|
|
506
|
+
} catch (_error) {
|
|
507
|
+
bytes = 0;
|
|
508
|
+
}
|
|
509
|
+
pruneObjectInfo(Date.now());
|
|
510
|
+
// Map 的插入顺序就是淘汰顺序:重写同一个键时先删再加,让它回到队尾。
|
|
511
|
+
evictObjectInfo(key);
|
|
512
|
+
objectInfoCache.set(key, { at: Date.now(), bytes, data });
|
|
513
|
+
objectInfoBytes += bytes;
|
|
514
|
+
// 超预算先淘汰旧条目,但永远保留刚写入的一条,否则超大签名会一次都缓存不上。
|
|
515
|
+
while (objectInfoBytes > OBJECT_INFO_BUDGET && objectInfoCache.size > 1) {
|
|
516
|
+
const oldest = objectInfoCache.keys().next().value;
|
|
517
|
+
if (oldest === undefined) break;
|
|
518
|
+
evictObjectInfo(oldest);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/** 同一个 key 的进行中请求只发一次;失败时占位也必须清掉(失败不缓存,下次还能重试)。 */
|
|
523
|
+
function trackObjectInfo(key, task) {
|
|
524
|
+
const pending = objectInfoInflight.get(key);
|
|
525
|
+
if (pending) return pending;
|
|
526
|
+
let tracked;
|
|
527
|
+
tracked = task.finally(() => {
|
|
528
|
+
if (objectInfoInflight.get(key) === tracked) objectInfoInflight.delete(key);
|
|
529
|
+
});
|
|
530
|
+
objectInfoInflight.set(key, tracked);
|
|
531
|
+
return tracked;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** 测试用:清掉 ComfyUI 节点签名缓存和进行中占位。 */
|
|
535
|
+
export function resetComfyObjectInfoCache() {
|
|
536
|
+
objectInfoCache.clear();
|
|
537
|
+
objectInfoInflight.clear();
|
|
538
|
+
objectInfoBytes = 0;
|
|
539
|
+
}
|
|
301
540
|
|
|
302
541
|
/** 拉全量节点签名并缓存;UI 格式工作流和模型列表都靠它。 */
|
|
303
542
|
async function comfyObjectInfo(base, provider, signal) {
|
|
304
|
-
const
|
|
305
|
-
const
|
|
306
|
-
if (hit
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
try { await response.body?.cancel(); } catch (_error) { /* ignore */ }
|
|
310
|
-
throw new Error('读不到 ComfyUI 的节点签名(/object_info),请确认服务在运行。');
|
|
543
|
+
const key = objectInfoKey(base, provider, '', '');
|
|
544
|
+
const hit = objectInfoCache.get(key);
|
|
545
|
+
if (hit) {
|
|
546
|
+
if (Date.now() - hit.at < OBJECT_INFO_TTL) return hit.data;
|
|
547
|
+
evictObjectInfo(key);
|
|
311
548
|
}
|
|
312
|
-
const
|
|
313
|
-
|
|
314
|
-
|
|
549
|
+
const pending = objectInfoInflight.get(key);
|
|
550
|
+
if (pending) return pending;
|
|
551
|
+
const task = (async () => {
|
|
552
|
+
const response = await request(base + '/object_info', { headers: headersFor(provider, false), signal });
|
|
553
|
+
if (!response.ok) {
|
|
554
|
+
try { await response.body?.cancel(); } catch (_error) { /* ignore */ }
|
|
555
|
+
throw new Error('读不到 ComfyUI 的节点签名(/object_info),请确认服务在运行。');
|
|
556
|
+
}
|
|
557
|
+
const data = await readJsonResponse(response, 40 * 1024 * 1024);
|
|
558
|
+
rememberObjectInfo(key, data);
|
|
559
|
+
return data;
|
|
560
|
+
})();
|
|
561
|
+
return trackObjectInfo(key, task);
|
|
315
562
|
}
|
|
316
563
|
|
|
317
564
|
/**
|
|
318
565
|
* 读某个 loader 节点可选的模型名。优先只取单节点,避免自定义节点很多时
|
|
319
566
|
* /object_info 的 20MB+ 全量响应;节点级 404 时退回全量。
|
|
567
|
+
*
|
|
568
|
+
* 节点候选只合并进行中的相同请求,不做 TTL 缓存:用户往 models/ 里放个文件
|
|
569
|
+
* 就该在下次拉列表时看到,这里必须保持「当下装了什么」的语义。
|
|
320
570
|
*/
|
|
321
571
|
async function comfyNodeOptions(base, provider, node, field, signal) {
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
572
|
+
const key = objectInfoKey(base, provider, node, field);
|
|
573
|
+
const pending = objectInfoInflight.get(key);
|
|
574
|
+
if (pending) return pending;
|
|
575
|
+
const task = (async () => {
|
|
576
|
+
let response = await request(base + '/object_info/' + node, { headers: headersFor(provider, false), signal });
|
|
577
|
+
if (!response.ok) {
|
|
578
|
+
try { await response.body?.cancel(); } catch (_error) { /* ignore */ }
|
|
579
|
+
response = await request(base + '/object_info', { headers: headersFor(provider, false), signal });
|
|
580
|
+
if (!response.ok) return [];
|
|
581
|
+
}
|
|
582
|
+
const payload = await readJsonResponse(response, 20 * 1024 * 1024);
|
|
583
|
+
const entry = payload && payload[node];
|
|
584
|
+
const inputs = entry && entry.input;
|
|
585
|
+
const names = (inputs && inputs.required && inputs.required[field])
|
|
586
|
+
|| (inputs && inputs.optional && inputs.optional[field]);
|
|
587
|
+
const list = Array.isArray(names) ? names[0] : null;
|
|
588
|
+
return Array.isArray(list) ? list.map((name) => String(name)).filter(Boolean) : [];
|
|
589
|
+
})();
|
|
590
|
+
return trackObjectInfo(key, task);
|
|
335
591
|
}
|
|
336
592
|
|
|
337
593
|
const FLUX2_NAME = /flux[._\-\s]?2/i;
|
|
@@ -550,6 +806,22 @@ async function comfyMossEditWorkflow(base, provider, { input, model, seed, signa
|
|
|
550
806
|
};
|
|
551
807
|
}
|
|
552
808
|
|
|
809
|
+
/**
|
|
810
|
+
* ComfyUI 队列轮询的退避曲线。
|
|
811
|
+
*
|
|
812
|
+
* 固定 700ms 会在排队久的任务上把本地服务刷成每秒 1.4 次查询;本地接口虽然便宜,
|
|
813
|
+
* 但 ComfyUI 单进程还要渲染,空转查询会抢它的时间片。改成 1s 起步、每次 ×1.15~1.3、
|
|
814
|
+
* 5s 封顶:刚提交时反应快,跑久了自动降频。抖动是必要的——多会话同时提交时,
|
|
815
|
+
* 没有抖动它们会一直同相位地一起打。
|
|
816
|
+
*/
|
|
817
|
+
const COMFY_POLL_START_MS = 1000;
|
|
818
|
+
const COMFY_POLL_MAX_MS = 5000;
|
|
819
|
+
|
|
820
|
+
function nextComfyPollDelay(current) {
|
|
821
|
+
const factor = 1.15 + Math.random() * 0.15;
|
|
822
|
+
return Math.min(COMFY_POLL_MAX_MS, Math.round(current * factor));
|
|
823
|
+
}
|
|
824
|
+
|
|
553
825
|
async function comfySubmitAndWait(base, provider, workflow, signal) {
|
|
554
826
|
const submitted = await readJsonResponse(await request(base + '/prompt', {
|
|
555
827
|
method: 'POST', headers: headersFor(provider, false),
|
|
@@ -557,11 +829,15 @@ async function comfySubmitAndWait(base, provider, workflow, signal) {
|
|
|
557
829
|
}), 2 * 1024 * 1024);
|
|
558
830
|
const promptId = submitted && submitted.prompt_id;
|
|
559
831
|
if (!promptId) throw new Error('ComfyUI 没有返回 prompt_id');
|
|
832
|
+
// 总超时不变(5 分钟),只是查询频率随等待变长而下降。
|
|
560
833
|
const deadline = Date.now() + 300000;
|
|
561
834
|
let history = null;
|
|
835
|
+
let wait = COMFY_POLL_START_MS;
|
|
562
836
|
while (Date.now() < deadline) {
|
|
563
837
|
if (signal && signal.aborted) throw new Error('已取消');
|
|
564
|
-
|
|
838
|
+
// 最后一次等待不能越过 deadline,否则「5 分钟超时」会被退避悄悄拉长。
|
|
839
|
+
await sleep(Math.min(wait, deadline - Date.now()));
|
|
840
|
+
wait = nextComfyPollDelay(wait);
|
|
565
841
|
const poll = await request(base + '/history/' + encodeURIComponent(promptId), {
|
|
566
842
|
headers: headersFor(provider, false), signal
|
|
567
843
|
});
|
|
@@ -652,6 +928,20 @@ export function applyWorkflowTemplate(template, values, options) {
|
|
|
652
928
|
return walk(template);
|
|
653
929
|
}
|
|
654
930
|
|
|
931
|
+
/**
|
|
932
|
+
* 这个模型的尺寸能不能落到请求里。
|
|
933
|
+
* 自定义工作流既没有 {{width}} 占位符、映射里也没有宽高时,尺寸写不进去——
|
|
934
|
+
* 调用方据此改走「把尺寸写进提示词」,而不是静默丢掉。
|
|
935
|
+
*/
|
|
936
|
+
export function comfySizeWritable(provider, model) {
|
|
937
|
+
const raw = comfyCustomWorkflow(provider, model);
|
|
938
|
+
if (!raw) return true;
|
|
939
|
+
if (/\{\{\s*width\s*\}\}/.test(raw) && /\{\{\s*height\s*\}\}/.test(raw)) return true;
|
|
940
|
+
const entry = provider && provider.modelOptions ? provider.modelOptions[model] : null;
|
|
941
|
+
const mapping = entry && entry.mapping ? entry.mapping : null;
|
|
942
|
+
return !!(mapping && mapping.width && mapping.height);
|
|
943
|
+
}
|
|
944
|
+
|
|
655
945
|
/** 该模型有没有配置自定义工作流;没有就返回空串,走内置图。 */
|
|
656
946
|
function comfyCustomWorkflow(provider, model) {
|
|
657
947
|
const options = provider && provider.modelOptions;
|
|
@@ -736,6 +1026,30 @@ async function comfyCustomGraph(base, provider, { kind, input, model, width, hei
|
|
|
736
1026
|
return { graph: out, note: '' };
|
|
737
1027
|
}
|
|
738
1028
|
|
|
1029
|
+
/** A1111 的采样参数:默认全部不暴露,用户在模型选项里逐项打开。 */
|
|
1030
|
+
export const A1111_PARAMS = [
|
|
1031
|
+
{ name: 'steps', type: 'INT' },
|
|
1032
|
+
{ name: 'cfg_scale', type: 'FLOAT' },
|
|
1033
|
+
{ name: 'sampler_name', type: 'STRING' },
|
|
1034
|
+
{ name: 'scheduler', type: 'STRING' },
|
|
1035
|
+
{ name: 'seed', type: 'INT' },
|
|
1036
|
+
{ name: 'denoising_strength', type: 'FLOAT' }
|
|
1037
|
+
];
|
|
1038
|
+
|
|
1039
|
+
/** 只有图生图才认的参数。 */
|
|
1040
|
+
const A1111_EDIT_ONLY = new Set(['denoising_strength']);
|
|
1041
|
+
|
|
1042
|
+
function coerceA1111Param(type, raw) {
|
|
1043
|
+
if (type === 'STRING') {
|
|
1044
|
+
const text = String(raw === undefined || raw === null ? '' : raw).trim();
|
|
1045
|
+
return text ? text : undefined;
|
|
1046
|
+
}
|
|
1047
|
+
const value = typeof raw === 'number' ? raw : Number(String(raw).trim());
|
|
1048
|
+
if (!Number.isFinite(value)) return undefined;
|
|
1049
|
+
if (type === 'INT') return Number.isInteger(value) ? value : Math.round(value);
|
|
1050
|
+
return value;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
739
1053
|
const ADAPTERS = [
|
|
740
1054
|
{
|
|
741
1055
|
id: 'dall-e-3',
|
|
@@ -791,7 +1105,31 @@ const ADAPTERS = [
|
|
|
791
1105
|
local: false,
|
|
792
1106
|
endpoints: { generate: '/models', edit: '/models' },
|
|
793
1107
|
transport: { generate: 'json', edit: 'json' },
|
|
794
|
-
size: {
|
|
1108
|
+
size: {
|
|
1109
|
+
control: 'aspect', style: 'aspect', field: 'aspect_ratio', default: '1024x1024', onUnsupported: 'nearest',
|
|
1110
|
+
aspects: ['1:1', '1:4', '4:1', '1:8', '8:1', '2:3', '3:2', '3:4', '4:3', '4:5', '5:4', '9:16', '16:9', '21:9']
|
|
1111
|
+
},
|
|
1112
|
+
// imageSize 大小写敏感(必须大写 K),且 512 只有 3.1-flash-image 认。
|
|
1113
|
+
sizeByModel: [
|
|
1114
|
+
{
|
|
1115
|
+
match: /3\.1-flash-image/i,
|
|
1116
|
+
size: {
|
|
1117
|
+
tiers: [
|
|
1118
|
+
{ value: '512', pixels: 262144 }, { value: '1K', pixels: 1048576 },
|
|
1119
|
+
{ value: '2K', pixels: 4194304 }, { value: '4K', pixels: 16777216 }
|
|
1120
|
+
]
|
|
1121
|
+
}
|
|
1122
|
+
},
|
|
1123
|
+
{
|
|
1124
|
+
match: /nano-banana|3-pro-image/i,
|
|
1125
|
+
size: {
|
|
1126
|
+
tiers: [
|
|
1127
|
+
{ value: '1K', pixels: 1048576 }, { value: '2K', pixels: 4194304 }, { value: '4K', pixels: 16777216 }
|
|
1128
|
+
]
|
|
1129
|
+
}
|
|
1130
|
+
},
|
|
1131
|
+
{ match: /2\.5-flash-image/i, size: { wire: [], hint: 'noSizeField' } }
|
|
1132
|
+
],
|
|
795
1133
|
maxN: 1,
|
|
796
1134
|
async listModels(provider, signal) {
|
|
797
1135
|
return (await this.listModelEntries(provider, signal)).map((entry) => entry.id);
|
|
@@ -841,16 +1179,17 @@ const ADAPTERS = [
|
|
|
841
1179
|
let prompt = String(input.prompt || '');
|
|
842
1180
|
if (input.negativePrompt) prompt += '\n\nAvoid: ' + String(input.negativePrompt);
|
|
843
1181
|
parts.push({ text: prompt });
|
|
844
|
-
|
|
845
|
-
const
|
|
846
|
-
const
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
1182
|
+
// 自动尺寸:不下发 imageConfig,让模型自己决定比例与档位。
|
|
1183
|
+
const explicit = !!(input.fitted && !input.fitted.omit);
|
|
1184
|
+
const width = explicit ? input.fitted.width : 0;
|
|
1185
|
+
const height = explicit ? input.fitted.height : 0;
|
|
1186
|
+
const generationConfig = { responseModalities: ['TEXT', 'IMAGE'] };
|
|
1187
|
+
if (explicit) {
|
|
1188
|
+
const aspect = input.fitted.aspect || nearestAspect(width, height, is25 ? ASPECT_CHOICES : ASPECT_CHOICES_14);
|
|
1189
|
+
const imageConfig = { aspectRatio: aspect };
|
|
1190
|
+
// 档位与大小写由策略给出(512 只有 3.1-flash 认,必须大写 K)。
|
|
1191
|
+
if (input.fitted.tier) imageConfig.imageSize = String(input.fitted.tier);
|
|
1192
|
+
generationConfig.imageConfig = imageConfig;
|
|
854
1193
|
}
|
|
855
1194
|
const response = await request(baseUrlOf(provider) + '/models/' + encodeURIComponent(model) + ':generateContent', {
|
|
856
1195
|
method: 'POST',
|
|
@@ -881,7 +1220,12 @@ const ADAPTERS = [
|
|
|
881
1220
|
local: false,
|
|
882
1221
|
endpoints: { generate: '/images/generations', edit: '' },
|
|
883
1222
|
transport: { generate: 'json', edit: 'json' },
|
|
884
|
-
size: {
|
|
1223
|
+
size: {
|
|
1224
|
+
control: 'aspect', style: 'aspect', field: 'aspect_ratio', default: '1024x1024', onUnsupported: 'nearest',
|
|
1225
|
+
aspects: ['1:1', '16:9', '9:16', '4:3', '3:4', '3:2', '2:3', '2:1', '1:2', '19.5:9', '9:19.5', '20:9', '9:20', '21:9', '5:2'],
|
|
1226
|
+
// xAI 的 resolution 是小写,而且只有两档;没有 4k。
|
|
1227
|
+
tiers: [{ value: '1k', pixels: 1048576 }, { value: '2k', pixels: 4194304 }]
|
|
1228
|
+
},
|
|
885
1229
|
maxN: 1,
|
|
886
1230
|
async listModels(provider, signal) {
|
|
887
1231
|
const response = await request(baseUrlOf(provider) + '/models', { headers: headersFor(provider, false), signal });
|
|
@@ -899,10 +1243,13 @@ const ADAPTERS = [
|
|
|
899
1243
|
if (input.negativePrompt) prompt += '\n\nAvoid: ' + String(input.negativePrompt);
|
|
900
1244
|
const body = {
|
|
901
1245
|
model: input.model, prompt: prompt, n: 1,
|
|
902
|
-
response_format: 'url'
|
|
903
|
-
aspect_ratio: nearestAspect(width, height),
|
|
904
|
-
resolution: resolutionFor(width, height)
|
|
1246
|
+
response_format: 'url'
|
|
905
1247
|
};
|
|
1248
|
+
// 自动尺寸:不下发比例与档位,让 Grok 自己决定。
|
|
1249
|
+
if (input.fitted && !input.fitted.omit) {
|
|
1250
|
+
body.aspect_ratio = input.fitted.aspect || nearestAspect(width, height);
|
|
1251
|
+
if (input.fitted.tier) body.resolution = String(input.fitted.tier);
|
|
1252
|
+
}
|
|
906
1253
|
const response = await request(baseUrlOf(provider) + '/images/generations', {
|
|
907
1254
|
method: 'POST', headers: headersFor(provider, false), body: JSON.stringify(body), signal
|
|
908
1255
|
});
|
|
@@ -922,7 +1269,44 @@ const ADAPTERS = [
|
|
|
922
1269
|
transport: { generate: 'json', edit: 'multipart' },
|
|
923
1270
|
editFields: ['image'],
|
|
924
1271
|
editExtras: { strength: 0.5 },
|
|
925
|
-
|
|
1272
|
+
// size 只认「该模型表内的 WxH」或任意比例;三族像素表完全不同,表外值直接 400。
|
|
1273
|
+
size: {
|
|
1274
|
+
control: 'preset', style: 'string', field: 'size', allowCustom: false, onUnsupported: 'nearest',
|
|
1275
|
+
allowed: [
|
|
1276
|
+
'1024x1024', '1536x768', '768x1536', '1280x832', '832x1280', '1216x896', '896x1216',
|
|
1277
|
+
'1152x896', '896x1152', '832x1344', '1280x896', '896x1280', '1344x768', '768x1344'
|
|
1278
|
+
]
|
|
1279
|
+
},
|
|
1280
|
+
sizeByModel: [
|
|
1281
|
+
{
|
|
1282
|
+
match: /_pro$/i,
|
|
1283
|
+
size: {
|
|
1284
|
+
control: 'preset', style: 'string', field: 'size', allowCustom: false,
|
|
1285
|
+
allowed: [
|
|
1286
|
+
'2048x2048', '3072x1536', '1536x3072', '2560x1664', '1664x2560', '2432x1792', '1792x2432',
|
|
1287
|
+
'2304x1792', '1792x2304', '1664x2688', '2560x1792', '1792x2560', '2688x1536', '1536x2688'
|
|
1288
|
+
]
|
|
1289
|
+
}
|
|
1290
|
+
},
|
|
1291
|
+
{
|
|
1292
|
+
match: /recraftv[23]|v[23]_/i,
|
|
1293
|
+
size: {
|
|
1294
|
+
control: 'preset', style: 'string', field: 'size', allowCustom: false,
|
|
1295
|
+
allowed: [
|
|
1296
|
+
'1024x1024', '2048x1024', '1024x2048', '1536x1024', '1024x1536', '1365x1024', '1024x1365',
|
|
1297
|
+
'1280x1024', '1024x1280', '1024x1707', '1434x1024', '1024x1434', '1820x1024', '1024x1820'
|
|
1298
|
+
]
|
|
1299
|
+
}
|
|
1300
|
+
},
|
|
1301
|
+
{
|
|
1302
|
+
match: /_vector/i,
|
|
1303
|
+
size: {
|
|
1304
|
+
control: 'aspect', style: 'string', field: 'size',
|
|
1305
|
+
aspects: ['1:1', '2:1', '1:2', '3:2', '2:3', '4:3', '3:4', '5:4', '4:5', '16:9', '9:16'],
|
|
1306
|
+
allowCustom: false
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
],
|
|
926
1310
|
maxN: 1,
|
|
927
1311
|
async listModels() {
|
|
928
1312
|
return ['recraftv4_1', 'recraftv4', 'recraftv3', 'recraftv2'];
|
|
@@ -964,7 +1348,47 @@ const ADAPTERS = [
|
|
|
964
1348
|
local: false,
|
|
965
1349
|
endpoints: { generate: (input) => ideogramPath(input), edit: '' },
|
|
966
1350
|
transport: { generate: 'multipart', edit: 'json' },
|
|
967
|
-
|
|
1351
|
+
// 三个模型三种协议:3 收比例(字母 x),4 只收 resolution 像素枚举,4-5 只收 size 宽高。
|
|
1352
|
+
size: {
|
|
1353
|
+
control: 'aspect', style: 'aspect', field: 'aspect_ratio', default: '1024x1024', onUnsupported: 'nearest',
|
|
1354
|
+
aspects: ['auto', '1x3', '3x1', '1x2', '2x1', '9x16', '16x9', '10x16', '16x10', '2x3', '3x2', '3x4', '4x3', '4x5', '5x4', '1x1']
|
|
1355
|
+
},
|
|
1356
|
+
sizeByModel: [
|
|
1357
|
+
{
|
|
1358
|
+
// 透明背景模型:比例 + 小写档位(v2 spec 是 1k/2k/4k/8k;v1 spec 写成大写,
|
|
1359
|
+
// 两边官方自相矛盾,这里按 v2 端点的小写来发)。
|
|
1360
|
+
match: /ideogram-4-transparent/i,
|
|
1361
|
+
size: {
|
|
1362
|
+
control: 'aspect', style: 'aspect', field: 'aspect_ratio',
|
|
1363
|
+
aspects: ['auto', '1x4', '1x3', '1x2', '9x16', '10x16', '2x3', '3x4', '4x5', '1x1', '5x4', '4x3', '3x2', '16x10', '16x9', '2x1', '3x1', '4x1'],
|
|
1364
|
+
tiers: [
|
|
1365
|
+
{ value: '1k', pixels: 1048576 }, { value: '2k', pixels: 4194304 },
|
|
1366
|
+
{ value: '4k', pixels: 16777216 }, { value: '8k', pixels: 67108864 }
|
|
1367
|
+
]
|
|
1368
|
+
}
|
|
1369
|
+
},
|
|
1370
|
+
{
|
|
1371
|
+
match: /ideogram-4-5/i,
|
|
1372
|
+
size: {
|
|
1373
|
+
control: 'wh', style: 'string', field: 'size', allowCustom: true,
|
|
1374
|
+
multiple: 32, minSide: 256, maxPixels: 4194304
|
|
1375
|
+
}
|
|
1376
|
+
},
|
|
1377
|
+
{
|
|
1378
|
+
match: /ideogram-4/i,
|
|
1379
|
+
size: {
|
|
1380
|
+
control: 'preset', style: 'string', field: 'resolution', allowCustom: false,
|
|
1381
|
+
allowed: [
|
|
1382
|
+
'2048x2048', '1440x2880', '2880x1440', '1664x2496', '2496x1664', '1792x2240', '2240x1792',
|
|
1383
|
+
'1440x2560', '2560x1440', '1600x2560', '2560x1600', '1728x2304', '2304x1728', '1296x3168',
|
|
1384
|
+
'3168x1296', '1152x2944', '2944x1152', '1248x3328', '3328x1248', '1280x3072', '3072x1280',
|
|
1385
|
+
'1024x3072', '3072x1024', '1024x1024', '896x1120', '1120x896', '864x1152', '1152x864',
|
|
1386
|
+
'832x1248', '1248x832', '800x1280', '1280x800', '720x1280', '1280x720', '720x1440', '1440x720',
|
|
1387
|
+
'512x1536', '1536x512'
|
|
1388
|
+
]
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
],
|
|
968
1392
|
quality: {
|
|
969
1393
|
label: '质量', default: 'default', fallback: 'default',
|
|
970
1394
|
allowed: ['turbo', 'default', 'quality'],
|
|
@@ -978,25 +1402,55 @@ const ADAPTERS = [
|
|
|
978
1402
|
},
|
|
979
1403
|
maxN: 1,
|
|
980
1404
|
async listModels() {
|
|
981
|
-
return ['ideogram-3', 'ideogram-4'];
|
|
1405
|
+
return ['ideogram-3', 'ideogram-4', 'ideogram-4-5', 'ideogram-4-transparent'];
|
|
982
1406
|
},
|
|
983
1407
|
async execute(kind, provider, input, signal) {
|
|
984
1408
|
if (kind === 'edit') throw new Error('Ideogram 原生编辑暂未支持;请使用生成或换支持编辑的模型。');
|
|
985
|
-
const
|
|
986
|
-
|
|
987
|
-
|
|
1409
|
+
const model = String(input.model || 'ideogram-3');
|
|
1410
|
+
const fitted = input.fitted && !input.fitted.omit ? input.fitted : null;
|
|
1411
|
+
const prompt = String(input.prompt || '');
|
|
988
1412
|
const speed = input.quality && input.quality !== 'off'
|
|
989
1413
|
? ({ turbo: 'TURBO', default: 'DEFAULT', quality: 'QUALITY' })[String(input.quality).toLowerCase()]
|
|
990
1414
|
: '';
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
const
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
body:
|
|
998
|
-
|
|
999
|
-
|
|
1415
|
+
let response;
|
|
1416
|
+
const isTransparent = /ideogram-4-transparent/i.test(model);
|
|
1417
|
+
const is45 = /ideogram-4-5/i.test(model);
|
|
1418
|
+
const is4 = !isTransparent && !is45 && /ideogram-4/i.test(model);
|
|
1419
|
+
if (is4) {
|
|
1420
|
+
// ideogram-4 是 JSON,而且只有 resolution(像素枚举),没有 aspect_ratio。
|
|
1421
|
+
const body = { prompt: prompt, num_images: 1 };
|
|
1422
|
+
if (fitted && fitted.size) body.resolution = String(fitted.size);
|
|
1423
|
+
if (speed) body.rendering_speed = speed;
|
|
1424
|
+
response = await request(baseUrlOf(provider) + ideogramPath(input), {
|
|
1425
|
+
method: 'POST',
|
|
1426
|
+
headers: keyedHeaders(provider, 'Api-Key', false),
|
|
1427
|
+
body: JSON.stringify(body),
|
|
1428
|
+
signal: signal
|
|
1429
|
+
});
|
|
1430
|
+
} else {
|
|
1431
|
+
const form = new FormData();
|
|
1432
|
+
form.append('prompt', prompt);
|
|
1433
|
+
if (input.negativePrompt) form.append('negative_prompt', String(input.negativePrompt));
|
|
1434
|
+
if (speed) form.append('rendering_speed', speed);
|
|
1435
|
+
if (fitted) {
|
|
1436
|
+
if (is45) {
|
|
1437
|
+
// ideogram-4-5 只收 size:auto / source / WxH(32 的倍数)。
|
|
1438
|
+
form.append('size', String(fitted.size));
|
|
1439
|
+
} else {
|
|
1440
|
+
// ideogram-3 与 transparent 的比例是字母 x(16x9),不是冒号。
|
|
1441
|
+
const aspect = fitted.aspect || nearestAspect(fitted.width, fitted.height);
|
|
1442
|
+
form.append('aspect_ratio', String(aspect).replace(':', 'x'));
|
|
1443
|
+
if (isTransparent && fitted.tier) form.append('output_resolution', String(fitted.tier));
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
form.append('num_images', '1');
|
|
1447
|
+
response = await request(baseUrlOf(provider) + ideogramPath(input), {
|
|
1448
|
+
method: 'POST',
|
|
1449
|
+
headers: keyedHeaders(provider, 'Api-Key', true),
|
|
1450
|
+
body: form,
|
|
1451
|
+
signal: signal
|
|
1452
|
+
});
|
|
1453
|
+
}
|
|
1000
1454
|
const payload = await readJsonResponse(response, 32 * 1024 * 1024);
|
|
1001
1455
|
const decoded = decodeImagePayload(payload, null);
|
|
1002
1456
|
if (decoded.bytes) return decoded;
|
|
@@ -1022,7 +1476,21 @@ const ADAPTERS = [
|
|
|
1022
1476
|
endpoints: { generate: '/images/generations', edit: '/images/generations' },
|
|
1023
1477
|
transport: { generate: 'json', edit: 'json' },
|
|
1024
1478
|
send: ['model', 'prompt', 'negative_prompt', 'image_size', 'num_inference_steps', 'guidance_scale', 'seed'],
|
|
1025
|
-
|
|
1479
|
+
// image_size 对 Kolors / Qwen-Image 必填;Qwen-Image-Edit 系列官方明确不收这个字段。
|
|
1480
|
+
size: {
|
|
1481
|
+
control: 'preset', custom: true, style: 'string', field: 'image_size', allowCustom: true,
|
|
1482
|
+
allowed: ['1024x1024', '960x1280', '768x1024', '720x1440', '720x1280']
|
|
1483
|
+
},
|
|
1484
|
+
sizeByModel: [
|
|
1485
|
+
{ match: /qwen-image-edit/i, size: { control: 'wh', wire: [], hint: 'noSizeField' } },
|
|
1486
|
+
{
|
|
1487
|
+
match: /qwen-image/i,
|
|
1488
|
+
size: {
|
|
1489
|
+
control: 'preset', custom: true, allowCustom: true,
|
|
1490
|
+
allowed: ['1328x1328', '1664x928', '928x1664', '1472x1140', '1140x1472', '1584x1056', '1056x1584']
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
],
|
|
1026
1494
|
maxN: 1,
|
|
1027
1495
|
editFields: ['image'],
|
|
1028
1496
|
// Qwen-Image-Edit 系列不接受 image_size,传了会 400。
|
|
@@ -1036,9 +1504,42 @@ const ADAPTERS = [
|
|
|
1036
1504
|
transport: { generate: 'json', edit: 'json' },
|
|
1037
1505
|
send: ['model', 'prompt', 'size', 'response_format', 'watermark', 'n', 'image'],
|
|
1038
1506
|
size: {
|
|
1507
|
+
control: 'preset', custom: true,
|
|
1039
1508
|
style: 'string', field: 'size', default: '1024x1024', onUnsupported: 'nearest',
|
|
1040
|
-
|
|
1509
|
+
tiers: [{ value: '2K', pixels: 2048 * 2048 }, { value: '3K', pixels: 3072 * 3072 }, { value: '4K', pixels: 4096 * 4096 }]
|
|
1041
1510
|
},
|
|
1511
|
+
// 档位集合按模型分叉:5.x 是 1K/1.5K/2K(没有 4K),lite 是 2K/3K/4K,4.5 只有 2K/4K。
|
|
1512
|
+
// 顺序有意义:更具体的(lite)必须排在更宽的(5.x)前面。
|
|
1513
|
+
sizeByModel: [
|
|
1514
|
+
{
|
|
1515
|
+
match: /seedream-?5\.0-lite/i,
|
|
1516
|
+
size: {
|
|
1517
|
+
tiers: [{ value: '2K', pixels: 4194304 }, { value: '3K', pixels: 9437184 }, { value: '4K', pixels: 16777216 }],
|
|
1518
|
+
minPixels: 3686400, maxPixels: 16777216
|
|
1519
|
+
}
|
|
1520
|
+
},
|
|
1521
|
+
{
|
|
1522
|
+
match: /seedream-?5\.[05]/i,
|
|
1523
|
+
size: {
|
|
1524
|
+
tiers: [{ value: '1K', pixels: 1048576 }, { value: '1.5K', pixels: 2359296 }, { value: '2K', pixels: 4194304 }],
|
|
1525
|
+
minPixels: 921600, maxPixels: 4624220
|
|
1526
|
+
}
|
|
1527
|
+
},
|
|
1528
|
+
{
|
|
1529
|
+
match: /seedream-?4\.5/i,
|
|
1530
|
+
size: {
|
|
1531
|
+
tiers: [{ value: '2K', pixels: 4194304 }, { value: '4K', pixels: 16777216 }],
|
|
1532
|
+
minPixels: 3686400, maxPixels: 16777216
|
|
1533
|
+
}
|
|
1534
|
+
},
|
|
1535
|
+
{
|
|
1536
|
+
match: /seedream-?4\.0/i,
|
|
1537
|
+
size: {
|
|
1538
|
+
tiers: [{ value: '1K', pixels: 1048576 }, { value: '2K', pixels: 4194304 }, { value: '4K', pixels: 16777216 }],
|
|
1539
|
+
minPixels: 921600, maxPixels: 16777216
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
],
|
|
1042
1543
|
responseFormat: 'url',
|
|
1043
1544
|
watermark: false,
|
|
1044
1545
|
maxN: 1,
|
|
@@ -1051,7 +1552,12 @@ const ADAPTERS = [
|
|
|
1051
1552
|
endpoints: { generate: '/v2beta/stable-image/generate/core', edit: '/v2beta/stable-image/generate/sd3' },
|
|
1052
1553
|
transport: { generate: 'multipart', edit: 'multipart' },
|
|
1053
1554
|
send: ['prompt', 'negative_prompt', 'aspect_ratio', 'output_format', 'model'],
|
|
1054
|
-
size: {
|
|
1555
|
+
size: {
|
|
1556
|
+
control: 'aspect', style: 'aspect', field: 'aspect_ratio', default: '1024x1024', onUnsupported: 'nearest',
|
|
1557
|
+
aspects: ['21:9', '16:9', '3:2', '5:4', '1:1', '4:5', '2:3', '9:16', '9:21'],
|
|
1558
|
+
// 三个端点都没有像素参数:core 固定 1.5MP,sd3/ultra 固定 1MP。
|
|
1559
|
+
hint: 'fixedPixels'
|
|
1560
|
+
},
|
|
1055
1561
|
quality: null,
|
|
1056
1562
|
binary: true,
|
|
1057
1563
|
maxN: 1,
|
|
@@ -1086,10 +1592,35 @@ const ADAPTERS = [
|
|
|
1086
1592
|
parameters: { size: fields.size, n: fields.n }
|
|
1087
1593
|
}),
|
|
1088
1594
|
size: {
|
|
1089
|
-
style: 'string', field: 'size', separator: '*',
|
|
1090
|
-
|
|
1091
|
-
|
|
1595
|
+
control: 'preset', custom: true, style: 'string', field: 'size', separator: '*',
|
|
1596
|
+
// wan2.6 的总像素区间是 [1280², 1440²];官方推荐的 1104×1472 反而比它自己写的下限小,
|
|
1597
|
+
// 所以只按推荐表走,不按 1638400 硬拒。
|
|
1598
|
+
allowed: ['1280x1280', '1696x960', '960x1696', '1472x1104', '1104x1472'],
|
|
1599
|
+
allowCustom: true, default: '1280x1280', hint: 'modelDecides'
|
|
1092
1600
|
},
|
|
1601
|
+
// 万相里只有 wan2.7 认档位缩写;image-pro 的上限是 4096²,普通版是 2048²。
|
|
1602
|
+
sizeByModel: [
|
|
1603
|
+
{
|
|
1604
|
+
match: /wan2\.7.*pro/i,
|
|
1605
|
+
size: {
|
|
1606
|
+
control: 'preset', custom: true, allowCustom: true,
|
|
1607
|
+
tiers: [
|
|
1608
|
+
{ value: '1K', pixels: 1048576 }, { value: '2K', pixels: 4194304 }, { value: '4K', pixels: 16777216 }
|
|
1609
|
+
],
|
|
1610
|
+
minPixels: 589824, maxPixels: 16777216
|
|
1611
|
+
}
|
|
1612
|
+
},
|
|
1613
|
+
{
|
|
1614
|
+
match: /wan2\.7/i,
|
|
1615
|
+
size: {
|
|
1616
|
+
control: 'preset', custom: true, allowCustom: true,
|
|
1617
|
+
tiers: [
|
|
1618
|
+
{ value: '1K', pixels: 1048576 }, { value: '2K', pixels: 4194304 }, { value: '4K', pixels: 16777216 }
|
|
1619
|
+
],
|
|
1620
|
+
minPixels: 589824, maxPixels: 4194304
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
],
|
|
1093
1624
|
quality: null,
|
|
1094
1625
|
maxN: 4,
|
|
1095
1626
|
editFields: []
|
|
@@ -1098,8 +1629,7 @@ const ADAPTERS = [
|
|
|
1098
1629
|
id: 'a1111',
|
|
1099
1630
|
label: 'AUTOMATIC1111 / Forge / SD.Next / Draw Things',
|
|
1100
1631
|
match: (provider) => /a1111|forge|sdnext|sd-next|drawthings/i.test(String((provider && provider.adapter) || '')),
|
|
1101
|
-
local: true,
|
|
1102
|
-
endpoints: { generate: '/sdapi/v1/txt2img', edit: '/sdapi/v1/img2img' },
|
|
1632
|
+
local: true, endpoints: { generate: '/sdapi/v1/txt2img', edit: '/sdapi/v1/img2img' },
|
|
1103
1633
|
transport: { generate: 'json', edit: 'json' },
|
|
1104
1634
|
size: { style: 'string', field: 'size', multiple: 1, default: '1024x1024', onUnsupported: 'nearest' },
|
|
1105
1635
|
async listModels(provider, signal) {
|
|
@@ -1113,9 +1643,10 @@ const ADAPTERS = [
|
|
|
1113
1643
|
.filter(Boolean);
|
|
1114
1644
|
},
|
|
1115
1645
|
async execute(kind, provider, input, signal) {
|
|
1646
|
+
// 自动尺寸:请求里不带宽高,让 WebUI 用它自己的默认值。
|
|
1116
1647
|
const size = input.fitted && !input.fitted.omit
|
|
1117
1648
|
? { width: input.fitted.width, height: input.fitted.height }
|
|
1118
|
-
: {
|
|
1649
|
+
: {};
|
|
1119
1650
|
const body = {
|
|
1120
1651
|
prompt: input.prompt,
|
|
1121
1652
|
negative_prompt: input.negativePrompt || '',
|
|
@@ -1135,6 +1666,21 @@ const ADAPTERS = [
|
|
|
1135
1666
|
body.height = dimensions.height;
|
|
1136
1667
|
endpoint = '/sdapi/v1/img2img';
|
|
1137
1668
|
}
|
|
1669
|
+
// 对话模型显式传入的采样参数:认得的覆盖,认不得的回报。
|
|
1670
|
+
const overrides = input.params && typeof input.params === 'object' ? input.params : null;
|
|
1671
|
+
let note = '';
|
|
1672
|
+
if (overrides) {
|
|
1673
|
+
const known = new Map(A1111_PARAMS.map((param) => [param.name, param]));
|
|
1674
|
+
const ignored = [];
|
|
1675
|
+
for (const [name, raw] of Object.entries(overrides)) {
|
|
1676
|
+
const param = known.get(name);
|
|
1677
|
+
if (!param || (kind !== 'edit' && A1111_EDIT_ONLY.has(name))) { ignored.push(name); continue; }
|
|
1678
|
+
const value = coerceA1111Param(param.type, raw);
|
|
1679
|
+
if (value === undefined) { ignored.push(name); continue; }
|
|
1680
|
+
body[name] = value;
|
|
1681
|
+
}
|
|
1682
|
+
if (ignored.length) note = '这些参数当前模型不支持或取值无效,已忽略:' + ignored.join('、');
|
|
1683
|
+
}
|
|
1138
1684
|
const response = await request(baseUrlOf(provider) + endpoint, {
|
|
1139
1685
|
method: 'POST', headers: headersFor(provider, false), body: JSON.stringify(body), signal
|
|
1140
1686
|
});
|
|
@@ -1142,7 +1688,7 @@ const ADAPTERS = [
|
|
|
1142
1688
|
const first = payload && Array.isArray(payload.images) ? payload.images[0] : null;
|
|
1143
1689
|
const decoded = base64ToImage(first);
|
|
1144
1690
|
if (!decoded) throw new Error('A1111 没有返回图片');
|
|
1145
|
-
return decoded;
|
|
1691
|
+
return note ? Object.assign({}, decoded, { note }) : decoded;
|
|
1146
1692
|
}
|
|
1147
1693
|
},
|
|
1148
1694
|
{
|
|
@@ -1161,8 +1707,13 @@ const ADAPTERS = [
|
|
|
1161
1707
|
},
|
|
1162
1708
|
async execute(kind, provider, input, signal) {
|
|
1163
1709
|
const base = baseUrlOf(provider);
|
|
1164
|
-
|
|
1165
|
-
const
|
|
1710
|
+
// 尺寸=自动且模型没指定宽高时不注入,把分辨率留给工作流自己的节点。
|
|
1711
|
+
const explicit = !!(input.fitted && !input.fitted.omit);
|
|
1712
|
+
const width = explicit ? input.fitted.width : undefined;
|
|
1713
|
+
const height = explicit ? input.fitted.height : undefined;
|
|
1714
|
+
// 内置模板由插件拼装,必须有具体数字,自动尺寸下退回 1024。
|
|
1715
|
+
const builtinWidth = explicit ? width : 1024;
|
|
1716
|
+
const builtinHeight = explicit ? height : 1024;
|
|
1166
1717
|
const seed = Math.floor(Math.random() * 2147483647);
|
|
1167
1718
|
const model = input.model;
|
|
1168
1719
|
|
|
@@ -1179,9 +1730,9 @@ const ADAPTERS = [
|
|
|
1179
1730
|
if (diffusionModels.includes(model) && /moss/i.test(model)) {
|
|
1180
1731
|
workflow = kind === 'edit'
|
|
1181
1732
|
? await comfyMossEditWorkflow(base, provider, { input, model, seed, signal })
|
|
1182
|
-
: await comfyMossWorkflow(base, provider, { input, model, width, height, seed, signal });
|
|
1733
|
+
: await comfyMossWorkflow(base, provider, { input, model, width: builtinWidth, height: builtinHeight, seed, signal });
|
|
1183
1734
|
} else if (diffusionModels.includes(model)) {
|
|
1184
|
-
workflow = await comfyFluxWorkflow(base, provider, { kind, input, model, width, height, seed, signal });
|
|
1735
|
+
workflow = await comfyFluxWorkflow(base, provider, { kind, input, model, width: builtinWidth, height: builtinHeight, seed, signal });
|
|
1185
1736
|
} else {
|
|
1186
1737
|
let imageName = null;
|
|
1187
1738
|
if (kind === 'edit') {
|
|
@@ -1189,7 +1740,7 @@ const ADAPTERS = [
|
|
|
1189
1740
|
if (!reference || !reference.buffer) throw new Error('ComfyUI 编辑需要一张参考图');
|
|
1190
1741
|
imageName = await comfyUploadImage(base, provider, reference, signal);
|
|
1191
1742
|
}
|
|
1192
|
-
workflow = comfyCheckpointWorkflow({ kind, input, model, width, height, seed, imageName });
|
|
1743
|
+
workflow = comfyCheckpointWorkflow({ kind, input, model, width: builtinWidth, height: builtinHeight, seed, imageName });
|
|
1193
1744
|
}
|
|
1194
1745
|
return comfySubmitAndWait(base, provider, workflow, signal);
|
|
1195
1746
|
}
|
|
@@ -1223,14 +1774,17 @@ const ADAPTERS = [
|
|
|
1223
1774
|
if (kind === 'edit') throw new Error('SwarmUI 原生编辑暂未支持;请改用支持 /images/edits 的 OpenAI 兼容网关');
|
|
1224
1775
|
const base = baseUrlOf(provider);
|
|
1225
1776
|
const session = await this.newSession(provider, signal);
|
|
1226
|
-
const width = input.fitted && !input.fitted.omit ? input.fitted.width : 1024;
|
|
1227
|
-
const height = input.fitted && !input.fitted.omit ? input.fitted.height : 1024;
|
|
1228
1777
|
const body = {
|
|
1229
1778
|
session_id: session, images: 1,
|
|
1230
1779
|
prompt: input.prompt, negativeprompt: input.negativePrompt || '',
|
|
1231
|
-
model: input.model,
|
|
1780
|
+
model: input.model,
|
|
1232
1781
|
cfgscale: 7, steps: 20, seed: -1
|
|
1233
1782
|
};
|
|
1783
|
+
// 自动尺寸:不带宽高,让 SwarmUI 用它自己的默认值。
|
|
1784
|
+
if (input.fitted && !input.fitted.omit) {
|
|
1785
|
+
body.width = input.fitted.width;
|
|
1786
|
+
body.height = input.fitted.height;
|
|
1787
|
+
}
|
|
1234
1788
|
const generated = await readJsonResponse(await request(base + '/API/GenerateText2Image', {
|
|
1235
1789
|
method: 'POST', headers: headersFor(provider, false), body: JSON.stringify(body), signal
|
|
1236
1790
|
}), 5 * 1024 * 1024);
|
|
@@ -1274,10 +1828,12 @@ const ADAPTERS = [
|
|
|
1274
1828
|
...OPENAI_ADAPTERS,
|
|
1275
1829
|
{
|
|
1276
1830
|
id: 'openai-images',
|
|
1277
|
-
label: 'OpenAI Images
|
|
1831
|
+
label: 'OpenAI Images 兼容(自动同步/异步)',
|
|
1278
1832
|
match: () => true,
|
|
1279
1833
|
endpoints: { generate: '/images/generations', edit: '/images/edits' },
|
|
1280
1834
|
transport: { generate: 'json', edit: 'multipart' },
|
|
1835
|
+
// 提交响应带任务 id / 轮询地址时自动跟着轮询;直接返回图片就照同步处理。
|
|
1836
|
+
autoAsync: true,
|
|
1281
1837
|
send: ['model', 'prompt', 'negative_prompt', 'size', 'quality', 'n'],
|
|
1282
1838
|
// 通用兼容族:尺寸原样透传(保持旧行为),被上游拒了再靠重试往上抬。
|
|
1283
1839
|
size: { style: 'string', field: 'size', multiple: 1, default: '1024x1024', onUnsupported: 'nearest' },
|
|
@@ -1379,7 +1935,7 @@ export const PROVIDER_PRESETS = [
|
|
|
1379
1935
|
id: 'ideogram', name: 'Ideogram',
|
|
1380
1936
|
baseUrl: 'https://api.ideogram.ai', adapter: 'ideogram',
|
|
1381
1937
|
website: 'https://developer.ideogram.ai/',
|
|
1382
|
-
models: ['ideogram-3']
|
|
1938
|
+
models: ['ideogram-3', 'ideogram-4']
|
|
1383
1939
|
},
|
|
1384
1940
|
{
|
|
1385
1941
|
id: 'stability', name: 'Stability AI',
|
|
@@ -1452,10 +2008,16 @@ export const PROVIDER_PRESETS = [
|
|
|
1452
2008
|
id: 'dashscope-wanx', name: '阿里云百炼 · 通义万相',
|
|
1453
2009
|
baseUrl: 'https://dashscope.aliyuncs.com/api/v1', adapter: 'dashscope-wanx',
|
|
1454
2010
|
website: 'https://bailian.console.aliyun.com/',
|
|
1455
|
-
models: ['wan2.6-t2i', 'wanx2.1-t2i-turbo']
|
|
2011
|
+
models: ['wan2.6-t2i', 'wan2.7-image-pro', 'wan2.7-image', 'wanx2.1-t2i-turbo']
|
|
1456
2012
|
},
|
|
1457
2013
|
{ id: 'siliconflow', name: '硅基流动', baseUrl: 'https://api.siliconflow.cn/v1', adapter: '', website: 'https://cloud.siliconflow.cn/account/ak', models: [] },
|
|
1458
|
-
{ id: 'bigmodel', name: '智谱 BigModel', baseUrl: 'https://open.bigmodel.cn/api/paas/v4', adapter: 'zhipu', website: 'https://bigmodel.cn/usercenter/apikeys', models: [] }
|
|
2014
|
+
{ id: 'bigmodel', name: '智谱 BigModel', baseUrl: 'https://open.bigmodel.cn/api/paas/v4', adapter: 'zhipu', website: 'https://bigmodel.cn/usercenter/apikeys', models: [] },
|
|
2015
|
+
{
|
|
2016
|
+
id: 'modelscope', name: '魔搭 ModelScope',
|
|
2017
|
+
baseUrl: 'https://api-inference.modelscope.cn', adapter: 'modelscope',
|
|
2018
|
+
website: 'https://www.modelscope.cn/my/myaccesstoken',
|
|
2019
|
+
models: ['Qwen/Qwen-Image-2.1', 'Qwen/Qwen-Image-2.1-Turbo', 'Qwen/Qwen-Image-Edit']
|
|
2020
|
+
}
|
|
1459
2021
|
];
|
|
1460
2022
|
|
|
1461
2023
|
export const LOCAL_PRESETS = [
|
|
@@ -1586,7 +2148,7 @@ function pickFields(adapter, candidate, drops) {
|
|
|
1586
2148
|
}
|
|
1587
2149
|
|
|
1588
2150
|
function buildFields(adapter, input) {
|
|
1589
|
-
const sizeFields = encodeSize(adapter, input.fitted);
|
|
2151
|
+
const sizeFields = encodeSize(adapter, input.fitted, input.model);
|
|
1590
2152
|
return Object.assign({
|
|
1591
2153
|
model: input.model,
|
|
1592
2154
|
prompt: input.prompt,
|
|
@@ -1655,18 +2217,134 @@ export function prepareAsync(adapter, provider, input, drops) {
|
|
|
1655
2217
|
return jsonRequest(adapter, provider, adapter.submit, composeBody(adapter, input, drops));
|
|
1656
2218
|
}
|
|
1657
2219
|
|
|
1658
|
-
/**
|
|
1659
|
-
|
|
2220
|
+
/**
|
|
2221
|
+
* 这些字段是请求的语义核心:少了提示词、模型或参考图,请求就不再是「同一个请求」,
|
|
2222
|
+
* 上游就算收下也只会生成一张不相干的图。兼容性重试只允许摘掉可选参数。
|
|
2223
|
+
*/
|
|
2224
|
+
const PHASE_PROTECTED_FIELDS = [
|
|
2225
|
+
'prompt', 'model',
|
|
2226
|
+
'input.prompt', 'input.model',
|
|
2227
|
+
'images', 'image', 'image_url', 'image_urls', 'input_image',
|
|
2228
|
+
'input.image', 'input.images', 'input.image_url', 'input.input_image'
|
|
2229
|
+
];
|
|
2230
|
+
|
|
2231
|
+
/** BFL 的多图字段是 input_image_2/3/4,和 input_image 同类,删掉等于丢参考图。 */
|
|
2232
|
+
const PHASE_PROTECTED_SIBLING = /^input_image_\d+$/i;
|
|
2233
|
+
|
|
2234
|
+
/**
|
|
2235
|
+
* drop 是否踩到必要字段。
|
|
2236
|
+
*
|
|
2237
|
+
* 除了同名字段,还要挡两个方向:删父对象(`input`)会连带删掉 `input.prompt`;
|
|
2238
|
+
* 删子路径(`input.prompt.text`)等价于改必要字段。两者都按「不生效」处理。
|
|
2239
|
+
*/
|
|
2240
|
+
function isProtectedPhaseDrop(path) {
|
|
2241
|
+
const text = String(path || '');
|
|
2242
|
+
if (!text) return false;
|
|
2243
|
+
if (PHASE_PROTECTED_SIBLING.test(text)) return true;
|
|
2244
|
+
for (const protectedPath of PHASE_PROTECTED_FIELDS) {
|
|
2245
|
+
if (text === protectedPath) return true;
|
|
2246
|
+
if (text.startsWith(protectedPath + '.')) return true;
|
|
2247
|
+
if (protectedPath.startsWith(text + '.')) return true;
|
|
2248
|
+
}
|
|
2249
|
+
return false;
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2252
|
+
/**
|
|
2253
|
+
* 从请求体对象里删掉一条点分路径(`input.width` 这种),返回是否真的删掉了。
|
|
2254
|
+
* 只支持对象层级,不支持数组下标:适配器声明的兼容性字段都是具名字段。
|
|
2255
|
+
*
|
|
2256
|
+
* 路径来自上游错误文本,不能让它摸到原型链:`__proto__.toString` 这种
|
|
2257
|
+
* 会删到 Object.prototype 上,直接把整个进程的字符串化打坏。
|
|
2258
|
+
*/
|
|
2259
|
+
const UNSAFE_PHASE_SEGMENTS = new Set(['__proto__', 'prototype', 'constructor']);
|
|
2260
|
+
|
|
2261
|
+
function ownValue(node, key) {
|
|
2262
|
+
if (!node || (typeof node !== 'object' && typeof node !== 'function')) return undefined;
|
|
2263
|
+
return Object.prototype.hasOwnProperty.call(node, key) ? node[key] : undefined;
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
function dropPhaseField(body, path) {
|
|
2267
|
+
const segments = String(path || '').split('.').filter(Boolean);
|
|
2268
|
+
if (!segments.length) return false;
|
|
2269
|
+
let node = body;
|
|
2270
|
+
for (let index = 0; index < segments.length - 1; index++) {
|
|
2271
|
+
const segment = segments[index];
|
|
2272
|
+
if (UNSAFE_PHASE_SEGMENTS.has(segment)) return false;
|
|
2273
|
+
const next = ownValue(node, segment);
|
|
2274
|
+
// 数组下标不在支持范围内:删元素会留下空洞(JSON 里变成 null),
|
|
2275
|
+
// 上游看到的还是一个坏数组,不如原样报回去。
|
|
2276
|
+
if (!next || typeof next !== 'object' || Array.isArray(next)) return false;
|
|
2277
|
+
node = next;
|
|
2278
|
+
}
|
|
2279
|
+
const leaf = segments[segments.length - 1];
|
|
2280
|
+
if (UNSAFE_PHASE_SEGMENTS.has(leaf)) return false;
|
|
2281
|
+
if (!node || (typeof node !== 'object' && typeof node !== 'function')) return false;
|
|
2282
|
+
if (!Object.prototype.hasOwnProperty.call(node, leaf)) return false;
|
|
2283
|
+
// 不存在的路径不算「删掉了」:调用方要按 dropped 判断请求是否真的变了,
|
|
2284
|
+
// 把无效路径报成 dropped 会骗它去做一次完全相同的重试。
|
|
2285
|
+
// 值为 undefined 的字段本来就进不了 JSON,同样不算真的删了东西。
|
|
2286
|
+
if (node[leaf] === undefined) return false;
|
|
2287
|
+
return delete node[leaf];
|
|
2288
|
+
}
|
|
2289
|
+
|
|
2290
|
+
/**
|
|
2291
|
+
* build 失败时保留原始错误对象:调用方要按 kind/status/cause 决定重试与降级,
|
|
2292
|
+
* 直接包装成新错误会把这些分类和栈丢掉,所以原错误一律挂到 cause 上。
|
|
2293
|
+
* message 保持不变,用户看到的文案不变。
|
|
2294
|
+
*/
|
|
2295
|
+
function phaseBuildError(error) {
|
|
2296
|
+
if (!(error instanceof Error)) {
|
|
2297
|
+
return localizedError(new Error('构造异步请求体失败:' + String(error)), 'upstream.invalidResponse', { phase: 'prepare' });
|
|
2298
|
+
}
|
|
2299
|
+
if (error.cause && error.cause !== error) return error;
|
|
2300
|
+
const wrapped = new Error(error.message, { cause: error });
|
|
2301
|
+
wrapped.name = error.name;
|
|
2302
|
+
for (const key of ['status', 'code', 'kind', 'retryable', 'detail', 'localized']) {
|
|
2303
|
+
if (error[key] !== undefined) wrapped[key] = error[key];
|
|
2304
|
+
}
|
|
2305
|
+
return wrapped;
|
|
2306
|
+
}
|
|
2307
|
+
|
|
2308
|
+
/**
|
|
2309
|
+
* 新式异步描述符:提交体与提交路径都由适配器自己决定;build 可以是异步的。
|
|
2310
|
+
*
|
|
2311
|
+
* drops 是兼容性重试前累积的「上游不认识/不接受」的字段路径(Set 或数组,点分路径)。
|
|
2312
|
+
* 删在对象上再序列化,而不是先 stringify 再 parse:body 里常有 base64 data URL,
|
|
2313
|
+
* 往返一次会多复制一份整张图。build 每次都返回新对象,直接在上面删是安全的。
|
|
2314
|
+
*/
|
|
2315
|
+
export async function preparePhaseAsync(adapter, provider, input, signal, drops) {
|
|
1660
2316
|
const phase = adapter.async || {};
|
|
1661
|
-
|
|
1662
|
-
const
|
|
2317
|
+
// 有的平台提交路径依赖 baseUrl(魔搭带不带 /v1 都要能拼对),所以把 provider 也传进去。
|
|
2318
|
+
const path = String(phase.submitPath(input, provider) || '');
|
|
2319
|
+
let built;
|
|
2320
|
+
try {
|
|
2321
|
+
built = await phase.build(input, provider, signal);
|
|
2322
|
+
} catch (error) {
|
|
2323
|
+
throw phaseBuildError(error);
|
|
2324
|
+
}
|
|
1663
2325
|
const body = built && built.body ? built.body : (built || {});
|
|
1664
2326
|
const note = built && built.note ? String(built.note) : '';
|
|
2327
|
+
const dropped = [];
|
|
2328
|
+
const blockedDrops = [];
|
|
2329
|
+
const wanted = drops instanceof Set ? Array.from(drops) : (Array.isArray(drops) ? drops : []);
|
|
2330
|
+
if (body && typeof body === 'object' && !Array.isArray(body)) {
|
|
2331
|
+
for (const raw of wanted) {
|
|
2332
|
+
const fieldPath = String(raw === undefined || raw === null ? '' : raw).trim();
|
|
2333
|
+
if (!fieldPath) continue;
|
|
2334
|
+
if (isProtectedPhaseDrop(fieldPath)) {
|
|
2335
|
+
if (!blockedDrops.includes(fieldPath)) blockedDrops.push(fieldPath);
|
|
2336
|
+
continue;
|
|
2337
|
+
}
|
|
2338
|
+
if (dropPhaseField(body, fieldPath) && !dropped.includes(fieldPath)) dropped.push(fieldPath);
|
|
2339
|
+
}
|
|
2340
|
+
}
|
|
1665
2341
|
return {
|
|
1666
2342
|
path: path,
|
|
1667
2343
|
headers: headersFor(provider, false, adapter.headers, adapter),
|
|
1668
2344
|
body: JSON.stringify(body),
|
|
1669
|
-
note: note
|
|
2345
|
+
note: note,
|
|
2346
|
+
dropped: dropped,
|
|
2347
|
+
blockedDrops: blockedDrops
|
|
1670
2348
|
};
|
|
1671
2349
|
}
|
|
1672
2350
|
|
|
@@ -1706,19 +2384,64 @@ export function adapterBase(provider) {
|
|
|
1706
2384
|
|
|
1707
2385
|
/* ----------------------------------------------------------------- resize */
|
|
1708
2386
|
|
|
2387
|
+
/** 居中裁切一段 RGBA(cover 用):比例不符时裁掉多余的一边,而不是拉伸。 */
|
|
2388
|
+
function cropRgba(rgba, width, height, left, top, cropWidth, cropHeight) {
|
|
2389
|
+
const out = new Uint8Array(cropWidth * cropHeight * 4);
|
|
2390
|
+
for (let y = 0; y < cropHeight; y++) {
|
|
2391
|
+
const from = ((top + y) * width + left) * 4;
|
|
2392
|
+
out.set(rgba.subarray(from, from + cropWidth * 4), y * cropWidth * 4);
|
|
2393
|
+
}
|
|
2394
|
+
return out;
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2397
|
+
function coverCrop(image, targetWidth, targetHeight) {
|
|
2398
|
+
const targetRatio = targetWidth / targetHeight;
|
|
2399
|
+
const ratio = image.width / image.height;
|
|
2400
|
+
if (Math.abs(ratio - targetRatio) <= 0.01) return null;
|
|
2401
|
+
let cropWidth = image.width;
|
|
2402
|
+
let cropHeight = Math.round(image.width / targetRatio);
|
|
2403
|
+
if (cropHeight > image.height) {
|
|
2404
|
+
cropHeight = image.height;
|
|
2405
|
+
cropWidth = Math.round(image.height * targetRatio);
|
|
2406
|
+
}
|
|
2407
|
+
if (cropWidth <= 0 || cropHeight <= 0) return null;
|
|
2408
|
+
const left = Math.max(0, Math.floor((image.width - cropWidth) / 2));
|
|
2409
|
+
const top = Math.max(0, Math.floor((image.height - cropHeight) / 2));
|
|
2410
|
+
return {
|
|
2411
|
+
width: cropWidth,
|
|
2412
|
+
height: cropHeight,
|
|
2413
|
+
rgba: cropRgba(image.rgba, image.width, image.height, left, top, cropWidth, cropHeight)
|
|
2414
|
+
};
|
|
2415
|
+
}
|
|
2416
|
+
|
|
1709
2417
|
/**
|
|
1710
2418
|
* 生成尺寸被抬大以后,把结果缩回用户要的尺寸。
|
|
1711
2419
|
* PNG 走自带的纯 JS 面积平均缩放(无依赖、任何进程可用);
|
|
2420
|
+
* 'cover' 会先把比例裁成目标比例(平台无视比例时用),默认 'exact' 直接重采样。
|
|
1712
2421
|
* 其它格式再试 Electron 的 nativeImage。
|
|
2422
|
+
*
|
|
2423
|
+
* 目标尺寸来自对话模型可填的 size:分配像素缓冲区前必须先挡住超大值,
|
|
2424
|
+
* 否则 `99999x99999` 会直接申请几十 GB 内存。上限与图片解码的 32MP 预算一致。
|
|
1713
2425
|
*/
|
|
1714
|
-
|
|
2426
|
+
const MAX_RESIZE_SIDE = 16384;
|
|
2427
|
+
const MAX_RESIZE_PIXELS = 32 * 1024 * 1024;
|
|
2428
|
+
|
|
2429
|
+
export function resizeTo(bytes, width, height, mode) {
|
|
1715
2430
|
if (!bytes || !width || !height) return null;
|
|
2431
|
+
if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width <= 0 || height <= 0) return null;
|
|
2432
|
+
if (width > MAX_RESIZE_SIDE || height > MAX_RESIZE_SIDE || width * height > MAX_RESIZE_PIXELS) return null;
|
|
2433
|
+
let cropped = false;
|
|
1716
2434
|
try {
|
|
1717
2435
|
const image = decodePng(bytes);
|
|
1718
2436
|
if (image) {
|
|
1719
|
-
if (image.width === width && image.height === height) return { bytes: bytes, contentType: 'image/png', resized: false };
|
|
1720
|
-
|
|
1721
|
-
|
|
2437
|
+
if (image.width === width && image.height === height) return { bytes: bytes, contentType: 'image/png', resized: false, cropped: false };
|
|
2438
|
+
let source = image;
|
|
2439
|
+
if (mode === 'cover') {
|
|
2440
|
+
const next = coverCrop(image, width, height);
|
|
2441
|
+
if (next) { source = next; cropped = true; }
|
|
2442
|
+
}
|
|
2443
|
+
const rgba = resizeRgba(source.rgba, source.width, source.height, width, height);
|
|
2444
|
+
return { bytes: encodePng(rgba, width, height), contentType: 'image/png', resized: true, cropped: cropped };
|
|
1722
2445
|
}
|
|
1723
2446
|
} catch (_error) {
|
|
1724
2447
|
/* 落到下面的 Electron 通路 */
|
|
@@ -1730,11 +2453,29 @@ export function resizeTo(bytes, width, height) {
|
|
|
1730
2453
|
const image = nativeImage.createFromBuffer(bytes);
|
|
1731
2454
|
if (!image || image.isEmpty()) return null;
|
|
1732
2455
|
const size = image.getSize();
|
|
1733
|
-
if (size.width === width && size.height === height) return { bytes: bytes, contentType: 'image/png', resized: false };
|
|
1734
|
-
|
|
2456
|
+
if (size.width === width && size.height === height) return { bytes: bytes, contentType: 'image/png', resized: false, cropped: false };
|
|
2457
|
+
let source = image;
|
|
2458
|
+
if (mode === 'cover' && size.width && size.height) {
|
|
2459
|
+
const targetRatio = width / height;
|
|
2460
|
+
if (Math.abs(size.width / size.height - targetRatio) > 0.01) {
|
|
2461
|
+
let cropWidth = size.width;
|
|
2462
|
+
let cropHeight = Math.round(size.width / targetRatio);
|
|
2463
|
+
if (cropHeight > size.height) {
|
|
2464
|
+
cropHeight = size.height;
|
|
2465
|
+
cropWidth = Math.round(size.height * targetRatio);
|
|
2466
|
+
}
|
|
2467
|
+
if (cropWidth > 0 && cropHeight > 0) {
|
|
2468
|
+
const left = Math.max(0, Math.floor((size.width - cropWidth) / 2));
|
|
2469
|
+
const top = Math.max(0, Math.floor((size.height - cropHeight) / 2));
|
|
2470
|
+
source = image.crop({ x: left, y: top, width: cropWidth, height: cropHeight });
|
|
2471
|
+
cropped = true;
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
const resized = source.resize({ width: width, height: height, quality: 'best' });
|
|
1735
2476
|
const png = resized.toPNG();
|
|
1736
2477
|
if (!png || !png.length) return null;
|
|
1737
|
-
return { bytes: png, contentType: 'image/png', resized: true };
|
|
2478
|
+
return { bytes: png, contentType: 'image/png', resized: true, cropped: cropped };
|
|
1738
2479
|
} catch (_error) {
|
|
1739
2480
|
return null;
|
|
1740
2481
|
}
|