@mastra/ai-sdk 0.2.5 → 0.2.6

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/CHANGELOG.md CHANGED
@@ -1,5 +1,35 @@
1
1
  # @mastra/ai-sdk
2
2
 
3
+ ## 0.2.6
4
+
5
+ ### Patch Changes
6
+
7
+ - update peerdeps ([`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc))
8
+
9
+ - Preserve lastMessageId in chatRoute ([#9728](https://github.com/mastra-ai/mastra/pull/9728))
10
+
11
+ - Handle custom data writes in agent network execution events in ai sdk transformers ([#9734](https://github.com/mastra-ai/mastra/pull/9734))
12
+
13
+ - Add support for suspend/resume in AI SDK workflowRoute ([#9732](https://github.com/mastra-ai/mastra/pull/9732))
14
+
15
+ - Updated dependencies [[`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc), [`6d7e90d`](https://github.com/mastra-ai/mastra/commit/6d7e90db09713e6250f4d6c3d3cff1b4740e50f9), [`f78b908`](https://github.com/mastra-ai/mastra/commit/f78b9080e11af765969b36b4a619761056030840), [`23c2614`](https://github.com/mastra-ai/mastra/commit/23c26140fdbf04b8c59e8d7d52106d67dad962ec), [`e365eda`](https://github.com/mastra-ai/mastra/commit/e365eda45795b43707310531cac1e2ce4e5a0712)]:
16
+ - @mastra/core@0.24.0
17
+
18
+ ## 0.2.6-alpha.0
19
+
20
+ ### Patch Changes
21
+
22
+ - update peerdeps ([`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc))
23
+
24
+ - Preserve lastMessageId in chatRoute ([#9728](https://github.com/mastra-ai/mastra/pull/9728))
25
+
26
+ - Handle custom data writes in agent network execution events in ai sdk transformers ([#9734](https://github.com/mastra-ai/mastra/pull/9734))
27
+
28
+ - Add support for suspend/resume in AI SDK workflowRoute ([#9732](https://github.com/mastra-ai/mastra/pull/9732))
29
+
30
+ - Updated dependencies [[`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc), [`6d7e90d`](https://github.com/mastra-ai/mastra/commit/6d7e90db09713e6250f4d6c3d3cff1b4740e50f9), [`f78b908`](https://github.com/mastra-ai/mastra/commit/f78b9080e11af765969b36b4a619761056030840), [`23c2614`](https://github.com/mastra-ai/mastra/commit/23c26140fdbf04b8c59e8d7d52106d67dad962ec), [`e365eda`](https://github.com/mastra-ai/mastra/commit/e365eda45795b43707310531cac1e2ce4e5a0712)]:
31
+ - @mastra/core@0.24.0-alpha.0
32
+
3
33
  ## 0.2.5
4
34
 
5
35
  ### Patch Changes
@@ -1 +1 @@
1
- {"version":3,"file":"chat-route.d.ts","sourceRoot":"","sources":["../src/chat-route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAIxD,MAAM,MAAM,gBAAgB,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,IAAI;IACtE,cAAc,CAAC,EAAE,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzD,GAAG,CACA;IACE,IAAI,EAAE,GAAG,MAAM,WAAW,MAAM,EAAE,CAAC;IACnC,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,GACD;IACE,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CACJ,CAAC;AAEF,wBAAgB,SAAS,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,EAAE,EACjE,IAAuB,EACvB,KAAK,EACL,cAAc,GACf,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAwJhE"}
1
+ {"version":3,"file":"chat-route.d.ts","sourceRoot":"","sources":["../src/chat-route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAIxD,MAAM,MAAM,gBAAgB,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,IAAI;IACtE,cAAc,CAAC,EAAE,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzD,GAAG,CACA;IACE,IAAI,EAAE,GAAG,MAAM,WAAW,MAAM,EAAE,CAAC;IACnC,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,GACD;IACE,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CACJ,CAAC;AAEF,wBAAgB,SAAS,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,EAAE,EACjE,IAAuB,EACvB,KAAK,EACL,cAAc,GACf,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CA6JhE"}
package/dist/index.cjs CHANGED
@@ -10,6 +10,12 @@ var stream = require('@mastra/core/stream');
10
10
  var isDataChunkType = (chunk) => {
11
11
  return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
12
12
  };
13
+ var isAgentExecutionDataChunkType = (chunk) => {
14
+ return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("agent-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
15
+ };
16
+ var isWorkflowExecutionDataChunkType = (chunk) => {
17
+ return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("workflow-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
18
+ };
13
19
 
14
20
  // src/helpers.ts
15
21
  function convertMastraChunkToAISDKv5({
@@ -443,7 +449,7 @@ function AgentNetworkToAISDKTransformer() {
443
449
  }
444
450
  });
445
451
  }
446
- function AgentStreamToAISDKTransformer() {
452
+ function AgentStreamToAISDKTransformer(lastMessageId) {
447
453
  let bufferedSteps = /* @__PURE__ */ new Map();
448
454
  return new TransformStream({
449
455
  transform(chunk, controller) {
@@ -454,7 +460,7 @@ function AgentStreamToAISDKTransformer() {
454
460
  sendSources: false,
455
461
  sendStart: true,
456
462
  sendFinish: true,
457
- responseMessageId: chunk.runId,
463
+ responseMessageId: lastMessageId,
458
464
  onError() {
459
465
  return "Error";
460
466
  }
@@ -638,7 +644,9 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
638
644
  name: payload.payload.id,
639
645
  status: payload.payload.status,
640
646
  input: payload.payload.payload ?? null,
641
- output: null
647
+ output: null,
648
+ suspendPayload: null,
649
+ resumePayload: null
642
650
  };
643
651
  bufferedWorkflows.set(payload.runId, current);
644
652
  return {
@@ -671,6 +679,27 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
671
679
  }
672
680
  };
673
681
  }
682
+ case "workflow-step-suspended": {
683
+ const current = bufferedWorkflows.get(payload.runId);
684
+ if (!current) return null;
685
+ current.steps[payload.payload.id] = {
686
+ ...current.steps[payload.payload.id],
687
+ status: payload.payload.status,
688
+ suspendPayload: payload.payload.suspendPayload ?? null,
689
+ resumePayload: payload.payload.resumePayload ?? null,
690
+ output: null
691
+ };
692
+ return {
693
+ type: isNested ? "data-tool-workflow" : "data-workflow",
694
+ id: payload.runId,
695
+ data: {
696
+ name: current.name,
697
+ status: "suspended",
698
+ steps: current.steps,
699
+ output: null
700
+ }
701
+ };
702
+ }
674
703
  case "workflow-finish": {
675
704
  const current = bufferedWorkflows.get(payload.runId);
676
705
  if (!current) return null;
@@ -745,7 +774,9 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
745
774
  name: payload.payload.agentId,
746
775
  status: "running",
747
776
  input: payload.payload.args || null,
748
- output: null
777
+ output: null,
778
+ suspendPayload: null,
779
+ resumePayload: null
749
780
  });
750
781
  bufferedNetworks.set(payload.runId, current);
751
782
  return {
@@ -763,7 +794,9 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
763
794
  name: payload.payload.name,
764
795
  status: "running",
765
796
  input: payload.payload.args || null,
766
- output: null
797
+ output: null,
798
+ suspendPayload: null,
799
+ resumePayload: null
767
800
  });
768
801
  bufferedNetworks.set(payload.runId, current);
769
802
  return {
@@ -781,7 +814,9 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
781
814
  name: payload.payload.args?.toolName,
782
815
  status: "running",
783
816
  input: payload.payload.args?.args || null,
784
- output: null
817
+ output: null,
818
+ suspendPayload: null,
819
+ resumePayload: null
785
820
  });
786
821
  bufferedNetworks.set(payload.runId, current);
787
822
  return {
@@ -800,7 +835,9 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
800
835
  name: payload.payload.agentId,
801
836
  status: "success",
802
837
  input: null,
803
- output: payload.payload.result
838
+ output: payload.payload.result,
839
+ suspendPayload: null,
840
+ resumePayload: null
804
841
  });
805
842
  return {
806
843
  type: isNested ? "data-tool-network" : "data-network",
@@ -820,7 +857,9 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
820
857
  name: payload.payload.toolName,
821
858
  status: "success",
822
859
  input: null,
823
- output: payload.payload.result
860
+ output: payload.payload.result,
861
+ suspendPayload: null,
862
+ resumePayload: null
824
863
  });
825
864
  return {
826
865
  type: isNested ? "data-tool-network" : "data-network",
@@ -839,7 +878,9 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
839
878
  name: payload.payload.name,
840
879
  status: "success",
841
880
  input: null,
842
- output: payload.payload.result
881
+ output: payload.payload.result,
882
+ suspendPayload: null,
883
+ resumePayload: null
843
884
  });
844
885
  return {
845
886
  type: isNested ? "data-tool-network" : "data-network",
@@ -903,6 +944,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
903
944
  }
904
945
  return payload;
905
946
  }
947
+ if (isAgentExecutionDataChunkType(payload)) {
948
+ if (!("data" in payload.payload)) {
949
+ throw new Error(
950
+ `UI Messages require a data property when using data- prefixed chunks
951
+ ${JSON.stringify(payload)}`
952
+ );
953
+ }
954
+ return payload.payload;
955
+ }
956
+ if (isWorkflowExecutionDataChunkType(payload)) {
957
+ if (!("data" in payload.payload)) {
958
+ throw new Error(
959
+ `UI Messages require a data property when using data- prefixed chunks
960
+ ${JSON.stringify(payload)}`
961
+ );
962
+ }
963
+ return payload.payload;
964
+ }
906
965
  return null;
907
966
  }
908
967
  }
@@ -918,7 +977,7 @@ function toAISdkFormat(stream, options = { from: "agent" }) {
918
977
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
919
978
  }
920
979
  const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
921
- return agentReadable.pipeThrough(AgentStreamToAISDKTransformer());
980
+ return agentReadable.pipeThrough(AgentStreamToAISDKTransformer(options?.lastMessageId));
922
981
  }
