compaction.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. export * as SessionCompaction from "./compaction"
  2. import { LLM, LLMError, LLMEvent, Message, type LLMRequest, type Model } from "@opencode-ai/llm"
  3. import { DateTime, Effect, Stream } from "effect"
  4. import type { Config } from "../config"
  5. import type { EventV2 } from "../event"
  6. import { SessionEvent } from "./event"
  7. import { SessionMessage } from "./message"
  8. import { SessionSchema } from "./schema"
  9. import { Token } from "../util/token"
  10. const DEFAULT_BUFFER = 20_000
  11. const DEFAULT_KEEP_TOKENS = 8_000
  12. const TOOL_OUTPUT_MAX_CHARS = 2_000
  13. const SUMMARY_OUTPUT_TOKENS = 4_096
  14. const SUMMARY_TEMPLATE = `Output exactly the Markdown structure shown inside <template> and keep the section order unchanged. Do not include the <template> tags in your response.
  15. <template>
  16. ## Goal
  17. - [single-sentence task summary]
  18. ## Constraints & Preferences
  19. - [user constraints, preferences, specs, or "(none)"]
  20. ## Progress
  21. ### Done
  22. - [completed work or "(none)"]
  23. ### In Progress
  24. - [current work or "(none)"]
  25. ### Blocked
  26. - [blockers or "(none)"]
  27. ## Key Decisions
  28. - [decision and why, or "(none)"]
  29. ## Next Steps
  30. - [ordered next actions or "(none)"]
  31. ## Critical Context
  32. - [important technical facts, errors, open questions, or "(none)"]
  33. ## Relevant Files
  34. - [file or directory path: why it matters, or "(none)"]
  35. </template>
  36. Rules:
  37. - Keep every section, even when empty.
  38. - Use terse bullets, not prose paragraphs.
  39. - Preserve exact file paths, commands, error strings, and identifiers when known.
  40. - Do not mention the summary process or that context was compacted.`
  41. type Entry = {
  42. readonly seq: number
  43. readonly message: SessionMessage.Message
  44. }
  45. type Settings = {
  46. readonly auto: boolean
  47. readonly buffer: number
  48. readonly tokens: number
  49. }
  50. type Dependencies = {
  51. readonly events: EventV2.Interface
  52. readonly llm: {
  53. readonly stream: (request: LLMRequest) => Stream.Stream<LLMEvent, LLMError>
  54. }
  55. readonly config: readonly Config.Entry[]
  56. }
  57. const estimate = (value: unknown) => Token.estimate(JSON.stringify(value))
  58. const truncate = (value: string) =>
  59. value.length <= TOOL_OUTPUT_MAX_CHARS ? value : `${value.slice(0, TOOL_OUTPUT_MAX_CHARS)}\n[truncated]`
  60. const serialize = (message: SessionMessage.Message) => {
  61. if (message.type === "user") {
  62. const files = message.files?.map((file) => `[Attached ${file.mime}: ${file.name ?? file.uri}]`) ?? []
  63. return [`[User]: ${message.text}`, ...files].join("\n")
  64. }
  65. if (message.type === "assistant") {
  66. return message.content
  67. .flatMap((part) => {
  68. if (part.type === "text") return [`[Assistant]: ${part.text}`]
  69. if (part.type === "reasoning") return part.text ? [`[Assistant reasoning]: ${part.text}`] : []
  70. const input = typeof part.state.input === "string" ? part.state.input : JSON.stringify(part.state.input)
  71. if (part.state.status === "completed")
  72. return [
  73. `[Assistant tool call]: ${part.name}(${input})`,
  74. `[Tool result]: ${truncate(JSON.stringify(part.state.content))}`,
  75. ]
  76. if (part.state.status === "error")
  77. return [`[Assistant tool call]: ${part.name}(${input})`, `[Tool error]: ${part.state.error.message}`]
  78. return [`[Assistant tool call]: ${part.name}(${input})`]
  79. })
  80. .join("\n")
  81. }
  82. if (message.type === "system") return `[System update]: ${message.text}`
  83. if (message.type === "synthetic") return `[Synthetic context]: ${message.text}`
  84. if (message.type === "shell") return `[Shell]: ${message.command}\n${truncate(message.output)}`
  85. return ""
  86. }
  87. const settings = (documents: readonly Config.Entry[]) => {
  88. const configured = documents
  89. .filter((entry): entry is Config.Document => entry.type === "document")
  90. .flatMap((entry) => (entry.info.compaction ? [entry.info.compaction] : []))
  91. return configured.reduce<Settings>(
  92. (result, current) => ({
  93. auto: current.auto ?? result.auto,
  94. buffer: current.buffer ?? result.buffer,
  95. tokens: current.keep?.tokens ?? result.tokens,
  96. }),
  97. { auto: true, buffer: DEFAULT_BUFFER, tokens: DEFAULT_KEEP_TOKENS },
  98. )
  99. }
  100. const select = (
  101. entries: readonly Entry[],
  102. tokens: number,
  103. ): { readonly head: string; readonly recent: string } | undefined => {
  104. const conversation = entries
  105. .filter((entry) => entry.message.type !== "compaction")
  106. .map((entry) => serialize(entry.message))
  107. .filter(Boolean)
  108. if (conversation.length === 0) return
  109. let total = 0
  110. let split = conversation.length
  111. let splitPrefix = ""
  112. let splitSuffix = ""
  113. for (let index = conversation.length - 1; index >= 0; index--) {
  114. const next = total + Token.estimate(conversation[index])
  115. if (next > tokens) {
  116. const remaining = Math.max(0, tokens - total) * 4
  117. if (remaining > 0) {
  118. splitPrefix = conversation[index].slice(0, -remaining)
  119. splitSuffix = conversation[index].slice(-remaining)
  120. split = index + 1
  121. }
  122. break
  123. }
  124. total = next
  125. split = index
  126. }
  127. return {
  128. head: [...conversation.slice(0, split), splitPrefix].filter(Boolean).join("\n\n"),
  129. recent: [splitSuffix, ...conversation.slice(split)].filter(Boolean).join("\n\n"),
  130. }
  131. }
  132. export const buildPrompt = (input: { readonly previousSummary?: string; readonly context: readonly string[] }) =>
  133. [
  134. input.previousSummary
  135. ? `Update the anchored summary below using the conversation history above.\nPreserve still-true details, remove stale details, and merge in the new facts.\n<previous-summary>\n${input.previousSummary}\n</previous-summary>`
  136. : "Create a new anchored summary from the conversation history.",
  137. SUMMARY_TEMPLATE,
  138. ...input.context,
  139. ].join("\n\n")
  140. export const make = (dependencies: Dependencies) => {
  141. const config = settings(dependencies.config)
  142. return Effect.fn("SessionCompaction.compactIfNeeded")(function* (input: {
  143. readonly sessionID: SessionSchema.ID
  144. readonly entries: readonly Entry[]
  145. readonly model: Model
  146. readonly request: LLMRequest
  147. }) {
  148. const context = input.model.route.defaults.limits?.context
  149. if (!config.auto || context === undefined || context <= 0) return false
  150. const output = input.request.generation?.maxTokens ?? input.model.route.defaults.limits?.output ?? 0
  151. if (
  152. estimate({ system: input.request.system, messages: input.request.messages, tools: input.request.tools }) <=
  153. context - Math.max(output, config.buffer)
  154. )
  155. return false
  156. const selected = select(input.entries, config.tokens)
  157. const previousSummary = input.entries.find((entry) => entry.message.type === "compaction")?.message
  158. if (!selected || (selected.head.length === 0 && previousSummary?.type !== "compaction")) return false
  159. const summaryPrompt = buildPrompt({
  160. previousSummary: previousSummary?.type === "compaction" ? previousSummary.summary : undefined,
  161. context: [previousSummary?.type === "compaction" ? previousSummary.recent : "", selected.head].filter(Boolean),
  162. })
  163. const summaryOutput = Math.min(output || SUMMARY_OUTPUT_TOKENS, SUMMARY_OUTPUT_TOKENS)
  164. if (Token.estimate(summaryPrompt) > context - summaryOutput) return false
  165. const messageID = SessionMessage.ID.create()
  166. yield* dependencies.events.publish(SessionEvent.Compaction.Started, {
  167. sessionID: input.sessionID,
  168. messageID,
  169. timestamp: yield* DateTime.now,
  170. reason: "auto",
  171. })
  172. const chunks: string[] = []
  173. yield* dependencies.llm
  174. .stream(
  175. LLM.request({
  176. model: input.model,
  177. messages: [Message.user(summaryPrompt)],
  178. tools: [],
  179. generation: { maxTokens: summaryOutput },
  180. }),
  181. )
  182. .pipe(
  183. Stream.runForEach((event) => {
  184. if (!LLMEvent.is.textDelta(event)) return Effect.void
  185. chunks.push(event.text)
  186. return Effect.void
  187. }),
  188. )
  189. const summary = chunks.join("")
  190. if (!summary.trim()) return yield* Effect.die("Compaction returned an empty summary")
  191. yield* dependencies.events.publish(SessionEvent.Compaction.Ended, {
  192. sessionID: input.sessionID,
  193. messageID,
  194. timestamp: yield* DateTime.now,
  195. reason: "auto",
  196. text: summary,
  197. recent: selected.recent,
  198. })
  199. return true
  200. })
  201. }