diff --git a/packages/opencode/src/session/compaction.ts b/packages/opencode/src/session/compaction.ts index f8ed149ba40..3e4c8020dee 100644 --- a/packages/opencode/src/session/compaction.ts +++ b/packages/opencode/src/session/compaction.ts @@ -13,6 +13,7 @@ import { Log } from "../util/log" import { SessionProcessor } from "./processor" import { fn } from "@/util/fn" import { Agent } from "@/agent/agent" +import { Plugin } from "@/plugin" export namespace SessionCompaction { const log = Log.create({ service: "session.compaction" }) @@ -125,6 +126,12 @@ export namespace SessionCompaction { model, abort: input.abort, }) + // Allow plugins to inject context for compaction + const compacting = await Plugin.trigger( + "experimental.session.compacting", + { sessionID: input.sessionID }, + { context: [] }, + ) const result = await processor.process({ user: userMessage, agent, @@ -139,7 +146,10 @@ export namespace SessionCompaction { content: [ { type: "text", - text: "Provide a detailed prompt for continuing our conversation above. Focus on information that would be helpful for continuing the conversation, including what we did, what we're doing, which files we're working on, and what we're going to do next considering new session will not have access to our conversation.", + text: [ + "Provide a detailed prompt for continuing our conversation above. Focus on information that would be helpful for continuing the conversation, including what we did, what we're doing, which files we're working on, and what we're going to do next considering new session will not have access to our conversation.", + ...compacting.context, + ].join("\n\n"), }, ], }, diff --git a/packages/plugin/src/index.ts b/packages/plugin/src/index.ts index ef06a4d8bad..487e6ed3ee2 100644 --- a/packages/plugin/src/index.ts +++ b/packages/plugin/src/index.ts @@ -191,6 +191,11 @@ export interface Hooks { system: string[] }, ) => Promise + /** + * Called before session compaction starts. Allows plugins to append + * additional context to the compaction prompt. + */ + "experimental.session.compacting"?: (input: { sessionID: string }, output: { context: string[] }) => Promise "experimental.text.complete"?: ( input: { sessionID: string; messageID: string; partID: string }, output: { text: string }, diff --git a/packages/web/src/content/docs/plugins.mdx b/packages/web/src/content/docs/plugins.mdx index 58673d46dc1..6982b4c932f 100644 --- a/packages/web/src/content/docs/plugins.mdx +++ b/packages/web/src/content/docs/plugins.mdx @@ -205,3 +205,31 @@ The `tool` helper creates a custom tool that opencode can call. It takes a Zod s - `execute`: Function that runs when the tool is called Your custom tools will be available to opencode alongside built-in tools. + +--- + +### Compaction hooks + +Customize the context included when a session is compacted: + +```ts title=".opencode/plugin/compaction.ts" +import type { Plugin } from "@opencode-ai/plugin" + +export const CompactionPlugin: Plugin = async (ctx) => { + return { + "experimental.session.compacting": async (input, output) => { + // Inject additional context into the compaction prompt + output.context.push(` +## Custom Context + +Include any state that should persist across compaction: +- Current task status +- Important decisions made +- Files being actively worked on +`) + }, + } +} +``` + +The `experimental.session.compacting` hook fires before the LLM generates a continuation summary. Use it to inject domain-specific context that the default compaction prompt would miss.