From f56bf9b014c0b012fbee1cdafb99fa2ece4624ca Mon Sep 17 00:00:00 2001 From: NamH Date: Tue, 24 Dec 2024 09:04:16 +0700 Subject: [PATCH] fix: ulid generator is generating invalid value: 0001KPJAMH0000000000000000 (#1822) Signed-off-by: James --- engine/services/file_service.cc | 4 ++-- engine/services/message_service.cc | 7 ++----- engine/services/thread_service.cc | 8 +++----- engine/utils/ulid_generator.h | 19 +++++++++++++++++++ 4 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 engine/utils/ulid_generator.h diff --git a/engine/services/file_service.cc b/engine/services/file_service.cc index f2514fbfb..3341227e0 100644 --- a/engine/services/file_service.cc +++ b/engine/services/file_service.cc @@ -1,6 +1,6 @@ #include "file_service.h" #include -#include "utils/ulid/ulid.hh" +#include "utils/ulid_generator.h" cpp::result FileService::UploadFile( const std::string& filename, const std::string& purpose, @@ -11,7 +11,7 @@ cpp::result FileService::UploadFile( std::chrono::system_clock::now().time_since_epoch()) .count(); - auto file_id{"file-" + ulid::Marshal(ulid::CreateNowRand())}; + auto file_id{"file-" + ulid::GenerateUlid()}; OpenAi::File file; file.id = file_id; file.object = "file"; diff --git a/engine/services/message_service.cc b/engine/services/message_service.cc index 5b871f447..9b57e0215 100644 --- a/engine/services/message_service.cc +++ b/engine/services/message_service.cc @@ -1,7 +1,7 @@ #include "services/message_service.h" #include "utils/logging_utils.h" #include "utils/result.hpp" -#include "utils/ulid/ulid.hh" +#include "utils/ulid_generator.h" cpp::result MessageService::CreateMessage( const std::string& thread_id, const OpenAi::Role& role, @@ -27,11 +27,8 @@ cpp::result MessageService::CreateMessage( std::get>>(content)); } - auto ulid = ulid::CreateNowRand(); - auto msg_id = ulid::Marshal(ulid); - OpenAi::Message msg; - msg.id = msg_id; + msg.id = ulid::GenerateUlid(); msg.object = "thread.message"; msg.created_at = seconds_since_epoch; msg.thread_id = thread_id; diff --git a/engine/services/thread_service.cc b/engine/services/thread_service.cc index 25784c2ee..0ec0ac89d 100644 --- a/engine/services/thread_service.cc +++ b/engine/services/thread_service.cc @@ -1,6 +1,7 @@ #include "thread_service.h" +#include #include "utils/logging_utils.h" -#include "utils/ulid/ulid.hh" +#include "utils/ulid_generator.h" cpp::result ThreadService::CreateThread( std::unique_ptr tool_resources, @@ -12,11 +13,8 @@ cpp::result ThreadService::CreateThread( std::chrono::system_clock::now().time_since_epoch()) .count(); - auto ulid = ulid::CreateNowRand(); - auto thread_id = ulid::Marshal(ulid); - OpenAi::Thread thread; - thread.id = thread_id; + thread.id = ulid::GenerateUlid(); thread.object = "thread"; thread.created_at = seconds_since_epoch; diff --git a/engine/utils/ulid_generator.h b/engine/utils/ulid_generator.h new file mode 100644 index 000000000..2d4bb2b0f --- /dev/null +++ b/engine/utils/ulid_generator.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include "utils/ulid/ulid.hh" + +namespace ulid { +inline std::string GenerateUlid() { + auto millisecs = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); + + std::random_device rd; + std::mt19937_64 gen(rd()); + std::uniform_int_distribution dis(0, 255); + auto ulid = ulid::Create( + millisecs, [&dis, &gen]() { return static_cast(dis(gen)); }); + return ulid::Marshal(ulid); +} +} // namespace ulid