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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# 更新日志
|
|
2
2
|
|
|
3
|
+
## v0.7.0
|
|
4
|
+
|
|
5
|
+
1. **尺寸选项优化与严格模式**:现在模型选择器中的size参数会根据模型实际情况调整。严格模式下成品强制等于请求尺寸,比例不符先居中裁切再缩放;结果同时给出成品 `size` 与上游原始 `generated`,原图保留为同目录的 `-original` 文件。
|
|
6
|
+
2. **新增只读状态服务 `moImageStatus`**:插件可直接读取当前提供商/模型、Auto 候选、Key 来源、编辑能力与参考图上限,不必解析 `config.json`、凭据库或猜「编辑模型」。服务只读,不写配置,旧调用方仍按原文件格式工作。
|
|
7
|
+
3. **协议补齐**:新增 Ideogram 4 / 4-5 / 4-transparent 三种协议及万相 wan2.7 档位;新增第三方模型提供商魔搭。
|
|
8
|
+
4. **可靠性优化**:异步提交不再重发语义相同的请求;提交、轮询、下载共用总超时与重试预算,受理状态不明时明确报告。
|
|
9
|
+
5. **异步请求适配**:API协议选择自动检测时,能够同时适配同步请求和异步请求。
|
|
10
|
+
6. **性能与 token 优化**:上游生图任务加并发队列(默认 2 并发、8 排队,排队可取消);ComfyUI 轮询改为退避;生图工具描述与系统提示词缩短了40%左右,减少常驻 token。
|
|
11
|
+
7. **SD参数支持**:支持暴露SD本地模型加载器的参数。
|
|
12
|
+
8. **ComfyUI参数优化**:不再忽略默认为空的参数;尺寸参数默认暴露,不再显示在暴露列表中。
|
|
13
|
+
|
|
3
14
|
## v0.6.0
|
|
4
15
|
|
|
5
16
|
1. 新增 7 个生图协议:OpenAI Responses、OpenRouter 专用 Images、Azure OpenAI、chat/completions 中转,以及 BFL、fal.ai、Replicate 三家异步队列。
|
package/README.md
CHANGED
|
@@ -112,7 +112,7 @@ npm run release -- minor --dry-run # 只看会做什么
|
|
|
112
112
|
|
|
113
113
|
- **质量**占据「推理等级」的位置:滑条按当前模型实际公布的档位排列(Auto 紧挨着 Off,不会被当成最高档),每档一色;拖动/键盘/Home/End 都可改档,最高档触发紫罗兰、星尘动画和「更快消耗使用额度」提示。
|
|
114
114
|
- 点弹层中间的档位标题进入 **provider → 模型**两级下钻,带搜索、当前勾选和「N 档」徽标。
|
|
115
|
-
-
|
|
115
|
+
- **★ 提示词优化** 是左侧工具(真正写进 `defaults.promptOptimization`,不是 Codex 的纯视觉 Fast;悬停显示 Codex 两行卡片「提示词优化 / 用量更多」,开启时滑条走 fast-mode 星尘动画),**齿轮**是右侧「其他设置」工具,只放尺寸设置,内容超高时可滚动。
|
|
116
116
|
- 弹层通过 portal 挂到 `document.body`,不赌输入区祖先没有 transform/filter/backdrop-filter,避免定位偏移和被裁剪。
|
|
117
117
|
|
|
118
118
|
未知的接管型插件没有公共协议,插件只采样它触发器的尺寸/字号/颜色做通用降级,弹层仍用宿主 token 自绘;这样并排不违和,但不承诺逐像素一致。想做到 1:1,需要和对方约定皮肤标记或 CSS 变量。
|
|
@@ -129,13 +129,13 @@ npm run release -- minor --dry-run # 只看会做什么
|
|
|
129
129
|
|
|
130
130
|
模型会在需要时调用:
|
|
131
131
|
|
|
132
|
-
- mo_image_settings
|
|
132
|
+
- mo_image_settings:读当前配置(模型、提示词优化开关、该模型的写法);本会话读过且配置没变时可以不调
|
|
133
133
|
- mo_image_generate:文生图 / 重做
|
|
134
134
|
- mo_image_edit:参考图编辑 / 局部修改
|
|
135
135
|
|
|
136
136
|
工具接收 prompt、negative_prompt、images;`model` 可选,尺寸和质量仍由生图选择器控制(工具不接收 `size` / `quality`)。
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
**一步确认配置**:`mo_image_settings` 返回一个很小的 JSON——当前提供商和模型的唯一 id、提示词优化开关、一句该模型的写法提示(Auto 模式下再加可选模型 id):
|
|
139
139
|
|
|
140
140
|
```json
|
|
141
141
|
{"provider":"comfyui","model":"flux-2-klein-4b.safetensors","promptOptimization":true,"hint":"自然语言散文,主体+动作前置,30–80 词;英文优先;无 negative prompt;完整写法没读过时先加载对应 skill,读过就不用再加载"}
|
|
@@ -143,7 +143,9 @@ npm run release -- minor --dry-run # 只看会做什么
|
|
|
143
143
|
|
|
144
144
|
提示词优化开启时,`hint` 末尾会带这半句:完整写法(skill 里的模板与示例)只在当前模型的还没读过时才要加载,读过就不用重复加载;优化关闭时没有这半句,因为那时直接照用户原话写。
|
|
145
145
|
|
|
146
|
-
|
|
146
|
+
系统提示词的 `mo-image-prompt` 节里有一行「配置状态」,由每个会话自己的快照决定:新会话还没读过时提示先调 `mo_image_settings`;读过之后只要配置没变就一直显示「一致」,模型可以直接生图,不必每轮重读。用户在设置里换了模型、切了提示词优化开关或改了尺寸/工作流参数后,这一行会变回「改过」(改回原值、中间没有对话则不算变化)。读取或交接会把这刻的配置锁成快照:之后配置再变只会产生新快照、让下一次调用重新判断,不会反过来影响已经发出去或已经锁定的那次生成。同一个会话里重复读一次配置,返回的是 `{"unchanged":true,"hint":"…"}`,不会再把整份配置重复一遍;上下文被压缩、已经看不到那份配置时,传 `refresh:true` 重读一次即可。
|
|
147
|
+
|
|
148
|
+
**漏读不会报错**:没读过(或读了之后配置又变了)就调用生图/改图工具,不会发请求、也不会红字报错,而是返回 `{"path":"","needsSettings":true,"settings":{…}}`——把当前配置直接交回给对话模型,让它按新写法重写提示词再调用一次。这样既不会用旧模型的口径生成图片,也不会因为一次手快的调用在界面上留下一条失败记录。
|
|
147
149
|
|
|
148
150
|
**`model` 是可选的核对项**:不传就用用户选中的那一项(提供商 + 模型 id 一起核对,不同提供商的同名模型不算同一个),传了不一致的值直接拒绝;Auto 模式下必须从当前提供商的模型列表里挑一个。传了 `provider` 也会核对,不一致同样拒绝——工具永远不会替用户切换提供商或模型。
|
|
149
151
|
|
|
@@ -155,7 +157,7 @@ npm run release -- minor --dry-run # 只看会做什么
|
|
|
155
157
|
|
|
156
158
|
## 质量档位
|
|
157
159
|
|
|
158
|
-
质量档位由「当前模型」决定,宿主通过 `/api/mo-image/config` 的 `capabilities` 下发。原版皮肤里,没有官方质量参数的模型隐藏这一行;Codex
|
|
160
|
+
质量档位由「当前模型」决定,宿主通过 `/api/mo-image/config` 的 `capabilities` 下发。原版皮肤里,没有官方质量参数的模型隐藏这一行;Codex 皮肤里显示「此模型未提供质量档位」,尺寸仍从齿轮进入,提示词优化用左上角星标切换。GPT-Image 的 `Auto` 是模式而不是画质档,滑条里紧挨着 `Off` 放,不排到最高档。
|
|
159
161
|
|
|
160
162
|
| 模型族 | 质量行 | 选项 |
|
|
161
163
|
|---|---|---|
|
|
@@ -177,7 +179,7 @@ npm run release -- minor --dry-run # 只看会做什么
|
|
|
177
179
|
|
|
178
180
|
## 提示词优化
|
|
179
181
|
|
|
180
|
-
生图模型选择器里新增「提示词优化」,默认 **On**(原版皮肤在首页一栏;Codex
|
|
182
|
+
生图模型选择器里新增「提示词优化」,默认 **On**(原版皮肤在首页一栏;Codex 皮肤是弹层左上角由三颗四角星组成的 AI 星标,悬停显示 Codex 两行卡片「提示词优化 / 用量更多」):
|
|
181
183
|
|
|
182
184
|
- **On**:保持现在的行为,按当前模型的提示词方言扩写/整理 prompt。
|
|
183
185
|
- **Off**:要求模型尽量把用户原话直接传给生图工具,不扩写、不补风格/光线/构图,也不自动补 negative prompt;只有用户明确说“优化/改写提示词”时才优化。
|
|
@@ -206,6 +208,32 @@ $DSH_HOME/mo-image/out/
|
|
|
206
208
|
|
|
207
209
|
建议把 `generated-images/` 加进项目的 `.gitignore`,避免把生成图误提交。
|
|
208
210
|
|
|
211
|
+
## 给其它插件读状态(moImageStatus)
|
|
212
|
+
|
|
213
|
+
0.7.0 起,插件在宿主注册只读服务 `moImageStatus`。同一个宿主里的插件可以:
|
|
214
|
+
|
|
215
|
+
~~~js
|
|
216
|
+
const service = ctx.get('moImageStatus')
|
|
217
|
+
if (service?.contract >= 1) {
|
|
218
|
+
const status = await service.describe()
|
|
219
|
+
}
|
|
220
|
+
~~~
|
|
221
|
+
|
|
222
|
+
`describe()` 返回一份快照,不写配置,也不依赖浏览器状态:
|
|
223
|
+
|
|
224
|
+
| 字段 | 含义 |
|
|
225
|
+
|---|---|
|
|
226
|
+
| `contract` | 契约版本,当前 `1` |
|
|
227
|
+
| `version` | 插件版本号 |
|
|
228
|
+
| `ready` / `configured` / `missing` | 是否可用,以及缺哪一项(`provider` / `baseUrl` / `key` / `model`) |
|
|
229
|
+
| `provider` | `{ id, name, baseUrl, optionalKey, protocol, adapter }` 或 `null` |
|
|
230
|
+
| `model` / `auto` / `models` | 当前模型;Auto 时 `model='auto'` 并带候选列表 |
|
|
231
|
+
| `key` | `{ ok, source, ref }`,`source` 为 `optional` / `credentials` / `env` / `config` |
|
|
232
|
+
| `capabilities` | `{ generate, edit, editModels, maxRefs, size }`;`edit` 按适配器 / ComfyUI 工作流判断,`editModels` 是 Auto 下可改图的模型,`maxRefs` 为 16 |
|
|
233
|
+
| `defaults` / `promptOptimization` | 选择器里的尺寸、质量与提示词优化开关 |
|
|
234
|
+
|
|
235
|
+
调用方必须自己处理服务不存在的情况:0.6 及更早版本没有这个服务,按旧的文件格式回退即可(工作台就是这么做的,并会标注 `source: 'legacy'`)。
|
|
236
|
+
|
|
209
237
|
配置保存在:
|
|
210
238
|
|
|
211
239
|
~~~text
|
|
@@ -240,17 +268,19 @@ $DSH_HOME/.credentials.yaml
|
|
|
240
268
|
| **Black Forest Labs** | `bfl` | `flux-2-pro`、`flux-2-max`、`flux-3-image`、`flux-kontext-pro` | 异步队列;生成 + FLUX.2/FLUX 3 参考图 |
|
|
241
269
|
| **fal.ai** | `fal` | `fal-ai/flux-2-pro`、`fal-ai/flux-pro/kontext` | 异步队列;`COMPLETED` 后还要取一次 `response_url` |
|
|
242
270
|
| **Replicate** | `replicate` | `black-forest-labs/flux-2-pro` | 异步队列;按模型 schema 决定输入字段 |
|
|
271
|
+
| **魔搭 ModelScope** | `modelscope` | `Qwen/Qwen-Image-2.1`、`Qwen/Qwen-Image-Edit` | 异步任务;提交带 `X-ModelScope-Async-Mode`,轮询 `/v1/tasks/{id}` |
|
|
243
272
|
| **xAI Grok Imagine** | `xai-imagine` | `grok-imagine-image` | 生成;原生编辑暂未支持 |
|
|
244
273
|
| **Recraft** | `recraft` | `recraftv4_1`、`recraftv4`、`recraftv3`、`recraftv2` | 生成 + `imageToImage` 编辑 |
|
|
245
|
-
| **Ideogram** | `ideogram` | `ideogram-3`、`ideogram-4` |
|
|
274
|
+
| **Ideogram** | `ideogram` | `ideogram-3`、`ideogram-4`、`ideogram-4-5`、`ideogram-4-transparent` | 生成;模型写在路径里,4 收 JSON+像素枚举,4-5 只收 `size`,3/transparent 收字母 `x` 的比例 |
|
|
246
275
|
| **Stability AI** | `stability` | `core` | 生成 + 编辑(multipart) |
|
|
247
276
|
| **Together AI** | `together` | `black-forest-labs/FLUX.1-schnell`、`FLUX.1-dev` | 原生 width/height 请求;生成 |
|
|
248
277
|
| **Fireworks AI** | `openai-images` | `accounts/fireworks/models/flux-1-schnell` | OpenAI Images 兼容网关 |
|
|
249
278
|
| **Novita AI** | `openai-images` | `flux-1-schnell` | OpenAI Images 兼容网关 |
|
|
250
|
-
|
|
|
279
|
+
| 自定义 / 新网关 | `openai-images` | 手动/获取 | 同步或异步自动识别(见下文) |
|
|
280
|
+
| 火山方舟 | `volcengine-ark` | 手动/获取 | 生成;档位按模型分(5.x 是 `1K/1.5K/2K`,lite 是 `2K/3K/4K`,4.5 是 `2K/4K`),也收任意宽高 |
|
|
251
281
|
| 火山方舟 Agent Plan | `volcengine-ark` | 手动/获取 | 与火山方舟同协议,基础地址换成 `/api/plan/v3` |
|
|
252
282
|
| 阿里云百炼 · Qwen-Image | `dashscope-qwen-image` | `qwen-image-3.0-pro`、`qwen-image-3.0` | 原生多模态同步接口 |
|
|
253
|
-
| 阿里云百炼 · 通义万相 | `dashscope-wanx` | `wan2.6-t2i`、`wanx2.1-t2i-turbo` |
|
|
283
|
+
| 阿里云百炼 · 通义万相 | `dashscope-wanx` | `wan2.6-t2i`、`wan2.7-image-pro`、`wan2.7-image`、`wanx2.1-t2i-turbo` | 异步入队任务;只有 wan2.7 认 `1K/2K/4K` 档位 |
|
|
254
284
|
| 硅基流动 | `siliconflow` | 手动/获取 | 生成 + 单图编辑 |
|
|
255
285
|
| 智谱 BigModel | `zhipu` | `cogview-4`、`glm-image` | 生成;一次只回一张 |
|
|
256
286
|
|
|
@@ -262,6 +292,8 @@ $DSH_HOME/.credentials.yaml
|
|
|
262
292
|
- 这些云端提供商的密钥仍只写入 DSH 凭据库,不写进 `config.json`。
|
|
263
293
|
- 内置列表来自宿主 manifest,客户端不单独维护一份;新增提供商会同时影响设置页和模型列表能力判断。
|
|
264
294
|
- Replicate 的输入字段由模型 schema 决定;schema 拉不到时只发 prompt 并在结果里注明,不会猜字段名。
|
|
295
|
+
- 魔搭是**异步任务**:提交 `POST /v1/images/generations`(带 `X-ModelScope-Async-Mode: true`)拿 `task_id`,再轮询 `GET /v1/tasks/{task_id}`(带 `X-ModelScope-Task-Type: image_generation`),图片在 `output_images`。`size` 是 `1024x1024` 这样的字符串,网关按每边 64–2048 校验(插件会提前把超界尺寸按比例收进区间);参考图走 `image_url`,一张发字符串、多张发数组,data URI 可直接用。提交响应可能提前回 `SUCCEED` 但还没有图,插件一律以轮询结果为准。
|
|
296
|
+
- 自定义模型 API 和不认识的域名走 `openai-images`:响应里有图就同步返回,有任务信号就自动轮询,新厂商大多不用再写适配器。识别不了的异步形状(需要专用请求头/轮询头)仍走专用适配器。
|
|
265
297
|
|
|
266
298
|
文档:
|
|
267
299
|
|
|
@@ -273,6 +305,7 @@ $DSH_HOME/.credentials.yaml
|
|
|
273
305
|
- [Together AI image generation](https://docs.together.ai/docs/inference/images/overview)
|
|
274
306
|
- [Fireworks AI](https://docs.fireworks.ai/)
|
|
275
307
|
- [Novita AI](https://novita.ai/docs/api-reference/api-reference-overview)
|
|
308
|
+
- [ModelScope API-Inference](https://www.modelscope.cn/docs/model-service/API-Inference/intro)
|
|
276
309
|
|
|
277
310
|
## 本地模型
|
|
278
311
|
|
|
@@ -288,7 +321,7 @@ $DSH_HOME/.credentials.yaml
|
|
|
288
321
|
| AUTOMATIC1111 / Forge / SD.Next / Draw Things | `a1111` | `http://127.0.0.1:7860` | `/sdapi/v1/sd-models` | 生成 + `/sdapi/v1/img2img` |
|
|
289
322
|
| ComfyUI | `comfyui` | `http://127.0.0.1:8188` | `/object_info`(CheckpointLoaderSimple) | 生成;原生编辑暂不支持 |
|
|
290
323
|
| SwarmUI | `swarmui` | `http://127.0.0.1:7801` | `/API/ListModels` | 生成;原生编辑暂不支持 |
|
|
291
|
-
| 其他 OpenAI 兼容网关 | `openai-images` | `http://127.0.0.1:3000/v1` | `/v1/models` |
|
|
324
|
+
| 其他 OpenAI 兼容网关 | `openai-images` | `http://127.0.0.1:3000/v1` | `/v1/models` | 取决于网关;异步任务自动轮询 |
|
|
292
325
|
|
|
293
326
|
说明:
|
|
294
327
|
|
|
@@ -365,35 +398,42 @@ $DSH_HOME/.credentials.yaml
|
|
|
365
398
|
| 字段 | 作用 |
|
|
366
399
|
|---|---|
|
|
367
400
|
| `send[]` | 白名单:只有列出来的字段才会写进请求体,没列的一律不发 |
|
|
368
|
-
| `size` |
|
|
401
|
+
| `size` | 尺寸怎么编码、该模型接受哪些尺寸。`control` 决定选择器给什么控件(`wh` 宽高 / `aspect` 比例+档位 / `preset` 档位下拉+自定义 / `prompt` 宽高写进提示词),`wire` 说明上游真实吃什么,`tiers` 是档位名与像素,`allowed` 是白名单(`allowCustom: true` 时白名单外的宽高也照发),`sizeByModel` 按模型覆盖整套策略 |
|
|
369
402
|
| `quality` | 该模型是否有质量档位、档位名称/数量,以及如何映射成平台参数(如 DALL·E 的 `standard/hd`、Ideogram 的 `rendering_speed`);没有就不下发,选择器也不显示质量行 |
|
|
370
403
|
| `transport` | `json` 或 `multipart` |
|
|
371
404
|
| `response` | 从任意形状的回应里挖出图片(b64 / data-url / 外链 / 二进制都认) |
|
|
372
405
|
| `authHeader` | `bearer`(默认)/ `api-key`(Azure)/ `x-key`(BFL)/ `fal-key`(fal) |
|
|
373
406
|
| `endpoints` | 可以是字符串,也可以是按模型拼路径的函数(Ideogram、fal、Replicate) |
|
|
374
407
|
| `async` + `poll` | 异步入队型;新描述符自带 `build / submitPath / pollUrl / pollHeaders / statusOf / resolve` |
|
|
408
|
+
| `autoAsync` | 通用兜底自己识别:响应里有图就同步返回,有任务 id / 轮询地址就跟着轮询(自定义 API 不需要选它) |
|
|
375
409
|
| `compose` | body 需要嵌套(如 `{input, parameters}`)时把白名单字段折叠进去 |
|
|
376
410
|
|
|
377
|
-
内置适配器:`openai-images
|
|
411
|
+
内置适配器:`openai-images`(通用兜底,自动同步/异步)、`openai-responses`、`openrouter-images`、`azure-openai`、`openai-chat`(中转)、`gpt-image`、`dall-e-3`、`google-gemini`、`xai-imagine`、`recraft`、`ideogram`、`stability`、`together`、`siliconflow`、`volcengine-ark`、`dashscope-qwen-image`、`dashscope-wanx`、`zhipu`、`bfl`、`fal`、`replicate`、`modelscope`、`a1111`、`comfyui`、`swarmui`、`localai`。
|
|
378
412
|
|
|
379
|
-
|
|
413
|
+
交互形态都覆盖:
|
|
380
414
|
|
|
381
415
|
- **同步**:一次请求直接返回图片
|
|
382
|
-
- **异步入队**:提交后拿到 `task_id` / `polling_url` / `status_url`,轮询到成功再下载;百炼万相、BFL、fal、Replicate
|
|
416
|
+
- **异步入队**:提交后拿到 `task_id` / `polling_url` / `status_url`,轮询到成功再下载;百炼万相、BFL、fal、Replicate、魔搭五家的提交与状态词都收在适配器的 `async` 描述符里
|
|
417
|
+
- **通用兜底自动识别**:自定义 API 和不认识的域名走 `openai-images`,它会自己判断——响应里直接有图就同步返回;有 `task_id`、`poll_url`、`status_url`、`urls.get`、`Location` 之类的任务信号就转轮询。轮询地址优先用响应给的,没给就按常见约定推 `/v1/tasks/{id}`、`/tasks/{id}`、提交地址`/{id}`,前一个 404/405 自动换下一个;状态词认 `SUCCEEDED` / `SUCCESS` / `COMPLETED` / `READY` 和 `FAILED` / `ERROR` / `CANCELED` 等,图出现在 `output_images`、`images`、`data`、`output`、`result` 里都能取到。所以接入新的异步厂商时,通常只要在「自定义模型 API」里填地址、Key 和模型 ID,不用再加适配器。
|
|
383
418
|
|
|
384
419
|
选择规则:设置页的「API 协议」下拉可直接指定适配器;选「自动识别」时按「`provider.adapter` 显式值 → API 域名 → 模型 ID → `openai-images` 兜底」匹配。
|
|
385
420
|
|
|
386
421
|
## 已知限制
|
|
387
422
|
|
|
388
|
-
-
|
|
423
|
+
- **通用异步识别有边界**:`openai-images` 的自动轮询只覆盖「OpenAI 兼容形状 + 常见任务字段/状态词」。如果厂商要求专用请求头(如魔搭的 `X-ModelScope-Async-Mode`)、专用轮询头、或者完全不同的 envelope,仍然需要专用适配器——这类平台已经内置了适配器(`modelscope`、`bfl`、`fal`、`replicate`、`dashscope-wanx`)。
|
|
424
|
+
- **新协议只做了 mock 验证**:`openai-responses`、`openrouter-images`、`azure-openai`、`openai-chat`、`bfl`、`fal`、`replicate`、`modelscope` 的协议形状来自 2026-11 官方文档/官方示例代码,但本机没有对应账号,未做真实连通验证。真实调用若报字段错误,请带上 `$DSH_HOME/mo-image/host.log` 反馈。
|
|
389
425
|
- **gpt-image 的新参数没做 UI**:`background` / `moderation` / `output_compression` 等 2026 年新增字段暂不发送,等设置页有入口再接。
|
|
390
426
|
- **C 级协议未实现**:Krea、Luma Photon、Runway、腾讯混元、百度文心一格、Adobe Firefly、Gemini Interactions API 留在后续版本。
|
|
391
427
|
- **Google Imagen 已停用**:Gemini API 下线、Vertex 侧 2026-06-30 停服,插件不再支持;请改用 `gemini-3.1-flash-image` 等 Nano Banana 模型。
|
|
392
428
|
- **Vertex AI 未支持**:需要 OAuth access token(1 小时过期)且没有模型列表端点。
|
|
393
429
|
|
|
394
|
-
|
|
430
|
+
**尺寸控件跟着模型变**:选择器的「尺寸」面板按当前模型能接受什么渲染——收宽高就给两个输入框(`wh`)、只收宽高比就给比例下拉加档位下拉(`aspect`)、只认档位/固定像素表就给尺寸下拉加「自定义」(`preset`)、完全不收尺寸参数就给宽高输入框并把尺寸写进提示词(`prompt`)。自动尺寸复选框仍在上方;**严格**复选框始终可用,自动尺寸下模型自己填了 `size` 时同样生效。
|
|
431
|
+
|
|
432
|
+
**严格模式**:开启后成品尺寸一定等于请求尺寸——上游出大了就缩、出小了就放大(会注明画质有损),比例不符时先按居中裁切再缩,绝不拉伸。工具结果同时给 `size`(成品)和 `generated`(上游原始尺寸);缩过图时上游那张原图会留在同一目录(文件名加 `-original`),结果的 `original` 指向它,缩放失败也会明说并保留原图。档位型模型(如方舟的 `2K`)的档位只是参考值,官方并不保证像素,这种情况下严格模式不缩,只回报真实尺寸。
|
|
433
|
+
|
|
434
|
+
**尺寸被拒不再自动改大**:上游报「尺寸超限/非法」时直接失败,并把该模型的合法范围写进错误(白名单、档位名,或像素/单边/倍数约束)。白名单吸附与像素夹取仍然保留——它们是把你的尺寸落成该模型真正接受的尺寸,和「偷偷换一个更大尺寸再试」不是一回事。
|
|
395
435
|
|
|
396
|
-
|
|
436
|
+
**错误自愈**:上游报「不认识某某参数」时摘掉该字段重试;429 / 5xx 退避重试。降级过程都会写日志,方便回头把描述符改准。
|
|
397
437
|
|
|
398
438
|
新增一家平台:在 `lib/adapters.js` 里加一条描述符即可,不用动工具层和前端。
|
|
399
439
|
|
package/icon.png
ADDED
|
Binary file
|
package/lib/adapter-kit.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { request } from './net.js';
|
|
8
8
|
import { localizedError } from './localized-error.js';
|
|
9
|
+
import { isPng, pngHeaderOf } from './png-scale.js';
|
|
9
10
|
|
|
10
11
|
export const ASPECT_CHOICES = ['1:1', '2:3', '3:2', '3:4', '4:3', '4:5', '5:4', '9:16', '16:9', '21:9'];
|
|
11
12
|
|
|
@@ -123,22 +124,131 @@ export function getPath(node, path) {
|
|
|
123
124
|
return current === undefined ? null : current;
|
|
124
125
|
}
|
|
125
126
|
|
|
127
|
+
/** 统一成可安全读写的 Buffer;其它类型(包括 Uint8Array 视图)也兼容。 */
|
|
128
|
+
function asBuffer(value) {
|
|
129
|
+
if (Buffer.isBuffer(value)) return value;
|
|
130
|
+
if (value instanceof Uint8Array) return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function asciiAt(buffer, offset, text) {
|
|
135
|
+
return buffer.length >= offset + text.length
|
|
136
|
+
&& buffer.toString('ascii', offset, offset + text.length) === text;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function pngSizeOf(buffer) {
|
|
140
|
+
if (!isPng(buffer)) return null;
|
|
141
|
+
const header = pngHeaderOf(buffer);
|
|
142
|
+
return header ? { width: header.width, height: header.height } : null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function gifSizeOf(buffer) {
|
|
146
|
+
// 6 字节签名 + 7 字节逻辑屏幕描述符;不足就连宽高都读不到。
|
|
147
|
+
if (buffer.length < 13) return null;
|
|
148
|
+
if (!asciiAt(buffer, 0, 'GIF87a') && !asciiAt(buffer, 0, 'GIF89a')) return null;
|
|
149
|
+
const width = buffer.readUInt16LE(6);
|
|
150
|
+
const height = buffer.readUInt16LE(8);
|
|
151
|
+
return width > 0 && height > 0 ? { width: width, height: height } : null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function webpSizeOf(buffer) {
|
|
155
|
+
// RIFF 头 12 字节 + 首块头 8 字节。
|
|
156
|
+
if (buffer.length < 20) return null;
|
|
157
|
+
if (!asciiAt(buffer, 0, 'RIFF') || !asciiAt(buffer, 8, 'WEBP')) return null;
|
|
158
|
+
const riffSize = buffer.readUInt32LE(4);
|
|
159
|
+
// 有些来源只写首块头、把 riffSize 留成 0,按未声明处理;
|
|
160
|
+
// 一旦声明就必须让容器落在 buffer 内,截断的图不能再当有效图片。
|
|
161
|
+
if (riffSize !== 0 && (riffSize < 12 || riffSize > buffer.length - 8)) return null;
|
|
162
|
+
const chunkSize = buffer.readUInt32LE(16);
|
|
163
|
+
if (chunkSize !== 0 && chunkSize > buffer.length - 20) return null;
|
|
164
|
+
const chunk = buffer.toString('ascii', 12, 16);
|
|
165
|
+
if (chunk === 'VP8 ') {
|
|
166
|
+
if (buffer.length < 30) return null;
|
|
167
|
+
const width = buffer.readUInt16LE(26) & 0x3fff;
|
|
168
|
+
const height = buffer.readUInt16LE(28) & 0x3fff;
|
|
169
|
+
return width > 0 && height > 0 ? { width: width, height: height } : null;
|
|
170
|
+
}
|
|
171
|
+
if (chunk === 'VP8L') {
|
|
172
|
+
if (buffer.length < 25) return null;
|
|
173
|
+
const bits = buffer.readUInt32LE(21);
|
|
174
|
+
return { width: (bits & 0x3fff) + 1, height: ((bits >>> 14) & 0x3fff) + 1 };
|
|
175
|
+
}
|
|
176
|
+
if (chunk === 'VP8X') {
|
|
177
|
+
if (buffer.length < 30) return null;
|
|
178
|
+
const width = (buffer[24] | (buffer[25] << 8) | (buffer[26] << 16)) + 1;
|
|
179
|
+
const height = (buffer[27] | (buffer[28] << 8) | (buffer[29] << 16)) + 1;
|
|
180
|
+
return { width: width, height: height };
|
|
181
|
+
}
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function isSofMarker(marker) {
|
|
186
|
+
return (marker >= 0xc0 && marker <= 0xc3)
|
|
187
|
+
|| (marker >= 0xc5 && marker <= 0xc7)
|
|
188
|
+
|| (marker >= 0xc9 && marker <= 0xcb)
|
|
189
|
+
|| (marker >= 0xcd && marker <= 0xcf);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function jpegSizeOf(buffer) {
|
|
193
|
+
if (buffer.length < 4 || buffer[0] !== 0xff || buffer[1] !== 0xd8) return null;
|
|
194
|
+
let offset = 2;
|
|
195
|
+
while (offset + 4 <= buffer.length) {
|
|
196
|
+
if (buffer[offset] !== 0xff) { offset++; continue; }
|
|
197
|
+
const marker = buffer[offset + 1];
|
|
198
|
+
if (marker === 0xff) { offset++; continue; } // 填充字节
|
|
199
|
+
if (marker === 0x00) { offset += 2; continue; } // 扫描数据里的转义
|
|
200
|
+
if (marker === 0xd8 || marker === 0xd9 || marker === 0x01
|
|
201
|
+
|| (marker >= 0xd0 && marker <= 0xd7)) { offset += 2; continue; }
|
|
202
|
+
if (marker === 0xda) return null; // 都到扫描数据了还没见 SOF
|
|
203
|
+
const length = buffer.readUInt16BE(offset + 2);
|
|
204
|
+
if (length < 2) return null;
|
|
205
|
+
// 整段必须落在 buffer 内:只够读宽高、段尾却被截断的 JPEG 仍是坏图。
|
|
206
|
+
const next = offset + 2 + length;
|
|
207
|
+
if (next > buffer.length) return null;
|
|
208
|
+
if (isSofMarker(marker)) {
|
|
209
|
+
// SOF 段头至少要覆盖精度、高、宽和分量数;不足就当作截断。
|
|
210
|
+
if (length < 8 || offset + 9 > buffer.length) return null;
|
|
211
|
+
const height = buffer.readUInt16BE(offset + 5);
|
|
212
|
+
const width = buffer.readUInt16BE(offset + 7);
|
|
213
|
+
return width > 0 && height > 0 ? { width: width, height: height } : null;
|
|
214
|
+
}
|
|
215
|
+
offset = next;
|
|
216
|
+
}
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 从文件头读出真实宽高(不解码整张图)。
|
|
222
|
+
* PNG / JPEG / WebP / GIF 足够覆盖各家回图;结构不完整或认不出返回 null。
|
|
223
|
+
*/
|
|
224
|
+
export function imageSizeOf(buffer) {
|
|
225
|
+
const bytes = asBuffer(buffer);
|
|
226
|
+
if (!bytes) return null;
|
|
227
|
+
try {
|
|
228
|
+
if (isPng(bytes)) return pngSizeOf(bytes);
|
|
229
|
+
const gif = gifSizeOf(bytes);
|
|
230
|
+
if (gif) return gif;
|
|
231
|
+
const webp = webpSizeOf(bytes);
|
|
232
|
+
if (webp) return webp;
|
|
233
|
+
return jpegSizeOf(bytes);
|
|
234
|
+
} catch (_error) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
126
239
|
export function sniffType(buffer) {
|
|
127
|
-
|
|
128
|
-
if (
|
|
129
|
-
if (
|
|
130
|
-
if (
|
|
131
|
-
if (
|
|
240
|
+
const bytes = asBuffer(buffer);
|
|
241
|
+
if (!bytes) return 'image/png';
|
|
242
|
+
if (isPng(bytes)) return 'image/png';
|
|
243
|
+
if (bytes.length >= 2 && bytes[0] === 0xff && bytes[1] === 0xd8) return 'image/jpeg';
|
|
244
|
+
if (asciiAt(bytes, 0, 'RIFF') && asciiAt(bytes, 8, 'WEBP')) return 'image/webp';
|
|
245
|
+
if (asciiAt(bytes, 0, 'GIF87a') || asciiAt(bytes, 0, 'GIF89a')) return 'image/gif';
|
|
132
246
|
return 'image/png';
|
|
133
247
|
}
|
|
134
248
|
|
|
249
|
+
/** 只有能读出合法宽高的文件头才算图片;截断内容不能混进参考图或下载结果。 */
|
|
135
250
|
export function looksLikeImage(buffer) {
|
|
136
|
-
|
|
137
|
-
const type = sniffType(buffer);
|
|
138
|
-
if (type === 'image/png') return buffer[0] === 0x89 && buffer[1] === 0x50;
|
|
139
|
-
if (type === 'image/jpeg') return buffer[0] === 0xff && buffer[1] === 0xd8;
|
|
140
|
-
if (type === 'image/webp') return buffer.slice(0, 4).toString('ascii') === 'RIFF' && buffer.slice(8, 12).toString('ascii') === 'WEBP';
|
|
141
|
-
return buffer.slice(0, 3).toString('ascii') === 'GIF';
|
|
251
|
+
return imageSizeOf(buffer) !== null;
|
|
142
252
|
}
|
|
143
253
|
|
|
144
254
|
export function base64ToImage(text) {
|
|
@@ -256,6 +366,248 @@ export async function fetchImageUrl(url, signal, maxBytes = MAX_IMAGE_BYTES) {
|
|
|
256
366
|
return { bytes: bytes, contentType: sniffType(bytes) };
|
|
257
367
|
}
|
|
258
368
|
|
|
369
|
+
/* ---------------------------------------------------------- 通用异步任务 */
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* 通用「提交拿任务 → 轮询拿图」形状的识别与轮询辅助,不绑定任何厂商。
|
|
373
|
+
*
|
|
374
|
+
* 覆盖的是 OpenAI 兼容网关里最常见的那套约定:
|
|
375
|
+
* - 任务 id:task_id / taskId / job_id / request_id / generation_id / prediction_id / id
|
|
376
|
+
* - 轮询地址:polling_url / status_url / task_url / get_url / urls.get ...
|
|
377
|
+
* - 状态词:SUCCEEDED / SUCCESS / COMPLETED / READY ...;FAILED / ERROR / CANCELED ...
|
|
378
|
+
* - 结果图:output_images / images / data / output / result 里的 url / b64_json
|
|
379
|
+
*
|
|
380
|
+
* 认不出来时不猜:调用方按同步响应处理,或者报「格式不认识」。
|
|
381
|
+
*/
|
|
382
|
+
const TASK_ID_KEYS = [
|
|
383
|
+
'task_id', 'taskId', 'job_id', 'jobId', 'request_id', 'requestId',
|
|
384
|
+
'generation_id', 'generationId', 'prediction_id', 'predictionId', 'id'
|
|
385
|
+
];
|
|
386
|
+
const TASK_URL_KEYS = [
|
|
387
|
+
'polling_url', 'pollingUrl', 'poll_url', 'pollUrl', 'status_url', 'statusUrl',
|
|
388
|
+
'task_url', 'taskUrl', 'get_url', 'getUrl', 'result_url', 'resultUrl'
|
|
389
|
+
];
|
|
390
|
+
const TASK_IMAGE_KEYS = [
|
|
391
|
+
'output_images', 'outputImages', 'images', 'image_urls', 'data', 'outputs', 'output',
|
|
392
|
+
'result', 'results', 'artifacts', 'files', 'url', 'image_url', 'imageUrl', 'b64_json', 'base64', 'image'
|
|
393
|
+
];
|
|
394
|
+
const TASK_SUCCESS = new Set([
|
|
395
|
+
'SUCCEED', 'SUCCEEDED', 'SUCCESS', 'SUCCESSFUL', 'COMPLETED', 'COMPLETE', 'DONE', 'FINISHED', 'READY'
|
|
396
|
+
]);
|
|
397
|
+
const TASK_FAILURE = new Set([
|
|
398
|
+
'FAILED', 'FAILURE', 'ERROR', 'CANCELED', 'CANCELLED', 'ABORTED', 'REJECTED', 'EXPIRED', 'MODERATED'
|
|
399
|
+
]);
|
|
400
|
+
|
|
401
|
+
/** 任务载荷的常见嵌套层:顶层、data、output、result(数组不算,避免把结果图当任务)。 */
|
|
402
|
+
function taskNodes(payload) {
|
|
403
|
+
const nodes = [];
|
|
404
|
+
const visit = (node, depth) => {
|
|
405
|
+
if (!node || typeof node !== 'object' || Array.isArray(node) || depth > 2) return;
|
|
406
|
+
nodes.push(node);
|
|
407
|
+
for (const key of ['data', 'output', 'result']) visit(node[key], depth + 1);
|
|
408
|
+
};
|
|
409
|
+
visit(payload, 0);
|
|
410
|
+
return nodes;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function firstTaskString(nodes, keys) {
|
|
414
|
+
// 字段名的优先级高于层级:嵌套的 task_id 应该压过顶层的通用 id。
|
|
415
|
+
for (const key of keys) {
|
|
416
|
+
for (const node of nodes) {
|
|
417
|
+
if (typeof node[key] === 'string' && node[key].trim()) return node[key].trim();
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return '';
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function urlsGetOf(nodes) {
|
|
424
|
+
for (const node of nodes) {
|
|
425
|
+
if (node.urls && typeof node.urls.get === 'string' && node.urls.get.trim()) return node.urls.get.trim();
|
|
426
|
+
}
|
|
427
|
+
return '';
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** 从提交响应里认任务:只要拿到任务 id 或轮询地址,就当异步任务。 */
|
|
431
|
+
export function readTaskEnvelope(payload) {
|
|
432
|
+
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) return null;
|
|
433
|
+
const nodes = taskNodes(payload);
|
|
434
|
+
const pollUrl = firstTaskString(nodes, TASK_URL_KEYS) || urlsGetOf(nodes);
|
|
435
|
+
const taskId = firstTaskString(nodes, TASK_ID_KEYS);
|
|
436
|
+
if (!pollUrl && !taskId) return null;
|
|
437
|
+
return { taskId: taskId, pollUrl: pollUrl };
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function absoluteTaskUrl(value, base) {
|
|
441
|
+
const text = String(value || '').trim();
|
|
442
|
+
if (!text) return '';
|
|
443
|
+
if (/^https?:\/\//i.test(text)) return text;
|
|
444
|
+
try {
|
|
445
|
+
return new URL(text, base.endsWith('/') ? base : base + '/').toString();
|
|
446
|
+
} catch (_error) {
|
|
447
|
+
return '';
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* 轮询候选地址:响应里给的优先;没给就按常见约定推两个,最后再试提交地址 + 任务 id。
|
|
453
|
+
* base 以 /v1 结尾时是 /v1/tasks/{id},只有站点根时补 /v1。
|
|
454
|
+
*/
|
|
455
|
+
export function taskPollUrls(provider, submission, submitUrl) {
|
|
456
|
+
const base = baseUrlOf(provider);
|
|
457
|
+
const nodes = taskNodes(submission || {});
|
|
458
|
+
const raw = [];
|
|
459
|
+
const direct = firstTaskString(nodes, TASK_URL_KEYS) || urlsGetOf(nodes);
|
|
460
|
+
if (direct) raw.push(direct);
|
|
461
|
+
const taskId = firstTaskString(nodes, TASK_ID_KEYS);
|
|
462
|
+
if (taskId) {
|
|
463
|
+
const id = encodeURIComponent(taskId);
|
|
464
|
+
const root = base.replace(/\/v\d+[a-z0-9]*$/i, '');
|
|
465
|
+
raw.push(root + '/v1/tasks/' + id);
|
|
466
|
+
raw.push(base + '/tasks/' + id);
|
|
467
|
+
if (submitUrl) raw.push(String(submitUrl).replace(/\/+$/, '') + '/' + id);
|
|
468
|
+
}
|
|
469
|
+
const urls = [];
|
|
470
|
+
for (const item of raw) {
|
|
471
|
+
const resolved = absoluteTaskUrl(item, base);
|
|
472
|
+
if (resolved && !urls.includes(resolved)) urls.push(resolved);
|
|
473
|
+
}
|
|
474
|
+
return urls;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function statusTextOf(nodes) {
|
|
478
|
+
// 任务专用状态词优先:顶层 status 可能只是「请求成功」,不能当成任务完成。
|
|
479
|
+
for (const key of ['task_status', 'taskStatus', 'status', 'state']) {
|
|
480
|
+
for (const node of nodes) {
|
|
481
|
+
const value = node[key];
|
|
482
|
+
if (typeof value === 'string' && value.trim()) return value.trim().toUpperCase();
|
|
483
|
+
if (value && typeof value === 'object') {
|
|
484
|
+
const nested = value.status || value.state;
|
|
485
|
+
if (typeof nested === 'string' && nested.trim()) return nested.trim().toUpperCase();
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return '';
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function errorTextOf(value, depth = 0) {
|
|
493
|
+
if (depth > 4 || value === null || value === undefined) return '';
|
|
494
|
+
if (typeof value === 'string') return value.trim();
|
|
495
|
+
if (Array.isArray(value)) {
|
|
496
|
+
for (const item of value) {
|
|
497
|
+
const text = errorTextOf(item, depth + 1);
|
|
498
|
+
if (text) return text;
|
|
499
|
+
}
|
|
500
|
+
return '';
|
|
501
|
+
}
|
|
502
|
+
if (typeof value !== 'object') return '';
|
|
503
|
+
for (const key of ['message', 'msg', 'detail', 'error', 'errors', 'reason', 'logs', 'fail_reason', 'error_message']) {
|
|
504
|
+
if (value[key] === undefined) continue;
|
|
505
|
+
const text = errorTextOf(value[key], depth + 1);
|
|
506
|
+
if (text) return text;
|
|
507
|
+
}
|
|
508
|
+
return '';
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/** 明确的错误字段;没有状态词时用它判断任务失败,不去猜普通的 message。 */
|
|
512
|
+
export function taskErrorOf(payload) {
|
|
513
|
+
if (!payload || typeof payload !== 'object') return '';
|
|
514
|
+
for (const node of taskNodes(payload)) {
|
|
515
|
+
for (const key of ['error', 'errors', 'fail_reason', 'error_message']) {
|
|
516
|
+
if (node[key] === undefined || node[key] === null) continue;
|
|
517
|
+
const text = errorTextOf(node[key]);
|
|
518
|
+
if (text) return text;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return '';
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/** 失败任务的人话原因:先找错误字段,再退到 message / detail / logs。 */
|
|
525
|
+
export function taskMessageOf(payload) {
|
|
526
|
+
if (!payload || typeof payload !== 'object') return '';
|
|
527
|
+
const error = taskErrorOf(payload);
|
|
528
|
+
if (error) return error;
|
|
529
|
+
for (const node of taskNodes(payload)) {
|
|
530
|
+
for (const key of ['message', 'msg', 'detail', 'logs', 'reason']) {
|
|
531
|
+
if (node[key] === undefined || node[key] === null) continue;
|
|
532
|
+
const text = errorTextOf(node[key]);
|
|
533
|
+
if (text) return text;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return '';
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function imageFromValue(value, depth = 0) {
|
|
540
|
+
if (depth > 6 || value === null || value === undefined) return null;
|
|
541
|
+
if (typeof value === 'string') {
|
|
542
|
+
const text = value.trim();
|
|
543
|
+
if (!text) return null;
|
|
544
|
+
const decoded = base64ToImage(text);
|
|
545
|
+
if (decoded) return { bytes: decoded.bytes, contentType: decoded.contentType };
|
|
546
|
+
if (/^https?:\/\//i.test(text)) return { url: text };
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
if (Array.isArray(value)) {
|
|
550
|
+
for (const item of value) {
|
|
551
|
+
const hit = imageFromValue(item, depth + 1);
|
|
552
|
+
if (hit) return hit;
|
|
553
|
+
}
|
|
554
|
+
return null;
|
|
555
|
+
}
|
|
556
|
+
if (typeof value === 'object') {
|
|
557
|
+
for (const key of TASK_IMAGE_KEYS) {
|
|
558
|
+
if (value[key] === undefined) continue;
|
|
559
|
+
const hit = imageFromValue(value[key], depth + 1);
|
|
560
|
+
if (hit) return hit;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return null;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function containerImageOf(payload) {
|
|
567
|
+
for (const key of ['output_images', 'outputImages', 'images', 'image_urls', 'data', 'outputs', 'output', 'result', 'results', 'artifacts', 'files']) {
|
|
568
|
+
if (payload[key] === undefined) continue;
|
|
569
|
+
const hit = imageFromValue(payload[key], 1);
|
|
570
|
+
if (hit) return hit;
|
|
571
|
+
}
|
|
572
|
+
return null;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* 只看约定的结果字段,避免把回显的参考图或状态地址当结果。
|
|
577
|
+
* containersOnly 用于「任务还在跑」的载荷:连顶层的 url / image 都不认,
|
|
578
|
+
* 免得把任务详情地址当成生成图去下载。
|
|
579
|
+
*/
|
|
580
|
+
export function taskImageOf(payload, options) {
|
|
581
|
+
if (!payload || typeof payload !== 'object') return null;
|
|
582
|
+
if (options && options.containersOnly) return containerImageOf(payload);
|
|
583
|
+
return imageFromValue(payload);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** 任务已成功但形状不常见时,再按「任意位置的图片」兜底扫一遍。 */
|
|
587
|
+
export function looseImageOf(payload) {
|
|
588
|
+
try {
|
|
589
|
+
return decodeImagePayload(payload, null) || null;
|
|
590
|
+
} catch (_error) {
|
|
591
|
+
return null;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/** 把任务载荷收敛成三态:成功词、失败词、其余都算进行中。 */
|
|
596
|
+
export function taskStatusOf(payload) {
|
|
597
|
+
const nodes = taskNodes(payload);
|
|
598
|
+
const status = statusTextOf(nodes);
|
|
599
|
+
if (status) {
|
|
600
|
+
if (TASK_SUCCESS.has(status)) return { state: 'success', status: status };
|
|
601
|
+
if (TASK_FAILURE.has(status)) {
|
|
602
|
+
return { state: 'failure', status: status, message: taskMessageOf(payload) || status };
|
|
603
|
+
}
|
|
604
|
+
return { state: 'pending', status: status };
|
|
605
|
+
}
|
|
606
|
+
const error = taskErrorOf(payload);
|
|
607
|
+
if (error) return { state: 'failure', status: '', message: error };
|
|
608
|
+
return { state: 'pending', status: '' };
|
|
609
|
+
}
|
|
610
|
+
|
|
259
611
|
export function extractMessage(text) {
|
|
260
612
|
const raw = String(text || '').trim();
|
|
261
613
|
if (!raw) return '';
|