issue while integrating Mastra v1 with AI SDK UI with both server and client side tool calls
Our Mastra agent uses both server-side tool calls and client-side tool calls.
For client-side tool calls, we are using the onToolCall hook with sendAutomaticallyWhen.
However, we consistently run into a problem where tool calls are duplicated in the response. The flow looks like this:
The user sends a message.
The server starts executing both server-side and client-side tool calls at the same time.
The server-side tool call is executed and saved to the database.
Meanwhile, the client finishes the client-side tool call and sends a follow-up request to the server with the tool call result.
At this point, the message ID sent by the client does not match the message ID already stored in the database, which results in an OpenAI error.
We then tried enabling sendStart. With this approach, the issue changes slightly: when the client finishes the tool call and sends the follow-up request, the server returns a different message ID. As a result, the frontend ends up with two separate messages:
one message containing only the server-side tool call, and
another message containing both the server-side and client-side tool calls.
Is there a recommended or correct way to handle mixed server-side and client-side tool calls in this setup without running into duplicated tool calls or mismatched message IDs?
For client-side tool calls, we are using the onToolCall hook with sendAutomaticallyWhen.
However, we consistently run into a problem where tool calls are duplicated in the response. The flow looks like this:
The user sends a message.
The server starts executing both server-side and client-side tool calls at the same time.
The server-side tool call is executed and saved to the database.
Meanwhile, the client finishes the client-side tool call and sends a follow-up request to the server with the tool call result.
At this point, the message ID sent by the client does not match the message ID already stored in the database, which results in an OpenAI error.
We then tried enabling sendStart. With this approach, the issue changes slightly: when the client finishes the tool call and sends the follow-up request, the server returns a different message ID. As a result, the frontend ends up with two separate messages:
one message containing only the server-side tool call, and
another message containing both the server-side and client-side tool calls.
Is there a recommended or correct way to handle mixed server-side and client-side tool calls in this setup without running into duplicated tool calls or mismatched message IDs?