forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubCopilotExtension.swift
More file actions
332 lines (290 loc) · 12.2 KB
/
GitHubCopilotExtension.swift
File metadata and controls
332 lines (290 loc) · 12.2 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import BuiltinExtension
import CopilotForXcodeKit
import Dependencies
import Foundation
import LanguageServerProtocol
import Logger
import Preferences
import Toast
import Workspace
public final class GitHubCopilotExtension: BuiltinExtension {
public var extensionIdentifier: String { "com.github.copilot" }
public let suggestionService: GitHubCopilotSuggestionService
public let chatService: GitHubCopilotChatService
private var extensionUsage = ExtensionUsage(
isSuggestionServiceInUse: false,
isChatServiceInUse: false
)
private var isLanguageServerInUse: Bool {
extensionUsage.isSuggestionServiceInUse || extensionUsage.isChatServiceInUse
}
@Dependency(\.toastController) var toast
let workspacePool: WorkspacePool
let serviceLocator: ServiceLocatorType
public init(workspacePool: WorkspacePool) {
self.workspacePool = workspacePool
serviceLocator = ServiceLocator(workspacePool: workspacePool)
suggestionService = .init(serviceLocator: serviceLocator)
chatService = .init(serviceLocator: serviceLocator)
}
public func workspaceDidOpen(_: WorkspaceInfo) {}
public func workspaceDidClose(_: WorkspaceInfo) {}
public func workspace(_ workspace: WorkspaceInfo, didOpenDocumentAt documentURL: URL) {
guard isLanguageServerInUse else { return }
// check if file size is larger than 15MB, if so, return immediately
if let attrs = try? FileManager.default
.attributesOfItem(atPath: documentURL.path),
let fileSize = attrs[FileAttributeKey.size] as? UInt64,
fileSize > 15 * 1024 * 1024
{ return }
Task {
do {
let content = try String(contentsOf: documentURL, encoding: .utf8)
guard let service = await serviceLocator.getService(from: workspace) else { return }
try await service.notifyOpenTextDocument(fileURL: documentURL, content: content)
} catch let error as ServerError {
let e = GitHubCopilotError.languageServerError(error)
Logger.gitHubCopilot.error(e.localizedDescription)
switch error {
case .serverUnavailable, .serverError:
toast.toast(content: e.localizedDescription, type: .error, duration: 10.0)
default:
break
}
} catch {
Logger.gitHubCopilot.error(error.localizedDescription)
}
}
}
public func workspace(_ workspace: WorkspaceInfo, didSaveDocumentAt documentURL: URL) {
guard isLanguageServerInUse else { return }
Task {
do {
guard let service = await serviceLocator.getService(from: workspace) else { return }
try await service.notifySaveTextDocument(fileURL: documentURL)
} catch {
Logger.gitHubCopilot.error(error.localizedDescription)
}
}
}
public func workspace(_ workspace: WorkspaceInfo, didCloseDocumentAt documentURL: URL) {
guard isLanguageServerInUse else { return }
Task {
do {
guard let service = await serviceLocator.getService(from: workspace) else { return }
try await service.notifyCloseTextDocument(fileURL: documentURL)
} catch {
Logger.gitHubCopilot.error(error.localizedDescription)
}
}
}
public func workspace(
_ workspace: WorkspaceInfo,
didUpdateDocumentAt documentURL: URL,
content: String?
) {
guard isLanguageServerInUse else { return }
// check if file size is larger than 15MB, if so, return immediately
if let attrs = try? FileManager.default
.attributesOfItem(atPath: documentURL.path),
let fileSize = attrs[FileAttributeKey.size] as? UInt64,
fileSize > 15 * 1024 * 1024
{ return }
Task {
guard let content else { return }
guard let service = await serviceLocator.getService(from: workspace) else { return }
do {
try await service.notifyChangeTextDocument(
fileURL: documentURL,
content: content,
version: 0
)
} catch let error as ServerError {
switch error {
case .serverError(-32602, _, _): // parameter incorrect
Logger.gitHubCopilot.error(error.localizedDescription)
// Reopen document if it's not found in the language server
self.workspace(workspace, didOpenDocumentAt: documentURL)
default:
Logger.gitHubCopilot.error(error.localizedDescription)
}
} catch {
Logger.gitHubCopilot.error(error.localizedDescription)
}
}
}
public func extensionUsageDidChange(_ usage: ExtensionUsage) {
extensionUsage = usage
if !usage.isChatServiceInUse && !usage.isSuggestionServiceInUse {
terminate()
}
}
public func terminate() {
for workspace in workspacePool.workspaces.values {
guard let plugin = workspace.plugin(for: GitHubCopilotWorkspacePlugin.self)
else { continue }
plugin.terminate()
}
}
}
protocol ServiceLocatorType {
func getService(from workspace: WorkspaceInfo) async -> GitHubCopilotService?
}
class ServiceLocator: ServiceLocatorType {
let workspacePool: WorkspacePool
init(workspacePool: WorkspacePool) {
self.workspacePool = workspacePool
}
func getService(from workspace: WorkspaceInfo) async -> GitHubCopilotService? {
guard let workspace = workspacePool.workspaces[workspace.workspaceURL],
let plugin = workspace.plugin(for: GitHubCopilotWorkspacePlugin.self)
else { return nil }
return await plugin.gitHubCopilotService
}
}
extension GitHubCopilotExtension {
public struct Token: Codable {
// let codesearch: Bool
public let individual: Bool
public let endpoints: Endpoints
public let chat_enabled: Bool
// public let sku: String
// public let copilotignore_enabled: Bool
// public let limited_user_quotas: String?
// public let tracking_id: String
// public let xcode: Bool
// public let limited_user_reset_date: String?
// public let telemetry: String
// public let prompt_8k: Bool
public let token: String
// public let nes_enabled: Bool
// public let vsc_electron_fetcher_v2: Bool
// public let code_review_enabled: Bool
// public let annotations_enabled: Bool
// public let chat_jetbrains_enabled: Bool
// public let xcode_chat: Bool
// public let refresh_in: Int
// public let snippy_load_test_enabled: Bool
// public let trigger_completion_after_accept: Bool
public let expires_at: Int
// public let public_suggestions: String
// public let code_quote_enabled: Bool
public struct Endpoints: Codable {
public let api: String
public let proxy: String
public let telemetry: String
// public let origin-tracker: String
}
}
struct AuthInfo: Codable {
public let user: String
public let oauth_token: String
public let githubAppId: String
}
static var authInfo: AuthInfo? {
guard let urls = try? GitHubCopilotBaseService.createFoldersIfNeeded()
else { return nil }
let path = urls.supportURL
.appendingPathComponent("undefined")
.appendingPathComponent(".config")
.appendingPathComponent("github-copilot")
.appendingPathComponent("apps.json").path
guard FileManager.default.fileExists(atPath: path) else { return nil }
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path))
let json = try JSONSerialization
.jsonObject(with: data, options: []) as? [String: [String: String]]
guard let firstEntry = json?.values.first else { return nil }
let jsonData = try JSONSerialization.data(withJSONObject: firstEntry, options: [])
return try JSONDecoder().decode(AuthInfo.self, from: jsonData)
} catch {
Logger.gitHubCopilot.error(error.localizedDescription)
return nil
}
}
@MainActor
static var cachedToken: Token?
public static func fetchToken() async throws -> Token {
guard let authToken = authInfo?.oauth_token
else { throw GitHubCopilotError.notLoggedIn }
let oldToken = await MainActor.run { cachedToken }
if let oldToken {
let expiresAt = Date(timeIntervalSince1970: TimeInterval(oldToken.expires_at))
if expiresAt > Date() {
return oldToken
}
}
let url = URL(string: "https://api.github.com/copilot_internal/v2/token")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("token \(authToken)", forHTTPHeaderField: "authorization")
request.setValue("unknown-editor/0", forHTTPHeaderField: "editor-version")
request.setValue("unknown-editor-plugin/0", forHTTPHeaderField: "editor-plugin-version")
request.setValue("1.236.0", forHTTPHeaderField: "copilot-language-server-version")
request.setValue("GithubCopilot/1.236.0", forHTTPHeaderField: "user-agent")
request.setValue("*/*", forHTTPHeaderField: "accept")
request.setValue("gzip,deflate,br", forHTTPHeaderField: "accept-encoding")
do {
let (data, _) = try await URLSession.shared.data(for: request)
if let jsonString = String(data: data, encoding: .utf8) {
print(jsonString)
}
let newToken = try JSONDecoder().decode(Token.self, from: data)
await MainActor.run { cachedToken = newToken }
return newToken
} catch {
Logger.service.error(error.localizedDescription)
throw error
}
}
public static func fetchLLMModels() async throws -> [GitHubCopilotLLMModel] {
let token = try await GitHubCopilotExtension.fetchToken()
guard let endpoint = URL(string: token.endpoints.api + "/models") else {
throw CancellationError()
}
var request = URLRequest(url: endpoint)
request.setValue(
"Copilot for Xcode/\(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "unknown")",
forHTTPHeaderField: "Editor-Version"
)
request.setValue("Bearer \(token.token)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("vscode-chat", forHTTPHeaderField: "Copilot-Integration-Id")
request.setValue("2023-07-07", forHTTPHeaderField: "X-Github-Api-Version")
let (data, response) = try await URLSession.shared.data(for: request)
guard let response = response as? HTTPURLResponse else {
throw CancellationError()
}
guard response.statusCode == 200 else {
throw CancellationError()
}
struct Model: Decodable {
struct Limit: Decodable {
var max_context_window_tokens: Int
}
struct Capability: Decodable {
var type: String?
var family: String?
var limit: Limit?
}
var id: String
var capabilities: Capability
}
struct Body: Decodable {
var data: [Model]
}
let models = try JSONDecoder().decode(Body.self, from: data)
.data
.filter {
$0.capabilities.type == "chat"
}
.map {
GitHubCopilotLLMModel(
modelId: $0.id,
familyName: $0.capabilities.family ?? "",
contextWindow: $0.capabilities.limit?.max_context_window_tokens ?? 0
)
}
return models
}
}