Add recursive chat folder browsing and confirmed file writes
This commit is contained in:
+72
-4
@@ -648,7 +648,7 @@ enum GiteaRepositoryLookup {
|
||||
|
||||
enum ChatFileRequest {
|
||||
static func path(in prompt: String) -> String? {
|
||||
let prefixes = ["/read ", "/ls ", "read file ", "open file ", "list folder ", "can you read file "]
|
||||
let prefixes = ["/read ", "/ls ", "/tree ", "read file ", "open file ", "list folder ", "can you read file "]
|
||||
guard let prefix = prefixes.first(where: { prompt.lowercased().hasPrefix($0) }) else { return nil }
|
||||
let path = String(prompt.dropFirst(prefix.count)).trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if path.hasPrefix("\""), path.hasSuffix("\""), path.count >= 2 {
|
||||
@@ -657,21 +657,89 @@ enum ChatFileRequest {
|
||||
return path
|
||||
}
|
||||
|
||||
static func read(_ path: String, inside folder: String) throws -> ChatFilePreview {
|
||||
struct Write {
|
||||
let path: String
|
||||
let text: String
|
||||
|
||||
init(_ prompt: String) throws {
|
||||
let lines = prompt.split(separator: "\n", maxSplits: 1, omittingEmptySubsequences: false)
|
||||
guard lines.count == 2, let path = ChatFileRequest.path(in: "/read " + lines[0].dropFirst(7)),
|
||||
!path.isEmpty else {
|
||||
throw StudioError("Use /write followed by the file path, then enter the text on the next line.")
|
||||
}
|
||||
self.path = path
|
||||
text = String(lines[1])
|
||||
}
|
||||
}
|
||||
|
||||
static func destination(_ path: String, inside folder: String) throws -> URL {
|
||||
guard !folder.isEmpty else { throw StudioError("Enable file access in Settings → Local Tools.") }
|
||||
guard !path.isEmpty else { throw StudioError("Enter the path of a file or folder.") }
|
||||
let root = URL(fileURLWithPath: folder, isDirectory: true).standardizedFileURL.resolvingSymlinksInPath()
|
||||
let expanded = (path as NSString).expandingTildeInPath
|
||||
let candidate = (expanded.hasPrefix("/") ? URL(fileURLWithPath: expanded) : root.appending(path: expanded))
|
||||
.standardizedFileURL.resolvingSymlinksInPath()
|
||||
let requested = (expanded.hasPrefix("/") ? URL(fileURLWithPath: expanded) : root.appending(path: expanded))
|
||||
.standardizedFileURL
|
||||
let candidate = requested.path == "/" ? requested :
|
||||
requested.deletingLastPathComponent().resolvingSymlinksInPath()
|
||||
.appending(path: requested.lastPathComponent).resolvingSymlinksInPath()
|
||||
let rootPath = root.path == "/" ? "/" : root.path + "/"
|
||||
guard candidate.path == root.path || candidate.path.hasPrefix(rootPath) else {
|
||||
throw StudioError("This path is outside the folder enabled in Settings → Local Tools.")
|
||||
}
|
||||
return candidate
|
||||
}
|
||||
|
||||
static func write(_ request: Write, inside folder: String, replace: Bool) throws -> URL {
|
||||
let url = try destination(request.path, inside: folder)
|
||||
let parent = url.deletingLastPathComponent()
|
||||
guard try parent.resourceValues(forKeys: [.isDirectoryKey]).isDirectory == true else {
|
||||
throw StudioError("Choose a destination in an existing folder.")
|
||||
}
|
||||
if FileManager.default.fileExists(atPath: url.path),
|
||||
try url.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile != true {
|
||||
throw StudioError("The destination must be a regular file.")
|
||||
}
|
||||
if replace {
|
||||
guard FileManager.default.fileExists(atPath: url.path) else {
|
||||
throw StudioError("The file no longer exists. Review the command again.")
|
||||
}
|
||||
}
|
||||
try Data(request.text.utf8).write(to: url, options: replace ? .atomic : .withoutOverwriting)
|
||||
return url
|
||||
}
|
||||
|
||||
static func read(_ path: String, inside folder: String, recursive: Bool = false) throws -> ChatFilePreview {
|
||||
let candidate = try destination(path, inside: folder)
|
||||
let metadata = try candidate.resourceValues(forKeys: [.isDirectoryKey, .isRegularFileKey])
|
||||
guard metadata.isDirectory == true || metadata.isRegularFile == true else {
|
||||
throw StudioError("Choose a normal file or folder.")
|
||||
}
|
||||
if recursive {
|
||||
guard metadata.isDirectory == true else { throw StudioError("Choose a folder for /tree.") }
|
||||
let keys: [URLResourceKey] = [.isSymbolicLinkKey]
|
||||
guard let entries = FileManager.default.enumerator(at: candidate, includingPropertiesForKeys: keys,
|
||||
options: [.skipsHiddenFiles, .skipsPackageDescendants]) else {
|
||||
throw StudioError("This folder could not be opened.")
|
||||
}
|
||||
var paths: [String] = []
|
||||
var truncated = false
|
||||
let prefix = candidate.path == "/" ? "/" : candidate.path + "/"
|
||||
for case let entry as URL in entries {
|
||||
if (try? entry.resourceValues(forKeys: Set(keys)).isSymbolicLink) == true {
|
||||
entries.skipDescendants()
|
||||
continue
|
||||
}
|
||||
let path = String(entry.path.dropFirst(prefix.count))
|
||||
if paths.count == 100 || paths.reduce(0, { $0 + $1.count + 1 }) + path.count > 7500 {
|
||||
truncated = true
|
||||
break
|
||||
}
|
||||
paths.append(path)
|
||||
}
|
||||
let listing = paths.sorted().joined(separator: "\n")
|
||||
return ChatFilePreview(name: candidate.lastPathComponent,
|
||||
text: listing + (truncated ? "\n[Listing truncated]" : ""))
|
||||
}
|
||||
return try ChatFilePreview.read(candidate)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user