Skip to content

Commit 7e6e07b

Browse files
committed
Implement cancellation in SuggestionService
1 parent 18c6109 commit 7e6e07b

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

Core/Sources/CodeiumService/CodeiumService.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public protocol CodeiumSuggestionServiceType {
1818
func notifyOpenTextDocument(fileURL: URL, content: String) async throws
1919
func notifyChangeTextDocument(fileURL: URL, content: String) async throws
2020
func notifyCloseTextDocument(fileURL: URL) async throws
21+
func cancelRequest() async
2122
}
2223

2324
enum CodeiumError: Error, LocalizedError {
@@ -43,6 +44,7 @@ public class CodeiumSuggestionService {
4344
var server: CodeiumLSP?
4445
var heartbeatTask: Task<Void, Error>?
4546
var requestCounter: UInt64 = 0
47+
var cancellationCounter: UInt64 = 0
4648
let openedDocumentPool = OpenedDocumentPool()
4749
let onServiceLaunched: () -> Void
4850

@@ -118,6 +120,7 @@ public class CodeiumSuggestionService {
118120
self?.server = nil
119121
self?.heartbeatTask?.cancel()
120122
self?.requestCounter = 0
123+
self?.cancellationCounter = 0
121124
Logger.codeium.info("Language server is terminated, will be restarted when needed.")
122125
}
123126

@@ -253,8 +256,16 @@ extension CodeiumSuggestionService: CodeiumSuggestionServiceType {
253256
)
254257
}
255258
))
259+
260+
if request.requestBody.metadata.request_id <= cancellationCounter {
261+
throw CancellationError()
262+
}
256263

257264
let result = try await (try await setupServerIfNeeded()).sendRequest(request)
265+
266+
if request.requestBody.metadata.request_id <= cancellationCounter {
267+
throw CancellationError()
268+
}
258269

259270
return result.completionItems?.filter { item in
260271
if ignoreSpaceOnlySuggestions {
@@ -280,6 +291,10 @@ extension CodeiumSuggestionService: CodeiumSuggestionServiceType {
280291
)
281292
} ?? []
282293
}
294+
295+
public func cancelRequest() async {
296+
cancellationCounter = requestCounter
297+
}
283298

284299
public func notifyAccepted(_ suggestion: CodeSuggestion) async {
285300
_ = try? await (try setupServerIfNeeded())

Core/Sources/GitHubCopilotService/GitHubCopilotService.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public protocol GitHubCopilotSuggestionServiceType {
3131
func notifyChangeTextDocument(fileURL: URL, content: String) async throws
3232
func notifyCloseTextDocument(fileURL: URL) async throws
3333
func notifySaveTextDocument(fileURL: URL) async throws
34+
func cancelRequest() async
3435
}
3536

3637
protocol GitHubCopilotLSP {
@@ -311,6 +312,10 @@ public final class GitHubCopilotSuggestionService: GitHubCopilotBaseService,
311312

312313
return try await task.value
313314
}
315+
316+
public func cancelRequest() async {
317+
await localProcessServer?.cancelOngoingTasks()
318+
}
314319

315320
public func notifyAccepted(_ completion: CodeSuggestion) async {
316321
_ = try? await server.sendRequest(

Core/Sources/SuggestionService/CodeiumSuggestionProvider.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,10 @@ extension CodeiumSuggestionProvider {
7070
}
7171

7272
func notifySaveTextDocument(fileURL: URL) async throws {}
73+
74+
func cancelRequest() async {
75+
await (try? createCodeiumServiceIfNeeded())?
76+
.cancelRequest()
77+
}
7378
}
7479

Core/Sources/SuggestionService/GitHubCopilotSuggestionProvider.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,10 @@ extension GitHubCopilotSuggestionProvider {
7373
try await (try? createGitHubCopilotServiceIfNeeded())?
7474
.notifySaveTextDocument(fileURL: fileURL)
7575
}
76+
77+
func cancelRequest() async {
78+
await (try? createGitHubCopilotServiceIfNeeded())?
79+
.cancelRequest()
80+
}
7681
}
7782

Core/Sources/SuggestionService/SuggestionService.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public protocol SuggestionServiceType {
2020
func notifyChangeTextDocument(fileURL: URL, content: String) async throws
2121
func notifyCloseTextDocument(fileURL: URL) async throws
2222
func notifySaveTextDocument(fileURL: URL) async throws
23+
func cancelRequest() async
2324
}
2425

2526
protocol SuggestionServiceProvider: SuggestionServiceType {}
@@ -116,5 +117,9 @@ public extension SuggestionService {
116117
func notifySaveTextDocument(fileURL: URL) async throws {
117118
try await suggestionProvider.notifySaveTextDocument(fileURL: fileURL)
118119
}
120+
121+
func cancelRequest() async {
122+
await suggestionProvider.cancelRequest()
123+
}
119124
}
120125

0 commit comments

Comments
 (0)