-
Notifications
You must be signed in to change notification settings - Fork 5.3k
feat: support custom /compact commands
#4149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
55d51e3 to
03211fa
Compare
772b621 to
eb855e1
Compare
|
@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 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. |
|
@rekram1-node in your suggestion, then, I would define my own |
03211fa to
10a1998
Compare
f1dc981 to
3e15a39
Compare
f8ee907 to
6a9856d
Compare
I agree, I think 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. |
|
Yup total agreement, currently jumping between several fixes and then I can come back to these new feature prs |
|
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.
10a1998 to
0ee7d14
Compare
/compact via markdown files
- 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`.
d47ece7 to
69bcc27
Compare
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.
|
@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. |
5370382 to
86c29ae
Compare
bd56afe to
ec204d7
Compare
/compact via markdown files/compact commands
- 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
b574f2b to
6df3686
Compare
|
@rekram1-node Keeping this up-to date is becoming a hassle - would you be able to review? |
What's this?
Makes
/compactcustomizable! 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
/compactYou can define commands in your configuration (e.g.,
.opencode/command/compact.mdor viaopencode.jsonconfiguration) with thecompactproperty.Example using Markdown (
.opencode/command/compact.md):Now when you hit
<leader>cor 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):Example using Configuration (General):
You can also configure this directly in your command configuration:
Then type
/handoveror 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:Now auto-compaction will use
/handoverinstead of/compactwhen the context window fills up.What changed under the hood
The minimal approach: Reuse existing command infrastructure, no reinventing the wheel.
Command precedence (
packages/opencode/src/command/index.ts)/init,/compact,/review) register firstConfig option for auto-compaction (
packages/opencode/src/config/config.ts)auto_compact_commandfield (optional, defaults to"compact")Prompt plumbing (various files)
promptparameter to the compaction flow/compactor custom commands) passes the template throughpackages/opencode/src/command/template/compact.txtSDK updates
Commandtype: addedcompact?: booleanflagTotal impact: 12 files, 105 additions, 40 deletions. Mostly refactoring to thread the prompt through.
Design decisions
Following maintainer feedback from PR comments:
/init)The implementation is minimal and composable — it fits naturally into OpenCode's existing patterns.
What's left to test
/compactwith a custom prompt and verify it works/handovercommand and use it manuallyauto_compact_command: handoverand trigger auto-compactionauto_compact_commandpoints to a non-existent commandReady for review