* @brief Splits Anthropic content blocks into OpenAI text / tool_calls / tool messages. */
| 278 | * @brief Splits Anthropic content blocks into OpenAI text / tool_calls / tool messages. |
| 279 | */ |
| 280 | void AI::OpenAIProvider::translateBlocks(const QJsonArray& blocks, |
| 281 | QString& textAccumulator, |
| 282 | QJsonArray& toolCalls, |
| 283 | QJsonArray& toolResultMessages) |
| 284 | { |
| 285 | for (const auto& bv : blocks) { |
| 286 | const auto block = bv.toObject(); |
| 287 | const auto type = block.value(QStringLiteral("type")).toString(); |
| 288 | |
| 289 | if (type == QStringLiteral("text")) { |
| 290 | const auto t = block.value(QStringLiteral("text")).toString(); |
| 291 | if (t.isEmpty()) |
| 292 | continue; |
| 293 | |
| 294 | if (!textAccumulator.isEmpty()) |
| 295 | textAccumulator.append(QChar('\n')); |
| 296 | |
| 297 | textAccumulator.append(t); |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | if (type == QStringLiteral("tool_use")) { |
| 302 | QJsonObject fn; |
| 303 | fn[QStringLiteral("name")] = sanitizeName(block.value(QStringLiteral("name")).toString()); |
| 304 | fn[QStringLiteral("arguments")] = |
| 305 | QString::fromUtf8(QJsonDocument(block.value(QStringLiteral("input")).toObject()) |
| 306 | .toJson(QJsonDocument::Compact)); |
| 307 | |
| 308 | QJsonObject tc; |
| 309 | tc[QStringLiteral("id")] = block.value(QStringLiteral("id")).toString(); |
| 310 | tc[QStringLiteral("type")] = QStringLiteral("function"); |
| 311 | tc[QStringLiteral("function")] = fn; |
| 312 | toolCalls.append(tc); |
| 313 | continue; |
| 314 | } |
| 315 | |
| 316 | if (type != QStringLiteral("tool_result")) |
| 317 | continue; |
| 318 | |
| 319 | QJsonObject toolMsg; |
| 320 | toolMsg[QStringLiteral("role")] = QStringLiteral("tool"); |
| 321 | toolMsg[QStringLiteral("tool_call_id")] = block.value(QStringLiteral("tool_use_id")).toString(); |
| 322 | toolMsg[QStringLiteral("content")] = block.value(QStringLiteral("content")).toString(); |
| 323 | toolResultMessages.append(toolMsg); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * @brief Converts AI-tool definitions into the OpenAI tool-choice schema. |
nothing calls this directly
no test coverage detected