magic-hour 0.36.0 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +202 -10
  2. package/dist/helpers/download.d.ts +5 -0
  3. package/dist/helpers/download.js +88 -0
  4. package/dist/helpers/generate-type.d.ts +51 -0
  5. package/dist/helpers/generate-type.js +2 -0
  6. package/dist/helpers/sleep.d.ts +4 -0
  7. package/dist/helpers/sleep.js +9 -0
  8. package/dist/logger.d.ts +48 -0
  9. package/dist/logger.js +97 -0
  10. package/dist/resources/v1/ai-clothes-changer/resource-client.d.ts +50 -0
  11. package/dist/resources/v1/ai-clothes-changer/resource-client.js +75 -0
  12. package/dist/resources/v1/ai-face-editor/resource-client.d.ts +55 -0
  13. package/dist/resources/v1/ai-face-editor/resource-client.js +85 -0
  14. package/dist/resources/v1/ai-gif-generator/resource-client.d.ts +29 -0
  15. package/dist/resources/v1/ai-gif-generator/resource-client.js +60 -0
  16. package/dist/resources/v1/ai-headshot-generator/resource-client.d.ts +38 -0
  17. package/dist/resources/v1/ai-headshot-generator/resource-client.js +68 -0
  18. package/dist/resources/v1/ai-image-editor/resource-client.d.ts +39 -0
  19. package/dist/resources/v1/ai-image-editor/resource-client.js +69 -0
  20. package/dist/resources/v1/ai-image-generator/resource-client.d.ts +31 -0
  21. package/dist/resources/v1/ai-image-generator/resource-client.js +62 -0
  22. package/dist/resources/v1/ai-image-upscaler/resource-client.d.ts +40 -0
  23. package/dist/resources/v1/ai-image-upscaler/resource-client.js +70 -0
  24. package/dist/resources/v1/ai-meme-generator/resource-client.d.ts +33 -0
  25. package/dist/resources/v1/ai-meme-generator/resource-client.js +64 -0
  26. package/dist/resources/v1/ai-photo-editor/resource-client.d.ts +49 -0
  27. package/dist/resources/v1/ai-photo-editor/resource-client.js +79 -0
  28. package/dist/resources/v1/ai-qr-code-generator/resource-client.d.ts +30 -0
  29. package/dist/resources/v1/ai-qr-code-generator/resource-client.js +61 -0
  30. package/dist/resources/v1/ai-talking-photo/resource-client.d.ts +51 -0
  31. package/dist/resources/v1/ai-talking-photo/resource-client.js +76 -0
  32. package/dist/resources/v1/animation/resource-client.d.ts +61 -0
  33. package/dist/resources/v1/animation/resource-client.js +98 -0
  34. package/dist/resources/v1/auto-subtitle-generator/resource-client.d.ts +41 -0
  35. package/dist/resources/v1/auto-subtitle-generator/resource-client.js +71 -0
  36. package/dist/resources/v1/face-detection/resource-client.d.ts +30 -0
  37. package/dist/resources/v1/face-detection/resource-client.js +75 -0
  38. package/dist/resources/v1/face-swap/resource-client.d.ts +77 -0
  39. package/dist/resources/v1/face-swap/resource-client.js +125 -0
  40. package/dist/resources/v1/face-swap-photo/resource-client.d.ts +73 -0
  41. package/dist/resources/v1/face-swap-photo/resource-client.js +117 -0
  42. package/dist/resources/v1/files/resource-client.d.ts +40 -0
  43. package/dist/resources/v1/files/resource-client.js +236 -0
  44. package/dist/resources/v1/files/resource-client.test.d.ts +1 -0
  45. package/dist/resources/v1/files/resource-client.test.js +422 -0
  46. package/dist/resources/v1/image-background-remover/resource-client.d.ts +49 -0
  47. package/dist/resources/v1/image-background-remover/resource-client.js +82 -0
  48. package/dist/resources/v1/image-projects/resource-client.d.ts +22 -0
  49. package/dist/resources/v1/image-projects/resource-client.js +68 -0
  50. package/dist/resources/v1/image-projects/resource-client.test.d.ts +1 -0
  51. package/dist/resources/v1/image-projects/resource-client.test.js +538 -0
  52. package/dist/resources/v1/image-to-video/resource-client.d.ts +40 -0
  53. package/dist/resources/v1/image-to-video/resource-client.js +70 -0
  54. package/dist/resources/v1/lip-sync/resource-client.d.ts +53 -0
  55. package/dist/resources/v1/lip-sync/resource-client.js +84 -0
  56. package/dist/resources/v1/photo-colorizer/resource-client.d.ts +38 -0
  57. package/dist/resources/v1/photo-colorizer/resource-client.js +68 -0
  58. package/dist/resources/v1/text-to-video/resource-client.d.ts +32 -0
  59. package/dist/resources/v1/text-to-video/resource-client.js +63 -0
  60. package/dist/resources/v1/video-projects/resource-client.d.ts +22 -0
  61. package/dist/resources/v1/video-projects/resource-client.js +68 -0
  62. package/dist/resources/v1/video-projects/resource-client.test.d.ts +1 -0
  63. package/dist/resources/v1/video-projects/resource-client.test.js +713 -0
  64. package/dist/resources/v1/video-to-video/resource-client.d.ts +48 -0
  65. package/dist/resources/v1/video-to-video/resource-client.js +84 -0
  66. package/package.json +15 -11
