Backfill missing swipe_info (#4831) * Backfill missing swipe_info * Add type for SwipeInfo * Init SwipeInfo with empty extra

1af87ea165e8febb2fdbe2a5e827f692ebe5b61c

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
3 files changed, +32 -15Showing whitespace changes
public/global.d.ts+11 -3
@@ -17,6 +17,7 @@ declare global {
1717 type TextCompletionSettings = typeof textgenerationwebui_settings;
1818 type MessageTimestamp = string | number | Date;
1919 type Character = import('./scripts/char-data').v1CharData;
20+ type ChatMessageExtra = BaseMessageExtra & Partial<ReasoningMessageExtra> & Record<string, any>;
2021
2122 interface Group {
2223 id: string;
@@ -69,12 +70,19 @@ declare global {
6970 force_avatar?: string;
7071 original_avatar?: string;
7172 swipes?: string[];
7273 swipe_info?: Record<string, any>SwipeInfo[];
7374 swipe_id?: number;
74- extra?: ChatMessageExtra & Partial<ReasoningMessageExtra> & Record<string, any>;
75+ extra?: ChatMessageExtra;
7576 };
7677
7778 interface ChatMessageExtraSwipeInfo {
79+ send_date?: MessageTimestamp;
80+ gen_started?: MessageTimestamp;
81+ gen_finished?: MessageTimestamp;
82+ extra?: ChatMessageExtra;
83+ }
84+
85+ interface BaseMessageExtra {
7886 gen_id?: number;
7987 bias?: string;
8088 uses_system_ui?: boolean;
public/script.js+20 -12
@@ -6404,27 +6404,35 @@ export function ensureSwipes(message) {
64046404 updated = true;
64056405 }
64066406
6407- for (let i = 0; i < message.swipes.length; i++) {
6408- if (typeof message.swipes[i] !== 'string') {
6409- updated = true;
6410- console.warn('The message had a swipe that is not a string. It has has been set to \'\'.', message);
6411- message.swipes[i] = '';
6412- }
6413- }
6414-
64156407 if (typeof message.swipe_id !== 'number') {
64166408 message.swipe_id = 0;
64176409 updated = true;
64186410 }
64196411
6420- if (!Array.isArray(message.swipe_info)) {
6412+ /** @type {() => SwipeInfo} */
64216413 message.swipe_info const createSwipeInfo = message.swipes.map(_) => ({
64226414 send_date: message.send_date,
64236415 gen_started: message.gen_started,
64246416 gen_finished: message.gen_finished,
64256417 extra: structuredClone(message.extra) ?? {},
64266418 }));
6419+
6420+ if (!Array.isArray(message.swipe_info)) {
6421+ message.swipe_info = message.swipes.map(_ => createSwipeInfo());
6422+ updated = true;
6423+ }
6424+
6425+ for (let i = 0; i < message.swipes.length; i++) {
6426+ if (typeof message.swipes[i] !== 'string') {
6427+ updated = true;
6428+ console.warn('The message had a swipe that is not a string. It has has been set to \'\'.', message);
6429+ message.swipes[i] = '';
6430+ }
6431+ if (!message.swipe_info[i] || typeof message.swipe_info[i] !== 'object') {
64276432 updated = true;
6433+ console.warn('The message had missing or invalid swipe_info for a swipe. It has been backfilled.', message);
6434+ message.swipe_info[i] = createSwipeInfo();
6435+ }
64286436 }
64296437
64306438 return updated;
public/scripts/logprobs.js+1 -0
@@ -417,6 +417,7 @@ function createSwipe(messageId, prompt) {
417417
418418 console.debug('cleanedPrompt: ', cleanedPrompt);
419419
420+ /** @type {SwipeInfo} */
420421 const newSwipeInfo = {
421422 send_date: msg.send_date,
422423 gen_started: msg.gen_started,