-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathChatMemory.swift
More file actions
119 lines (99 loc) · 4.38 KB
/
ChatMemory.swift
File metadata and controls
119 lines (99 loc) · 4.38 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
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 {
/// Append a message to the history.
func appendMessage(_ message: ChatMessage) async {
await mutateHistory { history in
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 }
}
}
/// Clear the history.
func clearHistory() async {
await mutateHistory { $0.removeAll() }
}
}
extension ChatMessage {
mutating func mergeMessage(with message: ChatMessage) {
// merge content
self.content = self.content + message.content
// merge references
var seen = Set<ConversationReference>()
// without duplicated and keep order
self.references = (self.references + message.references).filter { seen.insert($0).inserted }
// merge followUp
self.followUp = message.followUp ?? self.followUp
// merge suggested title
self.suggestedTitle = message.suggestedTitle ?? self.suggestedTitle
// merge error message
self.errorMessages = self.errorMessages + message.errorMessages
self.panelMessages = self.panelMessages + message.panelMessages
// merge steps
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
}
// merge agent steps
if !message.editAgentRounds.isEmpty {
let mergedAgentRounds = mergeEditAgentRounds(
oldRounds: self.editAgentRounds,
newRounds: message.editAgentRounds
)
self.editAgentRounds = mergedAgentRounds
}
self.codeReviewRound = message.codeReviewRound
}
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 progressMessage = newToolCall.progressMessage, !progressMessage.isEmpty {
mergedToolCalls[toolCallIndex].progressMessage = newToolCall.progressMessage
}
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
}
} else {
mergedAgentRounds.append(newRound)
}
}
return mergedAgentRounds
}
}