Add recursive chat folder browsing and confirmed file writes

This commit is contained in:
Mark Gregory
2026-11-17 11:36:55 +00:00
parent 79831c26d7
commit 0b8a07b10f
4 changed files with 131 additions and 7 deletions
+41 -2
View File
@@ -105,6 +105,12 @@ struct ChatView: View {
@State private var showingLibrary = false
@State private var useDocuments = false
@State private var pendingWrite: ChatFileRequest.Write?
@State private var writeDestination: URL?
@State private var confirmFileWrite = false
@State private var replacingFile = false
@AppStorage("chatFileAccessFolder") private var chatFileScope = ""
@AppStorage("chatFileWriteEnabled") private var chatFileWriteEnabled = false
@State private var filePreview: ChatFilePreview?
@State private var fileError = ""
@@ -223,6 +229,22 @@ struct ChatView: View {
Button("Rename") { studio.renameSelectedChat(newTitle) }
Button("Cancel", role: .cancel) {}
}
.alert("Save File?", isPresented: $confirmFileWrite, presenting: writeDestination) { destination in
Button(replacingFile ? "Replace File" : "Save File") {
guard let pendingWrite, chatFileWriteEnabled else { return }
do {
let current = try ChatFileRequest.destination(pendingWrite.path, inside: chatFileScope)
guard current == destination else { throw StudioError("The destination changed. Review the command again.") }
_ = try ChatFileRequest.write(pendingWrite, inside: chatFileScope, replace: replacingFile)
fileError = "Saved \(destination.path)"
studio.prompt = ""
} catch { fileError = error.localizedDescription }
self.pendingWrite = nil
}
Button("Cancel", role: .cancel) { pendingWrite = nil }
} message: { destination in
Text(destination.path)
}
.alert("Delete Chat?", isPresented: $confirmDelete, presenting: chatToDelete) { chat in
Button("Delete", role: .destructive) { studio.deleteChat(chat.id) }
Button("Cancel", role: .cancel) {}
@@ -421,6 +443,20 @@ struct ChatView: View {
}
private func sendMessage() {
let draft = studio.prompt
if draft.lowercased().hasPrefix("/write ") {
do {
guard chatFileWriteEnabled else { throw StudioError("Enable file writing in Settings → Local Tools.") }
let request = try ChatFileRequest.Write(draft)
let destination = try ChatFileRequest.destination(request.path, inside: chatFileScope)
writeDestination = destination
replacingFile = FileManager.default.fileExists(atPath: destination.path)
pendingWrite = request
confirmFileWrite = true
fileError = ""
} catch { fileError = error.localizedDescription }
return
}
studio.send(endpoint: activeEndpoint, model: activeModel, mlxPath: mlxPath, thinking: thinking,
failover: failover, mlxOnly: mlxOnly, compatible: compatibleEnabled, recall: recall, useDocuments: useDocuments,
instructions: promptMode.instructions(custom: customPrompt),
@@ -1023,6 +1059,7 @@ enum DockerCommand {
struct LocalToolsView: View {
@ObservedObject var studio: StudioModel
@AppStorage("chatFileAccessFolder") private var chatFileAccessFolder = ""
@AppStorage("chatFileWriteEnabled") private var chatFileWriteEnabled = false
@State private var confirmFullAccess = false
@State private var line = "docker ps"
@State private var pending: [String]?
@@ -1051,10 +1088,12 @@ struct LocalToolsView: View {
}
Button("Allow Any Readable Location…") { confirmFullAccess = true }
if !chatFileAccessFolder.isEmpty {
Button("Turn Off") { chatFileAccessFolder = "" }
Button("Turn Off") { chatFileAccessFolder = ""; chatFileWriteEnabled = false }
}
}
Text("In chat, use /read followed by a file path, or /ls followed by a folder path. The selected model receives the preview. macOS privacy permissions still apply.")
Toggle("Allow file writing", isOn: $chatFileWriteEnabled)
.disabled(chatFileAccessFolder.isEmpty)
Text("Use /read for files, /ls for a folder, or /tree for nested folders. The selected model receives the preview. To save text locally, enter /write and a path, then add the text on the next line. macOS permissions still apply.")
.foregroundStyle(.secondary)
}
Section("Docker") {