You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow configuration of external opencode serve process (#295)
* feature: Allow configuration of external opencode serve process (rather than spawning automatically). This lets a developer connect to a containerized or other remote opencode server instance.
---------
Co-authored-by: Francis Belanger <francis.belanger@gmail.com>
default_system_prompt=nil, -- Custom system prompt to use for all sessions. If nil, uses the default built-in system prompt
121
125
keymap_prefix='<leader>o', -- Default keymap prefix for global keymaps change to your preferred prefix and it will be applied to all keymaps starting with <leader>o
122
126
opencode_executable='opencode', -- Name of your opencode binary
127
+
128
+
-- Server configuration for custom/external opencode servers
port=nil, -- Port number (e.g., 8080), 'auto' for random port
132
+
timeout=5, -- Health check timeout in seconds when connecting
133
+
spawn_command=nil, -- Optional function to start the server: function(port, url) ... end
134
+
auto_kill=true, -- Kill spawned servers when last nvim instance exits (default: true) Only applies to servers spawned by the plugin with spawn_command/kill_command
135
+
path_map=nil, -- Map host paths to server paths: string ('/app') or function(path) -> string
136
+
},
137
+
123
138
keymap= {
124
139
editor= {
125
140
['<leader>og'] = { 'toggle' }, -- Open opencode. Close if opened
@@ -757,6 +772,177 @@ You can create custom agents through your opencode config file. Each agent can h
757
772
758
773
See [Opencode Agents Documentation](https://opencode.ai/docs/agents/) for full configuration options.
759
774
775
+
## 🔌 Custom/External Server Configuration
776
+
777
+
By default, opencode.nvim spawns a local `opencode serve` process. You can instead connect to an external or containerized opencode server by configuring the `server` table.
778
+
779
+
### Basic Connection
780
+
781
+
Connect to an existing server:
782
+
783
+
```lua
784
+
require('opencode').setup({
785
+
server= {
786
+
url='localhost', -- or 'http://192.168.1.100'
787
+
port=8080,
788
+
timeout=5,
789
+
},
790
+
})
791
+
```
792
+
793
+
### Auto-Spawning with Docker
794
+
795
+
Use `spawn_command` to automatically start your server and `kill_command` to stop it:
796
+
797
+
```lua
798
+
require('opencode').setup({
799
+
server= {
800
+
url='localhost',
801
+
port='auto', -- Random port for project isolation
802
+
-- Path mapping: translate host paths to container paths
803
+
path_map=function(host_path)
804
+
localcwd=vim.fn.getcwd()
805
+
-- Replace host project directory with container mount point
806
+
returnhost_path:gsub(vim.pesc(cwd), '/app')
807
+
end,
808
+
-- Spawn command: start Docker container with opencode server
auto_kill=true, -- Kill server when last nvim instance exits
923
+
},
924
+
})
925
+
```
926
+
927
+
### Configuration Options
928
+
929
+
-`url` (string | nil): Server hostname/URL (e.g., 'localhost', 'http://192.168.1.100')
930
+
-`port` (number | 'auto' | nil): Port number, `'auto'` for random port, or nil for default (4096)
931
+
-`timeout` (number): Health check timeout in seconds (default: 5)
932
+
-`spawn_command` (function | nil): Optional function to start server: `function(port, url) ... end`
933
+
-`kill_command` (function | nil): Optional function to stop server when `auto_kill` triggers: `function(port, url) ... end`
934
+
-`auto_kill` (boolean): Kill spawned servers when last nvim instance exits (default: true)
935
+
-`path_map` (string | function | nil): Transform host paths to server paths (for outgoing requests)
936
+
-`reverse_path_map` (function | nil): Transform server paths back to host paths (for incoming responses/events)
937
+
938
+
### Multi-Instance Support
939
+
940
+
When `port = 'auto'` is used, opencode.nvim:
941
+
942
+
- Tracks which nvim instances are using each port
943
+
- Only kills the server when the last nvim instance exits (if `auto_kill = true`). Only applies to servers spawned by the plugin with `spawn_command`/`kill_command`.
944
+
- Locally spawned servers will be killed automatically regardless of the auto_kill setting if they are the last nvim instance using them
945
+
760
946
## User Commands and Slash Commands
761
947
762
948
You can run predefined user commands and built-in slash commands from the input window by typing `/`. This opens a command picker where you can select a command to execute. The output of the command will be included in your prompt context.
0 commit comments