33--- - [implementation](https://github.com/sst/opencode/blob/dev/packages/opencode/src/server/server.ts)
44local M = {}
55
6- local config = require (" opencode.config" )
7-
86local sse_state = {
97 -- Track the port - `opencode` may have restarted, usually on a new port
108 port = nil ,
@@ -50,6 +48,23 @@ local function generate_uuid()
5048 )
5149end
5250
51+ --- Resolve the full URL for a given port and path, ensuring a valid hostname.
52+ --- @param port number
53+ --- @param path string
54+ --- @return string
55+ local function resolve_server_url (port , path )
56+ local config = require (" opencode.config" )
57+ local hostname = config .opts .hostname
58+ -- fallback to localhost as config is basically not validated
59+ if not hostname or hostname == " " then
60+ hostname = " localhost"
61+ end
62+ if not vim .startswith (path , " /" ) then
63+ path = " /" .. path
64+ end
65+ return string.format (" http://%s:%d%s" , hostname , port , path )
66+ end
67+
5368--- @param url string
5469--- @param method string
5570--- @param body table | nil
@@ -73,7 +88,7 @@ local function curl(url, method, body, callback)
7388 }
7489
7590 -- Add basic auth if credentials are available
76- local auth = config .opts .auth
91+ local auth = require ( " opencode. config" ) .opts .auth
7792 if auth and auth .username and auth .password and auth .password ~= " " then
7893 table.insert (command , " -u" )
7994 table.insert (command , auth .username .. " :" .. auth .password )
160175--- @param callback fun ( response : table )| nil
161176--- @return number job_id
162177function M .call (port , path , method , body , callback )
163- local hostname = config . opts . hostname
164- return curl (" http:// " .. hostname .. " : " .. port .. path , method , body , callback )
178+ local url = resolve_server_url ( port , path )
179+ return curl (url , method , body , callback )
165180end
166181
167182--- @param text string
@@ -270,22 +285,24 @@ end
270285function M .get_path (port )
271286 -- Query each port synchronously for working directory
272287 -- TODO: Migrate to align with async paradigm used elsewhere
273- local hostname = config .opts .hostname
274288 local curl_cmd = {
275289 " curl" ,
276290 " -s" ,
277291 " --connect-timeout" ,
278292 " 1" ,
279293 }
280294
295+ local config = require (" opencode.config" )
296+
281297 -- Add basic auth if credentials are available
282298 local auth = config .opts .auth
283299 if auth and auth .username and auth .password and auth .password ~= " " then
284300 table.insert (curl_cmd , " -u" )
285301 table.insert (curl_cmd , auth .username .. " :" .. auth .password )
286302 end
287303
288- table.insert (curl_cmd , " http://" .. hostname .. " :" .. port .. " /path" )
304+ local url = resolve_server_url (port , " /path" )
305+ table.insert (curl_cmd , url )
289306
290307 local curl_result = vim .system (curl_cmd ):wait ()
291308 require (" opencode.util" ).check_system_call (curl_result , " curl" )
0 commit comments