feat(server): Add live context capability to OpenCode - #318
Open
itsmeyaw wants to merge 9 commits into
Open
Conversation
- Implement pure Lua WebSocket server (RFC 6455) with no external dependencies - Add selection tracking with autocommands and debounced updates - Create lockfile manager for editor discovery (~/.claude/ide/[port].lock) - Add configuration options for live context feature - Provide user commands and API for programmatic control - Support both auto-start and manual activation modes - Broadcast selection_changed and at_mentioned events to OpenCode TUI - Compatible with Claude Code WebSocket protocol Components: - WebSocket server (lua/opencode/editor/) - TCP server using vim.uv - Frame encoder/decoder - Handshake with auth - Client connection management - Pure Lua utilities (SHA-1, base64, UTF-8) - Selection tracking (lua/opencode/editor/selection.lua) - CursorMoved, ModeChanged, BufEnter, TextChanged autocommands - 100ms debounced updates - Lockfile manager (lua/opencode/editor/lockfile.lua) - Atomic file creation - Stale lockfile cleanup API: - start_live_context(opts) - stop_live_context() - attach_context() Commands: - :OpenCodeLiveContextStart - :OpenCodeLiveContextStop - :OpenCodeAttach (visual mode) Configuration: - live_context.enabled (default: false) - live_context.port (default: 0 for random) - live_context.auth_token (optional)
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a “Live Context” feature to opencode.nvim, allowing Neovim to broadcast the current file/cursor/visual selection to the OpenCode TUI in real time via a local WebSocket server, discovered through a Claude Code–compatible lockfile.
Changes:
- Added a pure-Lua WebSocket server (handshake + frame encode/decode + TCP plumbing) with JSON-RPC notifications for
selection_changedandat_mentioned. - Implemented editor-side selection tracking with debounced autocmd updates, plus an attach action that emits
at_mentioned. - Exposed public API + user commands for starting/stopping live context, and documented configuration/usage in the README.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| README.md | Documents Live Context feature, config timing (init), API and commands. |
| plugin/live-context.lua | Adds :OpenCodeLiveContextStart/Stop and :OpenCodeAttach, plus auto-start/shutdown wiring. |
| lua/opencode.lua | Exposes start_live_context, stop_live_context, and attach_context in the public API. |
| lua/opencode/config.lua | Adds live_context options and LuaLS types. |
| lua/opencode/editor/selection.lua | Tracks cursor/visual selection and broadcasts updates / emits at_mentioned. |
| lua/opencode/editor/lockfile.lua | Creates/removes/cleans Claude Code–style lockfiles and generates auth tokens. |
| lua/opencode/editor/init.lua | Adds an opencode.editor module entrypoint. |
| lua/opencode/server/websocket/init.lua | Orchestrates server lifecycle, client management, and JSON-RPC broadcasts. |
| lua/opencode/server/websocket/tcp.lua | Provides libuv TCP server/client helpers. |
| lua/opencode/server/websocket/client.lua | Implements per-connection buffering, handshake parsing, and frame dispatch. |
| lua/opencode/server/websocket/frame.lua | Encodes/decodes WebSocket frames (RFC 6455). |
| lua/opencode/server/websocket/handshake.lua | Validates upgrade requests and generates the accept response (optional auth). |
| lua/opencode/server/websocket/utils.lua | Adds SHA-1/base64, header parsing, and small binary/UTF-8 helpers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tasks
Description
TL:DR; Add live context capability to OpenCode as in claude code (see claudecode.nvim as example)
This implementation adds real-time WebSocket-based live context broadcasting to opencode.nvim, enabling OpenCode TUI to track the current file/selection in Neovim.
Architecture
Components
WebSocket Server (
lua/opencode/server/websocket/)init.lua- Main server module with start/stop/broadcast functionstcp.lua- TCP server usingvim.uvframe.lua- WebSocket frame encoder/decoder (RFC 6455)handshake.lua- WebSocket upgrade handshake with authclient.lua- Client connection managementutils.lua- Pure Lua utilities (SHA-1, base64, UTF-8)Selection Tracking (
lua/opencode/editor/selection.lua)Lockfile Manager (
lua/opencode/editor/lockfile.lua)~/.claude/ide/[port].lockProtocol
JSON-RPC 2.0 Messages:
Usage
Configuration
Note: this must live inside the
initinstead ofconfigbecauselive_contextoptions should present before the plugin load.Command
:OpenCodeLiveContextStart- Start WebSocket server:OpenCodeLiveContextStop- Stop WebSocket server:'<,'>OpenCodeAttach- Attach visual selection (no submit)API
Recommended Keymap
OpenCode TUI Integration
OpenCode TUI automatically discovers the WebSocket server via:
Lockfile at
~/.claude/ide/[port].lock{ "pid": 12345, "workspaceFolders": ["/path/to/project"], "ideName": "Neovim", "transport": "ws", "authToken": "optional-token" }Environment Variable: for a fixed configured port,
OPENCODE_EDITOR_SSE_PORTorCLAUDE_CODE_SSE_PORTcan bypass lockfile discoveryImplementation Note
No External Dependencies
All WebSocket code is pure Lua using Neovim's
vim.uv(libuv) - no external Lua packages required.Compatibility
Testing
opencode)nvimand:OpenCodeLiveContextStartRelated Issue(s)
No related Issue
Screenshots/Videos
CleanShot.2026-08-07.at.18.13.56.mp4