-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathllamastorage.cpp
More file actions
578 lines (500 loc) · 19.8 KB
/
llamastorage.cpp
File metadata and controls
578 lines (500 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <QJsonArray>
#include <QJsonDocument>
#include <QLoggingCategory>
#include <QSettings>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <coreplugin/icore.h>
#include "llamastorage.h"
#include "llamatypes.h"
Q_LOGGING_CATEGORY(llamaStorage, "llama.cpp.storage", QtWarningMsg)
namespace LlamaCpp {
static QString serialize(const QList<QVariantMap> &list)
{
QJsonArray arr;
for (const QVariantMap &m : list)
arr.append(QJsonObject::fromVariantMap(m));
return QString::fromUtf8(QJsonDocument(arr).toJson(QJsonDocument::Compact));
}
static QList<QVariantMap> deserializeExtra(const QString &json)
{
QList<QVariantMap> list;
QJsonDocument d = QJsonDocument::fromJson(json.toUtf8());
if (!d.isArray())
return list;
for (const QJsonValue &v : d.array()) {
if (v.isObject())
list.append(v.toObject().toVariantMap());
}
return list;
}
static QString serialize(const TimingReport &tr)
{
QJsonObject obj;
obj["prompt_n"] = tr.prompt_n;
obj["prompt_ms"] = tr.prompt_ms;
obj["predicted_n"] = tr.predicted_n;
obj["predicted_ms"] = tr.predicted_ms;
QJsonDocument doc(obj);
return QString::fromUtf8(doc.toJson(QJsonDocument::Compact));
}
static TimingReport deserializeTimingsReport(const QString &json)
{
TimingReport result;
QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8());
if (!doc.isObject())
return result;
QJsonObject obj = doc.object();
auto getDouble = [&](const QString &key, double &field) {
if (obj.contains(key))
field = obj[key].toDouble();
};
getDouble("prompt_n", result.prompt_n);
getDouble("prompt_ms", result.prompt_ms);
getDouble("predicted_n", result.predicted_n);
getDouble("predicted_ms", result.predicted_ms);
return result;
}
Storage &Storage::instance()
{
static Storage inst;
return inst;
}
Storage::Storage()
{
const Utils::FilePath cacheResourceDir = Core::ICore::cacheResourcePath(".");
cacheResourceDir.ensureWritableDir();
const QString databasePath = Core::ICore::cacheResourcePath("llamacpp.db").path();
qCDebug(llamaStorage) << "Storage path:" << databasePath;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(databasePath);
if (!db.open()) {
qFatal("Failed to open database: %s", qPrintable(db.lastError().text()));
}
// create tables if not exist
QSqlQuery q(db);
if (!q.exec("CREATE TABLE IF NOT EXISTS conversations "
"(id TEXT PRIMARY KEY, lastModified INTEGER, currNode INTEGER, name TEXT)"))
qCCritical(llamaStorage) << "Failed to create table \"conversations\"" << q.lastError();
if (!q.exec("CREATE TABLE IF NOT EXISTS messages "
"(id INTEGER PRIMARY KEY AUTOINCREMENT, convId TEXT, type TEXT, timestamp INTEGER, "
"role TEXT, "
"content TEXT, "
"timings TEXT, "
"extra TEXT, "
"parent INTEGER, "
"children TEXT, "
"FOREIGN KEY(convId) REFERENCES conversations(id))"))
qCCritical(llamaStorage) << "Failed to create table \"messages\"" << q.lastError();
// create indexes for quick lookups
if (!q.exec("CREATE INDEX IF NOT EXISTS idx_messages_convId ON messages(convId)"))
qCCritical(llamaStorage) << "Failed to create table \"idx_messages_convId\""
<< q.lastError();
}
QList<Conversation> Storage::getAllConversations()
{
QVector<Conversation> res;
QSqlQuery q(db);
q.prepare("SELECT * FROM conversations ORDER BY lastModified DESC");
if (!q.exec())
qCWarning(llamaStorage) << "getAllConversations" << q.lastError();
while (q.next()) {
Conversation c;
c.id = q.value("id").toString();
c.lastModified = q.value("lastModified").toLongLong();
c.currNode = q.value("currNode").toLongLong();
c.name = q.value("name").toString();
res.append(c);
}
return res;
}
Conversation Storage::getOneConversation(const QString &convId)
{
QSqlQuery q(db);
q.prepare("SELECT * FROM conversations WHERE id = (:id)");
q.bindValue(":id", convId);
if (!q.exec()) {
qCWarning(llamaStorage) << "getOneConversation" << convId << q.lastError();
return {};
}
if (!q.next())
return {};
Conversation c;
c.id = q.value("id").toString();
c.lastModified = q.value("lastModified").toLongLong();
c.currNode = q.value("currNode").toLongLong();
c.name = q.value("name").toString();
return c;
}
Conversation Storage::createConversation(const QString &name)
{
qint64 now = QDateTime::currentMSecsSinceEpoch() - 1;
QSqlQuery q(db);
q.prepare("INSERT INTO conversations (id,lastModified,currNode,name) "
"VALUES (:id,:lm,:curr,:name)");
QString convId = QString("conv-%1").arg(now);
q.bindValue(":id", convId);
q.bindValue(":lm", now);
q.bindValue(":curr", -1); // Will be updated after root message is created
q.bindValue(":name", name);
if (!q.exec())
qCWarning(llamaStorage) << "createConversation insert into conversations" << q.lastError();
// create root node
q.prepare("INSERT INTO messages "
"(convId,type,timestamp,role,content,timings,extra,parent,children) "
"VALUES (:conv,:type,:ts,:role,:content,:timings,:extra,:parent,:children)");
q.bindValue(":conv", convId);
q.bindValue(":type", "root");
q.bindValue(":ts", now);
q.bindValue(":role", "system");
q.bindValue(":content", "");
q.bindValue(":timings", "");
q.bindValue(":extra", "[]");
q.bindValue(":parent", -1);
q.bindValue(":children", "[]");
if (!q.exec())
qCWarning(llamaStorage) << "createConversation insert into messages" << q.lastError();
// Get the auto-generated root message ID
qint64 rootMsgId = q.lastInsertId().toLongLong();
// Update conversation with actual root message ID
q.prepare("UPDATE conversations SET currNode = (:curr) WHERE id = (:id)");
q.bindValue(":curr", rootMsgId);
q.bindValue(":id", convId);
if (!q.exec())
qCWarning(llamaStorage) << "createConversation update currNode" << q.lastError();
Conversation c;
c.id = convId;
c.lastModified = now;
c.currNode = rootMsgId;
c.name = name;
emit conversationCreated(convId);
return c;
}
void Storage::renameConversation(const QString &convId, const QString &name)
{
QSqlQuery q(db);
q.prepare("UPDATE conversations SET name = (:name), lastModified = (:lm) WHERE id = (:id)");
q.bindValue(":name", name);
q.bindValue(":lm", QDateTime::currentMSecsSinceEpoch());
q.bindValue(":id", convId);
if (!q.exec())
qCWarning(llamaStorage) << "updateConversationName" << q.lastError();
emit conversationRenamed(convId);
}
void Storage::deleteConversation(const QString &convId)
{
QSqlQuery q(db);
q.prepare("DELETE FROM conversations WHERE id = (:id)");
q.bindValue(":id", convId);
if (!q.exec())
qCWarning(llamaStorage) << "removeConversation from conversations" << q.lastError();
q.prepare("DELETE FROM messages WHERE convId = (:id)");
q.bindValue(":id", convId);
if (!q.exec())
qCWarning(llamaStorage) << "removeConversation from messages" << q.lastError();
emit conversationDeleted(convId);
}
QVector<Message> Storage::getMessages(const QString &convId)
{
QVector<Message> res;
QSqlQuery q(db);
q.prepare("SELECT * FROM messages WHERE convId = (:id) ORDER BY timestamp ASC");
q.bindValue(":id", convId);
if (!q.exec()) {
qCWarning(llamaStorage) << "getMessages select from message" << convId << q.lastError();
return res;
}
while (q.next()) {
Message m;
m.id = q.value("id").toLongLong();
m.convId = q.value("convId").toString();
m.type = q.value("type").toString();
m.timestamp = q.value("timestamp").toLongLong();
m.role = q.value("role").toString();
m.content = q.value("content").toString();
m.timings = deserializeTimingsReport(q.value("timings").toString());
m.extra = deserializeExtra(q.value("extra").toString());
m.parent = q.value("parent").toLongLong();
QJsonArray arr = QJsonDocument::fromJson(q.value("children").toString().toUtf8()).array();
for (const QJsonValue &v : std::as_const(arr))
m.children.append(v.toInteger());
res.append(m);
}
return res;
}
void Storage::appendMsg(Message &msg, qint64 parentNodeId)
{
QSqlQuery q(db);
db.transaction();
// Check if the parent node actually exists.
// If not, the conversation's currNode is likely a "ghost" from a deleted branch.
QSqlQuery qCheck(db);
qCheck.prepare("SELECT id FROM messages WHERE id = :pid");
qCheck.bindValue(":pid", parentNodeId);
if (!qCheck.exec() || !qCheck.next()) {
qCWarning(llamaStorage) << "appendMsg: parent node" << parentNodeId
<< "not found. Recovering by attaching to conversation root.";
// Try to find the root message for this conversation to re-anchor the thread
QSqlQuery qRoot(db);
qRoot.prepare("SELECT id FROM messages WHERE convId = :conv AND type = 'root'");
qRoot.bindValue(":conv", msg.convId);
if (qRoot.exec() && qRoot.next()) {
parentNodeId = qRoot.value(0).toLongLong();
} else {
qCWarning(llamaStorage)
<< "appendMsg: Failed to find root for conversation" << msg.convId << ". Aborting.";
db.rollback();
return;
}
}
// Insert new message using the (potentially updated) parentNodeId
q.prepare("INSERT INTO messages "
"(convId,type,timestamp,role,content,timings,extra,parent,children) "
"VALUES (:conv,:type,:ts,:role,:content,:timings,:extra,:parent,:children)");
q.bindValue(":conv", msg.convId);
q.bindValue(":type", msg.type);
q.bindValue(":ts", msg.timestamp);
q.bindValue(":role", msg.role);
q.bindValue(":content", msg.content);
q.bindValue(":timings", serialize(msg.timings));
q.bindValue(":extra", serialize(msg.extra));
q.bindValue(":parent", parentNodeId);
q.bindValue(":children", "[]");
if (!q.exec()) {
qCWarning(llamaStorage) << "appendMsg: Failed to insert messages" << parentNodeId
<< q.lastError();
db.rollback();
return;
}
// Get the auto-generated ID
qint64 pendingId = msg.role == "assistant" ? msg.id : -1;
msg.id = q.lastInsertId().toLongLong();
// Update parent's children list
q.prepare("SELECT children FROM messages WHERE id = :pid");
q.bindValue(":pid", parentNodeId);
if (!q.exec() || !q.next()) {
qCWarning(llamaStorage) << "appendMsg: Failed to select children for parent node"
<< parentNodeId << q.lastError();
db.rollback();
return;
}
QJsonArray arr = QJsonDocument::fromJson(q.value(0).toString().toUtf8()).array();
arr.append(msg.id);
q.prepare("UPDATE messages SET children = (:arr) WHERE id = (:pid)");
q.bindValue(":arr", QJsonDocument(arr).toJson(QJsonDocument::Compact));
q.bindValue(":pid", parentNodeId);
if (!q.exec()) {
qCWarning(llamaStorage) << "appendMsg: Failed update children messages" << q.lastError();
db.rollback();
return;
}
// Update conversation lastModified & currNode
// This effectively "heals" the dangling pointer in the conversations table
q.prepare(
"UPDATE conversations SET lastModified = (:lm), currNode = (:node) WHERE id = (:conv)");
q.bindValue(":lm", QDateTime::currentMSecsSinceEpoch());
q.bindValue(":node", msg.id);
q.bindValue(":conv", msg.convId);
if (!q.exec())
qCWarning(llamaStorage) << "appendMsg: Failed to update conversations" << q.lastError();
db.commit();
emit messageAppended(msg, pendingId);
}
QVector<Message> Storage::filterByLeafNodeId(const QVector<Message> &msgs,
qint64 leafNodeId,
bool includeRoot)
{
QHash<qint64, Message> map;
for (const Message &m : msgs)
map.insert(m.id, m);
QVector<Message> res;
Message *curr = map.contains(leafNodeId) ? &map[leafNodeId] : nullptr;
if (!curr) {
// no exact match – pick the latest by timestamp
qint64 latest = -1;
for (auto it = map.constBegin(); it != map.constEnd(); ++it)
if (it.value().timestamp > latest) {
curr = const_cast<Message *>(&it.value());
latest = it.value().timestamp;
}
}
while (curr) {
if (curr->type != "root" || (curr->type == "root" && includeRoot))
res.append(*curr);
curr = map.contains(curr->parent) ? &map[curr->parent] : nullptr;
}
std::sort(res.begin(), res.end(), [](const Message &a, const Message &b) {
return a.timestamp < b.timestamp;
});
return res;
}
bool Storage::updateMessageExtra(const Message &msg, const QList<QVariantMap> &extra)
{
// Serialize the list to the same JSON format we use everywhere else.
const QString json = serialize(extra);
QSqlQuery q(db);
// We use an explicit transaction – if anything fails we roll back.
if (!db.transaction()) {
qCWarning(llamaStorage) << "updateMessageExtra: cannot start transaction" << db.lastError();
return false;
}
q.prepare("UPDATE messages "
"SET extra = :extra "
"WHERE id = :id");
q.bindValue(":extra", json);
q.bindValue(":id", msg.id);
if (!q.exec()) {
qCWarning(llamaStorage) << "updateMessageExtra: UPDATE failed for id" << msg.id
<< q.lastError();
db.rollback();
return false;
}
// Commit the transaction – this will also release any locks.
if (!db.commit()) {
qCWarning(llamaStorage) << "updateMessageExtra: commit failed" << db.lastError();
db.rollback();
return false;
}
// Let everybody know the extra field changed.
emit messageExtraUpdated(msg, extra);
return true;
}
bool Storage::updateMessageContent(const Message &msg)
{
// Serialize the timings – content is a plain string.
const QString timingsJson = serialize(msg.timings);
QSqlQuery q(db);
if (!db.transaction()) {
qCWarning(llamaStorage) << "updateMessageContent: cannot start transaction"
<< db.lastError();
return false;
}
q.prepare("UPDATE messages "
"SET content = :content, timings = :timings "
"WHERE id = :id");
q.bindValue(":content", msg.content);
q.bindValue(":timings", timingsJson);
q.bindValue(":id", msg.id);
if (!q.exec()) {
qCWarning(llamaStorage) << "updateMessageContent: UPDATE failed for id" << msg.id
<< q.lastError();
db.rollback();
return false;
}
if (!db.commit()) {
qCWarning(llamaStorage) << "updateMessageContent: commit failed" << db.lastError();
db.rollback();
return false;
}
emit messageContentUpdated(msg);
return true;
}
bool Storage::deleteMessageBranch(qint64 msgId)
{
if (!db.transaction()) {
qCWarning(llamaStorage) << "deleteMessageBranch: Failed to start transaction:"
<< db.lastError().text();
return false;
}
QVector<qint64> idsToDelete;
QVector<qint64> stack;
stack.push_back(msgId);
// Find the parent of the branch root to update its children list later
qint64 rootParentId = -1;
QSqlQuery qParent(db);
qParent.prepare("SELECT parent FROM messages WHERE id = :id");
qParent.bindValue(":id", msgId);
if (!qParent.exec()) {
qCWarning(llamaStorage) << "deleteMessageBranch: Failed to find parent for root msgId"
<< msgId << ":" << qParent.lastError().text();
db.rollback();
return false;
} else if (qParent.next()) {
rootParentId = qParent.value(0).toLongLong();
}
// Collect all IDs in the branch using the 'children' column (DFS)
while (!stack.isEmpty()) {
qint64 currentId = stack.takeLast();
idsToDelete.append(currentId);
QSqlQuery qChild(db);
qChild.prepare("SELECT children FROM messages WHERE id = :id");
qChild.bindValue(":id", currentId);
if (!qChild.exec()) {
qCWarning(llamaStorage) << "deleteMessageBranch: Failed to fetch children for msgId"
<< currentId << ":" << qChild.lastError().text();
db.rollback();
return false;
} else if (qChild.next()) {
QString childrenJson = qChild.value(0).toString();
QJsonArray arr = QJsonDocument::fromJson(childrenJson.toUtf8()).array();
for (const QJsonValue &v : arr) {
stack.push_back(v.toInteger());
}
}
}
// If there is a parent, remove the branch root from the parent's children list
if (rootParentId >= 0) {
QSqlQuery qUpdateParent(db);
qUpdateParent.prepare("SELECT children FROM messages WHERE id = :pid");
qUpdateParent.bindValue(":pid", rootParentId);
if (!qUpdateParent.exec()) {
qCWarning(llamaStorage)
<< "deleteMessageBranch: Failed to fetch parent's children (parentId:"
<< rootParentId << "):" << qUpdateParent.lastError().text();
db.rollback();
return false;
} else if (qUpdateParent.next()) {
QJsonArray children
= QJsonDocument::fromJson(qUpdateParent.value(0).toString().toUtf8()).array();
// Remove the target msgId from the parent's array
bool removed = false;
for (int i = 0; i < children.size(); ++i) {
if (children[i].toInteger() == msgId) {
children.removeAt(i);
removed = true;
break;
}
}
if (removed) {
qUpdateParent.prepare("UPDATE messages SET children = :c WHERE id = :pid");
qUpdateParent.bindValue(":c",
QJsonDocument(children).toJson(QJsonDocument::Compact));
qUpdateParent.bindValue(":pid", rootParentId);
if (!qUpdateParent.exec()) {
qCWarning(llamaStorage)
<< "deleteMessageBranch: Failed to update parent's children list (parentId:"
<< rootParentId << "):" << qUpdateParent.lastError().text();
db.rollback();
return false;
}
} else {
qCWarning(llamaStorage)
<< "deleteMessageBranch: msgId" << msgId << "not found in parent"
<< rootParentId << "children list.";
// We don't necessarily rollback here as it might be a data inconsistency,
// but we log it.
}
}
}
// Delete all messages in the branch
for (qint64 id : idsToDelete) {
QSqlQuery qDel(db);
qDel.prepare("DELETE FROM messages WHERE id = :id");
qDel.bindValue(":id", id);
if (!qDel.exec()) {
qCWarning(llamaStorage) << "deleteMessageBranch: Failed to delete msgId" << id << ":"
<< qDel.lastError().text();
db.rollback();
return false;
}
}
if (!db.commit()) {
qCWarning(llamaStorage) << "deleteMessageBranch: Failed to commit transaction:"
<< db.lastError().text();
db.rollback();
return false;
}
return true;
}
} // namespace LlamaCpp