// Conversation compaction — summarize the older "head" of a conversation into // a structured anchor while preserving the most recent turns verbatim. // // Ported from opencode's `session/compaction.ts` (SUMMARY_TEMPLATE, the // anchored-summary `buildPrompt`, and the `TOOL_OUTPUT_MAX_CHARS` cap). The // turn-budget `splitTurn` tail selection is intentionally simplified to a fixed // recent-turn count (`DEFAULT_TAIL_TURNS`) — Dispatch keeps the last N turns // verbatim rather than computing a token budget. // // This module is pure and DB-free so it can be unit-tested in isolation and // shared by the API orchestrator. import type { ChatMessage } from "../types/index.js"; /** Number of trailing turns kept verbatim (opencode's DEFAULT_TAIL_TURNS). */ export const DEFAULT_TAIL_TURNS = 2; /** Max characters of a single tool result fed into the summary request. */ export const TOOL_OUTPUT_MAX_CHARS = 2_000; /** * Marker prefixing the seeded summary turn in a compacted conversation. Lets a * subsequent compaction detect a prior summary and re-summarize (anchor) it * instead of treating it as ordinary conversation. */ export const SUMMARY_MARKER = "[CONVERSATION SUMMARY]"; /** * Structured Markdown template the summary must follow. Ported verbatim from * opencode's `SUMMARY_TEMPLATE`. */ export const SUMMARY_TEMPLATE = `Output exactly the Markdown structure shown inside