|
| 1 | +import type { GCMessage } from "./sdk-types.js"; |
| 2 | + |
| 3 | +// ── Token estimation ────────────────────────────────────────────────── |
| 4 | + |
| 5 | +/** Rough token estimate: 1 token ≈ 4 chars */ |
| 6 | +export function estimateTokens(text: string): number { |
| 7 | + return Math.ceil(text.length / 4); |
| 8 | +} |
| 9 | + |
| 10 | +/** Estimate total tokens across a message array */ |
| 11 | +export function estimateMessageTokens(messages: GCMessage[]): number { |
| 12 | + let total = 0; |
| 13 | + for (const msg of messages) { |
| 14 | + switch (msg.type) { |
| 15 | + case "assistant": |
| 16 | + total += estimateTokens(msg.content) + estimateTokens(msg.thinking ?? ""); |
| 17 | + break; |
| 18 | + case "user": |
| 19 | + total += estimateTokens(msg.content); |
| 20 | + break; |
| 21 | + case "tool_use": |
| 22 | + total += estimateTokens(JSON.stringify(msg.args)) + 50; |
| 23 | + break; |
| 24 | + case "tool_result": |
| 25 | + total += estimateTokens(msg.content); |
| 26 | + break; |
| 27 | + case "delta": |
| 28 | + total += estimateTokens(msg.content); |
| 29 | + break; |
| 30 | + case "system": |
| 31 | + total += estimateTokens(msg.content); |
| 32 | + break; |
| 33 | + } |
| 34 | + } |
| 35 | + return total; |
| 36 | +} |
| 37 | + |
| 38 | +// ── Compaction checks ───────────────────────────────────────────────── |
| 39 | + |
| 40 | +/** Check if messages are approaching context limit and need compaction */ |
| 41 | +export function needsCompaction( |
| 42 | + messages: GCMessage[], |
| 43 | + contextWindow: number = 200000, |
| 44 | +): { needed: boolean; tokenEstimate: number; ratio: number } { |
| 45 | + const tokenEstimate = estimateMessageTokens(messages); |
| 46 | + const ratio = tokenEstimate / contextWindow; |
| 47 | + return { needed: ratio > 0.75, tokenEstimate, ratio }; |
| 48 | +} |
| 49 | + |
| 50 | +// ── Tool result truncation ──────────────────────────────────────────── |
| 51 | + |
| 52 | +/** Truncate oversized tool results, keeping first and last portions */ |
| 53 | +export function truncateToolResults( |
| 54 | + messages: GCMessage[], |
| 55 | + maxCharsPerResult: number = 10000, |
| 56 | +): GCMessage[] { |
| 57 | + return messages.map((msg) => { |
| 58 | + if (msg.type === "tool_result" && msg.content.length > maxCharsPerResult) { |
| 59 | + const half = Math.floor(maxCharsPerResult / 2); |
| 60 | + const truncated = |
| 61 | + msg.content.slice(0, half) + |
| 62 | + `\n\n... [${msg.content.length - maxCharsPerResult} chars truncated] ...\n\n` + |
| 63 | + msg.content.slice(-half); |
| 64 | + return { ...msg, content: truncated }; |
| 65 | + } |
| 66 | + return msg; |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +// ── Conversation summarization ──────────────────────────────────────── |
| 71 | + |
| 72 | +/** |
| 73 | + * Build a text representation of messages for summarization. |
| 74 | + * Strips deltas and system messages, keeps the substantive conversation. |
| 75 | + */ |
| 76 | +export function messagesToText(messages: GCMessage[]): string { |
| 77 | + const parts: string[] = []; |
| 78 | + for (const msg of messages) { |
| 79 | + switch (msg.type) { |
| 80 | + case "assistant": |
| 81 | + parts.push(`Assistant: ${msg.content}`); |
| 82 | + break; |
| 83 | + case "user": |
| 84 | + parts.push(`User: ${msg.content}`); |
| 85 | + break; |
| 86 | + case "tool_use": |
| 87 | + parts.push(`Tool call: ${msg.toolName}(${JSON.stringify(msg.args).slice(0, 200)})`); |
| 88 | + break; |
| 89 | + case "tool_result": |
| 90 | + parts.push(`Tool result [${msg.toolName}]: ${msg.content.slice(0, 500)}`); |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + return parts.join("\n"); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Generate a compaction prompt that can be sent to the model to summarize |
| 99 | + * the conversation so far. The caller runs the actual query. |
| 100 | + */ |
| 101 | +export function buildCompactPrompt(messages: GCMessage[]): string { |
| 102 | + const text = messagesToText(messages); |
| 103 | + if (!text) return ""; |
| 104 | + return ( |
| 105 | + "Summarize this conversation concisely. Preserve key decisions, " + |
| 106 | + "file paths, code changes, and outcomes. Omit tool call details " + |
| 107 | + "unless they failed.\n\n" + |
| 108 | + text |
| 109 | + ); |
| 110 | +} |
0 commit comments