@hautechai/sdk 2.30.3 → 2.31.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.js CHANGED
@@ -10694,11 +10694,15 @@ var import_jose = require("jose");
10694
10694
  var getConfig = (options) => ({
10695
10695
  ...options,
10696
10696
  baseUrl: options.baseUrl ?? "https://api.hautech.ai",
10697
- baseWsUrl: options.baseWsUrl ?? options.baseUrl ?? "https://api.hautech.ai"
10697
+ baseWsUrl: options.baseWsUrl ?? options.baseUrl ?? "https://api.hautech.ai",
10698
+ onRequestError: options.onRequestError,
10699
+ invalidateAuthToken: () => void 0
10698
10700
  });
10699
10701
  var getDirectoryConfig = (options) => ({
10700
10702
  baseUrl: options.baseUrl ?? "https://api-directory.hautech.ai",
10701
- authToken: options.authToken
10703
+ authToken: options.authToken,
10704
+ onRequestError: options.onRequestError,
10705
+ invalidateAuthToken: () => void 0
10702
10706
  });
10703
10707
 
10704
10708
  // src/api-utils.ts
@@ -10711,49 +10715,125 @@ var isWrappedFn = (fn) => {
10711
10715
  var isCustomWrappedFn = (fn) => {
10712
10716
  return typeof fn === "function" && fn.__customMethodWrapper === true;
10713
10717
  };
10718
+ var retryLocks = /* @__PURE__ */ new WeakMap();
10719
+ var delay = (ms) => ms > 0 ? new Promise((resolve) => setTimeout(resolve, ms)) : Promise.resolve();
10720
+ var cloneRequestConfig = (request) => ({
10721
+ ...request,
10722
+ headers: request.headers ? { ...request.headers } : request.headers
10723
+ });
10724
+ var toResponseInfo = (response) => {
10725
+ if (!response) return void 0;
10726
+ return {
10727
+ status: response.status,
10728
+ headers: response.headers,
10729
+ data: response.data
10730
+ };
10731
+ };
10732
+ var enhanceAxiosError = (error) => {
10733
+ let responseData;
10734
+ try {
10735
+ const messageFromBody = error.response?.data?.message;
10736
+ if (typeof messageFromBody === "string" && messageFromBody.length > 0) {
10737
+ responseData = messageFromBody;
10738
+ } else if (typeof error.response?.data === "string") {
10739
+ responseData = error.response.data;
10740
+ } else if (error.response?.data !== void 0) {
10741
+ responseData = JSON.stringify(error.response.data);
10742
+ } else {
10743
+ responseData = "[no response data]";
10744
+ }
10745
+ } catch {
10746
+ responseData = "[unserializable data]";
10747
+ }
10748
+ error.message = `Request error: ${error.message || "Unknown error"}.
10749
+ ${responseData}`;
10750
+ };
10751
+ var runOnRequestError = async (config, info, context) => {
10752
+ if (!config.onRequestError) {
10753
+ return void 0;
10754
+ }
10755
+ const status = info.error.response?.status;
10756
+ if (status === 401) {
10757
+ const existing = retryLocks.get(config);
10758
+ if (existing) {
10759
+ return existing;
10760
+ }
10761
+ const pending = (async () => config.onRequestError(info, context))();
10762
+ retryLocks.set(config, pending);
10763
+ try {
10764
+ return await pending;
10765
+ } finally {
10766
+ retryLocks.delete(config);
10767
+ }
10768
+ }
10769
+ return await config.onRequestError(info, context);
10770
+ };
10771
+ var MAX_ATTEMPTS = 2;
10714
10772
  var createApiCallWrapper = (getBaseURL) => {
10715
10773
  return (fn, config) => {
10716
10774
  const wrapped = async function(...args) {
10717
- const token = await config.authToken();
10718
- const baseOptions = {
10719
- baseURL: getBaseURL(config),
10720
- headers: {
10721
- Authorization: `Bearer ${token}`
10722
- }
10723
- };
10724
- const finalArgs = [...args];
10725
- const lastParamIndex = fn.length - 1;
10726
- if (looksLikeAxiosRequestOptions(args[lastParamIndex])) {
10727
- finalArgs[lastParamIndex] = {
10728
- ...args[lastParamIndex],
10729
- ...baseOptions,
10775
+ const originalArgs = [...args];
10776
+ for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
10777
+ const token = await config.authToken();
10778
+ const baseOptions = {
10779
+ baseURL: getBaseURL(config),
10730
10780
  headers: {
10731
- ...args[lastParamIndex]?.headers ?? {},
10732
10781
  Authorization: `Bearer ${token}`
10733
10782
  }
10734
10783
  };
10735
- } else {
10736
- while (finalArgs.length < fn.length - 1) {
10737
- finalArgs.push(void 0);
10784
+ const finalArgs = [...originalArgs];
10785
+ const lastParamIndex = fn.length - 1;
10786
+ if (looksLikeAxiosRequestOptions(originalArgs[lastParamIndex])) {
10787
+ finalArgs[lastParamIndex] = {
10788
+ ...originalArgs[lastParamIndex],
10789
+ ...baseOptions,
10790
+ headers: {
10791
+ ...originalArgs[lastParamIndex]?.headers ?? {},
10792
+ Authorization: `Bearer ${token}`
10793
+ }
10794
+ };
10795
+ } else {
10796
+ while (finalArgs.length < fn.length - 1) {
10797
+ finalArgs.push(void 0);
10798
+ }
10799
+ finalArgs.push(baseOptions);
10738
10800
  }
10739
- finalArgs.push(baseOptions);
10740
- }
10741
- try {
10742
- const res = await fn.call(this, ...finalArgs);
10743
- return typeof res === "object" && "headers" in res ? res.data : res;
10744
- } catch (err) {
10745
- if (import_axios.default.isAxiosError(err)) {
10746
- let responseData;
10747
- try {
10748
- responseData = err.response?.data?.message ?? (typeof err.response?.data === "string" ? err.response.data : JSON.stringify(err.response?.data));
10749
- } catch {
10750
- responseData = "[unserializable data]";
10801
+ const requestConfig = finalArgs[finalArgs.length - 1] ?? {};
10802
+ try {
10803
+ const res = await fn.call(this, ...finalArgs);
10804
+ return typeof res === "object" && "headers" in res ? res.data : res;
10805
+ } catch (err) {
10806
+ if (!import_axios.default.isAxiosError(err)) {
10807
+ throw err;
10751
10808
  }
10752
- err.message = `Request error: ${err.message || "Unknown error"}.
10753
- ${responseData}`;
10809
+ enhanceAxiosError(err);
10810
+ const context = {
10811
+ attempt,
10812
+ request: cloneRequestConfig(requestConfig)
10813
+ };
10814
+ const handlerResult = await runOnRequestError(
10815
+ config,
10816
+ {
10817
+ error: err,
10818
+ response: toResponseInfo(err.response)
10819
+ },
10820
+ context
10821
+ );
10822
+ if (handlerResult?.invalidateToken) {
10823
+ config.invalidateAuthToken();
10824
+ }
10825
+ const hasAttemptsRemaining = attempt < MAX_ATTEMPTS;
10826
+ if (handlerResult?.retry && hasAttemptsRemaining) {
10827
+ const backoffMs = handlerResult.backoffMs ?? 0;
10828
+ if (backoffMs > 0) {
10829
+ await delay(backoffMs);
10830
+ }
10831
+ continue;
10832
+ }
10833
+ throw err;
10754
10834
  }
10755
- throw err;
10756
10835
  }
10836
+ throw new Error("Request error handling exceeded maximum attempts");
10757
10837
  };
10758
10838
  wrapped.__isWrapped = true;
10759
10839
  return wrapped;
@@ -12266,7 +12346,7 @@ var getOperations = () => {
12266
12346
  };
12267
12347
 
12268
12348
  // src/sdk/api-definitions/operations.ts
12269
- var waitOperation = wrapCustomMethod(async function(operation, timeoutMs = 6e4, delay = 3e3) {
12349
+ var waitOperation = wrapCustomMethod(async function(operation, timeoutMs = 6e4, delay2 = 3e3) {
12270
12350
  const deadline = Date.now() + timeoutMs;
12271
12351
  const sdk = this;
12272
12352
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -12279,7 +12359,7 @@ var waitOperation = wrapCustomMethod(async function(operation, timeoutMs = 6e4,
12279
12359
  while (Date.now() < deadline) {
12280
12360
  const result = await poll(operation.id);
12281
12361
  if (result) return result;
12282
- await sleep(delay);
12362
+ await sleep(delay2);
12283
12363
  }
12284
12364
  throw new Error("Operation timed out");
12285
12365
  });
@@ -17063,7 +17143,7 @@ var usePipelinesApi = () => {
17063
17143
  const sdk = this;
17064
17144
  return builder(buildPipeline(sdk));
17065
17145
  }),
17066
- wait: wrapCustomMethod(async function(pipeline, timeoutMs = 6e4, delay = 3e3) {
17146
+ wait: wrapCustomMethod(async function(pipeline, timeoutMs = 6e4, delay2 = 3e3) {
17067
17147
  const sdk = this;
17068
17148
  const deadline = Date.now() + timeoutMs;
17069
17149
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -17078,7 +17158,7 @@ var usePipelinesApi = () => {
17078
17158
  while (Date.now() < deadline) {
17079
17159
  const polled = await poll(pipeline.id);
17080
17160
  if (polled) return polled;
17081
- await sleep(delay);
17161
+ await sleep(delay2);
17082
17162
  }
17083
17163
  throw new Error("Operation timed out");
17084
17164
  })
@@ -17403,6 +17483,12 @@ var WsClient = class {
17403
17483
  this.getSocket().off("server_error");
17404
17484
  }
17405
17485
  }
17486
+ disconnect() {
17487
+ if (this.socket) {
17488
+ this.socket.disconnect();
17489
+ this.socket = void 0;
17490
+ }
17491
+ }
17406
17492
  };
17407
17493
 
17408
17494
  // src/sdk/api.ts
@@ -17437,6 +17523,9 @@ var getWsClientDefinitions = (config) => ({
17437
17523
  var createSDK = (options) => {
17438
17524
  let token = void 0;
17439
17525
  const config = getConfig(options);
17526
+ config.invalidateAuthToken = () => {
17527
+ token = void 0;
17528
+ };
17440
17529
  const authToken = config.authToken;
17441
17530
  const getAuthToken = async () => {
17442
17531
  if (token) {
@@ -17458,6 +17547,9 @@ var createSDK = (options) => {
17458
17547
  var createDirectorySDK = (options) => {
17459
17548
  let token = void 0;
17460
17549
  const config = getDirectoryConfig(options);
17550
+ config.invalidateAuthToken = () => {
17551
+ token = void 0;
17552
+ };
17461
17553
  const authToken = config.authToken;
17462
17554
  const getAuthToken = async () => {
17463
17555
  if (token) {