923
982
 
924
983
  // src/chat-route.ts
@@ -1052,10 +1111,14 @@ function chatRoute({
1052
1111
  ...rest,
1053
1112
  runtimeContext: runtimeContext || defaultOptions?.runtimeContext
1054
1113
  });
1114
+ let lastMessageId;
1115
+ if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
1116
+ lastMessageId = messages[messages.length - 1].id;
1117
+ }
1055
1118
  const uiMessageStream = ai.createUIMessageStream({
1056
1119
  originalMessages: messages,
1057
1120
  execute: async ({ writer }) => {
1058
- for await (const part of toAISdkFormat(result, { from: "agent" })) {
1121
+ for await (const part of toAISdkFormat(result, { from: "agent", lastMessageId })) {
1059
1122
  writer.write(part);
1060
1123
  }
1061
1124
  }
@@ -1115,7 +1178,7 @@ function workflowRoute({
1115
1178
  }
1116
1179
  },
1117
1180
  handler: async (c) => {
1118
- const { inputData, ...rest } = await c.req.json();
1181
+ const { inputData, resumeData, ...rest } = await c.req.json();
1119
1182
  const mastra = c.get("mastra");
1120
1183
  let workflowToUse = workflow;
1121
1184
  if (!workflow) {
@@ -1135,7 +1198,7 @@ function workflowRoute({
1135
1198
  throw new Error(`Workflow ${workflowToUse} not found`);
1136
1199
  }
1137
1200
  const run = await workflowObj.createRunAsync();
1138
- const stream = run.streamVNext({ inputData, ...rest });
1201
+ const stream = resumeData ? run.resumeStream({ resumeData, ...rest }) : run.stream({ inputData, ...rest });
1139
1202
  const uiMessageStream = ai.createUIMessageStream({
1140
1203
  execute: async ({ writer }) => {
1141
1204
  for await (const part of toAISdkFormat(stream, { from: "workflow" })) {