xonecas.io
64875
Mc

mini-coder / commit fca0a0d

refactor(llm-api): split turn execution phases and stream mapping

xonecas2026-03-14 fca0a0d8731934cb377090c7369d8ba1428cc025 parent f419f80

2 files changed

src/llm-api/turn-execution.ts+115−0
@@ -0,0 +1,115 @@
1+import type { FlexibleSchema, StepResult } from "ai";
2+import { dynamicTool, jsonSchema, type streamText } from "ai";
3+import {
4+ mapStreamChunkToTurnEvent,
5+ shouldLogStreamChunk,
6+} from "./turn-stream-events.ts";
7+import type { ToolDef, TurnEvent } from "./types.ts";
8+
9+type StreamTextOptions = Parameters<typeof streamText>[0];
10+type CoreMessage = NonNullable<StreamTextOptions["messages"]>[number];
11+type ToolSet = NonNullable<StreamTextOptions["tools"]>;
12+type ToolEntry = ToolSet extends Record<string, infer T> ? T : never;
13+
14+export type StreamTextResultFull = ReturnType<typeof streamText> & {
15+ fullStream: AsyncIterable<{ type?: string; [key: string]: unknown }>;
16+ response: Promise<{ messages?: CoreMessage[] }>;
17+};
18+
19+interface TurnStepState {
20+ stepCount: number;
21+ inputTokens: number;
22+ outputTokens: number;
23+ contextTokens: number;
24+ partialMessages: CoreMessage[];
25+}
26+
27+interface TurnStepTracker {
28+ onStepFinish: (step: StepResult<ToolSet>) => void;
29+ getState: () => TurnStepState;
30+}
31+
32+function isZodSchema(s: unknown): boolean {
33+ return s !== null && typeof s === "object" && "_def" in (s as object);
34+}
35+
36+function toCoreTool(def: ToolDef): ReturnType<typeof dynamicTool> {
37+ const schema = isZodSchema(def.schema)
38+ ? (def.schema as FlexibleSchema<unknown>)
39+ : jsonSchema(def.schema);
40+ return dynamicTool({
41+ description: def.description,
42+ inputSchema: schema,
43+ execute: async (input: unknown) => {
44+ try {
45+ return await def.execute(input);
46+ } catch (err) {
47+ throw err instanceof Error ? err : new Error(String(err));
48+ }
49+ },
50+ });
51+}
52+
53+export function buildToolSet(tools: ToolDef[]): ToolSet {
54+ const toolSet = {} as ToolSet;
55+ for (const def of tools) {
56+ (toolSet as Record<string, ToolEntry>)[def.name] = toCoreTool(def);
57+ }
58+ return toolSet;
59+}
60+
61+export function createTurnStepTracker(opts: {
62+ onStepLog: (entry: {
63+ stepNumber: number;
64+ finishReason: string | null | undefined;
65+ usage: unknown;
66+ }) => void;
67+}): TurnStepTracker {
68+ let stepCount = 0;
69+ let inputTokens = 0;
70+ let outputTokens = 0;
71+ let contextTokens = 0;
72+ let partialMessages: CoreMessage[] = [];
73+
74+ return {
75+ onStepFinish: (step: StepResult<ToolSet>) => {
76+ opts.onStepLog({
77+ stepNumber: stepCount + 1,
78+ finishReason: step.finishReason,
79+ usage: step.usage,
80+ });
81+ inputTokens += step.usage?.inputTokens ?? 0;
82+ outputTokens += step.usage?.outputTokens ?? 0;
83+ contextTokens = step.usage?.inputTokens ?? contextTokens;
84+ stepCount += 1;
85+
86+ const s = step as unknown as {
87+ response?: { messages?: CoreMessage[] };
88+ messages?: CoreMessage[];
89+ };
90+ partialMessages = s.response?.messages ?? s.messages ?? partialMessages;
91+ },
92+ getState: () => ({
93+ stepCount,
94+ inputTokens,
95+ outputTokens,
96+ contextTokens,
97+ partialMessages,
98+ }),
99+ };
100+}
101+
102+export async function* mapFullStreamToTurnEvents(
103+ stream: AsyncIterable<{ type?: string; [key: string]: unknown }>,
104+ opts: {
105+ onChunk?: (chunk: { type?: string; [key: string]: unknown }) => void;
106+ },
107+): AsyncGenerator<TurnEvent> {
108+ for await (const chunk of stream) {
109+ if (shouldLogStreamChunk(chunk)) {
110+ opts.onChunk?.(chunk);
111+ }
112+ const event = mapStreamChunkToTurnEvent(chunk);
113+ if (event) yield event;
114+ }
115+}
src/llm-api/turn.ts+33−77
@@ -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 }