Add user-controlled local file access in chat

This commit is contained in:
Mark Gregory
2026-11-17 11:05:23 +00:00
parent c849b751d3
commit 8748b7ee80
5 changed files with 110 additions and 6 deletions
+30
View File
@@ -646,6 +646,36 @@ 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 "]
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 {
return String(path.dropFirst().dropLast())
}
return path
}
static func read(_ path: String, inside folder: String) throws -> ChatFilePreview {
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 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.")
}
let metadata = try candidate.resourceValues(forKeys: [.isDirectoryKey, .isRegularFileKey])
guard metadata.isDirectory == true || metadata.isRegularFile == true else {
throw StudioError("Choose a normal file or folder.")
}
return try ChatFilePreview.read(candidate)
}
}
struct ChatFilePreview: Identifiable, Sendable {
let id = UUID()
let name: String