utilitas 1999.1.33 → 1999.1.35
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 +1 -0
- package/dist/utilitas.lite.mjs +1 -1
- package/dist/utilitas.lite.mjs.map +1 -1
- package/lib/image.mjs +74 -38
- package/lib/manifest.mjs +1 -1
- package/lib/storage.mjs +5 -4
- package/package.json +1 -1
package/lib/image.mjs
CHANGED
|
@@ -1,51 +1,87 @@
|
|
|
1
|
-
import { convert } from './storage.mjs';
|
|
2
1
|
import { ensureString, need } from './utilitas.mjs';
|
|
2
|
+
import { MIME_PNG, convert } from './storage.mjs';
|
|
3
3
|
|
|
4
|
-
const _NEED = [
|
|
5
|
-
|
|
6
|
-
];
|
|
7
|
-
|
|
8
|
-
const [BASE64, BUFFER] = ['BASE64', 'BUFFER'];
|
|
9
|
-
|
|
10
|
-
let client;
|
|
4
|
+
const _NEED = ['OpenAI'];
|
|
5
|
+
const [OPENAI, GEMINI, BASE64, BUFFER]
|
|
6
|
+
= ['OPENAI', 'GEMINI', 'BASE64', 'BUFFER'];
|
|
7
|
+
const clients = {};
|
|
11
8
|
|
|
12
9
|
const init = async (options) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
assert(options?.apiKey, 'API key is required.');
|
|
11
|
+
const provider = ensureString(options?.provider, { case: 'UP' });
|
|
12
|
+
switch (provider) {
|
|
13
|
+
case OPENAI:
|
|
14
|
+
const OpenAI = await need('openai');
|
|
15
|
+
const openai = new OpenAI(options);
|
|
16
|
+
clients[provider] = openai.images;
|
|
17
|
+
break;
|
|
18
|
+
case GEMINI:
|
|
19
|
+
clients[provider] = { apiKey: options.apiKey };
|
|
20
|
+
break;
|
|
21
|
+
default:
|
|
22
|
+
throw new Error('Invalid provider.');
|
|
17
23
|
}
|
|
18
|
-
|
|
19
|
-
return client;
|
|
24
|
+
return clients;
|
|
20
25
|
};
|
|
21
26
|
|
|
27
|
+
const extractImage = async (data, options) => await convert(
|
|
28
|
+
data, { input: BASE64, suffix: 'png', ...options || {} }
|
|
29
|
+
);
|
|
30
|
+
|
|
22
31
|
const generate = async (prompt, options) => {
|
|
23
|
-
|
|
32
|
+
let provider = ensureString(options?.provider, { case: 'UP' });
|
|
33
|
+
if (!provider && clients?.[GEMINI]) { provider = GEMINI; }
|
|
34
|
+
if (!provider && clients?.[OPENAI]) { provider = OPENAI; }
|
|
35
|
+
const client = clients?.[provider];
|
|
36
|
+
assert(client, 'No available image generation provider.');
|
|
24
37
|
prompt = ensureString(prompt);
|
|
25
|
-
assert(
|
|
26
|
-
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
38
|
+
assert(prompt.length <= 4000,
|
|
39
|
+
'Prompt must be less than 4000 characters.', 400);
|
|
40
|
+
switch (provider) {
|
|
41
|
+
case OPENAI:
|
|
42
|
+
options = {
|
|
43
|
+
...options || {},
|
|
44
|
+
expected: ensureString(options?.expected || BUFFER, { case: 'LOW' }),
|
|
45
|
+
};
|
|
46
|
+
const asUrl = options.expected === 'url';
|
|
47
|
+
var resp = await client.generate({
|
|
48
|
+
prompt, model: 'dall-e-3', n: 1, quality: 'hd',
|
|
49
|
+
response_format: asUrl ? 'url' : 'b64_json',
|
|
50
|
+
size: '1792x1024', // 1024x1024, 1792x1024, or 1024x1792
|
|
51
|
+
// style: 'vivid', // or 'natural'
|
|
52
|
+
// user: '',
|
|
53
|
+
...options?.params || {},
|
|
54
|
+
});
|
|
55
|
+
if (!options?.raw && !asUrl) {
|
|
56
|
+
resp.data = await Promise.all(resp.data.map(async x => ({
|
|
57
|
+
prompt: x.revised_prompt, mimeType: MIME_PNG,
|
|
58
|
+
data: await extractImage(x.b64_json, options),
|
|
59
|
+
})));
|
|
60
|
+
}
|
|
61
|
+
return resp?.data;
|
|
62
|
+
case GEMINI:
|
|
63
|
+
options.expected === 'URL' && (options.expected = 'FILE');
|
|
64
|
+
var resp = await (await fetch(
|
|
65
|
+
'https://generativelanguage.googleapis.com/v1beta/models/'
|
|
66
|
+
+ `imagen-3.0-generate-002:predict?key=${client.apiKey}`, {
|
|
67
|
+
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
68
|
+
body: JSON.stringify({
|
|
69
|
+
instances: [{ prompt }], parameters: {
|
|
70
|
+
sampleCount: 4, aspectRatio: '16:9',
|
|
71
|
+
...options?.params || {},
|
|
72
|
+
}, // "1:1" (default), "3:4", "4:3", "9:16", and "16:9"
|
|
73
|
+
})
|
|
74
|
+
})).json();
|
|
75
|
+
if (!options?.raw) {
|
|
76
|
+
resp = await Promise.all(resp?.predictions.map(async x => ({
|
|
77
|
+
mimeType: x.mimeType,
|
|
78
|
+
data: await extractImage(x.bytesBase64Encoded, options),
|
|
79
|
+
})));
|
|
80
|
+
}
|
|
81
|
+
return resp;
|
|
82
|
+
default:
|
|
83
|
+
throw new Error('Invalid provider.');
|
|
47
84
|
}
|
|
48
|
-
return resp?.data;
|
|
49
85
|
};
|
|
50
86
|
|
|
51
87
|
export default init;
|
package/lib/manifest.mjs
CHANGED
package/lib/storage.mjs
CHANGED
|
@@ -26,11 +26,11 @@ const sanitizeFilename = (s, r) => s.replace(/[\/?<>\\:*|"]/g, r || '_').trim();
|
|
|
26
26
|
|
|
27
27
|
const [
|
|
28
28
|
NULL, BASE64, BUFFER, FILE, STREAM, TEXT, _JSON, encoding, BINARY, BLOB,
|
|
29
|
-
DATAURL, mode, dirMode, MIME_TEXT, MIME_BINARY, MIME_JSON,
|
|
29
|
+
DATAURL, mode, dirMode, MIME_TEXT, MIME_BINARY, MIME_JSON, MIME_PNG,
|
|
30
30
|
] = [
|
|
31
31
|
'NULL', 'BASE64', 'BUFFER', 'FILE', 'STREAM', 'TEXT', 'JSON', 'utf8',
|
|
32
32
|
'binary', 'BLOB', 'DATAURL', '0644', '0755', 'text/plain',
|
|
33
|
-
'application/octet-stream', 'application/json',
|
|
33
|
+
'application/octet-stream', 'application/json', 'image/png',
|
|
34
34
|
];
|
|
35
35
|
|
|
36
36
|
const [encodeBase64, encodeBinary, encodeNull]
|
|
@@ -469,13 +469,14 @@ const deleteOnCloud = async (path, options) => {
|
|
|
469
469
|
|
|
470
470
|
export {
|
|
471
471
|
_NEED,
|
|
472
|
-
BUFFER,
|
|
473
472
|
BASE64,
|
|
473
|
+
BUFFER,
|
|
474
474
|
DATAURL,
|
|
475
475
|
FILE,
|
|
476
476
|
MIME_BINARY,
|
|
477
|
-
MIME_TEXT,
|
|
478
477
|
MIME_JSON,
|
|
478
|
+
MIME_PNG,
|
|
479
|
+
MIME_TEXT,
|
|
479
480
|
STREAM,
|
|
480
481
|
analyzeFile,
|
|
481
482
|
assertPath,
|