incognitolm commited on
Commit ·
c8ae980
1
Parent(s): 8c493a5
Update chat.js
Browse files- public/js/chat.js +32 -26
public/js/chat.js
CHANGED
|
@@ -40,12 +40,14 @@ on('ws:connected', () => {
|
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Extract a flat array from the tree-based message structure.
|
| 43 |
-
*
|
| 44 |
-
*
|
| 45 |
*/
|
| 46 |
function extractFlatHistoryFromTree(rootMessage) {
|
| 47 |
if (!rootMessage) return [];
|
| 48 |
|
|
|
|
|
|
|
| 49 |
const ensureValidContent = (msg) => {
|
| 50 |
if (msg.content === undefined || msg.content === null) {
|
| 51 |
msg.content = '';
|
|
@@ -53,31 +55,35 @@ function extractFlatHistoryFromTree(rootMessage) {
|
|
| 53 |
return msg;
|
| 54 |
};
|
| 55 |
|
| 56 |
-
|
| 57 |
-
const
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
const walkTail = (tail) => {
|
| 71 |
-
for (const msg of tail) {
|
| 72 |
-
history.push(ensureValidContent(msg));
|
| 73 |
-
const ver = msg.versions?.[msg.currentVersionIdx ?? 0];
|
| 74 |
-
if (ver?.tail && Array.isArray(ver.tail)) {
|
| 75 |
-
walkTail(ver.tail);
|
| 76 |
-
}
|
| 77 |
-
}
|
| 78 |
-
};
|
| 79 |
-
walkTail(currentTail);
|
| 80 |
-
}
|
| 81 |
return history;
|
| 82 |
}
|
| 83 |
|
|
|
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Extract a flat array from the tree-based message structure.
|
| 43 |
+
* Only follows the currently selected version branch at each level.
|
| 44 |
+
* Each message's version has a tail; only the selected version's tail is included.
|
| 45 |
*/
|
| 46 |
function extractFlatHistoryFromTree(rootMessage) {
|
| 47 |
if (!rootMessage) return [];
|
| 48 |
|
| 49 |
+
const history = [];
|
| 50 |
+
|
| 51 |
const ensureValidContent = (msg) => {
|
| 52 |
if (msg.content === undefined || msg.content === null) {
|
| 53 |
msg.content = '';
|
|
|
|
| 55 |
return msg;
|
| 56 |
};
|
| 57 |
|
| 58 |
+
// Recursively extract only the currently selected version's branch
|
| 59 |
+
const extractBranch = (message) => {
|
| 60 |
+
history.push(ensureValidContent(message));
|
| 61 |
+
|
| 62 |
+
// Get the currently selected version index for this message
|
| 63 |
+
const currentVersionIdx = message.currentVersionIdx ?? 0;
|
| 64 |
+
const versions = message.versions || [];
|
| 65 |
+
|
| 66 |
+
// Validate version index is in bounds
|
| 67 |
+
if (currentVersionIdx >= versions.length) {
|
| 68 |
+
return;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
const currentVersion = versions[currentVersionIdx];
|
| 72 |
+
if (!currentVersion) return;
|
| 73 |
+
|
| 74 |
+
// Only process the selected version's tail if it exists and has items
|
| 75 |
+
const tail = currentVersion.tail;
|
| 76 |
+
if (!Array.isArray(tail) || tail.length === 0) {
|
| 77 |
+
return;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
// Recursively extract each message in the current version's tail
|
| 81 |
+
for (const tailMessage of tail) {
|
| 82 |
+
extractBranch(tailMessage);
|
| 83 |
+
}
|
| 84 |
+
};
|
| 85 |
|
| 86 |
+
extractBranch(rootMessage);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
return history;
|
| 88 |
}
|
| 89 |
|