Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove sort msg by ulid #1799

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions engine/repositories/message_fs_repository.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,23 @@ MessageFsRepository::ListMessages(const std::string& thread_id, uint8_t limit,
messages.end());
}

const bool is_descending = (order == "desc");
std::sort(
messages.begin(), messages.end(),
[is_descending](const OpenAi::Message& a, const OpenAi::Message& b) {
return is_descending ? (a.id > b.id) : (a.id < b.id);
});

auto start_it = messages.begin();
auto end_it = messages.end();

if (!after.empty()) {
start_it = std::lower_bound(
messages.begin(), messages.end(), after,
[is_descending](const OpenAi::Message& msg, const std::string& value) {
return is_descending ? (msg.id > value) : (msg.id < value);
});

if (start_it != messages.end() && start_it->id == after) {
++start_it;
}
start_it = std::find_if(
messages.begin(), messages.end(),
[&after](const OpenAi::Message& msg) { return msg.id > after; });
}

if (!before.empty()) {
end_it = std::upper_bound(
start_it, messages.end(), before,
[is_descending](const std::string& value, const OpenAi::Message& msg) {
return is_descending ? (value > msg.id) : (value < msg.id);
});
end_it = std::find_if(
start_it, messages.end(),
[&before](const OpenAi::Message& msg) { return msg.id >= before; });
}

if (order == "desc") {
std::reverse(start_it, end_it);
}

const size_t available_messages = std::distance(start_it, end_it);
Expand Down
Loading