utilitas 1999.1.57 → 1999.1.58
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 -1
- package/dist/utilitas.lite.mjs +1 -1
- package/dist/utilitas.lite.mjs.map +1 -1
- package/lib/gen.mjs +14 -16
- package/lib/manifest.mjs +1 -1
- package/lib/utilitas.mjs +6 -2
- package/package.json +1 -1
package/lib/gen.mjs
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ensureString, log as _log, need, throwError, tryUntil,
|
|
3
|
+
} from './utilitas.mjs';
|
|
4
|
+
|
|
1
5
|
import { convert, MIME_PNG } from './storage.mjs';
|
|
2
|
-
import { ensureString, need, throwError, tryUntil } from './utilitas.mjs';
|
|
3
6
|
import { assertCommand, exec } from './shell.mjs';
|
|
4
7
|
|
|
5
8
|
const _NEED = ['OpenAI'];
|
|
6
|
-
|
|
9
|
+
const log = (cnt, opt) => _log(cnt, import.meta.url, { time: 1, ...opt || {} });
|
|
7
10
|
const [
|
|
8
11
|
clients, OPENAI, GEMINI, BASE64, BUFFER, ERROR_GENERATING, IMAGEN_MODEL,
|
|
9
12
|
OPENAI_MODEL, VEO_MODEL,
|
|
@@ -119,7 +122,7 @@ const getGeminiAccessToken = async (credentials) => {
|
|
|
119
122
|
return tokResp;
|
|
120
123
|
};
|
|
121
124
|
|
|
122
|
-
const getGeminiVideo = async (jobId) => {
|
|
125
|
+
const getGeminiVideo = async (jobId, accessToken) => {
|
|
123
126
|
const client = clients?.[GEMINI];
|
|
124
127
|
assert(client, 'No available video generation provider.');
|
|
125
128
|
const resp = await (await fetch(
|
|
@@ -128,16 +131,12 @@ const getGeminiVideo = async (jobId) => {
|
|
|
128
131
|
+ `${VEO_MODEL}:fetchPredictOperation`, {
|
|
129
132
|
method: 'POST', headers: {
|
|
130
133
|
'Content-Type': 'application/json',
|
|
131
|
-
'Authorization': `Bearer ${
|
|
132
|
-
},
|
|
133
|
-
body: JSON.stringify({
|
|
134
|
-
operationName: jobId,
|
|
135
|
-
})
|
|
134
|
+
'Authorization': `Bearer ${accessToken}`,
|
|
135
|
+
}, body: JSON.stringify({ operationName: jobId })
|
|
136
136
|
})).json();
|
|
137
|
-
assert(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
);
|
|
137
|
+
assert(resp?.response?.videos?.length,
|
|
138
|
+
'Waiting for Gemini video generation: '
|
|
139
|
+
+ jobId.replace(/^.*\/([^/]+)$/, '$1'));
|
|
141
140
|
return resp?.response?.videos;
|
|
142
141
|
};
|
|
143
142
|
|
|
@@ -181,10 +180,9 @@ const generateVideo = async (prompt, options) => {
|
|
|
181
180
|
resp?.error?.message || ERROR_GENERATING
|
|
182
181
|
);
|
|
183
182
|
if (options?.generateRaw) { return resp; }
|
|
184
|
-
var videos = await tryUntil(
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
);
|
|
183
|
+
var videos = await tryUntil(async () => await getGeminiVideo(
|
|
184
|
+
resp.name, accessToken
|
|
185
|
+
), { maxTry: 60 * 10, log });
|
|
188
186
|
assert(videos?.length, 'Failed to generate Gemini video.');
|
|
189
187
|
if (options?.videoRaw) { return videos; }
|
|
190
188
|
return await Promise.all(videos.map(async x => ({
|
package/lib/manifest.mjs
CHANGED
package/lib/utilitas.mjs
CHANGED
|
@@ -394,6 +394,7 @@ const log = (content, filename, options) => {
|
|
|
394
394
|
const args = ['[' + color.red(name) + color.yellow(strTime) + ']'
|
|
395
395
|
+ (isErr ? '' : ` ${content}`)];
|
|
396
396
|
if (isErr) { args.push(content); }
|
|
397
|
+
if (options?.return) { return args[0]; }
|
|
397
398
|
return console.info.apply(null, args);
|
|
398
399
|
};
|
|
399
400
|
|
|
@@ -669,12 +670,15 @@ const tryUntil = async (fnTry, options) => {
|
|
|
669
670
|
interval: 1000 * 1, maxTry: Infinity, log: false, error: 'Operation failed.',
|
|
670
671
|
verify: async (err, res) => { return !err; }, ...options || {}
|
|
671
672
|
};
|
|
672
|
-
let [curTry, result, err] = [0, null, null];
|
|
673
|
+
let [curTry, result, err, msg] = [0, null, null, null];
|
|
673
674
|
do {
|
|
674
675
|
try {
|
|
675
676
|
assert(await options.verify((err = null), (result = await fnTry())), options.error);
|
|
676
677
|
} catch (e) {
|
|
677
|
-
(err = e) &&
|
|
678
|
+
(err = e) && (msg = err?.message || err) && (
|
|
679
|
+
Function.isFunction(options?.log)
|
|
680
|
+
? await options.log(msg) : console.log(msg)
|
|
681
|
+
);
|
|
678
682
|
await timeout(options.interval);
|
|
679
683
|
}
|
|
680
684
|
} while (++curTry < options.maxTry && err)
|