@@ -29,7 +29,6 @@ struct ChatItem
29
29
Q_PROPERTY (bool thumbsUpState MEMBER thumbsUpState)
30
30
Q_PROPERTY (bool thumbsDownState MEMBER thumbsDownState)
31
31
Q_PROPERTY (QList<SourceExcerpt> sources MEMBER sources)
32
- Q_PROPERTY (QList<SourceExcerpt> consolidatedSources MEMBER consolidatedSources)
33
32
34
33
public:
35
34
// TODO: Maybe we should include the model name here as well as timestamp?
@@ -39,7 +38,6 @@ struct ChatItem
39
38
QString prompt;
40
39
QString newResponse;
41
40
QList<SourceExcerpt> sources;
42
- QList<SourceExcerpt> consolidatedSources;
43
41
bool currentResponse = false ;
44
42
bool stopped = false ;
45
43
bool thumbsUpState = false ;
@@ -65,8 +63,7 @@ class ChatModel : public QAbstractListModel
65
63
StoppedRole,
66
64
ThumbsUpStateRole,
67
65
ThumbsDownStateRole,
68
- SourcesRole,
69
- ConsolidatedSourcesRole
66
+ SourcesRole
70
67
};
71
68
72
69
int rowCount (const QModelIndex &parent = QModelIndex ()) const override
@@ -102,8 +99,6 @@ class ChatModel : public QAbstractListModel
102
99
return item.thumbsDownState ;
103
100
case SourcesRole:
104
101
return QVariant::fromValue (item.sources );
105
- case ConsolidatedSourcesRole:
106
- return QVariant::fromValue (item.consolidatedSources );
107
102
}
108
103
109
104
return QVariant ();
@@ -122,7 +117,6 @@ class ChatModel : public QAbstractListModel
122
117
roles[ThumbsUpStateRole] = " thumbsUpState" ;
123
118
roles[ThumbsDownStateRole] = " thumbsDownState" ;
124
119
roles[SourcesRole] = " sources" ;
125
- roles[ConsolidatedSourcesRole] = " consolidatedSources" ;
126
120
return roles;
127
121
}
128
122
@@ -200,34 +194,17 @@ class ChatModel : public QAbstractListModel
200
194
}
201
195
}
202
196
203
- QList<SourceExcerpt> consolidateSources (const QList<SourceExcerpt> &sources) {
204
- QMap<QString, SourceExcerpt> groupedData;
205
- for (const SourceExcerpt &info : sources) {
206
- QString key = !info.file .isEmpty () ? info.file : info.url ;
207
- if (groupedData.contains (key)) {
208
- groupedData[key].text += " \n ---\n " + info.text ;
209
- } else {
210
- groupedData[key] = info;
211
- }
212
- }
213
- QList<SourceExcerpt> consolidatedSources = groupedData.values ();
214
- return consolidatedSources;
215
- }
216
-
217
197
Q_INVOKABLE void updateSources (int index , const QList<SourceExcerpt> &sources)
218
198
{
219
199
if (index < 0 || index >= m_chatItems.size ()) return ;
220
200
221
201
ChatItem &item = m_chatItems[index ];
222
202
if (sources.isEmpty ()) {
223
203
item.sources .clear ();
224
- item.consolidatedSources .clear ();
225
204
} else {
226
205
item.sources << sources;
227
- item.consolidatedSources << consolidateSources (sources);
228
206
}
229
207
emit dataChanged (createIndex (index , 0 ), createIndex (index , 0 ), {SourcesRole});
230
- emit dataChanged (createIndex (index , 0 ), createIndex (index , 0 ), {ConsolidatedSourcesRole});
231
208
}
232
209
233
210
Q_INVOKABLE void updateThumbsUpState (int index , bool b)
@@ -278,61 +255,7 @@ class ChatModel : public QAbstractListModel
278
255
stream << c.stopped ;
279
256
stream << c.thumbsUpState ;
280
257
stream << c.thumbsDownState ;
281
- if (version > 7 ) {
282
- stream << c.sources .size ();
283
- for (const SourceExcerpt &info : c.sources ) {
284
- Q_ASSERT (!info.file .isEmpty ());
285
- stream << info.collection ;
286
- stream << info.path ;
287
- stream << info.file ;
288
- stream << info.title ;
289
- stream << info.author ;
290
- stream << info.date ;
291
- stream << info.text ;
292
- stream << info.page ;
293
- stream << info.from ;
294
- stream << info.to ;
295
- if (version > 9 ) {
296
- stream << info.url ;
297
- stream << info.favicon ;
298
- }
299
- }
300
- } else if (version > 2 ) {
301
- QList<QString> references;
302
- QList<QString> referencesContext;
303
- int validReferenceNumber = 1 ;
304
- for (const SourceExcerpt &info : c.sources ) {
305
- if (info.file .isEmpty ())
306
- continue ;
307
-
308
- QString reference;
309
- {
310
- QTextStream stream (&reference);
311
- stream << (validReferenceNumber++) << " . " ;
312
- if (!info.title .isEmpty ())
313
- stream << " \" " << info.title << " \" . " ;
314
- if (!info.author .isEmpty ())
315
- stream << " By " << info.author << " . " ;
316
- if (!info.date .isEmpty ())
317
- stream << " Date: " << info.date << " . " ;
318
- stream << " In " << info.file << " . " ;
319
- if (info.page != -1 )
320
- stream << " Page " << info.page << " . " ;
321
- if (info.from != -1 ) {
322
- stream << " Lines " << info.from ;
323
- if (info.to != -1 )
324
- stream << " -" << info.to ;
325
- stream << " . " ;
326
- }
327
- stream << " [Context](context://" << validReferenceNumber - 1 << " )" ;
328
- }
329
- references.append (reference);
330
- referencesContext.append (info.text );
331
- }
332
-
333
- stream << references.join (" \n " );
334
- stream << referencesContext;
335
- }
258
+ stream << SourceExcerpt::toJson (c.sources );
336
259
}
337
260
return stream.status () == QDataStream::Ok;
338
261
}
@@ -352,31 +275,36 @@ class ChatModel : public QAbstractListModel
352
275
stream >> c.stopped ;
353
276
stream >> c.thumbsUpState ;
354
277
stream >> c.thumbsDownState ;
355
- if (version > 7 ) {
278
+ if (version > 9 ) {
279
+ QList<SourceExcerpt> sources;
280
+ QString json;
281
+ stream >> json;
282
+ QString errorString;
283
+ sources = SourceExcerpt::fromJson (json, errorString);
284
+ Q_ASSERT (errorString.isEmpty ());
285
+ c.sources = sources;
286
+ } else if (version > 7 ) {
356
287
qsizetype count;
357
288
stream >> count;
358
289
QList<SourceExcerpt> sources;
359
290
for (int i = 0 ; i < count; ++i) {
360
- SourceExcerpt info;
361
- stream >> info.collection ;
362
- stream >> info.path ;
363
- stream >> info.file ;
364
- stream >> info.title ;
365
- stream >> info.author ;
366
- stream >> info.date ;
367
- stream >> info.text ;
368
- stream >> info.page ;
369
- stream >> info.from ;
370
- stream >> info.to ;
371
- if (version > 9 ) {
372
- stream >> info.url ;
373
- stream >> info.favicon ;
374
- }
375
- sources.append (info);
291
+ SourceExcerpt source;
292
+ stream >> source.collection ;
293
+ stream >> source.path ;
294
+ stream >> source.file ;
295
+ stream >> source.title ;
296
+ stream >> source.author ;
297
+ stream >> source.date ;
298
+ Excerpt excerpt;
299
+ stream >> excerpt.text ;
300
+ stream >> excerpt.page ;
301
+ stream >> excerpt.from ;
302
+ stream >> excerpt.to ;
303
+ source.excerpts = QList{ excerpt };
304
+ sources.append (source);
376
305
}
377
306
c.sources = sources;
378
- c.consolidatedSources = consolidateSources (sources);
379
- }else if (version > 2 ) {
307
+ } else if (version > 2 ) {
380
308
QString references;
381
309
QList<QString> referencesContext;
382
310
stream >> references;
@@ -398,7 +326,8 @@ class ChatModel : public QAbstractListModel
398
326
for (int j = 0 ; j < referenceList.size (); ++j) {
399
327
QString reference = referenceList[j];
400
328
QString context = referencesContext[j];
401
- SourceExcerpt info;
329
+ SourceExcerpt source;
330
+ Excerpt excerpt;
402
331
QTextStream refStream (&reference);
403
332
QString dummy;
404
333
int validReferenceNumber;
@@ -407,36 +336,36 @@ class ChatModel : public QAbstractListModel
407
336
if (reference.contains (" \" " )) {
408
337
int startIndex = reference.indexOf (' "' ) + 1 ;
409
338
int endIndex = reference.indexOf (' "' , startIndex);
410
- info .title = reference.mid (startIndex, endIndex - startIndex);
339
+ source .title = reference.mid (startIndex, endIndex - startIndex);
411
340
}
412
341
413
342
// Extract author (after "By " and before the next period)
414
343
if (reference.contains (" By " )) {
415
344
int startIndex = reference.indexOf (" By " ) + 3 ;
416
345
int endIndex = reference.indexOf (' .' , startIndex);
417
- info .author = reference.mid (startIndex, endIndex - startIndex).trimmed ();
346
+ source .author = reference.mid (startIndex, endIndex - startIndex).trimmed ();
418
347
}
419
348
420
349
// Extract date (after "Date: " and before the next period)
421
350
if (reference.contains (" Date: " )) {
422
351
int startIndex = reference.indexOf (" Date: " ) + 6 ;
423
352
int endIndex = reference.indexOf (' .' , startIndex);
424
- info .date = reference.mid (startIndex, endIndex - startIndex).trimmed ();
353
+ source .date = reference.mid (startIndex, endIndex - startIndex).trimmed ();
425
354
}
426
355
427
356
// Extract file name (after "In " and before the "[Context]")
428
357
if (reference.contains (" In " ) && reference.contains (" . [Context]" )) {
429
358
int startIndex = reference.indexOf (" In " ) + 3 ;
430
359
int endIndex = reference.indexOf (" . [Context]" , startIndex);
431
- info .file = reference.mid (startIndex, endIndex - startIndex).trimmed ();
360
+ source .file = reference.mid (startIndex, endIndex - startIndex).trimmed ();
432
361
}
433
362
434
363
// Extract page number (after "Page " and before the next space)
435
364
if (reference.contains (" Page " )) {
436
365
int startIndex = reference.indexOf (" Page " ) + 5 ;
437
366
int endIndex = reference.indexOf (' ' , startIndex);
438
367
if (endIndex == -1 ) endIndex = reference.length ();
439
- info .page = reference.mid (startIndex, endIndex - startIndex).toInt ();
368
+ excerpt .page = reference.mid (startIndex, endIndex - startIndex).toInt ();
440
369
}
441
370
442
371
// Extract lines (after "Lines " and before the next space or hyphen)
@@ -446,18 +375,18 @@ class ChatModel : public QAbstractListModel
446
375
if (endIndex == -1 ) endIndex = reference.length ();
447
376
int hyphenIndex = reference.indexOf (' -' , startIndex);
448
377
if (hyphenIndex != -1 && hyphenIndex < endIndex) {
449
- info .from = reference.mid (startIndex, hyphenIndex - startIndex).toInt ();
450
- info .to = reference.mid (hyphenIndex + 1 , endIndex - hyphenIndex - 1 ).toInt ();
378
+ excerpt .from = reference.mid (startIndex, hyphenIndex - startIndex).toInt ();
379
+ excerpt .to = reference.mid (hyphenIndex + 1 , endIndex - hyphenIndex - 1 ).toInt ();
451
380
} else {
452
- info .from = reference.mid (startIndex, endIndex - startIndex).toInt ();
381
+ excerpt .from = reference.mid (startIndex, endIndex - startIndex).toInt ();
453
382
}
454
383
}
455
- info.text = context;
456
- sources.append (info);
384
+ excerpt.text = context;
385
+ source.excerpts = QList{ excerpt };
386
+ sources.append (source);
457
387
}
458
388
459
389
c.sources = sources;
460
- c.consolidatedSources = consolidateSources (sources);
461
390
}
462
391
}
463
392
beginInsertRows (QModelIndex (), m_chatItems.size (), m_chatItems.size ());
0 commit comments