@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/CHANGELOG.md +214 -0
- package/README.md +2 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +134 -143
- package/dist/index.js.map +1 -1
- package/package.json +11 -11
- package/src/klingai-provider.ts +7 -7
- package/src/klingai-video-model.ts +13 -13
- package/dist/index.d.mts +0 -211
- package/dist/index.mjs +0 -630
- package/dist/index.mjs.map +0 -1
package/dist/index.mjs
DELETED
|
@@ -1,630 +0,0 @@
|
|
|
1
|
-
// src/klingai-provider.ts
|
|
2
|
-
import {
|
|
3
|
-
NoSuchModelError as NoSuchModelError2
|
|
4
|
-
} from "@ai-sdk/provider";
|
|
5
|
-
import {
|
|
6
|
-
withoutTrailingSlash,
|
|
7
|
-
withUserAgentSuffix
|
|
8
|
-
} from "@ai-sdk/provider-utils";
|
|
9
|
-
|
|
10
|
-
// src/klingai-auth.ts
|
|
11
|
-
import { loadSetting } from "@ai-sdk/provider-utils";
|
|
12
|
-
var base64url = (str) => btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
13
|
-
async function generateKlingAIAuthToken({
|
|
14
|
-
accessKey,
|
|
15
|
-
secretKey
|
|
16
|
-
}) {
|
|
17
|
-
const ak = loadSetting({
|
|
18
|
-
settingValue: accessKey,
|
|
19
|
-
settingName: "accessKey",
|
|
20
|
-
environmentVariableName: "KLINGAI_ACCESS_KEY",
|
|
21
|
-
description: "KlingAI access key"
|
|
22
|
-
});
|
|
23
|
-
const sk = loadSetting({
|
|
24
|
-
settingValue: secretKey,
|
|
25
|
-
settingName: "secretKey",
|
|
26
|
-
environmentVariableName: "KLINGAI_SECRET_KEY",
|
|
27
|
-
description: "KlingAI secret key"
|
|
28
|
-
});
|
|
29
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
30
|
-
const header = { alg: "HS256", typ: "JWT" };
|
|
31
|
-
const payload = {
|
|
32
|
-
iss: ak,
|
|
33
|
-
exp: now + 1800,
|
|
34
|
-
// Valid for 30 minutes
|
|
35
|
-
nbf: now - 5
|
|
36
|
-
// Valid 5 seconds before current time
|
|
37
|
-
};
|
|
38
|
-
const encoder = new TextEncoder();
|
|
39
|
-
const key = await crypto.subtle.importKey(
|
|
40
|
-
"raw",
|
|
41
|
-
encoder.encode(sk),
|
|
42
|
-
{ name: "HMAC", hash: "SHA-256" },
|
|
43
|
-
false,
|
|
44
|
-
["sign"]
|
|
45
|
-
);
|
|
46
|
-
const signingInput = `${base64url(JSON.stringify(header))}.${base64url(
|
|
47
|
-
JSON.stringify(payload)
|
|
48
|
-
)}`;
|
|
49
|
-
const signature = await crypto.subtle.sign(
|
|
50
|
-
"HMAC",
|
|
51
|
-
key,
|
|
52
|
-
encoder.encode(signingInput)
|
|
53
|
-
);
|
|
54
|
-
const signatureBytes = new Uint8Array(signature);
|
|
55
|
-
const signatureBase64 = base64url(
|
|
56
|
-
String.fromCharCode.apply(null, Array.from(signatureBytes))
|
|
57
|
-
);
|
|
58
|
-
return `${signingInput}.${signatureBase64}`;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// src/klingai-video-model.ts
|
|
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";
|
|
79
|
-
|
|
80
|
-
// src/klingai-error.ts
|
|
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()
|
|
86
|
-
});
|
|
87
|
-
var klingaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
88
|
-
errorSchema: klingaiErrorDataSchema,
|
|
89
|
-
errorToMessage: (data) => data.message
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
// src/klingai-video-model.ts
|
|
93
|
-
function detectMode(modelId) {
|
|
94
|
-
if (modelId.endsWith("-t2v")) return "t2v";
|
|
95
|
-
if (modelId.endsWith("-i2v")) return "i2v";
|
|
96
|
-
if (modelId.endsWith("-motion-control")) return "motion-control";
|
|
97
|
-
throw new NoSuchModelError({ modelId, modelType: "videoModel" });
|
|
98
|
-
}
|
|
99
|
-
var modeEndpointMap = {
|
|
100
|
-
t2v: "/v1/videos/text2video",
|
|
101
|
-
i2v: "/v1/videos/image2video",
|
|
102
|
-
"motion-control": "/v1/videos/motion-control"
|
|
103
|
-
};
|
|
104
|
-
function getApiModelName(modelId, mode) {
|
|
105
|
-
const suffix = mode === "motion-control" ? "-motion-control" : `-${mode}`;
|
|
106
|
-
const baseName = modelId.slice(0, -suffix.length);
|
|
107
|
-
return baseName.replace(/\.0$/, "").replace(/\./g, "-");
|
|
108
|
-
}
|
|
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(),
|
|
115
|
-
// T2V and I2V
|
|
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([
|
|
121
|
-
"simple",
|
|
122
|
-
"down_back",
|
|
123
|
-
"forward_up",
|
|
124
|
-
"right_turn_forward",
|
|
125
|
-
"left_turn_forward"
|
|
126
|
-
]),
|
|
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()
|
|
134
|
-
}).nullish()
|
|
135
|
-
}).nullish(),
|
|
136
|
-
// v3.0 multi-shot
|
|
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()
|
|
144
|
-
})
|
|
145
|
-
).nullish(),
|
|
146
|
-
// v3.0 element control (I2V)
|
|
147
|
-
elementList: z2.array(
|
|
148
|
-
z2.object({
|
|
149
|
-
element_id: z2.number()
|
|
150
|
-
})
|
|
151
|
-
).nullish(),
|
|
152
|
-
// v3.0 voice control
|
|
153
|
-
voiceList: z2.array(
|
|
154
|
-
z2.object({
|
|
155
|
-
voice_id: z2.string()
|
|
156
|
-
})
|
|
157
|
-
).nullish(),
|
|
158
|
-
// I2V-specific
|
|
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() }))
|
|
165
|
-
})
|
|
166
|
-
).nullish(),
|
|
167
|
-
// Motion-control-specific
|
|
168
|
-
videoUrl: z2.string().nullish(),
|
|
169
|
-
characterOrientation: z2.enum(["image", "video"]).nullish(),
|
|
170
|
-
keepOriginalSound: z2.enum(["yes", "no"]).nullish(),
|
|
171
|
-
watermarkEnabled: z2.boolean().nullish()
|
|
172
|
-
}).passthrough()
|
|
173
|
-
)
|
|
174
|
-
);
|
|
175
|
-
var HANDLED_PROVIDER_OPTIONS = /* @__PURE__ */ new Set([
|
|
176
|
-
"mode",
|
|
177
|
-
"pollIntervalMs",
|
|
178
|
-
"pollTimeoutMs",
|
|
179
|
-
"negativePrompt",
|
|
180
|
-
"sound",
|
|
181
|
-
"cfgScale",
|
|
182
|
-
"cameraControl",
|
|
183
|
-
"multiShot",
|
|
184
|
-
"shotType",
|
|
185
|
-
"multiPrompt",
|
|
186
|
-
"elementList",
|
|
187
|
-
"voiceList",
|
|
188
|
-
"imageTail",
|
|
189
|
-
"staticMask",
|
|
190
|
-
"dynamicMasks",
|
|
191
|
-
"videoUrl",
|
|
192
|
-
"characterOrientation",
|
|
193
|
-
"keepOriginalSound",
|
|
194
|
-
"watermarkEnabled"
|
|
195
|
-
]);
|
|
196
|
-
var KlingAIVideoModel = class {
|
|
197
|
-
constructor(modelId, config) {
|
|
198
|
-
this.modelId = modelId;
|
|
199
|
-
this.config = config;
|
|
200
|
-
this.specificationVersion = "v3";
|
|
201
|
-
this.maxVideosPerCall = 1;
|
|
202
|
-
}
|
|
203
|
-
get provider() {
|
|
204
|
-
return this.config.provider;
|
|
205
|
-
}
|
|
206
|
-
async doGenerate(options) {
|
|
207
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
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();
|
|
209
|
-
const warnings = [];
|
|
210
|
-
const mode = detectMode(this.modelId);
|
|
211
|
-
const klingaiOptions = await parseProviderOptions({
|
|
212
|
-
provider: "klingai",
|
|
213
|
-
providerOptions: options.providerOptions,
|
|
214
|
-
schema: klingaiVideoModelOptionsSchema
|
|
215
|
-
});
|
|
216
|
-
let body;
|
|
217
|
-
if (mode === "motion-control") {
|
|
218
|
-
body = this.buildMotionControlBody(options, klingaiOptions, warnings);
|
|
219
|
-
} else if (mode === "t2v") {
|
|
220
|
-
body = this.buildT2VBody(options, klingaiOptions, warnings);
|
|
221
|
-
} else {
|
|
222
|
-
body = this.buildI2VBody(options, klingaiOptions, warnings);
|
|
223
|
-
}
|
|
224
|
-
if (options.resolution) {
|
|
225
|
-
warnings.push({
|
|
226
|
-
type: "unsupported",
|
|
227
|
-
feature: "resolution",
|
|
228
|
-
details: "KlingAI video models do not support the resolution option."
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
if (options.seed) {
|
|
232
|
-
warnings.push({
|
|
233
|
-
type: "unsupported",
|
|
234
|
-
feature: "seed",
|
|
235
|
-
details: "KlingAI video models do not support seed for deterministic generation."
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
if (options.fps) {
|
|
239
|
-
warnings.push({
|
|
240
|
-
type: "unsupported",
|
|
241
|
-
feature: "fps",
|
|
242
|
-
details: "KlingAI video models do not support custom FPS."
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
if (options.n != null && options.n > 1) {
|
|
246
|
-
warnings.push({
|
|
247
|
-
type: "unsupported",
|
|
248
|
-
feature: "n",
|
|
249
|
-
details: "KlingAI video models do not support generating multiple videos per call. Only 1 video will be generated."
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
const endpointPath = modeEndpointMap[mode];
|
|
253
|
-
const { value: createResponse, responseHeaders: createHeaders } = await postJsonToApi({
|
|
254
|
-
url: `${this.config.baseURL}${endpointPath}`,
|
|
255
|
-
headers: combineHeaders(
|
|
256
|
-
await resolve(this.config.headers),
|
|
257
|
-
options.headers
|
|
258
|
-
),
|
|
259
|
-
body,
|
|
260
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
261
|
-
klingaiCreateTaskSchema
|
|
262
|
-
),
|
|
263
|
-
failedResponseHandler: klingaiFailedResponseHandler,
|
|
264
|
-
abortSignal: options.abortSignal,
|
|
265
|
-
fetch: this.config.fetch
|
|
266
|
-
});
|
|
267
|
-
const taskId = (_d = createResponse.data) == null ? void 0 : _d.task_id;
|
|
268
|
-
if (!taskId) {
|
|
269
|
-
throw new AISDKError({
|
|
270
|
-
name: "KLINGAI_VIDEO_GENERATION_ERROR",
|
|
271
|
-
message: `No task_id returned from KlingAI API. Response: ${JSON.stringify(createResponse)}`
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
const pollIntervalMs = (_e = klingaiOptions == null ? void 0 : klingaiOptions.pollIntervalMs) != null ? _e : 5e3;
|
|
275
|
-
const pollTimeoutMs = (_f = klingaiOptions == null ? void 0 : klingaiOptions.pollTimeoutMs) != null ? _f : 6e5;
|
|
276
|
-
const startTime = Date.now();
|
|
277
|
-
let finalResponse;
|
|
278
|
-
let responseHeaders = createHeaders;
|
|
279
|
-
while (true) {
|
|
280
|
-
await delay(pollIntervalMs, { abortSignal: options.abortSignal });
|
|
281
|
-
if (Date.now() - startTime > pollTimeoutMs) {
|
|
282
|
-
throw new AISDKError({
|
|
283
|
-
name: "KLINGAI_VIDEO_GENERATION_TIMEOUT",
|
|
284
|
-
message: `Video generation timed out after ${pollTimeoutMs}ms`
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
const { value: statusResponse, responseHeaders: pollHeaders } = await getFromApi({
|
|
288
|
-
url: `${this.config.baseURL}${endpointPath}/${taskId}`,
|
|
289
|
-
headers: combineHeaders(
|
|
290
|
-
await resolve(this.config.headers),
|
|
291
|
-
options.headers
|
|
292
|
-
),
|
|
293
|
-
successfulResponseHandler: createJsonResponseHandler(
|
|
294
|
-
klingaiTaskStatusSchema
|
|
295
|
-
),
|
|
296
|
-
failedResponseHandler: klingaiFailedResponseHandler,
|
|
297
|
-
abortSignal: options.abortSignal,
|
|
298
|
-
fetch: this.config.fetch
|
|
299
|
-
});
|
|
300
|
-
responseHeaders = pollHeaders;
|
|
301
|
-
const taskStatus = (_g = statusResponse.data) == null ? void 0 : _g.task_status;
|
|
302
|
-
if (taskStatus === "succeed") {
|
|
303
|
-
finalResponse = statusResponse;
|
|
304
|
-
break;
|
|
305
|
-
}
|
|
306
|
-
if (taskStatus === "failed") {
|
|
307
|
-
throw new AISDKError({
|
|
308
|
-
name: "KLINGAI_VIDEO_GENERATION_FAILED",
|
|
309
|
-
message: `Video generation failed: ${(_i = (_h = statusResponse.data) == null ? void 0 : _h.task_status_msg) != null ? _i : "Unknown error"}`
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
}
|
|
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)) {
|
|
314
|
-
throw new AISDKError({
|
|
315
|
-
name: "KLINGAI_VIDEO_GENERATION_ERROR",
|
|
316
|
-
message: `No videos in response. Response: ${JSON.stringify(finalResponse)}`
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
const videos = [];
|
|
320
|
-
const videoMetadata = [];
|
|
321
|
-
for (const video of finalResponse.data.task_result.videos) {
|
|
322
|
-
if (video.url) {
|
|
323
|
-
videos.push({
|
|
324
|
-
type: "url",
|
|
325
|
-
url: video.url,
|
|
326
|
-
mediaType: "video/mp4"
|
|
327
|
-
});
|
|
328
|
-
videoMetadata.push({
|
|
329
|
-
id: (_m = video.id) != null ? _m : "",
|
|
330
|
-
url: video.url,
|
|
331
|
-
...video.watermark_url ? { watermarkUrl: video.watermark_url } : {},
|
|
332
|
-
...video.duration ? { duration: video.duration } : {}
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
if (videos.length === 0) {
|
|
337
|
-
throw new AISDKError({
|
|
338
|
-
name: "KLINGAI_VIDEO_GENERATION_ERROR",
|
|
339
|
-
message: "No valid video URLs in response"
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
return {
|
|
343
|
-
videos,
|
|
344
|
-
warnings,
|
|
345
|
-
response: {
|
|
346
|
-
timestamp: currentDate,
|
|
347
|
-
modelId: this.modelId,
|
|
348
|
-
headers: responseHeaders
|
|
349
|
-
},
|
|
350
|
-
providerMetadata: {
|
|
351
|
-
klingai: {
|
|
352
|
-
taskId,
|
|
353
|
-
videos: videoMetadata
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
};
|
|
357
|
-
}
|
|
358
|
-
buildT2VBody(options, klingaiOptions, warnings) {
|
|
359
|
-
const mode = "t2v";
|
|
360
|
-
const body = {
|
|
361
|
-
model_name: getApiModelName(this.modelId, mode)
|
|
362
|
-
};
|
|
363
|
-
if (options.prompt != null) {
|
|
364
|
-
body.prompt = options.prompt;
|
|
365
|
-
}
|
|
366
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.negativePrompt) != null) {
|
|
367
|
-
body.negative_prompt = klingaiOptions.negativePrompt;
|
|
368
|
-
}
|
|
369
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.sound) != null) {
|
|
370
|
-
body.sound = klingaiOptions.sound;
|
|
371
|
-
}
|
|
372
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.cfgScale) != null) {
|
|
373
|
-
body.cfg_scale = klingaiOptions.cfgScale;
|
|
374
|
-
}
|
|
375
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.mode) != null) {
|
|
376
|
-
body.mode = klingaiOptions.mode;
|
|
377
|
-
}
|
|
378
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.cameraControl) != null) {
|
|
379
|
-
body.camera_control = klingaiOptions.cameraControl;
|
|
380
|
-
}
|
|
381
|
-
if (options.aspectRatio != null) {
|
|
382
|
-
body.aspect_ratio = options.aspectRatio;
|
|
383
|
-
}
|
|
384
|
-
if (options.duration != null) {
|
|
385
|
-
body.duration = String(options.duration);
|
|
386
|
-
}
|
|
387
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.multiShot) != null) {
|
|
388
|
-
body.multi_shot = klingaiOptions.multiShot;
|
|
389
|
-
}
|
|
390
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.shotType) != null) {
|
|
391
|
-
body.shot_type = klingaiOptions.shotType;
|
|
392
|
-
}
|
|
393
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.multiPrompt) != null) {
|
|
394
|
-
body.multi_prompt = klingaiOptions.multiPrompt;
|
|
395
|
-
}
|
|
396
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.voiceList) != null) {
|
|
397
|
-
body.voice_list = klingaiOptions.voiceList;
|
|
398
|
-
}
|
|
399
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.watermarkEnabled) != null) {
|
|
400
|
-
body.watermark_info = { enabled: klingaiOptions.watermarkEnabled };
|
|
401
|
-
}
|
|
402
|
-
if (options.image != null) {
|
|
403
|
-
warnings.push({
|
|
404
|
-
type: "unsupported",
|
|
405
|
-
feature: "image",
|
|
406
|
-
details: "KlingAI text-to-video does not support image input. Use an image-to-video model instead."
|
|
407
|
-
});
|
|
408
|
-
}
|
|
409
|
-
this.addPassthroughOptions(body, klingaiOptions);
|
|
410
|
-
return body;
|
|
411
|
-
}
|
|
412
|
-
buildI2VBody(options, klingaiOptions, warnings) {
|
|
413
|
-
const mode = "i2v";
|
|
414
|
-
const body = {
|
|
415
|
-
model_name: getApiModelName(this.modelId, mode)
|
|
416
|
-
};
|
|
417
|
-
if (options.prompt != null) {
|
|
418
|
-
body.prompt = options.prompt;
|
|
419
|
-
}
|
|
420
|
-
if (options.image != null) {
|
|
421
|
-
if (options.image.type === "url") {
|
|
422
|
-
body.image = options.image.url;
|
|
423
|
-
} else {
|
|
424
|
-
body.image = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase64(options.image.data);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.imageTail) != null) {
|
|
428
|
-
body.image_tail = klingaiOptions.imageTail;
|
|
429
|
-
}
|
|
430
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.negativePrompt) != null) {
|
|
431
|
-
body.negative_prompt = klingaiOptions.negativePrompt;
|
|
432
|
-
}
|
|
433
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.sound) != null) {
|
|
434
|
-
body.sound = klingaiOptions.sound;
|
|
435
|
-
}
|
|
436
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.cfgScale) != null) {
|
|
437
|
-
body.cfg_scale = klingaiOptions.cfgScale;
|
|
438
|
-
}
|
|
439
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.mode) != null) {
|
|
440
|
-
body.mode = klingaiOptions.mode;
|
|
441
|
-
}
|
|
442
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.cameraControl) != null) {
|
|
443
|
-
body.camera_control = klingaiOptions.cameraControl;
|
|
444
|
-
}
|
|
445
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.staticMask) != null) {
|
|
446
|
-
body.static_mask = klingaiOptions.staticMask;
|
|
447
|
-
}
|
|
448
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.dynamicMasks) != null) {
|
|
449
|
-
body.dynamic_masks = klingaiOptions.dynamicMasks;
|
|
450
|
-
}
|
|
451
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.multiShot) != null) {
|
|
452
|
-
body.multi_shot = klingaiOptions.multiShot;
|
|
453
|
-
}
|
|
454
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.shotType) != null) {
|
|
455
|
-
body.shot_type = klingaiOptions.shotType;
|
|
456
|
-
}
|
|
457
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.multiPrompt) != null) {
|
|
458
|
-
body.multi_prompt = klingaiOptions.multiPrompt;
|
|
459
|
-
}
|
|
460
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.elementList) != null) {
|
|
461
|
-
body.element_list = klingaiOptions.elementList;
|
|
462
|
-
}
|
|
463
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.voiceList) != null) {
|
|
464
|
-
body.voice_list = klingaiOptions.voiceList;
|
|
465
|
-
}
|
|
466
|
-
if ((klingaiOptions == null ? void 0 : klingaiOptions.watermarkEnabled) != null) {
|
|
467
|
-
body.watermark_info = { enabled: klingaiOptions.watermarkEnabled };
|
|
468
|
-
}
|
|
469
|
-
if (options.duration != null) {
|
|
470
|
-
body.duration = String(options.duration);
|
|
471
|
-
}
|
|
472
|
-
if (options.aspectRatio != null) {
|
|
473
|
-
warnings.push({
|
|
474
|
-
type: "unsupported",
|
|
475
|
-
feature: "aspectRatio",
|
|
476
|
-
details: "KlingAI image-to-video does not support aspectRatio. The output dimensions are determined by the input image."
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
this.addPassthroughOptions(body, klingaiOptions);
|
|
480
|
-
return body;
|
|
481
|
-
}
|
|
482
|
-
buildMotionControlBody(options, klingaiOptions, warnings) {
|
|
483
|
-
if (!(klingaiOptions == null ? void 0 : klingaiOptions.videoUrl) || !(klingaiOptions == null ? void 0 : klingaiOptions.characterOrientation) || !(klingaiOptions == null ? void 0 : klingaiOptions.mode)) {
|
|
484
|
-
throw new AISDKError({
|
|
485
|
-
name: "KLINGAI_VIDEO_MISSING_OPTIONS",
|
|
486
|
-
message: "KlingAI Motion Control requires providerOptions.klingai with videoUrl, characterOrientation, and mode."
|
|
487
|
-
});
|
|
488
|
-
}
|
|
489
|
-
const mode = "motion-control";
|
|
490
|
-
const body = {
|
|
491
|
-
model_name: getApiModelName(this.modelId, mode),
|
|
492
|
-
video_url: klingaiOptions.videoUrl,
|
|
493
|
-
character_orientation: klingaiOptions.characterOrientation,
|
|
494
|
-
mode: klingaiOptions.mode
|
|
495
|
-
};
|
|
496
|
-
if (options.prompt != null) {
|
|
497
|
-
body.prompt = options.prompt;
|
|
498
|
-
}
|
|
499
|
-
if (options.image != null) {
|
|
500
|
-
if (options.image.type === "url") {
|
|
501
|
-
body.image_url = options.image.url;
|
|
502
|
-
} else {
|
|
503
|
-
body.image_url = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase64(options.image.data);
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
if (klingaiOptions.keepOriginalSound != null) {
|
|
507
|
-
body.keep_original_sound = klingaiOptions.keepOriginalSound;
|
|
508
|
-
}
|
|
509
|
-
if (klingaiOptions.watermarkEnabled != null) {
|
|
510
|
-
body.watermark_info = { enabled: klingaiOptions.watermarkEnabled };
|
|
511
|
-
}
|
|
512
|
-
if (klingaiOptions.elementList != null) {
|
|
513
|
-
body.element_list = klingaiOptions.elementList;
|
|
514
|
-
}
|
|
515
|
-
if (options.aspectRatio) {
|
|
516
|
-
warnings.push({
|
|
517
|
-
type: "unsupported",
|
|
518
|
-
feature: "aspectRatio",
|
|
519
|
-
details: "KlingAI Motion Control does not support aspectRatio. The output dimensions are determined by the reference image/video."
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
if (options.duration) {
|
|
523
|
-
warnings.push({
|
|
524
|
-
type: "unsupported",
|
|
525
|
-
feature: "duration",
|
|
526
|
-
details: "KlingAI Motion Control does not support custom duration. The output duration matches the reference video duration."
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
this.addPassthroughOptions(body, klingaiOptions);
|
|
530
|
-
return body;
|
|
531
|
-
}
|
|
532
|
-
addPassthroughOptions(body, klingaiOptions) {
|
|
533
|
-
if (!klingaiOptions) return;
|
|
534
|
-
for (const [key, value] of Object.entries(klingaiOptions)) {
|
|
535
|
-
if (!HANDLED_PROVIDER_OPTIONS.has(key)) {
|
|
536
|
-
body[key] = value;
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
};
|
|
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()
|
|
550
|
-
}).nullish(),
|
|
551
|
-
created_at: z2.number().nullish(),
|
|
552
|
-
updated_at: z2.number().nullish()
|
|
553
|
-
}).nullish()
|
|
554
|
-
});
|
|
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()
|
|
565
|
-
}).nullish(),
|
|
566
|
-
watermark_info: z2.object({
|
|
567
|
-
enabled: z2.boolean().nullish()
|
|
568
|
-
}).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()
|
|
579
|
-
})
|
|
580
|
-
).nullish()
|
|
581
|
-
}).nullish()
|
|
582
|
-
}).nullish()
|
|
583
|
-
});
|
|
584
|
-
|
|
585
|
-
// src/version.ts
|
|
586
|
-
var VERSION = "4.0.0-beta.3";
|
|
587
|
-
|
|
588
|
-
// src/klingai-provider.ts
|
|
589
|
-
var defaultBaseURL = "https://api-singapore.klingai.com";
|
|
590
|
-
function createKlingAI(options = {}) {
|
|
591
|
-
var _a, _b;
|
|
592
|
-
const baseURL = (_b = withoutTrailingSlash((_a = options.baseURL) != null ? _a : defaultBaseURL)) != null ? _b : defaultBaseURL;
|
|
593
|
-
const getHeaders = async () => {
|
|
594
|
-
const token = await generateKlingAIAuthToken({
|
|
595
|
-
accessKey: options.accessKey,
|
|
596
|
-
secretKey: options.secretKey
|
|
597
|
-
});
|
|
598
|
-
return withUserAgentSuffix(
|
|
599
|
-
{
|
|
600
|
-
Authorization: `Bearer ${token}`,
|
|
601
|
-
...options.headers
|
|
602
|
-
},
|
|
603
|
-
`ai-sdk/klingai/${VERSION}`
|
|
604
|
-
);
|
|
605
|
-
};
|
|
606
|
-
const createVideoModel = (modelId) => new KlingAIVideoModel(modelId, {
|
|
607
|
-
provider: "klingai.video",
|
|
608
|
-
baseURL,
|
|
609
|
-
headers: getHeaders,
|
|
610
|
-
fetch: options.fetch
|
|
611
|
-
});
|
|
612
|
-
const noSuchModel = (modelId, modelType) => {
|
|
613
|
-
throw new NoSuchModelError2({ modelId, modelType });
|
|
614
|
-
};
|
|
615
|
-
const provider = {
|
|
616
|
-
specificationVersion: "v3",
|
|
617
|
-
video: createVideoModel,
|
|
618
|
-
videoModel: createVideoModel,
|
|
619
|
-
languageModel: (modelId) => noSuchModel(modelId, "languageModel"),
|
|
620
|
-
embeddingModel: (modelId) => noSuchModel(modelId, "embeddingModel"),
|
|
621
|
-
imageModel: (modelId) => noSuchModel(modelId, "imageModel")
|
|
622
|
-
};
|
|
623
|
-
return provider;
|
|
624
|
-
}
|
|
625
|
-
var klingai = createKlingAI();
|
|
626
|
-
export {
|
|
627
|
-
createKlingAI,
|
|
628
|
-
klingai
|
|
629
|
-
};
|
|
630
|
-
//# sourceMappingURL=index.mjs.map
|