@mynthio/sdk 0.0.48 → 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 +242 -107
- package/dist/convex/index.d.ts +8 -7
- package/dist/convex/index.js +6 -6
- package/dist/handler.d.ts +40 -6
- package/dist/handler.js +69 -89
- package/dist/index.d.ts +65 -226
- package/dist/index.js +100 -295
- 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/{types.d.ts → video-generation-result.d.ts} +349 -161
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { _ as toImageAltResult, a as parseWebhookEvent, c as toVideoGenerationResult, f as toImageUpscaleResult, g as toImageGenerationResult, h as toImageRateResult, i as getWebhookSecretFromEnv, l as completedTaskFromData, m as toImageRemoveBackgroundResult, n as WEBHOOK_SIGNATURE_HEADER, o as readHeader, p as toImageReviewResult, r as WEBHOOK_TIMESTAMP_HEADER, s as verifySignature, t as WEBHOOK_ID_HEADER } from "./utils.js";
|
|
1
2
|
//#region src/constants.ts
|
|
2
3
|
/** Base URL for the Mynth API */
|
|
3
4
|
const API_URL = "https://api.mynth.io";
|
|
@@ -399,11 +400,11 @@ const AVAILABLE_VIDEO_MODELS = [
|
|
|
399
400
|
var MynthAPIError = class extends Error {
|
|
400
401
|
/** HTTP status code of the failed request */
|
|
401
402
|
status;
|
|
402
|
-
/** Error code from the API response, if available */
|
|
403
|
+
/** Error code from the API response, if available, e.g. `insufficient_balance` */
|
|
403
404
|
code;
|
|
404
405
|
/**
|
|
405
|
-
* One entry per invalid field when `code` is `
|
|
406
|
-
*
|
|
406
|
+
* One entry per invalid or unknown field when `code` is `validation_error`.
|
|
407
|
+
* `message` already states all of them.
|
|
407
408
|
*/
|
|
408
409
|
issues;
|
|
409
410
|
constructor(message, status, code, issues) {
|
|
@@ -415,8 +416,9 @@ var MynthAPIError = class extends Error {
|
|
|
415
416
|
}
|
|
416
417
|
};
|
|
417
418
|
function createApiError(data, status) {
|
|
418
|
-
const
|
|
419
|
-
|
|
419
|
+
const body = typeof data === "object" && data !== null ? data : {};
|
|
420
|
+
const error = typeof body.error === "object" && body.error !== null ? body.error : {};
|
|
421
|
+
return new MynthAPIError(typeof error.message === "string" && error.message || `Request failed with status ${status}`, status, typeof error.code === "string" ? error.code : void 0, Array.isArray(error.issues) ? error.issues : void 0);
|
|
420
422
|
}
|
|
421
423
|
/** An error body that is not JSON (a proxy's HTML page, say) still yields an API error. */
|
|
422
424
|
async function readErrorBody(response) {
|
|
@@ -483,221 +485,6 @@ var MynthClient = class {
|
|
|
483
485
|
}
|
|
484
486
|
};
|
|
485
487
|
//#endregion
|
|
486
|
-
//#region src/image-alt-result.ts
|
|
487
|
-
/**
|
|
488
|
-
* Represents the result of a completed image alt text task.
|
|
489
|
-
*/
|
|
490
|
-
var ImageAltResult = class ImageAltResult {
|
|
491
|
-
/** The task ID created for this alt text request */
|
|
492
|
-
taskId;
|
|
493
|
-
/** Cost charged for the completed task */
|
|
494
|
-
cost;
|
|
495
|
-
/** Generated alt text */
|
|
496
|
-
alt;
|
|
497
|
-
constructor(data) {
|
|
498
|
-
this.taskId = data.taskId;
|
|
499
|
-
this.cost = data.cost;
|
|
500
|
-
this.alt = data.alt;
|
|
501
|
-
}
|
|
502
|
-
static fromTaskData(data) {
|
|
503
|
-
if (data.status !== "completed" || data.result === null) throw new Error(`Image alt task ${data.id} is not completed`);
|
|
504
|
-
if (data.cost === null) throw new Error(`Image alt task ${data.id} is missing cost`);
|
|
505
|
-
return new ImageAltResult({
|
|
506
|
-
taskId: data.id,
|
|
507
|
-
cost: data.cost,
|
|
508
|
-
alt: data.result.alt
|
|
509
|
-
});
|
|
510
|
-
}
|
|
511
|
-
};
|
|
512
|
-
//#endregion
|
|
513
|
-
//#region src/image-generation-result.ts
|
|
514
|
-
/**
|
|
515
|
-
* Represents a completed image generation task.
|
|
516
|
-
*
|
|
517
|
-
* @template MetadataT - Type of the metadata attached to the request
|
|
518
|
-
* @template RatingT - Type of the rating response
|
|
519
|
-
*/
|
|
520
|
-
var ImageGenerationResult = class {
|
|
521
|
-
/** Raw task data from the API */
|
|
522
|
-
data;
|
|
523
|
-
constructor(data) {
|
|
524
|
-
this.data = data;
|
|
525
|
-
}
|
|
526
|
-
/** Unique identifier for this task */
|
|
527
|
-
get id() {
|
|
528
|
-
return this.data.id;
|
|
529
|
-
}
|
|
530
|
-
/** Current status of the task */
|
|
531
|
-
get status() {
|
|
532
|
-
return this.data.status;
|
|
533
|
-
}
|
|
534
|
-
/**
|
|
535
|
-
* The generation result: images, the format each one was delivered in, and the model.
|
|
536
|
-
* Returns `null` if the task hasn't completed yet.
|
|
537
|
-
*/
|
|
538
|
-
get result() {
|
|
539
|
-
return this.data.result;
|
|
540
|
-
}
|
|
541
|
-
/** Whether the task completed successfully */
|
|
542
|
-
get isCompleted() {
|
|
543
|
-
return this.data.status === "completed";
|
|
544
|
-
}
|
|
545
|
-
/** Whether the task failed */
|
|
546
|
-
get isFailed() {
|
|
547
|
-
return this.data.status === "failed";
|
|
548
|
-
}
|
|
549
|
-
/**
|
|
550
|
-
* Get all successfully generated image URLs.
|
|
551
|
-
* Convenience method that extracts just the URLs from successful images.
|
|
552
|
-
* Images delivered only to a user destination may have a `null` url and are omitted here;
|
|
553
|
-
* use `getImages()` and read `mynth_url` to access the CDN URL directly.
|
|
554
|
-
*/
|
|
555
|
-
get urls() {
|
|
556
|
-
return this.data.result?.images.filter((img) => img.status === "success").map((img) => img.url).filter((url) => url !== null) ?? [];
|
|
557
|
-
}
|
|
558
|
-
getImages(options = {}) {
|
|
559
|
-
if (options.includeFailed) return this.data.result?.images ?? [];
|
|
560
|
-
return this.data.result?.images.filter((image) => image.status === "success") ?? [];
|
|
561
|
-
}
|
|
562
|
-
/**
|
|
563
|
-
* Get the metadata that was attached to the generation request.
|
|
564
|
-
*/
|
|
565
|
-
getMetadata() {
|
|
566
|
-
return this.data.request?.metadata;
|
|
567
|
-
}
|
|
568
|
-
};
|
|
569
|
-
//#endregion
|
|
570
|
-
//#region src/image-rate-result.ts
|
|
571
|
-
/**
|
|
572
|
-
* Represents the result of a completed image content rating task.
|
|
573
|
-
*
|
|
574
|
-
* @template LevelT - Union of possible rating level strings (e.g. `"sfw" | "nsfw"`)
|
|
575
|
-
*/
|
|
576
|
-
var ImageRateResult = class ImageRateResult {
|
|
577
|
-
/** The task ID created for this rating request */
|
|
578
|
-
taskId;
|
|
579
|
-
/** Cost charged for the completed task */
|
|
580
|
-
cost;
|
|
581
|
-
/** Assigned rating level */
|
|
582
|
-
level;
|
|
583
|
-
constructor(data) {
|
|
584
|
-
this.taskId = data.taskId;
|
|
585
|
-
this.cost = data.cost;
|
|
586
|
-
this.level = data.level;
|
|
587
|
-
}
|
|
588
|
-
static fromTaskData(data) {
|
|
589
|
-
if (data.status !== "completed" || data.result === null) throw new Error(`Image rate task ${data.id} is not completed`);
|
|
590
|
-
if (data.cost === null) throw new Error(`Image rate task ${data.id} is missing cost`);
|
|
591
|
-
return new ImageRateResult({
|
|
592
|
-
taskId: data.id,
|
|
593
|
-
cost: data.cost,
|
|
594
|
-
level: data.result.level
|
|
595
|
-
});
|
|
596
|
-
}
|
|
597
|
-
};
|
|
598
|
-
//#endregion
|
|
599
|
-
//#region src/image-remove-background-result.ts
|
|
600
|
-
/**
|
|
601
|
-
* Represents the result of a completed image background removal task.
|
|
602
|
-
*
|
|
603
|
-
* @template MetadataT - Type of the metadata attached to the request
|
|
604
|
-
*/
|
|
605
|
-
var ImageRemoveBackgroundResult = class ImageRemoveBackgroundResult {
|
|
606
|
-
/** The task ID created for this request */
|
|
607
|
-
taskId;
|
|
608
|
-
/** Cost charged for the completed task */
|
|
609
|
-
cost;
|
|
610
|
-
/** The image with its background removed */
|
|
611
|
-
image;
|
|
612
|
-
/** Metadata attached to the request */
|
|
613
|
-
metadata;
|
|
614
|
-
constructor(data) {
|
|
615
|
-
this.taskId = data.taskId;
|
|
616
|
-
this.cost = data.cost;
|
|
617
|
-
this.image = data.image;
|
|
618
|
-
this.metadata = data.metadata;
|
|
619
|
-
}
|
|
620
|
-
static fromTaskData(data) {
|
|
621
|
-
if (data.status !== "completed" || data.result === null) throw new Error(`Image remove background task ${data.id} is not completed`);
|
|
622
|
-
if (data.cost === null) throw new Error(`Image remove background task ${data.id} is missing cost`);
|
|
623
|
-
return new ImageRemoveBackgroundResult({
|
|
624
|
-
taskId: data.id,
|
|
625
|
-
cost: data.cost,
|
|
626
|
-
image: data.result.image,
|
|
627
|
-
metadata: data.request.metadata
|
|
628
|
-
});
|
|
629
|
-
}
|
|
630
|
-
};
|
|
631
|
-
//#endregion
|
|
632
|
-
//#region src/image-review-result.ts
|
|
633
|
-
/**
|
|
634
|
-
* Represents the result of a completed image quality review task.
|
|
635
|
-
*/
|
|
636
|
-
var ImageReviewResult = class ImageReviewResult {
|
|
637
|
-
/** The task ID created for this review request */
|
|
638
|
-
taskId;
|
|
639
|
-
/** Cost charged for the completed task */
|
|
640
|
-
cost;
|
|
641
|
-
/** Median reviewer score from 1 to 4. Higher is better. */
|
|
642
|
-
score;
|
|
643
|
-
/** Review summary */
|
|
644
|
-
summary;
|
|
645
|
-
/** Defects found by the reviewer panel */
|
|
646
|
-
findings;
|
|
647
|
-
/** Strengths identified by the reviewer panel */
|
|
648
|
-
strengths;
|
|
649
|
-
constructor(data) {
|
|
650
|
-
this.taskId = data.taskId;
|
|
651
|
-
this.cost = data.cost;
|
|
652
|
-
this.score = data.score;
|
|
653
|
-
this.summary = data.summary;
|
|
654
|
-
this.findings = data.findings;
|
|
655
|
-
this.strengths = data.strengths;
|
|
656
|
-
}
|
|
657
|
-
static fromTaskData(data) {
|
|
658
|
-
if (data.status !== "completed" || data.result === null) throw new Error(`Image review task ${data.id} is not completed`);
|
|
659
|
-
if (data.cost === null) throw new Error(`Image review task ${data.id} is missing cost`);
|
|
660
|
-
return new ImageReviewResult({
|
|
661
|
-
taskId: data.id,
|
|
662
|
-
cost: data.cost,
|
|
663
|
-
...data.result
|
|
664
|
-
});
|
|
665
|
-
}
|
|
666
|
-
};
|
|
667
|
-
//#endregion
|
|
668
|
-
//#region src/image-upscale-result.ts
|
|
669
|
-
/**
|
|
670
|
-
* Represents the result of a completed image upscale task.
|
|
671
|
-
*
|
|
672
|
-
* @template MetadataT - Type of the metadata attached to the request
|
|
673
|
-
*/
|
|
674
|
-
var ImageUpscaleResult = class ImageUpscaleResult {
|
|
675
|
-
/** The task ID created for this request */
|
|
676
|
-
taskId;
|
|
677
|
-
/** Cost charged for the completed task */
|
|
678
|
-
cost;
|
|
679
|
-
/** The upscaled image */
|
|
680
|
-
image;
|
|
681
|
-
/** Metadata attached to the request */
|
|
682
|
-
metadata;
|
|
683
|
-
constructor(data) {
|
|
684
|
-
this.taskId = data.taskId;
|
|
685
|
-
this.cost = data.cost;
|
|
686
|
-
this.image = data.image;
|
|
687
|
-
this.metadata = data.metadata;
|
|
688
|
-
}
|
|
689
|
-
static fromTaskData(data) {
|
|
690
|
-
if (data.status !== "completed" || data.result === null) throw new Error(`Image upscale task ${data.id} is not completed`);
|
|
691
|
-
if (data.cost === null) throw new Error(`Image upscale task ${data.id} is missing cost`);
|
|
692
|
-
return new ImageUpscaleResult({
|
|
693
|
-
taskId: data.id,
|
|
694
|
-
cost: data.cost,
|
|
695
|
-
image: data.result.image,
|
|
696
|
-
metadata: data.request.metadata
|
|
697
|
-
});
|
|
698
|
-
}
|
|
699
|
-
};
|
|
700
|
-
//#endregion
|
|
701
488
|
//#region src/task-async.ts
|
|
702
489
|
const POLLING_TIMEOUT_MS = 18e5;
|
|
703
490
|
const FAST_POLLING_DURATION_MS = 12e3;
|
|
@@ -1039,59 +826,66 @@ async function resolveInputs(client, inputs, { signal } = {}) {
|
|
|
1039
826
|
});
|
|
1040
827
|
}
|
|
1041
828
|
//#endregion
|
|
1042
|
-
//#region src/
|
|
829
|
+
//#region src/webhooks/verify.ts
|
|
830
|
+
/** A webhook request failed verification: unsigned, wrongly signed, stale, or not an event. */
|
|
831
|
+
var MynthWebhookVerificationError = class extends Error {
|
|
832
|
+
constructor(message) {
|
|
833
|
+
super(message);
|
|
834
|
+
this.name = "MynthWebhookVerificationError";
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
function toBytes(body) {
|
|
838
|
+
if (typeof body === "string") return new TextEncoder().encode(body);
|
|
839
|
+
return body instanceof Uint8Array ? body : new Uint8Array(body);
|
|
840
|
+
}
|
|
1043
841
|
/**
|
|
1044
|
-
*
|
|
842
|
+
* Verify a signed Mynth webhook and return its event.
|
|
843
|
+
*
|
|
844
|
+
* Checks the Standard Webhooks signature over `webhook-id`, `webhook-timestamp`
|
|
845
|
+
* and the raw body, rejects a timestamp more than five minutes off, and checks
|
|
846
|
+
* that the body's `id` is the `webhook-id`. Deduplicate on `event.id`: every
|
|
847
|
+
* delivery of the event repeats it.
|
|
848
|
+
*
|
|
849
|
+
* An event type newer than this SDK is returned as is, so keep a `default`
|
|
850
|
+
* branch when you switch on `event.type`.
|
|
851
|
+
*
|
|
852
|
+
* @param secret - The webhook's `whsec_` secret. Defaults to MYNTH_WEBHOOK_SECRET.
|
|
853
|
+
* @throws {MynthWebhookVerificationError} If the request is not a valid, signed event
|
|
854
|
+
* @throws {Error} If there is no secret, or it is not a `whsec_` secret
|
|
855
|
+
*
|
|
856
|
+
* @example
|
|
857
|
+
* ```typescript
|
|
858
|
+
* const event = await verifyWebhook({ body: await request.text(), headers: request.headers });
|
|
1045
859
|
*
|
|
1046
|
-
*
|
|
860
|
+
* switch (event.type) {
|
|
861
|
+
* case "task.image.generate.completed":
|
|
862
|
+
* console.log(event.data.result.images);
|
|
863
|
+
* break;
|
|
864
|
+
* }
|
|
865
|
+
* ```
|
|
1047
866
|
*/
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
get isCompleted() {
|
|
1071
|
-
return this.data.status === "completed";
|
|
1072
|
-
}
|
|
1073
|
-
/** Whether the task failed */
|
|
1074
|
-
get isFailed() {
|
|
1075
|
-
return this.data.status === "failed";
|
|
1076
|
-
}
|
|
1077
|
-
/**
|
|
1078
|
-
* Get all successfully generated video URLs.
|
|
1079
|
-
* Convenience method that extracts just the URLs from successful videos.
|
|
1080
|
-
*/
|
|
1081
|
-
get urls() {
|
|
1082
|
-
return this.getVideos().map((video) => video.url);
|
|
1083
|
-
}
|
|
1084
|
-
getVideos(options = {}) {
|
|
1085
|
-
if (options.includeFailed) return this.data.result?.videos ?? [];
|
|
1086
|
-
return this.data.result?.videos.filter((video) => video.status === "success") ?? [];
|
|
1087
|
-
}
|
|
1088
|
-
/**
|
|
1089
|
-
* Get the metadata that was attached to the generation request.
|
|
1090
|
-
*/
|
|
1091
|
-
getMetadata() {
|
|
1092
|
-
return this.data.request?.metadata;
|
|
1093
|
-
}
|
|
1094
|
-
};
|
|
867
|
+
async function verifyWebhook(request, secret = getWebhookSecretFromEnv()) {
|
|
868
|
+
if (!secret) throw new Error("MYNTH_WEBHOOK_SECRET is required. Either pass the secret or set the environment variable.");
|
|
869
|
+
const id = readHeader(request.headers, WEBHOOK_ID_HEADER);
|
|
870
|
+
const timestamp = readHeader(request.headers, WEBHOOK_TIMESTAMP_HEADER);
|
|
871
|
+
const signature = readHeader(request.headers, WEBHOOK_SIGNATURE_HEADER);
|
|
872
|
+
if (!id || !timestamp || !signature) throw new MynthWebhookVerificationError(`Missing ${[
|
|
873
|
+
!id && "webhook-id",
|
|
874
|
+
!timestamp && "webhook-timestamp",
|
|
875
|
+
!signature && "webhook-signature"
|
|
876
|
+
].filter(Boolean).join(", ")} header. Only registered webhooks are signed.`);
|
|
877
|
+
const body = toBytes(request.body);
|
|
878
|
+
if (!await verifySignature({
|
|
879
|
+
id,
|
|
880
|
+
timestamp,
|
|
881
|
+
signature,
|
|
882
|
+
body,
|
|
883
|
+
secret
|
|
884
|
+
})) throw new MynthWebhookVerificationError("The webhook signature does not match, or its timestamp is more than five minutes off.");
|
|
885
|
+
const event = parseWebhookEvent(body, id);
|
|
886
|
+
if (!event) throw new MynthWebhookVerificationError("The webhook body is not a Mynth event for this webhook-id.");
|
|
887
|
+
return event;
|
|
888
|
+
}
|
|
1095
889
|
//#endregion
|
|
1096
890
|
//#region src/index.ts
|
|
1097
891
|
/**
|
|
@@ -1196,6 +990,7 @@ var MynthImage = class {
|
|
|
1196
990
|
* ```typescript
|
|
1197
991
|
* const taskAsync = await image.generateAsync({
|
|
1198
992
|
* prompt: "A futuristic cityscape",
|
|
993
|
+
* generate_public_access_token: true,
|
|
1199
994
|
* });
|
|
1200
995
|
*
|
|
1201
996
|
* return { id: taskAsync.id, access: taskAsync.access };
|
|
@@ -1211,10 +1006,10 @@ var MynthImage = class {
|
|
|
1211
1006
|
inputs,
|
|
1212
1007
|
destination: request.destination ?? this.defaultDestination
|
|
1213
1008
|
}, { signal })).data;
|
|
1214
|
-
return new TaskAsync(data.
|
|
1009
|
+
return new TaskAsync(data.task_id, {
|
|
1215
1010
|
client: this.client,
|
|
1216
|
-
pat: data.
|
|
1217
|
-
resultFactory: (data) =>
|
|
1011
|
+
pat: data.public_access_token,
|
|
1012
|
+
resultFactory: (data) => toImageGenerationResult(completedTaskFromData(data, "Image generation"))
|
|
1218
1013
|
});
|
|
1219
1014
|
}
|
|
1220
1015
|
/**
|
|
@@ -1281,9 +1076,9 @@ var MynthImage = class {
|
|
|
1281
1076
|
...rest,
|
|
1282
1077
|
url
|
|
1283
1078
|
})).data;
|
|
1284
|
-
return new TaskAsync(data.
|
|
1079
|
+
return new TaskAsync(data.task_id, {
|
|
1285
1080
|
client: this.client,
|
|
1286
|
-
resultFactory: (taskData) =>
|
|
1081
|
+
resultFactory: (taskData) => toImageRateResult(completedTaskFromData(taskData, "Image rate"))
|
|
1287
1082
|
});
|
|
1288
1083
|
}
|
|
1289
1084
|
/**
|
|
@@ -1323,9 +1118,9 @@ var MynthImage = class {
|
|
|
1323
1118
|
async createAltTask(request) {
|
|
1324
1119
|
const url = await this.resolveUrlOrFile(request);
|
|
1325
1120
|
const data = (await this.client.post(ALT_IMAGE_PATH, { url })).data;
|
|
1326
|
-
return new TaskAsync(data.
|
|
1121
|
+
return new TaskAsync(data.task_id, {
|
|
1327
1122
|
client: this.client,
|
|
1328
|
-
resultFactory: (taskData) =>
|
|
1123
|
+
resultFactory: (taskData) => toImageAltResult(completedTaskFromData(taskData, "Image alt"))
|
|
1329
1124
|
});
|
|
1330
1125
|
}
|
|
1331
1126
|
/**
|
|
@@ -1360,9 +1155,9 @@ var MynthImage = class {
|
|
|
1360
1155
|
...rest,
|
|
1361
1156
|
url
|
|
1362
1157
|
})).data;
|
|
1363
|
-
return new TaskAsync(data.
|
|
1158
|
+
return new TaskAsync(data.task_id, {
|
|
1364
1159
|
client: this.client,
|
|
1365
|
-
resultFactory: (taskData) =>
|
|
1160
|
+
resultFactory: (taskData) => toImageReviewResult(completedTaskFromData(taskData, "Image review"))
|
|
1366
1161
|
});
|
|
1367
1162
|
}
|
|
1368
1163
|
/**
|
|
@@ -1391,7 +1186,10 @@ var MynthImage = class {
|
|
|
1391
1186
|
*
|
|
1392
1187
|
* @example
|
|
1393
1188
|
* ```typescript
|
|
1394
|
-
* const taskAsync = await image.removeBackgroundAsync({
|
|
1189
|
+
* const taskAsync = await image.removeBackgroundAsync({
|
|
1190
|
+
* url: "https://...",
|
|
1191
|
+
* generate_public_access_token: true,
|
|
1192
|
+
* });
|
|
1395
1193
|
*
|
|
1396
1194
|
* return { id: taskAsync.id, access: taskAsync.access };
|
|
1397
1195
|
* ```
|
|
@@ -1407,10 +1205,10 @@ var MynthImage = class {
|
|
|
1407
1205
|
url,
|
|
1408
1206
|
destination: request.destination ?? this.defaultDestination
|
|
1409
1207
|
})).data;
|
|
1410
|
-
return new TaskAsync(data.
|
|
1208
|
+
return new TaskAsync(data.task_id, {
|
|
1411
1209
|
client: this.client,
|
|
1412
|
-
pat: data.
|
|
1413
|
-
resultFactory: (taskData) =>
|
|
1210
|
+
pat: data.public_access_token,
|
|
1211
|
+
resultFactory: (taskData) => toImageRemoveBackgroundResult(completedTaskFromData(taskData, "Image remove background"))
|
|
1414
1212
|
});
|
|
1415
1213
|
}
|
|
1416
1214
|
/**
|
|
@@ -1418,7 +1216,7 @@ var MynthImage = class {
|
|
|
1418
1216
|
*
|
|
1419
1217
|
* Mynth picks the model. `effort` sets the price, so it has no default. The
|
|
1420
1218
|
* upscaled image can be at most 4096x4096 pixels; a larger request fails
|
|
1421
|
-
* with `
|
|
1219
|
+
* with `output_too_large` and is not charged.
|
|
1422
1220
|
*
|
|
1423
1221
|
* @param request - Image URL or local file, size, and effort, plus optional output, destination, webhook, and metadata
|
|
1424
1222
|
* @returns An ImageUpscaleResult with the enlarged image
|
|
@@ -1440,7 +1238,12 @@ var MynthImage = class {
|
|
|
1440
1238
|
*
|
|
1441
1239
|
* @example
|
|
1442
1240
|
* ```typescript
|
|
1443
|
-
* const taskAsync = await image.upscaleAsync({
|
|
1241
|
+
* const taskAsync = await image.upscaleAsync({
|
|
1242
|
+
* url: "https://...",
|
|
1243
|
+
* size: "2x",
|
|
1244
|
+
* effort: "low",
|
|
1245
|
+
* generate_public_access_token: true,
|
|
1246
|
+
* });
|
|
1444
1247
|
*
|
|
1445
1248
|
* return { id: taskAsync.id, access: taskAsync.access };
|
|
1446
1249
|
* ```
|
|
@@ -1456,10 +1259,10 @@ var MynthImage = class {
|
|
|
1456
1259
|
url,
|
|
1457
1260
|
destination: request.destination ?? this.defaultDestination
|
|
1458
1261
|
})).data;
|
|
1459
|
-
return new TaskAsync(data.
|
|
1262
|
+
return new TaskAsync(data.task_id, {
|
|
1460
1263
|
client: this.client,
|
|
1461
|
-
pat: data.
|
|
1462
|
-
resultFactory: (taskData) =>
|
|
1264
|
+
pat: data.public_access_token,
|
|
1265
|
+
resultFactory: (taskData) => toImageUpscaleResult(completedTaskFromData(taskData, "Image upscale"))
|
|
1463
1266
|
});
|
|
1464
1267
|
}
|
|
1465
1268
|
};
|
|
@@ -1528,7 +1331,8 @@ var MynthVideo = class {
|
|
|
1528
1331
|
* Start video generation without waiting for completion.
|
|
1529
1332
|
*
|
|
1530
1333
|
* The preferred entry point for server code: hand `taskAsync.id` and
|
|
1531
|
-
* `taskAsync.access.publicAccessToken` to the browser
|
|
1334
|
+
* `taskAsync.access.publicAccessToken` to the browser (pass
|
|
1335
|
+
* `generate_public_access_token: true` to get one), or register a webhook,
|
|
1532
1336
|
* instead of holding a connection open for the length of a render.
|
|
1533
1337
|
*
|
|
1534
1338
|
* @param request - Video generation request parameters
|
|
@@ -1539,6 +1343,7 @@ var MynthVideo = class {
|
|
|
1539
1343
|
* const taskAsync = await video.generateAsync({
|
|
1540
1344
|
* model: "prunaai/p-video",
|
|
1541
1345
|
* prompt: "A neon city street in the rain",
|
|
1346
|
+
* generate_public_access_token: true,
|
|
1542
1347
|
* });
|
|
1543
1348
|
*
|
|
1544
1349
|
* return { id: taskAsync.id, access: taskAsync.access };
|
|
@@ -1569,11 +1374,11 @@ var MynthVideo = class {
|
|
|
1569
1374
|
* video pins a concrete model, the returned estimate is exact.
|
|
1570
1375
|
*
|
|
1571
1376
|
* @param request - The same request you would pass to `generate()`
|
|
1572
|
-
* @returns The estimated cost in USD
|
|
1377
|
+
* @returns The estimated cost in USD, as the API returns it
|
|
1573
1378
|
*
|
|
1574
1379
|
* @example
|
|
1575
1380
|
* ```typescript
|
|
1576
|
-
* const {
|
|
1381
|
+
* const { estimated_cost } = await video.estimate({
|
|
1577
1382
|
* model: "google/gemini-omni-flash-1.1",
|
|
1578
1383
|
* prompt: "A timelapse of clouds over a canyon",
|
|
1579
1384
|
* duration: 10,
|
|
@@ -1594,11 +1399,11 @@ var MynthVideo = class {
|
|
|
1594
1399
|
}
|
|
1595
1400
|
async createGenerationTask(request) {
|
|
1596
1401
|
const data = (await this.client.post(GENERATE_VIDEO_PATH, await this.toRequestBody(request))).data;
|
|
1597
|
-
return new TaskAsync(data.
|
|
1402
|
+
return new TaskAsync(data.task_id, {
|
|
1598
1403
|
client: this.client,
|
|
1599
|
-
pat: data.
|
|
1404
|
+
pat: data.public_access_token,
|
|
1600
1405
|
polling: VIDEO_POLLING,
|
|
1601
|
-
resultFactory: (taskData) =>
|
|
1406
|
+
resultFactory: (taskData) => toVideoGenerationResult(completedTaskFromData(taskData, "Video generation"))
|
|
1602
1407
|
});
|
|
1603
1408
|
}
|
|
1604
1409
|
};
|
|
@@ -1675,4 +1480,4 @@ var Mynth = class {
|
|
|
1675
1480
|
}
|
|
1676
1481
|
};
|
|
1677
1482
|
//#endregion
|
|
1678
|
-
export { AVAILABLE_MODELS, AVAILABLE_VIDEO_MODELS,
|
|
1483
|
+
export { AVAILABLE_MODELS, AVAILABLE_VIDEO_MODELS, Mynth, Mynth as default, MynthAPIError, MynthImage, MynthModels, MynthVideo, MynthWebhookVerificationError, TaskAsync, TaskAsyncFetchError, TaskAsyncTaskFailedError, TaskAsyncTaskFetchError, TaskAsyncTimeoutError, TaskAsyncUnauthorizedError, verifyWebhook };
|
package/dist/next/index.d.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
+
import { p as MynthSDKTypes } from "../video-generation-result.js";
|
|
1
2
|
import { n as WebhookHandlerOptions, t as WebhookEventHandlers } from "../handler.js";
|
|
2
3
|
//#region src/next/index.d.ts
|
|
3
4
|
export type MynthWebhookHandlerContext = {
|
|
4
5
|
/** The original request received by the Next.js Route Handler. */
|
|
5
6
|
request: Request;
|
|
6
7
|
/**
|
|
7
|
-
* The `
|
|
8
|
-
* it and skip IDs you have already handled.
|
|
8
|
+
* The verified event. `event.id` is the `webhook-id`: every delivery of the
|
|
9
|
+
* event repeats it, so store it and skip IDs you have already handled.
|
|
9
10
|
*/
|
|
10
|
-
|
|
11
|
+
event: MynthSDKTypes.WebhookEvent;
|
|
11
12
|
};
|
|
12
13
|
export type MynthWebhookEventHandlers = WebhookEventHandlers<MynthWebhookHandlerContext>;
|
|
13
14
|
export type MynthWebhookHandlerOptions = WebhookHandlerOptions;
|
|
14
15
|
/**
|
|
15
|
-
* Create a Next.js App Router handler for
|
|
16
|
+
* Create a Next.js App Router handler for Mynth webhooks.
|
|
16
17
|
*
|
|
17
18
|
* @example
|
|
18
19
|
* ```ts
|
|
@@ -20,8 +21,8 @@ export type MynthWebhookHandlerOptions = WebhookHandlerOptions;
|
|
|
20
21
|
* import { mynthWebhookHandler } from "@mynthio/sdk/next";
|
|
21
22
|
*
|
|
22
23
|
* export const POST = mynthWebhookHandler({
|
|
23
|
-
* imageTaskCompleted: async (
|
|
24
|
-
* console.log(
|
|
24
|
+
* imageTaskCompleted: async (result, { event }) => {
|
|
25
|
+
* console.log(event.id, result.taskId);
|
|
25
26
|
* },
|
|
26
27
|
* });
|
|
27
28
|
* ```
|
package/dist/next/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { t as handleWebhookRequest } from "../handler.js";
|
|
2
2
|
//#region src/next/index.ts
|
|
3
3
|
/**
|
|
4
|
-
* Create a Next.js App Router handler for
|
|
4
|
+
* Create a Next.js App Router handler for Mynth webhooks.
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* ```ts
|
|
@@ -9,16 +9,16 @@ import { t as handleWebhookRequest } from "../handler.js";
|
|
|
9
9
|
* import { mynthWebhookHandler } from "@mynthio/sdk/next";
|
|
10
10
|
*
|
|
11
11
|
* export const POST = mynthWebhookHandler({
|
|
12
|
-
* imageTaskCompleted: async (
|
|
13
|
-
* console.log(
|
|
12
|
+
* imageTaskCompleted: async (result, { event }) => {
|
|
13
|
+
* console.log(event.id, result.taskId);
|
|
14
14
|
* },
|
|
15
15
|
* });
|
|
16
16
|
* ```
|
|
17
17
|
*/
|
|
18
18
|
function mynthWebhookHandler(eventHandlers, options = {}) {
|
|
19
|
-
return (request) => handleWebhookRequest(request, eventHandlers, (
|
|
19
|
+
return (request) => handleWebhookRequest(request, eventHandlers, (event) => ({
|
|
20
20
|
request,
|
|
21
|
-
|
|
21
|
+
event
|
|
22
22
|
}), options);
|
|
23
23
|
}
|
|
24
24
|
//#endregion
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { p as MynthSDKTypes } from "../video-generation-result.js";
|
|
1
2
|
import { n as WebhookHandlerOptions, t as WebhookEventHandlers } from "../handler.js";
|
|
2
3
|
//#region src/tanstack-start/index.d.ts
|
|
3
4
|
export type MynthTanStackStartHandlerContext<TContext = unknown, TParams extends Record<string, string> = Record<string, string>> = {
|
|
@@ -8,16 +9,16 @@ export type MynthTanStackStartHandlerContext<TContext = unknown, TParams extends
|
|
|
8
9
|
/** Context provided by TanStack Start request middleware. */
|
|
9
10
|
context: TContext;
|
|
10
11
|
};
|
|
11
|
-
/** What event callbacks receive: the route context plus the
|
|
12
|
+
/** What event callbacks receive: the route context plus the verified event. */
|
|
12
13
|
export type MynthTanStackStartEventContext<TContext = unknown, TParams extends Record<string, string> = Record<string, string>> = MynthTanStackStartHandlerContext<TContext, TParams> & {
|
|
13
14
|
/**
|
|
14
|
-
* The `
|
|
15
|
-
* it and skip IDs you have already handled.
|
|
15
|
+
* The verified event. `event.id` is the `webhook-id`: every delivery of the
|
|
16
|
+
* event repeats it, so store it and skip IDs you have already handled.
|
|
16
17
|
*/
|
|
17
|
-
|
|
18
|
+
event: MynthSDKTypes.WebhookEvent;
|
|
18
19
|
};
|
|
19
20
|
export type MynthTanStackStartEventHandlers<TContext = unknown, TParams extends Record<string, string> = Record<string, string>> = WebhookEventHandlers<MynthTanStackStartEventContext<TContext, TParams>>;
|
|
20
21
|
export type MynthTanStackStartHandlerOptions = WebhookHandlerOptions;
|
|
21
|
-
/** Create a TanStack Start server route handler for
|
|
22
|
+
/** Create a TanStack Start server route handler for Mynth webhooks. */
|
|
22
23
|
export declare function mynthWebhookHandler<TContext = unknown, TParams extends Record<string, string> = Record<string, string>>(eventHandlers: MynthTanStackStartEventHandlers<TContext, TParams>, options?: MynthTanStackStartHandlerOptions): (context: MynthTanStackStartHandlerContext<TContext, TParams>) => Promise<Response>;
|
|
23
24
|
//#endregion
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { t as handleWebhookRequest } from "../handler.js";
|
|
2
2
|
//#region src/tanstack-start/index.ts
|
|
3
|
-
/** Create a TanStack Start server route handler for
|
|
3
|
+
/** Create a TanStack Start server route handler for Mynth webhooks. */
|
|
4
4
|
function mynthWebhookHandler(eventHandlers, options = {}) {
|
|
5
|
-
return (context) => handleWebhookRequest(context.request.clone(), eventHandlers, (
|
|
5
|
+
return (context) => handleWebhookRequest(context.request.clone(), eventHandlers, (event) => ({
|
|
6
6
|
...context,
|
|
7
|
-
|
|
7
|
+
event
|
|
8
8
|
}), options);
|
|
9
9
|
}
|
|
10
10
|
//#endregion
|