@runapi.ai/kling 0.2.7 → 0.2.9

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/dist/index.js CHANGED
@@ -41,12 +41,447 @@ var import_core5 = require("@runapi.ai/core");
41
41
  // src/resources/text-to-video.ts
42
42
  var import_core = require("@runapi.ai/core");
43
43
  var import_internal = require("@runapi.ai/core/internal");
44
+
45
+ // src/contract_gen.ts
46
+ var contract = {
47
+ "avatar": {
48
+ "models": [
49
+ "kling-ai-avatar-pro",
50
+ "kling-ai-avatar-standard",
51
+ "kling-ai-avatar-v1-pro",
52
+ "kling-v1-avatar-standard"
53
+ ],
54
+ "fields_by_model": {
55
+ "kling-ai-avatar-pro": {
56
+ "model": {
57
+ "required": true
58
+ },
59
+ "prompt": {
60
+ "required": true
61
+ },
62
+ "source_audio_url": {
63
+ "required": true
64
+ },
65
+ "source_image_url": {
66
+ "required": true
67
+ }
68
+ },
69
+ "kling-ai-avatar-standard": {
70
+ "model": {
71
+ "required": true
72
+ },
73
+ "prompt": {
74
+ "required": true
75
+ },
76
+ "source_audio_url": {
77
+ "required": true
78
+ },
79
+ "source_image_url": {
80
+ "required": true
81
+ }
82
+ },
83
+ "kling-ai-avatar-v1-pro": {
84
+ "model": {
85
+ "required": true
86
+ },
87
+ "prompt": {
88
+ "required": true
89
+ },
90
+ "source_audio_url": {
91
+ "required": true
92
+ },
93
+ "source_image_url": {
94
+ "required": true
95
+ }
96
+ },
97
+ "kling-v1-avatar-standard": {
98
+ "model": {
99
+ "required": true
100
+ },
101
+ "prompt": {
102
+ "required": true
103
+ },
104
+ "source_audio_url": {
105
+ "required": true
106
+ },
107
+ "source_image_url": {
108
+ "required": true
109
+ }
110
+ }
111
+ }
112
+ },
113
+ "image-to-video": {
114
+ "models": [
115
+ "kling-v2.1-master-image-to-video",
116
+ "kling-v2.1-pro",
117
+ "kling-v2.1-standard",
118
+ "kling-v2.5-turbo-image-to-video-pro",
119
+ "kling-v3-turbo-image-to-video"
120
+ ],
121
+ "fields_by_model": {
122
+ "kling-v2.1-master-image-to-video": {
123
+ "duration_seconds": {
124
+ "enum": [
125
+ 5,
126
+ 10
127
+ ],
128
+ "type": "integer"
129
+ },
130
+ "first_frame_image_url": {
131
+ "required": true
132
+ },
133
+ "model": {
134
+ "required": true
135
+ },
136
+ "prompt": {
137
+ "required": true
138
+ }
139
+ },
140
+ "kling-v2.1-pro": {
141
+ "duration_seconds": {
142
+ "enum": [
143
+ 5,
144
+ 10
145
+ ],
146
+ "type": "integer"
147
+ },
148
+ "first_frame_image_url": {
149
+ "required": true
150
+ },
151
+ "model": {
152
+ "required": true
153
+ },
154
+ "prompt": {
155
+ "required": true
156
+ }
157
+ },
158
+ "kling-v2.1-standard": {
159
+ "duration_seconds": {
160
+ "enum": [
161
+ 5,
162
+ 10
163
+ ],
164
+ "type": "integer"
165
+ },
166
+ "first_frame_image_url": {
167
+ "required": true
168
+ },
169
+ "model": {
170
+ "required": true
171
+ },
172
+ "prompt": {
173
+ "required": true
174
+ }
175
+ },
176
+ "kling-v2.5-turbo-image-to-video-pro": {
177
+ "duration_seconds": {
178
+ "enum": [
179
+ 5,
180
+ 10
181
+ ],
182
+ "type": "integer"
183
+ },
184
+ "first_frame_image_url": {
185
+ "required": true
186
+ },
187
+ "model": {
188
+ "required": true
189
+ },
190
+ "prompt": {
191
+ "required": true
192
+ }
193
+ },
194
+ "kling-v3-turbo-image-to-video": {
195
+ "duration_seconds": {
196
+ "enum": [
197
+ 3,
198
+ 4,
199
+ 5,
200
+ 6,
201
+ 7,
202
+ 8,
203
+ 9,
204
+ 10,
205
+ 11,
206
+ 12,
207
+ 13,
208
+ 14,
209
+ 15
210
+ ],
211
+ "type": "integer"
212
+ },
213
+ "first_frame_image_url": {
214
+ "required": true
215
+ },
216
+ "model": {
217
+ "required": true
218
+ },
219
+ "output_resolution": {
220
+ "enum": [
221
+ "720p",
222
+ "1080p"
223
+ ]
224
+ },
225
+ "prompt": {
226
+ "required": true,
227
+ "min": 1,
228
+ "max": 2500,
229
+ "length": true
230
+ }
231
+ }
232
+ },
233
+ "rules": [
234
+ {
235
+ "when": {
236
+ "model": "kling-v2.1-master-image-to-video"
237
+ },
238
+ "forbidden": [
239
+ "output_resolution"
240
+ ]
241
+ },
242
+ {
243
+ "when": {
244
+ "model": "kling-v2.1-pro"
245
+ },
246
+ "forbidden": [
247
+ "output_resolution"
248
+ ]
249
+ },
250
+ {
251
+ "when": {
252
+ "model": "kling-v2.1-standard"
253
+ },
254
+ "forbidden": [
255
+ "output_resolution"
256
+ ]
257
+ },
258
+ {
259
+ "when": {
260
+ "model": "kling-v2.5-turbo-image-to-video-pro"
261
+ },
262
+ "forbidden": [
263
+ "output_resolution"
264
+ ]
265
+ },
266
+ {
267
+ "when": {
268
+ "model": "kling-v3-turbo-image-to-video"
269
+ },
270
+ "forbidden": [
271
+ "aspect_ratio",
272
+ "negative_prompt",
273
+ "cfg_scale",
274
+ "last_frame_image_url"
275
+ ]
276
+ }
277
+ ]
278
+ },
279
+ "motion-control": {
280
+ "models": [
281
+ "kling-3.0"
282
+ ],
283
+ "fields_by_model": {
284
+ "kling-3.0": {
285
+ "background_source": {
286
+ "enum": [
287
+ "video",
288
+ "image"
289
+ ]
290
+ },
291
+ "character_orientation": {
292
+ "enum": [
293
+ "video",
294
+ "image"
295
+ ]
296
+ },
297
+ "model": {
298
+ "required": true
299
+ },
300
+ "output_resolution": {
301
+ "enum": [
302
+ "720p",
303
+ "1080p"
304
+ ]
305
+ },
306
+ "reference_video_url": {
307
+ "required": true
308
+ },
309
+ "source_image_url": {
310
+ "required": true
311
+ }
312
+ }
313
+ }
314
+ },
315
+ "text-to-video": {
316
+ "models": [
317
+ "kling-3.0",
318
+ "kling-v2.1-master-text-to-video",
319
+ "kling-v2.5-turbo-text-to-video-pro",
320
+ "kling-v3-turbo-text-to-video"
321
+ ],
322
+ "fields_by_model": {
323
+ "kling-3.0": {
324
+ "aspect_ratio": {
325
+ "enum": [
326
+ "16:9",
327
+ "9:16",
328
+ "1:1"
329
+ ]
330
+ },
331
+ "duration_seconds": {
332
+ "enum": [
333
+ 3,
334
+ 4,
335
+ 5,
336
+ 6,
337
+ 7,
338
+ 8,
339
+ 9,
340
+ 10,
341
+ 11,
342
+ 12,
343
+ 13,
344
+ 14,
345
+ 15
346
+ ],
347
+ "type": "integer"
348
+ },
349
+ "model": {
350
+ "required": true
351
+ },
352
+ "output_resolution": {
353
+ "enum": [
354
+ "720p",
355
+ "1080p",
356
+ "4k"
357
+ ]
358
+ }
359
+ },
360
+ "kling-v2.1-master-text-to-video": {
361
+ "aspect_ratio": {
362
+ "enum": [
363
+ "16:9",
364
+ "9:16",
365
+ "1:1"
366
+ ]
367
+ },
368
+ "duration_seconds": {
369
+ "enum": [
370
+ 5,
371
+ 10
372
+ ],
373
+ "type": "integer"
374
+ },
375
+ "model": {
376
+ "required": true
377
+ }
378
+ },
379
+ "kling-v2.5-turbo-text-to-video-pro": {
380
+ "aspect_ratio": {
381
+ "enum": [
382
+ "16:9",
383
+ "9:16",
384
+ "1:1"
385
+ ]
386
+ },
387
+ "duration_seconds": {
388
+ "enum": [
389
+ 5,
390
+ 10
391
+ ],
392
+ "type": "integer"
393
+ },
394
+ "model": {
395
+ "required": true
396
+ }
397
+ },
398
+ "kling-v3-turbo-text-to-video": {
399
+ "aspect_ratio": {
400
+ "enum": [
401
+ "16:9",
402
+ "9:16",
403
+ "1:1"
404
+ ]
405
+ },
406
+ "duration_seconds": {
407
+ "enum": [
408
+ 3,
409
+ 4,
410
+ 5,
411
+ 6,
412
+ 7,
413
+ 8,
414
+ 9,
415
+ 10,
416
+ 11,
417
+ 12,
418
+ 13,
419
+ 14,
420
+ 15
421
+ ],
422
+ "type": "integer"
423
+ },
424
+ "model": {
425
+ "required": true
426
+ },
427
+ "output_resolution": {
428
+ "enum": [
429
+ "720p",
430
+ "1080p"
431
+ ]
432
+ },
433
+ "prompt": {
434
+ "required": true,
435
+ "min": 1,
436
+ "max": 2500,
437
+ "length": true
438
+ }
439
+ }
440
+ },
441
+ "rules": [
442
+ {
443
+ "when": {
444
+ "model": "kling-v3-turbo-text-to-video"
445
+ },
446
+ "forbidden": [
447
+ "enable_sound",
448
+ "negative_prompt",
449
+ "cfg_scale",
450
+ "multi_shots",
451
+ "multi_prompt",
452
+ "first_frame_image_url",
453
+ "last_frame_image_url",
454
+ "kling_elements"
455
+ ]
456
+ }
457
+ ]
458
+ }
459
+ };
460
+
461
+ // src/resources/text-to-video.ts
44
462
  var ENDPOINT = "/api/v1/kling/text_to_video";
