-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathConversationProgressHandler.swift
More file actions
54 lines (46 loc) · 1.96 KB
/
ConversationProgressHandler.swift
File metadata and controls
54 lines (46 loc) · 1.96 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
import Combine
import Foundation
import JSONRPC
import LanguageServerProtocol
public enum ProgressKind: String {
case begin, report, end
}
public protocol ConversationProgressHandler {
var onBegin: PassthroughSubject<(String, ConversationProgress), Never> { get }
var onProgress: PassthroughSubject<(String, ConversationProgress), Never> { get }
var onEnd: PassthroughSubject<(String, ConversationProgress), Never> { get }
func handleConversationProgress(_ progressParams: ProgressParams)
}
public final class ConversationProgressHandlerImpl: ConversationProgressHandler {
public static let shared = ConversationProgressHandlerImpl()
public var onBegin = PassthroughSubject<(String, ConversationProgress), Never>()
public var onProgress = PassthroughSubject<(String, ConversationProgress), Never>()
public var onEnd = PassthroughSubject<(String, ConversationProgress), 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),
let progress = try? JSONDecoder().decode(ConversationProgress.self, from: data) else {
print("Error encountered while parsing conversation progress params")
return
}
if let kind = ProgressKind(rawValue: progress.kind) {
switch kind {
case .begin:
onBegin.send((token, progress))
case .report:
onProgress.send((token, progress))
case .end:
onEnd.send((token, progress))
}
}
}
private func getValueAsString(from token: ProgressToken) -> String? {
switch token {
case .optionA(let intValue):
return String(intValue)
case .optionB(let stringValue):
return stringValue
}
}
}