From 3c30310539d10a82406dbb9a2a5becbc6b315d11 Mon Sep 17 00:00:00 2001 From: Adam Treat Date: Mon, 8 May 2023 05:52:57 -0400 Subject: [PATCH] Convert the old format properly. --- chat.cpp | 12 ++++++------ chat.h | 4 ++-- chatlistmodel.cpp | 12 ++++++------ chatllm.cpp | 14 +++++++++----- chatllm.h | 4 ++-- chatmodel.h | 4 ++-- 6 files changed, 27 insertions(+), 23 deletions(-) diff --git a/chat.cpp b/chat.cpp index 2350949f..6d1782ff 100644 --- a/chat.cpp +++ b/chat.cpp @@ -179,21 +179,21 @@ void Chat::handleModelNameChanged() emit modelNameChanged(); } -bool Chat::serialize(QDataStream &stream) const +bool Chat::serialize(QDataStream &stream, int version) const { stream << m_creationDate; stream << m_id; stream << m_name; stream << m_userName; stream << m_savedModelName; - if (!m_llmodel->serialize(stream)) + if (!m_llmodel->serialize(stream, version)) return false; - if (!m_chatModel->serialize(stream)) + if (!m_chatModel->serialize(stream, version)) return false; return stream.status() == QDataStream::Ok; } -bool Chat::deserialize(QDataStream &stream) +bool Chat::deserialize(QDataStream &stream, int version) { stream >> m_creationDate; stream >> m_id; @@ -202,9 +202,9 @@ bool Chat::deserialize(QDataStream &stream) stream >> m_userName; emit nameChanged(); stream >> m_savedModelName; - if (!m_llmodel->deserialize(stream)) + if (!m_llmodel->deserialize(stream, version)) return false; - if (!m_chatModel->deserialize(stream)) + if (!m_chatModel->deserialize(stream, version)) return false; emit chatModelChanged(); return stream.status() == QDataStream::Ok; diff --git a/chat.h b/chat.h index fa5db003..8e46b311 100644 --- a/chat.h +++ b/chat.h @@ -55,8 +55,8 @@ public: void reloadModel(); qint64 creationDate() const { return m_creationDate; } - bool serialize(QDataStream &stream) const; - bool deserialize(QDataStream &stream); + bool serialize(QDataStream &stream, int version) const; + bool deserialize(QDataStream &stream, int version); QList modelList() const; diff --git a/chatlistmodel.cpp b/chatlistmodel.cpp index cd0ef0c5..c2e81166 100644 --- a/chatlistmodel.cpp +++ b/chatlistmodel.cpp @@ -5,7 +5,7 @@ #include #define CHAT_FORMAT_MAGIC 0xF5D553CC -#define CHAT_FORMAT_VERSION 100 +#define CHAT_FORMAT_VERSION 1 ChatListModel::ChatListModel(QObject *parent) : QAbstractListModel(parent) @@ -70,7 +70,7 @@ void ChatListModel::saveChats() const out.setVersion(QDataStream::Qt_6_2); qDebug() << "serializing chat" << fileName; - if (!chat->serialize(out)) { + if (!chat->serialize(out, CHAT_FORMAT_VERSION)) { qWarning() << "ERROR: Couldn't serialize chat to file:" << file.fileName(); file.remove(); } @@ -169,6 +169,7 @@ void ChatsRestoreThread::run() } QDataStream in(&file); + qint32 version = 0; if (!f.oldFile) { // Read and check the header quint32 magic; @@ -179,14 +180,13 @@ void ChatsRestoreThread::run() } // Read the version - qint32 version; in >> version; - if (version < 100) { + if (version < 1) { qWarning() << "ERROR: Chat file has non supported version:" << file.fileName(); continue; } - if (version <= 100) + if (version <= 1) in.setVersion(QDataStream::Qt_6_2); } @@ -194,7 +194,7 @@ void ChatsRestoreThread::run() Chat *chat = new Chat; chat->moveToThread(qApp->thread()); - if (!chat->deserialize(in)) { + if (!chat->deserialize(in, version)) { qWarning() << "ERROR: Couldn't deserialize chat from file:" << file.fileName(); file.remove(); } else { diff --git a/chatllm.cpp b/chatllm.cpp index 7b2d89d6..1700c04f 100644 --- a/chatllm.cpp +++ b/chatllm.cpp @@ -368,7 +368,7 @@ bool ChatLLM::handleNameRecalculate(bool isRecalc) return true; } -bool ChatLLM::serialize(QDataStream &stream) +bool ChatLLM::serialize(QDataStream &stream, int version) { stream << response(); stream << generatedName(); @@ -388,7 +388,7 @@ bool ChatLLM::serialize(QDataStream &stream) return stream.status() == QDataStream::Ok; } -bool ChatLLM::deserialize(QDataStream &stream) +bool ChatLLM::deserialize(QDataStream &stream, int version) { QString response; stream >> response; @@ -407,9 +407,13 @@ bool ChatLLM::deserialize(QDataStream &stream) stream >> tokensSize; m_ctx.tokens.resize(tokensSize); stream.readRawData(reinterpret_cast(m_ctx.tokens.data()), tokensSize * sizeof(int)); - QByteArray compressed; - stream >> compressed; - m_state = qUncompress(compressed); + if (version > 0) { + QByteArray compressed; + stream >> compressed; + m_state = qUncompress(compressed); + } else { + stream >> m_state; + } #if defined(DEBUG) qDebug() << "chatllm deserialize" << m_chat->id(); #endif diff --git a/chatllm.h b/chatllm.h index dc1260b8..8a2732d1 100644 --- a/chatllm.h +++ b/chatllm.h @@ -35,8 +35,8 @@ public: QString generatedName() const { return QString::fromStdString(m_nameResponse); } - bool serialize(QDataStream &stream); - bool deserialize(QDataStream &stream); + bool serialize(QDataStream &stream, int version); + bool deserialize(QDataStream &stream, int version); public Q_SLOTS: bool prompt(const QString &prompt, const QString &prompt_template, int32_t n_predict, diff --git a/chatmodel.h b/chatmodel.h index f3e59fa3..e3c01a9a 100644 --- a/chatmodel.h +++ b/chatmodel.h @@ -210,7 +210,7 @@ public: int count() const { return m_chatItems.size(); } - bool serialize(QDataStream &stream) const + bool serialize(QDataStream &stream, int version) const { stream << count(); for (auto c : m_chatItems) { @@ -227,7 +227,7 @@ public: return stream.status() == QDataStream::Ok; } - bool deserialize(QDataStream &stream) + bool deserialize(QDataStream &stream, int version) { int size; stream >> size;