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