@axiom-lattice/client-sdk 8.5.0 → 8.6.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
@@ -1830,8 +1830,9 @@ var NetworkError = class extends Error {
1830
1830
  }
1831
1831
  };
1832
1832
  var AuthenticationError = class extends Error {
1833
- constructor(message) {
1833
+ constructor(message, code) {
1834
1834
  super(message);
1835
+ this.code = code;
1835
1836
  this.name = "AuthenticationError";
1836
1837
  }
1837
1838
  };
@@ -5692,6 +5693,7 @@ var _Client = class _Client extends AbstractClient {
5692
5693
  super(config);
5693
5694
  /** Last SSE event id observed per thread, for automatic resume. */
5694
5695
  this.resumeEventIds = /* @__PURE__ */ new Map();
5696
+ this.refreshInFlight = null;
5695
5697
  this.headers = {
5696
5698
  "Content-Type": "application/json",
5697
5699
  Authorization: `Bearer ${this.config.apiKey}`,
@@ -5713,7 +5715,15 @@ var _Client = class _Client extends AbstractClient {
5713
5715
  async handleResponse(response) {
5714
5716
  if (!response.ok) {
5715
5717
  if (response.status === 401) {
5716
- throw new AuthenticationError("Authentication failed");
5718
+ const body = await response.json().catch(() => void 0);
5719
+ const code = typeof body?.code === "string" ? body.code : void 0;
5720
+ if (code === "SESSION_INVALID" && !this.config.refreshSession) {
5721
+ this.getUnauthorizedHandler()?.();
5722
+ }
5723
+ throw new AuthenticationError(
5724
+ body?.error ?? body?.message ?? "Authentication failed",
5725
+ code
5726
+ );
5717
5727
  } else {
5718
5728
  const errorData = await response.json().catch(() => ({}));
5719
5729
  throw new ApiError(
@@ -5854,12 +5864,10 @@ var _Client = class _Client extends AbstractClient {
5854
5864
  * @param options - Request options
5855
5865
  * @returns A promise that resolves to the response data
5856
5866
  */
5857
- async makeRequest(url, options) {
5867
+ async makeRequest(url, options, retry = true) {
5858
5868
  const method = options?.method || "GET";
5859
- const workspaceHeaders = this.getWorkspaceHeaders();
5860
5869
  const requestHeaders = {
5861
- ...this.headers,
5862
- ...workspaceHeaders,
5870
+ ...this.getAllHeaders(),
5863
5871
  ...this.bodyAffinityHeaders(options?.body),
5864
5872
  ...options?.headers || {}
5865
5873
  };
@@ -5878,7 +5886,34 @@ var _Client = class _Client extends AbstractClient {
5878
5886
  requestOptions.body = JSON.stringify(options.body);
5879
5887
  }
5880
5888
  }
5881
- return this.fetchWithTimeout(url, requestOptions);
5889
+ try {
5890
+ return await this.fetchWithTimeout(url, requestOptions);
5891
+ } catch (error) {
5892
+ if (retry && error instanceof AuthenticationError && error.code === "SESSION_INVALID" && this.config.refreshSession && !url.includes("/api/auth/")) {
5893
+ const refreshed = await this.refreshOnce();
5894
+ if (refreshed) {
5895
+ if (!this.config.getApiKey) {
5896
+ this.headers.Authorization = `Bearer ${refreshed.token}`;
5897
+ }
5898
+ return this.makeRequest(url, options, false);
5899
+ }
5900
+ this.getUnauthorizedHandler()?.();
5901
+ }
5902
+ throw error;
5903
+ }
5904
+ }
5905
+ /** Host-facing single-flight refresh (used by ThreadStream on a 401). */
5906
+ async refreshSession() {
5907
+ return this.refreshOnce();
5908
+ }
5909
+ async refreshOnce() {
5910
+ if (!this.config.refreshSession) return null;
5911
+ if (!this.refreshInFlight) {
5912
+ this.refreshInFlight = this.config.refreshSession().finally(() => {
5913
+ this.refreshInFlight = null;
5914
+ });
5915
+ }
5916
+ return this.refreshInFlight;
5882
5917
  }
5883
5918
  /**
5884
5919
  * Implementation of the abstract streamRequest method for web clients
@@ -6006,8 +6041,10 @@ var _Client = class _Client extends AbstractClient {
6006
6041
  */
6007
6042
  getAllHeaders() {
6008
6043
  const workspaceHeaders = this.getWorkspaceHeaders();
6044
+ const apiKey = this.config.getApiKey?.() ?? this.config.apiKey;
6009
6045
  return {
6010
6046
  ...this.headers,
6047
+ ...apiKey ? { Authorization: `Bearer ${apiKey}` } : {},
6011
6048
  ...workspaceHeaders
6012
6049
  };
6013
6050
  }