Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(agent): Avoid unrecognized stream chunk warnings (#14296)

Ben Bachem3a6aad2d7265
diff --git a/web/src/__tests__/server/in-app-agent-stream.servertest.ts b/web/src/__tests__/server/in-app-agent-stream.servertest.ts
index 6f3f11c60e..3ea1fa1c83 100644
--- a/web/src/__tests__/server/in-app-agent-stream.servertest.ts
+++ b/web/src/__tests__/server/in-app-agent-stream.servertest.ts
@@ -6,6 +6,8 @@ import { describe, expect, it, vi } from "vitest";
import type { AgUiEvent } from "@/src/ee/features/in-app-agent/schema";
import { decodeFiltersGeneric } from "@/src/features/filters/lib/filter-query-encoding";
import { IN_APP_AGENT_REDIRECT_TOOL_NAME } from "@/src/ee/features/in-app-agent/constants";
+import { patchMastraToolCallInputStreaming } from "@/src/ee/features/in-app-agent/server/agent";
+import type { MastraAgent } from "@ag-ui/mastra";
const adapterEvents = vi.hoisted(() => ({
items: [] as AgUiEvent[],
@@ -38,7 +40,6 @@ vi.mock("@ag-ui/mastra", () => ({
complete: () => void;
}) => {
adapterEvents.inputs.push(input);
-
for (const event of adapterEvents.items) {
subscriber.next(event);
}
@@ -119,6 +120,211 @@ vi.mock("@/src/ee/features/in-app-agent/server/instrumentation", () => ({
instrumentationMocks.createInAppAgentInstrumentation,
}));
+const createPatchedChunkProcessor = () => {
+ const forwardedChunks: unknown[] = [];
+ const onError = vi.fn();
+ const flush = vi.fn();
+ const adapter = {
+ createChunkProcessor: vi.fn(() => ({
+ handleChunk: (chunk: unknown) => {
+ forwardedChunks.push(chunk);
+ return false;
+ },
+ flush,
+ })),
+ };
+
+ patchMastraToolCallInputStreaming(adapter as unknown as MastraAgent);
+
+ const processor = adapter.createChunkProcessor({ onError });
+
+ return { forwardedChunks, onError, processor, flush };
+};
+
+describe("patchMastraToolCallInputStreaming", () => {
+ it("converts streamed tool-call input chunks to one native tool-call", () => {
+ const { forwardedChunks, onError, processor } =
+ createPatchedChunkProcessor();
+
+ processor.handleChunk({
+ type: "tool-call-input-streaming-start",
+ payload: {
+ toolCallId: "tool-call-1",
+ toolName: "langfuseDocs_search",
+ },
+ });
+ processor.handleChunk({
+ type: "tool-call-delta",
+ payload: {
+ toolCallId: "tool-call-1",
+ argsTextDelta: '{"query":"invite',
+ },
+ });
+ processor.handleChunk({
+ type: "tool-call-delta",
+ payload: {
+ toolCallId: "tool-call-1",
+ argsTextDelta: ' users"}',
+ },
+ });
+ processor.handleChunk({
+ type: "tool-call-input-streaming-end",
+ payload: { toolCallId: "tool-call-1" },
+ });
+
+ expect(onError).not.toHaveBeenCalled();
+ expect(forwardedChunks).toEqual([
+ {
+ type: "tool-call",
+ payload: {
+ toolCallId: "tool-call-1",
+ toolName: "langfuseDocs_search",
+ args: { query: "invite users" },
+ },
+ },
+ ]);
+ });
+
+ it("uses empty args when streamed tool-call input is malformed JSON", () => {
+ const { forwardedChunks, onError, processor } =
+ createPatchedChunkProcessor();
+
+ processor.handleChunk({
+ type: "tool-call-input-streaming-start",
+ payload: {
+ toolCallId: "tool-call-1",
+ toolName: "langfuseDocs_search",
+ },
+ });
+ processor.handleChunk({
+ type: "tool-call-delta",
+ payload: {
+ toolCallId: "tool-call-1",
+ argsTextDelta: '{"query":"invite users"',
+ },
+ });
+ processor.handleChunk({
+ type: "tool-call-input-streaming-end",
+ payload: { toolCallId: "tool-call-1" },
+ });
+
+ expect(onError).not.toHaveBeenCalled();
+ expect(forwardedChunks).toEqual([
+ {
+ type: "tool-call",
+ payload: {
+ toolCallId: "tool-call-1",
+ toolName: "langfuseDocs_search",
+ args: {},
+ },
+ },
+ ]);
+ });
+
+ it("suppresses one duplicate native tool-call after synthesizing it", () => {
+ const { forwardedChunks, processor } = createPatchedChunkProcessor();
+
+ processor.handleChunk({
+ type: "tool-call-input-streaming-start",
+ payload: {
+ toolCallId: "tool-call-1",
+ toolName: "langfuseDocs_search",
+ },
+ });
+ processor.handleChunk({
+ type: "tool-call-delta",
+ payload: {
+ toolCallId: "tool-call-1",
+ argsTextDelta: '{"query":"invite users"}',
+ },
+ });
+ processor.handleChunk({
+ type: "tool-call-input-streaming-end",
+ payload: { toolCallId: "tool-call-1" },
+ });
+ processor.handleChunk({
+ type: "tool-call",
+ payload: {
+ toolCallId: "tool-call-1",
+ toolName: "langfuseDocs_search",
+ args: { query: "invite users" },
+ },
+ });
+ processor.handleChunk({
+ type: "tool-call",
+ payload: {
+ toolCallId: "tool-call-2",
+ toolName: "langfuse_search",
+ args: { traceId: "trace-1" },
+ },
+ });
+
+ expect(forwardedChunks).toEqual([
+ {
+ type: "tool-call",
+ payload: {
+ toolCallId: "tool-call-1",
+ toolName: "langfuseDocs_search",
+ args: { query: "invite users" },
+ },
+ },
+ {
+ type: "tool-call",
+ payload: {
+ toolCallId: "tool-call-2",
+ toolName: "langfuse_search",
+ args: { traceId: "trace-1" },
+ },
+ },
+ ]);
+ });
+
+ it("passes through native tool-calls that were not synthesized", () => {
+ const { forwardedChunks, processor } = createPatchedChunkProcessor();
+
+ processor.handleChunk({
+ type: "tool-call",
+ payload: {
+ toolCallId: "tool-call-1",
+ toolName: "langfuse_search",
+ args: { query: "errors" },
+ },
+ });
+
+ expect(forwardedChunks).toEqual([
+ {
+ type: "tool-call",
+ payload: {
+ toolCallId: "tool-call-1",
+ toolName: "langfuse_search",
+ args: { query: "errors" },
+ },
+ },
+ ]);
+ });
+
+ it("reports malformed tool-call deltas with no known tool name", () => {
+ const { forwardedChunks, onError, processor } =
+ createPatchedChunkProcessor();
+
+ const shouldStop = processor.handleChunk({
+ type: "tool-call-delta",
+ payload: {
+ toolCallId: "tool-call-1",
+ argsTextDelta: '{"query":"invite users"}',
+ },
+ });
+
+ expect(shouldStop).toBe(true);
+ expect(onError).toHaveBeenCalledWith(
+ new Error(
+ "Malformed tool-call-delta: missing toolName for unknown toolCallId in payload",
+ ),
+ );
+ expect(forwardedChunks).toEqual([]);
+ });
+});
+
describe("createAgUiStream", () => {
beforeEach(() => {
vi.clearAllMocks();
diff --git a/web/src/ee/features/in-app-agent/server/agent.ts b/web/src/ee/features/in-app-agent/server/agent.ts
index 1fdbf17c9b..dcd2207e8b 100644
--- a/web/src/ee/features/in-app-agent/server/agent.ts
+++ b/web/src/ee/features/in-app-agent/server/agent.ts
@@ -579,11 +579,16 @@ async function createMastraAdapter(params: {
},
});
+ const adapter = new MastraAgent({
+ agent,
+ resourceId: params.input.threadId,
+ });
+ // @ag-ui/[email protected] does not understand Mastra's newer streaming
+ // tool-call chunks yet, so translate them locally to avoid warning noise.
+ patchMastraToolCallInputStreaming(adapter);
+
return {
- adapter: new MastraAgent({
- agent,
- resourceId: params.input.threadId,
- }),
+ adapter,
interrupt: () => agent.abortRunStream(params.input.runId),
cleanup: () => mcpClient.disconnect(),
};
@@ -611,6 +616,161 @@ function prefixToolsetTools<TTool>(
);
}
+type MastraChunkProcessor = {
+ handleChunk: (chunk: unknown) => boolean;
+ flush: () => void;
+};
+
+type MastraStreamCallbacks = {
+ onError: (error: Error) => void;
+};
+
+type PatchableMastraAgent = {
+ createChunkProcessor?: (
+ callbacks: MastraStreamCallbacks,
+ ) => MastraChunkProcessor;
+};
+
+type MastraStreamChunk = {
+ type?: string;
+ payload?: {
+ toolCallId?: string;
+ toolName?: string;
+ argsTextDelta?: string;
+ args?: unknown;
+ };
+};
+
+type StreamingToolCall = {
+ toolCallId: string;
+ toolName: string;
+ argsText: string;
+};
+
+export function patchMastraToolCallInputStreaming(adapter: MastraAgent) {
+ const patchableAdapter = adapter as unknown as PatchableMastraAgent;
+ const createChunkProcessor = patchableAdapter.createChunkProcessor;
+
+ if (typeof createChunkProcessor !== "function") {
+ return;
+ }
+
+ patchableAdapter.createChunkProcessor = function patchedCreateChunkProcessor(
+ this: PatchableMastraAgent,
+ callbacks: MastraStreamCallbacks,
+ ) {
+ const processor = createChunkProcessor.call(this, callbacks);
+ const streamingToolCalls = new Map<string, StreamingToolCall>();
+ const synthesizedToolCallIds = new Set<string>();
+
+ const parseStreamingToolCallArgs = (argsText: string): unknown => {
+ try {
+ return JSON.parse(argsText || "{}");
+ } catch {
+ return {};
+ }
+ };
+
+ return {
+ handleChunk(chunk: unknown) {
+ const mastraChunk = chunk as MastraStreamChunk;
+
+ switch (mastraChunk.type) {
+ case "tool-call-input-streaming-start": {
+ const { toolCallId, toolName } = mastraChunk.payload ?? {};
+ if (!toolCallId || !toolName) {
+ callbacks.onError(
+ new Error(
+ "Malformed tool-call-input-streaming-start: missing toolCallId or toolName in payload",
+ ),
+ );
+ return true;
+ }
+
+ streamingToolCalls.set(toolCallId, {
+ toolCallId,
+ toolName,
+ argsText: "",
+ });
+ return false;
+ }
+ case "tool-call-delta": {
+ const { toolCallId, toolName, argsTextDelta } =
+ mastraChunk.payload ?? {};
+ if (!toolCallId) {
+ callbacks.onError(
+ new Error(
+ "Malformed tool-call-delta: missing toolCallId in payload",
+ ),
+ );
+ return true;
+ }
+
+ let streamingToolCall = streamingToolCalls.get(toolCallId);
+ if (!streamingToolCall) {
+ if (!toolName) {
+ callbacks.onError(
+ new Error(
+ "Malformed tool-call-delta: missing toolName for unknown toolCallId in payload",
+ ),
+ );
+ return true;
+ }
+
+ streamingToolCall = { toolCallId, toolName, argsText: "" };
+ streamingToolCalls.set(toolCallId, streamingToolCall);
+ }
+
+ streamingToolCall.argsText += argsTextDelta ?? "";
+ return false;
+ }
+ case "tool-call-input-streaming-end": {
+ const { toolCallId } = mastraChunk.payload ?? {};
+ const streamingToolCall = toolCallId
+ ? streamingToolCalls.get(toolCallId)
+ : undefined;
+ if (streamingToolCall) {
+ synthesizedToolCallIds.add(streamingToolCall.toolCallId);
+ const shouldStop = processor.handleChunk({
+ type: "tool-call",
+ payload: {
+ toolCallId: streamingToolCall.toolCallId,
+ toolName: streamingToolCall.toolName,
+ args: parseStreamingToolCallArgs(streamingToolCall.argsText),
+ },
+ });
+ streamingToolCalls.delete(streamingToolCall.toolCallId);
+ return shouldStop;
+ }
+ return false;
+ }
+ case "tool-call": {
+ const { toolCallId } = mastraChunk.payload ?? {};
+ if (toolCallId && synthesizedToolCallIds.has(toolCallId)) {
+ synthesizedToolCallIds.delete(toolCallId);
+ return false;
+ }
+ break;
+ }
+ case "tool-call-suspended": {
+ const { toolCallId } = mastraChunk.payload ?? {};
+ if (toolCallId) {
+ streamingToolCalls.delete(toolCallId);
+ synthesizedToolCallIds.delete(toolCallId);
+ }
+ break;
+ }
+ }
+
+ return processor.handleChunk(chunk);
+ },
+ flush() {
+ processor.flush();
+ },
+ };
+ };
+}
+
function normalizeAdapterEvent(
event: AgUiEvent,
input: AgUiRunAgentInput,