7 Commits
6 changed files with 56 additions and 30 deletions
+12
View File
@@ -1,5 +1,17 @@
# Changelog # Changelog
## 0.2.3
- Always ask the model to report whether custom compaction instructions were applied.
## 0.2.2
- Add explicit markers for applied and skipped custom compaction instructions.
## 0.2.1
- Add the server entrypoint expected by OpenCode's npm plugin loader.
## 0.1.2 ## 0.1.2
- Skip compaction prompt changes when no prompt or memory file is provided. - Skip compaction prompt changes when no prompt or memory file is provided.
+14 -12
View File
@@ -1,22 +1,22 @@
# opencode-plugin-compaction-prompt # opencode-plugin-compaction-prompt
Customize OpenCode's compaction prompt - decide which messages are prioritized/discarded in the next session.
**Compact fearlessly with priority-aware compaction.** **Compact fearlessly with priority-aware compaction.**
When you work across multiple features, OpenCode's default compaction doesn't know how to distinguish active work from older discussions, so it gives them equal priority. This can make important current details easy to lose. When you work across multiple features, OpenCode's default compaction doesn't know how to distinguish active work from older discussions, so it gives them equal priority. This can make important current details easy to lose.
This plugin enables priority-aware compaction, giving you more control and confidence when manually compacting a session. Specify which discussions to discard and which decisions, files, and snippets to prioritize in the summary. Customize OpenCode's compaction prompt for priority-aware compaction, deciding which messages are prioritized or discarded in the next session. Specify which discussions to discard and which decisions, files, and snippets to prioritize in the summary.
[GitHub](https://github.com/bendtherules/opencode-plugin-compaction-prompt) · [npm](https://www.npmjs.com/package/opencode-plugin-compaction-prompt) [GitHub](https://github.com/bendtherules/opencode-plugin-compaction-prompt) · [npm](https://www.npmjs.com/package/opencode-plugin-compaction-prompt)
## Install ## Install
```bash ```bash
opencode plugin opencode-plugin-compaction-prompt opencode plugin -g opencode-plugin-compaction-prompt
``` ```
Or add it to `opencode.json` manually: This installs the plugin in your global OpenCode configuration. To install it for one project only, omit `-g`.
You can also add it manually to your global `opencode.jsonc`:
```json ```json
{ {
@@ -27,7 +27,7 @@ Or add it to `opencode.json` manually:
{ {
"memoryFile": ".opencode/compaction.md", "memoryFile": ".opencode/compaction.md",
"mode": "append", "mode": "append",
"completionMarker": "Custom compaction request honored." "completionMarker": "opencode-plugin-compaction-prompt: Custom compaction done."
} }
] ]
] ]
@@ -38,15 +38,17 @@ Create `.opencode/compaction.md` in the project when you have project-specific c
## Options ## Options
| Option | Default | Description | | Option | Default | Description |
| ------------------ | ------------------------------------ | ---------------------------------------------------------------------------------------------------------- | | ------------------ | ------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------- |
| `memoryFile` | `.opencode/compaction.md` | File resolved relative to the active worktree. | | `memoryFile` | `.opencode/compaction.md` | File resolved relative to the active worktree. |
| `mode` | `append` | Append instructions to OpenCode's default prompt, or use `replace` to provide a complete prompt. | | `mode` | `append` | Append instructions to OpenCode's default prompt, or use `replace` to provide a complete prompt. |
| `prompt` | "" (empty) | Additional instructions used together with `memoryFile`; both are included in the compaction instructions. | | `prompt` | "" (empty) | Additional instructions used together with `memoryFile`; both are included in the compaction instructions. |
| `completionMarker` | `Custom compaction request honored.` | Exact text the model is asked to append at the end of the summary. | | `completionMarker` | `opencode-plugin-compaction-prompt: Custom compaction done.` | Exact text the model is asked to append at the end of the summary. |
Append mode is the recommended default because it preserves OpenCode's built-in compaction behavior. Replace mode is available when the complete prompt needs to be controlled by this plugin. Append mode is the recommended default because it preserves OpenCode's built-in compaction behavior. Replace mode is available when the complete prompt needs to be controlled by this plugin.
When neither a prompt nor a memory file is available, the plugin asks the model to echo `opencode-plugin-compaction-prompt: No custom compaction applied.` instead.
## Development ## Development
```bash ```bash
+1 -1
View File
@@ -7,7 +7,7 @@
"memoryFile": ".opencode/compaction.md", "memoryFile": ".opencode/compaction.md",
"mode": "append", "mode": "append",
"prompt": "<Example prompt for compaction>", "prompt": "<Example prompt for compaction>",
"completionMarker": "Custom compaction request honored." "completionMarker": "opencode-plugin-compaction-prompt: Custom compaction done."
} }
] ]
] ]
+7 -1
View File
@@ -1,8 +1,10 @@
{ {
"name": "opencode-plugin-compaction-prompt", "name": "opencode-plugin-compaction-prompt",
"version": "0.2.0", "version": "0.2.3",
"description": "Customize OpenCode's compaction prompt to preserve important context and omit unnecessary details.", "description": "Customize OpenCode's compaction prompt to preserve important context and omit unnecessary details.",
"type": "module", "type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/bendtherules/opencode-plugin-compaction-prompt.git" "url": "https://github.com/bendtherules/opencode-plugin-compaction-prompt.git"
@@ -15,6 +17,10 @@
".": { ".": {
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
"import": "./dist/index.js" "import": "./dist/index.js"
},
"./server": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
} }
}, },
"files": [ "files": [
+8 -11
View File
@@ -13,7 +13,10 @@ export type CompactionOptions = PluginOptions & {
const defaultMemoryFile = ".opencode/compaction.md"; const defaultMemoryFile = ".opencode/compaction.md";
const defaultPrompt = ""; const defaultPrompt = "";
const defaultCompletionMarker = "Custom compaction request honored."; const defaultCompletionMarker =
"opencode-plugin-compaction-prompt: Custom compaction done.";
const skippedCompactionMarker =
"opencode-plugin-compaction-prompt: No custom compaction applied.";
function asString(value: unknown): string | undefined { function asString(value: unknown): string | undefined {
if (typeof value !== "string") { if (typeof value !== "string") {
@@ -28,11 +31,7 @@ function buildInstructions(
prompt: string, prompt: string,
completionMarker: string, completionMarker: string,
memory: string, memory: string,
): string | undefined { ): string {
if (!prompt && !memory) {
return undefined;
}
const sections = [ const sections = [
"## User Compaction Instructions", "## User Compaction Instructions",
`These instructions take precedence over previous instructions if there is conflict. At the very end of the summary, echo exactly: **${completionMarker}**`, `These instructions take precedence over previous instructions if there is conflict. At the very end of the summary, echo exactly: **${completionMarker}**`,
@@ -112,11 +111,9 @@ export const CompactionPromptPlugin: Plugin = async (
output, output,
): Promise<void> => { ): Promise<void> => {
const memory = await readMemory(memoryPath, logError); const memory = await readMemory(memoryPath, logError);
const instructions = buildInstructions(prompt, completionMarker, memory); const marker =
prompt || memory ? completionMarker : skippedCompactionMarker;
if (!instructions) { const instructions = buildInstructions(prompt, marker, memory);
return;
}
if (mode === "replace") { if (mode === "replace") {
output.prompt = instructions; output.prompt = instructions;
+14 -5
View File
@@ -55,7 +55,9 @@ describe("CompactionPromptPlugin", () => {
); );
expect(output.context[0]).toContain("Keep the active hypothesis."); expect(output.context[0]).toContain("Keep the active hypothesis.");
expect(output.context[0]).toContain("Custom compaction request honored."); expect(output.context[0]).toContain(
"opencode-plugin-compaction-prompt: Custom compaction done.",
);
}); });
test("combines custom instructions with a configured memory file", async () => { test("combines custom instructions with a configured memory file", async () => {
@@ -114,10 +116,12 @@ describe("CompactionPromptPlugin", () => {
); );
expect(context.logs).toHaveLength(0); expect(context.logs).toHaveLength(0);
expect(output.context).toHaveLength(0); expect(output.context[0]).toContain(
"echo exactly: **opencode-plugin-compaction-prompt: No custom compaction applied.**",
);
}); });
test("does not modify compaction when no instructions are provided", async () => { test("adds a skipped marker when no instructions are provided", async () => {
const context = await createContext(); const context = await createContext();
const hooks = await CompactionPromptPlugin(context as never); const hooks = await CompactionPromptPlugin(context as never);
const output: { context: string[]; prompt?: string } = { const output: { context: string[]; prompt?: string } = {
@@ -129,7 +133,10 @@ describe("CompactionPromptPlugin", () => {
output, output,
); );
expect(output.context).toEqual(["Existing context"]); expect(output.context[0]).toBe("Existing context");
expect(output.context[1]).toContain(
"echo exactly: **opencode-plugin-compaction-prompt: No custom compaction applied.**",
);
expect(output.prompt).toBeUndefined(); expect(output.prompt).toBeUndefined();
}); });
@@ -147,6 +154,8 @@ describe("CompactionPromptPlugin", () => {
); );
expect(context.logs).toHaveLength(1); expect(context.logs).toHaveLength(1);
expect(output.context).toHaveLength(0); expect(output.context[0]).toContain(
"echo exactly: **opencode-plugin-compaction-prompt: No custom compaction applied.**",
);
}); });
}); });