-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathProjectContextSkill.swift
More file actions
64 lines (51 loc) · 2.32 KB
/
ProjectContextSkill.swift
File metadata and controls
64 lines (51 loc) · 2.32 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
61
62
63
64
import Foundation
import Workspace
import GitHubCopilotService
import JSONRPC
import XcodeInspector
/*
* project-context is different from others
* 1. The CLS only request this skill once `after initialized` instead of during conversation / turn.
* 2. After resolved skill, a file watcher needs to be start for syncing file modification to CLS
*/
public class ProjectContextSkill {
public static let ID = "project-context"
public static let ProgressID = "collect-project-context"
public static var resolvedWorkspace: Set<String> = Set()
public static func isWorkspaceResolved(_ path: String) -> Bool {
return ProjectContextSkill.resolvedWorkspace.contains(path)
}
public init() { }
/*
* The request from CLS only contain the projectPath (a initialization paramter for CLS)
* whereas to get files for xcode workspace, the workspacePath is needed.
*/
public static func resolveSkill(
request: WatchedFilesRequest,
workspacePath: String,
completion: JSONRPCResponseHandler
) {
guard !ProjectContextSkill.isWorkspaceResolved(workspacePath) else {return }
let params = request.params!
guard params.workspaceFolder.uri != "/" else { return }
/// build workspace URL
let workspaceURL = URL(fileURLWithPath: workspacePath)
/// refer to `init` in `Workspace`
let projectURL = WorkspaceXcodeWindowInspector.extractProjectURL(
workspaceURL: workspaceURL,
documentURL: nil
) ?? workspaceURL
/// ignore invalid resolve request
guard projectURL.absoluteString == params.workspaceFolder.uri else { return }
let files = WorkspaceFile.getWatchedFiles(
workspaceURL: workspaceURL,
projectURL: projectURL,
excludeGitIgnoredFiles: params.excludeGitignoredFiles,
excludeIDEIgnoredFiles: params.excludeIDEIgnoredFiles
)
let jsonResult = try? JSONEncoder().encode(["files": files])
let jsonValue = (try? JSONDecoder().decode(JSONValue.self, from: jsonResult ?? Data())) ?? JSONValue.null
completion(AnyJSONRPCResponse(id: request.id, result: jsonValue))
ProjectContextSkill.resolvedWorkspace.insert(workspacePath)
}
}