-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathChatMemory.swift
More file actions
268 lines (230 loc) · 13.7 KB
/
ChatMemory.swift
File metadata and controls
268 lines (230 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import Foundation
import ConversationServiceProvider
public protocol ChatMemory {
/// The message history.
var history: [ChatMessage] { get async }
/// Update the message history.
func mutateHistory(_ update: (inout [ChatMessage]) -> Void) async
}
public extension ChatMemory {
func appendMessage(_ message: ChatMessage) async {
await mutateHistory { history in
if let parentTurnId = message.parentTurnId {
history.removeAll { $0.id == message.id }
guard let parentIndex = history.firstIndex(where: { $0.id == parentTurnId }) else {
return
}
var parentMessage = history[parentIndex]
if !message.editAgentRounds.isEmpty {
var parentRounds = parentMessage.editAgentRounds
if let lastParentRoundIndex = parentRounds.indices.last {
var existingSubRounds = parentRounds[lastParentRoundIndex].subAgentRounds ?? []
for messageRound in message.editAgentRounds {
if let subIndex = existingSubRounds.firstIndex(where: { $0.roundId == messageRound.roundId }) {
existingSubRounds[subIndex].reply = existingSubRounds[subIndex].reply + messageRound.reply
if let messageToolCalls = messageRound.toolCalls, !messageToolCalls.isEmpty {
var mergedToolCalls = existingSubRounds[subIndex].toolCalls ?? []
for newToolCall in messageToolCalls {
if let toolCallIndex = mergedToolCalls.firstIndex(where: { $0.id == newToolCall.id }) {
mergedToolCalls[toolCallIndex].status = newToolCall.status
if let toolType = newToolCall.toolType {
mergedToolCalls[toolCallIndex].toolType = toolType
}
if let progressMessage = newToolCall.progressMessage, !progressMessage.isEmpty {
mergedToolCalls[toolCallIndex].progressMessage = progressMessage
}
if let input = newToolCall.input, !input.isEmpty {
mergedToolCalls[toolCallIndex].input = input
}
if let inputMessage = newToolCall.inputMessage, !inputMessage.isEmpty {
mergedToolCalls[toolCallIndex].inputMessage = inputMessage
}
if let result = newToolCall.result, !result.isEmpty {
mergedToolCalls[toolCallIndex].result = result
}
if let resultDetails = newToolCall.resultDetails, !resultDetails.isEmpty {
mergedToolCalls[toolCallIndex].resultDetails = resultDetails
}
if let error = newToolCall.error, !error.isEmpty {
mergedToolCalls[toolCallIndex].error = error
}
if let invokeParams = newToolCall.invokeParams {
mergedToolCalls[toolCallIndex].invokeParams = invokeParams
}
if let title = newToolCall.title {
mergedToolCalls[toolCallIndex].title = title
}
} else {
mergedToolCalls.append(newToolCall)
}
}
existingSubRounds[subIndex].toolCalls = mergedToolCalls
}
} else {
existingSubRounds.append(messageRound)
}
}
parentRounds[lastParentRoundIndex].subAgentRounds = existingSubRounds
parentMessage.editAgentRounds = parentRounds
}
}
history[parentIndex] = parentMessage
} else if let index = history.firstIndex(where: { $0.id == message.id }) {
history[index].mergeMessage(with: message)
} else {
history.append(message)
}
}
}
/// Remove a message from the history.
func removeMessage(_ id: String) async {
await mutateHistory {
$0.removeAll { $0.id == id }
}
}
/// Remove multiple messages from the history by their IDs.
func removeMessages(_ ids: [String]) async {
await mutateHistory { history in
history.removeAll { message in
ids.contains(message.id)
}
}
}
/// Clear the history.
func clearHistory() async {
await mutateHistory { $0.removeAll() }
}
}
extension ChatMessage {
mutating func mergeMessage(with message: ChatMessage) {
self.content = self.content + message.content
var seen = Set<ConversationReference>()
self.references = (self.references + message.references).filter { seen.insert($0).inserted }
self.followUp = message.followUp ?? self.followUp
self.suggestedTitle = message.suggestedTitle ?? self.suggestedTitle
self.errorMessages = self.errorMessages + message.errorMessages
self.panelMessages = self.panelMessages + message.panelMessages
if !message.steps.isEmpty {
var mergedSteps = self.steps
for newStep in message.steps {
if let index = mergedSteps.firstIndex(where: { $0.id == newStep.id }) {
mergedSteps[index] = newStep
} else {
mergedSteps.append(newStep)
}
}
self.steps = mergedSteps
}
if !message.editAgentRounds.isEmpty {
let mergedAgentRounds = mergeEditAgentRounds(
oldRounds: self.editAgentRounds,
newRounds: message.editAgentRounds
)
self.editAgentRounds = mergedAgentRounds
}
self.parentTurnId = message.parentTurnId ?? self.parentTurnId
self.codeReviewRound = message.codeReviewRound
self.fileEdits = mergeFileEdits(oldEdits: self.fileEdits, newEdits: message.fileEdits)
self.turnStatus = message.turnStatus ?? self.turnStatus
// merge modelName and billingMultiplier
self.modelName = message.modelName ?? self.modelName
self.billingMultiplier = message.billingMultiplier ?? self.billingMultiplier
}
private func mergeEditAgentRounds(oldRounds: [AgentRound], newRounds: [AgentRound]) -> [AgentRound] {
var mergedAgentRounds = oldRounds
for newRound in newRounds {
if let index = mergedAgentRounds.firstIndex(where: { $0.roundId == newRound.roundId }) {
mergedAgentRounds[index].reply = mergedAgentRounds[index].reply + newRound.reply
if newRound.toolCalls != nil, !newRound.toolCalls!.isEmpty {
var mergedToolCalls = mergedAgentRounds[index].toolCalls ?? []
for newToolCall in newRound.toolCalls! {
if let toolCallIndex = mergedToolCalls.firstIndex(where: { $0.id == newToolCall.id }) {
mergedToolCalls[toolCallIndex].status = newToolCall.status
if let toolType = newToolCall.toolType {
mergedToolCalls[toolCallIndex].toolType = toolType
}
if let progressMessage = newToolCall.progressMessage, !progressMessage.isEmpty {
mergedToolCalls[toolCallIndex].progressMessage = newToolCall.progressMessage
}
if let input = newToolCall.input, !input.isEmpty {
mergedToolCalls[toolCallIndex].input = input
}
if let inputMessage = newToolCall.inputMessage, !inputMessage.isEmpty {
mergedToolCalls[toolCallIndex].inputMessage = inputMessage
}
if let result = newToolCall.result, !result.isEmpty {
mergedToolCalls[toolCallIndex].result = result
}
if let resultDetails = newToolCall.resultDetails, !resultDetails.isEmpty {
mergedToolCalls[toolCallIndex].resultDetails = resultDetails
}
if let error = newToolCall.error, !error.isEmpty {
mergedToolCalls[toolCallIndex].error = newToolCall.error
}
if let invokeParams = newToolCall.invokeParams {
mergedToolCalls[toolCallIndex].invokeParams = invokeParams
}
} else {
mergedToolCalls.append(newToolCall)
}
}
mergedAgentRounds[index].toolCalls = mergedToolCalls
}
if let newSubAgentRounds = newRound.subAgentRounds, !newSubAgentRounds.isEmpty {
var mergedSubRounds = mergedAgentRounds[index].subAgentRounds ?? []
for newSubRound in newSubAgentRounds {
if let subIndex = mergedSubRounds.firstIndex(where: { $0.roundId == newSubRound.roundId }) {
mergedSubRounds[subIndex].reply = mergedSubRounds[subIndex].reply + newSubRound.reply
if let subToolCalls = newSubRound.toolCalls, !subToolCalls.isEmpty {
var mergedSubToolCalls = mergedSubRounds[subIndex].toolCalls ?? []
for newSubToolCall in subToolCalls {
if let toolCallIndex = mergedSubToolCalls.firstIndex(where: { $0.id == newSubToolCall.id }) {
mergedSubToolCalls[toolCallIndex].status = newSubToolCall.status
if let progressMessage = newSubToolCall.progressMessage, !progressMessage.isEmpty {
mergedSubToolCalls[toolCallIndex].progressMessage = newSubToolCall.progressMessage
}
if let error = newSubToolCall.error, !error.isEmpty {
mergedSubToolCalls[toolCallIndex].error = newSubToolCall.error
}
if let result = newSubToolCall.result, !result.isEmpty {
mergedSubToolCalls[toolCallIndex].result = result
}
if let resultDetails = newSubToolCall.resultDetails, !resultDetails.isEmpty {
mergedSubToolCalls[toolCallIndex].resultDetails = resultDetails
}
if let invokeParams = newSubToolCall.invokeParams {
mergedSubToolCalls[toolCallIndex].invokeParams = invokeParams
}
} else {
mergedSubToolCalls.append(newSubToolCall)
}
}
mergedSubRounds[subIndex].toolCalls = mergedSubToolCalls
}
} else {
mergedSubRounds.append(newSubRound)
}
}
mergedAgentRounds[index].subAgentRounds = mergedSubRounds
}
} else {
mergedAgentRounds.append(newRound)
}
}
return mergedAgentRounds
}
private func mergeFileEdits(oldEdits: [FileEdit], newEdits: [FileEdit]) -> [FileEdit] {
var edits = oldEdits
for newEdit in newEdits {
if let index = edits.firstIndex(
where: { $0.fileURL == newEdit.fileURL && $0.toolName == newEdit.toolName }
) {
edits[index].modifiedContent = newEdit.modifiedContent
edits[index].status = newEdit.status
} else {
edits.append(newEdit)
}
}
return edits
}
}