mcp-duojie-image 0.9.0 → 0.10.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/README.md +74 -16
- package/dist/errors.js +3 -1
- package/dist/image-processing.js +26 -0
- package/dist/index.js +112 -111
- package/dist/prompt.js +54 -13
- package/dist/provider.js +182 -82
- package/dist/resizer.js +1 -1
- package/dist/task-response.js +49 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,12 +5,12 @@ MCP server for image generation via duojie.games GPT image API.
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- Text-to-image generation with `gpt-image-2`
|
|
8
|
-
-
|
|
8
|
+
- Two business operations: new-image generation with optional references, and explicit local editing
|
|
9
9
|
- Prompt structuring helpers for use case, asset type, constraints, and effective prompt echo
|
|
10
10
|
- Optional chroma-key transparent background post-processing
|
|
11
|
-
- Async submission with task
|
|
11
|
+
- Async submission with explicit accepted/terminal semantics and required task polling
|
|
12
12
|
- File saving with overwrite/conflict handling
|
|
13
|
-
-
|
|
13
|
+
- Size request templates for `1K`, `2K`, and `4K`
|
|
14
14
|
|
|
15
15
|
## Configuration
|
|
16
16
|
|
|
@@ -23,7 +23,12 @@ Required:
|
|
|
23
23
|
|
|
24
24
|
## Tool: `generate_image`
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
Creates a new image. `reference_images` and `input_images` are optional guidance
|
|
27
|
+
for style, identity, composition, or material; they are never edit canvases.
|
|
28
|
+
A submitted response is not a successful generation. Call
|
|
29
|
+
`check_image_task` until it returns `status: "done"` or `status: "failed"`.
|
|
30
|
+
Non-terminal responses intentionally omit `success`; only terminal responses use
|
|
31
|
+
`success: true` or `success: false`.
|
|
27
32
|
|
|
28
33
|
Required parameters:
|
|
29
34
|
|
|
@@ -39,7 +44,7 @@ Optional parameters:
|
|
|
39
44
|
|---|---|---:|---|
|
|
40
45
|
| `size` | `1K` \| `2K` \| `4K` | `1K` | Output size tier |
|
|
41
46
|
| `aspect_ratio` | `1:1` \| `3:2` \| `2:3` \| `16:9` \| `9:16` | `1:1` | Verified ratio template |
|
|
42
|
-
| `reference_images` | string[] | - |
|
|
47
|
+
| `reference_images` | string[] | - | Local references used to create a new image |
|
|
43
48
|
| `input_images` | object[] | - | Structured local image inputs with `path`, optional `role`, and optional `note` |
|
|
44
49
|
| `use_case` | string | - | Prompt taxonomy slug, for example `product-mockup` or `background-extraction` |
|
|
45
50
|
| `asset_type` | string | - | Where the asset will be used |
|
|
@@ -49,47 +54,80 @@ Optional parameters:
|
|
|
49
54
|
| `format` | `png` \| `jpg` \| `jpeg` | `png` | Output format; content is encoded to match the extension |
|
|
50
55
|
| `overwrite` | boolean | `false` | Overwrite existing file |
|
|
51
56
|
| `negative_prompt` | string | - | Content to avoid |
|
|
52
|
-
| `reference_max_side` | number | `
|
|
57
|
+
| `reference_max_side` | number | `512` / `256` | Default 512 for one generation reference and 256 for multiple |
|
|
53
58
|
| `preserve_reference_detail` | boolean | `false` | Upload reference inputs without resizing |
|
|
54
59
|
| `silent` | boolean | `false` | Disable desktop notification |
|
|
55
60
|
|
|
56
61
|
Supported `input_images.role` values:
|
|
57
62
|
|
|
58
63
|
- `reference`
|
|
59
|
-
- `edit_target`
|
|
60
64
|
- `style_reference`
|
|
61
65
|
- `composition_reference`
|
|
62
66
|
- `insert_source`
|
|
63
67
|
|
|
64
|
-
|
|
68
|
+
| Tool | Meaning | Endpoint |
|
|
69
|
+
|---|---|---|
|
|
70
|
+
| `generate_image` | Create a new image; references are optional inputs | `/v1/images/generations` |
|
|
71
|
+
| `edit_image` | Modify one explicit target and preserve unspecified content | `/v1/images/edits` |
|
|
65
72
|
|
|
66
|
-
When
|
|
73
|
+
When an image should only guide a new result, use `generate_image`. Use
|
|
74
|
+
`edit_image` only for an explicit local change such as removing an object or
|
|
75
|
+
changing one color while keeping the original. If the user intent is unclear,
|
|
76
|
+
ask before calling either tool. Supplying both image fields returns
|
|
77
|
+
`E_INPUT_MODE_CONFLICT`.
|
|
67
78
|
|
|
68
79
|
Response:
|
|
69
80
|
|
|
70
81
|
```json
|
|
71
82
|
{
|
|
72
|
-
"
|
|
83
|
+
"accepted": true,
|
|
84
|
+
"terminal": false,
|
|
73
85
|
"status": "submitted",
|
|
74
86
|
"task_id": "uuid",
|
|
87
|
+
"poll_required": true,
|
|
88
|
+
"retry_after_ms": 1000,
|
|
89
|
+
"resolved_intent": "generate",
|
|
75
90
|
"effective_prompt": "Primary request: ...",
|
|
76
|
-
"
|
|
91
|
+
"prompt_stats": {
|
|
92
|
+
"characters": 123,
|
|
93
|
+
"utf8Bytes": 145,
|
|
94
|
+
"lines": 4
|
|
95
|
+
},
|
|
96
|
+
"message": "已接受异步生成任务;这不代表图片已生成,必须轮询 check_image_task"
|
|
77
97
|
}
|
|
78
98
|
```
|
|
79
99
|
|
|
100
|
+
## Tool: `edit_image`
|
|
101
|
+
|
|
102
|
+
Edits exactly one original image. It shares the output, sizing, optional
|
|
103
|
+
supporting-reference, and asynchronous polling parameters above, and adds:
|
|
104
|
+
|
|
105
|
+
| Parameter | Type | Description |
|
|
106
|
+
|---|---|---|
|
|
107
|
+
| `edit_target` | string | Required local path of the single image to modify |
|
|
108
|
+
|
|
109
|
+
The edit target is always uploaded first. Supporting `reference_images` or
|
|
110
|
+
`input_images` follow it. The effective prompt tells the model to preserve every
|
|
111
|
+
unspecified detail.
|
|
112
|
+
|
|
113
|
+
Provider output is locally fitted to the requested size template before saving,
|
|
114
|
+
so upstream-native dimensions such as `1254x1254` or `1536x1024` do not leak
|
|
115
|
+
through the public contract.
|
|
116
|
+
|
|
80
117
|
## Tool: `check_image_task`
|
|
81
118
|
|
|
82
119
|
Checks async task status and failure details.
|
|
83
120
|
|
|
84
121
|
| Parameter | Type | Description |
|
|
85
122
|
|---|---|---|
|
|
86
|
-
| `task_id` | string | Task id returned by `generate_image` |
|
|
123
|
+
| `task_id` | string | Task id returned by `generate_image` or `edit_image` |
|
|
87
124
|
|
|
88
125
|
Done response:
|
|
89
126
|
|
|
90
127
|
```json
|
|
91
128
|
{
|
|
92
129
|
"success": true,
|
|
130
|
+
"terminal": true,
|
|
93
131
|
"task_id": "uuid",
|
|
94
132
|
"status": "done",
|
|
95
133
|
"saved_path": "D:/images/output.png",
|
|
@@ -102,16 +140,36 @@ Failed response:
|
|
|
102
140
|
```json
|
|
103
141
|
{
|
|
104
142
|
"success": false,
|
|
143
|
+
"terminal": true,
|
|
105
144
|
"task_id": "uuid",
|
|
106
145
|
"status": "failed",
|
|
107
|
-
"error": "
|
|
108
|
-
"message": "
|
|
146
|
+
"error": "E_PROVIDER_EMPTY_RESPONSE",
|
|
147
|
+
"message": "Provider returned HTTP 200 but data contained no image",
|
|
148
|
+
"details": {
|
|
149
|
+
"request_id": "provider-request-id",
|
|
150
|
+
"response_summary": {
|
|
151
|
+
"status": 200,
|
|
152
|
+
"content_type": "application/json",
|
|
153
|
+
"body": {
|
|
154
|
+
"kind": "json_object",
|
|
155
|
+
"keys": ["data"],
|
|
156
|
+
"data_length": 1,
|
|
157
|
+
"data_0_keys": []
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
109
161
|
}
|
|
110
162
|
```
|
|
111
163
|
|
|
112
|
-
|
|
164
|
+
Provider diagnostics contain only response metadata and JSON shape/key summaries;
|
|
165
|
+
prompt text, image payloads, authorization values, and arbitrary upstream body
|
|
166
|
+
content are not echoed. The configured provider timeout covers receiving headers,
|
|
167
|
+
reading the response body, and downloading a returned image URL.
|
|
168
|
+
|
|
169
|
+
## GPT size request templates
|
|
113
170
|
|
|
114
|
-
|
|
171
|
+
The server maps size tiers and ratios to these request dimensions. Actual output
|
|
172
|
+
dimensions still depend on the upstream provider response.
|
|
115
173
|
|
|
116
174
|
| `size` | `1:1` | `3:2` | `2:3` | `16:9` | `9:16` |
|
|
117
175
|
|---|---:|---:|---:|---:|---:|
|
package/dist/errors.js
CHANGED
package/dist/image-processing.js
CHANGED
|
@@ -40,6 +40,32 @@ export async function encodeImage(imageBuffer, format) {
|
|
|
40
40
|
throw new AppError("E_SAVE_FAILED", `Cannot encode image as ${format}: ${err}`);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
+
export async function fitImageToSize(imageBuffer, targetSize) {
|
|
44
|
+
const match = targetSize.match(/^(\d+)x(\d+)$/);
|
|
45
|
+
if (!match) {
|
|
46
|
+
throw new AppError("E_INPUT_INVALID", `Invalid target image size: ${targetSize}`);
|
|
47
|
+
}
|
|
48
|
+
const width = Number(match[1]);
|
|
49
|
+
const height = Number(match[2]);
|
|
50
|
+
try {
|
|
51
|
+
const metadata = await sharp(imageBuffer, { failOn: "none" }).metadata();
|
|
52
|
+
if (metadata.width === width && metadata.height === height) {
|
|
53
|
+
return imageBuffer;
|
|
54
|
+
}
|
|
55
|
+
return await sharp(imageBuffer, { failOn: "none" })
|
|
56
|
+
.resize({
|
|
57
|
+
width,
|
|
58
|
+
height,
|
|
59
|
+
fit: "cover",
|
|
60
|
+
position: "attention",
|
|
61
|
+
})
|
|
62
|
+
.png()
|
|
63
|
+
.toBuffer();
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
throw new AppError("E_SAVE_FAILED", `Cannot normalize image to ${targetSize}: ${err}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
43
69
|
export async function removeChromaKey(imageBuffer, keyColor = "#00ff00") {
|
|
44
70
|
const key = parseHexColor(keyColor);
|
|
45
71
|
const image = sharp(imageBuffer, { failOn: "none" }).ensureAlpha();
|
package/dist/index.js
CHANGED
|
@@ -4,12 +4,13 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
import { createRequire } from "node:module";
|
|
6
6
|
import { loadConfig } from "./config.js";
|
|
7
|
-
import { ASPECT_RATIOS, IMAGE_SIZES, generateImage } from "./provider.js";
|
|
7
|
+
import { ASPECT_RATIOS, DEFAULT_GENERATION_REFERENCE_MAX_SIDE, DEFAULT_MULTI_REFERENCE_MAX_SIDE, IMAGE_SIZES, editImage, generateImage, mapOpenAISize, } from "./provider.js";
|
|
8
8
|
import { saveImage } from "./saver.js";
|
|
9
9
|
import { AppError } from "./errors.js";
|
|
10
10
|
import { createTask, getTask, updateTask } from "./task-queue.js";
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
11
|
+
import { taskStatusResponse, taskSubmissionResponse } from "./task-response.js";
|
|
12
|
+
import { PROMPT_USE_CASES, REFERENCE_IMAGE_ROLES, preparePrompt, } from "./prompt.js";
|
|
13
|
+
import { OUTPUT_FORMATS, fitImageToSize, removeChromaKey, } from "./image-processing.js";
|
|
13
14
|
const require = createRequire(import.meta.url);
|
|
14
15
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
16
|
const notifier = require("node-notifier");
|
|
@@ -29,66 +30,43 @@ function toolText(data) {
|
|
|
29
30
|
}],
|
|
30
31
|
};
|
|
31
32
|
}
|
|
33
|
+
function toolError(code, message) {
|
|
34
|
+
return {
|
|
35
|
+
...toolText({ success: false, error: code, message }),
|
|
36
|
+
isError: true,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function validateTransparentOutput(args) {
|
|
40
|
+
if (!args.transparent_background)
|
|
41
|
+
return undefined;
|
|
42
|
+
const filenameExt = args.output_filename.match(/\.([^.]+)$/)?.[1]?.toLowerCase();
|
|
43
|
+
if ((args.format && args.format !== "png") || (filenameExt && filenameExt !== "png")) {
|
|
44
|
+
return toolError("E_INPUT_INVALID", "transparent_background requires PNG output");
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
32
48
|
const server = new McpServer({
|
|
33
49
|
name: "duojie-image",
|
|
34
|
-
version: "0.
|
|
50
|
+
version: "0.10.0",
|
|
35
51
|
});
|
|
36
|
-
|
|
37
|
-
description: "Generate image async with gpt-image-2. Reference images route through /v1/images/edits (multipart form); text-only requests route through /v1/images/generations. Return after tool call; don't wait/check.",
|
|
38
|
-
inputSchema: z.object({
|
|
39
|
-
prompt: z.string().min(1).describe("Prompt"),
|
|
40
|
-
output_dir: z.string().describe("Absolute output dir"),
|
|
41
|
-
output_filename: z.string().describe("Filename without ext"),
|
|
42
|
-
reference_images: z.array(z.string()).optional().describe("Reference image paths"),
|
|
43
|
-
input_images: z.array(z.object({
|
|
44
|
-
path: z.string().min(1).describe("Local image path"),
|
|
45
|
-
role: z.enum(IMAGE_INPUT_ROLES).optional().describe("How this image should guide generation/editing"),
|
|
46
|
-
note: z.string().optional().describe("Short role-specific instruction"),
|
|
47
|
-
})).optional().describe("Structured image inputs with roles; supersedes reference_images when provided"),
|
|
48
|
-
use_case: z.enum(PROMPT_USE_CASES).optional().describe("Prompt taxonomy slug"),
|
|
49
|
-
asset_type: z.string().optional().describe("Where the image will be used"),
|
|
50
|
-
constraints: z.string().optional().describe("Must-keep or must-avoid constraints"),
|
|
51
|
-
transparent_background: z.boolean().optional().describe("Generate on a chroma-key background and remove it locally; PNG only"),
|
|
52
|
-
transparent_key_color: z.string().regex(/^#[0-9a-fA-F]{6}$/).optional().describe("Chroma-key color, default #00ff00"),
|
|
53
|
-
size: z.enum(IMAGE_SIZES).optional().describe("Default 1K. Supported: 1K, 2K, 4K"),
|
|
54
|
-
aspect_ratio: z.enum(ASPECT_RATIOS).optional().describe("Supported: 1:1, 3:2, 2:3, 16:9, 9:16"),
|
|
55
|
-
format: z.enum(OUTPUT_FORMATS).optional().describe("Default png. Supported: png, jpg, jpeg"),
|
|
56
|
-
overwrite: z.boolean().optional().describe("Default false"),
|
|
57
|
-
negative_prompt: z.string().optional().describe("Avoid"),
|
|
58
|
-
reference_max_side: z.number().int().min(256).max(3840).optional().describe("Resize reference inputs to this max side; default 1024"),
|
|
59
|
-
preserve_reference_detail: z.boolean().optional().describe("Do not resize reference inputs before upload"),
|
|
60
|
-
silent: z.boolean().optional().describe("No notification"),
|
|
61
|
-
}),
|
|
62
|
-
}, async (args) => {
|
|
52
|
+
async function submitImageTask(args, operation) {
|
|
63
53
|
let config;
|
|
64
54
|
try {
|
|
65
55
|
config = loadConfig();
|
|
66
56
|
}
|
|
67
57
|
catch (err) {
|
|
68
58
|
const code = err instanceof AppError ? err.code : "E_CONFIG_MISSING_KEY";
|
|
69
|
-
return
|
|
70
|
-
}
|
|
71
|
-
const silent = args.silent ?? false;
|
|
72
|
-
if (args.transparent_background) {
|
|
73
|
-
const filenameExt = args.output_filename.match(/\.([^.]+)$/)?.[1]?.toLowerCase();
|
|
74
|
-
if ((args.format && args.format !== "png") || (filenameExt && filenameExt !== "png")) {
|
|
75
|
-
return {
|
|
76
|
-
content: [{
|
|
77
|
-
type: "text",
|
|
78
|
-
text: JSON.stringify({
|
|
79
|
-
success: false,
|
|
80
|
-
error: "E_INPUT_INVALID",
|
|
81
|
-
message: "transparent_background requires PNG output",
|
|
82
|
-
}),
|
|
83
|
-
}],
|
|
84
|
-
isError: true,
|
|
85
|
-
};
|
|
86
|
-
}
|
|
59
|
+
return toolError(code, String(err instanceof Error ? err.message : err));
|
|
87
60
|
}
|
|
61
|
+
const transparentError = validateTransparentOutput(args);
|
|
62
|
+
if (transparentError)
|
|
63
|
+
return transparentError;
|
|
88
64
|
let preparedPrompt;
|
|
89
65
|
try {
|
|
90
66
|
preparedPrompt = preparePrompt({
|
|
67
|
+
operation,
|
|
91
68
|
prompt: args.prompt,
|
|
69
|
+
editTarget: args.edit_target,
|
|
92
70
|
negativePrompt: args.negative_prompt,
|
|
93
71
|
useCase: args.use_case,
|
|
94
72
|
assetType: args.asset_type,
|
|
@@ -101,21 +79,37 @@ server.registerTool("generate_image", {
|
|
|
101
79
|
}
|
|
102
80
|
catch (err) {
|
|
103
81
|
const code = err instanceof AppError ? err.code : "E_INPUT_INVALID";
|
|
104
|
-
return
|
|
82
|
+
return toolError(code, String(err instanceof Error ? err.message : err));
|
|
105
83
|
}
|
|
106
84
|
const task = createTask();
|
|
107
|
-
updateTask(task.id, {
|
|
108
|
-
|
|
109
|
-
|
|
85
|
+
updateTask(task.id, {
|
|
86
|
+
effectivePrompt: preparedPrompt.prompt,
|
|
87
|
+
promptStats: preparedPrompt.stats,
|
|
88
|
+
resolvedIntent: preparedPrompt.resolvedIntent,
|
|
89
|
+
});
|
|
90
|
+
void (async () => {
|
|
110
91
|
try {
|
|
111
92
|
updateTask(task.id, { status: "running", startedAt: Date.now() });
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
93
|
+
const referenceMaxSide = args.preserve_reference_detail
|
|
94
|
+
? false
|
|
95
|
+
: args.reference_max_side;
|
|
96
|
+
let imageBuffer = operation === "edit"
|
|
97
|
+
? await editImage(config, {
|
|
98
|
+
prompt: preparedPrompt.prompt,
|
|
99
|
+
editTarget: preparedPrompt.imagePaths[0],
|
|
100
|
+
referenceImages: preparedPrompt.imagePaths.slice(1),
|
|
101
|
+
size: args.size ?? "1K",
|
|
102
|
+
aspectRatio: args.aspect_ratio,
|
|
103
|
+
referenceMaxSide,
|
|
104
|
+
})
|
|
105
|
+
: await generateImage(config, {
|
|
106
|
+
prompt: preparedPrompt.prompt,
|
|
107
|
+
referenceImages: preparedPrompt.imagePaths,
|
|
108
|
+
size: args.size ?? "1K",
|
|
109
|
+
aspectRatio: args.aspect_ratio,
|
|
110
|
+
referenceMaxSide,
|
|
111
|
+
});
|
|
112
|
+
imageBuffer = await fitImageToSize(imageBuffer, mapOpenAISize(args.size ?? "1K", args.aspect_ratio));
|
|
119
113
|
const outputFormat = args.transparent_background ? "png" : args.format;
|
|
120
114
|
if (args.transparent_background) {
|
|
121
115
|
imageBuffer = await removeChromaKey(imageBuffer, args.transparent_key_color);
|
|
@@ -133,10 +127,10 @@ server.registerTool("generate_image", {
|
|
|
133
127
|
});
|
|
134
128
|
await server.sendLoggingMessage({
|
|
135
129
|
level: "info",
|
|
136
|
-
data:
|
|
130
|
+
data: `图片${operation === "edit" ? "编辑" : "生成"}完成 [${task.id}]:${savedPath}`,
|
|
137
131
|
});
|
|
138
|
-
if (!silent)
|
|
139
|
-
notify("
|
|
132
|
+
if (!args.silent)
|
|
133
|
+
notify("图片任务完成", savedPath);
|
|
140
134
|
}
|
|
141
135
|
catch (err) {
|
|
142
136
|
const code = err instanceof AppError ? err.code : "E_INTERNAL";
|
|
@@ -146,67 +140,74 @@ server.registerTool("generate_image", {
|
|
|
146
140
|
completedAt: Date.now(),
|
|
147
141
|
errorCode: code,
|
|
148
142
|
error: msg,
|
|
143
|
+
errorDetails: err instanceof AppError ? err.details : undefined,
|
|
149
144
|
});
|
|
150
145
|
await server.sendLoggingMessage({
|
|
151
146
|
level: "error",
|
|
152
|
-
data:
|
|
147
|
+
data: `图片任务失败 [${task.id}] [${code}]: ${msg}`,
|
|
153
148
|
}).catch(() => { });
|
|
154
|
-
if (!silent)
|
|
155
|
-
|
|
156
|
-
notify("图片生成超时", `${args.output_filename} 生成超过 300 秒,请重试`);
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
notify("图片生成失败", `[${code}] ${msg}`);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
149
|
+
if (!args.silent)
|
|
150
|
+
notify("图片任务失败", `[${code}] ${msg}`);
|
|
162
151
|
}
|
|
163
152
|
})();
|
|
164
|
-
return
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
153
|
+
return toolText(taskSubmissionResponse(getTask(task.id) ?? task, args.output_filename));
|
|
154
|
+
}
|
|
155
|
+
const structuredReferences = z.array(z.object({
|
|
156
|
+
path: z.string().min(1).describe("Local reference image path"),
|
|
157
|
+
role: z.enum(REFERENCE_IMAGE_ROLES).optional().describe("reference | style_reference | composition_reference | insert_source"),
|
|
158
|
+
note: z.string().optional().describe("Short role-specific instruction"),
|
|
159
|
+
})).optional().describe("Structured references; do not combine with reference_images");
|
|
160
|
+
const commonFields = {
|
|
161
|
+
prompt: z.string().min(1).describe("Image request"),
|
|
162
|
+
output_dir: z.string().describe("Absolute output directory"),
|
|
163
|
+
output_filename: z.string().describe("Filename without extension"),
|
|
164
|
+
use_case: z.enum(PROMPT_USE_CASES).optional().describe("Prompt taxonomy slug"),
|
|
165
|
+
asset_type: z.string().optional().describe("Where the image will be used"),
|
|
166
|
+
constraints: z.string().optional().describe("Must-keep or must-avoid constraints"),
|
|
167
|
+
size: z.enum(IMAGE_SIZES).optional().describe("Default 1K"),
|
|
168
|
+
aspect_ratio: z.enum(ASPECT_RATIOS).optional().describe("1:1 | 3:2 | 2:3 | 16:9 | 9:16"),
|
|
169
|
+
format: z.enum(OUTPUT_FORMATS).optional().describe("Default png"),
|
|
170
|
+
overwrite: z.boolean().optional().describe("Default false"),
|
|
171
|
+
negative_prompt: z.string().optional().describe("Content to avoid"),
|
|
172
|
+
reference_max_side: z.number().int().min(256).max(3840).optional()
|
|
173
|
+
.describe(`Input resize limit. generate defaults: ${DEFAULT_GENERATION_REFERENCE_MAX_SIDE} for one reference, ${DEFAULT_MULTI_REFERENCE_MAX_SIDE} for multiple; edit default 1024`),
|
|
174
|
+
preserve_reference_detail: z.boolean().optional().describe("Upload inputs without resizing; less stable"),
|
|
175
|
+
silent: z.boolean().optional().describe("Disable desktop notification"),
|
|
176
|
+
};
|
|
177
|
+
const referenceFields = {
|
|
178
|
+
reference_images: z.array(z.string()).optional()
|
|
179
|
+
.describe("Optional local reference paths for creating a new image; never treated as edit canvases"),
|
|
180
|
+
input_images: structuredReferences,
|
|
181
|
+
};
|
|
182
|
+
server.registerTool("generate_image", {
|
|
183
|
+
description: "Create a NEW image from a prompt, optionally guided by reference images. This is the default for style, identity, composition, material, scene-change, and same-style requests; references are never edit canvases. Do not use for a local change that must preserve the original. If the user intent is ambiguous, ask whether they want a new referenced image or a small edit before calling. Async: poll check_image_task until done or failed.",
|
|
184
|
+
inputSchema: z.object({
|
|
185
|
+
...commonFields,
|
|
186
|
+
...referenceFields,
|
|
187
|
+
transparent_background: z.boolean().optional().describe("Generate on chroma key and remove it locally; PNG only"),
|
|
188
|
+
transparent_key_color: z.string().regex(/^#[0-9a-fA-F]{6}$/).optional().describe("Default #00ff00"),
|
|
189
|
+
}),
|
|
190
|
+
}, async (args) => submitImageTask(args, "generate"));
|
|
191
|
+
server.registerTool("edit_image", {
|
|
192
|
+
description: "Make a SMALL, explicit change to one existing image while preserving everything not requested. Use only when the user clearly asks to modify the original (for example remove an object, change a color, or replace a local element). If it is unclear whether they want a new image or an edit, ask before calling. Exactly one edit_target is required. Async: poll check_image_task until done or failed.",
|
|
193
|
+
inputSchema: z.object({
|
|
194
|
+
...commonFields,
|
|
195
|
+
edit_target: z.string().min(1).describe("The single original image to modify"),
|
|
196
|
+
...referenceFields,
|
|
197
|
+
}),
|
|
198
|
+
}, async (args) => submitImageTask(args, "edit"));
|
|
177
199
|
server.registerTool("check_image_task", {
|
|
178
|
-
description: "
|
|
200
|
+
description: "Poll a generate_image or edit_image task. Continue while poll_required=true. Only status=done with success=true means an image was saved; failed is terminal with diagnostics.",
|
|
179
201
|
inputSchema: z.object({
|
|
180
|
-
task_id: z.string().min(1).describe("Task id returned by
|
|
202
|
+
task_id: z.string().min(1).describe("Task id returned by an image tool"),
|
|
181
203
|
}),
|
|
182
204
|
}, async (args) => {
|
|
183
205
|
const task = getTask(args.task_id);
|
|
184
206
|
if (!task) {
|
|
185
|
-
return {
|
|
186
|
-
...toolText({
|
|
187
|
-
success: false,
|
|
188
|
-
error: "E_TASK_NOT_FOUND",
|
|
189
|
-
message: `Task ${args.task_id} not found. It may have expired or never existed.`,
|
|
190
|
-
}),
|
|
191
|
-
isError: true,
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
const response = {
|
|
195
|
-
success: task.status !== "failed",
|
|
196
|
-
task_id: task.id,
|
|
197
|
-
status: task.status,
|
|
198
|
-
};
|
|
199
|
-
if (task.status === "done") {
|
|
200
|
-
response.saved_path = task.savedPath;
|
|
201
|
-
}
|
|
202
|
-
if (task.effectivePrompt) {
|
|
203
|
-
response.effective_prompt = task.effectivePrompt;
|
|
204
|
-
}
|
|
205
|
-
if (task.status === "failed") {
|
|
206
|
-
response.error = task.errorCode;
|
|
207
|
-
response.message = task.error;
|
|
207
|
+
return toolError("E_TASK_NOT_FOUND", `Task ${args.task_id} not found. It may have expired or never existed.`);
|
|
208
208
|
}
|
|
209
|
-
|
|
209
|
+
const response = toolText(taskStatusResponse(task));
|
|
210
|
+
return task.status === "failed" ? { ...response, isError: true } : response;
|
|
210
211
|
});
|
|
211
212
|
const transport = new StdioServerTransport();
|
|
212
213
|
await server.connect(transport);
|
package/dist/prompt.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { AppError } from "./errors.js";
|
|
2
|
-
export const
|
|
2
|
+
export const REFERENCE_IMAGE_ROLES = [
|
|
3
3
|
"reference",
|
|
4
|
-
"edit_target",
|
|
5
4
|
"style_reference",
|
|
6
5
|
"composition_reference",
|
|
7
6
|
"insert_source",
|
|
@@ -34,12 +33,28 @@ function compactLines(lines) {
|
|
|
34
33
|
function validateImageInputs(inputImages) {
|
|
35
34
|
if (!inputImages)
|
|
36
35
|
return undefined;
|
|
37
|
-
|
|
36
|
+
return inputImages.map((image, index) => {
|
|
38
37
|
if (!image.path?.trim()) {
|
|
39
38
|
throw new AppError("E_INPUT_INVALID", `input_images[${index}].path is required`);
|
|
40
39
|
}
|
|
40
|
+
return { ...image, path: image.path.trim() };
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function legacyReferenceInputs(referenceImages) {
|
|
44
|
+
if (!referenceImages?.length)
|
|
45
|
+
return undefined;
|
|
46
|
+
return referenceImages.map((path, index) => {
|
|
47
|
+
if (!path?.trim()) {
|
|
48
|
+
throw new AppError("E_INPUT_INVALID", `reference_images[${index}] is required`);
|
|
49
|
+
}
|
|
50
|
+
return { path: path.trim(), role: "reference" };
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function resolveReferenceInputs(opts) {
|
|
54
|
+
if (opts.inputImages !== undefined && opts.referenceImages !== undefined) {
|
|
55
|
+
throw new AppError("E_INPUT_MODE_CONFLICT", "Use either input_images or reference_images, not both");
|
|
41
56
|
}
|
|
42
|
-
return inputImages;
|
|
57
|
+
return validateImageInputs(opts.inputImages) ?? legacyReferenceInputs(opts.referenceImages);
|
|
43
58
|
}
|
|
44
59
|
function imageRoleLabel(role) {
|
|
45
60
|
return role?.replace(/_/g, " ") ?? "reference";
|
|
@@ -48,15 +63,18 @@ function imageLines(inputImages) {
|
|
|
48
63
|
if (!inputImages?.length)
|
|
49
64
|
return undefined;
|
|
50
65
|
const lines = inputImages.map((image, index) => {
|
|
51
|
-
const note = image.note?.trim() ? `; ${image.note.trim()}` : "";
|
|
66
|
+
const note = "note" in image && image.note?.trim() ? `; ${image.note.trim()}` : "";
|
|
52
67
|
return `Image ${index + 1}: ${imageRoleLabel(image.role)}${note}`;
|
|
53
68
|
});
|
|
54
69
|
return `Input images: ${lines.join("; ")}`;
|
|
55
70
|
}
|
|
56
|
-
function
|
|
57
|
-
if (
|
|
58
|
-
return
|
|
59
|
-
|
|
71
|
+
function operationPrompt(operation, hasReferences) {
|
|
72
|
+
if (operation === "edit") {
|
|
73
|
+
return "Intent: make only the requested changes to Image 1; preserve every unspecified object, identity, composition, camera angle, material, text, lighting, and detail. Later images are supporting references only.";
|
|
74
|
+
}
|
|
75
|
+
return hasReferences
|
|
76
|
+
? "Intent: create a new image; use input images only as style, composition, identity, or material references. Do not edit or reproduce any source canvas."
|
|
77
|
+
: "Intent: create a new image from the request.";
|
|
60
78
|
}
|
|
61
79
|
function transparentPrompt(keyColor) {
|
|
62
80
|
return compactLines([
|
|
@@ -66,7 +84,23 @@ function transparentPrompt(keyColor) {
|
|
|
66
84
|
]);
|
|
67
85
|
}
|
|
68
86
|
export function preparePrompt(opts) {
|
|
69
|
-
const
|
|
87
|
+
const references = resolveReferenceInputs(opts);
|
|
88
|
+
let imageInputs;
|
|
89
|
+
if (opts.operation === "edit") {
|
|
90
|
+
if (!opts.editTarget?.trim()) {
|
|
91
|
+
throw new AppError("E_INPUT_MODE_CONFLICT", "edit_image requires one edit_target");
|
|
92
|
+
}
|
|
93
|
+
imageInputs = [
|
|
94
|
+
{ path: opts.editTarget.trim(), role: "edit_target" },
|
|
95
|
+
...(references ?? []),
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
if (opts.editTarget !== undefined) {
|
|
100
|
+
throw new AppError("E_INPUT_MODE_CONFLICT", "generate_image does not accept edit_target; use edit_image for local changes");
|
|
101
|
+
}
|
|
102
|
+
imageInputs = references;
|
|
103
|
+
}
|
|
70
104
|
const keyColor = opts.transparentKeyColor ?? DEFAULT_KEY_COLOR;
|
|
71
105
|
const negativePrompt = opts.negativePrompt?.trim()
|
|
72
106
|
? `Avoid: ${opts.negativePrompt.trim()}`
|
|
@@ -74,7 +108,8 @@ export function preparePrompt(opts) {
|
|
|
74
108
|
const prompt = compactLines([
|
|
75
109
|
opts.useCase ? `Use case: ${opts.useCase}` : undefined,
|
|
76
110
|
opts.assetType ? `Asset type: ${opts.assetType}` : undefined,
|
|
77
|
-
|
|
111
|
+
operationPrompt(opts.operation, Boolean(references?.length)),
|
|
112
|
+
imageLines(imageInputs),
|
|
78
113
|
`Primary request: ${opts.prompt.trim()}`,
|
|
79
114
|
opts.constraints ? `Constraints: ${opts.constraints.trim()}` : undefined,
|
|
80
115
|
opts.transparentBackground ? transparentPrompt(keyColor) : undefined,
|
|
@@ -82,7 +117,13 @@ export function preparePrompt(opts) {
|
|
|
82
117
|
]);
|
|
83
118
|
return {
|
|
84
119
|
prompt,
|
|
85
|
-
|
|
86
|
-
|
|
120
|
+
resolvedIntent: opts.operation,
|
|
121
|
+
imagePaths: imageInputs?.map((image) => image.path) ?? [],
|
|
122
|
+
imageInputs,
|
|
123
|
+
stats: {
|
|
124
|
+
characters: Array.from(prompt).length,
|
|
125
|
+
utf8Bytes: Buffer.byteLength(prompt, "utf8"),
|
|
126
|
+
lines: prompt ? prompt.split("\n").length : 0,
|
|
127
|
+
},
|
|
87
128
|
};
|
|
88
129
|
}
|
package/dist/provider.js
CHANGED
|
@@ -2,42 +2,102 @@ import { AppError } from "./errors.js";
|
|
|
2
2
|
import { resizeIfNeeded } from "./resizer.js";
|
|
3
3
|
export const ASPECT_RATIOS = ["1:1", "3:2", "2:3", "16:9", "9:16"];
|
|
4
4
|
export const IMAGE_SIZES = ["1K", "2K", "4K"];
|
|
5
|
-
|
|
5
|
+
function getRequestId(res) {
|
|
6
|
+
return res.headers.get("x-request-id")
|
|
7
|
+
?? res.headers.get("request-id")
|
|
8
|
+
?? res.headers.get("x-trace-id")
|
|
9
|
+
?? res.headers.get("cf-ray");
|
|
10
|
+
}
|
|
11
|
+
function summarizeJson(value) {
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
return { kind: "json_array", length: value.length };
|
|
14
|
+
}
|
|
15
|
+
if (!value || typeof value !== "object") {
|
|
16
|
+
return { kind: "json_scalar", type: value === null ? "null" : typeof value };
|
|
17
|
+
}
|
|
18
|
+
const record = value;
|
|
19
|
+
const summary = {
|
|
20
|
+
kind: "json_object",
|
|
21
|
+
keys: Object.keys(record).slice(0, 20),
|
|
22
|
+
};
|
|
23
|
+
if (Array.isArray(record.data)) {
|
|
24
|
+
summary.data_length = record.data.length;
|
|
25
|
+
const first = record.data[0];
|
|
26
|
+
if (first && typeof first === "object" && !Array.isArray(first)) {
|
|
27
|
+
summary.data_0_keys = Object.keys(first).slice(0, 20);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (record.error && typeof record.error === "object" && !Array.isArray(record.error)) {
|
|
31
|
+
const error = record.error;
|
|
32
|
+
summary.error_keys = Object.keys(error).slice(0, 20);
|
|
33
|
+
if (typeof error.code === "string" && /^[a-zA-Z0-9_.:-]{1,80}$/.test(error.code)) {
|
|
34
|
+
summary.error_code = error.code;
|
|
35
|
+
}
|
|
36
|
+
if (typeof error.type === "string" && /^[a-zA-Z0-9_.:-]{1,80}$/.test(error.type)) {
|
|
37
|
+
summary.error_type = error.type;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return summary;
|
|
41
|
+
}
|
|
42
|
+
function responseDetails(res, body) {
|
|
43
|
+
const summary = {
|
|
44
|
+
status: res.status,
|
|
45
|
+
content_type: res.headers.get("content-type"),
|
|
46
|
+
content_length: res.headers.get("content-length"),
|
|
47
|
+
...(body ? { body } : {}),
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
request_id: getRequestId(res),
|
|
51
|
+
response_summary: summary,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function isAbortError(err) {
|
|
55
|
+
return err instanceof Error && (err.name === "AbortError" || err.name === "TimeoutError");
|
|
56
|
+
}
|
|
57
|
+
async function delayForRetry(attempt) {
|
|
58
|
+
const delayMs = 1_000 * Math.pow(2, attempt) + Math.random() * 500;
|
|
59
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
60
|
+
}
|
|
61
|
+
async function fetchWithRetry(url, init, timeoutMs, consume, retries = 0) {
|
|
6
62
|
let lastErr;
|
|
7
63
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
8
64
|
const controller = new AbortController();
|
|
9
65
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
66
|
+
let receivedResponse;
|
|
10
67
|
try {
|
|
11
68
|
const res = await fetch(url, { ...init, signal: controller.signal });
|
|
12
|
-
|
|
69
|
+
receivedResponse = res;
|
|
13
70
|
// 5xx: 上游随机故障(EOF等),立刻 abort body 连接并重试,不等 chunked 终止
|
|
14
71
|
if (res.status >= 500 && attempt < retries) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
catch { }
|
|
19
|
-
const delayMs = 1000 * Math.pow(2, attempt) + Math.random() * 500;
|
|
20
|
-
await new Promise((r) => setTimeout(r, delayMs));
|
|
72
|
+
controller.abort();
|
|
73
|
+
clearTimeout(timer);
|
|
74
|
+
await delayForRetry(attempt);
|
|
21
75
|
continue;
|
|
22
76
|
}
|
|
23
|
-
|
|
77
|
+
const result = await consume(res, controller.signal);
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
return result;
|
|
24
80
|
}
|
|
25
81
|
catch (err) {
|
|
26
82
|
clearTimeout(timer);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
83
|
+
if (err instanceof AppError) {
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
if (isAbortError(err)) {
|
|
87
|
+
throw new AppError("E_PROVIDER_TIMEOUT", `Provider request timed out after ${timeoutMs}ms`, {
|
|
88
|
+
timeout_ms: timeoutMs,
|
|
89
|
+
phase: receivedResponse ? "response_body_or_image_download" : "connection_or_headers",
|
|
90
|
+
...(receivedResponse ? responseDetails(receivedResponse) : {}),
|
|
91
|
+
});
|
|
34
92
|
}
|
|
35
|
-
|
|
36
|
-
|
|
93
|
+
// Once response headers have arrived, the generation may already be billable.
|
|
94
|
+
// Do not replay the POST for body/CDN failures and risk duplicate generation.
|
|
95
|
+
if (receivedResponse) {
|
|
96
|
+
throw new AppError("E_PROVIDER_BAD_REQUEST", "Provider response could not be read completely", responseDetails(receivedResponse));
|
|
37
97
|
}
|
|
38
98
|
lastErr = err;
|
|
39
99
|
if (attempt < retries) {
|
|
40
|
-
await
|
|
100
|
+
await delayForRetry(attempt);
|
|
41
101
|
}
|
|
42
102
|
}
|
|
43
103
|
}
|
|
@@ -67,90 +127,130 @@ const SIZE_TEMPLATES = {
|
|
|
67
127
|
"9:16": "2160x3840",
|
|
68
128
|
},
|
|
69
129
|
};
|
|
70
|
-
function mapOpenAISize(size, aspectRatio) {
|
|
130
|
+
export function mapOpenAISize(size, aspectRatio) {
|
|
71
131
|
const imageSize = size ?? "1K";
|
|
72
132
|
const ratio = aspectRatio ?? "1:1";
|
|
73
133
|
return SIZE_TEMPLATES[imageSize][ratio];
|
|
74
134
|
}
|
|
75
|
-
async function imageFromOpenAIResponse(res) {
|
|
76
|
-
|
|
135
|
+
async function imageFromOpenAIResponse(res, signal) {
|
|
136
|
+
let data;
|
|
137
|
+
try {
|
|
138
|
+
data = (await res.json());
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
if (isAbortError(err))
|
|
142
|
+
throw err;
|
|
143
|
+
if (!(err instanceof SyntaxError)) {
|
|
144
|
+
throw new AppError("E_PROVIDER_BAD_REQUEST", "Provider response body could not be read", responseDetails(res));
|
|
145
|
+
}
|
|
146
|
+
throw new AppError("E_PROVIDER_EMPTY_RESPONSE", "Provider returned HTTP 200 but the response body was not valid image JSON", responseDetails(res, { kind: "invalid_json" }));
|
|
147
|
+
}
|
|
77
148
|
const image = data.data?.[0];
|
|
78
149
|
if (image?.b64_json) {
|
|
79
|
-
|
|
150
|
+
const imageBuffer = Buffer.from(image.b64_json, "base64");
|
|
151
|
+
if (imageBuffer.length > 0) {
|
|
152
|
+
return imageBuffer;
|
|
153
|
+
}
|
|
80
154
|
}
|
|
81
155
|
if (image?.url) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
156
|
+
try {
|
|
157
|
+
const imgRes = await fetch(image.url, { signal });
|
|
158
|
+
if (!imgRes.ok) {
|
|
159
|
+
const summary = await readBodySummary(imgRes);
|
|
160
|
+
throw new AppError("E_PROVIDER_BAD_REQUEST", `Image URL fetch failed (HTTP ${imgRes.status})`, responseDetails(imgRes, summary));
|
|
161
|
+
}
|
|
162
|
+
const imageBuffer = Buffer.from(await imgRes.arrayBuffer());
|
|
163
|
+
if (imageBuffer.length > 0) {
|
|
164
|
+
return imageBuffer;
|
|
165
|
+
}
|
|
166
|
+
throw new AppError("E_PROVIDER_EMPTY_RESPONSE", "Provider image URL returned an empty body", responseDetails(imgRes, { kind: "empty", length: 0 }));
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
if (isAbortError(err) || err instanceof AppError)
|
|
170
|
+
throw err;
|
|
171
|
+
throw new AppError("E_PROVIDER_BAD_REQUEST", "Image URL download failed", responseDetails(res));
|
|
85
172
|
}
|
|
86
|
-
return Buffer.from(await imgRes.arrayBuffer());
|
|
87
173
|
}
|
|
88
|
-
throw new AppError("
|
|
174
|
+
throw new AppError("E_PROVIDER_EMPTY_RESPONSE", "Provider returned HTTP 200 but data contained no image", responseDetails(res, summarizeJson(data)));
|
|
175
|
+
}
|
|
176
|
+
async function readBodySummary(res) {
|
|
177
|
+
const text = await res.text();
|
|
178
|
+
if (!text)
|
|
179
|
+
return { kind: "empty", length: 0 };
|
|
180
|
+
try {
|
|
181
|
+
return summarizeJson(JSON.parse(text));
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
return { kind: "text", length: text.length };
|
|
185
|
+
}
|
|
89
186
|
}
|
|
90
187
|
async function handleProviderError(res) {
|
|
188
|
+
// 5xx: 上游随机故障,不读 body(chunked 连接可能挂起),直接返回安全的响应头摘要。
|
|
189
|
+
if (res.status >= 500) {
|
|
190
|
+
throw new AppError("E_PROVIDER_BAD_REQUEST", `Upstream error (HTTP ${res.status})`, responseDetails(res));
|
|
191
|
+
}
|
|
192
|
+
const summary = await readBodySummary(res);
|
|
193
|
+
const details = responseDetails(res, summary);
|
|
91
194
|
if (res.status === 401 || res.status === 403) {
|
|
92
|
-
throw new AppError("E_PROVIDER_AUTH", `Auth failed (HTTP ${res.status})
|
|
195
|
+
throw new AppError("E_PROVIDER_AUTH", `Auth failed (HTTP ${res.status})`, details);
|
|
93
196
|
}
|
|
94
197
|
if (res.status === 429) {
|
|
95
|
-
throw new AppError("E_PROVIDER_RATE_LIMIT", "Rate limit exceeded");
|
|
198
|
+
throw new AppError("E_PROVIDER_RATE_LIMIT", "Rate limit exceeded", details);
|
|
96
199
|
}
|
|
97
|
-
|
|
98
|
-
if (res.status >= 500) {
|
|
99
|
-
throw new AppError("E_PROVIDER_BAD_REQUEST", `Upstream error ${res.status} (body skipped to avoid chunked hang)`);
|
|
100
|
-
}
|
|
101
|
-
const text = await res.text().catch(() => "");
|
|
102
|
-
throw new AppError("E_PROVIDER_BAD_REQUEST", `Upstream error ${res.status}: ${text.slice(0, 200)}`);
|
|
200
|
+
throw new AppError("E_PROVIDER_BAD_REQUEST", `Upstream error (HTTP ${res.status})`, details);
|
|
103
201
|
}
|
|
104
|
-
async function
|
|
105
|
-
|
|
106
|
-
const size = mapOpenAISize(opts.size, opts.aspectRatio);
|
|
107
|
-
const hasReferenceImages = Boolean(opts.referenceImages?.length);
|
|
108
|
-
// 有参考图 → /v1/images/edits(multipart form)
|
|
109
|
-
// 无参考图 → /v1/images/generations(JSON body,纯文本)
|
|
110
|
-
const useEdits = hasReferenceImages;
|
|
111
|
-
const url = `${config.baseUrl}${useEdits ? "/v1/images/edits" : "/v1/images/generations"}`;
|
|
112
|
-
let body;
|
|
113
|
-
let headers = {
|
|
114
|
-
Authorization: `Bearer ${config.apiKey}`,
|
|
115
|
-
};
|
|
116
|
-
if (useEdits) {
|
|
117
|
-
// 有参考图:multipart form
|
|
118
|
-
const form = new FormData();
|
|
119
|
-
form.set("model", GPT_IMAGE_MODEL);
|
|
120
|
-
form.set("prompt", prompt);
|
|
121
|
-
form.set("size", size);
|
|
122
|
-
if (opts.aspectRatio) {
|
|
123
|
-
form.set("aspect_ratio", opts.aspectRatio);
|
|
124
|
-
}
|
|
125
|
-
for (const [index, imgPath] of opts.referenceImages.entries()) {
|
|
126
|
-
const data = await resizeIfNeeded(imgPath, opts.referenceMaxSide);
|
|
127
|
-
const bytes = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
|
|
128
|
-
form.append("image", new Blob([bytes], { type: "image/webp" }), `reference-${index}.webp`);
|
|
129
|
-
}
|
|
130
|
-
body = form;
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
// 纯文本生成
|
|
134
|
-
headers = { ...headers, "Content-Type": "application/json" };
|
|
135
|
-
body = JSON.stringify({
|
|
136
|
-
model: GPT_IMAGE_MODEL,
|
|
137
|
-
prompt,
|
|
138
|
-
size,
|
|
139
|
-
...(opts.aspectRatio ? { aspect_ratio: opts.aspectRatio } : {}),
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
// 启用 2 次重试,应对上游偶发的 EOF / 524(Cloudflare 100s 超时)。
|
|
143
|
-
// retries=0 时所有偶发失败一次过即死,指数退避代码是死代码。
|
|
144
|
-
const res = await fetchWithRetry(url, {
|
|
202
|
+
async function postForImage(config, path, body, headers, parseImage) {
|
|
203
|
+
return fetchWithRetry(`${config.baseUrl}${path}`, {
|
|
145
204
|
method: "POST",
|
|
146
205
|
headers,
|
|
147
206
|
body,
|
|
148
|
-
}, config.timeoutMs,
|
|
149
|
-
|
|
150
|
-
|
|
207
|
+
}, config.timeoutMs, async (res, signal) => {
|
|
208
|
+
if (!res.ok) {
|
|
209
|
+
await handleProviderError(res);
|
|
210
|
+
}
|
|
211
|
+
return parseImage(res, signal);
|
|
212
|
+
}, 2);
|
|
213
|
+
}
|
|
214
|
+
export const DEFAULT_GENERATION_REFERENCE_MAX_SIDE = 512;
|
|
215
|
+
export const DEFAULT_MULTI_REFERENCE_MAX_SIDE = 256;
|
|
216
|
+
async function generationBody(opts) {
|
|
217
|
+
const imageUrls = [];
|
|
218
|
+
const referenceImages = opts.referenceImages ?? [];
|
|
219
|
+
const defaultMaxSide = referenceImages.length > 1
|
|
220
|
+
? DEFAULT_MULTI_REFERENCE_MAX_SIDE
|
|
221
|
+
: DEFAULT_GENERATION_REFERENCE_MAX_SIDE;
|
|
222
|
+
for (const imgPath of referenceImages) {
|
|
223
|
+
const maxSide = opts.referenceMaxSide === undefined
|
|
224
|
+
? defaultMaxSide
|
|
225
|
+
: opts.referenceMaxSide;
|
|
226
|
+
const data = await resizeIfNeeded(imgPath, maxSide);
|
|
227
|
+
imageUrls.push(`data:image/webp;base64,${data.toString("base64")}`);
|
|
151
228
|
}
|
|
152
|
-
return
|
|
229
|
+
return JSON.stringify({
|
|
230
|
+
model: GPT_IMAGE_MODEL,
|
|
231
|
+
prompt: opts.prompt,
|
|
232
|
+
size: mapOpenAISize(opts.size, opts.aspectRatio),
|
|
233
|
+
...(imageUrls.length ? { image: imageUrls } : {}),
|
|
234
|
+
...(opts.aspectRatio ? { aspect_ratio: opts.aspectRatio } : {}),
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
export async function editImage(config, opts) {
|
|
238
|
+
const imagePaths = [opts.editTarget, ...(opts.referenceImages ?? [])];
|
|
239
|
+
const form = new FormData();
|
|
240
|
+
form.set("model", GPT_IMAGE_MODEL);
|
|
241
|
+
form.set("prompt", opts.prompt);
|
|
242
|
+
form.set("size", mapOpenAISize(opts.size, opts.aspectRatio));
|
|
243
|
+
const fieldName = imagePaths.length > 1 ? "image[]" : "image";
|
|
244
|
+
for (const [index, imgPath] of imagePaths.entries()) {
|
|
245
|
+
const data = await resizeIfNeeded(imgPath, opts.referenceMaxSide);
|
|
246
|
+
const bytes = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
|
|
247
|
+
form.append(fieldName, new Blob([bytes], { type: "image/webp" }), `input-${index + 1}.webp`);
|
|
248
|
+
}
|
|
249
|
+
return postForImage(config, "/v1/images/edits", form, { Authorization: `Bearer ${config.apiKey}` }, imageFromOpenAIResponse);
|
|
153
250
|
}
|
|
154
251
|
export async function generateImage(config, opts) {
|
|
155
|
-
return
|
|
252
|
+
return postForImage(config, "/v1/images/generations", await generationBody(opts), {
|
|
253
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
254
|
+
"Content-Type": "application/json",
|
|
255
|
+
}, imageFromOpenAIResponse);
|
|
156
256
|
}
|
package/dist/resizer.js
CHANGED
|
@@ -4,7 +4,7 @@ import { AppError } from "./errors.js";
|
|
|
4
4
|
export const DEFAULT_REFERENCE_MAX_SIDE = 1024;
|
|
5
5
|
/**
|
|
6
6
|
* 读入垫图,按需缩放到 maxSide 内(保持比例不放大),
|
|
7
|
-
* 强制重编码为 WebP(quality 85)
|
|
7
|
+
* 强制重编码为 WebP(quality 85),降低多模态与编辑请求的上传体积。
|
|
8
8
|
* PNG 通道比 WebP 大 3~5 倍,改用 WebP 后体积大幅下降,避免上游 100s 超时。
|
|
9
9
|
*/
|
|
10
10
|
export async function resizeIfNeeded(imgPath, maxSide = DEFAULT_REFERENCE_MAX_SIDE) {
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const POLL_RETRY_AFTER_MS = 1_000;
|
|
2
|
+
export function taskSubmissionResponse(task, outputFilename) {
|
|
3
|
+
return {
|
|
4
|
+
accepted: true,
|
|
5
|
+
terminal: false,
|
|
6
|
+
status: "submitted",
|
|
7
|
+
task_id: task.id,
|
|
8
|
+
poll_required: true,
|
|
9
|
+
retry_after_ms: POLL_RETRY_AFTER_MS,
|
|
10
|
+
resolved_intent: task.resolvedIntent,
|
|
11
|
+
effective_prompt: task.effectivePrompt,
|
|
12
|
+
prompt_stats: task.promptStats,
|
|
13
|
+
message: `已接受异步生成任务:${outputFilename}。这不代表图片已生成;必须调用 check_image_task,直到 status 为 done 或 failed。`,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function taskStatusResponse(task) {
|
|
17
|
+
const response = {
|
|
18
|
+
task_id: task.id,
|
|
19
|
+
status: task.status,
|
|
20
|
+
terminal: task.status === "done" || task.status === "failed",
|
|
21
|
+
};
|
|
22
|
+
if (task.effectivePrompt) {
|
|
23
|
+
response.effective_prompt = task.effectivePrompt;
|
|
24
|
+
}
|
|
25
|
+
if (task.promptStats) {
|
|
26
|
+
response.prompt_stats = task.promptStats;
|
|
27
|
+
}
|
|
28
|
+
if (task.resolvedIntent) {
|
|
29
|
+
response.resolved_intent = task.resolvedIntent;
|
|
30
|
+
}
|
|
31
|
+
if (task.status === "done") {
|
|
32
|
+
response.success = true;
|
|
33
|
+
response.saved_path = task.savedPath;
|
|
34
|
+
}
|
|
35
|
+
else if (task.status === "failed") {
|
|
36
|
+
response.success = false;
|
|
37
|
+
response.error = task.errorCode;
|
|
38
|
+
response.message = task.error;
|
|
39
|
+
if (task.errorDetails) {
|
|
40
|
+
response.details = task.errorDetails;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
response.accepted = true;
|
|
45
|
+
response.poll_required = true;
|
|
46
|
+
response.retry_after_ms = POLL_RETRY_AFTER_MS;
|
|
47
|
+
}
|
|
48
|
+
return response;
|
|
49
|
+
}
|