mirror of
https://github.com/open-webui/open-webui.git
synced 2026-07-10 04:20:44 +02:00
refac
This commit is contained in:
@@ -254,6 +254,23 @@ export const sanitizeHistory = (history) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Recover currentId before role reconstruction can make a malformed node
|
||||
// look valid.
|
||||
const currentMessage = history.messages?.[history.currentId];
|
||||
if (!currentMessage?.id || !currentMessage?.role) {
|
||||
let latestLeafId = null;
|
||||
let latestTimestamp = -1;
|
||||
|
||||
for (const [id, message] of Object.entries(history.messages)) {
|
||||
if (message.childrenIds.length === 0 && (message.timestamp ?? 0) > latestTimestamp) {
|
||||
latestLeafId = id;
|
||||
latestTimestamp = message.timestamp ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
history.currentId = latestLeafId ?? Object.keys(history.messages)[0] ?? null;
|
||||
}
|
||||
|
||||
// Reconstruct missing parentId and role
|
||||
for (const [id, message] of Object.entries(history.messages)) {
|
||||
// Well-formed: has role and explicit parentId (null is valid for root)
|
||||
@@ -280,20 +297,6 @@ export const sanitizeHistory = (history) => {
|
||||
for (const message of Object.values(history.messages)) {
|
||||
message.childrenIds = message.childrenIds.filter((childId) => history.messages[childId]);
|
||||
}
|
||||
|
||||
// Recover currentId if it points to a missing or incomplete node
|
||||
const currentMessage = history.messages?.[history.currentId];
|
||||
if (!currentMessage?.id || !currentMessage?.role) {
|
||||
let latestLeafId = null;
|
||||
let latestTimestamp = -1;
|
||||
for (const [id, message] of Object.entries(history.messages)) {
|
||||
if (message.childrenIds.length === 0 && (message.timestamp ?? 0) > latestTimestamp) {
|
||||
latestLeafId = id;
|
||||
latestTimestamp = message.timestamp ?? 0;
|
||||
}
|
||||
}
|
||||
history.currentId = latestLeafId ?? Object.keys(history.messages)[0] ?? null;
|
||||
}
|
||||
};
|
||||
|
||||
export const getGravatarURL = (email) => {
|
||||
|
||||
Reference in New Issue
Block a user