dsh-mo-image 0.5.1 → 0.6.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 +7 -0
- package/README.md +428 -409
- package/lib/adapter-kit.js +318 -0
- package/lib/adapters/async.js +351 -0
- package/lib/adapters/cn.js +95 -0
- package/lib/adapters/openai.js +233 -0
- package/lib/adapters.js +273 -356
- package/lib/client.js +549 -21
- package/lib/comfy-workflow.js +524 -0
- package/lib/index.js +230 -33
- package/lib/localized-error.js +1 -0
- package/lib/prompt-profiles.js +21 -0
- package/locale/messages.csv +214 -180
- package/package.json +1 -1
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 国内厂商里协议形状和 OpenAI Images 不一致的两家。
|
|
3
|
+
*
|
|
4
|
+
* - 百炼 Qwen-Image:原生多模态同步接口,size 用 `*` 分隔,图片在 output.choices[].message.content[].image
|
|
5
|
+
* - 智谱 BigModel:形状像 OpenAI,但不接受 n / response_format,一次只回一张 url
|
|
6
|
+
*/
|
|
7
|
+
import { request } from '../net.js';
|
|
8
|
+
import { baseUrlOf, headersFor, readJsonResponse, fetchImageUrl, base64ToImage } from '../adapter-kit.js';
|
|
9
|
+
|
|
10
|
+
function promptOf(input) {
|
|
11
|
+
const prompt = String((input && input.prompt) || '');
|
|
12
|
+
const negative = String((input && input.negativePrompt) || '').trim();
|
|
13
|
+
return negative ? prompt + '\n\nAvoid: ' + negative : prompt;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function sizeOf(input) {
|
|
17
|
+
const fitted = input && input.fitted;
|
|
18
|
+
if (!fitted || fitted.omit || fitted.size === 'none' || fitted.size === 'auto') return '';
|
|
19
|
+
if (fitted.width && fitted.height) return fitted.width + 'x' + fitted.height;
|
|
20
|
+
return String(fitted.size || '');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function urlToImage(url, signal) {
|
|
24
|
+
return fetchImageUrl(String(url), signal);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const CN_ADAPTERS = [
|
|
28
|
+
{
|
|
29
|
+
id: 'dashscope-qwen-image',
|
|
30
|
+
label: '阿里云百炼 · Qwen-Image',
|
|
31
|
+
match: (provider, model) => /dashscope\.aliyuncs\.com/i.test(baseUrlOf(provider)) && /qwen-image/i.test(String(model || '')),
|
|
32
|
+
local: false,
|
|
33
|
+
endpoints: { generate: '/services/aigc/multimodal-generation/generation', edit: '' },
|
|
34
|
+
transport: { generate: 'json', edit: 'json' },
|
|
35
|
+
maxN: 6,
|
|
36
|
+
editFields: [],
|
|
37
|
+
async execute(kind, provider, input, signal) {
|
|
38
|
+
if (kind === 'edit') throw new Error('百炼 Qwen-Image 暂未支持参考图编辑;请换用支持编辑的模型。');
|
|
39
|
+
const fitted = input.fitted && !input.fitted.omit ? input.fitted : { width: 1024, height: 1024 };
|
|
40
|
+
const parameters = { n: 1, size: fitted.width + '*' + fitted.height };
|
|
41
|
+
if (input.negativePrompt) parameters.negative_prompt = String(input.negativePrompt);
|
|
42
|
+
const body = {
|
|
43
|
+
model: input.model,
|
|
44
|
+
input: { messages: [{ role: 'user', content: [{ text: promptOf(input) }] }] },
|
|
45
|
+
parameters: parameters
|
|
46
|
+
};
|
|
47
|
+
const response = await request(baseUrlOf(provider) + '/services/aigc/multimodal-generation/generation', {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
headers: headersFor(provider, false, null, this),
|
|
50
|
+
body: JSON.stringify(body),
|
|
51
|
+
signal: signal
|
|
52
|
+
});
|
|
53
|
+
const payload = await readJsonResponse(response, 32 * 1024 * 1024);
|
|
54
|
+
const content = payload && payload.output && payload.output.choices
|
|
55
|
+
&& payload.output.choices[0] && payload.output.choices[0].message
|
|
56
|
+
&& payload.output.choices[0].message.content;
|
|
57
|
+
for (const item of Array.isArray(content) ? content : []) {
|
|
58
|
+
if (item && item.image) return urlToImage(item.image, signal);
|
|
59
|
+
}
|
|
60
|
+
throw new Error('百炼 Qwen-Image 没有返回图片');
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
id: 'zhipu',
|
|
65
|
+
label: '智谱 BigModel',
|
|
66
|
+
match: (provider) => /(^|\.)bigmodel\.cn$/i.test(String((provider && provider.baseUrl) || '').replace(/^https?:\/\//i, '').split('/')[0]),
|
|
67
|
+
local: false,
|
|
68
|
+
endpoints: { generate: '/images/generations', edit: '' },
|
|
69
|
+
transport: { generate: 'json', edit: 'json' },
|
|
70
|
+
maxN: 1,
|
|
71
|
+
editFields: [],
|
|
72
|
+
async execute(kind, provider, input, signal) {
|
|
73
|
+
if (kind === 'edit') throw new Error('智谱 BigModel 暂未支持参考图编辑;请换用支持编辑的模型。');
|
|
74
|
+
const body = { model: input.model, prompt: promptOf(input) };
|
|
75
|
+
const size = sizeOf(input);
|
|
76
|
+
if (size) body.size = size;
|
|
77
|
+
const quality = String(input.quality || '').trim().toLowerCase();
|
|
78
|
+
if (quality && quality !== 'off' && quality !== 'auto') body.quality = quality;
|
|
79
|
+
const response = await request(baseUrlOf(provider) + '/images/generations', {
|
|
80
|
+
method: 'POST',
|
|
81
|
+
headers: headersFor(provider, false, null, this),
|
|
82
|
+
body: JSON.stringify(body),
|
|
83
|
+
signal: signal
|
|
84
|
+
});
|
|
85
|
+
const payload = await readJsonResponse(response, 32 * 1024 * 1024);
|
|
86
|
+
const first = payload && Array.isArray(payload.data) ? payload.data[0] : null;
|
|
87
|
+
if (first && first.url) return urlToImage(first.url, signal);
|
|
88
|
+
if (first && first.b64_json) {
|
|
89
|
+
const decoded = base64ToImage(first.b64_json);
|
|
90
|
+
if (decoded) return decoded;
|
|
91
|
+
}
|
|
92
|
+
throw new Error('智谱 BigModel 没有返回图片');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
];
|
|
@@ -0,0 +1,233 @@
|
|
|
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, decodeImagePayload, fetchImageUrl
|
|
12
|
+
} from '../adapter-kit.js';
|
|
13
|
+
|
|
14
|
+
function hostOf(provider) {
|
|
15
|
+
return String((provider && provider.baseUrl) || '').replace(/^https?:\/\//i, '').split('/')[0].toLowerCase();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function sizeTextOf(input) {
|
|
19
|
+
const fitted = input && input.fitted;
|
|
20
|
+
if (!fitted || fitted.omit || fitted.size === 'none' || fitted.size === 'auto') return '';
|
|
21
|
+
if (fitted.width && fitted.height) return fitted.width + 'x' + fitted.height;
|
|
22
|
+
return String(fitted.size || '');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function promptWithNegative(input) {
|
|
26
|
+
const prompt = String((input && input.prompt) || '');
|
|
27
|
+
const negative = String((input && input.negativePrompt) || '').trim();
|
|
28
|
+
return negative ? prompt + '\n\nAvoid: ' + negative : prompt;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function imageParts(images) {
|
|
32
|
+
return (Array.isArray(images) ? images : [])
|
|
33
|
+
.filter((image) => image && image.buffer && image.contentType)
|
|
34
|
+
.map((image) => ({ type: 'image_url', image_url: { url: dataUrlOf(image) } }));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function decodeDataOrUrl(text) {
|
|
38
|
+
if (!text) return null;
|
|
39
|
+
const decoded = base64ToImage(text);
|
|
40
|
+
if (decoded) return decoded;
|
|
41
|
+
if (/^https?:\/\//i.test(String(text))) return { url: String(text) };
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** chat 出图没有统一响应格式:images[] → content 里的 data URL → markdown 链接 → 兜底扫描。 */
|
|
46
|
+
function chatImage(payload) {
|
|
47
|
+
const message = payload && payload.choices && payload.choices[0] && payload.choices[0].message;
|
|
48
|
+
const direct = message && Array.isArray(message.images) ? message.images : [];
|
|
49
|
+
for (const item of direct) {
|
|
50
|
+
const url = (item && item.image_url && item.image_url.url) || (item && item.url) || (typeof item === 'string' ? item : '');
|
|
51
|
+
const decoded = decodeDataOrUrl(url);
|
|
52
|
+
if (decoded) return decoded;
|
|
53
|
+
}
|
|
54
|
+
const content = message && message.content;
|
|
55
|
+
const texts = typeof content === 'string'
|
|
56
|
+
? [content]
|
|
57
|
+
: Array.isArray(content)
|
|
58
|
+
? content.map((part) => (typeof part === 'string' ? part : (part && part.text) || ''))
|
|
59
|
+
: [];
|
|
60
|
+
for (const text of texts) {
|
|
61
|
+
const hit = /data:image\/[a-z+]+;base64,[A-Za-z0-9+/=\r\n]+/i.exec(text);
|
|
62
|
+
if (hit) {
|
|
63
|
+
const decoded = base64ToImage(hit[0]);
|
|
64
|
+
if (decoded) return decoded;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (const text of texts) {
|
|
68
|
+
const hit = /https?:\/\/[^\s)"'<>]+\.(?:png|jpe?g|webp)(?:\?[^\s)"'<>]*)?/i.exec(text);
|
|
69
|
+
if (hit) return { url: hit[0] };
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
return decodeImagePayload(payload, null);
|
|
73
|
+
} catch (_error) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const GPT_QUALITY = {
|
|
79
|
+
label: '质量', default: 'high', fallback: 'high',
|
|
80
|
+
allowed: ['low', 'medium', 'high', 'auto'],
|
|
81
|
+
options: [
|
|
82
|
+
{ value: 'off', label: 'Off' },
|
|
83
|
+
{ value: 'low', label: 'Low' },
|
|
84
|
+
{ value: 'medium', label: 'Medium' },
|
|
85
|
+
{ value: 'high', label: 'High' },
|
|
86
|
+
{ value: 'auto', label: 'Auto' }
|
|
87
|
+
]
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export const OPENAI_ADAPTERS = [
|
|
91
|
+
{
|
|
92
|
+
id: 'openai-responses',
|
|
93
|
+
label: 'OpenAI Responses (image_generation)',
|
|
94
|
+
match: () => false,
|
|
95
|
+
local: false,
|
|
96
|
+
endpoints: { generate: '/responses', edit: '/responses' },
|
|
97
|
+
transport: { generate: 'json', edit: 'json' },
|
|
98
|
+
maxN: 1,
|
|
99
|
+
editFields: [],
|
|
100
|
+
async execute(kind, provider, input, signal) {
|
|
101
|
+
const model = String(input.model || '').trim();
|
|
102
|
+
const hostModel = String((provider && provider.hostModel) || 'gpt-5.2').trim();
|
|
103
|
+
const size = sizeTextOf(input);
|
|
104
|
+
const tool = { type: 'image_generation', model: model, ...(size ? { size: size } : {}) };
|
|
105
|
+
const quality = String(input.quality || '').trim().toLowerCase();
|
|
106
|
+
if (quality && quality !== 'off' && quality !== 'auto') tool.quality = quality;
|
|
107
|
+
tool.output_format = 'png';
|
|
108
|
+
|
|
109
|
+
const prompt = promptWithNegative(input);
|
|
110
|
+
const images = (input.images || []).filter((image) => image && image.buffer && image.contentType);
|
|
111
|
+
const payloadInput = (kind === 'edit' || images.length)
|
|
112
|
+
? [{
|
|
113
|
+
role: 'user',
|
|
114
|
+
content: [{ type: 'input_text', text: prompt }].concat(images.map((image) => ({
|
|
115
|
+
type: 'input_image', image_url: dataUrlOf(image), detail: 'auto'
|
|
116
|
+
})))
|
|
117
|
+
}]
|
|
118
|
+
: prompt;
|
|
119
|
+
|
|
120
|
+
const response = await request(baseUrlOf(provider) + '/responses', {
|
|
121
|
+
method: 'POST',
|
|
122
|
+
headers: headersFor(provider, false, null, this),
|
|
123
|
+
body: JSON.stringify({
|
|
124
|
+
model: hostModel,
|
|
125
|
+
input: payloadInput,
|
|
126
|
+
tools: [tool],
|
|
127
|
+
tool_choice: { type: 'image_generation' }
|
|
128
|
+
}),
|
|
129
|
+
signal: signal
|
|
130
|
+
});
|
|
131
|
+
const payload = await readJsonResponse(response, 64 * 1024 * 1024);
|
|
132
|
+
const items = payload && Array.isArray(payload.output) ? payload.output : [];
|
|
133
|
+
const call = items.find((item) => item && item.type === 'image_generation_call' && item.result);
|
|
134
|
+
const decoded = call ? base64ToImage(call.result) : null;
|
|
135
|
+
if (decoded) return decoded;
|
|
136
|
+
throw new Error('Responses API 没有返回图片(检查 tool_choice 与主模型是否支持 image_generation)');
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
id: 'openrouter-images',
|
|
141
|
+
label: 'OpenRouter Images',
|
|
142
|
+
match: (provider) => hostOf(provider) === 'openrouter.ai',
|
|
143
|
+
local: false,
|
|
144
|
+
endpoints: { generate: '/images', edit: '/images' },
|
|
145
|
+
transport: { generate: 'json', edit: 'json' },
|
|
146
|
+
maxN: 10,
|
|
147
|
+
editFields: [],
|
|
148
|
+
async listModels(provider, signal) {
|
|
149
|
+
const response = await request(baseUrlOf(provider) + '/images/models', {
|
|
150
|
+
headers: headersFor(provider, false), signal: signal
|
|
151
|
+
});
|
|
152
|
+
const payload = await readJsonResponse(response, 5 * 1024 * 1024);
|
|
153
|
+
return (payload && Array.isArray(payload.data) ? payload.data : [])
|
|
154
|
+
.map((item) => String((item && item.id) || '').trim())
|
|
155
|
+
.filter(Boolean)
|
|
156
|
+
.slice(0, 200);
|
|
157
|
+
},
|
|
158
|
+
async execute(kind, provider, input, signal) {
|
|
159
|
+
const body = {
|
|
160
|
+
model: input.model,
|
|
161
|
+
prompt: promptWithNegative(input),
|
|
162
|
+
n: 1,
|
|
163
|
+
output_format: 'png'
|
|
164
|
+
};
|
|
165
|
+
if (input.fitted && !input.fitted.omit && input.fitted.width && input.fitted.height) {
|
|
166
|
+
body.aspect_ratio = nearestAspect(input.fitted.width, input.fitted.height);
|
|
167
|
+
}
|
|
168
|
+
const quality = String(input.quality || '').trim().toLowerCase();
|
|
169
|
+
if (quality && quality !== 'off' && quality !== 'auto') body.quality = quality;
|
|
170
|
+
const references = imageParts(input.images);
|
|
171
|
+
if (references.length) body.input_references = references;
|
|
172
|
+
|
|
173
|
+
const response = await request(baseUrlOf(provider) + '/images', {
|
|
174
|
+
method: 'POST',
|
|
175
|
+
headers: headersFor(provider, false, null, this),
|
|
176
|
+
body: JSON.stringify(body),
|
|
177
|
+
signal: signal
|
|
178
|
+
});
|
|
179
|
+
const payload = await readJsonResponse(response, 64 * 1024 * 1024);
|
|
180
|
+
const first = payload && Array.isArray(payload.data) ? payload.data[0] : null;
|
|
181
|
+
const decoded = first ? base64ToImage(first.b64_json) : null;
|
|
182
|
+
if (!decoded) throw new Error('OpenRouter 没有返回图片');
|
|
183
|
+
return { bytes: decoded.bytes, contentType: first.media_type || decoded.contentType };
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
id: 'azure-openai',
|
|
188
|
+
label: 'Azure OpenAI Images',
|
|
189
|
+
match: (provider) => /(^|\.)openai\.azure\.com$/i.test(hostOf(provider)) || /(^|\.)services\.ai\.azure\.com$/i.test(hostOf(provider)),
|
|
190
|
+
authHeader: 'api-key',
|
|
191
|
+
local: false,
|
|
192
|
+
endpoints: { generate: '/images/generations', edit: '/images/edits' },
|
|
193
|
+
transport: { generate: 'json', edit: 'multipart' },
|
|
194
|
+
send: ['model', 'prompt', 'size', 'quality', 'n', 'output_format'],
|
|
195
|
+
size: { style: 'string', field: 'size', multiple: 16, minPixels: 655360, maxPixels: 8294400, default: '1024x1024', onUnsupported: 'nearest' },
|
|
196
|
+
quality: GPT_QUALITY,
|
|
197
|
+
outputFormat: 'png',
|
|
198
|
+
maxN: 10,
|
|
199
|
+
editFields: ['image[]', 'image']
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
id: 'openai-chat',
|
|
203
|
+
label: 'OpenAI 兼容 chat 出图(中转)',
|
|
204
|
+
match: () => false,
|
|
205
|
+
local: false,
|
|
206
|
+
endpoints: { generate: '/chat/completions', edit: '/chat/completions' },
|
|
207
|
+
transport: { generate: 'json', edit: 'json' },
|
|
208
|
+
maxN: 1,
|
|
209
|
+
editFields: [],
|
|
210
|
+
async execute(kind, provider, input, signal) {
|
|
211
|
+
const parts = [{ type: 'text', text: promptWithNegative(input) }].concat(imageParts(input.images));
|
|
212
|
+
const body = {
|
|
213
|
+
model: input.model,
|
|
214
|
+
messages: [{ role: 'user', content: parts.length === 1 ? parts[0].text : parts }],
|
|
215
|
+
modalities: ['image', 'text']
|
|
216
|
+
};
|
|
217
|
+
const response = await request(baseUrlOf(provider) + '/chat/completions', {
|
|
218
|
+
method: 'POST',
|
|
219
|
+
headers: headersFor(provider, false, null, this),
|
|
220
|
+
body: JSON.stringify(body),
|
|
221
|
+
signal: signal
|
|
222
|
+
});
|
|
223
|
+
const payload = await readJsonResponse(response, 64 * 1024 * 1024);
|
|
224
|
+
const decoded = chatImage(payload);
|
|
225
|
+
if (!decoded) {
|
|
226
|
+
throw new Error('该中转没有返回图片(检查 modalities 或换用 OpenRouter 专用接口)');
|
|
227
|
+
}
|
|
228
|
+
if (decoded.bytes) return decoded;
|
|
229
|
+
// 中转经常把图存到自己的 CDN 再回链接;当轮下载,不把 URL 交给上层。
|
|
230
|
+
return fetchImageUrl(decoded.url, signal);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
];
|