-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathChatModePicker.swift
More file actions
96 lines (88 loc) · 3.25 KB
/
ChatModePicker.swift
File metadata and controls
96 lines (88 loc) · 3.25 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import SwiftUI
import Persist
import ConversationServiceProvider
import GitHubCopilotService
import Combine
import SharedUIComponents
public extension Notification.Name {
static let gitHubCopilotChatModeDidChange = Notification
.Name("com.github.CopilotForXcode.ChatModeDidChange")
}
public enum ChatMode: String {
case Ask = "Ask"
case Agent = "Agent"
}
public struct ChatModePicker: View {
@Binding var chatMode: String
@Environment(\.colorScheme) var colorScheme
@State var isAgentModeFFEnabled: Bool
@State private var cancellables = Set<AnyCancellable>()
var onScopeChange: (PromptTemplateScope) -> Void
public init(chatMode: Binding<String>, onScopeChange: @escaping (PromptTemplateScope) -> Void = { _ in }) {
self._chatMode = chatMode
self.onScopeChange = onScopeChange
self.isAgentModeFFEnabled = FeatureFlagNotifierImpl.shared.featureFlags.agentMode
}
private func setChatMode(mode: ChatMode) {
chatMode = mode.rawValue
AppState.shared.setSelectedChatMode(mode.rawValue)
onScopeChange(mode == .Ask ? .chatPanel : .agentPanel)
NotificationCenter.default.post(
name: .gitHubCopilotChatModeDidChange,
object: nil
)
}
private func subscribeToFeatureFlagsDidChangeEvent() {
FeatureFlagNotifierImpl.shared.featureFlagsDidChange.sink(receiveValue: { featureFlags in
isAgentModeFFEnabled = featureFlags.agentMode
})
.store(in: &cancellables)
}
public var body: some View {
VStack {
if isAgentModeFFEnabled {
HStack(spacing: -1) {
ModeButton(
title: "Ask",
isSelected: chatMode == "Ask",
activeBackground: colorScheme == .dark ? Color.white.opacity(0.25) : Color.white,
activeTextColor: Color.primary,
inactiveTextColor: Color.primary.opacity(0.5),
action: {
setChatMode(mode: .Ask)
}
)
ModeButton(
title: "Agent",
isSelected: chatMode == "Agent",
activeBackground: Color.blue,
activeTextColor: Color.white,
inactiveTextColor: Color.primary.opacity(0.5),
action: {
setChatMode(mode: .Agent)
}
)
}
.scaledPadding(1)
.scaledFrame(height: 20, alignment: .topLeading)
.background(.primary.opacity(0.1))
.cornerRadius(5)
.padding(4)
.help("Set Mode")
} else {
EmptyView()
}
}
.task {
subscribeToFeatureFlagsDidChangeEvent()
if !isAgentModeFFEnabled {
setChatMode(mode: .Ask)
}
}
.onChange(of: isAgentModeFFEnabled) { newAgentModeFFEnabled in
if !newAgentModeFFEnabled {
setChatMode(mode: .Ask)
}
}
}
}