@ai-sdk/klingai 4.0.0-beta.3 → 4.0.0-beta.30

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
@@ -1,48 +1,26 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- createKlingAI: () => createKlingAI,
24
- klingai: () => klingai
25
- });
26
- module.exports = __toCommonJS(index_exports);
27
-
28
1
  // src/klingai-provider.ts
29
- var import_provider2 = require("@ai-sdk/provider");
30
- var import_provider_utils4 = require("@ai-sdk/provider-utils");
2
+ import {
3
+ NoSuchModelError as NoSuchModelError2
4
+ } from "@ai-sdk/provider";
5
+ import {
6
+ withoutTrailingSlash,
7
+ withUserAgentSuffix
8
+ } from "@ai-sdk/provider-utils";
31
9
 
32
10
  // src/klingai-auth.ts
33
- var import_provider_utils = require("@ai-sdk/provider-utils");
11
+ import { loadSetting } from "@ai-sdk/provider-utils";
34
12
  var base64url = (str) => btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
35
13
  async function generateKlingAIAuthToken({
36
14
  accessKey,
37
15
  secretKey
38
16
  }) {
39
- const ak = (0, import_provider_utils.loadSetting)({
17
+ const ak = loadSetting({
40
18
  settingValue: accessKey,
41
19
  settingName: "accessKey",
42
20
  environmentVariableName: "KLINGAI_ACCESS_KEY",
43
21
  description: "KlingAI access key"
44
22
  });
45
- const sk = (0, import_provider_utils.loadSetting)({
23
+ const sk = loadSetting({
46
24
  settingValue: secretKey,
47
25
  settingName: "secretKey",
48
26
  environmentVariableName: "KLINGAI_SECRET_KEY",
@@ -81,18 +59,32 @@ async function generateKlingAIAuthToken({
81
59
  }
82
60
 
83
61
  // src/klingai-video-model.ts
84
- var import_provider = require("@ai-sdk/provider");
85
- var import_provider_utils3 = require("@ai-sdk/provider-utils");
86
- var import_v42 = require("zod/v4");
62
+ import {
63
+ AISDKError,
64
+ NoSuchModelError
65
+ } from "@ai-sdk/provider";
66
+ import {
67
+ combineHeaders,
68
+ convertUint8ArrayToBase64,
69
+ createJsonResponseHandler,
70
+ delay,
71
+ getFromApi,
72
+ lazySchema,
73
+ parseProviderOptions,
74
+ postJsonToApi,
75
+ resolve,
76
+ zodSchema
77
+ } from "@ai-sdk/provider-utils";
78
+ import { z as z2 } from "zod/v4";
87
79
 
88
80
  // src/klingai-error.ts
89
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
90
- var import_v4 = require("zod/v4");
91
- var klingaiErrorDataSchema = import_v4.z.object({
92
- code: import_v4.z.number(),
93
- message: import_v4.z.string()
81
+ import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
82
+ import { z } from "zod/v4";
83
+ var klingaiErrorDataSchema = z.object({
84
+ code: z.number(),
85
+ message: z.string()
94
86
  });
95
- var klingaiFailedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)({
87
+ var klingaiFailedResponseHandler = createJsonErrorResponseHandler({
96
88
  errorSchema: klingaiErrorDataSchema,
97
89
  errorToMessage: (data) => data.message
98
90
  });
@@ -102,7 +94,7 @@ function detectMode(modelId) {
102
94
  if (modelId.endsWith("-t2v")) return "t2v";
103
95
  if (modelId.endsWith("-i2v")) return "i2v";
104
96
  if (modelId.endsWith("-motion-control")) return "motion-control";
105
- throw new import_provider.NoSuchModelError({ modelId, modelType: "videoModel" });
97
+ throw new NoSuchModelError({ modelId, modelType: "videoModel" });
106
98
  }
107
99
  var modeEndpointMap = {
108
100
  t2v: "/v1/videos/text2video",
@@ -114,69 +106,69 @@ function getApiModelName(modelId, mode) {
114
106
  const baseName = modelId.slice(0, -suffix.length);
115
107
  return baseName.replace(/\.0$/, "").replace(/\./g, "-");
116
108
  }
117
- var klingaiVideoModelOptionsSchema = (0, import_provider_utils3.lazySchema)(
118
- () => (0, import_provider_utils3.zodSchema)(
119
- import_v42.z.object({
120
- mode: import_v42.z.enum(["std", "pro"]).nullish(),
121
- pollIntervalMs: import_v42.z.number().positive().nullish(),
122
- pollTimeoutMs: import_v42.z.number().positive().nullish(),
109
+ var klingaiVideoModelOptionsSchema = lazySchema(
110
+ () => zodSchema(
111
+ z2.object({
112
+ mode: z2.enum(["std", "pro"]).nullish(),
113
+ pollIntervalMs: z2.number().positive().nullish(),
114
+ pollTimeoutMs: z2.number().positive().nullish(),
123
115
  // T2V and I2V
124
- negativePrompt: import_v42.z.string().nullish(),
125
- sound: import_v42.z.enum(["on", "off"]).nullish(),
126
- cfgScale: import_v42.z.number().nullish(),
127
- cameraControl: import_v42.z.object({
128
- type: import_v42.z.enum([
116
+ negativePrompt: z2.string().nullish(),
117
+ sound: z2.enum(["on", "off"]).nullish(),
118
+ cfgScale: z2.number().nullish(),
119
+ cameraControl: z2.object({
120
+ type: z2.enum([
129
121
  "simple",
130
122
  "down_back",
131
123
  "forward_up",
132
124
  "right_turn_forward",
133
125
  "left_turn_forward"
134
126
  ]),
135
- config: import_v42.z.object({
136
- horizontal: import_v42.z.number().nullish(),
137
- vertical: import_v42.z.number().nullish(),
138
- pan: import_v42.z.number().nullish(),
139
- tilt: import_v42.z.number().nullish(),
140
- roll: import_v42.z.number().nullish(),
141
- zoom: import_v42.z.number().nullish()
127
+ config: z2.object({
128
+ horizontal: z2.number().nullish(),
129
+ vertical: z2.number().nullish(),
130
+ pan: z2.number().nullish(),
131
+ tilt: z2.number().nullish(),
132
+ roll: z2.number().nullish(),
133
+ zoom: z2.number().nullish()
142
134
  }).nullish()
143
135
  }).nullish(),
144
136
  // v3.0 multi-shot
145
- multiShot: import_v42.z.boolean().nullish(),
146
- shotType: import_v42.z.enum(["customize", "intelligence"]).nullish(),
147
- multiPrompt: import_v42.z.array(
148
- import_v42.z.object({
149
- index: import_v42.z.number(),
150
- prompt: import_v42.z.string(),
151
- duration: import_v42.z.string()
137
+ multiShot: z2.boolean().nullish(),
138
+ shotType: z2.enum(["customize", "intelligence"]).nullish(),
139
+ multiPrompt: z2.array(
140
+ z2.object({
141
+ index: z2.number(),
142
+ prompt: z2.string(),
143
+ duration: z2.string()
152
144
  })
153
145
  ).nullish(),
154
146
  // v3.0 element control (I2V)
155
- elementList: import_v42.z.array(
156
- import_v42.z.object({
157
- element_id: import_v42.z.number()
147
+ elementList: z2.array(
148
+ z2.object({
149
+ element_id: z2.number()
158
150
  })
159
151
  ).nullish(),
160
152
  // v3.0 voice control
161
- voiceList: import_v42.z.array(
162
- import_v42.z.object({
163
- voice_id: import_v42.z.string()
153
+ voiceList: z2.array(
154
+ z2.object({
155
+ voice_id: z2.string()
164
156
  })
165
157
  ).nullish(),
166
158
  // I2V-specific
167
- imageTail: import_v42.z.string().nullish(),
168
- staticMask: import_v42.z.string().nullish(),
169
- dynamicMasks: import_v42.z.array(
170
- import_v42.z.object({
171
- mask: import_v42.z.string(),
172
- trajectories: import_v42.z.array(import_v42.z.object({ x: import_v42.z.number(), y: import_v42.z.number() }))
159
+ imageTail: z2.string().nullish(),
160
+ staticMask: z2.string().nullish(),
161
+ dynamicMasks: z2.array(
162
+ z2.object({
163
+ mask: z2.string(),
164
+ trajectories: z2.array(z2.object({ x: z2.number(), y: z2.number() }))
173
165
  })
174
166
  ).nullish(),
175
167
  // Motion-control-specific
176
- videoUrl: import_v42.z.string().nullish(),
177
- characterOrientation: import_v42.z.enum(["image", "video"]).nullish(),
178
- keepOriginalSound: import_v42.z.enum(["yes", "no"]).nullish(),
179
- watermarkEnabled: import_v42.z.boolean().nullish()
168
+ videoUrl: z2.string().nullish(),
169
+ characterOrientation: z2.enum(["image", "video"]).nullish(),
170
+ keepOriginalSound: z2.enum(["yes", "no"]).nullish(),
171
+ watermarkEnabled: z2.boolean().nullish()
180
172
  }).passthrough()
181
173
  )
182
174
  );
@@ -205,7 +197,7 @@ var KlingAIVideoModel = class {
205
197
  constructor(modelId, config) {
206
198
  this.modelId = modelId;
207
199
  this.config = config;
208
- this.specificationVersion = "v3";
200
+ this.specificationVersion = "v4";
209
201
  this.maxVideosPerCall = 1;
210
202
  }
211
203
  get provider() {
@@ -216,7 +208,7 @@ var KlingAIVideoModel = class {
216
208
  const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
217
209
  const warnings = [];
218
210
  const mode = detectMode(this.modelId);
219
- const klingaiOptions = await (0, import_provider_utils3.parseProviderOptions)({
211
+ const klingaiOptions = await parseProviderOptions({
220
212
  provider: "klingai",
221
213
  providerOptions: options.providerOptions,
222
214
  schema: klingaiVideoModelOptionsSchema
@@ -258,14 +250,14 @@ var KlingAIVideoModel = class {
258
250
  });
259
251
  }
260
252
  const endpointPath = modeEndpointMap[mode];
261
- const { value: createResponse, responseHeaders: createHeaders } = await (0, import_provider_utils3.postJsonToApi)({
253
+ const { value: createResponse, responseHeaders: createHeaders } = await postJsonToApi({
262
254
  url: `${this.config.baseURL}${endpointPath}`,
263
- headers: (0, import_provider_utils3.combineHeaders)(
264
- await (0, import_provider_utils3.resolve)(this.config.headers),
255
+ headers: combineHeaders(
256
+ await resolve(this.config.headers),
265
257
  options.headers
266
258
  ),
267
259
  body,
268
- successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
260
+ successfulResponseHandler: createJsonResponseHandler(
269
261
  klingaiCreateTaskSchema
270
262
  ),
271
263
  failedResponseHandler: klingaiFailedResponseHandler,
@@ -274,7 +266,7 @@ var KlingAIVideoModel = class {
274
266
  });
275
267
  const taskId = (_d = createResponse.data) == null ? void 0 : _d.task_id;
276
268
  if (!taskId) {
277
- throw new import_provider.AISDKError({
269
+ throw new AISDKError({
278
270
  name: "KLINGAI_VIDEO_GENERATION_ERROR",
279
271
  message: `No task_id returned from KlingAI API. Response: ${JSON.stringify(createResponse)}`
280
272
  });
@@ -285,20 +277,20 @@ var KlingAIVideoModel = class {
285
277
  let finalResponse;
286
278
  let responseHeaders = createHeaders;
287
279
  while (true) {
288
- await (0, import_provider_utils3.delay)(pollIntervalMs, { abortSignal: options.abortSignal });
280
+ await delay(pollIntervalMs, { abortSignal: options.abortSignal });
289
281
  if (Date.now() - startTime > pollTimeoutMs) {
290
- throw new import_provider.AISDKError({
282
+ throw new AISDKError({
291
283
  name: "KLINGAI_VIDEO_GENERATION_TIMEOUT",
292
284
  message: `Video generation timed out after ${pollTimeoutMs}ms`
293
285
  });
294
286
  }
295
- const { value: statusResponse, responseHeaders: pollHeaders } = await (0, import_provider_utils3.getFromApi)({
287
+ const { value: statusResponse, responseHeaders: pollHeaders } = await getFromApi({
296
288
  url: `${this.config.baseURL}${endpointPath}/${taskId}`,
297
- headers: (0, import_provider_utils3.combineHeaders)(
298
- await (0, import_provider_utils3.resolve)(this.config.headers),
289
+ headers: combineHeaders(
290
+ await resolve(this.config.headers),
299
291
  options.headers
300
292
  ),
301
- successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
293
+ successfulResponseHandler: createJsonResponseHandler(
302
294
  klingaiTaskStatusSchema
303
295
  ),
304
296
  failedResponseHandler: klingaiFailedResponseHandler,
@@ -312,14 +304,14 @@ var KlingAIVideoModel = class {
312
304
  break;
313
305
  }
314
306
  if (taskStatus === "failed") {
315
- throw new import_provider.AISDKError({
307
+ throw new AISDKError({
316
308
  name: "KLINGAI_VIDEO_GENERATION_FAILED",
317
309
  message: `Video generation failed: ${(_i = (_h = statusResponse.data) == null ? void 0 : _h.task_status_msg) != null ? _i : "Unknown error"}`
318
310
  });
319
311
  }
320
312
  }
321
313
  if (!((_l = (_k = (_j = finalResponse == null ? void 0 : finalResponse.data) == null ? void 0 : _j.task_result) == null ? void 0 : _k.videos) == null ? void 0 : _l.length)) {
322
- throw new import_provider.AISDKError({
314
+ throw new AISDKError({
323
315
  name: "KLINGAI_VIDEO_GENERATION_ERROR",
324
316
  message: `No videos in response. Response: ${JSON.stringify(finalResponse)}`
325
317
  });
@@ -342,7 +334,7 @@ var KlingAIVideoModel = class {
342
334
  }
343
335
  }
344
336
  if (videos.length === 0) {
345
- throw new import_provider.AISDKError({
337
+ throw new AISDKError({
346
338
  name: "KLINGAI_VIDEO_GENERATION_ERROR",
347
339
  message: "No valid video URLs in response"
348
340
  });
@@ -429,7 +421,7 @@ var KlingAIVideoModel = class {
429
421
  if (options.image.type === "url") {
430
422
  body.image = options.image.url;
431
423
  } else {
432
- body.image = typeof options.image.data === "string" ? options.image.data : (0, import_provider_utils3.convertUint8ArrayToBase64)(options.image.data);
424
+ body.image = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase64(options.image.data);
433
425
  }
434
426
  }
435
427
  if ((klingaiOptions == null ? void 0 : klingaiOptions.imageTail) != null) {
@@ -489,7 +481,7 @@ var KlingAIVideoModel = class {
489
481
  }
490
482
  buildMotionControlBody(options, klingaiOptions, warnings) {
491
483
  if (!(klingaiOptions == null ? void 0 : klingaiOptions.videoUrl) || !(klingaiOptions == null ? void 0 : klingaiOptions.characterOrientation) || !(klingaiOptions == null ? void 0 : klingaiOptions.mode)) {
492
- throw new import_provider.AISDKError({
484
+ throw new AISDKError({
493
485
  name: "KLINGAI_VIDEO_MISSING_OPTIONS",
494
486
  message: "KlingAI Motion Control requires providerOptions.klingai with videoUrl, characterOrientation, and mode."
495
487
  });
@@ -508,7 +500,7 @@ var KlingAIVideoModel = class {
508
500
  if (options.image.type === "url") {
509
501
  body.image_url = options.image.url;
510
502
  } else {
511
- body.image_url = typeof options.image.data === "string" ? options.image.data : (0, import_provider_utils3.convertUint8ArrayToBase64)(options.image.data);
503
+ body.image_url = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase64(options.image.data);
512
504
  }
513
505
  }
514
506
  if (klingaiOptions.keepOriginalSound != null) {
@@ -546,44 +538,44 @@ var KlingAIVideoModel = class {
546
538
  }
547
539
  }
548
540
  };
549
- var klingaiCreateTaskSchema = import_v42.z.object({
550
- code: import_v42.z.number(),
551
- message: import_v42.z.string(),
552
- request_id: import_v42.z.string().nullish(),
553
- data: import_v42.z.object({
554
- task_id: import_v42.z.string(),
555
- task_status: import_v42.z.string().nullish(),
556
- task_info: import_v42.z.object({
557
- external_task_id: import_v42.z.string().nullish()
541
+ var klingaiCreateTaskSchema = z2.object({
542
+ code: z2.number(),
543
+ message: z2.string(),
544
+ request_id: z2.string().nullish(),
545
+ data: z2.object({
546
+ task_id: z2.string(),
547
+ task_status: z2.string().nullish(),
548
+ task_info: z2.object({
549
+ external_task_id: z2.string().nullish()
558
550
  }).nullish(),
559
- created_at: import_v42.z.number().nullish(),
560
- updated_at: import_v42.z.number().nullish()
551
+ created_at: z2.number().nullish(),
552
+ updated_at: z2.number().nullish()
561
553
  }).nullish()
562
554
  });
563
- var klingaiTaskStatusSchema = import_v42.z.object({
564
- code: import_v42.z.number(),
565
- message: import_v42.z.string(),
566
- request_id: import_v42.z.string().nullish(),
567
- data: import_v42.z.object({
568
- task_id: import_v42.z.string(),
569
- task_status: import_v42.z.string(),
570
- task_status_msg: import_v42.z.string().nullish(),
571
- task_info: import_v42.z.object({
572
- external_task_id: import_v42.z.string().nullish()
555
+ var klingaiTaskStatusSchema = z2.object({
556
+ code: z2.number(),
557
+ message: z2.string(),
558
+ request_id: z2.string().nullish(),
559
+ data: z2.object({
560
+ task_id: z2.string(),
561
+ task_status: z2.string(),
562
+ task_status_msg: z2.string().nullish(),
563
+ task_info: z2.object({
564
+ external_task_id: z2.string().nullish()
573
565
  }).nullish(),
574
- watermark_info: import_v42.z.object({
575
- enabled: import_v42.z.boolean().nullish()
566
+ watermark_info: z2.object({
567
+ enabled: z2.boolean().nullish()
576
568
  }).nullish(),
577
- final_unit_deduction: import_v42.z.string().nullish(),
578
- created_at: import_v42.z.number().nullish(),
579
- updated_at: import_v42.z.number().nullish(),
580
- task_result: import_v42.z.object({
581
- videos: import_v42.z.array(
582
- import_v42.z.object({
583
- id: import_v42.z.string().nullish(),
584
- url: import_v42.z.string().nullish(),
585
- watermark_url: import_v42.z.string().nullish(),
586
- duration: import_v42.z.string().nullish()
569
+ final_unit_deduction: z2.string().nullish(),
570
+ created_at: z2.number().nullish(),
571
+ updated_at: z2.number().nullish(),
572
+ task_result: z2.object({
573
+ videos: z2.array(
574
+ z2.object({
575
+ id: z2.string().nullish(),
576
+ url: z2.string().nullish(),
577
+ watermark_url: z2.string().nullish(),
578
+ duration: z2.string().nullish()
587
579
  })
588
580
  ).nullish()
589
581
  }).nullish()
@@ -591,19 +583,19 @@ var klingaiTaskStatusSchema = import_v42.z.object({
591
583
  });
592
584
 
593
585
  // src/version.ts
594
- var VERSION = "4.0.0-beta.3";
586
+ var VERSION = "4.0.0-beta.30";
595
587
 
596
588
  // src/klingai-provider.ts
597
589
  var defaultBaseURL = "https://api-singapore.klingai.com";
598
590
  function createKlingAI(options = {}) {
599
591
  var _a, _b;
600
- const baseURL = (_b = (0, import_provider_utils4.withoutTrailingSlash)((_a = options.baseURL) != null ? _a : defaultBaseURL)) != null ? _b : defaultBaseURL;
592
+ const baseURL = (_b = withoutTrailingSlash((_a = options.baseURL) != null ? _a : defaultBaseURL)) != null ? _b : defaultBaseURL;
601
593
  const getHeaders = async () => {
602
594
  const token = await generateKlingAIAuthToken({
603
595
  accessKey: options.accessKey,
604
596
  secretKey: options.secretKey
605
597
  });
606
- return (0, import_provider_utils4.withUserAgentSuffix)(
598
+ return withUserAgentSuffix(
607
599
  {
608
600
  Authorization: `Bearer ${token}`,
609
601
  ...options.headers
@@ -618,10 +610,10 @@ function createKlingAI(options = {}) {
618
610
  fetch: options.fetch
619
611
  });
620
612
  const noSuchModel = (modelId, modelType) => {
621
- throw new import_provider2.NoSuchModelError({ modelId, modelType });
613
+ throw new NoSuchModelError2({ modelId, modelType });
622
614
  };
623
615
  const provider = {
624
- specificationVersion: "v3",
616
+ specificationVersion: "v4",
625
617
  video: createVideoModel,
626
618
  videoModel: createVideoModel,
627
619
  languageModel: (modelId) => noSuchModel(modelId, "languageModel"),
@@ -631,9 +623,8 @@ function createKlingAI(options = {}) {
631
623
  return provider;
632
624
  }
633
625
  var klingai = createKlingAI();
634
- // Annotate the CommonJS export names for ESM import in node:
635
- 0 && (module.exports = {
626
+ export {
636
627
  createKlingAI,
637
628
  klingai
638
- });
629
+ };
639
630
  //# sourceMappingURL=index.js.map