@@ -1,11 +1,5 @@
1-import type { FlexibleSchema, StepResult } from "ai";
2-import {
3- dynamicTool,
4- jsonSchema,
5- pruneMessages,
6- stepCountIs,
7- streamText,
8-} from "ai";
1+import type { StepResult } from "ai";
2+import { stepCountIs, streamText } from "ai";
93 import { isApiLogEnabled, logApiEvent } from "./api-log.ts";
104 import {
115 getReasoningDeltaFromStreamChunk,
@@ -23,6 +17,20 @@ import {
2317 getThinkingProviderOptions,
2418 type ThinkingEffort,
2519 } from "./providers.ts";
20+import {
21+ annotateAnthropicCacheBreakpoints,
22+ applyContextPruning,
23+ type ContextPruningMode,
24+ compactToolResultPayloads,
25+ DEFAULT_TOOL_RESULT_PAYLOAD_CAP_BYTES,
26+ getMessageDiagnostics,
27+ getMessageStats,
28+} from "./turn-context.ts";
29+import {
30+ mapStreamChunkToTurnEvent,
31+ shouldLogStreamChunk,
32+} from "./turn-stream-events.ts";
33+import { toCoreTool } from "./turn-tools.ts";
2634 import type { ToolDef, TurnEvent } from "./types.ts";
2735
2836 type StreamTextOptions = Parameters<typeof streamText>[0];
@@ -30,354 +38,19 @@ export type CoreMessage = NonNullable<StreamTextOptions["messages"]>[number];
3038 type CoreModel = StreamTextOptions["model"];
3139 type ToolSet = NonNullable<StreamTextOptions["tools"]>;
3240 type ToolEntry = ToolSet extends Record<string, infer T> ? T : never;
33-type StreamChunk = { type?: string; [key: string]: unknown };
34-
35-export type ContextPruningMode = "off" | "balanced" | "aggressive";
36-
37-interface ToolContributorStats {
38- toolName: string;
39- count: number;
40- bytes: number;
41-}
42-
43-interface RoleStats {
44- count: number;
45- bytes: number;
46-}
47-
48-interface MessageDiagnostics {
49- messageCount: number;
50- totalBytes: number;
51- roleBreakdown: Record<string, RoleStats>;
52- toolResults: {
53- count: number;
54- bytes: number;
55- topContributors: ToolContributorStats[];
56- };
57-}
58-
59-const DEFAULT_TOOL_RESULT_PAYLOAD_CAP_BYTES = 16 * 1024;
60-
61-function getByteLength(value: string): number {
62- return Buffer.byteLength(value, "utf8");
63-}
64-
65-function safeStringify(value: unknown): string {
66- try {
67- return JSON.stringify(value) ?? "null";
68- } catch {
69- return JSON.stringify(String(value));
70- }
71-}
72-
73-/**
74- * Lightweight alternative to getMessageDiagnostics that computes only the
75- * fields needed for the context-pruned yield event (no roleBreakdown / tool
76- * contributor maps). Used when the API log is disabled.
77- */
78-function getMessageStats(messages: CoreMessage[]): {
79- messageCount: number;
80- totalBytes: number;
81-} {
82- let totalBytes = 0;
83- for (const m of messages) totalBytes += getByteLength(safeStringify(m));
84- return { messageCount: messages.length, totalBytes };
85-}
86-
87-export function getMessageDiagnostics(
88- messages: CoreMessage[],
89-): MessageDiagnostics {
90- const roleBreakdown: Record<string, RoleStats> = {};
91- const toolContributorMap = new Map<
92- string,
93- { count: number; bytes: number }
94- >();
95- let totalBytes = 0;
96- let toolResultBytes = 0;
97- let toolResultCount = 0;
98-
99- for (const message of messages) {
100- const serializedMessage = safeStringify(message);
101- const messageBytes = getByteLength(serializedMessage);
102- totalBytes += messageBytes;
103-
104- const role = message.role;
105- const roleStats = roleBreakdown[role] ?? { count: 0, bytes: 0 };
106- roleStats.count += 1;
107- roleStats.bytes += messageBytes;
108- roleBreakdown[role] = roleStats;
109-
110- if (!Array.isArray(message.content)) continue;
111- for (const part of message.content) {
112- if (!isRecord(part)) continue;
113- const partType = (part as { type?: unknown }).type;
114- if (partType !== "tool-result") continue;
115- toolResultCount += 1;
116-
117- const partRecord = part as Record<string, unknown>;
118- const rawToolName = partRecord.toolName;
119- const toolName =
120- typeof rawToolName === "string" && rawToolName.length > 0
121- ? rawToolName
122- : "unknown";
123- const payload =
124- "output" in partRecord
125- ? partRecord.output
126- : "result" in partRecord
127- ? partRecord.result
128- : null;
129- const payloadBytes = getByteLength(safeStringify(payload));
130- toolResultBytes += payloadBytes;
131-
132- const existing = toolContributorMap.get(toolName) ?? {
133- count: 0,
134- bytes: 0,
135- };
136- existing.count += 1;
137- existing.bytes += payloadBytes;
138- toolContributorMap.set(toolName, existing);
139- }
140- }
141-
142- const topContributors = [...toolContributorMap.entries()]
143- .map(([toolName, stats]) => ({
144- toolName,
145- count: stats.count,
146- bytes: stats.bytes,
147- }))
148- .sort((a, b) =>
149- b.bytes === a.bytes
150- ? a.toolName.localeCompare(b.toolName)
151- : b.bytes - a.bytes,
152- )
153- .slice(0, 5);
154-
155- return {
156- messageCount: messages.length,
157- totalBytes,
158- roleBreakdown,
159- toolResults: {
160- count: toolResultCount,
161- bytes: toolResultBytes,
162- topContributors,
163- },
164- };
165-}
166-
167-export function applyContextPruning(
168- messages: CoreMessage[],
169- mode: ContextPruningMode,
170-): CoreMessage[] {
171- if (mode === "off") return messages;
172- if (mode === "aggressive") {
173- return pruneMessages({
174- messages,
175- reasoning: "before-last-message",
176- toolCalls: "before-last-20-messages",
177- emptyMessages: "remove",
178- }) as CoreMessage[];
179- }
180-
181- return pruneMessages({
182- messages,
183- reasoning: "before-last-message",
184- toolCalls: "before-last-40-messages",
185- emptyMessages: "remove",
186- }) as CoreMessage[];
187-}
188-
189-function compactHeadTail(
190- serialized: string,
191- maxChars = 4096,
192-): { head: string; tail: string } {
193- const chars = Math.max(512, maxChars);
194- const headLength = Math.floor(chars / 2);
195- const tailLength = chars - headLength;
196- return {
197- head: serialized.slice(0, headLength),
198- tail: serialized.slice(-tailLength),
199- };
200-}
201-
202-function wrapCompactedToolResultOutput(
203- compactedPayload: Record<string, unknown>,
204-): unknown {
205- return {
206- type: "json",
207- value: compactedPayload,
208- };
209-}
210-
211-export function compactToolResultPayloads(
212- messages: CoreMessage[],
213- capBytes = DEFAULT_TOOL_RESULT_PAYLOAD_CAP_BYTES,
214-): CoreMessage[] {
215- if (!Number.isFinite(capBytes) || capBytes <= 0) return messages;
216-
217- let mutated = false;
218- const compacted = messages.map((message) => {
219- if (message.role !== "tool" || !Array.isArray(message.content)) {
220- return message;
221- }
222-
223- let contentMutated = false;
224- const nextContent = message.content.map((part) => {
225- if (!isRecord(part)) return part;
226- const partType = (part as { type?: unknown }).type;
227- if (partType !== "tool-result") return part;
228-
229- const payload =
230- "output" in part ? part.output : "result" in part ? part.result : null;
231- const serializedPayload = safeStringify(payload);
232- const originalBytes = getByteLength(serializedPayload);
233- if (originalBytes <= capBytes) return part;
234-
235- const { head, tail } = compactHeadTail(
236- serializedPayload,
237- Math.floor(capBytes / 2),
238- );
239- const compactedPayload = {
240- truncated: true,
241- originalBytes,
242- strategy: "head-tail",
243- head,
244- tail,
245- };
246-
247- contentMutated = true;
248- const partRecord = part as Record<string, unknown>;
249- if ("output" in partRecord) {
250- return {
251- ...partRecord,
252- output: wrapCompactedToolResultOutput(compactedPayload),
253- };
254- }
255- if ("result" in partRecord) {
256- return { ...partRecord, result: compactedPayload };
257- }
258- return {
259- ...partRecord,
260- output: wrapCompactedToolResultOutput(compactedPayload),
261- };
262- });
263-
264- if (!contentMutated) return message;
265- mutated = true;
266- return {
267- ...message,
268- content: nextContent as CoreMessage["content"],
269- } as CoreMessage;
270- });
271-
272- return mutated ? compacted : messages;
273-}
27441
27542 type StreamTextResult = ReturnType<typeof streamText>;
27643 type StreamTextResultFull = StreamTextResult & {
277- fullStream: AsyncIterable<StreamChunk>;
44+ fullStream: AsyncIterable<{ type?: string; [key: string]: unknown }>;
27845 response: Promise<{ messages?: CoreMessage[] }>;
27946 };
28047
28148 const MAX_STEPS = 50;
282-// ─── Helpers ──────────────────────────────────────────────────────────────────
283-
284-function isZodSchema(s: unknown): boolean {
285- // Zod schemas have a _def property; plain JSON Schema objects don't.
286- return s !== null && typeof s === "object" && "_def" in (s as object);
287-}
288-
289-function toCoreTool(def: ToolDef): ReturnType<typeof dynamicTool> {
290- // MCP tools pass raw JSON Schema objects; the AI SDK requires them to be
291- // wrapped with jsonSchema(). Zod schemas are passed through as-is.
292- const schema = isZodSchema(def.schema)
293- ? (def.schema as FlexibleSchema<unknown>)
294- : jsonSchema(def.schema);
295- return dynamicTool({
296- description: def.description,
297- inputSchema: schema,
298- execute: async (input: unknown) => {
299- try {
300- return await def.execute(input);
301- } catch (err) {
302- throw err instanceof Error ? err : new Error(String(err));
303- }
304- },
305- });
306-}
30749
30850 function isRecord(value: unknown): value is Record<string, unknown> {
30951 return value !== null && typeof value === "object";
31052 }
31153
312-export {
313- getReasoningDeltaFromStreamChunk,
314- isOpenAIGPT,
315- normalizeOpenAICompatibleToolCallInputs,
316- sanitizeGeminiToolMessages,
Cut short: this commit's diff is over 1000 lines.