Skip to content

Commit eab177f

Browse files
authored
feat(plugin): allow compaction hook to replace prompt entirely (#5907)
1 parent 279dc04 commit eab177f

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

packages/opencode/src/session/compaction.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,15 @@ export namespace SessionCompaction {
130130
model,
131131
abort: input.abort,
132132
})
133-
// Allow plugins to inject context for compaction
133+
// Allow plugins to inject context or replace compaction prompt
134134
const compacting = await Plugin.trigger(
135135
"experimental.session.compacting",
136136
{ sessionID: input.sessionID },
137-
{ context: [] },
137+
{ context: [], prompt: undefined },
138138
)
139+
const defaultPrompt =
140+
"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."
141+
const promptText = compacting.prompt ?? [defaultPrompt, ...compacting.context].join("\n\n")
139142
const result = await processor.process({
140143
user: userMessage,
141144
agent,
@@ -150,10 +153,7 @@ export namespace SessionCompaction {
150153
content: [
151154
{
152155
type: "text",
153-
text: [
154-
"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.",
155-
...compacting.context,
156-
].join("\n\n"),
156+
text: promptText,
157157
},
158158
],
159159
},

packages/plugin/src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,16 @@ export interface Hooks {
192192
},
193193
) => Promise<void>
194194
/**
195-
* Called before session compaction starts. Allows plugins to append
196-
* additional context to the compaction prompt.
195+
* Called before session compaction starts. Allows plugins to customize
196+
* the compaction prompt.
197+
*
198+
* - `context`: Additional context strings appended to the default prompt
199+
* - `prompt`: If set, replaces the default compaction prompt entirely
197200
*/
198-
"experimental.session.compacting"?: (input: { sessionID: string }, output: { context: string[] }) => Promise<void>
201+
"experimental.session.compacting"?: (
202+
input: { sessionID: string },
203+
output: { context: string[]; prompt?: string },
204+
) => Promise<void>
199205
"experimental.text.complete"?: (
200206
input: { sessionID: string; messageID: string; partID: string },
201207
output: { text: string },

packages/web/src/content/docs/plugins.mdx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,30 @@ Include any state that should persist across compaction:
233233
```
234234

235235
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.
236+
237+
You can also replace the compaction prompt entirely by setting `output.prompt`:
238+
239+
```ts title=".opencode/plugin/custom-compaction.ts"
240+
import type { Plugin } from "@opencode-ai/plugin"
241+
242+
export const CustomCompactionPlugin: Plugin = async (ctx) => {
243+
return {
244+
"experimental.session.compacting": async (input, output) => {
245+
// Replace the entire compaction prompt
246+
output.prompt = `
247+
You are generating a continuation prompt for a multi-agent swarm session.
248+
249+
Summarize:
250+
1. The current task and its status
251+
2. Which files are being modified and by whom
252+
3. Any blockers or dependencies between agents
253+
4. The next steps to complete the work
254+
255+
Format as a structured prompt that a new agent can use to resume work.
256+
`
257+
},
258+
}
259+
}
260+
```
261+
262+
When `output.prompt` is set, it completely replaces the default compaction prompt. The `output.context` array is ignored in this case.

0 commit comments

Comments
 (0)