Skip to content

Mastra Adapter seems to be handling text-end incorrectly #836

@mathewsmathai1

Description

@mathewsmathai1

The below is the message handling code snippet from mastra.ts.

if (response && typeof response === "object") {
  for await (const chunk of response.fullStream) {
    switch (chunk.type) {
      // ...

      case "finish": {
        onFinishMessagePart?.();
        break;
      }
    }
  }

  await onRunFinished?.();
} else {
  throw new Error("Invalid response from local agent");
}

text-end event is not handled at all and message id is reset only when finish event is triggered. finish event marks the end of a run and not the end of text content.

As a result, multiple text messages from the agent are being merged into one since they are all using the same uuid.

Look at message id 0fd3e855-098b-430b-84f4-f773c58d2142 in the below result.

[
  {
    "id": "952f79dc-2199-4bec-acee-e9032fa058b5",
    "role": "system",
    "name": "Atom",
    "content": "hi"
  },
  {
    "id": "125",
    "role": "user",
    "name": "System",
    "content": "Please send a test email to [email protected]"
  },
  {
    "id": "0fd3e855-098b-430b-84f4-f773c58d2142",
    "role": "assistant",
    "content": "I'll send a test email to [email protected] for you. Done! ✅ Your test email has been successfully sent to [email protected] with the subject \"Test Email\".",
    "toolCalls": [
      {
        "type": "object"
      }
    ]
  },
  {
    "id": "cef32ecd-647f-4f61-a245-fdbc311aacc0",
    "role": "tool",
    "toolCallId": "toolu_012eLZjSzh2STF4RPRxVZTp2",
    "content": {
      "data": {
        "id": "19b3170de5e9913e",
        "labelIds": ["SENT"],
        "threadId": "19b3170de5e9913e"
      },
      "error": null,
      "successful": true,
      "logId": "log_q5c41uZb7whz"
    }
  },
  {
    "id": "0fd3e855-098b-430b-84f4-f773c58d2142",
    "role": "assistant",
    "content": ""
  }
]

AI-SDK reference

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions