1use crate::agent::config::HematiteConfig;
2use crate::agent::inference::tool_metadata_for_name;
3use crate::agent::types::{ToolDefinition, ToolFunction};
4use serde_json::Value;
5
6fn make_tool(name: &str, description: &str, parameters: Value) -> ToolDefinition {
7 ToolDefinition {
8 tool_type: "function".into(),
9 function: ToolFunction {
10 name: name.into(),
11 description: description.into(),
12 parameters,
13 },
14 metadata: tool_metadata_for_name(name),
15 }
16}
17
18pub fn get_tools() -> Vec<ToolDefinition> {
20 let os = std::env::consts::OS;
21 let mut tools = vec![
22 make_tool(
23 "shell",
24 &format!(
25 "Execute a command in the host shell ({os}). \
26 Use this ONLY for building, testing, or advanced system operations that have no dedicated Hematite tool. \
27 FORBIDDEN: Never use shell to run `mkdir`, `rm`, `cat`, `head`, `tail`, or `write-file` equivalents. \
28 Use the dedicated surgical tools (create_directory, read_file, tail_file) instead. \
29 Output is capped at 64KB. Prefer non-interactive commands."
30 ),
31 serde_json::json!({
32 "type": "object",
33 "properties": {
34 "command": {
35 "type": "string",
36 "description": "The command to run"
37 },
38 "reason": {
39 "type": "string",
40 "description": "For risky shell calls, explain what this command is verifying or changing."
41 },
42 "timeout_secs": {
43 "type": "integer",
44 "description": "Optional timeout in seconds (default 60)"
45 }
46 },
47 "required": ["command"]
48 }),
49 ),
50 make_tool(
51 "run_code",
52 "Execute a short JavaScript/TypeScript or Python snippet in a sandboxed subprocess. \
53 No network access, no filesystem escape, hard 10-second timeout. \
54 Use this to verify logic, test algorithms, compute values, or test functions \
55 when you need real output rather than a guess. \
56 ALWAYS include the `language` field — there is no default. \
57 \
58 JAVASCRIPT/TYPESCRIPT (language: \"javascript\"): \
59 Runs via Deno, NOT Node.js. `require()` does not exist — never use it. \
60 URL imports (e.g. from 'https://deno.land/...') are blocked — network is off. \
61 Use built-in Web APIs only: `crypto.subtle`, `TextEncoder`, `URL`, `atob`/`btoa`, etc. \
62 SHA-256 example: \
63 const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode('hello')); \
64 console.log([...new Uint8Array(buf)].map(b=>b.toString(16).padStart(2,'0')).join('')); \
65 \
66 PYTHON (language: \"python\"): \
67 Standard library is available. `hashlib`, `json`, `math`, `datetime`, `re`, `itertools` all work. \
68 `subprocess`, `socket`, `urllib`, `requests` are blocked. \
69 SHA-256 example: import hashlib; print(hashlib.sha256(b'hello').hexdigest()) \
70 \
71 Do NOT use this tool for PowerShell or shell scripting. This is strictly for high-precision computation in JavaScript, TypeScript, or Python only. \
72 Do NOT fall back to shell to run deno, python, or node — use this tool directly.",
73 serde_json::json!({
74 "type": "object",
75 "properties": {
76 "language": {
77 "type": "string",
78 "enum": ["javascript", "typescript", "python"],
79 "description": "The language to run. javascript/typescript requires Deno; python requires Python 3."
80 },
81 "code": {
82 "type": "string",
83 "description": "The code to execute. Keep it short and self-contained. Print results to stdout."
84 },
85 "timeout_seconds": {
86 "type": "integer",
87 "description": "Max execution time in seconds (default 10, max 60). Use higher values for longer computations."
88 }
89 },
90 "required": ["language", "code"]
91 }),
92 ),
93
94 make_tool(
95 "trace_runtime_flow",
96 "Return an authoritative read-only trace of Hematite runtime flow. \
97 Use this for architecture questions about keyboard input to final output, \
98 reasoning/specular separation, startup wiring, runtime subsystems, \
99 voice synthesis and Ctrl+T toggle, or \
100 session reset commands like /clear, /new, and /forget. Prefer this over guessing.",
101 serde_json::json!({
102 "type": "object",
103 "properties": {
104 "topic": {
105 "type": "string",
106 "enum": ["user_turn", "session_reset", "reasoning_split", "runtime_subsystems", "startup", "voice"],
107 "description": "Which verified runtime report to return. Use 'voice' for any question about Ctrl+T, voice toggle, or TTS pipeline. Use 'user_turn' for keyboard-to-output flow. Use 'session_reset' for /clear, /forget, /new. Use 'startup' for startup wiring. Use 'reasoning_split' for specular/thought routing. Use 'runtime_subsystems' for background subsystem overview."
108 },
109 "input": {
110 "type": "string",
111 "description": "Optional user input to label a normal user-turn trace"
112 },
113 "command": {
114 "type": "string",
115 "enum": ["/clear", "/new", "/forget", "all"],
116 "description": "Optional reset command when topic=session_reset"
117 }
118 },
119 "required": ["topic"]
120 }),
121 ),
122 make_tool(
123 "describe_toolchain",
124 "Return an authoritative read-only description of Hematite's actual tool surface and investigation strategy. \
125 Use this for tooling-discipline questions, best-tool selection, or read-only plans for tracing runtime behavior. \
126 Prefer this over improvising tool names or investigation steps from memory.",
127 serde_json::json!({
128 "type": "object",
129 "properties": {
130 "topic": {
131 "type": "string",
132 "enum": ["read_only_codebase", "user_turn_plan", "voice_latency_plan", "host_inspection_plan", "all"],
133 "description": "Which authoritative toolchain report to return"
134 },
135 "question": {
136 "type": "string",
137 "description": "Optional user question to label or tailor the read-only investigation plan"
138 }
139 }
140 }),
141 ),
142 make_tool(
143 "inspect_host",
144 "Return a structured read-only inspection of the current machine and environment. \
145 Prefer this over raw shell for questions about OS configuration (firewall, power, uptime), plain-English system health reports, silicon health and high-fidelity hardware telemetry (NVIDIA clocks/fans/power, CPU frequency averaging), installed developer tools, PATH issues, package-manager and environment health, network state, service state, running processes, desktop items, Downloads size, listening ports, repo health, or directory/disk summaries. \
146 For high-performance hardware testing, use topic=disk_benchmark to measure real-time kernel disk queue intensity. \
147 For remediation questions phrased like 'how do I fix cargo not found', 'how do I fix port 3000 already in use', or 'how do I fix LM Studio not reachable', use topic=fix_plan instead of diagnosis-only topics like env_doctor, path, or ports. \
148 Use topic=summary for a compact host snapshot, topic=toolchains for common dev tool versions, topic=path for PATH analysis, topic=env_doctor for package-manager and PATH health, topic=fix_plan for structured remediation plans, topic=network for adapters/IPs/gateways/DNS, topic=services for service status and startup mode, \
149 topic=processes for top processes by memory/cpu and real-time disk/network I/O stats (look for [I/O R:N/W:N] tags to identify disk-heavy processes), \
150 topic=desktop or topic=downloads for known folders, topic=ports for listening endpoints, topic=repo_doctor for a structured workspace health report, \
151 topic=log_check for recent critical/error events from system event logs or journalctl, topic=startup_items for programs and services that run at boot (registry Run keys and startup folders on Windows; systemd enabled units on Linux), \
152 topic=health_report for a plain-English tiered system health verdict (disk, RAM, tools, recent errors), \
153 topic=storage for all drives with capacity/free space plus large developer cache directories, \
154 topic=hardware for CPU model/cores, RAM size/speed, GPU name/driver, motherboard, BIOS, and display configuration, \
155 topic=updates for Windows Update status (last install date, pending update count, WU service state), \
156 topic=security for Windows Defender real-time protection status, last scan date, signature age, firewall profile states, Windows activation, and UAC state, \
157 topic=pending_reboot to check whether a system restart is required and why (Windows Update, CBS, file rename operations), \
158 topic=disk_health for physical drive health via Get-PhysicalDisk and SMART failure prediction, \
159 topic=battery for charge level, status, estimated runtime, and wear level (laptops only — reports no battery on desktops), \
160 topic=recent_crashes for BSOD and unexpected shutdown events plus application crash/hang events from the Windows event log, \
161 topic=scheduled_tasks for all non-disabled scheduled tasks including name, path, last run time, and executable, \
162 topic=dev_conflicts for cross-tool environment conflict detection (Node.js version managers, Python 2 vs 3 ambiguity, conda env shadowing, Rust toolchain path conflicts, Git identity/signing config, duplicate PATH entries), \
163 topic=bitlocker for drive encryption status (BitLocker on Windows, LUKS on Linux), \
164 topic=ad_user for Active Directory / Managed Identity details (SID, group memberships, domain role), \
165 topic=user_accounts for Local User and Group diagnostics (Built-in Administrators, local account state), \
166 topic=rdp for Remote Desktop configuration, port, and active sessions, \
167 topic=shadow_copies for Volume Shadow Copies (VSS) and system restore points, \
168 topic=pagefile for Windows page file configuration and current usage, \
169 topic=windows_features for enabled Windows optional features (IIS, Hyper-V, etc.), \
170 topic=printers for installed printers and active print jobs, \
171 topic=winrm for Windows Remote Management (WinRM) and PS Remoting status, \
172 topic=network_stats for adapter throughput (RX/TX), errors, and dropped packets, \
173 topic=udp_ports for active UDP listeners and notable port annotations, \
174 topic=gpo for applied Group Policy Objects, topic=certificates for local personal certificates, topic=integrity for Windows component store health (SFC/DISM state), topic=domain for Active Directory and domain join status, \
175 topic=device_health for identifying malfunctioning hardware with ConfigManager error codes (Yellow Bangs), topic=drivers for auditing active system drivers and their states, topic=peripherals for enumerating connected USB, input, and display hardware, \
176 topic=sessions for auditing active and disconnected user logon sessions, \
177 topic=ad_user for specific Active Directory user identity, SID, and group membership auditing, \
178 topic=dns_lookup for precision DNS record queries (SRV, MX, TXT), \
179 topic=mdm_enrollment for Intune/MDM enrollment state, Azure AD join, and device management health, \
180 topic=hyperv for local Hyper-V VM inventory and real-time load, \
181 topic=ip_config for detailed adapter configuration and DHCP lease state, \
182 topic=disk_benchmark for high-performance silicon-aware stress testing, \
183 topic=storage_spaces for Windows Storage Spaces pools, virtual disks, physical disk health, and Linux mdadm/LVM, \
184 topic=defender_quarantine for Windows Defender threat detections, quarantine history, and scan summary, \
185 topic=domain_health for domain controller connectivity, LDAP port tests, dsregcmd join state, and GPO last refresh, \
186 topic=service_dependencies for service dependency graph (what requires what, restart cascade planning), \
187 topic=wmi_health for WMI repository integrity, winmgmt verify, and repair steps, \
188 topic=local_security_policy for password/lockout policy, LM compatibility level, and UAC settings, \
189 topic=usb_history for USB device connection history from the USBSTOR registry, \
190 topic=print_spooler for Print Spooler state, PrintNightmare (CVE-2021-34527) hardening check, and print queue, \
191 and topic=directory or topic=disk for arbitrary paths.",
192 serde_json::json!({
193 "type": "object",
194 "properties": {
195 "topic": {
196 "type": "string",
197 "enum": ["summary", "toolchains", "path", "env_doctor", "fix_plan", "network", "services", "processes", "desktop", "downloads", "directory", "disk", "ports", "repo_doctor", "log_check", "startup_items", "health_report", "storage", "hardware", "updates", "security", "pending_reboot", "disk_health", "battery", "recent_crashes", "scheduled_tasks", "dev_conflicts", "os_config", "bitlocker", "rdp", "shadow_copies", "pagefile", "windows_features", "printers", "winrm", "network_stats", "udp_ports", "gpo", "certificates", "integrity", "domain", "domain_health", "device_health", "drivers", "peripherals", "disk_benchmark", "permissions", "login_history", "registry_audit", "share_access", "thermal", "activation", "patch_history", "ad_user", "dns_lookup", "hyperv", "ip_config", "mdm_enrollment", "storage_spaces", "defender_quarantine", "service_dependencies", "wmi_health", "local_security_policy", "usb_history", "print_spooler"],
198 "description": "Which structured host inspection to run. Use topic=ad_user for domain identity audit, topic=dns_lookup for SRV/MX records, topic=hyperv for VM load, topic=ip_config for detailed adapter info, topic=mdm_enrollment for Intune/MDM enrollment state, topic=storage_spaces for Windows Storage Spaces/RAID pools, topic=defender_quarantine for Defender threat history, topic=domain_health for DC connectivity and LDAP tests, topic=service_dependencies for restart cascade planning, topic=wmi_health for WMI repository integrity, topic=local_security_policy for password/lockout/NTLMv2 policy, topic=usb_history for USB forensics, and topic=print_spooler for PrintNightmare check."
199 },
200 "name": {
201 "type": "string",
202 "description": "Optional when topic=processes or topic=services. Case-insensitive substring filter for process or service names."
203 },
204 "issue": {
205 "type": "string",
206 "description": "Optional when topic=fix_plan. Plain-English issue description such as 'cargo not found', 'port 3000 already in use', or 'LM Studio not reachable on localhost:1234'."
207 },
208 "path": {
209 "type": "string",
210 "description": "Required when topic=directory. Optional for topic=disk or topic=repo_doctor. Absolute or relative path to inspect."
211 },
212 "port": {
213 "type": "integer",
214 "description": "Optional when topic=ports or topic=fix_plan. Filter the result to one listening TCP port or anchor a port-conflict fix plan."
215 },
216 "max_entries": {
217 "type": "integer",
218 "description": "Optional cap for listed entries. Defaults to 10 and is capped internally."
219 }
220 }
221 }),
222 ),
223 make_tool(
224 "resolve_host_issue",
225 "A safe, bounded tool for remediating OS and environment issues automatically with user approval. \
226 Use this to fix missing dependencies, restart stuck services, or clear disk space instead of using raw shell. \
227 The user will be prompted to approve the action. Keep targets exact.",
228 serde_json::json!({
229 "type": "object",
230 "properties": {
231 "action": {
232 "type": "string",
233 "enum": ["install_package", "restart_service", "clear_temp"],
234 "description": "The type of remediation to perform."
235 },
236 "target": {
237 "type": "string",
238 "description": "The specific target (e.g., 'python' for install_package, or 'docker' for restart_service). Optional for clear_temp."
239 }
240 },
241 "required": ["action"]
242 }),
243 ),
244 make_tool(
245 "run_hematite_maintainer_workflow",
246 "Run one of Hematite's known maintainer or release workflows with explicit approval. \
247 Prefer this over raw shell when the user explicitly asks to run one of Hematite's own scripts such as `clean.ps1`, `scripts/package-windows.ps1`, or `release.ps1`. \
248 Use workflow=clean for cleanup, workflow=package_windows for rebuilding the local Windows portable or installer, and workflow=release for the normal version bump/tag/push/publish flow. \
249 Keep this tool constrained to Hematite's own known workflows instead of inventing ad hoc shell commands or pretending to run arbitrary project scripts.",
250 serde_json::json!({
251 "type": "object",
252 "properties": {
253 "workflow": {
254 "type": "string",
255 "enum": ["clean", "package_windows", "release"],
256 "description": "Which known Hematite maintainer workflow to run."
257 },
258 "deep": {
259 "type": "boolean",
260 "description": "For workflow=clean. Also remove heavy build/runtime artifacts such as target/ and vein.db."
261 },
262 "reset": {
263 "type": "boolean",
264 "description": "For workflow=clean. Reset PLAN/TASK state in addition to normal cleanup."
265 },
266 "prune_dist": {
267 "type": "boolean",
268 "description": "For workflow=clean. Keep only the current Cargo.toml version under dist/."
269 },
270 "installer": {
271 "type": "boolean",
272 "description": "For workflow=package_windows. Also build the Windows installer."
273 },
274 "add_to_path": {
275 "type": "boolean",
276 "description": "For workflow=package_windows or workflow=release. Update the user PATH to the rebuilt portable."
277 },
278 "version": {
279 "type": "string",
280 "description": "For workflow=release. Exact semantic version such as 0.4.5."
281 },
282 "bump": {
283 "type": "string",
284 "enum": ["patch", "minor", "major"],
285 "description": "For workflow=release. Ask release.ps1 to calculate the next version."
286 },
287 "push": {
288 "type": "boolean",
289 "description": "For workflow=release. Push main and the new tag."
290 },
291 "skip_installer": {
292 "type": "boolean",
293 "description": "For workflow=release. Skip the Windows installer build."
294 },
295 "publish_crates": {
296 "type": "boolean",
297 "description": "For workflow=release. Publish hematite-cli to crates.io after a successful push."
298 },
299 "publish_voice_crate": {
300 "type": "boolean",
301 "description": "For workflow=release. Publish hematite-kokoros first, then hematite-cli."
302 }
303 },
304 "required": ["workflow"]
305 }),
306 ),
307 make_tool(
308 "run_workspace_workflow",
309 "Run an approval-gated workflow or script in the locked project workspace root. \
310 Use this for the current project's build, test, lint, fix, package.json scripts, just/task/make targets, explicit local script paths, exact workspace commands, or typed website server control. \
311 Website workflows are preferred when working on a local web app because they give Hematite a structured start/probe/validate/status/stop loop with stored runtime metadata instead of improvised shell. \
312 FORBIDDEN: The `command` field MUST be a real executable shell command (e.g. `npm install`, `cargo build`). \
313 NEVER put natural language, user-requests, or conversational intent into the `command` field. \
314 This tool is for the active workspace, not for Hematite's own maintainer scripts.",
315 serde_json::json!({
316 "type": "object",
317 "properties": {
318 "workflow": {
319 "type": "string",
320 "enum": ["build", "test", "lint", "fix", "package_script", "task", "just", "make", "script_path", "command", "website_start", "website_probe", "website_validate", "website_status", "website_stop"],
321 "description": "Which workspace workflow to run."
322 },
323 "name": {
324 "type": "string",
325 "description": "Required for workflow=package_script, task, just, or make. The script or target name."
326 },
327 "path": {
328 "type": "string",
329 "description": "Required for workflow=script_path. Relative path to a script inside the locked workspace root."
330 },
331 "command": {
332 "type": "string",
333 "description": "Required for workflow=command. Exact command to execute from the locked workspace root."
334 },
335 "mode": {
336 "type": "string",
337 "enum": ["dev", "preview", "start"],
338 "description": "Optional for workflow=website_start. Which website server mode to infer. Defaults to dev."
339 },
340 "script": {
341 "type": "string",
342 "description": "Optional for workflow=website_start. Exact package.json script to run instead of inferring one."
343 },
344 "url": {
345 "type": "string",
346 "description": "Optional for workflow=website_start, website_probe, or website_validate. Explicit local URL to probe, such as http://127.0.0.1:5173/."
347 },
348 "host": {
349 "type": "string",
350 "description": "Optional for workflow=website_start. Host used when constructing an inferred probe URL. Defaults to 127.0.0.1."
351 },
352 "port": {
353 "type": "integer",
354 "description": "Optional for workflow=website_start. Port used when constructing an inferred probe URL."
355 },
356 "label": {
357 "type": "string",
358 "description": "Optional for website workflows. Logical server name for storing runtime metadata. Defaults to default."
359 },
360 "routes": {
361 "type": "array",
362 "items": { "type": "string" },
363 "description": "Optional for workflow=website_validate. Relative routes or absolute URLs to validate, such as [\"/\", \"/pricing\", \"/about\"]."
364 },
365 "asset_limit": {
366 "type": "integer",
367 "description": "Optional for workflow=website_validate. Maximum number of linked local assets to probe after route validation."
368 },
369 "request_timeout_ms": {
370 "type": "integer",
371 "description": "Optional for workflow=website_start. Per-request HTTP timeout used by the readiness probe."
372 },
373 "timeout_ms": {
374 "type": "integer",
375 "description": "Optional timeout override in milliseconds. For website_start this is the boot/readiness timeout. For website_probe and website_status it is the probe timeout."
376 }
377 },
378 "required": ["workflow"]
379 }),
380 ),
381 make_tool(
382 "read_file",
383 "Read the contents of a file. For large files, use 'offset' and 'limit' to navigate.",
384 serde_json::json!({
385 "type": "object",
386 "properties": {
387 "path": {
388 "type": "string",
389 "description": "Path to the file, relative to the project root"
390 },
391 "offset": {
392 "type": "integer",
393 "description": "Starting line number (0-indexed)"
394 },
395 "limit": {
396 "type": "integer",
397 "description": "Number of lines to read"
398 }
399 },
400 "required": ["path"]
401 }),
402 ),
403 make_tool(
404 "lsp_definitions",
405 "Get the precise definition location (file:line:char) for a symbol at a specific position. \
406 Use this to jump to function/struct source code accurately.",
407 serde_json::json!({
408 "type": "object",
409 "properties": {
410 "path": { "type": "string", "description": "File path" },
411 "line": { "type": "integer", "description": "0-indexed line" },
412 "character": { "type": "integer", "description": "0-indexed character" }
413 },
414 "required": ["path", "line", "character"]
415 }),
416 ),
417 make_tool(
418 "lsp_references",
419 "Find all locations where a symbol is used across the entire workspace. \
420 Use this to understand the impact of a refactor or discover internal API users.",
421 serde_json::json!({
422 "type": "object",
423 "properties": {
424 "path": { "type": "string", "description": "File path" },
425 "line": { "type": "integer", "description": "0-indexed line" },
426 "character": { "type": "integer", "description": "0-indexed character" }
427 },
428 "required": ["path", "line", "character"]
429 }),
430 ),
431 make_tool(
432 "lsp_hover",
433 "Get hover information (documentation, function signature, type details) for a symbol. \
434 Use this for rapid spatial awareness without opening every file.",
435 serde_json::json!({
436 "type": "object",
437 "properties": {
438 "path": { "type": "string", "description": "File path" },
439 "line": { "type": "integer", "description": "0-indexed line" },
440 "character": { "type": "integer", "description": "0-indexed character" }
441 },
442 "required": ["path", "line", "character"]
443 }),
444 ),
445 make_tool(
446 "lsp_rename_symbol",
447 "Rename a symbol project-wide using the Language Server. Ensures all references are updated safely.",
448 serde_json::json!({
449 "type": "object",
450 "properties": {
451 "path": { "type": "string", "description": "File path" },
452 "line": { "type": "integer", "description": "0-indexed line" },
453 "character": { "type": "integer", "description": "0-indexed character" },
454 "new_name": { "type": "string", "description": "The new name for the symbol" }
455 },
456 "required": ["path", "line", "character", "new_name"]
457 }),
458 ),
459 make_tool(
460 "lsp_get_diagnostics",
461 "Get a list of current compiler errors and warnings for a specific file. \
462 Use this to verify your code compiles and and to find exactly where errors are located.",
463 serde_json::json!({
464 "type": "object",
465 "properties": {
466 "path": { "type": "string", "description": "File path" }
467 },
468 "required": ["path"]
469 }),
470 ),
471 make_tool(
472 "vision_analyze",
473 "Send an image file (screenshot, diagram, or UI mockup) to the multimodal vision model for technical analysis. \
474 Use this to identify UI bugs, confirm visual states, or understand architectural diagrams.",
475 serde_json::json!({
476 "type": "object",
477 "properties": {
478 "path": { "type": "string", "description": "Absolute or relative path to the image file." },
479 "prompt": { "type": "string", "description": "The specific question or analysis request for the vision model." }
480 },
481 "required": ["path", "prompt"]
482 }),
483 ),
484 make_tool(
485 "patch_hunk",
486 "Replace a specific line range [start_line, end_line] with new content. \
487 This is the most precise way to edit code and avoids search string failures.",
488 serde_json::json!({
489 "type": "object",
490 "properties": {
491 "path": { "type": "string", "description": "File path" },
492 "start_line": { "type": "integer", "description": "Starting line (1-indexed)" },
493 "end_line": { "type": "integer", "description": "Ending line (inclusive)" },
494 "replacement": { "type": "string", "description": "The new content for this range" }
495 },
496 "required": ["path", "start_line", "end_line", "replacement"]
497 }),
498 ),
499 make_tool(
500 "multi_search_replace",
501 "Replace multiple existing code blocks in a single file with new content. \
502 Each hunk specifies an EXACT 'search' string and a 'replace' string. \
503 The 'search' string MUST exactly match the existing file contents (including whitespace). \
504 This is the safest and most reliable way to make multiple structural edits.",
505 serde_json::json!({
506 "type": "object",
507 "properties": {
508 "path": { "type": "string", "description": "File path" },
509 "hunks": {
510 "type": "array",
511 "items": {
512 "type": "object",
513 "properties": {
514 "search": { "type": "string", "description": "Exact existing text to find and replace" },
515 "replace": { "type": "string", "description": "The new replacement text" }
516 },
517 "required": ["search", "replace"]
518 }
519 }
520 },
521 "required": ["path", "hunks"]
522 }),
523 ),
524 make_tool(
525 "write_file",
526 "Write content to a file, creating it (and any parent dirs) if needed. \
527 Overwrites existing files. \
528 SOVEREIGN PATHING: For files in common areas, use `@DESKTOP/file.txt`, `@DOCUMENTS/file.txt`, `@DOWNLOADS/file.txt`, or `@HOME/file.txt` to ensure 100% path accuracy.",
529 serde_json::json!({
530 "type": "object",
531 "properties": {
532 "path": { "type": "string", "description": "File path" },
533 "content": { "type": "string", "description": "Full file content to write" }
534 },
535 "required": ["path", "content"]
536 }),
537 ),
538 make_tool(
539 "create_directory",
540 "Authoritatively create a new directory (and any parent dirs) if they do not exist. \
541 Use this instead of raw shell (mkdir) for all filesystem organization. \
542 Supports both relative paths and absolute paths. \
543 SOVEREIGN PATHING: For directories in common areas, use `@DESKTOP/folder`, `@DOCUMENTS/folder`, `@DOWNLOADS/folder`, or `@HOME/folder` to ensure 100% path accuracy.",
544 serde_json::json!({
545 "type": "object",
546 "properties": {
547 "path": { "type": "string", "description": "Relative or absolute directory path" }
548 },
549 "required": ["path"]
550 }),
551 ),
552 make_tool(
553 "research_web",
554 "Perform a zero-cost technical search using DuckDuckGo. \
555 Use this to find documentation, latest API changes, or solutions to complex errors \
556 when your internal knowledge is insufficient. Returns snippets and URLs.",
557 serde_json::json!({
558 "type": "object",
559 "properties": {
560 "query": { "type": "string", "description": "The technical search query" }
561 },
562 "required": ["query"]
563 }),
564 ),
565 make_tool(
566 "fetch_docs",
567 "Fetch a URL and convert it to clean Markdown. Use this to 'read' the documentation \
568 links found via research_web. This tool uses a proxy to bypass IP blocks.",
569 serde_json::json!({
570 "type": "object",
571 "properties": {
572 "url": { "type": "string", "description": "The URL of the documentation to fetch" }
573 },
574 "required": ["url"]
575 }),
576 ),
577 make_tool(
578 "edit_file",
579 "Edit a file by replacing an exact string with another. \
580 The 'search' string does NOT need perfectly matching indentation (it is fuzzy), \
581 but the non-whitespace text must match exactly. Use this for targeted edits.",
582 serde_json::json!({
583 "type": "object",
584 "properties": {
585 "path": { "type": "string", "description": "File path" },
586 "search": {
587 "type": "string",
588 "description": "The exact text to find (must match whitespace/indentation precisely)"
589 },
590 "replace": {
591 "type": "string",
592 "description": "The replacement text"
593 }
594 },
595 "required": ["path", "search", "replace"]
596 }),
597 ),
598 make_tool(
599 "auto_pin_context",
600 "Select 1-3 core files to 'Lock' into prioritized memory. \
601 Use this to ensure the most important architecture files \
602 are always visible during complex refactorings.",
603 serde_json::json!({
604 "type": "object",
605 "properties": {
606 "paths": {
607 "type": "array",
608 "items": { "type": "string" }
609 },
610 "reason": { "type": "string" }
611 },
612 "required": ["paths", "reason"]
613 }),
614 ),
615 make_tool(
616 "list_pinned",
617 "List all files currently pinned in the model's active context.",
618 serde_json::json!({
619 "type": "object",
620 "properties": {}
621 }),
622 ),
623 make_tool(
624 "list_files",
625 "List files in a directory, optionally filtered by extension.",
626 serde_json::json!({
627 "type": "object",
628 "properties": {
629 "path": {
630 "type": "string",
631 "description": "Directory to list (default: current dir)"
632 },
633 "extension": {
634 "type": "string",
635 "description": "Only return files with this extension, e.g. 'rs', 'toml' (no dot)"
636 }
637 },
638 "required": []
639 }),
640 ),
641 make_tool(
642 "tail_file",
643 "Read the last N lines of a file — useful for log files, test output, \
644 build artifacts, and any large file where only the tail is relevant. \
645 Supports an optional grep filter to show only matching lines from the tail. \
646 Use this instead of read_file when you only need the end of a large file.",
647 serde_json::json!({
648 "type": "object",
649 "properties": {
650 "path": {
651 "type": "string",
652 "description": "Path to the file, relative to the project root"
653 },
654 "lines": {
655 "type": "integer",
656 "description": "Number of lines to return from the end (default: 50, max: 500)"
657 },
658 "grep": {
659 "type": "string",
660 "description": "Optional regex pattern — only return lines matching this pattern (applied before the tail slice)"
661 }
662 },
663 "required": ["path"]
664 }),
665 ),
666 make_tool(
667 "grep_files",
668 "Search file contents for a regex pattern. Supports context lines, files-only mode, \
669 and pagination. Returns file:line:content format by default.",
670 serde_json::json!({
671 "type": "object",
672 "properties": {
673 "pattern": {
674 "type": "string",
675 "description": "Regex pattern to search for (case-insensitive by default)"
676 },
677 "path": {
678 "type": "string",
679 "description": "Directory to search (default: current dir)"
680 },
681 "extension": {
682 "type": "string",
683 "description": "Only search files with this extension, e.g. 'rs'"
684 },
685 "mode": {
686 "type": "string",
687 "enum": ["content", "files_only"],
688 "description": "'content' (default) returns matching lines; 'files_only' returns only filenames"
689 },
690 "context": {
691 "type": "integer",
692 "description": "Lines of context before AND after each match (like rg -C)"
693 },
694 "before": {
695 "type": "integer",
696 "description": "Lines of context before each match (overrides context)"
697 },
698 "after": {
699 "type": "integer",
700 "description": "Lines of context after each match (overrides context)"
701 },
702 "head_limit": {
703 "type": "integer",
704 "description": "Max hunks (or files in files_only) to return (default: 50)"
705 },
706 "offset": {
707 "type": "integer",
708 "description": "Skip first N hunks/files - for pagination (default: 0)"
709 }
710 },
711 "required": ["pattern"]
712 }),
713 ),
714 make_tool(
715 "github_ops",
716 "Interact with GitHub via the `gh` CLI. Requires `gh` installed and `gh auth login` completed. \
717 Use for pull requests, issues, CI run status, and repo metadata. \
718 Never use `shell` to call `gh` — use this tool instead.",
719 serde_json::json!({
720 "type": "object",
721 "properties": {
722 "action": {
723 "type": "string",
724 "enum": [
725 "pr_list", "pr_view", "pr_create", "pr_status", "pr_checks", "pr_merge",
726 "issue_list", "issue_view", "issue_create",
727 "ci_status", "run_view",
728 "repo_view", "release_list"
729 ],
730 "description": "GitHub operation to perform"
731 },
732 "title": { "type": "string", "description": "PR or issue title (for create actions)" },
733 "body": { "type": "string", "description": "PR or issue body (for create actions)" },
734 "base": { "type": "string", "description": "Base branch for PR (default: main)" },
735 "draft": { "type": "boolean", "description": "Create PR as draft" },
736 "pr": { "type": "string", "description": "PR number or URL (for view/checks/merge)" },
737 "number": { "description": "Issue number (for issue_view)" },
738 "state": { "type": "string", "enum": ["open", "closed", "all"], "description": "Filter state for listings" },
739 "strategy": { "type": "string", "enum": ["merge", "squash", "rebase"], "description": "Merge strategy for pr_merge" },
740 "branch": { "type": "string", "description": "Branch name for ci_status (defaults to current branch)" },
741 "run_id": { "type": "string", "description": "Run ID for run_view" },
742 "limit": { "type": "integer", "description": "Max results to return (default 10)" }
743 },
744 "required": ["action"]
745 }),
746 ),
747 make_tool(
748 "git_commit",
749 "Stage all changes (git add -A) and create a commit. You MUST use 'Conventional Commits' (e.g. 'feat: description').",
750 serde_json::json!({
751 "type": "object",
752 "properties": {
753 "message": { "type": "string", "description": "Commit message (Conventional Commit style)" }
754 },
755 "required": ["message"]
756 }),
757 ),
758 make_tool(
759 "git_push",
760 "Push current branched changes to the remote origin. Requires an existing remote connection.",
761 serde_json::json!({
762 "type": "object",
763 "properties": {},
764 "required": []
765 }),
766 ),
767 make_tool(
768 "git_remote",
769 "View or manage git remotes. Use this for onboarding to GitHub/GitLab services.",
770 serde_json::json!({
771 "type": "object",
772 "properties": {
773 "action": {
774 "type": "string",
775 "enum": ["list", "add", "remove"],
776 "description": "Operation to perform"
777 },
778 "name": { "type": "string", "description": "Remote name (e.g. origin)" },
779 "url": { "type": "string", "description": "Remote URL (for 'add' action)" }
780 },
781 "required": ["action"]
782 }),
783 ),
784 make_tool(
785 "git_onboarding",
786 "High-level wizard to connect this repository to a remote host (GitHub/GitLab). \
787 Handles adding the remote and performing the initial tracking push in one step.",
788 serde_json::json!({
789 "type": "object",
790 "properties": {
791 "url": { "type": "string", "description": "The remote repository URL (HTTPS or SSH)" },
792 "name": { "type": "string", "description": "The remote name (default: origin)" },
793 "push": { "type": "boolean", "description": "Whether to perform an initial push to establish tracking (default: false)" }
794 },
795 "required": ["url"]
796 }),
797 ),
798 make_tool(
799 "verify_build",
800 "Run project verification for build, test, lint, or fix workflows. \
801 Prefer per-project verify profiles from `.hematite/settings.json`, and fall back to \
802 auto-detected defaults when no profile is configured. Returns BUILD OK or BUILD FAILED \
803 with command output. ALWAYS call this after scaffolding a new project or making structural changes.",
804 serde_json::json!({
805 "type": "object",
806 "properties": {
807 "action": {
808 "type": "string",
809 "enum": ["build", "test", "lint", "fix"],
810 "description": "Which verification action to run. Defaults to build."
811 },
812 "profile": {
813 "type": "string",
814 "description": "Optional named verify profile from `.hematite/settings.json`."
815 },
816 "timeout_secs": {
817 "type": "integer",
818 "description": "Optional timeout override for this verification run."
819 }
820 }
821 }),
822 ),
823 make_tool(
824 "git_worktree",
825 "Manage Git worktrees - isolated working directories on separate branches. \
826 Use 'add' to create a safe sandbox for risky/experimental work, \
827 'list' to see all worktrees, 'remove' to clean up, 'prune' to remove stale entries.",
828 serde_json::json!({
829 "type": "object",
830 "properties": {
831 "action": {
832 "type": "string",
833 "enum": ["list", "add", "remove", "prune"],
834 "description": "Worktree operation to perform"
835 },
836 "path": {
837 "type": "string",
838 "description": "Directory path for the new worktree (required for add/remove)"
839 },
840 "branch": {
841 "type": "string",
842 "description": "Branch name for the worktree (add only; defaults to path basename)"
843 }
844 },
845 "required": ["action"]
846 }),
847 ),
848 make_tool(
849 "clarify",
850 "Ask the user a clarifying question when you genuinely cannot proceed without \
851 more information. Use this ONLY when you are blocked and cannot make a \
852 reasonable assumption. Do NOT use it to ask permission - just act.",
853 serde_json::json!({
854 "type": "object",
855 "properties": {
856 "question": {
857 "type": "string",
858 "description": "The specific question to ask the user"
859 }
860 },
861 "required": ["question"]
862 }),
863 ),
864 make_tool(
865 "manage_tasks",
866 "Manage the persistent task ledger in .hematite/TASK.md. Use this to track long-term goals across restarts.",
867 crate::tools::tasks::get_tasks_params(),
868 ),
869 make_tool(
870 "maintain_plan",
871 "Document the architectural strategy and session blueprint in .hematite/PLAN.md. Use this to maintain context across restarts.",
872 crate::tools::plan::get_plan_params(),
873 ),
874 make_tool(
875 "generate_walkthrough",
876 "Generate a final session report in .hematite/WALKTHROUGH.md including achievements and verification results.",
877 crate::tools::plan::get_walkthrough_params(),
878 ),
879 make_tool(
880 "swarm",
881 "Delegate high-volume parallel tasks to a swarm of background workers. \
882 Use this for large-scale refactors, multi-file research, or parallel documentation updates. \
883 You must provide a 'tasks' array where each task has an 'id', 'target' (file), and 'instruction'.",
884 serde_json::json!({
885 "type": "object",
886 "properties": {
887 "tasks": {
888 "type": "array",
889 "items": {
890 "type": "object",
891 "properties": {
892 "id": { "type": "string" },
893 "target": { "type": "string", "description": "Target file or directory" },
894 "instruction": { "type": "string", "description": "Specific task for this worker" }
895 },
896 "required": ["id", "target", "instruction"]
897 }
898 },
899 "max_workers": {
900 "type": "integer",
901 "description": "Max parallel workers (default 3, auto-throttled by hardware)",
902 "default": 3
903 }
904 },
905 "required": ["tasks"]
906 }),
907 ),
908 ];
909
910 let lsp_defs = crate::tools::lsp_tools::get_lsp_definitions();
911 tools.push(make_tool(
912 "lsp_search_symbol",
913 "Find the location (file/line) of any function, struct, or variable in the entire project workspace. \
914 This is the fastest 'Golden Path' for navigating to a symbol by name.",
915 serde_json::json!({
916 "type": "object",
917 "properties": {
918 "query": { "type": "string", "description": "The name of the symbol to find (e.g. 'initialize_mcp')" }
919 },
920 "required": ["query"]
921 }),
922 ));
923 for def in lsp_defs {
924 let name = def["name"].as_str().unwrap();
925 tools.push(ToolDefinition {
926 tool_type: "function".into(),
927 function: ToolFunction {
928 name: name.into(),
929 description: def["description"].as_str().unwrap().into(),
930 parameters: def["parameters"].clone(),
931 },
932 metadata: tool_metadata_for_name(name),
933 });
934 }
935
936 tools
937}
938
939pub async fn dispatch_builtin_tool(
940 name: &str,
941 args: &Value,
942 config: &HematiteConfig,
943 budget_tokens: usize,
944) -> Result<String, String> {
945 match name {
946 "shell" => crate::tools::shell::execute(args, budget_tokens).await,
947 "run_code" => crate::tools::code_sandbox::execute(args).await,
948 "trace_runtime_flow" => crate::tools::runtime_trace::trace_runtime_flow(args).await,
949 "describe_toolchain" => crate::tools::toolchain::describe_toolchain(args).await,
950 "inspect_host" => crate::tools::host_inspect::inspect_host(args).await,
951 "resolve_host_issue" => crate::tools::host_inspect::resolve_host_issue(args).await,
952 "run_hematite_maintainer_workflow" => {
953 crate::tools::repo_script::run_hematite_maintainer_workflow(args).await
954 }
955 "run_workspace_workflow" => crate::tools::workspace_workflow::run_workspace_workflow(args).await,
956 "read_file" => crate::tools::file_ops::read_file(args, budget_tokens).await,
957 "inspect_lines" => crate::tools::file_ops::inspect_lines(args).await,
958 "tail_file" => crate::tools::file_ops::tail_file(args).await,
959 "write_file" => crate::tools::file_ops::write_file(args).await,
960 "create_directory" => crate::tools::file_ops::create_directory(args).await,
961 "edit_file" => crate::tools::file_ops::edit_file(args).await,
962 "patch_hunk" => crate::tools::file_ops::patch_hunk(args).await,
963 "multi_search_replace" => crate::tools::file_ops::multi_search_replace(args).await,
964 "list_files" => crate::tools::file_ops::list_files(args, budget_tokens).await,
965 "grep_files" => crate::tools::file_ops::grep_files(args, budget_tokens).await,
966 "github_ops" => crate::tools::github::execute(args).await,
967 "git_commit" => crate::tools::git::execute(args).await,
968 "git_push" => crate::tools::git::execute_push(args).await,
969 "git_remote" => crate::tools::git::execute_remote(args).await,
970 "git_onboarding" => crate::tools::git_onboarding::execute(args).await,
971 "verify_build" => crate::tools::verify_build::execute(args).await,
972 "git_worktree" => crate::tools::git::execute_worktree(args).await,
973 "health" => crate::tools::health::execute(args).await,
974 "research_web" => {
975 crate::tools::research::execute_search(args, config.searx_url.clone()).await
976 }
977 "fetch_docs" => crate::tools::research::execute_fetch(args).await,
978 "manage_tasks" => crate::tools::tasks::manage_tasks(args).await,
979 "maintain_plan" => crate::tools::plan::maintain_plan(args).await,
980 "generate_walkthrough" => crate::tools::plan::generate_walkthrough(args).await,
981 "clarify" => {
982 let q = args.get("question").and_then(|v| v.as_str()).unwrap_or("?");
983 Ok(format!("[clarify] {q}"))
984 }
985 "vision_analyze" => Err(
986 "Tool 'vision_analyze' must be dispatched by ConversationManager (it requires hardware engine access)."
987 .into(),
988 ),
989 other => {
990 if other.contains('.') || other.contains('/') || other.contains('\\') {
991 Err(format!(
992 "'{}' is a PATH, not a tool. You correctly identified the location, but you MUST use `read_file` or `list_files` (internal) or `powershell` (external) to access it.",
993 other
994 ))
995 } else if matches!(other.to_lowercase().as_str(), "hematite" | "assistant" | "ai") {
996 Err(format!(
997 "'{}' is YOUR IDENTITY, not a tool. Use list_files or read_file to explore the codebase.",
998 other
999 ))
1000 } else if matches!(
1001 other.to_lowercase().as_str(),
1002 "thought" | "think" | "reasoning" | "thinking" | "internal"
1003 ) {
1004 Err(format!(
1005 "'{}' is NOT a tool - it is a reasoning tag. Output your answer as plain text after your <think> block.",
1006 other
1007 ))
1008 } else {
1009 Err(format!("Unknown tool: '{}'", other))
1010 }
1011 }
1012 }
1013}
1014
1015pub fn get_mutation_label(name: &str, args: &Value) -> Option<String> {
1016 match name {
1017 "shell" => {
1018 let cmd = args.get("command").and_then(|v| v.as_str()).unwrap_or("");
1019 if cmd.contains("rm ") || cmd.contains("del ") {
1020 Some("Destructive File Deletion".into())
1021 } else if cmd.contains("mkdir ") {
1022 Some("Directory Creation".into())
1023 } else {
1024 Some("Execute Shell Command".into())
1025 }
1026 }
1027 "write_file" => {
1028 let path = args.get("path").and_then(|v| v.as_str()).unwrap_or("file");
1029 Some(format!("Create/Overwrite File: {}", path))
1030 }
1031 "create_directory" => {
1032 let path = args
1033 .get("path")
1034 .and_then(|v| v.as_str())
1035 .unwrap_or("folder");
1036 Some(format!("Create Directory: {}", path))
1037 }
1038 "edit_file" | "patch_hunk" | "multi_search_replace" => {
1039 let path = args.get("path").and_then(|v| v.as_str()).unwrap_or("file");
1040 Some(format!("Surgical Code Mutation: {}", path))
1041 }
1042 "github_ops" => {
1043 let action = args.get("action").and_then(|v| v.as_str()).unwrap_or("?");
1044 match action {
1045 "pr_create" | "pr_merge" | "issue_create" => Some(format!("GitHub: {}", action)),
1046 _ => None,
1047 }
1048 }
1049 "git_commit" => Some("Permanent Version History Commit".into()),
1050 "git_push" => Some("Remote Origin Synchronisation (Push)".into()),
1051 "resolve_host_issue" => Some("System-Level Host Remediation".into()),
1052 "run_workspace_workflow" => Some("Automated Workspace Re-alignment".into()),
1053 _ => None,
1054 }
1055}