@@ -1,8 +1,56 @@
1
1
  import { types } from "../../../index.js";
2
2
  import { ApiPromise, CoreClient, CoreResourceClient, RequestOptions, ResourceClientOptions } from "../../../core";
3
+ import { GenerateOptions, GenerateRequestType } from "../../../helpers/generate-type";
3
4
  import * as requests from "../../../resources/v1/image-background-remover/request-types";
5
+ type GenerateRequest = GenerateRequestType<requests.CreateRequest, {
6
+ /**
7
+ * The image used as the new background for the image_file_path. This value is either
8
+ * - a direct URL to the image file
9
+ * - a path to a local file
10
+ *
11
+ * Note: if the path begins with `api-assets`, it will be assumed to already be uploaded to Magic Hour's storage, and will not be uploaded again.
12
+ */
13
+ backgroundImageFilePath?: string | undefined;
14
+ /**
15
+ * The image to remove the background. This value is either
16
+ * - a direct URL to the image file
17
+ * - a path to a local file
18
+ *
19
+ * Note: if the path begins with `api-assets`, it will be assumed to already be uploaded to Magic Hour's storage, and will not be uploaded again.
20
+ */
21
+ imageFilePath: string;
22
+ }>;
4
23
  export declare class ImageBackgroundRemoverClient extends CoreResourceClient {
5
24
  constructor(coreClient: CoreClient, opts: ResourceClientOptions);
25
+ /**
26
+ * Image Background Remover
27
+ *
28
+ * Remove background from image
29
+ *
30
+ * This method provides a convenient way to create a request and automatically wait for completion and download outputs.
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * import Client from "../../../index.js";
35
+ *
36
+ * const client = new Client({ token: process.env["API_TOKEN"]!! });
37
+ * const res = await client.v1.imageBackgroundRemover.generate(
38
+ * {
39
+ * assets: {
40
+ * backgroundImageFilePath: "/path/to/1234.png",
41
+ * imageFilePath: "/path/to/1234.png",
42
+ * },
43
+ * name: "Background Remover image",
44
+ * },
45
+ * {
46
+ * waitForCompletion: true,
47
+ * downloadOutputs: true,
48
+ * downloadDirectory: "outputs",
49
+ * },
50
+ * );
51
+ * ```
52
+ */
53
+ generate(request: GenerateRequest, opts?: GenerateOptions): Promise<import("../image-projects/resource-client").V1ImageProjectsGetResponseWithDownloads>;
6
54
  /**
7
55
  * Image Background Remover
8
56
  *
@@ -12,3 +60,4 @@ export declare class ImageBackgroundRemoverClient extends CoreResourceClient {
12
60
  */
13
61
  create(request: requests.CreateRequest, opts?: RequestOptions): ApiPromise<types.V1ImageBackgroundRemoverCreateResponse>;
14
62
  }
63
+ export {};
@@ -1,13 +1,95 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
2
22
  Object.defineProperty(exports, "__esModule", { value: true });
3
23
  exports.ImageBackgroundRemoverClient = void 0;
4
24
  const core_1 = require("../../../core");
25
+ const files_1 = require("../../../resources/v1/files");
5
26
  const v1_image_background_remover_create_body_1 = require("../../../types/v1-image-background-remover-create-body");
6
27
  const v1_image_background_remover_create_response_1 = require("../../../types/v1-image-background-remover-create-response");
