forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotMessage.swift
More file actions
191 lines (178 loc) · 6.56 KB
/
BotMessage.swift
File metadata and controls
191 lines (178 loc) · 6.56 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import ComposableArchitecture
import Foundation
import MarkdownUI
import SharedUIComponents
import SwiftUI
struct BotMessage: View {
var r: Double { messageBubbleCornerRadius }
let id: String
let text: String
let references: [DisplayedChatMessage.Reference]
let chat: StoreOf<Chat>
@Environment(\.colorScheme) var colorScheme
@AppStorage(\.chatFontSize) var chatFontSize
@AppStorage(\.chatCodeFontSize) var chatCodeFontSize
@State var isReferencesPresented = false
@State var isReferencesHovered = false
var body: some View {
HStack(alignment: .bottom, spacing: 2) {
VStack(alignment: .leading, spacing: 16) {
if !references.isEmpty {
Button(action: {
isReferencesPresented.toggle()
}, label: {
HStack(spacing: 4) {
Image(systemName: "plus.circle")
Text("Used \(references.count) references")
}
.padding(8)
.background {
RoundedRectangle(cornerRadius: r - 4)
.foregroundStyle(Color(isReferencesHovered ? .black : .clear))
}
.overlay {
RoundedRectangle(cornerRadius: r - 4)
.stroke(Color(nsColor: .separatorColor), lineWidth: 1)
}
.foregroundStyle(.secondary)
})
.buttonStyle(.plain)
.popover(isPresented: $isReferencesPresented, arrowEdge: .trailing) {
ReferenceList(references: references)
}
}
Markdown(text)
.textSelection(.enabled)
.markdownTheme(.custom(fontSize: chatFontSize))
.markdownCodeSyntaxHighlighter(
ChatCodeSyntaxHighlighter(
brightMode: colorScheme != .dark,
fontSize: chatCodeFontSize
)
)
}
.frame(alignment: .trailing)
.padding()
.background {
RoundedCorners(tl: r, tr: r, bl: 0, br: r)
.fill(Color.contentBackground)
}
.overlay {
RoundedCorners(tl: r, tr: r, bl: 0, br: r)
.stroke(Color(nsColor: .separatorColor), lineWidth: 1)
}
.padding(.leading, 8)
.shadow(color: .black.opacity(0.1), radius: 2)
.contextMenu {
Button("Copy") {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(text, forType: .string)
}
Button("Set as Extra System Prompt") {
chat.send(.setAsExtraPromptButtonTapped(id))
}
Divider()
Button("Delete") {
chat.send(.deleteMessageButtonTapped(id))
}
}
CopyButton {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(text, forType: .string)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.trailing, 2)
}
}
struct ReferenceList: View {
let references: [DisplayedChatMessage.Reference]
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 8) {
ForEach(0..<references.endIndex, id: \.self) { index in
let reference = references[index]
Button(action: {
print("")
}) {
HStack(spacing: 8) {
Text(reference.title)
Text(reference.subtitle)
.foregroundStyle(.secondary)
}
.padding(.vertical, 6)
.padding(.horizontal, 8)
.frame(maxWidth: .infinity, alignment: .leading)
.overlay {
RoundedRectangle(cornerRadius: 4)
.stroke(Color(nsColor: .separatorColor), lineWidth: 1)
}
}
.buttonStyle(.plain)
}
}
.padding()
.frame(maxHeight: 500)
}
}
}
#Preview("Bot Message") {
BotMessage(
id: "1",
text: """
**Hey**! What can I do for you?**Hey**! What can I do for you?**Hey**! What can I do for you?**Hey**! What can I do for you?
```swift
func foo() {}
```
""",
references: [
.init(
title: "ReferenceList",
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
uri: "https://google.com"
),
.init(
title: "BotMessage.swift:100-102",
subtitle: "/Core/Sources/ChatGPTChatTab/Views",
uri: "https://google.com"
),
],
chat: .init(initialState: .init(), reducer: Chat(service: .init()))
)
.padding()
.fixedSize(horizontal: true, vertical: true)
}
#Preview("Reference List") {
ReferenceList(references: [
.init(
title: "ReferenceList",
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
uri: "https://google.com"
),
.init(
title: "BotMessage.swift:100-102",
subtitle: "/Core/Sources/ChatGPTChatTab/Views",
uri: "https://google.com"
),
.init(
title: "ReferenceList",
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
uri: "https://google.com"
),
.init(
title: "ReferenceList",
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
uri: "https://google.com"
),
.init(
title: "ReferenceList",
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
uri: "https://google.com"
),
.init(
title: "ReferenceList",
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
uri: "https://google.com"
),
])
}