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

Commit 16ccaf3

Browse files
committed
update create and modify assistant
1 parent ebce1b7 commit 16ccaf3

File tree

6 files changed

+187
-83
lines changed

6 files changed

+187
-83
lines changed

engine/common/thread.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <json/reader.h>
44
#include <json/value.h>
55
#include <json/writer.h>
6+
#include "common/assistant.h"
67
#include "common/thread_tool_resources.h"
78
#include "common/variant_map.h"
89
#include "json_serializable.h"
@@ -47,6 +48,9 @@ struct Thread : JsonSerializable {
4748
*/
4849
Cortex::VariantMap metadata;
4950

51+
// For supporting Jan
52+
std::optional<std::vector<JanAssistant>> assistants;
53+
5054
static cpp::result<Thread, std::string> FromJson(const Json::Value& json) {
5155
Thread thread;
5256

@@ -94,6 +98,21 @@ struct Thread : JsonSerializable {
9498
thread.metadata["title"] = json["title"].asString();
9599
}
96100

101+
if (json.isMember("assistants") && json["assistants"].isArray()) {
102+
std::vector<JanAssistant> assistants;
103+
for (Json::ArrayIndex i = 0; i < json["assistants"].size(); ++i) {
104+
Json::Value assistant_json = json["assistants"][i];
105+
auto assistant_result =
106+
JanAssistant::FromJson(std::move(assistant_json));
107+
if (assistant_result.has_error()) {
108+
return cpp::fail("Failed to parse assistant: " +
109+
assistant_result.error());
110+
}
111+
assistants.push_back(std::move(assistant_result.value()));
112+
}
113+
thread.assistants = std::move(assistants);
114+
}
115+
97116
return thread;
98117
}
99118

@@ -137,6 +156,19 @@ struct Thread : JsonSerializable {
137156
}
138157
json["metadata"] = metadata_json;
139158

159+
if (assistants.has_value()) {
160+
Json::Value assistants_json(Json::arrayValue);
161+
for (auto& assistant : assistants.value()) {
162+
auto assistant_json = assistant.ToJson();
163+
if (assistant_json.has_error()) {
164+
return cpp::fail("Failed to serialize assistant: " +
165+
assistant_json.error());
166+
}
167+
assistants_json.append(assistant_json.value());
168+
}
169+
json["assistants"] = assistants_json;
170+
}
171+
140172
return json;
141173
} catch (const std::exception& e) {
142174
return cpp::fail(std::string("ToJson failed: ") + e.what());

engine/controllers/assistants.cc

Lines changed: 94 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,61 @@ void Assistants::RetrieveAssistant(
3333
}
3434
}
3535

