@@ -5,7 +5,7 @@ let g:autoloaded_copilot_agent = 1
55
66scriptencoding utf- 8
77
8- let s: plugin_version = ' 1.11.4 '
8+ let s: plugin_version = ' 1.12.0 '
99
1010let s: error_exit = -1
1111
@@ -44,13 +44,9 @@ function! s:LogSend(request, line) abort
4444 return ' --> ' . a: line
4545endfunction
4646
47- let s: chansend = function (exists (' *chansend' ) ? ' chansend' : ' ch_sendraw' )
4847function ! s: Send (agent, request) abort
49- let request = extend ({' jsonrpc' : ' 2.0' }, a: request , ' keep' )
50- let body = json_encode (request)
51- call s: chansend (a: agent .job, " Content-Length: " . len (body) . " \r \n \r \n " . body)
52- call copilot#logger#Trace (function (' s:LogSend' , [request, body]))
53- return request
48+ call ch_sendexpr (a: agent .job, a: request )
49+ return a: request
5450endfunction
5551
5652function ! s: AgentNotify (method, params) dict abort
@@ -142,6 +138,12 @@ function! s:BufferText(bufnr) abort
142138 return join (getbufline (a: bufnr , 1 , ' $' ), " \n " ) . " \n "
143139endfunction
144140
141+ function ! s: ShowMessageRequest (params) abort
142+ let choice = inputlist ([a: params .message . " \n\n Request Actions:" ] +
143+ \ map (copy (get (a: params , ' actions' , [])), { i , v - > (i + 1 ) . ' . ' . v .title }))
144+ return choice > 0 ? get (a: params .actions, choice - 1 , v: null ) : v: null
145+ endfunction
146+
145147function ! s: AgentRequest (method, params, ... ) dict abort
146148 let s: id += 1
147149 let request = {' method' : a: method , ' params' : deepcopy (a: params ), ' id' : s: id }
@@ -224,32 +226,28 @@ function! s:DispatchMessage(agent, handler, id, params, ...) abort
224226endfunction
225227
226228function ! s: OnMessage (agent, body, ... ) abort
227- call copilot#logger#Trace ({ - > ' <-- ' . a: body })
228- let response = json_decode (a: body )
229- if type (response) != v: t_dict
230- return
229+ if ! has_key (a: body , ' method' )
230+ return s: OnResponse (a: agent , a: body )
231+ endif
232+ let request = a: body
233+ let id = get (request, ' id' , v: null )
234+ let params = get (request, ' params' , v: null )
235+ if empty (id)
236+ if has_key (a: agent .notifications, request.method)
237+ call timer_start (0 , { _ - > a: agent .notifications[request.method](params) })
238+ elseif request.method == # ' LogMessage'
239+ call copilot#logger#Raw (get (params, ' level' , 3 ), get (params, ' message' , ' ' ))
240+ endif
241+ elseif has_key (a: agent .methods, request.method)
242+ call timer_start (0 , function (' s:DispatchMessage' , [a: agent , a: agent .methods[request.method], id, params]))
243+ else
244+ return s: Send (a: agent , {" id" : id, " error" : {" code" : -32700 , " message" : " Method not found: " . request.method}})
231245 endif
232- return s: OnResponse (a: agent , response)
233246endfunction
234247
235248function ! s: OnResponse (agent, response, ... ) abort
236249 let response = a: response
237- let id = get (response, ' id' , v: null )
238- if has_key (response, ' method' )
239- let params = get (response, ' params' , v: null )
240- if empty (id)
241- if has_key (a: agent .notifications, response.method)
242- call timer_start (0 , { _ - > a: agent .notifications[response.method](params) })
243- elseif response.method == # ' LogMessage'
244- call copilot#logger#Raw (get (params, ' level' , 3 ), get (params, ' message' , ' ' ))
245- endif
246- elseif has_key (a: agent .methods, response.method)
247- call timer_start (0 , function (' s:DispatchMessage' , [a: agent , a: agent .methods[response.method], id, params]))
248- else
249- return s: Send (a: agent , {" id" : id, " error" : {" code" : -32700 , " message" : " Method not found: " . response.method}})
250- endif
251- return
252- endif
250+ let id = get (a: response , ' id' , v: null )
253251 if ! has_key (a: agent .requests, id)
254252 return
255253 endif
@@ -275,44 +273,11 @@ function! s:OnResponse(agent, response, ...) abort
275273 endif
276274endfunction
277275
278- function ! s: OnOut (agent, state , data) abort
279- let a: state .buffer .= a: data
280- while 1
281- if a: state .mode == # ' body'
282- let content_length = a: state .headers[' content-length' ]
283- if strlen (a: state .buffer ) >= content_length
284- let headers = remove (a: state , ' headers' )
285- let a: state .mode = ' headers'
286- let a: state .headers = {}
287- let body = strpart (a: state .buffer , 0 , content_length)
288- let a: state .buffer = strpart (a: state .buffer , content_length)
289- call timer_start (0 , function (' s:OnMessage' , [a: agent , body]))
290- else
291- return
292- endif
293- elseif a: state .mode == # ' headers' && a: state .buffer = ~# " \n "
294- let line = matchstr (a: state .buffer , " ^.[^\n ]*" )
295- let a: state .buffer = strpart (a: state .buffer , strlen (line ) + 1 )
296- let match = matchlist (line , ' ^\([^:]\+\): \(.\{-\}\)\r$' )
297- if len (match )
298- let a: state .headers[tolower (match [1 ])] = match [2 ]
299- elseif line = ~# " ^\r \\ =$"
300- let a: state .mode = ' body'
301- else
302- call copilot#logger#Error (" Invalid header: " . line )
303- call a: agent .Close ()
304- endif
305- else
306- return
307- endif
308- endwhile
309- endfunction
310-
311- function ! s: OnErr (agent, line ) abort
276+ function ! s: OnErr (agent, line , ... ) abort
312277 call copilot#logger#Debug (' <-! ' . a: line )
313278endfunction
314279
315- function ! s: OnExit (agent, code) abort
280+ function ! s: OnExit (agent, code, ... ) abort
316281 let a: agent .exit_status = a: code
317282 if has_key (a: agent , ' job' )
318283 call remove (a: agent , ' job' )
@@ -384,11 +349,11 @@ function! s:LspNotify(method, params) dict abort
384349 return v: lua .require' _copilot' .rpc_notify (self .id, a: method , a: params )
385350endfunction
386351
387- function ! copilot#agent#LspHandle (agent_id, response ) abort
352+ function ! copilot#agent#LspHandle (agent_id, request ) abort
388353 if ! has_key (s: instances , a: agent_id )
389354 return
390355 endif
391- call s: OnResponse (s: instances [a: agent_id ], a: response )
356+ call s: OnMessage (s: instances [a: agent_id ], a: request )
392357endfunction
393358
394359function ! s: GetNodeVersion (command ) abort
@@ -550,10 +515,15 @@ function! copilot#agent#New(...) abort
550515 else
551516 let state = {' headers' : {}, ' mode' : ' headers' , ' buffer' : ' ' }
552517 let instance.open_buffers = {}
553- let instance.job = copilot#job#Stream (command ,
554- \ function (' s:OnOut' , [instance, state ]),
555- \ function (' s:OnErr' , [instance]),
556- \ function (' s:OnExit' , [instance]))
518+ let instance.methods = extend ({' window/showMessageRequest' : function (' s:ShowMessageRequest' )}, instance.methods)
519+ let instance.job = job_start (command , {
520+ \ ' cwd' : copilot#job#Cwd (),
521+ \ ' in_mode' : ' lsp' ,
522+ \ ' out_mode' : ' lsp' ,
523+ \ ' out_cb' : { j , d - > timer_start (0 , function (' s:OnMessage' , [instance, d ])) },
524+ \ ' err_cb' : { j , d - > timer_start (0 , function (' s:OnErr' , [instance, d ])) },
525+ \ ' exit_cb' : { j , d - > timer_start (0 , function (' s:OnExit' , [instance, d ])) },
526+ \ })
557527 let instance.id = exists (' *jobpid' ) ? jobpid (instance.job) : job_info (instance.job).process
558528 let request = instance.Request (' initialize' , {' capabilities' : {' workspace' : {' workspaceFolders' : v: true }}}, function (' s:GetCapabilitiesResult' ), function (' s:GetCapabilitiesError' ), instance)
559529 endif
0 commit comments