| 202 | } |
| 203 | |
| 204 | bool ChatCompressor::createCompressedChatFile( |
| 205 | const QString &sourcePath, const QString &destPath, const QString &summary) |
| 206 | { |
| 207 | QFile sourceFile(sourcePath); |
| 208 | if (!sourceFile.open(QIODevice::ReadOnly)) { |
| 209 | LOG_MESSAGE(QString("Failed to open source chat file: %1").arg(sourcePath)); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | QJsonParseError parseError; |
| 214 | QJsonDocument doc = QJsonDocument::fromJson(sourceFile.readAll(), &parseError); |
| 215 | sourceFile.close(); |
| 216 | |
| 217 | if (doc.isNull() || !doc.isObject()) { |
| 218 | LOG_MESSAGE(QString("Invalid JSON in chat file: %1 (Error: %2)") |
| 219 | .arg(sourcePath, parseError.errorString())); |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | QJsonObject root = doc.object(); |
| 224 | |
| 225 | QJsonObject summaryMessage; |
| 226 | summaryMessage["role"] = "assistant"; |
| 227 | summaryMessage["content"] = QString("# Chat Summary\n\n%1").arg(summary); |
| 228 | summaryMessage["id"] = QUuid::createUuid().toString(QUuid::WithoutBraces); |
| 229 | summaryMessage["isRedacted"] = false; |
| 230 | summaryMessage["attachments"] = QJsonArray(); |
| 231 | summaryMessage["images"] = QJsonArray(); |
| 232 | |
| 233 | root["messages"] = QJsonArray{summaryMessage}; |
| 234 | root["compressedFrom"] = sourcePath; |
| 235 | root["compressedAt"] = QDateTime::currentDateTime().toString(Qt::ISODate); |
| 236 | |
| 237 | if (QFile::exists(destPath)) |
| 238 | QFile::remove(destPath); |
| 239 | |
| 240 | QFile destFile(destPath); |
| 241 | if (!destFile.open(QIODevice::WriteOnly)) { |
| 242 | LOG_MESSAGE(QString("Failed to create compressed chat file: %1").arg(destPath)); |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | destFile.write(QJsonDocument(root).toJson(QJsonDocument::Indented)); |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | void ChatCompressor::connectProviderSignals() |
| 251 | { |