Skip to content

Conversation

@maxeonyx
Copy link

@maxeonyx maxeonyx commented Nov 10, 2025

What's this?

Makes /compact customizable! You can now override the default compaction prompt or create entirely new compact-style commands (like /handover) using configuration or markdown files.

Why though?

The exact compaction prompt has a massive effect on performance after compaction, and this lets you experiment to find what works best for your workflow.

How it works

Override the default /compact

You can define commands in your configuration (e.g., .opencode/command/compact.md or via opencode.json configuration) with the compact property.

Example using Markdown (.opencode/command/compact.md):

---
description: my custom compact
compact: true
---

Provide a detailed summary focusing on:
- What code changed and why
- What testing was done
- What still needs to be done
- Include exact file paths and commands for next steps

Now when you hit <leader>c or auto-compaction triggers, it'll use your prompt instead of the default.

Create a custom compaction command

Want a "handover" mode? You can configure any command to be a compaction command by setting compact: true.

Example using Markdown (.opencode/command/handover.md):

---
description: handover to new agent
compact: true
---

You're receiving a handover from a previous agent. Summarize what was done
so a new agent can pick up where they left off. Include concrete next steps.

Example using Configuration (General):

You can also configure this directly in your command configuration:

"command": {
  "handover": {
    "description": "handover to new agent",
    "compact": true,
    "template": "..."
  }
}

Then type /handover or configure auto-compaction to use it (see below).

Configure auto-compaction

By default, auto-compaction uses /compact. Want it to use your custom command instead?

Add to your .config/opencode/opencode.json:

"auto_compact_command": "handover"

Now auto-compaction will use /handover instead of /compact when the context window fills up.

What changed under the hood

The minimal approach: Reuse existing command infrastructure, no reinventing the wheel.

  1. Command precedence (packages/opencode/src/command/index.ts)

    • Built-in commands (/init, /compact, /review) register first
    • User commands (from config or markdown) override built-ins
    • Clean and simple: same pattern as other commands
  2. Config option for auto-compaction (packages/opencode/src/config/config.ts)

    • Added auto_compact_command field (optional, defaults to "compact")
    • Validates the command exists and shows a helpful error if misconfigured
    • Lets you choose which compact command auto-compaction uses
  3. Prompt plumbing (various files)

    • Added prompt parameter to the compaction flow
    • Auto-compaction dynamically fetches the command template
    • Manual compaction (via /compact or custom commands) passes the template through
    • One source of truth: packages/opencode/src/command/template/compact.txt
  4. SDK updates

    • Command type: added compact?: boolean flag
    • Session API: accepts custom prompts

Total impact: 12 files, 105 additions, 40 deletions. Mostly refactoring to thread the prompt through.

Design decisions

Following maintainer feedback from PR comments:

  • ✅ Make compact a built-in slash command (like /init)
  • ✅ User commands override built-ins via precedence
  • ✅ Reuse existing command infrastructure (no new config complexity)
  • ✅ Auto-compaction respects customizations
  • ✅ No function execution needed (pure markdown files or config)

The implementation is minimal and composable — it fits naturally into OpenCode's existing patterns.

What's left to test

  • Override /compact with a custom prompt and verify it works
  • Create a /handover command and use it manually
  • Configure auto_compact_command: handover and trigger auto-compaction
  • Verify error message when auto_compact_command points to a non-existent command
  • Check that default behavior is unchanged when no customization exists

Ready for review

@maxeonyx maxeonyx changed the title feat: add handover mode to /compact command feat: make /compact prompt configurable via compactPrompt config Nov 10, 2025
@maxeonyx maxeonyx force-pushed the feature/compact-handover-mode branch from 55d51e3 to 03211fa Compare November 10, 2025 00:34
@rekram1-node
Copy link
Collaborator

@maxeonyx have u seen how we handle /init?

I was thinking a cleaner solution here would be to make compact a built in slash command, a user could override the prompt by defining their own compact and it wouldn't require additional config fields

The main change this would require is allowing slash commands to register functions to execute, which ofc couldn't be done in json but perhaps for now that functionality would remain internal

@maxeonyx
Copy link
Author

I haven't seen that, no, but I assumed compact has special handling due to the fact that it replaces the full context window. I am very open to doing this a different way.

@maxeonyx
Copy link
Author

@rekram1-node in your suggestion, then, I would define my own /handover slash command, with a different prompt, and configured to remove the rest of the context same as /compact (and we would also make /compact be implemented the same way)

@maxeonyx maxeonyx force-pushed the feature/compact-handover-mode branch from 03211fa to 10a1998 Compare November 11, 2025 23:23
@github-actions github-actions bot force-pushed the dev branch 3 times, most recently from f1dc981 to 3e15a39 Compare November 22, 2025 18:07
@github-actions github-actions bot force-pushed the dev branch 3 times, most recently from f8ee907 to 6a9856d Compare November 27, 2025 01:29
@einarpersson
Copy link

einarpersson commented Nov 28, 2025

@rekram1-node

@maxeonyx have u seen how we handle /init?

I was thinking a cleaner solution here would be to make compact a built in slash command, a user could override the prompt by defining their own compact and it wouldn't require additional config fields

The main change this would require is allowing slash commands to register functions to execute, which ofc couldn't be done in json but perhaps for now that functionality would remain internal

I agree, I think
1.compact should be a default includes slash command
2. Default included slash commands should be able to be overriden by user custom commands
3. User custom commands should be able to run as functions, something like #4411

