()
| 20 | } |
| 21 | |
| 22 | export function useAssistantBootstrap() { |
| 23 | const { t } = useI18n('notifications'); |
| 24 | const activeAttemptRef = useRef<ActiveBootstrapAttempt | null>(null); |
| 25 | const pendingRequestRef = useRef<BootstrapRequest | null>(null); |
| 26 | const latestWorkspacePathRef = useRef<string | null>(null); |
| 27 | const inFlightWorkspacePathRef = useRef<string | null>(null); |
| 28 | const blockedNoticeShownRef = useRef<Set<string>>(new Set()); |
| 29 | const requestBootstrapRef = useRef<(request: BootstrapRequest) => void>(() => {}); |
| 30 | |
| 31 | const drainPendingRequest = useCallback(() => { |
| 32 | const pending = pendingRequestRef.current; |
| 33 | if (!pending) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | if (latestWorkspacePathRef.current !== pending.workspacePath) { |
| 38 | pendingRequestRef.current = null; |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | pendingRequestRef.current = null; |
| 43 | requestBootstrapRef.current(pending); |
| 44 | }, []); |
| 45 | |
| 46 | const clearActiveAttempt = useCallback( |
| 47 | (event: { sessionId?: string; turnId?: string }) => { |
| 48 | const activeAttempt = activeAttemptRef.current; |
| 49 | if (!activeAttempt) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | if (event.sessionId !== activeAttempt.sessionId || event.turnId !== activeAttempt.turnId) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | activeAttemptRef.current = null; |
| 58 | drainPendingRequest(); |
| 59 | }, |
| 60 | [drainPendingRequest] |
| 61 | ); |
| 62 | |
| 63 | useEffect(() => { |
| 64 | const unlistenCompleted = agentAPI.onDialogTurnCompleted(clearActiveAttempt); |
| 65 | const unlistenFailed = agentAPI.onDialogTurnFailed(clearActiveAttempt); |
| 66 | const unlistenCancelled = agentAPI.onDialogTurnCancelled(clearActiveAttempt); |
| 67 | |
| 68 | return () => { |
| 69 | unlistenCompleted(); |
| 70 | unlistenFailed(); |
| 71 | unlistenCancelled(); |
| 72 | }; |
| 73 | }, [clearActiveAttempt]); |
| 74 | |
| 75 | const handleEnsureResponse = useCallback( |
| 76 | (request: BootstrapRequest, response: EnsureAssistantBootstrapResponse): void => { |
| 77 | switch (response.status) { |
| 78 | case 'started': |
| 79 | if (!response.turnId) { |
no test coverage detected