dsh-mo-image 0.6.0 → 1.0.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 +39 -0
- package/README.md +328 -55
- package/icon.png +0 -0
- package/lib/adapter-kit.js +289 -40
- package/lib/adapters/async.js +354 -140
- package/lib/adapters/cn.js +133 -68
- package/lib/adapters/openai.js +193 -143
- package/lib/adapters.js +3579 -695
- package/lib/client.js +6742 -828
- package/lib/comfy-workflow.js +100 -10
- package/lib/config-store.js +1628 -0
- package/lib/field-templates.js +385 -0
- package/lib/image-execution.js +58 -0
- package/lib/image-jobs.js +344 -0
- package/lib/image-queue.js +195 -0
- package/lib/image-request.js +830 -0
- package/lib/image-service.js +189 -0
- package/lib/image-worker.js +208 -0
- package/lib/index.js +1054 -967
- package/lib/localized-error.js +4 -0
- package/lib/model-rules.js +1034 -0
- package/lib/net.js +60 -25
- package/lib/output-profiles.js +1225 -0
- package/lib/png-scale.js +150 -68
- package/lib/prompt-profiles.js +196 -80
- package/lib/provider-operations.js +291 -0
- package/lib/proxy-dispatcher.js +11 -5
- package/lib/proxy.js +10 -8
- package/lib/request-auth.js +198 -0
- package/lib/retry.js +162 -0
- package/lib/settings-memory.js +117 -0
- package/lib/status.js +190 -0
- package/locale/messages.csv +206 -24
- package/package.json +3 -1
- package/scripts/release.mjs +79 -64
- package/skills/mo-image-editing/SKILL.md +36 -80
- package/skills/mo-image-generation/SKILL.md +62 -154
package/lib/adapters/openai.js
CHANGED
|
@@ -1,18 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* OpenAI
|
|
2
|
+
* OpenAI 家族的三条自拼 body 协议。
|
|
3
3
|
*
|
|
4
4
|
* - openai-responses:Responses API 的 image_generation 工具(图片在 output[].result,裸 base64)
|
|
5
5
|
* - openrouter-images:OpenRouter 专用 /api/v1/images(data[].b64_json + media_type)
|
|
6
|
-
* - azure-openai:Azure 的 OpenAI v1 路径,鉴权是 api-key 而不是 Bearer
|
|
7
6
|
* - openai-chat:中转生态的 chat/completions + modalities,响应形态不唯一,要多形态探测
|
|
7
|
+
*
|
|
8
|
+
* Azure OpenAI 不在这里:它与 `openai-images` 的地址三元组、传输方式、字段模板完全一样,
|
|
9
|
+
* 只差鉴权方式,所以是 `openai-images` + 提供商字段 `auth.mode: 'api-key'`
|
|
10
|
+
* (见 lib/adapter-kit.js 的 AUTH_MODES 与 adapters.js 的 LEGACY_PROTOCOL_ALIASES)。
|
|
11
|
+
*
|
|
12
|
+
* 其中这三条自己拼 body 的协议不再持有字段名:prompt / negative /
|
|
13
|
+
* model / n 写在哪,由字段模板(`fieldsFor()` 或协议自带默认模板)说了算,再用
|
|
14
|
+
* applyFields / promptParts 写进去。
|
|
15
|
+
* 留在协议里的有两类东西:
|
|
16
|
+
* - 请求形状:Responses 的 input 是「字符串」还是「content[]」,chat 的 content 是「字符串」
|
|
17
|
+
* 还是 parts、`modalities`,OpenRouter 的 aspect_ratio / resolution;
|
|
18
|
+
* - 六种模板能力表达不了的项对象:{ type: 'input_image', detail } 与
|
|
19
|
+
* { type: 'image_url', image_url: { url } }(带额外键),按设计红线留在协议里。
|
|
8
20
|
*/
|
|
9
|
-
import { request } from '../net.js';
|
|
10
21
|
import {
|
|
11
|
-
baseUrlOf, headersFor, readJsonResponse, dataUrlOf,
|
|
22
|
+
baseUrlOf, headersFor, joinEndpoint, readJsonResponse, dataUrlOf, nearestAspect
|
|
12
23
|
} from '../adapter-kit.js';
|
|
24
|
+
import { requestProvider } from '../request-auth.js';
|
|
25
|
+
import { applyFields, promptParts, resolveFields } from '../field-templates.js';
|
|
26
|
+
import { outputProfile } from '../output-profiles.js';
|
|
27
|
+
// 循环依赖:adapters.js 先 import 本模块、再求值完。fieldsFor 是函数声明(已提升),
|
|
28
|
+
// 只在 execute() 里调用,所以安全。
|
|
29
|
+
import { executeBuiltImage, fieldsFor } from '../adapters.js';
|
|
13
30
|
|
|
14
31
|
function hostOf(provider) {
|
|
15
|
-
return
|
|
32
|
+
try { return new URL(baseUrlOf(provider)).hostname.toLowerCase(); } catch (_error) { return ''; }
|
|
16
33
|
}
|
|
17
34
|
|
|
18
35
|
function sizeTextOf(input) {
|
|
@@ -22,10 +39,21 @@ function sizeTextOf(input) {
|
|
|
22
39
|
return String(fitted.size || '');
|
|
23
40
|
}
|
|
24
41
|
|
|
25
|
-
|
|
26
|
-
|
|
42
|
+
/** 提示词文本:负面词拼不拼进来由模板说(这几条协议的默认模板都是 appendToPrompt)。 */
|
|
43
|
+
function foldedPrompt(fields, input) {
|
|
27
44
|
const negative = String((input && input.negativePrompt) || '').trim();
|
|
28
|
-
return
|
|
45
|
+
return promptParts(fields, { prompt: input.prompt, negative: negative || undefined }).prompt;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 质量值:`off` / `auto` 的语义是「完全不发这个参数」。 */
|
|
49
|
+
function qualityOf(input) {
|
|
50
|
+
const quality = String((input && input.quality) || '').trim().toLowerCase();
|
|
51
|
+
return quality && quality !== 'off' && quality !== 'auto' ? quality : undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 参考图:老实现只认带 contentType 的图,别把不完整的数据塞进请求体。 */
|
|
55
|
+
function referenceImages(input) {
|
|
56
|
+
return (input.images || []).filter((image) => image && image.buffer && image.contentType);
|
|
29
57
|
}
|
|
30
58
|
|
|
31
59
|
function imageParts(images) {
|
|
@@ -34,81 +62,109 @@ function imageParts(images) {
|
|
|
34
62
|
.map((image) => ({ type: 'image_url', image_url: { url: dataUrlOf(image) } }));
|
|
35
63
|
}
|
|
36
64
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
65
|
+
/**
|
|
66
|
+
* 「用户覆盖 + 协议默认模板」:给没有预设、而且信封形状和模型字段规则对不上的两条协议用。
|
|
67
|
+
*
|
|
68
|
+
* 不用 fieldsFor() 的那两层:模型字段规则是照「扁平 body」写的(gpt-image 的提示词叫
|
|
69
|
+
* body.prompt、模型叫 body.model),套进 Responses 的 `tools[0]` 或 chat 的 `messages[]`
|
|
70
|
+
* 会静默写错位置——不抛错,只发出去一个坏 body。信封形状本来就不归规则管。
|
|
71
|
+
*/
|
|
72
|
+
function fieldsWithDefaults(defaults, provider, model) {
|
|
73
|
+
const options = provider && typeof provider === 'object' ? provider.modelOptions : null;
|
|
74
|
+
const entry = options && typeof options === 'object' ? options[String(model || '')] : null;
|
|
75
|
+
const declared = entry && entry.fields && typeof entry.fields === 'object' && !Array.isArray(entry.fields)
|
|
76
|
+
? entry.fields
|
|
77
|
+
: null;
|
|
78
|
+
return resolveFields({ override: declared, preset: defaults });
|
|
43
79
|
}
|
|
44
80
|
|
|
45
|
-
/**
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
}
|
|
81
|
+
/**
|
|
82
|
+
* Responses 的默认模板:模型名与质量在 `tools[0]` 里。
|
|
83
|
+
* 提示词(input 的字符串 / content[] 二选一)与参考图项(type + detail)表达不了,写 null 留在协议里。
|
|
84
|
+
*/
|
|
85
|
+
const RESPONSES_FIELDS = {
|
|
86
|
+
prompt: null,
|
|
87
|
+
negative: { appendToPrompt: '\n\nAvoid: ' },
|
|
88
|
+
model: { path: 'tools[0].model' },
|
|
89
|
+
n: null,
|
|
90
|
+
image: null,
|
|
91
|
+
quality: { path: 'tools[0].quality' }
|
|
92
|
+
};
|
|
77
93
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
94
|
+
/**
|
|
95
|
+
* chat 的默认模板:模型是顶层字段,负面词拼进提示词。
|
|
96
|
+
* messages[].content[] 里的 { type: 'text' } / { type: 'image_url' } 项带额外键,留在协议里。
|
|
97
|
+
*/
|
|
98
|
+
const CHAT_FIELDS = {
|
|
99
|
+
prompt: null,
|
|
100
|
+
negative: { appendToPrompt: '\n\nAvoid: ' },
|
|
101
|
+
model: { path: 'model' },
|
|
102
|
+
n: null,
|
|
103
|
+
image: null,
|
|
104
|
+
quality: null
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* OpenRouter 的默认模板(预设有同款):OpenRouter 主机会命中模型字段规则,
|
|
109
|
+
* 但有人把这条协议挂到别的主机上时规则不命中,兜底不能是空模板。
|
|
110
|
+
*/
|
|
111
|
+
const OPENROUTER_FIELDS = {
|
|
112
|
+
prompt: { path: 'prompt' },
|
|
113
|
+
negative: { appendToPrompt: '\n\nAvoid: ' },
|
|
114
|
+
model: { path: 'model' },
|
|
115
|
+
n: { path: 'n' },
|
|
116
|
+
image: null,
|
|
117
|
+
quality: null
|
|
88
118
|
};
|
|
89
119
|
|
|
90
120
|
export const OPENAI_ADAPTERS = [
|
|
91
121
|
{
|
|
92
122
|
id: 'openai-responses',
|
|
93
|
-
label: '
|
|
123
|
+
label: 'protocol.openai-responses',
|
|
124
|
+
acceptsHttpParams: true,
|
|
94
125
|
match: () => false,
|
|
95
126
|
local: false,
|
|
96
127
|
endpoints: { generate: '/responses', edit: '/responses' },
|
|
128
|
+
routes: { generate: '/responses', edit: '/responses', models: '/models' },
|
|
97
129
|
transport: { generate: 'json', edit: 'json' },
|
|
98
130
|
maxN: 1,
|
|
99
131
|
editFields: [],
|
|
100
|
-
|
|
101
|
-
|
|
132
|
+
// 输出格式描述符:图片在 output[] 里 type 为 image_generation_call 的那项的 result
|
|
133
|
+
// (裸 base64)。没有 URL 分支,也没有兜底扫描。
|
|
134
|
+
output: outputProfile({
|
|
135
|
+
kind: 'json',
|
|
136
|
+
pick: [{
|
|
137
|
+
path: 'output[]',
|
|
138
|
+
as: 'base64',
|
|
139
|
+
any: ['result'],
|
|
140
|
+
where: [{ path: 'type', equals: 'image_generation_call' }]
|
|
141
|
+
}],
|
|
142
|
+
maxBytes: 64 * 1024 * 1024,
|
|
143
|
+
emptyMessage: 'Responses API 没有返回图片(检查 tool_choice 与主模型是否支持 image_generation)'
|
|
144
|
+
}, 'openai-responses'),
|
|
145
|
+
async execute(kind, provider, input, signal, hooks) {
|
|
102
146
|
const hostModel = String((provider && provider.hostModel) || 'gpt-5.2').trim();
|
|
147
|
+
const fields = fieldsWithDefaults(RESPONSES_FIELDS, provider, input.model);
|
|
148
|
+
// 工具声明是请求形状(type / output_format 是响应形态常量)。
|
|
149
|
+
const tool = { type: 'image_generation', output_format: 'png' };
|
|
150
|
+
const body = {
|
|
151
|
+
model: hostModel,
|
|
152
|
+
tools: [tool],
|
|
153
|
+
tool_choice: { type: 'image_generation' }
|
|
154
|
+
};
|
|
155
|
+
// 模型名与质量的落点来自模板(默认是 tools[0].model / tools[0].quality)。
|
|
156
|
+
applyFields(body, { model: fields.model, quality: fields.quality }, {
|
|
157
|
+
model: String(input.model || '').trim(),
|
|
158
|
+
quality: qualityOf(input)
|
|
159
|
+
});
|
|
160
|
+
// 尺寸不属于六个逻辑字段:Responses 的尺寸在工具声明里。
|
|
103
161
|
const size = sizeTextOf(input);
|
|
104
|
-
|
|
105
|
-
const quality = String(input.quality || '').trim().toLowerCase();
|
|
106
|
-
if (quality && quality !== 'off' && quality !== 'auto') tool.quality = quality;
|
|
107
|
-
tool.output_format = 'png';
|
|
162
|
+
if (size) tool.size = size;
|
|
108
163
|
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
const
|
|
164
|
+
// input 是字符串还是 content[],取决于有没有参考图 / 是不是编辑方向:条件分支留在协议里。
|
|
165
|
+
const prompt = foldedPrompt(fields, input);
|
|
166
|
+
const images = referenceImages(input);
|
|
167
|
+
body.input = (kind === 'edit' || images.length)
|
|
112
168
|
? [{
|
|
113
169
|
role: 'user',
|
|
114
170
|
content: [{ type: 'input_text', text: prompt }].concat(images.map((image) => ({
|
|
@@ -117,117 +173,111 @@ export const OPENAI_ADAPTERS = [
|
|
|
117
173
|
}]
|
|
118
174
|
: prompt;
|
|
119
175
|
|
|
120
|
-
|
|
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)');
|
|
176
|
+
return executeBuiltImage(kind, this, provider, input, '/responses', body, signal, undefined, hooks);
|
|
137
177
|
}
|
|
138
178
|
},
|
|
139
179
|
{
|
|
140
180
|
id: 'openrouter-images',
|
|
141
|
-
label: '
|
|
181
|
+
label: 'protocol.openrouter-images',
|
|
182
|
+
acceptsHttpParams: true,
|
|
142
183
|
match: (provider) => hostOf(provider) === 'openrouter.ai',
|
|
143
184
|
local: false,
|
|
144
185
|
endpoints: { generate: '/images', edit: '/images' },
|
|
186
|
+
routes: { generate: '/images', edit: '/images', models: '/models' },
|
|
145
187
|
transport: { generate: 'json', edit: 'json' },
|
|
146
188
|
maxN: 10,
|
|
147
189
|
editFields: [],
|
|
190
|
+
// 输出格式描述符:OpenRouter 专用接口回 data[0].b64_json(裸 base64),
|
|
191
|
+
// contentType 取同级 media_type(没有就按字节签名)。只看 data[0],没有 URL 分支。
|
|
192
|
+
output: outputProfile({
|
|
193
|
+
kind: 'json',
|
|
194
|
+
pick: [{ path: 'data.0.b64_json', as: 'base64', contentTypePath: 'media_type' }],
|
|
195
|
+
maxBytes: 64 * 1024 * 1024,
|
|
196
|
+
emptyMessage: 'OpenRouter 没有返回图片'
|
|
197
|
+
}, 'openrouter-images'),
|
|
148
198
|
async listModels(provider, signal) {
|
|
149
|
-
const response = await
|
|
150
|
-
headers: headersFor(provider, false), signal
|
|
151
|
-
});
|
|
152
|
-
const payload = await readJsonResponse(response, 5 * 1024 * 1024);
|
|
199
|
+
const response = await requestProvider(joinEndpoint(baseUrlOf(provider), '/images/models'), {
|
|
200
|
+
headers: headersFor(provider, false, null, this), signal
|
|
201
|
+
}, provider, this);
|
|
202
|
+
const payload = await readJsonResponse(response, 5 * 1024 * 1024, provider, this);
|
|
153
203
|
return (payload && Array.isArray(payload.data) ? payload.data : [])
|
|
154
204
|
.map((item) => String((item && item.id) || '').trim())
|
|
155
205
|
.filter(Boolean)
|
|
156
206
|
.slice(0, 200);
|
|
157
207
|
},
|
|
158
|
-
async execute(kind, provider, input, signal) {
|
|
159
|
-
|
|
208
|
+
async execute(kind, provider, input, signal, hooks) {
|
|
209
|
+
// 模板:用户覆盖 > 模型字段规则 > 协议默认(OpenRouter 主机上的规则与默认值一致)。
|
|
210
|
+
const fields = resolveFields({
|
|
211
|
+
override: fieldsFor(this, provider, input.model),
|
|
212
|
+
preset: OPENROUTER_FIELDS
|
|
213
|
+
});
|
|
214
|
+
const values = {
|
|
215
|
+
prompt: input.prompt,
|
|
160
216
|
model: input.model,
|
|
161
|
-
prompt: promptWithNegative(input),
|
|
162
217
|
n: 1,
|
|
163
|
-
|
|
218
|
+
negative: String((input && input.negativePrompt) || '').trim() || undefined
|
|
164
219
|
};
|
|
220
|
+
const folded = promptParts(fields, values);
|
|
221
|
+
const body = applyFields({}, fields, Object.assign({}, values, { prompt: folded.prompt }));
|
|
222
|
+
// 响应形态常量,不是「提示词叫什么」。
|
|
223
|
+
body.output_format = 'png';
|
|
224
|
+
// 尺寸不属于六个逻辑字段:OpenRouter 收比例 + 档位。
|
|
165
225
|
if (input.fitted && !input.fitted.omit && input.fitted.width && input.fitted.height) {
|
|
166
|
-
body.aspect_ratio = nearestAspect(input.fitted.width, input.fitted.height);
|
|
226
|
+
body.aspect_ratio = input.fitted.aspect || nearestAspect(input.fitted.width, input.fitted.height);
|
|
227
|
+
// 只有认 resolution 的底层模型才发档位(fitSize 已经填好 tier);发错了 provider 会 400。
|
|
228
|
+
if (input.fitted.tier) body.resolution = String(input.fitted.tier);
|
|
167
229
|
}
|
|
168
|
-
|
|
169
|
-
if (quality && quality !== 'off' && quality !== 'auto') body.quality = quality;
|
|
230
|
+
// 参考图是 { type, image_url: { url } } 对象(带额外键),六种能力表达不了,留在协议里。
|
|
170
231
|
const references = imageParts(input.images);
|
|
171
232
|
if (references.length) body.input_references = references;
|
|
172
233
|
|
|
173
|
-
|
|
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 };
|
|
234
|
+
return executeBuiltImage(kind, this, provider, input, '/images', body, signal, undefined, hooks);
|
|
184
235
|
}
|
|
185
236
|
},
|
|
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
237
|
{
|
|
202
238
|
id: 'openai-chat',
|
|
203
|
-
label: '
|
|
239
|
+
label: 'protocol.openai-chat',
|
|
240
|
+
acceptsHttpParams: true,
|
|
204
241
|
match: () => false,
|
|
205
242
|
local: false,
|
|
206
243
|
endpoints: { generate: '/chat/completions', edit: '/chat/completions' },
|
|
244
|
+
routes: { generate: '/chat/completions', edit: '/chat/completions', models: '/models' },
|
|
207
245
|
transport: { generate: 'json', edit: 'json' },
|
|
246
|
+
// 这个协议没有尺寸字段:尺寸只能写进提示词(stitch),面板仍给宽高输入框。
|
|
247
|
+
size: { control: 'prompt', stitch: true, wire: [], style: 'string', field: 'size', default: '1024x1024' },
|
|
248
|
+
// 四级级联,顺序照抄迁移前的 chatImage():
|
|
249
|
+
// 1. message.images[](元素可以是 image_url.url / url / 字符串)
|
|
250
|
+
// 2. content 里的 data URL(content 可以是字符串或 parts[].text)
|
|
251
|
+
// 3. content 里的 http 图片链接
|
|
252
|
+
// 4. 任意位置扫描(scan)
|
|
253
|
+
// 2/3 两级用 text 表达:一条正则先扫全部文本,再扫下一条——不是逐段文本依次试。
|
|
254
|
+
output: outputProfile({
|
|
255
|
+
kind: 'json',
|
|
256
|
+
from: [
|
|
257
|
+
{ path: 'choices.0.message.images[]', as: 'auto', any: ['image_url.url', 'url', ''] },
|
|
258
|
+
{
|
|
259
|
+
path: 'choices.0.message.content',
|
|
260
|
+
text: [
|
|
261
|
+
{ regex: "data:image/[a-z+]+;base64,[A-Za-z0-9+/=\\r\\n]+", as: 'base64' },
|
|
262
|
+
{ regex: "https?://[^\\s)\"'<>]+\\.(?:png|jpe?g|webp)(?:\\?[^\\s)\"'<>]*)?", as: 'url' }
|
|
263
|
+
]
|
|
264
|
+
}
|
|
265
|
+
],
|
|
266
|
+
scan: true,
|
|
267
|
+
maxBytes: 64 * 1024 * 1024,
|
|
268
|
+
emptyMessage: '该中转没有返回图片(检查 modalities 或换用 OpenRouter 专用接口)'
|
|
269
|
+
}, 'openai-chat'),
|
|
208
270
|
maxN: 1,
|
|
209
271
|
editFields: [],
|
|
210
|
-
async execute(kind, provider, input, signal) {
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
|
|
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);
|
|
272
|
+
async execute(kind, provider, input, signal, hooks) {
|
|
273
|
+
const fields = fieldsWithDefaults(CHAT_FIELDS, provider, input.model);
|
|
274
|
+
// 模型落点来自模板;messages / modalities 是请求形状。
|
|
275
|
+
const body = { modalities: ['image', 'text'] };
|
|
276
|
+
applyFields(body, { model: fields.model }, { model: input.model });
|
|
277
|
+
// content 是「字符串」还是 parts、以及项里的 type,都是条件形状,留在协议里。
|
|
278
|
+
const parts = [{ type: 'text', text: foldedPrompt(fields, input) }].concat(imageParts(input.images));
|
|
279
|
+
body.messages = [{ role: 'user', content: parts.length === 1 ? parts[0].text : parts }];
|
|
280
|
+
return executeBuiltImage(kind, this, provider, input, '/chat/completions', body, signal, undefined, hooks);
|
|
231
281
|
}
|
|
232
282
|
}
|
|
233
283
|
];
|