I would advocate for trying to build the built-in slash commands such as /compact with the sdk, in other words forcing yourself to use the same functionality that a custom plugin developer would use. That would drive a architecture which makes sure that user custom commands can be just as powerful as the ones provided by default.

@rekram1-node
Copy link
Collaborator

Yup total agreement, currently jumping between several fixes and then I can come back to these new feature prs

@maxeonyx
Copy link
Author

maxeonyx commented Dec 2, 2025

I have the new version mostly implemented but I'm on holiday atm - feel free to take it over or close as I won't able to do the final "runnable as functions" part. But I have done the "custom md file to implement custom compact-type command". I can add precedence for overriding /compact - that's a good idea.

- Add 'compact' metadata field to Command schema
- Allow users to create custom compact-type commands in .opencode/command/
- Add experimental.autoCompactCommand config to choose which command for auto-compaction
- Default /compact command uses SessionCompaction.DEFAULT_PROMPT
- UI detects compact commands and triggers with custom template
- Users can override /compact by creating their own compact.md command

This enables users to customize the compaction prompt for their specific needs,
such as creating a /handover command with detailed context for team handoffs.
@maxeonyx maxeonyx force-pushed the feature/compact-handover-mode branch from 10a1998 to 0ee7d14 Compare December 7, 2025 23:25
@maxeonyx maxeonyx marked this pull request as draft December 7, 2025 23:36
@maxeonyx maxeonyx changed the title feat: make /compact prompt configurable via compactPrompt config feat: support custom compact commands via markdown files Dec 7, 2025
@maxeonyx maxeonyx changed the title feat: support custom compact commands via markdown files feat: support custom /compact via markdown files Dec 7, 2025
- Add command precedence: user commands override built-in defaults
- Thread prompt parameter through compaction flow
- Remove experimental.autoCompactCommand config
- Simplify autocomplete handling for compact commands
- Add example handover command demonstrating custom compaction

This enables users to customize /compact or create alternatives like
/handover with custom prompts, following the same pattern as /init.
- Move handover.md example to ~/.config/opencode/
- Restore session/index.tsx from dev, re-apply only our prompt change
- Extract default compact prompt to compact.txt file (matches PROMPT_INITIALIZE pattern)
- Remove committed log files
- Remove analysis/notes files from repo

Changes are now minimal and follow existing code patterns.
…ange

Only change needed is adding compact: z.boolean().optional() to Command schema
…herits /compact command

- Make prompt parameter required at all levels (no fallbacks to DEFAULT_PROMPT)
- Auto-compaction now looks up and uses the /compact command template
- This ensures user-defined /compact commands are respected by auto-compaction
- UI validates prompt exists before calling API (falls back to default /compact)
- Remove hardcoded inline prompt text, require explicit prompt from command
This allows using a different command for auto-compact, rather than the other way of customizing auto-compaction which is overriding the default "compact"
command. If you put `"auto_compact_command": "handover"` into opencode.json, then auto-compaction will use `/handover` instead of `/compact`.
@maxeonyx maxeonyx marked this pull request as ready for review December 8, 2025 02:36
@maxeonyx maxeonyx force-pushed the feature/compact-handover-mode branch from d47ece7 to 69bcc27 Compare December 8, 2025 03:36
The prompt field should be required since:
- SessionCompaction.create requires it as input
- There's no longer a hardcoded default prompt
- It's nonsensical to compact without a prompt
Merge the two sections into one comprehensive Compact section that explains both the config option and the compaction feature (manual and automatic).
Explain compaction concept first, then manual vs automatic modes, then configuration details.
Manual compaction doesn't have defaults - only auto-compaction has a default command.
Keep only the command definition example, config details are in the config page.
Use ~/.config/opencode/command/ instead of .opencode/command/ to match the global command location.
Per-project path is more appropriate for documentation examples.
Log error details before throwing to maintain consistency with codebase patterns.
@maxeonyx
Copy link
Author

maxeonyx commented Dec 8, 2025

@rekram1-node @einarpersson This PR is ready for your review, hope it's what you are imagining. It has turned out pretty clean I think.

@maxeonyx maxeonyx force-pushed the feature/compact-handover-mode branch from 5370382 to 86c29ae Compare December 11, 2025 02:16
@maxeonyx maxeonyx force-pushed the feature/compact-handover-mode branch from bd56afe to ec204d7 Compare December 12, 2025 01:23
@maxeonyx maxeonyx changed the title feat: support custom /compact via markdown files feat: support custom /compact commands via configuration Dec 12, 2025
@maxeonyx maxeonyx changed the title feat: support custom /compact commands via configuration feat: support custom /compact commands Dec 12, 2025
- Hide compact commands when no session is active
- Clean up autocomplete loops and remove type assertions
- Add compact field to Command type in SDK v2
- Resolve variable shadowing in autocomplete and session
- Add Config import for context overflow compaction logic
- Improve fuzzy search scoring for prefix matches
Resolve conflicts:
- dialog-select.tsx: Keep both gutter field and data parameter
- compaction.ts: Keep Agent import and PROMPT_COMPACT import
@maxeonyx maxeonyx force-pushed the feature/compact-handover-mode branch from b574f2b to 6df3686 Compare December 16, 2025 03:07
@maxeonyx
Copy link
Author

@rekram1-node Keeping this up-to date is becoming a hassle - would you be able to review?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants