| 73 | }; |
| 74 | |
| 75 | export function useThreadActions({ |
| 76 | dispatch, |
| 77 | itemsByThread, |
| 78 | threadsByWorkspace, |
| 79 | activeThreadIdByWorkspace, |
| 80 | activeTurnIdByThread, |
| 81 | threadParentById, |
| 82 | threadListCursorByWorkspace, |
| 83 | threadStatusById, |
| 84 | threadSortKey, |
| 85 | onDebug, |
| 86 | getCustomName, |
| 87 | threadActivityRef, |
| 88 | loadedThreadsRef, |
| 89 | replaceOnResumeRef, |
| 90 | applyCollabThreadLinksFromThread, |
| 91 | updateThreadParent, |
| 92 | onSubagentThreadDetected, |
| 93 | onThreadCodexMetadataDetected, |
| 94 | }: UseThreadActionsOptions) { |
| 95 | const resumeInFlightByThreadRef = useRef<Record<string, number>>({}); |
| 96 | const threadStatusByIdRef = useRef(threadStatusById); |
| 97 | const activeTurnIdByThreadRef = useRef(activeTurnIdByThread); |
| 98 | threadStatusByIdRef.current = threadStatusById; |
| 99 | activeTurnIdByThreadRef.current = activeTurnIdByThread; |
| 100 | |
| 101 | const applyThreadMetadata = useCallback( |
| 102 | ( |
| 103 | workspaceId: string, |
| 104 | threadId: string, |
| 105 | thread: Record<string, unknown>, |
| 106 | options?: { notifySubagent?: boolean }, |
| 107 | ) => { |
| 108 | const codexMetadata = extractThreadCodexMetadata(thread); |
| 109 | if (codexMetadata.modelId || codexMetadata.effort) { |
| 110 | onThreadCodexMetadataDetected?.(workspaceId, threadId, codexMetadata); |
| 111 | } |
| 112 | const sourceParentId = getParentThreadIdFromThread(thread); |
| 113 | if (sourceParentId) { |
| 114 | updateThreadParent(sourceParentId, [threadId]); |
| 115 | if (options?.notifySubagent) { |
| 116 | onSubagentThreadDetected(workspaceId, threadId); |
| 117 | } |
| 118 | } |
| 119 | }, |
| 120 | [ |
| 121 | onSubagentThreadDetected, |
| 122 | onThreadCodexMetadataDetected, |
| 123 | updateThreadParent, |
| 124 | ], |
| 125 | ); |
| 126 | |
| 127 | const dispatchPreviewMessage = useCallback( |
| 128 | (threadId: string, text: string, timestamp: number) => { |
| 129 | dispatch({ |
| 130 | type: "setLastAgentMessage", |
| 131 | threadId, |
| 132 | text, |