utilitas 1999.1.55 → 1999.1.56
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 +9 -7
- package/dist/utilitas.lite.mjs +1 -1
- package/dist/utilitas.lite.mjs.map +1 -1
- package/index.mjs +3 -3
- package/lib/gen.mjs +192 -0
- package/lib/manifest.mjs +1 -1
- package/lib/utilitas.mjs +1 -1
- package/package.json +1 -1
- package/lib/image.mjs +0 -102
package/index.mjs
CHANGED
|
@@ -14,7 +14,7 @@ import * as dbio from './lib/dbio.mjs';
|
|
|
14
14
|
import * as email from './lib/email.mjs';
|
|
15
15
|
import * as encryption from './lib/encryption.mjs';
|
|
16
16
|
import * as event from './lib/event.mjs';
|
|
17
|
-
import * as
|
|
17
|
+
import * as gen from './lib/gen.mjs';
|
|
18
18
|
import * as media from './lib/media.mjs';
|
|
19
19
|
import * as memory from './lib/memory.mjs';
|
|
20
20
|
import * as network from './lib/network.mjs';
|
|
@@ -39,8 +39,8 @@ export {
|
|
|
39
39
|
fileType, math, uuid,
|
|
40
40
|
// features
|
|
41
41
|
alan, bee, bot, boxes, cache, callosum, color, dbio, email, encryption,
|
|
42
|
-
event,
|
|
43
|
-
|
|
42
|
+
event, gen, manifest, media, memory, network, sentinel, shell, sms, speech,
|
|
43
|
+
ssl, storage, tape, uoid, utilitas, vision, web
|
|
44
44
|
};
|
|
45
45
|
|
|
46
46
|
if (utilitas.inBrowser() && !globalThis.utilitas) {
|
package/lib/gen.mjs
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { ensureString, need, throwError, tryUntil } from './utilitas.mjs';
|
|
2
|
+
import { MIME_PNG, convert } from './storage.mjs';
|
|
3
|
+
|
|
4
|
+
const _NEED = ['OpenAI'];
|
|
5
|
+
|
|
6
|
+
const [
|
|
7
|
+
clients, OPENAI, GEMINI, BASE64, BUFFER, ERROR_GENERATING, IMAGEN_MODEL,
|
|
8
|
+
OPENAI_MODEL, VEO_MODEL,
|
|
9
|
+
] = [
|
|
10
|
+
{}, 'OPENAI', 'GEMINI', 'BASE64', 'BUFFER', 'Error generating image.',
|
|
11
|
+
'imagen-3.0-generate-002', 'gpt-image-1', 'veo-2.0-generate-001',
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
const init = async (options) => {
|
|
15
|
+
assert(
|
|
16
|
+
options?.apiKey || (options?.projectId && options?.accessToken),
|
|
17
|
+
'API key or project ID (with access token) are required.'
|
|
18
|
+
);
|
|
19
|
+
const provider = ensureString(options?.provider, { case: 'UP' });
|
|
20
|
+
switch (provider) {
|
|
21
|
+
case OPENAI:
|
|
22
|
+
const OpenAI = await need('openai');
|
|
23
|
+
const openai = new OpenAI(options);
|
|
24
|
+
clients[provider] = openai.images;
|
|
25
|
+
break;
|
|
26
|
+
case GEMINI:
|
|
27
|
+
clients[provider] = {
|
|
28
|
+
apiKey: options.apiKey,
|
|
29
|
+
projectId: options.projectId,
|
|
30
|
+
accessToken: options.accessToken,
|
|
31
|
+
};
|
|
32
|
+
break;
|
|
33
|
+
default:
|
|
34
|
+
throw new Error('Invalid provider.');
|
|
35
|
+
}
|
|
36
|
+
return clients;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const extractImage = async (data, options) => await convert(
|
|
40
|
+
data, { input: BASE64, suffix: 'png', ...options || {} }
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const extractVideo = async (data, options) => await convert(
|
|
44
|
+
data, { input: BASE64, suffix: 'mp4', ...options || {} }
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const generateImage = async (prompt, options) => {
|
|
48
|
+
let provider = ensureString(options?.provider, { case: 'UP' });
|
|
49
|
+
if (!provider && clients?.[GEMINI] && client.apiKey) { provider = GEMINI; }
|
|
50
|
+
if (!provider && clients?.[OPENAI]) { provider = OPENAI; }
|
|
51
|
+
const client = clients?.[provider];
|
|
52
|
+
const n = options?.n || 4;
|
|
53
|
+
assert(client, 'No available image generation provider.');
|
|
54
|
+
prompt = ensureString(prompt);
|
|
55
|
+
assert(prompt.length <= 4000,
|
|
56
|
+
'Prompt must be less than 4000 characters.', 400);
|
|
57
|
+
options = {
|
|
58
|
+
...options || {},
|
|
59
|
+
expected: ensureString(options?.expected || BUFFER, { case: 'LOW' }),
|
|
60
|
+
};
|
|
61
|
+
switch (provider) {
|
|
62
|
+
case OPENAI:
|
|
63
|
+
try { // https://platform.openai.com/docs/guides/image-generation?image-generation-model=gpt-image-1
|
|
64
|
+
var resp = await client.generate({
|
|
65
|
+
prompt, model: OPENAI_MODEL, n, quality: 'high',
|
|
66
|
+
size: '1536x1024', moderation: 'low',
|
|
67
|
+
// 1024x1024 (square), 1536x1024 (landscape), 1024x1536 (portrait), auto (default)
|
|
68
|
+
// background: 'transparent',
|
|
69
|
+
...options?.params || {},
|
|
70
|
+
});
|
|
71
|
+
} catch (err) { throwError(err?.message || ERROR_GENERATING); }
|
|
72
|
+
if (!options?.raw) {
|
|
73
|
+
resp.data = await Promise.all(resp.data.map(async x => ({
|
|
74
|
+
caption: `🎨 by ${OPENAI_MODEL}`,
|
|
75
|
+
data: await extractImage(x.b64_json, options),
|
|
76
|
+
mimeType: MIME_PNG,
|
|
77
|
+
})));
|
|
78
|
+
}
|
|
79
|
+
return resp?.data;
|
|
80
|
+
case GEMINI:
|
|
81
|
+
var resp = await (await fetch(
|
|
82
|
+
'https://generativelanguage.googleapis.com/v1beta/models/'
|
|
83
|
+
+ `${IMAGEN_MODEL}:predict?key=${client.apiKey}`, {
|
|
84
|
+
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
85
|
+
body: JSON.stringify({
|
|
86
|
+
instances: [{ prompt }], parameters: {
|
|
87
|
+
sampleCount: n, aspectRatio: '16:9',
|
|
88
|
+
...options?.params || {},
|
|
89
|
+
}, // "1:1" (default), "3:4", "4:3", "9:16", and "16:9"
|
|
90
|
+
})
|
|
91
|
+
})).json();
|
|
92
|
+
assert(!resp?.error, resp?.error?.message || ERROR_GENERATING);
|
|
93
|
+
if (!options?.raw) {
|
|
94
|
+
resp = await Promise.all((resp?.predictions || []).map(
|
|
95
|
+
async x => ({
|
|
96
|
+
caption: `🎨 by ${IMAGEN_MODEL}`,
|
|
97
|
+
data: await extractImage(x.bytesBase64Encoded, options),
|
|
98
|
+
mimeType: x.mimeType,
|
|
99
|
+
})
|
|
100
|
+
));
|
|
101
|
+
}
|
|
102
|
+
return resp;
|
|
103
|
+
default:
|
|
104
|
+
throw new Error('Invalid provider.');
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const getGeminiVideo = async (jobId) => {
|
|
109
|
+
const client = clients?.[GEMINI];
|
|
110
|
+
assert(client, 'No available video generation provider.');
|
|
111
|
+
const resp = await (await fetch(
|
|
112
|
+
'https://us-central1-aiplatform.googleapis.com/v1/projects/'
|
|
113
|
+
+ `${client.projectId}/locations/us-central1/publishers/google/models/`
|
|
114
|
+
+ `${VEO_MODEL}:fetchPredictOperation`, {
|
|
115
|
+
method: 'POST', headers: {
|
|
116
|
+
'Content-Type': 'application/json',
|
|
117
|
+
'Authorization': `Bearer ${client.accessToken}`,
|
|
118
|
+
},
|
|
119
|
+
body: JSON.stringify({
|
|
120
|
+
operationName: jobId,
|
|
121
|
+
})
|
|
122
|
+
})).json();
|
|
123
|
+
assert(
|
|
124
|
+
resp?.response?.videos?.length,
|
|
125
|
+
`Waiting for Gemini video generation: \`${jobId}\`...`
|
|
126
|
+
);
|
|
127
|
+
return resp?.response?.videos;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const generateVideo = async (prompt, options) => {
|
|
131
|
+
let provider = ensureString(options?.provider, { case: 'UP' });
|
|
132
|
+
if (!provider && clients?.[GEMINI] && client.accessToken) {
|
|
133
|
+
provider = GEMINI;
|
|
134
|
+
}
|
|
135
|
+
const client = clients?.[provider];
|
|
136
|
+
assert(client, 'No available video generation provider.');
|
|
137
|
+
prompt = ensureString(prompt);
|
|
138
|
+
assert(prompt.length <= 4000,
|
|
139
|
+
'Prompt must be less than 4000 characters.', 400);
|
|
140
|
+
options = {
|
|
141
|
+
...options || {},
|
|
142
|
+
expected: ensureString(options?.expected || BUFFER, { case: 'LOW' }),
|
|
143
|
+
};
|
|
144
|
+
switch (provider) {
|
|
145
|
+
case GEMINI:
|
|
146
|
+
var resp = await (await fetch(
|
|
147
|
+
'https://us-central1-aiplatform.googleapis.com/v1/projects/'
|
|
148
|
+
+ `${client.projectId}/locations/us-central1/publishers/google/`
|
|
149
|
+
+ `models/${VEO_MODEL}:predictLongRunning`, {
|
|
150
|
+
method: 'POST', headers: {
|
|
151
|
+
'Content-Type': 'application/json',
|
|
152
|
+
'Authorization': `Bearer ${client.accessToken}`,
|
|
153
|
+
},
|
|
154
|
+
body: JSON.stringify({
|
|
155
|
+
instances: [{ prompt }], parameters: {
|
|
156
|
+
aspectRatio: '16:9', sampleCount: 4,
|
|
157
|
+
durationSeconds: '8', fps: '24',
|
|
158
|
+
personGeneration: 'allow_adult',
|
|
159
|
+
enablePromptRewriting: true, addWatermark: false,
|
|
160
|
+
includeRaiReason: true, ...options?.params || {},
|
|
161
|
+
},
|
|
162
|
+
})
|
|
163
|
+
})).json();
|
|
164
|
+
assert(
|
|
165
|
+
!resp?.error && resp?.name,
|
|
166
|
+
resp?.error?.message || ERROR_GENERATING
|
|
167
|
+
);
|
|
168
|
+
if (options?.generateRaw) { return resp; }
|
|
169
|
+
var videos = await tryUntil(
|
|
170
|
+
async () => await getGeminiVideo(resp.name),
|
|
171
|
+
{ maxTry: 60 * 10, log: true }
|
|
172
|
+
);
|
|
173
|
+
assert(videos?.length, 'Failed to generate Gemini video.');
|
|
174
|
+
if (options?.videoRaw) { return videos; }
|
|
175
|
+
return await Promise.all(videos.map(async x => ({
|
|
176
|
+
caption: `🎥 by ${VEO_MODEL}`,
|
|
177
|
+
data: await extractVideo(x.bytesBase64Encoded, options),
|
|
178
|
+
mimeType: x.mimeType, jobId: resp.name,
|
|
179
|
+
})));
|
|
180
|
+
default:
|
|
181
|
+
throw new Error('Invalid provider.');
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export default init;
|
|
186
|
+
export {
|
|
187
|
+
_NEED,
|
|
188
|
+
generateImage,
|
|
189
|
+
generateVideo,
|
|
190
|
+
getGeminiVideo,
|
|
191
|
+
init,
|
|
192
|
+
};
|
package/lib/manifest.mjs
CHANGED
package/lib/utilitas.mjs
CHANGED
|
@@ -674,7 +674,7 @@ const tryUntil = async (fnTry, options) => {
|
|
|
674
674
|
try {
|
|
675
675
|
assert(await options.verify((err = null), (result = await fnTry())), options.error);
|
|
676
676
|
} catch (e) {
|
|
677
|
-
(err = e) && options?.log && console.
|
|
677
|
+
(err = e) && options?.log && console.log(err?.message || err);
|
|
678
678
|
await timeout(options.interval);
|
|
679
679
|
}
|
|
680
680
|
} while (++curTry < options.maxTry && err)
|
package/package.json
CHANGED
package/lib/image.mjs
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { ensureString, need, throwError } from './utilitas.mjs';
|
|
2
|
-
import { MIME_PNG, convert } from './storage.mjs';
|
|
3
|
-
|
|
4
|
-
const _NEED = ['OpenAI'];
|
|
5
|
-
|
|
6
|
-
const [
|
|
7
|
-
clients, OPENAI, GEMINI, BASE64, BUFFER, ERROR_GENERATING, IMAGEN_MODEL,
|
|
8
|
-
OPENAI_MODEL,
|
|
9
|
-
] = [
|
|
10
|
-
{}, 'OPENAI', 'GEMINI', 'BASE64', 'BUFFER', 'Error generating image.',
|
|
11
|
-
'imagen-3.0-generate-002', 'gpt-image-1',
|
|
12
|
-
];
|
|
13
|
-
|
|
14
|
-
const init = async (options) => {
|
|
15
|
-
assert(options?.apiKey, 'API key is required.');
|
|
16
|
-
const provider = ensureString(options?.provider, { case: 'UP' });
|
|
17
|
-
switch (provider) {
|
|
18
|
-
case OPENAI:
|
|
19
|
-
const OpenAI = await need('openai');
|
|
20
|
-
const openai = new OpenAI(options);
|
|
21
|
-
clients[provider] = openai.images;
|
|
22
|
-
break;
|
|
23
|
-
case GEMINI:
|
|
24
|
-
clients[provider] = { apiKey: options.apiKey };
|
|
25
|
-
break;
|
|
26
|
-
default:
|
|
27
|
-
throw new Error('Invalid provider.');
|
|
28
|
-
}
|
|
29
|
-
return clients;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const extractImage = async (data, options) => await convert(
|
|
33
|
-
data, { input: BASE64, suffix: 'png', ...options || {} }
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
const generate = async (prompt, options) => {
|
|
37
|
-
let provider = ensureString(options?.provider, { case: 'UP' });
|
|
38
|
-
if (!provider && clients?.[GEMINI]) { provider = GEMINI; }
|
|
39
|
-
if (!provider && clients?.[OPENAI]) { provider = OPENAI; }
|
|
40
|
-
const client = clients?.[provider];
|
|
41
|
-
const n = options?.n || 4;
|
|
42
|
-
assert(client, 'No available image generation provider.');
|
|
43
|
-
prompt = ensureString(prompt);
|
|
44
|
-
assert(prompt.length <= 4000,
|
|
45
|
-
'Prompt must be less than 4000 characters.', 400);
|
|
46
|
-
options = {
|
|
47
|
-
...options || {},
|
|
48
|
-
expected: ensureString(options?.expected || BUFFER, { case: 'LOW' }),
|
|
49
|
-
};
|
|
50
|
-
switch (provider) {
|
|
51
|
-
case OPENAI:
|
|
52
|
-
try { // https://platform.openai.com/docs/guides/image-generation?image-generation-model=gpt-image-1
|
|
53
|
-
var resp = await client.generate({
|
|
54
|
-
prompt, model: OPENAI_MODEL, n, quality: 'high',
|
|
55
|
-
size: '1536x1024', moderation: 'low',
|
|
56
|
-
// 1024x1024 (square), 1536x1024 (landscape), 1024x1536 (portrait), auto (default)
|
|
57
|
-
// background: 'transparent',
|
|
58
|
-
...options?.params || {},
|
|
59
|
-
});
|
|
60
|
-
} catch (err) { throwError(err?.message || ERROR_GENERATING); }
|
|
61
|
-
if (!options?.raw) {
|
|
62
|
-
resp.data = await Promise.all(resp.data.map(async x => ({
|
|
63
|
-
caption: `🎨 by ${OPENAI_MODEL}`,
|
|
64
|
-
data: await extractImage(x.b64_json, options),
|
|
65
|
-
mimeType: MIME_PNG,
|
|
66
|
-
})));
|
|
67
|
-
}
|
|
68
|
-
return resp?.data;
|
|
69
|
-
case GEMINI:
|
|
70
|
-
var resp = await (await fetch(
|
|
71
|
-
'https://generativelanguage.googleapis.com/v1beta/models/'
|
|
72
|
-
+ `${IMAGEN_MODEL}:predict?key=${client.apiKey}`, {
|
|
73
|
-
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
74
|
-
body: JSON.stringify({
|
|
75
|
-
instances: [{ prompt }], parameters: {
|
|
76
|
-
sampleCount: n, aspectRatio: '16:9',
|
|
77
|
-
...options?.params || {},
|
|
78
|
-
}, // "1:1" (default), "3:4", "4:3", "9:16", and "16:9"
|
|
79
|
-
})
|
|
80
|
-
})).json();
|
|
81
|
-
assert(!resp?.error, resp?.error?.message || ERROR_GENERATING);
|
|
82
|
-
if (!options?.raw) {
|
|
83
|
-
resp = await Promise.all((resp?.predictions || []).map(
|
|
84
|
-
async x => ({
|
|
85
|
-
caption: `🎨 by ${IMAGEN_MODEL}`,
|
|
86
|
-
data: await extractImage(x.bytesBase64Encoded, options),
|
|
87
|
-
mimeType: x.mimeType,
|
|
88
|
-
})
|
|
89
|
-
));
|
|
90
|
-
}
|
|
91
|
-
return resp;
|
|
92
|
-
default:
|
|
93
|
-
throw new Error('Invalid provider.');
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
export default init;
|
|
98
|
-
export {
|
|
99
|
-
_NEED,
|
|
100
|
-
generate,
|
|
101
|
-
init,
|
|
102
|
-
};
|