feat: run python tool asynchronously (#1455)
This commit is contained in:
@@ -515,7 +515,7 @@ function M.rag_search(opts, on_log)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@type AvanteLLMToolFunc<{ code: string, rel_path: string, container_image?: string }>
|
---@type AvanteLLMToolFunc<{ code: string, rel_path: string, container_image?: string }>
|
||||||
function M.python(opts, on_log)
|
function M.python(opts, on_log, on_complete)
|
||||||
local abs_path = get_abs_path(opts.rel_path)
|
local abs_path = get_abs_path(opts.rel_path)
|
||||||
if not has_permission_to_access(abs_path) then return nil, "No permission to access path: " .. abs_path end
|
if not has_permission_to_access(abs_path) then return nil, "No permission to access path: " .. abs_path end
|
||||||
if not Path:new(abs_path):exists() then return nil, "Path not found: " .. abs_path end
|
if not Path:new(abs_path):exists() then return nil, "Path not found: " .. abs_path end
|
||||||
@@ -535,12 +535,15 @@ function M.python(opts, on_log)
|
|||||||
return nil, "User canceled"
|
return nil, "User canceled"
|
||||||
end
|
end
|
||||||
if vim.fn.executable("docker") == 0 then return nil, "Python tool is not available to execute any code" end
|
if vim.fn.executable("docker") == 0 then return nil, "Python tool is not available to execute any code" end
|
||||||
---change cwd to abs_path
|
|
||||||
local old_cwd = vim.fn.getcwd()
|
|
||||||
|
|
||||||
vim.fn.chdir(abs_path)
|
local function handle_result(result) ---@param result vim.SystemCompleted
|
||||||
local output = vim
|
if result.code ~= 0 then return nil, "Error: " .. (result.stderr or "Unknown error") end
|
||||||
.system({
|
|
||||||
|
Utils.debug("output", result.stdout)
|
||||||
|
return result.stdout, nil
|
||||||
|
end
|
||||||
|
local job = vim.system(
|
||||||
|
{
|
||||||
"docker",
|
"docker",
|
||||||
"run",
|
"run",
|
||||||
"--rm",
|
"--rm",
|
||||||
@@ -552,17 +555,20 @@ function M.python(opts, on_log)
|
|||||||
"python",
|
"python",
|
||||||
"-c",
|
"-c",
|
||||||
opts.code,
|
opts.code,
|
||||||
}, {
|
},
|
||||||
|
{
|
||||||
text = true,
|
text = true,
|
||||||
})
|
cwd = abs_path,
|
||||||
:wait()
|
},
|
||||||
|
vim.schedule_wrap(function(result)
|
||||||
vim.fn.chdir(old_cwd)
|
if not on_complete then return end
|
||||||
|
local output, err = handle_result(result)
|
||||||
if output.code ~= 0 then return nil, "Error: " .. (output.stderr or "Unknown error") end
|
on_complete(output, err)
|
||||||
|
end)
|
||||||
Utils.debug("output", output.stdout)
|
)
|
||||||
return output.stdout, nil
|
if on_complete then return end
|
||||||
|
local result = job:wait()
|
||||||
|
return handle_result(result)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return AvanteLLMTool[]
|
---@return AvanteLLMTool[]
|
||||||
|
|||||||
Reference in New Issue
Block a user