36+
void Assistants::CreateAssistant(
37+
const HttpRequestPtr& req,
38+
std::function<void(const HttpResponsePtr&)>&& callback,
39+
const std::string& assistant_id) {
40+
auto json_body = req->getJsonObject();
41+
if (json_body == nullptr) {
42+
Json::Value ret;
43+
ret["message"] = "Request body can't be empty";
44+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
45+
resp->setStatusCode(k400BadRequest);
46+
callback(resp);
47+
return;
48+
}
49+
50+
// Parse assistant from request body
51+
auto assistant_result = OpenAi::JanAssistant::FromJson(std::move(*json_body));
52+
if (assistant_result.has_error()) {
53+
Json::Value ret;
54+
ret["message"] = "Failed to parse assistant: " + assistant_result.error();
55+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
56+
resp->setStatusCode(k400BadRequest);
57+
callback(resp);
58+
return;
59+
}
60+
61+
// Call assistant service to create
62+
auto create_result = assistant_service_->CreateAssistant(
63+
assistant_id, assistant_result.value());
64+
if (create_result.has_error()) {
65+
Json::Value ret;
66+
ret["message"] = create_result.error();
67+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
68+
resp->setStatusCode(k400BadRequest);
69+
callback(resp);
70+
return;
71+
}
72+
73+
// Convert result to JSON and send response
74+
auto to_json_result = create_result->ToJson();
75+
if (to_json_result.has_error()) {
76+
CTL_ERR("Failed to convert assistant to json: " + to_json_result.error());
77+
Json::Value ret;
78+
ret["message"] = to_json_result.error();
79+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
80+
resp->setStatusCode(k400BadRequest);
81+
callback(resp);
82+
return;
83+
}
84+
85+
auto resp =
86+
cortex_utils::CreateCortexHttpJsonResponse(to_json_result.value());
87+
resp->setStatusCode(k201Created);
88+
callback(resp);
89+
}
90+
3691
void Assistants::ModifyAssistant(
3792
const HttpRequestPtr& req,
3893
std::function<void(const HttpResponsePtr&)>&& callback,
@@ -47,28 +102,43 @@ void Assistants::ModifyAssistant(
47102
return;
48103
}
49104

50-
// auto res =
51-
// assistant_service_->ModifyAssistant(thread_id, nullptr, metadata.value());
52-
// if (res.has_error()) {
53-
// Json::Value ret;
54-
// ret["message"] = res.error();
55-
// auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
56-
// resp->setStatusCode(k400BadRequest);
57-
// callback(resp);
58-
// } else {
59-
// auto message_to_json = res->ToJson();
60-
// if (message_to_json.has_error()) {
61-
// CTL_ERR("Failed to convert message to json: " + message_to_json.error());
62-
// Json::Value ret;
63-
// ret["message"] = message_to_json.error();
64-
// auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
65-
// resp->setStatusCode(k400BadRequest);
66-
// callback(resp);
67-
// } else {
68-
// auto resp =
69-
// cortex_utils::CreateCortexHttpJsonResponse(res->ToJson().value());
70-
// resp->setStatusCode(k200OK);
71-
// callback(resp);
72-
// }
73-
// }
105+
// Parse assistant from request body
106+
auto assistant_result = OpenAi::JanAssistant::FromJson(std::move(*json_body));
107+
if (assistant_result.has_error()) {
108+
Json::Value ret;
109+
ret["message"] = "Failed to parse assistant: " + assistant_result.error();
110+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
111+
resp->setStatusCode(k400BadRequest);
112+
callback(resp);
113+
return;
114+
}
115+
116+
// Call assistant service to create
117+
auto modify_result = assistant_service_->ModifyAssistant(
118+
assistant_id, assistant_result.value());
119+
if (modify_result.has_error()) {
120+
Json::Value ret;
121+
ret["message"] = modify_result.error();
122+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
123+
resp->setStatusCode(k400BadRequest);
124+
callback(resp);
125+
return;
126+
}
127+
128+
// Convert result to JSON and send response
129+
auto to_json_result = modify_result->ToJson();
130+
if (to_json_result.has_error()) {
131+
CTL_ERR("Failed to convert assistant to json: " + to_json_result.error());
132+
Json::Value ret;
133+
ret["message"] = to_json_result.error();
134+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
135+
resp->setStatusCode(k400BadRequest);
136+
callback(resp);
137+
return;
138+
}
139+
140+
auto resp =
141+
cortex_utils::CreateCortexHttpJsonResponse(to_json_result.value());
142+
resp->setStatusCode(k200OK);
143+
callback(resp);
74144
}

engine/controllers/assistants.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ class Assistants : public drogon::HttpController<Assistants, false> {
1111
METHOD_LIST_BEGIN
1212
ADD_METHOD_TO(Assistants::RetrieveAssistant, "/v1/assistants/{assistant_id}",
1313
Get);
14+
15+
ADD_METHOD_TO(Assistants::ModifyAssistant, "/v1/assistants/{assistant_id}",
16+
Options, Post);
17+
1418
ADD_METHOD_TO(Assistants::ModifyAssistant, "/v1/assistants/{assistant_id}",
15-
Options, Post, Patch);
19+
Options, Patch);
1620
METHOD_LIST_END
1721

