Skip to content

Commit 339878c

Browse files
committed
Support clear history and multiline input in chat panel
1 parent 031c5b1 commit 339878c

4 files changed

Lines changed: 226 additions & 129 deletions

File tree

Core/Sources/OpenAIService/ChatGPTService.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import Logger
55
public protocol ChatGPTServiceType {
66
func send(content: String, summary: String?) async throws -> AsyncThrowingStream<String, Error>
77
func stopReceivingMessage() async
8-
func restart() async
8+
func clearHistory() async
99
func mutateSystemPrompt(_ newPrompt: String) async
10+
func mutateHistory(_ mutate: (inout [ChatMessage]) -> Void) async
1011
}
1112

1213
public enum ChatGPTServiceError: Error, LocalizedError {
@@ -160,13 +161,17 @@ public actor ChatGPTService: ChatGPTServiceType, ObservableObject {
160161
isReceivingMessage = false
161162
}
162163

163-
public func restart() {
164+
public func clearHistory() {
164165
history = []
165166
}
166167

167168
public func mutateSystemPrompt(_ newPrompt: String) {
168169
systemPrompt = newPrompt
169170
}
171+
172+
public func mutateHistory(_ mutate: (inout [ChatMessage]) -> Void) async {
173+
mutate(&history)
174+
}
170175
}
171176

172177
extension ChatGPTService {

Core/Sources/Service/SuggestionPresenter/PresentInWindowSuggestionPresenter.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ struct PresentInWindowSuggestionPresenter {
7979
}
8080
}
8181

82+
chatRoom.onClear = {
83+
Task {
84+
await service.clearHistory()
85+
}
86+
}
87+
8288
Task { @MainActor in
8389
let controller = GraphicalUserInterfaceController.shared.suggestionWidget
8490
controller.presentChatRoom(chatRoom, fileURL: fileURL)

Core/Sources/SuggestionWidget/ChatRoom.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,29 @@ public final class ChatRoom: ObservableObject, Equatable {
55
@Published public var isReceivingMessage = false
66
public var onMessageSend: (String) -> Void
77
public var onStop: () -> Void
8-
public func send(_ message: String) { onMessageSend(message) }
9-
public func stop() { onStop() }
8+
public var onClear: () -> Void
109

1110
public init(
1211
history: [ChatMessage] = [],
1312
isReceivingMessage: Bool = false,
1413
onMessageSend: @escaping (String) -> Void = { _ in },
15-
onStop: @escaping () -> Void = {}
14+
onStop: @escaping () -> Void = {},
15+
onClear: @escaping () -> Void = {}
1616
) {
1717
self.history = history
1818
self.isReceivingMessage = isReceivingMessage
1919
self.onMessageSend = onMessageSend
2020
self.onStop = onStop
21+
self.onClear = onClear
2122
}
2223

2324
public static func == (lhs: ChatRoom, rhs: ChatRoom) -> Bool {
2425
lhs.history == rhs.history && lhs.isReceivingMessage == rhs.isReceivingMessage
2526
}
27+
28+
public func send(_ message: String) { onMessageSend(message) }
29+
public func stop() { onStop() }
30+
public func clear() { onClear() }
2631
}
2732

2833
public struct ChatMessage: Equatable {

0 commit comments

Comments
 (0)