@mastra/client-js 1.57.0-alpha.0 → 1.57.0-alpha.2
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/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/reference-client-js-agent-controller.md +1 -1
- package/dist/docs/references/reference-client-js-agents.md +17 -1
- package/dist/docs/references/reference-client-js-datasets.md +1 -1
- package/dist/index.cjs +130 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +130 -24
- package/dist/index.js.map +1 -1
- package/dist/resources/agent.d.ts +10 -3
- package/dist/resources/agent.d.ts.map +1 -1
- package/dist/route-types.generated.d.ts +76 -41
- package/dist/route-types.generated.d.ts.map +1 -1
- package/dist/utils/process-mastra-stream.d.ts.map +1 -1
- package/dist/utils/sse-event-splitter.d.ts +3 -0
- package/dist/utils/sse-event-splitter.d.ts.map +1 -0
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -1056,11 +1056,51 @@ function processClientTools(clientTools) {
|
|
|
1056
1056
|
}));
|
|
1057
1057
|
}
|
|
1058
1058
|
//#endregion
|
|
1059
|
+
//#region src/utils/sse-event-splitter.ts
|
|
1060
|
+
/** Split SSE events without rescanning or copying an unfinished event on each read. */
|
|
1061
|
+
function createSseEventSplitter() {
|
|
1062
|
+
let parts = [];
|
|
1063
|
+
let pendingNewline = false;
|
|
1064
|
+
return (text, isFinal = false) => {
|
|
1065
|
+
const events = [];
|
|
1066
|
+
let start = 0;
|
|
1067
|
+
if (text) {
|
|
1068
|
+
if (pendingNewline) {
|
|
1069
|
+
if (text.startsWith("\n")) {
|
|
1070
|
+
events.push(parts.join(""));
|
|
1071
|
+
parts = [];
|
|
1072
|
+
start = 1;
|
|
1073
|
+
} else parts.push("\n");
|
|
1074
|
+
pendingNewline = false;
|
|
1075
|
+
}
|
|
1076
|
+
let separator;
|
|
1077
|
+
while ((separator = text.indexOf("\n\n", start)) !== -1) {
|
|
1078
|
+
parts.push(text.slice(start, separator));
|
|
1079
|
+
events.push(parts.join(""));
|
|
1080
|
+
parts = [];
|
|
1081
|
+
start = separator + 2;
|
|
1082
|
+
}
|
|
1083
|
+
if (start < text.length) {
|
|
1084
|
+
pendingNewline = text.endsWith("\n");
|
|
1085
|
+
const end = pendingNewline ? text.length - 1 : text.length;
|
|
1086
|
+
if (start < end) parts.push(text.slice(start, end));
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
if (isFinal) {
|
|
1090
|
+
if (pendingNewline) parts.push("\n");
|
|
1091
|
+
events.push(parts.join(""));
|
|
1092
|
+
parts = [];
|
|
1093
|
+
pendingNewline = false;
|
|
1094
|
+
}
|
|
1095
|
+
return events;
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
//#endregion
|
|
1059
1099
|
//#region src/utils/process-mastra-stream.ts
|
|
1060
1100
|
async function sharedProcessMastraStream({ stream, onChunk, signal }) {
|
|
1061
1101
|
const reader = stream.getReader();
|
|
1062
1102
|
const decoder = new TextDecoder();
|
|
1063
|
-
|
|
1103
|
+
const splitEvents = createSseEventSplitter();
|
|
1064
1104
|
const abort = () => void reader.cancel();
|
|
1065
1105
|
if (signal?.aborted) abort();
|
|
1066
1106
|
else signal?.addEventListener("abort", abort, { once: true });
|
|
@@ -1068,9 +1108,7 @@ async function sharedProcessMastraStream({ stream, onChunk, signal }) {
|
|
|
1068
1108
|
while (true) {
|
|
1069
1109
|
const { done, value } = await reader.read();
|
|
1070
1110
|
if (done) break;
|
|
1071
|
-
|
|
1072
|
-
const lines = buffer.split("\n\n");
|
|
1073
|
-
buffer = lines.pop() || "";
|
|
1111
|
+
const lines = splitEvents(decoder.decode(value, { stream: true }));
|
|
1074
1112
|
for (const line of lines) if (line.startsWith("data: ")) {
|
|
1075
1113
|
const data = line.slice(6);
|
|
1076
1114
|
if (data === "[DONE]") return;
|
|
@@ -1870,10 +1908,18 @@ var Agent = class extends BaseResource {
|
|
|
1870
1908
|
};
|
|
1871
1909
|
return streamResponse;
|
|
1872
1910
|
}
|
|
1873
|
-
|
|
1911
|
+
/** Stops execution of a run, including runs without a memory thread. */
|
|
1912
|
+
async abortRun(runId, options) {
|
|
1913
|
+
return this.request(`/agents/${this.agentId}/runs/${encodeURIComponent(runId)}/abort`, {
|
|
1914
|
+
method: "POST",
|
|
1915
|
+
signal: options?.signal
|
|
1916
|
+
});
|
|
1917
|
+
}
|
|
1918
|
+
async abortThread(params, options) {
|
|
1874
1919
|
const { resourceId, threadId, clearPendingSignals, expectedRunId } = params;
|
|
1875
1920
|
return this.request(`/agents/${this.agentId}/threads/abort`, {
|
|
1876
1921
|
method: "POST",
|
|
1922
|
+
signal: options?.signal,
|
|
1877
1923
|
body: {
|
|
1878
1924
|
resourceId,
|
|
1879
1925
|
threadId,
|
|
@@ -2607,10 +2653,11 @@ var Agent = class extends BaseResource {
|
|
|
2607
2653
|
}
|
|
2608
2654
|
/**
|
|
2609
2655
|
* Creates the consumer-facing stream for the streaming methods. Cancelling the
|
|
2610
|
-
* returned stream
|
|
2611
|
-
*
|
|
2656
|
+
* returned stream or the client-wide signal detaches the HTTP observer and
|
|
2657
|
+
* stops any client-tool continuations. Per-call execution cancellation is
|
|
2658
|
+
* handled separately by processStreamResponse.
|
|
2612
2659
|
*/
|
|
2613
|
-
createCancellableStream(
|
|
2660
|
+
createCancellableStream() {
|
|
2614
2661
|
const abortController = new AbortController();
|
|
2615
2662
|
let readableController;
|
|
2616
2663
|
return {
|
|
@@ -2623,10 +2670,63 @@ var Agent = class extends BaseResource {
|
|
|
2623
2670
|
}
|
|
2624
2671
|
}),
|
|
2625
2672
|
controller: readableController,
|
|
2626
|
-
signal: mergeAbortSignals(abortController.signal,
|
|
2673
|
+
signal: mergeAbortSignals(abortController.signal, this.options.abortSignal)
|
|
2627
2674
|
};
|
|
2628
2675
|
}
|
|
2629
2676
|
async processStreamResponse(processedParams, controller, route = "stream", signal) {
|
|
2677
|
+
const suppliedRunId = processedParams.runId;
|
|
2678
|
+
processedParams = {
|
|
2679
|
+
...processedParams,
|
|
2680
|
+
runId: processedParams.runId ?? v4()
|
|
2681
|
+
};
|
|
2682
|
+
const callerSignal = processedParams.abortSignal;
|
|
2683
|
+
callerSignal?.throwIfAborted();
|
|
2684
|
+
const transportAbort = new AbortController();
|
|
2685
|
+
const cancellationAbort = new AbortController();
|
|
2686
|
+
const detachSignal = signal;
|
|
2687
|
+
signal = mergeAbortSignals(detachSignal, transportAbort.signal);
|
|
2688
|
+
let cancellationTimer;
|
|
2689
|
+
const stopCancellation = () => cancellationAbort.abort();
|
|
2690
|
+
const cancelExecution = () => {
|
|
2691
|
+
cancellationTimer = setTimeout(() => cancellationAbort.abort(/* @__PURE__ */ new Error("Agent run cancellation was not acknowledged")), 1e4);
|
|
2692
|
+
(async () => {
|
|
2693
|
+
try {
|
|
2694
|
+
while (true) {
|
|
2695
|
+
cancellationAbort.signal.throwIfAborted();
|
|
2696
|
+
if ((await this.abortRun(processedParams.runId, { signal: cancellationAbort.signal })).aborted) return;
|
|
2697
|
+
await new Promise((resolve, reject) => {
|
|
2698
|
+
const onAbort = () => {
|
|
2699
|
+
clearTimeout(timer);
|
|
2700
|
+
reject(cancellationAbort.signal.reason);
|
|
2701
|
+
};
|
|
2702
|
+
const timer = setTimeout(() => {
|
|
2703
|
+
cancellationAbort.signal.removeEventListener("abort", onAbort);
|
|
2704
|
+
resolve();
|
|
2705
|
+
}, 100);
|
|
2706
|
+
cancellationAbort.signal.addEventListener("abort", onAbort, { once: true });
|
|
2707
|
+
if (cancellationAbort.signal.aborted) onAbort();
|
|
2708
|
+
});
|
|
2709
|
+
}
|
|
2710
|
+
} catch (error) {
|
|
2711
|
+
if (!cancellationAbort.signal.aborted && !(error instanceof Error && "status" in error && (error.status === 404 || error.status === 501))) try {
|
|
2712
|
+
controller.error(error);
|
|
2713
|
+
} catch {}
|
|
2714
|
+
} finally {
|
|
2715
|
+
clearTimeout(cancellationTimer);
|
|
2716
|
+
transportAbort.abort(callerSignal?.reason);
|
|
2717
|
+
}
|
|
2718
|
+
})();
|
|
2719
|
+
};
|
|
2720
|
+
const cleanupCancellation = () => {
|
|
2721
|
+
callerSignal?.removeEventListener("abort", cancelExecution);
|
|
2722
|
+
detachSignal?.removeEventListener("abort", stopCancellation);
|
|
2723
|
+
clearTimeout(cancellationTimer);
|
|
2724
|
+
stopCancellation();
|
|
2725
|
+
};
|
|
2726
|
+
detachSignal?.addEventListener("abort", stopCancellation, { once: true });
|
|
2727
|
+
if (detachSignal?.aborted) stopCancellation();
|
|
2728
|
+
callerSignal?.addEventListener("abort", cancelExecution, { once: true });
|
|
2729
|
+
if (callerSignal?.aborted) cancelExecution();
|
|
2630
2730
|
const { memory } = processedParams ?? {};
|
|
2631
2731
|
const { resource, thread } = memory ?? {};
|
|
2632
2732
|
const threadId = processedParams.threadId ?? (typeof thread === "string" ? thread : thread?.id);
|
|
@@ -2643,9 +2743,15 @@ var Agent = class extends BaseResource {
|
|
|
2643
2743
|
body: requestBody,
|
|
2644
2744
|
stream: true,
|
|
2645
2745
|
signal
|
|
2746
|
+
}).catch((error) => {
|
|
2747
|
+
cleanupCancellation();
|
|
2748
|
+
throw error;
|
|
2646
2749
|
});
|
|
2647
|
-
if (!response.body)
|
|
2648
|
-
|
|
2750
|
+
if (!response.body) {
|
|
2751
|
+
cleanupCancellation();
|
|
2752
|
+
throw new Error("No response body");
|
|
2753
|
+
}
|
|
2754
|
+
const isAborted = () => Boolean(signal?.aborted || callerSignal?.aborted);
|
|
2649
2755
|
const isAbortError = (error) => isAborted() || error?.name === "AbortError";
|
|
2650
2756
|
const safeClose = () => {
|
|
2651
2757
|
try {
|
|
@@ -2658,18 +2764,16 @@ var Agent = class extends BaseResource {
|
|
|
2658
2764
|
};
|
|
2659
2765
|
try {
|
|
2660
2766
|
let messages = [];
|
|
2661
|
-
let streamRunId =
|
|
2767
|
+
let streamRunId = suppliedRunId;
|
|
2662
2768
|
let finishedHistory;
|
|
2663
2769
|
let suspended = false;
|
|
2664
2770
|
const resumedToolCallIds = /* @__PURE__ */ new Set();
|
|
2665
2771
|
const releaseOpenStream = streamRunId ? this.trackOpenStream(streamRunId) : () => {};
|
|
2666
2772
|
const [streamForController, streamForProcessing] = response.body.tee();
|
|
2667
2773
|
const decoder = new TextDecoder();
|
|
2668
|
-
|
|
2774
|
+
const splitEvents = createSseEventSplitter();
|
|
2669
2775
|
const enqueueReadableText = (text, isFinal = false) => {
|
|
2670
|
-
|
|
2671
|
-
const lines = pendingText.split("\n\n");
|
|
2672
|
-
pendingText = isFinal ? "" : lines.pop() ?? "";
|
|
2776
|
+
const lines = splitEvents(text, isFinal);
|
|
2673
2777
|
for (const line of lines) {
|
|
2674
2778
|
if (!line.startsWith("data: ") || !line.includes("\"type\":\"finish\"")) continue;
|
|
2675
2779
|
try {
|
|
@@ -2876,10 +2980,12 @@ var Agent = class extends BaseResource {
|
|
|
2876
2980
|
} catch {}
|
|
2877
2981
|
safeClose();
|
|
2878
2982
|
}).finally(() => {
|
|
2983
|
+
cleanupCancellation();
|
|
2879
2984
|
signal?.removeEventListener("abort", onAbort);
|
|
2880
2985
|
releaseOpenStream();
|
|
2881
2986
|
});
|
|
2882
2987
|
} catch (error) {
|
|
2988
|
+
cleanupCancellation();
|
|
2883
2989
|
if (!isAbortError(error)) console.error("Error processing stream response:", error);
|
|
2884
2990
|
}
|
|
2885
2991
|
return response;
|
|
@@ -2977,7 +3083,7 @@ var Agent = class extends BaseResource {
|
|
|
2977
3083
|
clientTools: processClientTools(params.clientToolsResolver?.() ?? params.clientTools),
|
|
2978
3084
|
structuredOutput
|
|
2979
3085
|
};
|
|
2980
|
-
const { readable, controller: readableController, signal } = this.createCancellableStream(
|
|
3086
|
+
const { readable, controller: readableController, signal } = this.createCancellableStream();
|
|
2981
3087
|
const response = await this.processStreamResponse(processedParams, readableController, "stream", signal);
|
|
2982
3088
|
const streamResponse = new Response(readable, {
|
|
2983
3089
|
status: response.status,
|
|
@@ -3008,7 +3114,7 @@ var Agent = class extends BaseResource {
|
|
|
3008
3114
|
clientTools: processClientTools(params.clientToolsResolver?.() ?? params.clientTools),
|
|
3009
3115
|
structuredOutput
|
|
3010
3116
|
};
|
|
3011
|
-
const { readable, controller: readableController, signal } = this.createCancellableStream(
|
|
3117
|
+
const { readable, controller: readableController, signal } = this.createCancellableStream();
|
|
3012
3118
|
const response = await this.processStreamResponse(processedParams, readableController, "stream-until-idle", signal);
|
|
3013
3119
|
const streamResponse = new Response(readable, {
|
|
3014
3120
|
status: response.status,
|
|
@@ -3052,7 +3158,7 @@ var Agent = class extends BaseResource {
|
|
|
3052
3158
|
requestContext: parseClientRequestContext(requestContext),
|
|
3053
3159
|
clientTools: processClientTools(executableClientTools)
|
|
3054
3160
|
};
|
|
3055
|
-
const { readable, controller: readableController, signal } = this.createCancellableStream(
|
|
3161
|
+
const { readable, controller: readableController, signal } = this.createCancellableStream();
|
|
3056
3162
|
const response = await this.processStreamResponse(processedParams, readableController, "approve-tool-call", signal).catch((error) => {
|
|
3057
3163
|
releaseHold({ force: true });
|
|
3058
3164
|
throw error;
|
|
@@ -3089,7 +3195,7 @@ var Agent = class extends BaseResource {
|
|
|
3089
3195
|
requestContext: parseClientRequestContext(requestContext),
|
|
3090
3196
|
clientTools: processClientTools(executableClientTools)
|
|
3091
3197
|
};
|
|
3092
|
-
const { readable, controller: readableController, signal } = this.createCancellableStream(
|
|
3198
|
+
const { readable, controller: readableController, signal } = this.createCancellableStream();
|
|
3093
3199
|
const response = await this.processStreamResponse(processedParams, readableController, "decline-tool-call", signal).catch((error) => {
|
|
3094
3200
|
releaseHold({ force: true });
|
|
3095
3201
|
throw error;
|
|
@@ -3165,7 +3271,7 @@ var Agent = class extends BaseResource {
|
|
|
3165
3271
|
schema: standardSchemaToJSONSchema(toStandardSchema(options.structuredOutput.schema))
|
|
3166
3272
|
} : void 0
|
|
3167
3273
|
};
|
|
3168
|
-
const { readable, controller: readableController, signal } = this.createCancellableStream(
|
|
3274
|
+
const { readable, controller: readableController, signal } = this.createCancellableStream();
|
|
3169
3275
|
const response = await this.processStreamResponse(processedParams, readableController, "resume-stream", signal).catch((error) => {
|
|
3170
3276
|
releaseHold({ force: true });
|
|
3171
3277
|
throw error;
|
|
@@ -3234,7 +3340,7 @@ var Agent = class extends BaseResource {
|
|
|
3234
3340
|
schema: standardSchemaToJSONSchema(toStandardSchema(options.structuredOutput.schema))
|
|
3235
3341
|
} : void 0
|
|
3236
3342
|
};
|
|
3237
|
-
const { readable, controller: readableController, signal } = this.createCancellableStream(
|
|
3343
|
+
const { readable, controller: readableController, signal } = this.createCancellableStream();
|
|
3238
3344
|
const response = await this.processStreamResponse(processedParams, readableController, "resume-stream-until-idle", signal).catch((error) => {
|
|
3239
3345
|
releaseHold({ force: true });
|
|
3240
3346
|
throw error;
|
|
@@ -4392,7 +4498,7 @@ var Workflow = class extends BaseResource {
|
|
|
4392
4498
|
*/
|
|
4393
4499
|
async createRun(params) {
|
|
4394
4500
|
const searchParams = new URLSearchParams();
|
|
4395
|
-
if (
|
|
4501
|
+
if (params?.runId) searchParams.set("runId", params.runId);
|
|
4396
4502
|
const res = await this.request(`/workflows/${this.workflowId}/create-run?${searchParams.toString()}`, {
|
|
4397
4503
|
method: "POST",
|
|
4398
4504
|
body: {
|
|
@@ -4950,7 +5056,7 @@ var AgentBuilder = class extends BaseResource {
|
|
|
4950
5056
|
*/
|
|
4951
5057
|
async createRun(params) {
|
|
4952
5058
|
const searchParams = new URLSearchParams();
|
|
4953
|
-
if (
|
|
5059
|
+
if (params?.runId) searchParams.set("runId", params.runId);
|
|
4954
5060
|
const url = `/agent-builder/${this.actionId}/create-run${searchParams.toString() ? `?${searchParams.toString()}` : ""}`;
|
|
4955
5061
|
return this.request(url, { method: "POST" });
|
|
4956
5062
|
}
|