agents 0.10.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { r as Agent } from "./index-BPkkIqMn.js";
1
+ import { r as Agent } from "./index-D2lfljd3.js";
2
2
  import {
3
3
  i as SerializableValue,
4
4
  n as RPCMethod,
@@ -1 +1 @@
1
- {"version":3,"file":"compaction-helpers-BPE1_ziA.js","names":[],"sources":["../src/experimental/memory/utils/tokens.ts","../src/experimental/memory/utils/compaction-helpers.ts"],"sourcesContent":["/**\n * Token Estimation Utilities\n *\n * IMPORTANT: These are heuristic estimates, not actual tokenizer counts.\n *\n * We intentionally avoid real tokenizers (e.g. tiktoken, sentencepiece) because:\n * - A single tiktoken instance costs ~80-120MB of heap\n * - Cloudflare Workers have tight memory limits (128MB)\n * - For compaction thresholds, a conservative estimate is sufficient\n *\n * The hybrid approach (max of character-based and word-based estimates) handles\n * both dense token content (JSON, code) and natural language reasonably well.\n *\n * Calibration notes:\n * - Character-based: ~4 chars per token (conservative, from OpenAI guidance)\n * - Word-based: ~1.3 tokens per word (empirical, from Mastra's memory system)\n * - Per-message overhead: ~4 tokens for role/framing (empirical)\n *\n * These ratios are tuned for English. CJK, emoji-heavy, or highly technical\n * content may have different ratios. The conservative estimates help ensure\n * compaction triggers before context windows are actually exceeded.\n */\n\nimport type { UIMessage } from \"ai\";\n\n/** Approximate characters per token for English text */\nexport const CHARS_PER_TOKEN = 4;\n\n/** Approximate token multiplier per whitespace-separated word */\nexport const WORDS_TOKEN_MULTIPLIER = 1.3;\n\n/** Approximate overhead tokens per message (role, framing) */\nexport const TOKENS_PER_MESSAGE = 4;\n\n/**\n * Estimate token count for a string using a hybrid heuristic.\n *\n * Takes the max of two estimates:\n * - Character-based: `length / 4` — better for dense content (JSON, code, URLs)\n * - Word-based: `words * 1.3` — better for natural language prose\n *\n * This is a heuristic. Do not use where exact counts are required.\n */\nexport function estimateStringTokens(text: string): number {\n if (!text) return 0;\n const charEstimate = text.length / CHARS_PER_TOKEN;\n const wordEstimate =\n text.split(/\\s+/).filter(Boolean).length * WORDS_TOKEN_MULTIPLIER;\n return Math.ceil(Math.max(charEstimate, wordEstimate));\n}\n\n/**\n * Estimate total token count for an array of UIMessages.\n *\n * Walks each message's parts (text, tool invocations, tool results)\n * and applies per-message overhead.\n *\n * This is a heuristic. Do not use where exact counts are required.\n */\nexport function estimateMessageTokens(messages: UIMessage[]): number {\n let tokens = 0;\n for (const msg of messages) {\n tokens += TOKENS_PER_MESSAGE;\n for (const part of msg.parts) {\n if (part.type === \"text\") {\n tokens += estimateStringTokens(\n (part as { type: \"text\"; text: string }).text\n );\n } else if (\n part.type.startsWith(\"tool-\") ||\n part.type === \"dynamic-tool\"\n ) {\n const toolPart = part as { input?: unknown; output?: unknown };\n if (toolPart.input) {\n tokens += estimateStringTokens(JSON.stringify(toolPart.input));\n }\n if (toolPart.output) {\n tokens += estimateStringTokens(JSON.stringify(toolPart.output));\n }\n }\n }\n }\n return tokens;\n}\n","/**\n * Compaction Helpers\n *\n * Utilities for full compaction (LLM-based summarization).\n * Used by the reference compaction implementation and available\n * for custom CompactFunction implementations.\n */\n\nimport type { UIMessage } from \"ai\";\nimport { estimateMessageTokens } from \"./tokens\";\n\n// ── Compaction ID constants ─────────────────────────────────────────\n\n/** Prefix for all compaction messages (overlays and summaries) */\nexport const COMPACTION_PREFIX = \"compaction_\";\n\n/** Check if a message is a compaction message */\nexport function isCompactionMessage(msg: UIMessage): boolean {\n return msg.id.startsWith(COMPACTION_PREFIX);\n}\n\n// ── Tool Pair Alignment ──────────────────────────────────────────────\n\n/**\n * Check if a message contains tool invocations.\n */\nfunction hasToolCalls(msg: UIMessage): boolean {\n return msg.parts.some(\n (p) => p.type.startsWith(\"tool-\") || p.type === \"dynamic-tool\"\n );\n}\n\n/**\n * Get tool call IDs from a message's parts.\n */\nfunction getToolCallIds(msg: UIMessage): Set<string> {\n const ids = new Set<string>();\n for (const part of msg.parts) {\n if (\n (part.type.startsWith(\"tool-\") || part.type === \"dynamic-tool\") &&\n \"toolCallId\" in part\n ) {\n ids.add((part as { toolCallId: string }).toolCallId);\n }\n }\n return ids;\n}\n\n/**\n * Check if a message is a tool result referencing a specific call ID.\n */\nfunction isToolResultFor(msg: UIMessage, callIds: Set<string>): boolean {\n return msg.parts.some(\n (p) =>\n (p.type.startsWith(\"tool-\") || p.type === \"dynamic-tool\") &&\n \"toolCallId\" in p &&\n callIds.has((p as { toolCallId: string }).toolCallId)\n );\n}\n\n/**\n * Align a boundary index forward to avoid splitting tool call/result groups.\n * If the boundary falls between an assistant message with tool calls and its\n * tool results, move it forward past the results.\n */\nexport function alignBoundaryForward(\n messages: UIMessage[],\n idx: number\n): number {\n if (idx <= 0 || idx >= messages.length) return idx;\n\n // Check if the message before the boundary has tool calls\n const prev = messages[idx - 1];\n if (prev.role === \"assistant\" && hasToolCalls(prev)) {\n const callIds = getToolCallIds(prev);\n // Skip forward past any tool results for these calls\n while (idx < messages.length && isToolResultFor(messages[idx], callIds)) {\n idx++;\n }\n }\n\n return idx;\n}\n\n/**\n * Align a boundary index backward to avoid splitting tool call/result groups.\n * If the boundary falls in the middle of tool results, move it backward to\n * include the assistant message that made the calls.\n */\nexport function alignBoundaryBackward(\n messages: UIMessage[],\n idx: number\n): number {\n if (idx <= 0 || idx >= messages.length) return idx;\n\n // If the message at idx is a tool result, walk backward to find the call\n while (idx > 0) {\n const msg = messages[idx];\n if (msg.role === \"assistant\" && hasToolCalls(msg)) {\n break; // This is a tool call message — include it\n }\n // Check if this looks like a tool result (assistant message following another)\n const prev = messages[idx - 1];\n if (prev.role === \"assistant\" && hasToolCalls(prev)) {\n const callIds = getToolCallIds(prev);\n if (isToolResultFor(msg, callIds)) {\n idx--; // Move back to include the call\n continue;\n }\n }\n break;\n }\n\n return idx;\n}\n\n// ── Token-Budget Tail Protection ─────────────────────────────────────\n\n/**\n * Find the compression end boundary using a token budget for the tail.\n * Walks backward from the end, accumulating tokens until budget is reached.\n * Returns the index where compression should stop (everything from this\n * index onward is protected).\n *\n * @param messages All messages\n * @param headEnd Index where the protected head ends (compression starts here)\n * @param tailTokenBudget Maximum tokens to keep in the tail\n * @param minTailMessages Minimum messages to protect in the tail (fallback)\n */\nexport function findTailCutByTokens(\n messages: UIMessage[],\n headEnd: number,\n tailTokenBudget = 20000,\n minTailMessages = 2\n): number {\n const n = messages.length;\n let accumulated = 0;\n let tokenCut = n;\n\n for (let i = n - 1; i >= headEnd; i--) {\n const msgTokens = estimateMessageTokens([messages[i]]);\n\n if (accumulated + msgTokens > tailTokenBudget && tokenCut < n) {\n // Budget exceeded and we already have at least one tail message\n break;\n }\n accumulated += msgTokens;\n tokenCut = i;\n }\n\n // Protect whichever is larger: token-based tail or minTailMessages\n const minCut = n - minTailMessages;\n const cutIdx = minCut >= headEnd ? Math.min(tokenCut, minCut) : tokenCut;\n\n // Align to avoid splitting tool groups\n return alignBoundaryBackward(messages, cutIdx);\n}\n\n// ── Tool Pair Sanitization ───────────────────────────────────────────\n\n/**\n * Fix orphaned tool call/result pairs after compaction.\n *\n * Two failure modes:\n * 1. Tool result references a call_id whose assistant tool_call was removed\n * → Remove the orphaned result\n * 2. Assistant has tool_calls whose results were dropped\n * → Add stub results so the API doesn't error\n *\n * @param messages Messages after compaction\n * @returns Sanitized messages with no orphaned pairs\n */\nexport function sanitizeToolPairs(messages: UIMessage[]): UIMessage[] {\n // Build set of surviving tool call IDs (from assistant messages)\n const survivingCallIds = new Set<string>();\n for (const msg of messages) {\n if (msg.role === \"assistant\") {\n for (const id of getToolCallIds(msg)) {\n survivingCallIds.add(id);\n }\n }\n }\n\n // Build set of tool result IDs\n const resultCallIds = new Set<string>();\n for (const msg of messages) {\n for (const part of msg.parts) {\n if (\n (part.type.startsWith(\"tool-\") || part.type === \"dynamic-tool\") &&\n \"toolCallId\" in part &&\n \"output\" in part\n ) {\n resultCallIds.add((part as { toolCallId: string }).toolCallId);\n }\n }\n }\n\n // Remove orphaned results (results whose calls were dropped)\n const orphanedResults = new Set<string>();\n for (const id of resultCallIds) {\n if (!survivingCallIds.has(id)) {\n orphanedResults.add(id);\n }\n }\n\n let result = messages;\n if (orphanedResults.size > 0) {\n result = result.map((msg) => {\n const filteredParts = msg.parts.filter((part) => {\n if (\n (part.type.startsWith(\"tool-\") || part.type === \"dynamic-tool\") &&\n \"toolCallId\" in part &&\n \"output\" in part\n ) {\n return !orphanedResults.has(\n (part as { toolCallId: string }).toolCallId\n );\n }\n return true;\n });\n if (filteredParts.length !== msg.parts.length) {\n return { ...msg, parts: filteredParts } as UIMessage;\n }\n return msg;\n });\n }\n\n // Add stub results for calls whose results were dropped\n const missingResults = new Set<string>();\n for (const id of survivingCallIds) {\n if (!resultCallIds.has(id) && !orphanedResults.has(id)) {\n missingResults.add(id);\n }\n }\n\n if (missingResults.size > 0) {\n const patched: UIMessage[] = [];\n for (const msg of result) {\n patched.push(msg);\n if (msg.role === \"assistant\") {\n for (const id of getToolCallIds(msg)) {\n if (missingResults.has(id)) {\n // Find the tool name from the call\n const callPart = msg.parts.find(\n (p) =>\n \"toolCallId\" in p &&\n (p as { toolCallId: string }).toolCallId === id\n ) as { toolName?: string } | undefined;\n\n patched.push({\n id: `stub-${id}`,\n role: \"assistant\",\n parts: [\n {\n type: \"tool-result\" as const,\n toolCallId: id,\n toolName: callPart?.toolName ?? \"unknown\",\n result:\n \"[Result from earlier conversation — see context summary above]\"\n } as unknown as UIMessage[\"parts\"][number]\n ],\n createdAt: new Date()\n } as UIMessage);\n }\n }\n }\n }\n result = patched;\n }\n\n // Remove empty messages (all parts filtered out)\n return result.filter((msg) => msg.parts.length > 0);\n}\n\n// ── Summary Budget ───────────────────────────────────────────────────\n\n/**\n * Compute a summary token budget based on the content being compressed.\n * 20% of the compressed content, clamped to 2K-8K tokens.\n */\nexport function computeSummaryBudget(messages: UIMessage[]): number {\n const contentTokens = estimateMessageTokens(messages);\n // Summary is ~20% of the content being compressed.\n // The summary replaces the compressed middle, so it's sized relative\n // to what it's replacing — not the tail budget (they occupy different\n // slots in the context window).\n const budget = Math.floor(contentTokens * 0.2);\n return Math.max(100, budget);\n}\n\n// ── Structured Summary Prompt ────────────────────────────────────────\n\n/**\n * Build a prompt for LLM summarization of compressed messages.\n *\n * @param messages Messages to summarize\n * @param previousSummary Previous summary for iterative updates (or null for first compaction)\n * @param budget Target token count for the summary\n */\nexport function buildSummaryPrompt(\n messages: UIMessage[],\n previousSummary: string | null,\n budget: number\n): string {\n const content = messages\n .map((msg) => {\n const textParts = msg.parts\n .filter((p) => p.type === \"text\")\n .map((p) => (p as { text: string }).text)\n .join(\"\\n\");\n\n const toolParts = msg.parts\n .filter((p) => p.type.startsWith(\"tool-\") || p.type === \"dynamic-tool\")\n .map((p) => {\n const tp = p as {\n toolName?: string;\n input?: unknown;\n output?: unknown;\n };\n const parts = [`[Tool: ${tp.toolName ?? \"unknown\"}]`];\n if (tp.input)\n parts.push(`Input: ${JSON.stringify(tp.input).slice(0, 500)}`);\n if (tp.output)\n parts.push(`Output: ${String(tp.output).slice(0, 500)}`);\n return parts.join(\"\\n\");\n })\n .join(\"\\n\");\n\n return `[${msg.role}]\\n${textParts}${toolParts ? \"\\n\" + toolParts : \"\"}`;\n })\n .join(\"\\n\\n---\\n\\n\");\n\n if (previousSummary) {\n return `You are updating a conversation summary. A previous summary exists below. New conversation turns have occurred since then and need to be incorporated.\n\nPREVIOUS SUMMARY:\n${previousSummary}\n\nNEW TURNS TO INCORPORATE:\n${content}\n\nUpdate the summary. PRESERVE existing information that is still relevant. ADD new information. Remove information only if it is clearly obsolete.\n\n## Topic\n[What the conversation is about]\n\n## Key Points\n[Important information, decisions, and conclusions from the conversation]\n\n## Current State\n[Where things stand now — what has been done, what is in progress]\n\n## Open Items\n[Unresolved questions, pending tasks, or next steps discussed]\n\nTarget ~${budget} tokens. Be factual — only include information that was explicitly discussed in the conversation. Do NOT invent file paths, commands, or details that were not mentioned. Write only the summary body.`;\n }\n\n return `Create a concise summary of this conversation that preserves the important information for future context.\n\nCONVERSATION TO SUMMARIZE:\n${content}\n\nUse this structure:\n\n## Topic\n[What the conversation is about]\n\n## Key Points\n[Important information, decisions, and conclusions from the conversation]\n\n## Current State\n[Where things stand now — what has been done, what is in progress]\n\n## Open Items\n[Unresolved questions, pending tasks, or next steps discussed]\n\nTarget ~${budget} tokens. Be factual — only include information that was explicitly discussed in the conversation. Do NOT invent file paths, commands, or details that were not mentioned. Write only the summary body.`;\n}\n\n// ── Reference Compaction Implementation ──────────────────────────────\n\n/**\n * Result of a compaction function — describes the overlay to store.\n */\nexport interface CompactResult {\n /** First message ID in the compacted range */\n fromMessageId: string;\n /** Last message ID in the compacted range */\n toMessageId: string;\n /** Summary text to store as the overlay */\n summary: string;\n}\n\nexport interface CompactOptions {\n /**\n * Function to call the LLM for summarization.\n * Takes a user prompt string, returns the LLM's text response.\n */\n summarize: (prompt: string) => Promise<string>;\n\n /** Number of head messages to protect (default: 2) */\n protectHead?: number;\n\n /** Token budget for tail protection (default: 20000) */\n tailTokenBudget?: number;\n\n /** Minimum tail messages to protect (default: 2) */\n minTailMessages?: number;\n}\n\n/**\n * Reference compaction implementation.\n *\n * Implements the full hermes-style compaction algorithm:\n * 1. Protect head messages (first N)\n * 2. Protect tail by token budget (walk backward)\n * 3. Align boundaries to tool call groups\n * 4. Summarize middle section with LLM (structured format)\n * 5. Sanitize orphaned tool pairs\n * 6. Iterative summary updates on subsequent compactions\n *\n * @example\n * ```typescript\n * import { createCompactFunction } from \"agents/experimental/memory/utils\";\n *\n * const session = new Session(provider, {\n * compaction: {\n * tokenThreshold: 100000,\n * fn: createCompactFunction({\n * summarize: (prompt) => generateText({ model, prompt }).then(r => r.text)\n * })\n * }\n * });\n * ```\n */\nexport function createCompactFunction(opts: CompactOptions) {\n const protectHead = opts.protectHead ?? 3;\n const tailTokenBudget = opts.tailTokenBudget ?? 20000;\n const minTailMessages = opts.minTailMessages ?? 2;\n\n return async (messages: UIMessage[]): Promise<CompactResult | null> => {\n if (messages.length <= protectHead + minTailMessages) {\n return null;\n }\n\n // 1. Find compression boundaries\n let compressStart = protectHead;\n compressStart = alignBoundaryForward(messages, compressStart);\n\n let compressEnd = findTailCutByTokens(\n messages,\n compressStart,\n tailTokenBudget,\n minTailMessages\n );\n\n if (compressEnd <= compressStart) {\n return null;\n }\n\n // Filter out compaction overlay messages — they have virtual IDs\n // and should not be included in the summary prompt or used as range IDs\n const middleMessages = messages\n .slice(compressStart, compressEnd)\n .filter((m) => !isCompactionMessage(m));\n\n if (middleMessages.length === 0) return null;\n\n // 2. Generate summary — extract previous summary from compaction overlays\n const existingCompaction = messages.find(isCompactionMessage);\n const previousSummary = existingCompaction\n ? existingCompaction.parts\n .filter((p) => p.type === \"text\")\n .map((p) => (p as { text: string }).text)\n .join(\"\\n\")\n : null;\n\n const budget = computeSummaryBudget(middleMessages);\n const prompt = buildSummaryPrompt(middleMessages, previousSummary, budget);\n const summary = await opts.summarize(prompt);\n\n if (!summary.trim()) return null;\n\n return {\n fromMessageId: middleMessages[0].id,\n toMessageId: middleMessages[middleMessages.length - 1].id,\n summary\n };\n };\n}\n"],"mappings":";;AA0BA,MAAa,kBAAkB;;AAG/B,MAAa,yBAAyB;;AAGtC,MAAa,qBAAqB;;;;;;;;;;AAWlC,SAAgB,qBAAqB,MAAsB;AACzD,KAAI,CAAC,KAAM,QAAO;CAClB,MAAM,eAAe,KAAK,SAAA;CAC1B,MAAM,eACJ,KAAK,MAAM,MAAM,CAAC,OAAO,QAAQ,CAAC,SAAS;AAC7C,QAAO,KAAK,KAAK,KAAK,IAAI,cAAc,aAAa,CAAC;;;;;;;;;;AAWxD,SAAgB,sBAAsB,UAA+B;CACnE,IAAI,SAAS;AACb,MAAK,MAAM,OAAO,UAAU;AAC1B,YAAA;AACA,OAAK,MAAM,QAAQ,IAAI,MACrB,KAAI,KAAK,SAAS,OAChB,WAAU,qBACP,KAAwC,KAC1C;WAED,KAAK,KAAK,WAA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mB,gBARV,qBACpB,mBAAmB,MAChB,QAAQ,MAAM,EAAE,SAAS,OAAO,CAChC,KAAK,MAAO,EAAuB,KAAK,CACxC,KAAK,KAAK,GACb,MAEW,qBAAqB,eAAe,CACuB;EAC1E,MAAM,UAAU,MAAM,KAAK,UAAU,OAAO;AAE5C,MAAI,CAAC,QAAQ,MAAM,CAAE,QAAO;AAE5B,SAAO;GACL,eAAe,eAAe,GAAG;GACjC,aAAa,eAAe,eAAe,SAAS,GAAG;GACvD;GACD"}
1
+ {"version":3,"file":"compaction-helpers-BPE1_ziA.js","names":[],"sources":["../src/experimental/memory/utils/tokens.ts","../src/experimental/memory/utils/compaction-helpers.ts"],"sourcesContent":["/**\n * Token Estimation Utilities\n *\n * IMPORTANT: These are heuristic estimates, not actual tokenizer counts.\n *\n * We intentionally avoid real tokenizers (e.g. tiktoken, sentencepiece) because:\n * - A single tiktoken instance costs ~80-120MB of heap\n * - Cloudflare Workers have tight memory limits (128MB)\n * - For compaction thresholds, a conservative estimate is sufficient\n *\n * The hybrid approach (max of character-based and word-based estimates) handles\n * both dense token content (JSON, code) and natural language reasonably well.\n *\n * Calibration notes:\n * - Character-based: ~4 chars per token (conservative, from OpenAI guidance)\n * - Word-based: ~1.3 tokens per word (empirical, from Mastra's memory system)\n * - Per-message overhead: ~4 tokens for role/framing (empirical)\n *\n * These ratios are tuned for English. CJK, emoji-heavy, or highly technical\n * content may have different ratios. The conservative estimates help ensure\n * compaction triggers before context windows are actually exceeded.\n */\n\nimport type { SessionMessage } from \"../session/types\";\n\n/** Approximate characters per token for English text */\nexport const CHARS_PER_TOKEN = 4;\n\n/** Approximate token multiplier per whitespace-separated word */\nexport const WORDS_TOKEN_MULTIPLIER = 1.3;\n\n/** Approximate overhead tokens per message (role, framing) */\nexport const TOKENS_PER_MESSAGE = 4;\n\n/**\n * Estimate token count for a string using a hybrid heuristic.\n *\n * Takes the max of two estimates:\n * - Character-based: `length / 4` — better for dense content (JSON, code, URLs)\n * - Word-based: `words * 1.3` — better for natural language prose\n *\n * This is a heuristic. Do not use where exact counts are required.\n */\nexport function estimateStringTokens(text: string): number {\n if (!text) return 0;\n const charEstimate = text.length / CHARS_PER_TOKEN;\n const wordEstimate =\n text.split(/\\s+/).filter(Boolean).length * WORDS_TOKEN_MULTIPLIER;\n return Math.ceil(Math.max(charEstimate, wordEstimate));\n}\n\n/**\n * Estimate total token count for an array of UIMessages.\n *\n * Walks each message's parts (text, tool invocations, tool results)\n * and applies per-message overhead.\n *\n * This is a heuristic. Do not use where exact counts are required.\n */\nexport function estimateMessageTokens(messages: SessionMessage[]): number {\n let tokens = 0;\n for (const msg of messages) {\n tokens += TOKENS_PER_MESSAGE;\n for (const part of msg.parts) {\n if (part.type === \"text\") {\n tokens += estimateStringTokens(\n (part as { type: \"text\"; text: string }).text\n );\n } else if (\n part.type.startsWith(\"tool-\") ||\n part.type === \"dynamic-tool\"\n ) {\n const toolPart = part as { input?: unknown; output?: unknown };\n if (toolPart.input) {\n tokens += estimateStringTokens(JSON.stringify(toolPart.input));\n }\n if (toolPart.output) {\n tokens += estimateStringTokens(JSON.stringify(toolPart.output));\n }\n }\n }\n }\n return tokens;\n}\n","/**\n * Compaction Helpers\n *\n * Utilities for full compaction (LLM-based summarization).\n * Used by the reference compaction implementation and available\n * for custom CompactFunction implementations.\n */\n\nimport type { SessionMessage } from \"../session/types\";\nimport { estimateMessageTokens } from \"./tokens\";\n\n// ── Compaction ID constants ─────────────────────────────────────────\n\n/** Prefix for all compaction messages (overlays and summaries) */\nexport const COMPACTION_PREFIX = \"compaction_\";\n\n/** Check if a message is a compaction message */\nexport function isCompactionMessage(msg: SessionMessage): boolean {\n return msg.id.startsWith(COMPACTION_PREFIX);\n}\n\n// ── Tool Pair Alignment ──────────────────────────────────────────────\n\n/**\n * Check if a message contains tool invocations.\n */\nfunction hasToolCalls(msg: SessionMessage): boolean {\n return msg.parts.some(\n (p) => p.type.startsWith(\"tool-\") || p.type === \"dynamic-tool\"\n );\n}\n\n/**\n * Get tool call IDs from a message's parts.\n */\nfunction getToolCallIds(msg: SessionMessage): Set<string> {\n const ids = new Set<string>();\n for (const part of msg.parts) {\n if (\n (part.type.startsWith(\"tool-\") || part.type === \"dynamic-tool\") &&\n \"toolCallId\" in part\n ) {\n ids.add((part as { toolCallId: string }).toolCallId);\n }\n }\n return ids;\n}\n\n/**\n * Check if a message is a tool result referencing a specific call ID.\n */\nfunction isToolResultFor(msg: SessionMessage, callIds: Set<string>): boolean {\n return msg.parts.some(\n (p) =>\n (p.type.startsWith(\"tool-\") || p.type === \"dynamic-tool\") &&\n \"toolCallId\" in p &&\n callIds.has((p as { toolCallId: string }).toolCallId)\n );\n}\n\n/**\n * Align a boundary index forward to avoid splitting tool call/result groups.\n * If the boundary falls between an assistant message with tool calls and its\n * tool results, move it forward past the results.\n */\nexport function alignBoundaryForward(\n messages: SessionMessage[],\n idx: number\n): number {\n if (idx <= 0 || idx >= messages.length) return idx;\n\n // Check if the message before the boundary has tool calls\n const prev = messages[idx - 1];\n if (prev.role === \"assistant\" && hasToolCalls(prev)) {\n const callIds = getToolCallIds(prev);\n // Skip forward past any tool results for these calls\n while (idx < messages.length && isToolResultFor(messages[idx], callIds)) {\n idx++;\n }\n }\n\n return idx;\n}\n\n/**\n * Align a boundary index backward to avoid splitting tool call/result groups.\n * If the boundary falls in the middle of tool results, move it backward to\n * include the assistant message that made the calls.\n */\nexport function alignBoundaryBackward(\n messages: SessionMessage[],\n idx: number\n): number {\n if (idx <= 0 || idx >= messages.length) return idx;\n\n // If the message at idx is a tool result, walk backward to find the call\n while (idx > 0) {\n const msg = messages[idx];\n if (msg.role === \"assistant\" && hasToolCalls(msg)) {\n break; // This is a tool call message — include it\n }\n // Check if this looks like a tool result (assistant message following another)\n const prev = messages[idx - 1];\n if (prev.role === \"assistant\" && hasToolCalls(prev)) {\n const callIds = getToolCallIds(prev);\n if (isToolResultFor(msg, callIds)) {\n idx--; // Move back to include the call\n continue;\n }\n }\n break;\n }\n\n return idx;\n}\n\n// ── Token-Budget Tail Protection ─────────────────────────────────────\n\n/**\n * Find the compression end boundary using a token budget for the tail.\n * Walks backward from the end, accumulating tokens until budget is reached.\n * Returns the index where compression should stop (everything from this\n * index onward is protected).\n *\n * @param messages All messages\n * @param headEnd Index where the protected head ends (compression starts here)\n * @param tailTokenBudget Maximum tokens to keep in the tail\n * @param minTailMessages Minimum messages to protect in the tail (fallback)\n */\nexport function findTailCutByTokens(\n messages: SessionMessage[],\n headEnd: number,\n tailTokenBudget = 20000,\n minTailMessages = 2\n): number {\n const n = messages.length;\n let accumulated = 0;\n let tokenCut = n;\n\n for (let i = n - 1; i >= headEnd; i--) {\n const msgTokens = estimateMessageTokens([messages[i]]);\n\n if (accumulated + msgTokens > tailTokenBudget && tokenCut < n) {\n // Budget exceeded and we already have at least one tail message\n break;\n }\n accumulated += msgTokens;\n tokenCut = i;\n }\n\n // Protect whichever is larger: token-based tail or minTailMessages\n const minCut = n - minTailMessages;\n const cutIdx = minCut >= headEnd ? Math.min(tokenCut, minCut) : tokenCut;\n\n // Align to avoid splitting tool groups\n return alignBoundaryBackward(messages, cutIdx);\n}\n\n// ── Tool Pair Sanitization ───────────────────────────────────────────\n\n/**\n * Fix orphaned tool call/result pairs after compaction.\n *\n * Two failure modes:\n * 1. Tool result references a call_id whose assistant tool_call was removed\n * → Remove the orphaned result\n * 2. Assistant has tool_calls whose results were dropped\n * → Add stub results so the API doesn't error\n *\n * @param messages Messages after compaction\n * @returns Sanitized messages with no orphaned pairs\n */\nexport function sanitizeToolPairs(\n messages: SessionMessage[]\n): SessionMessage[] {\n // Build set of surviving tool call IDs (from assistant messages)\n const survivingCallIds = new Set<string>();\n for (const msg of messages) {\n if (msg.role === \"assistant\") {\n for (const id of getToolCallIds(msg)) {\n survivingCallIds.add(id);\n }\n }\n }\n\n // Build set of tool result IDs\n const resultCallIds = new Set<string>();\n for (const msg of messages) {\n for (const part of msg.parts) {\n if (\n (part.type.startsWith(\"tool-\") || part.type === \"dynamic-tool\") &&\n \"toolCallId\" in part &&\n \"output\" in part\n ) {\n resultCallIds.add((part as { toolCallId: string }).toolCallId);\n }\n }\n }\n\n // Remove orphaned results (results whose calls were dropped)\n const orphanedResults = new Set<string>();\n for (const id of resultCallIds) {\n if (!survivingCallIds.has(id)) {\n orphanedResults.add(id);\n }\n }\n\n let result = messages;\n if (orphanedResults.size > 0) {\n result = result.map((msg) => {\n const filteredParts = msg.parts.filter((part) => {\n if (\n (part.type.startsWith(\"tool-\") || part.type === \"dynamic-tool\") &&\n \"toolCallId\" in part &&\n \"output\" in part\n ) {\n return !orphanedResults.has(\n (part as { toolCallId: string }).toolCallId\n );\n }\n return true;\n });\n if (filteredParts.length !== msg.parts.length) {\n return { ...msg, parts: filteredParts } as SessionMessage;\n }\n return msg;\n });\n }\n\n // Add stub results for calls whose results were dropped\n const missingResults = new Set<string>();\n for (const id of survivingCallIds) {\n if (!resultCallIds.has(id) && !orphanedResults.has(id)) {\n missingResults.add(id);\n }\n }\n\n if (missingResults.size > 0) {\n const patched: SessionMessage[] = [];\n for (const msg of result) {\n patched.push(msg);\n if (msg.role === \"assistant\") {\n for (const id of getToolCallIds(msg)) {\n if (missingResults.has(id)) {\n // Find the tool name from the call\n const callPart = msg.parts.find(\n (p) =>\n \"toolCallId\" in p &&\n (p as { toolCallId: string }).toolCallId === id\n ) as { toolName?: string } | undefined;\n\n patched.push({\n id: `stub-${id}`,\n role: \"assistant\",\n parts: [\n {\n type: \"tool-result\" as const,\n toolCallId: id,\n toolName: callPart?.toolName ?? \"unknown\",\n result:\n \"[Result from earlier conversation — see context summary above]\"\n } as unknown as SessionMessage[\"parts\"][number]\n ],\n createdAt: new Date()\n } as SessionMessage);\n }\n }\n }\n }\n result = patched;\n }\n\n // Remove empty messages (all parts filtered out)\n return result.filter((msg) => msg.parts.length > 0);\n}\n\n// ── Summary Budget ───────────────────────────────────────────────────\n\n/**\n * Compute a summary token budget based on the content being compressed.\n * 20% of the compressed content, clamped to 2K-8K tokens.\n */\nexport function computeSummaryBudget(messages: SessionMessage[]): number {\n const contentTokens = estimateMessageTokens(messages);\n // Summary is ~20% of the content being compressed.\n // The summary replaces the compressed middle, so it's sized relative\n // to what it's replacing — not the tail budget (they occupy different\n // slots in the context window).\n const budget = Math.floor(contentTokens * 0.2);\n return Math.max(100, budget);\n}\n\n// ── Structured Summary Prompt ────────────────────────────────────────\n\n/**\n * Build a prompt for LLM summarization of compressed messages.\n *\n * @param messages Messages to summarize\n * @param previousSummary Previous summary for iterative updates (or null for first compaction)\n * @param budget Target token count for the summary\n */\nexport function buildSummaryPrompt(\n messages: SessionMessage[],\n previousSummary: string | null,\n budget: number\n): string {\n const content = messages\n .map((msg) => {\n const textParts = msg.parts\n .filter((p) => p.type === \"text\")\n .map((p) => (p as { text: string }).text)\n .join(\"\\n\");\n\n const toolParts = msg.parts\n .filter((p) => p.type.startsWith(\"tool-\") || p.type === \"dynamic-tool\")\n .map((p) => {\n const tp = p as {\n toolName?: string;\n input?: unknown;\n output?: unknown;\n };\n const parts = [`[Tool: ${tp.toolName ?? \"unknown\"}]`];\n if (tp.input)\n parts.push(`Input: ${JSON.stringify(tp.input).slice(0, 500)}`);\n if (tp.output)\n parts.push(`Output: ${String(tp.output).slice(0, 500)}`);\n return parts.join(\"\\n\");\n })\n .join(\"\\n\");\n\n return `[${msg.role}]\\n${textParts}${toolParts ? \"\\n\" + toolParts : \"\"}`;\n })\n .join(\"\\n\\n---\\n\\n\");\n\n if (previousSummary) {\n return `You are updating a conversation summary. A previous summary exists below. New conversation turns have occurred since then and need to be incorporated.\n\nPREVIOUS SUMMARY:\n${previousSummary}\n\nNEW TURNS TO INCORPORATE:\n${content}\n\nUpdate the summary. PRESERVE existing information that is still relevant. ADD new information. Remove information only if it is clearly obsolete.\n\n## Topic\n[What the conversation is about]\n\n## Key Points\n[Important information, decisions, and conclusions from the conversation]\n\n## Current State\n[Where things stand now — what has been done, what is in progress]\n\n## Open Items\n[Unresolved questions, pending tasks, or next steps discussed]\n\nTarget ~${budget} tokens. Be factual — only include information that was explicitly discussed in the conversation. Do NOT invent file paths, commands, or details that were not mentioned. Write only the summary body.`;\n }\n\n return `Create a concise summary of this conversation that preserves the important information for future context.\n\nCONVERSATION TO SUMMARIZE:\n${content}\n\nUse this structure:\n\n## Topic\n[What the conversation is about]\n\n## Key Points\n[Important information, decisions, and conclusions from the conversation]\n\n## Current State\n[Where things stand now — what has been done, what is in progress]\n\n## Open Items\n[Unresolved questions, pending tasks, or next steps discussed]\n\nTarget ~${budget} tokens. Be factual — only include information that was explicitly discussed in the conversation. Do NOT invent file paths, commands, or details that were not mentioned. Write only the summary body.`;\n}\n\n// ── Reference Compaction Implementation ──────────────────────────────\n\n/**\n * Result of a compaction function — describes the overlay to store.\n */\nexport interface CompactResult {\n /** First message ID in the compacted range */\n fromMessageId: string;\n /** Last message ID in the compacted range */\n toMessageId: string;\n /** Summary text to store as the overlay */\n summary: string;\n}\n\nexport interface CompactOptions {\n /**\n * Function to call the LLM for summarization.\n * Takes a user prompt string, returns the LLM's text response.\n */\n summarize: (prompt: string) => Promise<string>;\n\n /** Number of head messages to protect (default: 2) */\n protectHead?: number;\n\n /** Token budget for tail protection (default: 20000) */\n tailTokenBudget?: number;\n\n /** Minimum tail messages to protect (default: 2) */\n minTailMessages?: number;\n}\n\n/**\n * Reference compaction implementation.\n *\n * Implements the full hermes-style compaction algorithm:\n * 1. Protect head messages (first N)\n * 2. Protect tail by token budget (walk backward)\n * 3. Align boundaries to tool call groups\n * 4. Summarize middle section with LLM (structured format)\n * 5. Sanitize orphaned tool pairs\n * 6. Iterative summary updates on subsequent compactions\n *\n * @example\n * ```typescript\n * import { createCompactFunction } from \"agents/experimental/memory/utils\";\n *\n * const session = new Session(provider, {\n * compaction: {\n * tokenThreshold: 100000,\n * fn: createCompactFunction({\n * summarize: (prompt) => generateText({ model, prompt }).then(r => r.text)\n * })\n * }\n * });\n * ```\n */\nexport function createCompactFunction(opts: CompactOptions) {\n const protectHead = opts.protectHead ?? 3;\n const tailTokenBudget = opts.tailTokenBudget ?? 20000;\n const minTailMessages = opts.minTailMessages ?? 2;\n\n return async (messages: SessionMessage[]): Promise<CompactResult | null> => {\n if (messages.length <= protectHead + minTailMessages) {\n return null;\n }\n\n // 1. Find compression boundaries\n let compressStart = protectHead;\n compressStart = alignBoundaryForward(messages, compressStart);\n\n let compressEnd = findTailCutByTokens(\n messages,\n compressStart,\n tailTokenBudget,\n minTailMessages\n );\n\n if (compressEnd <= compressStart) {\n return null;\n }\n\n // Filter out compaction overlay messages — they have virtual IDs\n // and should not be included in the summary prompt or used as range IDs\n const middleMessages = messages\n .slice(compressStart, compressEnd)\n .filter((m) => !isCompactionMessage(m));\n\n if (middleMessages.length === 0) return null;\n\n // 2. Generate summary — extract previous summary from compaction overlays\n const existingCompaction = messages.find(isCompactionMessage);\n const previousSummary = existingCompaction\n ? existingCompaction.parts\n .filter((p) => p.type === \"text\")\n .map((p) => (p as { text: string }).text)\n .join(\"\\n\")\n : null;\n\n const budget = computeSummaryBudget(middleMessages);\n const prompt = buildSummaryPrompt(middleMessages, previousSummary, budget);\n const summary = await opts.summarize(prompt);\n\n if (!summary.trim()) return null;\n\n return {\n fromMessageId: middleMessages[0].id,\n toMessageId: middleMessages[middleMessages.length - 1].id,\n summary\n };\n };\n}\n"],"mappings":";;AA0BA,MAAa,kBAAkB;;AAG/B,MAAa,yBAAyB;;AAGtC,MAAa,qBAAqB;;;;;;;;;;AAWlC,SAAgB,qBAAqB,MAAsB;AACzD,KAAI,CAAC,KAAM,QAAO;CAClB,MAAM,eAAe,KAAK,SAAA;CAC1B,MAAM,eACJ,KAAK,MAAM,MAAM,CAAC,OAAO,QAAQ,CAAC,SAAS;AAC7C,QAAO,KAAK,KAAK,KAAK,IAAI,cAAc,aAAa,CAAC;;;;;;;;;;AAWxD,SAAgB,sBAAsB,UAAoC;CACxE,IAAI,SAAS;AACb,MAAK,MAAM,OAAO,UAAU;AAC1B,YAAA;AACA,OAAK,MAAM,QAAQ,IAA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G,QAAO;EAGxC,MAAM,qBAAqB,SAAS,KAAK,oBAAoB;EAS7D,MAAM,SAAS,mBAAmB,gBARV,qBACpB,mBAAmB,MAChB,QAAQ,MAAM,EAAE,SAAS,OAAO,CAChC,KAAK,MAAO,EAAuB,KAAK,CACxC,KAAK,KAAK,GACb,MAEW,qBAAqB,eAAe,CACuB;EAC1E,MAAM,UAAU,MAAM,KAAK,UAAU,OAAO;AAE5C,MAAI,CAAC,QAAQ,MAAM,CAAE,QAAO;AAE5B,SAAO;GACL,eAAe,eAAe,GAAG;GACjC,aAAa,eAAe,eAAe,SAAS,GAAG;GACvD;GACD"}
@@ -0,0 +1,441 @@
1
+ import { ToolSet } from "ai";
2
+
3
+ //#region src/experimental/memory/session/types.d.ts
4
+ /**
5
+ * Minimal message part shape used by Session internals.
6
+ * Vercel AI SDK's `UIMessagePart` is structurally compatible.
7
+ */
8
+ interface SessionMessagePart {
9
+ type: string;
10
+ text?: string;
11
+ toolCallId?: string;
12
+ toolName?: string;
13
+ input?: unknown;
14
+ output?: unknown;
15
+ state?: string;
16
+ result?: unknown;
17
+ }
18
+ /**
19
+ * Minimal message shape used by Session internals.
20
+ * Vercel AI SDK's `UIMessage` is structurally compatible — you can pass
21
+ * `UIMessage` objects directly without conversion.
22
+ */
23
+ interface SessionMessage {
24
+ id: string;
25
+ role: string;
26
+ parts: SessionMessagePart[];
27
+ createdAt?: Date;
28
+ }
29
+ /**
30
+ * Options for creating a Session.
31
+ */
32
+ interface SessionOptions {
33
+ /** Context blocks for the system prompt. */
34
+ context?: ContextConfig[];
35
+ /** Provider for persisting the frozen system prompt. */
36
+ promptStore?: WritableContextProvider;
37
+ }
38
+ //#endregion
39
+ //#region src/experimental/memory/session/provider.d.ts
40
+ interface SearchResult {
41
+ id: string;
42
+ role: string;
43
+ content: string;
44
+ createdAt?: string;
45
+ sessionId?: string;
46
+ }
47
+ interface StoredCompaction {
48
+ id: string;
49
+ summary: string;
50
+ fromMessageId: string;
51
+ toMessageId: string;
52
+ createdAt: string;
53
+ }
54
+ /**
55
+ * Session storage provider.
56
+ * Messages are tree-structured via parentId for branching.
57
+ */
58
+ interface SessionProvider {
59
+ getMessage(id: string): SessionMessage | null;
60
+ /**
61
+ * Get conversation as a path from root to leaf.
62
+ * Applies compaction overlays. If leafId is null, uses the latest leaf.
63
+ */
64
+ getHistory(leafId?: string | null): SessionMessage[];
65
+ getLatestLeaf(): SessionMessage | null;
66
+ getBranches(messageId: string): SessionMessage[];
67
+ getPathLength(leafId?: string | null): number;
68
+ /**
69
+ * Append a message. Parented to the latest leaf unless parentId is provided.
70
+ * Idempotent — same message.id twice is a no-op.
71
+ */
72
+ appendMessage(message: SessionMessage, parentId?: string | null): void;
73
+ updateMessage(message: SessionMessage): void;
74
+ deleteMessages(messageIds: string[]): void;
75
+ clearMessages(): void;
76
+ addCompaction(
77
+ summary: string,
78
+ fromMessageId: string,
79
+ toMessageId: string
80
+ ): StoredCompaction;
81
+ getCompactions(): StoredCompaction[];
82
+ searchMessages?(query: string, limit?: number): SearchResult[];
83
+ }
84
+ //#endregion
85
+ //#region src/experimental/memory/session/providers/agent.d.ts
86
+ interface SqlProvider {
87
+ sql<T = Record<string, string | number | boolean | null>>(
88
+ strings: TemplateStringsArray,
89
+ ...values: (string | number | boolean | null)[]
90
+ ): T[];
91
+ }
92
+ declare class AgentSessionProvider implements SessionProvider {
93
+ private agent;
94
+ private initialized;
95
+ private sessionId;
96
+ /**
97
+ * @param agent - Agent or any object with a `sql` tagged template method
98
+ * @param sessionId - Optional session ID to isolate multiple sessions in the same DO.
99
+ * Messages are filtered by session_id within shared tables.
100
+ */
101
+ constructor(agent: SqlProvider, sessionId?: string);
102
+ private ensureTable;
103
+ getMessage(id: string): SessionMessage | null;
104
+ getHistory(leafId?: string | null): SessionMessage[];
105
+ getLatestLeaf(): SessionMessage | null;
106
+ getBranches(messageId: string): SessionMessage[];
107
+ getPathLength(leafId?: string | null): number;
108
+ appendMessage(message: SessionMessage, parentId?: string | null): void;
109
+ updateMessage(message: SessionMessage): void;
110
+ deleteMessages(messageIds: string[]): void;
111
+ clearMessages(): void;
112
+ addCompaction(
113
+ summary: string,
114
+ fromMessageId: string,
115
+ toMessageId: string
116
+ ): StoredCompaction;
117
+ getCompactions(): StoredCompaction[];
118
+ searchMessages(query: string, limit?: number): SearchResult[];
119
+ private latestLeafRow;
120
+ private indexFTS;
121
+ private deleteFTS;
122
+ private applyCompactions;
123
+ private parse;
124
+ private parseRows;
125
+ }
126
+ //#endregion
127
+ //#region src/experimental/memory/session/search.d.ts
128
+ /**
129
+ * Storage interface for searchable context.
130
+ *
131
+ * - `get()` returns a summary of indexed content (rendered into system prompt)
132
+ * - `search(query)` full-text search (via search_context tool)
133
+ * - `set(key, content)` indexes content under a key (via set_context tool)
134
+ */
135
+ interface SearchProvider extends ContextProvider {
136
+ search(query: string): Promise<string | null>;
137
+ set?(key: string, content: string): Promise<void>;
138
+ }
139
+ /**
140
+ * Check if a provider is a SearchProvider (has a `search` method).
141
+ */
142
+ declare function isSearchProvider(
143
+ provider: unknown
144
+ ): provider is SearchProvider;
145
+ /**
146
+ * SearchProvider backed by Durable Object SQLite with FTS5.
147
+ *
148
+ * - `get()` returns a count of indexed entries
149
+ * - `search(query)` full-text search using FTS5
150
+ * - `set(key, content)` indexes or replaces content under a key
151
+ *
152
+ * Each instance uses a namespaced FTS5 table to avoid collisions
153
+ * with the session message search.
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * Session.create(this)
158
+ * .withContext("knowledge", {
159
+ * provider: new AgentSearchProvider(this)
160
+ * })
161
+ * ```
162
+ */
163
+ declare class AgentSearchProvider implements SearchProvider {
164
+ private agent;
165
+ private label;
166
+ private initialized;
167
+ constructor(agent: SqlProvider);
168
+ init(label: string): void;
169
+ private ensureTable;
170
+ get(): Promise<string | null>;
171
+ search(query: string): Promise<string | null>;
172
+ set(key: string, content: string): Promise<void>;
173
+ private deleteFTS;
174
+ }
175
+ //#endregion
176
+ //#region src/experimental/memory/session/skills.d.ts
177
+ /**
178
+ * Storage interface for skill collections.
179
+ *
180
+ * - `get()` returns metadata listing (rendered into system prompt)
181
+ * - `load(key)` fetches full content (via load_context tool)
182
+ * - `set(key, content, description?)` writes an entry (via set_context tool)
183
+ */
184
+ interface SkillProvider extends ContextProvider {
185
+ load(key: string): Promise<string | null>;
186
+ set?(key: string, content: string, description?: string): Promise<void>;
187
+ }
188
+ /**
189
+ * Check if a provider is a SkillProvider (has a `load` method).
190
+ */
191
+ declare function isSkillProvider(provider: unknown): provider is SkillProvider;
192
+ /**
193
+ * SkillProvider backed by an R2 bucket.
194
+ *
195
+ * - `get()` returns a metadata listing of all skills (key + description)
196
+ * - `load(key)` fetches a skill's full content
197
+ * - `set(key, content, description?)` writes a skill
198
+ *
199
+ * Descriptions are pulled from R2 custom metadata (`description` key).
200
+ * If a prefix is provided, it is prepended on storage operations and
201
+ * stripped from keys in metadata.
202
+ *
203
+ * @example
204
+ * ```ts
205
+ * const skills = new R2SkillProvider(env.SKILLS_BUCKET, { prefix: "skills/" });
206
+ * ```
207
+ */
208
+ declare class R2SkillProvider implements SkillProvider {
209
+ private bucket;
210
+ private prefix;
211
+ constructor(
212
+ bucket: R2Bucket,
213
+ options?: {
214
+ prefix?: string;
215
+ }
216
+ );
217
+ get(): Promise<string | null>;
218
+ load(key: string): Promise<string | null>;
219
+ set(key: string, content: string, description?: string): Promise<void>;
220
+ }
221
+ //#endregion
222
+ //#region src/experimental/memory/session/context.d.ts
223
+ /**
224
+ * Base storage interface for a context block.
225
+ * A provider with only `get()` is readonly.
226
+ */
227
+ interface ContextProvider {
228
+ get(): Promise<string | null>;
229
+ /** Called by the context system to provide the block label before first use. */
230
+ init?(label: string): void;
231
+ }
232
+ /**
233
+ * Writable context provider — extends ContextProvider with `set()`.
234
+ * Blocks backed by this provider are writable via the `set_context` tool.
235
+ */
236
+ interface WritableContextProvider extends ContextProvider {
237
+ set(content: string): Promise<void>;
238
+ }
239
+ /**
240
+ * Check if a provider is writable (has a `set` method).
241
+ */
242
+ declare function isWritableProvider(
243
+ provider: unknown
244
+ ): provider is WritableContextProvider;
245
+ /**
246
+ * Configuration for a context block.
247
+ */
248
+ interface ContextConfig {
249
+ /** Block label — used as key and in tool descriptions */
250
+ label: string;
251
+ /** Human-readable description (shown to AI in tool) */
252
+ description?: string;
253
+ /** Maximum tokens allowed. Enforced on set. */
254
+ maxTokens?: number;
255
+ /** Storage provider. Determines block behavior:
256
+ * - ContextProvider (get only) → readonly
257
+ * - WritableContextProvider (get+set) → writable via set_context
258
+ * - SkillProvider (get+load+set?) → on-demand via load_context
259
+ * - SearchProvider (get+search+set?) → searchable via search_context
260
+ * If omitted, auto-wired to writable SQLite when using builder. */
261
+ provider?:
262
+ | ContextProvider
263
+ | WritableContextProvider
264
+ | SkillProvider
265
+ | SearchProvider;
266
+ }
267
+ /**
268
+ * A loaded context block with computed token count.
269
+ */
270
+ interface ContextBlock {
271
+ label: string;
272
+ description?: string;
273
+ content: string;
274
+ tokens: number;
275
+ maxTokens?: number;
276
+ /** True if provider is writable (has set) */
277
+ writable: boolean;
278
+ /** True if backed by a SkillProvider */
279
+ isSkill: boolean;
280
+ /** True if backed by a SearchProvider */
281
+ isSearchable: boolean;
282
+ }
283
+ //#endregion
284
+ //#region src/experimental/memory/utils/compaction-helpers.d.ts
285
+ /** Prefix for all compaction messages (overlays and summaries) */
286
+ declare const COMPACTION_PREFIX = "compaction_";
287
+ /** Check if a message is a compaction message */
288
+ declare function isCompactionMessage(msg: SessionMessage): boolean;
289
+ /**
290
+ * Align a boundary index forward to avoid splitting tool call/result groups.
291
+ * If the boundary falls between an assistant message with tool calls and its
292
+ * tool results, move it forward past the results.
293
+ */
294
+ declare function alignBoundaryForward(
295
+ messages: SessionMessage[],
296
+ idx: number
297
+ ): number;
298
+ /**
299
+ * Align a boundary index backward to avoid splitting tool call/result groups.
300
+ * If the boundary falls in the middle of tool results, move it backward to
301
+ * include the assistant message that made the calls.
302
+ */
303
+ declare function alignBoundaryBackward(
304
+ messages: SessionMessage[],
305
+ idx: number
306
+ ): number;
307
+ /**
308
+ * Find the compression end boundary using a token budget for the tail.
309
+ * Walks backward from the end, accumulating tokens until budget is reached.
310
+ * Returns the index where compression should stop (everything from this
311
+ * index onward is protected).
312
+ *
313
+ * @param messages All messages
314
+ * @param headEnd Index where the protected head ends (compression starts here)
315
+ * @param tailTokenBudget Maximum tokens to keep in the tail
316
+ * @param minTailMessages Minimum messages to protect in the tail (fallback)
317
+ */
318
+ declare function findTailCutByTokens(
319
+ messages: SessionMessage[],
320
+ headEnd: number,
321
+ tailTokenBudget?: number,
322
+ minTailMessages?: number
323
+ ): number;
324
+ /**
325
+ * Fix orphaned tool call/result pairs after compaction.
326
+ *
327
+ * Two failure modes:
328
+ * 1. Tool result references a call_id whose assistant tool_call was removed
329
+ * → Remove the orphaned result
330
+ * 2. Assistant has tool_calls whose results were dropped
331
+ * → Add stub results so the API doesn't error
332
+ *
333
+ * @param messages Messages after compaction
334
+ * @returns Sanitized messages with no orphaned pairs
335
+ */
336
+ declare function sanitizeToolPairs(
337
+ messages: SessionMessage[]
338
+ ): SessionMessage[];
339
+ /**
340
+ * Compute a summary token budget based on the content being compressed.
341
+ * 20% of the compressed content, clamped to 2K-8K tokens.
342
+ */
343
+ declare function computeSummaryBudget(messages: SessionMessage[]): number;
344
+ /**
345
+ * Build a prompt for LLM summarization of compressed messages.
346
+ *
347
+ * @param messages Messages to summarize
348
+ * @param previousSummary Previous summary for iterative updates (or null for first compaction)
349
+ * @param budget Target token count for the summary
350
+ */
351
+ declare function buildSummaryPrompt(
352
+ messages: SessionMessage[],
353
+ previousSummary: string | null,
354
+ budget: number
355
+ ): string;
356
+ /**
357
+ * Result of a compaction function — describes the overlay to store.
358
+ */
359
+ interface CompactResult {
360
+ /** First message ID in the compacted range */
361
+ fromMessageId: string;
362
+ /** Last message ID in the compacted range */
363
+ toMessageId: string;
364
+ /** Summary text to store as the overlay */
365
+ summary: string;
366
+ }
367
+ interface CompactOptions {
368
+ /**
369
+ * Function to call the LLM for summarization.
370
+ * Takes a user prompt string, returns the LLM's text response.
371
+ */
372
+ summarize: (prompt: string) => Promise<string>;
373
+ /** Number of head messages to protect (default: 2) */
374
+ protectHead?: number;
375
+ /** Token budget for tail protection (default: 20000) */
376
+ tailTokenBudget?: number;
377
+ /** Minimum tail messages to protect (default: 2) */
378
+ minTailMessages?: number;
379
+ }
380
+ /**
381
+ * Reference compaction implementation.
382
+ *
383
+ * Implements the full hermes-style compaction algorithm:
384
+ * 1. Protect head messages (first N)
385
+ * 2. Protect tail by token budget (walk backward)
386
+ * 3. Align boundaries to tool call groups
387
+ * 4. Summarize middle section with LLM (structured format)
388
+ * 5. Sanitize orphaned tool pairs
389
+ * 6. Iterative summary updates on subsequent compactions
390
+ *
391
+ * @example
392
+ * ```typescript
393
+ * import { createCompactFunction } from "agents/experimental/memory/utils";
394
+ *
395
+ * const session = new Session(provider, {
396
+ * compaction: {
397
+ * tokenThreshold: 100000,
398
+ * fn: createCompactFunction({
399
+ * summarize: (prompt) => generateText({ model, prompt }).then(r => r.text)
400
+ * })
401
+ * }
402
+ * });
403
+ * ```
404
+ */
405
+ declare function createCompactFunction(
406
+ opts: CompactOptions
407
+ ): (messages: SessionMessage[]) => Promise<CompactResult | null>;
408
+ //#endregion
409
+ export {
410
+ SessionOptions as A,
411
+ AgentSessionProvider as C,
412
+ StoredCompaction as D,
413
+ SessionProvider as E,
414
+ SessionMessage as O,
415
+ isSearchProvider as S,
416
+ SearchResult as T,
417
+ R2SkillProvider as _,
418
+ alignBoundaryForward as a,
419
+ AgentSearchProvider as b,
420
+ createCompactFunction as c,
421
+ sanitizeToolPairs as d,
422
+ ContextBlock as f,
423
+ isWritableProvider as g,
424
+ WritableContextProvider as h,
425
+ alignBoundaryBackward as i,
426
+ SessionMessagePart as k,
427
+ findTailCutByTokens as l,
428
+ ContextProvider as m,
429
+ CompactOptions as n,
430
+ buildSummaryPrompt as o,
431
+ ContextConfig as p,
432
+ CompactResult as r,
433
+ computeSummaryBudget as s,
434
+ COMPACTION_PREFIX as t,
435
+ isCompactionMessage as u,
436
+ SkillProvider as v,
437
+ SqlProvider as w,
438
+ SearchProvider as x,
439
+ isSkillProvider as y
440
+ };
441
+ //# sourceMappingURL=compaction-helpers-BdQbZiML.d.ts.map