forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPCServiceProtocol.swift
More file actions
157 lines (138 loc) · 4.86 KB
/
XPCServiceProtocol.swift
File metadata and controls
157 lines (138 loc) · 4.86 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import Foundation
import Status
import SuggestionBasic
@objc(XPCServiceProtocol)
public protocol XPCServiceProtocol {
func getSuggestedCode(
editorContent: Data,
withReply reply: @escaping (_ updatedContent: Data?, Error?) -> Void
)
func getNextSuggestedCode(
editorContent: Data,
withReply reply: @escaping (_ updatedContent: Data?, Error?) -> Void
)
func getPreviousSuggestedCode(
editorContent: Data,
withReply reply: @escaping (_ updatedContent: Data?, Error?) -> Void
)
func getSuggestionAcceptedCode(
editorContent: Data,
withReply reply: @escaping (_ updatedContent: Data?, Error?) -> Void
)
func getSuggestionRejectedCode(
editorContent: Data,
withReply reply: @escaping (_ updatedContent: Data?, Error?) -> Void
)
func getRealtimeSuggestedCode(
editorContent: Data,
withReply reply: @escaping (Data?, Error?) -> Void
)
func getPromptToCodeAcceptedCode(
editorContent: Data,
withReply reply: @escaping (_ updatedContent: Data?, Error?) -> Void
)
func openChat(
editorContent: Data,
withReply reply: @escaping (Data?, Error?) -> Void
)
func promptToCode(
editorContent: Data,
withReply reply: @escaping (Data?, Error?) -> Void
)
func customCommand(
id: String,
editorContent: Data,
withReply reply: @escaping (Data?, Error?) -> Void
)
func toggleRealtimeSuggestion(withReply reply: @escaping (Error?) -> Void)
func prefetchRealtimeSuggestions(
editorContent: Data,
withReply reply: @escaping () -> Void
)
func getXPCServiceVersion(withReply reply: @escaping (String, String) -> Void)
func getXPCServiceAccessibilityPermission(withReply reply: @escaping (ObservedAXStatus) -> Void)
func postNotification(name: String, withReply reply: @escaping () -> Void)
func send(endpoint: String, requestBody: Data, reply: @escaping (Data?, Error?) -> Void)
func quit(reply: @escaping () -> Void)
}
public struct NoResponse: Codable {
public static let none = NoResponse()
}
public protocol ExtensionServiceRequestType: Codable {
associatedtype ResponseBody: Codable
static var endpoint: String { get }
}
public enum ExtensionServiceRequests {
public struct OpenExtensionManager: ExtensionServiceRequestType {
public typealias ResponseBody = NoResponse
public static let endpoint = "OpenExtensionManager"
public init() {}
}
public struct GetExtensionSuggestionServices: ExtensionServiceRequestType {
public struct ServiceInfo: Codable {
public var bundleIdentifier: String
public var name: String
public init(bundleIdentifier: String, name: String) {
self.bundleIdentifier = bundleIdentifier
self.name = name
}
}
public typealias ResponseBody = [ServiceInfo]
public static let endpoint = "GetExtensionSuggestionServices"
public init() {}
}
}
public struct XPCRequestHandlerHitError: Error, LocalizedError {
public var errorDescription: String? {
"This is not an actual error, it just indicates a request handler was hit, and no more check is needed."
}
public init() {}
}
public struct XPCRequestNotHandledError: Error, LocalizedError {
public var errorDescription: String? {
"The request was not handled by the XPC server."
}
public init() {}
}
extension ExtensionServiceRequestType {
/// A helper method to handle requests.
static func _handle<Request: Codable, Response: Codable>(
endpoint: String,
requestBody data: Data,
reply: @escaping (Data?, Error?) -> Void,
handler: @escaping (Request) async throws -> Response
) throws {
guard endpoint == Self.endpoint else {
return
}
do {
let requestBody = try JSONDecoder().decode(Request.self, from: data)
Task {
do {
let responseBody = try await handler(requestBody)
let responseBodyData = try JSONEncoder().encode(responseBody)
reply(responseBodyData, nil)
} catch {
reply(nil, error)
}
}
} catch {
reply(nil, error)
}
throw XPCRequestHandlerHitError()
}
public static func handle(
endpoint: String,
requestBody data: Data,
reply: @escaping (Data?, Error?) -> Void,
handler: @escaping (Self) async throws -> Self.ResponseBody
) throws {
try _handle(
endpoint: endpoint,
requestBody: data,
reply: reply
) { (request: Self) async throws -> Self.ResponseBody in
try await handler(request)
}
}
}