33#include < json/reader.h>
44#include < json/value.h>
55#include < json/writer.h>
6- #include < optional>
76#include " common/thread_tool_resources.h"
87#include " common/variant_map.h"
98#include " json_serializable.h"
@@ -36,7 +35,7 @@ struct Thread : JsonSerializable {
3635 * of tool. For example, the code_interpreter tool requires a list of
3736 * file IDs, while the file_search tool requires a list of vector store IDs.
3837 */
39- std::optional<std:: unique_ptr<ThreadToolResources> > tool_resources;
38+ std::unique_ptr<ThreadToolResources> tool_resources;
4039
4140 /* *
4241 * Set of 16 key-value pairs that can be attached to an object.
@@ -57,7 +56,30 @@ struct Thread : JsonSerializable {
5756 if (thread.created_at == 0 && json[" created" ].asUInt64 () != 0 ) {
5857 thread.created_at = json[" created" ].asUInt64 () / 1000 ;
5958 }
60- // TODO: namh parse tool_resources
59+
60+ if (json.isMember (" tool_resources" ) && !json[" tool_resources" ].isNull ()) {
61+ const auto & tool_json = json[" tool_resources" ];
62+
63+ if (tool_json.isMember (" code_interpreter" )) {
64+ auto code_interpreter = std::make_unique<ThreadCodeInterpreter>();
65+ const auto & file_ids = tool_json[" code_interpreter" ][" file_ids" ];
66+ if (file_ids.isArray ()) {
67+ for (const auto & file_id : file_ids) {
68+ code_interpreter->file_ids .push_back (file_id.asString ());
69+ }
70+ }
71+ thread.tool_resources = std::move (code_interpreter);
72+ } else if (tool_json.isMember (" file_search" )) {
73+ auto file_search = std::make_unique<ThreadFileSearch>();
74+ const auto & store_ids = tool_json[" file_search" ][" vector_store_ids" ];
75+ if (store_ids.isArray ()) {
76+ for (const auto & store_id : store_ids) {
77+ file_search->vector_store_ids .push_back (store_id.asString ());
78+ }
79+ }
80+ thread.tool_resources = std::move (file_search);
81+ }
82+ }
6183
6284 if (json[" metadata" ].isObject () && !json[" metadata" ].empty ()) {
6385 auto res = Cortex::ConvertJsonValueToMap (json[" metadata" ]);
@@ -78,7 +100,24 @@ struct Thread : JsonSerializable {
78100 json[" id" ] = id;
79101 json[" object" ] = object;
80102 json[" created_at" ] = created_at;
81- // TODO: namh handle tool_resources
103+
104+ if (tool_resources) {
105+ auto tool_result = tool_resources->ToJson ();
106+ if (tool_result.has_error ()) {
107+ return cpp::fail (" Failed to serialize tool_resources: " +
108+ tool_result.error ());
109+ }
110+
111+ Json::Value tool_json;
112+ if (auto code_interpreter =
113+ dynamic_cast <ThreadCodeInterpreter*>(tool_resources.get ())) {
114+ tool_json[" code_interpreter" ] = tool_result.value ();
115+ } else if (auto file_search =
116+ dynamic_cast <ThreadFileSearch*>(tool_resources.get ())) {
117+ tool_json[" file_search" ] = tool_result.value ();
118+ }
119+ json[" tool_resources" ] = tool_json;
120+ }
82121
83122 Json::Value metadata_json{Json::objectValue};
84123 for (const auto & [key, value] : metadata) {
0 commit comments