|
| 1 | +# Model Optimization Toggle Feature |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Added a toggle to control model optimization in OpenCode with status bar display and plugin access. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +### 1. Status Bar Display |
| 10 | +The bottom status bar now shows the optimization state next to the model info: |
| 11 | +``` |
| 12 | +anthropic claude-sonnet-4-5 OPTIMIZE: ON |
| 13 | +``` |
| 14 | + |
| 15 | +- **Green "ON"** when enabled |
| 16 | +- **Gray "OFF"** when disabled |
| 17 | + |
| 18 | +### 2. Toggle Command |
| 19 | +Access via command palette (`Cmd+O` or configured keybind): |
| 20 | +- Command: "Toggle model optimization (ON/OFF)" |
| 21 | +- Category: Agent |
| 22 | +- Keybind: `optimize_toggle` (can be configured in keybindings) |
| 23 | + |
| 24 | +### 3. Plugin Access |
| 25 | +Plugins can now access the optimization state via the `prompt.before` hook: |
| 26 | + |
| 27 | +```typescript |
| 28 | +export const MyPlugin = async () => { |
| 29 | + return { |
| 30 | + "prompt.before": async (input, output) => { |
| 31 | + // Check if optimization is enabled |
| 32 | + if (input.optimizeEnabled) { |
| 33 | + // Apply optimizations |
| 34 | + if (input.prompt.includes("simple")) { |
| 35 | + output.model = { |
| 36 | + providerID: "anthropic", |
| 37 | + modelID: "claude-3-5-haiku-20241022" |
| 38 | + } |
| 39 | + } |
| 40 | + } else { |
| 41 | + // Skip optimization - use default model |
| 42 | + console.log("Optimization disabled, using default model") |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### 4. Persistent State |
| 50 | +The toggle state is saved to `~/.opencode/state/optimize.json` and persists across sessions. |
| 51 | + |
| 52 | +## Implementation Details |
| 53 | + |
| 54 | +### Files Modified |
| 55 | + |
| 56 | +1. **packages/opencode/src/cli/cmd/tui/context/local.tsx** |
| 57 | + - Added `optimize` state management |
| 58 | + - Provides `enabled`, `toggle()`, and `set()` methods |
| 59 | + - Persists state to disk |
| 60 | + |
| 61 | +2. **packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx** |
| 62 | + - Added "OPTIMIZE: ON/OFF" display in status bar |
| 63 | + - Shows next to model info with color coding |
| 64 | + |
| 65 | +3. **packages/opencode/src/cli/cmd/tui/app.tsx** |
| 66 | + - Added "Toggle model optimization" command |
| 67 | + - Accessible via command palette |
| 68 | + |
| 69 | +4. **packages/opencode/src/session/prompt.ts** |
| 70 | + - Added `optimizeEnabled` to `PromptInput` schema |
| 71 | + - Passes state to `prompt.before` hook |
| 72 | + |
| 73 | +5. **packages/plugin/src/index.ts** |
| 74 | + - Added `optimizeEnabled: boolean` to `prompt.before` input type |
| 75 | + - Plugins can now check optimization state |
| 76 | + |
| 77 | +## Usage |
| 78 | + |
| 79 | +### For Users |
| 80 | + |
| 81 | +1. **Toggle optimization:** |
| 82 | + - Press `Cmd+O` (or your command palette key) |
| 83 | + - Type "toggle model" |
| 84 | + - Select "Toggle model optimization" |
| 85 | + - Or configure a dedicated keybinding for `optimize_toggle` |
| 86 | + |
| 87 | +2. **Check current state:** |
| 88 | + - Look at the bottom status bar |
| 89 | + - You'll see "OPTIMIZE: ON" (green) or "OPTIMIZE: OFF" (gray) |
| 90 | + |
| 91 | +### For Plugin Developers |
| 92 | + |
| 93 | +Update your plugin to respect the optimization toggle: |
| 94 | + |
| 95 | +```typescript |
| 96 | +export const ModelOptimizerPlugin = async () => { |
| 97 | + return { |
| 98 | + "prompt.before": async (input, output) => { |
| 99 | + // Only optimize if user has it enabled |
| 100 | + if (!input.optimizeEnabled) { |
| 101 | + return // Skip optimization |
| 102 | + } |
| 103 | + |
| 104 | + // Your optimization logic here |
| 105 | + if (isSimpleTask(input.prompt)) { |
| 106 | + output.model = cheapModel |
| 107 | + } else if (isComplexTask(input.prompt)) { |
| 108 | + output.model = powerfulModel |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Example Plugin |
| 116 | + |
| 117 | +```typescript |
| 118 | +export const TestPlugin = async () => { |
| 119 | + console.log("🎯 Test Plugin Loaded!") |
| 120 | + |
| 121 | + return { |
| 122 | + "prompt.before": async (input: any, output: any) => { |
| 123 | + console.log("\n" + "=".repeat(60)) |
| 124 | + console.log("🔥 PROMPT.BEFORE HOOK FIRED!") |
| 125 | + console.log("=".repeat(60)) |
| 126 | + console.log("Session:", input.sessionID) |
| 127 | + console.log("Agent:", input.agent) |
| 128 | + console.log("Prompt:", input.prompt) |
| 129 | + console.log("Optimize Enabled:", input.optimizeEnabled ? "✅ ON" : "❌ OFF") |
| 130 | + console.log("=".repeat(60) + "\n") |
| 131 | + |
| 132 | + // Only apply model optimization if enabled |
| 133 | + if (input.optimizeEnabled) { |
| 134 | + if (input.prompt.toLowerCase().includes("simple")) { |
| 135 | + output.model = { |
| 136 | + providerID: "anthropic", |
| 137 | + modelID: "claude-3-5-haiku-20241022" |
| 138 | + } |
| 139 | + console.log("✅ SWITCHED TO HAIKU (optimization enabled)\n") |
| 140 | + } |
| 141 | + } else { |
| 142 | + console.log("⏭️ Skipping optimization (disabled by user)\n") |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +## Testing |
| 150 | + |
| 151 | +1. **Start OpenCode:** |
| 152 | + ```bash |
| 153 | + cd ~/Development/ai/opencode-auto |
| 154 | + bun dev |
| 155 | + ``` |
| 156 | + |
| 157 | +2. **Check initial state:** |
| 158 | + - Bottom status bar should show "OPTIMIZE: ON" (default) |
| 159 | + |
| 160 | +3. **Toggle it:** |
| 161 | + - Press `Cmd+O` |
| 162 | + - Type "toggle model" |
| 163 | + - Select the command |
| 164 | + - Status bar should update to "OPTIMIZE: OFF" |
| 165 | + - Toast message confirms the change |
| 166 | + |
| 167 | +4. **Test with plugin:** |
| 168 | + - Send prompt: "This is a simple task" |
| 169 | + - With OPTIMIZE ON: Should switch to Haiku |
| 170 | + - With OPTIMIZE OFF: Should use default model |
| 171 | + |
| 172 | +## Benefits |
| 173 | + |
| 174 | +1. **User Control**: Users can enable/disable model optimization without changing plugins |
| 175 | +2. **Cost Savings**: Users can turn off optimization to always use their preferred model |
| 176 | +3. **Debugging**: Easy to test with/without optimization |
| 177 | +4. **Plugin Respect**: Plugins can check the user's preference instead of forcing optimization |
| 178 | + |
| 179 | +## Next Steps |
| 180 | + |
| 181 | +- Add keybinding configuration documentation |
| 182 | +- Consider adding optimization profiles (aggressive/balanced/conservative) |
| 183 | +- Add metrics tracking (how much $ saved with optimization ON) |
0 commit comments