utilitas 1999.1.59 → 1999.1.60

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/gen.mjs CHANGED
@@ -1,9 +1,10 @@
1
1
  import {
2
- ensureString, log as _log, need, throwError, tryUntil,
2
+ ensureArray, ensureString, log as _log, need, throwError, tryUntil,
3
3
  } from './utilitas.mjs';
4
4
 
5
- import { convert, MIME_PNG } from './storage.mjs';
6
5
  import { assertCommand, exec } from './shell.mjs';
6
+ import { convert, MIME_PNG } from './storage.mjs';
7
+ import { createReadStream } from 'fs';
7
8
 
8
9
  const _NEED = ['OpenAI'];
9
10
  const log = (cnt, opt) => _log(cnt, import.meta.url, { time: 1, ...opt || {} });
@@ -25,7 +26,10 @@ const init = async (options) => {
25
26
  case OPENAI:
26
27
  const OpenAI = await need('openai');
27
28
  const openai = new OpenAI(options);
28
- clients[provider] = openai.images;
29
+ clients[provider] = {
30
+ image: openai.images,
31
+ toFile: OpenAI.toFile,
32
+ };
29
33
  break;
30
34
  case GEMINI:
31
35
  clients[provider] = {
@@ -48,6 +52,19 @@ const extractVideo = async (data, options) => await convert(
48
52
  data, { input: BASE64, suffix: 'mp4', ...options || {} }
49
53
  );
50
54
 
55
+ const prepareImage = async (files, repack, options) => {
56
+ if (!files) { return }
57
+ const multiple = Array.isArray(files);
58
+ files = ensureArray(files);
59
+ const resp = await Promise.all(files.map(async x => await repack(
60
+ createReadStream(await convert(
61
+ x, { expected: 'FILE', ...options || {} }
62
+ )), null, { type: MIME_PNG } // don't need to be right MIME type
63
+ )));
64
+ return multiple ? resp : resp[0];
65
+ };
66
+
67
+
51
68
  const image = async (prompt, options) => {
52
69
  let provider = ensureString(options?.provider, { case: 'UP' });
53
70
  if (!provider && clients?.[GEMINI]?.apiKey) { provider = GEMINI; }
@@ -64,19 +81,29 @@ const image = async (prompt, options) => {
64
81
  };
65
82
  switch (provider) {
66
83
  case OPENAI:
84
+ let [func, extraOptions] = ['generate', {}];
85
+ if (options?.reference || options?.mask) {
86
+ func = 'edit';
87
+ extraOptions = {
88
+ image: await prepareImage(options?.reference, client.toFile, options),
89
+ mask: await prepareImage(options?.mask, client.toFile, options),
90
+ };
91
+ }
67
92
  try { // https://platform.openai.com/docs/guides/image-generation?image-generation-model=gpt-image-1
68
- var resp = await client.generate({
93
+ var resp = await client.image[func]({
69
94
  prompt, model: OPENAI_MODEL, n, quality: 'high',
70
95
  size: '1536x1024', moderation: 'low',
71
96
  // 1024x1024 (square), 1536x1024 (landscape), 1024x1536 (portrait), auto (default)
72
97
  // background: 'transparent',
73
- ...options?.params || {},
98
+ ...extraOptions, ...options?.params || {},
74
99
  });
75
100
  } catch (err) { throwError(err?.message || ERROR_GENERATING); }
76
101
  if (!options?.raw) {
77
102
  resp.data = await Promise.all(resp.data.map(async x => ({
78
103
  caption: `🎨 by ${OPENAI_MODEL}`,
79
- data: await extractImage(x.b64_json, options),
104
+ data: await extractImage(x.b64_json, {
105
+ ...options || {}, input: BASE64,
106
+ }),
80
107
  mimeType: MIME_PNG,
81
108
  })));
82
109
  }
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.59",
4
+ "version": "1999.1.60",
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.59",
4
+ "version": "1999.1.60",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",