dsh-mo-image 0.7.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 +28 -0
- package/README.md +283 -50
- package/lib/adapter-kit.js +150 -253
- package/lib/adapters/async.js +264 -277
- package/lib/adapters/cn.js +133 -81
- package/lib/adapters/openai.js +188 -157
- package/lib/adapters.js +3087 -944
- package/lib/client.js +6444 -858
- package/lib/comfy-workflow.js +96 -10
- package/lib/config-store.js +1072 -136
- package/lib/field-templates.js +385 -0
- package/lib/image-execution.js +40 -817
- package/lib/image-jobs.js +23 -12
- package/lib/image-request.js +830 -0
- package/lib/image-service.js +96 -133
- package/lib/image-worker.js +84 -71
- package/lib/index.js +833 -327
- 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 +53 -46
- package/lib/prompt-profiles.js +83 -38
- 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 +86 -31
- package/lib/settings-memory.js +112 -28
- package/lib/status.js +6 -3
- package/locale/messages.csv +396 -226
- package/package.json +1 -1
- package/scripts/release.mjs +79 -64
- package/skills/mo-image-editing/SKILL.md +7 -13
- package/skills/mo-image-generation/SKILL.md +7 -6
package/lib/adapters/openai.js
CHANGED
|
@@ -1,19 +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,
|
|
12
|
-
decodeImagePayload, fetchImageUrl
|
|
22
|
+
baseUrlOf, headersFor, joinEndpoint, readJsonResponse, dataUrlOf, nearestAspect
|
|
13
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';
|
|
14
30
|
|
|
15
31
|
function hostOf(provider) {
|
|
16
|
-
return
|
|
32
|
+
try { return new URL(baseUrlOf(provider)).hostname.toLowerCase(); } catch (_error) { return ''; }
|
|
17
33
|
}
|
|
18
34
|
|
|
19
35
|
function sizeTextOf(input) {
|
|
@@ -23,10 +39,21 @@ function sizeTextOf(input) {
|
|
|
23
39
|
return String(fitted.size || '');
|
|
24
40
|
}
|
|
25
41
|
|
|
26
|
-
|
|
27
|
-
|
|
42
|
+
/** 提示词文本:负面词拼不拼进来由模板说(这几条协议的默认模板都是 appendToPrompt)。 */
|
|
43
|
+
function foldedPrompt(fields, input) {
|
|
28
44
|
const negative = String((input && input.negativePrompt) || '').trim();
|
|
29
|
-
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);
|
|
30
57
|
}
|
|
31
58
|
|
|
32
59
|
function imageParts(images) {
|
|
@@ -35,81 +62,109 @@ function imageParts(images) {
|
|
|
35
62
|
.map((image) => ({ type: 'image_url', image_url: { url: dataUrlOf(image) } }));
|
|
36
63
|
}
|
|
37
64
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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 });
|
|
44
79
|
}
|
|
45
80
|
|
|
46
|
-
/**
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
}
|
|
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
|
+
};
|
|
78
93
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
|
89
118
|
};
|
|
90
119
|
|
|
91
120
|
export const OPENAI_ADAPTERS = [
|
|
92
121
|
{
|
|
93
122
|
id: 'openai-responses',
|
|
94
|
-
label: '
|
|
123
|
+
label: 'protocol.openai-responses',
|
|
124
|
+
acceptsHttpParams: true,
|
|
95
125
|
match: () => false,
|
|
96
126
|
local: false,
|
|
97
127
|
endpoints: { generate: '/responses', edit: '/responses' },
|
|
128
|
+
routes: { generate: '/responses', edit: '/responses', models: '/models' },
|
|
98
129
|
transport: { generate: 'json', edit: 'json' },
|
|
99
130
|
maxN: 1,
|
|
100
131
|
editFields: [],
|
|
101
|
-
|
|
102
|
-
|
|
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) {
|
|
103
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 的尺寸在工具声明里。
|
|
104
161
|
const size = sizeTextOf(input);
|
|
105
|
-
|
|
106
|
-
const quality = String(input.quality || '').trim().toLowerCase();
|
|
107
|
-
if (quality && quality !== 'off' && quality !== 'auto') tool.quality = quality;
|
|
108
|
-
tool.output_format = 'png';
|
|
162
|
+
if (size) tool.size = size;
|
|
109
163
|
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
const
|
|
164
|
+
// input 是字符串还是 content[],取决于有没有参考图 / 是不是编辑方向:条件分支留在协议里。
|
|
165
|
+
const prompt = foldedPrompt(fields, input);
|
|
166
|
+
const images = referenceImages(input);
|
|
167
|
+
body.input = (kind === 'edit' || images.length)
|
|
113
168
|
? [{
|
|
114
169
|
role: 'user',
|
|
115
170
|
content: [{ type: 'input_text', text: prompt }].concat(images.map((image) => ({
|
|
@@ -118,135 +173,111 @@ export const OPENAI_ADAPTERS = [
|
|
|
118
173
|
}]
|
|
119
174
|
: prompt;
|
|
120
175
|
|
|
121
|
-
|
|
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)');
|
|
176
|
+
return executeBuiltImage(kind, this, provider, input, '/responses', body, signal, undefined, hooks);
|
|
138
177
|
}
|
|
139
178
|
},
|
|
140
179
|
{
|
|
141
180
|
id: 'openrouter-images',
|
|
142
|
-
label: '
|
|
181
|
+
label: 'protocol.openrouter-images',
|
|
182
|
+
acceptsHttpParams: true,
|
|
143
183
|
match: (provider) => hostOf(provider) === 'openrouter.ai',
|
|
144
184
|
local: false,
|
|
145
185
|
endpoints: { generate: '/images', edit: '/images' },
|
|
186
|
+
routes: { generate: '/images', edit: '/images', models: '/models' },
|
|
146
187
|
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
188
|
maxN: 10,
|
|
162
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'),
|
|
163
198
|
async listModels(provider, signal) {
|
|
164
|
-
const response = await
|
|
165
|
-
headers: headersFor(provider, false), signal
|
|
166
|
-
});
|
|
167
|
-
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);
|
|
168
203
|
return (payload && Array.isArray(payload.data) ? payload.data : [])
|
|
169
204
|
.map((item) => String((item && item.id) || '').trim())
|
|
170
205
|
.filter(Boolean)
|
|
171
206
|
.slice(0, 200);
|
|
172
207
|
},
|
|
173
|
-
async execute(kind, provider, input, signal) {
|
|
174
|
-
|
|
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,
|
|
175
216
|
model: input.model,
|
|
176
|
-
prompt: promptWithNegative(input),
|
|
177
217
|
n: 1,
|
|
178
|
-
|
|
218
|
+
negative: String((input && input.negativePrompt) || '').trim() || undefined
|
|
179
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 收比例 + 档位。
|
|
180
225
|
if (input.fitted && !input.fitted.omit && input.fitted.width && input.fitted.height) {
|
|
181
226
|
body.aspect_ratio = input.fitted.aspect || nearestAspect(input.fitted.width, input.fitted.height);
|
|
182
227
|
// 只有认 resolution 的底层模型才发档位(fitSize 已经填好 tier);发错了 provider 会 400。
|
|
183
228
|
if (input.fitted.tier) body.resolution = String(input.fitted.tier);
|
|
184
229
|
}
|
|
185
|
-
|
|
186
|
-
if (quality && quality !== 'off' && quality !== 'auto') body.quality = quality;
|
|
230
|
+
// 参考图是 { type, image_url: { url } } 对象(带额外键),六种能力表达不了,留在协议里。
|
|
187
231
|
const references = imageParts(input.images);
|
|
188
232
|
if (references.length) body.input_references = references;
|
|
189
233
|
|
|
190
|
-
|
|
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 };
|
|
234
|
+
return executeBuiltImage(kind, this, provider, input, '/images', body, signal, undefined, hooks);
|
|
201
235
|
}
|
|
202
236
|
},
|
|
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
237
|
{
|
|
219
238
|
id: 'openai-chat',
|
|
220
|
-
label: '
|
|
239
|
+
label: 'protocol.openai-chat',
|
|
240
|
+
acceptsHttpParams: true,
|
|
221
241
|
match: () => false,
|
|
222
242
|
local: false,
|
|
223
243
|
endpoints: { generate: '/chat/completions', edit: '/chat/completions' },
|
|
244
|
+
routes: { generate: '/chat/completions', edit: '/chat/completions', models: '/models' },
|
|
224
245
|
transport: { generate: 'json', edit: 'json' },
|
|
225
246
|
// 这个协议没有尺寸字段:尺寸只能写进提示词(stitch),面板仍给宽高输入框。
|
|
226
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'),
|
|
227
270
|
maxN: 1,
|
|
228
271
|
editFields: [],
|
|
229
|
-
async execute(kind, provider, input, signal) {
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
|
|
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);
|
|
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);
|
|
250
281
|
}
|
|
251
282
|
}
|
|
252
283
|
];
|