utilitas 1999.1.35 → 1999.1.37

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/lib/image.mjs CHANGED
@@ -1,10 +1,13 @@
1
- import { ensureString, need } from './utilitas.mjs';
1
+ import { ensureString, need, throwError } from './utilitas.mjs';
2
2
  import { MIME_PNG, convert } from './storage.mjs';
3
3
 
4
4
  const _NEED = ['OpenAI'];
5
- const [OPENAI, GEMINI, BASE64, BUFFER]
6
- = ['OPENAI', 'GEMINI', 'BASE64', 'BUFFER'];
7
- const clients = {};
5
+
6
+ const [clients, OPENAI, GEMINI, BASE64, BUFFER, ERROR_GENERATING, IMAGEN_MODEL]
7
+ = [
8
+ {}, 'OPENAI', 'GEMINI', 'BASE64', 'BUFFER', 'Error generating image.',
9
+ 'imagen-3.0-generate-002',
10
+ ];
8
11
 
9
12
  const init = async (options) => {
10
13
  assert(options?.apiKey, 'API key is required.');
@@ -44,18 +47,21 @@ const generate = async (prompt, options) => {
44
47
  expected: ensureString(options?.expected || BUFFER, { case: 'LOW' }),
45
48
  };
46
49
  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
- });
50
+ try {
51
+ var resp = await client.generate({
52
+ prompt, model: 'dall-e-3', n: 1, quality: 'hd',
53
+ response_format: asUrl ? 'url' : 'b64_json',
54
+ size: '1792x1024', // 1024x1024, 1792x1024, or 1024x1792
55
+ // style: 'vivid', // or 'natural'
56
+ // user: '',
57
+ ...options?.params || {},
58
+ });
59
+ } catch (err) { throwError(err?.message || ERROR_GENERATING); }
55
60
  if (!options?.raw && !asUrl) {
56
61
  resp.data = await Promise.all(resp.data.map(async x => ({
57
- prompt: x.revised_prompt, mimeType: MIME_PNG,
62
+ caption: x.revised_prompt, tts: x.revised_prompt,
58
63
  data: await extractImage(x.b64_json, options),
64
+ mimeType: MIME_PNG,
59
65
  })));
60
66
  }
61
67
  return resp?.data;
@@ -63,7 +69,7 @@ const generate = async (prompt, options) => {
63
69
  options.expected === 'URL' && (options.expected = 'FILE');
64
70
  var resp = await (await fetch(
65
71
  'https://generativelanguage.googleapis.com/v1beta/models/'
66
- + `imagen-3.0-generate-002:predict?key=${client.apiKey}`, {
72
+ + `${IMAGEN_MODEL}:predict?key=${client.apiKey}`, {
67
73
  method: 'POST', headers: { 'Content-Type': 'application/json' },
68
74
  body: JSON.stringify({
69
75
  instances: [{ prompt }], parameters: {
@@ -72,11 +78,15 @@ const generate = async (prompt, options) => {
72
78
  }, // "1:1" (default), "3:4", "4:3", "9:16", and "16:9"
73
79
  })
74
80
  })).json();
81
+ assert(!resp?.error, resp?.error?.message || ERROR_GENERATING);
75
82
  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
- })));
83
+ resp = await Promise.all((resp?.predictions || []).map(
84
+ async x => ({
85
+ caption: `🎨 by ${IMAGEN_MODEL}`, tts: null,
86
+ data: await extractImage(x.bytesBase64Encoded, options),
87
+ mimeType: x.mimeType,
88
+ })
89
+ ));
80
90
  }
81
91
  return resp;
82
92
  default:
package/lib/manifest.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  const manifest = {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1999.1.35",
4
+ "version": "1999.1.37",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1999.1.35",
4
+ "version": "1999.1.37",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",