1822
explicit Assistants(std::shared_ptr<AssistantService> assistant_srv)
@@ -22,6 +26,10 @@ class Assistants : public drogon::HttpController<Assistants, false> {
2226
std::function<void(const HttpResponsePtr&)>&& callback,
2327
const std::string& assistant_id) const;
2428

29+
void CreateAssistant(const HttpRequestPtr& req,
30+
std::function<void(const HttpResponsePtr&)>&& callback,
31+
const std::string& assistant_id);
32+
2533
void ModifyAssistant(const HttpRequestPtr& req,
2634
std::function<void(const HttpResponsePtr&)>&& callback,
2735
const std::string& assistant_id);

engine/repositories/thread_fs_repository.cc

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "thread_fs_repository.h"
22
#include <fstream>
33
#include <mutex>
4+
#include "common/assistant.h"
45
#include "utils/result.hpp"
56

67
cpp::result<std::vector<OpenAi::Thread>, std::string>
@@ -22,7 +23,6 @@ ThreadFsRepository::ListThreads(uint8_t limit, const std::string& order,
2223
continue;
2324

2425
auto current_thread_id = entry.path().filename().string();
25-
CTL_INF("ListThreads: Found thread: " + current_thread_id);
2626
std::shared_lock thread_lock(GrabThreadMutex(current_thread_id));
2727

2828
auto thread_result = LoadThread(current_thread_id);
@@ -206,10 +206,44 @@ ThreadFsRepository::LoadAssistant(const std::string& thread_id) const {
206206
cpp::result<OpenAi::JanAssistant, std::string>
207207
ThreadFsRepository::ModifyAssistant(const std::string& thread_id,
208208
const OpenAi::JanAssistant& assistant) {
209-
return cpp::fail("Not Implemented");
209+
std::unique_lock lock(GrabThreadMutex(thread_id));
210+
211+
// Load the existing thread
212+
auto thread_result = LoadThread(thread_id);
213+
if (!thread_result.has_value()) {
214+
return cpp::fail("Failed to load thread: " + thread_result.error());
215+
}
216+
217+
auto& thread = thread_result.value();
218+
if (thread.ToJson()
219+
->get("assistants", Json::Value(Json::arrayValue))
220+
.empty()) {
221+
return cpp::fail("No assistants found in thread: " + thread_id);
222+
}
223+
224+
thread.assistants = {assistant};
225+
226+
auto save_result = SaveThread(thread);
227+
if (!save_result.has_value()) {
228+
return cpp::fail("Failed to save thread: " + save_result.error());
229+
}
230+
231+
return assistant;
210232
}
211233

212234
cpp::result<void, std::string> ThreadFsRepository::CreateAssistant(
213235
const std::string& thread_id, const OpenAi::JanAssistant& assistant) {
214-
return cpp::fail("Not Implemented");
236+
std::unique_lock lock(GrabThreadMutex(thread_id));
237+
238+
// Load the existing thread
239+
auto thread_result = LoadThread(thread_id);
240+
if (!thread_result.has_value()) {
241+
return cpp::fail("Failed to load thread: " + thread_result.error());
242+
}
243+
244+
auto& thread = thread_result.value();
245+
thread.assistants = {assistant};
246+
247+
// Save the modified thread
248+
return SaveThread(thread);
215249
}

engine/services/assistant_service.cc

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
#include "utils/logging_utils.h"
33

44
cpp::result<OpenAi::JanAssistant, std::string>
5-
AssistantService::CreateAssistant(const OpenAi::JanAssistant& assistant) {
6-
CTL_INF("CreateAssistant");
5+
AssistantService::CreateAssistant(const std::string& thread_id,
6+
const OpenAi::JanAssistant& assistant) {
7+
CTL_INF("CreateAssistant: " + thread_id);
8+
auto res = thread_repository_->CreateAssistant(thread_id, assistant);
79

8-
return cpp::fail("Not implemented");
10+
if (res.has_error()) {
11+
return cpp::fail(res.error());
12+
}
13+
14+
return assistant;
915
}
1016

1117
cpp::result<OpenAi::JanAssistant, std::string>
@@ -15,40 +21,8 @@ AssistantService::RetrieveAssistant(const std::string& assistant_id) const {
1521
}
1622

1723
cpp::result<OpenAi::JanAssistant, std::string>
18-
AssistantService::ModifyAssistant(
19-
const std::string& thread_id,
20-
std::unique_ptr<OpenAi::ThreadToolResources> tool_resources,
21-
std::optional<Cortex::VariantMap> metadata) {
22-
LOG_TRACE << "ModifyThread " << thread_id;
23-
24-
return cpp::fail("Not implemented");
25-
// auto retrieve_res = RetrieveThread(thread_id);
26-
// if (retrieve_res.has_error()) {
27-
// return cpp::fail("Failed to retrieve thread: " + retrieve_res.error());
28-
// }
29-
//
30-
// if (tool_resources) {
31-
// retrieve_res->tool_resources = std::move(tool_resources);
32-
// }
33-
// retrieve_res->metadata = std::move(metadata.value());
34-
//
35-
// auto res = thread_repository_->ModifyThread(retrieve_res.value());
36-
// if (res.has_error()) {
37-
// CTL_ERR("Failed to modify thread: " + res.error());
38-
// return cpp::fail("Failed to modify thread: " + res.error());
39-
// } else {
40-
// return RetrieveAssistant(thread_id);
41-
// }
24+
AssistantService::ModifyAssistant(const std::string& thread_id,
25+
const OpenAi::JanAssistant& assistant) {
26+
CTL_INF("RetrieveAssistant: " + thread_id);
27+
return thread_repository_->ModifyAssistant(thread_id, assistant);
4228
}
43-
44-
// cpp::result<std::string, std::string> ThreadService::DeleteThread(
45-
// const std::string& thread_id) {
46-
// LOG_TRACE << "DeleteThread: " + thread_id;
47-
// auto res = thread_repository_->DeleteThread(thread_id);
48-
// if (res.has_error()) {
49-
// LOG_ERROR << "Failed to delete thread: " + res.error();
50-
// return cpp::fail("Failed to delete thread: " + res.error());
51-
// } else {
52-
// return thread_id;
53-
// }
54-
// }

engine/services/assistant_service.h

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#pragma once
22

3-
#include <optional>
43
#include "common/assistant.h"
5-
#include "common/thread_tool_resources.h"
6-
#include "common/variant_map.h"
74
#include "repositories/thread_fs_repository.h"
85
#include "utils/result.hpp"
96

@@ -14,24 +11,13 @@ class AssistantService {
1411
: thread_repository_{thread_repository} {}
1512

1613
cpp::result<OpenAi::JanAssistant, std::string> CreateAssistant(
17-
const OpenAi::JanAssistant& assistant);
18-
19-
// Not supported for now
20-
// cpp::result<std::vector<OpenAi::Thread>, std::string> ListThreads(
21-
// uint8_t limit, const std::string& order, const std::string& after,
22-
// const std::string& before) const;
14+
const std::string& thread_id, const OpenAi::JanAssistant& assistant);
2315

2416
cpp::result<OpenAi::JanAssistant, std::string> RetrieveAssistant(
2517
const std::string& thread_id) const;
2618

2719
cpp::result<OpenAi::JanAssistant, std::string> ModifyAssistant(
28-
const std::string& thread_id,
29-
std::unique_ptr<OpenAi::ThreadToolResources> tool_resources,
30-
std::optional<Cortex::VariantMap> metadata);
31-
32-
// Not supported for now
33-
// cpp::result<std::string, std::string> DeleteThread(
34-
// const std::string& thread_id);
20+
const std::string& thread_id, const OpenAi::JanAssistant& assistant);
3521

3622
private:
3723
std::shared_ptr<AssistantBackwardCompatibleSupport> thread_repository_;

0 commit comments

Comments
 (0)