-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.lua
More file actions
610 lines (563 loc) · 19.8 KB
/
Copy pathtools.lua
File metadata and controls
610 lines (563 loc) · 19.8 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
local json = require("vendor.json")
local CHROME_UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36"
local Tools = {}
local function _parse_frontmatter(content)
local fm = {}
local body = content
if content:match("^%-%-%-") then
local s, e = content:find("\n%-%-%-\n")
if s then
local fm_str = content:sub(5, s - 1)
body = content:sub(e + 1)
for line in fm_str:gmatch("[^\n]+") do
local key, val = line:match("^(%S+):%s*(.+)$")
if key then
val = val:gsub('^["\']', ""):gsub('["\']$', "")
if val == "true" then val = true
elseif val == "false" then val = false end
fm[key] = val
end
end
end
end
return fm, body
end
local function _scan_skills()
local home = os.getenv("HOME") or os.getenv("USERPROFILE") or "/tmp"
local dirs = { home .. "/.cluade/skills", "./.cluade/skills" }
local skills = {}
for _, dir in ipairs(dirs) do
local f = io.popen('ls -1 "' .. dir .. '" 2>/dev/null')
if f then
for entry in f:lines() do
local skill_dir = dir .. "/" .. entry
local md_path = skill_dir .. "/SKILL.md"
local md_file = io.open(md_path, "r")
if md_file then
local content = md_file:read("*a")
md_file:close()
local fm = _parse_frontmatter(content)
if fm["disable-model-invocation"] ~= true then
local name = fm["name"] or entry
local desc = fm["description"] or "(no description)"
if not skills[name] then
skills[name] = { name = name, path = md_path, description = desc }
end
end
end
end
f:close()
end
end
local list = {}
for _, s in pairs(skills) do list[#list + 1] = s end
table.sort(list, function(a, b) return a.name < b.name end)
return list
end
Tools._scan_skills = _scan_skills
local DEFAULT_PERMISSIONS = {
read = "allow",
write = "allow",
edit = "allow",
bash = "allow",
glob = "allow",
grep = "allow",
web_search = "allow",
web_fetch = "allow",
remote_bash = "ask",
compact = "allow",
skill = "allow",
subagent = "allow",
}
-- The command that re-invokes cluade itself, set by the bootstrap (cluade.lua).
-- Used to spawn subagents as child processes.
local self_cmd = nil
function Tools.set_self_cmd(cmd) self_cmd = cmd end
-- Build the child invocation for a subagent: cluade in --subagent mode (quiet +
-- ephemeral + dangercheck-as-deny), read-only when mode is "plan". The prompt is
-- shell-escaped so it can't break out of the command.
function Tools._subagent_cmd(cmd, prompt, mode)
local q = "'" .. tostring(prompt):gsub("'", "'\\''") .. "'"
local flag = (mode == "plan") and " --plan" or ""
return cmd .. " --subagent" .. flag .. " " .. q
end
local DEFS = {}
DEFS.read = {
type = "function",
["function"] = {
name = "read",
description = "Read a file from the local filesystem. Returns the content with line numbers.",
parameters = {
type = "object",
properties = {
filePath = { type = "string", description = "Absolute path to the file" },
},
required = { "filePath" },
},
},
}
DEFS.write = {
type = "function",
["function"] = {
name = "write",
description = "Write content to a file. Creates or overwrites the file.",
parameters = {
type = "object",
properties = {
filePath = { type = "string", description = "Absolute path to the file" },
content = { type = "string", description = "Content to write" },
},
required = { "filePath", "content" },
},
},
}
DEFS.edit = {
type = "function",
["function"] = {
name = "edit",
description = "Perform exact string replacement in a file. Fails if old_string is not found or matches multiple locations.",
parameters = {
type = "object",
properties = {
filePath = { type = "string", description = "Absolute path to the file" },
oldString = { type = "string", description = "Text to replace (must match exactly)" },
newString = { type = "string", description = "Replacement text" },
replaceAll = { type = "boolean", description = "Replace all occurrences (default false)" },
},
required = { "filePath", "oldString", "newString" },
},
},
}
DEFS.bash = {
type = "function",
["function"] = {
name = "bash",
description = "Execute a shell command. The system is a Linux/OpenWRT system using busybox ash. Use ash-compatible syntax.",
parameters = {
type = "object",
properties = {
command = { type = "string", description = "Shell command to execute" },
workdir = { type = "string", description = "Working directory for the command" },
},
required = { "command" },
},
},
}
DEFS.glob = {
type = "function",
["function"] = {
name = "glob",
description = "Find files matching a glob pattern. Use ** for recursive matching.",
parameters = {
type = "object",
properties = {
pattern = { type = "string", description = "Glob pattern (e.g. src/**/*.lua)" },
path = { type = "string", description = "Directory to search in; a relative pattern resolves against it (default: cwd)" },
},
required = { "pattern" },
},
},
}
DEFS.grep = {
type = "function",
["function"] = {
name = "grep",
description = "Search for a pattern in files. Returns matching files with line numbers.",
parameters = {
type = "object",
properties = {
pattern = { type = "string", description = "Regex pattern to search for" },
path = { type = "string", description = "Directory to search in (default: cwd)" },
include = { type = "string", description = "File pattern filter (e.g. *.lua)" },
},
required = { "pattern" },
},
},
}
DEFS.web_search = {
type = "function",
["function"] = {
name = "web_search",
description = "Search the web using DuckDuckGo HTML and return results.",
parameters = {
type = "object",
properties = {
query = { type = "string", description = "Search query" },
},
required = { "query" },
},
},
}
DEFS.web_fetch = {
type = "function",
["function"] = {
name = "web_fetch",
description = "Fetch content from a URL and return as text.",
parameters = {
type = "object",
properties = {
url = { type = "string", description = "URL to fetch" },
},
required = { "url" },
},
},
}
DEFS.remote_bash = {
type = "function",
["function"] = {
name = "remote_bash",
description = "Execute a shell command on a remote host via SSH (uses SSH keys, not passwords).",
parameters = {
type = "object",
properties = {
host = { type = "string", description = "Remote hostname or IP" },
command = { type = "string", description = "Shell command to run on the remote host" },
username = { type = "string", description = "SSH username (default: root)" },
port = { type = "integer", description = "SSH port (default: 22)" },
},
required = { "host", "command" },
},
},
}
DEFS.compact = {
type = "function",
["function"] = {
name = "compact",
description = "Frees context by summarizing prior conversation. Call after completing a significant task (feature done, bug fixed, commit >100 lines). Provide a concise summary of what was done, decisions made, and what remains.",
parameters = {
type = "object",
properties = {
summary = { type = "string", description = "Paragraph summarizing progress, key decisions, files changed, and next steps" },
},
required = { "summary" },
},
},
}
DEFS.subagent = {
type = "function",
["function"] = {
name = "subagent",
description = "Delegate a self-contained task to a child cluade with its own fresh context; returns only its final answer. Use it to keep your own context clean (research, lookups, reviews) or to hand off an independent chunk of work. mode 'build' (default) has full tools and can create/edit/delete files; mode 'plan' is read-only. The subagent runs unattended: it cannot prompt, and catastrophic commands are refused.",
parameters = {
type = "object",
properties = {
prompt = { type = "string", description = "The complete, self-contained task for the subagent." },
mode = { type = "string", description = "'build' (default): full tools incl. file writes. 'plan': read-only (read/grep/glob/web)." },
},
required = { "prompt" },
},
},
}
DEFS.skill = {
type = "function",
["function"] = {
name = "skill",
description = "Load a skill's full instructions from a SKILL.md file. Use when a skill name matches the current task.",
parameters = {
type = "object",
properties = {
name = { type = "string", description = "Name of the skill to load (e.g., brainstorming)" },
},
required = { "name" },
},
},
}
-- === executors ===
local function _read_file(path)
local f, err = io.open(path, "r")
if not f then return nil, err end
local content = f:read("*a")
f:close()
return content
end
local function _write_file(path, content)
local dir = path:match("^(.*)/")
if dir then os.execute('mkdir -p "' .. dir .. '" 2>/dev/null') end
local f, err = io.open(path, "w")
if not f then return nil, err end
f:write(content)
f:close()
return true
end
local function _shell_escape(s)
return "'" .. s:gsub("'", "'\\''") .. "'"
end
local function _run(cmd)
local f = io.popen(cmd .. " 2>&1")
if not f then return nil, "failed to execute" end
local output = f:read("*a")
local ok = { f:close() }
return output, ok[3] -- exit code
end
local function _token_estimate(text)
return math.floor(#text / 3.5)
end
function Tools.execute_read(cwd, params)
local path = params.filePath
if not path:match("^/") then path = cwd .. "/" .. path end
local content, err = _read_file(path)
if not content then
return ({ status = "error", error = tostring(err) }), _token_estimate(tostring(err))
end
local lines = {}
for line in content:gmatch("[^\n]*\n?") do
if #line > 0 then
lines[#lines + 1] = #lines + 1 .. ": " .. line:gsub("\n$", "")
end
end
local result = table.concat(lines, "\n")
return ({ status = "ok", output = result }), _token_estimate(result)
end
function Tools.execute_write(cwd, params)
if not params.filePath then
return ({ status = "error", error = "missing required param: filePath" }), 10
end
if not params.content then
return ({ status = "error", error = "missing required param: content" }), 10
end
local path = params.filePath
if not path:match("^/") then path = cwd .. "/" .. path end
local ok, err = _write_file(path, params.content)
if not ok then
return ({ status = "error", error = err }), _token_estimate(err)
end
local result = "wrote " .. path
return ({ status = "ok", output = result }), _token_estimate(result)
end
function Tools.execute_edit(cwd, params)
local path = params.filePath
if not path:match("^/") then path = cwd .. "/" .. path end
local content, err = _read_file(path)
if not content then
return ({ status = "error", error = err }), _token_estimate(err)
end
local old_str = params.oldString
local new_str = params.newString
local count = 0
local start, finish = content:find(old_str, 1, true)
if not start then
return ({ status = "error", error = "oldString not found in content" }), _token_estimate("oldString not found")
end
if params.replaceAll then
local n = 0
content, n = content:gsub(old_str:gsub("([%-%^%$%(%)%%%.%[%]%*%+%?])", "%%%1"), new_str)
count = n
else
local pos = content:find(old_str, start + #old_str, true)
if pos then
return ({ status = "error", error = "Found multiple matches for oldString" }), _token_estimate("multiple matches")
end
content = content:sub(1, start - 1) .. new_str .. content:sub(finish + 1)
count = 1
end
_write_file(path, content)
local result = "replaced " .. count .. " occurrence(s) in " .. path
return ({ status = "ok", output = result }), _token_estimate(result)
end
function Tools.execute_bash(cwd, params)
if not params.command then
return ({ status = "error", error = "missing required param: command" }), 10
end
local command = params.command
local workdir = params.workdir or cwd
local cmd = "cd " .. _shell_escape(workdir) .. " && " .. command
local output, exit_code = _run(cmd)
local result = output
if exit_code and exit_code ~= 0 then
result = result .. "\n[exit code: " .. exit_code .. "]"
end
return ({ status = "ok", output = result }), _token_estimate(result)
end
function Tools.execute_glob(cwd, params)
local pattern = params.pattern
local recursive = pattern:find("%*%*") ~= nil
-- A relative pattern resolves against params.path (like grep), else cwd. An
-- absolute pattern is used as-is and params.path is ignored.
if not pattern:match("^/") then
local base = params.path or cwd
if not base:match("^/") then base = cwd .. "/" .. base end
pattern = base .. "/" .. pattern
end
local cmd
if recursive then
-- Search from the directory portion BEFORE the first wildcard, matching the
-- trailing filename glob at any depth. ** collapses to * for find -name.
local fbase = pattern:match("^(.-)%*")
fbase = (fbase and fbase:gsub("/+$", "")) or cwd
if fbase == "" then fbase = "/" end
local name = pattern:match("([^/]*)$"):gsub("%*%*", "*")
cmd = 'find "' .. fbase .. '" -name "' .. name .. '" 2>/dev/null | head -100'
else
cmd = 'ls -d ' .. pattern .. ' 2>/dev/null | head -100'
end
local output = _run(cmd)
local result = (output and #output > 0) and output or "(no matches)"
return ({ status = "ok", output = result }), _token_estimate(result)
end
function Tools.execute_grep(cwd, params)
local pattern = params.pattern
local search_path = params.path or cwd
if not search_path:match("^/") then search_path = cwd .. "/" .. search_path end
local include = params.include and ('--include="' .. params.include .. '"') or ""
local cmd = 'grep -rn ' .. include .. ' ' .. _shell_escape(pattern) .. ' ' .. _shell_escape(search_path) .. ' 2>/dev/null | head -100'
local output = _run(cmd)
local result = (output and #output > 0) and output or "(no matches)"
return ({ status = "ok", output = result }), _token_estimate(result)
end
function Tools.execute_web_search(cwd, params)
local query = params.query:gsub("%s+", "+")
local url = "https://html.duckduckgo.com/html/?q=" .. query
local http = require("socket.http")
local https = require("ssl.https")
local ltn12 = require("ltn12")
local resp = {}
local ok, code = https.request({
url = url,
sink = ltn12.sink.table(resp),
timeout = 15,
protocol = "tlsv1_2",
headers = { ["User-Agent"] = CHROME_UA },
})
if not ok then
return ({ status = "error", error = "search failed: " .. tostring(code) }), _token_estimate("search failed")
end
local html = table.concat(resp)
local results = {}
for title, snippet, link in html:gmatch('result__a[^>]*>([^<]+).-result__snippet">([^<]+).-href="([^"]+)"') do
results[#results + 1] = title .. "\n " .. snippet:gsub("<.->", "") .. "\n " .. link .. "\n"
end
if #results == 0 then
return ({ status = "ok", output = "(no results)" }), _token_estimate("(no results)")
end
local out = table.concat(results, "\n")
return ({ status = "ok", output = out }), _token_estimate(out)
end
function Tools.execute_web_fetch(cwd, params)
local url = params.url
local http = require("socket.http")
local https = require("ssl.https")
local ltn12 = require("ltn12")
local resp = {}
local is_tls = url:sub(1, 8) == "https://"
local ok, code
if is_tls then
ok, code = https.request({
url = url,
sink = ltn12.sink.table(resp),
timeout = 15,
protocol = "tlsv1_2",
headers = { ["User-Agent"] = CHROME_UA },
})
else
ok, code = http.request({
url = url,
sink = ltn12.sink.table(resp),
timeout = 15,
headers = { ["User-Agent"] = CHROME_UA },
})
end
if not ok then
return ({ status = "error", error = "fetch failed: " .. tostring(code) }), _token_estimate("fetch failed")
end
local content = table.concat(resp)
local max_len = 32000
if #content > max_len then
content = content:sub(1, max_len) .. "\n[... truncated at " .. max_len .. " bytes]"
end
return ({ status = "ok", output = content }), _token_estimate(content)
end
function Tools.execute_remote_bash(cwd, params)
local host = params.host
local command = params.command
local username = params.username or "root"
local port = params.port or "22"
local cmd = "ssh -y -p " .. port .. " " .. username .. "@" .. host .. " " .. _shell_escape(command)
local output, exit_code = _run(cmd)
local result = output or ""
if exit_code and exit_code ~= 0 then
result = result .. "\n[exit code: " .. exit_code .. "]"
end
return ({ status = "ok", output = result }), _token_estimate(result)
end
function Tools.execute_compact(cwd, params)
return ({ status = "compacted", summary = params.summary }), _token_estimate(params.summary)
end
function Tools.execute_subagent(cwd, params)
if not self_cmd then
return ({ status = "error", error = "subagent unavailable: cluade self-command not set" }), 0
end
if not params.prompt or params.prompt == "" then
return ({ status = "error", error = "subagent requires a non-empty 'prompt'" }), 0
end
-- The child writes its final answer to stdout (progress goes to its stderr,
-- which passes through to ours); io.popen captures only that final answer.
local cmd = Tools._subagent_cmd(self_cmd, params.prompt, params.mode)
local f = io.popen(cmd)
local out = f and f:read("*a") or ""
if f then f:close() end
out = out:gsub("%s+$", "")
if out == "" then out = "(subagent returned no text)" end
return ({ status = "ok", output = out }), _token_estimate(out)
end
function Tools.execute_skill(cwd, params)
local skills = _scan_skills()
for _, s in ipairs(skills) do
if s.name == params.name then
local f = io.open(s.path, "r")
if f then
local content = f:read("*a")
f:close()
local _, body = _parse_frontmatter(content)
return ({ status = "ok", output = body }), _token_estimate(body)
end
end
end
local available = {}
for _, s in ipairs(skills) do available[#available + 1] = s.name end
local msg = "skill '" .. params.name .. "' not found. Available: " .. table.concat(available, ", ")
return ({ status = "error", error = msg }), _token_estimate(msg)
end
-- === registry ===
local EXECUTORS = {
read = Tools.execute_read,
write = Tools.execute_write,
edit = Tools.execute_edit,
bash = Tools.execute_bash,
glob = Tools.execute_glob,
grep = Tools.execute_grep,
web_search = Tools.execute_web_search,
web_fetch = Tools.execute_web_fetch,
remote_bash = Tools.execute_remote_bash,
compact = Tools.execute_compact,
skill = Tools.execute_skill,
subagent = Tools.execute_subagent,
}
function Tools.get_definitions(names)
local defs = {}
for _, name in ipairs(names) do
if DEFS[name] then
defs[#defs + 1] = DEFS[name]
end
end
return defs
end
function Tools.get_permission(name)
return DEFAULT_PERMISSIONS[name] or "ask"
end
function Tools.set_permissions(overrides)
for name, level in pairs(overrides) do
DEFAULT_PERMISSIONS[name] = level
end
end
function Tools.execute(cwd, name, params)
local executor = EXECUTORS[name]
if not executor then
return ({ status = "error", error = "unknown tool: " .. name }), 0
end
local ok, result, tokens = pcall(executor, cwd, params)
if not ok then
return ({ status = "error", error = tostring(result) }), 0
end
return result, tokens or 0
end
return Tools