@mynthio/sdk 0.0.49 → 0.0.50
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 +132 -44
- package/dist/convex/index.d.ts +5 -4
- package/dist/convex/index.js +3 -3
- package/dist/handler.d.ts +19 -3
- package/dist/handler.js +60 -113
- package/dist/index.d.ts +68 -10
- package/dist/index.js +97 -24
- package/dist/next/index.d.ts +7 -6
- package/dist/next/index.js +5 -5
- package/dist/tanstack-start/index.d.ts +6 -5
- package/dist/tanstack-start/index.js +3 -3
- package/dist/utils.js +312 -0
- package/dist/video-generation-result.d.ts +76 -161
- package/package.json +2 -1
- package/dist/video-generation-result.js +0 -216
|
@@ -76,12 +76,15 @@ declare namespace MynthSDKTypes {
|
|
|
76
76
|
type TaskBase = {
|
|
77
77
|
id: string;
|
|
78
78
|
status: TaskStatus;
|
|
79
|
-
|
|
80
|
-
userId: string;
|
|
79
|
+
/** USD charged. Set once the task completes. */
|
|
81
80
|
cost: string | null;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
/** Why the task failed. `null` unless `status` is `failed`. */
|
|
82
|
+
errors: TaskError[] | null;
|
|
83
|
+
user_id: string;
|
|
84
|
+
/** The API key that created the task. `null` for tasks created without one. */
|
|
85
|
+
api_key_id: string | null;
|
|
86
|
+
created_at: string;
|
|
87
|
+
updated_at: string;
|
|
85
88
|
};
|
|
86
89
|
type TaskError = {
|
|
87
90
|
code: string;
|
|
@@ -149,35 +152,35 @@ declare namespace MynthSDKTypes {
|
|
|
149
152
|
type ModelMode<Kind extends string = ImageModelInputKind | VideoModelInputKind> = {
|
|
150
153
|
inputs?: {
|
|
151
154
|
rules: ModelInputRule<Kind>[];
|
|
152
|
-
|
|
155
|
+
max_total?: number;
|
|
153
156
|
};
|
|
154
157
|
};
|
|
155
158
|
type ImageModelPricing = {
|
|
156
|
-
|
|
159
|
+
per_image: {
|
|
157
160
|
base: string;
|
|
158
161
|
"4k"?: string;
|
|
159
162
|
};
|
|
160
|
-
|
|
163
|
+
per_input?: string;
|
|
161
164
|
};
|
|
162
165
|
type VideoModelPricing = {
|
|
163
166
|
/** USD per second, keyed by resolution tier. Tiers the model cannot produce are absent. */
|
|
164
|
-
|
|
167
|
+
per_second: Partial<Record<VideoResolutionTier, string>>;
|
|
165
168
|
/** Present only when audio is billed on top of the per-second video rate. */
|
|
166
169
|
audio?: {
|
|
167
|
-
|
|
170
|
+
per_second: string;
|
|
168
171
|
};
|
|
169
172
|
};
|
|
170
173
|
type ModelPricing = ImageModelPricing | VideoModelPricing;
|
|
171
174
|
type ImageModel = {
|
|
172
175
|
id: string;
|
|
173
|
-
|
|
176
|
+
display_name: string | null;
|
|
174
177
|
type: "image";
|
|
175
178
|
modes: Partial<Record<"txt->img" | "img->img", ModelMode<ImageModelInputKind>>>;
|
|
176
179
|
pricing: ImageModelPricing | null;
|
|
177
180
|
};
|
|
178
181
|
type VideoModel = {
|
|
179
182
|
id: string;
|
|
180
|
-
|
|
183
|
+
display_name: string | null;
|
|
181
184
|
type: "video";
|
|
182
185
|
modes: Partial<Record<"txt->vid" | "img->vid", ModelMode<VideoModelInputKind>>>;
|
|
183
186
|
pricing: VideoModelPricing | null;
|
|
@@ -201,14 +204,9 @@ declare namespace MynthSDKTypes {
|
|
|
201
204
|
type ImageGenerationRequestCustomWebhook = {
|
|
202
205
|
url: string;
|
|
203
206
|
}[];
|
|
204
|
-
type ImageGenerationRequestAccessPat = {
|
|
205
|
-
enabled?: boolean;
|
|
206
|
-
};
|
|
207
|
-
type ImageGenerationRequestAccess = {
|
|
208
|
-
pat: ImageGenerationRequestAccessPat;
|
|
209
|
-
};
|
|
210
207
|
type ImageGenerationRequestWebhook = {
|
|
211
|
-
|
|
208
|
+
/** `false` skips the webhooks registered on the account for this task. */
|
|
209
|
+
registered?: boolean;
|
|
212
210
|
custom?: ImageGenerationRequestCustomWebhook;
|
|
213
211
|
};
|
|
214
212
|
type ImageGenerationRequestRatingLevel<T extends string = string> = {
|
|
@@ -224,7 +222,7 @@ declare namespace MynthSDKTypes {
|
|
|
224
222
|
type ImageGenerationRequestAspectRatio = "1:1" | "2:3" | "3:2" | "3:4" | "4:3" | "4:5" | "5:4" | "9:16" | "16:9" | "21:9" | "2:1" | "1:2";
|
|
225
223
|
type ImageGenerationRequestSizeAspectRatio = {
|
|
226
224
|
type: "aspect_ratio";
|
|
227
|
-
|
|
225
|
+
aspect_ratio: ImageGenerationRequestAspectRatio;
|
|
228
226
|
scale?: ImageGenerationRequestSizeScale;
|
|
229
227
|
};
|
|
230
228
|
type ImageGenerationRequestSizeAuto = {
|
|
@@ -267,7 +265,8 @@ declare namespace MynthSDKTypes {
|
|
|
267
265
|
output?: ImageGenerationRequestOutput;
|
|
268
266
|
webhook?: ImageGenerationRequestWebhook;
|
|
269
267
|
rating?: ImageGenerationRequestRating;
|
|
270
|
-
access
|
|
268
|
+
/** Return a public access token for browser-side polling. Defaults to `false`. */
|
|
269
|
+
generate_public_access_token?: boolean;
|
|
271
270
|
inputs?: (string | ImageGenerationRequestInput)[];
|
|
272
271
|
metadata?: Record<string, unknown>;
|
|
273
272
|
destination?: string;
|
|
@@ -357,8 +356,8 @@ declare namespace MynthSDKTypes {
|
|
|
357
356
|
};
|
|
358
357
|
type ImageRateClientRequest = ImageClientUrlOrFile & (ImageRateRequestRatingDefault | ImageRateRequestRatingCustom);
|
|
359
358
|
type ImageRateCreatedResponse = {
|
|
360
|
-
|
|
361
|
-
|
|
359
|
+
task_id: string;
|
|
360
|
+
estimated_cost: string;
|
|
362
361
|
};
|
|
363
362
|
type ImageRateTaskResult<LevelT extends string = string> = {
|
|
364
363
|
level: LevelT;
|
|
@@ -369,8 +368,8 @@ declare namespace MynthSDKTypes {
|
|
|
369
368
|
};
|
|
370
369
|
type ImageAltClientRequest = ImageClientUrlOrFile;
|
|
371
370
|
type ImageAltCreatedResponse = {
|
|
372
|
-
|
|
373
|
-
|
|
371
|
+
task_id: string;
|
|
372
|
+
estimated_cost: string;
|
|
374
373
|
};
|
|
375
374
|
type ImageAltTaskResult = {
|
|
376
375
|
alt: string;
|
|
@@ -389,8 +388,8 @@ declare namespace MynthSDKTypes {
|
|
|
389
388
|
effort?: ImageReviewEffort;
|
|
390
389
|
};
|
|
391
390
|
type ImageReviewCreatedResponse = {
|
|
392
|
-
|
|
393
|
-
|
|
391
|
+
task_id: string;
|
|
392
|
+
estimated_cost: string;
|
|
394
393
|
};
|
|
395
394
|
type ImageReviewFinding = {
|
|
396
395
|
/** What is wrong, in plain language. */
|
|
@@ -423,18 +422,13 @@ declare namespace MynthSDKTypes {
|
|
|
423
422
|
url: string;
|
|
424
423
|
output?: ImageRemoveBackgroundRequestOutput;
|
|
425
424
|
webhook?: ImageGenerationRequestWebhook;
|
|
426
|
-
access
|
|
425
|
+
/** Return a public access token for browser-side polling. Defaults to `false`. */
|
|
426
|
+
generate_public_access_token?: boolean;
|
|
427
427
|
metadata?: Record<string, unknown>;
|
|
428
428
|
destination?: string;
|
|
429
429
|
};
|
|
430
430
|
type ImageRemoveBackgroundClientRequest = ImageClientUrlOrFile & Omit<ImageRemoveBackgroundRequest, "url">;
|
|
431
|
-
type ImageRemoveBackgroundCreatedResponse =
|
|
432
|
-
taskId: string;
|
|
433
|
-
estimatedCost: string;
|
|
434
|
-
access?: {
|
|
435
|
-
publicAccessToken: string;
|
|
436
|
-
};
|
|
437
|
-
};
|
|
431
|
+
type ImageRemoveBackgroundCreatedResponse = TaskCreatedResponse;
|
|
438
432
|
type ImageRemoveBackgroundResultImage = {
|
|
439
433
|
id: string;
|
|
440
434
|
/** Destination URL, or the Mynth URL without a destination. `null` when delivery failed. */
|
|
@@ -467,18 +461,13 @@ declare namespace MynthSDKTypes {
|
|
|
467
461
|
effort: ImageUpscaleEffort;
|
|
468
462
|
output?: ImageUpscaleRequestOutput;
|
|
469
463
|
webhook?: ImageGenerationRequestWebhook;
|
|
470
|
-
access
|
|
464
|
+
/** Return a public access token for browser-side polling. Defaults to `false`. */
|
|
465
|
+
generate_public_access_token?: boolean;
|
|
471
466
|
metadata?: Record<string, unknown>;
|
|
472
467
|
destination?: string;
|
|
473
468
|
};
|
|
474
469
|
type ImageUpscaleClientRequest = ImageClientUrlOrFile & Omit<ImageUpscaleRequest, "url">;
|
|
475
|
-
type ImageUpscaleCreatedResponse =
|
|
476
|
-
taskId: string;
|
|
477
|
-
estimatedCost: string;
|
|
478
|
-
access?: {
|
|
479
|
-
publicAccessToken: string;
|
|
480
|
-
};
|
|
481
|
-
};
|
|
470
|
+
type ImageUpscaleCreatedResponse = TaskCreatedResponse;
|
|
482
471
|
type ImageUpscaleResultImage = {
|
|
483
472
|
id: string;
|
|
484
473
|
/** Destination URL, or the Mynth URL without a destination. `null` when delivery failed. */
|
|
@@ -496,7 +485,6 @@ declare namespace MynthSDKTypes {
|
|
|
496
485
|
type VideoGenerationModel = VideoGenerationModelId;
|
|
497
486
|
type VideoGenerationRequestResolution = "480p" | "720p" | "1080p" | "4k";
|
|
498
487
|
type VideoGenerationRequestWebhook = ImageGenerationRequestWebhook;
|
|
499
|
-
type VideoGenerationRequestAccess = ImageGenerationRequestAccess;
|
|
500
488
|
type VideoGenerationRequestInputSource = {
|
|
501
489
|
type: "url";
|
|
502
490
|
url: string;
|
|
@@ -525,31 +513,26 @@ declare namespace MynthSDKTypes {
|
|
|
525
513
|
audio?: boolean;
|
|
526
514
|
inputs?: (string | VideoGenerationRequestInput)[];
|
|
527
515
|
webhook?: VideoGenerationRequestWebhook;
|
|
528
|
-
access
|
|
516
|
+
/** Return a public access token for browser-side polling. Defaults to `false`. */
|
|
517
|
+
generate_public_access_token?: boolean;
|
|
529
518
|
metadata?: Record<string, unknown>;
|
|
530
519
|
};
|
|
531
520
|
type VideoGenerationClientRequest = Omit<VideoGenerationRequest, "inputs"> & {
|
|
532
521
|
inputs?: (string | ImageUploadInput | VideoGenerationClientInput)[];
|
|
533
522
|
};
|
|
534
|
-
type VideoGenerationCreatedResponse =
|
|
535
|
-
taskId: string;
|
|
536
|
-
/** Cost reserved for the task. Failed videos are refunded. */
|
|
537
|
-
estimatedCost: string;
|
|
538
|
-
access?: {
|
|
539
|
-
publicAccessToken: string;
|
|
540
|
-
};
|
|
541
|
-
};
|
|
523
|
+
type VideoGenerationCreatedResponse = TaskCreatedResponse;
|
|
542
524
|
type VideoGenerationEstimate = {
|
|
543
525
|
/** Estimated cost in USD. Nothing is generated or charged. */
|
|
544
|
-
|
|
526
|
+
estimated_cost: string;
|
|
545
527
|
currency: "usd";
|
|
546
528
|
/** Video generation always pins a model, so the estimate is exact. */
|
|
547
|
-
|
|
529
|
+
estimate_kind: "exact";
|
|
548
530
|
};
|
|
549
531
|
type VideoResultVideoSuccess = {
|
|
550
532
|
status: "success";
|
|
551
533
|
id: string;
|
|
552
|
-
|
|
534
|
+
/** Public URL of the video. `null` when it could not be delivered there. */
|
|
535
|
+
url: string | null;
|
|
553
536
|
mynth_url: string;
|
|
554
537
|
cost: string;
|
|
555
538
|
/** Duration of the generated video in seconds */
|
|
@@ -570,116 +553,42 @@ declare namespace MynthSDKTypes {
|
|
|
570
553
|
model: VideoGenerationModelId;
|
|
571
554
|
videos: VideoResultVideo[];
|
|
572
555
|
};
|
|
573
|
-
type
|
|
574
|
-
|
|
575
|
-
/**
|
|
556
|
+
type TaskCreatedResponse = {
|
|
557
|
+
task_id: string;
|
|
558
|
+
/** Cost reserved for the task. Failed media is refunded. */
|
|
559
|
+
estimated_cost: string;
|
|
560
|
+
/** Present when the request set `generate_public_access_token: true`. */
|
|
561
|
+
public_access_token?: string;
|
|
562
|
+
};
|
|
563
|
+
type CompletedTaskData<TypeT extends TaskType = TaskType> = Extract<TaskData, {
|
|
564
|
+
type: TypeT;
|
|
565
|
+
}> extends (infer DataT extends TaskData) ? DataT & {
|
|
566
|
+
status: "completed";
|
|
567
|
+
result: NonNullable<DataT["result"]>;
|
|
576
568
|
cost: string;
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
type WebhookTaskImageFailedPayload = {
|
|
585
|
-
task: {
|
|
586
|
-
id: string;
|
|
587
|
-
};
|
|
588
|
-
event: "task.image.generate.failed";
|
|
589
|
-
request: ImageGenerationRequest;
|
|
590
|
-
errors: TaskError[];
|
|
591
|
-
};
|
|
592
|
-
type WebhookTaskImageRateCompletedPayload<LevelT extends string = string> = {
|
|
593
|
-
task: WebhookTaskCompleted;
|
|
594
|
-
event: "task.image.rate.completed";
|
|
595
|
-
result: ImageRateTaskResult<LevelT>;
|
|
596
|
-
request: ImageRateRequest;
|
|
597
|
-
};
|
|
598
|
-
type WebhookTaskImageRateFailedPayload = {
|
|
599
|
-
task: {
|
|
600
|
-
id: string;
|
|
601
|
-
};
|
|
602
|
-
event: "task.image.rate.failed";
|
|
603
|
-
request: ImageRateRequest;
|
|
604
|
-
errors: TaskError[];
|
|
605
|
-
};
|
|
606
|
-
type WebhookTaskImageAltCompletedPayload = {
|
|
607
|
-
task: WebhookTaskCompleted;
|
|
608
|
-
event: "task.image.alt.completed";
|
|
609
|
-
result: ImageAltTaskResult;
|
|
610
|
-
request: ImageAltRequest;
|
|
611
|
-
};
|
|
612
|
-
type WebhookTaskImageAltFailedPayload = {
|
|
613
|
-
task: {
|
|
614
|
-
id: string;
|
|
615
|
-
};
|
|
616
|
-
event: "task.image.alt.failed";
|
|
617
|
-
request: ImageAltRequest;
|
|
618
|
-
errors: TaskError[];
|
|
619
|
-
};
|
|
620
|
-
type WebhookTaskImageReviewCompletedPayload = {
|
|
621
|
-
task: WebhookTaskCompleted;
|
|
622
|
-
event: "task.image.review.completed";
|
|
623
|
-
result: ImageReviewTaskResult;
|
|
624
|
-
request: ImageReviewRequest;
|
|
625
|
-
};
|
|
626
|
-
type WebhookTaskImageReviewFailedPayload = {
|
|
627
|
-
task: {
|
|
628
|
-
id: string;
|
|
629
|
-
};
|
|
630
|
-
event: "task.image.review.failed";
|
|
631
|
-
request: ImageReviewRequest;
|
|
632
|
-
errors: TaskError[];
|
|
633
|
-
};
|
|
634
|
-
type WebhookTaskImageRemoveBackgroundCompletedPayload = {
|
|
635
|
-
task: WebhookTaskCompleted;
|
|
636
|
-
event: "task.image.remove_background.completed";
|
|
637
|
-
result: ImageRemoveBackgroundTaskResult;
|
|
638
|
-
request: ImageRemoveBackgroundRequest;
|
|
639
|
-
};
|
|
640
|
-
type WebhookTaskImageRemoveBackgroundFailedPayload = {
|
|
641
|
-
task: {
|
|
642
|
-
id: string;
|
|
643
|
-
};
|
|
644
|
-
event: "task.image.remove_background.failed";
|
|
645
|
-
request: ImageRemoveBackgroundRequest;
|
|
646
|
-
errors: TaskError[];
|
|
647
|
-
};
|
|
648
|
-
type WebhookTaskImageUpscaleCompletedPayload = {
|
|
649
|
-
task: WebhookTaskCompleted;
|
|
650
|
-
event: "task.image.upscale.completed";
|
|
651
|
-
result: ImageUpscaleTaskResult;
|
|
652
|
-
request: ImageUpscaleRequest;
|
|
653
|
-
};
|
|
654
|
-
type WebhookTaskImageUpscaleFailedPayload = {
|
|
655
|
-
task: {
|
|
656
|
-
id: string;
|
|
657
|
-
};
|
|
658
|
-
event: "task.image.upscale.failed";
|
|
659
|
-
request: ImageUpscaleRequest;
|
|
660
|
-
errors: TaskError[];
|
|
661
|
-
};
|
|
662
|
-
type WebhookTaskVideoCompletedPayload = {
|
|
663
|
-
task: WebhookTaskCompleted;
|
|
664
|
-
event: "task.video.generate.completed";
|
|
665
|
-
result: VideoResult;
|
|
666
|
-
request: VideoGenerationRequest;
|
|
667
|
-
};
|
|
668
|
-
type WebhookTaskVideoFailedPayload = {
|
|
669
|
-
task: {
|
|
670
|
-
id: string;
|
|
671
|
-
};
|
|
672
|
-
event: "task.video.generate.failed";
|
|
673
|
-
request: VideoGenerationRequest;
|
|
569
|
+
errors: null;
|
|
570
|
+
} : never;
|
|
571
|
+
type FailedTaskData<TypeT extends TaskType = TaskType> = Extract<TaskData, {
|
|
572
|
+
type: TypeT;
|
|
573
|
+
}> extends (infer DataT extends TaskData) ? DataT & {
|
|
574
|
+
status: "failed";
|
|
575
|
+
result: null;
|
|
674
576
|
errors: TaskError[];
|
|
577
|
+
} : never;
|
|
578
|
+
type WebhookEventOf<TypeT extends TaskType, StatusT extends "completed" | "failed"> = {
|
|
579
|
+
id: string;
|
|
580
|
+
type: `task.${TypeT}.${StatusT}`;
|
|
581
|
+
timestamp: string;
|
|
582
|
+
data: StatusT extends "completed" ? CompletedTaskData<TypeT> : FailedTaskData<TypeT>;
|
|
675
583
|
};
|
|
676
|
-
type
|
|
584
|
+
type WebhookEvent = { [TypeT in TaskType]: WebhookEventOf<TypeT, "completed"> | WebhookEventOf<TypeT, "failed">; }[TaskType];
|
|
585
|
+
type WebhookEventType = WebhookEvent["type"];
|
|
677
586
|
}
|
|
678
587
|
//#endregion
|
|
679
588
|
//#region src/task-result.d.ts
|
|
680
589
|
/**
|
|
681
590
|
* A completed task, as every result class is built from it. Polled task data
|
|
682
|
-
* and webhook
|
|
591
|
+
* and webhook events both come down to this shape.
|
|
683
592
|
*/
|
|
684
593
|
type MynthCompletedTask<RequestT, ResultT> = {
|
|
685
594
|
taskId: string;
|
|
@@ -907,8 +816,11 @@ type ImageUpscaleResult<MetadataT = Record<string, unknown> | undefined> = {
|
|
|
907
816
|
type MynthOutputVideo = {
|
|
908
817
|
/** Video ID */
|
|
909
818
|
id: string;
|
|
910
|
-
/**
|
|
911
|
-
|
|
819
|
+
/**
|
|
820
|
+
* Public URL of the video. `null` when it could not be delivered there.
|
|
821
|
+
* Write `url ?? mynthUrl` where any URL will do.
|
|
822
|
+
*/
|
|
823
|
+
url: string | null;
|
|
912
824
|
/** Mynth CDN URL. Always set, and served for 7 days. */
|
|
913
825
|
mynthUrl: string;
|
|
914
826
|
/** Cost charged for this video */
|
|
@@ -941,7 +853,10 @@ type VideoGenerationResult<MetadataT = Record<string, unknown> | undefined> = {
|
|
|
941
853
|
* in `videos`.
|
|
942
854
|
*/
|
|
943
855
|
failures: MynthSDKTypes.TaskError[];
|
|
944
|
-
/**
|
|
856
|
+
/**
|
|
857
|
+
* The `url` of each successful video. Skips videos whose `url` is `null`;
|
|
858
|
+
* read `videos` to reach their `mynthUrl`.
|
|
859
|
+
*/
|
|
945
860
|
urls: string[];
|
|
946
861
|
/** Metadata attached to the request */
|
|
947
862
|
metadata: MetadataT;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mynthio/sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.50",
|
|
4
4
|
"description": "Official Mynth SDK for image generation and analysis",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
"@vitest/coverage-v8": "^5.0.2",
|
|
49
49
|
"convex": "^1.46.0",
|
|
50
50
|
"publint": "^0.3.24",
|
|
51
|
+
"standardwebhooks": "^1.1.1",
|
|
51
52
|
"tsdown": "^0.23.0",
|
|
52
53
|
"typescript": "7.0.2",
|
|
53
54
|
"vitest": "^5.0.2"
|
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
//#region src/image-alt-result.ts
|
|
2
|
-
/** Map a completed image alt text task. */
|
|
3
|
-
function toImageAltResult(task) {
|
|
4
|
-
return {
|
|
5
|
-
taskId: task.taskId,
|
|
6
|
-
cost: task.cost,
|
|
7
|
-
alt: task.result.alt,
|
|
8
|
-
raw: {
|
|
9
|
-
request: task.request,
|
|
10
|
-
result: task.result
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
//#endregion
|
|
15
|
-
//#region src/output-image.ts
|
|
16
|
-
const IMAGE_MIME_TYPES = {
|
|
17
|
-
png: "image/png",
|
|
18
|
-
jpg: "image/jpeg",
|
|
19
|
-
webp: "image/webp"
|
|
20
|
-
};
|
|
21
|
-
const SIZE_PATTERN = /^(\d+)x(\d+)$/;
|
|
22
|
-
function parseSize(size) {
|
|
23
|
-
const match = SIZE_PATTERN.exec(size);
|
|
24
|
-
if (!match) throw new Error(`Unexpected image size "${size}", expected {width}x{height}`);
|
|
25
|
-
return {
|
|
26
|
-
width: Number(match[1]),
|
|
27
|
-
height: Number(match[2])
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Map an image from the API wire format.
|
|
32
|
-
*
|
|
33
|
-
* @throws {Error} If `size` is not `{width}x{height}`
|
|
34
|
-
*/
|
|
35
|
-
function toOutputImage(image) {
|
|
36
|
-
const { width, height } = parseSize(image.size);
|
|
37
|
-
return {
|
|
38
|
-
id: image.id,
|
|
39
|
-
url: image.url,
|
|
40
|
-
mynthUrl: image.mynth_url,
|
|
41
|
-
width,
|
|
42
|
-
height,
|
|
43
|
-
size: image.size,
|
|
44
|
-
format: image.format,
|
|
45
|
-
mimeType: IMAGE_MIME_TYPES[image.format],
|
|
46
|
-
destination: image.destination
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
//#endregion
|
|
50
|
-
//#region src/image-generation-result.ts
|
|
51
|
-
/** Map a completed image generation task. */
|
|
52
|
-
function toImageGenerationResult(task) {
|
|
53
|
-
const images = [];
|
|
54
|
-
const failures = [];
|
|
55
|
-
for (const image of task.result.images) if (image.status === "success") images.push({
|
|
56
|
-
...toOutputImage(image),
|
|
57
|
-
rating: image.rating
|
|
58
|
-
});
|
|
59
|
-
else failures.push(image.error);
|
|
60
|
-
return {
|
|
61
|
-
taskId: task.taskId,
|
|
62
|
-
cost: task.cost,
|
|
63
|
-
model: task.result.model,
|
|
64
|
-
images,
|
|
65
|
-
failures,
|
|
66
|
-
urls: images.flatMap((image) => image.url === null ? [] : [image.url]),
|
|
67
|
-
magicPrompt: task.result.magic_prompt,
|
|
68
|
-
metadata: task.request.metadata,
|
|
69
|
-
raw: {
|
|
70
|
-
request: task.request,
|
|
71
|
-
result: task.result
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
//#endregion
|
|
76
|
-
//#region src/image-rate-result.ts
|
|
77
|
-
/** Map a completed image content rating task. */
|
|
78
|
-
function toImageRateResult(task) {
|
|
79
|
-
return {
|
|
80
|
-
taskId: task.taskId,
|
|
81
|
-
cost: task.cost,
|
|
82
|
-
level: task.result.level,
|
|
83
|
-
raw: {
|
|
84
|
-
request: task.request,
|
|
85
|
-
result: task.result
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
//#endregion
|
|
90
|
-
//#region src/image-remove-background-result.ts
|
|
91
|
-
/** Map a completed image background removal task. */
|
|
92
|
-
function toImageRemoveBackgroundResult(task) {
|
|
93
|
-
return {
|
|
94
|
-
taskId: task.taskId,
|
|
95
|
-
cost: task.cost,
|
|
96
|
-
image: toOutputImage(task.result.image),
|
|
97
|
-
metadata: task.request.metadata,
|
|
98
|
-
raw: {
|
|
99
|
-
request: task.request,
|
|
100
|
-
result: task.result
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
//#endregion
|
|
105
|
-
//#region src/image-review-result.ts
|
|
106
|
-
/** Map a completed image quality review task. */
|
|
107
|
-
function toImageReviewResult(task) {
|
|
108
|
-
return {
|
|
109
|
-
taskId: task.taskId,
|
|
110
|
-
cost: task.cost,
|
|
111
|
-
score: task.result.score,
|
|
112
|
-
summary: task.result.summary,
|
|
113
|
-
findings: task.result.findings,
|
|
114
|
-
strengths: task.result.strengths,
|
|
115
|
-
raw: {
|
|
116
|
-
request: task.request,
|
|
117
|
-
result: task.result
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
//#endregion
|
|
122
|
-
//#region src/image-upscale-result.ts
|
|
123
|
-
/** Map a completed image upscale task. */
|
|
124
|
-
function toImageUpscaleResult(task) {
|
|
125
|
-
return {
|
|
126
|
-
taskId: task.taskId,
|
|
127
|
-
cost: task.cost,
|
|
128
|
-
image: toOutputImage(task.result.image),
|
|
129
|
-
metadata: task.request.metadata,
|
|
130
|
-
raw: {
|
|
131
|
-
request: task.request,
|
|
132
|
-
result: task.result
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
//#endregion
|
|
137
|
-
//#region src/task-result.ts
|
|
138
|
-
/**
|
|
139
|
-
* Read a completed task from polled task data.
|
|
140
|
-
*
|
|
141
|
-
* @param label - Names the task in errors, e.g. `"Image upscale"`
|
|
142
|
-
* @throws {Error} If the task has not completed, or completed without a cost
|
|
143
|
-
*/
|
|
144
|
-
function completedTaskFromData(data, label) {
|
|
145
|
-
if (data.status !== "completed" || data.result === null) throw new Error(`${label} task ${data.id} is not completed`);
|
|
146
|
-
if (data.cost === null) throw new Error(`${label} task ${data.id} is missing cost`);
|
|
147
|
-
return {
|
|
148
|
-
taskId: data.id,
|
|
149
|
-
cost: data.cost,
|
|
150
|
-
request: data.request,
|
|
151
|
-
result: data.result
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Read a completed task from a `...Completed` webhook payload.
|
|
156
|
-
*
|
|
157
|
-
* @throws {Error} If the payload has no cost
|
|
158
|
-
*/
|
|
159
|
-
function completedTaskFromWebhook(payload) {
|
|
160
|
-
if (typeof payload.task.cost !== "string") throw new Error(`Webhook for task ${payload.task.id} is missing cost`);
|
|
161
|
-
return {
|
|
162
|
-
taskId: payload.task.id,
|
|
163
|
-
cost: payload.task.cost,
|
|
164
|
-
request: payload.request,
|
|
165
|
-
result: payload.result
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Read a failed task from a `...Failed` webhook payload. Callers add
|
|
170
|
-
* `metadata` for task types whose request takes it.
|
|
171
|
-
*/
|
|
172
|
-
function taskFailureFromWebhook(payload) {
|
|
173
|
-
return {
|
|
174
|
-
taskId: payload.task.id,
|
|
175
|
-
errors: payload.errors,
|
|
176
|
-
raw: { request: payload.request }
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
//#endregion
|
|
180
|
-
//#region src/output-video.ts
|
|
181
|
-
/** Map a successful video from the API wire format. */
|
|
182
|
-
function toOutputVideo(video) {
|
|
183
|
-
return {
|
|
184
|
-
id: video.id,
|
|
185
|
-
url: video.url,
|
|
186
|
-
mynthUrl: video.mynth_url,
|
|
187
|
-
cost: video.cost,
|
|
188
|
-
duration: video.duration,
|
|
189
|
-
resolution: video.resolution,
|
|
190
|
-
audio: video.audio
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
//#endregion
|
|
194
|
-
//#region src/video-generation-result.ts
|
|
195
|
-
/** Map a completed video generation task. */
|
|
196
|
-
function toVideoGenerationResult(task) {
|
|
197
|
-
const videos = [];
|
|
198
|
-
const failures = [];
|
|
199
|
-
for (const video of task.result.videos) if (video.status === "success") videos.push(toOutputVideo(video));
|
|
200
|
-
else failures.push(video.error);
|
|
201
|
-
return {
|
|
202
|
-
taskId: task.taskId,
|
|
203
|
-
cost: task.cost,
|
|
204
|
-
model: task.result.model,
|
|
205
|
-
videos,
|
|
206
|
-
failures,
|
|
207
|
-
urls: videos.map((video) => video.url),
|
|
208
|
-
metadata: task.request.metadata,
|
|
209
|
-
raw: {
|
|
210
|
-
request: task.request,
|
|
211
|
-
result: task.result
|
|
212
|
-
}
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
//#endregion
|
|
216
|
-
export { toImageUpscaleResult as a, toImageRateResult as c, taskFailureFromWebhook as i, toImageGenerationResult as l, completedTaskFromData as n, toImageReviewResult as o, completedTaskFromWebhook as r, toImageRemoveBackgroundResult as s, toVideoGenerationResult as t, toImageAltResult as u };
|