Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Sources/LocalLLMClientCore/LLMSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,28 @@ public extension LLMSession {
public func downloadModel(onProgress: @Sendable @escaping (Double) async -> Void = { _ in }) async throws {
try await downloader.download(onProgress: onProgress)
}

public static func pruneModels(in url: URL = FileDownloader.defaultRootDestination, excluding: [Model] = []) throws {
Comment thread
tattn marked this conversation as resolved.
Outdated
guard let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsPackageDescendants]) else { return }

let excludingNormalized: [URL] = excluding.compactMap {
let model = $0 as? Self
return model?.modelPath.resolvingSymlinksInPath().standardizedFileURL
}
Comment thread
ashtonmeuser marked this conversation as resolved.
Outdated

for case let fileURL as URL in enumerator {
if excludingNormalized.contains(fileURL.resolvingSymlinksInPath().standardizedFileURL) {
enumerator.skipDescendants()
continue
}

if (try? fileURL.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == false {
Comment thread
ashtonmeuser marked this conversation as resolved.
Outdated
try FileManager.default.removeItem(at: fileURL)
}
Comment on lines +311 to +315

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense for these models (DownloadModel and LocalModel) to share a protocol to avoid weird cases like this. Especially given that they share so much in common it's a bit odd for them to be siblings with redefined properties e.g. modelPath.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re absolutely right. I’d like to refactor this in the future.

}

try url.removeEmptyFolders()
}
}

struct LocalModel: Model {
Expand Down
13 changes: 13 additions & 0 deletions Sources/LocalLLMClientUtility/URL+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ package extension URL {
return url
#endif
}

func removeEmptyFolders() throws {
Comment thread
ashtonmeuser marked this conversation as resolved.
Outdated
guard isFileURL else { return }

let contents = try FileManager.default.contentsOfDirectory(at: self, includingPropertiesForKeys: [.isDirectoryKey])
let subdirectories = contents.filter { (try? $0.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true }

for subdirectory in subdirectories { try subdirectory.removeEmptyFolders() }
Comment thread
ashtonmeuser marked this conversation as resolved.
Outdated

guard try FileManager.default.contentsOfDirectory(at: self, includingPropertiesForKeys: nil).isEmpty else { return }

try FileManager.default.removeItem(at: self)
}
}