@mastra/ai-sdk 1.10.3 → 1.10.4-alpha.0

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.d.ts CHANGED
@@ -6,7 +6,7 @@ export type { WorkflowDataPart, WorkflowStepDataPart } from './transformers.js';
6
6
  export { networkRoute, handleNetworkStream } from './network-route.js';
7
7
  export type { NetworkRouteOptions, NetworkStreamHandlerParams, NetworkStreamHandlerOptions } from './network-route.js';
8
8
  export type { NetworkDataPart } from './transformers.js';
9
- export type { AgentDataPart, AgentStepDataPart } from './transformers.js';
9
+ export type { AgentDataPart, AgentStepDataPart, AgentDataPartWithAncestry, AgentStepDataPartWithAncestry, AgentStreamAncestryEntry, } from './transformers.js';
10
10
  export { toAISdkStream, toAISdkV5Stream } from './convert-streams.js';
11
11
  export { workflowSnapshotToStream } from './convert-snapshot.js';
12
12
  export { smoothStream } from './smooth-stream.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC3D,YAAY,EACV,gBAAgB,EAChB,uBAAuB,EACvB,wBAAwB,EACxB,wBAAwB,EACxB,mBAAmB,GACpB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AACvE,YAAY,EAAE,oBAAoB,EAAE,2BAA2B,EAAE,4BAA4B,EAAE,MAAM,kBAAkB,CAAC;AACxH,YAAY,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACpE,YAAY,EAAE,mBAAmB,EAAE,0BAA0B,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AACpH,YAAY,EAAE,eAAe,EAAE,MAA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EACV,aAAa,EACb,iBAAiB,EACjB,yBAAyB,EACzB,6BAA6B,EAC7B,wBAAwB,GACzB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,YAAY,EAAE,qBAAqB,EAAE,4BAA4B,EAAE,MAAM,iBAAiB,CAAC;AAC3F,OAAO,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAG3E,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,YAAY,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,+BAA+B,EAAE,MAAM,cAAc,CAAC;AAGhH,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC"}
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ import { MASTRA_RESOURCE_ID_KEY } from "@mastra/core/request-context";
9
9
  import { MessageList, TripWire, aiV5ModelMessageToV2PromptMessage } from "@mastra/core/agent";
10
10
  import { RequestContext } from "@mastra/core/di";
11
11
  import { MessageHistory, SemanticRecall, WorkingMemory } from "@mastra/core/processors";
12
- //#region ../../packages/_vendored/ai_v5/dist/dist-Dqv4QeJJ.js
12
+ //#region ../../packages/_vendored/ai_v5/dist/dist-CQthBS6n.js
13
13
  var marker$1$6 = "vercel.ai.error";
14
14
  var symbol$1$7 = Symbol.for(marker$1$6);
15
15
  var _a$1$7;
@@ -5027,7 +5027,7 @@ function createUIMessageStream$2({ execute, onError = () => "An error occurred."
5027
5027
  });
5028
5028
  }
5029
5029
  //#endregion
5030
- //#region ../../packages/_vendored/ai_v6/dist/dist-Cx98_f-i.js
5030
+ //#region ../../packages/_vendored/ai_v6/dist/dist-BJ8L5vTo.js
5031
5031
  var marker$1$4 = "vercel.ai.error";
5032
5032
  var symbol$1$4 = Symbol.for(marker$1$4);
5033
5033
  var _a$1$4;
@@ -8806,7 +8806,7 @@ var doWrap = ({ model, middleware: { transformParams, wrapGenerate, wrapStream,
8806
8806
  };
8807
8807
  };
8808
8808
  //#endregion
8809
- //#region ../../packages/_vendored/ai_v7/dist/dist-CiMv_aBf.js
8809
+ //#region ../../packages/_vendored/ai_v7/dist/dist-BWqbqY3Z.js
8810
8810
  var marker$1 = "vercel.ai.error";
8811
8811
  var symbol$1 = Symbol.for(marker$1);
8812
8812
  var _a$1;
@@ -12342,6 +12342,39 @@ function convertMastraChunkToAISDKv6({ chunk, mode = "stream" }) {
12342
12342
  includeRawFinishReason: true
12343
12343
  });
12344
12344
  }
