@@ -1,5 +1,5 @@
1-import type { StepResult } from "ai";
2-import { stepCountIs, streamText } from "ai";
1+import type { FlexibleSchema, StepResult } from "ai";
2+import { dynamicTool, jsonSchema, stepCountIs, streamText } from "ai";
33 import { isApiLogEnabled, logApiEvent } from "./api-log.ts";
44 import {
55 getReasoningDeltaFromStreamChunk,
@@ -32,7 +32,6 @@ import {
3232 mapStreamChunkToTurnEvent,
3333 shouldLogStreamChunk,
3434 } from "./turn-stream-events.ts";
35-import { toCoreTool } from "./turn-tools.ts";
3635 import type { ToolDef, TurnEvent } from "./types.ts";
3736
3837 type StreamTextOptions = Parameters<typeof streamText>[0];
@@ -53,6 +52,27 @@ function isRecord(value: unknown): value is Record<string, unknown> {
5352 return value !== null && typeof value === "object";
5453 }
5554
55+function isZodSchema(s: unknown): boolean {
56+ return s !== null && typeof s === "object" && "_def" in (s as object);
57+}
58+
59+function toCoreTool(def: ToolDef): ReturnType<typeof dynamicTool> {
60+ const schema = isZodSchema(def.schema)
61+ ? (def.schema as FlexibleSchema<unknown>)
62+ : jsonSchema(def.schema);
63+ return dynamicTool({
64+ description: def.description,
65+ inputSchema: schema,
66+ execute: async (input: unknown) => {
67+ try {
68+ return await def.execute(input);
69+ } catch (err) {
70+ throw err instanceof Error ? err : new Error(String(err));
71+ }
72+ },
73+ });
74+}
75+
5676 const mergeDeep = (
5777 target: Record<string, unknown>,
5878 source: Record<string, unknown>,