import { useMemo, type RefObject } from "react"; import type { AgentConversationVM } from "../../../shared/agentConversation/contracts/agentConversationVM"; import type { AgentComposerProps } from "../AgentComposer"; import type { AgentGUINodeViewProps } from "../AgentGUINodeView"; import type { AgentGUINodeViewModel } from "../model/agentGuiNodeTypes"; import { projectAgentComposerInputHistory } from "../model/agentComposerInputHistory"; import { useStableEventCallback } from "./agentGUIViewUtils"; type InputHistoryProps = Pick< AgentComposerProps, | "inputHistory" | "inputHistoryHasOlderPage" | "inputHistoryIsLoadingOlderPage" | "onRequestOlderInputHistoryPage" >; const EMPTY_INPUT_HISTORY: NonNullable = []; export function useAgentGUIComposerInputHistoryProps(input: { actions: AgentGUINodeViewProps["actions"]; conversation: AgentConversationVM | null; enabled: boolean; pendingPrependScrollAnchorRef: RefObject<{ conversationId: string; scrollHeight: number; scrollTop: number; } | null>; timelineRef: RefObject; viewModel: AgentGUINodeViewModel; }): InputHistoryProps { const onRequestOlderInputHistoryPage = useStableEventCallback(() => { const activeConversationId = input.viewModel.rail.activeConversationId; if ( !activeConversationId || !input.viewModel.detail.hasOlderMessages || input.viewModel.detail.isLoadingOlderMessages ) { return; } const timeline = input.timelineRef.current; if (timeline) { input.pendingPrependScrollAnchorRef.current = { conversationId: activeConversationId, scrollHeight: timeline.scrollHeight, scrollTop: timeline.scrollTop }; } input.actions.loadOlderConversationMessages(); }); const inputHistory = input.enabled ? projectAgentComposerInputHistory(input.conversation) : EMPTY_INPUT_HISTORY; return useMemo( () => ({ inputHistory, inputHistoryHasOlderPage: input.enabled && input.viewModel.detail.hasOlderMessages, inputHistoryIsLoadingOlderPage: input.enabled && input.viewModel.detail.isLoadingOlderMessages, onRequestOlderInputHistoryPage: input.enabled ? onRequestOlderInputHistoryPage : undefined }), [ input.viewModel.detail.hasOlderMessages, input.viewModel.detail.isLoadingOlderMessages, input.enabled, inputHistory, onRequestOlderInputHistoryPage ] ); }