@xsai/generate-image 0.2.0-beta.5 → 0.2.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/dist/index.d.ts +21 -1
- package/dist/index.js +37 -9
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,14 @@ interface GenerateImageOptions extends CommonRequestOptions {
|
|
|
9
9
|
n?: number;
|
|
10
10
|
/** A text description of the desired image(s). */
|
|
11
11
|
prompt: string;
|
|
12
|
+
/**
|
|
13
|
+
* The format in which generated images with `dall-e-2` and `dall-e-3` are returned from the API.
|
|
14
|
+
* `gpt-image-1` only supports `b64_json`, and `url` will be ignored.
|
|
15
|
+
*
|
|
16
|
+
* Images will always be Base64-encoded in the result. If `url` is used, the images will be
|
|
17
|
+
* fetched upon receiving the response.
|
|
18
|
+
*/
|
|
19
|
+
responseFormat?: 'b64_json' | 'url';
|
|
12
20
|
/**
|
|
13
21
|
* The size of the generated images.
|
|
14
22
|
* @default `1024x1024`
|
|
@@ -18,8 +26,20 @@ interface GenerateImageOptions extends CommonRequestOptions {
|
|
|
18
26
|
interface GenerateImageResponse {
|
|
19
27
|
created: number;
|
|
20
28
|
data: {
|
|
21
|
-
|
|
29
|
+
/**
|
|
30
|
+
* The Bse64-encoded JSON of the generated image. Default value for `gpt-image-1`, and only
|
|
31
|
+
* present if `response_format` is set to `b64_json` for `dall-e-2` and `dall-e-3`.
|
|
32
|
+
*/
|
|
33
|
+
b64_json?: string;
|
|
34
|
+
/**
|
|
35
|
+
* For `dall-e-3` only, the revised prompt that was used to generate the image.
|
|
36
|
+
*/
|
|
22
37
|
revised_prompt?: string;
|
|
38
|
+
/**
|
|
39
|
+
* When using `dall-e-2` or `dall-e-3`, the URL of the generated image if `response_format` is
|
|
40
|
+
* set to `url` (default value). Unsupported for `gpt-image-1`.
|
|
41
|
+
*/
|
|
42
|
+
url?: string;
|
|
23
43
|
}[];
|
|
24
44
|
}
|
|
25
45
|
interface GenerateImageResult {
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { requestURL, requestHeaders, requestBody, responseJSON } from '@xsai/shared';
|
|
1
|
+
import { requestURL, requestHeaders, requestBody, responseJSON, responseCatch } from '@xsai/shared';
|
|
2
2
|
|
|
3
3
|
const mimeTypes = {
|
|
4
4
|
"/9j/": "image/jpg",
|
|
@@ -16,20 +16,48 @@ const convertImage = (b64_json) => {
|
|
|
16
16
|
mimeType
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
|
+
const responseBlobAsDataURL = async (res) => responseCatch(res).then(async (res2) => {
|
|
20
|
+
const blob = await res2.blob();
|
|
21
|
+
try {
|
|
22
|
+
return await new Promise((resolve, reject) => {
|
|
23
|
+
const reader = new FileReader();
|
|
24
|
+
reader.onloadend = () => resolve(reader.result);
|
|
25
|
+
reader.onerror = reject;
|
|
26
|
+
reader.readAsDataURL(blob);
|
|
27
|
+
});
|
|
28
|
+
} catch {
|
|
29
|
+
throw new Error(`Failed to parse response blob, response URL: ${res2.url}`);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
19
32
|
const generateImage = async (options) => (options.fetch ?? globalThis.fetch)(requestURL("images/generations", options.baseURL), {
|
|
20
|
-
body: requestBody(
|
|
21
|
-
...options,
|
|
22
|
-
responseFormat: "b64_json"
|
|
23
|
-
}),
|
|
33
|
+
body: requestBody(options),
|
|
24
34
|
headers: requestHeaders({
|
|
25
35
|
"Content-Type": "application/json",
|
|
26
36
|
...options.headers
|
|
27
37
|
}, options.apiKey),
|
|
28
38
|
method: "POST",
|
|
29
39
|
signal: options.abortSignal
|
|
30
|
-
}).then(responseJSON).then(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
40
|
+
}).then(responseJSON).then(
|
|
41
|
+
async ({ data }) => Promise.all(
|
|
42
|
+
data.map(async (img, i) => {
|
|
43
|
+
if (typeof img.b64_json === "string") {
|
|
44
|
+
return convertImage(img.b64_json);
|
|
45
|
+
}
|
|
46
|
+
if (typeof img.url === "string") {
|
|
47
|
+
return (options.fetch ?? globalThis.fetch)(new URL(img.url), {
|
|
48
|
+
signal: options.abortSignal
|
|
49
|
+
}).then(responseBlobAsDataURL).then((dataURL) => {
|
|
50
|
+
const sepIndex = dataURL.indexOf(";");
|
|
51
|
+
const mimeType = dataURL.substring(5, sepIndex);
|
|
52
|
+
return {
|
|
53
|
+
base64: dataURL,
|
|
54
|
+
mimeType
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
throw new Error(`Unrecognized image at index ${i}: ${JSON.stringify(img)}`);
|
|
59
|
+
})
|
|
60
|
+
)
|
|
61
|
+
).then((images) => ({ image: images[0], images }));
|
|
34
62
|
|
|
35
63
|
export { generateImage };
|