@@ -1,6 +1,7 @@
11 import type { FlexibleSchema, StepResult } from "ai";
22 import { dynamicTool, jsonSchema, type streamText } from "ai";
33 import { normalizeUnknownError } from "./error-utils.ts";
4+import { isRecord } from "./history/shared.ts";
45 import {
56 extractToolArgs,
67 hasRenderableToolArgs,
@@ -119,7 +120,7 @@ interface StreamTextChunk {
119120
120121 const TOOL_RESULT_CHUNK_TYPES = new Set(["tool-result", "tool-error"]);
121122
122-function normalizeToolCallId(raw: unknown): string | null {
123+function normalizeStringId(raw: unknown): string | null {
123124 if (typeof raw !== "string") return null;
124125 const trimmed = raw.trim();
125126 return trimmed ? trimmed : null;
@@ -131,16 +132,6 @@ function normalizeToolName(raw: unknown): string {
131132 return trimmed || "tool";
132133 }
133134
134-function isRecord(value: unknown): value is Record<string, unknown> {
135- return value !== null && typeof value === "object";
136-}
137-
138-function normalizeTextPartId(raw: unknown): string | null {
139- if (typeof raw !== "string") return null;
140- const trimmed = raw.trim();
141- return trimmed ? trimmed : null;
142-}
143-
144135 function getOpenAITextPhase(
145136 chunk: StreamTextChunk,
146137 ): "commentary" | "final_answer" | null {
@@ -171,7 +162,7 @@ class StreamTextPhaseTracker {
171162 private sawExplicitReasoningThisStep = false;
172163
173164 route(chunk: StreamTextChunk): StreamTextRoute {
174- const textPartId = normalizeTextPartId(chunk.id);
165+ const textPartId = normalizeStringId(chunk.id);
175166 switch (chunk.type) {
176167 case "start-step": {
177168 this.sawExplicitReasoningThisStep = false;
@@ -232,7 +223,7 @@ class StreamToolCallTracker {
232223
233224 if (type === "tool-input-start") {
234225 const toolName = normalizeToolName(chunk.toolName);
235- const toolCallId = normalizeToolCallId(chunk.toolCallId);
226+ const toolCallId = normalizeStringId(chunk.toolCallId);
236227 const args = extractToolArgs(chunk);
237228 if (!hasRenderableToolArgs(args)) {
238229 if (!toolCallId) {
@@ -254,7 +245,7 @@ class StreamToolCallTracker {
254245 chunk: this.trackRenderableStart(
255246 chunk,
256247 toolName,
257- normalizeToolCallId(chunk.toolCallId),
248+ normalizeStringId(chunk.toolCallId),
258249 ),
259250 suppressTurnEvent: false,
260251 };
@@ -262,7 +253,7 @@ class StreamToolCallTracker {
262253
263254 if (TOOL_RESULT_CHUNK_TYPES.has(type)) {
264255 const toolName = normalizeToolName(chunk.toolName);
265- const existingToolCallId = normalizeToolCallId(chunk.toolCallId);
256+ const existingToolCallId = normalizeStringId(chunk.toolCallId);
266257 if (existingToolCallId) {
267258 this.consumeTracked(toolName, existingToolCallId);
268259 return { chunk, suppressTurnEvent: false };