Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/opencode/src/session/compaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
Expand Down Expand Up @@ -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,
Expand All @@ -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"),
},
],
},
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
28 changes: 28 additions & 0 deletions packages/web/src/content/docs/plugins.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.