Files
z-bar-qt/Plugins/ZShell/Llm/generation.cpp
T

127 lines
3.0 KiB
C++

#include "generation.hpp"
#include <QDateTime>
namespace ZShell::llm {
ChatGeneration::ChatGeneration(qint64 timestamp, QObject* parent)
: QObject(parent), m_timestamp(timestamp) {
m_timer.setParent(this);
m_timer.setInterval(500);
m_timer.setTimerType(Qt::CoarseTimer);
connect(&m_timer, &QTimer::timeout, this, [this]() {
if (!reasoningInFlight() && !contentInFlight()) {
m_timer.stop();
return;
}
Q_EMIT elapsedMsChanged();
});
}
qint64 ChatGeneration::reasoningElapsedMs() const {
if (m_reasoningStartedAt <= 0)
return 0;
const qint64 end = m_reasoningEndedAt > 0
? m_reasoningEndedAt
: QDateTime::currentMSecsSinceEpoch();
return end - m_reasoningStartedAt;
}
qint64 ChatGeneration::contentElapsedMs() const {
if (m_contentStartedAt <= 0)
return 0;
const qint64 end = m_contentEndedAt > 0
? m_contentEndedAt
: QDateTime::currentMSecsSinceEpoch();
return end - m_contentStartedAt;
}
void ChatGeneration::updateReasoningActive() {
const bool active = m_streaming && m_content.isEmpty();
if (m_reasoningActive == active)
return;
m_reasoningActive = active;
Q_EMIT reasoningActiveChanged();
}
void ChatGeneration::setContent(const QString& value) {
if (m_content == value)
return;
m_content = value;
Q_EMIT contentChanged();
}
void ChatGeneration::setReasoning(const QString& value) {
if (m_reasoning == value)
return;
m_reasoning = value;
Q_EMIT reasoningChanged();
}
void ChatGeneration::appendContent(const QString& piece) {
if (piece.isEmpty())
return;
if (m_content.isEmpty()) {
const qint64 now = QDateTime::currentMSecsSinceEpoch();
if (reasoningInFlight())
m_reasoningEndedAt = now;
m_contentStartedAt = now;
if (!m_timer.isActive())
m_timer.start();
}
m_content += piece;
Q_EMIT contentChanged();
updateReasoningActive();
Q_EMIT elapsedMsChanged();
}
void ChatGeneration::appendReasoning(const QString& piece) {
if (piece.isEmpty())
return;
if (m_reasoning.isEmpty()) {
if (m_reasoningStartedAt <= 0)
m_reasoningStartedAt = QDateTime::currentMSecsSinceEpoch();
if (!m_timer.isActive())
m_timer.start();
}
m_reasoning += piece;
Q_EMIT reasoningChanged();
updateReasoningActive();
Q_EMIT elapsedMsChanged();
}
void ChatGeneration::setElapsedMs(qint64 reasoningMs, qint64 contentMs) {
if (reasoningMs > 0) {
m_reasoningStartedAt = m_timestamp;
m_reasoningEndedAt = m_timestamp + reasoningMs;
}
if (contentMs > 0) {
m_contentStartedAt = m_timestamp;
m_contentEndedAt = m_timestamp + contentMs;
}
}
void ChatGeneration::setStreaming(bool value) {
if (m_streaming == value)
return;
m_streaming = value;
Q_EMIT streamingChanged();
if (value) {
if (m_reasoningStartedAt <= 0)
m_reasoningStartedAt = QDateTime::currentMSecsSinceEpoch();
if (!m_timer.isActive())
m_timer.start();
} else {
const qint64 now = QDateTime::currentMSecsSinceEpoch();
if (reasoningInFlight())
m_reasoningEndedAt = now;
if (contentInFlight())
m_contentEndedAt = now;
m_timer.stop();
Q_EMIT elapsedMsChanged();
}
updateReasoningActive();
}
} // namespace ZShell::llm