From 25010ab8076daaffdbbef69f71ddc37a1a8023bd Mon Sep 17 00:00:00 2001 From: Juan Peri Date: Tue, 21 Apr 2026 23:43:58 +0200 Subject: [PATCH] fix(server): send x-opencode-directory: nvim-cwd on every request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Routes tui.* events to the TUI whose --dir matches Neovim's cwd. In single-TUI / integrated-mode setups this matches the only TUI trivially; in multi-TUI setups (opencode serve + several `opencode attach --dir ...`) this ensures `tui.prompt.append` and `tui.command.execute` land in the intended TUI instead of silently no-op-ing. Background: opencode's server publishes TUI events on its internal bus, and each TUI's event subscription filters by request-instance directory (packages/opencode/src/cli/cmd/tui/context/event.ts). Without the header, events carry the server's ambient cwd, so none of the attached TUIs see them. Side effect: /path and /agent responses are also now request-scoped to Neovim's cwd. That makes Server.cwd reflect the user's editor context, which is strictly more useful for the existing "pick the server sharing my cwd" heuristic in select_server.lua. Closes #190 partially — this unblocks `opencode serve` + `opencode attach` setups without introducing a session-based API. --- lua/opencode/server/init.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lua/opencode/server/init.lua b/lua/opencode/server/init.lua index 358ece10..aca522ce 100644 --- a/lua/opencode/server/init.lua +++ b/lua/opencode/server/init.lua @@ -137,6 +137,16 @@ function Server:curl(path, method, body, on_success, on_error, opts) table.insert(cmd, 2) end + -- Route the request to the TUI whose working directory matches Neovim's. + -- In single-TUI / integrated-mode setups this matches the only TUI trivially. + -- In multi-TUI setups (`opencode serve` + several `opencode attach --dir ...`) + -- this ensures `tui.*` events are delivered to the intended TUI. + local nvim_cwd = vim.fn.getcwd() + if nvim_cwd and nvim_cwd ~= "" then + table.insert(cmd, "-H") + table.insert(cmd, "x-opencode-directory: " .. nvim_cwd) + end + if body then table.insert(cmd, "-d") table.insert(cmd, vim.fn.json_encode(body))