feat(plugin): add experimental.session.compacting hook for pre-compaction context injection (#5698)

Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
This commit is contained in:
Joel Hooks 2025-12-17 11:57:09 -08:00 committed by GitHub
parent 1f52731255
commit 24430287c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 44 additions and 1 deletions

View file

@ -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"),
},
],
},

View file

@ -191,6 +191,11 @@ export interface Hooks {
system: string[]
},
) => Promise<void>
/**
* 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<void>
"experimental.text.complete"?: (
input: { sessionID: string; messageID: string; partID: string },
output: { text: string },

View file

@ -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.