|
1 | 1 | import Foundation |
2 | 2 | import GPTEncoder |
3 | | - |
4 | | -public protocol ChatGPTMemory { |
5 | | - /// The visible messages to the ChatGPT service. |
6 | | - var messages: [ChatMessage] { get async } |
7 | | - /// The remaining tokens available for the reply. |
8 | | - var remainingTokens: Int? { get async } |
9 | | - /// Update the message history. |
10 | | - func mutateHistory(_ update: (inout [ChatMessage]) -> Void) async |
11 | | -} |
12 | | - |
13 | | -public extension ChatGPTMemory { |
14 | | - /// Append a message to the history. |
15 | | - func appendMessage(_ message: ChatMessage) async { |
16 | | - await mutateHistory { |
17 | | - $0.append(message) |
18 | | - } |
19 | | - } |
20 | | - |
21 | | - /// Update a message in the history. |
22 | | - func updateMessage(id: String, _ update: (inout ChatMessage) -> Void) async { |
23 | | - await mutateHistory { history in |
24 | | - if let index = history.firstIndex(where: { $0.id == id }) { |
25 | | - update(&history[index]) |
26 | | - } |
27 | | - } |
28 | | - } |
29 | | - |
30 | | - /// Remove a message from the history. |
31 | | - func removeMessage(_ id: String) async { |
32 | | - await mutateHistory { |
33 | | - $0.removeAll { $0.id == id } |
34 | | - } |
35 | | - } |
36 | | - |
37 | | - /// Stream a message to the history. |
38 | | - func streamMessage(id: String, role: ChatMessage.Role?, content: String?) async { |
39 | | - await mutateHistory { history in |
40 | | - if let index = history.firstIndex(where: { $0.id == id }) { |
41 | | - if let content { |
42 | | - history[index].content.append(content) |
43 | | - } |
44 | | - if let role { |
45 | | - history[index].role = role |
46 | | - } |
47 | | - } else { |
48 | | - history.append(.init( |
49 | | - id: id, |
50 | | - role: role ?? .system, |
51 | | - content: content ?? "" |
52 | | - )) |
53 | | - } |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - /// Clear the history. |
58 | | - func clearHistory() async { |
59 | | - await mutateHistory { $0.removeAll() } |
60 | | - } |
61 | | -} |
62 | | - |
63 | | -public actor ConversationChatGPTMemory: ChatGPTMemory { |
64 | | - public var messages: [ChatMessage] = [] |
65 | | - public var remainingTokens: Int? { nil } |
66 | | - |
67 | | - public init(systemPrompt: String) { |
68 | | - messages.append(.init(role: .system, content: systemPrompt)) |
69 | | - } |
70 | | - |
71 | | - public func mutateHistory(_ update: (inout [ChatMessage]) -> Void) { |
72 | | - update(&messages) |
73 | | - } |
74 | | -} |
| 3 | +import Preferences |
75 | 4 |
|
76 | 5 | /// A memory that automatically manages the history according to max tokens and max message count. |
77 | 6 | public actor AutoManagedChatGPTMemory: ChatGPTMemory { |
@@ -101,6 +30,13 @@ public actor AutoManagedChatGPTMemory: ChatGPTMemory { |
101 | 30 | public func mutateSystemPrompt(_ newPrompt: String) { |
102 | 31 | systemPrompt.content = newPrompt |
103 | 32 | } |
| 33 | + |
| 34 | + public nonisolated |
| 35 | + func observeHistoryChange(_ onChange: @escaping () -> Void) { |
| 36 | + Task { |
| 37 | + await setOnHistoryChangeBlock(onChange) |
| 38 | + } |
| 39 | + } |
104 | 40 |
|
105 | 41 | func generateSendingHistory( |
106 | 42 | maxNumberOfMessages: Int = UserDefaults.shared.value(for: \.chatGPTMaxMessageCount), |
@@ -146,13 +82,6 @@ public actor AutoManagedChatGPTMemory: ChatGPTMemory { |
146 | 82 | return max(configuration.minimumReplyTokens, configuration.maxTokens - tokensCount) |
147 | 83 | } |
148 | 84 |
|
149 | | - public nonisolated |
150 | | - func observeHistoryChange(_ onChange: @escaping () -> Void) { |
151 | | - Task { |
152 | | - await setOnHistoryChangeBlock(onChange) |
153 | | - } |
154 | | - } |
155 | | - |
156 | 85 | func setOnHistoryChangeBlock(_ onChange: @escaping () -> Void) { |
157 | 86 | onHistoryChange = onChange |
158 | 87 | } |
|
0 commit comments