@acedatacloud/sdk 2026.716.1 → 2026.718.1

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" } });
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;
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 } };
258
296
  }
297
+ throw mapError(resp.status, body);
259
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();
@@ -355,6 +436,16 @@ var Chat = class {
355
436
  };
356
437
 
357
438
  // src/runtime/tasks.ts
439
+ function taskStatus(state) {
440
+ const response = state.response ?? state;
441
+ if (response.status === "succeeded" || response.status === "failed") return response.status;
442
+ if (response.status !== void 0 && response.status !== null) return "";
443
+ const finished = response.finished_at !== void 0 && response.finished_at !== null || state.finished_at !== void 0 && state.finished_at !== null;
444
+ if (!finished) return "";
445
+ if (response.success === true) return "succeeded";
446
+ if (response.success === false) return "failed";
447
+ return "";
448
+ }
358
449
  var TaskHandle = class {
359
450
  id;
360
451
  pollEndpoint;
@@ -372,8 +463,7 @@ var TaskHandle = class {
372
463
  }
373
464
  async isCompleted() {
374
465
  const state = await this.get();
375
- const response = state.response ?? state;
376
- const status = response.status;
466
+ const status = taskStatus(state);
377
467
  return status === "succeeded" || status === "failed";
378
468
  }
379
469
  async wait(opts = {}) {
@@ -382,8 +472,7 @@ var TaskHandle = class {
382
472
  const start = Date.now();
383
473
  while (Date.now() - start < maxWait) {
384
474
  const state = await this.get();
385
- const response = state.response ?? state;
386
- const status = response.status;
475
+ const status = taskStatus(state);
387
476
  if (status === "succeeded" || status === "failed") {
388
477
  this._result = state;
389
478
  return state;