12345
+ function createToolOutputAncestryEntry(toolCallId, toolName) {
12346
+ const agentId = toolName?.startsWith("agent-") ? toolName.slice(6) : void 0;
12347
+ return {
12348
+ toolCallId,
12349
+ ...toolName ? { toolName } : {},
12350
+ ...agentId ? { agentId } : {}
12351
+ };
12352
+ }
12353
+ /**
12354
+ * Each agent-as-tool delegation wraps progressive chunks in another `tool-output`
12355
+ * envelope. Preserve every boundary for routing while returning only the originating
12356
+ * leaf chunk to downstream transformers.
12357
+ */
12358
+ function normalizeNestedToolOutput(part) {
12359
+ const ancestry = [createToolOutputAncestryEntry(part.toolCallId, part.toolName)];
12360
+ const seen = /* @__PURE__ */ new Set();
12361
+ let current = part.output;
12362
+ while (current !== null && typeof current === "object") {
12363
+ if (seen.has(current)) return;
12364
+ seen.add(current);
12365
+ if (current.type !== "tool-output") return {
12366
+ output: current,
12367
+ ancestry
12368
+ };
12369
+ const toolCallId = current.payload?.toolCallId ?? current.toolCallId;
12370
+ if (typeof toolCallId !== "string") return;
12371
+ const toolName = current.payload?.toolName ?? current.toolName;
12372
+ ancestry.push(createToolOutputAncestryEntry(toolCallId, typeof toolName === "string" ? toolName : void 0));
12373
+ const nested = current.payload?.output ?? current.output;
12374
+ if (nested === void 0) return;
12375
+ current = nested;
12376
+ }
12377
+ }
12345
12378
  function convertFullStreamChunkToUIMessageStream({ part, messageMetadataValue, sendReasoning, sendSources, onError, sendStart, sendFinish, responseMessageId }) {
12346
12379
  const partType = part?.type;
12347
12380
  if (!sendReasoning && (partType === "text-start" || partType === "text-delta" || partType === "text-end") && part.providerMetadata?.openai?.itemId != null) {
@@ -12457,25 +12490,29 @@ function convertFullStreamChunkToUIMessageStream({ part, messageMetadataValue, s
12457
12490
  type: "tool-output-denied",
12458
12491
  toolCallId: part.toolCallId
12459
12492
  };
12460
- case "tool-output":
12461
- if (part.output.from === "AGENT") return {
12493
+ case "tool-output": {
12494
+ const normalized = normalizeNestedToolOutput(part);
12495
+ if (!normalized) return;
12496
+ const { output, ancestry } = normalized;
12497
+ if (output.from === "AGENT") return {
12462
12498
  type: "tool-agent",
12463
12499
  toolCallId: part.toolCallId,
12464
- payload: part.output
12500
+ payload: output,
12501
+ ancestry
12465
12502
  };
12466
- else if (part.output.from === "WORKFLOW") return {
12503
+ else if (output.from === "WORKFLOW") return {
12467
12504
  type: "tool-workflow",
12468
12505
  toolCallId: part.toolCallId,
12469
- payload: part.output
12506
+ payload: output
12470
12507
  };
12471
- else if (part.output.from === "NETWORK") return {
12508
+ else if (output.from === "NETWORK") return {
12472
12509
  type: "tool-network",
12473
12510
  toolCallId: part.toolCallId,
12474
- payload: part.output
12511
+ payload: output
12475
12512
  };
12476
- else if (isDataChunkType(part.output)) {
12477
- if (!("data" in part.output)) throw new Error(`UI Messages require a data property when using data- prefixed chunks \n ${JSON.stringify(part)}`);
12478
- const { type, data, id } = part.output;
12513
+ else if (isDataChunkType(output)) {
12514
+ if (!("data" in output)) throw new Error(`UI Messages require a data property when using data- prefixed chunks \n ${JSON.stringify(part)}`);
12515
+ const { type, data, id } = output;
12479
12516
  return {
12480
12517
  type,
12481
12518
  data,
@@ -12483,6 +12520,7 @@ function convertFullStreamChunkToUIMessageStream({ part, messageMetadataValue, s
12483
12520
  };
12484
12521
  }
12485
12522
  return;
12523
+ }
12486
12524
  case "tool-error": return {
12487
12525
  type: "tool-output-error",
12488
12526
  toolCallId: part.toolCallId,
@@ -12637,7 +12675,7 @@ function AgentNetworkToAISDKTransformer() {
12637
12675
  function AgentNetworkToAISDKV6Transformer() {
12638
12676
  return createAgentNetworkToAISDKTransformer();
12639
12677
  }
12640
- function createAgentStreamToAISDKTransformer(convertMastraChunkToAISDK, { lastMessageId, sendStart = true, sendFinish = true, sendReasoning, sendSources, messageMetadata, onError }) {
12678
+ function createAgentStreamToAISDKTransformer(convertMastraChunkToAISDK, { lastMessageId, sendStart = true, sendFinish = true, sendReasoning, sendSources, messageMetadata, onError, includeSubAgentMetadata = false }) {
12641
12679
  let bufferedSteps = /* @__PURE__ */ new Map();
12642
12680
  let tripwireOccurred = false;
12643
12681
  let finishEventSent = false;
@@ -12668,8 +12706,9 @@ function createAgentStreamToAISDKTransformer(convertMastraChunkToAISDK, { lastMe
12668
12706
  }
12669
12707
  });
12670
12708
  if (transformedChunk) if (transformedChunk.type === "tool-agent") {
12671
- const payload = transformedChunk.payload;
12672
- const agentTransformed = transformAgent(payload, bufferedSteps);
12709
+ if (!includeSubAgentMetadata) return;
12710
+ const { payload, ancestry } = transformedChunk;
12711
+ const agentTransformed = transformAgent(payload, bufferedSteps, ancestry);
12673
12712
  if (agentTransformed) if (Array.isArray(agentTransformed)) for (const part of agentTransformed) controller.enqueue(part);
12674
12713
  else controller.enqueue(agentTransformed);
12675
12714
  } else if (transformedChunk.type === "tool-workflow") {
@@ -12731,7 +12770,7 @@ function createAgentStreamToAISDKTransformer(convertMastraChunkToAISDK, { lastMe
12731
12770
  }
12732
12771
  });
12733
12772
  }
12734
- function AgentStreamToAISDKTransformer({ lastMessageId, sendStart = true, sendFinish = true, sendReasoning, sendSources, messageMetadata, onError }) {
12773
+ function AgentStreamToAISDKTransformer({ lastMessageId, sendStart = true, sendFinish = true, sendReasoning, sendSources, messageMetadata, onError, includeSubAgentMetadata = false }) {
12735
12774
  return createAgentStreamToAISDKTransformer(convertMastraChunkToAISDKv5, {
12736
12775
  lastMessageId,
12737
12776
  sendStart,
@@ -12739,10 +12778,11 @@ function AgentStreamToAISDKTransformer({ lastMessageId, sendStart = true, sendFi
12739
12778
  sendReasoning,
12740
12779
  sendSources,
12741
12780
  messageMetadata,
12742
- onError
12781
+ onError,
12782
+ includeSubAgentMetadata
12743
12783
  });
12744
12784
  }
12745
- function AgentStreamToAISDKV6Transformer({ lastMessageId, sendStart = true, sendFinish = true, sendReasoning, sendSources, messageMetadata, onError }) {
12785
+ function AgentStreamToAISDKV6Transformer({ lastMessageId, sendStart = true, sendFinish = true, sendReasoning, sendSources, messageMetadata, onError, includeSubAgentMetadata = false }) {
12746
12786
  return createAgentStreamToAISDKTransformer(convertMastraChunkToAISDKv6, {
12747
12787
  lastMessageId,
12748
12788
  sendStart,
@@ -12750,7 +12790,8 @@ function AgentStreamToAISDKV6Transformer({ lastMessageId, sendStart = true, send
12750
12790
  sendReasoning,
12751
12791
  sendSources,
12752
12792
  messageMetadata,
12753
- onError
12793
+ onError,
12794
+ includeSubAgentMetadata
12754
12795
  });
12755
12796
  }
12756
12797
  function ensureAgentRunState(bufferedSteps, runId) {
@@ -12862,19 +12903,28 @@ function serializeAgentRun(current, { includeCompletedStepDetails, includeRespon
12862
12903
  steps: data.steps.map((step) => cloneAgentStep(step, { includeDetails: includeCompletedStepDetails }))
12863
12904
  };
12864
12905
  }
12906
+ function createAgentStreamAncestryMetadata(ancestry) {
12907
+ const parentAgentId = ancestry.at(-2)?.agentId;
12908
+ return {
12909
+ ancestry,
12910
+ depth: ancestry.length,
12911
+ ...parentAgentId ? { parentAgentId } : {}
12912
+ };
12913
+ }
12865
12914
  function createAgentDataPart(args) {
12866
- const { current, runId, includeCompletedStepDetails, includeResponseMessages } = args;
12915
+ const { current, runId, ancestry, includeCompletedStepDetails, includeResponseMessages } = args;
12867
12916
  return {
12868
12917
  type: "data-tool-agent",
12869
12918
  id: runId,
12870
12919
  data: serializeAgentRun(current, {
12871
12920
  includeCompletedStepDetails,
12872
12921
  includeResponseMessages
12873
- })
12922
+ }),
12923
+ ...createAgentStreamAncestryMetadata(ancestry)
12874
12924
  };
12875
12925
  }
12876
12926
  function createAgentStepDataPart(args) {
12877
- const { runId, stepIndex, step } = args;
12927
+ const { runId, stepIndex, step, ancestry } = args;
12878
12928
  return {
12879
12929
  type: "data-tool-agent-step",
12880
12930
  id: `${runId}:${stepIndex}`,
@@ -12882,10 +12932,11 @@ function createAgentStepDataPart(args) {
12882
12932
  runId,
12883
12933
  stepIndex,
12884
12934
  step: cloneAgentStep(step, { includeDetails: true })
12885
- }
12935
+ },
12936
+ ...createAgentStreamAncestryMetadata(ancestry)
12886
12937
  };
12887
12938
  }
12888
- function transformAgent(payload, bufferedSteps) {
12939
+ function transformAgent(payload, bufferedSteps, ancestry = []) {
12889
12940
  let hasChanged = false;
12890
12941
  let completedStep = null;
12891
12942
  switch (payload.type) {
@@ -13098,13 +13149,15 @@ function transformAgent(payload, bufferedSteps) {
13098
13149
  const snapshot = createAgentDataPart({
13099
13150
  current: bufferedSteps.get(payload.runId),
13100
13151
  runId: payload.runId,
13152
+ ancestry,
13101
13153
  includeCompletedStepDetails: payload.type === "finish",
13102
13154
  includeResponseMessages: payload.type === "finish"
13103
13155
  });
13104
13156
  if (completedStep) return [snapshot, createAgentStepDataPart({
13105
13157
  runId: payload.runId,
13106
13158
  stepIndex: completedStep.stepIndex,
13107
- step: completedStep.step
13159
+ step: completedStep.step,
13160
+ ancestry
13108
13161
  })];
13109
13162
  return snapshot;
13110
13163
  }
@@ -13623,7 +13676,8 @@ function toAISdkV5Stream(stream, options = {
13623
13676
  sendReasoning: options.sendReasoning,
13624
13677
  sendSources: options.sendSources,
13625
13678
  messageMetadata: options.messageMetadata,
13626
- onError: options.onError
13679
+ onError: options.onError,
13680
+ includeSubAgentMetadata: options.includeSubAgentMetadata
13627
13681
  }));
13628
13682
  }
13629
13683
  function toAISdkStream(stream, options = {
@@ -13649,7 +13703,8 @@ function toAISdkStream(stream, options = {
13649
13703
  sendReasoning: options.sendReasoning,
13650
13704
  sendSources: options.sendSources,
13651
13705
  messageMetadata: options.messageMetadata,
13652
- onError: options.onError
13706
+ onError: options.onError,
13707
+ includeSubAgentMetadata: options.includeSubAgentMetadata
13653
13708
  }));
13654
13709
  }
13655
13710
  const from = options.from;
@@ -13669,7 +13724,8 @@ function toAISdkStream(stream, options = {
13669
13724
  sendReasoning: options.sendReasoning,
13670
13725
  sendSources: options.sendSources,
13671
13726
  messageMetadata: options.messageMetadata,
13672
- onError: options.onError
13727
+ onError: options.onError,
13728
+ includeSubAgentMetadata: options.includeSubAgentMetadata
13673
13729
  }));
13674
13730
  }
13675
13731
  //#endregion