@mastra/client-js 1.0.0-beta.14 → 1.0.0-beta.16

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
@@ -1061,6 +1061,41 @@ async function processDataStream({
1061
1061
  }
1062
1062
  }
1063
1063
  }
1064
+ function isComplexValue(value) {
1065
+ if (value === null || value === void 0) return false;
1066
+ if (value instanceof Date) return false;
1067
+ return typeof value === "object";
1068
+ }
1069
+ function serializeQueryValue(value) {
1070
+ if (value instanceof Date) {
1071
+ return value.toISOString();
1072
+ }
1073
+ if (isComplexValue(value)) {
1074
+ return JSON.stringify(value, (_key, val) => {
1075
+ if (val instanceof Date) {
1076
+ return val.toISOString();
1077
+ }
1078
+ return val;
1079
+ });
1080
+ }
1081
+ return String(value);
1082
+ }
1083
+ function toQueryParams(params, flattenKeys = []) {
1084
+ const searchParams = new URLSearchParams();
1085
+ const keysToFlatten = flattenKeys;
1086
+ function addParams(obj) {
1087
+ for (const [key, value] of Object.entries(obj)) {
1088
+ if (value === void 0 || value === null) continue;
1089
+ if (keysToFlatten.includes(key) && typeof value === "object" && !Array.isArray(value) && !(value instanceof Date)) {
1090
+ addParams(value);
1091
+ } else {
1092
+ searchParams.set(key, serializeQueryValue(value));
1093
+ }
1094
+ }
1095
+ }
1096
+ addParams(params);
1097
+ return searchParams.toString();
1098
+ }
1064
1099
  function parseClientRequestContext(requestContext) {
1065
1100
  if (requestContext) {
1066
1101
  if (requestContext instanceof RequestContext) {
@@ -2424,7 +2459,8 @@ var Agent = class extends BaseResource {
2424
2459
  */
2425
2460
  resetModel() {
2426
2461
  return this.request(`/api/agents/${this.agentId}/model/reset`, {
2427
- method: "POST"
2462
+ method: "POST",
2463
+ body: {}
2428
2464
  });
2429
2465
  }
2430
2466
  /**
@@ -2687,12 +2723,79 @@ var Run = class extends BaseResource {
2687
2723
  /**
2688
2724
  * Cancels a specific workflow run by its ID
2689
2725
  * @returns Promise containing a success message
2726
+ * @deprecated Use `cancel()` instead
2690
2727
  */
2691
2728
  cancelRun() {
2692
2729
  return this.request(`/api/workflows/${this.workflowId}/runs/${this.runId}/cancel`, {
2693
2730
  method: "POST"
2694
2731
  });
2695
2732
  }
2733
+ /**
2734
+ * Cancels a workflow run.
2735
+ *
2736
+ * This method aborts any running steps and updates the workflow status to 'canceled' .
2737
+ * It works for both actively running workflows and suspended/waiting workflows.
2738
+ *
2739
+ * ## How cancellation works
2740
+ *
2741
+ * When called, the workflow will:
2742
+ * 1. **Trigger the abort signal** - Uses the standard Web API AbortSignal to notify running steps
2743
+ * 2. **Prevent subsequent steps** - No further steps will be executed
2744
+ *
2745
+ * ## Abort signal behavior
2746
+ *
2747
+ * Steps that check the `abortSignal` parameter can respond to cancellation:
2748
+ * - Steps can listen to the 'abort' event: `abortSignal.addEventListener('abort', callback)`
2749
+ * - Steps can check if already aborted: `if (abortSignal.aborted) { ... }`
2750
+ * - Useful for canceling timeouts, network requests, or long-running operations
2751
+ *
2752
+ * **Note:** Steps must actively check the abort signal to be canceled mid-execution.
2753
+ * Steps that don't check the signal will run to completion, but subsequent steps won't execute.
2754
+ *
2755
+ * @returns Promise that resolves with `{ message: 'Workflow run canceled' }` when cancellation succeeds
2756
+ * @throws {HTTPException} 400 - If workflow ID or run ID is missing
2757
+ * @throws {HTTPException} 404 - If workflow or workflow run is not found
2758
+ *
2759
+ * @example
2760
+ * ```typescript
2761
+ * const run = await workflow.createRun({ runId: 'run-123' });
2762
+ * await run.cancel();
2763
+ * // Returns: { message: 'Workflow run canceled' }
2764
+ * ```
2765
+ *
2766
+ * @example
2767
+ * ```typescript
2768
+ * // Example of a step that responds to cancellation
2769
+ * const step = createStep({
2770
+ * id: 'long-running-step',
2771
+ * execute: async ({ inputData, abortSignal, abort }) => {
2772
+ * const timeout = new Promise((resolve) => {
2773
+ * const timer = setTimeout(() => resolve('done'), 10000);
2774
+ *
2775
+ * // Clean up if canceled
2776
+ * abortSignal.addEventListener('abort', () => {
2777
+ * clearTimeout(timer);
2778
+ * resolve('canceled');
2779
+ * });
2780
+ * });
2781
+ *
2782
+ * const result = await timeout;
2783
+ *
2784
+ * // Check if aborted after async operation
2785
+ * if (abortSignal.aborted) {
2786
+ * return abort(); // Stop execution
2787
+ * }
2788
+ *
2789
+ * return { result };
2790
+ * }
2791
+ * });
2792
+ * ```
2793
+ */
2794
+ cancel() {
2795
+ return this.request(`/api/workflows/${this.workflowId}/runs/${this.runId}/cancel`, {
2796
+ method: "POST"
2797
+ });
2798
+ }
2696
2799
  /**
2697
2800
  * Starts a workflow run synchronously without waiting for the workflow to complete
2698
2801
  * @param params - Object containing the inputData, initialState and requestContext
@@ -2706,7 +2809,8 @@ var Run = class extends BaseResource {
2706
2809
  inputData: params?.inputData,
2707
2810
  initialState: params?.initialState,
2708
2811
  requestContext,
2709
- tracingOptions: params.tracingOptions
2812
+ tracingOptions: params.tracingOptions,
2813
+ perStep: params.perStep
2710
2814
  }
2711
2815
  });
2712
2816
  }
@@ -2719,6 +2823,7 @@ var Run = class extends BaseResource {
2719
2823
  step,
2720
2824
  resumeData,
2721
2825
  tracingOptions,
2826
+ perStep,
2722
2827
  ...rest
2723
2828
  }) {
2724
2829
  const requestContext = parseClientRequestContext(rest.requestContext);
@@ -2728,7 +2833,8 @@ var Run = class extends BaseResource {
2728
2833
  step,
2729
2834
  resumeData,
2730
2835
  requestContext,
2731
- tracingOptions
2836
+ tracingOptions,
2837
+ perStep
2732
2838
  }
2733
2839
  });
2734
2840
  }
@@ -2748,7 +2854,8 @@ var Run = class extends BaseResource {
2748
2854
  initialState: params.initialState,
2749
2855
  requestContext,
2750
2856
  tracingOptions: params.tracingOptions,
2751
- resourceId: params.resourceId
2857
+ resourceId: params.resourceId,
2858
+ perStep: params.perStep
2752
2859
  }
2753
2860
  }).then(deserializeWorkflowError);
2754
2861
  }
@@ -2770,7 +2877,8 @@ var Run = class extends BaseResource {
2770
2877
  initialState: params.initialState,
2771
2878
  requestContext,
2772
2879
  tracingOptions: params.tracingOptions,
2773
- resourceId: params.resourceId
2880
+ resourceId: params.resourceId,
2881
+ perStep: params.perStep
2774
2882
  },
2775
2883
  stream: true
2776
2884
  }
@@ -2824,7 +2932,8 @@ var Run = class extends BaseResource {
2824
2932
  requestContext,
2825
2933
  closeOnSuspend: params.closeOnSuspend,
2826
2934
  tracingOptions: params.tracingOptions,
2827
- resourceId: params.resourceId
2935
+ resourceId: params.resourceId,
2936
+ perStep: params.perStep
2828
2937
  },
2829
2938
  stream: true
2830
2939
  }
@@ -2872,7 +2981,8 @@ var Run = class extends BaseResource {
2872
2981
  step: params.step,
2873
2982
  resumeData: params.resumeData,
2874
2983
  requestContext,
2875
- tracingOptions: params.tracingOptions
2984
+ tracingOptions: params.tracingOptions,
2985
+ perStep: params.perStep
2876
2986
  }
2877
2987
  }).then(deserializeWorkflowError);
2878
2988
  }
@@ -2893,7 +3003,8 @@ var Run = class extends BaseResource {
2893
3003
  step: params.step,
2894
3004
  resumeData: params.resumeData,
2895
3005
  requestContext,
2896
- tracingOptions: params.tracingOptions
3006
+ tracingOptions: params.tracingOptions,
3007
+ perStep: params.perStep
2897
3008
  },
2898
3009
  stream: true
2899
3010
  }
@@ -3672,9 +3783,12 @@ var Observability = class extends BaseResource {
3672
3783
  return this.request(`/api/observability/traces/${traceId}`);
3673
3784
  }
3674
3785
  /**
3675
- * Retrieves paginated list of traces with optional filtering
3676
- * @param params - Parameters for pagination and filtering
3786
+ * Retrieves paginated list of traces with optional filtering.
3787
+ * This is the legacy API preserved for backward compatibility.
3788
+ *
3789
+ * @param params - Parameters for pagination and filtering (legacy format)
3677
3790
  * @returns Promise containing paginated traces and pagination info
3791
+ * @deprecated Use {@link listTraces} instead for new features like ordering and more filters.
3678
3792
  */
3679
3793
  getTraces(params) {
3680
3794
  const { pagination, filters } = params;
@@ -3707,25 +3821,34 @@ var Observability = class extends BaseResource {
3707
3821
  const queryString = searchParams.toString();
3708
3822
  return this.request(`/api/observability/traces${queryString ? `?${queryString}` : ""}`);
3709
3823
  }
3824
+ /**
3825
+ * Retrieves paginated list of traces with optional filtering and sorting.
3826
+ * This is the new API with improved filtering options.
3827
+ *
3828
+ * @param params - Parameters for pagination, filtering, and ordering
3829
+ * @returns Promise containing paginated traces and pagination info
3830
+ */
3831
+ listTraces(params = {}) {
3832
+ const queryString = toQueryParams(params, ["filters", "pagination", "orderBy"]);
3833
+ return this.request(`/api/observability/traces${queryString ? `?${queryString}` : ""}`);
3834
+ }
3710
3835
  /**
3711
3836
  * Retrieves scores by trace ID and span ID
3712
3837
  * @param params - Parameters containing trace ID, span ID, and pagination options
3713
3838
  * @returns Promise containing scores and pagination info
3714
3839
  */
3715
3840
  listScoresBySpan(params) {
3716
- const { traceId, spanId, page, perPage } = params;
3717
- const searchParams = new URLSearchParams();
3718
- if (page !== void 0) {
3719
- searchParams.set("page", String(page));
3720
- }
3721
- if (perPage !== void 0) {
3722
- searchParams.set("perPage", String(perPage));
3723
- }
3724
- const queryString = searchParams.toString();
3841
+ const { traceId, spanId, ...pagination } = params;
3842
+ const queryString = toQueryParams(pagination);
3725
3843
  return this.request(
3726
3844
  `/api/observability/traces/${encodeURIComponent(traceId)}/${encodeURIComponent(spanId)}/scores${queryString ? `?${queryString}` : ""}`
3727
3845
  );
3728
3846
  }
3847
+ /**
3848
+ * Scores one or more traces using a specified scorer.
3849
+ * @param params - Scorer name and targets to score
3850
+ * @returns Promise containing the scoring status
3851
+ */
3729
3852
  score(params) {
3730
3853
  return this.request(`/api/observability/traces/score`, {
3731
3854
  method: "POST",
@@ -4275,9 +4398,27 @@ var MastraClient = class extends BaseResource {
4275
4398
  getTrace(traceId) {
4276
4399
  return this.observability.getTrace(traceId);
4277
4400
  }
4401
+ /**
4402
+ * Retrieves paginated list of traces with optional filtering.
4403
+ * This is the legacy API preserved for backward compatibility.
4404
+ *
4405
+ * @param params - Parameters for pagination and filtering (legacy format)
4406
+ * @returns Promise containing paginated traces and pagination info
4407
+ * @deprecated Use {@link listTraces} instead for new features like ordering and more filters.
4408
+ */
4278
4409
  getTraces(params) {
4279
4410
  return this.observability.getTraces(params);
4280
4411
  }
4412
+ /**
4413
+ * Retrieves paginated list of traces with optional filtering and sorting.
4414
+ * This is the new API with improved filtering options.
4415
+ *
4416
+ * @param params - Parameters for pagination, filtering, and ordering
4417
+ * @returns Promise containing paginated traces and pagination info
4418
+ */
4419
+ listTraces(params = {}) {
4420
+ return this.observability.listTraces(params);
4421
+ }
4281
4422
  listScoresBySpan(params) {
4282
4423
  return this.observability.listScoresBySpan(params);
4283
4424
  }
@@ -4330,6 +4471,16 @@ var MastraClient = class extends BaseResource {
4330
4471
  getStoredAgent(storedAgentId) {
4331
4472
  return new StoredAgent(this.options, storedAgentId);
4332
4473
  }
4474
+ // ============================================================================
4475
+ // System
4476
+ // ============================================================================
4477
+ /**
4478
+ * Retrieves installed Mastra packages and their versions
4479
+ * @returns Promise containing the list of installed Mastra packages
4480
+ */
4481
+ getSystemPackages() {
4482
+ return this.request("/api/system/packages");
4483
+ }
4333
4484
  };
4334
4485
 
4335
4486
  // src/tools.ts