11# Lua API References
22
3- This plugin provides 2 sets of APIs that provides similar functionalities. The
3+ This plugin provides 2 sets of high-level APIs that provides similar functionalities. The
44synchronous APIs provide more up-to-date retrieval results at the cost of
55blocking the main neovim UI, while the async APIs use a caching mechanism to
66provide asynchronous retrieval results almost instantaneously, but the result
@@ -9,6 +9,13 @@ blocked/frozen doesn't hurt much because you spend the time waiting for response
99anyway, and you can use the synchronous API in this case. For other tasks like
1010completion, the async API will minimise the interruption to your workflow.
1111
12+ These APIs are wrappers around the lower-level
13+ [ job runner API] ( https://github.com/Davidyz/VectorCode/tree/main/lua/vectorcode/jobrunner ) ,
14+ which provides a unified interface for calling VectorCode commands that can be
15+ executed by either the LSP or the generic CLI backend. If the high-level APIs
16+ are sufficient for your usecase, it's usually not necessary to use the job
17+ runners directly.
18+
1219<!-- mtoc-start -->
1320
1421* [ Synchronous API] ( #synchronous-api )
@@ -24,6 +31,10 @@ completion, the async API will minimise the interruption to your workflow.
2431 * [ ` cacher_backend.buf_job_count(bufnr?) ` ] ( #cacher_backendbuf_job_countbufnr )
2532 * [ ` cacher_backend.make_prompt_component(bufnr?, component_cb?) ` ] ( #cacher_backendmake_prompt_componentbufnr-component_cb )
2633 * [ Built-in Query Callbacks] ( #built-in-query-callbacks )
34+ * [ JobRunners] ( #jobrunners )
35+ * [ ` run_async(args, callback, bufnr) ` and ` run(args, timeout_ms, bufnr) ` ] ( #run_asyncargs-callback-bufnr-and-runargs-timeout_ms-bufnr )
36+ * [ ` is_job_running(job_handle):boolean ` ] ( #is_job_runningjob_handleboolean )
37+ * [ ` stop_job(job_handle) ` ] ( #stop_jobjob_handle )
2738
2839<!-- mtoc-end -->
2940
@@ -272,3 +283,69 @@ constructor for you to play around with it, but you can easily build your own!
272283 fallback to ` make_surrounding_lines_cb(-1) ` . The default value for ` max_num `
273284 is 50.
274285
286+
287+ ## JobRunners
288+
289+ The ` VectorCode.JobRunner ` is an abstract class for vectorcode command
290+ execution. There are 2 concrete child classes that you can use:
291+ - ` require("vectorcode.jobrunner.cmd") ` uses the CLI (` vectorcode ` commands) to
292+ interact with the database;
293+ - ` quire("vectorcode.jobrunner.lsp") ` use the LSP server, which avoids some of
294+ the IO overhead and provides LSP progress notifications.
295+
296+ The available methods for a ` VectorCode.JobRunner ` object includes:
297+
298+ ### ` run_async(args, callback, bufnr) ` and ` run(args, timeout_ms, bufnr) `
299+ Calls a vectorcode command.
300+
301+ The ` args ` parameter (of type ` string[] ` ) is whatever argument that comes after
302+ ` vectorcode ` when you run it in the CLI. For example, if you want to query for
303+ 10 chunks in the shell, you'd call the following command:
304+
305+ ``` bash
306+ vectorcode query -n 10 keyword1 keyword2 --include chunk
307+ ```
308+
309+ Then for the job runner (either LSP or cmd), the ` args ` parameter would be:
310+ ``` lua
311+ args = {" query" , " -n" , " 10" , " keyword1" , " keyword2" , " --include" , " chunk" }
312+ ```
313+
314+ For the ` run_async ` method, the ` callback ` function has the
315+ following signature:
316+ ``` lua
317+ --- @type fun ( result : table , error : table , code : integer , signal : integer ?)?
318+ ```
319+ For the ` run ` method, the return value can be captured as follow:
320+ ``` lua
321+ res , err , _code , _signal = jobrunner .run (args , - 1 , 0 )
322+ ```
323+
324+ The result (for both synchronous and asynchronous method) is a ` vim.json.decode ` ed
325+ table of the result of the command execution. Consult
326+ [ the CLI documentation] ( ../cli.md#for-developers ) for the schema of the results for
327+ the command that you call.
328+
329+ For example, the query command mentioned above will return a
330+ ` VectorCode.QueryResult[] ` , where ` VectorCode.QueryResult ` is defined as
331+ follows:
332+ ``` lua
333+ --- @class VectorCode.QueryResult
334+ --- @field path string Path to the file
335+ --- @field document string ? Content of the file
336+ --- @field chunk string ?
337+ --- @field start_line integer ?
338+ --- @field end_line integer ?
339+ --- @field chunk_id string ?
340+ ```
341+
342+ The ` run_async ` will return a ` job_handle ` which is defined as an ` integer? ` .
343+ For the LSP backend, the job handle is the ` request_id ` . For the cmd runner, the
344+ job handle is the ` PID ` of the process.
345+
346+ ### ` is_job_running(job_handle):boolean `
347+ Checks if a job associated with the given handle is currently running;
348+
349+
350+ ### ` stop_job(job_handle) `
351+ Attempts to stop or cancel the async job associated with the given handle.
0 commit comments