Skip to content

Claude/add prompt before hook 01 sab pegi wd k3 j mk2p6u4 g hx#4403

Closed
analoguezone wants to merge 11 commits into
anomalyco:devfrom
analoguezone:claude/add-prompt-before-hook-01SabPegiWdK3JMk2p6u4GHx
Closed

Claude/add prompt before hook 01 sab pegi wd k3 j mk2p6u4 g hx#4403
analoguezone wants to merge 11 commits into
anomalyco:devfrom
analoguezone:claude/add-prompt-before-hook-01SabPegiWdK3JMk2p6u4GHx

Conversation

@analoguezone
Copy link
Copy Markdown

No description provided.

This commit implements a new plugin hook `prompt.before` that fires BEFORE
OpenCode sends a prompt to the LLM, enabling powerful plugin capabilities:

Features:
- Inspect prompt text and session context before LLM call
- Override model selection dynamically based on task complexity
- Inject additional context into prompts
- Block prompts based on custom validation logic

Implementation:
- Added hook type definition in packages/plugin/src/index.ts
- Integrated hook execution in packages/opencode/src/session/prompt.ts
- Hook fires after agent resolution but before model selection
- Plugins can modify model, add context, or block prompts

Test Plugins:
- PromptLoggerPlugin: Logs all prompts for debugging
- ModelSwitcherPlugin: Dynamic model selection based on complexity
- PromptBlockerPlugin: Content filtering and validation

Example Use Cases:
- Cost optimization by routing simple tasks to cheaper models
- Security filtering of sensitive content
- Task-specific model selection (coding vs chat)
- Prompt enhancement with additional context
- Rate limiting and quota management

Breaking Changes: None
Backward Compatible: Yes (optional hook)

Resolves: Dynamic model selection requirement
Related: OpenCode plugin system enhancement
…n access

Implemented a user-controlled toggle for model optimization that:
- Shows OPTIMIZE: ON/OFF in the status bar (bottom, next to model info)
- Provides toggle command via command palette (Cmd+O)
- Makes state available to plugins via prompt.before hook
- Persists state across sessions to ~/.opencode/state/optimize.json

Changes:
- Added optimize state management to local context (TUI)
- Updated status bar to display optimization state with color coding
- Added toggle command to command palette under Agent category
- Extended PromptInput schema to include optimizeEnabled field
- Updated prompt.before hook to pass optimization state to plugins
- Updated plugin types to include optimizeEnabled in input

Plugins can now check input.optimizeEnabled and skip model switching
when disabled, giving users full control over optimization behavior.

Default state: ON (optimization enabled)
- Move wrapMode from box to text elements
- Remove keybind field (not in KeybindsConfig type)
- Command still accessible via command palette
Changed default keybinding for opening command palette from Ctrl+P to Ctrl+O
- Added optimize_toggle keybinding with Ctrl+O default
- Reverted command_list back to Ctrl+P
- Users can now press Ctrl+O to instantly toggle optimization on/off
- Toggle still accessible via command palette

Note: SDK types will be regenerated on next server run/build
The optimizeEnabled field was not being sent from the TUI client to the
server, causing plugins to always receive true (the default fallback).

Now correctly passes local.optimize.enabled to the server in the prompt
request body, so plugins can properly check the user's toggle state.

Note: SDK types will regenerate on next run to include optimizeEnabled field
…mization toggle

Added two detailed documentation files:

1. PROMPT_BEFORE_HOOK.md
   - Complete hook API reference
   - Input/output parameters
   - Execution order and timing
   - Multiple practical examples (logger, optimizer, filter, context injector)
   - Best practices and performance tips
   - Comparison with chat.params hook
   - Use cases and troubleshooting

2. OPTIMIZATION_TOGGLE.md
   - User guide for the optimization toggle feature
   - Ctrl+O keybinding documentation
   - Status bar display explanation
   - When to use ON vs OFF
   - Plugin integration guide
   - Persistent state details
   - FAQ and troubleshooting

Both docs are ready for users and plugin developers.
Created BUILD.md with complete instructions for:
- Quick build (--single flag for current platform)
- Full build (all platforms)
- Testing the binary
- Installing globally (copy, symlink, or PATH)
- Build output structure
- Differences between bun dev and binary
- Development workflow
- Troubleshooting common issues
- Platform-specific notes
- CI/CD integration

Users can now easily build and distribute OpenCode binaries.
@rekram1-node
Copy link
Copy Markdown
Collaborator

im not really sure what this means

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