@xsai/generate-image 0.2.0-beta.2

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Moeru AI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ https://xsai.js.org/docs/packages/generate/image
@@ -0,0 +1,36 @@
1
+ import { CommonRequestOptions } from '@xsai/shared';
2
+
3
+ interface GenerateImageOptions extends CommonRequestOptions {
4
+ [key: string]: unknown;
5
+ /**
6
+ * The number of images to generate.
7
+ * @default `1`
8
+ */
9
+ n?: number;
10
+ /** A text description of the desired image(s). */
11
+ prompt: string;
12
+ /**
13
+ * The size of the generated images.
14
+ * @default `1024x1024`
15
+ */
16
+ size?: `${number}x${number}`;
17
+ }
18
+ interface GenerateImageResponse {
19
+ created: number;
20
+ data: {
21
+ b64_json: string;
22
+ revised_prompt?: string;
23
+ }[];
24
+ }
25
+ interface GenerateImageResult {
26
+ image: GenerateImageResultImage;
27
+ images: GenerateImageResultImage[];
28
+ }
29
+ interface GenerateImageResultImage {
30
+ base64: string;
31
+ mimeType: string;
32
+ }
33
+ /** @experimental */
34
+ declare const generateImage: (options: GenerateImageOptions) => Promise<GenerateImageResult>;
35
+
36
+ export { type GenerateImageOptions, type GenerateImageResponse, type GenerateImageResult, type GenerateImageResultImage, generateImage };
package/dist/index.js ADDED
@@ -0,0 +1,35 @@
1
+ import { requestURL, requestHeaders, requestBody, responseJSON } from '@xsai/shared';
2
+
3
+ const mimeTypes = {
4
+ "/9j/": "image/jpg",
5
+ "AAAAIGZ0eXBhdmlm": "image/avif",
6
+ "iVBORw0KGgo": "image/png",
7
+ "R0lGOD": "image/gif",
8
+ "UklGRg==": "image/webp"
9
+ };
10
+ const convertImage = (b64_json) => {
11
+ const key = Object.keys(mimeTypes).find((prefix) => b64_json.startsWith(prefix));
12
+ const mimeType = mimeTypes[key ?? "iVBORw0KGgo"];
13
+ return {
14
+ // eslint-disable-next-line @masknet/string-no-data-url
15
+ base64: `data:${mimeType};base64,${b64_json}`,
16
+ mimeType
17
+ };
18
+ };
19
+ const generateImage = async (options) => (options.fetch ?? globalThis.fetch)(requestURL("images/generations", options.baseURL), {
20
+ body: requestBody({
21
+ ...options,
22
+ responseFormat: "b64_json"
23
+ }),
24
+ headers: requestHeaders({
25
+ "Content-Type": "application/json",
26
+ ...options.headers
27
+ }, options.apiKey),
28
+ method: "POST",
29
+ signal: options.abortSignal
30
+ }).then(responseJSON).then(({ data }) => {
31
+ const images = data.map(({ b64_json }) => convertImage(b64_json));
32
+ return { image: images[0], images };
33
+ });
34
+
35
+ export { generateImage };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@xsai/generate-image",
3
+ "type": "module",
4
+ "version": "0.2.0-beta.2",
5
+ "description": "extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.",
6
+ "author": "Moeru AI",
7
+ "license": "MIT",
8
+ "homepage": "https://xsai.js.org",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/moeru-ai/xsai.git",
12
+ "directory": "packages/generate-image"
13
+ },
14
+ "bugs": "https://github.com/moeru-ai/xsai/issues",
15
+ "keywords": [
16
+ "xsai",
17
+ "openai",
18
+ "ai"
19
+ ],
20
+ "sideEffects": false,
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "dependencies": {
32
+ "@xsai/shared": ""
33
+ },
34
+ "scripts": {
35
+ "build": "pkgroll"
36
+ },
37
+ "main": "./dist/index.js",
38
+ "types": "./dist/index.d.ts"
39
+ }