@mastra/ai-sdk 1.7.0 → 1.7.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
@@ -8858,7 +8858,7 @@ function convertMastraChunkToAISDKBase({ chunk, mode = "stream", normalizeWarnin
8858
8858
  type: "finish",
8859
8859
  finishReason: normalizeFinishReason(chunk.payload.stepResult.reason),
8860
8860
  ...includeRawFinishReason ? { rawFinishReason: chunk.payload.stepResult.reason } : {},
8861
- totalUsage: normalizeUsage(chunk.payload.output.usage)
8861
+ totalUsage: normalizeUsage(chunk.payload.output?.usage ?? chunk.payload.usage)
8862
8862
  };
8863
8863
  case "reasoning-start": return {
8864
8864
  type: "reasoning-start",
@@ -9414,52 +9414,88 @@ function createAgentStreamToAISDKTransformer(convertMastraChunkToAISDK, { lastMe
9414
9414
  let bufferedSteps = /* @__PURE__ */ new Map();
9415
9415
  let tripwireOccurred = false;
9416
9416
  let finishEventSent = false;
9417
+ let heldChunks = null;
9418
+ let startIdResolved = false;
9419
+ const processChunk = (chunk, controller) => {
9420
+ if (chunk.type === "tripwire") tripwireOccurred = true;
9421
+ if (chunk.type === "finish") finishEventSent = true;
9422
+ if (chunk.type === "object-result") controller.enqueue({
9423
+ type: "data-structured-output",
9424
+ data: { object: chunk.object }
9425
+ });
9426
+ const part = convertMastraChunkToAISDK({
9427
+ chunk,
9428
+ mode: "stream"
9429
+ });
9430
+ const enqueueTransformedPart = (p) => {
9431
+ const transformedChunk = convertFullStreamChunkToUIMessageStream({
9432
+ part: p,
9433
+ sendReasoning,
9434
+ sendSources,
9435
+ messageMetadataValue: p ? messageMetadata?.({ part: p }) : void 0,
9436
+ sendStart,
9437
+ sendFinish,
9438
+ responseMessageId: lastMessageId,
9439
+ onError(error) {
9440
+ return onError ? onError(error) : safeParseErrorObject(error);
9441
+ }
9442
+ });
9443
+ if (transformedChunk) if (transformedChunk.type === "tool-agent") {
9444
+ const payload = transformedChunk.payload;
9445
+ const agentTransformed = transformAgent(payload, bufferedSteps);
9446
+ if (agentTransformed) controller.enqueue(agentTransformed);
9447
+ } else if (transformedChunk.type === "tool-workflow") {
9448
+ const payload = transformedChunk.payload;
9449
+ const workflowChunk = transformWorkflow(payload, bufferedSteps, true, void 0, void 0, convertMastraChunkToAISDK);
9450
+ if (workflowChunk) if (Array.isArray(workflowChunk)) for (const item of workflowChunk) controller.enqueue(item);
9451
+ else controller.enqueue(workflowChunk);
9452
+ } else if (transformedChunk.type === "tool-network") {
9453
+ const payload = transformedChunk.payload;
9454
+ const networkChunk = transformNetwork(payload, bufferedSteps, true);
9455
+ if (Array.isArray(networkChunk)) {
9456
+ for (const c of networkChunk) if (c) controller.enqueue(c);
9457
+ } else if (networkChunk) controller.enqueue(networkChunk);
9458
+ } else controller.enqueue(transformedChunk);
9459
+ };
9460
+ if (Array.isArray(part)) for (const p of part) enqueueTransformedPart(p);
9461
+ else enqueueTransformedPart(part);
9462
+ };
9417
9463
  return new TransformStream$1({
9418
9464
  transform(chunk, controller) {
9419
- if (chunk.type === "tripwire") tripwireOccurred = true;
9420
- if (chunk.type === "finish") finishEventSent = true;
9421
- if (chunk.type === "object-result") controller.enqueue({
9422
- type: "data-structured-output",
9423
- data: { object: chunk.object }
9424
- });
9425
- const part = convertMastraChunkToAISDK({
9426
- chunk,
9427
- mode: "stream"
9428
- });
9429
- const enqueueTransformedPart = (p) => {
9430
- const transformedChunk = convertFullStreamChunkToUIMessageStream({
9431
- part: p,
9432
- sendReasoning,
9433
- sendSources,
9434
- messageMetadataValue: p ? messageMetadata?.({ part: p }) : void 0,
9435
- sendStart,
9436
- sendFinish,
9437
- responseMessageId: lastMessageId,
9438
- onError(error) {
9439
- return onError ? onError(error) : safeParseErrorObject(error);
9465
+ if (!startIdResolved) {
9466
+ if (sendStart && chunk.type === "start") {
9467
+ heldChunks = [chunk];
9468
+ return;
9469
+ }
9470
+ if (heldChunks) {
9471
+ if (typeof chunk.type === "string" && chunk.type.startsWith("data-")) {
9472
+ heldChunks.push(chunk);
9473
+ return;
9440
9474
  }
9441
- });
9442
- if (transformedChunk) if (transformedChunk.type === "tool-agent") {
9443
- const payload = transformedChunk.payload;
9444
- const agentTransformed = transformAgent(payload, bufferedSteps);
9445
- if (agentTransformed) controller.enqueue(agentTransformed);
9446
- } else if (transformedChunk.type === "tool-workflow") {
9447
- const payload = transformedChunk.payload;
9448
- const workflowChunk = transformWorkflow(payload, bufferedSteps, true, void 0, void 0, convertMastraChunkToAISDK);
9449
- if (workflowChunk) if (Array.isArray(workflowChunk)) for (const item of workflowChunk) controller.enqueue(item);
9450
- else controller.enqueue(workflowChunk);
9451
- } else if (transformedChunk.type === "tool-network") {
9452
- const payload = transformedChunk.payload;
9453
- const networkChunk = transformNetwork(payload, bufferedSteps, true);
9454
- if (Array.isArray(networkChunk)) {
9455
- for (const c of networkChunk) if (c) controller.enqueue(c);
9456
- } else if (networkChunk) controller.enqueue(networkChunk);
9457
- } else controller.enqueue(transformedChunk);
9458
- };
9459
- if (Array.isArray(part)) for (const p of part) enqueueTransformedPart(p);
9460
- else enqueueTransformedPart(part);
9475
+ startIdResolved = true;
9476
+ const held = heldChunks;
9477
+ heldChunks = null;
9478
+ if (chunk.type === "step-start") {
9479
+ const startPayload = held[0]?.payload;
9480
+ const stepMessageId = chunk.payload?.messageId;
9481
+ if (held[0] && typeof stepMessageId === "string" && typeof startPayload?.messageId === "string" && stepMessageId !== startPayload.messageId) held[0] = {
9482
+ ...held[0],
9483
+ payload: {
9484
+ ...startPayload,
9485
+ messageId: stepMessageId
9486
+ }
9487
+ };
9488
+ }
9489
+ for (const heldChunk of held) processChunk(heldChunk, controller);
9490
+ } else startIdResolved = true;
9491
+ }
9492
+ processChunk(chunk, controller);
9461
9493
  },
9462
9494
  flush(controller) {
9495
+ if (heldChunks) {
9496
+ for (const heldChunk of heldChunks) processChunk(heldChunk, controller);
9497
+ heldChunks = null;
9498
+ }
9463
9499
  if (tripwireOccurred && !finishEventSent && sendFinish) controller.enqueue({
9464
9500
  type: "finish",
9465
9501
  finishReason: "other"