Skip to content

Commit

Permalink
avoid removing when index is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
shigedangao committed Aug 26, 2024
1 parent 1900774 commit 0574b49
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ impl MessagesHistory {
// Replacing the oldest message if the limit is reached
// The oldest message is the first one, unless it's a system message
if messages.len() >= self.messages_number_limit as usize {
let index_to_remove = messages
.first()
.map(|m| if m.role == MessageRole::System { 1 } else { 0 })
.unwrap_or(0);

messages.remove(index_to_remove);
if let Some(index_to_remove) =
messages
.first()
.map(|m| if m.role == MessageRole::System { 1 } else { 0 })
{
messages.remove(index_to_remove);
}
}

if message.role == MessageRole::System {
Expand Down

0 comments on commit 0574b49

Please sign in to comment.