Skip to content

Commit 350b69d

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 07eaaf4 commit 350b69d

File tree

15 files changed

+1336
-907
lines changed

15 files changed

+1336
-907
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
@@ -633,6 +640,21 @@ The plugin defines several highlight groups that can be customized to match your
633640

634641
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.
635642

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

638660
You can define custom functions to be called at specific events in Opencode:
@@ -665,20 +687,6 @@ require('opencode').setup({
665687
})
666688
```
667689

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

684692
- **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.
@@ -691,6 +699,8 @@ require('opencode').setup({
691699
Quick chat allows you to start a temporary opencode session with context from the current line or selection.
692700
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.
693701

702+
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.
703+
694704
### Starting a quick chat
695705

696706
Press `<leader>o/` in normal mode to open a quick chat input window.
@@ -725,3 +735,8 @@ If you're new to opencode:
725735
3. **Configuration:**
726736
- Run `opencode auth login` to set up your LLM provider
727737
- Configure your preferred LLM provider and model in the `~/.config/opencode/config.json` or `~/.config/opencode/opencode.json` file
738+
739+
## 🙏 Acknowledgements
740+
741+
This plugin is a fork of the original [goose.nvim](https://github.com/azorng/goose.nvim) plugin by [azorng](https://github.com/azorng/)
742+
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)
@@ -1038,7 +1038,7 @@ M.commands = {
10381038
local title = table.concat(vim.list_slice(args, 2), ' ')
10391039
M.rename_session(state.active_session, title)
10401040
else
1041-
local valid_subcmds = table.concat(M.commands.session.completions, ', ')
1041+
local valid_subcmds = table.concat(M.commands.session.completions or {}, ', ')
10421042
vim.notify('Invalid session subcommand. Use: ' .. valid_subcmds, vim.log.levels.ERROR)
10431043
end
10441044
end,
@@ -1068,7 +1068,7 @@ M.commands = {
10681068
elseif subcmd == 'close' then
10691069
M.diff_close()
10701070
else
1071-
local valid_subcmds = table.concat(M.commands.diff.completions, ', ')
1071+
local valid_subcmds = table.concat(M.commands.diff.completions or {}, ', ')
10721072
vim.notify('Invalid diff subcommand. Use: ' .. valid_subcmds, vim.log.levels.ERROR)
10731073
end
10741074
end,
@@ -1147,7 +1147,7 @@ M.commands = {
11471147
elseif subcmd == 'select' then
11481148
M.select_agent()
11491149
else
1150-
local valid_subcmds = table.concat(M.commands.agent.completions, ', ')
1150+
local valid_subcmds = table.concat(M.commands.agent.completions or {}, ', ')
11511151
vim.notify('Invalid agent subcommand. Use: ' .. valid_subcmds, vim.log.levels.ERROR)
11521152
end
11531153
end,
@@ -1235,7 +1235,7 @@ M.commands = {
12351235
elseif subcmd == 'deny' then
12361236
M.permission_deny()
12371237
else
1238-
local valid_subcmds = table.concat(M.commands.permission.completions, ', ')
1238+
local valid_subcmds = table.concat(M.commands.permission.completions or {}, ', ')
12391239
vim.notify('Invalid permission subcommand. Use: ' .. valid_subcmds, vim.log.levels.ERROR)
12401240
end
12411241
end,

lua/opencode/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ M.defaults = {
217217
},
218218
quick_chat = {
219219
default_model = nil,
220-
default_agent = 'plan', -- plan ensure no file modifications by default
220+
default_agent = nil,
221221
instructions = nil, -- Use instructions prompt by default
222222
},
223223
}

0 commit comments

Comments
 (0)