utilitas 1999.1.56 → 1999.1.57

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,5 +1,6 @@
1
+ import { convert, MIME_PNG } from './storage.mjs';
1
2
  import { ensureString, need, throwError, tryUntil } from './utilitas.mjs';
2
- import { MIME_PNG, convert } from './storage.mjs';
3
+ import { assertCommand, exec } from './shell.mjs';
3
4
 
4
5
  const _NEED = ['OpenAI'];
5
6
 
@@ -13,8 +14,8 @@ const [
13
14
 
14
15
  const init = async (options) => {
15
16
  assert(
16
- options?.apiKey || (options?.projectId && options?.accessToken),
17
- 'API key or project ID (with access token) are required.'
17
+ options?.apiKey || (options?.credentials && options?.projectId),
18
+ 'API key or credentials are required.'
18
19
  );
19
20
  const provider = ensureString(options?.provider, { case: 'UP' });
20
21
  switch (provider) {
@@ -27,7 +28,7 @@ const init = async (options) => {
27
28
  clients[provider] = {
28
29
  apiKey: options.apiKey,
29
30
  projectId: options.projectId,
30
- accessToken: options.accessToken,
31
+ credentials: options.credentials,
31
32
  };
32
33
  break;
33
34
  default:
@@ -46,7 +47,7 @@ const extractVideo = async (data, options) => await convert(
46
47
 
47
48
  const generateImage = async (prompt, options) => {
48
49
  let provider = ensureString(options?.provider, { case: 'UP' });
49
- if (!provider && clients?.[GEMINI] && client.apiKey) { provider = GEMINI; }
50
+ if (!provider && clients?.[GEMINI]?.apiKey) { provider = GEMINI; }
50
51
  if (!provider && clients?.[OPENAI]) { provider = OPENAI; }
51
52
  const client = clients?.[provider];
52
53
  const n = options?.n || 4;
@@ -105,6 +106,19 @@ const generateImage = async (prompt, options) => {
105
106
  }
106
107
  };
107
108
 
109
+ const getGeminiAccessToken = async (credentials) => {
110
+ await assertCommand('gcloud');
111
+ const actResp = await exec(
112
+ `gcloud auth activate-service-account --key-file=${credentials}`,
113
+ { acceptError: true }
114
+ );
115
+ assert(actResp.includes('Activated service account credentials'),
116
+ 'Failed to activate service account credentials.', 500);
117
+ const tokResp = (await exec(`gcloud auth print-access-token`)).trim();
118
+ assert(tokResp, 'Failed to get access token.', 500);
119
+ return tokResp;
120
+ };
121
+
108
122
  const getGeminiVideo = async (jobId) => {
109
123
  const client = clients?.[GEMINI];
110
124
  assert(client, 'No available video generation provider.');
@@ -129,11 +143,12 @@ const getGeminiVideo = async (jobId) => {
129
143
 
130
144
  const generateVideo = async (prompt, options) => {
131
145
  let provider = ensureString(options?.provider, { case: 'UP' });
132
- if (!provider && clients?.[GEMINI] && client.accessToken) {
133
- provider = GEMINI;
134
- }
146
+ if (!provider
147
+ && clients?.[GEMINI]?.credentials
148
+ && clients?.[GEMINI]?.projectId) { provider = GEMINI; }
135
149
  const client = clients?.[provider];
136
150
  assert(client, 'No available video generation provider.');
151
+ const accessToken = await getGeminiAccessToken(client.credentials);
137
152
  prompt = ensureString(prompt);
138
153
  assert(prompt.length <= 4000,
139
154
  'Prompt must be less than 4000 characters.', 400);
@@ -149,7 +164,7 @@ const generateVideo = async (prompt, options) => {
149
164
  + `models/${VEO_MODEL}:predictLongRunning`, {
150
165
  method: 'POST', headers: {
151
166
  'Content-Type': 'application/json',
152
- 'Authorization': `Bearer ${client.accessToken}`,
167
+ 'Authorization': `Bearer ${accessToken}`,
153
168
  },
154
169
  body: JSON.stringify({
155
170
  instances: [{ prompt }], parameters: {
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.56",
4
+ "version": "1999.1.57",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
package/lib/shell.mjs CHANGED
@@ -11,8 +11,10 @@ const exist = (bin) => { assertCommand(bin); return which(bin); };
11
11
  const exec = async (command, options = {}) => {
12
12
  assertCommand(command);
13
13
  const { stdout, stderr } = await pmsExec(command);
14
- assert(!stderr, stderr, 500);
15
- return stdout;
14
+ assert(options?.acceptError || !stderr, stderr, 500);
15
+ return options?.acceptError
16
+ ? [stdout, stderr].map(x => x.trim()).filter(x => x).join('\n')
17
+ : stdout.trim();
16
18
  };
17
19
 
18
20
  const which = async (bin) => {
@@ -28,4 +30,4 @@ const assertExist = async (bin, er, code = 500) => {
28
30
  };
29
31
 
30
32
  export default exec;
31
- export { assertExist, exec, exist, which };
33
+ export { assertCommand, assertExist, exec, exist, which };
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.56",
4
+ "version": "1999.1.57",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",