-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathWorkspaceFileIndex.swift
More file actions
60 lines (51 loc) · 2.28 KB
/
WorkspaceFileIndex.swift
File metadata and controls
60 lines (51 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import Foundation
import ConversationServiceProvider
public class WorkspaceFileIndex {
public static let shared = WorkspaceFileIndex()
/// Maximum number of files allowed per workspace
public static let maxFilesPerWorkspace = 1_000_000
private var workspaceIndex: [URL: [ConversationFileReference]] = [:]
private let queue = DispatchQueue(label: "com.copilot.workspace-file-index")
/// Reset files for a specific workspace URL
public func setFiles(_ files: [ConversationFileReference], for workspaceURL: URL) {
queue.sync {
// Enforce the file limit when setting files
if files.count > Self.maxFilesPerWorkspace {
self.workspaceIndex[workspaceURL] = Array(files.prefix(Self.maxFilesPerWorkspace))
} else {
self.workspaceIndex[workspaceURL] = files
}
}
}
/// Get all files for a specific workspace URL
public func getFiles(for workspaceURL: URL) -> [ConversationFileReference]? {
return workspaceIndex[workspaceURL]
}
/// Add a file to the workspace index
/// - Returns: true if the file was added successfully, false if the workspace has reached the maximum file limit
@discardableResult
public func addFile(_ file: ConversationFileReference, to workspaceURL: URL) -> Bool {
return queue.sync {
if self.workspaceIndex[workspaceURL] == nil {
self.workspaceIndex[workspaceURL] = []
}
// Check if we've reached the maximum file limit
let currentFileCount = self.workspaceIndex[workspaceURL]!.count
if currentFileCount >= Self.maxFilesPerWorkspace {
return false
}
// Avoid duplicates by checking if file already exists
if !self.workspaceIndex[workspaceURL]!.contains(file) {
self.workspaceIndex[workspaceURL]!.append(file)
return true
}
return true // File already exists, so we consider this a successful "add"
}
}
/// Remove a file from the workspace index
public func removeFile(_ file: ConversationFileReference, from workspaceURL: URL) {
queue.sync {
self.workspaceIndex[workspaceURL]?.removeAll { $0 == file }
}
}
}