@acedatacloud/sdk 2026.716.0 → 2026.718.0

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
@@ -115,6 +115,16 @@ function backoffDelay(attempt) {
115
115
  function sleep(ms) {
116
116
  return new Promise((resolve) => setTimeout(resolve, ms));
117
117
  }
118
+ function isAbortError(error) {
119
+ return typeof error === "object" && error !== null && "name" in error && error.name === "AbortError";
120
+ }
121
+ function timeoutError(error) {
122
+ return new TimeoutError({
123
+ message: `Request timed out${error instanceof Error && error.message ? `: ${error.message}` : ""}`,
124
+ statusCode: 0,
125
+ code: "timeout"
126
+ });
127
+ }
118
128
  var Transport = class {
119
129
  baseURL;
120
130
  platformBaseURL;
@@ -131,7 +141,7 @@ var Transport = class {
131
141
  code: "no_token"
132
142
  });
133
143
  }
134
- this.baseURL = (opts.baseURL ?? "https://api.acedata.cloud").replace(/\/+$/, "");
144
+ this.baseURL = (opts.baseURL ?? "https://x402.acedata.cloud").replace(/\/+$/, "");
135
145
  this.platformBaseURL = (opts.platformBaseURL ?? "https://platform.acedata.cloud").replace(/\/+$/, "");
136
146
  this.timeout = opts.timeout ?? 3e5;
137
147
  this.maxRetries = opts.maxRetries ?? 2;
