@mastra/client-js 0.12.0 → 0.12.1-alpha.1

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
@@ -358,7 +358,11 @@ var Agent = class extends BaseResource {
358
358
  ...params,
359
359
  output: params.output ? zodToJsonSchema(params.output) : void 0,
360
360
  runtimeContext: parseClientRuntimeContext(params.runtimeContext),
361
- clientTools: processClientTools(params.clientTools)
361
+ clientTools: processClientTools(params.clientTools),
362
+ structuredOutput: params.structuredOutput ? {
363
+ ...params.structuredOutput,
364
+ schema: zodToJsonSchema(params.structuredOutput.schema)
365
+ } : void 0
362
366
  };
363
367
  const { runId, resourceId, threadId, runtimeContext } = processedParams;
364
368
  const response = await this.request(
@@ -863,7 +867,7 @@ var Agent = class extends BaseResource {
863
867
  step,
864
868
  toolCallId: chunk.payload.toolCallId,
865
869
  toolName: chunk.payload.toolName,
866
- args: void 0
870
+ args: chunk.payload.args
867
871
  };
868
872
  message.toolInvocations.push(invocation);
869
873
  updateToolInvocationPart(chunk.payload.toolCallId, invocation);
@@ -917,14 +921,14 @@ var Agent = class extends BaseResource {
917
921
  }
918
922
  case "step-finish": {
919
923
  step += 1;
920
- currentTextPart = chunk.payload.isContinued ? currentTextPart : void 0;
924
+ currentTextPart = chunk.payload.stepResult.isContinued ? currentTextPart : void 0;
921
925
  currentReasoningPart = void 0;
922
926
  currentReasoningTextDetail = void 0;
923
927
  execUpdate();
924
928
  break;
925
929
  }
926
930
  case "finish": {
927
- finishReason = chunk.payload.finishReason;
931
+ finishReason = chunk.payload.stepResult.reason;
928
932
  if (chunk.payload.usage != null) {
929
933
  usage = chunk.payload.usage;
930
934
  }
@@ -948,9 +952,28 @@ var Agent = class extends BaseResource {
948
952
  let toolCalls = [];
949
953
  let messages = [];
950
954
  const [streamForWritable, streamForProcessing] = response.body.tee();
951
- streamForWritable.pipeTo(writable, {
952
- preventClose: true
953
- }).catch((error) => {
955
+ streamForWritable.pipeTo(
956
+ new WritableStream({
957
+ async write(chunk) {
958
+ try {
959
+ const text = new TextDecoder().decode(chunk);
960
+ if (text.includes("[DONE]")) {
961
+ return;
962
+ }
963
+ } catch {
964
+ }
965
+ const writer = writable.getWriter();
966
+ try {
967
+ await writer.write(chunk);
968
+ } finally {
969
+ writer.releaseLock();
970
+ }
971
+ }
972
+ }),
973
+ {
974
+ preventClose: true
975
+ }
976
+ ).catch((error) => {
954
977
  console.error("Error piping to writable stream:", error);
955
978
  });
956
979
  this.processChatResponse_vNext({
@@ -987,7 +1010,8 @@ var Agent = class extends BaseResource {
987
1010
  toolCallId: toolCall2?.toolCallId
988
1011
  }
989
1012
  );
990
- const lastMessage = JSON.parse(JSON.stringify(messages[messages.length - 1]));
1013
+ const lastMessageRaw = messages[messages.length - 1];
1014
+ const lastMessage = lastMessageRaw != null ? JSON.parse(JSON.stringify(lastMessageRaw)) : void 0;
991
1015
  const toolInvocationPart = lastMessage?.parts?.find(
992
1016
  (part) => part.type === "tool-invocation" && part.toolInvocation?.toolCallId === toolCall2.toolCallId
993
1017
  );
@@ -1005,25 +1029,13 @@ var Agent = class extends BaseResource {
1005
1029
  toolInvocation.state = "result";
1006
1030
  toolInvocation.result = result;
1007
1031
  }
1008
- const writer = writable.getWriter();
1009
- try {
1010
- await writer.write(
1011
- new TextEncoder().encode(
1012
- "a:" + JSON.stringify({
1013
- toolCallId: toolCall2.toolCallId,
1014
- result
1015
- }) + "\n"
1016
- )
1017
- );
1018
- } finally {
1019
- writer.releaseLock();
1020
- }
1021
1032
  const originalMessages = processedParams.messages;
1022
1033
  const messageArray = Array.isArray(originalMessages) ? originalMessages : [originalMessages];
1034
+ const updatedMessages = lastMessage != null ? [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage] : [...messageArray, ...messages];
1023
1035
  this.processStreamResponse_vNext(
1024
1036
  {
1025
1037
  ...processedParams,
1026
- messages: [...messageArray, ...messages.filter((m) => m.id !== lastMessage.id), lastMessage]
1038
+ messages: updatedMessages
1027
1039
  },
1028
1040
  writable
1029
1041
  ).catch((error) => {
@@ -1051,7 +1063,11 @@ var Agent = class extends BaseResource {
1051
1063
  ...params,
1052
1064
  output: params.output ? zodToJsonSchema(params.output) : void 0,
1053
1065
  runtimeContext: parseClientRuntimeContext(params.runtimeContext),
1054
- clientTools: processClientTools(params.clientTools)
1066
+ clientTools: processClientTools(params.clientTools),
1067
+ structuredOutput: params.structuredOutput ? {
1068
+ ...params.structuredOutput,
1069
+ schema: zodToJsonSchema(params.structuredOutput.schema)
1070
+ } : void 0
1055
1071
  };
1056
1072
  const { readable, writable } = new TransformStream();
1057
1073
  const response = await this.processStreamResponse_vNext(processedParams, writable);
@@ -1897,6 +1913,57 @@ var Workflow = class extends BaseResource {
1897
1913
  });
1898
1914
  return response.body.pipeThrough(transformStream);
1899
1915
  }
1916
+ /**
1917
+ * Starts a workflow run and returns a stream
1918
+ * @param params - Object containing the optional runId, inputData and runtimeContext
1919
+ * @returns Promise containing the workflow execution results
1920
+ */
1921
+ async streamVNext(params) {
1922
+ const searchParams = new URLSearchParams();
1923
+ if (!!params?.runId) {
1924
+ searchParams.set("runId", params.runId);
1925
+ }
1926
+ const runtimeContext = parseClientRuntimeContext(params.runtimeContext);
1927
+ const response = await this.request(
1928
+ `/api/workflows/${this.workflowId}/streamVNext?${searchParams.toString()}`,
1929
+ {
1930
+ method: "POST",
1931
+ body: { inputData: params.inputData, runtimeContext },
1932
+ stream: true
1933
+ }
1934
+ );
1935
+ if (!response.ok) {
1936
+ throw new Error(`Failed to stream vNext workflow: ${response.statusText}`);
1937
+ }
1938
+ if (!response.body) {
1939
+ throw new Error("Response body is null");
1940
+ }
1941
+ let failedChunk = void 0;
1942
+ const transformStream = new TransformStream({
1943
+ start() {
1944
+ },
1945
+ async transform(chunk, controller) {
1946
+ try {
1947
+ const decoded = new TextDecoder().decode(chunk);
1948
+ const chunks = decoded.split(RECORD_SEPARATOR2);
1949
+ for (const chunk2 of chunks) {
1950
+ if (chunk2) {
1951
+ const newChunk = failedChunk ? failedChunk + chunk2 : chunk2;
1952
+ try {
1953
+ const parsedChunk = JSON.parse(newChunk);
1954
+ controller.enqueue(parsedChunk);
1955
+ failedChunk = void 0;
1956
+ } catch {
1957
+ failedChunk = newChunk;
1958
+ }
1959
+ }
1960
+ }
1961
+ } catch {
1962
+ }
1963
+ }
1964
+ });
1965
+ return response.body.pipeThrough(transformStream);
1966
+ }
1900
1967
  /**
1901
1968
  * Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
1902
1969
  * @param params - Object containing the runId, step, resumeData and runtimeContext
@@ -2459,7 +2526,7 @@ var Observability = class extends BaseResource {
2459
2526
  getTraces(params) {
2460
2527
  const { pagination, filters } = params;
2461
2528
  const { page, perPage, dateRange } = pagination || {};
2462
- const { name, spanType } = filters || {};
2529
+ const { name, spanType, entityId, entityType } = filters || {};
2463
2530
  const searchParams = new URLSearchParams();
2464
2531
  if (page !== void 0) {
2465
2532
  searchParams.set("page", String(page));
@@ -2473,6 +2540,10 @@ var Observability = class extends BaseResource {
2473
2540
  if (spanType !== void 0) {
2474
2541
  searchParams.set("spanType", String(spanType));
2475
2542
  }
2543
+ if (entityId && entityType) {
2544
+ searchParams.set("entityId", entityId);
2545
+ searchParams.set("entityType", entityType);
2546
+ }
2476
2547
  if (dateRange) {
2477
2548
  const dateRangeStr = JSON.stringify({
2478
2549
  start: dateRange.start instanceof Date ? dateRange.start.toISOString() : dateRange.start,