forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversationProgressHandler.swift
More file actions
57 lines (49 loc) · 2.1 KB
/
ConversationProgressHandler.swift
File metadata and controls
57 lines (49 loc) · 2.1 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
import Combine
import Foundation
import JSONRPC
import LanguageServerProtocol
import Logger
public enum ProgressKind: String {
case begin, report, end
}
public protocol ConversationProgressHandler {
var onBegin: PassthroughSubject<(String, ConversationProgressBegin), Never> { get }
var onProgress: PassthroughSubject<(String, ConversationProgressReport), Never> { get }
var onEnd: PassthroughSubject<(String, ConversationProgressEnd), Never> { get }
func handleConversationProgress(_ progressParams: ProgressParams)
}
public final class ConversationProgressHandlerImpl: ConversationProgressHandler {
public static let shared = ConversationProgressHandlerImpl()
public var onBegin = PassthroughSubject<(String, ConversationProgressBegin), Never>()
public var onProgress = PassthroughSubject<(String, ConversationProgressReport), Never>()
public var onEnd = PassthroughSubject<(String, ConversationProgressEnd), Never>()
private var cancellables = Set<AnyCancellable>()
public func handleConversationProgress(_ progressParams: ProgressParams) {
guard let token = getValueAsString(from: progressParams.token),
let data = try? JSONEncoder().encode(progressParams.value) else {
print("Error encountered while parsing conversation progress params")
Logger.gitHubCopilot.error("Error encountered while parsing conversation progress params")
return
}
let progress = try? JSONDecoder().decode(ConversationProgressContainer.self, from: data)
switch progress {
case .begin(let begin):
onBegin.send((token, begin))
case .report(let report):
onProgress.send((token, report))
case .end(let end):
onEnd.send((token, end))
default:
print("Invalid progress kind")
return
}
}
private func getValueAsString(from token: ProgressToken) -> String? {
switch token {
case .optionA(let intValue):
return String(intValue)
case .optionB(let stringValue):
return stringValue
}
}
}