forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncXPCService.swift
More file actions
167 lines (151 loc) · 5.4 KB
/
AsyncXPCService.swift
File metadata and controls
167 lines (151 loc) · 5.4 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
157
158
159
160
161
162
163
164
165
166
167
import CopilotModel
import Foundation
struct AsyncXPCService {
let connection: NSXPCConnection
func checkStatus() async throws -> CopilotStatus {
try await withXPCServiceConnected(connection: connection) {
service, continuation in
service.checkStatus { status, error in
if let error {
continuation.reject(error)
return
}
continuation.resume(
status.flatMap(CopilotStatus.init(rawValue:))
?? CopilotStatus.notAuthorized
)
}
}
}
func getVersion() async throws -> String {
try await withXPCServiceConnected(connection: connection) {
service, continuation in
service.getVersion { version, error in
if let error {
continuation.reject(error)
return
}
continuation.resume(version ?? "--")
}
}
}
func signInInitiate() async throws -> (verificationUri: String, userCode: String) {
try await withXPCServiceConnected(connection: connection) {
service, continuation in
service.signInInitiate { verificationUri, userCode, error in
if let error {
continuation.reject(error)
return
}
continuation.resume((verificationUri ?? "", userCode ?? ""))
}
}
}
func signInConfirm(userCode: String) async throws -> (username: String, status: CopilotStatus) {
try await withXPCServiceConnected(connection: connection) {
service, continuation in
service.signInConfirm(userCode: userCode) { username, status, error in
if let error {
continuation.reject(error)
return
}
continuation.resume((
username ?? "",
status.flatMap(CopilotStatus.init(rawValue:)) ?? .alreadySignedIn
))
}
}
}
func signOut() async throws -> CopilotStatus {
try await withXPCServiceConnected(connection: connection) {
service, continuation in
service.signOut { finishstatus, error in
if let error {
continuation.reject(error)
return
}
continuation.resume(finishstatus.flatMap(CopilotStatus.init(rawValue:)) ?? .notSignedIn)
}
}
}
func getSuggestedCode(editorContent: EditorContent) async throws -> UpdatedContent {
try await suggestionRequest(
connection,
editorContent,
{ $0.getSuggestedCode }
)
}
func getNextSuggestedCode(editorContent: EditorContent) async throws -> UpdatedContent {
try await suggestionRequest(
connection,
editorContent,
{ $0.getNextSuggestedCode }
)
}
func getPreviousSuggestedCode(editorContent: EditorContent) async throws -> UpdatedContent {
try await suggestionRequest(
connection,
editorContent,
{ $0.getPreviousSuggestedCode }
)
}
func getSuggestionAcceptedCode(editorContent: EditorContent) async throws -> UpdatedContent {
try await suggestionRequest(
connection,
editorContent,
{ $0.getSuggestionAcceptedCode }
)
}
func getSuggestionRejectedCode(editorContent: EditorContent) async throws -> UpdatedContent {
try await suggestionRequest(
connection,
editorContent,
{ $0.getSuggestionRejectedCode }
)
}
}
private struct AutoFinishContinuation<T> {
var continuation: AsyncThrowingStream<T, Error>.Continuation
func resume(_ value: T) {
continuation.yield(value)
continuation.finish()
}
func reject(_ error: Error) {
continuation.finish(throwing: error)
}
}
private func withXPCServiceConnected<T>(
connection: NSXPCConnection,
_ fn: @escaping (XPCServiceProtocol, AutoFinishContinuation<T>) -> Void
) async throws -> T {
let stream: AsyncThrowingStream<T, Error> = AsyncThrowingStream { continuation in
let service = connection.remoteObjectProxyWithErrorHandler {
continuation.finish(throwing: $0)
} as! XPCServiceProtocol
fn(service, .init(continuation: continuation))
}
return try await stream.first(where: { _ in true })!
}
private func suggestionRequest(
_ connection: NSXPCConnection,
_ editorContent: EditorContent,
_ fn: @escaping (any XPCServiceProtocol) -> (Data, @escaping (Data?, Error?) -> Void) -> Void
) async throws -> UpdatedContent {
let data = try JSONEncoder().encode(editorContent)
return try await withXPCServiceConnected(connection: connection) {
service, continuation in
fn(service)(data) { updatedData, error in
if let error {
continuation.reject(error)
return
}
do {
let updatedContent = try JSONDecoder()
.decode(UpdatedContent.self, from: updatedData ?? Data())
continuation.resume(updatedContent)
} catch {
continuation.reject(error)
}
}
}
}