dsh-mo-image 0.5.1 → 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 +18 -0
- package/README.md +468 -409
- package/icon.png +0 -0
- package/lib/adapter-kit.js +670 -0
- package/lib/adapters/async.js +578 -0
- package/lib/adapters/cn.js +108 -0
- package/lib/adapters/openai.js +252 -0
- package/lib/adapters.js +1112 -454
- package/lib/client.js +1006 -150
- package/lib/comfy-workflow.js +528 -0
- 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 +517 -739
- package/lib/localized-error.js +1 -0
- package/lib/png-scale.js +103 -28
- package/lib/prompt-profiles.js +160 -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 +49 -3
- package/package.json +3 -1
- package/skills/mo-image-editing/SKILL.md +34 -72
- package/skills/mo-image-generation/SKILL.md +59 -152
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI 家族的四个协议。
|
|
3
|
+
*
|
|
4
|
+
* - openai-responses:Responses API 的 image_generation 工具(图片在 output[].result,裸 base64)
|
|
5
|
+
* - openrouter-images:OpenRouter 专用 /api/v1/images(data[].b64_json + media_type)
|
|
6
|
+
* - azure-openai:Azure 的 OpenAI v1 路径,鉴权是 api-key 而不是 Bearer
|
|
7
|
+
* - openai-chat:中转生态的 chat/completions + modalities,响应形态不唯一,要多形态探测
|
|
8
|
+
*/
|
|
9
|
+
import { request } from '../net.js';
|
|
10
|
+
import {
|
|
11
|
+
baseUrlOf, headersFor, readJsonResponse, dataUrlOf, base64ToImage, nearestAspect,
|
|
12
|
+
decodeImagePayload, fetchImageUrl
|
|
13
|
+
} from '../adapter-kit.js';
|
|
14
|
+
|
|
15
|
+
function hostOf(provider) {
|
|
16
|
+
return String((provider && provider.baseUrl) || '').replace(/^https?:\/\//i, '').split('/')[0].toLowerCase();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function sizeTextOf(input) {
|
|
20
|
+
const fitted = input && input.fitted;
|
|
21
|
+
if (!fitted || fitted.omit || fitted.size === 'none' || fitted.size === 'auto') return '';
|
|
22
|
+
if (fitted.width && fitted.height) return fitted.width + 'x' + fitted.height;
|
|
23
|
+
return String(fitted.size || '');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function promptWithNegative(input) {
|
|
27
|
+
const prompt = String((input && input.prompt) || '');
|
|
28
|
+
const negative = String((input && input.negativePrompt) || '').trim();
|
|
29
|
+
return negative ? prompt + '\n\nAvoid: ' + negative : prompt;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function imageParts(images) {
|
|
33
|
+
return (Array.isArray(images) ? images : [])
|
|
34
|
+
.filter((image) => image && image.buffer && image.contentType)
|
|
35
|
+
.map((image) => ({ type: 'image_url', image_url: { url: dataUrlOf(image) } }));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function decodeDataOrUrl(text) {
|
|
39
|
+
if (!text) return null;
|
|
40
|
+
const decoded = base64ToImage(text);
|
|
41
|
+
if (decoded) return decoded;
|
|
42
|
+
if (/^https?:\/\//i.test(String(text))) return { url: String(text) };
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** chat 出图没有统一响应格式:images[] → content 里的 data URL → markdown 链接 → 兜底扫描。 */
|
|
47
|
+
function chatImage(payload) {
|
|
48
|
+
const message = payload && payload.choices && payload.choices[0] && payload.choices[0].message;
|
|
49
|
+
const direct = message && Array.isArray(message.images) ? message.images : [];
|
|
50
|
+
for (const item of direct) {
|
|
51
|
+
const url = (item && item.image_url && item.image_url.url) || (item && item.url) || (typeof item === 'string' ? item : '');
|
|
52
|
+
const decoded = decodeDataOrUrl(url);
|
|
53
|
+
if (decoded) return decoded;
|
|
54
|
+
}
|
|
55
|
+
const content = message && message.content;
|
|
56
|
+
const texts = typeof content === 'string'
|
|
57
|
+
? [content]
|
|
58
|
+
: Array.isArray(content)
|
|
59
|
+
? content.map((part) => (typeof part === 'string' ? part : (part && part.text) || ''))
|
|
60
|
+
: [];
|
|
61
|
+
for (const text of texts) {
|
|
62
|
+
const hit = /data:image\/[a-z+]+;base64,[A-Za-z0-9+/=\r\n]+/i.exec(text);
|
|
63
|
+
if (hit) {
|
|
64
|
+
const decoded = base64ToImage(hit[0]);
|
|
65
|
+
if (decoded) return decoded;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
for (const text of texts) {
|
|
69
|
+
const hit = /https?:\/\/[^\s)"'<>]+\.(?:png|jpe?g|webp)(?:\?[^\s)"'<>]*)?/i.exec(text);
|
|
70
|
+
if (hit) return { url: hit[0] };
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
return decodeImagePayload(payload, null);
|
|
74
|
+
} catch (_error) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const GPT_QUALITY = {
|
|
80
|
+
label: '质量', default: 'high', fallback: 'high',
|
|
81
|
+
allowed: ['low', 'medium', 'high', 'auto'],
|
|
82
|
+
options: [
|
|
83
|
+
{ value: 'off', label: 'Off' },
|
|
84
|
+
{ value: 'low', label: 'Low' },
|
|
85
|
+
{ value: 'medium', label: 'Medium' },
|
|
86
|
+
{ value: 'high', label: 'High' },
|
|
87
|
+
{ value: 'auto', label: 'Auto' }
|
|
88
|
+
]
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export const OPENAI_ADAPTERS = [
|
|
92
|
+
{
|
|
93
|
+
id: 'openai-responses',
|
|
94
|
+
label: 'OpenAI Responses (image_generation)',
|
|
95
|
+
match: () => false,
|
|
96
|
+
local: false,
|
|
97
|
+
endpoints: { generate: '/responses', edit: '/responses' },
|
|
98
|
+
transport: { generate: 'json', edit: 'json' },
|
|
99
|
+
maxN: 1,
|
|
100
|
+
editFields: [],
|
|
101
|
+
async execute(kind, provider, input, signal) {
|
|
102
|
+
const model = String(input.model || '').trim();
|
|
103
|
+
const hostModel = String((provider && provider.hostModel) || 'gpt-5.2').trim();
|
|
104
|
+
const size = sizeTextOf(input);
|
|
105
|
+
const tool = { type: 'image_generation', model: model, ...(size ? { size: size } : {}) };
|
|
106
|
+
const quality = String(input.quality || '').trim().toLowerCase();
|
|
107
|
+
if (quality && quality !== 'off' && quality !== 'auto') tool.quality = quality;
|
|
108
|
+
tool.output_format = 'png';
|
|
109
|
+
|
|
110
|
+
const prompt = promptWithNegative(input);
|
|
111
|
+
const images = (input.images || []).filter((image) => image && image.buffer && image.contentType);
|
|
112
|
+
const payloadInput = (kind === 'edit' || images.length)
|
|
113
|
+
? [{
|
|
114
|
+
role: 'user',
|
|
115
|
+
content: [{ type: 'input_text', text: prompt }].concat(images.map((image) => ({
|
|
116
|
+
type: 'input_image', image_url: dataUrlOf(image), detail: 'auto'
|
|
117
|
+
})))
|
|
118
|
+
}]
|
|
119
|
+
: prompt;
|
|
120
|
+
|
|
121
|
+
const response = await request(baseUrlOf(provider) + '/responses', {
|
|
122
|
+
method: 'POST',
|
|
123
|
+
headers: headersFor(provider, false, null, this),
|
|
124
|
+
body: JSON.stringify({
|
|
125
|
+
model: hostModel,
|
|
126
|
+
input: payloadInput,
|
|
127
|
+
tools: [tool],
|
|
128
|
+
tool_choice: { type: 'image_generation' }
|
|
129
|
+
}),
|
|
130
|
+
signal: signal
|
|
131
|
+
});
|
|
132
|
+
const payload = await readJsonResponse(response, 64 * 1024 * 1024);
|
|
133
|
+
const items = payload && Array.isArray(payload.output) ? payload.output : [];
|
|
134
|
+
const call = items.find((item) => item && item.type === 'image_generation_call' && item.result);
|
|
135
|
+
const decoded = call ? base64ToImage(call.result) : null;
|
|
136
|
+
if (decoded) return decoded;
|
|
137
|
+
throw new Error('Responses API 没有返回图片(检查 tool_choice 与主模型是否支持 image_generation)');
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
id: 'openrouter-images',
|
|
142
|
+
label: 'OpenRouter Images',
|
|
143
|
+
match: (provider) => hostOf(provider) === 'openrouter.ai',
|
|
144
|
+
local: false,
|
|
145
|
+
endpoints: { generate: '/images', edit: '/images' },
|
|
146
|
+
transport: { generate: 'json', edit: 'json' },
|
|
147
|
+
// 各家底层模型能力不同:gemini 系有 resolution,gpt-image / flux.2 只有比例。
|
|
148
|
+
size: { control: 'aspect', style: 'aspect', field: 'aspect_ratio', default: '1024x1024', onUnsupported: 'nearest' },
|
|
149
|
+
sizeByModel: [
|
|
150
|
+
{
|
|
151
|
+
match: /gemini/i,
|
|
152
|
+
size: {
|
|
153
|
+
control: 'aspect', style: 'aspect', field: 'aspect_ratio',
|
|
154
|
+
tiers: [
|
|
155
|
+
{ value: '512', pixels: 262144 }, { value: '1K', pixels: 1048576 },
|
|
156
|
+
{ value: '2K', pixels: 4194304 }, { value: '4K', pixels: 16777216 }
|
|
157
|
+
]
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
],
|
|
161
|
+
maxN: 10,
|
|
162
|
+
editFields: [],
|
|
163
|
+
async listModels(provider, signal) {
|
|
164
|
+
const response = await request(baseUrlOf(provider) + '/images/models', {
|
|
165
|
+
headers: headersFor(provider, false), signal: signal
|
|
166
|
+
});
|
|
167
|
+
const payload = await readJsonResponse(response, 5 * 1024 * 1024);
|
|
168
|
+
return (payload && Array.isArray(payload.data) ? payload.data : [])
|
|
169
|
+
.map((item) => String((item && item.id) || '').trim())
|
|
170
|
+
.filter(Boolean)
|
|
171
|
+
.slice(0, 200);
|
|
172
|
+
},
|
|
173
|
+
async execute(kind, provider, input, signal) {
|
|
174
|
+
const body = {
|
|
175
|
+
model: input.model,
|
|
176
|
+
prompt: promptWithNegative(input),
|
|
177
|
+
n: 1,
|
|
178
|
+
output_format: 'png'
|
|
179
|
+
};
|
|
180
|
+
if (input.fitted && !input.fitted.omit && input.fitted.width && input.fitted.height) {
|
|
181
|
+
body.aspect_ratio = input.fitted.aspect || nearestAspect(input.fitted.width, input.fitted.height);
|
|
182
|
+
// 只有认 resolution 的底层模型才发档位(fitSize 已经填好 tier);发错了 provider 会 400。
|
|
183
|
+
if (input.fitted.tier) body.resolution = String(input.fitted.tier);
|
|
184
|
+
}
|
|
185
|
+
const quality = String(input.quality || '').trim().toLowerCase();
|
|
186
|
+
if (quality && quality !== 'off' && quality !== 'auto') body.quality = quality;
|
|
187
|
+
const references = imageParts(input.images);
|
|
188
|
+
if (references.length) body.input_references = references;
|
|
189
|
+
|
|
190
|
+
const response = await request(baseUrlOf(provider) + '/images', {
|
|
191
|
+
method: 'POST',
|
|
192
|
+
headers: headersFor(provider, false, null, this),
|
|
193
|
+
body: JSON.stringify(body),
|
|
194
|
+
signal: signal
|
|
195
|
+
});
|
|
196
|
+
const payload = await readJsonResponse(response, 64 * 1024 * 1024);
|
|
197
|
+
const first = payload && Array.isArray(payload.data) ? payload.data[0] : null;
|
|
198
|
+
const decoded = first ? base64ToImage(first.b64_json) : null;
|
|
199
|
+
if (!decoded) throw new Error('OpenRouter 没有返回图片');
|
|
200
|
+
return { bytes: decoded.bytes, contentType: first.media_type || decoded.contentType };
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
id: 'azure-openai',
|
|
205
|
+
label: 'Azure OpenAI Images',
|
|
206
|
+
match: (provider) => /(^|\.)openai\.azure\.com$/i.test(hostOf(provider)) || /(^|\.)services\.ai\.azure\.com$/i.test(hostOf(provider)),
|
|
207
|
+
authHeader: 'api-key',
|
|
208
|
+
local: false,
|
|
209
|
+
endpoints: { generate: '/images/generations', edit: '/images/edits' },
|
|
210
|
+
transport: { generate: 'json', edit: 'multipart' },
|
|
211
|
+
send: ['model', 'prompt', 'size', 'quality', 'n', 'output_format'],
|
|
212
|
+
size: { style: 'string', field: 'size', multiple: 16, minPixels: 655360, maxPixels: 8294400, default: '1024x1024', onUnsupported: 'nearest' },
|
|
213
|
+
quality: GPT_QUALITY,
|
|
214
|
+
outputFormat: 'png',
|
|
215
|
+
maxN: 10,
|
|
216
|
+
editFields: ['image[]', 'image']
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
id: 'openai-chat',
|
|
220
|
+
label: 'OpenAI 兼容 chat 出图(中转)',
|
|
221
|
+
match: () => false,
|
|
222
|
+
local: false,
|
|
223
|
+
endpoints: { generate: '/chat/completions', edit: '/chat/completions' },
|
|
224
|
+
transport: { generate: 'json', edit: 'json' },
|
|
225
|
+
// 这个协议没有尺寸字段:尺寸只能写进提示词(stitch),面板仍给宽高输入框。
|
|
226
|
+
size: { control: 'prompt', stitch: true, wire: [], style: 'string', field: 'size', default: '1024x1024' },
|
|
227
|
+
maxN: 1,
|
|
228
|
+
editFields: [],
|
|
229
|
+
async execute(kind, provider, input, signal) {
|
|
230
|
+
const parts = [{ type: 'text', text: promptWithNegative(input) }].concat(imageParts(input.images));
|
|
231
|
+
const body = {
|
|
232
|
+
model: input.model,
|
|
233
|
+
messages: [{ role: 'user', content: parts.length === 1 ? parts[0].text : parts }],
|
|
234
|
+
modalities: ['image', 'text']
|
|
235
|
+
};
|
|
236
|
+
const response = await request(baseUrlOf(provider) + '/chat/completions', {
|
|
237
|
+
method: 'POST',
|
|
238
|
+
headers: headersFor(provider, false, null, this),
|
|
239
|
+
body: JSON.stringify(body),
|
|
240
|
+
signal: signal
|
|
241
|
+
});
|
|
242
|
+
const payload = await readJsonResponse(response, 64 * 1024 * 1024);
|
|
243
|
+
const decoded = chatImage(payload);
|
|
244
|
+
if (!decoded) {
|
|
245
|
+
throw new Error('该中转没有返回图片(检查 modalities 或换用 OpenRouter 专用接口)');
|
|
246
|
+
}
|
|
247
|
+
if (decoded.bytes) return decoded;
|
|
248
|
+
// 中转经常把图存到自己的 CDN 再回链接;当轮下载,不把 URL 交给上层。
|
|
249
|
+
return fetchImageUrl(decoded.url, signal);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
];
|