@@ -2,6 +2,7 @@ import ChatService
22import ChatTab
33import Combine
44import Foundation
5+ import OpenAIService
56import Preferences
67import SwiftUI
78
@@ -13,6 +14,11 @@ public class ChatGPTChatTab: ChatTab {
1314 public let provider : ChatProvider
1415 private var cancellable = Set < AnyCancellable > ( )
1516
17+ struct RestorableState : Codable {
18+ var history : [ OpenAIService . ChatMessage ]
19+ var configuration : OverridingChatGPTConfiguration . Overriding
20+ }
21+
1622 struct Builder : ChatTabBuilder {
1723 var title : String
1824 var buildable : Bool { true }
@@ -37,6 +43,28 @@ public class ChatGPTChatTab: ChatTab {
3743 ChatContextMenu ( chat: provider)
3844 }
3945
46+ public func restorableState( ) async -> Data {
47+ let state = RestorableState (
48+ history: await service. memory. history,
49+ configuration: service. configuration. overriding
50+ )
51+ return ( try ? JSONEncoder ( ) . encode ( state) ) ?? Data ( )
52+ }
53+
54+ public static func restore(
55+ from data: Data ,
56+ externalDependency: Void
57+ ) async throws -> any ChatTab {
58+ let state = try JSONDecoder ( ) . decode ( RestorableState . self, from: data)
59+ let tab = ChatGPTChatTab ( )
60+ tab. service. configuration. overriding = state. configuration
61+ await tab. service. memory. mutateHistory { history in
62+ history = state. history
63+ }
64+
65+ return tab
66+ }
67+
4068 public static func chatBuilders( externalDependency: Void ) -> [ ChatTabBuilder ] {
4169 let customCommands = UserDefaults . shared. value ( for: \. customCommands) . compactMap {
4270 command in
0 commit comments