463
+ var V3_TURBO_MODEL = "kling-v3-turbo-text-to-video";
464
+ var V3_TURBO_UNSUPPORTED_FIELDS = [
465
+ "enable_sound",
466
+ "negative_prompt",
467
+ "cfg_scale",
468
+ "multi_shots",
469
+ "multi_prompt",
470
+ "first_frame_image_url",
471
+ "last_frame_image_url",
472
+ "kling_elements"
473
+ ];
45
474
  var TextToVideo = class {
46
475
  constructor(http) {
47
476
  this.http = http;
48
477
  }
49
478
  http;
479
+ /**
480
+ * Generate a video from a text prompt and wait until complete.
481
+ * @param params Text-to-video parameters.
482
+ * @param options Per-request and polling overrides.
483
+ * @returns The completed task with videos.
484
+ */
50
485
  async run(params, options) {
51
486
  const { id } = await this.create(params, options);
52
487
  const response = await (0, import_internal.pollUntilComplete)(() => this.get(id, options), {
@@ -55,28 +490,78 @@ var TextToVideo = class {
55
490
  });
56
491
  return response;
57
492
  }
493
+ /**
494
+ * Create a text-to-video task; returns immediately with a task id.
495
+ * @param params Text-to-video parameters.
496
+ * @param options Per-request overrides.
497
+ * @returns The task creation result with id.
498
+ */
58
499
  async create(params, options) {
500
+ const body = (0, import_core.compactParams)(params);
501
+ rejectUnsupportedV3TurboFields(body);
502
+ (0, import_core.validateParams)(contract["text-to-video"], body);
59
503
  return this.http.request("POST", ENDPOINT, {
60
- body: (0, import_core.compactParams)(params),
504
+ body,
61
505
  ...options
62
506
  });
63
507
  }
508
+ /**
509
+ * Fetch the current status of a text-to-video task.
510
+ * @param id The task id.
511
+ * @param options Per-request overrides.
512
+ * @returns The current text-to-video task status.
513
+ */
64
514
  async get(id, options) {
65
515
  return this.http.request("GET", `${ENDPOINT}/${id}`, {
66
516
  ...options
67
517
  });
68
518
  }
69
519
  };
520
+ function rejectUnsupportedV3TurboFields(body) {
521
+ if (body.model !== V3_TURBO_MODEL) return;
522
+ const field = V3_TURBO_UNSUPPORTED_FIELDS.find((candidate) => fieldPresent(body, candidate));
523
+ if (field) {
524
+ throw new import_core.ValidationError(`${field} is not supported by ${V3_TURBO_MODEL}`);
525
+ }
526
+ }
527
+ function fieldPresent(params, field) {
528
+ if (!(field in params)) return false;
529
+ const value = params[field];
530
+ if (value === false) return true;
531
+ if (Array.isArray(value)) return value.some((item) => presentValue(item));
532
+ return presentValue(value);
533
+ }
534
+ function presentValue(value) {
535
+ if (value === null || value === void 0 || value === false) return false;
536
+ if (value === true) return true;
537
+ if (typeof value === "string") return value.trim() !== "";
538
+ if (Array.isArray(value)) return value.length > 0;
539
+ if (typeof value === "object") return Object.keys(value).length > 0;
540
+ return true;
541
+ }
70
542
 
71
543
  // src/resources/image-to-video.ts
72
544
  var import_core2 = require("@runapi.ai/core");
73
545
  var import_internal2 = require("@runapi.ai/core/internal");
74
546
  var ENDPOINT2 = "/api/v1/kling/image_to_video";
547
+ var V3_TURBO_MODEL2 = "kling-v3-turbo-image-to-video";
548
+ var V3_TURBO_UNSUPPORTED_FIELDS2 = [
549
+ "aspect_ratio",
550
+ "negative_prompt",
551
+ "cfg_scale",
552
+ "last_frame_image_url"
553
+ ];
75
554
  var ImageToVideo = class {
76
555
  constructor(http) {
77
556
  this.http = http;
78
557
  }
79
558
  http;
559
+ /**
560
+ * Generate a video from an input image and wait until complete.
561
+ * @param params Image-to-video parameters.
562
+ * @param options Per-request and polling overrides.
563
+ * @returns The completed task with videos.
564
+ */
80
565
  async run(params, options) {
81
566
  const { id } = await this.create(params, options);
82
567
  const response = await (0, import_internal2.pollUntilComplete)(() => this.get(id, options), {
@@ -85,18 +570,55 @@ var ImageToVideo = class {
85
570
  });
86
571
  return response;
87
572
  }
573
+ /**
574
+ * Create an image-to-video task; returns immediately with a task id.
575
+ * @param params Image-to-video parameters.
576
+ * @param options Per-request overrides.
577
+ * @returns The task creation result with id.
578
+ */
88
579
  async create(params, options) {
580
+ const body = (0, import_core2.compactParams)(params);
581
+ rejectUnsupportedV3TurboFields2(body);
582
+ (0, import_core2.validateParams)(contract["image-to-video"], body);
89
583
  return this.http.request("POST", ENDPOINT2, {
90
- body: (0, import_core2.compactParams)(params),
584
+ body,
91
585
  ...options
92
586
  });
93
587
  }
588
+ /**
589
+ * Fetch the current status of an image-to-video task.
590
+ * @param id The task id.
591
+ * @param options Per-request overrides.
592
+ * @returns The current image-to-video task status.
593
+ */
94
594
  async get(id, options) {
95
595
  return this.http.request("GET", `${ENDPOINT2}/${id}`, {
96
596
  ...options
97
597
  });
98
598
  }
99
599
  };
600
+ function rejectUnsupportedV3TurboFields2(body) {
601
+ if (body.model !== V3_TURBO_MODEL2) return;
602
+ const field = V3_TURBO_UNSUPPORTED_FIELDS2.find((candidate) => fieldPresent2(body, candidate));
603
+ if (field) {
604
+ throw new import_core2.ValidationError(`${field} is not supported by ${V3_TURBO_MODEL2}`);
605
+ }
606
+ }
607
+ function fieldPresent2(params, field) {
608
+ if (!(field in params)) return false;
609
+ const value = params[field];
610
+ if (value === false) return true;
611
+ if (Array.isArray(value)) return value.some((item) => presentValue2(item));
612
+ return presentValue2(value);
613
+ }
614
+ function presentValue2(value) {
615
+ if (value === null || value === void 0 || value === false) return false;
616
+ if (value === true) return true;
617
+ if (typeof value === "string") return value.trim() !== "";
618
+ if (Array.isArray(value)) return value.length > 0;
619
+ if (typeof value === "object") return Object.keys(value).length > 0;
620
+ return true;
621
+ }
100
622
 
101
623
  // src/resources/ai-avatar.ts
102
624
  var import_core3 = require("@runapi.ai/core");
@@ -107,6 +629,12 @@ var AiAvatar = class {
107
629
  this.http = http;
108
630
  }
109
631
  http;
632
+ /**
633
+ * Generate an AI avatar video and wait until complete.
634
+ * @param params AI avatar parameters.
635
+ * @param options Per-request and polling overrides.
636
+ * @returns The completed task with videos.
637
+ */
110
638
  async run(params, options) {
111
639
  const { id } = await this.create(params, options);
112
640
  const response = await (0, import_internal3.pollUntilComplete)(() => this.get(id, options), {
@@ -115,12 +643,26 @@ var AiAvatar = class {
115
643
  });
116
644
  return response;
117
645
  }
646
+ /**
647
+ * Create an AI avatar generation task; returns immediately with a task id.
648
+ * @param params AI avatar parameters.
649
+ * @param options Per-request overrides.
650
+ * @returns The task creation result with id.
651
+ */
118
652
  async create(params, options) {
653
+ const body = (0, import_core3.compactParams)(params);
654
+ (0, import_core3.validateParams)(contract["avatar"], body);
119
655
  return this.http.request("POST", ENDPOINT3, {
120
- body: (0, import_core3.compactParams)(params),
656
+ body,
121
657
  ...options
122
658
  });
123
659
  }
660
+ /**
661
+ * Fetch the current status of an AI avatar task.
662
+ * @param id The task id.
663
+ * @param options Per-request overrides.
664
+ * @returns The current AI avatar task status.
665
+ */
124
666
  async get(id, options) {
125
667
  return this.http.request("GET", `${ENDPOINT3}/${id}`, {
126
668
  ...options
@@ -137,6 +679,12 @@ var MotionControl = class {
137
679
  this.http = http;
138
680
  }
139
681
  http;
682
+ /**
683
+ * Generate a video with motion transfer from a reference video and wait until complete.
684
+ * @param params Motion control parameters.
685
+ * @param options Per-request and polling overrides.
686
+ * @returns The completed task with videos.
687
+ */
140
688
  async run(params, options) {
141
689
  const { id } = await this.create(params, options);
142
690
  const response = await (0, import_internal4.pollUntilComplete)(() => this.get(id, options), {
@@ -145,12 +693,26 @@ var MotionControl = class {
145
693
  });
146
694
  return response;
147
695
  }
696
+ /**
697
+ * Create a motion control generation task; returns immediately with a task id.
698
+ * @param params Motion control parameters.
699
+ * @param options Per-request overrides.
700
+ * @returns The task creation result with id.
701
+ */
148
702
  async create(params, options) {
703
+ const body = (0, import_core4.compactParams)(params);
704
+ (0, import_core4.validateParams)(contract["motion-control"], body);
149
705
  return this.http.request("POST", ENDPOINT4, {
150
- body: (0, import_core4.compactParams)(params),
706
+ body,
151
707
  ...options
152
708
  });
153
709
  }
710
+ /**
711
+ * Fetch the current status of a motion control task.
712
+ * @param id The task id.
713
+ * @param options Per-request overrides.
714
+ * @returns The current motion control task status.
715
+ */
154
716
  async get(id, options) {
155
717
  return this.http.request("GET", `${ENDPOINT4}/${id}`, {
156
718
  ...options
@@ -159,21 +721,21 @@ var MotionControl = class {
159
721
  };
160
722
 
161
723
  // src/client.ts
162
- var KlingClient = class {
163
- /** Text-to-video operations. */
724
+ var KlingClient = class extends import_core5.BaseClient {
725
+ /** Text-to-video generation with multi-shot, sound, and Kling elements support on kling-3.0. */
164
726
  textToVideo;
165
- /** Image-to-video operations. */
727
+ /** Image-to-video animation from a first-frame image guided by a text prompt. */
166
728
  imageToVideo;
167
- /** AI avatar operations. */
729
+ /** AI avatar lip-sync: animate a face image to match an audio track. */
168
730
  aiAvatar;
169
- /** Motion control operations. */
731
+ /** Transfer motion from a reference video onto a subject image. */
170
732
  motionControl;
171
733
  constructor(options = {}) {
172
- const http = (0, import_core5.createHttpClient)(options);
173
- this.textToVideo = new TextToVideo(http);
174
- this.imageToVideo = new ImageToVideo(http);
175
- this.aiAvatar = new AiAvatar(http);
176
- this.motionControl = new MotionControl(http);
734
+ super(options);
735
+ this.textToVideo = new TextToVideo(this.http);
736
+ this.imageToVideo = new ImageToVideo(this.http);
737
+ this.aiAvatar = new AiAvatar(this.http);
738
+ this.motionControl = new MotionControl(this.http);
177
739
  }
178
740
  };
179
741