import { useCallback, useEffect } from '../vendor/preact-htm.js'; import { finalizeStalledResponse as finalizeStalledResponseState } from './app-agent-status-orchestration.js'; type StateSetter = (next: T | ((prev: T) => T)) => void; interface RefBox { current: T; } export function removeStalledPostFromTimeline(options: { stalledPostIdRef: RefBox; setPosts: StateSetter; }): void { const { stalledPostIdRef, setPosts } = options; const stalledId = stalledPostIdRef.current; if (!stalledId) return; setPosts((prev) => (prev ? prev.filter((post) => post.id !== stalledId) : prev)); stalledPostIdRef.current = null; } export function runFinalizeStalledResponse(options: Record): void { finalizeStalledResponseState(options as any); } export function useViewStateRefSync(options: { viewStateRef: RefBox | null | undefined>; currentHashtag: string | null; searchQuery: string | null; searchOpen: boolean; }): void { const { viewStateRef, currentHashtag, searchQuery, searchOpen, } = options; useEffect(() => { viewStateRef.current = { currentHashtag, searchQuery, searchOpen }; }, [currentHashtag, searchOpen, searchQuery, viewStateRef]); } export function useAgentRecoveryCallbacks(options: { isAgentRunningRef: RefBox; lastSilenceNoticeRef: RefBox; lastAgentEventRef: RefBox; currentTurnIdRef: RefBox; thoughtExpandedRef: RefBox; draftExpandedRef: RefBox; draftBufferRef: RefBox; thoughtBufferRef: RefBox; pendingRequestRef: RefBox; lastAgentResponseRef: RefBox; stalledPostIdRef: RefBox; scrollToBottomRef: RefBox<(() => void) | null>; setCurrentTurnId: (value: string | null) => void; setAgentDraft: (value: any) => void; setAgentPlan: (value: any) => void; setAgentThought: (value: any) => void; setPendingRequest: (value: any) => void; setAgentStatus: (value: any) => void; setPosts: StateSetter; dedupePosts: (posts: any[]) => any[]; }) { const { stalledPostIdRef, setPosts, isAgentRunningRef, lastSilenceNoticeRef, lastAgentEventRef, currentTurnIdRef, thoughtExpandedRef, draftExpandedRef, draftBufferRef, thoughtBufferRef, pendingRequestRef, lastAgentResponseRef, scrollToBottomRef, setCurrentTurnId, setAgentDraft, setAgentPlan, setAgentThought, setPendingRequest, setAgentStatus, dedupePosts, } = options; const removeStalledPost = useCallback(() => { removeStalledPostFromTimeline({ stalledPostIdRef, setPosts, }); }, [setPosts, stalledPostIdRef]); const finalizeStalledResponse = useCallback(() => { runFinalizeStalledResponse({ isAgentRunningRef, lastSilenceNoticeRef, lastAgentEventRef, currentTurnIdRef, thoughtExpandedRef, draftExpandedRef, draftBufferRef, thoughtBufferRef, pendingRequestRef, lastAgentResponseRef, stalledPostIdRef, scrollToBottomRef, setCurrentTurnId, setAgentDraft, setAgentPlan, setAgentThought, setPendingRequest, setAgentStatus, setPosts, dedupePosts, }); }, [ currentTurnIdRef, dedupePosts, draftBufferRef, draftExpandedRef, isAgentRunningRef, lastAgentEventRef, lastAgentResponseRef, lastSilenceNoticeRef, pendingRequestRef, scrollToBottomRef, setAgentDraft, setAgentPlan, setAgentStatus, setAgentThought, setCurrentTurnId, setPendingRequest, setPosts, stalledPostIdRef, thoughtBufferRef, thoughtExpandedRef, ]); return { removeStalledPost, finalizeStalledResponse, }; }