Skip to content

Commit e9fe64d

Browse files
committed
Make ChatGPTChatTab persistable
1 parent b539596 commit e9fe64d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Core/Sources/ChatGPTChatTab/ChatGPTChatTab.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class ChatGPTChatTab: ChatTab {
1818
struct RestorableState: Codable {
1919
var history: [OpenAIService.ChatMessage]
2020
var configuration: OverridingChatGPTConfiguration.Overriding
21+
var systemPrompt: String
22+
var extraSystemPrompt: String
2123
}
2224

2325
struct Builder: ChatTabBuilder {
@@ -46,7 +48,9 @@ public class ChatGPTChatTab: ChatTab {
4648
public func restorableState() async -> Data {
4749
let state = RestorableState(
4850
history: await service.memory.history,
49-
configuration: service.configuration.overriding
51+
configuration: service.configuration.overriding,
52+
systemPrompt: service.systemPrompt,
53+
extraSystemPrompt: service.extraSystemPrompt
5054
)
5155
return (try? JSONEncoder().encode(state)) ?? Data()
5256
}
@@ -58,6 +62,8 @@ public class ChatGPTChatTab: ChatTab {
5862
let state = try JSONDecoder().decode(RestorableState.self, from: data)
5963
let builder = Builder(title: "Chat") { @MainActor tab in
6064
tab.service.configuration.overriding = state.configuration
65+
tab.service.mutateSystemPrompt(state.systemPrompt)
66+
tab.service.mutateExtraSystemPrompt(state.extraSystemPrompt)
6167
await tab.service.memory.mutateHistory { history in
6268
history = state.history
6369
}
@@ -85,11 +91,25 @@ public class ChatGPTChatTab: ChatTab {
8591

8692
public func start() {
8793
chatTabViewStore.send(.updateTitle("Chat"))
94+
95+
service.$systemPrompt.removeDuplicates().sink { _ in
96+
Task { @MainActor [weak self] in
97+
self?.chatTabViewStore.send(.tabContentUpdated)
98+
}
99+
}.store(in: &cancellable)
100+
101+
service.$extraSystemPrompt.removeDuplicates().sink { _ in
102+
Task { @MainActor [weak self] in
103+
self?.chatTabViewStore.send(.tabContentUpdated)
104+
}
105+
}.store(in: &cancellable)
106+
88107
provider.$history.sink { [weak self] _ in
89108
Task { @MainActor [weak self] in
90109
if let title = self?.provider.title {
91110
self?.chatTabViewStore.send(.updateTitle(title))
92111
}
112+
self?.chatTabViewStore.send(.tabContentUpdated)
93113
}
94114
}.store(in: &cancellable)
95115
}

Pro

Submodule Pro updated from 0a25da7 to 240703a

0 commit comments

Comments
 (0)