fix: truncate webfetch content to fit context

This commit is contained in:
2026-09-30 15:01:28 +02:00
parent e700a4f96b
commit e2786a3f1e
31 changed files with 1386 additions and 135 deletions
+47 -2
View File
@@ -1,5 +1,9 @@
#include "generation.hpp"
#include "llmclient.hpp"
#include "messagemodel.hpp"
#include "session.hpp"
#include <QDateTime>
namespace ZShell::llm {
@@ -46,7 +50,7 @@ QString ChatGeneration::reasoning() const {
bool ChatGeneration::reasoningActive() const {
if (!m_streaming) return false;
if (!content().isEmpty()) return false;
return !hasRunningTool();
return !hasRunningTool() && !hasPendingTool();
}
qint64 ChatGeneration::reasoningElapsedMs() const {
@@ -87,6 +91,45 @@ bool ChatGeneration::hasRunningTool() const {
return false;
}
bool ChatGeneration::hasPendingTool() const {
for (const auto* segment : m_segments)
if (segment->type() == LlmSegment::Type::ToolCall &&
segment->status() == LlmSegment::Status::Pending)
return true;
return false;
}
void ChatGeneration::setApprovalPending(bool value) {
if (m_approvalPending == value) return;
m_approvalPending = value;
Q_EMIT toolApprovalPendingChanged();
}
QVariantList ChatGeneration::pendingToolCalls() const {
QVariantList out;
if (!m_approvalPending) return out;
for (const auto* segment : m_segments)
if (segment->type() == LlmSegment::Type::ToolCall &&
segment->status() == LlmSegment::Status::Pending)
out.append(QVariant::fromValue(segment));
return out;
}
void ChatGeneration::approveTools() {
if (auto* llmClient = client()) llmClient->approveTools();
}
void ChatGeneration::denyTools() {
if (auto* llmClient = client()) llmClient->denyTools();
}
LlmClient* ChatGeneration::client() const {
if (auto* message = qobject_cast<ChatMessage*>(parent()))
if (auto* model = qobject_cast<ChatMessageModel*>(message->parent()))
if (auto* session = model->session()) return session->client();
return nullptr;
}
void ChatGeneration::updateReasoningActive() {
const bool active = reasoningActive();
if (m_reasoningActive == active) return;
@@ -177,7 +220,9 @@ LlmSegment* ChatGeneration::beginToolCall(
LlmSegment::Type::ToolCall, QDateTime::currentMSecsSinceEpoch(), this);
segment->setName(name);
segment->setToolCallId(toolCallId);
segment->setStatus(LlmSegment::Status::Running);
// Awaiting user approval until LlmClient::approveTools() promotes it
// to Running; denyTools()/endStream() finalize it as an error.
segment->setStatus(LlmSegment::Status::Pending);
segment->begin();
addSegment(segment);
Q_EMIT toolStateChanged();