File indexing completed on 2025-01-05 05:18:56

0001 // SPDX-FileCopyrightText: 2023 Loren Burkholder <computersemiexpert@outlook.com>
0002 //
0003 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #pragma once
0006 
0007 #include <QAbstractListModel>
0008 #include <QtQmlIntegration>
0009 
0010 #include "KLLMInterface.h"
0011 #include "KLLMReply.h"
0012 
0013 class ChatModel : public QAbstractListModel
0014 {
0015     Q_OBJECT
0016 
0017     QML_ELEMENT
0018 
0019     Q_PROPERTY(KLLMCore::KLLMInterface *llm READ llm CONSTANT FINAL)
0020     Q_PROPERTY(bool replyInProgress READ replyInProgress NOTIFY replyInProgressChanged FINAL)
0021 
0022 public:
0023     enum Roles {
0024         MessageRole,
0025         SenderRole,
0026         FinishedRole,
0027     };
0028 
0029     enum Sender {
0030         LLM,
0031         User,
0032     };
0033     Q_ENUM(Sender)
0034 
0035     explicit ChatModel(QObject *parent = nullptr);
0036     ~ChatModel() override;
0037 
0038     [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
0039     [[nodiscard]] int rowCount(const QModelIndex & = {}) const override;
0040     [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
0041 
0042     [[nodiscard]] KLLMCore::KLLMInterface *llm() const;
0043     [[nodiscard]] bool replyInProgress() const;
0044 
0045 public Q_SLOTS:
0046     void sendMessage(const QString &message);
0047     void resetConversation();
0048 
0049 Q_SIGNALS:
0050     void replyInProgressChanged();
0051 
0052 private:
0053     struct ChatMessage {
0054         bool inProgress = false;
0055         QString content;
0056         Sender sender;
0057         KLLMCore::KLLMReply *llmReply = nullptr;
0058         KLLMCore::KLLMContext context;
0059     };
0060 
0061     QList<ChatMessage> m_messages;
0062     KLLMCore::KLLMInterface *const m_llm;
0063 
0064     QMultiHash<KLLMCore::KLLMReply *, QMetaObject::Connection> m_connections;
0065 };