@@ -17,12 +17,20 @@ local is_connection_error = utils.is_connection_error
1717
1818local M = {}
1919
20- local function effective_base_url ()
20+ local function effective_base_url (base_url_override )
21+ local override = normalize_base_url (base_url_override )
22+ if type (override ) == ' string' and override ~= ' ' then
23+ return override
24+ end
25+
2126 if state .base_url_managed ~= false then
2227 local ok , service = pcall (require , ' copilot_agent.service' )
2328 if ok and type (service ) == ' table' and type (service .refresh_managed_base_url ) == ' function' then
24- service .refresh_managed_base_url ()
29+ if service .refresh_managed_base_url () then
30+ return normalize_base_url (state .config .base_url )
31+ end
2532 end
33+ return ' '
2634 end
2735
2836 local base_url = normalize_base_url (state .config .base_url )
@@ -42,20 +50,20 @@ local function effective_base_url()
4250 return base_url
4351end
4452
45- function M .build_url (path )
46- return effective_base_url () .. path
53+ function M .build_url (path , base_url_override )
54+ return effective_base_url (base_url_override ) .. path
4755end
4856
4957local function has_base_url ()
5058 local base_url = effective_base_url ()
5159 return type (base_url ) == ' string' and base_url ~= ' '
5260end
5361
54- local function log_http_request (mode , method , path , body )
62+ local function log_http_request (mode , method , path , body , base_url_override )
5563 if not should_log (vim .log .levels .DEBUG ) then
5664 return
5765 end
58- log (string.format (' http.%s request method=%s path=%s url=%s body=%s' , mode , tostring (method ), tostring (path ), M .build_url (path ), serialize_log_value (body )), vim .log .levels .DEBUG )
66+ log (string.format (' http.%s request method=%s path=%s url=%s body=%s' , mode , tostring (method ), tostring (path ), M .build_url (path , base_url_override ), serialize_log_value (body )), vim .log .levels .DEBUG )
5967end
6068
6169local function log_http_response (mode , method , path , status , exit_code , payload , err )
@@ -132,12 +140,13 @@ function M.encode_json(value)
132140end
133141
134142-- Synchronous HTTP request via vim.system / vim.fn.system.
135- function M .sync_request (method , path , body )
143+ function M .sync_request (method , path , body , opts )
144+ opts = opts or {}
136145 if not M .ensure_curl () then
137146 return nil , ' curl executable not found: ' .. state .config .curl_bin
138147 end
139148
140- log_http_request (' sync' , method , path , body )
149+ log_http_request (' sync' , method , path , body , opts . base_url )
141150 local args = {
142151 state .config .curl_bin ,
143152 ' -sS' ,
@@ -147,7 +156,7 @@ function M.sync_request(method, path, body)
147156 ' \n %{http_code}' ,
148157 ' -X' ,
149158 method ,
150- M .build_url (path ),
159+ M .build_url (path , opts . base_url ),
151160 ' -H' ,
152161 ' Accept: application/json' ,
153162 }
@@ -216,13 +225,14 @@ function M.sync_request(method, path, body)
216225end
217226
218227-- Async HTTP request via vim.fn.jobstart.
219- function M .raw_request (method , path , body , callback )
228+ function M .raw_request (method , path , body , callback , opts )
229+ opts = opts or {}
220230 if not M .ensure_curl () then
221231 callback (nil , ' curl executable not found: ' .. state .config .curl_bin )
222232 return
223233 end
224234
225- log_http_request (' async' , method , path , body )
235+ log_http_request (' async' , method , path , body , opts . base_url )
226236 local stdout = {}
227237 local stderr = {}
228238 local args = {
@@ -234,7 +244,7 @@ function M.raw_request(method, path, body, callback)
234244 ' \n %{http_code}' ,
235245 ' -X' ,
236246 method ,
237- M .build_url (path ),
247+ M .build_url (path , opts . base_url ),
238248 ' -H' ,
239249 ' Accept: application/json' ,
240250 }
@@ -313,12 +323,13 @@ function M.request(method, path, body, callback, opts)
313323
314324 -- Lazy-require to avoid circular dependency with service.lua.
315325 local service = require (' copilot_agent.service' )
326+ service .forget_service_addr ()
316327 service .ensure_service_running (function (start_err )
317328 if start_err then
318329 callback (nil , start_err , nil )
319330 return
320331 end
321- M .raw_request (method , path , body , callback )
332+ M .raw_request (method , path , body , callback , opts )
322333 end )
323334 return
324335 end
@@ -331,14 +342,15 @@ function M.request(method, path, body, callback, opts)
331342 )
332343 -- Lazy-require to break http↔service circular dependency.
333344 local service = require (' copilot_agent.service' )
345+ service .forget_service_addr ()
334346 service .ensure_service_running (function (start_err )
335347 if start_err then
336348 log (string.format (' http.request retry failed method=%s path=%s startup_error=%s' , tostring (method ), tostring (path ), serialize_log_value (start_err , { max_len = 600 })), vim .log .levels .WARN )
337349 callback (nil , err .. ' \n ' .. start_err , status )
338350 return
339351 end
340352 log (string.format (' http.request retrying method=%s path=%s after service start' , tostring (method ), tostring (path )), vim .log .levels .DEBUG )
341- M .raw_request (method , path , body , callback )
353+ M .raw_request (method , path , body , callback , opts )
342354 end )
343355 return
344356 end
0 commit comments