utilitas 1999.1.35 → 1999.1.36

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,9 +1,9 @@
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'];
5
+ const [OPENAI, GEMINI, BASE64, BUFFER, ERROR_GENERATING]
6
+ = ['OPENAI', 'GEMINI', 'BASE64', 'BUFFER', 'Error generating image.'];
7
7
  const clients = {};
8
8
 
9
9
  const init = async (options) => {
@@ -44,14 +44,16 @@ const generate = async (prompt, options) => {
44
44
  expected: ensureString(options?.expected || BUFFER, { case: 'LOW' }),
45
45
  };
46
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
- });
47
+ try {
48
+ var resp = await client.generate({
49
+ prompt, model: 'dall-e-3', n: 1, quality: 'hd',
50
+ response_format: asUrl ? 'url' : 'b64_json',
51
+ size: '1792x1024', // 1024x1024, 1792x1024, or 1024x1792
52
+ // style: 'vivid', // or 'natural'
53
+ // user: '',
54
+ ...options?.params || {},
55
+ });
56
+ } catch (err) { throwError(err?.message || ERROR_GENERATING); }
55
57
  if (!options?.raw && !asUrl) {
56
58
  resp.data = await Promise.all(resp.data.map(async x => ({
57
59
  prompt: x.revised_prompt, mimeType: MIME_PNG,
@@ -72,11 +74,14 @@ const generate = async (prompt, options) => {
72
74
  }, // "1:1" (default), "3:4", "4:3", "9:16", and "16:9"
73
75
  })
74
76
  })).json();
77
+ assert(!resp?.error, resp?.error?.message || ERROR_GENERATING);
75
78
  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
- })));
79
+ resp = await Promise.all((resp?.predictions || []).map(
80
+ async x => ({
81
+ mimeType: x.mimeType,
82
+ data: await extractImage(x.bytesBase64Encoded, options),
83
+ })
84
+ ));
80
85
  }
81
86
  return resp;
82
87
  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.36",
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.36",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",