@mynthio/sdk 0.0.41 → 0.0.43
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 +28 -9
- package/dist/index.d.ts +37 -10
- package/dist/index.js +135 -57
- package/dist/types.d.ts +14 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -136,6 +136,22 @@ if (status.status === "completed") {
|
|
|
136
136
|
}
|
|
137
137
|
```
|
|
138
138
|
|
|
139
|
+
### Cancelling
|
|
140
|
+
|
|
141
|
+
Pass an `AbortSignal` to stop waiting, for example when the incoming request disconnects:
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
const task = await mynth.image.generate(
|
|
145
|
+
{ prompt: "A lighthouse in a storm" },
|
|
146
|
+
{ signal: AbortSignal.timeout(60_000) },
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
// Or on an async task:
|
|
150
|
+
const completedTask = await taskAsync.wait({ signal: request.signal });
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
An abort cancels pending uploads and API requests and stops polling, and the call rejects with the signal's reason. A task that was already created keeps running on Mynth. For `generateAsync()`, the signal covers the upload and the create request; pass one to `wait()` to abort the wait. When several `wait()` calls share a task, polling stops only after every one of them has aborted.
|
|
154
|
+
|
|
139
155
|
## Request Shape
|
|
140
156
|
|
|
141
157
|
`generate()` accepts a typed `ImageGenerationRequest`. The simplest request is just a prompt:
|
|
@@ -161,7 +177,6 @@ const task = await mynth.image.generate({
|
|
|
161
177
|
count: 2,
|
|
162
178
|
output: {
|
|
163
179
|
format: "webp",
|
|
164
|
-
quality: 80,
|
|
165
180
|
},
|
|
166
181
|
webhook: {
|
|
167
182
|
dashboard: false,
|
|
@@ -198,7 +213,7 @@ const task = await mynth.image.generate({
|
|
|
198
213
|
|
|
199
214
|
`access.pat.enabled` controls whether the create-task response includes a short-lived Public Access Token for browser-side polling. It defaults to `true`.
|
|
200
215
|
|
|
201
|
-
`output` is optional.
|
|
216
|
+
`output` is optional. Set `output.format` to `png`, `jpg`, or `webp`. When it is omitted, the result keeps the format the provider delivered.
|
|
202
217
|
|
|
203
218
|
## Upload Images
|
|
204
219
|
|
|
@@ -525,7 +540,7 @@ console.log(model);
|
|
|
525
540
|
// {
|
|
526
541
|
// id: "google/gemini-3.1-flash-image",
|
|
527
542
|
// label: "Nano Banana 2",
|
|
528
|
-
// capabilities: ["inputs", "4k"
|
|
543
|
+
// capabilities: ["inputs", "4k"]
|
|
529
544
|
// }
|
|
530
545
|
```
|
|
531
546
|
|
|
@@ -538,6 +553,7 @@ Current model IDs include:
|
|
|
538
553
|
- `alibaba/qwen-image-3.0-pro`
|
|
539
554
|
- `bytedance/seedream-5.0-lite`
|
|
540
555
|
- `bytedance/seedream-pro`
|
|
556
|
+
- `bytedance/seedream-v5-flash`
|
|
541
557
|
- `black-forest-labs/flux.1-dev`
|
|
542
558
|
- `black-forest-labs/flux-1-schnell`
|
|
543
559
|
- `black-forest-labs/flux.2-dev`
|
|
@@ -545,6 +561,7 @@ Current model IDs include:
|
|
|
545
561
|
- `black-forest-labs/flux.2-flex`
|
|
546
562
|
- `black-forest-labs/flux.2-max`
|
|
547
563
|
- `black-forest-labs/flux.2-klein-4b`
|
|
564
|
+
- `goofy-ai/prefect-pony-xl-lora`
|
|
548
565
|
- `google/gemini-3.1-flash-lite-image`
|
|
549
566
|
- `google/gemini-3.1-flash-image`
|
|
550
567
|
- `google/gemini-3-pro-image-preview`
|
|
@@ -561,6 +578,7 @@ Current model IDs include:
|
|
|
561
578
|
- `openai/gpt-image-2`
|
|
562
579
|
- `purplesmartai/pony-diffusion-v6-xl`
|
|
563
580
|
- `recraft/recraft-v4`
|
|
581
|
+
- `recraft/recraft-v4.1-flash`
|
|
564
582
|
- `recraft/recraft-v4-pro`
|
|
565
583
|
- `sourceful/riverflow-2.0-pro`
|
|
566
584
|
- `tongyi-mai/z-image`
|
|
@@ -689,13 +707,14 @@ console.log(model);
|
|
|
689
707
|
// }
|
|
690
708
|
```
|
|
691
709
|
|
|
692
|
-
| Model | Resolutions
|
|
693
|
-
| ------------------------------ |
|
|
694
|
-
| `bytedance/seedance-2.0-mini` | 480p, 720p
|
|
695
|
-
| `google/gemini-omni-flash-1.1` | 720p, 1080p, 4k
|
|
696
|
-
| `prunaai/p-video` | 720p, 1080p
|
|
710
|
+
| Model | Resolutions | Duration (s) | Inputs |
|
|
711
|
+
| ------------------------------ | ----------------- | ---------------- | ----------------------- |
|
|
712
|
+
| `bytedance/seedance-2.0-mini` | 480p, 720p | 4–15 (default 5) | first frame, last frame |
|
|
713
|
+
| `google/gemini-omni-flash-1.1` | 720p, 1080p, 4k | 3–10 (default 8) | first frame, last frame |
|
|
714
|
+
| `prunaai/p-video` | 720p, 1080p | 1–10 (default 5) | first frame |
|
|
715
|
+
| `xai/grok-imagine-video-1.5` | 480p, 720p, 1080p | 1–15 (default 8) | first frame |
|
|
697
716
|
|
|
698
|
-
|
|
717
|
+
Each of these supports generated audio.
|
|
699
718
|
|
|
700
719
|
`mynth.models.list()` covers these video models alongside the image catalog.
|
|
701
720
|
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ type MynthClientRequestOptions = {
|
|
|
14
14
|
headers?: Record<string, string>;
|
|
15
15
|
accessToken?: string | undefined;
|
|
16
16
|
auth?: boolean | undefined;
|
|
17
|
+
signal?: AbortSignal | undefined;
|
|
17
18
|
};
|
|
18
19
|
/**
|
|
19
20
|
* Internal HTTP client for making API requests.
|
|
@@ -26,10 +27,10 @@ declare class MynthClient {
|
|
|
26
27
|
apiKey?: string | undefined;
|
|
27
28
|
baseUrl?: string | undefined;
|
|
28
29
|
});
|
|
29
|
-
getAuthHeaders(override?:
|
|
30
|
+
getAuthHeaders(override?: Pick<MynthClientRequestOptions, "accessToken" | "auth">): Record<string, string>;
|
|
30
31
|
getUrl(path: string): string;
|
|
31
|
-
post<DataType>(path: string, data: unknown): Promise<DataType>;
|
|
32
|
-
get<DataType>(path: string, { headers, accessToken, auth }?: MynthClientRequestOptions): Promise<{
|
|
32
|
+
post<DataType>(path: string, data: unknown, { signal }?: Pick<MynthClientRequestOptions, "signal">): Promise<DataType>;
|
|
33
|
+
get<DataType>(path: string, { headers, accessToken, auth, signal }?: MynthClientRequestOptions): Promise<{
|
|
33
34
|
data: DataType;
|
|
34
35
|
status: number;
|
|
35
36
|
ok: boolean;
|
|
@@ -80,7 +81,7 @@ export declare class ImageGenerationResult<MetadataT = Record<string, unknown> |
|
|
|
80
81
|
/** Current status of the task */
|
|
81
82
|
get status(): MynthSDKTypes.TaskStatus;
|
|
82
83
|
/**
|
|
83
|
-
* The generation result
|
|
84
|
+
* The generation result: images, the format each one was delivered in, and the model.
|
|
84
85
|
* Returns `null` if the task hasn't completed yet.
|
|
85
86
|
*/
|
|
86
87
|
get result(): TypedImageResult<RatingT> | null;
|
|
@@ -230,6 +231,17 @@ export declare class TaskAsyncTaskFetchError extends Error {
|
|
|
230
231
|
export declare class TaskAsyncTaskFailedError extends Error {
|
|
231
232
|
constructor(taskId: string);
|
|
232
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Options for {@link TaskAsync.wait}.
|
|
236
|
+
*/
|
|
237
|
+
type TaskAsyncWaitOptions = {
|
|
238
|
+
/**
|
|
239
|
+
* Stops this wait when aborted, rejecting with the signal's reason. Polling
|
|
240
|
+
* stops as well once no other `wait()` call is still waiting on the task.
|
|
241
|
+
* Aborting never cancels the task itself: Mynth keeps generating it.
|
|
242
|
+
*/
|
|
243
|
+
signal?: AbortSignal | undefined;
|
|
244
|
+
};
|
|
233
245
|
/**
|
|
234
246
|
* Public access information for a task, used for client-side polling.
|
|
235
247
|
*/
|
|
@@ -250,7 +262,7 @@ export declare class TaskAsync<ResultT> {
|
|
|
250
262
|
private readonly _access;
|
|
251
263
|
private readonly resultFactory;
|
|
252
264
|
private readonly polling;
|
|
253
|
-
private
|
|
265
|
+
private poll;
|
|
254
266
|
constructor(id: string, options: {
|
|
255
267
|
client: MynthClient;
|
|
256
268
|
pat?: string | undefined;
|
|
@@ -265,15 +277,17 @@ export declare class TaskAsync<ResultT> {
|
|
|
265
277
|
toString(): string;
|
|
266
278
|
/**
|
|
267
279
|
* Polls the task until completion and returns the typed result.
|
|
268
|
-
*
|
|
280
|
+
* Concurrent calls share one polling loop and settle with the same result.
|
|
269
281
|
*
|
|
282
|
+
* @param options.signal - Stops this wait when aborted (see {@link TaskAsyncWaitOptions})
|
|
270
283
|
* @throws {TaskAsyncTimeoutError} If polling exceeds the timeout
|
|
271
284
|
* @throws {TaskAsyncUnauthorizedError} If access is denied
|
|
272
285
|
* @throws {TaskAsyncFetchError} If fetching status fails repeatedly
|
|
273
286
|
* @throws {TaskAsyncTaskFetchError} If fetching the completed task fails
|
|
274
287
|
* @throws {TaskAsyncTaskFailedError} If the task fails before completion
|
|
275
288
|
*/
|
|
276
|
-
wait(): Promise<ResultT>;
|
|
289
|
+
wait({ signal }?: TaskAsyncWaitOptions): Promise<ResultT>;
|
|
290
|
+
private startPoll;
|
|
277
291
|
private pollUntilCompleted;
|
|
278
292
|
private fetchStatus;
|
|
279
293
|
private fetchTask;
|
|
@@ -346,6 +360,17 @@ type MynthOptions = {
|
|
|
346
360
|
*/
|
|
347
361
|
destination?: string | undefined;
|
|
348
362
|
};
|
|
363
|
+
/**
|
|
364
|
+
* Per-call options for SDK methods that talk to the API.
|
|
365
|
+
*/
|
|
366
|
+
type MynthRequestOptions = {
|
|
367
|
+
/**
|
|
368
|
+
* Aborts the call: pending uploads and API requests are cancelled, and a
|
|
369
|
+
* method that waits for completion stops polling. A task that was already
|
|
370
|
+
* created keeps running on Mynth.
|
|
371
|
+
*/
|
|
372
|
+
signal?: AbortSignal | undefined;
|
|
373
|
+
};
|
|
349
374
|
type MynthModel = MynthSDKTypes.Model;
|
|
350
375
|
type MynthModelPricing = MynthSDKTypes.ModelPricing;
|
|
351
376
|
type ExtractMetadata<T extends MynthSDKTypes.ImageGenerationClientRequest> = T["metadata"];
|
|
@@ -416,6 +441,7 @@ declare class MynthImage {
|
|
|
416
441
|
* Generate images from a text prompt.
|
|
417
442
|
*
|
|
418
443
|
* @param request - Image generation request parameters
|
|
444
|
+
* @param options.signal - Aborts the upload, the create request, or the wait for completion
|
|
419
445
|
* @returns A completed ImageGenerationResult with the generation results
|
|
420
446
|
*
|
|
421
447
|
* @example
|
|
@@ -427,7 +453,7 @@ declare class MynthImage {
|
|
|
427
453
|
* console.log(result.urls);
|
|
428
454
|
* ```
|
|
429
455
|
*/
|
|
430
|
-
generate<const T extends MynthSDKTypes.ImageGenerationClientRequest>(request: T): Promise<ImageGenerationResult<ExtractMetadata<T>, ExtractRatingResponse<T>>>;
|
|
456
|
+
generate<const T extends MynthSDKTypes.ImageGenerationClientRequest>(request: T, options?: MynthRequestOptions): Promise<ImageGenerationResult<ExtractMetadata<T>, ExtractRatingResponse<T>>>;
|
|
431
457
|
/**
|
|
432
458
|
* Upload one or more images to Mynth temporary input storage.
|
|
433
459
|
*
|
|
@@ -447,6 +473,7 @@ declare class MynthImage {
|
|
|
447
473
|
* Start image generation without waiting for completion.
|
|
448
474
|
*
|
|
449
475
|
* @param request - Image generation request parameters
|
|
476
|
+
* @param options.signal - Aborts the upload or the create request. Pass a signal to `.wait()` to abort the wait.
|
|
450
477
|
* @returns A TaskAsync that can be polled for completion via `.wait()`
|
|
451
478
|
*
|
|
452
479
|
* @example
|
|
@@ -458,7 +485,7 @@ declare class MynthImage {
|
|
|
458
485
|
* return { id: taskAsync.id, access: taskAsync.access };
|
|
459
486
|
* ```
|
|
460
487
|
*/
|
|
461
|
-
generateAsync<const T extends MynthSDKTypes.ImageGenerationClientRequest>(request: T): Promise<TaskAsync<ImageGenerationResult<ExtractMetadata<T>, ExtractRatingResponse<T>>>>;
|
|
488
|
+
generateAsync<const T extends MynthSDKTypes.ImageGenerationClientRequest>(request: T, options?: MynthRequestOptions): Promise<TaskAsync<ImageGenerationResult<ExtractMetadata<T>, ExtractRatingResponse<T>>>>;
|
|
462
489
|
private createGenerationTask;
|
|
463
490
|
/**
|
|
464
491
|
* Rate the content of a single image.
|
|
@@ -760,4 +787,4 @@ declare class Mynth {
|
|
|
760
787
|
get video(): MynthVideo;
|
|
761
788
|
}
|
|
762
789
|
//#endregion
|
|
763
|
-
export { AVAILABLE_MODELS, AVAILABLE_VIDEO_MODELS, type AvailableModel, type AvailableVideoModel, type ModelCapability, Mynth, Mynth as default, MynthImage, type MynthModel, type MynthModelPricing, MynthModels, type MynthOptions, type MynthSDKTypes, MynthVideo, type TaskAsyncAccess, type VideoInputRole, type VideoResolutionTier };
|
|
790
|
+
export { AVAILABLE_MODELS, AVAILABLE_VIDEO_MODELS, type AvailableModel, type AvailableVideoModel, type ModelCapability, Mynth, Mynth as default, MynthImage, type MynthModel, type MynthModelPricing, MynthModels, type MynthOptions, type MynthRequestOptions, type MynthSDKTypes, MynthVideo, type TaskAsyncAccess, type TaskAsyncWaitOptions, type VideoInputRole, type VideoResolutionTier };
|
package/dist/index.js
CHANGED
|
@@ -30,22 +30,22 @@ const AVAILABLE_MODELS = [
|
|
|
30
30
|
{
|
|
31
31
|
id: "alibaba/qwen-image-2.0",
|
|
32
32
|
label: "Qwen Image 2.0",
|
|
33
|
-
capabilities: ["inputs"
|
|
33
|
+
capabilities: ["inputs"]
|
|
34
34
|
},
|
|
35
35
|
{
|
|
36
36
|
id: "alibaba/qwen-image-2.0-pro",
|
|
37
37
|
label: "Qwen Image 2.0 Pro",
|
|
38
|
-
capabilities: ["inputs"
|
|
38
|
+
capabilities: ["inputs"]
|
|
39
39
|
},
|
|
40
40
|
{
|
|
41
41
|
id: "alibaba/qwen-image-3.0",
|
|
42
42
|
label: "Qwen Image 3.0",
|
|
43
|
-
capabilities: ["inputs"
|
|
43
|
+
capabilities: ["inputs"]
|
|
44
44
|
},
|
|
45
45
|
{
|
|
46
46
|
id: "alibaba/qwen-image-3.0-pro",
|
|
47
47
|
label: "Qwen Image 3.0 Pro",
|
|
48
|
-
capabilities: ["inputs"
|
|
48
|
+
capabilities: ["inputs"]
|
|
49
49
|
},
|
|
50
50
|
{
|
|
51
51
|
id: "bytedance/seedream-5.0-lite",
|
|
@@ -57,6 +57,11 @@ const AVAILABLE_MODELS = [
|
|
|
57
57
|
label: "Seedream Pro",
|
|
58
58
|
capabilities: ["inputs"]
|
|
59
59
|
},
|
|
60
|
+
{
|
|
61
|
+
id: "bytedance/seedream-v5-flash",
|
|
62
|
+
label: "Seedream 5.0 Flash",
|
|
63
|
+
capabilities: ["inputs"]
|
|
64
|
+
},
|
|
60
65
|
{
|
|
61
66
|
id: "black-forest-labs/flux.1-dev",
|
|
62
67
|
label: "FLUX.1 Dev",
|
|
@@ -117,6 +122,11 @@ const AVAILABLE_MODELS = [
|
|
|
117
122
|
label: "Anima",
|
|
118
123
|
capabilities: ["inputs"]
|
|
119
124
|
},
|
|
125
|
+
{
|
|
126
|
+
id: "goofy-ai/prefect-pony-xl-lora",
|
|
127
|
+
label: "Prefect Pony XL LoRA",
|
|
128
|
+
capabilities: []
|
|
129
|
+
},
|
|
120
130
|
{
|
|
121
131
|
id: "john6666/bismuth-illustrious-mix",
|
|
122
132
|
label: "Bismuth Illustrious Mix",
|
|
@@ -192,6 +202,11 @@ const AVAILABLE_MODELS = [
|
|
|
192
202
|
label: "Recraft V4",
|
|
193
203
|
capabilities: []
|
|
194
204
|
},
|
|
205
|
+
{
|
|
206
|
+
id: "recraft/recraft-v4.1-flash",
|
|
207
|
+
label: "Recraft V4.1 Flash",
|
|
208
|
+
capabilities: []
|
|
209
|
+
},
|
|
195
210
|
{
|
|
196
211
|
id: "recraft/recraft-v4-pro",
|
|
197
212
|
label: "Recraft V4 Pro",
|
|
@@ -210,34 +225,22 @@ const AVAILABLE_MODELS = [
|
|
|
210
225
|
{
|
|
211
226
|
id: "sourceful/riverflow-2.0-pro",
|
|
212
227
|
label: "Riverflow 2.0 Pro",
|
|
213
|
-
capabilities: [
|
|
214
|
-
"inputs",
|
|
215
|
-
"4k",
|
|
216
|
-
"native_enhance_prompt"
|
|
217
|
-
]
|
|
228
|
+
capabilities: ["inputs", "4k"]
|
|
218
229
|
},
|
|
219
230
|
{
|
|
220
231
|
id: "google/gemini-3.1-flash-lite-image",
|
|
221
232
|
label: "Nano Banana 2 Lite",
|
|
222
|
-
capabilities: ["inputs"
|
|
233
|
+
capabilities: ["inputs"]
|
|
223
234
|
},
|
|
224
235
|
{
|
|
225
236
|
id: "google/gemini-3.1-flash-image",
|
|
226
237
|
label: "Nano Banana 2",
|
|
227
|
-
capabilities: [
|
|
228
|
-
"inputs",
|
|
229
|
-
"4k",
|
|
230
|
-
"native_enhance_prompt"
|
|
231
|
-
]
|
|
238
|
+
capabilities: ["inputs", "4k"]
|
|
232
239
|
},
|
|
233
240
|
{
|
|
234
241
|
id: "google/gemini-3-pro-image-preview",
|
|
235
242
|
label: "Nano Banana Pro",
|
|
236
|
-
capabilities: [
|
|
237
|
-
"inputs",
|
|
238
|
-
"4k",
|
|
239
|
-
"native_enhance_prompt"
|
|
240
|
-
]
|
|
243
|
+
capabilities: ["inputs", "4k"]
|
|
241
244
|
},
|
|
242
245
|
{
|
|
243
246
|
id: "imagineart/imagineart-1.5-pro",
|
|
@@ -357,6 +360,24 @@ const AVAILABLE_VIDEO_MODELS = [
|
|
|
357
360
|
audio: true,
|
|
358
361
|
inputs: ["first_frame"],
|
|
359
362
|
maxInputs: 1
|
|
363
|
+
},
|
|
364
|
+
{
|
|
365
|
+
id: "xai/grok-imagine-video-1.5",
|
|
366
|
+
label: "Grok Imagine Video 1.5",
|
|
367
|
+
resolutions: [
|
|
368
|
+
"480p",
|
|
369
|
+
"720p",
|
|
370
|
+
"1080p"
|
|
371
|
+
],
|
|
372
|
+
defaultResolution: "480p",
|
|
373
|
+
duration: {
|
|
374
|
+
default: 8,
|
|
375
|
+
min: 1,
|
|
376
|
+
max: 15
|
|
377
|
+
},
|
|
378
|
+
audio: true,
|
|
379
|
+
inputs: ["first_frame"],
|
|
380
|
+
maxInputs: 1
|
|
360
381
|
}
|
|
361
382
|
];
|
|
362
383
|
//#endregion
|
|
@@ -399,7 +420,7 @@ var MynthClient = class {
|
|
|
399
420
|
getUrl(path) {
|
|
400
421
|
return `${this.baseUrl}${path}`;
|
|
401
422
|
}
|
|
402
|
-
async post(path, data) {
|
|
423
|
+
async post(path, data, { signal } = {}) {
|
|
403
424
|
const isFormData = typeof FormData !== "undefined" && data instanceof FormData;
|
|
404
425
|
const response = await fetch(this.getUrl(path), {
|
|
405
426
|
method: "POST",
|
|
@@ -407,20 +428,24 @@ var MynthClient = class {
|
|
|
407
428
|
...isFormData ? {} : { "Content-Type": "application/json" },
|
|
408
429
|
...this.getAuthHeaders()
|
|
409
430
|
},
|
|
410
|
-
body: isFormData ? data : JSON.stringify(data)
|
|
431
|
+
body: isFormData ? data : JSON.stringify(data),
|
|
432
|
+
...signal ? { signal } : {}
|
|
411
433
|
});
|
|
412
434
|
const json = await response.json();
|
|
413
435
|
if (!response.ok) throw createApiError(json, response.status);
|
|
414
436
|
return json;
|
|
415
437
|
}
|
|
416
|
-
async get(path, { headers, accessToken, auth } = {}) {
|
|
417
|
-
const response = await fetch(this.getUrl(path), {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
438
|
+
async get(path, { headers, accessToken, auth, signal } = {}) {
|
|
439
|
+
const response = await fetch(this.getUrl(path), {
|
|
440
|
+
headers: {
|
|
441
|
+
...this.getAuthHeaders({
|
|
442
|
+
accessToken,
|
|
443
|
+
auth
|
|
444
|
+
}),
|
|
445
|
+
...headers
|
|
446
|
+
},
|
|
447
|
+
...signal ? { signal } : {}
|
|
448
|
+
});
|
|
424
449
|
return {
|
|
425
450
|
data: await response.json(),
|
|
426
451
|
status: response.status,
|
|
@@ -483,7 +508,7 @@ var ImageGenerationResult = class {
|
|
|
483
508
|
return this.data.status;
|
|
484
509
|
}
|
|
485
510
|
/**
|
|
486
|
-
* The generation result
|
|
511
|
+
* The generation result: images, the format each one was delivered in, and the model.
|
|
487
512
|
* Returns `null` if the task hasn't completed yet.
|
|
488
513
|
*/
|
|
489
514
|
get result() {
|
|
@@ -683,7 +708,7 @@ var TaskAsync = class {
|
|
|
683
708
|
_access;
|
|
684
709
|
resultFactory;
|
|
685
710
|
polling;
|
|
686
|
-
|
|
711
|
+
poll = null;
|
|
687
712
|
constructor(id, options) {
|
|
688
713
|
this.id = id;
|
|
689
714
|
this.client = options.client;
|
|
@@ -708,19 +733,54 @@ var TaskAsync = class {
|
|
|
708
733
|
}
|
|
709
734
|
/**
|
|
710
735
|
* Polls the task until completion and returns the typed result.
|
|
711
|
-
*
|
|
736
|
+
* Concurrent calls share one polling loop and settle with the same result.
|
|
712
737
|
*
|
|
738
|
+
* @param options.signal - Stops this wait when aborted (see {@link TaskAsyncWaitOptions})
|
|
713
739
|
* @throws {TaskAsyncTimeoutError} If polling exceeds the timeout
|
|
714
740
|
* @throws {TaskAsyncUnauthorizedError} If access is denied
|
|
715
741
|
* @throws {TaskAsyncFetchError} If fetching status fails repeatedly
|
|
716
742
|
* @throws {TaskAsyncTaskFetchError} If fetching the completed task fails
|
|
717
743
|
* @throws {TaskAsyncTaskFailedError} If the task fails before completion
|
|
718
744
|
*/
|
|
719
|
-
async wait() {
|
|
720
|
-
|
|
721
|
-
|
|
745
|
+
async wait({ signal } = {}) {
|
|
746
|
+
signal?.throwIfAborted();
|
|
747
|
+
const poll = this.poll ??= this.startPoll();
|
|
748
|
+
if (!signal) {
|
|
749
|
+
poll.pinned = true;
|
|
750
|
+
return poll.promise;
|
|
751
|
+
}
|
|
752
|
+
poll.abortableWaiters++;
|
|
753
|
+
return new Promise((resolve, reject) => {
|
|
754
|
+
const onAbort = () => {
|
|
755
|
+
poll.abortableWaiters--;
|
|
756
|
+
reject(signal.reason);
|
|
757
|
+
if (poll.abortableWaiters === 0 && !poll.pinned) {
|
|
758
|
+
poll.controller.abort(signal.reason);
|
|
759
|
+
if (this.poll === poll) this.poll = null;
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
763
|
+
poll.promise.then((result) => {
|
|
764
|
+
signal.removeEventListener("abort", onAbort);
|
|
765
|
+
resolve(result);
|
|
766
|
+
}, (error) => {
|
|
767
|
+
signal.removeEventListener("abort", onAbort);
|
|
768
|
+
reject(error);
|
|
769
|
+
});
|
|
770
|
+
});
|
|
722
771
|
}
|
|
723
|
-
|
|
772
|
+
startPoll() {
|
|
773
|
+
const controller = new AbortController();
|
|
774
|
+
const promise = this.pollUntilCompleted(controller.signal);
|
|
775
|
+
promise.catch(() => {});
|
|
776
|
+
return {
|
|
777
|
+
promise,
|
|
778
|
+
controller,
|
|
779
|
+
abortableWaiters: 0,
|
|
780
|
+
pinned: false
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
async pollUntilCompleted(signal) {
|
|
724
784
|
const { timeoutMs, fastDurationMs, fastIntervalMs, intervalMs } = this.polling;
|
|
725
785
|
const startTime = Date.now();
|
|
726
786
|
let retryCount = 0;
|
|
@@ -729,10 +789,10 @@ var TaskAsync = class {
|
|
|
729
789
|
while (true) {
|
|
730
790
|
const elapsed = Date.now() - startTime;
|
|
731
791
|
if (elapsed >= timeoutMs) throw new TaskAsyncTimeoutError(this.id, timeoutMs);
|
|
732
|
-
const result = await this.fetchStatus(useApiKeyFallback);
|
|
792
|
+
const result = await this.fetchStatus(useApiKeyFallback, signal);
|
|
733
793
|
if (result.ok) {
|
|
734
794
|
if (result.status === "completed") {
|
|
735
|
-
const fetched = await this.fetchTask();
|
|
795
|
+
const fetched = await this.fetchTask(signal);
|
|
736
796
|
if (fetched.ok) return this.resultFactory(fetched.data);
|
|
737
797
|
if (fetched.unauthorized) throw new TaskAsyncUnauthorizedError(this.id);
|
|
738
798
|
retryCount++;
|
|
@@ -754,13 +814,16 @@ var TaskAsync = class {
|
|
|
754
814
|
const interval = (elapsed < fastDurationMs ? fastIntervalMs : intervalMs) + Math.random() * 500;
|
|
755
815
|
const remainingTime = timeoutMs - elapsed;
|
|
756
816
|
const waitTime = Math.min(interval, remainingTime);
|
|
757
|
-
await this.sleep(waitTime);
|
|
817
|
+
await this.sleep(waitTime, signal);
|
|
758
818
|
}
|
|
759
819
|
}
|
|
760
|
-
async fetchStatus(useApiKey) {
|
|
820
|
+
async fetchStatus(useApiKey, signal) {
|
|
761
821
|
const accessToken = useApiKey || !this._access.publicAccessToken ? void 0 : this._access.publicAccessToken;
|
|
762
822
|
try {
|
|
763
|
-
const response = await this.client.get(TASK_STATUS_PATH(this.id), {
|
|
823
|
+
const response = await this.client.get(TASK_STATUS_PATH(this.id), {
|
|
824
|
+
accessToken,
|
|
825
|
+
signal
|
|
826
|
+
});
|
|
764
827
|
if (response.ok) return {
|
|
765
828
|
ok: true,
|
|
766
829
|
status: response.data.data.status
|
|
@@ -787,6 +850,7 @@ var TaskAsync = class {
|
|
|
787
850
|
retryable: false
|
|
788
851
|
};
|
|
789
852
|
} catch (error) {
|
|
853
|
+
signal.throwIfAborted();
|
|
790
854
|
return {
|
|
791
855
|
ok: false,
|
|
792
856
|
unauthorized: false,
|
|
@@ -795,9 +859,9 @@ var TaskAsync = class {
|
|
|
795
859
|
};
|
|
796
860
|
}
|
|
797
861
|
}
|
|
798
|
-
async fetchTask() {
|
|
862
|
+
async fetchTask(signal) {
|
|
799
863
|
try {
|
|
800
|
-
const response = await this.client.get(TASK_DETAILS_PATH(this.id));
|
|
864
|
+
const response = await this.client.get(TASK_DETAILS_PATH(this.id), { signal });
|
|
801
865
|
if (response.ok) return {
|
|
802
866
|
ok: true,
|
|
803
867
|
data: response.data.data
|
|
@@ -820,6 +884,7 @@ var TaskAsync = class {
|
|
|
820
884
|
status: response.status
|
|
821
885
|
};
|
|
822
886
|
} catch (error) {
|
|
887
|
+
signal.throwIfAborted();
|
|
823
888
|
return {
|
|
824
889
|
ok: false,
|
|
825
890
|
unauthorized: false,
|
|
@@ -828,8 +893,19 @@ var TaskAsync = class {
|
|
|
828
893
|
};
|
|
829
894
|
}
|
|
830
895
|
}
|
|
831
|
-
sleep(ms) {
|
|
832
|
-
return new Promise((resolve) =>
|
|
896
|
+
sleep(ms, signal) {
|
|
897
|
+
return new Promise((resolve, reject) => {
|
|
898
|
+
signal.throwIfAborted();
|
|
899
|
+
const onAbort = () => {
|
|
900
|
+
clearTimeout(timer);
|
|
901
|
+
reject(signal.reason);
|
|
902
|
+
};
|
|
903
|
+
const timer = setTimeout(() => {
|
|
904
|
+
signal.removeEventListener("abort", onAbort);
|
|
905
|
+
resolve();
|
|
906
|
+
}, ms);
|
|
907
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
908
|
+
});
|
|
833
909
|
}
|
|
834
910
|
};
|
|
835
911
|
//#endregion
|
|
@@ -843,25 +919,25 @@ function isUploadInput(value) {
|
|
|
843
919
|
* Upload images to Mynth temporary input storage.
|
|
844
920
|
* Image and video generation share this endpoint: video inputs are images.
|
|
845
921
|
*/
|
|
846
|
-
async function uploadImages(client, input) {
|
|
922
|
+
async function uploadImages(client, input, { signal } = {}) {
|
|
847
923
|
const form = new FormData();
|
|
848
924
|
const inputs = Array.isArray(input) ? input : [input];
|
|
849
925
|
for (const upload of inputs) form.append(UPLOAD_FIELD_NAME, upload, typeof File !== "undefined" && upload instanceof File ? upload.name : UPLOAD_FILENAME);
|
|
850
|
-
return (await client.post(UPLOAD_IMAGE_PATH, form)).data;
|
|
926
|
+
return (await client.post(UPLOAD_IMAGE_PATH, form, { signal })).data;
|
|
851
927
|
}
|
|
852
928
|
/**
|
|
853
929
|
* Turn caller inputs into wire inputs, uploading any local files first.
|
|
854
930
|
* All files go up in a single request, and the returned URLs are slotted back
|
|
855
931
|
* into their original positions so input order (and role) is preserved.
|
|
856
932
|
*/
|
|
857
|
-
async function resolveInputs(client, inputs) {
|
|
933
|
+
async function resolveInputs(client, inputs, { signal } = {}) {
|
|
858
934
|
if (!inputs?.length) return;
|
|
859
935
|
const files = inputs.flatMap((input) => {
|
|
860
936
|
if (isUploadInput(input)) return [input];
|
|
861
937
|
if (typeof input !== "string" && input.source.type === "file") return [input.source.file];
|
|
862
938
|
return [];
|
|
863
939
|
});
|
|
864
|
-
const urls = files.length ? (await uploadImages(client, files)).urls : [];
|
|
940
|
+
const urls = files.length ? (await uploadImages(client, files, { signal })).urls : [];
|
|
865
941
|
let i = 0;
|
|
866
942
|
return inputs.map((input) => {
|
|
867
943
|
if (typeof input === "string") return input;
|
|
@@ -995,6 +1071,7 @@ var MynthImage = class {
|
|
|
995
1071
|
* Generate images from a text prompt.
|
|
996
1072
|
*
|
|
997
1073
|
* @param request - Image generation request parameters
|
|
1074
|
+
* @param options.signal - Aborts the upload, the create request, or the wait for completion
|
|
998
1075
|
* @returns A completed ImageGenerationResult with the generation results
|
|
999
1076
|
*
|
|
1000
1077
|
* @example
|
|
@@ -1006,8 +1083,8 @@ var MynthImage = class {
|
|
|
1006
1083
|
* console.log(result.urls);
|
|
1007
1084
|
* ```
|
|
1008
1085
|
*/
|
|
1009
|
-
async generate(request) {
|
|
1010
|
-
return (await this.createGenerationTask(request)).wait();
|
|
1086
|
+
async generate(request, options = {}) {
|
|
1087
|
+
return (await this.createGenerationTask(request, options)).wait(options);
|
|
1011
1088
|
}
|
|
1012
1089
|
/**
|
|
1013
1090
|
* Upload one or more images to Mynth temporary input storage.
|
|
@@ -1030,6 +1107,7 @@ var MynthImage = class {
|
|
|
1030
1107
|
* Start image generation without waiting for completion.
|
|
1031
1108
|
*
|
|
1032
1109
|
* @param request - Image generation request parameters
|
|
1110
|
+
* @param options.signal - Aborts the upload or the create request. Pass a signal to `.wait()` to abort the wait.
|
|
1033
1111
|
* @returns A TaskAsync that can be polled for completion via `.wait()`
|
|
1034
1112
|
*
|
|
1035
1113
|
* @example
|
|
@@ -1041,16 +1119,16 @@ var MynthImage = class {
|
|
|
1041
1119
|
* return { id: taskAsync.id, access: taskAsync.access };
|
|
1042
1120
|
* ```
|
|
1043
1121
|
*/
|
|
1044
|
-
async generateAsync(request) {
|
|
1045
|
-
return this.createGenerationTask(request);
|
|
1122
|
+
async generateAsync(request, options = {}) {
|
|
1123
|
+
return this.createGenerationTask(request, options);
|
|
1046
1124
|
}
|
|
1047
|
-
async createGenerationTask(request) {
|
|
1048
|
-
const inputs = await resolveInputs(this.client, request.inputs);
|
|
1125
|
+
async createGenerationTask(request, { signal }) {
|
|
1126
|
+
const inputs = await resolveInputs(this.client, request.inputs, { signal });
|
|
1049
1127
|
const data = (await this.client.post(GENERATE_IMAGE_PATH, {
|
|
1050
1128
|
...request,
|
|
1051
1129
|
inputs,
|
|
1052
1130
|
destination: request.destination ?? this.defaultDestination
|
|
1053
|
-
})).data;
|
|
1131
|
+
}, { signal })).data;
|
|
1054
1132
|
return new TaskAsync(data.taskId, {
|
|
1055
1133
|
client: this.client,
|
|
1056
1134
|
pat: data.access?.publicAccessToken,
|
package/dist/types.d.ts
CHANGED
|
@@ -4,9 +4,8 @@
|
|
|
4
4
|
* - `negative_prompt`: Supports structured negative prompts
|
|
5
5
|
* - `inputs`: Supports input images
|
|
6
6
|
* - `4k`: Supports 4k resolution output
|
|
7
|
-
* - `native_enhance_prompt`: Supports provider-native prompt enhancement
|
|
8
7
|
*/
|
|
9
|
-
type ModelCapability = "inputs" | "negative_prompt" | "4k"
|
|
8
|
+
type ModelCapability = "inputs" | "negative_prompt" | "4k";
|
|
10
9
|
/**
|
|
11
10
|
* Information about an available image generation model.
|
|
12
11
|
*/
|
|
@@ -131,16 +130,18 @@ declare namespace MynthSDKTypes {
|
|
|
131
130
|
type VideoGenerationTaskData = Extract<TaskData, {
|
|
132
131
|
type: "video.generate";
|
|
133
132
|
}>;
|
|
134
|
-
type
|
|
133
|
+
type ImageModelInputKind = "source" | "reference";
|
|
134
|
+
type VideoModelInputKind = "first_frame" | "last_frame" | "reference" | "source";
|
|
135
|
+
type ModelInputRule<Kind extends string = ImageModelInputKind | VideoModelInputKind> = {
|
|
135
136
|
type: "image";
|
|
136
137
|
/** Role the input plays. Absent means the rule accepts any image. */
|
|
137
|
-
kind?:
|
|
138
|
+
kind?: Kind;
|
|
138
139
|
min?: number;
|
|
139
140
|
max: number;
|
|
140
141
|
};
|
|
141
|
-
type ModelMode = {
|
|
142
|
+
type ModelMode<Kind extends string = ImageModelInputKind | VideoModelInputKind> = {
|
|
142
143
|
inputs?: {
|
|
143
|
-
rules: ModelInputRule[];
|
|
144
|
+
rules: ModelInputRule<Kind>[];
|
|
144
145
|
maxTotal?: number;
|
|
145
146
|
};
|
|
146
147
|
};
|
|
@@ -164,19 +165,19 @@ declare namespace MynthSDKTypes {
|
|
|
164
165
|
id: string;
|
|
165
166
|
displayName: string | null;
|
|
166
167
|
type: "image";
|
|
167
|
-
modes: Partial<Record<"txt->img" | "img->img", ModelMode
|
|
168
|
+
modes: Partial<Record<"txt->img" | "img->img", ModelMode<ImageModelInputKind>>>;
|
|
168
169
|
pricing: ImageModelPricing | null;
|
|
169
170
|
};
|
|
170
171
|
type VideoModel = {
|
|
171
172
|
id: string;
|
|
172
173
|
displayName: string | null;
|
|
173
174
|
type: "video";
|
|
174
|
-
modes: Partial<Record<"txt->vid" | "img->vid", ModelMode
|
|
175
|
+
modes: Partial<Record<"txt->vid" | "img->vid", ModelMode<VideoModelInputKind>>>;
|
|
175
176
|
pricing: VideoModelPricing | null;
|
|
176
177
|
};
|
|
177
178
|
type Model = ImageModel | VideoModel;
|
|
178
179
|
type ModelsListResponse = ApiResponse<Model[]>;
|
|
179
|
-
type ImageGenerationModelId = "alibaba/qwen-image-2.0" | "alibaba/qwen-image-2.0-pro" | "alibaba/qwen-image-3.0" | "alibaba/qwen-image-3.0-pro" | "bytedance/seedream-5.0-lite" | "bytedance/seedream-pro" | "black-forest-labs/flux.1-dev" | "black-forest-labs/flux-1-schnell" | "black-forest-labs/flux.2-dev" | "black-forest-labs/flux.2-pro" | "black-forest-labs/flux.2-flex" | "black-forest-labs/flux.2-max" | "black-forest-labs/flux.2-klein-4b" | "bria/fibo-edit-1.5" | "bria/fibo-generate-1.5" | "circlestone-labs/anima" | "google/gemini-3.1-flash-lite-image" | "google/gemini-3.1-flash-image" | "google/gemini-3-pro-image-preview" | "imagineart/imagineart-1.5-pro" | "imagineart/imagineart-2.0" | "klingai/kling-image-3.0" | "klingai/kling-image-o3" | "krea/krea-2-turbo" | "krea/krea-2-medium" | "krea/krea-2-large" | "luma/uni-1" | "luma/uni-1-max" | "meta/muse-image" | "microsoft/mai-image-2.6" | "microsoft/mai-image-2.6-flash" | "minimax/h3" | "openai/gpt-image-2" | "openai/gpt-image-2.5-flare" | "openai/gpt-image-2.5-sunburst" | "tongyi-mai/z-image" | "tongyi-mai/z-image-turbo" | "john6666/bismuth-illustrious-mix" | "maxfeifei8/one-obsession" | "purplesmartai/pony-diffusion-v6-xl" | "recraft/recraft-v4" | "recraft/recraft-v4-pro" | "reve/reve" | "reve/reve-remix" | "sourceful/riverflow-2.0-pro" | "wan/wan2.6-image" | "wan/wan2.7-image" | "wan/wan2.7-image-pro" | "xai/grok-imagine-image" | "xai/grok-imagine-image-2.0" | "xai/grok-imagine-image-quality";
|
|
180
|
+
type ImageGenerationModelId = "alibaba/qwen-image-2.0" | "alibaba/qwen-image-2.0-pro" | "alibaba/qwen-image-3.0" | "alibaba/qwen-image-3.0-pro" | "bytedance/seedream-5.0-lite" | "bytedance/seedream-pro" | "bytedance/seedream-v5-flash" | "black-forest-labs/flux.1-dev" | "black-forest-labs/flux-1-schnell" | "black-forest-labs/flux.2-dev" | "black-forest-labs/flux.2-pro" | "black-forest-labs/flux.2-flex" | "black-forest-labs/flux.2-max" | "black-forest-labs/flux.2-klein-4b" | "bria/fibo-edit-1.5" | "bria/fibo-generate-1.5" | "circlestone-labs/anima" | "goofy-ai/prefect-pony-xl-lora" | "google/gemini-3.1-flash-lite-image" | "google/gemini-3.1-flash-image" | "google/gemini-3-pro-image-preview" | "imagineart/imagineart-1.5-pro" | "imagineart/imagineart-2.0" | "klingai/kling-image-3.0" | "klingai/kling-image-o3" | "krea/krea-2-turbo" | "krea/krea-2-medium" | "krea/krea-2-large" | "luma/uni-1" | "luma/uni-1-max" | "meta/muse-image" | "microsoft/mai-image-2.6" | "microsoft/mai-image-2.6-flash" | "minimax/h3" | "openai/gpt-image-2" | "openai/gpt-image-2.5-flare" | "openai/gpt-image-2.5-sunburst" | "tongyi-mai/z-image" | "tongyi-mai/z-image-turbo" | "john6666/bismuth-illustrious-mix" | "maxfeifei8/one-obsession" | "purplesmartai/pony-diffusion-v6-xl" | "recraft/recraft-v4" | "recraft/recraft-v4.1-flash" | "recraft/recraft-v4-pro" | "reve/reve" | "reve/reve-remix" | "sourceful/riverflow-2.0-pro" | "wan/wan2.6-image" | "wan/wan2.7-image" | "wan/wan2.7-image-pro" | "xai/grok-imagine-image" | "xai/grok-imagine-image-2.0" | "xai/grok-imagine-image-quality";
|
|
180
181
|
type ImageGenerationModel = ImageGenerationModelId | "auto";
|
|
181
182
|
type GenerateImageOptionsIn = {
|
|
182
183
|
prompt: string;
|
|
@@ -187,9 +188,8 @@ declare namespace MynthSDKTypes {
|
|
|
187
188
|
type ImageGenerationRequestPrompt = GenerateImageOptionsIn["prompt"];
|
|
188
189
|
type ImageGenerationRequestOutputFormat = "png" | "jpg" | "webp";
|
|
189
190
|
type ImageGenerationRequestOutput = {
|
|
190
|
-
format
|
|
191
|
-
|
|
192
|
-
quality: number;
|
|
191
|
+
/** Converts the result to this format. When omitted, the provider's format is kept. */
|
|
192
|
+
format?: ImageGenerationRequestOutputFormat;
|
|
193
193
|
};
|
|
194
194
|
type ImageGenerationRequestCustomWebhook = {
|
|
195
195
|
url: string;
|
|
@@ -294,7 +294,7 @@ declare namespace MynthSDKTypes {
|
|
|
294
294
|
url: string | null;
|
|
295
295
|
mynth_url: string;
|
|
296
296
|
size: string;
|
|
297
|
-
|
|
297
|
+
format: ImageGenerationRequestOutputFormat;
|
|
298
298
|
destination?: ImageResultDestination;
|
|
299
299
|
rating?: ImageResultRating;
|
|
300
300
|
};
|
|
@@ -435,7 +435,7 @@ declare namespace MynthSDKTypes {
|
|
|
435
435
|
type ImageRemoveBackgroundTaskResult = {
|
|
436
436
|
image: ImageRemoveBackgroundResultImage;
|
|
437
437
|
};
|
|
438
|
-
type VideoGenerationModelId = "bytedance/seedance-2.0-mini" | "google/gemini-omni-flash-1.1" | "prunaai/p-video";
|
|
438
|
+
type VideoGenerationModelId = "bytedance/seedance-2.0-mini" | "google/gemini-omni-flash-1.1" | "prunaai/p-video" | "xai/grok-imagine-video-1.5";
|
|
439
439
|
type VideoGenerationModel = VideoGenerationModelId;
|
|
440
440
|
type VideoGenerationRequestResolution = "480p" | "720p" | "1080p" | "4k";
|
|
441
441
|
type VideoGenerationRequestWebhook = ImageGenerationRequestWebhook;
|