@@ -1,5 +1,11 @@
11 import type { FlexibleSchema, StepResult } from "ai";
2-import { dynamicTool, jsonSchema, stepCountIs, streamText } from "ai";
2+import {
3+ dynamicTool,
4+ jsonSchema,
5+ pruneMessages,
6+ stepCountIs,
7+ streamText,
8+} from "ai";
39 import { logApiEvent } from "./api-log.ts";
410 import {
511 getThinkingProviderOptions,
@@ -16,6 +22,217 @@ type ToolSet = NonNullable<StreamTextOptions["tools"]>;
1622 type ToolEntry = ToolSet extends Record<string, infer T> ? T : never;
1723 type StreamChunk = { type?: string; [key: string]: unknown };
1824
25+export type ContextPruningMode = "off" | "balanced" | "aggressive";
26+
27+interface ToolContributorStats {
28+ toolName: string;
29+ count: number;
30+ bytes: number;
31+}
32+
33+interface RoleStats {
34+ count: number;
35+ bytes: number;
36+}
37+
38+interface MessageDiagnostics {
39+ messageCount: number;
40+ totalBytes: number;
41+ roleBreakdown: Record<string, RoleStats>;
42+ toolResults: {
43+ count: number;
44+ bytes: number;
45+ topContributors: ToolContributorStats[];
46+ };
47+}
48+
49+const DEFAULT_TOOL_RESULT_PAYLOAD_CAP_BYTES = 16 * 1024;
50+
51+function getByteLength(value: string): number {
52+ return Buffer.byteLength(value, "utf8");
53+}
54+
55+function safeStringify(value: unknown): string {
56+ try {
57+ return JSON.stringify(value) ?? "null";
58+ } catch {
59+ return JSON.stringify(String(value));
60+ }
61+}
62+
63+export function getMessageDiagnostics(
64+ messages: CoreMessage[],
65+): MessageDiagnostics {
66+ const roleBreakdown: Record<string, RoleStats> = {};
67+ const toolContributorMap = new Map<
68+ string,
69+ { count: number; bytes: number }
70+ >();
71+ let totalBytes = 0;
72+ let toolResultBytes = 0;
73+ let toolResultCount = 0;
74+
75+ for (const message of messages) {
76+ const serializedMessage = safeStringify(message);
77+ const messageBytes = getByteLength(serializedMessage);
78+ totalBytes += messageBytes;
79+
80+ const role = message.role;
81+ const roleStats = roleBreakdown[role] ?? { count: 0, bytes: 0 };
82+ roleStats.count += 1;
83+ roleStats.bytes += messageBytes;
84+ roleBreakdown[role] = roleStats;
85+
86+ if (!Array.isArray(message.content)) continue;
87+ for (const part of message.content) {
88+ if (!isRecord(part)) continue;
89+ const partType = (part as { type?: unknown }).type;
90+ if (partType !== "tool-result") continue;
91+ toolResultCount += 1;
92+
93+ const partRecord = part as Record<string, unknown>;
94+ const rawToolName = partRecord.toolName;
95+ const toolName =
96+ typeof rawToolName === "string" && rawToolName.length > 0
97+ ? rawToolName
98+ : "unknown";
99+ const payload =
100+ "output" in partRecord
101+ ? partRecord.output
102+ : "result" in partRecord
103+ ? partRecord.result
104+ : null;
105+ const payloadBytes = getByteLength(safeStringify(payload));
106+ toolResultBytes += payloadBytes;
107+
108+ const existing = toolContributorMap.get(toolName) ?? {
109+ count: 0,
110+ bytes: 0,
111+ };
112+ existing.count += 1;
113+ existing.bytes += payloadBytes;
114+ toolContributorMap.set(toolName, existing);
115+ }
116+ }
117+
118+ const topContributors = [...toolContributorMap.entries()]
119+ .map(([toolName, stats]) => ({
120+ toolName,
121+ count: stats.count,
122+ bytes: stats.bytes,
123+ }))
124+ .sort((a, b) =>
125+ b.bytes === a.bytes
126+ ? a.toolName.localeCompare(b.toolName)
127+ : b.bytes - a.bytes,
128+ )
129+ .slice(0, 5);
130+
131+ return {
132+ messageCount: messages.length,
133+ totalBytes,
134+ roleBreakdown,
135+ toolResults: {
136+ count: toolResultCount,
137+ bytes: toolResultBytes,
138+ topContributors,
139+ },
140+ };
141+}
142+
143+export function applyContextPruning(
144+ messages: CoreMessage[],
145+ mode: ContextPruningMode,
146+): CoreMessage[] {
147+ if (mode === "off") return messages;
148+ if (mode === "aggressive") {
149+ return pruneMessages({
150+ messages,
151+ reasoning: "before-last-message",
152+ toolCalls: "before-last-2-messages",
153+ emptyMessages: "remove",
154+ }) as CoreMessage[];
155+ }
156+
157+ return pruneMessages({
158+ messages,
159+ reasoning: "before-last-message",
160+ toolCalls: "before-last-3-messages",
161+ emptyMessages: "remove",
162+ }) as CoreMessage[];
163+}
164+
165+function compactHeadTail(
166+ serialized: string,
167+ maxChars = 4096,
168+): { head: string; tail: string } {
169+ const chars = Math.max(512, maxChars);
170+ const headLength = Math.floor(chars / 2);
171+ const tailLength = chars - headLength;
172+ return {
173+ head: serialized.slice(0, headLength),
174+ tail: serialized.slice(-tailLength),
175+ };
176+}
177+
178+export function compactToolResultPayloads(
179+ messages: CoreMessage[],
180+ capBytes = DEFAULT_TOOL_RESULT_PAYLOAD_CAP_BYTES,
181+): CoreMessage[] {
182+ if (!Number.isFinite(capBytes) || capBytes <= 0) return messages;
183+
184+ let mutated = false;
185+ const compacted = messages.map((message) => {
186+ if (message.role !== "tool" || !Array.isArray(message.content)) {
187+ return message;
188+ }
189+
190+ let contentMutated = false;
191+ const nextContent = message.content.map((part) => {
192+ if (!isRecord(part)) return part;
193+ const partType = (part as { type?: unknown }).type;
194+ if (partType !== "tool-result") return part;
195+
196+ const payload =
197+ "output" in part ? part.output : "result" in part ? part.result : null;
198+ const serializedPayload = safeStringify(payload);
199+ const originalBytes = getByteLength(serializedPayload);
200+ if (originalBytes <= capBytes) return part;
201+
202+ const { head, tail } = compactHeadTail(
203+ serializedPayload,
204+ Math.floor(capBytes / 2),
205+ );
206+ const compactedPayload = {
207+ truncated: true,
208+ originalBytes,
209+ strategy: "head-tail",
210+ head,
211+ tail,
212+ };
213+
214+ contentMutated = true;
215+ const partRecord = part as Record<string, unknown>;
216+ if ("output" in partRecord) {
217+ return { ...partRecord, output: compactedPayload };
218+ }
219+ if ("result" in partRecord) {
220+ return { ...partRecord, result: compactedPayload };
221+ }
222+ return { ...partRecord, output: compactedPayload };
223+ });
224+
225+ if (!contentMutated) return message;
226+ mutated = true;
227+ return {
228+ ...message,
229+ content: nextContent as CoreMessage["content"],
230+ } as CoreMessage;
231+ });
232+
233+ return mutated ? compacted : messages;
234+}
235+
19236 export function getReasoningDeltaFromStreamChunk(
20237 chunk: StreamChunk,
21238 ): string | null {
@@ -187,6 +404,8 @@ export async function* runTurn(options: {
187404 systemPrompt?: string;
188405 signal?: AbortSignal;
189406 thinkingEffort?: ThinkingEffort;
407+ pruningMode?: ContextPruningMode;
408+ toolResultPayloadCapBytes?: number;
190409 }): AsyncGenerator<TurnEvent> {
191410 const {
192411 model,
@@ -196,6 +415,8 @@ export async function* runTurn(options: {
196415 systemPrompt,
197416 signal,
198417 thinkingEffort,
418+ pruningMode = "balanced",
419+ toolResultPayloadCapBytes = DEFAULT_TOOL_RESULT_PAYLOAD_CAP_BYTES,
199420 } = options;
200421
201422 let stepCount = 0;
@@ -233,18 +454,44 @@ export async function* runTurn(options: {
233454 modelString,
234455 messageCount: messages.length,
235456 reasoningSummaryRequested,
457+ pruningMode,
458+ toolResultPayloadCapBytes,
236459 });
237460
238- const turnMessages = sanitizeGeminiToolMessages(
461+ const geminiSanitizedMessages = sanitizeGeminiToolMessages(
239462 messages,
240463 modelString,
241464 toolCount > 0,
242465 );
243- if (turnMessages.length !== messages.length) {
466+ if (geminiSanitizedMessages.length !== messages.length) {
244467 logApiEvent("gemini tool history truncated", {
245468 modelString,
246469 originalMessages: messages.length,
247- sanitizedMessages: turnMessages.length,
470+ sanitizedMessages: geminiSanitizedMessages.length,
471+ });
472+ }
473+
474+ logApiEvent(
475+ "turn context pre-prune",
476+ getMessageDiagnostics(geminiSanitizedMessages),
477+ );
478+ const prunedMessages = applyContextPruning(
479+ geminiSanitizedMessages,
480+ pruningMode,
481+ );
482+ logApiEvent(
483+ "turn context post-prune",
484+ getMessageDiagnostics(prunedMessages),
485+ );
486+
487+ const turnMessages = compactToolResultPayloads(
488+ prunedMessages,
489+ toolResultPayloadCapBytes,
490+ );
491+ if (turnMessages !== prunedMessages) {
492+ logApiEvent("turn context post-compaction", {
493+ capBytes: toolResultPayloadCapBytes,
494+ diagnostics: getMessageDiagnostics(turnMessages),
248495 });
249496 }
250497