Skip to content

Commit db4d4ea

Browse files
committed
Support deleting and resending messages
1 parent a927318 commit db4d4ea

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

Core/Sources/ChatService/ChatService.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ public final class ChatService: ObservableObject {
8989
}
9090
await chatGPTService.clearHistory()
9191
}
92+
93+
public func deleteMessage(id: String) async {
94+
await chatGPTService.mutateHistory { messages in
95+
messages.removeAll(where: { $0.id == id })
96+
}
97+
}
98+
99+
public func resendMessage(id: String) async throws {
100+
if let message = (await chatGPTService.history).first(where: { $0.id == id }) {
101+
try await send(content: message.content)
102+
}
103+
}
92104

93105
public func mutateSystemPrompt(_ newPrompt: String) async {
94106
await chatGPTService.mutateSystemPrompt(newPrompt)

Core/Sources/Service/GUI/ChatProvider+Service.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,25 @@ extension ChatProvider {
5555
onCloseChat()
5656
}
5757
}
58-
58+
5959
self.onSwitchContext = {
6060
onSwitchContext()
6161
}
62+
63+
onDeleteMessage = { id in
64+
Task {
65+
await service.deleteMessage(id: id)
66+
}
67+
}
68+
69+
onResendMessage = { id in
70+
Task {
71+
do {
72+
try await service.resendMessage(id: id)
73+
} catch {
74+
PresentInWindowSuggestionPresenter().presentError(error)
75+
}
76+
}
77+
}
6278
}
6379
}

Core/Sources/SuggestionWidget/ChatProvider.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public final class ChatProvider: ObservableObject {
1010
public var onClear: () -> Void
1111
public var onClose: () -> Void
1212
public var onSwitchContext: () -> Void
13+
public var onDeleteMessage: (String) -> Void
14+
public var onResendMessage: (String) -> Void
1315

1416
public init(
1517
history: [ChatMessage] = [],
@@ -18,7 +20,9 @@ public final class ChatProvider: ObservableObject {
1820
onStop: @escaping () -> Void = {},
1921
onClear: @escaping () -> Void = {},
2022
onClose: @escaping () -> Void = {},
21-
onSwitchContext: @escaping () -> Void = {}
23+
onSwitchContext: @escaping () -> Void = {},
24+
onDeleteMessage: @escaping (String) -> Void = { _ in },
25+
onResendMessage: @escaping (String) -> Void = { _ in }
2226
) {
2327
self.history = history
2428
self.isReceivingMessage = isReceivingMessage
@@ -27,13 +31,17 @@ public final class ChatProvider: ObservableObject {
2731
self.onClear = onClear
2832
self.onClose = onClose
2933
self.onSwitchContext = onSwitchContext
34+
self.onDeleteMessage = onDeleteMessage
35+
self.onResendMessage = onResendMessage
3036
}
3137

3238
public func send(_ message: String) { onMessageSend(message) }
3339
public func stop() { onStop() }
3440
public func clear() { onClear() }
3541
public func close() { onClose() }
3642
public func switchContext() { onSwitchContext() }
43+
public func deleteMessage(id: String) { onDeleteMessage(id) }
44+
public func resendMessage(id: String) { onResendMessage(id) }
3745
}
3846

3947
public struct ChatMessage: Equatable {

Core/Sources/SuggestionWidget/SuggestionPanelContent/ChatPanel.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ struct ChatPanelMessages: View {
110110
.text
111111

112112
if message.isUser {
113-
UserMessage(text: text)
113+
UserMessage(id: message.id, text: text, chat: chat)
114114
.listRowInsets(EdgeInsets(top: 0, leading: -8, bottom: 0, trailing: -8))
115115
.padding(.vertical, 4)
116116
} else {
117-
BotMessage(text: text)
117+
BotMessage(id: message.id, text: text, chat: chat)
118118
.listRowInsets(EdgeInsets(top: 0, leading: -8, bottom: 0, trailing: -8))
119119
.padding(.vertical, 4)
120120
}
@@ -165,7 +165,9 @@ private struct StopRespondingButton: View {
165165
}
166166

167167
private struct UserMessage: View {
168+
let id: String
168169
let text: String
170+
let chat: ChatProvider
169171
@Environment(\.colorScheme) var colorScheme
170172

171173
var body: some View {
@@ -195,13 +197,24 @@ private struct UserMessage: View {
195197
NSPasteboard.general.clearContents()
196198
NSPasteboard.general.setString(text, forType: .string)
197199
}
198-
.buttonStyle(.borderless)
200+
201+
Button("Send Again") {
202+
chat.resendMessage(id: id)
203+
}
204+
205+
Divider()
206+
207+
Button("Delete") {
208+
chat.deleteMessage(id: id)
209+
}
199210
}
200211
}
201212
}
202213

203214
private struct BotMessage: View {
215+
let id: String
204216
let text: String
217+
let chat: ChatProvider
205218
@Environment(\.colorScheme) var colorScheme
206219

207220
var body: some View {
@@ -236,7 +249,12 @@ private struct BotMessage: View {
236249
NSPasteboard.general.clearContents()
237250
NSPasteboard.general.setString(text, forType: .string)
238251
}
239-
.buttonStyle(.borderless)
252+
253+
Divider()
254+
255+
Button("Delete") {
256+
chat.deleteMessage(id: id)
257+
}
240258
}
241259
}
242260
.frame(maxWidth: .infinity, alignment: .trailing)

0 commit comments

Comments
 (0)