@ai-sdk/klingai 4.0.0-beta.4 → 4.0.0-beta.51

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