#include "message.hpp"
nlohmann::json toJson(const SystemMessage& msg)
{
return {{"role", "system"}, {"content", msg.content}};
}
nlohmann::json toJson(const UserMessage& msg)
{
return {{"role", "user"}, {"content", msg.content}};
}
nlohmann::json toJson(const ToolCall& msg)
{
return {{"id", msg.id},
{"type", "function"},
{"function",
{{"name", msg.name}, {"arguments", msg.arguments.dump()}}}};
}
nlohmann::json toJson(const AssistantMessage& msg)
{
nlohmann::json j = {{"role", "assistant"}};
if(msg.content)
{
j["content"] = *msg.content;
}
if(!msg.tool_calls.empty())
{
nlohmann::json calls_json = nlohmann::json::array();
for(const auto& call : msg.tool_calls)
{
calls_json.push_back(toJson(call));
}
j["tool_calls"] = calls_json;
}
return j;
}
nlohmann::json toJson(const ToolResultMessage& msg)
{
return {{"role", "tool"},
{"tool_call_id", msg.tool_call_id},
{"content", msg.content}};
}
nlohmann::json toJson(const Message& msg)
{
return std::visit([](const auto& m) { return toJson(m); }, msg);
}