@@ -80,34 +80,23 @@ MessageFsRepository::ListMessages(const std::string& thread_id, uint8_t limit,
8080 messages.end ());
8181 }
8282
83- const bool is_descending = (order == " desc" );
84- std::sort (
85- messages.begin (), messages.end (),
86- [is_descending](const OpenAi::Message& a, const OpenAi::Message& b) {
87- return is_descending ? (a.id > b.id ) : (a.id < b.id );
88- });
89-
9083 auto start_it = messages.begin ();
9184 auto end_it = messages.end ();
9285
9386 if (!after.empty ()) {
94- start_it = std::lower_bound (
95- messages.begin (), messages.end (), after,
96- [is_descending](const OpenAi::Message& msg, const std::string& value) {
97- return is_descending ? (msg.id > value) : (msg.id < value);
98- });
99-
100- if (start_it != messages.end () && start_it->id == after) {
101- ++start_it;
102- }
87+ start_it = std::find_if (
88+ messages.begin (), messages.end (),
89+ [&after](const OpenAi::Message& msg) { return msg.id > after; });
10390 }
10491
10592 if (!before.empty ()) {
106- end_it = std::upper_bound (
107- start_it, messages.end (), before,
108- [is_descending](const std::string& value, const OpenAi::Message& msg) {
109- return is_descending ? (value > msg.id ) : (value < msg.id );
110- });
93+ end_it = std::find_if (
94+ start_it, messages.end (),
95+ [&before](const OpenAi::Message& msg) { return msg.id >= before; });
96+ }
97+
98+ if (order == " desc" ) {
99+ std::reverse (start_it, end_it);
111100 }
112101
113102 const size_t available_messages = std::distance (start_it, end_it);
0 commit comments