* @brief Finalizes a meta.fetchHelp request: parses the body, records, resumes. */
| 1562 | * @brief Finalizes a meta.fetchHelp request: parses the body, records, resumes. |
| 1563 | */ |
| 1564 | void AI::Conversation::completeHelpFetch(const QString& callId, |
| 1565 | const QUrl& url, |
| 1566 | QNetworkReply* reply) |
| 1567 | { |
| 1568 | const auto status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); |
| 1569 | const auto netError = reply->error(); |
| 1570 | const auto host = url.host(); |
| 1571 | const auto path = url.path(); |
| 1572 | const bool isHelpDoc = host.endsWith(QStringLiteral("githubusercontent.com")) |
| 1573 | && path.contains(QStringLiteral("/doc/help/")); |
| 1574 | |
| 1575 | QJsonObject result; |
| 1576 | result[QStringLiteral("url")] = url.toString(); |
| 1577 | |
| 1578 | if (status == 404 && isHelpDoc && !path.endsWith(QStringLiteral("help.json"))) { |
| 1579 | reply->deleteLater(); |
| 1580 | fetchHelpIndex(callId, url); |
| 1581 | return; |
| 1582 | } |
| 1583 | |
| 1584 | if (netError != QNetworkReply::NoError) { |
| 1585 | result[QStringLiteral("ok")] = false; |
| 1586 | result[QStringLiteral("error")] = reply->errorString(); |
| 1587 | } else { |
| 1588 | const auto bytes = reply->readAll(); |
| 1589 | const bool isRawMarkdown = host.endsWith(QStringLiteral("githubusercontent.com")) |
| 1590 | || path.endsWith(QStringLiteral(".md"), Qt::CaseInsensitive); |
| 1591 | |
| 1592 | QString text; |
| 1593 | if (isRawMarkdown) { |
| 1594 | text = QString::fromUtf8(bytes); |
| 1595 | } else { |
| 1596 | QTextDocument doc; |
| 1597 | doc.setHtml(QString::fromUtf8(bytes)); |
| 1598 | text = doc.toPlainText(); |
| 1599 | } |
| 1600 | |
| 1601 | constexpr int kMaxBytes = 32 * 1024; |
| 1602 | if (text.size() > kMaxBytes) |
| 1603 | text = text.left(kMaxBytes) + QStringLiteral("\n... [truncated]"); |
| 1604 | |
| 1605 | result[QStringLiteral("ok")] = true; |
| 1606 | result[QStringLiteral("content")] = text; |
| 1607 | } |
| 1608 | |
| 1609 | reply->deleteLater(); |
| 1610 | |
| 1611 | recordToolResult(callId, QStringLiteral("meta.fetchHelp"), result); |
| 1612 | updateToolCallCard(callId, |
| 1613 | result.value(QStringLiteral("ok")).toBool() ? CallStatus::Done |
| 1614 | : CallStatus::Error, |
| 1615 | result); |
| 1616 | releaseOutstandingToolResult(); |
| 1617 | |
| 1618 | if (m_outstandingToolResults == 0 && m_awaitingConfirm.isEmpty() && !m_reply) |
| 1619 | resumeAfterToolBatch(); |
| 1620 | } |
| 1621 |