({
dispatch,
threadParentById,
onSubagentThreadDetected,
}: UseThreadLinkingOptions)
| 98 | } |
| 99 | |
| 100 | export function useThreadLinking({ |
| 101 | dispatch, |
| 102 | threadParentById, |
| 103 | onSubagentThreadDetected, |
| 104 | }: UseThreadLinkingOptions) { |
| 105 | const wouldCreateThreadCycle = useCallback( |
| 106 | (parentId: string, childId: string) => { |
| 107 | const visited = new Set([childId]); |
| 108 | let current: string | undefined = parentId; |
| 109 | while (current) { |
| 110 | if (visited.has(current)) { |
| 111 | return true; |
| 112 | } |
| 113 | visited.add(current); |
| 114 | current = threadParentById[current]; |
| 115 | } |
| 116 | return false; |
| 117 | }, |
| 118 | [threadParentById], |
| 119 | ); |
| 120 | |
| 121 | const updateThreadParent = useCallback( |
| 122 | (parentId: string, childIds: string[]) => { |
| 123 | if (!parentId || childIds.length === 0) { |
| 124 | return; |
| 125 | } |
| 126 | childIds.forEach((childId) => { |
| 127 | if (!childId || childId === parentId) { |
| 128 | return; |
| 129 | } |
| 130 | const existingParent = threadParentById[childId]; |
| 131 | if (existingParent === parentId) { |
| 132 | return; |
| 133 | } |
| 134 | if (existingParent) { |
| 135 | return; |
| 136 | } |
| 137 | if (wouldCreateThreadCycle(parentId, childId)) { |
| 138 | return; |
| 139 | } |
| 140 | dispatch({ type: "setThreadParent", threadId: childId, parentId }); |
| 141 | }); |
| 142 | }, |
| 143 | [dispatch, threadParentById, wouldCreateThreadCycle], |
| 144 | ); |
| 145 | |
| 146 | const applyCollabThreadLinks = useCallback( |
| 147 | ( |
| 148 | workspaceId: string, |
| 149 | fallbackThreadId: string, |
| 150 | item: Record<string, unknown>, |
| 151 | ) => { |
| 152 | const itemType = asString(item?.type ?? ""); |
| 153 | const isCollabType = |
| 154 | itemType === "collabToolCall" || itemType === "collabAgentToolCall"; |
| 155 | if (!isCollabType && !hasCollabLinkHints(item)) { |
| 156 | return; |
| 157 | } |
no test coverage detected