-
-
Notifications
You must be signed in to change notification settings - Fork 161
feat: support OpenCode v2 #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e9eaa4b
feat: support OpenCode v2
metal3d 7ae4a0a
fix: address PR review (v2 routing, discovery, panel)
metal3d 2875ae6
fix: address second review pass
metal3d 41a7429
Merge remote-tracking branch 'upstream/main' into feat/opencode-v2
metal3d 22fdb16
fix(server): prefer Neovim cwd over service directory for routing
metal3d e867d4b
fix(panel): keep sessions in the project directory
metal3d 44e9eb3
fix(panel): resume the last open tab instead of a fresh session
metal3d e05e253
refactor: share tabs.json state helpers; drop dead session short-circuit
metal3d 19082f5
chore: document leftover session-directory TODOs
metal3d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,116 @@ | ||
| local M = {} | ||
|
|
||
| local Promise = require("opencode.promise") | ||
|
|
||
| ---Commands that only the legacy `/tui/*` control endpoints can perform. | ||
| ---These have no OpenCode v2 HTTP API equivalent (scroll, navigation, prompt box). | ||
| ---@type table<string, boolean> | ||
| local TUI_ONLY = { | ||
| ["prompt.clear"] = true, | ||
| ["prompt.submit"] = true, | ||
| ["session.first"] = true, | ||
| ["session.last"] = true, | ||
| ["session.half.page.up"] = true, | ||
| ["session.half.page.down"] = true, | ||
| ["session.page.up"] = true, | ||
| ["session.page.down"] = true, | ||
| ["session.select"] = true, | ||
| ["session.share"] = true, | ||
| ["session.redo"] = true, | ||
| ["session.undo"] = true, | ||
| } | ||
|
|
||
| ---Cycle the active session's agent through the visible primary agents. | ||
| --- | ||
| ---@param server opencode.server.Server | ||
| ---@return Promise<any> | ||
| local function cycle_agent(server) | ||
| return server:resolve_session_id():next(function(session_id) | ||
| return Promise.all({ | ||
| server:get_sessions(), | ||
| server:get_agents(), | ||
| }):next(function(results) | ||
| local sessions, agents = results[1], results[2] | ||
|
|
||
| -- The session's current agent is only exposed on list items. | ||
| local current | ||
| for _, session in ipairs(sessions) do | ||
| if session.id == session_id then | ||
| current = session.agent | ||
| break | ||
| end | ||
| end | ||
|
|
||
| local primary = vim.tbl_filter(function(agent) ---@param agent { id: string, mode: string, hidden?: boolean } | ||
| return agent.mode == "primary" and not agent.hidden | ||
| end, agents or {}) | ||
|
|
||
| if #primary == 0 then | ||
| return Promise.resolve(nil) | ||
| end | ||
|
|
||
| local next_agent = primary[1] | ||
| for index, agent in ipairs(primary) do | ||
| if agent.id == current then | ||
| next_agent = primary[index % #primary + 1] | ||
| break | ||
| end | ||
| end | ||
|
|
||
| return server:switch_agent(session_id, next_agent.id) | ||
| end) | ||
| end) | ||
| end | ||
|
|
||
| ---Execute a built-in OpenCode command. | ||
| --- | ||
| ---Commands map onto v2 HTTP API calls where possible; TUI-only commands | ||
| ---(scroll, navigation, prompt box) require the legacy `/tui/*` endpoints and | ||
| ---degrade to a silent no-op when the connected server does not expose them. | ||
| --- | ||
| ---@param command opencode.server.Command | string | ||
| ---@param server opencode.server.Server | ||
| ---@return Promise<any> | ||
| function M.command(command, server) | ||
| return server:tui_execute_command(command):next(function() | ||
| if command == "session.interrupt" then | ||
| -- Evidently OpenCode only uses this command for their "double-tap Esc to interrupt" user keybind. | ||
| -- So we have to double-send it to actually interrupt. | ||
| return server:tui_execute_command(command) | ||
| if command == "session.new" then | ||
| -- TODO: `create_session()` POSTs /api/session, which on the shared service | ||
| -- always creates the session in the service's ambient cwd (often ~) — the | ||
| -- same limitation as the retired panel session. Revisit once opencode lets | ||
| -- the API place a session in a target directory. | ||
| return server:create_session():next(function(created) | ||
| return Promise.resolve(created) | ||
| end) | ||
| elseif command == "session.interrupt" then | ||
| return server:resolve_session_id():next(function(session_id) | ||
| return server:interrupt(session_id) | ||
| end) | ||
| elseif command == "session.compact" then | ||
| return server:resolve_session_id():next(function(session_id) | ||
| return server:compact(session_id) | ||
| end) | ||
| elseif command == "agent.cycle" then | ||
| return cycle_agent(server) | ||
| end | ||
|
|
||
| if TUI_ONLY[command] then | ||
| if not server.tui then | ||
| -- No `/tui/*` on OpenCode v2.0.x: these have no API equivalent. | ||
| return Promise.resolve(nil) | ||
| end | ||
| end) | ||
| return server:tui_execute_command(command):next(function() | ||
| if command == "session.interrupt" then | ||
| -- Evidently OpenCode only uses this command for their "double-tap Esc to interrupt" user keybind. | ||
| -- So we have to double-send it to actually interrupt. | ||
| return server:tui_execute_command(command) | ||
| end | ||
| end) | ||
| end | ||
|
|
||
| -- Unknown command: forward to the TUI when available, otherwise surface it. | ||
| if server.tui then | ||
| return server:tui_execute_command(command) | ||
| end | ||
| return Promise.reject("Unknown OpenCode command: `" .. command .. "`") | ||
| end | ||
|
|
||
| return M |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.