Skip to content

Commit f1d3f23

Browse files
committed
Fix GitHubCopilot chat error
1 parent ddb97d2 commit f1d3f23

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

Tool/Sources/GitHubCopilotService/LanguageServer/CopilotLocalProcessServer.swift

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class CopilotLocalProcessServer {
1414
private var wrappedServer: CustomJSONRPCLanguageServer?
1515
var terminationHandler: (() -> Void)?
1616
@MainActor var ongoingCompletionRequestIDs: [JSONId] = []
17+
@MainActor var ongoingConversationRequestIDs: [String: JSONId] = [:]
1718

1819
public convenience init(
1920
path: String,
@@ -58,6 +59,21 @@ class CopilotLocalProcessServer {
5859
Task { @MainActor [weak self] in
5960
self?.ongoingCompletionRequestIDs.append(request.id)
6061
}
62+
} else if request.method == "conversation/create" {
63+
Task { @MainActor [weak self] in
64+
if let paramsData = try? JSONEncoder().encode(request.params) {
65+
do {
66+
let params = try JSONDecoder().decode(
67+
GitHubCopilotRequest.ConversationCreate.RequestBody.self,
68+
from: paramsData
69+
)
70+
self?.ongoingConversationRequestIDs[params.workDoneToken] = request.id
71+
} catch {
72+
// Handle decoding error
73+
print("Error decoding ConversationCreateParams: \(error)")
74+
}
75+
}
76+
}
6177
}
6278
}
6379

@@ -131,19 +147,35 @@ extension CopilotLocalProcessServer: LanguageServerProtocol.Server {
131147

132148
let task = Task { @MainActor in
133149
for id in self.ongoingCompletionRequestIDs {
134-
switch id {
135-
case let .numericId(id):
136-
try? await server.sendNotification(.protocolCancelRequest(.init(id: id)))
137-
case let .stringId(id):
138-
try? await server.sendNotification(.protocolCancelRequest(.init(id: id)))
139-
}
150+
await cancelTask(id)
140151
}
141152
self.ongoingCompletionRequestIDs = []
142153
}
143154

144155
await task.value
145156
}
146157

158+
public func cancelOngoingTask(workDoneToken: String) async {
159+
let task = Task { @MainActor in
160+
guard let id = ongoingConversationRequestIDs[workDoneToken] else { return }
161+
await cancelTask(id)
162+
}
163+
await task.value
164+
}
165+
166+
public func cancelTask(_ id: JSONId) async {
167+
guard let server = wrappedServer, process.isRunning else {
168+
return
169+
}
170+
171+
switch id {
172+
case let .numericId(id):
173+
try? await server.sendNotification(.protocolCancelRequest(.init(id: id)))
174+
case let .stringId(id):
175+
try? await server.sendNotification(.protocolCancelRequest(.init(id: id)))
176+
}
177+
}
178+
147179
public func sendRequest<Response: Codable>(
148180
_ request: ClientRequest,
149181
completionHandler: @escaping (ServerResult<Response>) -> Void

Tool/Sources/GitHubCopilotService/Services/GitHubCopilotChatService.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ public final class GitHubCopilotChatService: BuiltinExtensionChatServiceType {
5050
let startTimestamp = Date()
5151

5252
continuation.onTermination = { _ in
53-
Task { service.unregisterNotificationHandler(id: id) }
53+
Task {
54+
service.unregisterNotificationHandler(id: id)
55+
await service.cancelOngoingTask(workDoneToken: workDoneToken)
56+
}
5457
}
5558

5659
service.registerNotificationHandler(id: id) { notification, data in
@@ -71,12 +74,20 @@ public final class GitHubCopilotChatService: BuiltinExtensionChatServiceType {
7174
if let reply = progress.value.reply, progress.value.kind == "report" {
7275
continuation.yield(reply)
7376
} else if progress.value.kind == "end" {
74-
if let error = progress.value.error,
77+
if let error = progress.value.error?.message,
7578
progress.value.cancellationReason == nil
7679
{
77-
continuation.finish(
78-
throwing: GitHubCopilotError.chatEndsWithError(error)
79-
)
80+
if error.contains("400") {
81+
continuation.finish(
82+
throwing: GitHubCopilotError.chatEndsWithError(
83+
"\(error). Please try enabling pretend IDE as VSCode."
84+
)
85+
)
86+
} else {
87+
continuation.finish(
88+
throwing: GitHubCopilotError.chatEndsWithError(error)
89+
)
90+
}
8091
} else {
8192
continuation.finish()
8293
}
@@ -198,6 +209,11 @@ extension GitHubCopilotChatService {
198209
var message: String
199210
}
200211

212+
struct Error: Decodable {
213+
var responseIsIncomplete: Bool?
214+
var message: String?
215+
}
216+
201217
var kind: String
202218
var title: String?
203219
var conversationId: String
@@ -209,7 +225,7 @@ extension GitHubCopilotChatService {
209225
var annotations: [String]?
210226
var hideText: Bool?
211227
var cancellationReason: String?
212-
var error: String?
228+
var error: Error?
213229
}
214230

215231
var token: String

0 commit comments

Comments
 (0)