@@ -1,58 +1,29 @@
1-import type { FlexibleSchema, StepResult } from "ai";
2-import { dynamicTool, jsonSchema, stepCountIs, streamText } from "ai";
1+import { stepCountIs, streamText } from "ai";
32 import { isApiLogEnabled, logApiEvent } from "./api-log.ts";
43 import type { ThinkingEffort } from "./provider-options.ts";
54 import {
65 type ContextPruningMode,
76 DEFAULT_TOOL_RESULT_PAYLOAD_CAP_BYTES,
87 } from "./turn-context.ts";
8+import {
9+ buildToolSet,
10+ createTurnStepTracker,
11+ mapFullStreamToTurnEvents,
12+ type StreamTextResultFull,
13+} from "./turn-execution.ts";
914 import { prepareTurnMessages } from "./turn-prepare-messages.ts";
1015 import { buildTurnProviderOptions } from "./turn-provider-options.ts";
1116
1217 export type { ContextPruningMode } from "./turn-context.ts";
1318
14-import {
15- mapStreamChunkToTurnEvent,
16- shouldLogStreamChunk,
17-} from "./turn-stream-events.ts";
1819 import type { ToolDef, TurnEvent } from "./types.ts";
1920
2021 type StreamTextOptions = Parameters<typeof streamText>[0];
2122 export type CoreMessage = NonNullable<StreamTextOptions["messages"]>[number];
2223 type CoreModel = StreamTextOptions["model"];
23-type ToolSet = NonNullable<StreamTextOptions["tools"]>;
24-type ToolEntry = ToolSet extends Record<string, infer T> ? T : never;
25-
26-type StreamTextResultFull = ReturnType<typeof streamText> & {
27- fullStream: AsyncIterable<{ type?: string; [key: string]: unknown }>;
28- response: Promise<{ messages?: CoreMessage[] }>;
29-};
3024
3125 const MAX_STEPS = 50;
3226
33-// ─── Local helpers ────────────────────────────────────────────────────────────
34-
35-function isZodSchema(s: unknown): boolean {
36- return s !== null && typeof s === "object" && "_def" in (s as object);
37-}
38-
39-function toCoreTool(def: ToolDef): ReturnType<typeof dynamicTool> {
40- const schema = isZodSchema(def.schema)
41- ? (def.schema as FlexibleSchema<unknown>)
42- : jsonSchema(def.schema);
43- return dynamicTool({
44- description: def.description,
45- inputSchema: schema,
46- execute: async (input: unknown) => {
47- try {
48- return await def.execute(input);
49- } catch (err) {
50- throw err instanceof Error ? err : new Error(String(err));
51- }
52- },
53- });
54-}
55-
5627 // ─── runTurn ──────────────────────────────────────────────────────────────────
5728
5829 /**
@@ -90,16 +61,16 @@ export async function* runTurn(options: {
9061 toolResultPayloadCapBytes = DEFAULT_TOOL_RESULT_PAYLOAD_CAP_BYTES,
9162 } = options;
9263
93- let stepCount = 0;
94- const toolSet = {} as ToolSet;
95- for (const def of tools) {
96- (toolSet as Record<string, ToolEntry>)[def.name] = toCoreTool(def);
97- }
98-
99- let inputTokens = 0;
100- let outputTokens = 0;
101- let contextTokens = 0;
102- const partialState = { messages: [] as CoreMessage[] };
64+ const toolSet = buildToolSet(tools);
65+ const stepTracker = createTurnStepTracker({
66+ onStepLog: ({ stepNumber, finishReason, usage }) => {
67+ logApiEvent("step finish", {
68+ stepNumber,
69+ finishReason,
70+ usage,
71+ });
72+ },
73+ });
10374
10475 try {
10576 const toolCount = Object.keys(toolSet).length;
@@ -161,23 +132,7 @@ export async function* runTurn(options: {
161132 messages: prepared.messages,
162133 tools: toolSet,
163134 stopWhen: stepCountIs(MAX_STEPS),
164- onStepFinish: (step: StepResult<ToolSet>) => {
165- logApiEvent("step finish", {
166- stepNumber: stepCount + 1,
167- finishReason: step.finishReason,
168- usage: step.usage,
169- });
170- inputTokens += step.usage?.inputTokens ?? 0;
171- outputTokens += step.usage?.outputTokens ?? 0;
172- contextTokens = step.usage?.inputTokens ?? contextTokens;
173- stepCount++;
174- const s = step as unknown as {
175- response?: { messages?: CoreMessage[] };
176- messages?: CoreMessage[];
177- };
178- partialState.messages =
179- s.response?.messages ?? s.messages ?? partialState.messages;
180- },
135+ onStepFinish: stepTracker.onStepFinish,
181136 prepareStep: ({ stepNumber }: { stepNumber: number }) => {
182137 // On the last allowed step, disable tools so the model gives a final answer
183138 if (stepNumber >= MAX_STEPS - 1) {
@@ -194,9 +149,8 @@ export async function* runTurn(options: {
194149 } as StreamTextOptions) as StreamTextResultFull;
195150 result.response.catch(() => {});
196151
197- for await (const chunk of result.fullStream) {
198- const streamChunk = chunk as Record<string, unknown> & { type?: string };
199- if (shouldLogStreamChunk(streamChunk)) {
152+ for await (const event of mapFullStreamToTurnEvents(result.fullStream, {
153+ onChunk: (streamChunk) => {
200154 logApiEvent("stream chunk", {
201155 type: streamChunk.type,
202156 toolCallId: streamChunk.toolCallId,
@@ -205,30 +159,32 @@ export async function* runTurn(options: {
205159 hasArgs: "args" in streamChunk || "input" in streamChunk,
206160 hasOutput: "output" in streamChunk || "result" in streamChunk,
207161 });
208- }
209- const event = mapStreamChunkToTurnEvent(streamChunk);
210- if (event) yield event;
162+ },
163+ })) {
164+ yield event;
211165 }
212166
167+ const finalState = stepTracker.getState();
213168 logApiEvent("turn complete", {
214- newMessagesCount: partialState.messages.length,
215- inputTokens,
216- outputTokens,
169+ newMessagesCount: finalState.partialMessages.length,
170+ inputTokens: finalState.inputTokens,
171+ outputTokens: finalState.outputTokens,
217172 });
218173
219174 yield {
220175 type: "turn-complete",
221- inputTokens,
222- outputTokens,
223- contextTokens,
224- messages: partialState.messages,
176+ inputTokens: finalState.inputTokens,
177+ outputTokens: finalState.outputTokens,
178+ contextTokens: finalState.contextTokens,
179+ messages: finalState.partialMessages,
225180 };
226181 } catch (err) {
182+ const finalState = stepTracker.getState();
227183 logApiEvent("turn error", err);
228184 yield {
229185 type: "turn-error",
230186 error: err instanceof Error ? err : new Error(String(err)),
231- partialMessages: partialState.messages,
187+ partialMessages: finalState.partialMessages,
232188 };
233189 }
234190 }