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: ulid generator is generating invalid value: 0001KPJAMH0000000000000000 #1822

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
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
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