forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatProvider.swift
More file actions
49 lines (44 loc) · 1.49 KB
/
ChatProvider.swift
File metadata and controls
49 lines (44 loc) · 1.49 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
import Foundation
import SwiftUI
public final class ChatProvider: ObservableObject {
let id = UUID()
@Published public var history: [ChatMessage] = []
@Published public var isReceivingMessage = false
public var onMessageSend: (String) -> Void
public var onStop: () -> Void
public var onClear: () -> Void
public var onClose: () -> Void
public var onSwitchContext: () -> Void
public init(
history: [ChatMessage] = [],
isReceivingMessage: Bool = false,
onMessageSend: @escaping (String) -> Void = { _ in },
onStop: @escaping () -> Void = {},
onClear: @escaping () -> Void = {},
onClose: @escaping () -> Void = {},
onSwitchContext: @escaping () -> Void = {}
) {
self.history = history
self.isReceivingMessage = isReceivingMessage
self.onMessageSend = onMessageSend
self.onStop = onStop
self.onClear = onClear
self.onClose = onClose
self.onSwitchContext = onSwitchContext
}
public func send(_ message: String) { onMessageSend(message) }
public func stop() { onStop() }
public func clear() { onClear() }
public func close() { onClose() }
public func switchContext() { onSwitchContext() }
}
public struct ChatMessage: Equatable {
public var id: String
public var isUser: Bool
public var text: String
public init(id: String, isUser: Bool, text: String) {
self.id = id
self.isUser = isUser
self.text = text
}
}