utilitas 1999.1.32 → 1999.1.34
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/alan.mjs +12 -4
- package/lib/image.mjs +73 -38
- package/lib/manifest.mjs +1 -1
- package/lib/storage.mjs +5 -4
- package/lib/web.mjs +9 -3
- package/package.json +1 -1
package/lib/alan.mjs
CHANGED
|
@@ -747,12 +747,20 @@ const packResp = async (resp, options) => {
|
|
|
747
747
|
).join('\n');
|
|
748
748
|
}
|
|
749
749
|
txt = txt.split('\n');
|
|
750
|
-
const
|
|
750
|
+
const [reJinaStr, reJinaEnd]
|
|
751
|
+
= [`^\s*(${THINK_STR})`, `(${THINK_END})\s*$`].map(x => new RegExp(x));
|
|
751
752
|
const fixJina = [];
|
|
752
753
|
for (let l of txt) {
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
754
|
+
let catched = false;
|
|
755
|
+
if (reJinaStr.test(l)) {
|
|
756
|
+
fixJina.push(THINK_STR);
|
|
757
|
+
l = l.replace(reJinaStr, '');
|
|
758
|
+
}
|
|
759
|
+
if (reJinaEnd.test(l)) {
|
|
760
|
+
l = l.replace(reJinaEnd, '');
|
|
761
|
+
catched = true;
|
|
762
|
+
}
|
|
763
|
+
fixJina.push(l, ...catched ? [THINK_END, ''] : []);
|
|
756
764
|
}
|
|
757
765
|
txt = fixJina;
|
|
758
766
|
for (let i in txt) {
|
package/lib/image.mjs
CHANGED
|
@@ -1,51 +1,86 @@
|
|
|
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
|
+
var resp = await (await fetch(
|
|
64
|
+
'https://generativelanguage.googleapis.com/v1beta/models/'
|
|
65
|
+
+ `imagen-3.0-generate-002:predict?key=${client.apiKey}`, {
|
|
66
|
+
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
67
|
+
body: JSON.stringify({
|
|
68
|
+
instances: [{ prompt }], parameters: {
|
|
69
|
+
sampleCount: 4, aspectRatio: '16:9',
|
|
70
|
+
...options?.params || {},
|
|
71
|
+
}, // "1:1" (default), "3:4", "4:3", "9:16", and "16:9"
|
|
72
|
+
})
|
|
73
|
+
})).json();
|
|
74
|
+
if (!options?.raw) {
|
|
75
|
+
resp = await Promise.all(resp?.predictions.map(async x => ({
|
|
76
|
+
mimeType: x.mimeType,
|
|
77
|
+
data: await extractImage(x.bytesBase64Encoded, options),
|
|
78
|
+
})));
|
|
79
|
+
}
|
|
80
|
+
return resp;
|
|
81
|
+
default:
|
|
82
|
+
throw new Error('Invalid provider.');
|
|
47
83
|
}
|
|
48
|
-
return resp?.data;
|
|
49
84
|
};
|
|
50
85
|
|
|
51
86
|
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,
|
package/lib/web.mjs
CHANGED
|
@@ -222,7 +222,7 @@ const search = async (query, options = {}) => {
|
|
|
222
222
|
headers: {
|
|
223
223
|
'Authorization': `Bearer ${key}`,
|
|
224
224
|
'Accept': options?.aiFriendly ? MIME_TEXT : MIME_JSON,
|
|
225
|
-
'X-Respond-With': 'no-content',
|
|
225
|
+
...options?.fetch ? {} : { 'X-Respond-With': 'no-content' },
|
|
226
226
|
...options?.aiFriendly ? {} : { 'X-With-Favicons': true },
|
|
227
227
|
}
|
|
228
228
|
}, ...options || {},
|
|
@@ -329,8 +329,14 @@ const distill = async (url, options = {}) => {
|
|
|
329
329
|
const key = options.apiKey || jinaApiKey;
|
|
330
330
|
if (key) {
|
|
331
331
|
const resp = await get(`https://r.jina.ai/${encodeURIComponent(url)}`, {
|
|
332
|
-
fetch: {
|
|
333
|
-
|
|
332
|
+
fetch: {
|
|
333
|
+
headers: {
|
|
334
|
+
'Authorization': `Bearer ${key}`,
|
|
335
|
+
// 'X-Engine': 'cf-browser-rendering',
|
|
336
|
+
// 'X-Respond-With': 'readerlm-v2',
|
|
337
|
+
'X-Base': 'final',
|
|
338
|
+
}
|
|
339
|
+
}, ...options || {},
|
|
334
340
|
});
|
|
335
341
|
return { content: null, summary: resp?.content };
|
|
336
342
|
}
|