Add local file previews to chat and split the Toolbox layout

This commit is contained in:
Mark Gregory
2026-11-16 16:21:32 +00:00
parent 4ebf2b82be
commit 0f4b6b03e4
5 changed files with 107 additions and 15 deletions
+27
View File
@@ -395,3 +395,30 @@ enum GiteaRepositoryLookup {
"""
}
}
struct ChatFilePreview: Identifiable, Sendable {
let id = UUID()
let name: String
let text: String
static func read(_ url: URL) throws -> Self {
let scoped = url.startAccessingSecurityScopedResource()
defer { if scoped { url.stopAccessingSecurityScopedResource() } }
let text: String
if try url.resourceValues(forKeys: [.isDirectoryKey]).isDirectory == true {
let entries = try FileManager.default.contentsOfDirectory(
at: url, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles]
).sorted { $0.lastPathComponent.localizedStandardCompare($1.lastPathComponent) == .orderedAscending }
let names = entries.prefix(75).map { $0.lastPathComponent + ($0.hasDirectoryPath ? "/" : "") }
text = "Folder: \(url.lastPathComponent)\n" +
(names.isEmpty ? "(Empty)" : names.joined(separator: "\n")) +
(entries.count > 75 ? "\n[Listing limited to 75 entries]" : "")
} else if url.pathExtension.lowercased() == "pdf" {
text = try LibraryImport.chunks(at: url).prefix(5).joined(separator: "\n\n")
} else {
text = try LocalFilePreview.read(url, limit: 7500)
}
let bounded = text.count > 8000 ? String(text.prefix(7960)) + "\n[Preview truncated]" : text
return Self(name: url.lastPathComponent, text: bounded)
}
}