@axiom-lattice/client-sdk 8.4.1 → 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
  };
@@ -2250,7 +2251,9 @@ var AbstractClient = class {
2250
2251
  auditLogs: async (params) => {
2251
2252
  const searchParams = new URLSearchParams();
2252
2253
  if (params?.credentialId) searchParams.set("credentialId", params.credentialId);
2254
+ if (params?.projectId) searchParams.set("projectId", params.projectId);
2253
2255
  if (params?.domain) searchParams.set("domain", params.domain);
2256
+ if (params?.action) searchParams.set("action", params.action);
2254
2257
  if (params?.status) searchParams.set("status", params.status);
2255
2258
  if (params?.from) searchParams.set("from", params.from);
2256
2259
  if (params?.to) searchParams.set("to", params.to);
@@ -2259,6 +2262,11 @@ var AbstractClient = class {
2259
2262
  const qs = searchParams.toString();
2260
2263
  const response = await this.makeRequest(`/api/open/audit${qs ? `?${qs}` : ""}`);
2261
2264
  return response.data.records;
2265
+ },
2266
+ /** Read one tenant-scoped Open audit record by id. */
2267
+ auditLog: async (id) => {
2268
+ const response = await this.makeRequest(`/api/open/audit/${encodeURIComponent(id)}`);
2269
+ return response.data.record;
2262
2270
  }
2263
2271
  };
2264
2272
  /**
@@ -3174,6 +3182,47 @@ var AbstractClient = class {
3174
3182
  );
3175
3183
  }
3176
3184
  };
3185
+ /**
3186
+ * Read-only tenant monitor surface (owner/admin only on the server).
3187
+ */
3188
+ this.monitor = {
3189
+ overview: async (options) => {
3190
+ const params = new URLSearchParams();
3191
+ if (options?.sinceHours !== void 0) {
3192
+ params.set("sinceHours", String(options.sinceHours));
3193
+ }
3194
+ const query = params.toString();
3195
+ const response = await this.makeRequest(`/api/tenant-monitor/overview${query ? `?${query}` : ""}`);
3196
+ return response.data;
3197
+ },
3198
+ threads: {
3199
+ list: async (query = {}) => {
3200
+ const params = new URLSearchParams();
3201
+ if (query.limit !== void 0) params.set("limit", String(query.limit));
3202
+ if (query.offset !== void 0) params.set("offset", String(query.offset));
3203
+ if (query.kind) params.set("kind", query.kind);
3204
+ if (query.assistantId) params.set("assistantId", query.assistantId);
3205
+ if (query.createdAfter) params.set("createdAfter", query.createdAfter);
3206
+ const suffix = params.toString();
3207
+ const response = await this.makeRequest(`/api/tenant-monitor/threads${suffix ? `?${suffix}` : ""}`);
3208
+ return response.data;
3209
+ }
3210
+ },
3211
+ runs: {
3212
+ list: async (query = {}) => {
3213
+ const params = new URLSearchParams();
3214
+ if (query.limit !== void 0) params.set("limit", String(query.limit));
3215
+ if (query.offset !== void 0) params.set("offset", String(query.offset));
3216
+ if (query.status) params.set("status", query.status);
3217
+ if (query.source) params.set("source", query.source);
3218
+ if (query.assistantId) params.set("assistantId", query.assistantId);
3219
+ if (query.createdAfter) params.set("createdAfter", query.createdAfter);
3220
+ const suffix = params.toString();
3221
+ const response = await this.makeRequest(`/api/tenant-monitor/runs${suffix ? `?${suffix}` : ""}`);
3222
+ return response.data;
3223
+ }
3224
+ }
3225
+ };
3177
3226
  /**
3178
3227
  * Tenant-scoped namespaced key-value store (viewer/editor surface).
3179
3228
  */
@@ -5644,6 +5693,7 @@ var _Client = class _Client extends AbstractClient {
5644
5693
  super(config);
5645
5694
  /** Last SSE event id observed per thread, for automatic resume. */
5646
5695
  this.resumeEventIds = /* @__PURE__ */ new Map();
5696
+ this.refreshInFlight = null;
5647
5697
  this.headers = {
5648
5698
  "Content-Type": "application/json",
5649
5699
  Authorization: `Bearer ${this.config.apiKey}`,
@@ -5665,7 +5715,15 @@ var _Client = class _Client extends AbstractClient {
5665
5715
  async handleResponse(response) {
5666
5716
  if (!response.ok) {
5667
5717
  if (response.status === 401) {
5668
- 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
+ );
5669
5727
  } else {
5670
5728
  const errorData = await response.json().catch(() => ({}));
5671
5729
  throw new ApiError(
@@ -5806,12 +5864,10 @@ var _Client = class _Client extends AbstractClient {
5806
5864
  * @param options - Request options
5807
5865
  * @returns A promise that resolves to the response data
5808
5866
  */
5809
- async makeRequest(url, options) {
5867
+ async makeRequest(url, options, retry = true) {
5810
5868
  const method = options?.method || "GET";
5811
- const workspaceHeaders = this.getWorkspaceHeaders();
5812
5869
  const requestHeaders = {
5813
- ...this.headers,
5814
- ...workspaceHeaders,
5870
+ ...this.getAllHeaders(),
5815
5871
  ...this.bodyAffinityHeaders(options?.body),
5816
5872
  ...options?.headers || {}
5817
5873
  };
@@ -5830,7 +5886,34 @@ var _Client = class _Client extends AbstractClient {
5830
5886
  requestOptions.body = JSON.stringify(options.body);
5831
5887
  }
5832
5888
  }
5833
- 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;
5834
5917
  }
5835
5918
  /**
5836
5919
  * Implementation of the abstract streamRequest method for web clients
@@ -5958,8 +6041,10 @@ var _Client = class _Client extends AbstractClient {
5958
6041
  */
5959
6042
  getAllHeaders() {
5960
6043
  const workspaceHeaders = this.getWorkspaceHeaders();
6044
+ const apiKey = this.config.getApiKey?.() ?? this.config.apiKey;
5961
6045
  return {
5962
6046
  ...this.headers,
6047
+ ...apiKey ? { Authorization: `Bearer ${apiKey}` } : {},
5963
6048
  ...workspaceHeaders
5964
6049
  };
5965
6050
  }