mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-06 11:37:57 -05:00
refac: share one guarded helper for chat branch descent (#30070)
The branch navigation handlers each carried their own copy of the loop that walks a message's childrenIds down to the deepest child, eleven copies in total across the single-response view, the multi-response view and Chat.svelte. They now call one getDeepestChildId helper in the frontend utils, which tracks the ids it has already visited and stops when one repeats, the same way the message list build already does. The helper also absorbs the "start from the last root message" fallback that two of the call sites repeated inline, so every site is now a single call.
This commit is contained in:
1 parent
d9c8de9c39
commit
befdd86eb1
4 files changed
+41
-98
No files matched your search
@@ -1433,6 +1433,29 @@ export const createMessagesList = (history, messageId) => {
|
||||
return list.reverse();
|
||||
};
|
||||
|
||||
export const getDeepestChildId = (history, messageId) => {
|
||||
let deepestId = messageId;
|
||||
const visitedMessageIds = new Set([deepestId]);
|
||||
let childrenIds =
|
||||
deepestId === null
|
||||
? Object.keys(history.messages).filter((id) => history.messages[id].parentId === null)
|
||||
: (history.messages[deepestId]?.childrenIds ?? []);
|
||||
|
||||
while (childrenIds.length !== 0) {
|
||||
const childId = childrenIds.at(-1);
|
||||
if (visitedMessageIds.has(childId)) {
|
||||
console.warn('Circular dependency detected in message history', childId);
|
||||
break;
|
||||
}
|
||||
|
||||
visitedMessageIds.add(childId);
|
||||
deepestId = childId;
|
||||
childrenIds = history.messages[deepestId]?.childrenIds ?? [];
|
||||
}
|
||||
|
||||
return deepestId;
|
||||
};
|
||||
|
||||
const toTokenCount = (value: unknown) => {
|
||||
const parsed = Number(value || 0);
|
||||
return Number.isFinite(parsed) ? Math.trunc(parsed) : 0;
|
||||
|
||||
Reference in new issue
Block a user