@makemidas/cli 0.1.1 → 0.3.0
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 +162 -13
- package/dist/api.js +188 -3
- package/dist/args.js +324 -1
- package/dist/cli.js +304 -25
- package/dist/commands.js +520 -23
- package/dist/config.js +1 -1
- package/dist/errors.js +28 -1
- package/dist/films.js +357 -0
- package/package.json +1 -1
package/dist/commands.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs';
|
|
2
2
|
import { mkdir, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { join, resolve } from 'node:path';
|
|
4
|
+
import { createInterface } from 'node:readline/promises';
|
|
4
5
|
import { envCredential, login } from "./auth.js";
|
|
5
6
|
import { deleteProfile, loadProfile, saveProfile } from "./config.js";
|
|
6
7
|
import { CliError, EXIT } from "./errors.js";
|
|
@@ -10,7 +11,36 @@ const EXTENSIONS = {
|
|
|
10
11
|
'image/png': 'png',
|
|
11
12
|
'image/webp': 'webp',
|
|
12
13
|
'video/mp4': 'mp4',
|
|
14
|
+
'video/quicktime': 'mov',
|
|
15
|
+
'video/webm': 'webm',
|
|
16
|
+
'audio/mpeg': 'mp3',
|
|
17
|
+
'audio/mp3': 'mp3',
|
|
18
|
+
'audio/wav': 'wav',
|
|
19
|
+
'audio/x-wav': 'wav',
|
|
20
|
+
'audio/wave': 'wav',
|
|
21
|
+
'audio/mp4': 'm4a',
|
|
22
|
+
'audio/x-m4a': 'm4a',
|
|
23
|
+
'audio/aac': 'aac',
|
|
24
|
+
'audio/flac': 'flac',
|
|
25
|
+
'audio/ogg': 'ogg',
|
|
26
|
+
'audio/opus': 'opus',
|
|
27
|
+
'audio/webm': 'webm',
|
|
28
|
+
'application/json': 'json',
|
|
29
|
+
'text/plain': 'txt',
|
|
13
30
|
};
|
|
31
|
+
/** Where a generation's file goes: `<dir>/makemidas-<id>-<n>.<ext>`. */
|
|
32
|
+
function outputPath(dir, generation, n, ext) {
|
|
33
|
+
return join(dir, `makemidas-${generation.id.slice(0, 8)}-${n}.${ext}`);
|
|
34
|
+
}
|
|
35
|
+
export async function writeOutput(file, bytes, force) {
|
|
36
|
+
if (existsSync(file) && !force) {
|
|
37
|
+
warn(`${file} exists; skipped (use --force to overwrite).`);
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
await writeFile(file, bytes);
|
|
41
|
+
log(`Saved ${file} (${formatBytes(bytes.byteLength)})`);
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
14
44
|
// ---------------------------------------------------------------------------
|
|
15
45
|
// auth
|
|
16
46
|
// ---------------------------------------------------------------------------
|
|
@@ -64,15 +94,10 @@ async function saveOutputs(api, generation, save) {
|
|
|
64
94
|
if (output.status !== 'done' || !output.url)
|
|
65
95
|
continue;
|
|
66
96
|
const { bytes, type } = await api.download(output.url);
|
|
67
|
-
const ext = EXTENSIONS[type.split(';')[0]] ?? 'bin';
|
|
68
|
-
const file =
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
await writeFile(file, bytes);
|
|
74
|
-
log(`Saved ${file} (${formatBytes(bytes.byteLength)})`);
|
|
75
|
-
files.push(file);
|
|
97
|
+
const ext = EXTENSIONS[type.split(';')[0].trim()] ?? 'bin';
|
|
98
|
+
const file = outputPath(dir, generation, index + 1, ext);
|
|
99
|
+
if (await writeOutput(file, bytes, save.force))
|
|
100
|
+
files.push(file);
|
|
76
101
|
}
|
|
77
102
|
return files;
|
|
78
103
|
}
|
|
@@ -86,7 +111,7 @@ async function waitFor(api, generation, timeoutMin) {
|
|
|
86
111
|
}
|
|
87
112
|
return current;
|
|
88
113
|
}
|
|
89
|
-
function report(g, generation, files) {
|
|
114
|
+
function report(g, generation, files, links = generation.outputs.flatMap((o) => (o.url ? [o.url] : []))) {
|
|
90
115
|
if (g.json) {
|
|
91
116
|
printJson({ ...generation, files });
|
|
92
117
|
}
|
|
@@ -94,12 +119,10 @@ function report(g, generation, files) {
|
|
|
94
119
|
if (files.length)
|
|
95
120
|
for (const file of files)
|
|
96
121
|
print(file);
|
|
97
|
-
else
|
|
98
|
-
for (const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
log(`${generation.kind} ${generation.id}: ${generation.status}, ${generation.credits} credits${generation.watermarked ? ' (watermarked: free plan)' : ''}.`);
|
|
122
|
+
else
|
|
123
|
+
for (const link of links)
|
|
124
|
+
print(link);
|
|
125
|
+
log(`${generation.kind} ${generation.id}: ${generation.status}, ${generation.credits} credits${generation.watermarked ? ' (watermarked: no plan or credit pack)' : ''}.`);
|
|
103
126
|
}
|
|
104
127
|
if (generation.status === 'failed') {
|
|
105
128
|
throw new CliError(generation.error ?? 'The model could not make this; credits were refunded.', EXIT.FAILED, 'generation_failed');
|
|
@@ -108,20 +131,131 @@ function report(g, generation, files) {
|
|
|
108
131
|
throw new CliError(`Still rendering. Run \`makemidas get ${generation.id} --wait\` later.`, EXIT.NOT_READY, 'not_ready');
|
|
109
132
|
}
|
|
110
133
|
}
|
|
111
|
-
|
|
134
|
+
/** Upload each `--ref` and return the reference ids. */
|
|
135
|
+
async function uploadRefs(api, refs) {
|
|
112
136
|
const referenceIds = [];
|
|
113
137
|
for (const ref of refs) {
|
|
114
138
|
log(`Uploading ${ref}…`);
|
|
115
139
|
referenceIds.push(await api.upload(ref));
|
|
116
140
|
}
|
|
141
|
+
return referenceIds;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Price the request first and refuse when it costs more than `maxCredits`;
|
|
145
|
+
* otherwise send the estimated request back with its price as
|
|
146
|
+
* `quotedCredits`, so the server charges exactly that or nothing.
|
|
147
|
+
*/
|
|
148
|
+
async function quotedRequest(api, input, maxCredits) {
|
|
149
|
+
const quote = await api.estimate(input);
|
|
150
|
+
if (quote.credits > maxCredits) {
|
|
151
|
+
throw new CliError(`This costs ${quote.credits} credits, over --max-credits ${maxCredits}. Nothing was charged.`, EXIT.CREDITS, 'over_budget');
|
|
152
|
+
}
|
|
153
|
+
if (!quote.affordable) {
|
|
154
|
+
throw new CliError(`This costs ${quote.credits} credits and your balance is ${quote.balance}. Nothing was charged.`, EXIT.CREDITS, 'insufficient_credits', quote.suggestedModel ? { suggestedModel: quote.suggestedModel } : {});
|
|
155
|
+
}
|
|
156
|
+
log(`Quoted ${quote.credits} credits.`);
|
|
157
|
+
return { ...quote.request, quotedCredits: quote.credits };
|
|
158
|
+
}
|
|
159
|
+
/** Upload a file or link to audio- or video-uploads; returns its fileId. */
|
|
160
|
+
async function uploadMedia(api, kind, source) {
|
|
161
|
+
// An id from an earlier upload (or estimate) is used as it is.
|
|
162
|
+
if (UUID.test(source) && !existsSync(source))
|
|
163
|
+
return source;
|
|
164
|
+
log(`Uploading ${source}…`);
|
|
165
|
+
const upload = await api.mediaUpload(kind, source);
|
|
166
|
+
log(`Uploaded ${upload.fileName ?? source}: ${formatSeconds(upload.durationSeconds)}.`);
|
|
167
|
+
return upload.fileId;
|
|
168
|
+
}
|
|
169
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
170
|
+
function formatSeconds(seconds) {
|
|
171
|
+
if (seconds < 60)
|
|
172
|
+
return `${Math.round(seconds * 10) / 10} s`;
|
|
173
|
+
const m = Math.floor(seconds / 60);
|
|
174
|
+
const s = Math.round(seconds % 60);
|
|
175
|
+
return `${m} min ${String(s).padStart(2, '0')} s`;
|
|
176
|
+
}
|
|
177
|
+
/** Upload what the request needs and fill in referenceIds or fileId. */
|
|
178
|
+
async function prepare(api, input, refs, media) {
|
|
179
|
+
const referenceIds = await uploadRefs(api, refs);
|
|
180
|
+
const fileId = media ? await uploadMedia(api, media.kind, media.source) : undefined;
|
|
181
|
+
// `--brand default` names the default brand; the API takes its id.
|
|
182
|
+
const brandId = input.brandId === 'default' ? (await api.brand('default')).id : input.brandId;
|
|
183
|
+
return {
|
|
184
|
+
...input,
|
|
185
|
+
...(brandId ? { brandId } : {}),
|
|
186
|
+
// Images and videos always send the list; other kinds only when given.
|
|
187
|
+
...(input.kind === 'image' || input.kind === 'video' || referenceIds.length
|
|
188
|
+
? { referenceIds }
|
|
189
|
+
: {}),
|
|
190
|
+
...(fileId ? { fileId } : {}),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
/** Upload, price when asked, start the generation and wait for it. */
|
|
194
|
+
async function start(api, input, refs, wait, maxCredits, media) {
|
|
195
|
+
const prepared = await prepare(api, input, refs, media);
|
|
196
|
+
const request = maxCredits === undefined
|
|
197
|
+
? prepared
|
|
198
|
+
: await quotedRequest(api, prepared, maxCredits);
|
|
117
199
|
log(`Generating ${input.kind}…`);
|
|
118
|
-
let generation = await api.generate(
|
|
200
|
+
let generation = await api.generate(request);
|
|
119
201
|
if (generation.status === 'running' && wait.wait) {
|
|
120
202
|
generation = await waitFor(api, generation, wait.timeoutMin);
|
|
121
203
|
}
|
|
204
|
+
return generation;
|
|
205
|
+
}
|
|
206
|
+
export async function generate(api, g, input, refs, save, wait, maxCredits, media) {
|
|
207
|
+
const generation = await start(api, input, refs, wait, maxCredits, media);
|
|
122
208
|
const files = await saveOutputs(api, generation, save);
|
|
123
209
|
report(g, generation, files);
|
|
124
210
|
}
|
|
211
|
+
// ---------------------------------------------------------------------------
|
|
212
|
+
// transcribe
|
|
213
|
+
// ---------------------------------------------------------------------------
|
|
214
|
+
/**
|
|
215
|
+
* Upload the audio (or send its link), transcribe it, and save the
|
|
216
|
+
* transcript in `format`. `save.out` of `-` prints the transcript itself.
|
|
217
|
+
*/
|
|
218
|
+
export async function transcribe(api, g, input, source, format, save, wait, maxCredits) {
|
|
219
|
+
if (save.out === '-' && g.json) {
|
|
220
|
+
throw new CliError('`--out -` prints the transcript itself; leave out --json.', EXIT.USAGE, 'usage');
|
|
221
|
+
}
|
|
222
|
+
const generation = await start(api, input, [], wait, maxCredits, {
|
|
223
|
+
kind: 'audio',
|
|
224
|
+
source,
|
|
225
|
+
});
|
|
226
|
+
const files = [];
|
|
227
|
+
const links = [];
|
|
228
|
+
for (const [index, output] of generation.outputs.entries()) {
|
|
229
|
+
if (output.status !== 'done')
|
|
230
|
+
continue;
|
|
231
|
+
const link = output.downloads?.[format];
|
|
232
|
+
if (!link) {
|
|
233
|
+
if (output.url) {
|
|
234
|
+
warn(`No ${format} download for this transcript; printing its JSON link.`);
|
|
235
|
+
links.push(output.url);
|
|
236
|
+
}
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
links.push(link);
|
|
240
|
+
if (!save.out)
|
|
241
|
+
continue;
|
|
242
|
+
const { bytes } = await api.download(link);
|
|
243
|
+
if (save.out === '-') {
|
|
244
|
+
process.stdout.write(bytes);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
const dir = resolve(save.out);
|
|
248
|
+
await mkdir(dir, { recursive: true });
|
|
249
|
+
const file = outputPath(dir, generation, index + 1, format);
|
|
250
|
+
if (await writeOutput(file, bytes, save.force))
|
|
251
|
+
files.push(file);
|
|
252
|
+
}
|
|
253
|
+
if (save.out === '-') {
|
|
254
|
+
report({ json: false }, generation, [], []);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
report(g, generation, files, links);
|
|
258
|
+
}
|
|
125
259
|
export async function get(api, g, id, save, wait) {
|
|
126
260
|
if (!id) {
|
|
127
261
|
throw new CliError('Give a generation id: `makemidas get <id>`.', EXIT.USAGE, 'usage');
|
|
@@ -134,6 +268,58 @@ export async function get(api, g, id, save, wait) {
|
|
|
134
268
|
report(g, generation, files);
|
|
135
269
|
}
|
|
136
270
|
// ---------------------------------------------------------------------------
|
|
271
|
+
// estimate
|
|
272
|
+
// ---------------------------------------------------------------------------
|
|
273
|
+
/** The settings a request resolved to, e.g. `16:9, 720p, 5s, audio`. */
|
|
274
|
+
function settings(request) {
|
|
275
|
+
const parts = [];
|
|
276
|
+
if (request.task)
|
|
277
|
+
parts.push(request.task);
|
|
278
|
+
if (request.aspectRatio)
|
|
279
|
+
parts.push(request.aspectRatio);
|
|
280
|
+
if (request.resolution)
|
|
281
|
+
parts.push(request.resolution);
|
|
282
|
+
if (request.duration)
|
|
283
|
+
parts.push(`${request.duration}s`);
|
|
284
|
+
if (request.durationSeconds)
|
|
285
|
+
parts.push(`${request.durationSeconds}s`);
|
|
286
|
+
if (request.audio)
|
|
287
|
+
parts.push('audio');
|
|
288
|
+
if (request.frameMode && request.frameMode !== 'none') {
|
|
289
|
+
parts.push(`frame mode ${request.frameMode}`);
|
|
290
|
+
}
|
|
291
|
+
if (request.upscaleFactor)
|
|
292
|
+
parts.push(`${request.upscaleFactor}x`);
|
|
293
|
+
if (request.voice)
|
|
294
|
+
parts.push(`voice ${request.voice}`);
|
|
295
|
+
if (request.instrumental)
|
|
296
|
+
parts.push('instrumental');
|
|
297
|
+
if (request.loop)
|
|
298
|
+
parts.push('loop');
|
|
299
|
+
if (request.kind === 'image' || request.kind === 'video') {
|
|
300
|
+
parts.push(`${request.count} output${request.count === 1 ? '' : 's'}`);
|
|
301
|
+
}
|
|
302
|
+
return parts.join(', ');
|
|
303
|
+
}
|
|
304
|
+
export async function estimate(api, g, input, refs, command = input.kind, media) {
|
|
305
|
+
const result = await api.estimate(await prepare(api, input, refs, media));
|
|
306
|
+
if (g.json) {
|
|
307
|
+
printJson(result);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const off = result.discount > 0
|
|
311
|
+
? ` (list ${result.listCredits}, ${Math.round(result.discount * 100)}% off)`
|
|
312
|
+
: '';
|
|
313
|
+
print(`${result.credits} credits${off}`);
|
|
314
|
+
print(`Model: ${result.model}`);
|
|
315
|
+
print(`Settings: ${settings(result.request)}`);
|
|
316
|
+
print(`Balance: ${result.balance} credits, ${result.affordable ? 'affordable' : 'not enough'}`);
|
|
317
|
+
if (!result.affordable && result.suggestedModel) {
|
|
318
|
+
print(`Suggested model: ${result.suggestedModel} (fits your balance)`);
|
|
319
|
+
}
|
|
320
|
+
log(`Nothing was charged. Generate at this price with \`makemidas ${command}\` and the same options plus --max-credits ${result.credits}.`);
|
|
321
|
+
}
|
|
322
|
+
// ---------------------------------------------------------------------------
|
|
137
323
|
// list / credits / models
|
|
138
324
|
// ---------------------------------------------------------------------------
|
|
139
325
|
export async function list(api, g, query) {
|
|
@@ -158,7 +344,7 @@ export async function credits(api, g) {
|
|
|
158
344
|
}
|
|
159
345
|
print(`${result.balance} credits (plan: ${result.plan ?? 'free'}).`);
|
|
160
346
|
if (result.watermarked) {
|
|
161
|
-
print('
|
|
347
|
+
print('No plan or credit pack yet: credits come from a plan or a credit pack (`makemidas credits buy`).');
|
|
162
348
|
}
|
|
163
349
|
else {
|
|
164
350
|
print(`Up to ${result.concurrency} at once, ${result.maxOutputs} outputs each.`);
|
|
@@ -174,9 +360,320 @@ export async function models(api, g, kind) {
|
|
|
174
360
|
return;
|
|
175
361
|
}
|
|
176
362
|
for (const model of result) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
363
|
+
print(`${headline(model)}: ${prices(model)}`);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function headline(model) {
|
|
367
|
+
const task = 'task' in model ? ` ${model.task}` : '';
|
|
368
|
+
return `${model.id} ${model.kind}${task}${model.tier === 'free' ? ' (free)' : ''} ${model.label}`;
|
|
369
|
+
}
|
|
370
|
+
function prices(model) {
|
|
371
|
+
switch (model.kind) {
|
|
372
|
+
case 'image':
|
|
373
|
+
return model.credits.map((c) => `${c.resolution} ${c.credits}`).join(', ');
|
|
374
|
+
case 'video':
|
|
375
|
+
return `${model.credits.map((c) => `${c.resolution} ${c.creditsPerSecond}/s`).join(', ')}; ${model.durations.join('/')}s`;
|
|
376
|
+
case 'audio':
|
|
377
|
+
if (model.creditsPer1kChars !== undefined) {
|
|
378
|
+
return `${model.creditsPer1kChars} per 1,000 characters`;
|
|
379
|
+
}
|
|
380
|
+
return `${model.creditsPerTrack ?? '?'} per request`;
|
|
381
|
+
case 'transcription':
|
|
382
|
+
return `${model.creditsPerHour} per hour of audio`;
|
|
383
|
+
case 'edit':
|
|
384
|
+
if (model.creditsPerSecond?.length) {
|
|
385
|
+
return model.creditsPerSecond
|
|
386
|
+
.map((c) => `${c.resolution} ${c.creditsPerSecond}/s`)
|
|
387
|
+
.join(', ');
|
|
388
|
+
}
|
|
389
|
+
return (model.credits ?? [])
|
|
390
|
+
.map((c) => `${c.resolution} ${c.credits}`)
|
|
391
|
+
.join(', ');
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
export async function modelDetails(api, g, id) {
|
|
395
|
+
const result = await api.modelDetails(id);
|
|
396
|
+
if (g.json) {
|
|
397
|
+
printJson(result);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
const { model, example } = result;
|
|
401
|
+
print(headline(model));
|
|
402
|
+
if (model.family)
|
|
403
|
+
print(` Family: ${model.family}`);
|
|
404
|
+
if (model.aspectRatios?.length) {
|
|
405
|
+
print(` Aspect ratios: ${model.aspectRatios.join(', ')}`);
|
|
406
|
+
}
|
|
407
|
+
if (model.resolutions?.length) {
|
|
408
|
+
const fallback = model.defaultResolution
|
|
409
|
+
? ` (default ${model.defaultResolution})`
|
|
410
|
+
: '';
|
|
411
|
+
print(` Resolutions: ${model.resolutions.join(', ')}${fallback}`);
|
|
412
|
+
}
|
|
413
|
+
switch (model.kind) {
|
|
414
|
+
case 'image': {
|
|
415
|
+
const extra = model.creditsPerReference
|
|
416
|
+
? ` (+${model.creditsPerReference} credits each)`
|
|
417
|
+
: '';
|
|
418
|
+
print(` References: up to ${model.maxReferences}${extra}`);
|
|
419
|
+
print(` Credits: ${prices(model)}`);
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
case 'video': {
|
|
423
|
+
const fallback = model.defaultDuration
|
|
424
|
+
? ` (default ${model.defaultDuration}s)`
|
|
425
|
+
: '';
|
|
426
|
+
print(` Durations: ${model.durations.join('/')}s${fallback}`);
|
|
427
|
+
const modes = model.frameModes.map((mode) => {
|
|
428
|
+
const limit = model.frameLimits?.[mode];
|
|
429
|
+
if (!limit || mode === 'none')
|
|
430
|
+
return mode;
|
|
431
|
+
const images = limit.min === limit.max ? `${limit.min}` : `${limit.min}-${limit.max}`;
|
|
432
|
+
return `${mode} (${images} image${limit.max === 1 ? '' : 's'})`;
|
|
433
|
+
});
|
|
434
|
+
print(` Frame modes: ${modes.join(', ')}`);
|
|
435
|
+
print(` Audio: ${model.audio ? 'yes' : 'no'}`);
|
|
436
|
+
const rates = model.credits.map((c) => `${c.resolution} ${c.creditsPerSecond}/s${c.creditsPerSecondWithAudio ? ` (${c.creditsPerSecondWithAudio}/s with audio)` : ''}`);
|
|
437
|
+
const extra = model.creditsPerFrame
|
|
438
|
+
? `; +${model.creditsPerFrame} per frame or reference image`
|
|
439
|
+
: '';
|
|
440
|
+
print(` Credits: ${rates.join(', ')}${extra}`);
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
case 'audio': {
|
|
444
|
+
if (model.task !== 'speech') {
|
|
445
|
+
const fallback = model.defaultDuration
|
|
446
|
+
? ` (default ${model.defaultDuration}s)`
|
|
447
|
+
: '';
|
|
448
|
+
print(` Durations: ${model.durations.length ? `${model.durations.join('/')}s${fallback}` : 'set by the model'}`);
|
|
449
|
+
}
|
|
450
|
+
const options = [
|
|
451
|
+
model.lyrics && 'lyrics',
|
|
452
|
+
model.instrumental && 'instrumental',
|
|
453
|
+
model.loop && 'loop and bpm',
|
|
454
|
+
model.maxDeliveryChars && `delivery (up to ${model.maxDeliveryChars} characters)`,
|
|
455
|
+
model.speedRange &&
|
|
456
|
+
`speed ${model.speedRange.min}-${model.speedRange.max} and stability`,
|
|
457
|
+
].filter(Boolean);
|
|
458
|
+
if (options.length)
|
|
459
|
+
print(` Options: ${options.join(', ')}`);
|
|
460
|
+
if (model.maxChars)
|
|
461
|
+
print(` Text: up to ${model.maxChars} characters`);
|
|
462
|
+
if (model.voices?.length) {
|
|
463
|
+
print(` Voices${model.defaultVoice ? ` (default ${model.defaultVoice})` : ''}:`);
|
|
464
|
+
for (const voice of model.voices) {
|
|
465
|
+
print(` ${voice.id} ${voice.name}: ${voice.description}`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (model.languages?.length) {
|
|
469
|
+
print(` Languages: ${model.languages.join(', ')}`);
|
|
470
|
+
}
|
|
471
|
+
print(` Credits: ${prices(model)}`);
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
case 'transcription':
|
|
475
|
+
print(` Languages: about ${model.languages}${model.speakers ? ', speaker labels' : ''}`);
|
|
476
|
+
print(` Files: ${model.formats}, up to ${Math.round(model.maxDurationSeconds / 60)} min and ${formatBytes(model.maxBytes)}`);
|
|
477
|
+
print(` Downloads: ${model.outputFormats.join(', ')}`);
|
|
478
|
+
print(` Credits: ${prices(model)}, at least 1`);
|
|
479
|
+
break;
|
|
480
|
+
case 'edit':
|
|
481
|
+
print(` Takes: ${model.input}`);
|
|
482
|
+
print(` Limits: ${model.limits}`);
|
|
483
|
+
if (model.upscaleFactors?.length) {
|
|
484
|
+
print(` Scales: ${model.upscaleFactors.map((f) => `${f}x`).join(', ')}`);
|
|
485
|
+
}
|
|
486
|
+
print(` Credits: ${prices(model)}`);
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
if (model.discount) {
|
|
490
|
+
print(` Discount: ${Math.round(model.discount * 100)}% off the list price`);
|
|
491
|
+
}
|
|
492
|
+
print('Example request:');
|
|
493
|
+
print(JSON.stringify(example, null, 2));
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Price each model as the comparison will run it (one image at its default
|
|
497
|
+
* size) and refuse when the total is over `maxCredits` or the balance.
|
|
498
|
+
*/
|
|
499
|
+
async function quotedComparison(api, request, maxCredits) {
|
|
500
|
+
const quotes = await Promise.all(request.models.map((model) => api.estimate({
|
|
501
|
+
kind: 'image',
|
|
502
|
+
prompt: request.prompt,
|
|
503
|
+
model,
|
|
504
|
+
count: 1,
|
|
505
|
+
...(request.aspectRatio ? { aspectRatio: request.aspectRatio } : {}),
|
|
506
|
+
referenceIds: request.referenceIds,
|
|
507
|
+
})));
|
|
508
|
+
const total = quotes.reduce((sum, quote) => sum + quote.credits, 0);
|
|
509
|
+
const balance = quotes[0]?.balance ?? 0;
|
|
510
|
+
if (total > maxCredits) {
|
|
511
|
+
throw new CliError(`This comparison costs ${total} credits (${quotes.map((q) => `${q.model} ${q.credits}`).join(', ')}), over --max-credits ${maxCredits}. Nothing was charged.`, EXIT.CREDITS, 'over_budget');
|
|
512
|
+
}
|
|
513
|
+
if (total > balance) {
|
|
514
|
+
throw new CliError(`This comparison costs ${total} credits and your balance is ${balance}. Nothing was charged.`, EXIT.CREDITS, 'insufficient_credits');
|
|
515
|
+
}
|
|
516
|
+
log(`Quoted ${total} credits.`);
|
|
517
|
+
return total;
|
|
518
|
+
}
|
|
519
|
+
/** One prompt on 2-4 image models; saves every model's image. */
|
|
520
|
+
export async function compare(api, g, request, refs, save, maxCredits) {
|
|
521
|
+
const referenceIds = await uploadRefs(api, refs);
|
|
522
|
+
const body = { ...request, referenceIds };
|
|
523
|
+
if (maxCredits !== undefined) {
|
|
524
|
+
body.quotedCredits = await quotedComparison(api, body, maxCredits);
|
|
525
|
+
}
|
|
526
|
+
log(`Comparing ${request.models.join(', ')}…`);
|
|
527
|
+
const result = await api.compare(body);
|
|
528
|
+
const files = [];
|
|
529
|
+
for (const generation of result.generations) {
|
|
530
|
+
files.push(...(await saveOutputs(api, generation, save)));
|
|
531
|
+
}
|
|
532
|
+
if (g.json) {
|
|
533
|
+
printJson({ ...result, files });
|
|
534
|
+
}
|
|
535
|
+
else {
|
|
536
|
+
for (const generation of result.generations) {
|
|
537
|
+
const mine = files.filter((file) => file.includes(`makemidas-${generation.id.slice(0, 8)}-`));
|
|
538
|
+
const links = mine.length
|
|
539
|
+
? mine
|
|
540
|
+
: generation.outputs.flatMap((o) => (o.url ? [o.url] : []));
|
|
541
|
+
for (const line of links)
|
|
542
|
+
print(`${generation.model} ${line}`);
|
|
543
|
+
log(`${generation.model} ${generation.id}: ${generation.status}, ${generation.credits} credits${generation.error ? ` (${generation.error})` : ''}.`);
|
|
544
|
+
}
|
|
545
|
+
log(`${result.credits} credits in all.`);
|
|
546
|
+
}
|
|
547
|
+
for (const item of result.failed) {
|
|
548
|
+
warn(`${item.model} did not start: ${item.error.message}`);
|
|
549
|
+
}
|
|
550
|
+
if (!result.generations.some((generation) => generation.status === 'done')) {
|
|
551
|
+
const first = result.failed[0]?.error;
|
|
552
|
+
throw new CliError(first?.message ??
|
|
553
|
+
result.generations[0]?.error ??
|
|
554
|
+
'No model could make this; credits were refunded.', EXIT.FAILED, 'generation_failed', first
|
|
555
|
+
? {
|
|
556
|
+
...(first.retryable !== undefined ? { retryable: first.retryable } : {}),
|
|
557
|
+
...(first.requiresPromptRevision !== undefined
|
|
558
|
+
? { requiresPromptRevision: first.requiresPromptRevision }
|
|
559
|
+
: {}),
|
|
560
|
+
}
|
|
561
|
+
: {});
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
// ---------------------------------------------------------------------------
|
|
565
|
+
// delete / feedback / credits link
|
|
566
|
+
// ---------------------------------------------------------------------------
|
|
567
|
+
/** Ask a yes/no question on the terminal; anything but y/yes is no. */
|
|
568
|
+
export async function askYesNo(question) {
|
|
569
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
570
|
+
try {
|
|
571
|
+
const answer = await rl.question(`${question} [y/N] `);
|
|
572
|
+
return /^y(es)?$/i.test(answer.trim());
|
|
573
|
+
}
|
|
574
|
+
finally {
|
|
575
|
+
rl.close();
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
export async function deleteGeneration(api, g, id, opts) {
|
|
579
|
+
if (!id) {
|
|
580
|
+
throw new CliError('Give a generation id: `makemidas delete <id>`.', EXIT.USAGE, 'usage');
|
|
581
|
+
}
|
|
582
|
+
if (!opts.yes) {
|
|
583
|
+
if (!opts.interactive) {
|
|
584
|
+
throw new CliError(`Deleting ${id} removes its files for good. Run again with --yes to confirm.`, EXIT.USAGE, 'confirmation_required');
|
|
585
|
+
}
|
|
586
|
+
const confirm = opts.confirm ?? askYesNo;
|
|
587
|
+
const ok = await confirm(`Delete generation ${id} and its files for good? Credits are not refunded.`);
|
|
588
|
+
if (!ok) {
|
|
589
|
+
throw new CliError('Not deleted.', EXIT.USAGE, 'cancelled');
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
const result = await api.deleteGeneration(id);
|
|
593
|
+
if (g.json)
|
|
594
|
+
printJson(result);
|
|
595
|
+
else
|
|
596
|
+
print(`Deleted ${result.id}.`);
|
|
597
|
+
}
|
|
598
|
+
export async function feedback(api, g, input) {
|
|
599
|
+
if (!input.message.trim()) {
|
|
600
|
+
throw new CliError('Give a message: `makemidas feedback "The upscale kept the noise"`.', EXIT.USAGE, 'usage');
|
|
601
|
+
}
|
|
602
|
+
const result = await api.feedback({ ...input, message: input.message.trim() });
|
|
603
|
+
if (g.json)
|
|
604
|
+
printJson(result);
|
|
605
|
+
else
|
|
606
|
+
print('Thanks, your feedback was sent.');
|
|
607
|
+
}
|
|
608
|
+
/** Where to buy credits; prints links only and opens nothing. */
|
|
609
|
+
export async function creditsLink(api, g) {
|
|
610
|
+
const result = await api.purchaseLink();
|
|
611
|
+
if (g.json) {
|
|
612
|
+
printJson(result);
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
print(result.url);
|
|
616
|
+
print(`Balance: ${result.balance} credits (plan: ${result.plan ?? 'none'}).`);
|
|
617
|
+
if (!result.purchasable) {
|
|
618
|
+
print('Checkout is switched off on this site for now.');
|
|
619
|
+
}
|
|
620
|
+
if (result.plans.length) {
|
|
621
|
+
print(`Plans (${result.plansUrl}):`);
|
|
622
|
+
for (const plan of result.plans) {
|
|
623
|
+
const marks = [plan.current && 'current', plan.popular && 'popular']
|
|
624
|
+
.filter(Boolean)
|
|
625
|
+
.join(', ');
|
|
626
|
+
print(` ${plan.name}: $${plan.priceUsd}/${plan.interval}, ${plan.monthlyCredits} credits a month, ${plan.concurrency} at once${marks ? ` (${marks})` : ''}`);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
if (result.packs.length) {
|
|
630
|
+
print(`Credit packs (${result.packsUrl}):`);
|
|
631
|
+
for (const pack of result.packs) {
|
|
632
|
+
print(` $${pack.priceUsd}: ${pack.credits} credits (${pack.subscriberCredits} with a plan)${pack.popular ? ' (popular)' : ''}`);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
print(`Manage billing: ${result.billingUrl}`);
|
|
636
|
+
}
|
|
637
|
+
// ---------------------------------------------------------------------------
|
|
638
|
+
// brands, characters, assets (read-only: what a creative brief can use)
|
|
639
|
+
// ---------------------------------------------------------------------------
|
|
640
|
+
export async function brands(api, g, id) {
|
|
641
|
+
if (id) {
|
|
642
|
+
const brand = await api.brand(id);
|
|
643
|
+
if (g.json)
|
|
644
|
+
return printJson(brand);
|
|
645
|
+
print(`${brand.name} (${brand.id})${brand.isDefault ? ' · default' : ''}`);
|
|
646
|
+
print(brand.guidelines);
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
const list = await api.brands();
|
|
650
|
+
if (g.json)
|
|
651
|
+
return printJson({ brands: list });
|
|
652
|
+
if (!list.length)
|
|
653
|
+
return print('No saved brands (add one on makemidas.com/dashboard/brands).');
|
|
654
|
+
for (const brand of list) {
|
|
655
|
+
const palette = brand.colors.map((c) => c.hex).join(' ');
|
|
656
|
+
print(`${brand.id} ${brand.isDefault ? '*' : ' '} ${brand.name}${palette ? ` ${palette}` : ''}`);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
export async function characters(api, g) {
|
|
660
|
+
const list = await api.characters();
|
|
661
|
+
if (g.json)
|
|
662
|
+
return printJson({ characters: list });
|
|
663
|
+
if (!list.length)
|
|
664
|
+
return print('No saved characters (add one on makemidas.com/dashboard/characters).');
|
|
665
|
+
for (const c of list) {
|
|
666
|
+
print(`${c.id} ${c.name} (${c.imageCount} image${c.imageCount === 1 ? '' : 's'})${c.description ? `: ${c.description}` : ''}`);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
export async function assets(api, g, query) {
|
|
670
|
+
const page = await api.assets(query);
|
|
671
|
+
if (g.json)
|
|
672
|
+
return printJson(page);
|
|
673
|
+
if (!page.assets.length)
|
|
674
|
+
return print('No matching files in the asset library.');
|
|
675
|
+
for (const a of page.assets) {
|
|
676
|
+
const size = a.width && a.height ? `${a.width}x${a.height}` : a.durationSeconds ? `${a.durationSeconds}s` : '';
|
|
677
|
+
print(`${a.id} ${a.kind.padEnd(5)} ${a.name}${size ? ` ${size}` : ''}${a.tags.length ? ` #${a.tags.join(' #')}` : ''}`);
|
|
181
678
|
}
|
|
182
679
|
}
|
package/dist/config.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
2
2
|
import { homedir } from 'node:os';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
|
-
export const VERSION = '0.
|
|
4
|
+
export const VERSION = '0.3.0';
|
|
5
5
|
export const USER_AGENT = `makemidas-cli/${VERSION}`;
|
|
6
6
|
export const DEFAULT_BASE_URL = 'https://makemidas.com';
|
|
7
7
|
export function resolveBaseUrl(flag) {
|
package/dist/errors.js
CHANGED
|
@@ -11,12 +11,39 @@ export const EXIT = {
|
|
|
11
11
|
export class CliError extends Error {
|
|
12
12
|
exitCode;
|
|
13
13
|
code;
|
|
14
|
-
|
|
14
|
+
details;
|
|
15
|
+
constructor(message, exitCode, code = 'ERROR', details = {}) {
|
|
15
16
|
super(message);
|
|
16
17
|
this.exitCode = exitCode;
|
|
17
18
|
this.code = code;
|
|
19
|
+
this.details = details;
|
|
18
20
|
}
|
|
19
21
|
}
|
|
22
|
+
/** One short line on what to do next, from the server's error details. */
|
|
23
|
+
export function errorHint(error) {
|
|
24
|
+
const { retryable, requiresPromptRevision, suggestedModel } = error.details;
|
|
25
|
+
const parts = [];
|
|
26
|
+
if (requiresPromptRevision) {
|
|
27
|
+
parts.push('Change the prompt or reference images before trying again.');
|
|
28
|
+
}
|
|
29
|
+
else if (retryable) {
|
|
30
|
+
parts.push('Trying again later may work.');
|
|
31
|
+
}
|
|
32
|
+
if (suggestedModel) {
|
|
33
|
+
parts.push(`\`--model ${suggestedModel}\` fits your balance.`);
|
|
34
|
+
}
|
|
35
|
+
return parts.length ? parts.join(' ') : undefined;
|
|
36
|
+
}
|
|
37
|
+
/** What the CLI writes to stderr for a failed command. */
|
|
38
|
+
export function formatError(error, json) {
|
|
39
|
+
if (json) {
|
|
40
|
+
return JSON.stringify({
|
|
41
|
+
error: { code: error.code, message: error.message, ...error.details },
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
const hint = errorHint(error);
|
|
45
|
+
return `Error: ${error.message}${hint ? `\nHint: ${hint}` : ''}`;
|
|
46
|
+
}
|
|
20
47
|
const EXIT_BY_CODE = {
|
|
21
48
|
unauthorized: EXIT.AUTH,
|
|
22
49
|
email_not_verified: EXIT.AUTH,
|