Skip to content

Commit 6f12583

Browse files
[Firebase AI] Fix Chat history append locking (#15115)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent b33b85e commit 6f12583

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

FirebaseAI/Sources/Chat.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public final class Chat: Sendable {
4545
}
4646
}
4747

48+
private func appendHistory(_ newElement: ModelContent) {
49+
historyLock.withLock {
50+
_history.append(newElement)
51+
}
52+
}
53+
4854
/// Sends a message using the existing history of this chat as context. If successful, the message
4955
/// and response will be added to the history. If unsuccessful, history will remain unchanged.
5056
/// - Parameter parts: The new content to send as a single chat message.
@@ -82,7 +88,7 @@ public final class Chat: Sendable {
8288

8389
// Append the request and successful result to history, then return the value.
8490
appendHistory(contentsOf: newContent)
85-
history.append(toAdd)
91+
appendHistory(toAdd)
8692
return result
8793
}
8894

@@ -134,7 +140,7 @@ public final class Chat: Sendable {
134140

135141
// Aggregate the content to add it to the history before we finish.
136142
let aggregated = self.aggregatedChunks(aggregatedContent)
137-
self.history.append(aggregated)
143+
self.appendHistory(aggregated)
138144
continuation.finish()
139145
}
140146
}

0 commit comments

Comments
 (0)