refac
This commit is contained in:
1 parent
61a2672215
commit
2308b59f13
1 file changed
+17
-14
+17
-14
@@ -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
|
// Reconstruct missing parentId and role
|
||||||
for (const [id, message] of Object.entries(history.messages)) {
|
for (const [id, message] of Object.entries(history.messages)) {
|
||||||
// Well-formed: has role and explicit parentId (null is valid for root)
|
// 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)) {
|
for (const message of Object.values(history.messages)) {
|
||||||
message.childrenIds = message.childrenIds.filter((childId) => history.messages[childId]);
|
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) => {
|
export const getGravatarURL = (email) => {
|
||||||
|
|||||||
Reference in new issue
Block a user