84 lines
2.2 KiB
C++
84 lines
2.2 KiB
C++
#ifndef CHATBLOCKMODEL_H
|
|
#define CHATBLOCKMODEL_H
|
|
|
|
#include <QAbstractListModel>
|
|
#include <QList>
|
|
#include <QString>
|
|
#include <QRegularExpression>
|
|
|
|
struct ChatBlock {
|
|
enum Type {
|
|
User,
|
|
Assistant,
|
|
Reasoning,
|
|
Call,
|
|
Tool,
|
|
Error,
|
|
Info,
|
|
Retry,
|
|
Interrupt,
|
|
Default
|
|
};
|
|
|
|
Type type = Default;
|
|
QString name;
|
|
QString content;
|
|
QString renderedHtml; // non-empty when block contains rendered fenced code/diff
|
|
};
|
|
|
|
class ChatBlockModel : public QAbstractListModel
|
|
{
|
|
Q_OBJECT
|
|
Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
|
|
|
|
public:
|
|
enum Roles {
|
|
BlockTypeRole = Qt::UserRole + 1,
|
|
BlockNameRole,
|
|
ContentRole,
|
|
RenderedHtmlRole,
|
|
};
|
|
|
|
explicit ChatBlockModel(QObject *parent = nullptr);
|
|
|
|
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);
|
|
|
|
// Fence state machine
|
|
enum FenceState { NoFence, InFence };
|
|
FenceState m_fenceState = NoFence;
|
|
QString m_fenceLang;
|
|
QString m_fenceBuffer;
|
|
|
|
// Block state machine — tracks whether we're inside a [[[type]]]...[[[end]]] block.
|
|
// Content lines outside a block are ignored, making the format robust against
|
|
// accidental header-like content in the wild.
|
|
bool m_inBlock = false;
|
|
|
|
// Syntax highlighting (shared with Kate plugin pattern)
|
|
QString highlightCode(const QString &code, const QString &language) const;
|
|
QString renderDiff(const QString &diff) const;
|
|
QString escapeHtml(const QString &text) const;
|
|
|
|
QList<ChatBlock> m_blocks;
|
|
QString m_lineBuffer;
|
|
|
|
static const QRegularExpression s_headerRe;
|
|
static const QRegularExpression s_endRe;
|
|
static const QRegularExpression s_interruptOpenRe;
|
|
static const QRegularExpression s_interruptCloseRe;
|
|
};
|
|
|
|
#endif // CHATBLOCKMODEL_H
|