initial change to tools logic
This commit is contained in:
65
lua/chatgpt_nvim/tools/manager.lua
Normal file
65
lua/chatgpt_nvim/tools/manager.lua
Normal file
@@ -0,0 +1,65 @@
|
||||
local tools_module = require("chatgpt_nvim.tools")
|
||||
local uv = vim.loop
|
||||
local M = {}
|
||||
|
||||
-- Simple destructive command check
|
||||
local function is_destructive_command(cmd)
|
||||
if not cmd then return false end
|
||||
local destructive_list = { "rm", "sudo", "mv", "cp" }
|
||||
for _, keyword in ipairs(destructive_list) do
|
||||
if cmd:match("(^" .. keyword .. "[%s$])") or cmd:match("[%s]" .. keyword .. "[%s$]") then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- Prompt user if not auto-accepted or if command is destructive
|
||||
local function prompt_user_tool_accept(tool_call, conf)
|
||||
local function ask_user(msg)
|
||||
vim.api.nvim_out_write(msg .. " [y/N]: ")
|
||||
local ans = vim.fn.input("")
|
||||
if ans:lower() == "y" then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local auto_accept = conf.tool_auto_accept[tool_call.tool]
|
||||
if tool_call.tool == "executeCommand" and auto_accept then
|
||||
if is_destructive_command(tool_call.command) then
|
||||
auto_accept = false
|
||||
end
|
||||
end
|
||||
|
||||
if not auto_accept then
|
||||
return ask_user(("Tool request: %s -> Accept?"):format(tool_call.tool or "unknown"))
|
||||
else
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
-- We'll pass references to `read_file` from init.
|
||||
local function handle_tool_calls(tools, conf, is_subpath_fn, read_file_fn)
|
||||
local messages = {}
|
||||
for _, call in ipairs(tools) do
|
||||
-- Prompt user acceptance
|
||||
local accepted = prompt_user_tool_accept(call, conf)
|
||||
if not accepted then
|
||||
table.insert(messages, string.format("Tool [%s] was rejected by user.", call.tool or "nil"))
|
||||
else
|
||||
local tool_impl = tools_module.tools_by_name[call.tool]
|
||||
if tool_impl then
|
||||
local msg = tool_impl.run(call, conf, prompt_user_tool_accept, is_subpath_fn, read_file_fn)
|
||||
table.insert(messages, msg)
|
||||
else
|
||||
table.insert(messages, string.format("Unknown tool type: '%s'", call.tool or "nil"))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return table.concat(messages, "\n\n")
|
||||
end
|
||||
|
||||
M.handle_tool_calls = handle_tool_calls
|
||||
return M
|
||||
Reference in New Issue
Block a user