import { selectSessionMessages, selectSessionMessageWindow, type AgentSessionFamilySnapshot, type AgentSessionEngine } from "@tutti-os/agent-activity-core"; import type { RefObject } from "react"; import { useCallback } from "react"; import { useAgentActivitySessionMessages, type AgentGUIRuntime } from "../../../agentActivityRuntime"; import { useAgentSessionControllerState } from "../../../contexts/workspace/presentation/renderer/agentSessions/useAgentSessionControllerState"; import type { AgentGUINodeData } from "../../../types"; import { reportAgentGUIMessagePageDiagnostic, reportAgentGUIRuntimeError } from "./agentGuiController.reporting"; import { useAgentConversationMessagePaging } from "./useAgentConversationMessagePaging"; import { useEngineSelector } from "../../../shared/engine/useEngineSelector"; export function useAgentGUISessionDetailTransport(input: { activeConversationId: string | null; activeConversationIdRef: RefObject; agentActivityRuntime: AgentGUIRuntime; agentActivityRuntimeOrigin: string; dataRef: RefObject; isMountedRef: RefObject; sessionFamily: AgentSessionFamilySnapshot; sessionEngine: AgentSessionEngine; workspaceId: string; }) { const { activeConversationId, activeConversationIdRef, agentActivityRuntime, agentActivityRuntimeOrigin, dataRef, isMountedRef, sessionFamily, sessionEngine, workspaceId } = input; const sessionViewRef = useCallback( (agentSessionId: string | null | undefined) => ({ workspaceId, agentSessionId, origin: agentActivityRuntimeOrigin }), [agentActivityRuntimeOrigin, workspaceId] ); const activeCanonicalMessages = useEngineSelector( sessionEngine, (engineState) => selectSessionMessages(engineState, activeConversationId) ); const projectedSessionMessagesById = useAgentActivitySessionMessages( workspaceId, [ activeConversationId, sessionFamily.rootSession?.agentSessionId, ...sessionFamily.childSessions.map((session) => session.agentSessionId) ] ); const activeCanonicalWindow = useEngineSelector( sessionEngine, (engineState) => selectSessionMessageWindow(engineState, activeConversationId) ); const state = useAgentSessionControllerState( sessionViewRef(activeConversationId), activeCanonicalMessages, activeCanonicalWindow ); const resolveSessionMessages = useCallback( (agentSessionId: string | null | undefined) => { const normalized = agentSessionId?.trim() ?? ""; if (!normalized) return []; return ( projectedSessionMessagesById[normalized] ?? selectSessionMessages(sessionEngine.getSnapshot(), normalized) ); }, [projectedSessionMessagesById, sessionEngine] ); const loadSessionState = useCallback( (agentSessionId: string) => { const normalized = agentSessionId.trim(); if (!normalized) return; sessionEngine.dispatch({ agentSessionId: normalized, needsMessages: false, needsState: true, type: "session/reconcileRequested", workspaceId }); }, [sessionEngine, workspaceId] ); const paging = useAgentConversationMessagePaging({ diagnostics: { error: ({ agentSessionId, context, error, phase }) => reportAgentGUIRuntimeError({ agentSessionId, context, error, phase, provider: dataRef.current.provider, runtime: agentActivityRuntime, workspaceId }), page: ({ agentSessionId, details, event, level, messages }) => reportAgentGUIMessagePageDiagnostic({ agentSessionId, details, event, level, messages, runtime: agentActivityRuntime, workspaceId }) }, getActiveSessionId: () => activeConversationIdRef.current, isMounted: () => isMountedRef.current, onOlderPageLoadingChanged: (loading) => state.setAgentSessionViewOlderMessagesLoading( sessionViewRef(activeConversationIdRef.current), loading ), runtime: agentActivityRuntime, sessionEngine, workspaceId }); const markSelectedConversationDetailPending = useCallback( (agentSessionId: string) => { const normalized = agentSessionId.trim(); if (!normalized) return null; const ref = sessionViewRef(normalized); state.setAgentSessionViewError(ref, null); return normalized; }, [sessionViewRef, state] ); return { ...state, loadOlderConversationMessages: paging.loadOlderMessages, loadSelectedConversationMessages: paging.loadInitialMessages, loadSessionState, markSelectedConversationDetailPending, projectedSessionMessagesById, resolveSessionMessages, setActiveMessageSession: paging.setActiveSession, sessionViewRef }; }