xonecas.io
64875
Mc

mini-coder / commit b8aea54

refactor(llm-api): split turn pipeline into focused modules

xonecas2026-03-14 b8aea54bbf970d19b627ceeaeeaf233944265e81 parent fe79440

7 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

src/llm-api/model-routing.ts+72−0
@@ -0,0 +1,72 @@
1+interface ParsedModelString {
2+ provider: string;
3+ modelId: string;
4+}
5+
6+/**
7+ * Split a "<provider>/<model-id>" string into its two parts.
8+ * When there is no slash, provider is the full string and modelId is "".
9+ */
10+export function parseModelString(modelString: string): ParsedModelString {
11+ const slashIdx = modelString.indexOf("/");
12+ if (slashIdx === -1) return { provider: modelString, modelId: "" };
13+ return {
14+ provider: modelString.slice(0, slashIdx),
15+ modelId: modelString.slice(slashIdx + 1),
16+ };
17+}
18+
19+function isZenProvider(provider: string): boolean {
20+ return provider === "zen";
21+}
22+
23+export function isAnthropicModelFamily(modelString: string): boolean {
24+ const { provider, modelId } = parseModelString(modelString);
25+ return (
26+ provider === "anthropic" ||
27+ (isZenProvider(provider) && modelId.startsWith("claude-"))
28+ );
29+}
30+
31+export function isGeminiModelFamily(modelString: string): boolean {
32+ const { provider, modelId } = parseModelString(modelString);
33+ return (
34+ (provider === "google" || isZenProvider(provider)) &&
35+ modelId.startsWith("gemini-")
36+ );
37+}
38+
39+export function isOpenAIGPTModelFamily(modelString: string): boolean {
40+ const { provider, modelId } = parseModelString(modelString);
41+ return (
42+ (provider === "openai" || isZenProvider(provider)) &&
43+ modelId.startsWith("gpt-")
44+ );
45+}
46+
47+export function isOpenAIReasoningModelFamily(modelString: string): boolean {
48+ const { provider, modelId } = parseModelString(modelString);
49+ return (
50+ (provider === "openai" || isZenProvider(provider)) &&
51+ (modelId.startsWith("o") || modelId.startsWith("gpt-5"))
52+ );
53+}
54+
55+export function isZenOpenAICompatibleChatModel(modelString: string): boolean {
56+ const { provider, modelId } = parseModelString(modelString);
57+ if (!isZenProvider(provider)) return false;
58+ return (
59+ !modelId.startsWith("gpt-") &&
60+ !modelId.startsWith("gemini-") &&
61+ !modelId.startsWith("claude-")
62+ );
63+}
64+
65+type ZenBackend = "anthropic" | "openai" | "google" | "compat";
66+
67+export function getZenBackend(modelId: string): ZenBackend {
68+ if (modelId.startsWith("claude-")) return "anthropic";
69+ if (modelId.startsWith("gpt-")) return "openai";
70+ if (modelId.startsWith("gemini-")) return "google";
71+ return "compat";
72+}
src/llm-api/providers.ts+23−50
@@ -11,6 +11,13 @@ import {
1111 getContextWindow as getContextWindowFromCache,
1212 supportsThinking as supportsThinkingFromCache,
1313 } from "./model-info.ts";
14+import {
15+ getZenBackend,
16+ isAnthropicModelFamily,
17+ isGeminiModelFamily,
18+ isOpenAIReasoningModelFamily,
19+ parseModelString,
20+} from "./model-routing.ts";
1421
1522 // ─── Zen endpoint constants ────────────────────────────────────────────────────
1623
@@ -47,10 +54,16 @@ const ZEN_BASE = "https://opencode.ai/zen/v1";
4754
4855 // Zen endpoint routing — matched in order, fallthrough to OpenAI-compatible
4956 function zenEndpointFor(modelId: string) {
50- if (modelId.startsWith("claude-")) return zenAnthropic()(modelId);
51- if (modelId.startsWith("gpt-")) return zenOpenAI().responses(modelId);
52- if (modelId.startsWith("gemini-")) return zenGoogle()(modelId);
53- return zenCompat()(modelId);
57+ switch (getZenBackend(modelId)) {
58+ case "anthropic":
59+ return zenAnthropic()(modelId);
60+ case "openai":
61+ return zenOpenAI().responses(modelId);
62+ case "google":
63+ return zenGoogle()(modelId);
64+ case "compat":
65+ return zenCompat()(modelId);
66+ }
5467 }
5568
5669 // ─── Lazy provider factories (created on first use) ───────────────────────────
@@ -157,22 +170,6 @@ function directGoogle() {
157170
158171 // ─── Model string parsing ─────────────────────────────────────────────────────
159172
160-/**
161- * Split a "<provider>/<model-id>" string into its two parts.
162- * When there is no slash, provider is the full string and modelId is "".
163- */
164-export function parseModelString(modelString: string): {
165- provider: string;
166- modelId: string;
167-} {
168- const slashIdx = modelString.indexOf("/");
169- if (slashIdx === -1) return { provider: modelString, modelId: "" };
170- return {
171- provider: modelString.slice(0, slashIdx),
172- modelId: modelString.slice(slashIdx + 1),
173- };
174-}
175-
176173 export type ThinkingEffort = "low" | "medium" | "high" | "xhigh";
177174
178175 function supportsThinking(modelString: string): boolean {
@@ -206,13 +203,10 @@ export function getThinkingProviderOptions(
206203 ): Record<string, unknown> | null {
207204 if (!supportsThinking(modelString)) return null;
208205
209- const { provider, modelId } = parseModelString(modelString);
206+ const { modelId } = parseModelString(modelString);
210207
211208 // Anthropic (direct or via Zen routing through @ai-sdk/anthropic)
212- if (
213- provider === "anthropic" ||
214- (provider === "zen" && modelId.startsWith("claude-"))
215- ) {
209+ if (isAnthropicModelFamily(modelString)) {
216210 // Claude 3.7+, Sonnet 4.x, Opus 4.x support adaptive effort strings.
217211 const isAdaptive =
218212 /^claude-3-7/.test(modelId) ||
@@ -237,11 +231,7 @@ export function getThinkingProviderOptions(
237231 }
238232
239233 // OpenAI o-series and GPT-5 (direct or via Zen routing through @ai-sdk/openai)
240- if (
241- provider === "openai" ||
242- (provider === "zen" &&
243- (modelId.startsWith("o") || modelId.startsWith("gpt-5")))
244- ) {
234+ if (isOpenAIReasoningModelFamily(modelString)) {
245235 // xhigh only valid on gpt-5.2+, o4+; clamp for others.
246236 const supportsXhigh = /^gpt-5\.[2-9]/.test(modelId) || /^o4/.test(modelId);
247237 const clamped = supportsXhigh ? effort : clampEffort(effort, "high");
@@ -249,10 +239,7 @@ export function getThinkingProviderOptions(
249239 }
250240
251241 // Google Gemini (direct or via Zen routing through @ai-sdk/google)
252- if (
253- provider === "google" ||
254- (provider === "zen" && modelId.startsWith("gemini-"))
255- ) {
242+ if (isGeminiModelFamily(modelString)) {
256243 // Gemini 3.x: thinkingLevel enum. 2.5.x: budgetTokens.
257244 if (/^gemini-3/.test(modelId)) {
258245 // No xhigh for Gemini 3.
@@ -297,22 +284,8 @@ interface CachingOptions {
297284 type CacheFamily = "google" | "anthropic" | "none";
298285
299286 export function getCacheFamily(modelString: string): CacheFamily {
300- const { provider, modelId } = parseModelString(modelString);
301-
302- if (
303- provider === "anthropic" ||
304- (provider === "zen" && modelId.startsWith("claude-"))
305- ) {
306- return "anthropic";
307- }
308-
309- if (
310- provider === "google" ||
311- (provider === "zen" && modelId.startsWith("gemini-"))
312- ) {
313- return "google";
314- }
315-
287+ if (isAnthropicModelFamily(modelString)) return "anthropic";
288+ if (isGeminiModelFamily(modelString)) return "google";
316289 return "none";
317290 }
318291
src/llm-api/turn-context.ts+295−0
@@ -0,0 +1,295 @@
1+import { pruneMessages } from "ai";
2+import type { CoreMessage } from "./turn.ts";
3+
4+interface ToolContributorStats {
5+ toolName: string;
6+ count: number;
7+ bytes: number;
8+}
9+
10+interface RoleStats {
11+ count: number;
12+ bytes: number;
13+}
14+
15+interface MessageDiagnostics {
16+ messageCount: number;
17+ totalBytes: number;
18+ roleBreakdown: Record<string, RoleStats>;
19+ toolResults: {
20+ count: number;
21+ bytes: number;
22+ topContributors: ToolContributorStats[];
23+ };
24+}
25+
26+export const DEFAULT_TOOL_RESULT_PAYLOAD_CAP_BYTES = 16 * 1024;
27+
28+function getByteLength(value: string): number {
29+ return Buffer.byteLength(value, "utf8");
30+}
31+
32+function safeStringify(value: unknown): string {
33+ try {
34+ return JSON.stringify(value) ?? "null";
35+ } catch {
36+ return JSON.stringify(String(value));
37+ }
38+}
39+
40+function isRecord(value: unknown): value is Record<string, unknown> {
41+ return value !== null && typeof value === "object";
42+}
43+
44+/**
45+ * Lightweight alternative to getMessageDiagnostics that computes only the
46+ * fields needed for the context-pruned yield event (no roleBreakdown / tool
47+ * contributor maps). Used when the API log is disabled.
48+ */
49+export function getMessageStats(messages: CoreMessage[]): {
50+ messageCount: number;
51+ totalBytes: number;
52+} {
53+ let totalBytes = 0;
54+ for (const m of messages) totalBytes += getByteLength(safeStringify(m));
55+ return { messageCount: messages.length, totalBytes };
56+}
57+
58+export function getMessageDiagnostics(
59+ messages: CoreMessage[],
60+): MessageDiagnostics {
61+ const roleBreakdown: Record<string, RoleStats> = {};
62+ const toolContributorMap = new Map<
63+ string,
64+ { count: number; bytes: number }
65+ >();
66+ let totalBytes = 0;
67+ let toolResultBytes = 0;
68+ let toolResultCount = 0;
69+
70+ for (const message of messages) {
71+ const serializedMessage = safeStringify(message);
72+ const messageBytes = getByteLength(serializedMessage);
73+ totalBytes += messageBytes;
74+
75+ const role = message.role;
76+ const roleStats = roleBreakdown[role] ?? { count: 0, bytes: 0 };
77+ roleStats.count += 1;
78+ roleStats.bytes += messageBytes;
79+ roleBreakdown[role] = roleStats;
80+
81+ if (!Array.isArray(message.content)) continue;
82+ for (const part of message.content) {
83+ if (!isRecord(part)) continue;
84+ const partType = (part as { type?: unknown }).type;
85+ if (partType !== "tool-result") continue;
86+ toolResultCount += 1;
87+
88+ const partRecord = part as Record<string, unknown>;
89+ const rawToolName = partRecord.toolName;
90+ const toolName =
91+ typeof rawToolName === "string" && rawToolName.length > 0
92+ ? rawToolName
93+ : "unknown";
94+ const payload =
95+ "output" in partRecord
96+ ? partRecord.output
97+ : "result" in partRecord
98+ ? partRecord.result
99+ : null;
100+ const payloadBytes = getByteLength(safeStringify(payload));
101+ toolResultBytes += payloadBytes;
102+
103+ const existing = toolContributorMap.get(toolName) ?? {
104+ count: 0,
105+ bytes: 0,
106+ };
107+ existing.count += 1;
108+ existing.bytes += payloadBytes;
109+ toolContributorMap.set(toolName, existing);
110+ }
111+ }
112+
113+ const topContributors = [...toolContributorMap.entries()]
114+ .map(([toolName, stats]) => ({
115+ toolName,
116+ count: stats.count,
117+ bytes: stats.bytes,
118+ }))
119+ .sort((a, b) =>
120+ b.bytes === a.bytes
121+ ? a.toolName.localeCompare(b.toolName)
122+ : b.bytes - a.bytes,
123+ )
124+ .slice(0, 5);
125+
126+ return {
127+ messageCount: messages.length,
128+ totalBytes,
129+ roleBreakdown,
130+ toolResults: {
131+ count: toolResultCount,
132+ bytes: toolResultBytes,
133+ topContributors,
134+ },
135+ };
136+}
137+
138+export type ContextPruningMode = "off" | "balanced" | "aggressive";
139+
140+export function applyContextPruning(
141+ messages: CoreMessage[],
142+ mode: ContextPruningMode,
143+): CoreMessage[] {
144+ if (mode === "off") return messages;
145+ if (mode === "aggressive") {
146+ return pruneMessages({
147+ messages,
148+ reasoning: "before-last-message",
149+ toolCalls: "before-last-20-messages",
150+ emptyMessages: "remove",
151+ }) as CoreMessage[];
152+ }
153+
154+ return pruneMessages({
155+ messages,
156+ reasoning: "before-last-message",
157+ toolCalls: "before-last-40-messages",
158+ emptyMessages: "remove",
159+ }) as CoreMessage[];
160+}
161+
162+function compactHeadTail(
163+ serialized: string,
164+ maxChars = 4096,
165+): { head: string; tail: string } {
166+ const chars = Math.max(512, maxChars);
167+ const headLength = Math.floor(chars / 2);
168+ const tailLength = chars - headLength;
169+ return {
170+ head: serialized.slice(0, headLength),
171+ tail: serialized.slice(-tailLength),
172+ };
173+}
174+
175+function wrapCompactedToolResultOutput(
176+ compactedPayload: Record<string, unknown>,
177+): unknown {
178+ return {
179+ type: "json",
180+ value: compactedPayload,
181+ };
182+}
183+
184+export function compactToolResultPayloads(
185+ messages: CoreMessage[],
186+ capBytes = DEFAULT_TOOL_RESULT_PAYLOAD_CAP_BYTES,
187+): CoreMessage[] {
188+ if (!Number.isFinite(capBytes) || capBytes <= 0) return messages;
189+
190+ let mutated = false;
191+ const compacted = messages.map((message) => {
192+ if (message.role !== "tool" || !Array.isArray(message.content)) {
193+ return message;
194+ }
195+
196+ let contentMutated = false;
197+ const nextContent = message.content.map((part) => {
198+ if (!isRecord(part)) return part;
199+ const partType = (part as { type?: unknown }).type;
200+ if (partType !== "tool-result") return part;
201+
202+ const payload =
203+ "output" in part ? part.output : "result" in part ? part.result : null;
204+ const serializedPayload = safeStringify(payload);
205+ const originalBytes = getByteLength(serializedPayload);
206+ if (originalBytes <= capBytes) return part;
207+
208+ const { head, tail } = compactHeadTail(
209+ serializedPayload,
210+ Math.floor(capBytes / 2),
211+ );
212+ const compactedPayload = {
213+ truncated: true,
214+ originalBytes,
215+ strategy: "head-tail",
216+ head,
217+ tail,
218+ };
219+
220+ contentMutated = true;
221+ if ("output" in part) {
222+ return {
223+ ...part,
224+ output: wrapCompactedToolResultOutput(compactedPayload),
225+ };
226+ }
227+ if ("result" in part) {
228+ return { ...part, result: compactedPayload };
229+ }
230+ return {
231+ ...part,
232+ output: wrapCompactedToolResultOutput(compactedPayload),
233+ };
234+ });
235+
236+ if (!contentMutated) return message;
237+ mutated = true;
238+ return {
239+ ...message,
240+ content: nextContent as CoreMessage["content"],
241+ } as CoreMessage;
242+ });
243+
244+ return mutated ? compacted : messages;
245+}
246+
247+export function annotateAnthropicCacheBreakpoints(
248+ turnMessages: CoreMessage[],
249+ systemPrompt?: string,
250+): {
251+ messages: CoreMessage[];
252+ systemPrompt?: string;
253+ diagnostics: { breakpointsAdded: number; stableIdx: number };
254+} {
255+ const finalMessages = [...turnMessages];
256+ let breakpointsAdded = 0;
257+
258+ const stableIdx = finalMessages.length > 2 ? finalMessages.length - 2 : -1;
259+ if (stableIdx >= 0 && finalMessages[stableIdx]) {
260+ const msg = finalMessages[stableIdx];
261+ finalMessages[stableIdx] = {
262+ ...msg,
263+ providerOptions: {
264+ ...(msg?.providerOptions ?? {}),
265+ anthropic: {
266+ ...((msg?.providerOptions?.anthropic as Record<string, unknown>) ??
267+ {}),
268+ cacheControl: { type: "ephemeral" },
269+ },
270+ },
271+ } as CoreMessage;
272+ breakpointsAdded++;
273+ }
274+
275+ let finalSystemPrompt = systemPrompt;
276+ if (systemPrompt) {
277+ finalMessages.unshift({
278+ role: "system",
279+ content: systemPrompt,
280+ providerOptions: {
281+ anthropic: { cacheControl: { type: "ephemeral" } },
282+ },
283+ });
284+ finalSystemPrompt = undefined;
285+ breakpointsAdded++;
286+ }
287+
288+ return {
289+ messages: finalMessages,
290+ ...(finalSystemPrompt !== undefined
291+ ? { systemPrompt: finalSystemPrompt }
292+ : {}),
293+ diagnostics: { breakpointsAdded, stableIdx },
294+ };
295+}
src/llm-api/turn-stream-events.test.ts+82−0
@@ -0,0 +1,82 @@
1+import { describe, expect, test } from "bun:test";
2+import {
3+ mapStreamChunkToTurnEvent,
4+ shouldLogStreamChunk,
5+} from "./turn-stream-events.ts";
6+
7+describe("shouldLogStreamChunk", () => {
8+ test("suppresses text and reasoning deltas", () => {
9+ expect(shouldLogStreamChunk({ type: "text-delta" })).toBe(false);
10+ expect(shouldLogStreamChunk({ type: "reasoning" })).toBe(false);
11+ expect(shouldLogStreamChunk({ type: "reasoning-delta" })).toBe(false);
12+ expect(shouldLogStreamChunk({ type: "tool-call" })).toBe(true);
13+ });
14+});
15+
16+describe("mapStreamChunkToTurnEvent", () => {
17+ test("maps text deltas", () => {
18+ expect(
19+ mapStreamChunkToTurnEvent({ type: "text-delta", text: "hello" }),
20+ ).toEqual({
21+ type: "text-delta",
22+ delta: "hello",
23+ });
24+ });
25+
26+ test("maps tool call events using input or args", () => {
27+ expect(
28+ mapStreamChunkToTurnEvent({
29+ type: "tool-call",
30+ toolCallId: "id-1",
31+ toolName: "read",
32+ input: { path: "a.ts" },
33+ }),
34+ ).toEqual({
35+ type: "tool-call-start",
36+ toolCallId: "id-1",
37+ toolName: "read",
38+ args: { path: "a.ts" },
39+ });
40+ });
41+
42+ test("maps tool-result events from output/result and preserves isError", () => {
43+ expect(
44+ mapStreamChunkToTurnEvent({
45+ type: "tool-result",
46+ toolCallId: "id-2",
47+ toolName: "shell",
48+ output: { ok: true },
49+ isError: 1,
50+ }),
51+ ).toEqual({
52+ type: "tool-result",
53+ toolCallId: "id-2",
54+ toolName: "shell",
55+ result: { ok: true },
56+ isError: true,
57+ });
58+ });
59+
60+ test("maps tool-error to tool-result error", () => {
61+ expect(
62+ mapStreamChunkToTurnEvent({
63+ type: "tool-error",
64+ toolCallId: "id-3",
65+ toolName: "replace",
66+ error: "bad anchor",
67+ }),
68+ ).toEqual({
69+ type: "tool-result",
70+ toolCallId: "id-3",
71+ toolName: "replace",
72+ result: "bad anchor",
73+ isError: true,
74+ });
75+ });
76+
77+ test("throws on stream error chunks", () => {
78+ expect(() =>
79+ mapStreamChunkToTurnEvent({ type: "error", error: new Error("boom") }),
80+ ).toThrow("boom");
81+ });
82+});
src/llm-api/turn-stream-events.ts+74−0
@@ -0,0 +1,74 @@
1+import { getReasoningDeltaFromStreamChunk } from "./history-transforms.ts";
2+import type { TurnEvent } from "./types.ts";
3+
4+type StreamChunk = { type?: string; [key: string]: unknown };
5+
6+export function shouldLogStreamChunk(c: StreamChunk): boolean {
7+ return (
8+ c.type !== "text-delta" &&
9+ c.type !== "reasoning" &&
10+ c.type !== "reasoning-delta"
11+ );
12+}
13+
14+export function mapStreamChunkToTurnEvent(c: StreamChunk): TurnEvent | null {
15+ switch (c.type) {
16+ case "text-delta": {
17+ const delta = typeof c.text === "string" ? c.text : "";
18+ return {
19+ type: "text-delta",
20+ delta,
21+ };
22+ }
23+ case "reasoning-delta":
24+ case "reasoning": {
25+ const delta = getReasoningDeltaFromStreamChunk(c);
26+ if (delta === null) return null;
27+ return {
28+ type: "reasoning-delta",
29+ delta,
30+ };
31+ }
32+ case "tool-input-start": {
33+ return {
34+ type: "tool-input-start",
35+ toolCallId: String(c.toolCallId ?? ""),
36+ toolName: String(c.toolName ?? ""),
37+ args: c.input ?? c.args,
38+ };
39+ }
40+ case "tool-call": {
41+ const toolName = String(c.toolName ?? "");
42+ const toolCallId = String(c.toolCallId ?? "");
43+ return {
44+ type: "tool-call-start",
45+ toolCallId,
46+ toolName,
47+ args: c.input ?? c.args,
48+ };
49+ }
50+ case "tool-result": {
51+ return {
52+ type: "tool-result",
53+ toolCallId: String(c.toolCallId ?? ""),
54+ toolName: String(c.toolName ?? ""),
55+ result: "output" in c ? c.output : "result" in c ? c.result : undefined,
56+ isError: "isError" in c ? Boolean(c.isError) : false,
57+ };
58+ }
59+ case "tool-error":
60+ return {
61+ type: "tool-result",
62+ toolCallId: String(c.toolCallId ?? ""),
63+ toolName: String(c.toolName ?? ""),
64+ result: c.error ?? "Tool execution failed",
65+ isError: true,
66+ };
67+ case "error": {
68+ const err = c.error;
69+ throw err instanceof Error ? err : new Error(String(err));
70+ }
71+ default:
72+ return null;
73+ }
74+}
src/llm-api/turn-tools.ts+24−0
@@ -0,0 +1,24 @@
1+import type { FlexibleSchema } from "ai";
2+import { dynamicTool, jsonSchema } from "ai";
3+import type { ToolDef } from "./types.ts";
4+
5+function isZodSchema(s: unknown): boolean {
6+ return s !== null && typeof s === "object" && "_def" in (s as object);
7+}
8+
9+export function toCoreTool(def: ToolDef): ReturnType<typeof dynamicTool> {
10+ const schema = isZodSchema(def.schema)
11+ ? (def.schema as FlexibleSchema<unknown>)
12+ : jsonSchema(def.schema);
13+ return dynamicTool({
14+ description: def.description,
15+ inputSchema: schema,
16+ execute: async (input: unknown) => {
17+ try {
18+ return await def.execute(input);
19+ } catch (err) {
20+ throw err instanceof Error ? err : new Error(String(err));
21+ }
22+ },
23+ });
24+}
src/llm-api/turn.ts+52−467
@@ -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.