-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.lua
More file actions
112 lines (99 loc) · 3.11 KB
/
Copy pathtools.lua
File metadata and controls
112 lines (99 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
local context = require "plugins.assistant.tool_context"
local file = require "plugins.assistant.tool.file"
local applypatch = require "plugins.assistant.tool.applypatch"
local process = require "plugins.assistant.tool.process"
local git = require "plugins.assistant.tool.git"
local misc = require "plugins.assistant.tool.misc"
local memory = require "plugins.assistant.tool.memory"
local Tool = require "plugins.assistant.tool"
---Facade and registry for assistant tools.
---
---Tool implementations live in `tool/*` modules. This facade exposes their
---callbacks for tests/direct use and registers declarative tool specs with an
---agent.
---@class assistant.tools
local tools = {}
local has_net = rawget(_G, "net") ~= nil
local web = has_net and require "plugins.assistant.tool.web" or nil
local modules = {
misc,
file,
applypatch,
process,
git,
memory
}
if has_net then
table.insert(modules, 5, web)
end
local external_tools = {}
tools.set_confirm_write = context.set_confirm_write
tools.confirm = context.confirm
tools.read_path_allowed_without_confirmation = context.read_path_allowed_without_confirmation
tools.search = file.search
tools.list = file.list
tools.read = file.read
tools.file_info = file.file_info
tools.write = file.write_file
tools.edit = file.edit
tools.apply_patch = applypatch.apply_patch
tools.exec_command = process.exec_command
tools.write_stdin = process.write_stdin
tools.exec_status = process.exec_status
tools.send_eof = process.send_eof
tools.interrupt_exec = process.interrupt_exec
tools.close_exec = process.close_exec
tools.web_fetch = web and web.web_fetch or nil
tools.web_search = web and web.web_search or nil
tools.web_find = web and web.web_find or nil
tools.git_status = git.git_status
tools.git_diff = git.git_diff
tools.search_memory = memory.search_memory
tools.remember = memory.remember
tools.forget = memory.forget
tools.time = misc.time
tools.tool_catalog = misc.tool_catalog
tools.update_plan = misc.update_plan
tools.request_user_input = misc.request_user_input
tools.implement_plan = misc.implement_plan
---Register an external assistant tool.
---@param name string
---@param spec assistant.ToolSpec
---@return boolean ok
---@return string|nil err
function tools.register_external_tool(name, spec)
spec.name = name
local tool = spec
if getmetatable(tool) ~= Tool then
local ok, result = pcall(function()
return Tool:new(spec)
end)
if not ok then return false, result end
tool = result
end
external_tools[name] = tool
return true
end
---Remove a registered external assistant tool.
---@param name string
---@return boolean removed
function tools.unregister_external_tool(name)
local removed = external_tools[name] ~= nil
external_tools[name] = nil
return removed
end
---Handle registration of agent tools.
---@param agent assistant.Agent
---@return assistant.Agent agent
function tools.register_agent_tools(agent)
for _, module in ipairs(modules) do
for _, tool in ipairs(module.tools or {}) do
tool:register(agent, tools)
end
end
for _, tool in pairs(external_tools) do
tool:register(agent, tools)
end
return agent
end
return tools