Files
t3code/apps/web/src/components/ChatView.tsx
T

9199 lines
349 KiB
TypeScript

import { useLoadBalancedEnvironment } from "../hooks/useLoadBalancedEnvironment";
import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
import type { UsageLimitSourceSnapshots } from "@t3tools/contracts";
import {
collectProviderUsageLimits,
hasProviderUsageLimits,
isUsageLimitsCommand,
} from "@t3tools/shared/usageLimits";
import { feedbackBannerItem } from "./chat/ComposerFeedback";
import { usageLimitsBannerItem } from "./chat/ComposerUsageLimits";
import { derivePendingRequests } from "@t3tools/client-runtime/pending-requests";
import {
questionAttachmentDraftId,
questionAttachmentDraftPrefix,
clearQuestionAttachmentDraft,
useQuestionAttachmentPreparation,
} from "../questionAttachments";
import { useAttachmentUploadStore } from "../lib/attachmentUploadQueue";
import {
type AssistantCitation,
type ApprovalRequestId,
type ChatFileAttachment,
DEFAULT_MODEL,
type EnvironmentId,
type MessageId,
type ModelSelection,
type ProjectScript,
type ProjectId,
type ProviderApprovalDecision,
type PreviewAnnotationPayload,
ProviderInstanceId,
type ServerProvider,
type ResolvedKeybindingsConfig,
type ScopedThreadRef,
type ThreadId,
type ThreadLinkedPullRequest,
type TurnId,
type KeybindingCommand,
OrchestrationThreadActivity,
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
ProviderInteractionMode,
ProviderDriverKind,
resolveEnvironmentMachineKind,
RuntimeMode,
TerminalOpenInput,
} from "@t3tools/contracts";
import { type EnvironmentConnectionPresentation } from "@t3tools/client-runtime/connection";
import { wasBootstrapThreadDeleted } from "@t3tools/client-runtime/errors";
import { type CodexArtifactTemplate } from "@t3tools/client-runtime/codex-artifact-templates";
import { effectiveSnoozed, threadWokeAt } from "@t3tools/client-runtime/state/thread-settled";
import {
parseCodexFeedbackCommand,
submitCodexFeedback,
type CodexFeedbackSubmission,
} from "@t3tools/client-runtime/state/threads";
import {
parseScopedThreadKey,
scopedThreadKey,
scopeProjectRef,
scopeThreadRef,
} from "@t3tools/client-runtime/environment";
import {
applyClaudePromptEffortPrefix,
createModelSelection,
resolvePromptInjectedEffort,
} from "@t3tools/shared/model";
import {
projectScriptCwd,
projectScriptRuntimeEnv,
resolveProjectScripts,
} from "@t3tools/shared/projectScripts";
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
import { truncate } from "@t3tools/shared/String";
import { resolveThreadReferenceCopyTarget } from "@t3tools/shared/threadReference";
import {
getTerminalLabel,
nextTerminalId,
resolveTerminalSessionLabel,
} from "@t3tools/shared/terminalLabels";
import { Debouncer } from "@tanstack/react-pacer";
import { useAtomValue } from "@effect/atom-react";
import {
lazy,
memo,
Suspense,
useCallback,
useEffect,
useEffectEvent,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react";
import { flushSync } from "react-dom";
import { useLocation, useNavigate } from "@tanstack/react-router";
import { assistantCitationsToPlainText } from "@t3tools/shared/assistantCitations";
import { assistantCitationFromLocation } from "../lib/assistantCitationNavigation";
import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
import { useShallow } from "zustand/react/shallow";
import {
isAtomCommandInterrupted,
mapAtomCommandResult,
settlePromise,
squashAtomCommandFailure,
type AtomCommandResult,
} from "@t3tools/client-runtime/state/runtime";
import * as Cause from "effect/Cause";
import * as Schema from "effect/Schema";
import { AsyncResult } from "effect/unstable/reactivity";
import { isElectron } from "../env";
import { readLocalApi } from "../localApi";
import { useDiffPanelStore } from "../diffPanelStore";
import {
collapseExpandedComposerCursor,
type ComposerSubmissionIntent,
parseStandaloneComposerSlashCommand,
} from "../composer-logic";
import {
createMessageAttachmentPreviewProjector,
derivePhase,
deriveTimelineEntriesWithState,
deriveActiveWorkStartedAt,
deriveActivePlanState,
findLatestProposedPlan,
deriveWorkLogEntries,
hasActionableProposedPlan,
isLatestTurnSettled,
selectHandoffImageResources,
type TimelineEntriesProjection,
} from "../session-logic";
import { type LegendListRef } from "@legendapp/list/react";
import {
CHAT_TIMELINE_ANCHOR_OFFSET,
getAnchoredTurnMetrics,
timelineContentOverflowsViewport,
type TimelineScrollMode,
} from "./chat/timelineScrollAnchoring";
import {
buildPendingUserInputAnswers,
derivePendingUserInputProgress,
setPendingUserInputCustomAnswer,
togglePendingUserInputOptionSelection,
type PendingUserInputDraftAnswer,
} from "../pendingUserInput";
import { useUiStateStore } from "../uiStateStore";
import {
latestWorkspaceMutationId,
useWorkspaceMutationRefresh,
} from "../hooks/useWorkspaceMutationRefresh";
import {
buildPlanImplementationThreadTitle,
buildPlanImplementationPrompt,
resolvePlanFollowUpSubmission,
} from "../proposedPlan";
import {
DEFAULT_INTERACTION_MODE,
DEFAULT_THREAD_TERMINAL_ID,
MAX_TERMINALS_PER_GROUP,
type ChatMessage,
isBrowserPreviewAttachment,
isImageAttachment,
type SessionPhase,
type Thread,
} from "../types";
import { useTheme } from "../hooks/useTheme";
import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
import { isCommandPaletteOpen } from "../commandPaletteBus";
import { subscribeSnapShotComposerFocus } from "../lib/desktopSnapShot";
import { buildTemporaryWorktreeBranchName } from "@t3tools/shared/git";
import { useMediaQuery } from "../hooks/useMediaQuery";
import { RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY } from "../rightPanelLayout";
import {
pullRequestSurface,
selectActiveRightPanel,
selectActiveRightPanelSurface,
selectThreadRightPanelState,
type RightPanelSurface,
useRightPanelStore,
} from "../rightPanelStore";
import {
isPreviewSupportedInRuntime,
setActivePreviewTab,
useThreadPreviewState,
} from "../previewStateStore";
import { previewRuntimeTabId } from "../browser/previewRuntimeTabId";
import { BrowserSettingsReadError } from "../browser/openFileInPreview";
import { addBrowserSurface } from "./preview/addBrowserSurface";
import { closePreviewSession } from "./preview/closePreviewSession";
import { ThreadPreviewMiniPlayer } from "./preview/ThreadPreviewMiniPlayer";
import { subscribePreviewAction } from "./preview/previewActionBus";
import { getConfiguredPreviewUrls } from "./preview/previewEmptyStateLogic";
import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
import {
isSameSidebarThreadRef,
useSidebarPendingFileDropStore,
} from "../sidebarPendingFileDropStore";
import {
browserMiniPlayerSource,
previewMiniPlayerSourceKey,
selectThreadPreviewMiniPlayer,
usePreviewMiniPlayerStore,
} from "../previewMiniPlayerStore";
import { isThreadOwnPullRequest } from "./pullRequest/pullRequestDetail.logic";
import { PullRequestDetailPanel } from "./pullRequest/PullRequestDetailPanel";
import { PullRequestDetailGhost } from "./pullRequest/PullRequestGhosts";
import { PullRequestsUnavailableState } from "./pullRequest/PullRequestsUnavailableState";
import { RightPanelTabs } from "./RightPanelTabs";
import { AgentsPanel } from "./AgentsPanel";
import { LinkPullRequestDialogHost } from "./pullRequest/LinkPullRequestDialog";
import { ThreadPullRequestsPanel } from "./pullRequest/ThreadPullRequestsPanel";
import { useDeviceState } from "~/state/device";
import { DeviceSetup } from "./device/DeviceSetup";
import { Dialog } from "./ui/dialog";
import { WizardPopup } from "./ui/wizard";
import {
deriveAgentPanelModel,
foldSubagentActivities,
} from "@t3tools/client-runtime/state/subagentRuntime";
import { BranchToolbar } from "./BranchToolbar";
import { resolveShortcutCommand, shortcutLabelForCommand } from "../keybindings";
import ThreadTerminalDrawer from "./ThreadTerminalDrawer";
import {
AlarmClockIcon,
CheckCircle2Icon,
ChevronDownIcon,
GitBranchIcon,
Minimize2Icon,
PaperclipIcon,
WifiOffIcon,
} from "lucide-react";
import { cn, randomHex, randomUUID } from "~/lib/utils";
import { stackedThreadToast, toastManager } from "./ui/toast";
import { decodeProjectScriptKeybindingRule } from "~/lib/projectScriptKeybindings";
import { type NewProjectScriptInput } from "./ProjectScriptsControl";
import {
buildProjectScript,
commandForProjectScript,
nextProjectScriptId,
projectScriptIdFromCommand,
} from "~/projectScripts";
import { newDraftId, newMessageId, newThreadId } from "~/lib/utils";
import { useBrowserHistoryStore } from "~/browserHistoryStore";
import { registerFaviconProjectForThread } from "~/browserFaviconStore";
import { getProviderModelCapabilities } from "../providerModels";
import {
applyProviderInstanceSettings,
deriveProviderInstanceEntries,
NO_PROVIDER_MODEL_SELECTION,
sortProviderInstanceEntries,
} from "../providerInstances";
import {
useClientSettings,
useClientSettingsHydrated,
useEnvironmentSettings,
} from "../hooks/useSettings";
import { useNowMinute } from "../hooks/useNowMinute";
import { usePanelAnimationSettings, usePanelPresence } from "../panelAnimations";
import { useNewThreadHandler } from "../hooks/useHandleNewThread";
import { useOpenPanelPullRequestUrl } from "../hooks/useOpenPanelPullRequestUrl";
import { useThreadActions } from "../hooks/useThreadActions";
import { resolveAppModelSelectionForInstance } from "../modelSelection";
import { confirmTerminalClose, isTerminalCloseConfirmPending } from "../lib/terminalCloseConfirm";
import { isPreviewFocused } from "../lib/previewFocus";
import { getTerminalFocusOwner } from "../lib/terminalFocus";
import {
preventRepeatedTerminalCloseShortcut,
preventTerminalCloseShortcut,
} from "../lib/terminalCloseShortcut";
import { resolveNewDraftStartFromOrigin } from "../lib/chatThreadActions";
import {
derivePhysicalProjectKey,
deriveLogicalProjectKeyFromSettings,
selectProjectGroupingSettings,
} from "../logicalProject";
import { buildPhysicalToLogicalProjectKeyMap } from "../sidebarProjectGrouping";
import { buildDraftThreadRouteParams, buildThreadRouteParams } from "../threadRoutes";
import {
beginBackgroundDraftSubmissionByRef,
clearBackgroundDraftSubmissionByRef,
composerDraftHasUserContent,
type ComposerFileAttachment,
type ComposerImageAttachment,
type DraftThreadEnvMode,
finalizePromotedDraftThreadByRef,
markPromotedDraftThreadByRef,
useComposerDraftStore,
DraftId,
} from "../composerDraftStore";
import {
appendTerminalContextsToPrompt,
formatTerminalContextLabel,
type TerminalContextDraft,
type TerminalContextSelection,
} from "../lib/terminalContext";
import {
appendElementContextsToPrompt,
type ElementContextDraft,
formatElementContextLabel,
} from "../lib/elementContext";
import { appendPreviewAnnotationPrompt } from "../lib/previewAnnotation";
import { appendReviewCommentsToPrompt, type ReviewCommentContext } from "../reviewCommentContext";
import { environmentCatalog } from "../connection/catalog";
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
import { useKnownTerminalSessions, useThreadRunningTerminalIds } from "../state/terminalSessions";
import { useEnvironmentQuery } from "../state/query";
import {
environmentServerConfigsAtom,
primaryServerAvailableEditorsAtom,
primaryServerKeybindingsAtom,
serverEnvironment,
} from "../state/server";
import { terminalEnvironment } from "../state/terminal";
import { threadEnvironment, useEnvironmentThread } from "../state/threads";
import {
requestOlderThreadTurns,
threadHasOlderTurns,
} from "@t3tools/client-runtime/state/threads";
import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills";
import { vcsEnvironment } from "../state/vcs";
import { useEnvironments, usePrimaryEnvironment } from "../state/environments";
import {
useProject,
useProjects,
useThread,
useThreadRefs,
useThreadShell,
} from "../state/entities";
import { environmentShell } from "../state/shell";
import { ChatComposer, type ChatComposerHandle } from "./chat/ChatComposer";
import { createPageScrollController, type PageScrollKey } from "./chat/pageScrollController";
import { DraftHeroHeadline } from "./chat/DraftHeroHeadline";
import { ExpandedImageDialog } from "./chat/ExpandedImageDialog";
import { PullRequestThreadDialog } from "./PullRequestThreadDialog";
import { MessagesTimeline } from "./chat/MessagesTimeline";
import type { AssistantCitationRequest } from "./chat/AssistantCitationSource";
import { resolveTimelineIsAtEnd } from "./chat/MessagesTimeline.logic";
import { resolveComposerTimelineInset, resolveScrollToEndClearance } from "./composerFooterLayout";
import { ChatHeader } from "./chat/ChatHeader";
import { PanelLayoutControls, RightPanelMaximizeControl } from "./chat/PanelLayoutControls";
import { expandedImageKey, type ExpandedImagePreview } from "./chat/ExpandedImagePreview";
import { NoActiveThreadState } from "./NoActiveThreadState";
import { WorkspacePageHeader } from "./WorkspacePageHeader";
import {
type EnvironmentOption,
resolveEffectiveEnvMode,
resolveLocalCheckoutBranchMismatch,
shouldShowComposerContextStrip,
shouldShowEnvironmentIndicator,
} from "./BranchToolbar.logic";
import {
getProviderStatusBannerKey,
ProviderStatusBanner,
shouldShowProviderStatusBanner,
} from "./chat/ProviderStatusBanner";
import {
dismissThreadErrorBannerForSession,
getThreadErrorBannerKey,
isThreadErrorBannerDismissedForSession,
shouldShowThreadErrorBanner,
ThreadErrorBanner,
} from "./chat/ThreadErrorBanner";
import type { ComposerBannerStackItem } from "./chat/ComposerBannerStack";
import { ComposerSurface } from "./chat/ComposerSurface";
import {
hasAvailableCompactionProvider,
hasDismissedResumeCompaction,
shouldOfferResumeCompaction,
} from "./chat/ContextWindowMeter.logic";
import { deriveLatestContextWindowSnapshot, formatContextWindowTokens } from "../lib/contextWindow";
import {
DRAFT_HERO_TRANSITION_ANIMATION_ID,
DRAFT_HERO_TRANSITION_DURATION_MS,
DRAFT_HERO_TRANSITION_EASING,
MOBILE_COMPOSER_VIEW_TRANSITION_NAME,
MOBILE_DRAFT_HEADLINE_VIEW_TRANSITION_NAME,
runMobileComposerTransition,
} from "./chat/draftHeroTransition";
import {
MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
agentControlledBrowserCloseConfirmation,
branchMismatchKey,
buildExpiredTerminalContextToastCopy,
buildLocalDraftThread,
buildLoadingThreadFromShell,
buildRunningThreadTurnInterruptInput,
buildThreadTurnInterruptInput,
collectUserMessageBlobPreviewUrls,
createLocalDispatchSnapshot,
deriveComposerSendState,
dismissBranchMismatchForSession,
hasEnvironmentReconnectWarningGraceElapsed,
latestTurnStartFailureId,
scheduleEnvironmentReconnectWarning,
hasServerAcknowledgedLocalDispatch,
isBranchMismatchDismissedForSession,
shouldDockDraftHeroForSubmission,
shouldReleaseTimelineAnchorForToolActivity,
shouldShowBranchMismatchBanner,
shouldShowPlanFollowUpPrompt,
shouldOpenProactivePullRequest,
shouldRetargetThreadPullRequestPanel,
shouldOpenProactiveTurnDiff,
shouldRenderPreviewMiniPlayer,
getStartedThreadModelChangeBlockReason,
LAST_INVOKED_SCRIPT_BY_PROJECT_KEY,
LastInvokedScriptByProjectSchema,
type LocalDispatchSnapshot,
PullRequestDialogState,
cloneComposerImageForRetry,
deriveLockedProvider,
readFileAsDataUrl,
resolveFileAttachmentUrl,
prepareRevertedMessageAttachments,
waitForRevertedMessage,
reconcileMountedTerminalThreadIds,
recallCheckoutIsRepo,
rememberCheckoutIsRepo,
resolveBackgroundDraftWorkspaceOptions,
resolveComposerInteractionMode,
resolveComposerProviderSelection,
resolveDraftHeroState,
isPaintOnlyThreadTimeline,
peekHeldThreadTimeline,
peekRememberedThreadTimeline,
rememberReadyThreadTimeline,
resolveThreadSwitchTimeline,
timelineHasEphemeralPreviewUrls,
observeProactivePanelUserChoice,
resolveProactiveTurnDiffAction,
resolveThreadMetadataUpdateForNextTurn,
resolveSendEnvMode,
revokeBlobPreviewUrl,
revokeUserMessagePreviewUrls,
shouldWriteThreadErrorToCurrentServerThread,
startNewThreadForProject,
codexArtifactTemplatePromptToAppend,
toolGroupConsumesUpwardNavigation,
waitForStartedServerThread,
shouldRefocusComposerOnWindowFocus,
} from "./ChatView.logic";
import type { ThreadSyncPhase } from "../threadSync";
import { useLocalStorage } from "~/hooks/useLocalStorage";
import { useComposerHandleContext } from "../composerHandleContext";
import {
awaitAttachmentUploads,
getUploadedAttachments,
releaseDraftAttachments,
startAttachmentUpload,
} from "../lib/attachmentUploadQueue";
import { sanitizeThreadErrorMessage } from "~/rpc/transportError";
import { RightPanelSheet } from "./RightPanelSheet";
import { previewEnvironment } from "../state/preview";
import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments";
import { appAtomRegistry } from "../rpc/atomRegistry";
import { fileAttachmentCapabilityBlockReason } from "./chat/composerAttachmentFiles";
import { assetEnvironment } from "../state/assets";
import { readPreparedConnection } from "../state/session";
import { useAtomCommand } from "../state/use-atom-command";
import { useAtomQueryRunner } from "../state/use-atom-query-runner";
import { Button } from "./ui/button";
import {
AlertDialog,
AlertDialogClose,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogPopup,
AlertDialogTitle,
} from "./ui/alert-dialog";
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
import { ServerUpdateAction } from "./ServerUpdateAction";
import { useAutoBalanceUpdateBanner } from "./chat/useAutoBalanceUpdateBanner";
import {
ComposerServerUpdateIcon,
ComposerServerUpdateStatus,
} from "./chat/ComposerServerUpdateStatus";
import {
buildVersionMismatchDismissalKey,
dismissServerUpdateFailure,
dismissVersionMismatch,
isServerUpdateFailureDismissed,
isVersionMismatchDismissed,
resolveServerConfigVersionMismatch,
resolveServerSelfUpdateCapability,
serverUpdateGuidance,
supportsDesktopAppUpdate,
supportsServerUpdateThreadContinuation,
} from "../versionSkew";
import { useAssetUrls } from "../assets/assetUrls";
import {
ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
recallableComposerPrompt,
} from "./chat/composerPromptHistory";
const EMPTY_ACTIVITIES: OrchestrationThreadActivity[] = [];
const EMPTY_PROVIDERS: ServerProvider[] = [];
const EMPTY_USAGE_LIMIT_SOURCES: UsageLimitSourceSnapshots = [];
const EMPTY_PROVIDER_SKILLS: ServerProvider["skills"] = [];
const EMPTY_PENDING_USER_INPUT_ANSWERS: Record<string, PendingUserInputDraftAnswer> = {};
function useDraftHeroLayoutTransition(isDraftHeroState: boolean) {
const transitionGroupRef = useRef<HTMLDivElement | null>(null);
const composerAnchorRef = useRef<HTMLDivElement | null>(null);
const previousStateRef = useRef(isDraftHeroState);
const previousComposerRectRef = useRef<DOMRect | null>(null);
const animationRef = useRef<Animation | null>(null);
const attachTransitionGroupRef = (element: HTMLDivElement | null) => {
transitionGroupRef.current = element;
};
const attachComposerAnchorRef = (element: HTMLDivElement | null) => {
composerAnchorRef.current = element;
};
const captureComposerRect = () => {
previousComposerRectRef.current = composerAnchorRef.current?.getBoundingClientRect() ?? null;
};
useLayoutEffect(() => {
const transitionGroup = transitionGroupRef.current;
const nextComposerRect = composerAnchorRef.current?.getBoundingClientRect() ?? null;
const stateChanged = previousStateRef.current !== isDraftHeroState;
const prefersReducedMotion =
typeof window !== "undefined" &&
window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
const mobileComposerTransitionActive =
typeof document !== "undefined" &&
document.documentElement.dataset.mobileComposerRouteTransition === "true";
animationRef.current?.cancel();
animationRef.current = null;
const previousComposerRect = previousComposerRectRef.current;
if (
stateChanged &&
!prefersReducedMotion &&
!mobileComposerTransitionActive &&
transitionGroup &&
previousComposerRect &&
nextComposerRect &&
typeof transitionGroup.animate === "function"
) {
const translateX = previousComposerRect.left - nextComposerRect.left;
const translateY = previousComposerRect.top - nextComposerRect.top;
if (Math.abs(translateX) >= 0.5 || Math.abs(translateY) >= 0.5) {
const animation = transitionGroup.animate(
[
{ transform: `translate3d(${translateX}px, ${translateY}px, 0)` },
{ transform: "translate3d(0, 0, 0)" },
],
{
duration: DRAFT_HERO_TRANSITION_DURATION_MS,
easing: DRAFT_HERO_TRANSITION_EASING,
},
);
animation.id = DRAFT_HERO_TRANSITION_ANIMATION_ID;
animationRef.current = animation;
void animation.finished
.catch(() => undefined)
.then(() => {
if (animationRef.current !== animation) {
return;
}
animationRef.current = null;
});
}
}
previousStateRef.current = isDraftHeroState;
previousComposerRectRef.current = nextComposerRect;
}, [isDraftHeroState]);
return [attachTransitionGroupRef, attachComposerAnchorRef, captureComposerRect] as const;
}
const PreviewPanel = lazy(() =>
import("./preview/PreviewPanel").then((module) => ({ default: module.PreviewPanel })),
);
const DiffPanel = lazy(() => import("./DiffPanel"));
const selectAutoShowFloatingPreview = (settings: { browserAutoShowFloatingPreview: boolean }) =>
settings.browserAutoShowFloatingPreview;
const DevicePanel = lazy(() =>
import("./device/DevicePanel").then((module) => ({ default: module.DevicePanel })),
);
const FilePreviewPanel = lazy(() => import("./files/FilePreviewPanel"));
const EMPTY_PENDING_FILE_SURFACE_IDS: ReadonlySet<string> = new Set();
const TYPE_TO_FOCUS_EDITABLE_SELECTOR = [
"input",
"textarea",
"select",
'[contenteditable="true"]',
'[contenteditable="plaintext-only"]',
'[role="textbox"]',
].join(",");
const TYPE_TO_FOCUS_INTERACTIVE_SELECTOR = [
"button",
"a[href]",
"summary",
'[role="button"]',
'[role="checkbox"]',
'[role="menuitem"]',
'[role="option"]',
'[role="radio"]',
'[role="switch"]',
'[role="tab"]',
].join(",");
const TYPE_TO_FOCUS_FLOATING_LAYER_SELECTOR = [
'[role="dialog"][aria-modal="true"]',
'[data-slot="alert-dialog-popup"]:is([data-open],[data-ending-style])',
'[data-slot="command-dialog-popup"]:is([data-open],[data-ending-style])',
'[data-slot="dialog-popup"]:is([data-open],[data-ending-style])',
'[data-slot="sheet-popup"]:is([data-open],[data-ending-style])',
'[data-slot="sidebar"][data-mobile="true"]:is([data-open],[data-ending-style])',
'[data-slot="menu-popup"]',
'[data-slot="select-popup"]',
'[data-slot="popover-popup"]',
'[data-slot="combobox-popup"]',
'[data-slot="autocomplete-popup"]',
].join(",");
type EnvironmentUnavailableState = {
readonly environmentId: EnvironmentId;
readonly label: string;
readonly connection: EnvironmentConnectionPresentation;
};
function eventPathContainsSelector(event: Event, selector: string): boolean {
const path = event.composedPath();
if (path.length === 0 && event.target) {
path.push(event.target);
}
return path.some((target) => target instanceof Element && target.closest(selector));
}
/**
* Whether input that landed outside any editable or interactive element
* should be redirected into the composer. Shared by type-to-focus and
* paste-to-focus so both honour the same surfaces.
*/
function shouldRedirectInputToComposer(event: Event): boolean {
if (event.defaultPrevented) return false;
if (eventPathContainsSelector(event, TYPE_TO_FOCUS_EDITABLE_SELECTOR)) return false;
if (eventPathContainsSelector(event, TYPE_TO_FOCUS_INTERACTIVE_SELECTOR)) return false;
if (document.querySelector(TYPE_TO_FOCUS_FLOATING_LAYER_SELECTOR)) return false;
return true;
}
function shouldTypeToFocusComposer(event: KeyboardEvent): boolean {
if (event.isComposing) return false;
if (event.metaKey || event.ctrlKey || event.altKey) return false;
if (event.key.length !== 1) return false;
if (!shouldRedirectInputToComposer(event)) return false;
// The right-panel surface launcher claims its shortcut letters while it is
// visible (data attribute set in RightPanelTabs); those keys open surfaces
// instead of typing into the composer.
const launcherKeys = document
.querySelector("[data-surface-launcher-keys]")
?.getAttribute("data-surface-launcher-keys");
if (launcherKeys && launcherKeys.toLowerCase().includes(event.key.toLowerCase())) return false;
return true;
}
/**
* Plain text pasted with nothing editable focused, such as after the resting
* composer blurred. Files are left to the composer's own paste handler.
*/
function pasteTextToFocusComposer(event: ClipboardEvent): string | null {
if (!event.clipboardData || event.clipboardData.files.length > 0) return null;
if (!shouldRedirectInputToComposer(event)) return null;
const text = event.clipboardData.getData("text/plain");
return text.length > 0 ? text : null;
}
function formatOutgoingPrompt(params: {
provider: ProviderDriverKind;
model: string | null;
models: ReadonlyArray<ServerProvider["models"][number]>;
effort: string | null;
text: string;
}): string {
const caps = getProviderModelCapabilities(params.models, params.model, params.provider);
const promptEffort = resolvePromptInjectedEffort(caps, params.effort);
return applyClaudePromptEffortPrefix(params.text, promptEffort);
}
const SCRIPT_TERMINAL_COLS = 120;
const SCRIPT_TERMINAL_ROWS = 30;
function isCompactCommandMessage(message: ChatMessage): boolean {
const text = message.text.trim().toLowerCase();
return message.role === "user" && text === "/compact" && !message.attachments?.length;
}
type ChatViewProps =
| {
environmentId: EnvironmentId;
threadId: ThreadId;
onDiffPanelOpen?: () => void;
reserveTitleBarControlInset?: boolean;
forceExpandedMobileComposer?: boolean;
threadSyncPhase?: ThreadSyncPhase | null;
routeKind: "server";
draftId?: never;
}
| {
environmentId: EnvironmentId;
threadId: ThreadId;
onDiffPanelOpen?: () => void;
reserveTitleBarControlInset?: boolean;
forceExpandedMobileComposer?: boolean;
threadSyncPhase?: never;
routeKind: "draft";
draftId: DraftId;
};
interface TerminalLaunchContext {
threadId: ThreadId;
cwd: string;
worktreePath: string | null;
}
type PersistentTerminalLaunchContext = Pick<TerminalLaunchContext, "cwd" | "worktreePath">;
function useLocalDispatchState(input: {
activeThread: Thread | undefined;
activeLatestTurn: Thread["latestTurn"] | null;
phase: SessionPhase;
activePendingApproval: ApprovalRequestId | null;
activePendingUserInput: ApprovalRequestId | null;
threadError: string | null | undefined;
}) {
const [localDispatch, setLocalDispatch] = useState<LocalDispatchSnapshot | null>(null);
const latestUserMessage = input.activeThread?.messages.findLast(
(message) => message.role === "user",
);
const latestUserMessageId = latestUserMessage?.id ?? null;
const currentTurnStartFailureId =
localDispatch === null
? null
: latestTurnStartFailureId(input.activeThread, latestUserMessageId);
const resetLocalDispatch = useCallback(() => {
setLocalDispatch(null);
}, []);
const serverAcknowledgedLocalDispatch = useMemo(
() =>
hasServerAcknowledgedLocalDispatch({
localDispatch,
phase: input.phase,
latestTurn: input.activeLatestTurn,
latestUserMessageId,
session: input.activeThread?.session ?? null,
hasPendingApproval: input.activePendingApproval !== null,
hasPendingUserInput: input.activePendingUserInput !== null,
latestTurnStartFailureId: currentTurnStartFailureId,
threadError: input.threadError,
}),
[
input.activeLatestTurn,
input.activePendingApproval,
input.activePendingUserInput,
input.activeThread?.session,
input.phase,
input.threadError,
latestUserMessageId,
currentTurnStartFailureId,
localDispatch,
],
);
const activeLocalDispatch = serverAcknowledgedLocalDispatch ? null : localDispatch;
const beginLocalDispatch = useCallback(
(options?: { preparingWorktree?: boolean; submissionIntent?: ComposerSubmissionIntent }) => {
const preparingWorktree = Boolean(options?.preparingWorktree);
setLocalDispatch((current) => {
const active = serverAcknowledgedLocalDispatch ? null : current;
if (active) {
const submissionIntent = options?.submissionIntent ?? active.submissionIntent;
return active.preparingWorktree === preparingWorktree &&
active.submissionIntent === submissionIntent
? active
: { ...active, preparingWorktree, submissionIntent };
}
return createLocalDispatchSnapshot(input.activeThread, options);
});
},
[input.activeThread, serverAcknowledgedLocalDispatch],
);
return {
beginLocalDispatch,
resetLocalDispatch,
localDispatchStartedAt: activeLocalDispatch?.startedAt ?? null,
latestUserMessageAt: latestUserMessage?.createdAt ?? null,
isPreparingWorktree: activeLocalDispatch?.preparingWorktree ?? false,
isSendBusy: activeLocalDispatch !== null,
backgroundSubmissionPending: localDispatch?.submissionIntent === "background",
};
}
/** Same terminal ids (order ignored) — avoids reconcile when only server session ordering differs. */
function terminalIdListsEqual(left: readonly string[], right: readonly string[]): boolean {
if (left.length !== right.length) {
return false;
}
if (left.length === 0) {
return true;
}
const sortedLeft = left.toSorted((a, b) => a.localeCompare(b));
const sortedRight = right.toSorted((a, b) => a.localeCompare(b));
for (let index = 0; index < sortedLeft.length; index += 1) {
if (sortedLeft[index] !== sortedRight[index]) {
return false;
}
}
return true;
}
/**
* Server knows about fewer sessions than the client, but every server id still exists locally.
* Typical right after `terminal.open`: known-session list lags; reconciling would drop the new id
* and later re-add it as a separate group (no split layout).
*/
function serverTerminalIdsStrictSubsetOfClient(
serverIds: readonly string[],
clientIds: readonly string[],
): boolean {
if (serverIds.length >= clientIds.length || clientIds.length === 0) {
return false;
}
const clientSet = new Set(clientIds);
for (const id of serverIds) {
if (!clientSet.has(id)) {
return false;
}
}
return true;
}
interface PersistentThreadTerminalDrawerProps {
threadRef: { environmentId: EnvironmentId; threadId: ThreadId };
threadId: ThreadId;
active: boolean;
launchContext: PersistentTerminalLaunchContext | null;
focusRequestId: number;
splitShortcutLabel: string | undefined;
splitVerticalShortcutLabel: string | undefined;
newShortcutLabel: string | undefined;
closeShortcutLabel: string | undefined;
keybindings: ResolvedKeybindingsConfig;
onAddTerminalContext: (selection: TerminalContextSelection) => void;
}
const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerminalDrawer({
threadRef,
threadId,
active,
launchContext,
focusRequestId,
splitShortcutLabel,
splitVerticalShortcutLabel,
newShortcutLabel,
closeShortcutLabel,
keybindings,
onAddTerminalContext,
}: PersistentThreadTerminalDrawerProps) {
const openTerminal = useAtomCommand(terminalEnvironment.open, "terminal open");
const writeTerminal = useAtomCommand(terminalEnvironment.write, "terminal write");
const closeTerminalMutation = useAtomCommand(terminalEnvironment.close, "terminal close");
const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef));
const serverThread = useThread(threadRef, { waitForShell: draftThread !== null });
const projectRef = serverThread
? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
: draftThread
? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
: null;
const project = useProject(projectRef);
const terminalUiState = useTerminalUiStateStore((state) =>
selectThreadTerminalUiState(state.terminalUiStateByThreadKey, threadRef),
);
const visible = active && terminalUiState.terminalOpen;
const knownTerminalSessions = useKnownTerminalSessions({
environmentId: threadRef.environmentId,
threadId,
});
const panelSurfaces = useRightPanelStore(
(state) => selectThreadRightPanelState(state.byThreadKey, threadRef).surfaces,
);
const panelTerminalIds = useMemo(
() =>
new Set(
panelSurfaces.flatMap((surface) =>
surface.kind === "terminal" ? surface.terminalIds : [],
),
),
[panelSurfaces],
);
const drawerTerminalSessions = useMemo(
() =>
knownTerminalSessions.filter((session) => !panelTerminalIds.has(session.target.terminalId)),
[knownTerminalSessions, panelTerminalIds],
);
const terminalLabelsById = useMemo(() => {
const next = new Map<string, string>();
for (const session of drawerTerminalSessions) {
next.set(
session.target.terminalId,
resolveTerminalSessionLabel(session.target.terminalId, session.state.summary),
);
}
return next;
}, [drawerTerminalSessions]);
const terminalLaunchLocationsById = useMemo(() => {
const next = new Map<
string,
{
readonly cwd: string;
readonly worktreePath: string | null;
readonly runtimeEnv: Record<string, string>;
}
>();
if (!project) {
return next;
}
for (const session of drawerTerminalSessions) {
const summary = session.state.summary;
if (!summary) {
continue;
}
const worktreePathForLaunch =
launchContext !== null ? launchContext.worktreePath : summary.worktreePath;
next.set(session.target.terminalId, {
cwd: launchContext?.cwd ?? summary.cwd,
worktreePath: worktreePathForLaunch,
runtimeEnv: projectScriptRuntimeEnv({
project: { cwd: project.workspaceRoot },
worktreePath: worktreePathForLaunch,
}),
});
}
return next;
}, [drawerTerminalSessions, launchContext, project]);
const serverOrderedTerminalIds = useMemo(
() => drawerTerminalSessions.map((session) => session.target.terminalId),
[drawerTerminalSessions],
);
// Every client-side id source participates in allocation: the server list
// lags fresh opens, and panel terminals are filtered out of the drawer's
// sessions — an id collision attaches two viewports to one PTY session.
const allocatableTerminalIds = useMemo(
() => [
...new Set([
...serverOrderedTerminalIds,
...terminalUiState.terminalIds,
...panelTerminalIds,
]),
],
[panelTerminalIds, serverOrderedTerminalIds, terminalUiState.terminalIds],
);
const storeSetTerminalHeight = useTerminalUiStateStore((state) => state.setTerminalHeight);
const storeSplitTerminal = useTerminalUiStateStore((state) => state.splitTerminal);
const storeSplitTerminalVertical = useTerminalUiStateStore(
(state) => state.splitTerminalVertical,
);
const storeNewTerminal = useTerminalUiStateStore((state) => state.newTerminal);
const storeSetActiveTerminal = useTerminalUiStateStore((state) => state.setActiveTerminal);
const storeCloseTerminal = useTerminalUiStateStore((state) => state.closeTerminal);
const reconcileTerminalIds = useTerminalUiStateStore((state) => state.reconcileTerminalIds);
useEffect(() => {
if (terminalIdListsEqual(serverOrderedTerminalIds, terminalUiState.terminalIds)) {
return;
}
if (
serverTerminalIdsStrictSubsetOfClient(serverOrderedTerminalIds, terminalUiState.terminalIds)
) {
return;
}
reconcileTerminalIds(threadRef, serverOrderedTerminalIds);
}, [reconcileTerminalIds, serverOrderedTerminalIds, terminalUiState.terminalIds, threadRef]);
const [localFocusRequestId, setLocalFocusRequestId] = useState(0);
const worktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null;
const effectiveWorktreePath = useMemo(() => {
if (launchContext !== null) {
return launchContext.worktreePath;
}
return worktreePath;
}, [launchContext, worktreePath]);
const cwd = useMemo(
() =>
launchContext?.cwd ??
(project
? projectScriptCwd({
project: { cwd: project.workspaceRoot },
worktreePath: effectiveWorktreePath,
})
: null),
[effectiveWorktreePath, launchContext?.cwd, project],
);
const runtimeEnv = useMemo(
() =>
project
? projectScriptRuntimeEnv({
project: { cwd: project.workspaceRoot },
worktreePath: effectiveWorktreePath,
})
: {},
[effectiveWorktreePath, project],
);
const bumpFocusRequestId = useCallback(() => {
if (!visible) {
return;
}
setLocalFocusRequestId((value) => value + 1);
}, [visible]);
const setTerminalHeight = useCallback(
(height: number) => {
storeSetTerminalHeight(threadRef, height);
},
[storeSetTerminalHeight, threadRef],
);
const splitTerminal = useCallback(() => {
if (!cwd) {
return;
}
const terminalId = nextTerminalId(allocatableTerminalIds);
storeSplitTerminal(threadRef, terminalId);
bumpFocusRequestId();
void openTerminal({
environmentId: threadRef.environmentId,
input: {
threadId,
terminalId,
cwd,
...(effectiveWorktreePath != null ? { worktreePath: effectiveWorktreePath } : {}),
env: runtimeEnv,
},
});
}, [
allocatableTerminalIds,
bumpFocusRequestId,
cwd,
effectiveWorktreePath,
runtimeEnv,
storeSplitTerminal,
threadId,
threadRef,
openTerminal,
]);
const splitTerminalVertical = useCallback(() => {
if (!cwd) {
return;
}
const terminalId = nextTerminalId(allocatableTerminalIds);
storeSplitTerminalVertical(threadRef, terminalId);
bumpFocusRequestId();
void openTerminal({
environmentId: threadRef.environmentId,
input: {
threadId,
terminalId,
cwd,
...(effectiveWorktreePath != null ? { worktreePath: effectiveWorktreePath } : {}),
env: runtimeEnv,
},
});
}, [
allocatableTerminalIds,
bumpFocusRequestId,
cwd,
effectiveWorktreePath,
openTerminal,
runtimeEnv,
storeSplitTerminalVertical,
threadId,
threadRef,
]);
const createNewTerminal = useCallback(() => {
if (!cwd) {
return;
}
const terminalId = nextTerminalId(allocatableTerminalIds);
storeNewTerminal(threadRef, terminalId);
bumpFocusRequestId();
void openTerminal({
environmentId: threadRef.environmentId,
input: {
threadId,
terminalId,
cwd,
...(effectiveWorktreePath != null ? { worktreePath: effectiveWorktreePath } : {}),
env: runtimeEnv,
},
});
}, [
bumpFocusRequestId,
cwd,
effectiveWorktreePath,
allocatableTerminalIds,
runtimeEnv,
storeNewTerminal,
threadId,
threadRef,
openTerminal,
]);
const activateTerminal = useCallback(
(terminalId: string) => {
storeSetActiveTerminal(threadRef, terminalId);
bumpFocusRequestId();
},
[bumpFocusRequestId, storeSetActiveTerminal, threadRef],
);
const closeTerminal = useCallback(
(terminalId: string) => {
const fallbackExitWrite = () =>
writeTerminal({
environmentId: threadRef.environmentId,
input: { threadId, terminalId, data: "exit\n" },
});
void (async () => {
const closeResult = await closeTerminalMutation({
environmentId: threadRef.environmentId,
input: {
threadId,
terminalId,
deleteHistory: true,
},
});
if (closeResult._tag === "Failure" && !isAtomCommandInterrupted(closeResult)) {
await fallbackExitWrite();
}
})();
storeCloseTerminal(threadRef, terminalId);
bumpFocusRequestId();
},
[
bumpFocusRequestId,
storeCloseTerminal,
threadId,
threadRef,
closeTerminalMutation,
writeTerminal,
],
);
const handleAddTerminalContext = useCallback(
(selection: TerminalContextSelection) => {
if (!visible) {
return;
}
onAddTerminalContext(selection);
},
[onAddTerminalContext, visible],
);
if (!project || (!terminalUiState.terminalOpen && !active) || !cwd) {
return null;
}
return (
<div
className={cn(
"grid shrink-0 overflow-clip",
active ? (visible ? "grid-rows-[1fr]" : "grid-rows-[0fr]") : "hidden",
active &&
"[[data-panel-animations=true]_&]:transition-[grid-template-rows] [[data-panel-animations=true]_&]:[transition-duration:var(--panel-animation-duration)] [[data-panel-animations=true]_&]:ease-out",
active && visible && "[[data-panel-animations=true]_&]:starting:grid-rows-[0fr]!",
)}
>
<div className="min-h-0 overflow-clip">
<ThreadTerminalDrawer
threadRef={threadRef}
threadId={threadId}
cwd={cwd}
worktreePath={effectiveWorktreePath}
runtimeEnv={runtimeEnv}
visible={visible}
height={terminalUiState.terminalHeight}
// Known-session order is MRU and changes on focus; persisted store order keeps sidebar labels stable.
terminalIds={terminalUiState.terminalIds}
activeTerminalId={terminalUiState.activeTerminalId}
terminalGroups={terminalUiState.terminalGroups}
activeTerminalGroupId={terminalUiState.activeTerminalGroupId}
focusRequestId={focusRequestId + localFocusRequestId + (visible ? 1 : 0)}
onSplitTerminal={splitTerminal}
onSplitTerminalVertical={splitTerminalVertical}
onNewTerminal={createNewTerminal}
splitShortcutLabel={visible ? splitShortcutLabel : undefined}
splitVerticalShortcutLabel={visible ? splitVerticalShortcutLabel : undefined}
newShortcutLabel={visible ? newShortcutLabel : undefined}
closeShortcutLabel={visible ? closeShortcutLabel : undefined}
keybindings={keybindings}
onActiveTerminalChange={activateTerminal}
onCloseTerminal={closeTerminal}
onHeightChange={setTerminalHeight}
onAddTerminalContext={handleAddTerminalContext}
terminalLabelsById={terminalLabelsById}
terminalLaunchLocationsById={terminalLaunchLocationsById}
/>
</div>
</div>
);
});
interface PersistentThreadTerminalPanelProps {
visible: boolean;
threadRef: ScopedThreadRef;
surface: Extract<RightPanelSurface, { kind: "terminal" }>;
launchContext: PersistentTerminalLaunchContext | null;
focusRequestId: number;
keybindings: ResolvedKeybindingsConfig;
onAddTerminalContext: (selection: TerminalContextSelection) => void;
onSplitTerminal: () => void;
onSplitTerminalVertical: () => void;
onNewTerminal: () => void;
onActiveTerminalChange: (terminalId: string) => void;
onCloseTerminal: (terminalId: string) => void;
splitShortcutLabel?: string | undefined;
splitVerticalShortcutLabel?: string | undefined;
newShortcutLabel?: string | undefined;
closeShortcutLabel?: string | undefined;
}
const PersistentThreadTerminalPanel = memo(function PersistentThreadTerminalPanel({
visible,
threadRef,
surface,
launchContext,
focusRequestId,
keybindings,
onAddTerminalContext,
onSplitTerminal,
onSplitTerminalVertical,
onNewTerminal,
onActiveTerminalChange,
onCloseTerminal,
splitShortcutLabel,
splitVerticalShortcutLabel,
newShortcutLabel,
closeShortcutLabel,
}: PersistentThreadTerminalPanelProps) {
const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef));
const serverThread = useThread(threadRef, { waitForShell: draftThread !== null });
const projectRef = serverThread
? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
: draftThread
? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
: null;
const project = useProject(projectRef);
const knownTerminalSessions = useKnownTerminalSessions({
environmentId: threadRef.environmentId,
threadId: threadRef.threadId,
});
const threadWorktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null;
const activeSummary =
knownTerminalSessions.find((session) => session.target.terminalId === surface.activeTerminalId)
?.state.summary ?? null;
const worktreePath =
launchContext?.worktreePath ?? activeSummary?.worktreePath ?? threadWorktreePath;
const cwd = useMemo(
() =>
launchContext?.cwd ??
activeSummary?.cwd ??
(project
? projectScriptCwd({
project: { cwd: project.workspaceRoot },
worktreePath,
})
: null),
[activeSummary?.cwd, launchContext?.cwd, project, worktreePath],
);
const runtimeEnv = useMemo(
() =>
project
? projectScriptRuntimeEnv({
project: { cwd: project.workspaceRoot },
worktreePath,
})
: {},
[project, worktreePath],
);
const terminalLabelsById = useMemo(() => {
const labels = new Map<string, string>();
for (const terminalId of surface.terminalIds) {
const summary =
knownTerminalSessions.find((session) => session.target.terminalId === terminalId)?.state
.summary ?? null;
labels.set(terminalId, resolveTerminalSessionLabel(terminalId, summary));
}
return labels;
}, [knownTerminalSessions, surface.terminalIds]);
const terminalLaunchLocationsById = useMemo(() => {
const locations = new Map<
string,
{
readonly cwd: string;
readonly worktreePath: string | null;
readonly runtimeEnv: Record<string, string>;
}
>();
for (const terminalId of surface.terminalIds) {
const summary =
knownTerminalSessions.find((session) => session.target.terminalId === terminalId)?.state
.summary ?? null;
const terminalWorktreePath =
launchContext?.worktreePath ?? summary?.worktreePath ?? threadWorktreePath;
const terminalCwd =
launchContext?.cwd ??
summary?.cwd ??
(project
? projectScriptCwd({
project: { cwd: project.workspaceRoot },
worktreePath: terminalWorktreePath,
})
: null);
if (!terminalCwd || !project) continue;
locations.set(terminalId, {
cwd: terminalCwd,
worktreePath: terminalWorktreePath,
runtimeEnv: projectScriptRuntimeEnv({
project: { cwd: project.workspaceRoot },
worktreePath: terminalWorktreePath,
}),
});
}
return locations;
}, [
knownTerminalSessions,
launchContext?.cwd,
launchContext?.worktreePath,
project,
surface.terminalIds,
threadWorktreePath,
]);
if (!project || !cwd) return null;
return (
<ThreadTerminalDrawer
mode="panel"
visible={visible}
threadRef={threadRef}
threadId={threadRef.threadId}
cwd={cwd}
worktreePath={worktreePath}
runtimeEnv={runtimeEnv}
height={0}
terminalIds={surface.terminalIds}
activeTerminalId={surface.activeTerminalId}
terminalGroups={[
{
id: surface.id,
terminalIds: surface.terminalIds,
...(surface.splitDirection === "vertical" ? { splitDirection: "vertical" as const } : {}),
},
]}
activeTerminalGroupId={surface.id}
focusRequestId={focusRequestId}
onSplitTerminal={onSplitTerminal}
onSplitTerminalVertical={onSplitTerminalVertical}
onNewTerminal={onNewTerminal}
splitShortcutLabel={splitShortcutLabel}
splitVerticalShortcutLabel={splitVerticalShortcutLabel}
newShortcutLabel={newShortcutLabel}
closeShortcutLabel={closeShortcutLabel}
onActiveTerminalChange={onActiveTerminalChange}
onCloseTerminal={onCloseTerminal}
onHeightChange={() => undefined}
onAddTerminalContext={onAddTerminalContext}
terminalLabelsById={terminalLabelsById}
terminalLaunchLocationsById={terminalLaunchLocationsById}
keybindings={keybindings}
/>
);
});
// Errors surface through two maps (draft-keyed and thread-keyed) whose entries
// can race around promotion, so each write carries its time to let the latest
// one win when they collide.
type LocalThreadErrorEntry = {
readonly message: string | null;
readonly at: number;
};
function chatActionErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : "An error occurred.";
}
const ENVIRONMENT_UNAVAILABLE_SEND_TOAST_TRAIL_SIZE = 3;
const EMPTY_HELD_TURN_DIFF_SUMMARIES: readonly never[] = [];
const noopHeldTurnDiff = (_turnId: TurnId, _filePath?: string) => {};
const noopHeldRevert = (_targetTurnCount: number) => {};
const noopHeldAttachment = (_attachment: ChatFileAttachment) => {};
/**
* Drops the send-time anchored end space. That space is what holds a sent
* message near the top while its turn streams, and it keeps LegendList's
* maintainScrollAtEnd switched off for as long as it is installed — ChatView
* drives the streaming scrolls itself, but only in "anchoring-new-turn" mode.
* So every return to the live edge has to release the anchor too, otherwise the
* timeline settles into "following-end" with nothing following anything.
*/
function releaseChatTimelineAnchor<T extends { readonly messageId: MessageId | null }>(
current: T,
): T {
return current.messageId === null ? current : { ...current, messageId: null };
}
export default function ChatView(props: ChatViewProps) {
const {
environmentId,
threadId,
routeKind,
onDiffPanelOpen,
reserveTitleBarControlInset = true,
forceExpandedMobileComposer = false,
} = props;
const draftId = routeKind === "draft" ? props.draftId : null;
const threadSyncPhase = routeKind === "server" ? (props.threadSyncPhase ?? null) : null;
const threadDetailLoading = threadSyncPhase === "loading";
const handleNewThread = useNewThreadHandler();
const { settleThread, pinThread, confirmAndUnpinThread } = useThreadActions();
const routeThreadRef = useMemo(
() => scopeThreadRef(environmentId, threadId),
[environmentId, threadId],
);
const routeThreadKey = useMemo(() => scopedThreadKey(routeThreadRef), [routeThreadRef]);
const currentRouteThreadKeyRef = useRef<string | null>(routeThreadKey);
useLayoutEffect(() => {
currentRouteThreadKeyRef.current = routeThreadKey;
return () => {
currentRouteThreadKeyRef.current = null;
};
}, [routeThreadKey]);
const updateProjectScriptSettings = useAtomCommand(serverEnvironment.updateSettings, {
reportFailure: false,
});
const upsertKeybinding = useAtomCommand(serverEnvironment.upsertKeybinding, {
reportFailure: false,
});
const openTerminal = useAtomCommand(terminalEnvironment.open, "terminal open");
const writeTerminal = useAtomCommand(terminalEnvironment.write, "terminal write");
const closeTerminalMutation = useAtomCommand(terminalEnvironment.close, "terminal close");
const createThread = useAtomCommand(threadEnvironment.create, { reportFailure: false });
const deleteThread = useAtomCommand(threadEnvironment.delete, { reportFailure: false });
const updateThreadMetadata = useAtomCommand(threadEnvironment.updateMetadata, {
reportFailure: false,
});
const switchGitRef = useAtomCommand(vcsEnvironment.switchRef, { reportFailure: false });
const setThreadRuntimeMode = useAtomCommand(threadEnvironment.setRuntimeMode, {
reportFailure: false,
});
const setThreadInteractionMode = useAtomCommand(threadEnvironment.setInteractionMode, {
reportFailure: false,
});
const startThreadTurn = useAtomCommand(threadEnvironment.startTurn, { reportFailure: false });
const createAttachmentAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, {
reportFailure: false,
refresh: true,
});
const uploadThreadFeedback = useAtomCommand(threadEnvironment.uploadFeedback, {
reportFailure: false,
});
const interruptThreadTurn = useAtomCommand(threadEnvironment.interruptTurn, {
reportFailure: false,
});
const respondToThreadApproval = useAtomCommand(threadEnvironment.respondToApproval, {
reportFailure: false,
});
const respondToThreadUserInput = useAtomCommand(threadEnvironment.respondToUserInput, {
reportFailure: false,
});
const dismissThreadUserInput = useAtomCommand(threadEnvironment.dismissUserInput, {
reportFailure: false,
});
const revertThreadCheckpoint = useAtomCommand(threadEnvironment.revertCheckpoint, {
reportFailure: false,
});
const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
const closePreview = useAtomCommand(previewEnvironment.close, "preview close");
const { environments } = useEnvironments();
const primaryEnvironment = usePrimaryEnvironment();
const retryEnvironment = useAtomCommand(environmentCatalog.retryNow, { reportFailure: false });
const environmentById = useMemo(
() => new Map(environments.map((environment) => [environment.environmentId, environment])),
[environments],
);
const composerDraftTarget: ScopedThreadRef | DraftId =
routeKind === "server" ? routeThreadRef : props.draftId;
const draftThread = useComposerDraftStore((store) =>
routeKind === "server"
? store.getDraftSessionByRef(routeThreadRef)
: draftId
? store.getDraftSession(draftId)
: null,
);
const routeServerThreadShell = useThreadShell(routeKind === "server" ? routeThreadRef : null);
const serverThread = useThread(routeThreadRef, { waitForShell: draftThread !== null });
const loadingServerThread = useMemo(
() =>
threadDetailLoading && routeServerThreadShell
? buildLoadingThreadFromShell(routeServerThreadShell)
: null,
[routeServerThreadShell, threadDetailLoading],
);
const activeServerThread = serverThread ?? loadingServerThread;
// Pagination window state for the routed server thread: drives the
// "load earlier turns" header when the loaded window has older history.
const routeThreadState = useEnvironmentThread(
routeKind === "server" ? routeThreadRef.environmentId : null,
routeKind === "server" ? routeThreadRef.threadId : null,
);
const loadEarlierTurns = useMemo(() => {
if (routeKind !== "server" || !threadHasOlderTurns(routeThreadState)) {
return null;
}
return {
loading: routeThreadState.page._tag === "Some" && routeThreadState.page.value.loadingOlder,
cursor:
routeThreadState.page._tag === "Some" ? routeThreadState.page.value.beforeCursor : null,
onLoadEarlier: () => {
requestOlderThreadTurns(routeThreadRef.environmentId, routeThreadRef.threadId);
},
};
}, [routeKind, routeThreadRef, routeThreadState]);
const markThreadVisited = useUiStateStore((store) => store.markThreadVisited);
const settings = useEnvironmentSettings(environmentId);
const setStickyComposerModelSelection = useComposerDraftStore(
(store) => store.setStickyModelSelection,
);
const timestampFormat = settings.timestampFormat;
const navigate = useNavigate();
const citationLocation = useLocation({
select: (location) => ({
href: location.href,
key: location.state.assistantCitationActivation ?? location.state.__TSR_key,
}),
});
const citationRequest = useMemo<AssistantCitationRequest | null>(() => {
const citation = assistantCitationFromLocation(citationLocation.href);
return citation && citation.environmentId === environmentId && citation.threadId === threadId
? { citation, key: citationLocation.key ?? citationLocation.href }
: null;
}, [citationLocation.href, citationLocation.key, environmentId, threadId]);
const { resolvedTheme } = useTheme();
// Granular store selectors — avoid subscribing to prompt changes.
const composerRuntimeMode = useComposerDraftStore(
(store) => store.getComposerDraft(composerDraftTarget)?.runtimeMode ?? null,
);
const composerInteractionMode = useComposerDraftStore(
(store) => store.getComposerDraft(composerDraftTarget)?.interactionMode ?? null,
);
const composerActiveProvider = useComposerDraftStore(
(store) => store.getComposerDraft(composerDraftTarget)?.activeProvider ?? null,
);
const composerHasUnsentContent = useComposerDraftStore((store) =>
composerDraftHasUserContent(store.getComposerDraft(composerDraftTarget)),
);
const composerHasAttachments = useComposerDraftStore((store) => {
const draft = store.getComposerDraft(composerDraftTarget);
return (draft?.images.length ?? 0) > 0 || (draft?.files.length ?? 0) > 0;
});
// Anything beyond the prompt text: attachments, terminal or element contexts, annotations.
const composerHasNonPromptContent = useComposerDraftStore((store) => {
const draft = store.getComposerDraft(composerDraftTarget);
return draft ? composerDraftHasUserContent({ ...draft, prompt: "" }) : false;
});
const setComposerDraftPrompt = useComposerDraftStore((store) => store.setPrompt);
const addComposerDraftImages = useComposerDraftStore((store) => store.addImages);
const addComposerDraftFiles = useComposerDraftStore((store) => store.addFiles);
const setComposerDraftTerminalContexts = useComposerDraftStore(
(store) => store.setTerminalContexts,
);
const setComposerDraftElementContexts = useComposerDraftStore(
(store) => store.setElementContexts,
);
const setComposerDraftPreviewAnnotations = useComposerDraftStore(
(store) => store.setPreviewAnnotations,
);
const setComposerDraftReviewComments = useComposerDraftStore((store) => store.setReviewComments);
const setComposerDraftModelSelection = useComposerDraftStore((store) => store.setModelSelection);
const setComposerDraftRuntimeMode = useComposerDraftStore((store) => store.setRuntimeMode);
const setComposerDraftInteractionMode = useComposerDraftStore(
(store) => store.setInteractionMode,
);
const clearComposerDraftContent = useComposerDraftStore((store) => store.clearComposerContent);
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
const getDraftSessionByLogicalProjectKey = useComposerDraftStore(
(store) => store.getDraftSessionByLogicalProjectKey,
);
const getDraftSession = useComposerDraftStore((store) => store.getDraftSession);
const setLogicalProjectDraftThreadId = useComposerDraftStore(
(store) => store.setLogicalProjectDraftThreadId,
);
const promptRef = useRef("");
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
const composerFilesRef = useRef<ComposerFileAttachment[]>([]);
const composerTerminalContextsRef = useRef<TerminalContextDraft[]>([]);
const composerElementContextsRef = useRef<ElementContextDraft[]>([]);
const localComposerRef = useRef<ChatComposerHandle | null>(null);
const composerRef = useComposerHandleContext() ?? localComposerRef;
const [restingComposerControlsHost, setRestingComposerControlsHost] =
useState<HTMLDivElement | null>(null);
const [restingComposerControlsVisible, setRestingComposerControlsVisible] = useState(false);
const citeAssistantText = useCallback(
(citation: AssistantCitation, sourceAnchor: AssistantCitationSourceAnchor) => {
const inserted = composerRef.current?.citeAssistantText(citation, sourceAnchor) ?? false;
if (!inserted) {
toastManager.add({
type: "warning",
title: "The composer is not ready",
description:
"Try citing the selection after the connection or pending input is resolved.",
});
}
return inserted;
},
[composerRef],
);
const [isWorkspaceFileDragActive, setIsWorkspaceFileDragActive] = useState(false);
const [showScrollToBottom, setShowScrollToBottom] = useState(false);
const [expandedImage, setExpandedImage] = useState<ExpandedImagePreview | null>(null);
useEffect(() => {
const item = expandedImage?.images[expandedImage.index];
if (item?.type !== "video" || item.src === null || !item.src.startsWith("blob:")) return;
const src = item.src;
return () => revokeBlobPreviewUrl(src);
}, [expandedImage]);
const [optimisticUserMessages, setOptimisticUserMessages] = useState<ChatMessage[]>([]);
const [feedbackSubmissionsByThreadKey, setFeedbackSubmissionsByThreadKey] = useState<
Record<string, ReadonlyArray<CodexFeedbackSubmission>>
>({});
const feedbackSubmissions = useMemo(
() => feedbackSubmissionsByThreadKey[routeThreadKey] ?? [],
[feedbackSubmissionsByThreadKey, routeThreadKey],
);
const feedbackUploading = feedbackSubmissions.some(
(submission) => submission.status === "uploading",
);
const optimisticUserMessagesRef = useRef(optimisticUserMessages);
optimisticUserMessagesRef.current = optimisticUserMessages;
const [localDraftErrorsByDraftId, setLocalDraftErrorsByDraftId] = useState<
Record<string, LocalThreadErrorEntry>
>({});
const [localServerErrorsByThreadKey, setLocalServerErrorsByThreadKey] = useState<
Record<string, LocalThreadErrorEntry>
>({});
const [isConnecting, _setIsConnecting] = useState(false);
const isRevertingCheckpoint = useComposerDraftStore((store) =>
store.rewindingThreadKeys.has(routeThreadKey),
);
const [maximizedRightPanelThreadKey, setMaximizedRightPanelThreadKey] = useState<string | null>(
null,
);
const [respondingRequestIds, setRespondingRequestIds] = useState<ApprovalRequestId[]>([]);
const userInputResponsesInFlight = useRef(new Set<string>());
const [respondingUserInputRequestIds, setRespondingUserInputRequestIds] = useState<
ApprovalRequestId[]
>([]);
useEffect(() => {
setIsWorkspaceFileDragActive(false);
}, [draftId, routeThreadKey]);
useEffect(() => {
if (!isWorkspaceFileDragActive) return;
const clearWorkspaceFileDrag = () => setIsWorkspaceFileDragActive(false);
window.addEventListener("dragend", clearWorkspaceFileDrag);
return () => window.removeEventListener("dragend", clearWorkspaceFileDrag);
}, [isWorkspaceFileDragActive]);
const [pendingUserInputAnswersByRequestId, setPendingUserInputAnswersByRequestId] = useState<
Record<string, Record<string, PendingUserInputDraftAnswer>>
>({});
const [pendingUserInputQuestionIndexByRequestId, setPendingUserInputQuestionIndexByRequestId] =
useState<Record<string, number>>({});
const shouldUseRightPanelSheet = useMediaQuery(RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY);
const isMobileViewport = useMediaQuery("max-sm");
const [terminalFocusRequestId, setTerminalFocusRequestId] = useState(0);
const [pullRequestDialogState, setPullRequestDialogState] =
useState<PullRequestDialogState | null>(null);
const [terminalUiLaunchContext, setTerminalUiLaunchContext] =
useState<TerminalLaunchContext | null>(null);
const [attachmentPreviewHandoffByMessageId, setAttachmentPreviewHandoffByMessageId] = useState<
Record<string, string[]>
>({});
const [pendingServerThreadEnvMode, setPendingServerThreadEnvMode] =
useState<DraftThreadEnvMode | null>(null);
const [pendingServerThreadBranch, setPendingServerThreadBranch] = useState<string | null>();
const [
pendingServerThreadStartFromOriginByThreadId,
setPendingServerThreadStartFromOriginByThreadId,
] = useState<Record<string, boolean>>({});
const [lastInvokedScriptByProjectId, setLastInvokedScriptByProjectId] = useLocalStorage(
LAST_INVOKED_SCRIPT_BY_PROJECT_KEY,
{},
LastInvokedScriptByProjectSchema,
);
const legendListRef = useRef<LegendListRef | null>(null);
const getTimelineScrollableNode = useCallback(
() => legendListRef.current?.getScrollableNode() ?? null,
[],
);
const [composerOverlayElement, setComposerOverlayElement] = useState<HTMLDivElement | null>(null);
// Space the timeline keeps clear above its end. Tracks the overlay while the
// composer is expanded and holds that height while it rests, so the resting
// composer never exposes rows that its expansion will cover.
const [composerTimelineInset, setComposerTimelineInset] = useState(0);
const composerTimelineInsetRef = useRef(0);
const composerRestingRef = useRef(false);
const [scrollToEndClearance, setScrollToEndClearance] = useState(0);
const isAtEndRef = useRef(true);
const isTimelineAtLogicalEnd = useCallback(
() => resolveTimelineIsAtEnd(legendListRef.current?.getState()) ?? isAtEndRef.current,
[],
);
// Whether the timeline's rows extend past the viewport above the composer.
// The composer only rests when there is reading space to give back.
const [timelineOverflows, setTimelineOverflows] = useState(false);
const attachmentPreviewHandoffByMessageIdRef = useRef<Record<string, string[]>>({});
const attachmentPreviewPromotionInFlightByMessageIdRef = useRef<Record<string, true>>({});
const sendInFlightRef = useRef(false);
const environmentUnavailableSendToastSlotRef = useRef(0);
const feedbackUploadsInFlightRef = useRef(new Set<string>());
const terminalUiOpenByThreadRef = useRef<Record<string, boolean>>({});
const terminalUiState = useTerminalUiStateStore((state) =>
selectThreadTerminalUiState(state.terminalUiStateByThreadKey, routeThreadRef),
);
const openTerminalThreadKeys = useTerminalUiStateStore(
useShallow((state) =>
Object.entries(state.terminalUiStateByThreadKey).flatMap(
([nextThreadKey, nextTerminalUiState]) =>
nextTerminalUiState.terminalOpen ? [nextThreadKey] : [],
),
),
);
const storeSetTerminalOpen = useTerminalUiStateStore((s) => s.setTerminalOpen);
const storeEnsureTerminal = useTerminalUiStateStore((state) => state.ensureTerminal);
const storeSplitTerminal = useTerminalUiStateStore((s) => s.splitTerminal);
const storeSplitTerminalVertical = useTerminalUiStateStore((s) => s.splitTerminalVertical);
const storeNewTerminal = useTerminalUiStateStore((s) => s.newTerminal);
const storeSetActiveTerminal = useTerminalUiStateStore((s) => s.setActiveTerminal);
const storeCloseTerminal = useTerminalUiStateStore((s) => s.closeTerminal);
const serverThreadRefs = useThreadRefs();
const serverThreadKeys = useMemo(() => serverThreadRefs.map(scopedThreadKey), [serverThreadRefs]);
const draftThreadsByThreadKey = useComposerDraftStore((store) => store.draftThreadsByThreadKey);
const draftThreadKeys = useMemo(
() =>
Object.values(draftThreadsByThreadKey).map((draftThread) =>
scopedThreadKey(scopeThreadRef(draftThread.environmentId, draftThread.threadId)),
),
[draftThreadsByThreadKey],
);
const [mountedTerminalThreadKeys, setMountedTerminalThreadKeys] = useState<string[]>([]);
const mountedTerminalThreadRefs = useMemo(
() =>
mountedTerminalThreadKeys.flatMap((mountedThreadKey) => {
const mountedThreadRef = parseScopedThreadKey(mountedThreadKey);
return mountedThreadRef ? [{ key: mountedThreadKey, threadRef: mountedThreadRef }] : [];
}),
[mountedTerminalThreadKeys],
);
const fallbackDraftProjectRef = draftThread
? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
: null;
const fallbackDraftProject = useProject(fallbackDraftProjectRef);
const localDraftError = activeServerThread
? null
: ((draftId ? localDraftErrorsByDraftId[draftId]?.message : null) ?? null);
const localServerError = localServerErrorsByThreadKey[routeThreadKey]?.message ?? null;
// Draft errors are keyed by draftId while server errors are keyed by thread
// key, so a pending draft entry must migrate when the server thread loads or
// a failed send would silently disappear on promotion. When both keys hold
// an entry, the most recent write wins.
useEffect(() => {
if (!activeServerThread || !draftId) {
return;
}
const pendingDraftEntry = localDraftErrorsByDraftId[draftId];
if (pendingDraftEntry === undefined) {
return;
}
setLocalDraftErrorsByDraftId((existing) => {
if (existing[draftId] === undefined) {
return existing;
}
const next = { ...existing };
delete next[draftId];
return next;
});
setLocalServerErrorsByThreadKey((existing) => {
const currentEntry = existing[routeThreadKey];
if (
currentEntry !== undefined &&
(currentEntry.at > pendingDraftEntry.at ||
currentEntry.message === pendingDraftEntry.message)
) {
return existing;
}
return {
...existing,
[routeThreadKey]: pendingDraftEntry,
};
});
}, [activeServerThread, draftId, localDraftErrorsByDraftId, routeThreadKey]);
const localDraftThread = useMemo(
() =>
draftThread
? buildLocalDraftThread(
threadId,
draftThread,
resolveProjectSettings(
settings,
fallbackDraftProject?.id ?? null,
fallbackDraftProject ?? undefined,
).settings.defaultModelSelection ?? NO_PROVIDER_MODEL_SELECTION,
)
: undefined,
[draftThread, fallbackDraftProject, settings, threadId],
);
// Promotion is data-driven: the draft route keeps rendering while the
// server thread (same pre-allocated ref) starts, so live state must not
// depend on which route is mounted.
const isServerThread = activeServerThread !== null;
const activeThread = activeServerThread ?? localDraftThread;
const threadError = isServerThread
? (localServerError ?? activeServerThread?.session?.lastError ?? null)
: localDraftError;
// Dismissals can only mask the shown error, never clear it: a server thread
// keeps its error in session.lastError, so clearing the local shadow would
// just fall through to the persisted one. Mask the current error until a
// different error arrives, mirroring the provider status banner.
const threadErrorBannerKey = getThreadErrorBannerKey(routeThreadKey, threadError);
const visibleThreadError = shouldShowThreadErrorBanner(
routeThreadKey,
threadError,
isThreadErrorBannerDismissedForSession(threadErrorBannerKey),
)
? threadError
: null;
// Dismissing only mutates the session-scoped mask set, which does not
// trigger a render on its own; setThreadError(null) can also bail when the
// local shadow is already empty and the banner is driven purely by
// session.lastError. Bump a tick so the banner hides immediately. Mirrors
// the branch mismatch banner.
const [, setThreadErrorBannerDismissTick] = useState(0);
const defaultRuntimeMode = resolveProjectSettings(settings, activeThread?.projectId ?? null)
.settings.defaultRuntimeMode;
// Implicit drafts follow their current project/environment, including retargets.
// Explicit composer choices and existing server threads retain their permissions.
const runtimeMode = composerRuntimeMode ?? activeServerThread?.runtimeMode ?? defaultRuntimeMode;
const isLocalDraftThread = !isServerThread && localDraftThread !== undefined;
const canCheckoutPullRequestIntoThread = isLocalDraftThread;
const activeThreadId = activeThread?.id ?? null;
const activeThreadEnvironmentId = activeThread?.environmentId ?? null;
const runningTerminalIds = useThreadRunningTerminalIds({
environmentId: activeThread?.environmentId ?? null,
threadId: activeThreadId,
});
const activeThreadKnownSessionsRaw = useKnownTerminalSessions({
environmentId: activeThread?.environmentId ?? null,
threadId: activeThreadId,
});
const activeThreadKnownSessions = useMemo(() => {
if (activeThreadId === null) {
return [];
}
return activeThreadKnownSessionsRaw.filter(
(session) => session.target.threadId === activeThreadId,
);
}, [activeThreadId, activeThreadKnownSessionsRaw]);
const activeServerOrderedTerminalIds = useMemo(
() => activeThreadKnownSessions.map((session) => session.target.terminalId),
[activeThreadKnownSessions],
);
const activeKnownTerminalIds = useMemo(
() => [...new Set([...activeServerOrderedTerminalIds, ...terminalUiState.terminalIds])],
[activeServerOrderedTerminalIds, terminalUiState.terminalIds],
);
const activeTerminalLabelsById = useMemo(() => {
const labels = new Map<string, string>();
for (const session of activeThreadKnownSessions) {
labels.set(
session.target.terminalId,
resolveTerminalSessionLabel(session.target.terminalId, session.state.summary),
);
}
return labels;
}, [activeThreadKnownSessions]);
const activeThreadRef = useMemo(
() =>
activeThreadEnvironmentId && activeThreadId
? scopeThreadRef(activeThreadEnvironmentId, activeThreadId)
: null,
[activeThreadEnvironmentId, activeThreadId],
);
const activeThreadKey = activeThreadRef ? scopedThreadKey(activeThreadRef) : null;
const activeThreadShell = useThreadShell(isServerThread ? activeThreadRef : null);
const [timelineAnchor, setTimelineAnchor] = useState<{
readonly threadKey: string | null;
readonly messageId: MessageId | null;
}>({ threadKey: activeThreadKey, messageId: null });
if (timelineAnchor.threadKey !== activeThreadKey) {
setTimelineAnchor({ threadKey: activeThreadKey, messageId: null });
}
const timelineAnchorMessageId = timelineAnchor.messageId;
const activeRightPanelKind = useRightPanelStore((state) =>
selectActiveRightPanel(state.byThreadKey, activeThreadRef),
);
const diffOpen = activeRightPanelKind === "diff";
const rightPanelState = useRightPanelStore((state) =>
selectThreadRightPanelState(state.byThreadKey, activeThreadRef),
);
const activeRightPanelSurface = useRightPanelStore((state) =>
selectActiveRightPanelSurface(state.byThreadKey, activeThreadRef),
);
const activePreviewState = useThreadPreviewState(activeThreadRef);
const activePreviewServerEpoch = activePreviewState.serverEpoch;
const resolvePreviewRuntimeTabId = useMemo(
() =>
activeThreadRef
? (tabId: string) => previewRuntimeTabId(activeThreadRef, activePreviewServerEpoch, tabId)
: undefined,
[activeThreadRef, activePreviewServerEpoch],
);
const activePreviewMiniPlayer = usePreviewMiniPlayerStore((state) =>
selectThreadPreviewMiniPlayer(state.byThreadKey, activeThreadRef),
);
const panelTerminalIds = useMemo(
() =>
new Set(
rightPanelState.surfaces.flatMap((surface) =>
surface.kind === "terminal" ? surface.terminalIds : [],
),
),
[rightPanelState.surfaces],
);
const allocatableActiveTerminalIds = useMemo(
() => [...new Set([...activeKnownTerminalIds, ...panelTerminalIds])],
[activeKnownTerminalIds, panelTerminalIds],
);
const previewPanelOpen = activeRightPanelKind === "preview" && isPreviewSupportedInRuntime();
const rightPanelOpen = rightPanelState.isOpen;
const { active: panelAnimationsActive, durationMs: panelAnimationDurationMs } =
usePanelAnimationSettings();
const activeTerminalDrawerPresence = usePanelPresence(
Boolean(activeThreadKey && terminalUiState.terminalOpen),
true,
panelAnimationsActive,
activeThreadKey,
panelAnimationDurationMs,
);
const rightPanelPresenceValue = useMemo(
() => ({
activeSurface: activeRightPanelSurface,
surfaces: rightPanelState.surfaces,
}),
[activeRightPanelSurface, rightPanelState.surfaces],
);
const rightPanelPresence = usePanelPresence(
rightPanelOpen && activeThreadRef !== null,
rightPanelPresenceValue,
panelAnimationsActive,
activeThreadKey,
panelAnimationDurationMs,
);
const rightPanelPresent = rightPanelPresence.present;
const rightPanelControlsInPanel = shouldUseRightPanelSheet && rightPanelPresent && rightPanelOpen;
const rightPanelControlsAtRoot = rightPanelPresent && !shouldUseRightPanelSheet;
const renderedRightPanelSurface = rightPanelPresence.value?.activeSurface ?? null;
const renderedRightPanelSurfaces = rightPanelPresence.value?.surfaces ?? [];
const previewMiniPlayerVisible = shouldRenderPreviewMiniPlayer(
activePreviewMiniPlayer?.source ?? null,
renderedRightPanelSurface,
);
const canMaximizeRightPanel = rightPanelOpen && !shouldUseRightPanelSheet;
const rightPanelMaximized =
canMaximizeRightPanel && maximizedRightPanelThreadKey === routeThreadKey;
const inlineRightPanelOwnsTitleBar = rightPanelOpen && !shouldUseRightPanelSheet;
useEffect(() => {
if (!activeThreadRef) return;
useRightPanelStore
.getState()
.reconcileBrowserSurfaces(activeThreadRef, Object.keys(activePreviewState.sessions));
}, [activePreviewState.sessions, activeThreadRef]);
useEffect(() => {
if (!activeThreadRef || activePreviewMiniPlayer?.source.kind !== "browser") return;
const miniTabStillExists = Boolean(
activePreviewState.sessions[activePreviewMiniPlayer.source.tabId],
);
if (!miniTabStillExists) {
usePreviewMiniPlayerStore.getState().close(activeThreadRef);
}
}, [activePreviewMiniPlayer, activePreviewState.sessions, activeThreadRef]);
const existingOpenTerminalThreadKeys = useMemo(() => {
const existingThreadKeys = new Set<string>([...serverThreadKeys, ...draftThreadKeys]);
return openTerminalThreadKeys.filter((nextThreadKey) => existingThreadKeys.has(nextThreadKey));
}, [draftThreadKeys, openTerminalThreadKeys, serverThreadKeys]);
const activeLatestTurn = activeThread?.latestTurn ?? null;
const activeRunningTurnId =
(activeThread?.session?.status === "running" ? activeThread.session.activeTurnId : null) ??
(activeLatestTurn?.state === "running" ? activeLatestTurn.turnId : null);
// Reading a finished thread clears the sidebar's Done badge. The visit is
// stamped at the turn's completion time — not now/updatedAt — so it clears
// exactly the completion the user is looking at: a wake or completion that
// lands later still gets its signal (markThreadVisited never moves the
// timestamp backwards).
useEffect(() => {
const completedAt = serverThread?.latestTurn?.completedAt;
if (!serverThread?.id || !completedAt) return;
markThreadVisited(
scopedThreadKey(scopeThreadRef(serverThread.environmentId, serverThread.id)),
completedAt,
);
}, [
markThreadVisited,
serverThread?.environmentId,
serverThread?.id,
serverThread?.latestTurn?.completedAt,
]);
useEffect(() => {
setMountedTerminalThreadKeys((currentThreadIds) => {
const nextThreadIds = reconcileMountedTerminalThreadIds({
currentThreadIds,
openThreadIds: existingOpenTerminalThreadKeys,
activeThreadId: activeThreadKey,
activeThreadTerminalOpen: activeTerminalDrawerPresence.present,
maxHiddenThreadCount: MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
});
return currentThreadIds.length === nextThreadIds.length &&
currentThreadIds.every((nextThreadId, index) => nextThreadId === nextThreadIds[index])
? currentThreadIds
: nextThreadIds;
});
}, [activeTerminalDrawerPresence.present, activeThreadKey, existingOpenTerminalThreadKeys]);
const latestTurnSettled = isLatestTurnSettled(activeLatestTurn, activeThread?.session ?? null);
const activeProjectRef = useMemo(
() =>
activeThread ? scopeProjectRef(activeThread.environmentId, activeThread.projectId) : null,
[activeThread?.environmentId, activeThread?.projectId],
);
const activeProject = useProject(activeProjectRef);
// Environment settings with the active project's overrides applied.
const activeProjectSettings = useMemo(
() => resolveProjectSettings(settings, activeProject?.id ?? null, activeProject ?? undefined),
[activeProject, settings],
);
const activeProjectScripts = useMemo(
() => (activeProject ? resolveProjectScripts(settings, activeProject) : []),
[activeProject, settings],
);
const activeProjectDefaultModelSelection = activeProjectSettings.settings.defaultModelSelection;
const handleNewThreadInActiveProject = useCallback(() => {
startNewThreadForProject(activeProjectRef, handleNewThread);
}, [activeProjectRef, handleNewThread]);
const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);
const activeDraftLogicalProjectKey =
!isServerThread && activeProject
? deriveLogicalProjectKeyFromSettings(activeProject, projectGroupingSettings)
: undefined;
const handleOpenDraftProjectSettings = useCallback(() => {
if (!activeDraftLogicalProjectKey) return;
void navigate({
to: "/projects/$projectKey",
params: { projectKey: activeDraftLogicalProjectKey },
});
}, [activeDraftLogicalProjectKey, navigate]);
const activeEnvironmentShell = useEnvironmentQuery(
activeThread ? environmentShell.stateAtom(activeThread.environmentId) : null,
);
const activeEnvironmentBootstrapComplete = activeEnvironmentShell.data?.snapshot._tag === "Some";
const activeProjectKey = activeProject
? `${activeProject.environmentId}:${activeProject.workspaceRoot}`
: null;
const clientSettingsHydrated = useClientSettingsHydrated();
const [pendingFileSurfaceIdsByProject, setPendingFileSurfaceIdsByProject] = useState<
ReadonlyMap<string, ReadonlySet<string>>
>(() => new Map());
const pendingFileSurfaceIds = activeProjectKey
? (pendingFileSurfaceIdsByProject.get(activeProjectKey) ?? EMPTY_PENDING_FILE_SURFACE_IDS)
: EMPTY_PENDING_FILE_SURFACE_IDS;
const handleFilePendingChange = useCallback(
(relativePath: string, pending: boolean) => {
if (!activeProjectKey) return;
setPendingFileSurfaceIdsByProject((currentByProject) => {
const current = currentByProject.get(activeProjectKey) ?? EMPTY_PENDING_FILE_SURFACE_IDS;
const surfaceId = `file:${relativePath}`;
if (current.has(surfaceId) === pending) return currentByProject;
const next = new Set(current);
if (pending) next.add(surfaceId);
else next.delete(surfaceId);
const nextByProject = new Map(currentByProject);
if (next.size === 0) nextByProject.delete(activeProjectKey);
else nextByProject.set(activeProjectKey, next);
return nextByProject;
});
},
[activeProjectKey],
);
const configuredPreviewUrls = useMemo(
() => getConfiguredPreviewUrls(activeProjectScripts),
[activeProjectScripts],
);
useEffect(() => {
if (!activeThreadRef || !activeEnvironmentBootstrapComplete) return;
useRightPanelStore.getState().reconcileFileSurfaces(activeThreadRef, activeProject !== null);
}, [activeEnvironmentBootstrapComplete, activeProject, activeThreadRef]);
// Compute the list of environments this logical project spans, used to
// drive the environment picker in BranchToolbar.
const allProjects = useProjects();
const primaryEnvironmentId = primaryEnvironment?.environmentId ?? null;
useEffect(() => {
if (!activeThreadRef || !activeProjectRef) return;
registerFaviconProjectForThread(activeThreadRef, activeProjectRef);
}, [activeProjectRef, activeThreadRef]);
useEffect(() => {
if (!clientSettingsHydrated || !activeThreadRef || !activeProject) return;
// Reuse the sidebar's grouping so history follows the project rows the user
// sees. Deriving the key from the active project alone would miss the
// identity a duplicate row borrows from its siblings.
const logicalKeyByPhysicalKey = buildPhysicalToLogicalProjectKeyMap({
projects: allProjects,
settings: projectGroupingSettings,
primaryEnvironmentId,
});
useBrowserHistoryStore
.getState()
.registerThreadProject(
activeThreadRef,
logicalKeyByPhysicalKey.get(derivePhysicalProjectKey(activeProject)) ??
deriveLogicalProjectKeyFromSettings(activeProject, projectGroupingSettings),
);
}, [
activeProject,
activeThreadRef,
allProjects,
clientSettingsHydrated,
primaryEnvironmentId,
projectGroupingSettings,
]);
const activeEnvironment =
activeThread == null ? null : (environmentById.get(activeThread.environmentId) ?? null);
const activeEnvironmentConnectionPhase = activeEnvironment?.connection.phase ?? "available";
const activeEnvironmentUnavailable =
activeEnvironment !== null && activeEnvironmentConnectionPhase !== "connected";
const activeReconnectingEnvironmentId =
activeEnvironmentConnectionPhase === "connecting" ||
activeEnvironmentConnectionPhase === "reconnecting"
? (activeEnvironment?.environmentId ?? null)
: null;
const [reconnectWarningGraceElapsedEnvironmentId, setReconnectWarningGraceElapsedEnvironmentId] =
useState<EnvironmentId | null>(null);
const reconnectWarningGraceElapsed = hasEnvironmentReconnectWarningGraceElapsed(
activeReconnectingEnvironmentId,
reconnectWarningGraceElapsedEnvironmentId,
);
useEffect(() => {
setReconnectWarningGraceElapsedEnvironmentId(null);
if (activeReconnectingEnvironmentId === null) return;
return scheduleEnvironmentReconnectWarning(() =>
setReconnectWarningGraceElapsedEnvironmentId(activeReconnectingEnvironmentId),
);
}, [activeReconnectingEnvironmentId]);
const activeEnvironmentUnavailableLabel = activeEnvironment?.label ?? null;
const activeEnvironmentUnavailableState = useMemo<EnvironmentUnavailableState | null>(() => {
if (!activeEnvironmentUnavailable || !activeEnvironmentUnavailableLabel || !activeEnvironment) {
return null;
}
return {
environmentId: activeEnvironment.environmentId,
label: activeEnvironmentUnavailableLabel,
connection: activeEnvironment.connection,
};
}, [activeEnvironment, activeEnvironmentUnavailable, activeEnvironmentUnavailableLabel]);
const handleReconnectActiveEnvironment = useCallback(
async (environmentId: EnvironmentId) => {
const result = await retryEnvironment(environmentId);
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
toastManager.add(
stackedThreadToast({
type: "error",
title: "Could not reconnect environment",
description: error instanceof Error ? error.message : "Failed to reconnect.",
}),
);
}
},
[retryEnvironment],
);
const logicalProjectEnvironments = useMemo(() => {
if (!activeProject) return [];
const logicalKey = deriveLogicalProjectKeyFromSettings(activeProject, projectGroupingSettings);
const memberProjects = allProjects.filter(
(p) => deriveLogicalProjectKeyFromSettings(p, projectGroupingSettings) === logicalKey,
);
const seen = new Set<string>();
const envs: EnvironmentOption[] = [];
for (const p of memberProjects) {
if (seen.has(p.environmentId)) continue;
seen.add(p.environmentId);
const isPrimary = p.environmentId === primaryEnvironmentId;
const environment = environmentById.get(p.environmentId) ?? null;
envs.push({
environmentId: p.environmentId,
projectId: p.id,
label: environment?.label ?? p.environmentId,
isPrimary,
machine: resolveEnvironmentMachineKind(environment?.serverConfig ?? null),
});
}
// Sort: primary first, then alphabetical
envs.sort((a, b) => {
if (a.isPrimary !== b.isPrimary) return a.isPrimary ? -1 : 1;
return a.label.localeCompare(b.label);
});
return envs;
}, [activeProject, allProjects, projectGroupingSettings, primaryEnvironmentId, environmentById]);
const hasMultipleEnvironments = logicalProjectEnvironments.length > 1;
const activeEnvironmentOption =
logicalProjectEnvironments.find(
(environment) => environment.environmentId === activeThread?.environmentId,
) ?? null;
const showComposerEnvironmentIndicator = shouldShowEnvironmentIndicator({
activeEnvironment: activeEnvironmentOption,
canPickEnvironment: hasMultipleEnvironments,
});
const openPullRequestDialog = useCallback(
(reference?: string) => {
if (!canCheckoutPullRequestIntoThread) {
return;
}
setPullRequestDialogState({
initialReference: reference ?? null,
key: Date.now(),
});
},
[canCheckoutPullRequestIntoThread],
);
const closePullRequestDialog = useCallback(() => {
setPullRequestDialogState(null);
}, []);
const openOrReuseProjectDraftThread = useCallback(
async (input: { branch: string; worktreePath: string | null; envMode: DraftThreadEnvMode }) => {
if (!activeProject) {
throw new Error("No active project is available for this pull request.");
}
const activeProjectRef = scopeProjectRef(activeProject.environmentId, activeProject.id);
const logicalProjectKey = deriveLogicalProjectKeyFromSettings(
activeProject,
projectGroupingSettings,
);
const storedDraftSession = getDraftSessionByLogicalProjectKey(logicalProjectKey);
if (storedDraftSession) {
setDraftThreadContext(storedDraftSession.draftId, input);
setLogicalProjectDraftThreadId(
logicalProjectKey,
activeProjectRef,
storedDraftSession.draftId,
{
threadId: storedDraftSession.threadId,
...input,
},
);
if (routeKind !== "draft" || draftId !== storedDraftSession.draftId) {
await navigate({
to: "/draft/$draftId",
params: buildDraftThreadRouteParams(storedDraftSession.draftId),
});
}
return storedDraftSession.threadId;
}
const activeDraftSession = routeKind === "draft" && draftId ? getDraftSession(draftId) : null;
if (
!isServerThread &&
activeDraftSession?.logicalProjectKey === logicalProjectKey &&
draftId
) {
setDraftThreadContext(draftId, input);
setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, draftId, {
threadId: activeDraftSession.threadId,
createdAt: activeDraftSession.createdAt,
runtimeMode: activeDraftSession.runtimeMode,
interactionMode: activeDraftSession.interactionMode,
...input,
});
return activeDraftSession.threadId;
}
const nextDraftId = newDraftId();
const nextThreadId = newThreadId();
setLogicalProjectDraftThreadId(logicalProjectKey, activeProjectRef, nextDraftId, {
threadId: nextThreadId,
createdAt: new Date().toISOString(),
runtimeMode: resolveProjectSettings(settings, activeProject.id, activeProject).settings
.defaultRuntimeMode,
interactionMode: DEFAULT_INTERACTION_MODE,
...input,
});
await navigate({
to: "/draft/$draftId",
params: buildDraftThreadRouteParams(nextDraftId),
});
return nextThreadId;
},
[
activeProject,
draftId,
getDraftSession,
getDraftSessionByLogicalProjectKey,
isServerThread,
navigate,
projectGroupingSettings,
routeKind,
settings,
setDraftThreadContext,
setLogicalProjectDraftThreadId,
],
);
const handlePreparedPullRequestThread = useCallback(
async (input: { branch: string; worktreePath: string | null }) => {
await openOrReuseProjectDraftThread({
branch: input.branch,
worktreePath: input.worktreePath,
envMode: input.worktreePath ? "worktree" : "local",
});
},
[openOrReuseProjectDraftThread],
);
// Once a thread selects an environment, never substitute the primary
// environment's config while the selected environment is still loading.
const serverConfig = activeThread
? (activeEnvironment?.serverConfig ?? null)
: (primaryEnvironment?.serverConfig ?? null);
const providerStatuses = serverConfig?.providers ?? EMPTY_PROVIDERS;
const selectedProviderByThreadId = composerActiveProvider ?? null;
const threadProvider =
activeThread?.modelSelection.instanceId ??
activeProjectDefaultModelSelection?.instanceId ??
null;
const lockedProvider = deriveLockedProvider({
thread: activeThread,
selectedProvider: selectedProviderByThreadId,
threadProvider,
providers: providerStatuses,
});
const pullRequestsCapabilityKnown = serverConfig !== null;
const supportsPullRequests = serverConfig?.environment.capabilities.pullRequests === true;
const attachmentEnvironmentConfig = environmentById.get(environmentId)?.serverConfig ?? null;
const attachmentUploadsCapabilityKnown = attachmentEnvironmentConfig !== null;
const supportsQuestionAttachments =
attachmentEnvironmentConfig?.environment.capabilities.questionAttachments === true;
const supportsAttachmentUploads =
attachmentEnvironmentConfig?.environment.capabilities.attachmentUploads === true;
const advertisedFileAttachmentBytes =
attachmentEnvironmentConfig?.environment.capabilities.fileAttachments?.maxUploadBytes ?? null;
const maxFileAttachmentBytes =
advertisedFileAttachmentBytes === null
? null
: clampFileAttachmentUploadBytes(advertisedFileAttachmentBytes);
const envLocked = Boolean(
activeThread &&
(activeThread.messages.length > 0 ||
(activeThread.session !== null && activeThread.session.status !== "stopped")),
);
const loadBalancingSettings = useClientSettings();
const automaticEnvironment = Boolean(
clientSettingsHydrated &&
draftId &&
!envLocked &&
hasMultipleEnvironments &&
loadBalancingSettings.loadBalancingEnabled &&
draftThread?.environmentSelection !== "manual" &&
(!composerHasAttachments || Boolean(draftThread?.loadBalancedEnvironmentId)) &&
(!draftThread?.branch || draftThread.environmentSelection === "auto") &&
!draftThread?.worktreePath,
);
const autoUpdateEnvironments = useMemo(
() =>
automaticEnvironment
? logicalProjectEnvironments.flatMap(({ environmentId }) => {
const environment = environmentById.get(environmentId);
return environment ? [environment] : [];
})
: [],
[automaticEnvironment, logicalProjectEnvironments, environmentById],
);
const autoBalanceUpdateBanner = useAutoBalanceUpdateBanner(autoUpdateEnvironments);
const versionMismatch = resolveServerConfigVersionMismatch(serverConfig);
const versionMismatchDismissKey =
versionMismatch && activeThread
? buildVersionMismatchDismissalKey(activeThread.environmentId, versionMismatch)
: null;
const [dismissedVersionMismatchKey, setDismissedVersionMismatchKey] = useState<string | null>(
null,
);
const versionMismatchDismissed =
versionMismatchDismissKey === dismissedVersionMismatchKey ||
isVersionMismatchDismissed(versionMismatchDismissKey);
const showVersionMismatchBanner =
versionMismatch !== null && versionMismatchDismissKey !== null && !versionMismatchDismissed;
const hasMultipleRegisteredEnvironments = environments.length > 1;
const versionMismatchServerLabel =
hasMultipleRegisteredEnvironments && activeThread
? `${environmentById.get(activeThread.environmentId)?.label ?? serverConfig?.environment.label ?? activeThread.environmentId} server`
: "server";
const serverUpdateEnvironmentId = activeThread?.environmentId ?? null;
const versionMismatchSelfUpdate = resolveServerSelfUpdateCapability(serverConfig);
const versionMismatchDesktopAppUpdate = supportsDesktopAppUpdate(serverConfig);
const versionMismatchThreadContinuation = supportsServerUpdateThreadContinuation(serverConfig);
const serverUpdateState = useAtomValue(
serverEnvironment.updateStateAtom(serverUpdateEnvironmentId),
);
const [dismissedServerUpdateState, setDismissedServerUpdateState] = useState<
typeof serverUpdateState | null
>(null);
const serverUpdateFailureDismissed =
serverUpdateState === dismissedServerUpdateState ||
isServerUpdateFailureDismissed(serverUpdateState);
const systemComposerBannerItems = useMemo<ComposerBannerStackItem[]>(() => {
const items: ComposerBannerStackItem[] = [];
const updateRunning = serverUpdateState.status === "running";
const unavailableConnection = activeEnvironmentUnavailableState?.connection ?? null;
const environmentReconnecting =
unavailableConnection !== null &&
(unavailableConnection.phase === "connecting" ||
unavailableConnection.phase === "reconnecting");
// Reconnecting to a version-skewed server with no update in flight
// usually means the server is restarting mid-update and a refresh wiped
// the in-memory update state. Fold the reconnect and version banners
// into one calm line instead of stacking "Failed to connect" on
// "versions differ". A failed update never folds: its error and retry
// action must stay visible.
const reconnectingThroughVersionSkew =
serverUpdateState.status === "idle" && environmentReconnecting && versionMismatch !== null;
// While an update runs, transient connect blips are expected (the server
// restarts) and the update banner already shows progress. Hard failure
// phases still surface so the Reconnect action stays reachable.
const suppressUnavailableBanner =
environmentReconnecting &&
(updateRunning || (!reconnectingThroughVersionSkew && !reconnectWarningGraceElapsed));
if (activeEnvironmentUnavailableState && unavailableConnection && !suppressUnavailableBanner) {
if (reconnectingThroughVersionSkew) {
items.push({
id: `environment-unavailable:${activeEnvironmentUnavailableState.environmentId}`,
variant: "default",
// Prioritize live connection progress among the notices.
priority: "urgent",
icon: (
<span
className="size-1.5 animate-status-pulse rounded-full bg-foreground"
aria-hidden="true"
/>
),
title: `${unavailableConnection.phase === "connecting" ? "Connecting" : "Reconnecting"} to ${activeEnvironmentUnavailableState.label}`,
description: "Finishing an update",
});
} else {
items.push({
id: `environment-unavailable:${activeEnvironmentUnavailableState.environmentId}`,
variant: unavailableConnection.phase === "error" ? "error" : "warning",
icon: <WifiOffIcon />,
title: `${activeEnvironmentUnavailableState.label} is ${environmentReconnecting ? "reconnecting" : "offline"}`,
description: environmentReconnecting ? "Trying again" : "Reconnect to continue",
actions: (
<>
<Button
size="xs"
variant="ghost"
disabled={environmentReconnecting}
onClick={() =>
void handleReconnectActiveEnvironment(
activeEnvironmentUnavailableState.environmentId,
)
}
>
{environmentReconnecting ? "Reconnecting..." : "Reconnect"}
</Button>
<Button
size="xs"
variant="ghost"
onClick={() => void navigate({ to: "/settings/connections" })}
>
Connections
</Button>
</>
),
});
}
}
if (
!automaticEnvironment &&
serverUpdateEnvironmentId &&
!reconnectingThroughVersionSkew &&
(serverUpdateState.status === "idle"
? showVersionMismatchBanner
: !serverUpdateFailureDismissed)
) {
const updateInProgress = serverUpdateState.status === "running";
const updateFailed = serverUpdateState.status === "failed";
items.push({
id: `server-version:${serverUpdateEnvironmentId}`,
variant: updateFailed ? "error" : "default",
// Prioritize update progress over passive notices, but keep activity attached.
priority: updateInProgress ? "urgent" : "notice",
icon: <ComposerServerUpdateIcon status={serverUpdateState.status} />,
title:
updateInProgress || updateFailed ? (
<ComposerServerUpdateStatus
state={serverUpdateState}
serverLabel={versionMismatchServerLabel}
/>
) : versionMismatch ? (
<Tooltip>
<TooltipTrigger
render={
<button
type="button"
className="block max-w-full cursor-help truncate rounded-sm text-left"
>
Server update available
</button>
}
/>
<TooltipPopup side="top">
{versionMismatchServerLabel} {versionMismatch.serverVersion}{" "}
<span aria-hidden="true">→</span> {versionMismatch.clientVersion}
</TooltipPopup>
</Tooltip>
) : (
"Server update available"
),
description:
!updateInProgress &&
!updateFailed &&
versionMismatchSelfUpdate !== null &&
(versionMismatchSelfUpdate !== "desktop-managed" || !versionMismatchDesktopAppUpdate)
? serverUpdateGuidance(versionMismatchSelfUpdate)
: undefined,
actions:
updateInProgress ||
!versionMismatch ||
(versionMismatchSelfUpdate === "desktop-managed" &&
!versionMismatchDesktopAppUpdate) ? undefined : (
<ServerUpdateAction
environmentId={serverUpdateEnvironmentId}
serverLabel={versionMismatchServerLabel}
selfUpdate={versionMismatchSelfUpdate}
desktopAppUpdate={versionMismatchDesktopAppUpdate}
threadContinuation={versionMismatchThreadContinuation}
targetVersion={versionMismatch.clientVersion}
label={updateFailed ? "Retry" : "Update"}
variant="ghost"
/>
),
...(updateInProgress || (!updateFailed && !versionMismatchDismissKey)
? {}
: {
dismissLabel: "Dismiss update notice",
onDismiss: () => {
if (updateFailed) {
dismissServerUpdateFailure(serverUpdateState);
setDismissedServerUpdateState(serverUpdateState);
}
dismissVersionMismatch(versionMismatchDismissKey);
setDismissedVersionMismatchKey(versionMismatchDismissKey);
},
}),
});
}
if (autoBalanceUpdateBanner) items.push(autoBalanceUpdateBanner);
return items;
}, [
automaticEnvironment,
autoBalanceUpdateBanner,
activeEnvironmentUnavailableState,
reconnectWarningGraceElapsed,
handleReconnectActiveEnvironment,
navigate,
setDismissedVersionMismatchKey,
showVersionMismatchBanner,
serverUpdateFailureDismissed,
serverUpdateState,
versionMismatch,
versionMismatchDismissKey,
serverUpdateEnvironmentId,
versionMismatchSelfUpdate,
versionMismatchDesktopAppUpdate,
versionMismatchThreadContinuation,
versionMismatchServerLabel,
]);
const providerInstanceEntries = useMemo(
() =>
sortProviderInstanceEntries(
applyProviderInstanceSettings(deriveProviderInstanceEntries(providerStatuses), settings),
),
[providerStatuses, settings],
);
const { selectedProviderEntry, requestedDriverKind } = useMemo(
() =>
resolveComposerProviderSelection({
entries: providerInstanceEntries,
candidateInstanceIds: [
selectedProviderByThreadId,
activeThread?.session?.providerInstanceId,
activeThread?.modelSelection.instanceId,
activeProjectDefaultModelSelection?.instanceId,
],
lockedProvider,
lockedInstanceId:
activeThread?.session?.providerInstanceId ?? activeThread?.modelSelection.instanceId,
}),
[
activeProjectDefaultModelSelection?.instanceId,
activeThread?.modelSelection.instanceId,
activeThread?.session?.providerInstanceId,
lockedProvider,
providerInstanceEntries,
selectedProviderByThreadId,
],
);
const selectedProvider = selectedProviderEntry?.driverKind ?? requestedDriverKind;
const activeProviderInstanceId = selectedProviderEntry?.instanceId ?? null;
const activeProviderStatus = selectedProviderEntry?.snapshot ?? null;
const { enabled: interactionModeEnabled, interactionMode } = resolveComposerInteractionMode({
planModeEnabled: settings.planModeEnabled,
provider: activeProviderStatus,
interactionMode:
composerInteractionMode ?? activeThread?.interactionMode ?? DEFAULT_INTERACTION_MODE,
});
const conversationProviderStatus =
providerStatuses.find(
(status) => status.instanceId === activeThread?.session?.providerInstanceId,
) ?? activeProviderStatus;
const supportsConversationRollback =
conversationProviderStatus !== null &&
conversationProviderStatus.supportsConversationRollback !== false;
const phase = derivePhase(activeThread?.session ?? null);
const threadActivities = activeThread?.activities ?? EMPTY_ACTIVITIES;
const latestCheckpointCompletedAt = activeThread?.checkpoints.at(-1)?.completedAt ?? null;
const workspaceMutationId = useMemo(() => {
const activityId = latestWorkspaceMutationId(threadActivities);
return activityId === null && latestCheckpointCompletedAt === null
? null
: JSON.stringify([activityId, latestCheckpointCompletedAt]);
}, [latestCheckpointCompletedAt, threadActivities]);
const activeContextWindow = useMemo(
() => deriveLatestContextWindowSnapshot(threadActivities),
[threadActivities],
);
const workLogEntries = useMemo(() => deriveWorkLogEntries(threadActivities), [threadActivities]);
// Native subagent fold: memoized by activity-list identity, shared by the
// Agents surface, live strip, and workflow cards. v2Projection is null
// until orchestration-v2 lands (source precedence lives in the derive).
// sessionLive derives interruption for agents orphaned by session death.
const agentSessionLive = phase !== "disconnected";
const agentPanelModel = useMemo(
() =>
deriveAgentPanelModel({
agents: foldSubagentActivities(threadActivities, { sessionLive: agentSessionLive }),
}),
[agentSessionLive, threadActivities],
);
const { approvals: pendingApprovals, userInputs: pendingUserInputs } = useMemo(
() => derivePendingRequests(threadActivities),
[threadActivities],
);
const activePendingUserInput = pendingUserInputs[0] ?? null;
const activePendingRequestKey = JSON.stringify([
environmentId,
activeThreadId,
activePendingUserInput?.requestId,
]);
const pendingQuestionDraftKeys = useMemo(
() =>
activeThreadId
? pendingUserInputs.flatMap((request) =>
request.questions.map((question) =>
questionAttachmentDraftId(
environmentId,
activeThreadId,
request.requestId,
question.id,
),
),
)
: [],
[activeThreadId, environmentId, pendingUserInputs],
);
const questionComposerDrafts = useComposerDraftStore(
useShallow((state) =>
Object.fromEntries(
pendingQuestionDraftKeys.map((key) => [key, state.draftsByThreadKey[key]]),
),
),
);
const questionUploadsBlocked = useAttachmentUploadStore(
useShallow((state) =>
Object.fromEntries(
pendingQuestionDraftKeys.map((key) => {
const draft = questionComposerDrafts[key];
const attachments = draft ? [...draft.images, ...draft.files] : [];
return [
key,
attachments.some((attachment) => {
const upload = state.uploadsByImageId[attachment.id];
return upload?.status !== "ready" || upload.environmentId !== environmentId;
}),
];
}),
),
),
);
const questionPreparations = useQuestionAttachmentPreparation(
useShallow((state) =>
Object.fromEntries(pendingQuestionDraftKeys.map((key) => [key, state.counts[key] ?? 0])),
),
);
useEffect(() => {
if (routeThreadState.status !== "live" || routeThreadState.data._tag !== "Some") return;
const questionThread = routeThreadState.data.value;
const { userInputs: currentRequests } = derivePendingRequests(questionThread.activities);
const prefix = questionAttachmentDraftPrefix(environmentId, questionThread.id);
const retained = new Set(
currentRequests.flatMap((request) =>
request.questions.map((question) =>
questionAttachmentDraftId(
environmentId,
questionThread.id,
request.requestId,
question.id,
),
),
),
);
const keys = new Set([
...Object.keys(useComposerDraftStore.getState().draftsByThreadKey),
...Object.keys(useQuestionAttachmentPreparation.getState().counts),
]);
for (const key of keys) {
if (key.startsWith(prefix) && !retained.has(DraftId.make(key)))
clearQuestionAttachmentDraft(DraftId.make(key));
}
}, [environmentId, routeThreadState.data, routeThreadState.status]);
const activePendingDraftAnswers = useMemo(() => {
if (!activePendingUserInput || !activeThreadId) return EMPTY_PENDING_USER_INPUT_ANSWERS;
return Object.fromEntries(
activePendingUserInput.questions.map((question) => {
const key = questionAttachmentDraftId(
environmentId,
activeThreadId,
activePendingUserInput.requestId,
question.id,
);
const draft = questionComposerDrafts[key];
const attachments = draft ? [...draft.images, ...draft.files] : [];
return [
question.id,
{
...pendingUserInputAnswersByRequestId[activePendingRequestKey]?.[question.id],
attachmentCount: attachments.length,
attachmentsBlocked:
(attachments.length > 0 && !supportsQuestionAttachments) ||
(questionPreparations[key] ?? 0) > 0 ||
questionUploadsBlocked[key] === true,
},
];
}),
);
}, [
activePendingUserInput,
activeThreadId,
environmentId,
questionComposerDrafts,
questionUploadsBlocked,
supportsQuestionAttachments,
questionPreparations,
pendingUserInputAnswersByRequestId,
activePendingRequestKey,
]);
const activePendingQuestionIndex = activePendingUserInput
? (pendingUserInputQuestionIndexByRequestId[activePendingRequestKey] ?? 0)
: 0;
const activePendingProgress = useMemo(
() =>
activePendingUserInput
? derivePendingUserInputProgress(
activePendingUserInput.questions,
activePendingDraftAnswers,
activePendingQuestionIndex,
)
: null,
[activePendingDraftAnswers, activePendingQuestionIndex, activePendingUserInput],
);
const activePendingResolvedAnswers = useMemo(
() =>
activePendingUserInput
? buildPendingUserInputAnswers(activePendingUserInput.questions, activePendingDraftAnswers)
: null,
[activePendingDraftAnswers, activePendingUserInput],
);
const activePendingIsResponding = activePendingUserInput
? respondingUserInputRequestIds.includes(activePendingUserInput.requestId)
: false;
const activeProposedPlan = useMemo(() => {
if (!latestTurnSettled) {
return null;
}
return findLatestProposedPlan(
activeThread?.proposedPlans ?? [],
activeLatestTurn?.turnId ?? null,
);
}, [activeLatestTurn?.turnId, activeThread?.proposedPlans, latestTurnSettled]);
const activePlan = useMemo(
() => deriveActivePlanState(threadActivities, activeLatestTurn?.turnId ?? undefined),
[activeLatestTurn?.turnId, threadActivities],
);
const showPlanFollowUpPrompt = shouldShowPlanFollowUpPrompt({
pendingUserInputCount: pendingUserInputs.length,
interactionMode,
latestTurnSettled,
hasActionableProposedPlan: hasActionableProposedPlan(activeProposedPlan),
hasComposerAttachments: composerHasAttachments,
});
const activePendingApproval = pendingApprovals[0] ?? null;
// The open /usage-limits panel for this thread, model and turn. Only the open
// moment is stored: the rows read live provider data, so a redeemed reset
// credit or refreshed probe shows through. Anything that spends quota closes
// it: a new turn from any source, or the agent resuming after an approval or
// answered question.
const [usageLimitsPanel, setUsageLimitsPanel] = useState<{
readonly key: string;
readonly threadKey: string;
readonly now: number;
} | null>(null);
// Null while the provider list or the thread itself is unavailable, such as
// during a reconnect; the panel then stays hidden rather than being dropped.
// A pending approval or question is part of the key: once it is answered,
// from this client or any other, the agent resumes and spends quota.
const usageLimitsKey =
activeProviderInstanceId === null || (isServerThread && activeThread === undefined)
? null
: [
routeThreadKey,
activeProviderInstanceId,
activeThread?.latestTurn?.turnId ?? "",
activePendingApproval?.requestId ?? activePendingUserInput?.requestId ?? "",
].join(":");
// Drop the snapshot as soon as the thread or model changes so it cannot resurface stale.
if (
usageLimitsPanel !== null &&
usageLimitsKey !== null &&
usageLimitsPanel.key !== usageLimitsKey
) {
setUsageLimitsPanel(null);
}
const usageLimitSources = serverConfig?.usageLimitSources ?? EMPTY_USAGE_LIMIT_SOURCES;
const usageLimitsReport = useMemo(
() =>
usageLimitsPanel !== null &&
usageLimitsKey !== null &&
usageLimitsPanel.key === usageLimitsKey &&
activeProviderInstanceId !== null
? collectProviderUsageLimits(
activeProviderInstanceId,
providerStatuses,
usageLimitSources,
usageLimitsPanel.now,
)
: null,
[
activeProviderInstanceId,
providerStatuses,
usageLimitSources,
usageLimitsKey,
usageLimitsPanel,
],
);
const usageLimitsBanner = useMemo(
() =>
usageLimitsReport !== null && usageLimitsPanel !== null
? // A fresh id per opening: the stack keeps the last dismissed id as "exiting".
usageLimitsBannerItem(
`usage-limits:${usageLimitsPanel.key}:${usageLimitsPanel.now}`,
usageLimitsReport,
environmentId,
() => setUsageLimitsPanel(null),
)
: null,
[environmentId, usageLimitsPanel, usageLimitsReport],
);
// T3 owns /usage-limits only where Limits has data for the selected provider;
// elsewhere the name stays the provider's own and is sent through untouched.
const usageLimitsOffered =
activeProviderStatus !== null &&
hasProviderUsageLimits(activeProviderStatus.driver, providerStatuses, usageLimitSources);
// Answered locally from the last Limits snapshot; the agent never sees it.
const openUsageLimits = useCallback(() => {
const now = Date.now();
const report =
activeProviderInstanceId !== null && usageLimitsKey !== null
? collectProviderUsageLimits(
activeProviderInstanceId,
providerStatuses,
usageLimitSources,
now,
)
: null;
if (report && usageLimitsKey !== null) {
setUsageLimitsPanel({ key: usageLimitsKey, threadKey: routeThreadKey, now });
return true;
}
setUsageLimitsPanel(null);
toastManager.add({ type: "info", title: "Usage limits are unavailable for this provider" });
return false;
}, [
activeProviderInstanceId,
providerStatuses,
routeThreadKey,
usageLimitSources,
usageLimitsKey,
]);
// Responses can resolve after navigating away; only the originating thread's panel clears.
const clearUsageLimitsFor = useCallback(
(threadKey: string) =>
setUsageLimitsPanel((current) =>
current !== null && current.threadKey === threadKey ? null : current,
),
[],
);
const {
beginLocalDispatch,
resetLocalDispatch,
localDispatchStartedAt,
latestUserMessageAt,
isPreparingWorktree,
isSendBusy,
backgroundSubmissionPending,
} = useLocalDispatchState({
activeThread,
activeLatestTurn,
phase,
activePendingApproval: activePendingApproval?.requestId ?? null,
activePendingUserInput: activePendingUserInput?.requestId ?? null,
threadError,
});
const optimisticCompactionMessage = optimisticUserMessages.at(-1);
const pendingCompactionMessage =
isSendBusy &&
optimisticCompactionMessage !== undefined &&
isCompactCommandMessage(optimisticCompactionMessage)
? optimisticCompactionMessage
: activeThread?.messages.findLast(isCompactCommandMessage);
const compactRequestIsActive =
pendingCompactionMessage !== undefined &&
(pendingCompactionMessage.createdAt >
(activeLatestTurn?.requestedAt ?? pendingCompactionMessage.createdAt) ||
(activeLatestTurn?.state === "running" &&
pendingCompactionMessage.createdAt === activeLatestTurn.requestedAt));
const compactionSettled =
pendingCompactionMessage !== undefined &&
(latestTurnStartFailureId(activeThread, pendingCompactionMessage.id) !== null ||
activeThread?.activities.some((activity) => {
if (activity.kind !== "context-compaction") return false;
const payload = activity.payload as { readonly requestId?: unknown } | null | undefined;
return payload?.requestId === pendingCompactionMessage.id;
}));
const isCompacting =
(isSendBusy || phase === "connecting" || phase === "running") &&
compactRequestIsActive &&
!compactionSettled;
const isWorking =
phase === "running" || isSendBusy || isConnecting || isRevertingCheckpoint || isCompacting;
const activeWorkStartedAt = deriveActiveWorkStartedAt(
activeLatestTurn,
activeThread?.session ?? null,
localDispatchStartedAt,
latestUserMessageAt,
);
useEffect(() => {
attachmentPreviewHandoffByMessageIdRef.current = attachmentPreviewHandoffByMessageId;
}, [attachmentPreviewHandoffByMessageId]);
const clearAttachmentPreviewHandoff = useCallback(
(messageId: MessageId, previewUrls?: ReadonlyArray<string>) => {
delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
const currentPreviewUrls =
previewUrls ?? attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];
setAttachmentPreviewHandoffByMessageId((existing) => {
if (!(messageId in existing)) {
return existing;
}
const next = { ...existing };
delete next[messageId];
attachmentPreviewHandoffByMessageIdRef.current = next;
return next;
});
for (const previewUrl of currentPreviewUrls) {
revokeBlobPreviewUrl(previewUrl);
}
},
[],
);
const clearAttachmentPreviewHandoffs = useCallback(() => {
attachmentPreviewPromotionInFlightByMessageIdRef.current = {};
for (const previewUrls of Object.values(attachmentPreviewHandoffByMessageIdRef.current)) {
for (const previewUrl of previewUrls) {
revokeBlobPreviewUrl(previewUrl);
}
}
attachmentPreviewHandoffByMessageIdRef.current = {};
setAttachmentPreviewHandoffByMessageId({});
}, []);
useEffect(() => {
return () => {
clearAttachmentPreviewHandoffs();
for (const message of optimisticUserMessagesRef.current) {
revokeUserMessagePreviewUrls(message);
}
};
}, [clearAttachmentPreviewHandoffs]);
const handoffAttachmentPreviews = useCallback((messageId: MessageId, previewUrls: string[]) => {
if (previewUrls.length === 0) return;
const previousPreviewUrls = attachmentPreviewHandoffByMessageIdRef.current[messageId] ?? [];
const nextPreviewUrlSet = new Set(previewUrls);
for (const previewUrl of previousPreviewUrls) {
if (!nextPreviewUrlSet.has(previewUrl)) {
revokeBlobPreviewUrl(previewUrl);
}
}
setAttachmentPreviewHandoffByMessageId((existing) => {
const next = {
...existing,
[messageId]: previewUrls,
};
attachmentPreviewHandoffByMessageIdRef.current = next;
return next;
});
}, []);
const serverMessages = activeThread?.messages;
const [projectServerMessagePreviews] = useState(createMessageAttachmentPreviewProjector);
const [projectHandoffMessagePreviews] = useState(createMessageAttachmentPreviewProjector);
const downloadFileAttachment = useCallback(
async (attachment: ChatFileAttachment) => {
const connection = readPreparedConnection(environmentId);
if (!connection) {
toastManager.add({ type: "error", title: "The environment is not connected." });
return;
}
try {
const url = await resolveFileAttachmentUrl({
attachment,
environmentId,
httpBaseUrl: connection.httpBaseUrl,
createAssetUrl: createAttachmentAssetUrl,
});
const anchor = document.createElement("a");
anchor.href = url;
anchor.download = attachment.name;
anchor.click();
} catch (error) {
toastManager.add({
type: "error",
title: "Could not download " + attachment.name,
description: error instanceof Error ? error.message : "The attachment is unavailable.",
});
}
},
[createAttachmentAssetUrl, environmentId],
);
const openFileAttachment = useCallback(
(attachment: ChatFileAttachment) => {
if (isBrowserPreviewAttachment(attachment) && activeThreadRef) {
useRightPanelStore.getState().openAttachment(activeThreadRef, attachment);
return;
}
void downloadFileAttachment(attachment);
},
[activeThreadRef, downloadFileAttachment],
);
const serverAttachmentResources = useMemo(
() => selectHandoffImageResources(serverMessages, attachmentPreviewHandoffByMessageId),
[serverMessages, attachmentPreviewHandoffByMessageId],
);
const serverAttachmentUrls = useAssetUrls(environmentId, serverAttachmentResources);
const serverAttachmentUrlById = useMemo(
() =>
new Map(
serverAttachmentResources.flatMap((resource, index) => {
const url = serverAttachmentUrls[index];
return url ? [[resource.attachmentId, url] as const] : [];
}),
),
[serverAttachmentResources, serverAttachmentUrls],
);
const displayServerMessages = useMemo<ReadonlyArray<ChatMessage>>(() => {
if (!serverMessages) return [];
return serverMessages.map((message) =>
projectServerMessagePreviews(message, (attachment) =>
serverAttachmentUrlById.get(attachment.id),
),
);
}, [projectServerMessagePreviews, serverAttachmentUrlById, serverMessages]);
useEffect(() => {
if (typeof Image === "undefined" || displayServerMessages.length === 0) {
return;
}
const cleanups: Array<() => void> = [];
const userMessagesById = new Map<string, ChatMessage>(
displayServerMessages
.filter((message) => message.role === "user")
.map((message) => [String(message.id), message] as const),
);
for (const [messageId, handoffPreviewUrls] of Object.entries(
attachmentPreviewHandoffByMessageId,
)) {
if (attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId]) {
continue;
}
const serverMessage = userMessagesById.get(messageId);
if (!serverMessage?.attachments || serverMessage.attachments.length === 0) {
continue;
}
const serverPreviewUrls = serverMessage.attachments.flatMap((attachment) =>
isImageAttachment(attachment) && attachment.previewUrl ? [attachment.previewUrl] : [],
);
if (
serverPreviewUrls.length === 0 ||
serverPreviewUrls.length !== handoffPreviewUrls.length ||
serverPreviewUrls.some((previewUrl) => previewUrl.startsWith("blob:"))
) {
continue;
}
attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId] = true;
let cancelled = false;
const imageInstances: HTMLImageElement[] = [];
const preloadServerPreviews = Promise.all(
serverPreviewUrls.map(
(previewUrl) =>
new Promise<void>((resolve, reject) => {
const image = new Image();
imageInstances.push(image);
const handleLoad = () => resolve();
const handleError = () =>
reject(new Error(`Failed to load server preview for ${messageId}.`));
image.addEventListener("load", handleLoad, { once: true });
image.addEventListener("error", handleError, { once: true });
image.src = previewUrl;
}),
),
);
void preloadServerPreviews
.then(() => {
if (cancelled) {
return;
}
clearAttachmentPreviewHandoff(messageId as MessageId, handoffPreviewUrls);
})
.catch(() => {
if (!cancelled) {
delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
}
});
cleanups.push(() => {
cancelled = true;
delete attachmentPreviewPromotionInFlightByMessageIdRef.current[messageId];
for (const image of imageInstances) {
image.src = "";
}
});
}
return () => {
for (const cleanup of cleanups) {
cleanup();
}
};
}, [attachmentPreviewHandoffByMessageId, clearAttachmentPreviewHandoff, displayServerMessages]);
const timelineMessages = useMemo(() => {
const messages = displayServerMessages;
const serverMessagesWithPreviewHandoff =
Object.keys(attachmentPreviewHandoffByMessageId).length === 0
? messages
: messages.map((message) => {
if (
message.role !== "user" ||
!message.attachments ||
message.attachments.length === 0
) {
return message;
}
const handoffPreviewUrls = attachmentPreviewHandoffByMessageId[message.id];
if (!handoffPreviewUrls || handoffPreviewUrls.length === 0) {
return message;
}
let imageIndex = 0;
return projectHandoffMessagePreviews(message, (attachment) => {
if (!isImageAttachment(attachment)) {
return undefined;
}
const handoffPreviewUrl = handoffPreviewUrls[imageIndex];
imageIndex += 1;
return handoffPreviewUrl;
});
});
const localMessages = optimisticUserMessages;
if (localMessages.length === 0) {
return serverMessagesWithPreviewHandoff;
}
const serverIds = new Set(serverMessagesWithPreviewHandoff.map((message) => message.id));
const pendingMessages = localMessages.filter((message) => !serverIds.has(message.id));
if (pendingMessages.length === 0) {
return serverMessagesWithPreviewHandoff;
}
return [...serverMessagesWithPreviewHandoff, ...pendingMessages];
}, [
attachmentPreviewHandoffByMessageId,
displayServerMessages,
optimisticUserMessages,
projectHandoffMessagePreviews,
]);
const timelineProjectionRef = useRef<{
threadKey: string | null;
projection: TimelineEntriesProjection;
} | null>(null);
const timelineEntries = useMemo(() => {
const previous = timelineProjectionRef.current;
const projection = deriveTimelineEntriesWithState(
timelineMessages,
activeThread?.proposedPlans ?? [],
workLogEntries,
previous?.threadKey === activeThreadKey ? previous.projection : null,
);
timelineProjectionRef.current = { threadKey: activeThreadKey, projection };
return projection.entries;
}, [
timelineProjectionRef,
activeThreadKey,
activeThread?.proposedPlans,
timelineMessages,
workLogEntries,
]);
const displayedTimeline = resolveThreadSwitchTimeline({
loading: timelineEntries.length === 0 && threadSyncPhase !== null,
activeThreadKey,
nextEntries: timelineEntries,
rememberedForActive: peekRememberedThreadTimeline<typeof timelineEntries>(activeThreadKey),
});
const displayedTimelineKey = displayedTimeline.displayThreadKey ?? routeThreadKey;
const paintOnlyDisplayedTimeline = isPaintOnlyThreadTimeline(
displayedTimeline.displayThreadKey,
activeThreadKey,
);
const displayedThreadRef = parseScopedThreadKey(displayedTimelineKey);
const [dockedDraftHeroThreadKey, setDockedDraftHeroThreadKey] = useState<string | null>(null);
const draftHeroDockRequested =
activeThreadKey !== null && dockedDraftHeroThreadKey === activeThreadKey;
const isDraftHeroState = resolveDraftHeroState({
isLocalDraftThread,
hasTimelineEntries: timelineEntries.length > 0,
isWorking,
draftHeroDockRequested,
backgroundSubmissionPending,
});
const [
attachDraftHeroTransitionGroupRef,
attachDraftHeroComposerAnchorRef,
captureDraftHeroComposerRect,
] = useDraftHeroLayoutTransition(isDraftHeroState);
const gitCwd = activeProject
? projectScriptCwd({
project: { cwd: activeProject.workspaceRoot },
worktreePath: activeThread?.worktreePath ?? null,
})
: null;
const gitStatusCwd = activeThread?.worktreePath ?? gitCwd;
const gitStatusQuery = useEnvironmentQuery(
gitStatusCwd === null
? null
: vcsEnvironment.status({
environmentId,
input: { cwd: gitStatusCwd },
}),
);
useWorkspaceMutationRefresh({
enabled: gitStatusCwd !== null,
mutationId: workspaceMutationId,
refresh: gitStatusQuery.refresh,
resourceKey: `git-status:${activeThreadKey ?? ""}:${gitStatusCwd ?? ""}`,
});
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
const availableEditors = useAtomValue(primaryServerAvailableEditorsAtom);
const manualCompactionProviderAvailable = useMemo(
() =>
hasAvailableCompactionProvider({
providers: providerInstanceEntries,
driverKind: selectedProvider,
instanceId: activeProviderInstanceId,
lockedInstanceId: lockedProvider
? (activeThread?.session?.providerInstanceId ??
activeThread?.modelSelection.instanceId ??
null)
: null,
}),
[
activeProviderInstanceId,
activeThread?.modelSelection.instanceId,
activeThread?.session?.providerInstanceId,
lockedProvider,
providerInstanceEntries,
selectedProvider,
],
);
const [resumeCompactionPermanentlyDismissed, setResumeCompactionPermanentlyDismissed] =
useLocalStorage(
`t3code:resume-compaction-dismissed:${environmentId}:${activeProviderInstanceId ?? "claudeAgent"}`,
false,
Schema.Boolean,
);
const nativeResumeCompactionDismissed = useMemo(
() => hasDismissedResumeCompaction(threadActivities),
[threadActivities],
);
useEffect(() => {
if (nativeResumeCompactionDismissed && !resumeCompactionPermanentlyDismissed) {
setResumeCompactionPermanentlyDismissed(true);
}
}, [
nativeResumeCompactionDismissed,
resumeCompactionPermanentlyDismissed,
setResumeCompactionPermanentlyDismissed,
]);
const providerStatusBannerKey = getProviderStatusBannerKey(activeProviderStatus);
const [dismissedProviderStatusBannerKey, setDismissedProviderStatusBannerKey] = useState<
string | null
>(null);
useEffect(() => {
if (providerStatusBannerKey === null && dismissedProviderStatusBannerKey !== null) {
setDismissedProviderStatusBannerKey(null);
}
}, [dismissedProviderStatusBannerKey, providerStatusBannerKey]);
const visibleProviderStatus = shouldShowProviderStatusBanner(
activeProviderStatus,
dismissedProviderStatusBannerKey,
)
? activeProviderStatus
: null;
const hasTimelineTopBanner = Boolean(visibleThreadError) || visibleProviderStatus !== null;
const activeProjectCwd = activeProject?.workspaceRoot ?? null;
const activeThreadWorktreePath = activeThread?.worktreePath ?? null;
const activeWorkspaceRoot = activeThreadWorktreePath ?? activeProjectCwd ?? undefined;
useLayoutEffect(() => {
if (
threadDetailLoading ||
timelineEntries.length === 0 ||
timelineHasEphemeralPreviewUrls(timelineEntries)
) {
return;
}
rememberReadyThreadTimeline({
threadKey: activeThreadKey,
entries: timelineEntries,
markdownCwd: gitCwd,
workspaceRoot: activeWorkspaceRoot ?? null,
});
}, [activeThreadKey, activeWorkspaceRoot, gitCwd, threadDetailLoading, timelineEntries]);
const heldPaintContext = paintOnlyDisplayedTimeline
? peekHeldThreadTimeline<typeof timelineEntries>()
: null;
const activeTerminalLaunchContext =
terminalUiLaunchContext?.threadId === activeThreadId ? terminalUiLaunchContext : null;
// Git status arrives after the composer paints. A checkout seen earlier in
// this session answers from memory, so a non-Git project does not mount the
// branch strip and then drop it. A never-seen checkout assumes Git, which
// is what nearly every project is.
const liveIsGitRepo = gitStatusQuery.data?.isRepo;
useEffect(() => {
if (gitStatusCwd !== null && liveIsGitRepo !== undefined) {
rememberCheckoutIsRepo(environmentId, gitStatusCwd, liveIsGitRepo);
}
}, [environmentId, gitStatusCwd, liveIsGitRepo]);
const isGitRepo = liveIsGitRepo ?? recallCheckoutIsRepo(environmentId, gitStatusCwd) ?? true;
// Keep a hidden, off-flow strip mounted for existing threads so the composer
// can measure whether its relocated controls fit. The visible chrome remains
// content-driven: Git/environment context or controls that actually fit.
const mountComposerContextStrip = shouldShowComposerContextStrip({
hasActiveProject: activeProject !== null,
isGitRepo,
showEnvironmentIndicator: showComposerEnvironmentIndicator,
hostsRestingComposerControls: routeKind === "server",
});
const showComposerContextStrip = shouldShowComposerContextStrip({
hasActiveProject: activeProject !== null,
isGitRepo,
showEnvironmentIndicator: showComposerEnvironmentIndicator,
hostsRestingComposerControls: routeKind === "server" && restingComposerControlsVisible,
});
const initialDiffPanelGitScope =
gitStatusQuery.data?.hasWorkingTreeChanges === true ? "unstaged" : "branch";
const diffPanelGitStatusResolutionKey = gitStatusQuery.data ? "resolved" : "pending";
const terminalShortcutLabelOptions = useMemo(
() => ({
context: {
terminalFocus: true,
terminalOpen: Boolean(terminalUiState.terminalOpen),
},
}),
[terminalUiState.terminalOpen],
);
const splitTerminalShortcutLabel = useMemo(
() => shortcutLabelForCommand(keybindings, "terminal.split", terminalShortcutLabelOptions),
[keybindings, terminalShortcutLabelOptions],
);
const splitTerminalVerticalShortcutLabel = useMemo(
() =>
shortcutLabelForCommand(keybindings, "terminal.splitVertical", terminalShortcutLabelOptions),
[keybindings, terminalShortcutLabelOptions],
);
const newTerminalShortcutLabel = useMemo(
() => shortcutLabelForCommand(keybindings, "terminal.new", terminalShortcutLabelOptions),
[keybindings, terminalShortcutLabelOptions],
);
const closeTerminalShortcutLabel = useMemo(
() => shortcutLabelForCommand(keybindings, "terminal.close", terminalShortcutLabelOptions),
[keybindings, terminalShortcutLabelOptions],
);
const onToggleDiff = useCallback(() => {
if (!isServerThread) {
return;
}
if (!diffOpen) {
onDiffPanelOpen?.();
}
if (activeThreadRef) {
useRightPanelStore.getState().toggle(activeThreadRef, "diff");
}
}, [activeThreadRef, diffOpen, isServerThread, onDiffPanelOpen]);
const needsLoadBalancing = automaticEnvironment && !draftThread?.loadBalancedEnvironmentId;
const loadBalancingCandidates = useMemo(
() =>
needsLoadBalancing
? logicalProjectEnvironments
.filter((candidate) => {
const environment = environmentById.get(candidate.environmentId);
return (
environment?.connection.phase === "connected" &&
(loadBalancingSettings.loadBalancingWeights[candidate.environmentId] ?? 50) > 0 &&
environment.serverConfig?.providers.some(
(provider) =>
(activeProviderInstanceId === null ||
provider.instanceId === activeProviderInstanceId) &&
provider.driver === selectedProvider &&
provider.enabled &&
provider.installed &&
provider.status !== "error" &&
provider.auth.status !== "unauthenticated" &&
provider.availability !== "unavailable",
)
);
})
.map((candidate) => candidate.environmentId)
: [],
[
needsLoadBalancing,
logicalProjectEnvironments,
environmentById,
loadBalancingSettings.loadBalancingWeights,
activeProviderInstanceId,
selectedProvider,
],
);
const loadBalancing = useLoadBalancedEnvironment(
loadBalancingCandidates,
loadBalancingSettings.loadBalancingWeights,
);
useEffect(() => {
if (!needsLoadBalancing || loadBalancing.pending || !draftId || sendInFlightRef.current) return;
const target = logicalProjectEnvironments.find(
(environment) => environment.environmentId === loadBalancing.environmentId,
);
if (!target) return;
setDraftThreadContext(draftId, {
projectRef: scopeProjectRef(target.environmentId, target.projectId),
environmentSelection: "auto",
loadBalancedEnvironmentId: target.environmentId,
});
}, [
needsLoadBalancing,
loadBalancing.pending,
loadBalancing.environmentId,
draftId,
logicalProjectEnvironments,
setDraftThreadContext,
]);
const onAutoEnvironment = useCallback(() => {
if (envLocked || !draftId) return;
if (composerHasAttachments) {
toastManager.add({
type: "warning",
id: "load-balancing-attachments",
title: "Keep attachments on this machine",
description:
"Remove attachments before choosing automatic routing, then attach them on the selected machine.",
});
return;
}
loadBalancing.refresh(
logicalProjectEnvironments.map((environment) => environment.environmentId),
);
setDraftThreadContext(draftId, {
environmentSelection: "auto",
loadBalancedEnvironmentId: null,
branch: null,
worktreePath: null,
});
}, [
envLocked,
draftId,
setDraftThreadContext,
loadBalancing.refresh,
logicalProjectEnvironments,
composerHasAttachments,
]);
const autoEnvironmentLabel = automaticEnvironment
? draftThread?.loadBalancedEnvironmentId
? "Auto balance"
: loadBalancing.pending
? "Checking machines…"
: loadBalancing.failed
? "Auto balance unavailable"
: "Auto balance"
: undefined;
// Handle environment change for draft threads. When the user picks a
// different environment we update the draft context to point at the physical
// project in that environment while keeping the same logical project.
const onEnvironmentChange = useCallback(
(nextEnvironmentId: EnvironmentId) => {
if (envLocked || !draftId) return;
const target = logicalProjectEnvironments.find(
(env) => env.environmentId === nextEnvironmentId,
);
if (!target) return;
setDraftThreadContext(draftId, {
projectRef: scopeProjectRef(target.environmentId, target.projectId),
environmentSelection: "manual",
loadBalancedEnvironmentId: null,
});
},
[draftId, envLocked, logicalProjectEnvironments, setDraftThreadContext],
);
const activeTerminalGroup =
terminalUiState.terminalGroups.find(
(group) => group.id === terminalUiState.activeTerminalGroupId,
) ??
terminalUiState.terminalGroups.find((group) =>
group.terminalIds.includes(terminalUiState.activeTerminalId),
) ??
null;
const hasReachedSplitLimit =
(activeTerminalGroup?.terminalIds.length ?? 0) >= MAX_TERMINALS_PER_GROUP;
const setThreadError = useCallback(
(targetThreadId: ThreadId | null, error: string | null) => {
if (!targetThreadId) return;
const nextError = sanitizeThreadErrorMessage(error);
const nextEntry: LocalThreadErrorEntry = { message: nextError, at: Date.now() };
if (
shouldWriteThreadErrorToCurrentServerThread({
activeServerThread,
routeThreadRef,
targetThreadId,
})
) {
setLocalServerErrorsByThreadKey((existing) => {
if ((existing[routeThreadKey]?.message ?? null) === nextError) {
return existing;
}
return {
...existing,
[routeThreadKey]: nextEntry,
};
});
return;
}
const localDraftErrorKey = draftId ?? targetThreadId;
setLocalDraftErrorsByDraftId((existing) => {
if ((existing[localDraftErrorKey]?.message ?? null) === nextError) {
return existing;
}
return {
...existing,
[localDraftErrorKey]: nextEntry,
};
});
},
[activeServerThread, draftId, routeThreadKey, routeThreadRef],
);
const interruptContextRef = useRef({ activeThread, phase, setThreadError });
interruptContextRef.current = { activeThread, phase, setThreadError };
const onInterrupt = useCallback(async () => {
const { activeThread, phase, setThreadError } = interruptContextRef.current;
const input = buildRunningThreadTurnInterruptInput(activeThread, phase);
if (!input || !activeThread) return;
const result = await interruptThreadTurn({
environmentId: activeThread.environmentId,
input,
});
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
setThreadError(
activeThread.id,
error instanceof Error ? error.message : "Failed to interrupt the current turn.",
);
}
}, [interruptThreadTurn]);
const canInterruptRunningThread =
buildRunningThreadTurnInterruptInput(activeThread, phase) !== null;
const focusComposer = useCallback(() => {
composerRef.current?.focusAtEnd();
}, [composerRef]);
useEffect(() => subscribeSnapShotComposerFocus(focusComposer), [focusComposer]);
const scheduleComposerFocus = useCallback(() => {
window.requestAnimationFrame(() => {
focusComposer();
});
}, [focusComposer]);
const useArtifactTemplate = useCallback(
(template: CodexArtifactTemplate) => {
const composer = composerRef.current;
if (!composer) return;
const currentDraft = composer.getSendContext().prompt;
const prompt = codexArtifactTemplatePromptToAppend(currentDraft, template);
if (prompt !== null && !composer.insertTextAtEnd(prompt, { ensureLeadingBoundary: true })) {
toastManager.add({
type: "error",
title: "Unable to add to chat",
description: "The composer is busy; try again once it is ready.",
});
return;
}
scheduleComposerFocus();
},
[composerRef, scheduleComposerFocus],
);
const addTerminalContextToDraft = useCallback(
(selection: TerminalContextSelection) => {
composerRef.current?.addTerminalContext(selection);
},
[composerRef],
);
const setTerminalOpen = useCallback(
(open: boolean) => {
if (!activeThreadRef) return;
storeSetTerminalOpen(activeThreadRef, open);
},
[activeThreadRef, storeSetTerminalOpen],
);
const toggleTerminalVisibility = useCallback(() => {
if (!activeThreadRef) return;
const nextOpen = !terminalUiState.terminalOpen;
if (nextOpen && terminalUiState.terminalIds.length === 0) {
if (!activeThreadId || !activeProject) {
return;
}
const cwdForOpen = gitCwd ?? activeProject.workspaceRoot;
if (!cwdForOpen) {
return;
}
const terminalId = nextTerminalId(allocatableActiveTerminalIds);
storeEnsureTerminal(activeThreadRef, terminalId, { open: true });
void openTerminal({
environmentId,
input: {
threadId: activeThreadId,
terminalId,
cwd: cwdForOpen,
...(activeThreadWorktreePath != null ? { worktreePath: activeThreadWorktreePath } : {}),
env: projectScriptRuntimeEnv({
project: { cwd: activeProject.workspaceRoot },
worktreePath: activeThreadWorktreePath,
}),
},
});
return;
}
setTerminalOpen(nextOpen);
}, [
activeProject,
activeThreadId,
activeThreadRef,
activeThreadWorktreePath,
allocatableActiveTerminalIds,
environmentId,
gitCwd,
openTerminal,
setTerminalOpen,
storeEnsureTerminal,
terminalUiState.terminalIds.length,
terminalUiState.terminalOpen,
]);
const splitTerminal = useCallback(
(direction: "horizontal" | "vertical" = "horizontal") => {
if (!activeThreadRef || hasReachedSplitLimit || !activeThreadId || !activeProject) {
return;
}
const cwdForOpen = gitCwd ?? activeProject.workspaceRoot;
if (!cwdForOpen) {
return;
}
const terminalId = nextTerminalId(allocatableActiveTerminalIds);
if (direction === "vertical") {
storeSplitTerminalVertical(activeThreadRef, terminalId);
} else {
storeSplitTerminal(activeThreadRef, terminalId);
}
setTerminalFocusRequestId((value) => value + 1);
void openTerminal({
environmentId,
input: {
threadId: activeThreadId,
terminalId,
cwd: cwdForOpen,
...(activeThreadWorktreePath != null ? { worktreePath: activeThreadWorktreePath } : {}),
env: projectScriptRuntimeEnv({
project: { cwd: activeProject.workspaceRoot },
worktreePath: activeThreadWorktreePath,
}),
},
});
},
[
activeProject,
activeThreadId,
allocatableActiveTerminalIds,
activeThreadRef,
openTerminal,
activeThreadWorktreePath,
environmentId,
gitCwd,
hasReachedSplitLimit,
storeSplitTerminal,
storeSplitTerminalVertical,
],
);
const createNewTerminal = useCallback(() => {
if (!activeThreadRef || !activeThreadId || !activeProject) {
return;
}
const cwdForOpen = gitCwd ?? activeProject.workspaceRoot;
if (!cwdForOpen) {
return;
}
const terminalId = nextTerminalId(allocatableActiveTerminalIds);
storeNewTerminal(activeThreadRef, terminalId);
setTerminalFocusRequestId((value) => value + 1);
void openTerminal({
environmentId,
input: {
threadId: activeThreadId,
terminalId,
cwd: cwdForOpen,
...(activeThreadWorktreePath != null ? { worktreePath: activeThreadWorktreePath } : {}),
env: projectScriptRuntimeEnv({
project: { cwd: activeProject.workspaceRoot },
worktreePath: activeThreadWorktreePath,
}),
},
});
}, [
activeProject,
activeThreadId,
allocatableActiveTerminalIds,
activeThreadRef,
openTerminal,
activeThreadWorktreePath,
environmentId,
gitCwd,
storeNewTerminal,
]);
const closeTerminal = useCallback(
(terminalId: string) => {
if (!activeThreadId || !activeThreadRef) return;
const fallbackExitWrite = () =>
writeTerminal({
environmentId,
input: { threadId: activeThreadId, terminalId, data: "exit\n" },
});
void (async () => {
const closeResult = await closeTerminalMutation({
environmentId,
input: {
threadId: activeThreadId,
terminalId,
deleteHistory: true,
},
});
if (closeResult._tag === "Failure" && !isAtomCommandInterrupted(closeResult)) {
await fallbackExitWrite();
}
})();
storeCloseTerminal(activeThreadRef, terminalId);
setTerminalFocusRequestId((value) => value + 1);
},
[
activeThreadId,
activeThreadRef,
closeTerminalMutation,
environmentId,
storeCloseTerminal,
writeTerminal,
],
);
const runProjectScript = useCallback(
async (
script: ProjectScript,
options?: {
cwd?: string;
env?: Record<string, string>;
worktreePath?: string | null;
preferNewTerminal?: boolean;
rememberAsLastInvoked?: boolean;
},
) => {
if (!activeThreadId || !activeProject || !activeThread) return;
if (options?.rememberAsLastInvoked !== false) {
setLastInvokedScriptByProjectId((current) => {
if (current[activeProject.id] === script.id) return current;
return { ...current, [activeProject.id]: script.id };
});
}
const targetCwd = options?.cwd ?? gitCwd ?? activeProject.workspaceRoot;
const baseTerminalId =
terminalUiState.activeTerminalId || activeKnownTerminalIds[0] || DEFAULT_THREAD_TERMINAL_ID;
const isBaseTerminalBusy = runningTerminalIds.includes(baseTerminalId);
const wantsNewTerminal = Boolean(options?.preferNewTerminal) || isBaseTerminalBusy;
const shouldCreateNewTerminal = wantsNewTerminal;
const targetWorktreePath = options?.worktreePath ?? activeThread.worktreePath ?? null;
setTerminalUiLaunchContext({
threadId: activeThreadId,
cwd: targetCwd,
worktreePath: targetWorktreePath,
});
setTerminalOpen(true);
if (!activeThreadRef) {
return;
}
setTerminalFocusRequestId((value) => value + 1);
const runtimeEnv = projectScriptRuntimeEnv({
project: {
cwd: activeProject.workspaceRoot,
},
worktreePath: targetWorktreePath,
...(options?.env ? { extraEnv: options.env } : {}),
});
const targetTerminalId = shouldCreateNewTerminal
? nextTerminalId(allocatableActiveTerminalIds)
: baseTerminalId;
const openTerminalInput: TerminalOpenInput = shouldCreateNewTerminal
? {
threadId: activeThreadId,
terminalId: targetTerminalId,
cwd: targetCwd,
...(targetWorktreePath !== null ? { worktreePath: targetWorktreePath } : {}),
env: runtimeEnv,
cols: SCRIPT_TERMINAL_COLS,
rows: SCRIPT_TERMINAL_ROWS,
}
: {
threadId: activeThreadId,
terminalId: targetTerminalId,
cwd: targetCwd,
...(targetWorktreePath !== null ? { worktreePath: targetWorktreePath } : {}),
env: runtimeEnv,
};
if (shouldCreateNewTerminal) {
storeNewTerminal(activeThreadRef, targetTerminalId);
} else {
storeSetActiveTerminal(activeThreadRef, targetTerminalId);
}
const openResult = await openTerminal({ environmentId, input: openTerminalInput });
if (openResult._tag === "Failure") {
if (!isAtomCommandInterrupted(openResult)) {
const error = squashAtomCommandFailure(openResult);
setThreadError(
activeThreadId,
error instanceof Error ? error.message : `Failed to run script "${script.name}".`,
);
}
return;
}
const writeResult = await writeTerminal({
environmentId,
input: {
threadId: activeThreadId,
terminalId: targetTerminalId,
data: `${script.command}\r`,
},
});
if (writeResult._tag === "Failure" && !isAtomCommandInterrupted(writeResult)) {
const error = squashAtomCommandFailure(writeResult);
setThreadError(
activeThreadId,
error instanceof Error ? error.message : `Failed to run script "${script.name}".`,
);
}
},
[
activeProject,
activeThread,
activeThreadId,
activeThreadRef,
gitCwd,
setTerminalOpen,
setThreadError,
storeNewTerminal,
storeSetActiveTerminal,
setLastInvokedScriptByProjectId,
environmentId,
openTerminal,
activeKnownTerminalIds,
allocatableActiveTerminalIds,
runningTerminalIds,
terminalUiState.activeTerminalId,
writeTerminal,
],
);
const supportsProjectSettingsOverrides =
environmentById.get(environmentId)?.serverConfig?.environment.capabilities
.projectSettingsOverrides === true;
const persistProjectScripts = useCallback(
async (input: {
projectId: ProjectId;
projectCwd: string;
previousScripts: ReadonlyArray<ProjectScript>;
nextScripts: ReadonlyArray<ProjectScript>;
keybinding?: string | null;
keybindingCommand: KeybindingCommand | null;
}): Promise<AtomCommandResult<void, unknown>> => {
const updateResult = mapAtomCommandResult(
await updateProjectScriptSettings({
environmentId,
input: {
// The canonical key on servers that understand it; the legacy
// per-project map is still translated on older ones.
patch: supportsProjectSettingsOverrides
? {
projectSettingsOverrides: {
[input.projectId]: {
...settings.projectSettingsOverrides[input.projectId],
defaultProjectScripts: input.nextScripts,
},
},
}
: {
projectScriptOverrides: {
[input.projectId]: input.nextScripts,
},
},
},
}),
() => undefined,
);
if (updateResult._tag === "Failure") {
return updateResult;
}
const keybindingRule = decodeProjectScriptKeybindingRule({
keybinding: input.keybinding,
command: input.keybindingCommand,
});
if (isElectron && keybindingRule) {
return mapAtomCommandResult(
await upsertKeybinding({
environmentId,
input: keybindingRule,
}),
() => undefined,
);
}
return updateResult;
},
[
environmentId,
settings.projectSettingsOverrides,
supportsProjectSettingsOverrides,
updateProjectScriptSettings,
upsertKeybinding,
],
);
const saveProjectScript = useCallback(
async (input: NewProjectScriptInput): Promise<AtomCommandResult<void, unknown>> => {
if (!activeProject) {
return AsyncResult.success(undefined);
}
const nextId = nextProjectScriptId(
input.name,
activeProjectScripts.map((script) => script.id),
);
const nextScript = buildProjectScript(nextId, input);
const nextScripts = input.runOnWorktreeCreate
? [
...activeProjectScripts.map((script) =>
script.runOnWorktreeCreate ? { ...script, runOnWorktreeCreate: false } : script,
),
nextScript,
]
: [...activeProjectScripts, nextScript];
return persistProjectScripts({
projectId: activeProject.id,
projectCwd: activeProject.workspaceRoot,
previousScripts: activeProjectScripts,
nextScripts,
keybinding: input.keybinding,
keybindingCommand: commandForProjectScript(nextId),
});
},
[activeProject, activeProjectScripts, persistProjectScripts],
);
const updateProjectScript = useCallback(
async (
scriptId: string,
input: NewProjectScriptInput,
): Promise<AtomCommandResult<void, unknown>> => {
if (!activeProject) {
return AsyncResult.success(undefined);
}
const existingScript = activeProjectScripts.find((script) => script.id === scriptId);
if (!existingScript) {
return AsyncResult.failure(Cause.fail(new Error("Script not found.")));
}
const updatedScript = buildProjectScript(existingScript.id, input);
const nextScripts = activeProjectScripts.map((script) =>
script.id === scriptId
? updatedScript
: input.runOnWorktreeCreate
? { ...script, runOnWorktreeCreate: false }
: script,
);
return persistProjectScripts({
projectId: activeProject.id,
projectCwd: activeProject.workspaceRoot,
previousScripts: activeProjectScripts,
nextScripts,
keybinding: input.keybinding,
keybindingCommand: commandForProjectScript(scriptId),
});
},
[activeProject, activeProjectScripts, persistProjectScripts],
);
const deleteProjectScript = useCallback(
async (scriptId: string): Promise<AtomCommandResult<void, unknown>> => {
if (!activeProject) {
return AsyncResult.success(undefined);
}
const nextScripts = activeProjectScripts.filter((script) => script.id !== scriptId);
const deletedName = activeProjectScripts.find((s) => s.id === scriptId)?.name;
const result = await persistProjectScripts({
projectId: activeProject.id,
projectCwd: activeProject.workspaceRoot,
previousScripts: activeProjectScripts,
nextScripts,
keybinding: null,
keybindingCommand: commandForProjectScript(scriptId),
});
if (result._tag === "Success") {
toastManager.add({
type: "success",
title: `Deleted action "${deletedName ?? "Unknown"}"`,
});
} else if (!isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
toastManager.add(
stackedThreadToast({
type: "error",
title: "Could not delete action",
description: error instanceof Error ? error.message : "An unexpected error occurred.",
}),
);
}
return result;
},
[activeProject, activeProjectScripts, persistProjectScripts],
);
const handleRuntimeModeChange = useCallback(
(mode: RuntimeMode) => {
if (mode === runtimeMode) return;
setComposerDraftRuntimeMode(composerDraftTarget, mode);
if (isLocalDraftThread) {
setDraftThreadContext(composerDraftTarget, { runtimeMode: mode });
}
scheduleComposerFocus();
},
[
isLocalDraftThread,
runtimeMode,
scheduleComposerFocus,
composerDraftTarget,
setComposerDraftRuntimeMode,
setDraftThreadContext,
],
);
const handleInteractionModeChange = useCallback(
(mode: ProviderInteractionMode) => {
if (mode === "plan" && !interactionModeEnabled) return;
if (mode === interactionMode) return;
setComposerDraftInteractionMode(composerDraftTarget, mode);
if (isLocalDraftThread) {
setDraftThreadContext(composerDraftTarget, { interactionMode: mode });
}
scheduleComposerFocus();
},
[
interactionMode,
interactionModeEnabled,
isLocalDraftThread,
scheduleComposerFocus,
composerDraftTarget,
setComposerDraftInteractionMode,
setDraftThreadContext,
],
);
const toggleInteractionMode = useCallback(() => {
if (!interactionModeEnabled) return;
handleInteractionModeChange(interactionMode === "plan" ? "default" : "plan");
}, [handleInteractionModeChange, interactionMode, interactionModeEnabled]);
const openProviderSetup = useCallback(
(instanceId: ProviderInstanceId) => {
void navigate({
to: "/settings/providers",
search: { environmentId, instanceId },
});
},
[environmentId, navigate],
);
const createBrowserSurface = useCallback(
(profileId?: string) => {
if (!activeThreadRef) return;
void addBrowserSurface({
threadRef: activeThreadRef,
openPreview,
...(profileId === undefined ? {} : { profileId }),
}).then((result) => {
if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
const error = squashAtomCommandFailure(result);
if (error instanceof BrowserSettingsReadError) {
toastManager.add(
stackedThreadToast({
type: "error",
title: "Unable to open browser",
description: error.message,
}),
);
}
});
},
[activeThreadRef, openPreview],
);
const addDiffSurface = useCallback(() => {
if (!activeThreadRef || !isServerThread || !isGitRepo) return;
useRightPanelStore.getState().open(activeThreadRef, "diff");
onDiffPanelOpen?.();
}, [activeThreadRef, isGitRepo, isServerThread, onDiffPanelOpen]);
const addFilesSurface = useCallback(() => {
if (!activeThreadRef || !activeProject) return;
useRightPanelStore.getState().open(activeThreadRef, "files");
}, [activeProject, activeThreadRef]);
const addAgentsSurface = useCallback(() => {
if (!activeThreadRef) return;
useRightPanelStore.getState().open(activeThreadRef, "agents");
}, [activeThreadRef]);
const supportsThreadPullRequests =
serverConfig?.environment.capabilities.threadPullRequests === true;
const pullRequestsSurfaceAvailable =
isServerThread &&
supportsThreadPullRequests &&
visibleThreadPullRequests((activeThreadShell ?? activeThread)?.pullRequests ?? []).length > 0;
const addPullRequestsSurface = useCallback(() => {
if (!activeThreadRef || !pullRequestsSurfaceAvailable) return;
useRightPanelStore.getState().open(activeThreadRef, "pull-requests");
}, [activeThreadRef, pullRequestsSurfaceAvailable]);
const { state: deviceState, loaded: deviceStateLoaded } = useDeviceState(
activeThreadRef?.environmentId ?? null,
);
const [deviceSetupThread, setDeviceSetupThread] = useState<ScopedThreadRef | null>(null);
const addDeviceSurface = useCallback(() => {
if (!activeThreadRef) return;
if (!deviceState.onboardingCompleted || deviceState.hostStatus === "disabled") {
setDeviceSetupThread(activeThreadRef);
return;
}
useRightPanelStore.getState().open(activeThreadRef, "device");
}, [activeThreadRef, deviceState.onboardingCompleted, deviceState.hostStatus]);
// A device the agent opens floats over chat like an agent-driven browser,
// or becomes a panel tab when floating previews are off. Sessions opened by
// another client arrive the same way; sheet layouts get neither. The first
// snapshot is a baseline: persisted tabs restore themselves, and existing
// sessions must not resurrect closed tabs. A session whose device summary
// has not arrived yet stays out of the baseline so a later snapshot opens it.
const autoShowFloatingPreview = useClientSettings(selectAutoShowFloatingPreview);
const previousDeviceSessions = useRef(new Map<string, Set<string>>());
useEffect(() => {
if (!activeThreadRef || !deviceStateLoaded) return;
const threadKey = `${activeThreadRef.environmentId}:${activeThreadRef.threadId}`;
const sessions = deviceState.sessions.filter(
(session) => session.threadId === activeThreadRef.threadId,
);
const key = (session: (typeof sessions)[number]) => `${session.hostId}:${session.deviceId}`;
const deviceFor = (session: (typeof sessions)[number]) =>
deviceState.devices.find(
(entry) => entry.hostId === session.hostId && entry.id === session.deviceId,
);
const previous = previousDeviceSessions.current.get(threadKey);
previousDeviceSessions.current.set(
threadKey,
new Set(sessions.filter((session) => deviceFor(session) !== undefined).map(key)),
);
if (!previous || shouldUseRightPanelSheet) return;
for (const session of sessions) {
if (previous.has(key(session))) continue;
const device = deviceFor(session);
if (!device) continue;
const target = {
hostId: session.hostId,
deviceId: session.deviceId,
platform: device.platform,
name: device.name,
};
if (autoShowFloatingPreview) {
usePreviewMiniPlayerStore.getState().open(activeThreadRef, { kind: "device", ...target });
continue;
}
const existing = useRightPanelStore
.getState()
.byThreadKey[scopedThreadKey(activeThreadRef)]?.surfaces.some(
(surface) =>
surface.kind === "device" &&
surface.target?.hostId === session.hostId &&
surface.target.deviceId === session.deviceId,
);
if (existing) continue;
useRightPanelStore.getState().openDevice(activeThreadRef, target, true);
}
}, [
activeThreadRef,
autoShowFloatingPreview,
deviceStateLoaded,
shouldUseRightPanelSheet,
deviceState.sessions,
deviceState.devices,
]);
// A floating device follows its session: once the agent or another client
// closes the device there is nothing left to stream.
useEffect(() => {
if (!activeThreadRef || !deviceStateLoaded) return;
const source = activePreviewMiniPlayer?.source;
if (source?.kind !== "device") return;
const sessionStillExists = deviceState.sessions.some(
(session) =>
session.threadId === activeThreadRef.threadId &&
session.hostId === source.hostId &&
session.deviceId === source.deviceId,
);
if (!sessionStillExists) usePreviewMiniPlayerStore.getState().close(activeThreadRef);
}, [activePreviewMiniPlayer, activeThreadRef, deviceState.sessions, deviceStateLoaded]);
const openFileSurface = useCallback(
(relativePath: string) => {
if (!activeThreadRef || !activeProject) return;
useRightPanelStore.getState().openFile(activeThreadRef, relativePath);
},
[activeProject, activeThreadRef],
);
// The shell carries server PR updates even while thread detail is still loading.
const activeThreadMetadata = activeThreadShell ?? activeThread;
const linkedThreadPullRequest =
activeThreadMetadata?.linkedPullRequest ?? activeThreadMetadata?.branchPullRequest ?? null;
const activeProjectRepository = activeProject?.repositoryIdentity?.displayName ?? null;
const linkedThreadPullRequestKey = linkedThreadPullRequest
? JSON.stringify([
linkedThreadPullRequest.projectId,
linkedThreadPullRequest.repository,
linkedThreadPullRequest.number,
])
: null;
const observedThreadPullRequestRef = useRef<{
readonly threadKey: string;
readonly reference: ThreadLinkedPullRequest | null;
} | null>(null);
const openProjectPullRequest = useCallback(
(number: number) => {
if (
!supportsPullRequests ||
!activeThreadRef ||
!activeProject ||
activeProjectRepository === null
) {
return;
}
useRightPanelStore.getState().openPullRequest(activeThreadRef, {
projectId: activeProject.id,
repository: activeProjectRepository,
number,
});
},
[activeProject, activeProjectRepository, activeThreadRef, supportsPullRequests],
);
const proactivePanelObservationRef = useRef<ReturnType<
typeof observeProactivePanelUserChoice
> | null>(null);
useEffect(() => {
if (!isServerThread || activeThreadKey === null || activeThreadRef === null) {
proactivePanelObservationRef.current = null;
observedThreadPullRequestRef.current = null;
return;
}
const panels = useRightPanelStore.getState();
const observation = observeProactivePanelUserChoice(proactivePanelObservationRef.current, {
threadKey: activeThreadKey,
runningTurnId: activeRunningTurnId,
userActionRevision: panels.getUserActionRevision(activeThreadRef),
});
proactivePanelObservationRef.current = observation;
const {
runningTurnId: previousRunningTurnId,
targetKey: previousTargetKey,
userActionRevision,
} = observation;
const openSurface = selectActiveRightPanelSurface(panels.byThreadKey, activeThreadRef);
const previousPullRequest = observedThreadPullRequestRef.current;
observedThreadPullRequestRef.current = {
threadKey: activeThreadKey,
reference: linkedThreadPullRequest,
};
const followSelectedPullRequest =
previousPullRequest?.threadKey === activeThreadKey &&
shouldRetargetThreadPullRequestPanel(
previousPullRequest.reference,
linkedThreadPullRequest,
openSurface,
);
// Following the selected linked PR does not open an unrelated panel, so it
// remains available with proactive panels off. It still respects a later choice.
if (followSelectedPullRequest && linkedThreadPullRequest !== null) {
panels.openProactive(
activeThreadRef,
pullRequestSurface(linkedThreadPullRequest),
userActionRevision,
);
}
if (!clientSettingsHydrated || threadDetailLoading) return;
const settledTurnId = latestTurnSettled ? (activeLatestTurn?.turnId ?? null) : null;
const newlyCompletedTurnId = shouldOpenProactiveTurnDiff({
previousRunningTurnId,
runningTurnId: activeRunningTurnId,
settledTurnId,
turnCompleted: activeLatestTurn?.state === "completed",
})
? settledTurnId
: null;
const proactivePanelsEnabled = settings.proactivePanelsEnabled && !shouldUseRightPanelSheet;
const eligibleCompletion = proactivePanelsEnabled && newlyCompletedTurnId !== null;
const completedCheckpoint = eligibleCompletion
? activeThread?.checkpoints.find((checkpoint) => checkpoint.turnId === newlyCompletedTurnId)
: undefined;
const diffAction = eligibleCompletion
? resolveProactiveTurnDiffAction({
checkpoint: completedCheckpoint,
isGitRepo: gitStatusQuery.data?.isRepo,
})
: "ignore";
const eligibleLink =
proactivePanelsEnabled &&
shouldOpenProactivePullRequest(previousTargetKey, linkedThreadPullRequestKey);
const shouldDeferLink = eligibleLink && !pullRequestsCapabilityKnown;
proactivePanelObservationRef.current = {
...observation,
// Preserve first-entry eligibility while the checkpoint or repository is loading.
runningTurnId: diffAction === "defer" ? previousRunningTurnId : activeRunningTurnId,
targetKey: shouldDeferLink ? (previousTargetKey ?? null) : linkedThreadPullRequestKey,
};
if (
!followSelectedPullRequest &&
eligibleLink &&
pullRequestsCapabilityKnown &&
supportsPullRequests &&
linkedThreadPullRequest !== null
) {
panels.openProactive(
activeThreadRef,
pullRequestSurface(linkedThreadPullRequest),
userActionRevision,
);
}
if (diffAction !== "open" || newlyCompletedTurnId === null) return;
if (!panels.openProactive(activeThreadRef, { id: "diff", kind: "diff" }, userActionRevision)) {
return;
}
useDiffPanelStore.getState().selectTurn(activeThreadRef, newlyCompletedTurnId);
onDiffPanelOpen?.();
}, [
activeThread?.checkpoints,
activeLatestTurn?.turnId,
activeLatestTurn?.state,
activeRunningTurnId,
activeThreadKey,
activeThreadRef,
clientSettingsHydrated,
gitStatusQuery.data?.isRepo,
isServerThread,
latestTurnSettled,
linkedThreadPullRequest,
linkedThreadPullRequestKey,
onDiffPanelOpen,
pullRequestsCapabilityKnown,
settings.proactivePanelsEnabled,
shouldUseRightPanelSheet,
supportsPullRequests,
threadDetailLoading,
]);
const closePreviewPanel = useCallback(() => {
if (activeThreadRef) {
// Closing the panel on a live browser or device floats it instead of dropping it.
if (activeRightPanelSurface?.kind === "preview" && activeRightPanelSurface.resourceId) {
usePreviewMiniPlayerStore
.getState()
.open(activeThreadRef, browserMiniPlayerSource(activeRightPanelSurface.resourceId));
} else if (activeRightPanelSurface?.kind === "device" && activeRightPanelSurface.target) {
usePreviewMiniPlayerStore
.getState()
.open(activeThreadRef, { kind: "device", ...activeRightPanelSurface.target });
}
setMaximizedRightPanelThreadKey(null);
useRightPanelStore.getState().close(activeThreadRef);
}
}, [activeRightPanelSurface, activeThreadRef]);
const togglePreviewPanel = useCallback(() => {
if (!activeThreadRef || !isPreviewSupportedInRuntime()) return;
if (previewPanelOpen) {
closePreviewPanel();
return;
}
const activeTabId = activePreviewState.activeTabId;
if (activeTabId) {
useRightPanelStore.getState().openBrowser(activeThreadRef, activeTabId);
} else {
createBrowserSurface();
}
}, [
activePreviewState.activeTabId,
activeThreadRef,
closePreviewPanel,
createBrowserSurface,
previewPanelOpen,
]);
const addTerminalSurface = useCallback(() => {
if (!activeThreadRef || !activeThreadId || !activeProject) return;
const cwd = gitCwd ?? activeProject.workspaceRoot;
const terminalId = nextTerminalId(allocatableActiveTerminalIds);
useRightPanelStore.getState().openTerminal(activeThreadRef, terminalId);
setTerminalFocusRequestId((value) => value + 1);
void openTerminal({
environmentId: activeThreadRef.environmentId,
input: {
threadId: activeThreadId,
terminalId,
cwd,
...(activeThreadWorktreePath != null ? { worktreePath: activeThreadWorktreePath } : {}),
env: projectScriptRuntimeEnv({
project: { cwd: activeProject.workspaceRoot },
worktreePath: activeThreadWorktreePath,
}),
},
});
}, [
activeProject,
activeThreadId,
activeThreadRef,
activeThreadWorktreePath,
allocatableActiveTerminalIds,
gitCwd,
openTerminal,
]);
const splitPanelTerminal = useCallback(
(direction: "horizontal" | "vertical" = "horizontal") => {
if (
!activeThreadRef ||
!activeThreadId ||
!activeProject ||
activeRightPanelSurface?.kind !== "terminal" ||
activeRightPanelSurface.terminalIds.length >= MAX_TERMINALS_PER_GROUP
) {
return;
}
const terminalId = nextTerminalId(allocatableActiveTerminalIds);
const cwd = gitCwd ?? activeProject.workspaceRoot;
useRightPanelStore
.getState()
.splitTerminal(activeThreadRef, activeRightPanelSurface.id, terminalId, direction);
setTerminalFocusRequestId((value) => value + 1);
void openTerminal({
environmentId: activeThreadRef.environmentId,
input: {
threadId: activeThreadId,
terminalId,
cwd,
...(activeThreadWorktreePath != null ? { worktreePath: activeThreadWorktreePath } : {}),
env: projectScriptRuntimeEnv({
project: { cwd: activeProject.workspaceRoot },
worktreePath: activeThreadWorktreePath,
}),
},
});
},
[
activeProject,
activeRightPanelSurface,
activeThreadId,
activeThreadRef,
activeThreadWorktreePath,
allocatableActiveTerminalIds,
gitCwd,
openTerminal,
],
);
const splitPanelTerminalVertical = useCallback(() => {
splitPanelTerminal("vertical");
}, [splitPanelTerminal]);
const activatePanelTerminal = useCallback(
(terminalId: string) => {
if (!activeThreadRef || activeRightPanelSurface?.kind !== "terminal") return;
useRightPanelStore
.getState()
.activateTerminal(activeThreadRef, activeRightPanelSurface.id, terminalId);
setTerminalFocusRequestId((value) => value + 1);
},
[activeRightPanelSurface, activeThreadRef],
);
const closePanelTerminal = useCallback(
(terminalId: string) => {
if (!activeThreadRef || activeRightPanelSurface?.kind !== "terminal") return;
void closeTerminalMutation({
environmentId: activeThreadRef.environmentId,
input: { threadId: activeThreadRef.threadId, terminalId, deleteHistory: true },
});
storeCloseTerminal(activeThreadRef, terminalId);
useRightPanelStore
.getState()
.closeTerminal(activeThreadRef, activeRightPanelSurface.id, terminalId);
setTerminalFocusRequestId((value) => value + 1);
},
[activeRightPanelSurface, activeThreadRef, closeTerminalMutation, storeCloseTerminal],
);
const requestCloseTerminal = useCallback(
(terminalId: string) => {
const label = activeTerminalLabelsById.get(terminalId) ?? getTerminalLabel(terminalId);
void confirmTerminalClose([label]).then((confirmed) => {
if (confirmed) closeTerminal(terminalId);
});
},
[activeTerminalLabelsById, closeTerminal],
);
const requestClosePanelTerminal = useCallback(
(terminalId: string) => {
const label = activeTerminalLabelsById.get(terminalId) ?? getTerminalLabel(terminalId);
void confirmTerminalClose([label]).then((confirmed) => {
if (confirmed) closePanelTerminal(terminalId);
});
},
[activeTerminalLabelsById, closePanelTerminal],
);
const activateRightPanelSurface = useCallback(
(surface: RightPanelSurface) => {
if (!activeThreadRef) return;
useRightPanelStore.getState().activateSurface(activeThreadRef, surface.id);
if (surface.kind === "preview" && surface.resourceId) {
setActivePreviewTab(activeThreadRef, surface.resourceId);
}
if (surface.kind === "terminal") {
setTerminalFocusRequestId((value) => value + 1);
}
if (surface.kind === "diff" && !diffOpen) {
onDiffPanelOpen?.();
}
},
[activeThreadRef, diffOpen, onDiffPanelOpen],
);
const toggleRightPanel = useCallback(() => {
if (!activeThreadRef) return;
if (rightPanelOpen) {
closePreviewPanel();
return;
}
useRightPanelStore.getState().toggleVisibility(activeThreadRef);
}, [activeThreadRef, closePreviewPanel, rightPanelOpen]);
const toggleRightPanelMaximized = useCallback(() => {
if (!canMaximizeRightPanel) return;
setMaximizedRightPanelThreadKey((threadKey) =>
threadKey === routeThreadKey ? null : routeThreadKey,
);
}, [canMaximizeRightPanel, routeThreadKey]);
const cleanupRightPanelSurfaces = useCallback(
(surfaces: readonly RightPanelSurface[]) => {
if (!activeThreadRef) return;
for (const surface of surfaces) {
if (surface.kind === "preview" && surface.resourceId) {
void closePreviewSession({
closePreview,
snapshot: activePreviewState.sessions[surface.resourceId] ?? null,
tabId: surface.resourceId,
threadRef: activeThreadRef,
});
}
if (surface.kind === "terminal") {
for (const terminalId of surface.terminalIds) {
storeCloseTerminal(activeThreadRef, terminalId);
void closeTerminalMutation({
environmentId: activeThreadRef.environmentId,
input: { threadId: activeThreadRef.threadId, terminalId, deleteHistory: true },
});
}
}
}
},
[
activeThreadRef,
activePreviewState.sessions,
closePreview,
closeTerminalMutation,
storeCloseTerminal,
],
);
const closeAfterAgentBrowserConfirmation = useCallback(
(surfaces: readonly RightPanelSurface[], closeSurfaces: () => void) => {
const message = agentControlledBrowserCloseConfirmation(
surfaces,
activePreviewState.desktopByTabId,
);
if (!message) {
closeSurfaces();
return;
}
const localApi = readLocalApi();
if (!localApi) return;
void localApi.dialogs.confirm(message, { variant: "destructive" }).then(
(confirmed) => {
if (confirmed) closeSurfaces();
},
() => undefined,
);
},
[activePreviewState.desktopByTabId],
);
const syncActivePreviewSurface = useCallback(() => {
if (!activeThreadRef) return;
const nextActiveSurface = selectActiveRightPanelSurface(
useRightPanelStore.getState().byThreadKey,
activeThreadRef,
);
if (nextActiveSurface?.kind === "preview" && nextActiveSurface.resourceId) {
setActivePreviewTab(activeThreadRef, nextActiveSurface.resourceId);
}
}, [activeThreadRef]);
const finishRightPanelSurfaceClose = useCallback(
(surfaces: readonly RightPanelSurface[]) => {
if (!activeThreadRef) return;
cleanupRightPanelSurfaces(surfaces);
const store = useRightPanelStore.getState();
for (const surface of surfaces) {
store.closeSurface(activeThreadRef, surface.id);
}
syncActivePreviewSurface();
},
[activeThreadRef, cleanupRightPanelSurfaces, syncActivePreviewSurface],
);
const closeRightPanelSurface = useCallback(
(surface: RightPanelSurface) => {
if (!activeThreadRef) return;
const finishClose = () => finishRightPanelSurfaceClose([surface]);
if (surface.kind === "preview") {
closeAfterAgentBrowserConfirmation([surface], finishClose);
return;
}
if (surface.kind !== "terminal") {
finishClose();
return;
}
const activeLabel =
activeTerminalLabelsById.get(surface.activeTerminalId) ??
getTerminalLabel(surface.activeTerminalId);
const otherLabels = surface.terminalIds
.filter((terminalId) => terminalId !== surface.activeTerminalId)
.map(
(terminalId) => activeTerminalLabelsById.get(terminalId) ?? getTerminalLabel(terminalId),
);
void confirmTerminalClose([activeLabel, ...otherLabels]).then((confirmed) => {
if (confirmed) finishClose();
});
},
[
activeThreadRef,
activeTerminalLabelsById,
closeAfterAgentBrowserConfirmation,
finishRightPanelSurfaceClose,
],
);
const closeOtherRightPanelSurfaces = useCallback(
(surface: RightPanelSurface) => {
if (!activeThreadRef) return;
const surfaces = rightPanelState.surfaces.filter((entry) => entry.id !== surface.id);
const finishClose = () => finishRightPanelSurfaceClose(surfaces);
closeAfterAgentBrowserConfirmation(surfaces, finishClose);
},
[
activeThreadRef,
closeAfterAgentBrowserConfirmation,
finishRightPanelSurfaceClose,
rightPanelState.surfaces,
],
);
const closeRightPanelSurfacesToRight = useCallback(
(surface: RightPanelSurface) => {
if (!activeThreadRef) return;
const surfaceIndex = rightPanelState.surfaces.findIndex((entry) => entry.id === surface.id);
if (surfaceIndex < 0) return;
const surfaces = rightPanelState.surfaces.slice(surfaceIndex + 1);
const finishClose = () => finishRightPanelSurfaceClose(surfaces);
closeAfterAgentBrowserConfirmation(surfaces, finishClose);
},
[
activeThreadRef,
closeAfterAgentBrowserConfirmation,
finishRightPanelSurfaceClose,
rightPanelState.surfaces,
],
);
const closeAllRightPanelSurfaces = useCallback(() => {
if (!activeThreadRef) return;
const finishClose = () => finishRightPanelSurfaceClose(rightPanelState.surfaces);
closeAfterAgentBrowserConfirmation(rightPanelState.surfaces, finishClose);
}, [
activeThreadRef,
closeAfterAgentBrowserConfirmation,
finishRightPanelSurfaceClose,
rightPanelState.surfaces,
]);
const copyRightPanelFilePath = useCallback((relativePath: string) => {
if (typeof window === "undefined" || !navigator.clipboard?.writeText) {
toastManager.add(
stackedThreadToast({
type: "error",
title: "Failed to copy path",
description: "Clipboard API unavailable.",
}),
);
return;
}
void navigator.clipboard.writeText(relativePath).then(
() => {
toastManager.add({
type: "success",
title: "Path copied",
description: relativePath,
});
},
(error) => {
toastManager.add(
stackedThreadToast({
type: "error",
title: "Failed to copy path",
description: error instanceof Error ? error.message : "An error occurred.",
}),
);
},
);
}, []);
useEffect(
() =>
subscribePreviewAction((action) => {
if (action === "toggle-panel") togglePreviewPanel();
}),
[togglePreviewPanel],
);
const persistThreadSettingsForNextTurn = useCallback(
async (input: {
threadId: ThreadId;
createdAt: string;
modelSelection?: ModelSelection;
branch?: string;
runtimeMode: RuntimeMode;
interactionMode: ProviderInteractionMode;
}): Promise<AtomCommandResult<void, unknown>> => {
if (!serverThread) {
return AsyncResult.success(undefined);
}
let result: AtomCommandResult<void, unknown> = AsyncResult.success(undefined);
const metadataUpdate = resolveThreadMetadataUpdateForNextTurn({
currentModelSelection: serverThread.modelSelection,
...(input.modelSelection ? { nextModelSelection: input.modelSelection } : {}),
currentBranch: serverThread.branch,
...(input.branch ? { nextBranch: input.branch } : {}),
});
if (metadataUpdate) {
result = mapAtomCommandResult(
await updateThreadMetadata({
environmentId,
input: {
threadId: input.threadId,
...metadataUpdate,
},
}),
() => undefined,
);
if (result._tag === "Failure") {
return result;
}
}
if (input.runtimeMode !== serverThread.runtimeMode) {
result = mapAtomCommandResult(
await setThreadRuntimeMode({
environmentId,
input: {
threadId: input.threadId,
runtimeMode: input.runtimeMode,
createdAt: input.createdAt,
},
}),
() => undefined,
);
if (result._tag === "Failure") {
return result;
}
}
if (input.interactionMode !== serverThread.interactionMode) {
result = mapAtomCommandResult(
await setThreadInteractionMode({
environmentId,
input: {
threadId: input.threadId,
interactionMode: input.interactionMode,
createdAt: input.createdAt,
},
}),
() => undefined,
);
}
return result;
},
[
environmentId,
serverThread,
setThreadInteractionMode,
setThreadRuntimeMode,
updateThreadMetadata,
],
);
// Debounce *showing* the scroll-to-bottom pill so it doesn't flash during
// thread switches. LegendList fires scroll events with isAtEnd=false while
// initialScrollAtEnd is settling; hiding is always immediate.
const showScrollDebouncer = useRef(
new Debouncer(() => setShowScrollToBottom(true), { wait: 150 }),
);
const timelineScrollIntentRef = useRef<"toward-end" | "away-from-end" | null>(null);
const timelineScrollModeRef = useRef<TimelineScrollMode>("following-end");
// State mirror of the follow mode refs. LegendList's maintainScrollAtEnd
// re-pins on its own (independent of the refs), so the timeline needs a
// render-visible flag to switch it off once the user scrolls away.
const [timelineLiveFollowEnabled, setTimelineLiveFollowEnabled] = useState(true);
const pendingTimelineAnchorRef = useRef<MessageId | null>(null);
const positionedTimelineAnchorRef = useRef<MessageId | null>(null);
const settledTimelineAnchorRef = useRef<MessageId | null>(null);
const activeTimelineAnchorIndexRef = useRef<number | null>(null);
const anchorUserScrollGenerationRef = useRef(0);
const liveFollowUserScrollGenerationRef = useRef<number | null>(0);
// Manual navigation stops live-follow without removing anchored end space.
// Collapsing that space during a gesture clamps the viewport back to the end.
const cancelTimelineLiveFollowForUserNavigation = useCallback(() => {
anchorUserScrollGenerationRef.current += 1;
timelineScrollModeRef.current = "free-scrolling";
liveFollowUserScrollGenerationRef.current = null;
setTimelineLiveFollowEnabled(false);
pendingTimelineAnchorRef.current = null;
positionedTimelineAnchorRef.current = null;
settledTimelineAnchorRef.current = null;
activeTimelineAnchorIndexRef.current = null;
}, []);
const cancelTimelineLiveFollowForUserNavigationRef = useRef(
cancelTimelineLiveFollowForUserNavigation,
);
useEffect(() => {
cancelTimelineLiveFollowForUserNavigationRef.current =
cancelTimelineLiveFollowForUserNavigation;
}, [cancelTimelineLiveFollowForUserNavigation]);
const getActiveTimelineTurnMetrics = useCallback(
(list?: LegendListRef | null) => {
const resolvedList = list ?? legendListRef.current;
const anchorIndex = activeTimelineAnchorIndexRef.current;
const state = resolvedList?.getState();
if (!resolvedList || !state || anchorIndex === null) {
return null;
}
return getAnchoredTurnMetrics({
state,
anchorIndex,
composerOverlayHeight: composerTimelineInset,
anchorOffset: CHAT_TIMELINE_ANCHOR_OFFSET,
});
},
[composerTimelineInset],
);
const timelineRealContentOverflowsViewport = useCallback(
(list?: LegendListRef | null) =>
timelineContentOverflowsViewport((list ?? legendListRef.current)?.getState(), {
composerInset: composerTimelineInset,
anchorOffset: CHAT_TIMELINE_ANCHOR_OFFSET,
}),
[composerTimelineInset],
);
const pageScrollControllerRef = useRef<ReturnType<typeof createPageScrollController> | null>(
null,
);
const handlePageScrollStart = useEffectEvent((key: PageScrollKey) => {
timelineScrollIntentRef.current = key === "PageUp" ? "away-from-end" : "toward-end";
composerRef.current?.collapseForTimelineScrollKey(key);
if ((key === "PageUp" && timelineRealContentOverflowsViewport()) || !isTimelineAtLogicalEnd()) {
cancelTimelineLiveFollowForUserNavigation();
}
});
useEffect(() => {
const controller = createPageScrollController({
getContainer: () => legendListRef.current?.getScrollableNode() ?? null,
getScrollPaddingBottomPx: () => composerOverlayElement?.getBoundingClientRect().height ?? 0,
onScrollStart: handlePageScrollStart,
});
pageScrollControllerRef.current = controller;
return () => {
controller.dispose();
if (pageScrollControllerRef.current === controller) {
pageScrollControllerRef.current = null;
}
};
}, [composerOverlayElement]);
const onComposerPageScrollKeyDown = useCallback((key: PageScrollKey) => {
pageScrollControllerRef.current?.handleKeyDown(key);
}, []);
const onComposerPageScrollKeyUp = useCallback((key: string) => {
pageScrollControllerRef.current?.handleKeyUp(key);
}, []);
const onComposerPageScrollRelease = useCallback(() => {
pageScrollControllerRef.current?.releaseActiveKey();
}, []);
// Live-follow stays active after send/thread-open until an actual list scroll
// gesture opts out.
const scrollToEnd = useCallback((animated = false) => {
isAtEndRef.current = true;
timelineScrollModeRef.current = "following-end";
liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current;
setTimelineLiveFollowEnabled(true);
pendingTimelineAnchorRef.current = null;
positionedTimelineAnchorRef.current = null;
settledTimelineAnchorRef.current = null;
activeTimelineAnchorIndexRef.current = null;
showScrollDebouncer.current.cancel();
setShowScrollToBottom(false);
setTimelineAnchor(releaseChatTimelineAnchor);
requestAnimationFrame(() => {
void legendListRef.current?.scrollToEnd?.({ animated });
});
}, []);
const displayedTimelineKeyRef = useRef(displayedTimeline.displayThreadKey);
useLayoutEffect(() => {
const displayKey = displayedTimeline.displayThreadKey;
if (displayKey === null || displayKey !== activeThreadKey) {
displayedTimelineKeyRef.current = displayKey;
return;
}
if (displayedTimelineKeyRef.current === displayKey) {
return;
}
displayedTimelineKeyRef.current = displayKey;
// Keep the list mounted across jumps; pin the newly displayed thread to
// its end the way a remount used to via initialScrollAtEnd.
scrollToEnd();
}, [activeThreadKey, displayedTimeline.displayThreadKey, scrollToEnd]);
useLayoutEffect(() => {
if (timelineScrollModeRef.current !== "anchoring-new-turn") {
return;
}
if (
shouldReleaseTimelineAnchorForToolActivity({
anchorMessageId: timelineAnchorMessageId,
liveFollowEnabled: timelineLiveFollowEnabled,
runningTurnId: activeRunningTurnId,
timelineEntries,
})
) {
scrollToEnd();
}
}, [
activeRunningTurnId,
scrollToEnd,
timelineAnchorMessageId,
timelineEntries,
timelineLiveFollowEnabled,
]);
useEffect(() => {
let removeListeners: (() => void) | null = null;
let frame: number | null = null;
const attach = (remainingAttempts: number) => {
frame = requestAnimationFrame(() => {
frame = null;
const scrollNode = legendListRef.current?.getScrollableNode();
if (!scrollNode) {
// The list may not have mounted on the first frame after a thread
// switch — without a retry the opt-out listeners never attach and
// live-follow becomes impossible to escape for the whole thread.
if (remainingAttempts > 0) {
attach(remainingAttempts - 1);
}
return;
}
const handleManualNavigation = () => {
cancelTimelineLiveFollowForUserNavigationRef.current();
};
// The gestures below must only break follow when they can actually
// move the viewport away from the live edge. Follow now gates
// LegendList's maintainScrollAtEnd, so a spurious break while pinned
// at the end produces no scroll event, never re-arms, and streaming
// silently stops following. Underflowing content can't scroll at all,
// so nothing there should break follow.
const contentScrollsUp = () => timelineRealContentOverflowsViewport();
// The follow re-arm band, not the strict flag: streaming growth makes
// isAtEnd flicker false for a frame before the follow scroll catches
// up, and a gesture landing in that window while still pinned would
// otherwise break follow with no scroll event left to re-arm it.
const viewportIsAwayFromEnd = () =>
resolveTimelineIsAtEnd(legendListRef.current?.getState()) === false;
// Only an upward wheel is a navigation intent; wheeling down while
// following either does nothing (at the end) or moves toward it.
const handleWheel = (event: WheelEvent) => {
if (event.deltaY > 0) {
timelineScrollIntentRef.current = "toward-end";
if (isAtEndRef.current) {
composerRef.current?.restoreAfterTimelineReachedEnd();
}
} else if (event.deltaY < 0) {
timelineScrollIntentRef.current = "away-from-end";
}
if (
event.deltaY < 0 &&
contentScrollsUp() &&
!toolGroupConsumesUpwardNavigation(event.target)
) {
handleManualNavigation();
}
};
// Touch direction isn't observable here (touchmove fires on any
// finger motion, scrolling or not), so break only once the drag has
// actually carried the viewport out of the end band — an upward flick
// gets there within its first few events and later touchmoves break.
const handleTouchMove = () => {
if (viewportIsAwayFromEnd()) {
handleManualNavigation();
}
};
// Scrollbar drags produce no wheel/touch events; they are the only
// pointerdowns whose target is the scroll node itself rather than a
// message row. Content clicks break follow only away from the end
// (reading or selecting up there must hold position); clicking near
// the live edge keeps following.
const handlePointerDown = (event: PointerEvent) => {
if (event.target === scrollNode) {
if (contentScrollsUp()) {
handleManualNavigation();
}
return;
}
if (viewportIsAwayFromEnd()) {
handleManualNavigation();
}
};
// Keyboard scrolling (PageUp/Home/ArrowUp) bypasses wheel and
// pointer events entirely; without this the timeline yanks back to
// the end on the next stream chunk. Clicking message text can leave
// DOM focus on body, so these keys must also be heard at document.
const handleKeyDown = (event: KeyboardEvent) => {
if (
!(event.target instanceof Node) ||
(!scrollNode.contains(event.target) &&
event.target !== document.body &&
event.target !== document.documentElement) ||
event.defaultPrevented ||
event.isComposing ||
event.altKey ||
event.ctrlKey ||
event.metaKey ||
event.shiftKey ||
eventPathContainsSelector(event, TYPE_TO_FOCUS_EDITABLE_SELECTOR) ||
document.querySelector(TYPE_TO_FOCUS_FLOATING_LAYER_SELECTOR)
) {
return;
}
switch (event.key) {
case "PageUp":
case "Home":
case "ArrowUp":
timelineScrollIntentRef.current = "away-from-end";
if (contentScrollsUp() && !toolGroupConsumesUpwardNavigation(event.target)) {
handleManualNavigation();
composerRef.current?.collapseForTimelineScrollKey(event.key);
}
break;
case "PageDown":
case "End":
case "ArrowDown":
timelineScrollIntentRef.current = "toward-end";
if (viewportIsAwayFromEnd()) {
handleManualNavigation();
}
composerRef.current?.collapseForTimelineScrollKey(event.key);
if (isTimelineAtLogicalEnd()) {
composerRef.current?.restoreAfterTimelineReachedEnd();
}
break;
default:
break;
}
};
scrollNode.addEventListener("wheel", handleWheel, {
passive: true,
});
scrollNode.addEventListener("touchmove", handleTouchMove, {
passive: true,
});
scrollNode.addEventListener("pointerdown", handlePointerDown, {
passive: true,
});
document.addEventListener("keydown", handleKeyDown);
removeListeners = () => {
scrollNode.removeEventListener("wheel", handleWheel);
scrollNode.removeEventListener("touchmove", handleTouchMove);
scrollNode.removeEventListener("pointerdown", handlePointerDown);
document.removeEventListener("keydown", handleKeyDown);
};
});
};
attach(12);
return () => {
if (frame !== null) {
cancelAnimationFrame(frame);
}
removeListeners?.();
};
}, [activeThread?.id, isTimelineAtLogicalEnd, timelineRealContentOverflowsViewport]);
const onTimelineAnchorReady = useCallback((messageId: MessageId, anchorIndex: number) => {
// Anchored-end space can be remeasured when the turn completes. Once the
// user has scrolled away (or returned to ordinary end-following), that
// remeasurement must not restart the send-time anchor positioning.
if (timelineScrollModeRef.current !== "anchoring-new-turn") {
return;
}
if (pendingTimelineAnchorRef.current === messageId) {
pendingTimelineAnchorRef.current = null;
}
activeTimelineAnchorIndexRef.current = anchorIndex;
if (positionedTimelineAnchorRef.current === messageId) {
return;
}
positionedTimelineAnchorRef.current = messageId;
settledTimelineAnchorRef.current = null;
const positionAnchor = (remainingAttempts: number) => {
requestAnimationFrame(() => {
if (positionedTimelineAnchorRef.current !== messageId) {
return;
}
const list = legendListRef.current;
if (!list) {
if (remainingAttempts > 0) {
positionAnchor(remainingAttempts - 1);
}
return;
}
void list
.scrollToIndex({
index: anchorIndex,
animated: true,
viewPosition: 0,
viewOffset: CHAT_TIMELINE_ANCHOR_OFFSET,
})
.then(() => {
if (positionedTimelineAnchorRef.current !== messageId) {
return;
}
settledTimelineAnchorRef.current = messageId;
});
});
};
requestAnimationFrame(() => positionAnchor(12));
}, []);
const onToolOutputCollapsedAtEnd = useCallback(() => {
composerRef.current?.restoreAfterTimelineReachedEnd();
}, []);
const onIsAtEndChange = useCallback((isAtEnd: boolean) => {
if (
!isAtEnd &&
liveFollowUserScrollGenerationRef.current === anchorUserScrollGenerationRef.current
) {
showScrollDebouncer.current.cancel();
setShowScrollToBottom(false);
return;
}
if (isAtEndRef.current === isAtEnd) return;
isAtEndRef.current = isAtEnd;
if (isAtEnd) {
if (timelineScrollIntentRef.current === "toward-end") {
composerRef.current?.restoreAfterTimelineReachedEnd();
}
timelineScrollModeRef.current = "following-end";
liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current;
setTimelineLiveFollowEnabled(true);
// Reachable only once manual navigation has already broken follow, so
// the anchored turn framing is over: the user scrolled back to the live
// edge and expects the stream to stick to it again, exactly like the
// scroll-to-bottom pill.
setTimelineAnchor(releaseChatTimelineAnchor);
showScrollDebouncer.current.cancel();
setShowScrollToBottom(false);
} else {
timelineScrollModeRef.current = "free-scrolling";
liveFollowUserScrollGenerationRef.current = null;
showScrollDebouncer.current.maybeExecute();
}
}, []);
// Anchored end space intentionally disables LegendList's normal end-follow so
// the sent message can stay near the top. T3 only owns streaming adjustments
// during that mode; LegendList owns ordinary end-follow everywhere else.
useEffect(() => {
if (!activeThread?.id) {
return;
}
if (liveFollowUserScrollGenerationRef.current !== anchorUserScrollGenerationRef.current) {
return;
}
if (timelineScrollModeRef.current !== "anchoring-new-turn") {
return;
}
let secondFrame: number | null = null;
const frame = requestAnimationFrame(() => {
secondFrame = requestAnimationFrame(() => {
if (liveFollowUserScrollGenerationRef.current !== anchorUserScrollGenerationRef.current) {
return;
}
if (pendingTimelineAnchorRef.current !== null) {
return;
}
if (
positionedTimelineAnchorRef.current !== null &&
settledTimelineAnchorRef.current !== positionedTimelineAnchorRef.current
) {
return;
}
const list = legendListRef.current;
if (!list) {
return;
}
const metrics = getActiveTimelineTurnMetrics(list);
if (!metrics || metrics.scrollDeltaToRevealEnd <= 1) {
return;
}
const nextOffset = list.getState().scroll + metrics.scrollDeltaToRevealEnd;
void list.scrollToOffset({ offset: nextOffset, animated: false });
});
});
return () => {
cancelAnimationFrame(frame);
if (secondFrame !== null) {
cancelAnimationFrame(secondFrame);
}
};
}, [activeThread?.id, timelineEntries, getActiveTimelineTurnMetrics]);
useEffect(() => {
setPullRequestDialogState(null);
isAtEndRef.current = true;
timelineScrollIntentRef.current = null;
timelineScrollModeRef.current = "following-end";
liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current;
setTimelineLiveFollowEnabled(true);
pendingTimelineAnchorRef.current = null;
positionedTimelineAnchorRef.current = null;
settledTimelineAnchorRef.current = null;
activeTimelineAnchorIndexRef.current = null;
showScrollDebouncer.current.cancel();
setShowScrollToBottom(false);
// activeThreadRef resets transitively with the active thread.
}, [activeThread?.id]);
useEffect(() => {
if (!activeThread?.id || terminalUiState.terminalOpen) return;
const frame = window.requestAnimationFrame(() => {
focusComposer();
});
return () => {
window.cancelAnimationFrame(frame);
};
}, [activeThread?.id, focusComposer, terminalUiState.terminalOpen]);
// Tabbing back into the app lands focus wherever it last was, often the right panel or the
// body. Put it in the composer unless something that takes typing already holds it. The
// drawer terminal owns keyboard input while it is open, so it opts out here; a right panel
// terminal is a surface and is recognized by the predicate instead. Mobile is left alone so
// returning to the app does not raise the keyboard.
useEffect(() => {
if (!activeThread?.id || terminalUiState.terminalOpen || isMobileViewport) return;
let frame: number | null = null;
const onWindowFocus = () => {
if (frame !== null) window.cancelAnimationFrame(frame);
// The element that held focus receives it again after the window's own event, and the
// composer ignores that same frame so a restored focus does not lift a scroll-collapsed
// composer. Wait one more frame so this focus counts as a request to expand it.
frame = window.requestAnimationFrame(() => {
frame = window.requestAnimationFrame(() => {
frame = null;
if (shouldRefocusComposerOnWindowFocus(document.activeElement)) focusComposer();
});
});
};
window.addEventListener("focus", onWindowFocus);
return () => {
window.removeEventListener("focus", onWindowFocus);
if (frame !== null) window.cancelAnimationFrame(frame);
};
}, [activeThread?.id, focusComposer, isMobileViewport, terminalUiState.terminalOpen]);
useEffect(() => {
if (!activeThread?.id) return;
if (activeThread.messages.length === 0) {
return;
}
const serverIds = new Set(activeThread.messages.map((message) => message.id));
const removedMessages = optimisticUserMessages.filter((message) => serverIds.has(message.id));
if (removedMessages.length === 0) {
return;
}
const timer = window.setTimeout(() => {
setOptimisticUserMessages((existing) =>
existing.filter((message) => !serverIds.has(message.id)),
);
}, 0);
for (const removedMessage of removedMessages) {
const previewUrls = collectUserMessageBlobPreviewUrls(removedMessage);
if (previewUrls.length > 0) {
handoffAttachmentPreviews(removedMessage.id, previewUrls);
continue;
}
revokeUserMessagePreviewUrls(removedMessage);
}
return () => {
window.clearTimeout(timer);
};
}, [activeThread?.id, activeThread?.messages, handoffAttachmentPreviews, optimisticUserMessages]);
useEffect(() => {
setOptimisticUserMessages((existing) => {
for (const message of existing) {
revokeUserMessagePreviewUrls(message);
}
return [];
});
resetLocalDispatch();
setExpandedImage(null);
}, [draftId, resetLocalDispatch, threadId]);
const closeExpandedImage = useCallback(() => {
setExpandedImage(null);
}, []);
const activeWorktreePath = activeThread?.worktreePath ?? null;
const derivedEnvMode: DraftThreadEnvMode = resolveEffectiveEnvMode({
activeWorktreePath,
hasServerThread: isServerThread,
draftThreadEnvMode: isLocalDraftThread ? draftThread?.envMode : undefined,
});
const canOverrideServerThreadEnvMode = Boolean(
isServerThread &&
activeThread &&
activeThread.messages.length === 0 &&
activeThread.worktreePath === null &&
!envLocked,
);
const envMode: DraftThreadEnvMode = canOverrideServerThreadEnvMode
? (pendingServerThreadEnvMode ?? draftThread?.envMode ?? derivedEnvMode)
: derivedEnvMode;
const activeThreadBranch =
canOverrideServerThreadEnvMode && pendingServerThreadBranch !== undefined
? pendingServerThreadBranch
: (activeThread?.branch ?? null);
const startFromOrigin = isLocalDraftThread
? (draftThread?.startFromOrigin ?? false)
: canOverrideServerThreadEnvMode
? (pendingServerThreadStartFromOriginByThreadId[activeThread?.id ?? ""] ??
activeProjectSettings.settings.newWorktreesStartFromOrigin)
: false;
const sendEnvMode = resolveSendEnvMode({
requestedEnvMode: envMode,
isGitRepo,
});
const localCheckoutBranchMismatch = useMemo(
() =>
isServerThread
? resolveLocalCheckoutBranchMismatch({
effectiveEnvMode: envMode,
activeWorktreePath,
activeThreadBranch,
currentGitBranch: gitStatusQuery.data?.refName ?? null,
})
: null,
[activeThreadBranch, activeWorktreePath, envMode, gitStatusQuery.data?.refName, isServerThread],
);
const activeComposerTasksProgress = useMemo(() => {
if (!activeLatestTurn || latestTurnSettled || activePlan?.turnId !== activeLatestTurn.turnId) {
return null;
}
const currentStep =
activePlan.steps.find((step) => step.status === "inProgress") ??
activePlan.steps.find((step) => step.status === "pending");
if (!currentStep) return null;
return {
step: currentStep.step,
completedSteps: activePlan.steps.filter((step) => step.status === "completed").length,
totalSteps: activePlan.steps.length,
};
}, [activeLatestTurn, activePlan, latestTurnSettled]);
const activeComposerTaskSteps =
activeComposerTasksProgress && activePlan && activePlan.turnId === activeLatestTurn?.turnId
? activePlan.steps
: null;
const publishComposerOverlayHeight = useCallback(
(height: number) => {
const nextHeight = Math.ceil(height);
if (nextHeight <= 0) return;
const nextInset = resolveComposerTimelineInset({
currentInset: composerTimelineInsetRef.current,
overlayHeight: nextHeight,
isResting: composerRestingRef.current,
});
if (composerTimelineInsetRef.current !== nextInset) {
composerTimelineInsetRef.current = nextInset;
setComposerTimelineInset(nextInset);
}
const mainSurface = composerOverlayElement?.querySelector<HTMLElement>(
'[data-chat-composer-main-surface="true"]',
);
const button = composerOverlayElement?.parentElement?.querySelector<HTMLElement>(
'button[aria-label="Scroll to end"]',
);
const clearance =
composerOverlayElement && mainSurface && button
? resolveScrollToEndClearance({
overlayHeight: nextHeight,
mainSurfaceTop: mainSurface.getBoundingClientRect().top,
button: button.getBoundingClientRect(),
attachments: Array.from(
composerOverlayElement.querySelectorAll<HTMLElement>(
'[data-composer-banner-surface="attached"]',
),
(element) => element.getBoundingClientRect(),
),
})
: nextHeight;
setScrollToEndClearance(clearance);
},
[composerOverlayElement],
);
// The composer reports its resting flag from a layout effect, which runs
// before this component's own layout effects and before any resize
// observation, so every measurement below sees the flag for its layout.
// Only the flag is stored here: the stored height still belongs to the
// previous layout, and the composer publishes the new layout's height
// itself once it has measured it.
const onComposerRestingChange = useCallback((resting: boolean) => {
composerRestingRef.current = resting;
}, []);
// A held reservation belongs to the previous thread's draft. Rebuild it from
// this thread's overlay so a tall draft elsewhere does not pad this one.
useLayoutEffect(() => {
if (!composerOverlayElement) return;
composerTimelineInsetRef.current = 0;
publishComposerOverlayHeight(composerOverlayElement.getBoundingClientRect().height);
}, [activeThreadKey, composerOverlayElement, publishComposerOverlayHeight]);
useLayoutEffect(() => {
if (!composerOverlayElement) return;
const updateHeight = () => {
publishComposerOverlayHeight(composerOverlayElement.getBoundingClientRect().height);
};
updateHeight();
if (typeof ResizeObserver === "undefined") return;
const resizeObserver = new ResizeObserver(updateHeight);
resizeObserver.observe(composerOverlayElement);
return () => {
resizeObserver.disconnect();
};
}, [composerOverlayElement, publishComposerOverlayHeight, showScrollToBottom]);
const openPanelPullRequestUrl = useOpenPanelPullRequestUrl(activeThreadRef);
const activeThreadReferenceCopyTarget = useMemo(
() =>
activeThreadId === null || !isServerThread
? null
: resolveThreadReferenceCopyTarget({
threadId: activeThreadId,
openPanelPullRequestUrl,
pullRequests: activeThreadMetadata?.pullRequests,
linkedPullRequestUrl: linkedThreadPullRequest?.url ?? null,
}),
[
activeThreadId,
isServerThread,
activeThreadMetadata?.pullRequests,
linkedThreadPullRequest?.url,
openPanelPullRequestUrl,
],
);
const copyActiveThreadReference = useCallback(() => {
const target = activeThreadReferenceCopyTarget;
if (target === null) return;
void writeTextToClipboard(target.value, target.clipboardTarget).then(
(didCopy) => {
if (!didCopy) return;
toastManager.add({
type: "success",
title: target.successTitle,
description: target.value,
});
},
(error) => {
console.error(error);
toastManager.add(
stackedThreadToast({
type: "error",
title: target.failureTitle,
description: error instanceof Error ? error.message : "An error occurred.",
}),
);
},
);
}, [activeThreadReferenceCopyTarget]);
const addPullRequestSurface = useCallback(() => {
if (!supportsPullRequests || activeThreadRef === null || linkedThreadPullRequest === null)
return;
useRightPanelStore.getState().openPullRequest(activeThreadRef, linkedThreadPullRequest);
}, [activeThreadRef, linkedThreadPullRequest, supportsPullRequests]);
const pullRequestSurfaceAvailable = supportsPullRequests && linkedThreadPullRequest !== null;
const supportsSettlement = serverConfig?.environment.capabilities.threadSettlement === true;
const supportsSnooze = serverConfig?.environment.capabilities.threadSnooze === true;
const supportsPinning = serverConfig?.environment.capabilities.threadPinning === true;
const activeThreadPinned = supportsPinning && activeThreadShell?.pinnedAt != null;
const nowMinute = useNowMinute();
const snoozeNow = new Date().toISOString();
const activeThreadSnoozed =
activeThreadShell !== null &&
supportsSnooze &&
effectiveSnoozed(activeThreadShell, { now: snoozeNow });
const [snoozeWakeTick, bumpSnoozeWakeTick] = useState(0);
void snoozeWakeTick;
const activeThreadWokeAt =
activeThreadShell !== null && supportsSnooze
? threadWokeAt(activeThreadShell, { now: snoozeNow })
: null;
useEffect(() => {
if (!activeThreadSnoozed) return;
const wakeAtMs = Date.parse(activeThreadShell?.snoozedUntil ?? "");
if (!Number.isFinite(wakeAtMs)) return;
const id = window.setTimeout(
() => bumpSnoozeWakeTick((tick) => tick + 1),
Math.min(Math.max(0, wakeAtMs - Date.now()) + 50, 2_147_483_647),
);
return () => window.clearTimeout(id);
}, [activeThreadShell?.snoozedUntil, activeThreadSnoozed, snoozeWakeTick]);
const acknowledgeActiveThreadWoke = useCallback(() => {
if (activeThreadRef === null || activeThreadWokeAt === null) return;
markThreadVisited(scopedThreadKey(activeThreadRef), activeThreadWokeAt);
}, [activeThreadRef, activeThreadWokeAt, markThreadVisited]);
// Mirror of the sidebar's Woke pill for the open thread.
const activeThreadLastVisitedAt = useUiStateStore((store) =>
activeThreadKey === null ? undefined : store.threadLastVisitedAtById[activeThreadKey],
);
const activeThreadWokeVisible = useMemo(() => {
if (activeThreadWokeAt === null) return false;
if (activeThreadShell?.settledOverride === "settled") return false;
const wokeAtMs = Date.parse(activeThreadWokeAt);
if (Number.isNaN(wokeAtMs)) return false;
// Having the thread open counts as a visit at completedAt (the effect
// above stamps it); folding that floor in here keeps a completion-
// triggered wake from flashing a banner for one frame before the stamp
// lands. An unparseable stored visit counts as never-visited: corrupt
// local data must not eat the wake signal.
const storedVisitMs = activeThreadLastVisitedAt ? Date.parse(activeThreadLastVisitedAt) : NaN;
const completedAtMs = activeLatestTurn?.completedAt
? Date.parse(activeLatestTurn.completedAt)
: NaN;
const lastVisitedMs = Math.max(
Number.isNaN(storedVisitMs) ? -Infinity : storedVisitMs,
Number.isNaN(completedAtMs) ? -Infinity : completedAtMs,
);
return lastVisitedMs < wokeAtMs;
}, [
activeLatestTurn?.completedAt,
activeThreadLastVisitedAt,
activeThreadShell,
activeThreadWokeAt,
]);
const activeThreadSettled =
supportsSettlement && activeThreadShell?.settledOverride === "settled";
const unsettleThreadMutation = useAtomCommand(threadEnvironment.unsettle, {
reportFailure: false,
});
// Keyed by thread, not a boolean: the pending state must follow the thread
// it belongs to across navigation, and a request resolving for thread A
// must never clear (or re-enable) thread B's button.
const [unsettlingThreadKey, setUnsettlingThreadKey] = useState<string | null>(null);
const isUnsettling = unsettlingThreadKey !== null && unsettlingThreadKey === activeThreadKey;
const handleUnsettleActiveThread = useCallback(async () => {
if (!activeThreadRef) return;
const threadKey = scopedThreadKey(activeThreadRef);
setUnsettlingThreadKey(threadKey);
try {
const result = await unsettleThreadMutation({
environmentId: activeThreadRef.environmentId,
input: { threadId: activeThreadRef.threadId, reason: "user" },
});
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
toastManager.add(
stackedThreadToast({
type: "error",
title: "Failed to un-settle thread",
description: error instanceof Error ? error.message : "An error occurred.",
}),
);
}
} finally {
setUnsettlingThreadKey((current) => (current === threadKey ? null : current));
}
}, [activeThreadRef, unsettleThreadMutation]);
const unsnoozeThreadMutation = useAtomCommand(threadEnvironment.unsnooze, {
reportFailure: false,
});
const [unsnoozingThreadKey, setUnsnoozingThreadKey] = useState<string | null>(null);
const isUnsnoozing = unsnoozingThreadKey !== null && unsnoozingThreadKey === activeThreadKey;
const handleUnsnoozeActiveThread = useCallback(async () => {
if (!activeThreadRef) return;
const threadKey = scopedThreadKey(activeThreadRef);
setUnsnoozingThreadKey(threadKey);
try {
const result = await unsnoozeThreadMutation({
environmentId: activeThreadRef.environmentId,
input: { threadId: activeThreadRef.threadId, reason: "user" },
});
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
toastManager.add(
stackedThreadToast({
type: "error",
title: "Failed to wake thread",
description: error instanceof Error ? error.message : "An error occurred.",
}),
);
}
} finally {
setUnsnoozingThreadKey((current) => (current === threadKey ? null : current));
}
}, [activeThreadRef, unsnoozeThreadMutation]);
const [isRestoringThreadBranch, setIsRestoringThreadBranch] = useState(false);
const [branchRestoreConfirmOpen, setBranchRestoreConfirmOpen] = useState(false);
// Once revealed for a given mismatch, the banner stays mounted until the
// mismatch changes or resolves, so clearing the draft doesn't flicker it.
const [revealedBranchMismatchKey, setRevealedBranchMismatchKey] = useState<string | null>(null);
// Dismissal lives in a module-level set (survives remounts); this tick just
// forces a re-render so the banner leaves immediately.
const [, setBranchMismatchDismissTick] = useState(0);
const activeBranchMismatchKey = branchMismatchKey(
activeThread?.id ?? null,
localCheckoutBranchMismatch,
);
const showBranchMismatchBanner = shouldShowBranchMismatchBanner({
hasMismatch: localCheckoutBranchMismatch !== null,
isDismissed: isBranchMismatchDismissedForSession(activeBranchMismatchKey),
composerHasContent: composerHasUnsentContent,
wasShownForCurrentMismatch:
revealedBranchMismatchKey !== null && revealedBranchMismatchKey === activeBranchMismatchKey,
});
useEffect(() => {
setRevealedBranchMismatchKey((revealed) => {
if (showBranchMismatchBanner) {
return activeBranchMismatchKey;
}
// Hysteresis is scoped to an uninterrupted mismatch: reset when the
// mismatch resolves or changes so a recurrence re-gates on intent.
return revealed !== null && revealed !== activeBranchMismatchKey ? null : revealed;
});
}, [activeBranchMismatchKey, showBranchMismatchBanner]);
const handleSwitchCheckoutToThread = useCallback(async () => {
if (
!activeProjectCwd ||
!activeThread ||
!localCheckoutBranchMismatch ||
isRestoringThreadBranch
) {
return;
}
setIsRestoringThreadBranch(true);
const checkoutResult = await switchGitRef({
environmentId,
input: {
cwd: activeProjectCwd,
refName: localCheckoutBranchMismatch.threadBranch,
},
});
if (checkoutResult._tag === "Failure") {
setIsRestoringThreadBranch(false);
if (!isAtomCommandInterrupted(checkoutResult)) {
toastManager.add(
stackedThreadToast({
type: "error",
title: "Failed to switch checkout",
description: chatActionErrorMessage(squashAtomCommandFailure(checkoutResult)),
}),
);
}
return;
}
const nextBranch = checkoutResult.value.refName ?? localCheckoutBranchMismatch.threadBranch;
if (nextBranch !== activeThread.branch) {
const updateResult = await updateThreadMetadata({
environmentId,
input: { threadId: activeThread.id, branch: nextBranch, worktreePath: null },
});
if (updateResult._tag === "Failure") {
setIsRestoringThreadBranch(false);
if (!isAtomCommandInterrupted(updateResult)) {
toastManager.add(
stackedThreadToast({
type: "error",
title: "Checkout switched, but the thread could not be updated",
description: chatActionErrorMessage(squashAtomCommandFailure(updateResult)),
}),
);
}
gitStatusQuery.refresh();
return;
}
}
gitStatusQuery.refresh();
setIsRestoringThreadBranch(false);
scheduleComposerFocus();
}, [
activeProjectCwd,
activeThread,
environmentId,
gitStatusQuery,
isRestoringThreadBranch,
localCheckoutBranchMismatch,
scheduleComposerFocus,
switchGitRef,
updateThreadMetadata,
]);
// Background work (subagent fleets, workflow runs, watch loops) can outlive
// the turn; once it settles, the composer stop button is gone, so this
// banner is the only visible stop affordance. Stop routes through the
// stop-everything interrupt: it kills every live background task before
// interrupting, and works by session, so no active turn is needed.
const activeBackgroundLiveness =
!isWorking && activeThread ? (activeThreadShell?.backgroundLiveness ?? null) : null;
const [isStoppingBackgroundWork, setIsStoppingBackgroundWork] = useState(false);
useEffect(() => {
// "Stopping..." holds until the liveness clears; the interrupt command
// returning only means the request was accepted.
if (activeBackgroundLiveness === null) {
setIsStoppingBackgroundWork(false);
}
}, [activeBackgroundLiveness]);
useEffect(() => {
// Per-thread state: switching threads while A's stop is pending must not
// disable B's Stop button (review finding).
setIsStoppingBackgroundWork(false);
}, [activeThreadId]);
const handleStopBackgroundWork = useCallback(async () => {
if (!activeThread) return;
setIsStoppingBackgroundWork(true);
const result = await interruptThreadTurn({
environmentId,
input: buildThreadTurnInterruptInput(activeThread),
});
if (result._tag === "Failure") {
// Every failure clears the pending state — an interrupted command
// never reached the server, so liveness would hold "Stopping..."
// forever. Only real failures toast.
setIsStoppingBackgroundWork(false);
if (!isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
setThreadError(
activeThread.id,
error instanceof Error ? error.message : "Failed to stop background work.",
);
}
}
}, [activeThread, environmentId, interruptThreadTurn, setThreadError]);
const backgroundLivenessBannerItem = useMemo<ComposerBannerStackItem | null>(() => {
if (activeBackgroundLiveness === null || !activeThread) {
return null;
}
const working = activeBackgroundLiveness === "working";
const liveCount = agentPanelModel.liveCount;
return {
id: `background-liveness:${activeThread.id}`,
variant: "default",
priority: "activity",
icon: (
<span
className={cn("size-1.5 rounded-full bg-foreground", working && "animate-status-pulse")}
aria-hidden="true"
/>
),
title: working
? liveCount > 0
? `${liveCount} ${liveCount === 1 ? "agent" : "agents"} working`
: "Background work"
: "Monitoring",
actions: (
<Button
size="xs"
variant="ghost"
disabled={isStoppingBackgroundWork}
onClick={() => void handleStopBackgroundWork()}
>
{isStoppingBackgroundWork ? "Stopping..." : "Stop"}
</Button>
),
};
}, [
activeBackgroundLiveness,
activeThread,
agentPanelModel.liveCount,
handleStopBackgroundWork,
isStoppingBackgroundWork,
]);
// A woken thread announces itself in the open view, not just the sidebar
// pill. Dismissing marks the wake as seen (same acknowledgment as the
// pill); sending a message clears it as a side effect of the send path.
const wokeThreadBannerItem = useMemo<ComposerBannerStackItem | null>(() => {
if (!activeThreadWokeVisible) {
return null;
}
return {
id: `thread-woke:${activeThread?.id ?? "unknown"}`,
variant: "info",
icon: <AlarmClockIcon />,
title: "Thread woke from snooze",
description: "Send a message to continue",
dismissLabel: "Dismiss Woke notification",
onDismiss: acknowledgeActiveThreadWoke,
};
}, [acknowledgeActiveThreadWoke, activeThread?.id, activeThreadWokeVisible]);
const parkedThreadBannerItem = useMemo<ComposerBannerStackItem | null>(() => {
if (!activeThreadSnoozed && !activeThreadSettled) {
return null;
}
const isSnoozed = activeThreadSnoozed;
return {
id: `thread-${isSnoozed ? "snoozed" : "settled"}:${activeThread?.id ?? "unknown"}`,
variant: "info",
icon: isSnoozed ? <AlarmClockIcon /> : <CheckCircle2Icon />,
title: `This thread is ${isSnoozed ? "snoozed" : "settled"}`,
description: `Send a message to ${isSnoozed ? "wake" : "unsettle"}`,
actions: (
<Button
size="xs"
variant="ghost"
disabled={isSnoozed ? isUnsnoozing : isUnsettling}
onClick={() =>
void (isSnoozed ? handleUnsnoozeActiveThread() : handleUnsettleActiveThread())
}
>
{isSnoozed
? isUnsnoozing
? "Waking..."
: "Wake now"
: isUnsettling
? "Un-settling..."
: "Un-settle"}
</Button>
),
};
}, [
activeThread?.id,
activeThreadSettled,
activeThreadSnoozed,
handleUnsnoozeActiveThread,
handleUnsettleActiveThread,
isUnsnoozing,
isUnsettling,
]);
// Session-scoped dismissals, one key per (thread, snapshot). A set rather
// than a single slot so dismissing the banner on one thread does not
// resurface it on another thread dismissed earlier.
const [dismissedResumeCompactionKeys, setDismissedResumeCompactionKeys] = useState<
ReadonlySet<string>
>(new Set());
const resumeCompactionKey =
activeThread && activeContextWindow
? `${activeThread.id}:${activeContextWindow.updatedAt}`
: null;
const activeThreadHasCompactableConversation =
activeThread?.messages.some(
(message) => message.role === "user" && !isCompactCommandMessage(message),
) ?? false;
const compactThreadUnavailable =
!activeThread ||
!activeThreadHasCompactableConversation ||
!activeProject ||
!isServerThread ||
!manualCompactionProviderAvailable ||
isWorking ||
threadDetailLoading ||
isPreparingWorktree ||
activeEnvironmentUnavailable ||
feedbackUploading ||
pendingApprovals.length > 0 ||
pendingUserInputs.length > 0 ||
showPlanFollowUpPrompt;
const compactDisabled = compactThreadUnavailable;
const compactDisabledReason = compactDisabled
? !activeProject
? "Choose a project before compacting"
: !manualCompactionProviderAvailable
? "Compaction is unavailable for this provider"
: "Compacting is unavailable right now"
: null;
const resumeCompactionBannerItem = useMemo<ComposerBannerStackItem | null>(() => {
if (
!activeThread ||
!activeContextWindow ||
resumeCompactionKey === null ||
dismissedResumeCompactionKeys.has(resumeCompactionKey) ||
resumeCompactionPermanentlyDismissed ||
nativeResumeCompactionDismissed ||
pendingUserInputs.length > 0 ||
phase === "running" ||
!shouldOfferResumeCompaction({
provider: selectedProvider,
usedTokens: activeContextWindow.usedTokens,
updatedAt: activeContextWindow.updatedAt,
now: `${nowMinute}:00.000Z`,
})
) {
return null;
}
const dismiss = () =>
setDismissedResumeCompactionKeys((keys) => new Set(keys).add(resumeCompactionKey));
const compactAction = (
<Button
size="xs"
variant="ghost"
disabled={compactDisabled}
onClick={() => {
if (compactDisabled) return;
composerRef.current?.compactContext();
}}
>
Compact
</Button>
);
return {
id: `resume-compaction:${resumeCompactionKey}`,
variant: "info",
icon: <Minimize2Icon />,
title: "Resume with less context",
description: `${formatContextWindowTokens(activeContextWindow.usedTokens)} tokens from earlier`,
actions: compactDisabledReason ? (
<Tooltip>
<TooltipTrigger render={<span className="inline-flex">{compactAction}</span>} />
<TooltipPopup side="top">{compactDisabledReason}</TooltipPopup>
</Tooltip>
) : (
compactAction
),
dismissLabel: "Keep full history",
onDismiss: dismiss,
};
}, [
activeContextWindow,
activeThread,
compactDisabled,
compactDisabledReason,
composerRef,
dismissedResumeCompactionKeys,
nativeResumeCompactionDismissed,
nowMinute,
pendingUserInputs.length,
phase,
resumeCompactionKey,
resumeCompactionPermanentlyDismissed,
selectedProvider,
]);
const handleRestoreThreadBranch = useCallback(() => {
if (gitStatusQuery.data?.hasWorkingTreeChanges) {
setBranchRestoreConfirmOpen(true);
return;
}
void handleSwitchCheckoutToThread();
}, [gitStatusQuery.data?.hasWorkingTreeChanges, handleSwitchCheckoutToThread]);
const feedbackBannerItems = useMemo(
() =>
feedbackSubmissions.flatMap((submission) => {
const item = feedbackBannerItem(submission, () => {
setFeedbackSubmissionsByThreadKey((current) => ({
...current,
[routeThreadKey]: (current[routeThreadKey] ?? []).filter(
(entry) => entry.id !== submission.id,
),
}));
});
return item ? [item] : [];
}),
[feedbackSubmissions, routeThreadKey],
);
const composerBannerItems = useMemo<ComposerBannerStackItem[]>(() => {
const backgroundLivenessItems =
backgroundLivenessBannerItem === null ? [] : [backgroundLivenessBannerItem];
const resumeCompactionItems =
resumeCompactionBannerItem === null ? [] : [resumeCompactionBannerItem];
const wokeThreadItems = wokeThreadBannerItem === null ? [] : [wokeThreadBannerItem];
const parkedThreadItems = parkedThreadBannerItem === null ? [] : [parkedThreadBannerItem];
// The user asked for this one, so it leads the notice tier instead of trailing it.
const usageLimitsItems = usageLimitsBanner === null ? [] : [usageLimitsBanner];
if (!localCheckoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
return [
...feedbackBannerItems,
...usageLimitsItems,
...systemComposerBannerItems,
...backgroundLivenessItems,
...resumeCompactionItems,
...wokeThreadItems,
...parkedThreadItems,
];
}
return [
...feedbackBannerItems,
...usageLimitsItems,
...systemComposerBannerItems,
...backgroundLivenessItems,
...resumeCompactionItems,
...wokeThreadItems,
{
id: `branch-mismatch:${activeBranchMismatchKey}`,
variant: "info",
icon: <GitBranchIcon />,
title: (
<span className="flex min-w-0 items-baseline gap-1.5">
<span className="shrink-0 font-normal text-muted-foreground">Branch changed — was</span>
<Tooltip>
<TooltipTrigger
render={
<code className="min-w-0 truncate font-medium text-foreground">
{localCheckoutBranchMismatch.threadBranch}
</code>
}
/>
<TooltipPopup side="top" className="max-w-80">
This thread last ran on {localCheckoutBranchMismatch.threadBranch}. Sending will
continue on {localCheckoutBranchMismatch.currentBranch}.
</TooltipPopup>
</Tooltip>
</span>
),
actions: (
<Button
size="xs"
variant="ghost"
disabled={isRestoringThreadBranch}
onClick={handleRestoreThreadBranch}
>
{isRestoringThreadBranch ? "Restoring..." : "Restore branch"}
</Button>
),
dismissLabel: "Dismiss branch change notice",
onDismiss: () => {
dismissBranchMismatchForSession(activeBranchMismatchKey);
setBranchMismatchDismissTick((tick) => tick + 1);
},
},
...parkedThreadItems,
];
}, [
activeBranchMismatchKey,
backgroundLivenessBannerItem,
feedbackBannerItems,
handleRestoreThreadBranch,
isRestoringThreadBranch,
localCheckoutBranchMismatch,
parkedThreadBannerItem,
resumeCompactionBannerItem,
showBranchMismatchBanner,
systemComposerBannerItems,
usageLimitsBanner,
wokeThreadBannerItem,
]);
useEffect(() => {
setPendingServerThreadEnvMode(null);
setPendingServerThreadBranch(undefined);
}, [activeThread?.id]);
useEffect(() => {
if (canOverrideServerThreadEnvMode) {
return;
}
setPendingServerThreadEnvMode(null);
setPendingServerThreadBranch(undefined);
}, [canOverrideServerThreadEnvMode]);
useEffect(() => {
if (!activeThreadId) {
setTerminalUiLaunchContext(null);
return;
}
setTerminalUiLaunchContext((current) => {
if (!current) return current;
if (current.threadId === activeThreadId) return current;
return null;
});
}, [activeThreadId]);
useEffect(() => {
if (!activeThreadId || !activeProjectCwd) {
return;
}
setTerminalUiLaunchContext((current) => {
if (!current || current.threadId !== activeThreadId) {
return current;
}
const settledCwd = projectScriptCwd({
project: { cwd: activeProjectCwd },
worktreePath: activeThreadWorktreePath,
});
if (
settledCwd === current.cwd &&
(activeThreadWorktreePath ?? null) === current.worktreePath
) {
return null;
}
return current;
});
}, [activeProjectCwd, activeThreadId, activeThreadWorktreePath]);
useEffect(() => {
if (terminalUiState.terminalOpen) {
return;
}
setTerminalUiLaunchContext((current) =>
current?.threadId === activeThreadId ? null : current,
);
}, [activeThreadId, terminalUiState.terminalOpen]);
useEffect(() => {
if (!activeThreadKey) return;
const previous = terminalUiOpenByThreadRef.current[activeThreadKey] ?? false;
const current = Boolean(terminalUiState.terminalOpen);
if (!previous && current) {
terminalUiOpenByThreadRef.current[activeThreadKey] = current;
setTerminalFocusRequestId((value) => value + 1);
return;
} else if (previous && !current) {
terminalUiOpenByThreadRef.current[activeThreadKey] = current;
const frame = window.requestAnimationFrame(() => {
focusComposer();
});
return () => {
window.cancelAnimationFrame(frame);
};
}
terminalUiOpenByThreadRef.current[activeThreadKey] = current;
}, [activeThreadKey, focusComposer, terminalUiState.terminalOpen]);
useEffect(() => {
const handler = (event: globalThis.KeyboardEvent) => {
if (preventRepeatedTerminalCloseShortcut(event, keybindings)) {
event.stopPropagation();
return;
}
// While a close confirmation is open, terminal focus has moved to the
// dialog, so a deliberate second close shortcut would otherwise fall
// through to the native window/tab close accelerator.
if (isTerminalCloseConfirmPending() && preventTerminalCloseShortcut(event, keybindings)) {
event.stopPropagation();
return;
}
if (!activeThreadId || isCommandPaletteOpen()) {
return;
}
const terminalFocusOwner = getTerminalFocusOwner();
if (event.defaultPrevented && terminalFocusOwner === null) {
return;
}
const shortcutContext = {
terminalFocus: terminalFocusOwner !== null,
terminalOpen: Boolean(terminalUiState.terminalOpen),
previewFocus: isPreviewFocused(),
previewOpen: previewPanelOpen,
modelPickerOpen: composerRef.current?.isModelPickerOpen() ?? false,
};
if (
!shortcutContext.terminalFocus &&
!shortcutContext.modelPickerOpen &&
shouldTypeToFocusComposer(event)
) {
if (composerRef.current?.insertTextAtEnd(event.key)) {
event.preventDefault();
event.stopPropagation();
return;
}
}
const command = resolveShortcutCommand(event, keybindings, {
context: shortcutContext,
});
if (!command) return;
if (command === "thread.copyReference") {
event.preventDefault();
event.stopPropagation();
if (!event.repeat) copyActiveThreadReference();
return;
}
if (command === "thread.settle") {
event.preventDefault();
event.stopPropagation();
if (!isServerThread || !activeThreadRef || !supportsSettlement) return;
if (activeThreadSettled) {
void handleUnsettleActiveThread();
return;
}
void settleThread(activeThreadRef).then((result) => {
if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
const error = squashAtomCommandFailure(result);
toastManager.add(
stackedThreadToast({
type: "error",
title: "Failed to settle thread",
description: error instanceof Error ? error.message : "An error occurred.",
}),
);
});
return;
}
if (command === "thread.pin") {
event.preventDefault();
event.stopPropagation();
if (!isServerThread || !activeThreadRef || !supportsPinning) return;
const pinned = activeThreadPinned;
void (pinned ? confirmAndUnpinThread(activeThreadRef) : pinThread(activeThreadRef)).then(
(result) => {
if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
const error = squashAtomCommandFailure(result);
toastManager.add(
stackedThreadToast({
type: "error",
title: pinned ? "Failed to unpin thread" : "Failed to pin thread",
description: error instanceof Error ? error.message : "An error occurred.",
}),
);
},
);
return;
}
if (command === "terminal.toggle") {
event.preventDefault();
event.stopPropagation();
toggleTerminalVisibility();
return;
}
if (command === "rightPanel.toggle") {
event.preventDefault();
event.stopPropagation();
toggleRightPanel();
return;
}
if (command === "rightPanel.toggleMaximized") {
event.preventDefault();
event.stopPropagation();
toggleRightPanelMaximized();
return;
}
if (command === "rightPanel.close") {
// Nothing open: leave the event alone so the shortcut keeps its
// native meaning (close window on desktop, close tab in a browser).
if (!activeRightPanelSurface) return;
event.preventDefault();
event.stopPropagation();
if (!event.repeat) closeRightPanelSurface(activeRightPanelSurface);
return;
}
if (command === "terminal.split") {
event.preventDefault();
event.stopPropagation();
if (terminalFocusOwner === "right-panel") {
splitPanelTerminal();
return;
}
if (!terminalUiState.terminalOpen) {
setTerminalOpen(true);
}
splitTerminal();
return;
}
if (command === "terminal.splitVertical") {
event.preventDefault();
event.stopPropagation();
if (terminalFocusOwner === "right-panel") {
splitPanelTerminal("vertical");
return;
}
if (!terminalUiState.terminalOpen) {
setTerminalOpen(true);
}
splitTerminal("vertical");
return;
}
if (command === "terminal.close") {
event.preventDefault();
event.stopPropagation();
if (terminalFocusOwner === "right-panel" && activeRightPanelSurface?.kind === "terminal") {
requestClosePanelTerminal(activeRightPanelSurface.activeTerminalId);
return;
}
if (!terminalUiState.terminalOpen) return;
requestCloseTerminal(terminalUiState.activeTerminalId);
return;
}
if (command === "terminal.new") {
event.preventDefault();
event.stopPropagation();
if (terminalFocusOwner === "right-panel") {
addTerminalSurface();
return;
}
if (!terminalUiState.terminalOpen) {
setTerminalOpen(true);
}
createNewTerminal();
return;
}
if (command === "diff.toggle") {
event.preventDefault();
event.stopPropagation();
onToggleDiff();
return;
}
if (command === "modelPicker.toggle") {
event.preventDefault();
event.stopPropagation();
composerRef.current?.toggleModelPicker();
return;
}
if (command === "thread.stop") {
// An unavailable command should not shadow contextual shortcuts such as Escape to close a dialog.
if (!canInterruptRunningThread) return;
event.preventDefault();
event.stopPropagation();
if (event.repeat) return;
void onInterrupt();
return;
}
const scriptId = projectScriptIdFromCommand(command);
if (!scriptId || !activeProject) return;
const script = activeProjectScripts.find((entry) => entry.id === scriptId);
if (!script) return;
event.preventDefault();
event.stopPropagation();
void runProjectScript(script);
};
window.addEventListener("keydown", handler, true);
return () => window.removeEventListener("keydown", handler, true);
}, [
activeProject,
activeRightPanelSurface,
activeProjectScripts,
addTerminalSurface,
activeThreadRef,
activeThreadPinned,
activeThreadSettled,
canInterruptRunningThread,
terminalUiState.terminalOpen,
terminalUiState.activeTerminalId,
activeThreadId,
closeRightPanelSurface,
requestCloseTerminal,
requestClosePanelTerminal,
createNewTerminal,
setTerminalOpen,
runProjectScript,
splitTerminal,
splitPanelTerminal,
keybindings,
handleUnsettleActiveThread,
isServerThread,
onInterrupt,
onToggleDiff,
pinThread,
settleThread,
supportsPinning,
supportsSettlement,
confirmAndUnpinThread,
copyActiveThreadReference,
previewPanelOpen,
toggleRightPanel,
toggleRightPanelMaximized,
toggleTerminalVisibility,
composerRef,
]);
// Paste-to-focus: the resting composer blurs on a click into the timeline,
// so a paste that follows has no editable target and would be dropped.
// Route it to the composer like a typed key, which also expands it.
useEffect(() => {
const handler = (event: ClipboardEvent) => {
if (!activeThreadId || isCommandPaletteOpen()) return;
if (getTerminalFocusOwner() !== null) return;
if (composerRef.current?.isModelPickerOpen()) return;
const text = pasteTextToFocusComposer(event);
if (text === null) return;
if (composerRef.current?.insertTextAtEnd(text)) {
event.preventDefault();
event.stopPropagation();
}
};
window.addEventListener("paste", handler, true);
return () => window.removeEventListener("paste", handler, true);
}, [activeThreadId, composerRef]);
const [pendingRevert, setPendingRevert] = useState<{
turnCount: number;
messageId: MessageId;
routeThreadKey: string;
} | null>(null);
if (pendingRevert && pendingRevert.routeThreadKey !== routeThreadKey) {
setPendingRevert(null);
}
const onRevertToTurnCount = useCallback(
async (turnCount: number, messageId: MessageId, restoreFiles?: boolean) => {
const localApi = readLocalApi();
if (!localApi || !activeThread || isRevertingCheckpoint) return;
const message = activeThread.messages.find((message) => message.id === messageId);
if (!message || message.role !== "user") return;
if (!supportsConversationRollback) {
setThreadError(
activeThread.id,
"This provider does not support reverting conversation history. Start a new thread instead.",
);
return;
}
if (activeEnvironmentUnavailable && activeEnvironmentUnavailableLabel) {
setThreadError(
activeThread.id,
`Reconnect ${activeEnvironmentUnavailableLabel} before reverting checkpoints.`,
);
return;
}
if (phase === "running" || isSendBusy || isConnecting) {
setThreadError(activeThread.id, "Interrupt the current turn before reverting checkpoints.");
return;
}
if (restoreFiles === undefined) {
setPendingRevert({ turnCount, messageId, routeThreadKey });
return;
}
useComposerDraftStore.setState((store) => ({
rewindingThreadKeys: new Set(store.rewindingThreadKeys).add(routeThreadKey),
}));
setThreadError(activeThread.id, null);
try {
if (composerRef.current?.hasPendingAttachments()) {
throw new Error("Wait for attachments to finish preparing before rewinding.");
}
const connection = readPreparedConnection(environmentId);
if (!connection) throw new Error("The environment is not connected.");
const files = await prepareRevertedMessageAttachments({
message,
environmentId,
httpBaseUrl: connection.httpBaseUrl,
createAssetUrl: createAttachmentAssetUrl,
});
const store = useComposerDraftStore.getState();
const draft = store.getComposerDraft(composerDraftTarget);
if (
(draft?.images.length ?? 0) + (draft?.files.length ?? 0) + files.length >
PROVIDER_SEND_TURN_MAX_ATTACHMENTS
) {
throw new Error(
"Make room for this message's attachments in the composer before rewinding.",
);
}
await waitForRevertedMessage(routeThreadRef, messageId, turnCount, async () => {
const result = await revertThreadCheckpoint({
environmentId,
input: { threadId: activeThread.id, turnCount, restoreFiles },
});
if (result._tag === "Failure") throw squashAtomCommandFailure(result);
});
const currentPrompt = store.getComposerDraft(composerDraftTarget)?.prompt ?? "";
const restoredPrompt = recallableComposerPrompt(message.text);
const nextPrompt =
restoredPrompt.length === 0
? currentPrompt
: currentPrompt.length > 0
? `${currentPrompt}\n\n${restoredPrompt}`
: restoredPrompt;
store.setPrompt(composerDraftTarget, nextPrompt);
const images: ComposerImageAttachment[] = [];
const restoredFiles: ComposerFileAttachment[] = [];
files.forEach((file, index) => {
const attachment = {
id: randomUUID(),
name: file.name,
mimeType: file.type,
sizeBytes: file.size,
file,
};
if (message.attachments?.[index]?.type === "image") {
images.push({ ...attachment, type: "image", previewUrl: URL.createObjectURL(file) });
} else {
restoredFiles.push({ ...attachment, type: "file" });
}
});
store.addImages(composerDraftTarget, images, { allowDuplicates: true });
store.addFiles(composerDraftTarget, restoredFiles, { allowDuplicates: true });
if (currentRouteThreadKeyRef.current === routeThreadKey) {
promptRef.current = nextPrompt;
composerRef.current?.resetCursorState({ prompt: nextPrompt, cursor: nextPrompt.length });
requestAnimationFrame(() => {
if (currentRouteThreadKeyRef.current === routeThreadKey)
composerRef.current?.focusAtEnd();
});
}
} catch (error) {
setThreadError(
activeThread.id,
error instanceof Error ? error.message : "Failed to revert thread state.",
);
} finally {
useComposerDraftStore.setState((store) => {
const remaining = new Set(store.rewindingThreadKeys);
remaining.delete(routeThreadKey);
return { rewindingThreadKeys: remaining };
});
}
},
[
activeThread,
activeEnvironmentUnavailable,
activeEnvironmentUnavailableLabel,
composerDraftTarget,
composerRef,
createAttachmentAssetUrl,
environmentId,
isConnecting,
isRevertingCheckpoint,
isSendBusy,
phase,
revertThreadCheckpoint,
routeThreadKey,
routeThreadRef,
setThreadError,
supportsConversationRollback,
],
);
const onCompactContext = async () => {
if (compactDisabled || !activeThread || !clientSettingsHydrated || sendInFlightRef.current) {
return;
}
const context = composerRef.current?.getSendContext();
if (!context?.providerAvailable) return;
// Compaction is a standalone command; the draft and its attachments stay local.
const threadId = activeThread.id;
const messageId = newMessageId();
const createdAt = new Date().toISOString();
sendInFlightRef.current = true;
beginLocalDispatch();
setThreadError(threadId, null);
setOptimisticUserMessages((messages) => [
...messages,
{
id: messageId,
role: "user",
text: "/compact",
turnId: null,
createdAt,
updatedAt: createdAt,
streaming: false,
},
]);
scrollToEnd();
try {
const settingsResult = await persistThreadSettingsForNextTurn({
threadId,
createdAt,
modelSelection: context.selectedModelSelection,
...(localCheckoutBranchMismatch
? { branch: localCheckoutBranchMismatch.currentBranch }
: {}),
runtimeMode,
interactionMode: context.interactionMode,
});
const result =
settingsResult._tag === "Failure"
? settingsResult
: await startThreadTurn({
environmentId,
input: {
threadId,
message: { messageId, role: "user", text: "/compact", attachments: [] },
modelSelection: context.selectedModelSelection,
runtimeMode,
interactionMode: context.interactionMode,
createdAt,
},
});
if (result._tag === "Failure") {
setOptimisticUserMessages((messages) =>
messages.filter((message) => message.id !== messageId),
);
resetLocalDispatch();
if (!isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
setThreadError(
threadId,
error instanceof Error ? error.message : "Failed to compact context.",
);
}
} else {
clearUsageLimitsFor(routeThreadKey);
}
} finally {
sendInFlightRef.current = false;
}
};
const onSend = async (
e?: { preventDefault: () => void },
submissionIntent: ComposerSubmissionIntent = "foreground",
directAnnotation?: {
annotation: PreviewAnnotationPayload;
image: ComposerImageAttachment | null;
},
) => {
e?.preventDefault();
// Typed out in full rather than picked from the menu. Attachments or contexts
// mean the user is sending a prompt, so those go through as usual.
if (
usageLimitsOffered &&
usageLimitsKey !== null &&
!directAnnotation &&
!composerHasNonPromptContent &&
isUsageLimitsCommand(promptRef.current)
) {
if (openUsageLimits()) {
promptRef.current = "";
setComposerDraftPrompt(composerDraftTarget, "");
composerRef.current?.resetCursorState();
}
return;
}
const notifyDirectAnnotationAttached = () => {
if (!directAnnotation) return;
toastManager.add(
stackedThreadToast({
type: "info",
title: "Annotation attached to draft",
description: "Sending is unavailable right now. Finish the current action, then send.",
}),
);
};
if (
!activeThread ||
isSendBusy ||
isConnecting ||
isRevertingCheckpoint ||
!clientSettingsHydrated ||
threadDetailLoading ||
sendInFlightRef.current ||
feedbackUploadsInFlightRef.current.has(routeThreadKey)
) {
notifyDirectAnnotationAttached();
return;
}
if (needsLoadBalancing) {
toastManager.add({
type: "warning",
title: loadBalancing.pending
? "Checking machine resources"
: "Choose a machine to continue",
description: loadBalancing.pending
? "Resource checks are still running. You can choose a machine in the composer."
: "No eligible machine has available resources. Choose a machine in the composer to override.",
});
return;
}
if (activeEnvironmentUnavailable) {
const toastSlot = environmentUnavailableSendToastSlotRef.current;
environmentUnavailableSendToastSlotRef.current =
(toastSlot + 1) % ENVIRONMENT_UNAVAILABLE_SEND_TOAST_TRAIL_SIZE;
toastManager.add({
...stackedThreadToast({
type: "warning",
title: "Not connected: message not sent",
description: "Reconnecting to the environment. Try again once it is connected.",
}),
id: `chat-send-environment-unavailable:${toastSlot}`,
});
return;
}
if (activePendingProgress) {
if (directAnnotation) {
notifyDirectAnnotationAttached();
return;
}
onAdvanceActivePendingUserInput();
return;
}
const sendCtx = composerRef.current?.getSendContext();
if (!sendCtx?.providerAvailable) {
notifyDirectAnnotationAttached();
return;
}
const {
images: sendContextImages,
files: composerFiles,
terminalContexts: composerTerminalContexts,
elementContexts: composerElementContexts,
previewAnnotations: sendContextPreviewAnnotations,
reviewComments: composerReviewComments,
selectedProvider: ctxSelectedProvider,
selectedModel: ctxSelectedModel,
selectedProviderModels: ctxSelectedProviderModels,
selectedPromptEffort: ctxSelectedPromptEffort,
selectedModelSelection: ctxSelectedModelSelection,
interactionMode: sendInteractionMode,
interactionModeEnabled: sendInteractionModeEnabled,
} = sendCtx;
const annotationImageAlreadyAttached =
directAnnotation?.image !== undefined &&
sendContextImages.some((image) => image.id === directAnnotation.image?.id);
// A full composer (e.g. 8 files) cannot take the annotation screenshot;
// over the cap the server rejects the whole turn.
const annotationImageAppended =
directAnnotation?.image !== undefined &&
!annotationImageAlreadyAttached &&
sendContextImages.length + composerFiles.length < PROVIDER_SEND_TURN_MAX_ATTACHMENTS;
const composerImages =
directAnnotation?.image && annotationImageAppended
? [...sendContextImages, directAnnotation.image]
: sendContextImages;
const composerPreviewAnnotations =
directAnnotation &&
!sendContextPreviewAnnotations.some(
(annotation) => annotation.id === directAnnotation.annotation.id,
)
? [
...sendContextPreviewAnnotations,
{
...directAnnotation.annotation,
// Claim an attached crop only when the screenshot really rides
// along; a cap-dropped image must not produce a lying prompt.
screenshot:
directAnnotation.annotation.screenshot &&
(annotationImageAppended || annotationImageAlreadyAttached)
? { ...directAnnotation.annotation.screenshot, dataUrl: "" }
: null,
},
]
: sendContextPreviewAnnotations;
const promptForSend = promptRef.current;
const {
trimmedPrompt: trimmed,
sendableTerminalContexts: sendableComposerTerminalContexts,
expiredTerminalContextCount,
hasSendableContent,
} = deriveComposerSendState({
prompt: promptForSend,
imageCount: composerImages.length + composerFiles.length,
terminalContexts: composerTerminalContexts,
elementContextCount:
composerElementContexts.length +
composerPreviewAnnotations.length +
composerReviewComments.length,
});
const feedbackCommand =
ctxSelectedProvider === "codex" &&
composerImages.length === 0 &&
composerFiles.length === 0 &&
sendableComposerTerminalContexts.length === 0 &&
composerElementContexts.length === 0 &&
composerPreviewAnnotations.length === 0 &&
composerReviewComments.length === 0
? parseCodexFeedbackCommand(trimmed)
: null;
if (feedbackCommand) {
if (!isServerThread || activeThread.session === null) {
toastManager.add(
stackedThreadToast({
type: "warning",
title: "Start a Codex thread first",
description: "Send a message before you submit feedback.",
}),
);
return;
}
feedbackUploadsInFlightRef.current.add(routeThreadKey);
await submitCodexFeedback({
submission: {
id: newMessageId(),
command: trimmed,
createdAt: new Date().toISOString(),
},
clearDraft: () => {
promptRef.current = "";
clearComposerDraftContent(composerDraftTarget);
composerRef.current?.resetCursorState();
},
onUpdate: (submission) => {
setFeedbackSubmissionsByThreadKey((current) => {
const existing = current[routeThreadKey] ?? [];
const found = existing.some((entry) => entry.id === submission.id);
return {
...current,
[routeThreadKey]: found
? existing.map((entry) => (entry.id === submission.id ? submission : entry))
: [...existing, submission],
};
});
},
upload: () =>
uploadThreadFeedback({
environmentId,
input: {
threadId: activeThread.id,
...feedbackCommand,
},
}),
}).finally(() => {
feedbackUploadsInFlightRef.current.delete(routeThreadKey);
});
return;
}
if (
!directAnnotation &&
sendInteractionModeEnabled &&
showPlanFollowUpPrompt &&
activeProposedPlan &&
composerImages.length === 0 &&
composerFiles.length === 0
) {
const followUp = resolvePlanFollowUpSubmission({
draftText: trimmed,
planMarkdown: activeProposedPlan.planMarkdown,
});
const outgoingFollowUpText = formatOutgoingPrompt({
provider: ctxSelectedProvider,
model: ctxSelectedModel,
models: ctxSelectedProviderModels,
effort: ctxSelectedPromptEffort,
text: followUp.text.trim(),
});
if (composerRef.current?.validateProviderInput(outgoingFollowUpText) === false) {
return;
}
promptRef.current = "";
clearComposerDraftContent(composerDraftTarget);
composerRef.current?.resetCursorState();
await onSubmitPlanFollowUp({
text: followUp.text,
interactionMode: followUp.interactionMode,
});
return;
}
// Providers without the legacy toggle receive their native commands unchanged.
const standaloneSlashCommand =
sendInteractionModeEnabled &&
composerImages.length === 0 &&
composerFiles.length === 0 &&
sendableComposerTerminalContexts.length === 0 &&
composerElementContexts.length === 0 &&
composerPreviewAnnotations.length === 0 &&
composerReviewComments.length === 0
? parseStandaloneComposerSlashCommand(trimmed)
: null;
if (standaloneSlashCommand) {
handleInteractionModeChange(standaloneSlashCommand);
promptRef.current = "";
clearComposerDraftContent(composerDraftTarget);
composerRef.current?.resetCursorState();
return;
}
if (!hasSendableContent) {
if (expiredTerminalContextCount > 0) {
const toastCopy = buildExpiredTerminalContextToastCopy(
expiredTerminalContextCount,
"empty",
);
toastManager.add(
stackedThreadToast({
type: "warning",
title: toastCopy.title,
description: toastCopy.description,
}),
);
}
return;
}
if (!activeProject) {
toastManager.add(
stackedThreadToast({
type: "warning",
title: "Choose a project first",
description: "This draft no longer points to an available project.",
}),
);
return;
}
const threadIdForSend = activeThread.id;
const isFirstMessage = !isServerThread || activeThread.messages.length === 0;
const baseBranchForWorktree =
isFirstMessage && sendEnvMode === "worktree" && !activeThread.worktreePath
? activeThreadBranch
: null;
// In worktree mode, require an explicit base branch so we don't silently
// fall back to local execution when branch selection is missing.
const shouldCreateWorktree =
isFirstMessage && sendEnvMode === "worktree" && !activeThread.worktreePath;
if (shouldCreateWorktree && !activeThreadBranch) {
setThreadError(threadIdForSend, "Select a base branch before sending in New worktree mode.");
return;
}
const composerImagesSnapshot = [...composerImages];
const composerFilesSnapshot = [...composerFiles];
const composerAttachmentsSnapshot = [...composerImagesSnapshot, ...composerFilesSnapshot];
const composerTerminalContextsSnapshot = [...sendableComposerTerminalContexts];
const composerElementContextsSnapshot = [...composerElementContexts];
const composerPreviewAnnotationsSnapshot = [...composerPreviewAnnotations];
const composerReviewCommentsSnapshot: ReviewCommentContext[] = [...composerReviewComments];
const messageTextWithContexts = appendElementContextsToPrompt(
appendTerminalContextsToPrompt(promptForSend, composerTerminalContextsSnapshot),
composerElementContextsSnapshot,
);
const messageTextWithPreviewAnnotations = composerPreviewAnnotationsSnapshot.reduce(
(text, annotation) => appendPreviewAnnotationPrompt(text, annotation),
messageTextWithContexts,
);
const messageTextForSend = appendReviewCommentsToPrompt(
messageTextWithPreviewAnnotations,
composerReviewCommentsSnapshot,
);
const outgoingMessageText = formatOutgoingPrompt({
provider: ctxSelectedProvider,
model: ctxSelectedModel,
models: ctxSelectedProviderModels,
effort: ctxSelectedPromptEffort,
text: messageTextForSend || ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
});
if (composerRef.current?.validateProviderInput(outgoingMessageText) === false) {
return;
}
const readLiveAttachmentCapabilities = () => {
const config = appAtomRegistry.get(environmentServerConfigsAtom).get(environmentId) ?? null;
const liveSupportsAttachmentUploads =
config?.environment.capabilities.attachmentUploads === true;
return {
supportsAttachmentUploads: liveSupportsAttachmentUploads,
fileBlockReason: fileAttachmentCapabilityBlockReason({
files: composerFilesSnapshot,
attachmentUploadsCapabilityKnown: config !== null,
supportsAttachmentUploads: liveSupportsAttachmentUploads,
maxFileAttachmentBytes:
config?.environment.capabilities.fileAttachments?.maxUploadBytes ?? null,
}),
};
};
sendInFlightRef.current = true;
const attachmentCapabilitiesBeforeUpload = readLiveAttachmentCapabilities();
if (attachmentCapabilitiesBeforeUpload.fileBlockReason !== null) {
sendInFlightRef.current = false;
setThreadError(threadIdForSend, attachmentCapabilitiesBeforeUpload.fileBlockReason);
return;
}
const turnUsesAttachmentUploads =
composerFilesSnapshot.length > 0
? attachmentCapabilitiesBeforeUpload.supportsAttachmentUploads
: supportsAttachmentUploads;
if (turnUsesAttachmentUploads && composerAttachmentsSnapshot.length > 0) {
for (const attachment of composerAttachmentsSnapshot) {
startAttachmentUpload({
environmentId,
image: attachment,
draftTarget: composerDraftTarget,
});
}
await awaitAttachmentUploads(composerAttachmentsSnapshot.map((attachment) => attachment.id));
const attachmentCapabilitiesAfterUpload = readLiveAttachmentCapabilities();
if (attachmentCapabilitiesAfterUpload.fileBlockReason !== null) {
sendInFlightRef.current = false;
setThreadError(threadIdForSend, attachmentCapabilitiesAfterUpload.fileBlockReason);
return;
}
if (getUploadedAttachments({ environmentId, images: composerAttachmentsSnapshot }) === null) {
sendInFlightRef.current = false;
setThreadError(threadIdForSend, "Retry or remove failed uploads before sending.");
return;
}
}
const resolvedSubmissionIntent =
submissionIntent === "background" && isLocalDraftThread ? "background" : "foreground";
if (
shouldDockDraftHeroForSubmission({
isDraftHeroState,
activeThreadKey,
submissionIntent: resolvedSubmissionIntent,
}) &&
activeThreadKey
) {
let resolveDockStarted: (() => void) | undefined;
const dockStarted = new Promise<void>((resolve) => {
resolveDockStarted = resolve;
});
const dockTransition = runMobileComposerTransition(() => {
flushSync(() => {
captureDraftHeroComposerRect();
setDockedDraftHeroThreadKey(activeThreadKey);
});
resolveDockStarted?.();
});
void dockTransition.catch(() => resolveDockStarted?.());
await dockStarted;
}
const attachmentCapabilitiesBeforeDispatch = readLiveAttachmentCapabilities();
if (attachmentCapabilitiesBeforeDispatch.fileBlockReason !== null) {
sendInFlightRef.current = false;
setThreadError(threadIdForSend, attachmentCapabilitiesBeforeDispatch.fileBlockReason);
setDockedDraftHeroThreadKey((currentThreadKey) =>
currentThreadKey === activeThreadKey ? null : currentThreadKey,
);
return;
}
beginLocalDispatch({
preparingWorktree: Boolean(baseBranchForWorktree),
submissionIntent: resolvedSubmissionIntent,
});
const messageIdForSend = newMessageId();
const messageCreatedAt = new Date().toISOString();
const turnAttachmentsPromise = Promise.all(
composerAttachmentsSnapshot.map(async (attachment) => {
if (turnUsesAttachmentUploads) {
const uploaded = getUploadedAttachments({ environmentId, images: [attachment] })?.[0];
if (!uploaded) {
throw new Error(`Attachment '${attachment.name}' did not finish uploading.`);
}
return uploaded;
}
if (attachment.type !== "image") {
throw new Error("This server does not support file attachments.");
}
return {
type: "image" as const,
name: attachment.name,
mimeType: attachment.mimeType,
sizeBytes: attachment.sizeBytes,
dataUrl: await readFileAsDataUrl(attachment.file),
...(attachment.source ? { source: attachment.source } : {}),
};
}),
);
const optimisticAttachments = composerAttachmentsSnapshot.map((attachment) =>
attachment.type === "image"
? {
type: "image" as const,
id: attachment.id,
name: attachment.name,
mimeType: attachment.mimeType,
sizeBytes: attachment.sizeBytes,
previewUrl: attachment.previewUrl,
...(attachment.source ? { source: attachment.source } : {}),
}
: {
type: "file" as const,
id: attachment.id,
name: attachment.name,
mimeType: attachment.mimeType,
sizeBytes: attachment.sizeBytes,
downloadable: false,
},
);
const shouldAnchorFirstMessage =
activeThread.latestTurn === null &&
!timelineMessages.some((message) => message.role === "user");
if (shouldAnchorFirstMessage) {
isAtEndRef.current = true;
timelineScrollModeRef.current = "anchoring-new-turn";
liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current;
setTimelineLiveFollowEnabled(true);
pendingTimelineAnchorRef.current = messageIdForSend;
activeTimelineAnchorIndexRef.current = null;
showScrollDebouncer.current.cancel();
setShowScrollToBottom(false);
setTimelineAnchor({
threadKey: scopedThreadKey(scopeThreadRef(activeThread.environmentId, threadIdForSend)),
messageId: messageIdForSend,
});
} else {
scrollToEnd();
}
setOptimisticUserMessages((existing) => [
...existing,
{
id: messageIdForSend,
role: "user",
text: outgoingMessageText,
...(optimisticAttachments.length > 0 ? { attachments: optimisticAttachments } : {}),
turnId: null,
createdAt: messageCreatedAt,
updatedAt: messageCreatedAt,
streaming: false,
},
]);
setThreadError(threadIdForSend, null);
if (expiredTerminalContextCount > 0) {
const toastCopy = buildExpiredTerminalContextToastCopy(
expiredTerminalContextCount,
"omitted",
);
toastManager.add(
stackedThreadToast({
type: "warning",
title: toastCopy.title,
description: toastCopy.description,
}),
);
}
promptRef.current = "";
clearComposerDraftContent(composerDraftTarget);
composerRef.current?.resetCursorState();
let firstComposerImageName: string | null = null;
if (composerImagesSnapshot.length > 0) {
const firstComposerImage = composerImagesSnapshot[0];
if (firstComposerImage) {
firstComposerImageName = firstComposerImage.name;
}
}
let titleSeed = assistantCitationsToPlainText(trimmed);
if (!titleSeed) {
if (firstComposerImageName) {
titleSeed = `Image: ${firstComposerImageName}`;
} else if (composerFilesSnapshot[0]) {
titleSeed = `File: ${composerFilesSnapshot[0].name}`;
} else if (composerTerminalContextsSnapshot.length > 0) {
titleSeed = formatTerminalContextLabel(composerTerminalContextsSnapshot[0]!);
} else if (composerElementContextsSnapshot.length > 0) {
titleSeed = formatElementContextLabel(composerElementContextsSnapshot[0]!);
} else {
titleSeed = "New thread";
}
}
const title = truncate(titleSeed);
const threadCreateModelSelection = createModelSelection(
ctxSelectedModelSelection.instanceId,
ctxSelectedModel || activeProjectDefaultModelSelection?.model || DEFAULT_MODEL,
ctxSelectedModelSelection.options,
);
let failure: AtomCommandResult<unknown, unknown> | null = null;
// Auto-title from first message
if (isFirstMessage && isServerThread) {
const titleResult = await updateThreadMetadata({
environmentId,
input: {
threadId: threadIdForSend,
title,
},
});
if (titleResult._tag === "Failure") {
failure = titleResult;
}
}
if (failure === null && isServerThread) {
const settingsResult = await persistThreadSettingsForNextTurn({
threadId: threadIdForSend,
createdAt: messageCreatedAt,
...(ctxSelectedModel ? { modelSelection: ctxSelectedModelSelection } : {}),
...(localCheckoutBranchMismatch
? { branch: localCheckoutBranchMismatch.currentBranch }
: {}),
runtimeMode,
interactionMode: sendInteractionMode,
});
if (settingsResult._tag === "Failure") {
failure = settingsResult;
}
}
const turnAttachmentsResult = await settlePromise(async () => {
const turnAttachments = await turnAttachmentsPromise;
const liveFileBlockReason = readLiveAttachmentCapabilities().fileBlockReason;
if (liveFileBlockReason !== null) {
throw new Error(liveFileBlockReason);
}
return turnAttachments;
});
if (failure === null && turnAttachmentsResult._tag === "Failure") {
failure = turnAttachmentsResult;
}
let turnStartSucceeded = false;
if (failure === null && turnAttachmentsResult._tag === "Success") {
const bootstrap =
isLocalDraftThread || baseBranchForWorktree
? {
...(isLocalDraftThread
? {
createThread: {
projectId: activeProject.id,
title,
modelSelection: threadCreateModelSelection,
runtimeMode,
interactionMode: sendInteractionMode,
branch: activeThreadBranch,
worktreePath: activeThread.worktreePath,
createdAt: activeThread.createdAt,
},
}
: {}),
...(baseBranchForWorktree
? {
prepareWorktree: {
projectCwd: activeProject.workspaceRoot,
baseBranch: baseBranchForWorktree,
branch: buildTemporaryWorktreeBranchName(randomHex),
...(startFromOrigin ? { startFromOrigin: true } : {}),
},
runSetupScript: true,
}
: {}),
}
: undefined;
const backgroundThreadRef =
resolvedSubmissionIntent === "background"
? scopeThreadRef(activeThread.environmentId, threadIdForSend)
: null;
if (backgroundThreadRef) {
beginBackgroundDraftSubmissionByRef(backgroundThreadRef);
}
const startResult = await startThreadTurn({
environmentId,
input: {
threadId: threadIdForSend,
message: {
messageId: messageIdForSend,
role: "user",
text: outgoingMessageText,
attachments: turnAttachmentsResult.value,
},
modelSelection: ctxSelectedModelSelection,
titleSeed: title,
runtimeMode,
interactionMode: sendInteractionMode,
...(bootstrap ? { bootstrap } : {}),
createdAt: messageCreatedAt,
},
});
if (startResult._tag === "Failure") {
if (backgroundThreadRef) {
clearBackgroundDraftSubmissionByRef(backgroundThreadRef);
}
failure = startResult;
} else {
turnStartSucceeded = true;
// The turn is under way and will spend quota, so that thread's limits
// snapshot is stale. Uploads may have outlasted a navigation, so only
// the sending thread's panel clears.
clearUsageLimitsFor(routeThreadKey);
if (turnUsesAttachmentUploads) {
releaseDraftAttachments(composerAttachmentsSnapshot);
}
acknowledgeActiveThreadWoke();
if (backgroundThreadRef) {
markPromotedDraftThreadByRef(backgroundThreadRef);
try {
const nextDraft = await handleNewThread(
scopeProjectRef(activeProject.environmentId, activeProject.id),
resolveBackgroundDraftWorkspaceOptions({
envMode: sendEnvMode,
branch: activeThreadBranch,
startFromOrigin,
}),
);
if (nextDraft) {
finalizePromotedDraftThreadByRef(backgroundThreadRef);
toastManager.add(
stackedThreadToast({
type: "success",
title: "Started in background",
timeout: 5_000,
actionProps: {
children: "Open",
onClick: () => {
void navigate({
to: "/$environmentId/$threadId",
params: buildThreadRouteParams(backgroundThreadRef),
});
},
},
}),
);
} else {
clearBackgroundDraftSubmissionByRef(backgroundThreadRef);
}
} catch (error) {
clearBackgroundDraftSubmissionByRef(backgroundThreadRef);
resetLocalDispatch();
toastManager.add(
stackedThreadToast({
type: "warning",
title: "Task started in the background",
description:
error instanceof Error
? `Could not open a fresh composer: ${error.message}`
: "Could not open a fresh composer.",
}),
);
}
}
}
}
if (failure !== null) {
if (
promptRef.current.length === 0 &&
composerImagesRef.current.length === 0 &&
composerFilesRef.current.length === 0 &&
composerTerminalContextsRef.current.length === 0 &&
composerElementContextsRef.current.length === 0 &&
(useComposerDraftStore.getState().getComposerDraft(composerDraftTarget)?.previewAnnotations
.length ?? 0) === 0 &&
(useComposerDraftStore.getState().getComposerDraft(composerDraftTarget)?.reviewComments
.length ?? 0) === 0
) {
setOptimisticUserMessages((existing) => {
const removed = existing.filter((message) => message.id === messageIdForSend);
for (const message of removed) {
revokeUserMessagePreviewUrls(message);
}
const next = existing.filter((message) => message.id !== messageIdForSend);
return next.length === existing.length ? existing : next;
});
promptRef.current = promptForSend;
const retryComposerImages = composerImagesSnapshot.map(cloneComposerImageForRetry);
composerImagesRef.current = retryComposerImages;
composerFilesRef.current = composerFilesSnapshot;
composerTerminalContextsRef.current = composerTerminalContextsSnapshot;
composerElementContextsRef.current = composerElementContextsSnapshot;
setComposerDraftPrompt(composerDraftTarget, promptForSend);
addComposerDraftImages(composerDraftTarget, retryComposerImages);
addComposerDraftFiles(composerDraftTarget, composerFilesSnapshot);
setComposerDraftTerminalContexts(composerDraftTarget, composerTerminalContextsSnapshot);
setComposerDraftElementContexts(composerDraftTarget, composerElementContextsSnapshot);
setComposerDraftPreviewAnnotations(composerDraftTarget, composerPreviewAnnotationsSnapshot);
setComposerDraftReviewComments(composerDraftTarget, composerReviewCommentsSnapshot);
composerRef.current?.resetCursorState({
cursor: collapseExpandedComposerCursor(promptForSend, promptForSend.length),
prompt: promptForSend,
detectTrigger: true,
});
}
if (!isAtomCommandInterrupted(failure)) {
const error = squashAtomCommandFailure(failure);
if (isLocalDraftThread && draftId && wasBootstrapThreadDeleted(error)) {
const failedDraftSession = getDraftSession(draftId);
if (failedDraftSession?.threadId === threadIdForSend) {
setLogicalProjectDraftThreadId(
failedDraftSession.logicalProjectKey,
scopeProjectRef(failedDraftSession.environmentId, failedDraftSession.projectId),
draftId,
{
threadId: newThreadId(),
createdAt: new Date().toISOString(),
},
);
}
}
setThreadError(
threadIdForSend,
error instanceof Error ? error.message : "Failed to send message.",
);
}
}
sendInFlightRef.current = false;
if (!turnStartSucceeded) {
setDockedDraftHeroThreadKey((currentThreadKey) =>
currentThreadKey === activeThreadKey ? null : currentThreadKey,
);
resetLocalDispatch();
}
};
const onRespondToApproval = useCallback(
async (requestId: ApprovalRequestId, decision: ProviderApprovalDecision) => {
if (!activeThreadId) return;
setRespondingRequestIds((existing) =>
existing.includes(requestId) ? existing : [...existing, requestId],
);
const result = await respondToThreadApproval({
environmentId,
input: {
threadId: activeThreadId,
requestId,
decision,
},
});
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
setThreadError(
activeThreadId,
error instanceof Error ? error.message : "Failed to submit approval decision.",
);
}
setRespondingRequestIds((existing) => existing.filter((id) => id !== requestId));
return result;
},
[activeThreadId, environmentId, respondToThreadApproval, setThreadError],
);
const onRespondToUserInput = useCallback(
async (requestId: ApprovalRequestId, answers: Record<string, unknown>) => {
if (!activeThreadId || !activePendingUserInput || activePendingIsResponding) return;
const responseKey = JSON.stringify([environmentId, activeThreadId, requestId]);
if (userInputResponsesInFlight.current.has(responseKey)) return;
const attachmentsByQuestionId = new Map<
string,
import("@t3tools/contracts").UserInputAttachments[string]
>();
for (const question of activePendingUserInput.questions) {
const target = questionAttachmentDraftId(
environmentId,
activeThreadId,
requestId,
question.id,
);
if ((useQuestionAttachmentPreparation.getState().counts[target] ?? 0) > 0) return;
const draft = useComposerDraftStore.getState().getComposerDraft(target);
const attachments = draft ? [...draft.images, ...draft.files] : [];
if (attachments.length === 0) continue;
const uploaded = getUploadedAttachments({ environmentId, images: attachments });
if (!uploaded) {
setThreadError(
activeThreadId,
"Wait for attachments to finish uploading, or remove failed uploads.",
);
return;
}
attachmentsByQuestionId.set(
question.id,
uploaded as import("@t3tools/contracts").UserInputAttachments[string],
);
}
userInputResponsesInFlight.current.add(responseKey);
setRespondingUserInputRequestIds((existing) =>
existing.includes(requestId) ? existing : [...existing, requestId],
);
const result = await respondToThreadUserInput({
environmentId,
input: {
threadId: activeThreadId,
requestId,
answers,
...(attachmentsByQuestionId.size > 0
? { attachmentsByQuestionId: Object.fromEntries(attachmentsByQuestionId) }
: {}),
},
});
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
setThreadError(
activeThreadId,
error instanceof Error ? error.message : "Failed to submit user input.",
);
}
userInputResponsesInFlight.current.delete(responseKey);
setRespondingUserInputRequestIds((existing) => existing.filter((id) => id !== requestId));
return result;
},
[
activeThreadId,
activePendingUserInput,
activePendingIsResponding,
environmentId,
respondToThreadUserInput,
setThreadError,
],
);
// Closes an async question without messaging the agent. The server records
// the dismissal so every client releases the composer.
const onDismissUserInput = useCallback(
async (requestId: ApprovalRequestId) => {
if (!activeThreadId) return;
setRespondingUserInputRequestIds((existing) =>
existing.includes(requestId) ? existing : [...existing, requestId],
);
const result = await dismissThreadUserInput({
environmentId,
input: { threadId: activeThreadId, requestId },
});
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
setThreadError(
activeThreadId,
error instanceof Error ? error.message : "Failed to dismiss the question.",
);
}
setRespondingUserInputRequestIds((existing) => existing.filter((id) => id !== requestId));
return result;
},
[activeThreadId, dismissThreadUserInput, environmentId, setThreadError],
);
const setActivePendingUserInputQuestionIndex = useCallback(
(nextQuestionIndex: number) => {
if (!activePendingUserInput) {
return;
}
setPendingUserInputQuestionIndexByRequestId((existing) => ({
...existing,
[activePendingRequestKey]: nextQuestionIndex,
}));
},
[activePendingUserInput, activePendingRequestKey],
);
const onSelectActivePendingUserInputOption = useCallback(
(questionId: string, optionValue: string) => {
if (!activePendingUserInput) {
return;
}
setPendingUserInputAnswersByRequestId((existing) => {
const question =
(activePendingProgress?.activeQuestion?.id === questionId
? activePendingProgress.activeQuestion
: undefined) ??
activePendingUserInput.questions.find((entry) => entry.id === questionId);
if (!question) {
return existing;
}
return {
...existing,
[activePendingRequestKey]: {
...existing[activePendingRequestKey],
[questionId]: togglePendingUserInputOptionSelection(
question,
existing[activePendingRequestKey]?.[questionId],
optionValue,
),
},
};
});
promptRef.current = "";
composerRef.current?.resetCursorState({ cursor: 0 });
},
[
activePendingProgress?.activeQuestion,
activePendingUserInput,
activePendingRequestKey,
composerRef,
],
);
const onChangeActivePendingUserInputCustomAnswer = useCallback(
(
questionId: string,
value: string,
nextCursor: number,
expandedCursor: number,
_cursorAdjacentToMention: boolean,
) => {
if (!activePendingUserInput) {
return;
}
const question = activePendingUserInput.questions.find((entry) => entry.id === questionId);
if (!question || question.allowCustomAnswer === false) {
return;
}
promptRef.current = value;
setPendingUserInputAnswersByRequestId((existing) => ({
...existing,
[activePendingRequestKey]: {
...existing[activePendingRequestKey],
[questionId]: setPendingUserInputCustomAnswer(
existing[activePendingRequestKey]?.[questionId],
value,
),
},
}));
const snapshot = composerRef.current?.readSnapshot();
if (
snapshot?.value !== value ||
snapshot.cursor !== nextCursor ||
snapshot.expandedCursor !== expandedCursor
) {
composerRef.current?.focusAt(nextCursor);
}
},
[activePendingUserInput, activePendingRequestKey, composerRef],
);
const onAdvanceActivePendingUserInput = useCallback(() => {
if (
!activePendingUserInput ||
!activePendingProgress ||
!activePendingProgress.canAdvance ||
activePendingIsResponding
) {
return;
}
if (activePendingProgress.isLastQuestion) {
if (activePendingResolvedAnswers) {
void onRespondToUserInput(activePendingUserInput.requestId, activePendingResolvedAnswers);
}
return;
}
setActivePendingUserInputQuestionIndex(activePendingProgress.questionIndex + 1);
}, [
activePendingProgress,
activePendingResolvedAnswers,
activePendingUserInput,
activePendingIsResponding,
onRespondToUserInput,
setActivePendingUserInputQuestionIndex,
]);
const onPreviousActivePendingUserInputQuestion = useCallback(() => {
if (!activePendingProgress) {
return;
}
setActivePendingUserInputQuestionIndex(Math.max(activePendingProgress.questionIndex - 1, 0));
}, [activePendingProgress, setActivePendingUserInputQuestionIndex]);
const onSubmitPlanFollowUp = useCallback(
async ({
text,
interactionMode: nextInteractionMode,
}: {
text: string;
interactionMode: "default" | "plan";
}) => {
if (
!activeThread ||
!isServerThread ||
isSendBusy ||
isConnecting ||
sendInFlightRef.current
) {
return;
}
const trimmed = text.trim();
if (!trimmed) {
return;
}
const sendCtx = composerRef.current?.getSendContext();
if (!sendCtx?.providerAvailable || !sendCtx.interactionModeEnabled) {
return;
}
const {
selectedProvider: ctxSelectedProvider,
selectedModel: ctxSelectedModel,
selectedProviderModels: ctxSelectedProviderModels,
selectedPromptEffort: ctxSelectedPromptEffort,
selectedModelSelection: ctxSelectedModelSelection,
} = sendCtx;
const threadIdForSend = activeThread.id;
const messageIdForSend = newMessageId();
const messageCreatedAt = new Date().toISOString();
const outgoingMessageText = formatOutgoingPrompt({
provider: ctxSelectedProvider,
model: ctxSelectedModel,
models: ctxSelectedProviderModels,
effort: ctxSelectedPromptEffort,
text: trimmed,
});
sendInFlightRef.current = true;
beginLocalDispatch({ preparingWorktree: false });
setThreadError(threadIdForSend, null);
scrollToEnd();
setOptimisticUserMessages((existing) => [
...existing,
{
id: messageIdForSend,
role: "user",
text: outgoingMessageText,
turnId: null,
createdAt: messageCreatedAt,
updatedAt: messageCreatedAt,
streaming: false,
},
]);
const settingsResult = await persistThreadSettingsForNextTurn({
threadId: threadIdForSend,
createdAt: messageCreatedAt,
modelSelection: ctxSelectedModelSelection,
...(localCheckoutBranchMismatch
? { branch: localCheckoutBranchMismatch.currentBranch }
: {}),
runtimeMode,
interactionMode: nextInteractionMode,
});
let failure: AtomCommandResult<unknown, unknown> | null =
settingsResult._tag === "Failure" ? settingsResult : null;
if (failure === null) {
// Keep the mode toggle and plan-follow-up banner in sync immediately
// while the same-thread implementation turn is starting.
setComposerDraftInteractionMode(
scopeThreadRef(activeThread.environmentId, threadIdForSend),
nextInteractionMode,
);
const startResult = await startThreadTurn({
environmentId,
input: {
threadId: threadIdForSend,
message: {
messageId: messageIdForSend,
role: "user",
text: outgoingMessageText,
attachments: [],
},
modelSelection: ctxSelectedModelSelection,
titleSeed: activeThread.title,
runtimeMode,
interactionMode: nextInteractionMode,
...(nextInteractionMode === "default" && activeProposedPlan
? {
sourceProposedPlan: {
threadId: activeThread.id,
planId: activeProposedPlan.id,
},
}
: {}),
createdAt: messageCreatedAt,
},
});
failure = startResult._tag === "Failure" ? startResult : null;
}
if (failure === null) {
clearUsageLimitsFor(routeThreadKey);
acknowledgeActiveThreadWoke();
sendInFlightRef.current = false;
return;
}
setOptimisticUserMessages((existing) =>
existing.filter((message) => message.id !== messageIdForSend),
);
if (!isAtomCommandInterrupted(failure)) {
const error = squashAtomCommandFailure(failure);
setThreadError(
threadIdForSend,
error instanceof Error ? error.message : "Failed to send plan follow-up.",
);
}
sendInFlightRef.current = false;
resetLocalDispatch();
},
[
activeThread,
activeProposedPlan,
acknowledgeActiveThreadWoke,
beginLocalDispatch,
isConnecting,
isSendBusy,
isServerThread,
localCheckoutBranchMismatch,
persistThreadSettingsForNextTurn,
resetLocalDispatch,
runtimeMode,
scrollToEnd,
setComposerDraftInteractionMode,
setThreadError,
startThreadTurn,
environmentId,
composerRef,
clearUsageLimitsFor,
routeThreadKey,
],
);
const onImplementPlanInNewThread = useCallback(async () => {
if (
!activeThread ||
!activeProject ||
!activeProposedPlan ||
!isServerThread ||
isSendBusy ||
isConnecting ||
activeEnvironmentUnavailable ||
sendInFlightRef.current
) {
return;
}
const sendCtx = composerRef.current?.getSendContext();
if (!sendCtx?.providerAvailable || !sendCtx.interactionModeEnabled) {
return;
}
const {
selectedProvider: ctxSelectedProvider,
selectedModel: ctxSelectedModel,
selectedProviderModels: ctxSelectedProviderModels,
selectedPromptEffort: ctxSelectedPromptEffort,
selectedModelSelection: ctxSelectedModelSelection,
} = sendCtx;
const createdAt = new Date().toISOString();
const nextThreadId = newThreadId();
const planMarkdown = activeProposedPlan.planMarkdown;
const implementationPrompt = buildPlanImplementationPrompt(planMarkdown);
const outgoingImplementationPrompt = formatOutgoingPrompt({
provider: ctxSelectedProvider,
model: ctxSelectedModel,
models: ctxSelectedProviderModels,
effort: ctxSelectedPromptEffort,
text: implementationPrompt,
});
if (composerRef.current?.validateProviderInput(outgoingImplementationPrompt) === false) {
return;
}
const nextThreadTitle = truncate(buildPlanImplementationThreadTitle(planMarkdown));
const nextThreadModelSelection: ModelSelection = ctxSelectedModelSelection;
sendInFlightRef.current = true;
beginLocalDispatch({ preparingWorktree: false });
const finish = () => {
sendInFlightRef.current = false;
resetLocalDispatch();
};
const createResult = await createThread({
environmentId,
input: {
threadId: nextThreadId,
projectId: activeProject.id,
title: nextThreadTitle,
modelSelection: nextThreadModelSelection,
runtimeMode: defaultRuntimeMode,
interactionMode: "default",
branch: activeThreadBranch,
worktreePath: activeThread.worktreePath,
createdAt,
},
});
let failure: AtomCommandResult<unknown, unknown> | null =
createResult._tag === "Failure" ? createResult : null;
if (failure === null) {
const startResult = await startThreadTurn({
environmentId,
input: {
threadId: nextThreadId,
message: {
messageId: newMessageId(),
role: "user",
text: outgoingImplementationPrompt,
attachments: [],
},
modelSelection: ctxSelectedModelSelection,
titleSeed: nextThreadTitle,
runtimeMode: defaultRuntimeMode,
interactionMode: "default",
sourceProposedPlan: {
threadId: activeThread.id,
planId: activeProposedPlan.id,
},
createdAt,
},
});
failure = startResult._tag === "Failure" ? startResult : null;
}
if (failure === null) {
const startedResult = await settlePromise(() =>
waitForStartedServerThread(scopeThreadRef(activeThread.environmentId, nextThreadId)),
);
failure = startedResult._tag === "Failure" ? startedResult : null;
}
if (failure === null) {
const navigateResult = await settlePromise(() =>
navigate({
to: "/$environmentId/$threadId",
params: {
environmentId: activeThread.environmentId,
threadId: nextThreadId,
},
}),
);
failure = navigateResult._tag === "Failure" ? navigateResult : null;
}
if (failure !== null) {
const cleanupResult = await deleteThread({
environmentId,
input: {
threadId: nextThreadId,
},
});
if (cleanupResult._tag === "Failure" && !isAtomCommandInterrupted(cleanupResult)) {
console.warn(
"Failed to clean up implementation thread after start failure.",
squashAtomCommandFailure(cleanupResult),
);
}
if (!isAtomCommandInterrupted(failure)) {
const error = squashAtomCommandFailure(failure);
toastManager.add(
stackedThreadToast({
type: "error",
title: "Could not start implementation thread",
description:
error instanceof Error
? error.message
: "An error occurred while creating the new thread.",
}),
);
}
}
finish();
}, [
activeProject,
activeProposedPlan,
activeThreadBranch,
activeThread,
beginLocalDispatch,
activeEnvironmentUnavailable,
createThread,
deleteThread,
isConnecting,
isSendBusy,
isServerThread,
navigate,
resetLocalDispatch,
defaultRuntimeMode,
startThreadTurn,
environmentId,
composerRef,
]);
const getModelDisabledReason = useCallback(
(instanceId: ProviderInstanceId, model: string): string | null => {
if (!activeThread) {
return null;
}
const reason = getStartedThreadModelChangeBlockReason({
providers: providerStatuses,
hasStartedSession: activeThread.session !== null,
currentModelSelection: activeThread.modelSelection,
currentProviderInstanceId: activeThread.session?.providerInstanceId ?? null,
nextModelSelection: { instanceId, model },
});
return reason ? `${reason.description} Start a new thread to use this model.` : null;
},
[activeThread, providerStatuses],
);
const onProviderModelSelect = useCallback(
(instanceId: ProviderInstanceId, model: string) => {
if (!activeThread) return;
// Look up the configured instance so model normalization and custom
// model lookup stay scoped to that exact instance. Unknown instance ids
// are rejected by returning early; the server remains authoritative too.
const entry = providerStatuses.find((snapshot) => snapshot.instanceId === instanceId);
const resolvedDriverKind = entry?.driver ?? null;
if (
lockedProvider !== null &&
resolvedDriverKind !== null &&
resolvedDriverKind !== lockedProvider
) {
scheduleComposerFocus();
return;
}
if (lockedProvider !== null && activeThread.session?.providerInstanceId) {
const currentEntry = providerStatuses.find(
(snapshot) => snapshot.instanceId === activeThread.session?.providerInstanceId,
);
if (
currentEntry?.continuation?.groupKey &&
entry?.continuation?.groupKey &&
currentEntry.continuation.groupKey !== entry.continuation.groupKey
) {
scheduleComposerFocus();
return;
}
}
const resolvedModel = resolveAppModelSelectionForInstance(
instanceId,
settings,
providerStatuses,
model,
);
if (!resolvedModel) {
scheduleComposerFocus();
return;
}
const nextModelSelection: ModelSelection = {
instanceId,
model: resolvedModel,
};
const modelChangeBlockReason = getStartedThreadModelChangeBlockReason({
providers: providerStatuses,
hasStartedSession: activeThread.session !== null,
currentModelSelection: activeThread.modelSelection,
currentProviderInstanceId: activeThread.session?.providerInstanceId ?? null,
nextModelSelection,
});
if (modelChangeBlockReason) {
toastManager.add({
type: "warning",
title: modelChangeBlockReason.title,
description: modelChangeBlockReason.description,
});
scheduleComposerFocus();
return;
}
setComposerDraftModelSelection(
scopeThreadRef(activeThread.environmentId, activeThread.id),
nextModelSelection,
{ explicit: true },
);
setStickyComposerModelSelection(nextModelSelection);
scheduleComposerFocus();
},
[
activeThread,
lockedProvider,
scheduleComposerFocus,
setComposerDraftModelSelection,
setStickyComposerModelSelection,
providerStatuses,
settings,
],
);
const onEnvModeChange = useCallback(
(mode: DraftThreadEnvMode) => {
if (canOverrideServerThreadEnvMode) {
setPendingServerThreadEnvMode(mode);
scheduleComposerFocus();
return;
}
if (isLocalDraftThread) {
setDraftThreadContext(composerDraftTarget, {
envMode: mode,
startFromOrigin: resolveNewDraftStartFromOrigin({
envMode: mode,
newWorktreesStartFromOrigin: activeProjectSettings.settings.newWorktreesStartFromOrigin,
}),
...(mode === "worktree" && draftThread?.worktreePath ? { worktreePath: null } : {}),
});
}
scheduleComposerFocus();
},
[
canOverrideServerThreadEnvMode,
composerDraftTarget,
draftThread?.worktreePath,
isLocalDraftThread,
activeProjectSettings.settings.newWorktreesStartFromOrigin,
setPendingServerThreadEnvMode,
scheduleComposerFocus,
setDraftThreadContext,
],
);
const onStartFromOriginChange = (nextStartFromOrigin: boolean) => {
if (canOverrideServerThreadEnvMode && activeThread) {
setPendingServerThreadStartFromOriginByThreadId((current) =>
current[activeThread.id] === nextStartFromOrigin
? current
: { ...current, [activeThread.id]: nextStartFromOrigin },
);
return;
}
if (isLocalDraftThread) {
setDraftThreadContext(composerDraftTarget, {
startFromOrigin: nextStartFromOrigin,
});
}
};
const onExpandTimelineImage = useCallback((preview: ExpandedImagePreview) => {
setExpandedImage(preview);
}, []);
const onOpenTurnDiff = useCallback(
(turnId: TurnId, filePath?: string) => {
if (!isServerThread || !activeThreadRef) return;
useDiffPanelStore.getState().selectTurn(activeThreadRef, turnId, filePath);
useRightPanelStore.getState().open(activeThreadRef, "diff");
onDiffPanelOpen?.();
},
[activeThreadRef, isServerThread, onDiffPanelOpen],
);
// The revert handler is read from a ref at call-time so the callback
// reference is fully stable and never busts TimelineRowCtx identity.
const onRevertToTurnCountRef = useRef(onRevertToTurnCount);
onRevertToTurnCountRef.current = onRevertToTurnCount;
const onRevertTimelineTurn = useCallback((targetTurnCount: number, messageId: MessageId) => {
void onRevertToTurnCountRef.current(targetTurnCount, messageId);
}, []);
// Files dropped on a sidebar row land here once the dropped-on thread is
// actually open, then take the exact same path as a workspace drop:
// validate, compress, focus the composer, never send. Kept above the
// no-active-thread early return so hook order never changes.
const pendingSidebarFileDrops = useSidebarPendingFileDropStore((state) => state.pending);
const consumePendingFileDrop = useSidebarPendingFileDropStore(
(state) => state.consumePendingFileDrop,
);
useEffect(() => {
if (pendingSidebarFileDrops.length === 0) return;
// A promoting draft can mount this view with the server thread id while
// its composer is still draft-keyed; finalization would discard what we
// attach there. Only the canonical thread target may consume a drop.
if (
typeof composerDraftTarget === "string" ||
!pendingSidebarFileDrops.some((drop) =>
isSameSidebarThreadRef(composerDraftTarget, drop.threadRef),
)
) {
return;
}
if (!activeThread) return;
if (!composerRef.current) {
const raf = window.requestAnimationFrame(() => {
if (!composerRef.current) return;
if (typeof composerDraftTarget === "string") return;
// Consume matches by target, so a newer drop that arrived meanwhile
// is collected too rather than orphaned.
const files = consumePendingFileDrop(composerDraftTarget);
if (files !== null) {
composerRef.current?.addDroppedFiles(files);
}
});
return () => window.cancelAnimationFrame(raf);
}
const files = consumePendingFileDrop(composerDraftTarget);
if (files !== null) {
composerRef.current.addDroppedFiles(files);
}
}, [
activeThread,
composerDraftTarget,
composerRef,
consumePendingFileDrop,
pendingSidebarFileDrops,
]);
// Empty state: no active thread
if (!activeThread) {
return <NoActiveThreadState />;
}
const panelToggleControls = (
<PanelLayoutControls
terminalAvailable={activeProject !== null}
terminalOpen={terminalUiState.terminalOpen}
terminalShortcutLabel={shortcutLabelForCommand(keybindings, "terminal.toggle")}
rightPanelAvailable={activeProject !== null}
rightPanelOpen={rightPanelOpen}
rightPanelShortcutLabel={shortcutLabelForCommand(keybindings, "rightPanel.toggle")}
// Suppressed while the Agents surface is visible: the roster itself is
// on screen, so the toggle badge would be pointing at nothing.
liveAgentCount={
rightPanelOpen && activeRightPanelSurface?.kind === "agents" ? 0 : agentPanelModel.liveCount
}
onToggleTerminal={toggleTerminalVisibility}
onToggleRightPanel={toggleRightPanel}
/>
);
const panelLayoutControls = (
<div
className={cn(
// Keep one viewport anchor inside the header's no-drag region. The
// header can shrink behind the right panel without moving the controls.
"pointer-events-none fixed top-[var(--workspace-controls-top)] right-[var(--workspace-controls-right)] z-50 mr-px flex h-[var(--workspace-topbar-height)] items-center gap-1 [-webkit-app-region:no-drag]",
)}
data-workspace-titlebar-controls
>
{!shouldUseRightPanelSheet ? (
<span
aria-hidden={!rightPanelOpen}
className={cn(
"flex shrink-0",
panelAnimationsActive &&
"motion-safe:transition-opacity motion-safe:[transition-duration:var(--panel-animation-duration)] motion-safe:ease-out",
rightPanelOpen ? "pointer-events-auto opacity-100" : "pointer-events-none opacity-0",
)}
inert={!rightPanelOpen}
>
<RightPanelMaximizeControl
maximized={rightPanelMaximized}
onToggle={toggleRightPanelMaximized}
/>
</span>
) : null}
<div className="pointer-events-auto flex h-full items-center">{panelToggleControls}</div>
</div>
);
const rightPanelContent = activeThreadRef ? (
renderedRightPanelSurface?.kind === "preview" ? (
<Suspense fallback={null}>
<PreviewPanel
mode="embedded"
threadRef={activeThreadRef}
tabId={renderedRightPanelSurface.resourceId}
configuredUrls={configuredPreviewUrls}
visible={rightPanelOpen}
onSendAnnotation={(annotation, image) => {
void onSend(undefined, "foreground", { annotation, image });
}}
/>
</Suspense>
) : renderedRightPanelSurface?.kind === "terminal" ? (
<PersistentThreadTerminalPanel
visible={rightPanelOpen}
threadRef={activeThreadRef}
surface={renderedRightPanelSurface}
launchContext={activeTerminalLaunchContext ?? null}
focusRequestId={terminalFocusRequestId}
keybindings={keybindings}
onAddTerminalContext={addTerminalContextToDraft}
onSplitTerminal={splitPanelTerminal}
onSplitTerminalVertical={splitPanelTerminalVertical}
onNewTerminal={addTerminalSurface}
onActiveTerminalChange={activatePanelTerminal}
onCloseTerminal={closePanelTerminal}
splitShortcutLabel={splitTerminalShortcutLabel ?? undefined}
splitVerticalShortcutLabel={splitTerminalVerticalShortcutLabel ?? undefined}
newShortcutLabel={newTerminalShortcutLabel ?? undefined}
closeShortcutLabel={closeTerminalShortcutLabel ?? undefined}
/>
) : renderedRightPanelSurface?.kind === "diff" ? (
<Suspense fallback={null}>
<DiffPanel
key={`${activeThreadKey}:${diffPanelGitStatusResolutionKey}`}
mode="embedded"
composerDraftTarget={composerDraftTarget}
initialGitScope={initialDiffPanelGitScope}
workspaceMutationId={workspaceMutationId}
/>
</Suspense>
) : renderedRightPanelSurface?.kind === "pull-request" && !pullRequestsCapabilityKnown ? (
<PullRequestDetailGhost />
) : renderedRightPanelSurface?.kind === "pull-request" && !supportsPullRequests ? (
<PullRequestsUnavailableState
title="Pull requests unavailable"
error="Update this environment's T3 Code server to browse pull requests."
/>
) : renderedRightPanelSurface?.kind === "pull-request" ? (
// No onClose: the surface tab's own X owns closing here, and a second X in the header
// would be the same action twice. The thread context also drops the checkout button, so it
// is only right for the thread's own pull request, whose branch is already under the
// reader's feet. A link the agent wrote can open any other one here, and that one has to be
// checkable out like it is anywhere else.
<PullRequestDetailPanel
key={`${renderedRightPanelSurface.host ?? ""}:${renderedRightPanelSurface.repository}#${renderedRightPanelSurface.number}`}
environmentId={activeThread.environmentId}
onSelectPullRequest={(reference) => {
if (activeThreadRef)
useRightPanelStore.getState().openPullRequest(activeThreadRef, {
projectId: reference.projectId,
repository: reference.repository,
number: reference.number,
...(reference.host ? { host: reference.host } : {}),
});
}}
threadRef={activeThreadRef}
reference={{
projectId: renderedRightPanelSurface.projectId as ProjectId,
...(renderedRightPanelSurface.host ? { host: renderedRightPanelSurface.host } : {}),
repository: renderedRightPanelSurface.repository,
number: renderedRightPanelSurface.number,
}}
context={
isThreadOwnPullRequest(
{
projectId: linkedThreadPullRequest?.projectId ?? null,
repository: linkedThreadPullRequest?.repository ?? null,
number: linkedThreadPullRequest?.number ?? null,
},
{
projectId: renderedRightPanelSurface.projectId,
repository: renderedRightPanelSurface.repository,
number: renderedRightPanelSurface.number,
},
)
? "thread"
: "page"
}
composerDraftTarget={composerDraftTarget}
onBack={
activeThreadRef !== null && pullRequestsSurfaceAvailable
? addPullRequestsSurface
: undefined
}
/>
) : renderedRightPanelSurface?.kind === "pull-requests" && activeThreadRef ? (
<ThreadPullRequestsPanel threadRef={activeThreadRef} />
) : renderedRightPanelSurface?.kind === "agents" ? (
<AgentsPanel
model={agentPanelModel}
environmentId={activeThreadRef?.environmentId ?? null}
threadId={activeThreadRef?.threadId ?? null}
/>
) : renderedRightPanelSurface?.kind === "device" ? (
<Suspense fallback={null}>
<DevicePanel
mode="embedded"
threadRef={activeThreadRef}
key={renderedRightPanelSurface.id}
surface={renderedRightPanelSurface}
visible={rightPanelOpen}
onDismissSetup={() => {
closeRightPanelSurface(renderedRightPanelSurface);
useRightPanelStore.getState().show(activeThreadRef);
}}
/>
</Suspense>
) : (renderedRightPanelSurface?.kind === "files" ||
renderedRightPanelSurface?.kind === "file") &&
((activeProject && activeWorkspaceRoot) ||
(renderedRightPanelSurface.kind === "file" && renderedRightPanelSurface.attachment)) ? (
<Suspense fallback={null}>
<FilePreviewPanel
key={`${activeThread.environmentId}:${
renderedRightPanelSurface.kind === "file" && renderedRightPanelSurface.attachment
? `attachment:${renderedRightPanelSurface.attachment.id}`
: activeWorkspaceRoot
}`}
environmentId={activeThread.environmentId}
cwd={activeWorkspaceRoot ?? ""}
projectName={activeProject?.title ?? ""}
threadRef={activeThreadRef}
composerDraftTarget={composerDraftTarget}
keybindings={keybindings}
availableEditors={availableEditors}
relativePath={
renderedRightPanelSurface.kind === "file"
? renderedRightPanelSurface.relativePath
: null
}
{...(renderedRightPanelSurface.kind === "file" && renderedRightPanelSurface.attachment
? { attachment: renderedRightPanelSurface.attachment }
: {})}
revealLine={
renderedRightPanelSurface.kind === "file"
? (renderedRightPanelSurface.revealLine ?? null)
: null
}
revealRequestId={
renderedRightPanelSurface.kind === "file"
? renderedRightPanelSurface.revealRequestId
: 0
}
onOpenFile={openFileSurface}
onPendingChange={handleFilePendingChange}
selectedFilePending={
renderedRightPanelSurface.kind === "file" &&
pendingFileSurfaceIds.has(renderedRightPanelSurface.id)
}
workspaceMutationId={workspaceMutationId}
/>
</Suspense>
) : null
) : null;
const workspaceFileDropHandlers = makeWorkspaceFileDropHandlers({
setDragActive: setIsWorkspaceFileDragActive,
addFiles: (files) => composerRef.current?.addDroppedFiles(files),
});
return (
<div className="relative flex min-h-0 min-w-0 flex-1 overflow-hidden bg-background">
<Dialog
open={
deviceSetupThread !== null &&
deviceSetupThread.environmentId === activeThreadRef?.environmentId &&
deviceSetupThread.threadId === activeThreadRef?.threadId
}
onOpenChange={(open) => {
if (!open) setDeviceSetupThread(null);
}}
>
<WizardPopup>
{activeThreadRef ? (
<DeviceSetup
environmentId={activeThreadRef.environmentId}
state={deviceState}
onComplete={() => {
useRightPanelStore.getState().open(activeThreadRef, "device");
setDeviceSetupThread(null);
}}
/>
) : null}
</WizardPopup>
</Dialog>
{rightPanelControlsAtRoot ? panelLayoutControls : null}
<div
className={cn(
"flex min-h-0 min-w-0 flex-col overflow-x-hidden",
rightPanelMaximized ? "w-0 flex-none" : "flex-1",
)}
data-chat-column-maximized-away={rightPanelMaximized ? "true" : "false"}
>
{/* Top bar */}
<WorkspacePageHeader
data-chat-header
electron={isElectron}
reserveNativeControls={reserveTitleBarControlInset && !inlineRightPanelOwnsTitleBar}
className="relative bg-background"
>
{isElectron && rightPanelControlsAtRoot ? (
<span
aria-hidden
className="pointer-events-none fixed top-[var(--workspace-controls-top)] right-[var(--workspace-controls-right)] h-[var(--workspace-topbar-height)] w-28 [-webkit-app-region:no-drag]"
/>
) : null}
{!rightPanelControlsAtRoot && !rightPanelControlsInPanel ? panelLayoutControls : null}
<ChatHeader
{...(!supportsPullRequests || activeProjectRepository === null
? {}
: { onOpenPullRequest: openProjectPullRequest })}
activeThreadEnvironmentId={activeThread.environmentId}
activeThreadId={activeThread.id}
{...(routeKind === "draft" && draftId ? { draftId } : {})}
activeThreadTitle={activeThread.title}
isServerThread={isServerThread}
activeProject={activeProject}
openInCwd={gitCwd}
activeProjectScripts={activeProjectScripts}
preferredScriptId={
activeProject ? (lastInvokedScriptByProjectId[activeProject.id] ?? null) : null
}
keybindings={keybindings}
availableEditors={availableEditors}
rightPanelOpen={rightPanelOpen}
gitCwd={gitCwd}
onNewThreadInProject={handleNewThreadInActiveProject}
{...(activeDraftLogicalProjectKey
? { onOpenProjectSettings: handleOpenDraftProjectSettings }
: {})}
onRunProjectScript={runProjectScript}
onAddProjectScript={saveProjectScript}
onUpdateProjectScript={updateProjectScript}
onDeleteProjectScript={deleteProjectScript}
/>
</WorkspacePageHeader>
{/* Main content area with optional plan sidebar */}
<div className="flex min-h-0 min-w-0 flex-1">
{/* Chat column */}
<div
className="relative flex min-h-0 min-w-0 flex-1 flex-col"
data-chat-workspace-drop-target="true"
onDragEnter={workspaceFileDropHandlers.onDragEnter}
onDragOver={workspaceFileDropHandlers.onDragOver}
onDragLeave={workspaceFileDropHandlers.onDragLeave}
onDrop={workspaceFileDropHandlers.onDrop}
>
{isWorkspaceFileDragActive ? (
<div
className="pointer-events-none absolute inset-2 z-40 flex items-center justify-center rounded-2xl border-2 border-dashed border-primary/60 bg-primary/[0.035]"
data-chat-workspace-drop-overlay="true"
>
<div
role="status"
className="flex items-center gap-2 rounded-full border border-primary/25 bg-background/95 px-4 py-2.5 text-sm font-medium text-foreground shadow-lg"
>
<PaperclipIcon className="size-4 text-primary" aria-hidden="true" />
Drop files to attach
</div>
</div>
) : null}
{/* Banners overlay the timeline without changing its content height. */}
<div className="pointer-events-none absolute inset-x-0 top-0 z-20 flex flex-col">
<ProviderStatusBanner
status={visibleProviderStatus}
onDismiss={() => setDismissedProviderStatusBannerKey(providerStatusBannerKey)}
onOpenProviderSetup={openProviderSetup}
/>
<ThreadErrorBanner
error={visibleThreadError}
onDismiss={() => {
setThreadError(activeThread.id, null);
dismissThreadErrorBannerForSession(threadErrorBannerKey);
setThreadErrorBannerDismissTick((tick) => tick + 1);
}}
/>
</div>
{/* Messages Wrapper */}
<div className="relative flex min-h-0 flex-1 flex-col bg-background">
{/* Messages — LegendList handles virtualization and scrolling internally */}
<MessagesTimeline
citationRequest={paintOnlyDisplayedTimeline ? null : citationRequest}
citationHistoryLoading={threadDetailLoading}
{...(!paintOnlyDisplayedTimeline
? {
onCiteAssistantText: citeAssistantText,
agentPanelModel,
onOpenAgents: addAgentsSurface,
onUseArtifactTemplate: useArtifactTemplate,
}
: {})}
isWorking={!paintOnlyDisplayedTimeline && isWorking}
isPreparingWorktree={!paintOnlyDisplayedTimeline && isPreparingWorktree}
isCompacting={!paintOnlyDisplayedTimeline && isCompacting}
activeTurnStartedAt={paintOnlyDisplayedTimeline ? null : activeWorkStartedAt}
listRef={legendListRef}
timelineEntries={displayedTimeline.entries}
latestTurn={paintOnlyDisplayedTimeline ? null : activeLatestTurn}
runningTurnId={paintOnlyDisplayedTimeline ? null : activeRunningTurnId}
turnDiffSummaries={
paintOnlyDisplayedTimeline
? EMPTY_HELD_TURN_DIFF_SUMMARIES
: activeThread.checkpoints
}
activeThreadEnvironmentId={
displayedThreadRef?.environmentId ?? activeThread.environmentId
}
routeThreadKey={displayedTimelineKey}
displayThreadKey={displayedTimelineKey}
onOpenTurnDiff={paintOnlyDisplayedTimeline ? noopHeldTurnDiff : onOpenTurnDiff}
supportsConversationRollback={
!paintOnlyDisplayedTimeline && supportsConversationRollback
}
onRevertToTurnCount={
paintOnlyDisplayedTimeline ? noopHeldRevert : onRevertTimelineTurn
}
isRevertingCheckpoint={!paintOnlyDisplayedTimeline && isRevertingCheckpoint}
onImageExpand={onExpandTimelineImage}
onFileOpen={paintOnlyDisplayedTimeline ? noopHeldAttachment : openFileAttachment}
onFileDownload={
paintOnlyDisplayedTimeline ? noopHeldAttachment : downloadFileAttachment
}
markdownCwd={
paintOnlyDisplayedTimeline
? (heldPaintContext?.markdownCwd ?? undefined)
: (gitCwd ?? undefined)
}
resolvedTheme={resolvedTheme}
timestampFormat={timestampFormat}
workspaceRoot={
paintOnlyDisplayedTimeline
? (heldPaintContext?.workspaceRoot ?? undefined)
: activeWorkspaceRoot
}
skills={
activeProviderStatus
? resolveProviderSkillsForCwd(activeProviderStatus, gitCwd)
: EMPTY_PROVIDER_SKILLS
}
anchorMessageId={paintOnlyDisplayedTimeline ? null : timelineAnchorMessageId}
onAnchorReady={onTimelineAnchorReady}
contentInsetEndAdjustment={composerTimelineInset}
liveFollowEnabled={!paintOnlyDisplayedTimeline && timelineLiveFollowEnabled}
onIsAtEndChange={onIsAtEndChange}
onContentOverflowChange={setTimelineOverflows}
onToolOutputCollapsedAtEnd={onToolOutputCollapsedAtEnd}
onManualNavigation={cancelTimelineLiveFollowForUserNavigation}
hideEmptyPlaceholder={isDraftHeroState || threadDetailLoading}
topFadeEnabled={!hasTimelineTopBanner}
loadEarlier={paintOnlyDisplayedTimeline ? null : loadEarlierTurns}
/>
{/* scroll to end pill — shown when user has scrolled away from the live edge */}
{showScrollToBottom && (
<div
className="pointer-events-none absolute left-1/2 z-30 flex -translate-x-1/2 justify-center py-1.5"
style={{ bottom: scrollToEndClearance + 4 }}
>
<Button
aria-label="Scroll to end"
onPointerDown={(event) => event.preventDefault()}
onClick={() => {
composerRef.current?.restoreAfterTimelineReachedEnd();
scrollToEnd(true);
}}
className="pointer-events-auto gap-1.5 rounded-full px-3 text-muted-foreground hover:text-foreground"
size="xs"
variant="glass"
>
<ChevronDownIcon className="size-3.5" />
Scroll to end
</Button>
</div>
)}
</div>
{/* Input bar — centered hero while a draft has no messages, docked at the bottom otherwise */}
<div
ref={setComposerOverlayElement}
inert={isRevertingCheckpoint}
data-chat-composer-overlay="true"
className={
isDraftHeroState
? "pointer-events-none absolute inset-0 z-20 flex items-center"
: "pointer-events-none absolute inset-x-0 bottom-0 z-20 pt-1.5 sm:pt-2"
}
>
<div
ref={attachDraftHeroTransitionGroupRef}
className="w-full ps-[calc(env(safe-area-inset-left)+0.75rem)] pe-[calc(env(safe-area-inset-right)+0.75rem)] sm:ps-[calc(env(safe-area-inset-left)+1.25rem)] sm:pe-[calc(env(safe-area-inset-right)+1.25rem)]"
>
<div
data-chat-composer-stack="true"
className="group/composer-stack pointer-events-auto relative z-10 mx-auto w-full max-w-3xl"
>
{isDraftHeroState ? (
<div className="absolute inset-x-0 bottom-full z-0">
<div
className="pb-8 group-has-data-[composer-shoulder-tab]/composer-stack:pb-4"
style={
forceExpandedMobileComposer
? {
viewTransitionName: MOBILE_DRAFT_HEADLINE_VIEW_TRANSITION_NAME,
}
: undefined
}
>
<DraftHeroHeadline
draftId={draftId}
activeProjectRef={activeProjectRef}
activeProjectTitle={activeProject?.title ?? null}
/>
</div>
</div>
) : null}
<div
className="relative"
style={
forceExpandedMobileComposer
? { viewTransitionName: MOBILE_COMPOSER_VIEW_TRANSITION_NAME }
: undefined
}
>
<ComposerSurface.Shell contextStrip={showComposerContextStrip}>
<ComposerSurface.Host>
<div ref={attachDraftHeroComposerAnchorRef} className="relative z-10">
<ChatComposer
composerRef={composerRef}
composerDraftTarget={composerDraftTarget}
environmentId={environmentId}
attachmentUploadsCapabilityKnown={attachmentUploadsCapabilityKnown}
supportsAttachmentUploads={supportsAttachmentUploads}
supportsQuestionAttachments={supportsQuestionAttachments}
maxFileAttachmentBytes={maxFileAttachmentBytes}
routeKind={routeKind}
routeThreadRef={routeThreadRef}
draftId={draftId}
activeThreadId={activeThreadId}
activeThreadEnvironmentId={activeThread?.environmentId}
activeThread={activeThread}
activeThreadShell={routeServerThreadShell}
promptHistoryMessages={timelineMessages}
isServerThread={isServerThread}
isLocalDraftThread={isLocalDraftThread}
forceExpandedOnMobile={forceExpandedMobileComposer && isDraftHeroState}
projectSelectionRequired={isLocalDraftThread && activeProject === null}
phase={phase}
isConnecting={isConnecting}
isSendBusy={isSendBusy}
isRevertingCheckpoint={isRevertingCheckpoint}
sendDisabledReason={
isRevertingCheckpoint
? "Rewinding conversation"
: feedbackUploading
? "Sending feedback"
: threadDetailLoading
? "Messages loading"
: null
}
isPreparingWorktree={isPreparingWorktree}
bannerItems={composerBannerItems}
// With attachments or contexts aboard the pick just inserts the
// text, so it sends as a prompt like the typed path would.
onUsageLimitsCommand={
usageLimitsOffered &&
usageLimitsKey !== null &&
!composerHasNonPromptContent
? openUsageLimits
: undefined
}
environmentUnavailable={activeEnvironmentUnavailableState}
activePendingApproval={activePendingApproval}
pendingApprovals={pendingApprovals}
pendingUserInputs={pendingUserInputs}
activePendingProgress={activePendingProgress}
activePendingResolvedAnswers={activePendingResolvedAnswers}
activePendingIsResponding={activePendingIsResponding}
activePendingDraftAnswers={activePendingDraftAnswers}
activePendingQuestionIndex={activePendingQuestionIndex}
respondingRequestIds={respondingRequestIds}
showPlanFollowUpPrompt={showPlanFollowUpPrompt}
activeProposedPlan={activeProposedPlan}
activeTasksProgress={activeComposerTasksProgress}
activeTaskSteps={activeComposerTaskSteps}
threadSyncPhase={activeEnvironmentUnavailable ? null : threadSyncPhase}
runtimeMode={runtimeMode}
interactionMode={interactionMode}
lockedProvider={lockedProvider}
providerStatuses={providerStatuses as ServerProvider[]}
providerCatalogKnown={serverConfig !== null}
activeProjectDefaultModelSelection={activeProjectDefaultModelSelection}
activeThreadModelSelection={activeThread?.modelSelection}
activeContextWindow={activeContextWindow}
compactThreadUnavailable={compactThreadUnavailable}
compactDisabled={compactDisabled}
compactDisabledReason={compactDisabledReason}
resolvedTheme={resolvedTheme}
settings={settings}
keybindings={keybindings}
terminalOpen={Boolean(terminalUiState.terminalOpen)}
gitCwd={gitCwd}
restingControlsHost={restingComposerControlsHost}
restingControlsHaveLeadingContext={
isGitRepo || showComposerEnvironmentIndicator
}
onRestingControlsVisibilityChange={setRestingComposerControlsVisible}
getTimelineScrollableNode={getTimelineScrollableNode}
isTimelineAtLogicalEnd={isTimelineAtLogicalEnd}
timelineOverflows={timelineOverflows}
onComposerOverlayHeightChange={publishComposerOverlayHeight}
onRestingChange={onComposerRestingChange}
promptRef={promptRef}
composerImagesRef={composerImagesRef}
composerFilesRef={composerFilesRef}
composerTerminalContextsRef={composerTerminalContextsRef}
composerElementContextsRef={composerElementContextsRef}
onPageScrollKeyDown={onComposerPageScrollKeyDown}
onPageScrollKeyUp={onComposerPageScrollKeyUp}
onPageScrollRelease={onComposerPageScrollRelease}
onCompactContext={onCompactContext}
onSend={onSend}
onInterrupt={onInterrupt}
onImplementPlanInNewThread={onImplementPlanInNewThread}
onRespondToApproval={onRespondToApproval}
onSelectActivePendingUserInputOption={
onSelectActivePendingUserInputOption
}
onAdvanceActivePendingUserInput={onAdvanceActivePendingUserInput}
onDismissActivePendingUserInput={onDismissUserInput}
onPreviousActivePendingUserInputQuestion={
onPreviousActivePendingUserInputQuestion
}
onChangeActivePendingUserInputCustomAnswer={
onChangeActivePendingUserInputCustomAnswer
}
onProviderModelSelect={onProviderModelSelect}
onOpenProviderSetup={openProviderSetup}
getModelDisabledReason={getModelDisabledReason}
toggleInteractionMode={toggleInteractionMode}
handleRuntimeModeChange={handleRuntimeModeChange}
handleInteractionModeChange={handleInteractionModeChange}
focusComposer={focusComposer}
scheduleComposerFocus={scheduleComposerFocus}
setThreadError={setThreadError}
onExpandImage={onExpandTimelineImage}
onFileOpen={openFileAttachment}
/>
</div>
</ComposerSurface.Host>
<div className="min-h-0">
<div
data-terminal-open={terminalUiState.terminalOpen ? "true" : undefined}
className="relative z-0"
>
{mountComposerContextStrip && (
<div className="pointer-events-auto">
<BranchToolbar
environmentId={activeThread.environmentId}
threadId={activeThread.id}
showGitControls={isGitRepo}
{...(routeKind === "draft" && draftId ? { draftId } : {})}
onEnvModeChange={onEnvModeChange}
startFromOrigin={startFromOrigin}
onStartFromOriginChange={onStartFromOriginChange}
{...(canOverrideServerThreadEnvMode
? { effectiveEnvModeOverride: envMode }
: {})}
{...(canOverrideServerThreadEnvMode
? {
activeThreadBranchOverride: activeThreadBranch,
onActiveThreadBranchOverrideChange:
setPendingServerThreadBranch,
}
: {})}
envLocked={envLocked}
onComposerFocusRequest={scheduleComposerFocus}
{...(canCheckoutPullRequestIntoThread
? { onCheckoutPullRequestRequest: openPullRequestDialog }
: {})}
{...(hasMultipleEnvironments ? { onEnvironmentChange } : {})}
autoEnvironmentLabel={autoEnvironmentLabel}
onAutoEnvironment={
draftId &&
!envLocked &&
hasMultipleEnvironments &&
loadBalancingSettings.loadBalancingEnabled
? onAutoEnvironment
: undefined
}
availableEnvironments={logicalProjectEnvironments}
composerControlsHostRef={setRestingComposerControlsHost}
contextStripVisible={showComposerContextStrip}
/>
</div>
)}
</div>
</div>
</ComposerSurface.Shell>
<div
aria-hidden
className="h-[calc(env(safe-area-inset-bottom)+1rem)] sm:h-[calc(env(safe-area-inset-bottom)+1.25rem)]"
/>
</div>
</div>
</div>
</div>
{activeThreadRef && activePreviewMiniPlayer && previewMiniPlayerVisible ? (
<ThreadPreviewMiniPlayer
key={`${activeThreadKey}:${previewMiniPlayerSourceKey(activePreviewMiniPlayer.source)}`}
threadRef={activeThreadRef}
miniPlayer={activePreviewMiniPlayer}
composerOverlayElement={isDraftHeroState ? null : composerOverlayElement}
/>
) : null}
<AlertDialog open={branchRestoreConfirmOpen} onOpenChange={setBranchRestoreConfirmOpen}>
<AlertDialogPopup>
<AlertDialogHeader>
<AlertDialogTitle>
Switch to{" "}
<code className="font-medium">
{localCheckoutBranchMismatch?.threadBranch ?? ""}
</code>
?
</AlertDialogTitle>
<AlertDialogDescription>
You have uncommitted changes. They'll carry over to the other branch, or block
the switch if they conflict.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
<Button
variant="default"
onClick={() => {
setBranchRestoreConfirmOpen(false);
void handleSwitchCheckoutToThread();
}}
>
Switch branch
</Button>
</AlertDialogFooter>
</AlertDialogPopup>
</AlertDialog>
{pullRequestDialogState ? (
<PullRequestThreadDialog
key={pullRequestDialogState.key}
open
environmentId={activeThread.environmentId}
threadId={activeThread.id}
cwd={activeProject?.workspaceRoot ?? null}
initialReference={pullRequestDialogState.initialReference}
onOpenChange={(open) => {
if (!open) {
closePullRequestDialog();
}
}}
onPrepared={handlePreparedPullRequestThread}
/>
) : null}
</div>
{/* end chat column */}
</div>
{/* end horizontal flex container */}
{mountedTerminalThreadRefs.map(({ key: mountedThreadKey, threadRef: mountedThreadRef }) => (
<PersistentThreadTerminalDrawer
key={mountedThreadKey}
threadRef={mountedThreadRef}
threadId={mountedThreadRef.threadId}
active={mountedThreadKey === activeThreadKey}
launchContext={
mountedThreadKey === activeThreadKey ? (activeTerminalLaunchContext ?? null) : null
}
focusRequestId={mountedThreadKey === activeThreadKey ? terminalFocusRequestId : 0}
splitShortcutLabel={splitTerminalShortcutLabel ?? undefined}
splitVerticalShortcutLabel={splitTerminalVerticalShortcutLabel ?? undefined}
newShortcutLabel={newTerminalShortcutLabel ?? undefined}
closeShortcutLabel={closeTerminalShortcutLabel ?? undefined}
keybindings={keybindings}
onAddTerminalContext={addTerminalContextToDraft}
/>
))}
</div>
{rightPanelPresent && !shouldUseRightPanelSheet && activeThreadRef ? (
<RightPanelTabs
mode="inline"
open={rightPanelOpen}
maximized={rightPanelMaximized}
surfaces={renderedRightPanelSurfaces}
environmentId={activeThreadRef.environmentId}
activeSurfaceId={renderedRightPanelSurface?.id ?? null}
pendingSurfaceIds={pendingFileSurfaceIds}
previewSessions={activePreviewState.sessions}
desktopByTabId={activePreviewState.desktopByTabId}
previewRuntimeTabId={resolvePreviewRuntimeTabId}
terminalLabelsById={activeTerminalLabelsById}
onActivate={activateRightPanelSurface}
onCloseSurface={closeRightPanelSurface}
onRenameDevice={(surfaceId, title) => {
if (activeThreadRef)
useRightPanelStore.getState().renameDevice(activeThreadRef, surfaceId, title);
}}
onCloseOtherSurfaces={closeOtherRightPanelSurfaces}
onCloseSurfacesToRight={closeRightPanelSurfacesToRight}
onCloseAllSurfaces={closeAllRightPanelSurfaces}
onCopyFilePath={copyRightPanelFilePath}
onAddBrowser={() => createBrowserSurface()}
onAddBrowserInProfile={createBrowserSurface}
onAddTerminal={addTerminalSurface}
onAddDiff={addDiffSurface}
onAddFiles={addFilesSurface}
onAddPullRequest={addPullRequestSurface}
onAddPullRequests={addPullRequestsSurface}
onAddAgents={addAgentsSurface}
onAddDevice={addDeviceSurface}
browserAvailable={isPreviewSupportedInRuntime()}
terminalAvailable={activeProject !== null}
diffAvailable={isServerThread && isGitRepo}
filesAvailable={activeProject !== null}
pullRequestAvailable={pullRequestSurfaceAvailable}
pullRequestsAvailable={pullRequestsSurfaceAvailable}
agentsAvailable
deviceAvailable={activeThreadRef !== null}
liveAgentCount={agentPanelModel.liveCount}
>
{rightPanelContent}
</RightPanelTabs>
) : null}
{rightPanelPresent && shouldUseRightPanelSheet && activeThreadRef ? (
<RightPanelSheet
animationDurationMs={panelAnimationsActive ? panelAnimationDurationMs : 0}
open={rightPanelOpen}
underFloatingPreview={previewMiniPlayerVisible}
onClose={closePreviewPanel}
>
<RightPanelTabs
mode="sheet"
// Same effective inset as the closed-state titlebar controls
// (pr-3 in the tab bar plus this pixel equals the absolute
// right inset plus mr-px), so the cluster does not creep when
// the sheet opens.
layoutControls={
rightPanelOpen ? (
<div className="mr-px flex items-center">{panelToggleControls}</div>
) : null
}
surfaces={renderedRightPanelSurfaces}
environmentId={activeThreadRef.environmentId}
activeSurfaceId={renderedRightPanelSurface?.id ?? null}
pendingSurfaceIds={pendingFileSurfaceIds}
previewSessions={activePreviewState.sessions}
desktopByTabId={activePreviewState.desktopByTabId}
previewRuntimeTabId={resolvePreviewRuntimeTabId}
terminalLabelsById={activeTerminalLabelsById}
onActivate={activateRightPanelSurface}
onCloseSurface={closeRightPanelSurface}
onRenameDevice={(surfaceId, title) => {
if (activeThreadRef)
useRightPanelStore.getState().renameDevice(activeThreadRef, surfaceId, title);
}}
onCloseOtherSurfaces={closeOtherRightPanelSurfaces}
onCloseSurfacesToRight={closeRightPanelSurfacesToRight}
onCloseAllSurfaces={closeAllRightPanelSurfaces}
onCopyFilePath={copyRightPanelFilePath}
onAddBrowser={() => createBrowserSurface()}
onAddBrowserInProfile={createBrowserSurface}
onAddTerminal={addTerminalSurface}
onAddDiff={addDiffSurface}
onAddFiles={addFilesSurface}
onAddPullRequest={addPullRequestSurface}
onAddPullRequests={addPullRequestsSurface}
onAddAgents={addAgentsSurface}
onAddDevice={addDeviceSurface}
browserAvailable={isPreviewSupportedInRuntime()}
terminalAvailable={activeProject !== null}
diffAvailable={isServerThread && isGitRepo}
filesAvailable={activeProject !== null}
pullRequestAvailable={pullRequestSurfaceAvailable}
pullRequestsAvailable={pullRequestsSurfaceAvailable}
agentsAvailable
deviceAvailable={activeThreadRef !== null}
liveAgentCount={agentPanelModel.liveCount}
>
{rightPanelContent}
</RightPanelTabs>
</RightPanelSheet>
) : null}
<AlertDialog
open={pendingRevert !== null && pendingRevert.routeThreadKey === routeThreadKey}
onOpenChange={(open) => {
if (!open) setPendingRevert(null);
}}
>
<AlertDialogPopup>
<AlertDialogHeader>
<AlertDialogTitle>Edit from here?</AlertDialogTitle>
<AlertDialogDescription>
Rewind chat to before this message. Your prompt and attachments return to the
composer.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
<Button
variant="destructive"
onClick={() => {
if (!pendingRevert || pendingRevert.routeThreadKey !== routeThreadKey) return;
setPendingRevert(null);
void onRevertToTurnCount(pendingRevert.turnCount, pendingRevert.messageId, true);
}}
>
Revert files too
</Button>
<Button
onClick={() => {
if (!pendingRevert || pendingRevert.routeThreadKey !== routeThreadKey) return;
setPendingRevert(null);
void onRevertToTurnCount(pendingRevert.turnCount, pendingRevert.messageId, false);
}}
>
Revert and keep changes
</Button>
</AlertDialogFooter>
</AlertDialogPopup>
</AlertDialog>
<LinkPullRequestDialogHost />
{expandedImage && (
<ExpandedImageDialog
key={expandedImageKey(expandedImage)}
preview={expandedImage}
onClose={closeExpandedImage}
/>
)}
</div>
);
}