mirror of
https://github.com/sst/opencode.git
synced 2025-12-23 10:11:41 +00:00
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:
parent
1f52731255
commit
24430287c5
3 changed files with 44 additions and 1 deletions
|
|
@ -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"),
|
||||
},
|
||||
],
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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 },
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue