Skip to content

Commit

Permalink
Use synchronized() with lock on _messages access (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
talumbau committed Aug 28, 2024
1 parent a07683d commit c63353e
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,21 @@ class GemmaUiState(
) : UiState {
private val START_TURN = "<start_of_turn>"
private val END_TURN = "<end_of_turn>"
private val lock = Any()

private val _messages: MutableList<ChatMessage> = messages.toMutableStateList()
override val messages: List<ChatMessage>
get() = _messages
.map {
// Remove the prefix and suffix before showing a message in the UI
it.copy(
message = it.message.replace(START_TURN + it.author + "\n", "")
.replace(END_TURN, "")
)
}.reversed()
get() = synchronized(lock) {
_messages. apply{
for (i in indices) {
this[i] = this[i].copy(
message = this[i].message.replace(START_TURN + this[i].author + "\n", "")
.replace(END_TURN, "")
)
}
}.asReversed()

}

// Only using the last 4 messages to keep input + output short
override val fullPrompt: String
Expand Down

0 comments on commit c63353e

Please sign in to comment.