Files
QodeAssist/templates/ToolMessages.hpp
2026-05-17 21:27:18 +02:00

46 lines
1.4 KiB
C++

// Copyright (C) 2024-2026 Petr Mironychev
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include "pluginllmcore/ContextData.hpp"
namespace QodeAssist::Templates {
inline bool appendOpenAIToolMessage(QJsonArray &messages, const PluginLLMCore::Message &msg)
{
if (!msg.toolCalls.isEmpty()) {
QJsonArray toolCalls;
for (const auto &call : msg.toolCalls) {
toolCalls.append(QJsonObject{
{"id", call.id},
{"type", "function"},
{"function",
QJsonObject{
{"name", call.name},
{"arguments",
QString::fromUtf8(
QJsonDocument(call.arguments).toJson(QJsonDocument::Compact))}}}});
}
QJsonObject toolMessage{{"role", "assistant"}, {"tool_calls", toolCalls}};
toolMessage["content"] = msg.content.isEmpty() ? QJsonValue() : QJsonValue(msg.content);
messages.append(toolMessage);
return true;
}
if (msg.role == QLatin1String("tool")) {
messages.append(QJsonObject{
{"role", "tool"}, {"tool_call_id", msg.toolCallId}, {"content", msg.content}});
return true;
}
return false;
}
} // namespace QodeAssist::Templates