@mastra/client-js 1.56.0-alpha.2 → 1.56.0-alpha.4

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
@@ -376,6 +376,16 @@ function isTerminalHttpError(error) {
376
376
  return typeof status === "number" && (status >= 400 && status < 500 || status === 501);
377
377
  }
378
378
  /**
379
+ * Whether an error is the server's trace query execution timeout (504 with code
380
+ * `TRACE_QUERY_EXECUTION_TIMEOUT`). Re-sending the same query hits the same
381
+ * timeout budget, so retrying only repeats an expensive database query.
382
+ * Plain 504s from proxies or gateways do not carry this code and stay retryable.
383
+ */
384
+ function isTraceQueryTimeoutError(error) {
385
+ const { status, body } = error ?? {};
386
+ return status === 504 && body?.code === "TRACE_QUERY_EXECUTION_TIMEOUT";
387
+ }
388
+ /**
379
389
  * Whether a failed resubscribe should end a long-lived subscription instead of
380
390
  * retrying. Like {@link isTerminalHttpError}, but 408 Request Timeout and
381
391
  * 429 Too Many Requests stay retryable because they mean "try again later".
@@ -1194,7 +1204,7 @@ var BaseResource = class {
1194
1204
  } catch (error) {
1195
1205
  lastError = error;
1196
1206
  if (signal?.aborted || error?.name === "AbortError") throw error;
1197
- if (isTerminalHttpError(error)) throw error;
1207
+ if (isTerminalHttpError(error) || isTraceQueryTimeoutError(error)) throw error;
1198
1208
  if (attempt === retries) break;
1199
1209
  await new Promise((resolve, reject) => {
1200
1210
  const timer = setTimeout(() => {
@@ -5126,10 +5136,10 @@ var Observability = class extends BaseResource {
5126
5136
  });
5127
5137
  }
5128
5138
  /**
5129
- * Queries thread identities using eligible-trace and cross-trace predicates.
5139
+ * Queries full-history thread rows using a trace or thread activity window.
5130
5140
  *
5131
- * @param params - Thread query with its eligible trace selection
5132
- * @returns Matching thread identities
5141
+ * @param params - Thread query with its qualification window, filters and pagination
5142
+ * @returns Matching thread rows. Legacy requests retain their identity-only response.
5133
5143
  */
