| 7 | |
| 8 | // 构建到指定消息的路径 |
| 9 | function buildPathToMessage(messages: ChatMessage[], targetMessageId: string): string[] { |
| 10 | const messageMap = new Map<string, ChatMessage>() |
| 11 | messages.forEach((msg) => messageMap.set(msg.id, msg)) |
| 12 | |
| 13 | const targetMessage = messageMap.get(targetMessageId) |
| 14 | if (!targetMessage) return [] |
| 15 | |
| 16 | // 构建从根到目标消息的路径 |
| 17 | const path: string[] = [] |
| 18 | let currentMsg: ChatMessage | undefined = targetMessage |
| 19 | |
| 20 | while (currentMsg) { |
| 21 | path.unshift(currentMsg.id) |
| 22 | if (currentMsg.parentId) { |
| 23 | currentMsg = messageMap.get(currentMsg.parentId) |
| 24 | } else { |
| 25 | break |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // 如果目标消息有子节点,继续延伸到第一个子节点 |
| 30 | let lastNode = targetMessage |
| 31 | while (lastNode.children && lastNode.children.length > 0) { |
| 32 | const firstChildId = lastNode.children[0] |
| 33 | const firstChild = messageMap.get(firstChildId) |
| 34 | if (firstChild) { |
| 35 | path.push(firstChildId) |
| 36 | lastNode = firstChild |
| 37 | } else { |
| 38 | break |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return path |
| 43 | } |
| 44 | |
| 45 | export interface TabsState { |
| 46 | openTabs: string[] // 所有打开的 tab ID |