Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 2c833e7

Browse files
committed
feat: add thread
1 parent 4bf5e75 commit 2c833e7

File tree

16 files changed

+835
-49
lines changed

16 files changed

+835
-49
lines changed

engine/common/api-dto/messages/delete_message_response.h renamed to engine/common/api-dto/delete_success_response.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "common/json_serializable.h"
44

55
namespace api_response {
6-
struct DeleteMessageResponse : JsonSerializable {
6+
struct DeleteSuccessResponse : JsonSerializable {
77
std::string id;
88
std::string object;
99
bool deleted;

engine/common/message.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,11 @@
1717
#include "utils/logging_utils.h"
1818
#include "utils/result.hpp"
1919

20+
// TODO: change name to OpenAi
2021
namespace ThreadMessage {
2122

2223
// Represents a message within a thread.
2324
struct Message : JsonSerializable {
24-
Message() = default;
25-
26-
Message(Message&&) = default;
27-
28-
Message& operator=(Message&&) = default;
29-
30-
Message(const Message&) = delete;
31-
32-
Message& operator=(const Message&) = delete;
33-
3425
// The identifier, which can be referenced in API endpoints.
3526
std::string id;
3627

engine/common/message_attachment_factory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include <optional>
24
#include "common/message_attachment.h"
35
#include "utils/result.hpp"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "common/message.h"
4+
#include "common/thread.h"
5+
#include "utils/result.hpp"
6+
7+
class ThreadRepository {
8+
public:
9+
virtual cpp::result<void, std::string> CreateThread(
10+
OpenAi::Thread& thread,
11+
std::optional<std::vector<ThreadMessage::Message>> messages) = 0;
12+
13+
virtual cpp::result<std::vector<OpenAi::Thread>, std::string> ListThreads(
14+
uint8_t limit, const std::string& order, const std::string&,
15+
const std::string& before) const = 0;
16+
17+
virtual cpp::result<OpenAi::Thread, std::string> RetrieveThread(
18+
const std::string& thread_id) const = 0;
19+
20+
virtual cpp::result<void, std::string> ModifyThread(
21+
OpenAi::Thread& thread) = 0;
22+
23+
virtual cpp::result<void, std::string> DeleteThread(
24+
const std::string& thread_id) = 0;
25+
26+
virtual ~ThreadRepository() = default;
27+
};

engine/common/thread.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#pragma once
2+
3+
#include <json/reader.h>
4+
#include <json/value.h>
5+
#include <json/writer.h>
6+
#include "common/thread_tool_resources.h"
7+
#include "common/variant_map.h"
8+
#include "json_serializable.h"
9+
#include "utils/logging_utils.h"
10+
11+
namespace OpenAi {
12+
13+
/**
14+
* Represents a thread that contains messages.
15+
*/
16+
struct Thread : JsonSerializable {
17+
/**
18+
* The identifier, which can be referenced in API endpoints.
19+
*/
20+
std::string id;
21+
22+
/**
23+
* The object type, which is always thread.
24+
*/
25+
std::string object = "thread";
26+
27+
/**
28+
* The Unix timestamp (in seconds) for when the thread was created.
29+
*/
30+
uint64_t created_at;
31+
32+
/**
33+
* A set of resources that are made available to the assistant's
34+
* tools in this thread. The resources are specific to the type
35+
* of tool. For example, the code_interpreter tool requires a list of
36+
* file IDs, while the file_search tool requires a list of vector store IDs.
37+
*/
38+
std::optional<std::unique_ptr<ToolResources>> tool_resources;
39+
40+
/**
41+
* Set of 16 key-value pairs that can be attached to an object.
42+
* This can be useful for storing additional information about the object
43+
* in a structured format.
44+
*
45+
* Keys can be a maximum of 64 characters long and values can be a maximum
46+
* of 512 characters long.
47+
*/
48+
Cortex::VariantMap metadata;
49+
50+
static cpp::result<Thread, std::string> FromJson(const Json::Value& json) {
51+
Thread thread;
52+
53+
thread.id = json["id"].asString();
54+
thread.object = "thread";
55+
thread.created_at = json["created_at"].asUInt();
56+
if (thread.created_at == 0 && json["created"].asUInt64() != 0) {
57+
thread.created_at = json["created"].asUInt64() / 1000;
58+
}
59+
// TODO: namh parse tool_resources
60+
61+
if (json["metadata"].isObject() && !json["metadata"].empty()) {
62+
auto res = Cortex::ConvertJsonValueToMap(json["metadata"]);
63+
if (res.has_error()) {
64+
CTL_WRN("Failed to convert metadata to map: " + res.error());
65+
} else {
66+
thread.metadata = res.value();
67+
}
68+
}
69+
70+
return thread;
71+
}
72+
73+
cpp::result<Json::Value, std::string> ToJson() override {
74+
try {
75+
Json::Value json;
76+
77+
json["id"] = id;
78+
json["object"] = object;
79+
json["created_at"] = created_at;
80+
// TODO: namh handle tool_resources
81+
82+
Json::Value metadata_json{Json::objectValue};
83+
for (const auto& [key, value] : metadata) {
84+
if (std::holds_alternative<bool>(value)) {
85+
metadata_json[key] = std::get<bool>(value);
86+
} else if (std::holds_alternative<uint64_t>(value)) {
87+
metadata_json[key] = std::get<uint64_t>(value);
88+
} else if (std::holds_alternative<double>(value)) {
89+
metadata_json[key] = std::get<double>(value);
90+
} else {
91+
metadata_json[key] = std::get<std::string>(value);
92+
}
93+
}
94+
json["metadata"] = metadata_json;
95+
96+
return json;
97+
} catch (const std::exception& e) {
98+
return cpp::fail(std::string("ToJson failed: ") + e.what());
99+
}
100+
}
101+
};
102+
} // namespace OpenAi
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
#include "common/json_serializable.h"
6+
7+
namespace OpenAi {
8+
9+
struct ToolResources : JsonSerializable {
10+
~ToolResources() = default;
11+
12+
virtual cpp::result<Json::Value, std::string> ToJson() override = 0;
13+
};
14+
15+
struct CodeInterpreter : ToolResources {
16+
std::vector<std::string> file_ids;
17+
18+
cpp::result<Json::Value, std::string> ToJson() override {
19+
try {
20+
Json::Value json;
21+
Json::Value file_ids_json{Json::arrayValue};
22+
for (auto& file_id : file_ids) {
23+
file_ids_json.append(file_id);
24+
}
25+
json["file_ids"] = file_ids_json;
26+
return json;
27+
} catch (const std::exception& e) {
28+
return cpp::fail(std::string("ToJson failed: ") + e.what());
29+
}
30+
}
31+
};
32+
33+
struct FileSearch : ToolResources {
34+
std::vector<std::string> vector_store_ids;
35+
36+
cpp::result<Json::Value, std::string> ToJson() override {
37+
try {
38+
Json::Value json;
39+
Json::Value vector_store_ids_json{Json::arrayValue};
40+
for (auto& vector_store_id : vector_store_ids) {
41+
vector_store_ids_json.append(vector_store_id);
42+
}
43+
json["vector_store_ids"] = vector_store_ids_json;
44+
return json;
45+
} catch (const std::exception& e) {
46+
return cpp::fail(std::string("ToJson failed: ") + e.what());
47+
}
48+
}
49+
};
50+
} // namespace OpenAi

engine/controllers/messages.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "messages.h"
2-
#include "common/api-dto/messages/delete_message_response.h"
2+
#include "common/api-dto/delete_success_response.h"
33
#include "common/message_content.h"
44
#include "common/message_role.h"
55
#include "common/variant_map.h"
@@ -287,7 +287,7 @@ void Messages::DeleteMessage(
287287
return;
288288
}
289289

290-
api_response::DeleteMessageResponse response;
290+
api_response::DeleteSuccessResponse response;
291291
response.id = message_id;
292292
response.object = "thread.message.deleted";
293293
response.deleted = true;

0 commit comments

Comments
 (0)