@xsai/image 0.0.0-stage → 1.0.0-beta.1
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 +21 -0
- package/README.md +25 -2
- package/dist/index.d.ts +45 -0
- package/dist/index.js +69 -0
- package/package.json +40 -3
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
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @xsai/image
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<!-- automd:file src="/docs/snippets/image.md" -->
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pnpm add @xsai/image
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { writeFile } from 'node:fs/promises'
|
|
11
|
+
|
|
12
|
+
import { generateImage, generations } from '@xsai/image'
|
|
13
|
+
|
|
14
|
+
const model = generations({
|
|
15
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
16
|
+
baseURL: 'https://api.openai.com/v1/',
|
|
17
|
+
model: 'YOUR_IMAGE_MODEL_ID',
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
const { image } = await generateImage(model, { input: 'A small cabin in a quiet forest.' })
|
|
21
|
+
await writeFile('cabin.png', new Uint8Array(await image.arrayBuffer()))
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
<!-- /automd -->
|
|
25
|
+
|
|
26
|
+
Read the [documentation](https://xsai.js.org/image).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Promisable, HttpOptions } from '@xsai/shared';
|
|
2
|
+
|
|
3
|
+
type ImageModel = (options: ImageModelOptions) => Promisable<ImageModelResult>;
|
|
4
|
+
interface ImageModelOptions {
|
|
5
|
+
input: string;
|
|
6
|
+
n?: number;
|
|
7
|
+
providerOptions?: ImageModelProviderOptions;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
size?: `${number}x${number}`;
|
|
10
|
+
}
|
|
11
|
+
interface ImageModelProviderOptions {
|
|
12
|
+
}
|
|
13
|
+
interface ImageModelResult {
|
|
14
|
+
images: Blob[];
|
|
15
|
+
providerMetadata?: ImageModelResultProviderMetadata;
|
|
16
|
+
}
|
|
17
|
+
interface ImageModelResultProviderMetadata {
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface GenerateImageResult extends ImageModelResult {
|
|
21
|
+
image: Blob;
|
|
22
|
+
}
|
|
23
|
+
declare const generateImage: (model: ImageModel, options: ImageModelOptions) => Promise<GenerateImageResult>;
|
|
24
|
+
|
|
25
|
+
declare module '@xsai/image' {
|
|
26
|
+
interface ImageModelProviderOptions {
|
|
27
|
+
generations?: {
|
|
28
|
+
background?: 'auto' | 'opaque' | 'transparent';
|
|
29
|
+
outputCompression?: number;
|
|
30
|
+
outputFormat?: 'jpeg' | 'png' | 'webp';
|
|
31
|
+
quality?: 'auto' | 'high' | 'low' | 'max' | 'medium' | 'xhigh';
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
interface ImageModelResultProviderMetadata {
|
|
35
|
+
generations?: {
|
|
36
|
+
images?: {
|
|
37
|
+
revisedPrompt?: string;
|
|
38
|
+
}[];
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
declare const generations: (options: HttpOptions) => ImageModel;
|
|
43
|
+
|
|
44
|
+
export { generateImage, generations };
|
|
45
|
+
export type { GenerateImageResult, ImageModel, ImageModelOptions, ImageModelProviderOptions, ImageModelResult, ImageModelResultProviderMetadata };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { XSAIError, sendRequest } from '@xsai/shared';
|
|
2
|
+
|
|
3
|
+
const generateImage = async (model, options) => {
|
|
4
|
+
const result = await model(options);
|
|
5
|
+
if (result.images.length === 0)
|
|
6
|
+
throw new XSAIError("invalid-response", "Image model returned no images");
|
|
7
|
+
return { ...result, image: result.images[0] };
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const imageBlob = (base64) => {
|
|
11
|
+
const bytes = Uint8Array.fromBase64(base64);
|
|
12
|
+
const matches = (pattern, offset = 0) => {
|
|
13
|
+
for (let index = 0; index < pattern.length; index++) {
|
|
14
|
+
if (bytes[offset + index] !== pattern.charCodeAt(index))
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
return true;
|
|
18
|
+
};
|
|
19
|
+
let type;
|
|
20
|
+
if (matches("\x89PNG\r\n\n")) {
|
|
21
|
+
type = "image/png";
|
|
22
|
+
} else if (matches("\xFF\xD8\xFF")) {
|
|
23
|
+
type = "image/jpeg";
|
|
24
|
+
} else if (matches("GIF87a") || matches("GIF89a")) {
|
|
25
|
+
type = "image/gif";
|
|
26
|
+
} else if (matches("RIFF") && matches("WEBPVP", 8)) {
|
|
27
|
+
type = "image/webp";
|
|
28
|
+
} else if (matches("ftyp", 4)) {
|
|
29
|
+
const end = Math.min(bytes.length, new DataView(bytes.buffer).getUint32(0));
|
|
30
|
+
for (let offset = 8; offset + 4 <= end; offset += 4) {
|
|
31
|
+
if (offset !== 12 && (matches("avif", offset) || matches("avis", offset))) {
|
|
32
|
+
type = "image/avif";
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (type == null)
|
|
38
|
+
throw new XSAIError("invalid-response", "Unrecognized image format");
|
|
39
|
+
return new Blob([bytes], { type });
|
|
40
|
+
};
|
|
41
|
+
const generations = (options) => async (modelOptions) => {
|
|
42
|
+
const wireOptions = modelOptions.providerOptions?.generations;
|
|
43
|
+
const response = await sendRequest({
|
|
44
|
+
body: {
|
|
45
|
+
background: wireOptions?.background,
|
|
46
|
+
model: options.model,
|
|
47
|
+
n: modelOptions.n,
|
|
48
|
+
output_compression: wireOptions?.outputCompression,
|
|
49
|
+
output_format: wireOptions?.outputFormat,
|
|
50
|
+
prompt: modelOptions.input,
|
|
51
|
+
quality: wireOptions?.quality,
|
|
52
|
+
size: modelOptions.size
|
|
53
|
+
},
|
|
54
|
+
path: "images/generations",
|
|
55
|
+
signal: modelOptions.signal
|
|
56
|
+
}, options);
|
|
57
|
+
const json = await response.json();
|
|
58
|
+
const result = { images: json.data.map((image) => imageBlob(image.b64_json)) };
|
|
59
|
+
if (json.data.some((image) => image.revised_prompt != null)) {
|
|
60
|
+
result.providerMetadata = {
|
|
61
|
+
generations: {
|
|
62
|
+
images: json.data.map((image) => image.revised_prompt == null ? {} : { revisedPrompt: image.revised_prompt })
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export { generateImage, generations };
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/image",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0-beta.1",
|
|
5
|
+
"description": "extra-small AI SDK: image generation.",
|
|
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/image"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://github.com/moeru-ai/xsai/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"xsai",
|
|
17
|
+
"ai",
|
|
18
|
+
"openai",
|
|
19
|
+
"image",
|
|
20
|
+
"image-generation"
|
|
21
|
+
],
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@xsai/shared": "1.0.0-beta.1"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "pkgroll --clean-dist",
|
|
38
|
+
"test": "vitest run --exclude e2e/**",
|
|
39
|
+
"test:watch": "vitest"
|
|
40
|
+
},
|
|
41
|
+
"main": "./dist/index.js",
|
|
42
|
+
"types": "./dist/index.d.ts"
|
|
6
43
|
}
|