better chat list view + anchor to bottom. sqlite db for chats + LIFO-ordered

This commit is contained in:
2026-08-23 16:37:11 +02:00
parent 37c6a9986e
commit aae3757daf
36 changed files with 2653 additions and 1459 deletions
+48 -52
View File
@@ -1,27 +1,29 @@
#pragma once
#include "generation.hpp"
#include <QList>
#include <QObject>
#include <QString>
#include <QTimer>
#include <QtQml>
namespace ZShell {
class Chat;
namespace ZShell::llm {
class ChatMessage : public QObject {
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("Chat messages are created by the Chat singleton")
Q_PROPERTY(Role role READ role NOTIFY roleChanged)
Q_PROPERTY(QString content READ content NOTIFY contentChanged)
Q_PROPERTY(QString reasoning READ reasoning NOTIFY reasoningChanged)
Q_PROPERTY(bool reasoningActive READ reasoningActive NOTIFY reasoningActiveChanged)
Q_PROPERTY(qint64 reasoningElapsedMs READ reasoningElapsedMs NOTIFY elapsedMsChanged)
Q_PROPERTY(qint64 contentElapsedMs READ contentElapsedMs NOTIFY elapsedMsChanged)
Q_PROPERTY(bool streaming READ streaming NOTIFY streamingChanged)
Q_PROPERTY(Role role READ role CONSTANT)
Q_PROPERTY(qint64 timestamp READ timestamp CONSTANT)
Q_PROPERTY(int generationCount READ generationCount NOTIFY generationsChanged)
Q_PROPERTY(
QList<ZShell::llm::ChatGeneration*> generations READ generations
NOTIFY generationsChanged)
Q_PROPERTY(
ZShell::llm::ChatGeneration* activeGeneration READ activeGeneration
NOTIFY activeGenerationChanged)
Q_PROPERTY(int activeGenerationIndex READ activeGenerationIndex NOTIFY activeGenerationChanged)
public:
enum class Role : int {
@@ -31,57 +33,51 @@ class ChatMessage : public QObject {
Q_ENUM(Role)
explicit ChatMessage(
Role role,
const QString& content,
qint64 timestamp,
QObject* parent = nullptr);
Role role, qint64 timestamp, QObject* parent = nullptr);
[[nodiscard]] Role role() const { return m_role; }
[[nodiscard]] QString content() const { return m_content; }
[[nodiscard]] QString reasoning() const { return m_reasoning; }
[[nodiscard]] bool reasoningActive() const { return m_reasoningActive; }
[[nodiscard]] qint64 reasoningElapsedMs() const;
[[nodiscard]] qint64 contentElapsedMs() const;
[[nodiscard]] bool streaming() const { return m_streaming; }
[[nodiscard]] qint64 timestamp() const { return m_timestamp; }
[[nodiscard]] int generationCount() const {
return static_cast<int>(m_generations.size());
}
[[nodiscard]] QList<ChatGeneration*> generations() const {
return m_generations;
}
[[nodiscard]] ChatGeneration* activeGeneration() const {
return generation(m_active);
}
[[nodiscard]] int activeGenerationIndex() const { return m_active; }
[[nodiscard]] ChatGeneration* generation(int index) const {
if (index < 0 || index >= m_generations.size())
return nullptr;
return m_generations.at(index);
}
void appendContent(const QString& piece);
void appendReasoning(const QString& piece);
void setReasoning(const QString& value);
void setElapsedMs(qint64 reasoningMs, qint64 contentMs);
void setStreaming(bool value);
Q_INVOKABLE void setActiveGeneration(int index);
Q_INVOKABLE void edit(const QString& newContent);
Q_INVOKABLE void retry();
Q_INVOKABLE void generate();
ChatGeneration* addGeneration(
qint64 timestamp,
const QString& content,
const QString& reasoning,
qint64 reasoningElapsedMs,
qint64 contentElapsedMs);
ChatGeneration* appendGeneration(qint64 timestamp);
void removeGeneration(ChatGeneration* generation);
Q_SIGNALS:
void roleChanged();
void contentChanged();
void reasoningChanged();
void reasoningActiveChanged();
void elapsedMsChanged();
void streamingChanged();
void generationsChanged();
void activeGenerationChanged();
private:
void updateReasoningActive();
void setActiveInternal(int index);
[[nodiscard]] bool reasoningInFlight() const {
return m_reasoningStartedAt > 0 && m_reasoningEndedAt <= 0;
}
[[nodiscard]] bool contentInFlight() const {
return m_contentStartedAt > 0 && m_contentEndedAt <= 0;
}
QTimer m_timer;
Role m_role;
QString m_content;
QString m_reasoning;
bool m_reasoningActive = false;
bool m_streaming = false;
qint64 m_timestamp;
qint64 m_reasoningStartedAt = 0;
qint64 m_reasoningEndedAt = 0;
qint64 m_contentStartedAt = 0;
qint64 m_contentEndedAt = 0;
friend class Chat;
QList<ChatGeneration*> m_generations;
int m_active = -1;
};
} // namespace ZShell
} // namespace ZShell::llm