@@ -275,6 +275,29 @@ export function getReasoningDeltaFromStreamChunk(
275275 return "";
276276 }
277277
278+export function consumePendingToolInputStart(
279+ pendingToolStartsById: Set<string>,
280+ pendingToolStartsByName: Map<string, number>,
281+ toolCallId: string,
282+ toolName: string,
283+): boolean {
284+ if (toolCallId.length > 0) {
285+ if (!pendingToolStartsById.has(toolCallId)) return false;
286+ pendingToolStartsById.delete(toolCallId);
287+ return true;
288+ }
289+
290+ if (toolName.length === 0) return false;
291+ const pending = pendingToolStartsByName.get(toolName) ?? 0;
292+ if (pending <= 0) return false;
293+ if (pending === 1) {
294+ pendingToolStartsByName.delete(toolName);
295+ } else {
296+ pendingToolStartsByName.set(toolName, pending - 1);
297+ }
298+ return true;
299+}
300+
278301 type StreamTextResult = ReturnType<typeof streamText>;
279302 type StreamTextResultFull = StreamTextResult & {
280303 fullStream: AsyncIterable<StreamChunk>;
@@ -1093,6 +1116,8 @@ export async function* runTurn(options: {
10931116 // awaited, causing an unhandled rejection that crashes the app.
10941117 // We catch it here to mark it as handled (awaiting it later will still throw).
10951118 result.response.catch(() => {});
1119+ const pendingToolStartsById = new Set<string>();
1120+ const pendingToolStartsByName = new Map<string, number>();
10961121
10971122 // Stream events
10981123 for await (const chunk of result.fullStream) {
@@ -1135,11 +1160,49 @@ export async function* runTurn(options: {
11351160 break;
11361161 }
11371162
1163+ case "tool-input-start": {
1164+ const toolName =
1165+ typeof c.toolName === "string" && c.toolName.length > 0
1166+ ? c.toolName
1167+ : "";
1168+ const toolCallId = String(c.toolCallId ?? "");
1169+ if (toolCallId.length > 0) {
1170+ pendingToolStartsById.add(toolCallId);
1171+ } else if (toolName.length > 0) {
1172+ pendingToolStartsByName.set(
1173+ toolName,
1174+ (pendingToolStartsByName.get(toolName) ?? 0) + 1,
1175+ );
1176+ } else {
1177+ break;
1178+ }
1179+ yield {
1180+ type: "tool-call-start",
1181+ toolCallId,
1182+ toolName,
1183+ // tool-input-start usually arrives before full args are available.
1184+ args: c.input ?? c.args,
1185+ };
1186+ break;
1187+ }
1188+
11381189 case "tool-call": {
1190+ const toolName = String(c.toolName ?? "");
1191+ const toolCallId = String(c.toolCallId ?? "");
1192+ if (
1193+ consumePendingToolInputStart(
1194+ pendingToolStartsById,
1195+ pendingToolStartsByName,
1196+ toolCallId,
1197+ toolName,
1198+ )
1199+ ) {
1200+ break;
1201+ }
11391202 yield {
11401203 type: "tool-call-start",
1141- toolCallId: String(c.toolCallId ?? ""),
1142- toolName: String(c.toolName ?? ""),
1204+ toolCallId,
1205+ toolName,
11431206 // AI SDK v6: property is `input`, not `args`
11441207 args: c.input ?? c.args,
11451208 };