import { useCallback, useRef, type Dispatch, type SetStateAction } from "react"; import type { PendingActivationIntentRecord } from "@tutti-os/agent-activity-core"; import type { AgentGUIRuntime } from "../../../agentActivityRuntime"; import { translate } from "../../../i18n/index"; import type { AgentHostAccountUserProfile } from "../../../shared/contracts/dto"; import type { AgentComposerDraft } from "../model/agentGuiNodeTypes"; import type { AgentGUIConversationSummary } from "../model/agentGuiConversationModel"; import type { AgentGUIConversationUserProject } from "../model/agentGuiConversationProjectResolver"; import { resolveAgentGUIExplicitConversationTitle } from "../model/agentGuiProviderIdentity"; import { agentComposerDraftPrompt, emptyAgentComposerDraft, updateAgentComposerDraft } from "../model/agentComposerDraft"; import { resolveAgentComposerDraftScopeKey } from "../model/agentComposerDraftScope"; import { buildContinueInNewConversationPrompt } from "./agentGuiController.conversationHelpers"; import { resolveAgentGUINewConversationProjectSelection } from "./agentGuiNewConversationRequest"; import { reportAgentGUIActiveConversationCleared } from "./agentGuiController.reporting"; import { isPendingNewConversationActivationForSession } from "./useAgentGUIActivation"; import { resolveConversationSummaryById, type ConversationIntent } from "./useAgentConversationSelection"; interface CurrentValue { current: T; } interface UseAgentGUIContinueConversationInput { accountProfilesByUserId: Readonly< Record >; activeConversationIdRef: CurrentValue; activePendingActivation: PendingActivationIntentRecord | null; agentActivityRuntime: AgentGUIRuntime; conversations: readonly AgentGUIConversationSummary[]; currentUserId: string | null | undefined; draftByScopeKey: Record; isComposerHomeRef: CurrentValue; loadDraftComposerOptions(): void; persistActiveConversation(agentSessionId: string | null): void; setActiveConversationId: Dispatch>; setDetailError: Dispatch>; setDraftByScopeKey: Dispatch< SetStateAction> >; setIntent: Dispatch>; setIsComposerHome: Dispatch>; setIsLoadingMessages: Dispatch>; selectedProjectPathRef: CurrentValue; setSelectedProjectPath: Dispatch>; transientConversation: AgentGUIConversationSummary | null; unactivate(agentSessionId: string): Promise; userProjectsRef: CurrentValue; workspaceId: string; } export function buildContinueInNewConversationDraft(input: { sourceDraft: AgentComposerDraft; prompt: string; }): AgentComposerDraft { return updateAgentComposerDraft(input.sourceDraft, { prompt: input.prompt }); } export function useAgentGUIContinueConversation( input: UseAgentGUIContinueConversationInput ) { const inputRef = useRef(input); inputRef.current = input; return useCallback(() => { const current = inputRef.current; const currentConversationId = current.activeConversationIdRef.current; if (!currentConversationId) return; const projectSelection = resolveAgentGUINewConversationProjectSelection({ activeConversationId: currentConversationId, conversations: current.conversations, transientConversation: current.transientConversation, userProjects: current.userProjectsRef.current }); if (projectSelection.kind !== "replace") { current.setDetailError( translate("agentHost.agentGui.sessionActivationFailed") ); return; } const activeConversation = resolveConversationSummaryById( current.conversations, currentConversationId, current.transientConversation ); if (!activeConversation) return; const sourceDraftScopeKey = resolveAgentComposerDraftScopeKey({ agentSessionId: currentConversationId }); const sourceDraft = current.draftByScopeKey[sourceDraftScopeKey] ?? emptyAgentComposerDraft(); const nextDraftPrompt = buildContinueInNewConversationPrompt({ workspaceId: current.workspaceId, agentSessionId: activeConversation.id, conversationUserId: activeConversation.userId, currentUserId: current.currentUserId, userProfilesByUserId: current.accountProfilesByUserId, provider: activeConversation.provider, agentTargetId: activeConversation.agentTargetId, conversationTitle: resolveAgentGUIExplicitConversationTitle(activeConversation) ?? translate("agentHost.workspaceAgentsUntitledConversation"), existingDraftPrompt: agentComposerDraftPrompt(sourceDraft) }); reportAgentGUIActiveConversationCleared({ details: { sourceConversationId: activeConversation.id }, previousAgentSessionId: currentConversationId, reason: "continue_in_new_conversation", runtime: current.agentActivityRuntime, workspaceId: current.workspaceId }); if ( !isPendingNewConversationActivationForSession( current.activePendingActivation, currentConversationId ) ) { void current.unactivate(currentConversationId); } current.setIntent({ tag: "home" }); current.selectedProjectPathRef.current = projectSelection.projectPath; current.setSelectedProjectPath(projectSelection.projectPath); current.isComposerHomeRef.current = true; current.setIsComposerHome(true); current.activeConversationIdRef.current = null; current.setActiveConversationId(null); current.setIsLoadingMessages(false); current.setDetailError(null); current.setDraftByScopeKey((drafts) => ({ ...drafts, [resolveAgentComposerDraftScopeKey({})]: buildContinueInNewConversationDraft({ sourceDraft, prompt: nextDraftPrompt }) })); current.persistActiveConversation(null); current.loadDraftComposerOptions(); }, []); }