84 lines
2.0 KiB
C++
84 lines
2.0 KiB
C++
#ifndef CHATBLOCKMODEL_H
|
|
#define CHATBLOCKMODEL_H
|
|
|
|
#include <QAbstractListModel>
|
|
#include <QList>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
#include <QRegularExpression>
|
|
|
|
namespace KSyntaxHighlighting {
|
|
class Repository;
|
|
}
|
|
|
|
struct ChatBlock {
|
|
enum Type {
|
|
User,
|
|
Assistant,
|
|
Reasoning,
|
|
Call,
|
|
Tool,
|
|
Error,
|
|
Info,
|
|
Retry,
|
|
Interrupt,
|
|
Default
|
|
};
|
|
|
|
Type type = Default;
|
|
QString name;
|
|
QString content;
|
|
QString renderedHtml; // Markdown-rendered block content
|
|
};
|
|
|
|
class ChatBlockModel : public QAbstractListModel
|
|
{
|
|
Q_OBJECT
|
|
Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
|
|
|
|
public:
|
|
// Two-state parser for [[[blockname]]] ... [[[end]]] blocks.
|
|
enum class State {
|
|
BlockStart, // Waiting for a block header
|
|
BlockEnd // Collecting Markdown content until [[[end]]]
|
|
};
|
|
|
|
enum Roles {
|
|
BlockTypeRole = Qt::UserRole + 1,
|
|
BlockNameRole,
|
|
ContentRole,
|
|
RenderedHtmlRole,
|
|
};
|
|
|
|
explicit ChatBlockModel(QObject *parent = nullptr);
|
|
~ChatBlockModel() override;
|
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
|
QHash<int, QByteArray> roleNames() const override;
|
|
|
|
Q_INVOKABLE void appendData(const QString &text);
|
|
Q_INVOKABLE void reset();
|
|
Q_INVOKABLE void trimFront(int keepCount);
|
|
|
|
signals:
|
|
void countChanged();
|
|
|
|
private:
|
|
void processLine(const QString &line);
|
|
void appendContent(const QString &line);
|
|
|
|
QString renderMarkdown(const QString &markdown) const;
|
|
QString highlightCode(const QString &code, const QString &language) const;
|
|
|
|
State m_state = State::BlockStart;
|
|
KSyntaxHighlighting::Repository *m_repo = nullptr;
|
|
|
|
QList<ChatBlock> m_blocks;
|
|
QString m_lineBuffer;
|
|
|
|
static const QRegularExpression s_headerRe;
|
|
static const QRegularExpression s_endRe;
|
|
};
|
|
|
|
#endif // CHATBLOCKMODEL_H
|