| 108 | } |
| 109 | |
| 110 | function getDescendantIds( |
| 111 | childrenMap: Map<string | undefined, ChatMessage[]>, |
| 112 | messageId: string |
| 113 | ): Set<string> { |
| 114 | const ids = new Set<string>() |
| 115 | const stack = [messageId] |
| 116 | |
| 117 | while (stack.length > 0) { |
| 118 | const currentId = stack.pop()! |
| 119 | const children = childrenMap.get(currentId) ?? [] |
| 120 | for (const child of children) { |
| 121 | ids.add(child.id) |
| 122 | stack.push(child.id) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return ids |
| 127 | } |
| 128 | |
| 129 | export function getNextBranchIndex(messages: ChatMessage[], parentId: string | undefined): number { |
| 130 | const children = getChildMessages(messages, parentId) |