|
| 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 |
0 commit comments