@mastra/ai-sdk 0.0.0-fix-issue-10434-concurrent-write-corruption-20251124213939 → 0.0.0-fix-backport-setserver-20251201151948

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
@@ -8,6 +8,31 @@ import { DefaultGeneratedFile, DefaultGeneratedFileWithType } from '@mastra/core
8
8
  var isDataChunkType = (chunk) => {
9
9
  return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
10
10
  };
11
+ var isMastraTextStreamChunk = (chunk) => {
12
+ return chunk && typeof chunk === "object" && "type" in chunk && typeof chunk.type === "string" && [
13
+ "text-start",
14
+ "text-delta",
15
+ "text-end",
16
+ "reasoning-start",
17
+ "reasoning-delta",
18
+ "reasoning-end",
19
+ "file",
20
+ "source",
21
+ "tool-input-start",
22
+ "tool-input-delta",
23
+ "tool-call",
24
+ "tool-result",
25
+ "tool-error",
26
+ "error",
27
+ "start-step",
28
+ "finish-step",
29
+ "start",
30
+ "finish",
31
+ "abort",
32
+ "tool-input-end",
33
+ "raw"
34
+ ].includes(chunk.type);
35
+ };
11
36
  function safeParseErrorObject(obj) {
12
37
  if (typeof obj !== "object" || obj === null) {
13
38
  return String(obj);
@@ -489,7 +514,10 @@ function convertFullStreamChunkToUIMessageStream({
489
514
  }
490
515
 
491
516
  // src/transformers.ts
492
- function WorkflowStreamToAISDKTransformer() {
517
+ var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
518
+ function WorkflowStreamToAISDKTransformer({
519
+ includeTextStreamParts
520
+ } = {}) {
493
521
  const bufferedWorkflows = /* @__PURE__ */ new Map();
494
522
  return new TransformStream({
495
523
  start(controller) {
@@ -503,7 +531,7 @@ function WorkflowStreamToAISDKTransformer() {
503
531
  });
504
532
  },
505
533
  transform(chunk, controller) {
506
- const transformed = transformWorkflow(chunk, bufferedWorkflows);
534
+ const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
507
535
  if (transformed) controller.enqueue(transformed);
508
536
  }
509
537
  });
@@ -724,7 +752,7 @@ function transformAgent(payload, bufferedSteps) {
724
752
  }
725
753
  return null;
726
754
  }
727
- function transformWorkflow(payload, bufferedWorkflows, isNested) {
755
+ function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
728
756
  switch (payload.type) {
729
757
  case "workflow-start":
730
758
  bufferedWorkflows.set(payload.runId, {
@@ -819,6 +847,16 @@ function transformWorkflow(payload, bufferedWorkflows, isNested) {
819
847
  }
820
848
  case "workflow-step-output": {
821
849
  const output = payload.payload.output;
850
+ if (includeTextStreamParts && output && isMastraTextStreamChunk(output)) {
851
+ const part = convertMastraChunkToAISDKv5({ chunk: output, mode: "stream" });
852
+ const transformedChunk = convertFullStreamChunkToUIMessageStream({
853
+ part,
854
+ onError(error) {
855
+ return safeParseErrorObject(error);
856
+ }
857
+ });
858
+ return transformedChunk;
859
+ }
822
860
  if (output && isDataChunkType(output)) {
823
861
  if (!("data" in output)) {
824
862
  throw new Error(
@@ -849,12 +887,29 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
849
887
  case "routing-agent-start": {
850
888
  if (!bufferedNetworks.has(payload.runId)) {
851
889
  bufferedNetworks.set(payload.runId, {
852
- name: payload.payload.agentId,
890
+ name: payload.payload.networkId,
853
891
  steps: [],
854
892
  usage: null,
855
893
  output: null
856
894
  });
857
895
  }
896
+ const current = bufferedNetworks.get(payload.runId);
897
+ current.steps.push({
898
+ id: payload.payload.runId,
899
+ name: payload.payload.agentId,
900
+ status: "running",
901
+ iteration: payload.payload.inputData.iteration,
902
+ input: {
903
+ task: payload.payload.inputData.task,
904
+ threadId: payload.payload.inputData.threadId,
905
+ threadResourceId: payload.payload.inputData.threadResourceId
906
+ },
907
+ output: "",
908
+ task: null,
909
+ suspendPayload: null,
910
+ resumePayload: null,
911
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
912
+ });
858
913
  return {
859
914
  type: isNested ? "data-tool-network" : "data-network",
860
915
  id: payload.runId,
@@ -885,14 +940,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
885
940
  };
886
941
  }
887
942
  case "agent-execution-start": {
888
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
943
+ const current = bufferedNetworks.get(payload.runId);
944
+ if (!current) return null;
889
945
  current.steps.push({
946
+ id: payload.payload.runId,
890
947
  name: payload.payload.agentId,
891
948
  status: "running",
892
- input: payload.payload.args || null,
949
+ iteration: payload.payload.args?.iteration ?? 0,
950
+ input: { prompt: payload.payload.args?.prompt ?? "" },
893
951
  output: null,
952
+ task: null,
894
953
  suspendPayload: null,
895
- resumePayload: null
954
+ resumePayload: null,
955
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
896
956
  });
897
957
  bufferedNetworks.set(payload.runId, current);
898
958
  return {
@@ -905,14 +965,19 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
905
965
  };
906
966
  }
907
967
  case "workflow-execution-start": {
908
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
968
+ const current = bufferedNetworks.get(payload.runId);
969
+ if (!current) return null;
909
970
  current.steps.push({
910
- name: payload.payload.name,
971
+ id: payload.payload.runId,
972
+ name: payload.payload.workflowId,
911
973
  status: "running",
912
- input: payload.payload.args || null,
974
+ iteration: payload.payload.args?.iteration ?? 0,
975
+ input: { prompt: payload.payload.args?.prompt ?? "" },
913
976
  output: null,
977
+ task: null,
914
978
  suspendPayload: null,
915
- resumePayload: null
979
+ resumePayload: null,
980
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
916
981
  });
917
982
  bufferedNetworks.set(payload.runId, current);
918
983
  return {
@@ -925,14 +990,21 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
925
990
  };
926
991
  }
927
992
  case "tool-execution-start": {
928
- const current = bufferedNetworks.get(payload.runId) || { name: "", steps: [], usage: null, output: null };
993
+ const current = bufferedNetworks.get(payload.runId);
994
+ if (!current) return null;
929
995
  current.steps.push({
996
+ id: payload.payload.args.toolCallId,
930
997
  name: payload.payload.args?.toolName,
931
998
  status: "running",
999
+ iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
1000
+ task: {
1001
+ id: payload.payload.args?.toolName
1002
+ },
932
1003
  input: payload.payload.args?.args || null,
933
1004
  output: null,
934
1005
  suspendPayload: null,
935
- resumePayload: null
1006
+ resumePayload: null,
1007
+ [PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
936
1008
  });
937
1009
  bufferedNetworks.set(payload.runId, current);
938
1010
  return {
@@ -947,14 +1019,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
947
1019
  case "agent-execution-end": {
948
1020
  const current = bufferedNetworks.get(payload.runId);
949
1021
  if (!current) return null;
950
- current.steps.push({
951
- name: payload.payload.agentId,
952
- status: "success",
953
- input: null,
954
- output: payload.payload.result,
955
- suspendPayload: null,
956
- resumePayload: null
957
- });
1022
+ const stepId = payload.payload.runId;
1023
+ const step = current.steps.find((step2) => step2.id === stepId);
1024
+ if (!step) {
1025
+ return null;
1026
+ }
1027
+ step.status = "success";
1028
+ step.output = payload.payload.result;
958
1029
  return {
959
1030
  type: isNested ? "data-tool-network" : "data-network",
960
1031
  id: payload.runId,
@@ -969,14 +1040,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
969
1040
  case "tool-execution-end": {
970
1041
  const current = bufferedNetworks.get(payload.runId);
971
1042
  if (!current) return null;
972
- current.steps.push({
973
- name: payload.payload.toolName,
974
- status: "success",
975
- input: null,
976
- output: payload.payload.result,
977
- suspendPayload: null,
978
- resumePayload: null
979
- });
1043
+ const stepId = payload.payload.toolCallId;
1044
+ const step = current.steps.find((step2) => step2.id === stepId);
1045
+ if (!step) {
1046
+ return null;
1047
+ }
1048
+ step.status = "success";
1049
+ step.output = payload.payload.result;
980
1050
  return {
981
1051
  type: isNested ? "data-tool-network" : "data-network",
982
1052
  id: payload.runId,
@@ -990,14 +1060,13 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
990
1060
  case "workflow-execution-end": {
991
1061
  const current = bufferedNetworks.get(payload.runId);
992
1062
  if (!current) return null;
993
- current.steps.push({
994
- name: payload.payload.name,
995
- status: "success",
996
- input: null,
997
- output: payload.payload.result,
998
- suspendPayload: null,
999
- resumePayload: null
1000
- });
1063
+ const stepId = payload.payload.runId;
1064
+ const step = current.steps.find((step2) => step2.id === stepId);
1065
+ if (!step) {
1066
+ return null;
1067
+ }
1068
+ step.status = "success";
1069
+ step.output = payload.payload.result;
1001
1070
  return {
1002
1071
  type: isNested ? "data-tool-network" : "data-network",
1003
1072
  id: payload.runId,
@@ -1012,12 +1081,24 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
1012
1081
  case "routing-agent-end": {
1013
1082
  const current = bufferedNetworks.get(payload.runId);
1014
1083
  if (!current) return null;
1084
+ const stepId = payload.payload.runId;
1085
+ const step = current.steps.find((step2) => step2.id === stepId);
1086
+ if (!step) {
1087
+ return null;
1088
+ }
1089
+ step.status = "success";
1090
+ step.task = {
1091
+ id: payload.payload.primitiveId,
1092
+ type: payload.payload.primitiveType,
1093
+ name: payload.payload.task,
1094
+ reason: payload.payload.selectionReason
1095
+ };
1096
+ step.output = payload.payload.result;
1015
1097
  return {
1016
1098
  type: isNested ? "data-tool-network" : "data-network",
1017
1099
  id: payload.runId,
1018
1100
  data: {
1019
1101
  ...current,
1020
- status: "finished",
1021
1102
  usage: payload.payload?.usage ?? current.usage,
1022
1103
  output: payload.payload?.result ?? current.output
1023
1104
  }
@@ -1051,6 +1132,39 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
1051
1132
  };
1052
1133
  }
1053
1134
  default: {
1135
+ if (payload.type.startsWith("agent-execution-event-")) {
1136
+ const stepId = payload.payload.runId;
1137
+ const current = bufferedNetworks.get(payload.runId);
1138
+ if (!current) return null;
1139
+ const step = current.steps.find((step2) => step2.id === stepId);
1140
+ if (!step) {
1141
+ return null;
1142
+ }
1143
+ step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
1144
+ const result = transformAgent(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
1145
+ if (result) {
1146
+ const { request, response, ...data } = result.data;
1147
+ step.task = data;
1148
+ }
1149
+ }
1150
+ if (payload.type.startsWith("workflow-execution-event-")) {
1151
+ const stepId = payload.payload.runId;
1152
+ const current = bufferedNetworks.get(payload.runId);
1153
+ if (!current) return null;
1154
+ const step = current.steps.find((step2) => step2.id === stepId);
1155
+ if (!step) {
1156
+ return null;
1157
+ }
1158
+ step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
1159
+ const result = transformWorkflow(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
1160
+ if (result && "data" in result) {
1161
+ const data = result.data;
1162
+ step.task = data;
1163
+ if (data.name && step.task) {
1164
+ step.task.id = data.name;
1165
+ }
1166
+ }
1167
+ }
1054
1168
  if (isDataChunkType(payload)) {
1055
1169
  if (!("data" in payload)) {
1056
1170
  throw new Error(
@@ -1058,7 +1172,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
1058
1172
  ${JSON.stringify(payload)}`
1059
1173
  );
1060
1174
  }
1061
- return payload;
1175
+ const { type, data } = payload;
1176
+ return { type, data };
1062
1177
  }
1063
1178
  if (isAgentExecutionDataChunkType(payload)) {
1064
1179
  if (!("data" in payload.payload)) {
@@ -1067,7 +1182,8 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
1067
1182
  ${JSON.stringify(payload)}`
1068
1183
  );
1069
1184
  }
1070
- return payload.payload;
1185
+ const { type, data } = payload.payload;
1186
+ return { type, data };
1071
1187
  }
1072
1188
  if (isWorkflowExecutionDataChunkType(payload)) {
1073
1189
  if (!("data" in payload.payload)) {
@@ -1076,22 +1192,26 @@ function transformNetwork(payload, bufferedNetworks, isNested) {
1076
1192
  ${JSON.stringify(payload)}`
1077
1193
  );
1078
1194
  }
1079
- return payload.payload;
1195
+ const { type, data } = payload.payload;
1196
+ return { type, data };
1080
1197
  }
1081
1198
  return null;
1082
1199
  }
1083
1200
  }
1084
1201
  }
1085
1202
 
1086
- // src/convert-streams.ts
1087
- function toAISdkV5Stream(stream, options = {
1203
+ // src/to-ai-sdk-format.ts
1204
+ function toAISdkFormat(stream, options = {
1088
1205
  from: "agent",
1089
1206
  sendStart: true,
1090
1207
  sendFinish: true
1091
1208
  }) {
1092
1209
  const from = options?.from;
1093
1210
  if (from === "workflow") {
1094
- return stream.pipeThrough(WorkflowStreamToAISDKTransformer());
1211
+ const includeTextStreamParts = options?.includeTextStreamParts ?? false;
1212
+ return stream.pipeThrough(
1213
+ WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
1214
+ );
1095
1215
  }
1096
1216
  if (from === "network") {
1097
1217
  return stream.pipeThrough(AgentNetworkToAISDKTransformer());
@@ -1219,7 +1339,7 @@ function chatRoute({
1219
1339
  handler: async (c) => {
1220
1340
  const { messages, ...rest } = await c.req.json();
1221
1341
  const mastra = c.get("mastra");
1222
- const requestContext = c.get("requestContext");
1342
+ const runtimeContext = c.get("runtimeContext");
1223
1343
  let agentToUse = agent;
1224
1344
  if (!agent) {
1225
1345
  const agentId = c.req.param("agentId");
@@ -1230,20 +1350,20 @@ function chatRoute({
1230
1350
  `Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
1231
1351
  );
1232
1352
  }
1233
- if (requestContext && defaultOptions?.requestContext) {
1234
- mastra.getLogger()?.warn(`"requestContext" set in the route options will be overridden by the request's "requestContext".`);
1353
+ if (runtimeContext && defaultOptions?.runtimeContext) {
1354
+ mastra.getLogger()?.warn(`"runtimeContext" set in the route options will be overridden by the request's "runtimeContext".`);
1235
1355
  }
1236
1356
  if (!agentToUse) {
1237
1357
  throw new Error("Agent ID is required");
1238
1358
  }
1239
- const agentObj = mastra.getAgent(agentToUse);
1359
+ const agentObj = mastra.getAgentById(agentToUse);
1240
1360
  if (!agentObj) {
1241
1361
  throw new Error(`Agent ${agentToUse} not found`);
1242
1362
  }
1243
1363
  const result = await agentObj.stream(messages, {
1244
1364
  ...defaultOptions,
1245
1365
  ...rest,
1246
- requestContext: requestContext || defaultOptions?.requestContext
1366
+ runtimeContext: runtimeContext || defaultOptions?.runtimeContext
1247
1367
  });
1248
1368
  let lastMessageId;
1249
1369
  if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
@@ -1252,7 +1372,7 @@ function chatRoute({
1252
1372
  const uiMessageStream = createUIMessageStream({
1253
1373
  originalMessages: messages,
1254
1374
  execute: async ({ writer }) => {
1255
- for await (const part of toAISdkV5Stream(result, {
1375
+ for await (const part of toAISdkFormat(result, {
1256
1376
  from: "agent",
1257
1377
  lastMessageId,
1258
1378
  sendStart,
@@ -1272,7 +1392,8 @@ function chatRoute({
1272
1392
  }
1273
1393
  function workflowRoute({
1274
1394
  path = "/api/workflows/:workflowId/stream",
1275
- workflow
1395
+ workflow,
1396
+ includeTextStreamParts = false
1276
1397
  }) {
1277
1398
  if (!workflow && !path.includes("/:workflowId")) {
1278
1399
  throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
@@ -1303,7 +1424,7 @@ function workflowRoute({
1303
1424
  resourceId: { type: "string" },
1304
1425
  inputData: { type: "object", additionalProperties: true },
1305
1426
  resumeData: { type: "object", additionalProperties: true },
1306
- requestContext: { type: "object", additionalProperties: true },
1427
+ runtimeContext: { type: "object", additionalProperties: true },
1307
1428
  tracingOptions: { type: "object", additionalProperties: true },
1308
1429
  step: { type: "string" }
1309
1430
  }
@@ -1325,7 +1446,7 @@ function workflowRoute({
1325
1446
  handler: async (c) => {
1326
1447
  const { runId, resourceId, inputData, resumeData, ...rest } = await c.req.json();
1327
1448
  const mastra = c.get("mastra");
1328
- const requestContext = c.get("requestContext");
1449
+ const runtimeContext = c.get("runtimeContext");
1329
1450
  let workflowToUse = workflow;
1330
1451
  if (!workflow) {
1331
1452
  const workflowId = c.req.param("workflowId");
@@ -1339,20 +1460,20 @@ function workflowRoute({
1339
1460
  if (!workflowToUse) {
1340
1461
  throw new Error("Workflow ID is required");
1341
1462
  }
1342
- const workflowObj = mastra.getWorkflow(workflowToUse);
1463
+ const workflowObj = mastra.getWorkflowById(workflowToUse);
1343
1464
  if (!workflowObj) {
1344
1465
  throw new Error(`Workflow ${workflowToUse} not found`);
1345
1466
  }
1346
- if (requestContext && rest.requestContext) {
1467
+ if (runtimeContext && rest.runtimeContext) {
1347
1468
  mastra.getLogger()?.warn(
1348
- `"requestContext" from the request body will be ignored because "requestContext" is already set in the route options.`
1469
+ `"runtimeContext" from the request body will be ignored because "runtimeContext" is already set in the route options.`
1349
1470
  );
1350
1471
  }
1351
- const run = await workflowObj.createRun({ runId, resourceId, ...rest });
1352
- const stream = resumeData ? run.resumeStream({ resumeData, ...rest, requestContext: requestContext || rest.requestContext }) : run.stream({ inputData, ...rest, requestContext: requestContext || rest.requestContext });
1472
+ const run = await workflowObj.createRunAsync({ runId, resourceId, ...rest });
1473
+ const stream = resumeData ? run.resumeStream({ resumeData, ...rest, runtimeContext: runtimeContext || rest.runtimeContext }) : run.stream({ inputData, ...rest, runtimeContext: runtimeContext || rest.runtimeContext });
1353
1474
  const uiMessageStream = createUIMessageStream({
1354
1475
  execute: async ({ writer }) => {
1355
- for await (const part of toAISdkV5Stream(stream, { from: "workflow" })) {
1476
+ for await (const part of toAISdkFormat(stream, { from: "workflow", includeTextStreamParts })) {
1356
1477
  writer.write(part);
1357
1478
  }
1358
1479
  }
@@ -1392,12 +1513,13 @@ function networkRoute({
1392
1513
  type: "object",
1393
1514
  properties: {
1394
1515
  messages: { type: "array", items: { type: "object" } },
1395
- requestContext: { type: "object", additionalProperties: true },
1516
+ runtimeContext: { type: "object", additionalProperties: true },
1396
1517
  runId: { type: "string" },
1397
1518
  maxSteps: { type: "number" },
1398
1519
  threadId: { type: "string" },
1399
1520
  resourceId: { type: "string" },
1400
1521
  modelSettings: { type: "object", additionalProperties: true },
1522
+ telemetry: { type: "object", additionalProperties: true },
1401
1523
  tools: { type: "array", items: { type: "object" } }
1402
1524
  },
1403
1525
  required: ["messages"]
@@ -1436,7 +1558,7 @@ function networkRoute({
1436
1558
  if (!agentToUse) {
1437
1559
  throw new Error("Agent ID is required");
1438
1560
  }
1439
- const agentObj = mastra.getAgent(agentToUse);
1561
+ const agentObj = mastra.getAgentById(agentToUse);
1440
1562
  if (!agentObj) {
1441
1563
  throw new Error(`Agent ${agentToUse} not found`);
1442
1564
  }
@@ -1446,7 +1568,7 @@ function networkRoute({
1446
1568
  });
1447
1569
  const uiMessageStream = createUIMessageStream({
1448
1570
  execute: async ({ writer }) => {
1449
- for await (const part of toAISdkV5Stream(result, { from: "network" })) {
1571
+ for await (const part of toAISdkFormat(result, { from: "network" })) {
1450
1572
  writer.write(part);
1451
1573
  }
1452
1574
  }
@@ -1456,13 +1578,6 @@ function networkRoute({
1456
1578
  });
1457
1579
  }
1458
1580
 
1459
- // src/to-ai-sdk-format.ts
1460
- function toAISdkFormat() {
1461
- throw new Error(
1462
- 'toAISdkFormat() has been deprecated. Please use toAISdkStream() instead.\n\nMigration:\n import { toAISdkFormat } from "@mastra/ai-sdk";\n // Change to:\n import { toAISdkStream } from "@mastra/ai-sdk";\n\nThe function signature remains the same.'
1463
- );
1464
- }
1465
-
1466
- export { chatRoute, networkRoute, toAISdkFormat, toAISdkV5Stream as toAISdkStream, workflowRoute };
1581
+ export { chatRoute, networkRoute, toAISdkFormat, workflowRoute };
1467
1582
  //# sourceMappingURL=index.js.map
1468
1583
  //# sourceMappingURL=index.js.map