@mastra/ai-sdk 1.7.1-alpha.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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @mastra/ai-sdk
2
2
 
3
+ ## 1.7.1-alpha.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed the AI SDK stream announcing a response message id that matches no stored message when a processor rotates the response message id before the model runs (for example observational memory sealing a buffer chunk). The stream's `start` chunk now announces the id the assistant response is actually persisted under, so `useChat` and other AI SDK consumers can reconcile streamed messages with memory. Fixes [#19810](https://github.com/mastra-ai/mastra/issues/19810) ([#20084](https://github.com/mastra-ai/mastra/pull/20084))
8
+
9
+ - Updated dependencies [[`4844167`](https://github.com/mastra-ai/mastra/commit/4844167cff2d5ec5004e94edd34970833040fa3f), [`5faf93f`](https://github.com/mastra-ai/mastra/commit/5faf93f03e19daea394b9e2a923f2e4f833407f2), [`80ad891`](https://github.com/mastra-ai/mastra/commit/80ad891f8cd10379aa5b5af7510c763783b2ab56), [`a1cb98d`](https://github.com/mastra-ai/mastra/commit/a1cb98d11990b560b98482292a1f34aa1a2d9092), [`598ad82`](https://github.com/mastra-ai/mastra/commit/598ad82d41c41389a686338a1d0e50b7400e1938), [`1fd6aad`](https://github.com/mastra-ai/mastra/commit/1fd6aad1ea4a9d32f65efa832307c35e981a4c0a)]:
10
+ - @mastra/core@1.56.0-alpha.4
11
+
3
12
  ## 1.7.1-alpha.0
4
13
 
5
14
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -9437,52 +9437,88 @@ function createAgentStreamToAISDKTransformer(convertMastraChunkToAISDK, { lastMe
9437
9437
  let bufferedSteps = /* @__PURE__ */ new Map();
9438
9438
  let tripwireOccurred = false;
9439
9439
  let finishEventSent = false;
9440
+ let heldChunks = null;
9441
+ let startIdResolved = false;
9442
+ const processChunk = (chunk, controller) => {
9443
+ if (chunk.type === "tripwire") tripwireOccurred = true;
9444
+ if (chunk.type === "finish") finishEventSent = true;
9445
+ if (chunk.type === "object-result") controller.enqueue({
9446
+ type: "data-structured-output",
9447
+ data: { object: chunk.object }
9448
+ });
9449
+ const part = convertMastraChunkToAISDK({
9450
+ chunk,
9451
+ mode: "stream"
9452
+ });
9453
+ const enqueueTransformedPart = (p) => {
9454
+ const transformedChunk = convertFullStreamChunkToUIMessageStream({
9455
+ part: p,
9456
+ sendReasoning,
9457
+ sendSources,
9458
+ messageMetadataValue: p ? messageMetadata?.({ part: p }) : void 0,
9459
+ sendStart,
9460
+ sendFinish,
9461
+ responseMessageId: lastMessageId,
9462
+ onError(error) {
9463
+ return onError ? onError(error) : safeParseErrorObject(error);
9464
+ }
9465
+ });
9466
+ if (transformedChunk) if (transformedChunk.type === "tool-agent") {
9467
+ const payload = transformedChunk.payload;
9468
+ const agentTransformed = transformAgent(payload, bufferedSteps);
9469
+ if (agentTransformed) controller.enqueue(agentTransformed);
9470
+ } else if (transformedChunk.type === "tool-workflow") {
9471
+ const payload = transformedChunk.payload;
9472
+ const workflowChunk = transformWorkflow(payload, bufferedSteps, true, void 0, void 0, convertMastraChunkToAISDK);
9473
+ if (workflowChunk) if (Array.isArray(workflowChunk)) for (const item of workflowChunk) controller.enqueue(item);
9474
+ else controller.enqueue(workflowChunk);
9475
+ } else if (transformedChunk.type === "tool-network") {
9476
+ const payload = transformedChunk.payload;
9477
+ const networkChunk = transformNetwork(payload, bufferedSteps, true);
9478
+ if (Array.isArray(networkChunk)) {
9479
+ for (const c of networkChunk) if (c) controller.enqueue(c);
9480
+ } else if (networkChunk) controller.enqueue(networkChunk);
9481
+ } else controller.enqueue(transformedChunk);
9482
+ };
9483
+ if (Array.isArray(part)) for (const p of part) enqueueTransformedPart(p);
9484
+ else enqueueTransformedPart(part);
9485
+ };
9440
9486
  return new stream_web.TransformStream({
9441
9487
  transform(chunk, controller) {
9442
- if (chunk.type === "tripwire") tripwireOccurred = true;
9443
- if (chunk.type === "finish") finishEventSent = true;
9444
- if (chunk.type === "object-result") controller.enqueue({
9445
- type: "data-structured-output",
9446
- data: { object: chunk.object }
9447
- });
9448
- const part = convertMastraChunkToAISDK({
9449
- chunk,
9450
- mode: "stream"
9451
- });
9452
- const enqueueTransformedPart = (p) => {
9453
- const transformedChunk = convertFullStreamChunkToUIMessageStream({
9454
- part: p,
9455
- sendReasoning,
9456
- sendSources,
9457
- messageMetadataValue: p ? messageMetadata?.({ part: p }) : void 0,
9458
- sendStart,
9459
- sendFinish,
9460
- responseMessageId: lastMessageId,
9461
- onError(error) {
9462
- return onError ? onError(error) : safeParseErrorObject(error);
9488
+ if (!startIdResolved) {
9489
+ if (sendStart && chunk.type === "start") {
9490
+ heldChunks = [chunk];
9491
+ return;
9492
+ }
9493
+ if (heldChunks) {
9494
+ if (typeof chunk.type === "string" && chunk.type.startsWith("data-")) {
9495
+ heldChunks.push(chunk);
9496
+ return;
9463
9497
  }
9464
- });
9465
- if (transformedChunk) if (transformedChunk.type === "tool-agent") {
9466
- const payload = transformedChunk.payload;
9467
- const agentTransformed = transformAgent(payload, bufferedSteps);
9468
- if (agentTransformed) controller.enqueue(agentTransformed);
9469
- } else if (transformedChunk.type === "tool-workflow") {
9470
- const payload = transformedChunk.payload;
9471
- const workflowChunk = transformWorkflow(payload, bufferedSteps, true, void 0, void 0, convertMastraChunkToAISDK);
9472
- if (workflowChunk) if (Array.isArray(workflowChunk)) for (const item of workflowChunk) controller.enqueue(item);
9473
- else controller.enqueue(workflowChunk);
9474
- } else if (transformedChunk.type === "tool-network") {
9475
- const payload = transformedChunk.payload;
9476
- const networkChunk = transformNetwork(payload, bufferedSteps, true);
9477
- if (Array.isArray(networkChunk)) {
9478
- for (const c of networkChunk) if (c) controller.enqueue(c);
9479
- } else if (networkChunk) controller.enqueue(networkChunk);
9480
- } else controller.enqueue(transformedChunk);
9481
- };
9482
- if (Array.isArray(part)) for (const p of part) enqueueTransformedPart(p);
9483
- else enqueueTransformedPart(part);
9498
+ startIdResolved = true;
9499
+ const held = heldChunks;
9500
+ heldChunks = null;
9501
+ if (chunk.type === "step-start") {
9502
+ const startPayload = held[0]?.payload;
9503
+ const stepMessageId = chunk.payload?.messageId;
9504
+ if (held[0] && typeof stepMessageId === "string" && typeof startPayload?.messageId === "string" && stepMessageId !== startPayload.messageId) held[0] = {
9505
+ ...held[0],
9506
+ payload: {
9507
+ ...startPayload,
9508
+ messageId: stepMessageId
9509
+ }
9510
+ };
9511
+ }
9512
+ for (const heldChunk of held) processChunk(heldChunk, controller);
9513
+ } else startIdResolved = true;
9514
+ }
9515
+ processChunk(chunk, controller);
9484
9516
  },
9485
9517
  flush(controller) {
9518
+ if (heldChunks) {
9519
+ for (const heldChunk of heldChunks) processChunk(heldChunk, controller);
9520
+ heldChunks = null;
9521
+ }
9486
9522
  if (tripwireOccurred && !finishEventSent && sendFinish) controller.enqueue({
9487
9523
  type: "finish",
9488
9524
  finishReason: "other"