Skip to content

Commit 80d02d2

Browse files
committed
feat(server): share process across multiple instances
Add a lock file mechanism to coordinate a single server process among multiple Neovim clients. Track ownership and active clients in opencode-server.lock to ensure the process persists until the last instance closes. Additionally, implement URI encoding for API queries.
1 parent dab6ef9 commit 80d02d2

File tree

5 files changed

+605
-28
lines changed

5 files changed

+605
-28
lines changed

lua/opencode/api_client.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,20 @@ function OpencodeApiClient:_call(endpoint, method, body, query)
5858
end
5959
local url = self.base_url .. endpoint
6060

61-
if query then
62-
local params = {}
61+
query = query or {}
62+
if query.directory == nil then
63+
query.directory = vim.fn.getcwd()
64+
end
6365

64-
for k, v in pairs(query) do
65-
if v ~= nil then
66-
table.insert(params, k .. '=' .. tostring(v))
67-
end
66+
local params = {}
67+
for k, v in pairs(query) do
68+
if v ~= nil then
69+
table.insert(params, k .. '=' .. vim.uri_encode(tostring(v)))
6870
end
71+
end
6972

70-
if #params > 0 then
71-
url = url .. '?' .. table.concat(params, '&')
72-
end
73+
if #params > 0 then
74+
url = url .. '?' .. table.concat(params, '&')
7375
end
7476

7577
return server_job.call_api(url, method, body)
@@ -394,13 +396,11 @@ end
394396
--- Subscribe to events (streaming)
395397
--- @param directory string|nil Directory path
396398
--- @param on_event fun(event: table) Event callback
397-
--- @return table The streaming job handle
399+
--- @return table streaming job handle
398400
function OpencodeApiClient:subscribe_to_events(directory, on_event)
399401
self:_ensure_base_url()
400-
local url = self.base_url .. '/event'
401-
if directory then
402-
url = url .. '?directory=' .. directory
403-
end
402+
directory = directory or vim.fn.getcwd()
403+
local url = self.base_url .. '/event?directory=' .. vim.uri_encode(directory)
404404

405405
return server_job.stream_api(url, 'GET', nil, function(chunk)
406406
-- strip data: prefix if present

0 commit comments

Comments
 (0)