dsh-image-gen 0.1.4 → 0.1.5
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.en.md +166 -163
- package/README.md +169 -159
- package/README.zh-CN.md +169 -159
- package/lib/client.js +22 -11
- package/lib/client.js.map +1 -1
- package/lib/index.js +125 -3
- package/lib/types/client/index.d.ts +2 -0
- package/lib/types/config.d.ts +12 -2
- package/lib/types/dashscope.d.ts +16 -0
- package/lib/types/shared.d.ts +3 -1
- package/package.json +5 -2
package/lib/index.js
CHANGED
|
@@ -12,16 +12,19 @@ const IMAGE_GENERATION_NAMESPACE = "image-generation";
|
|
|
12
12
|
const IMAGE_PROVIDERS = [
|
|
13
13
|
"google",
|
|
14
14
|
"openai",
|
|
15
|
-
"seedream"
|
|
15
|
+
"seedream",
|
|
16
|
+
"dashscope"
|
|
16
17
|
];
|
|
17
18
|
/** Default endpoints and base URLs. */
|
|
18
19
|
const DEFAULT_GOOGLE_ENDPOINT = "https://generativelanguage.googleapis.com/v1beta/interactions";
|
|
19
20
|
const DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1";
|
|
20
21
|
const DEFAULT_SEEDREAM_BASE_URL = "https://ark.cn-beijing.volces.com/api/v3";
|
|
22
|
+
const DEFAULT_DASHSCOPE_ENDPOINT = "https://dashscope.aliyuncs.com/api/v1";
|
|
21
23
|
/** Default model names. */
|
|
22
24
|
const DEFAULT_GOOGLE_MODEL = "gemini-3.1-flash-image";
|
|
23
25
|
const DEFAULT_OPENAI_MODEL = "gpt-image-2";
|
|
24
26
|
const DEFAULT_SEEDREAM_MODEL = "doubao-seedream-5-0-260128";
|
|
27
|
+
const DEFAULT_DASHSCOPE_MODEL = "wanx2.1-t2i-turbo";
|
|
25
28
|
//#endregion
|
|
26
29
|
//#region lib/types/config.js
|
|
27
30
|
/** User-facing configuration for supported image providers. */
|
|
@@ -31,6 +34,8 @@ const GOOGLE_API_KEY_ENV = "GEMINI_API_KEY";
|
|
|
31
34
|
const OPENAI_API_KEY_ENV = "OPENAI_API_KEY";
|
|
32
35
|
/** Volcengine Ark credential reference. */
|
|
33
36
|
const SEEDREAM_API_KEY_ENV = "ARK_API_KEY";
|
|
37
|
+
/** DashScope credential reference. */
|
|
38
|
+
const DASHSCOPE_API_KEY_ENV = "DASHSCOPE_API_KEY";
|
|
34
39
|
/** Cordis configuration schema. */
|
|
35
40
|
const Config = z.object({
|
|
36
41
|
provider: z.union(IMAGE_PROVIDERS).default("google"),
|
|
@@ -39,7 +44,9 @@ const Config = z.object({
|
|
|
39
44
|
openaiBaseURL: z.string().default(DEFAULT_OPENAI_BASE_URL),
|
|
40
45
|
openaiModel: z.string().default(DEFAULT_OPENAI_MODEL),
|
|
41
46
|
seedreamBaseURL: z.string().default(DEFAULT_SEEDREAM_BASE_URL),
|
|
42
|
-
seedreamModel: z.string().default(DEFAULT_SEEDREAM_MODEL)
|
|
47
|
+
seedreamModel: z.string().default(DEFAULT_SEEDREAM_MODEL),
|
|
48
|
+
dashscopeEndpoint: z.string().default(DEFAULT_DASHSCOPE_ENDPOINT),
|
|
49
|
+
dashscopeModel: z.string().default(DEFAULT_DASHSCOPE_MODEL)
|
|
43
50
|
});
|
|
44
51
|
/** Resolve exactly one provider profile for a tool call. */
|
|
45
52
|
function resolveProvider(config) {
|
|
@@ -58,6 +65,13 @@ function resolveProvider(config) {
|
|
|
58
65
|
baseURL: config.seedreamBaseURL ?? "https://ark.cn-beijing.volces.com/api/v3",
|
|
59
66
|
imageSize: "2K"
|
|
60
67
|
};
|
|
68
|
+
case "dashscope": return {
|
|
69
|
+
provider: "dashscope",
|
|
70
|
+
apiKeyEnv: DASHSCOPE_API_KEY_ENV,
|
|
71
|
+
model: config.dashscopeModel ?? "wanx2.1-t2i-turbo",
|
|
72
|
+
endpoint: config.dashscopeEndpoint ?? "https://dashscope.aliyuncs.com/api/v1",
|
|
73
|
+
imageSize: "1024*1024"
|
|
74
|
+
};
|
|
61
75
|
case "google": return {
|
|
62
76
|
provider: "google",
|
|
63
77
|
apiKeyEnv: GOOGLE_API_KEY_ENV,
|
|
@@ -69,6 +83,102 @@ function resolveProvider(config) {
|
|
|
69
83
|
}
|
|
70
84
|
}
|
|
71
85
|
//#endregion
|
|
86
|
+
//#region lib/types/dashscope.js
|
|
87
|
+
/** Call DashScope image synthesis/generation API and handle both synchronous output & async task polling. */
|
|
88
|
+
async function generateDashScopeImage(options) {
|
|
89
|
+
const base = options.endpoint.replace(/\/+$/, "");
|
|
90
|
+
const formattedSize = (options.size ?? "1024*1024").replace("x", "*");
|
|
91
|
+
const isQwenImage = options.model.toLowerCase().startsWith("qwen-image");
|
|
92
|
+
const submitUrl = isQwenImage ? `${base}/services/aigc/multimodal-generation/generation` : `${base}/services/aigc/text2image/image-synthesis`;
|
|
93
|
+
const requestBody = isQwenImage ? {
|
|
94
|
+
model: options.model,
|
|
95
|
+
input: { messages: [{
|
|
96
|
+
role: "user",
|
|
97
|
+
content: [{ text: options.prompt }]
|
|
98
|
+
}] },
|
|
99
|
+
parameters: { size: formattedSize }
|
|
100
|
+
} : {
|
|
101
|
+
model: options.model,
|
|
102
|
+
input: { prompt: options.prompt },
|
|
103
|
+
parameters: {
|
|
104
|
+
size: formattedSize,
|
|
105
|
+
n: 1
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const submitResponse = await fetch(submitUrl, {
|
|
109
|
+
method: "POST",
|
|
110
|
+
...options.signal ? { signal: options.signal } : {},
|
|
111
|
+
headers: {
|
|
112
|
+
"content-type": "application/json",
|
|
113
|
+
authorization: `Bearer ${options.apiKey}`,
|
|
114
|
+
"X-DashScope-Async": "enable"
|
|
115
|
+
},
|
|
116
|
+
body: JSON.stringify(requestBody)
|
|
117
|
+
});
|
|
118
|
+
if (!submitResponse.ok) {
|
|
119
|
+
const errorText = await submitResponse.text();
|
|
120
|
+
throw new Error(`DashScope image generation failed (${String(submitResponse.status)}): ${errorText}`);
|
|
121
|
+
}
|
|
122
|
+
const submitResult = await submitResponse.json();
|
|
123
|
+
const directImageUrl = extractImageUrl(submitResult);
|
|
124
|
+
if (directImageUrl !== void 0) return downloadImageBlob(directImageUrl, options);
|
|
125
|
+
const taskId = submitResult.output?.task_id;
|
|
126
|
+
if (taskId === void 0 || taskId.length === 0) throw new Error(`DashScope did not return an image URL or task_id: ${submitResult.message ?? JSON.stringify(submitResult)}`);
|
|
127
|
+
const taskQueryUrl = `${base}/tasks/${taskId}`;
|
|
128
|
+
const startTime = Date.now();
|
|
129
|
+
const timeoutMs = 6e4;
|
|
130
|
+
while (Date.now() - startTime < timeoutMs) {
|
|
131
|
+
if (options.signal?.aborted) throw new Error("DashScope image task polling aborted");
|
|
132
|
+
await new Promise((resolve, reject) => {
|
|
133
|
+
const timer = setTimeout(resolve, 1500);
|
|
134
|
+
if (options.signal !== void 0) options.signal.addEventListener("abort", () => {
|
|
135
|
+
clearTimeout(timer);
|
|
136
|
+
reject(/* @__PURE__ */ new Error("DashScope image generation aborted"));
|
|
137
|
+
}, { once: true });
|
|
138
|
+
});
|
|
139
|
+
const taskResponse = await fetch(taskQueryUrl, {
|
|
140
|
+
method: "GET",
|
|
141
|
+
...options.signal ? { signal: options.signal } : {},
|
|
142
|
+
headers: { authorization: `Bearer ${options.apiKey}` }
|
|
143
|
+
});
|
|
144
|
+
if (!taskResponse.ok) {
|
|
145
|
+
const errorText = await taskResponse.text();
|
|
146
|
+
throw new Error(`DashScope task query failed (${String(taskResponse.status)}): ${errorText}`);
|
|
147
|
+
}
|
|
148
|
+
const taskResult = await taskResponse.json();
|
|
149
|
+
const status = taskResult.output?.task_status;
|
|
150
|
+
if (status === "SUCCEEDED") {
|
|
151
|
+
const imageUrl = extractImageUrl(taskResult);
|
|
152
|
+
if (imageUrl === void 0 || imageUrl.length === 0) throw new Error("DashScope task succeeded but returned no image URL");
|
|
153
|
+
return downloadImageBlob(imageUrl, options);
|
|
154
|
+
}
|
|
155
|
+
if (status === "FAILED") throw new Error(`DashScope image generation failed: ${taskResult.output?.message ?? taskResult.message ?? "Unknown error"}`);
|
|
156
|
+
}
|
|
157
|
+
throw new Error(`DashScope image generation timed out after ${String(timeoutMs / 1e3)} seconds`);
|
|
158
|
+
}
|
|
159
|
+
function extractImageUrl(response) {
|
|
160
|
+
const resultUrl = response.output?.results?.[0]?.url;
|
|
161
|
+
if (resultUrl !== void 0 && resultUrl.length > 0) return resultUrl;
|
|
162
|
+
const contents = response.output?.choices?.[0]?.message?.content;
|
|
163
|
+
if (Array.isArray(contents)) for (const item of contents) {
|
|
164
|
+
if (item.image !== void 0 && item.image.length > 0) return item.image;
|
|
165
|
+
if (item.image_url !== void 0 && item.image_url.length > 0) return item.image_url;
|
|
166
|
+
if (item.url !== void 0 && item.url.length > 0) return item.url;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async function downloadImageBlob(imageUrl, options) {
|
|
170
|
+
const imageResponse = await fetch(imageUrl, { ...options.signal ? { signal: options.signal } : {} });
|
|
171
|
+
if (!imageResponse.ok) throw new Error(`Failed to fetch DashScope image from URL (${String(imageResponse.status)})`);
|
|
172
|
+
const buffer = await imageResponse.arrayBuffer();
|
|
173
|
+
if (buffer.byteLength > options.maxBytes) throw new Error(`DashScope generated image (${String(buffer.byteLength)} bytes) exceeds the ${String(options.maxBytes)} byte limit`);
|
|
174
|
+
const contentType = imageResponse.headers.get("content-type");
|
|
175
|
+
const mediaType = contentType?.includes("png") ? "image/png" : contentType?.includes("webp") ? "image/webp" : "image/jpeg";
|
|
176
|
+
return {
|
|
177
|
+
data: new Uint8Array(buffer),
|
|
178
|
+
mediaType
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
//#endregion
|
|
72
182
|
//#region lib/types/google.js
|
|
73
183
|
const ERROR_LIMIT$1 = 4096;
|
|
74
184
|
const REQUESTED_MEDIA_TYPE = "image/jpeg";
|
|
@@ -405,7 +515,7 @@ function apply(ctx, config = {}) {
|
|
|
405
515
|
},
|
|
406
516
|
size: {
|
|
407
517
|
type: "string",
|
|
408
|
-
description: "Optional dimensions or size tier for OpenAI or
|
|
518
|
+
description: "Optional dimensions or size tier for OpenAI, Seedream, or DashScope."
|
|
409
519
|
}
|
|
410
520
|
},
|
|
411
521
|
output: {
|
|
@@ -486,6 +596,18 @@ function apply(ctx, config = {}) {
|
|
|
486
596
|
signal: exec.signal
|
|
487
597
|
}), active.provider, active.model, `${aspectRatio}, ${imageSize}`);
|
|
488
598
|
}
|
|
599
|
+
if (active.provider === "dashscope") {
|
|
600
|
+
const size = args.size ?? active.imageSize;
|
|
601
|
+
return saveGenerated(ctx, await generateDashScopeImage({
|
|
602
|
+
apiKey: credential.value,
|
|
603
|
+
endpoint: active.endpoint,
|
|
604
|
+
model: active.model,
|
|
605
|
+
prompt: args.prompt,
|
|
606
|
+
size,
|
|
607
|
+
maxBytes: ctx.attachments.imageLimits.maxImageBytes,
|
|
608
|
+
signal: exec.signal
|
|
609
|
+
}), active.provider, active.model, size);
|
|
610
|
+
}
|
|
489
611
|
const size = args.size ?? active.imageSize;
|
|
490
612
|
return saveGenerated(ctx, await generateOpenAICompatibleImage({
|
|
491
613
|
provider: active.provider,
|
package/lib/types/config.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
/** User-facing configuration for supported image providers. */
|
|
2
2
|
import z from '@deepseek-ai/schemastery';
|
|
3
|
-
import { DEFAULT_GOOGLE_ENDPOINT, DEFAULT_GOOGLE_MODEL, DEFAULT_OPENAI_BASE_URL, DEFAULT_OPENAI_MODEL, DEFAULT_SEEDREAM_BASE_URL, DEFAULT_SEEDREAM_MODEL, IMAGE_PROVIDERS, type ImageProvider } from './shared.js';
|
|
4
|
-
export { DEFAULT_GOOGLE_ENDPOINT, DEFAULT_GOOGLE_MODEL, DEFAULT_OPENAI_BASE_URL, DEFAULT_OPENAI_MODEL, DEFAULT_SEEDREAM_BASE_URL, DEFAULT_SEEDREAM_MODEL, IMAGE_PROVIDERS, type ImageProvider, };
|
|
3
|
+
import { DEFAULT_DASHSCOPE_ENDPOINT, DEFAULT_DASHSCOPE_MODEL, DEFAULT_GOOGLE_ENDPOINT, DEFAULT_GOOGLE_MODEL, DEFAULT_OPENAI_BASE_URL, DEFAULT_OPENAI_MODEL, DEFAULT_SEEDREAM_BASE_URL, DEFAULT_SEEDREAM_MODEL, IMAGE_PROVIDERS, type ImageProvider } from './shared.js';
|
|
4
|
+
export { DEFAULT_DASHSCOPE_ENDPOINT, DEFAULT_DASHSCOPE_MODEL, DEFAULT_GOOGLE_ENDPOINT, DEFAULT_GOOGLE_MODEL, DEFAULT_OPENAI_BASE_URL, DEFAULT_OPENAI_MODEL, DEFAULT_SEEDREAM_BASE_URL, DEFAULT_SEEDREAM_MODEL, IMAGE_PROVIDERS, type ImageProvider, };
|
|
5
5
|
/** Google API credential reference. */
|
|
6
6
|
export declare const GOOGLE_API_KEY_ENV = "GEMINI_API_KEY";
|
|
7
7
|
/** OpenAI Platform or compatible relay credential reference. */
|
|
8
8
|
export declare const OPENAI_API_KEY_ENV = "OPENAI_API_KEY";
|
|
9
9
|
/** Volcengine Ark credential reference. */
|
|
10
10
|
export declare const SEEDREAM_API_KEY_ENV = "ARK_API_KEY";
|
|
11
|
+
/** DashScope credential reference. */
|
|
12
|
+
export declare const DASHSCOPE_API_KEY_ENV = "DASHSCOPE_API_KEY";
|
|
11
13
|
/** Google tool-level controls. */
|
|
12
14
|
export declare const ASPECT_RATIOS: readonly ["1:1", "3:2", "2:3", "4:3", "3:4", "16:9", "9:16"];
|
|
13
15
|
export declare const IMAGE_SIZES: readonly ["1K", "2K", "4K"];
|
|
@@ -22,6 +24,8 @@ export interface Config {
|
|
|
22
24
|
openaiModel?: string;
|
|
23
25
|
seedreamBaseURL?: string;
|
|
24
26
|
seedreamModel?: string;
|
|
27
|
+
dashscopeEndpoint?: string;
|
|
28
|
+
dashscopeModel?: string;
|
|
25
29
|
}
|
|
26
30
|
/** Cordis configuration schema. */
|
|
27
31
|
export declare const Config: z<Config>;
|
|
@@ -45,4 +49,10 @@ export declare function resolveProvider(config: Config): {
|
|
|
45
49
|
model: string;
|
|
46
50
|
baseURL: string;
|
|
47
51
|
imageSize: string;
|
|
52
|
+
} | {
|
|
53
|
+
provider: 'dashscope';
|
|
54
|
+
apiKeyEnv: string;
|
|
55
|
+
model: string;
|
|
56
|
+
endpoint: string;
|
|
57
|
+
imageSize: string;
|
|
48
58
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** DashScope (Aliyun Wanx & Qwen-Image) image-generation protocol adapter. */
|
|
2
|
+
import type { ImageAttachmentRef } from '@deepseek-ai/dsh-attachment';
|
|
3
|
+
export interface DashScopeImageOptions {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
endpoint: string;
|
|
6
|
+
model: string;
|
|
7
|
+
prompt: string;
|
|
8
|
+
size?: string;
|
|
9
|
+
maxBytes: number;
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
/** Call DashScope image synthesis/generation API and handle both synchronous output & async task polling. */
|
|
13
|
+
export declare function generateDashScopeImage(options: DashScopeImageOptions): Promise<{
|
|
14
|
+
data: Uint8Array;
|
|
15
|
+
mediaType: ImageAttachmentRef['mediaType'];
|
|
16
|
+
}>;
|
package/lib/types/shared.d.ts
CHANGED
|
@@ -4,15 +4,17 @@ export declare const IMAGE_ROUTE = "/plugins/dsh-image-gen/image";
|
|
|
4
4
|
/** Namespace persisted through DSH Settings. */
|
|
5
5
|
export declare const IMAGE_GENERATION_NAMESPACE = "image-generation";
|
|
6
6
|
/** Supported providers. */
|
|
7
|
-
export declare const IMAGE_PROVIDERS: readonly ["google", "openai", "seedream"];
|
|
7
|
+
export declare const IMAGE_PROVIDERS: readonly ["google", "openai", "seedream", "dashscope"];
|
|
8
8
|
export type ImageProvider = typeof IMAGE_PROVIDERS[number];
|
|
9
9
|
/** Default endpoints and base URLs. */
|
|
10
10
|
export declare const DEFAULT_GOOGLE_ENDPOINT = "https://generativelanguage.googleapis.com/v1beta/interactions";
|
|
11
11
|
export declare const DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1";
|
|
12
12
|
export declare const DEFAULT_SEEDREAM_BASE_URL = "https://ark.cn-beijing.volces.com/api/v3";
|
|
13
|
+
export declare const DEFAULT_DASHSCOPE_ENDPOINT = "https://dashscope.aliyuncs.com/api/v1";
|
|
13
14
|
/** Default model names. */
|
|
14
15
|
export declare const DEFAULT_GOOGLE_MODEL = "gemini-3.1-flash-image";
|
|
15
16
|
export declare const DEFAULT_OPENAI_MODEL = "gpt-image-2";
|
|
16
17
|
export declare const DEFAULT_SEEDREAM_MODEL = "doubao-seedream-5-0-260128";
|
|
18
|
+
export declare const DEFAULT_DASHSCOPE_MODEL = "wanx2.1-t2i-turbo";
|
|
17
19
|
export declare const DEFAULT_MODELS: Record<ImageProvider, string>;
|
|
18
20
|
export declare const DEFAULT_BASE_URLS: Record<ImageProvider, string>;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-image-gen",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Bring ChatGPT-like image generation to DeepSeek Harness — Gemini, OpenAI, Seedream & more.",
|
|
3
|
+
"version": "0.1.5",
|
|
4
|
+
"description": "Bring ChatGPT-like image generation to DeepSeek Harness — Gemini, OpenAI, Seedream, DashScope & more.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "shanliuling",
|
|
7
7
|
"repository": {
|
|
@@ -71,6 +71,9 @@
|
|
|
71
71
|
"openai",
|
|
72
72
|
"openai-compatible",
|
|
73
73
|
"seedream",
|
|
74
|
+
"dashscope",
|
|
75
|
+
"wanx",
|
|
76
|
+
"aliyun",
|
|
74
77
|
"volcengine",
|
|
75
78
|
"byok",
|
|
76
79
|
"cordis"
|