28
+ const logger_1 = require("../../../logger");
29
+ const image_projects_1 = require("../../../resources/v1/image-projects");
7
30
  class ImageBackgroundRemoverClient extends core_1.CoreResourceClient {
8
31
  constructor(coreClient, opts) {
9
32
  super(coreClient, opts);
10
33
  }
34
+ /**
35
+ * Image Background Remover
36
+ *
37
+ * Remove background from image
38
+ *
39
+ * This method provides a convenient way to create a request and automatically wait for completion and download outputs.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * import Client from "../../../index.js";
44
+ *
45
+ * const client = new Client({ token: process.env["API_TOKEN"]!! });
46
+ * const res = await client.v1.imageBackgroundRemover.generate(
47
+ * {
48
+ * assets: {
49
+ * backgroundImageFilePath: "/path/to/1234.png",
50
+ * imageFilePath: "/path/to/1234.png",
51
+ * },
52
+ * name: "Background Remover image",
53
+ * },
54
+ * {
55
+ * waitForCompletion: true,
56
+ * downloadOutputs: true,
57
+ * downloadDirectory: "outputs",
58
+ * },
59
+ * );
60
+ * ```
61
+ */
62
+ generate(request_1) {
63
+ return __awaiter(this, arguments, void 0, function* (request, opts = {}) {
64
+ const { waitForCompletion = true, downloadOutputs = true, downloadDirectory = undefined } = opts, createOpts = __rest(opts, ["waitForCompletion", "downloadOutputs", "downloadDirectory"]);
65
+ const fileClient = new files_1.FilesClient(this._client, this._opts);
66
+ const _a = request.assets, { backgroundImageFilePath, imageFilePath } = _a, restAssets = __rest(_a, ["backgroundImageFilePath", "imageFilePath"]);
67
+ if (backgroundImageFilePath) {
68
+ (0, logger_1.getLogger)().debug(`Uploading file ${backgroundImageFilePath} to Magic Hour's storage`);
69
+ }
70
+ (0, logger_1.getLogger)().debug(`Uploading file ${imageFilePath} to Magic Hour's storage`);
71
+ const [uploadedBackgroundImageFilePath, uploadedImageFilePath] = yield Promise.all([
72
+ backgroundImageFilePath
73
+ ? fileClient.uploadFile(backgroundImageFilePath)
74
+ : Promise.resolve(backgroundImageFilePath),
75
+ fileClient.uploadFile(imageFilePath),
76
+ ]);
77
+ if (backgroundImageFilePath) {
78
+ (0, logger_1.getLogger)().info(`Uploaded file ${backgroundImageFilePath} to Magic Hour's storage as ${uploadedBackgroundImageFilePath}`);
79
+ }
80
+ (0, logger_1.getLogger)().info(`Uploaded file ${imageFilePath} to Magic Hour's storage as ${uploadedImageFilePath}`);
81
+ const createResponse = yield this.create(Object.assign(Object.assign({}, request), { assets: Object.assign(Object.assign({}, restAssets), { backgroundImageFilePath: backgroundImageFilePath
82
+ ? uploadedBackgroundImageFilePath
83
+ : backgroundImageFilePath, imageFilePath: uploadedImageFilePath }) }), createOpts);
84
+ (0, logger_1.getLogger)().info(`Created ImageBackgroundRemoverClient project ${createResponse.id}`);
85
+ const projectsClient = new image_projects_1.ImageProjectsClient(this._client, this._opts);
86
+ (0, logger_1.getLogger)().debug(`Checking result for ImageBackgroundRemoverClient project ${createResponse.id}`);
87
+ const result = yield projectsClient.checkResult({ id: createResponse.id }, Object.assign({ waitForCompletion,
88
+ downloadOutputs,
89
+ downloadDirectory }, createOpts));
90
+ return result;
91
+ });
92
+ }
11
93
  /**
12
94
  * Image Background Remover
13
95
  *
@@ -1,8 +1,30 @@
1
1
  import { types } from "../../../index.js";
2
2
  import { ApiPromise, CoreClient, CoreResourceClient, RequestOptions, ResourceClientOptions } from "../../../core";
3
+ import { GenerateOptions } from "../../../helpers/generate-type";
3
4
  import * as requests from "../../../resources/v1/image-projects/request-types";
5
+ /**
6
+ * Extended response interface that includes downloaded paths
7
+ */
8
+ export interface V1ImageProjectsGetResponseWithDownloads extends types.V1ImageProjectsGetResponse {
9
+ /**
10
+ * The paths to the downloaded files.
11
+ * This field is only populated if `download_outputs` is True and the image project is complete.
12
+ */
13
+ downloadedPaths?: string[];
14
+ }
4
15
  export declare class ImageProjectsClient extends CoreResourceClient {
5
16
  constructor(coreClient: CoreClient, opts: ResourceClientOptions);
17
+ /**
18
+ * Check the result of an image project with optional waiting and downloading.
19
+ *
20
+ * This method retrieves the status of an image project and optionally waits for completion
21
+ * and downloads the output files.
22
+ *
23
+ * @param request - Request object containing the image project ID
24
+ * @param opts - Additional request options
25
+ * @returns The image project response with optional downloaded file paths included
26
+ */
27
+ checkResult(request: requests.GetRequest, opts?: GenerateOptions): Promise<V1ImageProjectsGetResponseWithDownloads>;
6
28
  /**
7
29
  * Delete image
8
30
  *
@@ -1,12 +1,80 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
2
22
  Object.defineProperty(exports, "__esModule", { value: true });
3
23
  exports.ImageProjectsClient = void 0;
4
24
  const core_1 = require("../../../core");
25
+ const download_1 = require("../../../helpers/download");
26
+ const sleep_1 = require("../../../helpers/sleep");
27
+ const logger_1 = require("../../../logger");
5
28
  const v1_image_projects_get_response_1 = require("../../../types/v1-image-projects-get-response");
6
29
  class ImageProjectsClient extends core_1.CoreResourceClient {
7
30
  constructor(coreClient, opts) {
8
31
  super(coreClient, opts);
9
32
  }
33
+ /**
34
+ * Check the result of an image project with optional waiting and downloading.
35
+ *
36
+ * This method retrieves the status of an image project and optionally waits for completion
37
+ * and downloads the output files.
38
+ *
39
+ * @param request - Request object containing the image project ID
40
+ * @param opts - Additional request options
41
+ * @returns The image project response with optional downloaded file paths included
42
+ */
43
+ checkResult(request, opts) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ var _a, _b;
46
+ const _c = opts || {}, { waitForCompletion = true, downloadOutputs = true, downloadDirectory = undefined } = _c, requestOpts = __rest(_c, ["waitForCompletion", "downloadOutputs", "downloadDirectory"]);
47
+ let apiResponse = yield this.get({ id: request.id }, requestOpts);
48
+ if (!waitForCompletion) {
49
+ return Object.assign({}, apiResponse);
50
+ }
51
+ const pollInterval = parseFloat(process.env["MAGIC_HOUR_POLL_INTERVAL"] || "0.5");
52
+ (0, logger_1.getLogger)().debug(`Polling image project ${request.id} every ${pollInterval} seconds`);
53
+ while (!["complete", "error", "canceled"].includes(apiResponse.status)) {
54
+ yield (0, sleep_1.sleep)(pollInterval * 1000); // Convert seconds to milliseconds
55
+ (0, logger_1.getLogger)().info(`Image project ${apiResponse.id} status: ${apiResponse.status}, waiting for ${pollInterval} seconds and checking again`);
56
+ apiResponse = yield this.get({ id: request.id }, requestOpts);
57
+ }
58
+ if (apiResponse.status !== "complete") {
59
+ const message = `Image project ${request.id} has status ${apiResponse.status}: [${(_a = apiResponse.error) === null || _a === void 0 ? void 0 : _a.code}] ${(_b = apiResponse.error) === null || _b === void 0 ? void 0 : _b.message}`;
60
+ if (apiResponse.status === "error") {
61
+ (0, logger_1.getLogger)().error(message);
62
+ }
63
+ else {
64
+ (0, logger_1.getLogger)().info(message);
65
+ }
66
+ return Object.assign({}, apiResponse);
67
+ }
68
+ if (!downloadOutputs) {
69
+ (0, logger_1.getLogger)().info(`Download outputs is disabled. Returning image project ${request.id} with status ${apiResponse.status}`);
70
+ return Object.assign({}, apiResponse);
71
+ }
72
+ (0, logger_1.getLogger)().debug(`Downloading outputs for image project ${request.id} to ${downloadDirectory !== null && downloadDirectory !== void 0 ? downloadDirectory : "current directory"}`);
73
+ const downloadedPaths = yield (0, download_1.downloadFiles)(apiResponse.downloads, downloadDirectory);
74
+ (0, logger_1.getLogger)().info(`Downloaded outputs for image project ${request.id} to ${downloadedPaths}`);
75
+ return Object.assign(Object.assign({}, apiResponse), { downloadedPaths });
76
+ });
77
+ }
10
78
  /**
11
79
  * Delete image
12
80
  *