5134
5144
  queryTraceThreads(params) {
5135
5145
  return this.request("/observability/threads/query", {
@@ -6781,10 +6791,12 @@ var AgentControllerSession = class extends BaseResource {
6781
6791
  delayMs: finiteOr(options.reconnect.delayMs, 1e3),
6782
6792
  maxDelayMs: finiteOr(options.reconnect.maxDelayMs, 3e4)
6783
6793
  } : null;
6794
+ const idleTimeoutMs = finiteOr(options.idleTimeoutMs, 6e4);
6784
6795
  /** Exponential backoff for the Nth (1-based) retry of an outage streak. */
6785
6796
  const backoffDelay = (attempt) => reconnectOptions ? Math.min(reconnectOptions.delayMs * 2 ** (attempt - 1), reconnectOptions.maxDelayMs) : 0;
6786
6797
  let cancelled = false;
6787
6798
  let currentReader = null;
6799
+ let currentAbort = null;
6788
6800
  let reconnectTimer;
6789
6801
  let delayResolve;
6790
6802
  const settleDelay = () => {
@@ -6800,8 +6812,39 @@ var AgentControllerSession = class extends BaseResource {
6800
6812
  delayResolve = resolve;
6801
6813
  reconnectTimer = setTimeout(settleDelay, ms);
6802
6814
  });
6815
+ const idleTimeoutError = () => {
6816
+ const error = /* @__PURE__ */ new Error(`Agent controller session stream received no data for ${idleTimeoutMs}ms`);
6817
+ error.name = "AgentControllerStreamIdleError";
6818
+ return error;
6819
+ };
6803
6820
  const requestStream = async () => {
6804
- const response = await this.request(this.url(`${this.base()}/stream`), { stream: true });
6821
+ const abort = new AbortController();
6822
+ currentAbort = abort;
6823
+ let openTimer;
6824
+ const request = this.request(this.url(`${this.base()}/stream`), {
6825
+ stream: true,
6826
+ signal: abort.signal
6827
+ });
6828
+ request.then((response) => {
6829
+ if (abort.signal.aborted) response.body?.cancel().catch(() => {});
6830
+ }).catch(() => {});
6831
+ let response;
6832
+ try {
6833
+ if (idleTimeoutMs > 0) response = await Promise.race([request, new Promise((_resolve, reject) => {
6834
+ openTimer = setTimeout(() => {
6835
+ const error = idleTimeoutError();
6836
+ abort.abort(error);
6837
+ reject(error);
6838
+ }, idleTimeoutMs);
6839
+ })]);
6840
+ else response = await request;
6841
+ } finally {
6842
+ clearTimeout(openTimer);
6843
+ }
6844
+ if (abort.signal.aborted) {
6845
+ response.body?.cancel().catch(() => {});
6846
+ throw abort.signal.reason;
6847
+ }
6805
6848
  if (!response.body) throw new Error("No response body for agent controller session stream");
6806
6849
  return response;
6807
6850
  };
@@ -6827,12 +6870,40 @@ var AgentControllerSession = class extends BaseResource {
6827
6870
  const pump = async (response) => {
6828
6871
  const reader = response.body.getReader();
6829
6872
  currentReader = reader;
6873
+ const abort = currentAbort;
6830
6874
  const decoder = new TextDecoder();
6831
6875
  let buffer = "";
6876
+ let idleTimer;
6877
+ let timedOut = false;
6878
+ const armIdleTimer = () => {
6879
+ if (idleTimeoutMs <= 0) return;
6880
+ clearTimeout(idleTimer);
6881
+ idleTimer = setTimeout(() => {
6882
+ timedOut = true;
6883
+ reader.cancel(idleTimeoutError()).catch(() => {});
6884
+ abort?.abort(idleTimeoutError());
6885
+ }, idleTimeoutMs);
6886
+ };
6832
6887
  try {
6888
+ armIdleTimer();
6833
6889
  while (!cancelled) {
6834
- const { done, value } = await reader.read();
6890
+ let chunk;
6891
+ try {
6892
+ chunk = await reader.read();
6893
+ } catch (error) {
6894
+ if (timedOut && !cancelled) return {
6895
+ kind: "transport_error",
6896
+ error: idleTimeoutError()
6897
+ };
6898
+ throw error;
6899
+ }
6900
+ if (timedOut && !cancelled) return {
6901
+ kind: "transport_error",
6902
+ error: idleTimeoutError()
6903
+ };
6904
+ const { done, value } = chunk;
6835
6905
  if (done) return cancelled ? { kind: "cancelled" } : { kind: "done" };
6906
+ armIdleTimer();
6836
6907
  if (cancelled) return { kind: "cancelled" };
6837
6908
  buffer += decoder.decode(value, { stream: true });
6838
6909
  let separator;
@@ -6866,7 +6937,9 @@ var AgentControllerSession = class extends BaseResource {
6866
6937
  error
6867
6938
  };
6868
6939
  } finally {
6940
+ clearTimeout(idleTimer);
6869
6941
  if (currentReader === reader) currentReader = null;
6942
+ if (currentAbort === abort) currentAbort = null;
6870
6943
  reader.cancel().catch(() => {});
6871
6944
  }
6872
6945
  };
@@ -6943,6 +7016,7 @@ var AgentControllerSession = class extends BaseResource {
6943
7016
  cancelled = true;
6944
7017
  settleDelay();
6945
7018
  currentReader?.cancel().catch(() => {});
7019
+ currentAbort?.abort();
6946
7020
  } };
6947
7021
  }
6948
7022
  /**
@@ -6982,6 +7056,7 @@ var AgentControllerSession = class extends BaseResource {
6982
7056
  body: {
6983
7057
  toolCallId,
6984
7058
  approved,
7059
+ ...options?.alwaysAllowCategory ? { alwaysAllowCategory: true } : {},
6985
7060
  ...requestContext ? { requestContext } : {}
6986
7061
  }
6987
7062
  });
@@ -7848,7 +7923,7 @@ var MastraClient = class extends BaseResource {
7848
7923
  getTraceQueryValues(params, options) {
7849
7924
  return this.observability.getTraceQueryValues(params, options);
7850
7925
  }
7851
- /** Queries thread identities using eligible-trace and cross-trace predicates. */
7926
+ /** Queries full-history thread rows using a trace or thread activity window. */
7852
7927
  queryTraceThreads(params) {
7853
7928
  return this.observability.queryTraceThreads(params);
7854
7929
  }