@@ -1,6 +1,8 @@
11 import type { FlexibleSchema, StepResult } from "ai";
22 import { dynamicTool, jsonSchema, type streamText } from "ai";
33 import {
4+ extractToolArgs,
5+ hasRenderableToolArgs,
46 mapStreamChunkToTurnEvent,
57 shouldLogStreamChunk,
68 } from "./turn-stream-events.ts";
@@ -114,7 +116,6 @@ interface StreamTextChunk {
114116 [key: string]: unknown;
115117 }
116118
117-const TOOL_START_CHUNK_TYPES = new Set(["tool-input-start", "tool-call"]);
118119 const TOOL_RESULT_CHUNK_TYPES = new Set(["tool-result", "tool-error"]);
119120
120121 function normalizeToolCallId(raw: unknown): string | null {
@@ -217,18 +218,45 @@ function mapCommentaryChunkToTurnEvent(
217218 class StreamToolCallTracker {
218219 private syntheticCount = 0;
219220 private pendingByTool = new Map<string, string[]>();
221+ private deferredStartsByTool = new Map<string, number>();
220222
221- assign(chunk: StreamToolChunk): StreamToolChunk {
223+ prepare(chunk: StreamToolChunk): {
224+ chunk: StreamToolChunk;
225+ suppressTurnEvent: boolean;
226+ } {
222227 const type = chunk.type;
223- if (!type) return chunk;
228+ if (!type) {
229+ return { chunk, suppressTurnEvent: false };
230+ }
231+
232+ if (type === "tool-input-start") {
233+ const toolName = normalizeToolName(chunk.toolName);
234+ const toolCallId = normalizeToolCallId(chunk.toolCallId);
235+ const args = extractToolArgs(chunk);
236+ if (!hasRenderableToolArgs(args)) {
237+ if (!toolCallId) {
238+ this.trackDeferredStart(toolName);
239+ return { chunk, suppressTurnEvent: true };
240+ }
241+ return { chunk, suppressTurnEvent: false };
242+ }
243+ return {
244+ chunk: this.trackRenderableStart(chunk, toolName, toolCallId),
245+ suppressTurnEvent: false,
246+ };
247+ }
224248
225- if (TOOL_START_CHUNK_TYPES.has(type)) {
249+ if (type === "tool-call") {
226250 const toolName = normalizeToolName(chunk.toolName);
227- const toolCallId =
228- normalizeToolCallId(chunk.toolCallId) ?? this.nextSyntheticToolCallId();
229- this.trackStart(toolName, toolCallId);
230- if (toolCallId === chunk.toolCallId) return chunk;
231- return { ...chunk, toolCallId };
251+ this.consumeDeferredStart(toolName);
252+ return {
253+ chunk: this.trackRenderableStart(
254+ chunk,
255+ toolName,
256+ normalizeToolCallId(chunk.toolCallId),
257+ ),
258+ suppressTurnEvent: false,
259+ };
232260 }
233261
234262 if (TOOL_RESULT_CHUNK_TYPES.has(type)) {
@@ -236,14 +264,28 @@ class StreamToolCallTracker {
236264 const existingToolCallId = normalizeToolCallId(chunk.toolCallId);
237265 if (existingToolCallId) {
238266 this.consumeTracked(toolName, existingToolCallId);
239- return chunk;
267+ return { chunk, suppressTurnEvent: false };
240268 }
241- const toolCallId =
269+ const nextToolCallId =
242270 this.consumeNextTracked(toolName) ?? this.nextSyntheticToolCallId();
243- return { ...chunk, toolCallId };
271+ return {
272+ chunk: { ...chunk, toolCallId: nextToolCallId },
273+ suppressTurnEvent: false,
274+ };
244275 }
245276
246- return chunk;
277+ return { chunk, suppressTurnEvent: false };
278+ }
279+
280+ private trackRenderableStart(
281+ chunk: StreamToolChunk,
282+ toolName: string,
283+ existingToolCallId: string | null,
284+ ): StreamToolChunk {
285+ const toolCallId = existingToolCallId ?? this.nextSyntheticToolCallId();
286+ this.trackStart(toolName, toolCallId);
287+ if (toolCallId === chunk.toolCallId) return chunk;
288+ return { ...chunk, toolCallId };
247289 }
248290
249291 private nextSyntheticToolCallId(): string {
@@ -257,6 +299,23 @@ class StreamToolCallTracker {
257299 this.pendingByTool.set(toolName, pending);
258300 }
259301
302+ private trackDeferredStart(toolName: string): void {
303+ this.deferredStartsByTool.set(
304+ toolName,
305+ (this.deferredStartsByTool.get(toolName) ?? 0) + 1,
306+ );
307+ }
308+
309+ private consumeDeferredStart(toolName: string): void {
310+ const count = this.deferredStartsByTool.get(toolName) ?? 0;
311+ if (count <= 0) return;
312+ if (count === 1) {
313+ this.deferredStartsByTool.delete(toolName);
314+ return;
315+ }
316+ this.deferredStartsByTool.set(toolName, count - 1);
317+ }
318+
260319 private consumeTracked(toolName: string, toolCallId: string): void {
261320 const pending = this.pendingByTool.get(toolName);
262321 if (!pending || pending.length === 0) return;
@@ -284,12 +343,17 @@ export async function* mapFullStreamToTurnEvents(
284343 const toolCallTracker = new StreamToolCallTracker();
285344 const textPhaseTracker = new StreamTextPhaseTracker();
286345 for await (const originalChunk of stream) {
287- const chunk = toolCallTracker.assign(originalChunk);
346+ const prepared = toolCallTracker.prepare(originalChunk);
347+ const chunk = prepared.chunk;
288348 const route = textPhaseTracker.route(chunk);
289- if (route !== "skip" && shouldLogStreamChunk(chunk)) {
349+ if (
350+ !prepared.suppressTurnEvent &&
351+ route !== "skip" &&
352+ shouldLogStreamChunk(chunk)
353+ ) {
290354 opts.onChunk?.(chunk);
291355 }
292- if (route === "skip") continue;
356+ if (prepared.suppressTurnEvent || route === "skip") continue;
293357 const event =
294358 route === "reasoning"
295359 ? mapCommentaryChunkToTurnEvent(chunk)