dymokomi/luced
Source of release 0.4.1
0.4.1 / src / editor / ai_tools.luc
## Read-only inspection and undoable edit tools the AI agent may call. Every path is
## resolved under the workspace root and refused if it escapes; edits go through the
## editor, so one Undo reverts anything the model changed. Each tool returns a short
## string that is sent back to the model as that tool call's result.
from luce_ui.ui import TextEditor
from luce_std.files import FileKind
from workspace.controller import Workspace
from luce_std import files
import workspace.paths as paths
let MAX_RESULT = 24000
let MAX_FILE = 131072
let MAX_SCAN_FILES = 400
let MAX_MATCHES = 40
pub class AiTools:
let workspace: Workspace
var scanned: int = 0
var found: int = 0
pub func init(self, workspace: Workspace):
self.workspace = workspace
## Resolve `path` under the project root, or none if it would escape the root.
func resolve(self, path: str) -> str?:
if path.starts_with("/") or path.starts_with("~") or path.contains(".."):
return none
let root = self.workspace.directory()
if path == "" or path == ".":
return root
return paths.join(root, path)
func clip(self, text: str) -> str:
if text.length > MAX_RESULT:
return text[0..<MAX_RESULT] + "\n…(truncated)"
return text
func relative(self, full: str) -> str:
let root = self.workspace.directory()
if full.starts_with(root + "/"):
return full[root.length + 1..<full.length]
return full
pub func read_file(self, path: str) -> str:
let full = self.resolve(path) else return "error: path is outside the project"
let content = files.read_text(full, MAX_FILE) catch failure:
return "error: " + failure.message
return self.clip(content)
pub func list_dir(self, path: str) -> str:
let full = self.resolve(path) else return "error: path is outside the project"
let names = files.list(full) catch failure:
return "error: " + failure.message
var out = ""
var shown = 0
for name in names:
if name == "." or name == "..":
continue
let kind = files.metadata(paths.join(full, name)).kind catch failure: continue
out += (name + "/\n") if kind == FileKind.directory else (name + "\n")
shown += 1
if shown >= MAX_SCAN_FILES:
break
return self.clip(out if out != "" else "(empty)")
pub func find_text(self, query: str) -> str:
if query == "":
return "error: empty query"
self.scanned = 0
self.found = 0
let out = self.scan(self.workspace.directory(), query, "")
return self.clip(out if out != "" else "no matches")
func scan(self, dir: str, query: str, acc: str) -> str:
var out = acc
if self.scanned >= MAX_SCAN_FILES or self.found >= MAX_MATCHES:
return out
let names = files.list(dir) catch failure: return out
for name in names:
if self.found >= MAX_MATCHES:
break
if name.starts_with(".") or name in ["node_modules", "build", "__pycache__"]:
continue
let full = paths.join(dir, name)
let kind = files.metadata(full).kind catch failure: continue
if kind == FileKind.directory:
out = self.scan(full, query, out)
elif kind == FileKind.regular:
self.scanned += 1
if self.scanned > MAX_SCAN_FILES:
break
let content = files.read_text(full, MAX_FILE) catch failure: continue
let rel = self.relative(full)
var lineno = 0
for line in content.split("\n"):
lineno += 1
if line.contains(query):
out += rel + ":" + str(lineno) + ": " + line + "\n"
self.found += 1
if self.found >= MAX_MATCHES:
break
return out
## The selected text and the 1-based line range it covers, so the model can discover
## what the user is pointing at and where. Reports when nothing is selected.
pub func read_selection(self) -> str:
let editor = self.workspace.current_editor() else return "error: no active editor"
if not editor.has_selection():
return "No text is selected — use read_document to see the whole file."
let text = editor.selected_text() catch failure:
return "error: " + failure.message
return "Lines " + str(editor.selection_first_line()) + " to " + str(editor.selection_last_line()) + " are selected:\n" + self.clip(text)
## The current document with 1-based line numbers, so the model edits by line.
pub func read_document(self) -> str:
let editor = self.workspace.current_editor() else return "error: no active editor"
let full = editor.text() catch failure:
return "error: " + failure.message
return self.numbered(full)
## Replace lines `first`..`last` (inclusive, 1-based) with `text` (which may span
## several lines or be empty). Applied as one undoable edit; returns the updated
## numbered document so the next edit uses fresh line numbers.
pub func replace_lines(self, first: int, last: int, text: str) -> str:
let editor = self.workspace.current_editor() else return "error: no active editor"
let source = editor.text() catch failure:
return "error: " + failure.message
let lines = source.split("\n")
let count = lines.length
var lo = first if first >= 1 else 1
var hi = last if last <= count else count
if lo > hi:
return "error: first line is after last line"
let rebuilt = self.join(lines[0..<lo - 1] + text.split("\n") + lines[hi..<count])
return self.apply(editor, rebuilt)
## Insert `text` as new lines after line `after` (0 = at the very top).
pub func insert_lines(self, after: int, text: str) -> str:
let editor = self.workspace.current_editor() else return "error: no active editor"
let source = editor.text() catch failure:
return "error: " + failure.message
let lines = source.split("\n")
let count = lines.length
var at = after if after >= 0 else 0
if at > count:
at = count
let rebuilt = self.join(lines[0..<at] + text.split("\n") + lines[at..<count])
return self.apply(editor, rebuilt)
## Delete lines `first`..`last` (inclusive, 1-based).
pub func delete_lines(self, first: int, last: int) -> str:
let editor = self.workspace.current_editor() else return "error: no active editor"
let source = editor.text() catch failure:
return "error: " + failure.message
let lines = source.split("\n")
let count = lines.length
var lo = first if first >= 1 else 1
var hi = last if last <= count else count
if lo > hi:
return "error: first line is after last line"
let rebuilt = self.join(lines[0..<lo - 1] + lines[hi..<count])
return self.apply(editor, rebuilt)
## Replace the entire document with `text` (a reliable whole-file rewrite that avoids
## line-number arithmetic — best for a full pass like spellchecking the whole file).
pub func set_document(self, text: str) -> str:
let editor = self.workspace.current_editor() else return "error: no active editor"
return self.apply(editor, text)
## Replace the whole document with `text` as one undoable edit, then report the new
## numbered document. The editor is the single source of truth, so every edit tool
## rebuilds from the current text and lands here.
func apply(self, editor: TextEditor, text: str) -> str:
editor.select_all() catch failure:
return "error: " + failure.message
editor.insert(text) catch failure:
return "error: " + failure.message
return "ok — document is now:\n" + self.numbered(text)
func join(self, lines: list[str]) -> str:
var result = ""
for index in 0..<lines.length:
if index > 0:
result += "\n"
result += lines[index]
return result
func numbered(self, text: str) -> str:
let lines = text.split("\n")
var result = ""
for index in 0..<lines.length:
result += str(index + 1) + ": " + lines[index] + "\n"
if result.length > MAX_RESULT:
result += "…(truncated)\n"
break
return result