Skip to content

Commit

Permalink
fix: ulid generator is generating invalid value: 0001KPJAMH0000000000…
Browse files Browse the repository at this point in the history
…000000 (#1822)

Signed-off-by: James <[email protected]>
  • Loading branch information
namchuai authored Dec 24, 2024
1 parent 3414f18 commit f56bf9b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions engine/services/file_service.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "file_service.h"
#include <cstdint>
#include "utils/ulid/ulid.hh"
#include "utils/ulid_generator.h"

cpp::result<OpenAi::File, std::string> FileService::UploadFile(
const std::string& filename, const std::string& purpose,
Expand All @@ -11,7 +11,7 @@ cpp::result<OpenAi::File, std::string> 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";
Expand Down
7 changes: 2 additions & 5 deletions engine/services/message_service.cc
Original file line number Diff line number Diff line change
@@ -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<OpenAi::Message, std::string> MessageService::CreateMessage(
const std::string& thread_id, const OpenAi::Role& role,
Expand All @@ -27,11 +27,8 @@ cpp::result<OpenAi::Message, std::string> MessageService::CreateMessage(
std::get<std::vector<std::unique_ptr<OpenAi::Content>>>(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;
Expand Down
8 changes: 3 additions & 5 deletions engine/services/thread_service.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "thread_service.h"
#include <chrono>
#include "utils/logging_utils.h"
#include "utils/ulid/ulid.hh"
#include "utils/ulid_generator.h"

cpp::result<OpenAi::Thread, std::string> ThreadService::CreateThread(
std::unique_ptr<OpenAi::ThreadToolResources> tool_resources,
Expand All @@ -12,11 +13,8 @@ cpp::result<OpenAi::Thread, std::string> 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;

Expand Down
19 changes: 19 additions & 0 deletions engine/utils/ulid_generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <string>
#include "utils/ulid/ulid.hh"

namespace ulid {
inline std::string GenerateUlid() {
auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();

std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<unsigned int> dis(0, 255);
auto ulid = ulid::Create(
millisecs, [&dis, &gen]() { return static_cast<uint8_t>(dis(gen)); });
return ulid::Marshal(ulid);
}
} // namespace ulid

0 comments on commit f56bf9b

Please sign in to comment.