"use client"; import { useCallback, useLayoutEffect, useMemo, useRef, useState } from "react"; import { AgentLogo } from "@/components/agent-logo"; import { GridSpinner } from "@/components/grid-spinner"; import { PanelLoadingState } from "@/components/panel-loading-state"; import { SessionTabs, type SessionTab } from "@/components/session-tabs"; import { useAppStore } from "@/components/state-provider"; import { findTaskInSnapshots } from "@/lib/kanban/find-task"; import { useSessionResumption } from "@/hooks/domains/session/use-session-resumption"; import { useTaskSessions } from "@/hooks/use-task-sessions"; import type { UseEnsureTaskSessionResult } from "@/hooks/domains/session/use-ensure-task-session"; import type { AgentProfileOption } from "@/lib/state/slices"; import type { TaskSession } from "@/lib/types/http"; import { sendMessageRequest } from "@/hooks/use-message-handler"; import type { ChatSubmitPayload } from "./chat/chat-input-container"; import { EnsureSessionErrorEmptyState, SessionRecoveryFeedback } from "./ensure-session-error"; import { PassthroughToolbar } from "./passthrough-toolbar"; import { PreviewSessionTabMenu } from "./preview-session-tab-menu"; import { SessionTabDialogs } from "./session-tab-menu"; import { TabRenameInput } from "./tab-rename-input"; import { PreviewPlanPanel, usePreviewPlanSummary } from "./preview-plan-panel"; import { TaskChatPanel } from "./task-chat-panel"; import type { HandoffPreset } from "./new-session-dialog"; import { MAX_SESSION_NAME_LENGTH, useSessionRenameCommitter } from "./use-session-rename"; import { buildAgentLabelsById, isSessionActive, pickActiveSessionId, resolveAgentLabelFor, sortSessions, } from "./session-sort"; import { useTranslation } from "react-i18next"; const LABEL_SEPARATOR = " \u2022 "; const PLAN_TAB_ID = "plan"; const PREVIEW_TAB_CLASS_NAME = "bg-muted/50 data-[state=active]:bg-muted [@media(pointer:coarse)]:!min-h-11"; type PreviewSessionTabsProps = { taskId: string; sessionId: string | null; isArchived?: boolean; ensureSession?: UseEnsureTaskSessionResult; workspaceId?: string | null; onSessionChange?: (sessionId: string | null) => void; }; /** Delete-confirmation state hoisted out of the per-tab menu: `SessionTabs` * only gives each tab a slot for its `ContextMenuContent`, not a sibling * slot for a dialog, so the confirmation popover and the anchor it needs * live here instead. */ type MenuDeleteState = { sessionId: string; confirm: () => Promise }; /** Holds the rename/delete/share/handoff UI state shared by every preview tab. */ function usePreviewSessionTabDialogs(taskId: string, sortedSessions: TaskSession[]) { const [renamingSessionId, setRenamingSessionId] = useState(null); const [menuDeleteState, setMenuDeleteState] = useState(null); const menuDeleteAnchorRef = useRef(null); const menuDeleteFocusBoundaryRef = useRef(null); const [shareSessionId, setShareSessionId] = useState(null); const [handoffOpen, setHandoffOpen] = useState(false); const [handoffPreset, setHandoffPreset] = useState(null); const handleRequestDelete = useCallback( (targetSessionId: string, event: Event, confirmDelete: () => Promise) => { const anchor = event.currentTarget; if (!(anchor instanceof HTMLElement)) return; menuDeleteAnchorRef.current = anchor; menuDeleteFocusBoundaryRef.current = anchor.closest('[data-slot="context-menu-content"]'); setMenuDeleteState({ sessionId: targetSessionId, confirm: confirmDelete }); }, [], ); const handleConfirmDelete = useCallback(() => { void menuDeleteState?.confirm(); }, [menuDeleteState]); const handleMenuDeleteOpenChange = useCallback((open: boolean) => { if (!open) setMenuDeleteState(null); }, []); const handleHandoffProfile = useCallback((sourceSessionId: string, targetProfileId: string) => { setHandoffPreset({ sourceSessionId, targetProfileId }); setHandoffOpen(true); }, []); const renamingSession = sortedSessions.find((s) => s.id === renamingSessionId) ?? null; const handleCommitRename = useSessionRenameCommitter( renamingSessionId ?? undefined, taskId, renamingSession?.name ?? null, () => setRenamingSessionId(null), ); return { renamingSessionId, setRenamingSessionId, handleCommitRename, menuDeleteState, menuDeleteAnchorRef, menuDeleteFocusBoundaryRef, handleRequestDelete, handleConfirmDelete, handleMenuDeleteOpenChange, shareSessionId, setShareSessionId, handoffOpen, setHandoffOpen, handoffPreset, setHandoffPreset, handleHandoffProfile, }; } type PreviewDialogsState = ReturnType; /** Builds the `SessionTab[]` for the preview strip, wiring each tab's * context menu and (while renaming) its inline rename input. */ function useBuildPreviewTabs({ sortedSessions, profilesById, agentLabelsById, taskId, isPrimarySession, dialogs, handleSessionRemoved, }: { sortedSessions: TaskSession[]; profilesById: Record; agentLabelsById: Record; taskId: string; isPrimarySession: (session: TaskSession) => boolean; dialogs: PreviewDialogsState; handleSessionRemoved: (sessionId: string) => void; }): SessionTab[] { return useMemo( () => sortedSessions.map((session) => { const profile = session.agent_profile_id ? profilesById[session.agent_profile_id] : null; // Custom name wins over the agent-derived label, mirroring the // full-page tab title (resolveSessionTabTitle) so a rename is // visible here, not just persisted. const label = session.name || resolveProfileSubLabel(session, profile, agentLabelsById); return { id: session.id, label, icon: isSessionActive(session.state) ? ( ) : ( ), testId: `preview-session-tab-${session.id}`, className: PREVIEW_TAB_CLASS_NAME, renderContextMenu: () => ( ), content: dialogs.renamingSessionId === session.id ? ( dialogs.setRenamingSessionId(null)} testId="preview-session-tab-rename-input" maxLength={MAX_SESSION_NAME_LENGTH} /> ) : undefined, }; }), [ sortedSessions, agentLabelsById, profilesById, taskId, isPrimarySession, dialogs, handleSessionRemoved, ], ); } /** Hoisted delete/share/handoff dialogs, keyed off whichever session is * currently the target (see `usePreviewSessionTabDialogs`). */ function PreviewSessionTabDialogHost({ dialogs, sortedSessions, profilesById, agentLabelsById, taskId, isPrimarySession, }: { dialogs: PreviewDialogsState; sortedSessions: TaskSession[]; profilesById: Record; agentLabelsById: Record; taskId: string; isPrimarySession: (session: TaskSession) => boolean; }) { const deleteTargetSession = dialogs.menuDeleteState ? (sortedSessions.find((s) => s.id === dialogs.menuDeleteState?.sessionId) ?? null) : null; const deleteTargetName = deleteTargetSession ? deleteTargetSession.name || resolveProfileSubLabel( deleteTargetSession, deleteTargetSession.agent_profile_id ? profilesById[deleteTargetSession.agent_profile_id] : null, agentLabelsById, ) : null; return ( { if (!open) dialogs.setShareSessionId(null); }} handoffOpen={dialogs.handoffOpen} setHandoffOpen={dialogs.setHandoffOpen} handoffPreset={dialogs.handoffPreset} setHandoffPreset={dialogs.setHandoffPreset} groupId={undefined} /> ); } /** * Session tabs for the kanban preview panel. * * Right-click a tab for the same lifecycle context menu as the full-page * task view (rename, set primary, stop/resume, delete, share, handoff). * Creating a new session is still restricted to the full-page view. */ // eslint-disable-next-line max-lines-per-function -- preview owns the shared session and Plan-tab composition. export function PreviewSessionTabs({ taskId, sessionId, isArchived, ensureSession, workspaceId, onSessionChange, }: PreviewSessionTabsProps) { const { t } = useTranslation(); const { sessions, isLoaded } = useTaskSessions(taskId); const agentProfiles = useAppStore((state) => state.agentProfiles.items); const primarySessionId = useAppStore( (state) => ( state.kanban.tasks.find((task) => task.id === taskId) ?? findTaskInSnapshots(taskId, state.kanbanMulti.snapshots) )?.primarySessionId ?? null, ); const sortedSessions = useMemo(() => sortSessions(sessions), [sessions]); const agentLabelsById = useMemo(() => buildAgentLabelsById(agentProfiles), [agentProfiles]); const profilesById = useMemo( () => Object.fromEntries(agentProfiles.map((p) => [p.id, p])), [agentProfiles], ); const isPrimarySession = useCallback( (session: TaskSession) => primarySessionId ? primarySessionId === session.id : session.is_primary === true, [primarySessionId], ); const activeSessionId = useMemo( () => pickActiveSessionId(sortedSessions, sessionId), [sortedSessions, sessionId], ); const activeSession = useMemo( () => sortedSessions.find((s) => s.id === activeSessionId) ?? null, [sortedSessions, activeSessionId], ); // Mirrors the full-page task view: ensure the backend execution for the // active session is ready (resumes / restores workspace after a kandev // restart where the session row is persisted but agentctl isn't alive). const resumption = useSessionResumption(taskId, activeSessionId, isArchived ?? null); const dialogs = usePreviewSessionTabDialogs(taskId, sortedSessions); // `handleSessionRemoved` is captured once by `useSessionActions`'s `remove` // closure at the moment delete is confirmed, and `session.delete` can take // a WS round-trip to settle. Reading these off refs (updated every render) // instead of closing over `activeSessionId`/`sortedSessions` means the // eventual `onDeleted` call sees which tab is active *now*, not which one // was active when the delete was requested. const activeSessionIdRef = useRef(activeSessionId); activeSessionIdRef.current = activeSessionId; const sortedSessionsRef = useRef(sortedSessions); sortedSessionsRef.current = sortedSessions; const handleSessionRemoved = useCallback( (removedSessionId: string) => { if (removedSessionId !== activeSessionIdRef.current) return; const remaining = sortedSessionsRef.current.filter((s) => s.id !== removedSessionId); onSessionChange?.(remaining[0]?.id ?? null); }, [onSessionChange], ); // Do not force the Plan view while the session list is still loading. The // first render can have zero sessions before the list arrives, and treating // that transient state as sessionless would mark an unseen plan as seen. const hasNoSessions = isLoaded && sortedSessions.length === 0; const planTabState = usePreviewPlanTab(taskId, hasNoSessions, onSessionChange); const { viewMode, planTab, hasPlan } = planTabState; const sessionTabs = useBuildPreviewTabs({ sortedSessions, profilesById, agentLabelsById, taskId, isPrimarySession, dialogs, handleSessionRemoved, }); const tabs = useMemo(() => [...sessionTabs, planTab], [sessionTabs, planTab]); if (!isLoaded && sortedSessions.length === 0) { return ; } if (hasNoSessions && !hasPlan) { return ( ); } // A task can lose every session (deletion doesn't delete its plan) and // still have a plan worth showing — fall back to the Plan view rather than // an empty session body when there's nothing else to select. return (
void resumption.resumeSession()} retryDisabled={ resumption.resumptionState === "checking" || resumption.resumptionState === "resuming" } workspaceId={workspaceId ?? null} />
); } /** Renders the Plan tab's content or the active session's chat, per `viewMode`. */ function PreviewTabBody({ viewMode, planState, activeSession, taskId, }: { viewMode: "session" | "plan"; planState: ReturnType; activeSession: TaskSession | null; taskId: string; }) { if (viewMode === "plan") { return ; } if (!activeSession) return null; return ; } /** * Owns the preview panel's Plan tab: local session-vs-plan view mode, the * unseen-plan indicator (mirrors `plan-tab.tsx`'s dot logic, scoped to this * task instead of the global active task), and clearing it on selection. */ function usePreviewPlanTab( taskId: string, hasNoSessions: boolean, onSessionChange?: (sessionId: string | null) => void, ) { const { t } = useTranslation(); const planState = usePreviewPlanSummary(taskId); const { plan, loaded, failed, retry } = planState; const hasPlan = loaded && plan !== null; const lastSeenPlanAt = useAppStore((state) => state.taskPlans.lastSeenUpdatedAtByTaskId[taskId]); const markTaskPlanSeen = useAppStore((state) => state.markTaskPlanSeen); const hasUnseenPlan = plan?.created_by === "agent" && lastSeenPlanAt !== plan.updated_at; const [viewMode, setViewMode] = useState<"session" | "plan">("session"); // A new preview task shouldn't inherit the previous task's Plan selection. // Reset during render (not a passive effect) because `PreviewSessionTabs` // is reused across tasks on card click without remounting: a passive // effect runs after the layout effect below, so it would still see // `viewMode === "plan"` on the taskId-change render and mark the new // task's plan seen before the reset took effect. const [prevTaskId, setPrevTaskId] = useState(taskId); if (prevTaskId !== taskId) { setPrevTaskId(taskId); setViewMode("session"); } const displayViewMode = hasNoSessions && hasPlan ? "plan" : viewMode; // While the Plan tab is the active view, re-mark seen whenever the plan's // updated_at changes. Covers both clicking Plan before the fetch resolves // (nothing is marked seen until the plan actually arrives) and a WS plan // update landing while the tab is already open. useLayoutEffect so the // seen-mark commits before paint, matching plan-tab.tsx's dot logic. const planUpdatedAt = plan?.updated_at; useLayoutEffect(() => { if (displayViewMode === "plan" && planUpdatedAt !== undefined) { markTaskPlanSeen(taskId); } }, [displayViewMode, taskId, markTaskPlanSeen, planUpdatedAt]); const handleTabChange = useCallback( (id: string) => { if (id === PLAN_TAB_ID) { setViewMode("plan"); if (loaded) markTaskPlanSeen(taskId); // A prior fetch for this task failed; re-selecting the tab is the // user's explicit signal to try again, since the panel is reused // across tasks without remounting and would otherwise stay stuck. else if (failed) retry(); } else { setViewMode("session"); onSessionChange?.(id); } }, [taskId, loaded, failed, retry, markTaskPlanSeen, onSessionChange], ); const planTab = useMemo( () => ({ id: PLAN_TAB_ID, label: t("task:plan"), icon: , testId: "preview-plan-tab", className: PREVIEW_TAB_CLASS_NAME, }), [t, hasUnseenPlan], ); return { viewMode: displayViewMode, handleTabChange, planTab, planState, hasPlan }; } function resolveProfileSubLabel( session: TaskSession, profile: AgentProfileOption | null | undefined, agentLabelsById: Record, ): string { const fullLabel = profile?.label ?? resolveAgentLabelFor(session, agentLabelsById); const parts = fullLabel.split(LABEL_SEPARATOR); return parts[1] ?? parts[0] ?? fullLabel; } function SessionAgentLogo({ profile }: { profile: AgentProfileOption | null | undefined }) { if (!profile?.agent_name) { // Keep tabs visually aligned when the agent profile is missing/unknown. return (