@@ -159,7 +169,8 @@ var Transport = class {
159
169
  let lastError = null;
160
170
  let paymentAttempted = false;
161
171
  let extraHeaders = {};
162
- for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
172
+ let attempt = 0;
173
+ while (true) {
163
174
  const controller = new AbortController();
164
175
  const timer = setTimeout(() => controller.abort(), timeoutMs);
165
176
  try {
@@ -178,14 +189,17 @@ var Transport = class {
178
189
  } catch {
179
190
  throw mapError(402, { error: { code: "invalid_402", message: text } });
180
191
  }
181
- if (!body.accepts?.length) {
192
+ if (!body || typeof body !== "object" || !Array.isArray(body.accepts) || !body.accepts.length || !body.accepts.every(
193
+ (requirement) => requirement !== null && typeof requirement === "object"
194
+ )) {
182
195
  throw mapError(402, { error: { code: "invalid_402", message: "No payment requirements" } });
183
196
  }
197
+ const paymentRequired = body;
184
198
  const result = await this.paymentHandler({
185
199
  url,
186
200
  method,
187
201
  body: opts.json,
188
- accepts: body.accepts
202
+ accepts: paymentRequired.accepts
189
203
  });
190
204
  extraHeaders = { ...extraHeaders, ...result.headers };
191
205
  paymentAttempted = true;
@@ -199,8 +213,9 @@ var Transport = class {
199
213
  } catch {
200
214
  body = { error: { code: "unknown", message: text } };
201
215
  }
202
- if (RETRY_STATUS_CODES.has(resp.status) && attempt < this.maxRetries) {
216
+ if (!paymentAttempted && RETRY_STATUS_CODES.has(resp.status) && attempt < this.maxRetries) {
203
217
  await sleep(backoffDelay(attempt) * 1e3);
218
+ attempt += 1;
204
219
  continue;
205
220
  }
206
221
  throw mapError(resp.status, body);
@@ -209,57 +224,117 @@ var Transport = class {
209
224
  } catch (err) {
210
225
  clearTimeout(timer);
211
226
  if (err instanceof APIError) throw err;
212
- lastError = err;
213
- if (attempt < this.maxRetries) {
227
+ if (isAbortError(err)) {
228
+ lastError = timeoutError(err);
229
+ } else {
230
+ lastError = err;
231
+ }
232
+ if (!paymentAttempted && attempt < this.maxRetries) {
214
233
  await sleep(backoffDelay(attempt) * 1e3);
234
+ attempt += 1;
215
235
  continue;
216
236
  }
237
+ throw lastError;
217
238
  }
218
239
  }
219
- throw lastError ?? new TransportError("Request failed after retries");
220
240
  }
221
241
  async *requestStream(method, path2, opts = {}) {
222
242
  const url = `${this.baseURL}${path2}`;
223
243
  const headers = { ...this.headers, accept: "text/event-stream" };
224
- const controller = new AbortController();
225
- const timer = setTimeout(() => controller.abort(), opts.timeout ?? this.timeout);
226
- try {
227
- const resp = await fetch(url, {
228
- method,
229
- headers,
230
- body: opts.json ? JSON.stringify(opts.json) : void 0,
231
- signal: controller.signal
232
- });
233
- if (resp.status >= 400) {
234
- const text = await resp.text();
235
- let body;
244
+ let paymentAttempted = false;
245
+ let extraHeaders = {};
246
+ while (true) {
247
+ const controller = new AbortController();
248
+ const timeoutMs = opts.timeout ?? this.timeout;
249
+ const connectionTimer = setTimeout(() => controller.abort(), timeoutMs);
250
+ try {
251
+ let resp;
236
252
  try {
237
- body = JSON.parse(text);
238
- } catch {
239
- body = { error: { code: "unknown", message: text } };
253
+ resp = await fetch(url, {
254
+ method,
255
+ headers: { ...headers, ...extraHeaders },
256
+ body: opts.json ? JSON.stringify(opts.json) : void 0,
257
+ signal: controller.signal
258
+ });
259
+ } catch (error) {
260
+ if (isAbortError(error)) throw timeoutError(error);
261
+ throw error;
262
+ } finally {
263
+ clearTimeout(connectionTimer);
240
264
  }
241
- throw mapError(resp.status, body);
242
- }
243
- if (!resp.body) throw new TransportError("No response body for stream");
244
- const reader = resp.body.getReader();
245
- const decoder = new TextDecoder();
246
- let buffer = "";
247
- while (true) {
248
- const { done, value } = await reader.read();
249
- if (done) break;
250
- buffer += decoder.decode(value, { stream: true });
251
- const lines = buffer.split("\n");
252
- buffer = lines.pop() ?? "";
253
- for (const line of lines) {
254
- if (line.startsWith("data: ")) {
255
- const data = line.slice(6);
256
- if (data === "[DONE]") return;
257
- yield data;
265
+ if (resp.status === 402 && this.paymentHandler && !paymentAttempted) {
266
+ const text = await resp.text();
267
+ let body;
268
+ try {
269
+ body = JSON.parse(text);
270
+ } catch {
271
+ throw mapError(402, { error: { code: "invalid_402", message: text } });
272
+ }
273
+ if (!body || typeof body !== "object" || !Array.isArray(body.accepts) || !body.accepts.length || !body.accepts.every(
274
+ (requirement) => requirement !== null && typeof requirement === "object"
275
+ )) {
276
+ throw mapError(402, { error: { code: "invalid_402", message: "No payment requirements" } });
258
277
  }
278
+ const paymentRequired = body;
279
+ const result = await this.paymentHandler({
280
+ url,
281
+ method,
282
+ body: opts.json,
283
+ accepts: paymentRequired.accepts
284
+ });
285
+ extraHeaders = { ...extraHeaders, ...result.headers };
286
+ paymentAttempted = true;
287
+ continue;
259
288
  }
289
+ if (resp.status >= 400) {
290
+ const text = await resp.text();
291
+ let body;
292
+ try {
293
+ body = JSON.parse(text);
294
+ } catch {
295
+ body = { error: { code: "unknown", message: text } };
296
+ }
297
+ throw mapError(resp.status, body);
298
+ }
299
+ if (!resp.body) throw new TransportError("No response body for stream");
300
+ const reader = resp.body.getReader();
301
+ const decoder = new TextDecoder();
302
+ let buffer = "";
303
+ try {
304
+ while (true) {
305
+ const idleTimer = setTimeout(() => controller.abort(), timeoutMs);
306
+ let result;
307
+ try {
308
+ result = await reader.read();
309
+ } catch (error) {
310
+ if (isAbortError(error)) throw timeoutError(error);
311
+ throw error;
312
+ } finally {
313
+ clearTimeout(idleTimer);
314
+ }
315
+ const { done, value } = result;
316
+ if (done) break;
317
+ buffer += decoder.decode(value, { stream: true });
318
+ const lines = buffer.split("\n");
319
+ buffer = lines.pop() ?? "";
320
+ for (const line of lines) {
321
+ if (line.startsWith("data: ")) {
322
+ const data = line.slice(6);
323
+ if (data === "[DONE]") return;
324
+ yield data;
325
+ }
326
+ }
327
+ }
328
+ } finally {
329
+ try {
330
+ await reader.cancel();
331
+ } catch {
332
+ }
333
+ }
334
+ return;
335
+ } finally {
336
+ clearTimeout(connectionTimer);
260
337
  }
261
- } finally {
262
- clearTimeout(timer);
263
338
  }
264
339
  }
265
340
  async upload(path2, fileData, filename, opts = {}) {
@@ -279,12 +354,18 @@ var Transport = class {
279
354
  const controller = new AbortController();
280
355
  const timer = setTimeout(() => controller.abort(), opts.timeout ?? this.timeout);
281
356
  try {
282
- const resp = await fetch(url, {
283
- method: "POST",
284
- headers: authHeaders,
285
- body,
286
- signal: controller.signal
287
- });
357
+ let resp;
358
+ try {
359
+ resp = await fetch(url, {
360
+ method: "POST",
361
+ headers: authHeaders,
362
+ body,
363
+ signal: controller.signal
364
+ });
365
+ } catch (error) {
366
+ if (isAbortError(error)) throw timeoutError(error);
367
+ throw error;
368
+ }
288
369
  clearTimeout(timer);
289
370
  if (resp.status >= 400) {
290
371
  const text = await resp.text();
@@ -856,12 +937,148 @@ var Veo = class {
856
937
  };
857
938
 
858
939
  // src/resources/kling.ts
940
+ var KLING_MODELS = [
941
+ "kling-v1",
942
+ "kling-v1-6",
943
+ "kling-v2-master",
944
+ "kling-v2-1-master",
945
+ "kling-v2-5-turbo",
946
+ "kling-v2-6",
947
+ "kling-v3",
948
+ "kling-v3-omni",
949
+ "kling-o1"
950
+ ];
951
+ function isHttpUrl(value) {
952
+ try {
953
+ const url = new URL(value);
954
+ return url.protocol === "http:" || url.protocol === "https:";
955
+ } catch {
956
+ return false;
957
+ }
958
+ }
959
+ function validateGenerateOptions(opts) {
960
+ if (!KLING_MODELS.includes(opts.model)) {
961
+ throw new Error(`model must be one of: ${KLING_MODELS.join(", ")}`);
962
+ }
963
+ const isV3 = opts.model === "kling-v3" || opts.model === "kling-v3-omni";
964
+ const hasReferences = Boolean(opts.imageList?.length || opts.videoList?.length);
965
+ if (opts.imageList !== void 0 && opts.imageList.length === 0) {
966
+ throw new Error("imageList must be non-empty or omitted");
967
+ }
968
+ if (opts.videoList !== void 0 && opts.videoList.length === 0) {
969
+ throw new Error("videoList must be non-empty or omitted");
970
+ }
971
+ if ((opts.action === "text2video" || opts.action === "image2video") && !opts.prompt) {
972
+ throw new Error("prompt is required for text2video and image2video");
973
+ }
974
+ if (opts.action === "image2video" && !opts.startImageUrl) {
975
+ throw new Error("startImageUrl is required for image2video");
976
+ }
977
+ if (opts.action === "extend" && !opts.videoId) {
978
+ throw new Error("videoId is required for extend");
979
+ }
980
+ if (opts.endImageUrl && !opts.startImageUrl) {
981
+ throw new Error("startImageUrl is required with endImageUrl");
982
+ }
983
+ if (opts.startImageUrl && !isHttpUrl(opts.startImageUrl)) {
984
+ throw new Error("startImageUrl must be an HTTP URL");
985
+ }
986
+ if (opts.endImageUrl && !isHttpUrl(opts.endImageUrl)) {
987
+ throw new Error("endImageUrl must be an HTTP URL");
988
+ }
989
+ if (opts.callbackUrl && !isHttpUrl(opts.callbackUrl)) {
990
+ throw new Error("callbackUrl must be an HTTP URL");
991
+ }
992
+ if (opts.cfgScale !== void 0 && (opts.cfgScale < 0 || opts.cfgScale > 1)) {
993
+ throw new Error("cfgScale must be between 0 and 1");
994
+ }
995
+ if (opts.duration !== void 0 && !Number.isInteger(opts.duration)) {
996
+ throw new Error("duration must be an integer");
997
+ }
998
+ if (isV3 && opts.duration !== void 0 && (opts.duration < 3 || opts.duration > 15)) {
999
+ throw new Error("Kling V3 duration must be between 3 and 15 seconds");
1000
+ }
1001
+ if (!isV3 && opts.model !== "kling-o1" && opts.duration !== void 0 && ![5, 10].includes(opts.duration)) {
1002
+ throw new Error("This Kling model supports only 5- or 10-second generation");
1003
+ }
1004
+ if (opts.model === "kling-o1" && opts.duration !== void 0 && opts.duration !== 5) {
1005
+ throw new Error("kling-o1 supports only 5-second generation");
1006
+ }
1007
+ if (opts.model === "kling-o1" && opts.mode !== void 0 && !["std", "pro"].includes(opts.mode)) {
1008
+ throw new Error("kling-o1 supports only std and pro modes");
1009
+ }
1010
+ if (opts.mode === "4k" && !isV3) {
1011
+ throw new Error("4k mode requires kling-v3 or kling-v3-omni");
1012
+ }
1013
+ if (opts.action === "extend" && !["kling-v1", "kling-v1-6", "kling-v2-5-turbo"].includes(opts.model)) {
1014
+ throw new Error("extend requires kling-v1, kling-v1-6, or kling-v2-5-turbo");
1015
+ }
1016
+ if (opts.action === "extend" && hasReferences) {
1017
+ throw new Error("imageList and videoList are not supported with extend");
1018
+ }
1019
+ if (hasReferences && opts.model !== "kling-o1" && opts.model !== "kling-v3-omni") {
1020
+ throw new Error("Omni references require kling-o1 or kling-v3-omni");
1021
+ }
1022
+ if (hasReferences && opts.mode === "4k") {
1023
+ throw new Error("4k cannot be combined with Omni references");
1024
+ }
1025
+ if ((opts.model === "kling-o1" || hasReferences) && (opts.negativePrompt !== void 0 || opts.cameraControl !== void 0 || opts.cfgScale !== void 0)) {
1026
+ throw new Error("Kling O1 and Omni references do not support negativePrompt, cameraControl, or cfgScale");
1027
+ }
1028
+ if (opts.model === "kling-o1" && opts.generateAudio) {
1029
+ throw new Error("kling-o1 does not support generateAudio");
1030
+ }
1031
+ if (opts.generateAudio && !isV3 && opts.model !== "kling-v2-6") {
1032
+ throw new Error("generateAudio requires a V3 model or kling-v2-6 pro mode");
1033
+ }
1034
+ if (opts.generateAudio && opts.model === "kling-v2-6" && opts.mode !== "pro") {
1035
+ throw new Error("kling-v2-6 supports generateAudio only in pro mode");
1036
+ }
1037
+ if (opts.generateAudio && opts.videoList?.length) {
1038
+ throw new Error("generateAudio cannot be used with videoList");
1039
+ }
1040
+ if (opts.videoList && (opts.videoList.length === 0 || opts.videoList.length > 1)) {
1041
+ throw new Error("videoList must contain exactly one reference video");
1042
+ }
1043
+ for (const image of opts.imageList ?? []) {
1044
+ if (!isHttpUrl(image.imageUrl)) throw new Error("Every reference image requires an HTTP imageUrl");
1045
+ if (image.type !== void 0 && !["first_frame", "end_frame"].includes(image.type)) {
1046
+ throw new Error("Reference image type must be first_frame or end_frame");
1047
+ }
1048
+ }
1049
+ for (const video of opts.videoList ?? []) {
1050
+ if (!isHttpUrl(video.videoUrl)) throw new Error("Every reference video requires an HTTP videoUrl");
1051
+ if (video.referType !== void 0 && !["base", "feature"].includes(video.referType)) {
1052
+ throw new Error("Reference video referType must be base or feature");
1053
+ }
1054
+ if (video.keepOriginalSound !== void 0 && !["yes", "no"].includes(video.keepOriginalSound)) {
1055
+ throw new Error("Reference video keepOriginalSound must be yes or no");
1056
+ }
1057
+ }
1058
+ const firstFrames = Number(Boolean(opts.startImageUrl)) + (opts.imageList ?? []).filter((item) => item.type === "first_frame").length;
1059
+ const endFrames = Number(Boolean(opts.endImageUrl)) + (opts.imageList ?? []).filter((item) => item.type === "end_frame").length;
1060
+ if (firstFrames > 1 || endFrames > 1) {
1061
+ throw new Error("Only one first frame and one end frame are allowed");
1062
+ }
1063
+ if (endFrames > 0 && firstFrames === 0) {
1064
+ throw new Error("A first frame is required with an end frame");
1065
+ }
1066
+ if ((opts.videoList?.[0]?.referType ?? "base") === "base" && opts.videoList?.length && (firstFrames > 0 || endFrames > 0)) {
1067
+ throw new Error("A base reference video cannot be combined with first or end frames");
1068
+ }
1069
+ const imageCount = (opts.imageList?.length ?? 0) + Number(Boolean(opts.startImageUrl)) + Number(Boolean(opts.endImageUrl));
1070
+ const imageLimit = opts.videoList?.length ? 4 : 7;
1071
+ if (imageCount > imageLimit) {
1072
+ throw new Error(`Reference images cannot exceed ${imageLimit} for this request`);
1073
+ }
1074
+ }
859
1075
  var Kling = class {
860
1076
  constructor(transport) {
861
1077
  this.transport = transport;
862
1078
  }
863
1079
  transport;
864
1080
  async generate(opts) {
1081
+ validateGenerateOptions(opts);
865
1082
  const {
866
1083
  action,
867
1084
  mode,
@@ -873,15 +1090,16 @@ var Kling = class {
873
1090
  cfgScale,
874
1091
  aspectRatio,
875
1092
  callbackUrl,
1093
+ async: asyncMode,
1094
+ timeout,
876
1095
  endImageUrl,
877
1096
  cameraControl,
878
- elementList,
1097
+ imageList,
879
1098
  videoList,
880
1099
  negativePrompt,
881
- startImageUrl,
882
- ...rest
1100
+ startImageUrl
883
1101
  } = opts;
884
- const body = { action, ...rest };
1102
+ const body = { action };
885
1103
  if (mode !== void 0) body.mode = mode;
886
1104
  if (model !== void 0) body.model = model;
887
1105
  if (prompt !== void 0) body.prompt = prompt;
@@ -891,26 +1109,46 @@ var Kling = class {
891
1109
  if (cfgScale !== void 0) body.cfg_scale = cfgScale;
892
1110
  if (aspectRatio !== void 0) body.aspect_ratio = aspectRatio;
893
1111
  if (callbackUrl !== void 0) body.callback_url = callbackUrl;
1112
+ if (asyncMode !== void 0) body.async = asyncMode;
1113
+ if (timeout !== void 0) body.timeout = timeout;
894
1114
  if (endImageUrl !== void 0) body.end_image_url = endImageUrl;
895
1115
  if (cameraControl !== void 0) body.camera_control = cameraControl;
896
- if (elementList !== void 0) body.element_list = elementList;
897
- if (videoList !== void 0) body.video_list = videoList;
1116
+ if (imageList !== void 0) {
1117
+ body.image_list = imageList.map(({ imageUrl, type }) => ({ image_url: imageUrl, ...type ? { type } : {} }));
1118
+ }
1119
+ if (videoList !== void 0) {
1120
+ body.video_list = videoList.map(({ videoUrl, referType, keepOriginalSound }) => ({
1121
+ video_url: videoUrl,
1122
+ ...referType ? { refer_type: referType } : {},
1123
+ ...keepOriginalSound ? { keep_original_sound: keepOriginalSound } : {}
1124
+ }));
1125
+ }
898
1126
  if (negativePrompt !== void 0) body.negative_prompt = negativePrompt;
899
1127
  if (startImageUrl !== void 0) body.start_image_url = startImageUrl;
900
1128
  return this.transport.request("POST", "/kling/videos", { json: body });
901
1129
  }
902
1130
  async motion(opts) {
903
- const { mode, imageUrl, videoUrl, characterOrientation, keepOriginalSound, prompt, callbackUrl, ...rest } = opts;
1131
+ const { mode, imageUrl, videoUrl, characterOrientation, keepOriginalSound, prompt, callbackUrl } = opts;
1132
+ if (!["std", "pro"].includes(mode)) throw new Error("mode must be std or pro");
1133
+ if (!isHttpUrl(imageUrl)) throw new Error("imageUrl must be an HTTP URL");
1134
+ if (!isHttpUrl(videoUrl)) throw new Error("videoUrl must be an HTTP URL");
1135
+ if (!["image", "video"].includes(characterOrientation)) {
1136
+ throw new Error("characterOrientation must be image or video");
1137
+ }
1138
+ if (keepOriginalSound !== void 0 && !["yes", "no"].includes(keepOriginalSound)) {
1139
+ throw new Error("keepOriginalSound must be yes or no");
1140
+ }
1141
+ if (callbackUrl && !isHttpUrl(callbackUrl)) throw new Error("callbackUrl must be an HTTP URL");
904
1142
  const body = {
905
1143
  mode,
906
1144
  image_url: imageUrl,
907
1145
  video_url: videoUrl,
908
- character_orientation: characterOrientation,
909
- ...rest
1146
+ character_orientation: characterOrientation
910
1147
  };
911
1148
  if (keepOriginalSound !== void 0) body.keep_original_sound = keepOriginalSound;
912
1149
  if (prompt !== void 0) body.prompt = prompt;
913
1150
  if (callbackUrl !== void 0) body.callback_url = callbackUrl;
1151
+ if (opts.async !== void 0) body.async = opts.async;
914
1152
  return this.transport.request("POST", "/kling/motion", { json: body });
915
1153
  }
916
1154
  };