Skip to content

Commit 63873cf

Browse files
committed
feat: refactor context system for modularity and quick chat improvements
- Refactored context system to use static modules (ChatContext, QuickChatContext, BaseContext) for better modularity and testability. - Removed legacy context instance class and JSON/plain-text formatters; now each context type has its own implementation. - Updated quick chat formatting, increased flexibility for selection and diagnostics context, and improved whitespace-tolerant patching. - Improved README: clarified quick chat, moved acknowledgements, added links and instructions for prompt guard. - Enhanced test coverage—especially for flexible whitespace patch application.
1 parent 33d4d31 commit 63873cf

File tree

14 files changed

+1335
-906
lines changed

14 files changed

+1335
-906
lines changed

README.md

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
# 🤖 opencode.nvim
22

3+
> neovim frontend for opencode - a terminal-based AI coding agent
4+
5+
## Main Features
6+
7+
### Chat Panel
8+
39
<div align="center">
410
<img src="https://raw.githubusercontent.com/sst/opencode/dev/packages/web/src/assets/logo-ornate-dark.svg" alt="Opencode logo" width="30%" />
511
</div>
612

7-
## Quick buffer chat
13+
### Quick buffer chat (<leader>o/) EXPERIMENTAL:
14+
15+
This is an experimental feature that allows you to chat with the AI using the current buffer context. In visual mode, it captures the selected text as context, while in normal mode, it uses the current line. The AI will respond with quick edits to the files that are applied by the plugin.
16+
17+
Don't hesitate to give it a try and provide feedback!
18+
19+
Refer to the [Quick Chat](#-quick-chat) section for more details.
820

921
<div align="center">
1022
<img src="https://i.imgur.com/5JNlFZn.png">
1123
</div>
1224

13-
> neovim frontend for opencode - a terminal-based AI coding agent
14-
1525
<div align="center">
1626

1727
![Neovim](https://img.shields.io/badge/NeoVim-%2357A143.svg?&style=for-the-badge&logo=neovim&logoColor=white)
@@ -20,11 +30,6 @@
2030

2131
</div>
2232

23-
## 🙏 Acknowledgements
24-
25-
This plugin is a fork of the original [goose.nvim](https://github.com/azorng/goose.nvim) plugin by [azorng](https://github.com/azorng/)
26-
For git history purposes the original code is copied instead of just forked.
27-
2833
## ✨ Description
2934

3035
This plugin provides a bridge between neovim and the [opencode](https://github.com/sst/opencode) AI agent, creating a chat interface while capturing editor context (current file, selections) to enhance your prompts. It maintains persistent sessions tied to your workspace, allowing for continuous conversations with the AI assistant similar to what tools like Cursor AI offer.
@@ -44,6 +49,8 @@ This plugin provides a bridge between neovim and the [opencode](https://github.c
4449
- [Agents](#-agents)
4550
- [User Commands](#user-commands)
4651
- [Contextual Actions for Snapshots](#-contextual-actions-for-snapshots)
52+
- [Prompt Guard](#-prompt-guard)
53+
- [Quick Chat](#-quick-chat)
4754
- [Setting up opencode](#-setting-up-opencode)
4855

4956
## ⚠️Caution
@@ -636,6 +643,21 @@ The plugin defines several highlight groups that can be customized to match your
636643

637644
The `prompt_guard` configuration option allows you to control when prompts can be sent to Opencode. This is useful for preventing accidental or unauthorized AI interactions in certain contexts.
638645

646+
### Configuration
647+
648+
Set `prompt_guard` to a function that returns a boolean:
649+
650+
```lua
651+
require('opencode').setup({
652+
prompt_guard = function()
653+
-- Your custom logic here
654+
-- Return true to allow, false to deny
655+
return true
656+
end,
657+
})
658+
659+
```
660+
639661
## 🪝 Custom user hooks
640662

641663
You can define custom functions to be called at specific events in Opencode:
@@ -668,20 +690,6 @@ require('opencode').setup({
668690
})
669691
```
670692

671-
### Configuration
672-
673-
Set `prompt_guard` to a function that returns a boolean:
674-
675-
```lua
676-
require('opencode').setup({
677-
prompt_guard = function()
678-
-- Your custom logic here
679-
-- Return true to allow, false to deny
680-
return true
681-
end,
682-
})
683-
```
684-
685693
### Behavior
686694

687695
- **Before sending prompts**: The guard is checked before any prompt is sent to the AI. If denied, an ERROR notification is shown and the prompt is not sent.
@@ -694,6 +702,8 @@ require('opencode').setup({
694702
Quick chat allows you to start a temporary opencode session with context from the current line or selection.
695703
This is optimized for narrow code edits or insertion. When the request is complex it will and require more context, it is recommended to use the full opencode UI.
696704

705+
Due to the narrow context the resulting may be less accurate and edits may sometime fails. For best results, try to keep the request focused and simple.
706+
697707
### Starting a quick chat
698708

699709
Press `<leader>o/` in normal mode to open a quick chat input window.
@@ -728,3 +738,8 @@ If you're new to opencode:
728738
3. **Configuration:**
729739
- Run `opencode auth login` to set up your LLM provider
730740
- Configure your preferred LLM provider and model in the `~/.config/opencode/config.json` or `~/.config/opencode/opencode.json` file
741+
742+
## 🙏 Acknowledgements
743+
744+
This plugin is a fork of the original [goose.nvim](https://github.com/azorng/goose.nvim) plugin by [azorng](https://github.com/azorng/)
745+
For git history purposes the original code is copied instead of just forked.

lua/opencode/api.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ function M.quick_chat(message, range)
127127

128128
if not message or #message == 0 then
129129
vim.ui.input({ prompt = 'Quick Chat Message: ', win = { relative = 'cursor' } }, function(input)
130-
local prompt, ctx = util.parse_quick_context_args(input)
131130
if input and input ~= '' then
131+
local prompt, ctx = util.parse_quick_context_args(input)
132132
quick_chat.quick_chat(prompt, { context_config = ctx }, range)
133133
end
134134
end)
@@ -1051,7 +1051,7 @@ M.commands = {
10511051
local title = table.concat(vim.list_slice(args, 2), ' ')
10521052
M.rename_session(state.active_session, title)
10531053
else
1054-
local valid_subcmds = table.concat(M.commands.session.completions, ', ')
1054+
local valid_subcmds = table.concat(M.commands.session.completions or {}, ', ')
10551055
vim.notify('Invalid session subcommand. Use: ' .. valid_subcmds, vim.log.levels.ERROR)
10561056
end
10571057
end,
@@ -1081,7 +1081,7 @@ M.commands = {
10811081
elseif subcmd == 'close' then
10821082
M.diff_close()
10831083
else
1084-
local valid_subcmds = table.concat(M.commands.diff.completions, ', ')
1084+
local valid_subcmds = table.concat(M.commands.diff.completions or {}, ', ')
10851085
vim.notify('Invalid diff subcommand. Use: ' .. valid_subcmds, vim.log.levels.ERROR)
10861086
end
10871087
end,
@@ -1160,7 +1160,7 @@ M.commands = {
11601160
elseif subcmd == 'select' then
11611161
M.select_agent()
11621162
else
1163-
local valid_subcmds = table.concat(M.commands.agent.completions, ', ')
1163+
local valid_subcmds = table.concat(M.commands.agent.completions or {}, ', ')
11641164
vim.notify('Invalid agent subcommand. Use: ' .. valid_subcmds, vim.log.levels.ERROR)
11651165
end
11661166
end,
@@ -1248,7 +1248,7 @@ M.commands = {
12481248
elseif subcmd == 'deny' then
12491249
M.permission_deny()
12501250
else
1251-
local valid_subcmds = table.concat(M.commands.permission.completions, ', ')
1251+
local valid_subcmds = table.concat(M.commands.permission.completions or {}, ', ')
12521252
vim.notify('Invalid permission subcommand. Use: ' .. valid_subcmds, vim.log.levels.ERROR)
12531253
end
12541254
end,

lua/opencode/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ M.defaults = {
222222
},
223223
quick_chat = {
224224
default_model = nil,
225-
default_agent = 'plan', -- plan ensure no file modifications by default
225+
default_agent = nil,
226226
instructions = nil, -- Use instructions prompt by default
227227
},
228228
}

0 commit comments

Comments
 (0)