7822 lines
291 KiB
TypeScript
7822 lines
291 KiB
TypeScript
import {
|
|
type AssistantCitation,
|
|
type ApprovalRequestId,
|
|
type ChatFileAttachment,
|
|
DEFAULT_MODEL,
|
|
defaultInstanceIdForDriver,
|
|
type EnvironmentId,
|
|
type MessageId,
|
|
type ModelSelection,
|
|
type ProjectScript,
|
|
type ProjectId,
|
|
type ProviderApprovalDecision,
|
|
type PreviewAnnotationPayload,
|
|
ProviderInstanceId,
|
|
type ServerProvider,
|
|
type ResolvedKeybindingsConfig,
|
|
type ScopedThreadRef,
|
|
type ThreadId,
|
|
type TurnId,
|
|
type KeybindingCommand,
|
|
OrchestrationThreadActivity,
|
|
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
|
|
ProviderInteractionMode,
|
|
ProviderDriverKind,
|
|
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 {
|
|
codexFeedbackMessage,
|
|
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 } from "@t3tools/shared/projectScripts";
|
|
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,
|
|
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 {
|
|
derivePendingApprovals,
|
|
derivePendingUserInputs,
|
|
derivePhase,
|
|
deriveTimelineEntries,
|
|
deriveActiveWorkStartedAt,
|
|
deriveActivePlanState,
|
|
findLatestProposedPlan,
|
|
deriveWorkLogEntries,
|
|
hasActionableProposedPlan,
|
|
isLatestTurnSettled,
|
|
} from "../session-logic";
|
|
import { type LegendListRef } from "@legendapp/list/react";
|
|
import {
|
|
CHAT_TIMELINE_ANCHOR_OFFSET,
|
|
getAnchoredTurnMetrics,
|
|
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_RUNTIME_MODE,
|
|
DEFAULT_THREAD_TERMINAL_ID,
|
|
MAX_TERMINALS_PER_GROUP,
|
|
type ChatMessage,
|
|
isImageAttachment,
|
|
videoMimeType,
|
|
type SessionPhase,
|
|
type Thread,
|
|
type TurnDiffSummary,
|
|
} from "../types";
|
|
import { useTheme } from "../hooks/useTheme";
|
|
import { writeTextToClipboard } from "../hooks/useCopyToClipboard";
|
|
import { useTurnDiffSummaries } from "../hooks/useTurnDiffSummaries";
|
|
import { isCommandPaletteOpen } from "../commandPaletteBus";
|
|
import { buildTemporaryWorktreeBranchName } from "@t3tools/shared/git";
|
|
import { useMediaQuery } from "../hooks/useMediaQuery";
|
|
import { RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY } from "../rightPanelLayout";
|
|
import {
|
|
selectActiveRightPanel,
|
|
selectActiveRightPanelSurface,
|
|
selectThreadRightPanelState,
|
|
type RightPanelSurface,
|
|
updatePullRequestTabStatus,
|
|
useRightPanelStore,
|
|
} from "../rightPanelStore";
|
|
import {
|
|
isPreviewSupportedInRuntime,
|
|
setActivePreviewTab,
|
|
useThreadPreviewState,
|
|
} from "../previewStateStore";
|
|
import { previewRuntimeTabId } from "../browser/previewRuntimeTabId";
|
|
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 {
|
|
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, type PullRequestTabStatus } from "./RightPanelTabs";
|
|
import { AgentsPanel } from "./AgentsPanel";
|
|
import {
|
|
deriveAgentPanelModel,
|
|
foldSubagentActivities,
|
|
} from "@t3tools/client-runtime/state/subagentRuntime";
|
|
import { DiffWorkerPoolProvider } from "./DiffWorkerPoolProvider";
|
|
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 } 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, resolveSelectableProvider } from "../providerModels";
|
|
import {
|
|
applyProviderInstanceSettings,
|
|
deriveProviderInstanceEntries,
|
|
NO_PROVIDER_MODEL_SELECTION,
|
|
} 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 { useThreadActions } from "../hooks/useThreadActions";
|
|
import { resolveAppModelSelectionForInstance } from "../modelSelection";
|
|
import { confirmTerminalClose, isTerminalCloseConfirmPending } from "../lib/terminalCloseConfirm";
|
|
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,
|
|
type 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 { projectEnvironment } from "../state/projects";
|
|
import { linkedPullRequestDetailAtom } from "../state/pullRequests";
|
|
import { useEnvironmentQuery } from "../state/query";
|
|
import {
|
|
environmentServerConfigsAtom,
|
|
primaryServerAvailableEditorsAtom,
|
|
primaryServerKeybindingsAtom,
|
|
primaryServerSettingsAtom,
|
|
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 { 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 { ChatHeader } from "./chat/ChatHeader";
|
|
import { PanelLayoutControls, RightPanelMaximizeControl } from "./chat/PanelLayoutControls";
|
|
import { type ExpandedImagePreview } from "./chat/ExpandedImagePreview";
|
|
import { NoActiveThreadState } from "./NoActiveThreadState";
|
|
import { WorkspacePageHeader } from "./WorkspacePageHeader";
|
|
import {
|
|
resolveEffectiveEnvMode,
|
|
resolveLocalCheckoutBranchMismatch,
|
|
shouldShowComposerContextStrip,
|
|
shouldShowEnvironmentIndicator,
|
|
} from "./BranchToolbar.logic";
|
|
import {
|
|
getProviderStatusBannerKey,
|
|
ProviderStatusBanner,
|
|
shouldShowProviderStatusBanner,
|
|
} from "./chat/ProviderStatusBanner";
|
|
import {
|
|
dismissThreadErrorBannerForSession,
|
|
getThreadErrorBannerKey,
|
|
isThreadErrorBannerDismissedForSession,
|
|
shouldShowThreadErrorBanner,
|
|
ThreadErrorBanner,
|
|
} from "./chat/ThreadErrorBanner";
|
|
import {
|
|
resolveDisplayedThreadPr,
|
|
threadPullRequestRefreshSource,
|
|
threadChangeRequestSnapshotsAtom,
|
|
useLinkedThreadPullRequest,
|
|
} from "./ThreadStatusIndicators";
|
|
import type { ComposerBannerStackItem } from "./chat/ComposerBannerStack";
|
|
import { ComposerSurface } from "./chat/ComposerSurface";
|
|
import {
|
|
hasAvailableClaudeCompactionProvider,
|
|
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,
|
|
buildThreadTurnInterruptInput,
|
|
collectUserMessageBlobPreviewUrls,
|
|
createLocalDispatchSnapshot,
|
|
deriveComposerSendState,
|
|
dismissBranchMismatchForSession,
|
|
hasEnvironmentReconnectWarningGraceElapsed,
|
|
scheduleEnvironmentReconnectWarning,
|
|
hasServerAcknowledgedLocalDispatch,
|
|
isBranchMismatchDismissedForSession,
|
|
shouldDockDraftHeroForSubmission,
|
|
shouldReleaseTimelineAnchorForToolActivity,
|
|
shouldShowBranchMismatchBanner,
|
|
shouldShowPlanFollowUpPrompt,
|
|
getStartedThreadModelChangeBlockReason,
|
|
LAST_INVOKED_SCRIPT_BY_PROJECT_KEY,
|
|
LastInvokedScriptByProjectSchema,
|
|
type LocalDispatchSnapshot,
|
|
PullRequestDialogState,
|
|
cloneComposerImageForRetry,
|
|
deriveLockedProvider,
|
|
readFileAsDataUrl,
|
|
resolveFileAttachmentUrl,
|
|
isVideoPreviewRequestCurrent,
|
|
reconcileMountedTerminalThreadIds,
|
|
resolveBackgroundDraftWorkspaceOptions,
|
|
resolveDraftHeroState,
|
|
resolveThreadMetadataUpdateForNextTurn,
|
|
resolveSendEnvMode,
|
|
revokeBlobPreviewUrl,
|
|
revokeUserMessagePreviewUrls,
|
|
shouldWriteThreadErrorToCurrentServerThread,
|
|
startNewThreadForProject,
|
|
codexArtifactTemplatePromptToAppend,
|
|
toolGroupConsumesUpwardNavigation,
|
|
waitForStartedServerThread,
|
|
} 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 {
|
|
ComposerServerUpdateIcon,
|
|
ComposerServerUpdateStatus,
|
|
} from "./chat/ComposerServerUpdateStatus";
|
|
import {
|
|
buildVersionMismatchDismissalKey,
|
|
dismissServerUpdateFailure,
|
|
dismissVersionMismatch,
|
|
isServerUpdateFailureDismissed,
|
|
isVersionMismatchDismissed,
|
|
resolveServerConfigVersionMismatch,
|
|
resolveServerSelfUpdateCapability,
|
|
serverUpdateGuidance,
|
|
supportsDesktopAppUpdate,
|
|
supportsServerUpdateThreadContinuation,
|
|
} from "../versionSkew";
|
|
import { useAssetUrls } from "../assets/assetUrls";
|
|
|
|
const ATTACHMENT_ONLY_BOOTSTRAP_PROMPT =
|
|
"[User attached one or more files without additional text. Respond using the conversation context and the attached files.]";
|
|
const EMPTY_ACTIVITIES: OrchestrationThreadActivity[] = [];
|
|
const EMPTY_PROVIDERS: ServerProvider[] = [];
|
|
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 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 = [
|
|
'[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));
|
|
}
|
|
|
|
function shouldTypeToFocusComposer(event: KeyboardEvent): boolean {
|
|
if (event.defaultPrevented || event.isComposing) return false;
|
|
if (event.metaKey || event.ctrlKey || event.altKey) return false;
|
|
if (event.key.length !== 1) 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;
|
|
|
|
// 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;
|
|
}
|
|
|
|
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;
|
|
|
|
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 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,
|
|
threadError: input.threadError,
|
|
}),
|
|
[
|
|
input.activeLatestTurn,
|
|
input.activePendingApproval,
|
|
input.activePendingUserInput,
|
|
input.activeThread?.session,
|
|
input.phase,
|
|
input.threadError,
|
|
latestUserMessageId,
|
|
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 {
|
|
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({
|
|
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"
|
|
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.";
|
|
}
|
|
|
|
/**
|
|
* 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 };
|
|
}
|
|
|
|
function ChatViewContent(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 updateProject = useAtomCommand(projectEnvironment.update, { 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 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);
|
|
// New-thread defaults live in the primary environment's settings.json (the
|
|
// settings UI never writes to remote environments), so read them from the
|
|
// primary server rather than the thread's environment.
|
|
const primaryServerSettings = useAtomValue(primaryServerSettingsAtom);
|
|
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;
|
|
});
|
|
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 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 routeThreadKeyRef = useRef(routeThreadKey);
|
|
routeThreadKeyRef.current = routeThreadKey;
|
|
const videoPreviewRequestIdRef = useRef(0);
|
|
const cancelVideoPreviewRequest = useCallback(() => {
|
|
videoPreviewRequestIdRef.current += 1;
|
|
}, []);
|
|
const [openingVideoAttachmentId, setOpeningVideoAttachmentId] = useState<string | null>(null);
|
|
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.startsWith("blob:")) return;
|
|
return () => revokeBlobPreviewUrl(item.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, setIsRevertingCheckpoint] = useState(false);
|
|
const [maximizedRightPanelThreadKey, setMaximizedRightPanelThreadKey] = useState<string | null>(
|
|
null,
|
|
);
|
|
const [respondingRequestIds, setRespondingRequestIds] = useState<ApprovalRequestId[]>([]);
|
|
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 [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 [composerOverlayElement, setComposerOverlayElement] = useState<HTMLDivElement | null>(null);
|
|
const [composerOverlayHeight, setComposerOverlayHeight] = useState(0);
|
|
const [scrollToEndClearance, setScrollToEndClearance] = useState(0);
|
|
const isAtEndRef = useRef(true);
|
|
const attachmentPreviewHandoffByMessageIdRef = useRef<Record<string, string[]>>({});
|
|
const attachmentPreviewPromotionInFlightByMessageIdRef = useRef<Record<string, true>>({});
|
|
const sendInFlightRef = useRef(false);
|
|
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,
|
|
fallbackDraftProject?.defaultModelSelection ?? NO_PROVIDER_MODEL_SELECTION,
|
|
)
|
|
: undefined,
|
|
[draftThread, fallbackDraftProject?.defaultModelSelection, 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 runtimeMode = composerRuntimeMode ?? activeThread?.runtimeMode ?? DEFAULT_RUNTIME_MODE;
|
|
// Plan mode is legacy (Settings → Beta). With the flag off the effective
|
|
// mode is forced to "default" — even for threads with a stored plan mode —
|
|
// so nobody is trapped in plan mode while its toggle is hidden. The next
|
|
// send persists "default" back to the thread.
|
|
const interactionMode = settings.planModeEnabled
|
|
? (composerInteractionMode ?? activeThread?.interactionMode ?? DEFAULT_INTERACTION_MODE)
|
|
: DEFAULT_INTERACTION_MODE;
|
|
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 changeRequestSnapshotByKey = useAtomValue(threadChangeRequestSnapshotsAtom);
|
|
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 [pullRequestTabStatuses, setPullRequestTabStatuses] = useState<
|
|
Record<string, PullRequestTabStatus>
|
|
>({});
|
|
// Keyed by the surface the panel is showing rather than by a key rebuilt from the status, so
|
|
// the tab is found again whether or not that surface was opened with an environment on it.
|
|
const activePullRequestSurfaceId =
|
|
activeRightPanelSurface?.kind === "pull-request" ? activeRightPanelSurface.id : undefined;
|
|
const updatePullRequestTabStatusFromPanel = useCallback(
|
|
(status: PullRequestTabStatus) => {
|
|
const id = activePullRequestSurfaceId;
|
|
if (id === undefined) return;
|
|
setPullRequestTabStatuses((current) => updatePullRequestTabStatus(current, id, status));
|
|
},
|
|
[activePullRequestSurfaceId],
|
|
);
|
|
const refreshVcsStatus = useAtomCommand(vcsEnvironment.refreshStatus, { reportFailure: false });
|
|
const sidebarPrRefreshKeyRef = useRef<string | null>(null);
|
|
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 =
|
|
rightPanelPresent && (!shouldUseRightPanelSheet || rightPanelOpen);
|
|
const renderedRightPanelSurface = rightPanelPresence.value?.activeSurface ?? null;
|
|
const renderedRightPanelSurfaces = rightPanelPresence.value?.surfaces ?? [];
|
|
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) return;
|
|
const miniTabStillExists = Boolean(activePreviewState.sessions[activePreviewMiniPlayer.tabId]);
|
|
const sameTabOpenInPanel =
|
|
previewPanelOpen &&
|
|
activeRightPanelSurface?.kind === "preview" &&
|
|
activeRightPanelSurface.resourceId === activePreviewMiniPlayer.tabId;
|
|
if (!miniTabStillExists || sameTabOpenInPanel) {
|
|
usePreviewMiniPlayerStore.getState().close(activeThreadRef);
|
|
}
|
|
}, [
|
|
activePreviewMiniPlayer,
|
|
activePreviewState.sessions,
|
|
activeRightPanelSurface,
|
|
activeThreadRef,
|
|
previewPanelOpen,
|
|
]);
|
|
|
|
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);
|
|
const handleNewThreadInActiveProject = useCallback(() => {
|
|
startNewThreadForProject(activeProjectRef, handleNewThread);
|
|
}, [activeProjectRef, handleNewThread]);
|
|
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 projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);
|
|
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(activeProject?.scripts),
|
|
[activeProject?.scripts],
|
|
);
|
|
|
|
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: Array<{
|
|
environmentId: EnvironmentId;
|
|
projectId: ProjectId;
|
|
label: string;
|
|
isPrimary: boolean;
|
|
}> = [];
|
|
for (const p of memberProjects) {
|
|
if (seen.has(p.environmentId)) continue;
|
|
seen.add(p.environmentId);
|
|
const isPrimary = p.environmentId === primaryEnvironmentId;
|
|
const label = environmentById.get(p.environmentId)?.label ?? p.environmentId;
|
|
envs.push({
|
|
environmentId: p.environmentId,
|
|
projectId: p.id,
|
|
label,
|
|
isPrimary,
|
|
});
|
|
}
|
|
// 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: DEFAULT_RUNTIME_MODE,
|
|
interactionMode: DEFAULT_INTERACTION_MODE,
|
|
...input,
|
|
});
|
|
await navigate({
|
|
to: "/draft/$draftId",
|
|
params: buildDraftThreadRouteParams(nextDraftId),
|
|
});
|
|
return nextThreadId;
|
|
},
|
|
[
|
|
activeProject,
|
|
draftId,
|
|
getDraftSession,
|
|
getDraftSessionByLogicalProjectKey,
|
|
isServerThread,
|
|
navigate,
|
|
projectGroupingSettings,
|
|
routeKind,
|
|
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],
|
|
);
|
|
|
|
const selectedProviderByThreadId = composerActiveProvider ?? null;
|
|
const threadProvider =
|
|
activeThread?.modelSelection.instanceId ??
|
|
activeProject?.defaultModelSelection?.instanceId ??
|
|
null;
|
|
const lockedProvider = deriveLockedProvider({
|
|
thread: activeThread,
|
|
selectedProvider: selectedProviderByThreadId,
|
|
threadProvider,
|
|
});
|
|
// 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 pullRequestsCapabilityKnown = serverConfig !== null;
|
|
const supportsPullRequests = serverConfig?.environment.capabilities.pullRequests === true;
|
|
const attachmentEnvironmentConfig = environmentById.get(environmentId)?.serverConfig ?? null;
|
|
const attachmentUploadsCapabilityKnown = attachmentEnvironmentConfig !== null;
|
|
const supportsAttachmentUploads =
|
|
attachmentEnvironmentConfig?.environment.capabilities.attachmentUploads === true;
|
|
const advertisedFileAttachmentBytes =
|
|
attachmentEnvironmentConfig?.environment.capabilities.fileAttachments?.maxUploadBytes ?? null;
|
|
const maxFileAttachmentBytes =
|
|
advertisedFileAttachmentBytes === null
|
|
? null
|
|
: clampFileAttachmentUploadBytes(advertisedFileAttachmentBytes);
|
|
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 (
|
|
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="cursor-help 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);
|
|
},
|
|
}),
|
|
});
|
|
}
|
|
return items;
|
|
}, [
|
|
activeEnvironmentUnavailableState,
|
|
reconnectWarningGraceElapsed,
|
|
handleReconnectActiveEnvironment,
|
|
navigate,
|
|
setDismissedVersionMismatchKey,
|
|
showVersionMismatchBanner,
|
|
serverUpdateFailureDismissed,
|
|
serverUpdateState,
|
|
versionMismatch,
|
|
versionMismatchDismissKey,
|
|
serverUpdateEnvironmentId,
|
|
versionMismatchSelfUpdate,
|
|
versionMismatchDesktopAppUpdate,
|
|
versionMismatchThreadContinuation,
|
|
versionMismatchServerLabel,
|
|
]);
|
|
const providerStatuses = serverConfig?.providers ?? EMPTY_PROVIDERS;
|
|
const unlockedSelectedProvider = resolveSelectableProvider(
|
|
providerStatuses,
|
|
selectedProviderByThreadId ?? threadProvider,
|
|
);
|
|
const selectedProvider: ProviderDriverKind = lockedProvider ?? unlockedSelectedProvider;
|
|
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 pendingApprovals = useMemo(
|
|
() => derivePendingApprovals(threadActivities),
|
|
[threadActivities],
|
|
);
|
|
const pendingUserInputs = useMemo(
|
|
() => derivePendingUserInputs(threadActivities),
|
|
[threadActivities],
|
|
);
|
|
const activePendingUserInput = pendingUserInputs[0] ?? null;
|
|
const activePendingDraftAnswers = useMemo(
|
|
() =>
|
|
activePendingUserInput
|
|
? (pendingUserInputAnswersByRequestId[activePendingUserInput.requestId] ??
|
|
EMPTY_PENDING_USER_INPUT_ANSWERS)
|
|
: EMPTY_PENDING_USER_INPUT_ANSWERS,
|
|
[activePendingUserInput, pendingUserInputAnswersByRequestId],
|
|
);
|
|
const activePendingQuestionIndex = activePendingUserInput
|
|
? (pendingUserInputQuestionIndexByRequestId[activePendingUserInput.requestId] ?? 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;
|
|
const {
|
|
beginLocalDispatch,
|
|
resetLocalDispatch,
|
|
localDispatchStartedAt,
|
|
latestUserMessageAt,
|
|
isPreparingWorktree,
|
|
isSendBusy,
|
|
backgroundSubmissionPending,
|
|
} = useLocalDispatchState({
|
|
activeThread,
|
|
activeLatestTurn,
|
|
phase,
|
|
activePendingApproval: activePendingApproval?.requestId ?? null,
|
|
activePendingUserInput: activePendingUserInput?.requestId ?? null,
|
|
threadError,
|
|
});
|
|
const isWorking = phase === "running" || isSendBusy || isConnecting || isRevertingCheckpoint;
|
|
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();
|
|
cancelVideoPreviewRequest();
|
|
for (const message of optimisticUserMessagesRef.current) {
|
|
revokeUserMessagePreviewUrls(message);
|
|
}
|
|
};
|
|
}, [cancelVideoPreviewRequest, 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 openFileAttachment = useCallback(
|
|
async (attachment: ChatFileAttachment) => {
|
|
const connection = readPreparedConnection(environmentId);
|
|
if (!connection) {
|
|
toastManager.add({ type: "error", title: "The environment is not connected." });
|
|
return;
|
|
}
|
|
const isVideo = videoMimeType(attachment) !== null;
|
|
const action = isVideo ? "play" : "download";
|
|
const videoPreviewRequestId = isVideo ? ++videoPreviewRequestIdRef.current : 0;
|
|
const isCurrentRequest = () =>
|
|
!isVideo ||
|
|
isVideoPreviewRequestCurrent(
|
|
routeThreadKey,
|
|
routeThreadKeyRef.current,
|
|
videoPreviewRequestId,
|
|
videoPreviewRequestIdRef.current,
|
|
);
|
|
if (isVideo) setOpeningVideoAttachmentId(attachment.id);
|
|
|
|
try {
|
|
const url = await resolveFileAttachmentUrl({
|
|
attachment,
|
|
environmentId,
|
|
httpBaseUrl: connection.httpBaseUrl,
|
|
createAssetUrl: createAttachmentAssetUrl,
|
|
});
|
|
if (!isCurrentRequest()) return;
|
|
if (isVideo) {
|
|
setExpandedImage({
|
|
images: [{ src: url, name: attachment.name, type: "video" }],
|
|
index: 0,
|
|
});
|
|
return;
|
|
}
|
|
const anchor = document.createElement("a");
|
|
anchor.href = url;
|
|
anchor.download = attachment.name;
|
|
anchor.click();
|
|
} catch (error) {
|
|
if (!isCurrentRequest()) return;
|
|
toastManager.add({
|
|
type: "error",
|
|
title: "Could not " + action + " " + attachment.name,
|
|
description: error instanceof Error ? error.message : "The attachment is unavailable.",
|
|
});
|
|
} finally {
|
|
if (isVideo && isCurrentRequest()) setOpeningVideoAttachmentId(null);
|
|
}
|
|
},
|
|
[createAttachmentAssetUrl, environmentId, routeThreadKey],
|
|
);
|
|
const serverAttachmentIds = useMemo(() => {
|
|
const attachmentIds = new Set<string>();
|
|
for (const message of serverMessages ?? []) {
|
|
for (const attachment of message.attachments ?? []) {
|
|
if (isImageAttachment(attachment)) {
|
|
attachmentIds.add(attachment.id);
|
|
}
|
|
}
|
|
}
|
|
return [...attachmentIds];
|
|
}, [serverMessages]);
|
|
const serverAttachmentResources = useMemo(
|
|
() =>
|
|
serverAttachmentIds.map((attachmentId) => ({
|
|
_tag: "attachment" as const,
|
|
attachmentId,
|
|
})),
|
|
[serverAttachmentIds],
|
|
);
|
|
const serverAttachmentUrls = useAssetUrls(environmentId, serverAttachmentResources);
|
|
const serverAttachmentUrlById = useMemo(
|
|
() =>
|
|
new Map(
|
|
serverAttachmentIds.flatMap((attachmentId, index) => {
|
|
const url = serverAttachmentUrls[index];
|
|
return url ? [[attachmentId, url] as const] : [];
|
|
}),
|
|
),
|
|
[serverAttachmentIds, serverAttachmentUrls],
|
|
);
|
|
const displayServerMessages = useMemo<ReadonlyArray<ChatMessage>>(() => {
|
|
if (!serverMessages) return [];
|
|
return serverMessages.map((message) => {
|
|
if (!message.attachments || message.attachments.length === 0) {
|
|
return message;
|
|
}
|
|
return {
|
|
...message,
|
|
attachments: message.attachments.map((attachment) => {
|
|
const previewUrl = serverAttachmentUrlById.get(attachment.id);
|
|
return previewUrl ? { ...attachment, previewUrl } : attachment;
|
|
}),
|
|
};
|
|
});
|
|
}, [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
|
|
: // Spread only fires for the few messages that actually changed;
|
|
// unchanged ones early-return their original reference.
|
|
// In-place mutation would break React's immutable state contract.
|
|
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 changed = false;
|
|
let imageIndex = 0;
|
|
const attachments = message.attachments.map((attachment) => {
|
|
if (!isImageAttachment(attachment)) {
|
|
return attachment;
|
|
}
|
|
const handoffPreviewUrl = handoffPreviewUrls[imageIndex];
|
|
imageIndex += 1;
|
|
if (!handoffPreviewUrl || attachment.previewUrl === handoffPreviewUrl) {
|
|
return attachment;
|
|
}
|
|
changed = true;
|
|
return {
|
|
...attachment,
|
|
previewUrl: handoffPreviewUrl,
|
|
};
|
|
});
|
|
|
|
return changed ? { ...message, attachments } : message;
|
|
});
|
|
|
|
const localMessages = [
|
|
...optimisticUserMessages,
|
|
...feedbackSubmissions.flatMap((submission) =>
|
|
submission.status === "interrupted"
|
|
? []
|
|
: [codexFeedbackMessage(submission), codexFeedbackMessage(submission, "assistant")],
|
|
),
|
|
];
|
|
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,
|
|
feedbackSubmissions,
|
|
optimisticUserMessages,
|
|
]);
|
|
const timelineEntries = useMemo(
|
|
() =>
|
|
deriveTimelineEntries(timelineMessages, activeThread?.proposedPlans ?? [], workLogEntries),
|
|
[activeThread?.proposedPlans, timelineMessages, workLogEntries],
|
|
);
|
|
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 { turnDiffSummaries, inferredCheckpointTurnCountByTurnId } =
|
|
useTurnDiffSummaries(activeThread);
|
|
const turnDiffSummaryByAssistantMessageId = useMemo(() => {
|
|
const byMessageId = new Map<MessageId, TurnDiffSummary>();
|
|
for (const summary of turnDiffSummaries) {
|
|
if (!summary.assistantMessageId) continue;
|
|
byMessageId.set(summary.assistantMessageId, summary);
|
|
}
|
|
return byMessageId;
|
|
}, [turnDiffSummaries]);
|
|
const revertTurnCountByUserMessageId = useMemo(() => {
|
|
const byUserMessageId = new Map<MessageId, number>();
|
|
for (let index = 0; index < timelineEntries.length; index += 1) {
|
|
const entry = timelineEntries[index];
|
|
if (!entry || entry.kind !== "message" || entry.message.role !== "user") {
|
|
continue;
|
|
}
|
|
|
|
for (let nextIndex = index + 1; nextIndex < timelineEntries.length; nextIndex += 1) {
|
|
const nextEntry = timelineEntries[nextIndex];
|
|
if (!nextEntry || nextEntry.kind !== "message") {
|
|
continue;
|
|
}
|
|
if (nextEntry.message.role === "user") {
|
|
break;
|
|
}
|
|
const summary = turnDiffSummaryByAssistantMessageId.get(nextEntry.message.id);
|
|
if (!summary) {
|
|
continue;
|
|
}
|
|
const turnCount =
|
|
summary.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[summary.turnId];
|
|
if (typeof turnCount !== "number") {
|
|
break;
|
|
}
|
|
byUserMessageId.set(entry.message.id, Math.max(0, turnCount - 1));
|
|
break;
|
|
}
|
|
}
|
|
|
|
return byUserMessageId;
|
|
}, [inferredCheckpointTurnCountByTurnId, timelineEntries, turnDiffSummaryByAssistantMessageId]);
|
|
|
|
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);
|
|
// Prefer an instance-id match so a custom Codex instance (e.g.
|
|
// `codex_personal`) surfaces its own status/message in the banner rather
|
|
// than the default Codex's. Falls back to first-match-by-kind when no
|
|
// saved instance id is available or the instance no longer exists.
|
|
const selectedProviderInstanceId =
|
|
providerStatuses.find((status) => status.instanceId === selectedProviderByThreadId)
|
|
?.instanceId ?? null;
|
|
const activeProviderInstanceId =
|
|
selectedProviderInstanceId ??
|
|
activeThread?.session?.providerInstanceId ??
|
|
activeThread?.modelSelection.instanceId ??
|
|
activeProject?.defaultModelSelection?.instanceId ??
|
|
null;
|
|
const compactionProviderAvailable = useMemo(
|
|
() =>
|
|
hasAvailableClaudeCompactionProvider({
|
|
providers: applyProviderInstanceSettings(
|
|
deriveProviderInstanceEntries(providerStatuses),
|
|
settings,
|
|
),
|
|
instanceId: activeProviderInstanceId,
|
|
lockedInstanceId: lockedProvider
|
|
? (activeThread?.session?.providerInstanceId ??
|
|
activeThread?.modelSelection.instanceId ??
|
|
null)
|
|
: null,
|
|
}),
|
|
[
|
|
activeProviderInstanceId,
|
|
activeThread?.modelSelection.instanceId,
|
|
activeThread?.session?.providerInstanceId,
|
|
lockedProvider,
|
|
providerStatuses,
|
|
settings,
|
|
],
|
|
);
|
|
const activeProviderStatus = useMemo(() => {
|
|
if (activeProviderInstanceId) {
|
|
return (
|
|
providerStatuses.find((status) => status.instanceId === activeProviderInstanceId) ?? null
|
|
);
|
|
}
|
|
const defaultInstanceId = defaultInstanceIdForDriver(selectedProvider);
|
|
return providerStatuses.find((status) => status.instanceId === defaultInstanceId) ?? null;
|
|
}, [activeProviderInstanceId, providerStatuses, 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;
|
|
const activeTerminalLaunchContext =
|
|
terminalUiLaunchContext?.threadId === activeThreadId ? terminalUiLaunchContext : null;
|
|
// Default true while loading to avoid toolbar flicker.
|
|
const isGitRepo = gitStatusQuery.data?.isRepo ?? true;
|
|
const showComposerContextStrip = shouldShowComposerContextStrip({
|
|
hasActiveProject: activeProject !== null,
|
|
isGitRepo,
|
|
showEnvironmentIndicator: showComposerEnvironmentIndicator,
|
|
});
|
|
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 envLocked = Boolean(
|
|
activeThread &&
|
|
(activeThread.messages.length > 0 ||
|
|
(activeThread.session !== null && activeThread.session.status !== "stopped")),
|
|
);
|
|
|
|
// 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),
|
|
});
|
|
},
|
|
[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 focusComposer = useCallback(() => {
|
|
composerRef.current?.focusAtEnd();
|
|
}, [composerRef]);
|
|
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 persistProjectScripts = useCallback(
|
|
async (input: {
|
|
projectId: ProjectId;
|
|
projectCwd: string;
|
|
previousScripts: ReadonlyArray<ProjectScript>;
|
|
nextScripts: ReadonlyArray<ProjectScript>;
|
|
keybinding?: string | null;
|
|
keybindingCommand: KeybindingCommand;
|
|
}): Promise<AtomCommandResult<void, unknown>> => {
|
|
const updateResult = mapAtomCommandResult(
|
|
await updateProject({
|
|
environmentId,
|
|
input: {
|
|
projectId: input.projectId,
|
|
scripts: 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, updateProject, upsertKeybinding],
|
|
);
|
|
const saveProjectScript = useCallback(
|
|
async (input: NewProjectScriptInput): Promise<AtomCommandResult<void, unknown>> => {
|
|
if (!activeProject) {
|
|
return AsyncResult.success(undefined);
|
|
}
|
|
const nextId = nextProjectScriptId(
|
|
input.name,
|
|
activeProject.scripts.map((script) => script.id),
|
|
);
|
|
const nextScript = buildProjectScript(nextId, input);
|
|
const nextScripts = input.runOnWorktreeCreate
|
|
? [
|
|
...activeProject.scripts.map((script) =>
|
|
script.runOnWorktreeCreate ? { ...script, runOnWorktreeCreate: false } : script,
|
|
),
|
|
nextScript,
|
|
]
|
|
: [...activeProject.scripts, nextScript];
|
|
|
|
return persistProjectScripts({
|
|
projectId: activeProject.id,
|
|
projectCwd: activeProject.workspaceRoot,
|
|
previousScripts: activeProject.scripts,
|
|
nextScripts,
|
|
keybinding: input.keybinding,
|
|
keybindingCommand: commandForProjectScript(nextId),
|
|
});
|
|
},
|
|
[activeProject, persistProjectScripts],
|
|
);
|
|
const updateProjectScript = useCallback(
|
|
async (
|
|
scriptId: string,
|
|
input: NewProjectScriptInput,
|
|
): Promise<AtomCommandResult<void, unknown>> => {
|
|
if (!activeProject) {
|
|
return AsyncResult.success(undefined);
|
|
}
|
|
const existingScript = activeProject.scripts.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 = activeProject.scripts.map((script) =>
|
|
script.id === scriptId
|
|
? updatedScript
|
|
: input.runOnWorktreeCreate
|
|
? { ...script, runOnWorktreeCreate: false }
|
|
: script,
|
|
);
|
|
|
|
return persistProjectScripts({
|
|
projectId: activeProject.id,
|
|
projectCwd: activeProject.workspaceRoot,
|
|
previousScripts: activeProject.scripts,
|
|
nextScripts,
|
|
keybinding: input.keybinding,
|
|
keybindingCommand: commandForProjectScript(scriptId),
|
|
});
|
|
},
|
|
[activeProject, persistProjectScripts],
|
|
);
|
|
const deleteProjectScript = useCallback(
|
|
async (scriptId: string): Promise<AtomCommandResult<void, unknown>> => {
|
|
if (!activeProject) {
|
|
return AsyncResult.success(undefined);
|
|
}
|
|
const nextScripts = activeProject.scripts.filter((script) => script.id !== scriptId);
|
|
|
|
const deletedName = activeProject.scripts.find((s) => s.id === scriptId)?.name;
|
|
|
|
const result = await persistProjectScripts({
|
|
projectId: activeProject.id,
|
|
projectCwd: activeProject.workspaceRoot,
|
|
previousScripts: activeProject.scripts,
|
|
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, 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 === interactionMode) return;
|
|
setComposerDraftInteractionMode(composerDraftTarget, mode);
|
|
if (isLocalDraftThread) {
|
|
setDraftThreadContext(composerDraftTarget, { interactionMode: mode });
|
|
}
|
|
scheduleComposerFocus();
|
|
},
|
|
[
|
|
interactionMode,
|
|
isLocalDraftThread,
|
|
scheduleComposerFocus,
|
|
composerDraftTarget,
|
|
setComposerDraftInteractionMode,
|
|
setDraftThreadContext,
|
|
],
|
|
);
|
|
const toggleInteractionMode = useCallback(() => {
|
|
handleInteractionModeChange(interactionMode === "plan" ? "default" : "plan");
|
|
}, [handleInteractionModeChange, interactionMode]);
|
|
const createBrowserSurface = useCallback(
|
|
(profileId?: string) => {
|
|
if (!activeThreadRef) return;
|
|
void addBrowserSurface({
|
|
threadRef: activeThreadRef,
|
|
openPreview,
|
|
...(profileId === undefined ? {} : { profileId }),
|
|
});
|
|
},
|
|
[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 openFileSurface = useCallback(
|
|
(relativePath: string) => {
|
|
if (!activeThreadRef || !activeProject) return;
|
|
useRightPanelStore.getState().openFile(activeThreadRef, relativePath);
|
|
},
|
|
[activeProject, activeThreadRef],
|
|
);
|
|
// The thread's own change request, placed against the project it belongs to. Without a
|
|
// project there is nothing to resolve it against, so the caller falls back to the browser.
|
|
const linkedThreadPullRequest = activeThread?.linkedPullRequest ?? null;
|
|
const activeProjectRepository = activeProject?.repositoryIdentity?.displayName ?? null;
|
|
const threadRepository = linkedThreadPullRequest?.repository ?? activeProjectRepository;
|
|
const openThreadPullRequest = useCallback(
|
|
(number: number) => {
|
|
if (!supportsPullRequests || !activeThreadRef) {
|
|
return;
|
|
}
|
|
const projectId = linkedThreadPullRequest?.projectId ?? activeProject?.id;
|
|
const repository = linkedThreadPullRequest?.repository ?? activeProjectRepository;
|
|
if (projectId === undefined || repository === null) return;
|
|
useRightPanelStore.getState().openPullRequest(activeThreadRef, {
|
|
projectId,
|
|
repository,
|
|
number,
|
|
});
|
|
},
|
|
[
|
|
activeProject,
|
|
activeProjectRepository,
|
|
activeThreadRef,
|
|
linkedThreadPullRequest,
|
|
supportsPullRequests,
|
|
],
|
|
);
|
|
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 togglePreviewPanel = useCallback(() => {
|
|
if (!activeThreadRef || !isPreviewSupportedInRuntime()) return;
|
|
if (previewPanelOpen) {
|
|
useRightPanelStore.getState().close(activeThreadRef);
|
|
return;
|
|
}
|
|
const activeTabId = activePreviewState.activeTabId;
|
|
if (activeTabId) {
|
|
useRightPanelStore.getState().openBrowser(activeThreadRef, activeTabId);
|
|
} else {
|
|
createBrowserSurface();
|
|
}
|
|
}, [activePreviewState.activeTabId, activeThreadRef, createBrowserSurface, previewPanelOpen]);
|
|
const closePreviewPanel = useCallback(() => {
|
|
if (activeThreadRef) {
|
|
setMaximizedRightPanelThreadKey(null);
|
|
useRightPanelStore.getState().close(activeThreadRef);
|
|
}
|
|
}, [activeThreadRef]);
|
|
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 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,
|
|
anchorOffset: CHAT_TIMELINE_ANCHOR_OFFSET,
|
|
});
|
|
},
|
|
[composerOverlayHeight],
|
|
);
|
|
const timelineRealContentOverflowsViewport = useCallback(
|
|
(list?: LegendListRef | null) => {
|
|
const resolvedList = list ?? legendListRef.current;
|
|
const state = resolvedList?.getState();
|
|
if (!resolvedList || !state || state.data.length === 0) {
|
|
return false;
|
|
}
|
|
|
|
const lastRowIndex = state.data.length - 1;
|
|
const lastRowTop = state.positionAtIndex(lastRowIndex);
|
|
const lastRowHeight = state.sizeAtIndex(lastRowIndex);
|
|
if (
|
|
typeof lastRowTop !== "number" ||
|
|
typeof lastRowHeight !== "number" ||
|
|
!Number.isFinite(lastRowTop) ||
|
|
!Number.isFinite(lastRowHeight)
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
const realContentBottom = lastRowTop + Math.max(1, lastRowHeight);
|
|
const visibleScrollLength = Math.max(
|
|
0,
|
|
(state.scrollLength ?? 0) - composerOverlayHeight - CHAT_TIMELINE_ANCHOR_OFFSET,
|
|
);
|
|
return realContentBottom > visibleScrollLength;
|
|
},
|
|
[composerOverlayHeight],
|
|
);
|
|
// 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 });
|
|
});
|
|
}, []);
|
|
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(), composerOverlayHeight) ===
|
|
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 &&
|
|
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.
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
switch (event.key) {
|
|
case "PageUp":
|
|
case "Home":
|
|
case "ArrowUp":
|
|
if (contentScrollsUp() && !toolGroupConsumesUpwardNavigation(event.target)) {
|
|
handleManualNavigation();
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
};
|
|
scrollNode.addEventListener("wheel", handleWheel, {
|
|
passive: true,
|
|
});
|
|
scrollNode.addEventListener("touchmove", handleTouchMove, {
|
|
passive: true,
|
|
});
|
|
scrollNode.addEventListener("pointerdown", handlePointerDown, {
|
|
passive: true,
|
|
});
|
|
scrollNode.addEventListener("keydown", handleKeyDown);
|
|
removeListeners = () => {
|
|
scrollNode.removeEventListener("wheel", handleWheel);
|
|
scrollNode.removeEventListener("touchmove", handleTouchMove);
|
|
scrollNode.removeEventListener("pointerdown", handlePointerDown);
|
|
scrollNode.removeEventListener("keydown", handleKeyDown);
|
|
};
|
|
});
|
|
};
|
|
attach(12);
|
|
|
|
return () => {
|
|
if (frame !== null) {
|
|
cancelAnimationFrame(frame);
|
|
}
|
|
removeListeners?.();
|
|
};
|
|
}, [activeThread?.id, composerOverlayHeight, 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 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) {
|
|
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;
|
|
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(() => {
|
|
setIsRevertingCheckpoint(false);
|
|
}, [activeThread?.id]);
|
|
|
|
useEffect(() => {
|
|
if (!activeThread?.id || terminalUiState.terminalOpen) return;
|
|
const frame = window.requestAnimationFrame(() => {
|
|
focusComposer();
|
|
});
|
|
return () => {
|
|
window.cancelAnimationFrame(frame);
|
|
};
|
|
}, [activeThread?.id, focusComposer, 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();
|
|
cancelVideoPreviewRequest();
|
|
setOpeningVideoAttachmentId(null);
|
|
setExpandedImage(null);
|
|
}, [cancelVideoPreviewRequest, 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 ?? ""] ??
|
|
primaryServerSettings.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],
|
|
);
|
|
// The server-projected settled state keeps the banner and sidebar in sync.
|
|
const activeThreadShell = useThreadShell(isServerThread ? activeThreadRef : null);
|
|
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;
|
|
useLayoutEffect(() => {
|
|
if (!composerOverlayElement) return;
|
|
|
|
const updateHeight = () => {
|
|
const nextHeight = Math.ceil(composerOverlayElement.getBoundingClientRect().height);
|
|
if (nextHeight <= 0) return;
|
|
setComposerOverlayHeight((currentHeight) =>
|
|
currentHeight === nextHeight ? currentHeight : nextHeight,
|
|
);
|
|
setScrollToEndClearance((currentClearance) =>
|
|
currentClearance === nextHeight ? currentClearance : nextHeight,
|
|
);
|
|
};
|
|
|
|
updateHeight();
|
|
if (typeof ResizeObserver === "undefined") return;
|
|
|
|
const resizeObserver = new ResizeObserver(updateHeight);
|
|
resizeObserver.observe(composerOverlayElement);
|
|
return () => {
|
|
resizeObserver.disconnect();
|
|
};
|
|
}, [composerOverlayElement]);
|
|
const linkedPullRequestStatus = useLinkedThreadPullRequest(
|
|
activeThreadRef?.environmentId ?? null,
|
|
linkedThreadPullRequest,
|
|
);
|
|
const activeThreadPr = resolveDisplayedThreadPr({
|
|
threadBranch: activeThread?.branch ?? null,
|
|
gitStatus: gitStatusQuery.data ?? null,
|
|
snapshot: activeThreadKey ? changeRequestSnapshotByKey.get(activeThreadKey) : undefined,
|
|
retainTerminalOnBranchMismatch: activeThread?.worktreePath === null,
|
|
linkedPullRequest: linkedThreadPullRequest,
|
|
linkedPullRequestStatus,
|
|
});
|
|
const handlePullRequestTabStatusChange = useCallback(
|
|
(status: PullRequestTabStatus) => {
|
|
updatePullRequestTabStatusFromPanel(status);
|
|
const source = threadPullRequestRefreshSource({
|
|
panel: status,
|
|
thread: {
|
|
repository: threadRepository,
|
|
number: linkedThreadPullRequest?.number ?? activeThreadPr?.number ?? null,
|
|
state: activeThreadPr?.state ?? null,
|
|
linked: linkedThreadPullRequest !== null,
|
|
},
|
|
});
|
|
if (source === null) {
|
|
sidebarPrRefreshKeyRef.current = null;
|
|
return;
|
|
}
|
|
const refreshKey = `${activeThreadKey}:${source}:${status.repository}#${status.number}:${status.state}`;
|
|
if (sidebarPrRefreshKeyRef.current === refreshKey) return;
|
|
sidebarPrRefreshKeyRef.current = refreshKey;
|
|
|
|
if (source === "linked-detail" && activeThreadRef && linkedThreadPullRequest) {
|
|
appAtomRegistry.refresh(
|
|
linkedPullRequestDetailAtom({
|
|
environmentId: activeThreadRef.environmentId,
|
|
input: {
|
|
projectId: linkedThreadPullRequest.projectId,
|
|
repository: linkedThreadPullRequest.repository,
|
|
number: linkedThreadPullRequest.number,
|
|
},
|
|
}),
|
|
);
|
|
return;
|
|
}
|
|
if (source === "vcs" && activeThreadRef && gitCwd !== null) {
|
|
void refreshVcsStatus({
|
|
environmentId: activeThreadRef.environmentId,
|
|
input: { cwd: gitCwd },
|
|
}).then(() => {
|
|
if (sidebarPrRefreshKeyRef.current === refreshKey) {
|
|
sidebarPrRefreshKeyRef.current = null;
|
|
}
|
|
});
|
|
}
|
|
},
|
|
[
|
|
activeThreadKey,
|
|
activeThreadPr?.number,
|
|
activeThreadPr?.state,
|
|
activeThreadRef,
|
|
gitCwd,
|
|
linkedThreadPullRequest,
|
|
refreshVcsStatus,
|
|
threadRepository,
|
|
updatePullRequestTabStatusFromPanel,
|
|
],
|
|
);
|
|
const activeThreadReferenceCopyTarget = useMemo(
|
|
() =>
|
|
activeThreadId === null || !isServerThread
|
|
? null
|
|
: resolveThreadReferenceCopyTarget({
|
|
threadId: activeThreadId,
|
|
linkedPullRequestUrl: linkedThreadPullRequest?.url ?? null,
|
|
detectedPullRequestUrl: activeThreadPr?.url ?? null,
|
|
}),
|
|
[activeThreadId, activeThreadPr?.url, isServerThread, linkedThreadPullRequest?.url],
|
|
);
|
|
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]);
|
|
// The right panel offers the thread's own change request, so it can only offer it once the
|
|
// branch has one; until then the picker says so rather than opening an empty panel.
|
|
const addPullRequestSurface = useCallback(() => {
|
|
if (activeThreadPr === null) return;
|
|
openThreadPullRequest(activeThreadPr.number);
|
|
}, [activeThreadPr, openThreadPullRequest]);
|
|
const pullRequestSurfaceAvailable =
|
|
supportsPullRequests && activeThreadPr !== null && threadRepository !== 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 compactDisabled =
|
|
!activeThread ||
|
|
!activeProject ||
|
|
!isServerThread ||
|
|
selectedProvider !== "claudeAgent" ||
|
|
!compactionProviderAvailable ||
|
|
isWorking ||
|
|
threadDetailLoading ||
|
|
isPreparingWorktree ||
|
|
activeEnvironmentUnavailable ||
|
|
feedbackUploading ||
|
|
pendingApprovals.length > 0 ||
|
|
pendingUserInputs.length > 0 ||
|
|
showPlanFollowUpPrompt ||
|
|
composerHasUnsentContent;
|
|
const compactDisabledReason = compactDisabled
|
|
? composerHasUnsentContent
|
|
? "Send or clear your draft before compacting"
|
|
: !activeProject
|
|
? "Choose a project before compacting"
|
|
: !compactionProviderAvailable
|
|
? "Enable a Claude provider before compacting"
|
|
: "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 composerBannerItems = useMemo<ComposerBannerStackItem[]>(() => {
|
|
const backgroundLivenessItems =
|
|
backgroundLivenessBannerItem === null ? [] : [backgroundLivenessBannerItem];
|
|
const resumeCompactionItems =
|
|
resumeCompactionBannerItem === null ? [] : [resumeCompactionBannerItem];
|
|
const wokeThreadItems = wokeThreadBannerItem === null ? [] : [wokeThreadBannerItem];
|
|
const parkedThreadItems = parkedThreadBannerItem === null ? [] : [parkedThreadBannerItem];
|
|
if (!localCheckoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
|
|
return [
|
|
...systemComposerBannerItems,
|
|
...backgroundLivenessItems,
|
|
...resumeCompactionItems,
|
|
...wokeThreadItems,
|
|
...parkedThreadItems,
|
|
];
|
|
}
|
|
return [
|
|
...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,
|
|
handleRestoreThreadBranch,
|
|
isRestoringThreadBranch,
|
|
localCheckoutBranchMismatch,
|
|
parkedThreadBannerItem,
|
|
resumeCompactionBannerItem,
|
|
showBranchMismatchBanner,
|
|
systemComposerBannerItems,
|
|
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),
|
|
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 === "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;
|
|
}
|
|
|
|
const scriptId = projectScriptIdFromCommand(command);
|
|
if (!scriptId || !activeProject) return;
|
|
const script = activeProject.scripts.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,
|
|
addTerminalSurface,
|
|
activeThreadRef,
|
|
activeThreadPinned,
|
|
activeThreadSettled,
|
|
terminalUiState.terminalOpen,
|
|
terminalUiState.activeTerminalId,
|
|
activeThreadId,
|
|
requestCloseTerminal,
|
|
requestClosePanelTerminal,
|
|
createNewTerminal,
|
|
setTerminalOpen,
|
|
runProjectScript,
|
|
splitTerminal,
|
|
splitPanelTerminal,
|
|
keybindings,
|
|
handleUnsettleActiveThread,
|
|
isServerThread,
|
|
onToggleDiff,
|
|
pinThread,
|
|
settleThread,
|
|
supportsPinning,
|
|
supportsSettlement,
|
|
confirmAndUnpinThread,
|
|
copyActiveThreadReference,
|
|
toggleRightPanel,
|
|
toggleRightPanelMaximized,
|
|
toggleTerminalVisibility,
|
|
composerRef,
|
|
]);
|
|
|
|
const onRevertToTurnCount = useCallback(
|
|
async (turnCount: number) => {
|
|
const localApi = readLocalApi();
|
|
if (!localApi || !activeThread || isRevertingCheckpoint) 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;
|
|
}
|
|
const confirmed = await localApi.dialogs.confirm(
|
|
[
|
|
`Revert this thread to checkpoint ${turnCount}?`,
|
|
"This will discard newer messages and turn diffs in this thread.",
|
|
"This action cannot be undone.",
|
|
].join("\n"),
|
|
{ variant: "destructive" },
|
|
);
|
|
if (!confirmed) {
|
|
return;
|
|
}
|
|
|
|
setIsRevertingCheckpoint(true);
|
|
setThreadError(activeThread.id, null);
|
|
const result = await revertThreadCheckpoint({
|
|
environmentId,
|
|
input: {
|
|
threadId: activeThread.id,
|
|
turnCount,
|
|
},
|
|
});
|
|
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
|
const error = squashAtomCommandFailure(result);
|
|
setThreadError(
|
|
activeThread.id,
|
|
error instanceof Error ? error.message : "Failed to revert thread state.",
|
|
);
|
|
}
|
|
setIsRevertingCheckpoint(false);
|
|
},
|
|
[
|
|
activeThread,
|
|
activeEnvironmentUnavailable,
|
|
activeEnvironmentUnavailableLabel,
|
|
environmentId,
|
|
isConnecting,
|
|
isRevertingCheckpoint,
|
|
isSendBusy,
|
|
phase,
|
|
revertThreadCheckpoint,
|
|
setThreadError,
|
|
],
|
|
);
|
|
|
|
const onSend = async (
|
|
e?: { preventDefault: () => void },
|
|
submissionIntent: ComposerSubmissionIntent = "foreground",
|
|
directAnnotation?: {
|
|
annotation: PreviewAnnotationPayload;
|
|
image: ComposerImageAttachment | null;
|
|
},
|
|
) => {
|
|
e?.preventDefault();
|
|
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 ||
|
|
threadDetailLoading ||
|
|
sendInFlightRef.current ||
|
|
feedbackUploadsInFlightRef.current.has(routeThreadKey)
|
|
) {
|
|
notifyDirectAnnotationAttached();
|
|
return;
|
|
}
|
|
if (activeEnvironmentUnavailable) {
|
|
toastManager.add(
|
|
stackedThreadToast({
|
|
type: "warning",
|
|
title: "Not connected: message not sent",
|
|
description: "Reconnecting to the environment. Try again once it is connected.",
|
|
}),
|
|
);
|
|
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,
|
|
} = 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);
|
|
const result = await submitCodexFeedback({
|
|
submission: {
|
|
id: newMessageId(),
|
|
command: trimmed,
|
|
createdAt: new Date().toISOString(),
|
|
},
|
|
clearDraft: () => {
|
|
promptRef.current = "";
|
|
clearComposerDraftContent(composerDraftTarget);
|
|
composerRef.current?.resetCursorState();
|
|
scrollToEnd();
|
|
},
|
|
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);
|
|
});
|
|
if (result._tag === "Failure") {
|
|
if (!isAtomCommandInterrupted(result)) {
|
|
toastManager.add(
|
|
stackedThreadToast({
|
|
type: "error",
|
|
title: "Could not send feedback to OpenAI",
|
|
description: chatActionErrorMessage(squashAtomCommandFailure(result)),
|
|
}),
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
const feedbackId = result.value.feedbackId;
|
|
toastManager.add(
|
|
stackedThreadToast({
|
|
type: "success",
|
|
title: "Feedback sent to OpenAI",
|
|
description: `Thread ID: ${feedbackId}`,
|
|
timeout: 0,
|
|
actionProps: {
|
|
children: "Copy ID",
|
|
onClick: () => {
|
|
void writeTextToClipboard(feedbackId, "Codex feedback thread ID").catch(
|
|
(error: unknown) => {
|
|
toastManager.add(
|
|
stackedThreadToast({
|
|
type: "error",
|
|
title: "Could not copy thread ID",
|
|
description: chatActionErrorMessage(error),
|
|
}),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
return;
|
|
}
|
|
if (
|
|
!directAnnotation &&
|
|
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;
|
|
}
|
|
// Legacy plan mode: /plan and /default only act when the beta flag is on;
|
|
// otherwise they send as plain text like any other message.
|
|
const standaloneSlashCommand =
|
|
settings.planModeEnabled &&
|
|
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),
|
|
};
|
|
}),
|
|
);
|
|
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,
|
|
}
|
|
: {
|
|
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 || activeProject.defaultModelSelection?.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,
|
|
});
|
|
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,
|
|
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,
|
|
...(bootstrap ? { bootstrap } : {}),
|
|
createdAt: messageCreatedAt,
|
|
},
|
|
});
|
|
if (startResult._tag === "Failure") {
|
|
if (backgroundThreadRef) {
|
|
clearBackgroundDraftSubmissionByRef(backgroundThreadRef);
|
|
}
|
|
failure = startResult;
|
|
} else {
|
|
turnStartSucceeded = true;
|
|
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 onInterrupt = async () => {
|
|
if (!activeThread) return;
|
|
const result = await interruptThreadTurn({
|
|
environmentId,
|
|
input: buildThreadTurnInterruptInput(activeThread),
|
|
});
|
|
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
|
const error = squashAtomCommandFailure(result);
|
|
setThreadError(
|
|
activeThread.id,
|
|
error instanceof Error ? error.message : "Failed to interrupt the current turn.",
|
|
);
|
|
}
|
|
};
|
|
|
|
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) return;
|
|
|
|
setRespondingUserInputRequestIds((existing) =>
|
|
existing.includes(requestId) ? existing : [...existing, requestId],
|
|
);
|
|
const result = await respondToThreadUserInput({
|
|
environmentId,
|
|
input: {
|
|
threadId: activeThreadId,
|
|
requestId,
|
|
answers,
|
|
},
|
|
});
|
|
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
|
const error = squashAtomCommandFailure(result);
|
|
setThreadError(
|
|
activeThreadId,
|
|
error instanceof Error ? error.message : "Failed to submit user input.",
|
|
);
|
|
}
|
|
setRespondingUserInputRequestIds((existing) => existing.filter((id) => id !== requestId));
|
|
return result;
|
|
},
|
|
[activeThreadId, environmentId, respondToThreadUserInput, setThreadError],
|
|
);
|
|
|
|
const setActivePendingUserInputQuestionIndex = useCallback(
|
|
(nextQuestionIndex: number) => {
|
|
if (!activePendingUserInput) {
|
|
return;
|
|
}
|
|
setPendingUserInputQuestionIndexByRequestId((existing) => ({
|
|
...existing,
|
|
[activePendingUserInput.requestId]: nextQuestionIndex,
|
|
}));
|
|
},
|
|
[activePendingUserInput],
|
|
);
|
|
|
|
const onSelectActivePendingUserInputOption = useCallback(
|
|
(questionId: string, optionLabel: 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,
|
|
[activePendingUserInput.requestId]: {
|
|
...existing[activePendingUserInput.requestId],
|
|
[questionId]: togglePendingUserInputOptionSelection(
|
|
question,
|
|
existing[activePendingUserInput.requestId]?.[questionId],
|
|
optionLabel,
|
|
),
|
|
},
|
|
};
|
|
});
|
|
promptRef.current = "";
|
|
composerRef.current?.resetCursorState({ cursor: 0 });
|
|
},
|
|
[activePendingProgress?.activeQuestion, activePendingUserInput, composerRef],
|
|
);
|
|
|
|
const onChangeActivePendingUserInputCustomAnswer = useCallback(
|
|
(
|
|
questionId: string,
|
|
value: string,
|
|
nextCursor: number,
|
|
expandedCursor: number,
|
|
_cursorAdjacentToMention: boolean,
|
|
) => {
|
|
if (!activePendingUserInput) {
|
|
return;
|
|
}
|
|
promptRef.current = value;
|
|
setPendingUserInputAnswersByRequestId((existing) => ({
|
|
...existing,
|
|
[activePendingUserInput.requestId]: {
|
|
...existing[activePendingUserInput.requestId],
|
|
[questionId]: setPendingUserInputCustomAnswer(
|
|
existing[activePendingUserInput.requestId]?.[questionId],
|
|
value,
|
|
),
|
|
},
|
|
}));
|
|
const snapshot = composerRef.current?.readSnapshot();
|
|
if (
|
|
snapshot?.value !== value ||
|
|
snapshot.cursor !== nextCursor ||
|
|
snapshot.expandedCursor !== expandedCursor
|
|
) {
|
|
composerRef.current?.focusAt(nextCursor);
|
|
}
|
|
},
|
|
[activePendingUserInput, composerRef],
|
|
);
|
|
|
|
const onAdvanceActivePendingUserInput = useCallback(() => {
|
|
if (!activePendingUserInput || !activePendingProgress) {
|
|
return;
|
|
}
|
|
if (activePendingProgress.isLastQuestion) {
|
|
if (activePendingResolvedAnswers) {
|
|
void onRespondToUserInput(activePendingUserInput.requestId, activePendingResolvedAnswers);
|
|
}
|
|
return;
|
|
}
|
|
setActivePendingUserInputQuestionIndex(activePendingProgress.questionIndex + 1);
|
|
}, [
|
|
activePendingProgress,
|
|
activePendingResolvedAnswers,
|
|
activePendingUserInput,
|
|
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) {
|
|
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) {
|
|
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,
|
|
],
|
|
);
|
|
|
|
const onImplementPlanInNewThread = useCallback(async () => {
|
|
if (
|
|
!activeThread ||
|
|
!activeProject ||
|
|
!activeProposedPlan ||
|
|
!isServerThread ||
|
|
isSendBusy ||
|
|
isConnecting ||
|
|
activeEnvironmentUnavailable ||
|
|
sendInFlightRef.current
|
|
) {
|
|
return;
|
|
}
|
|
|
|
const sendCtx = composerRef.current?.getSendContext();
|
|
if (!sendCtx?.providerAvailable) {
|
|
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,
|
|
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,
|
|
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,
|
|
runtimeMode,
|
|
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: primaryServerSettings.newWorktreesStartFromOrigin,
|
|
}),
|
|
...(mode === "worktree" && draftThread?.worktreePath ? { worktreePath: null } : {}),
|
|
});
|
|
}
|
|
scheduleComposerFocus();
|
|
},
|
|
[
|
|
canOverrideServerThreadEnvMode,
|
|
composerDraftTarget,
|
|
draftThread?.worktreePath,
|
|
isLocalDraftThread,
|
|
primaryServerSettings.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) => {
|
|
cancelVideoPreviewRequest();
|
|
setOpeningVideoAttachmentId(null);
|
|
setExpandedImage(preview);
|
|
},
|
|
[cancelVideoPreviewRequest],
|
|
);
|
|
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],
|
|
);
|
|
// Both the Map and the revert handler are read from refs at call-time so
|
|
// the callback reference is fully stable and never busts context identity.
|
|
const revertTurnCountRef = useRef(revertTurnCountByUserMessageId);
|
|
revertTurnCountRef.current = revertTurnCountByUserMessageId;
|
|
const onRevertToTurnCountRef = useRef(onRevertToTurnCount);
|
|
onRevertToTurnCountRef.current = onRevertToTurnCount;
|
|
const onRevertUserMessage = useCallback((messageId: MessageId) => {
|
|
const targetTurnCount = revertTurnCountRef.current.get(messageId);
|
|
if (typeof targetTurnCount !== "number") {
|
|
return;
|
|
}
|
|
void onRevertToTurnCountRef.current(targetTurnCount);
|
|
}, []);
|
|
|
|
// 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
|
|
onSendAnnotation={(annotation, image) => {
|
|
void onSend(undefined, "foreground", { annotation, image });
|
|
}}
|
|
/>
|
|
</Suspense>
|
|
) : renderedRightPanelSurface?.kind === "terminal" ? (
|
|
<PersistentThreadTerminalPanel
|
|
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.repository}#${renderedRightPanelSurface.number}`}
|
|
environmentId={activeThread.environmentId}
|
|
reference={{
|
|
projectId: renderedRightPanelSurface.projectId as ProjectId,
|
|
repository: renderedRightPanelSurface.repository,
|
|
number: renderedRightPanelSurface.number,
|
|
}}
|
|
context={
|
|
isThreadOwnPullRequest(
|
|
{
|
|
projectId: linkedThreadPullRequest?.projectId ?? activeProject?.id ?? null,
|
|
repository: threadRepository,
|
|
number: activeThreadPr?.number ?? null,
|
|
},
|
|
{
|
|
projectId: renderedRightPanelSurface.projectId,
|
|
repository: renderedRightPanelSurface.repository,
|
|
number: renderedRightPanelSurface.number,
|
|
},
|
|
)
|
|
? "thread"
|
|
: "page"
|
|
}
|
|
composerDraftTarget={composerDraftTarget}
|
|
onStateChange={handlePullRequestTabStatusChange}
|
|
/>
|
|
) : renderedRightPanelSurface?.kind === "agents" ? (
|
|
<AgentsPanel
|
|
model={agentPanelModel}
|
|
environmentId={activeThreadRef?.environmentId ?? null}
|
|
threadId={activeThreadRef?.threadId ?? null}
|
|
/>
|
|
) : (renderedRightPanelSurface?.kind === "files" ||
|
|
renderedRightPanelSurface?.kind === "file") &&
|
|
activeProject &&
|
|
activeWorkspaceRoot ? (
|
|
<Suspense fallback={null}>
|
|
<FilePreviewPanel
|
|
key={`${activeProject.environmentId}:${activeWorkspaceRoot}`}
|
|
environmentId={activeProject.environmentId}
|
|
cwd={activeWorkspaceRoot}
|
|
projectName={activeProject.title}
|
|
threadRef={activeThreadRef}
|
|
composerDraftTarget={composerDraftTarget}
|
|
keybindings={keybindings}
|
|
availableEditors={availableEditors}
|
|
relativePath={
|
|
renderedRightPanelSurface.kind === "file"
|
|
? renderedRightPanelSurface.relativePath
|
|
: null
|
|
}
|
|
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">
|
|
<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"
|
|
>
|
|
{!shouldUseRightPanelSheet || !rightPanelControlsInPanel ? panelLayoutControls : null}
|
|
<ChatHeader
|
|
{...(!supportsPullRequests || activeProjectRepository === null
|
|
? {}
|
|
: { onOpenPullRequest: openProjectPullRequest })}
|
|
activeThreadEnvironmentId={activeThread.environmentId}
|
|
activeThreadId={activeThread.id}
|
|
{...(routeKind === "draft" && draftId ? { draftId } : {})}
|
|
activeThreadTitle={activeThread.title}
|
|
isServerThread={isServerThread}
|
|
activeProjectName={activeProject?.title}
|
|
activeProjectCwd={activeProject?.workspaceRoot ?? null}
|
|
activeProjectFaviconPath={activeProject?.faviconPath ?? null}
|
|
openInCwd={gitCwd}
|
|
activeProjectScripts={activeProject?.scripts}
|
|
preferredScriptId={
|
|
activeProject ? (lastInvokedScriptByProjectId[activeProject.id] ?? null) : null
|
|
}
|
|
keybindings={keybindings}
|
|
availableEditors={availableEditors}
|
|
rightPanelOpen={rightPanelOpen}
|
|
gitCwd={gitCwd}
|
|
onNewThreadInProject={handleNewThreadInActiveProject}
|
|
onRunProjectScript={runProjectScript}
|
|
onAddProjectScript={saveProjectScript}
|
|
onUpdateProjectScript={updateProjectScript}
|
|
onDeleteProjectScript={deleteProjectScript}
|
|
/>
|
|
</WorkspacePageHeader>
|
|
|
|
<ThreadErrorBanner
|
|
error={visibleThreadError}
|
|
onDismiss={() => {
|
|
setThreadError(activeThread.id, null);
|
|
dismissThreadErrorBannerForSession(threadErrorBannerKey);
|
|
setThreadErrorBannerDismissTick((tick) => tick + 1);
|
|
}}
|
|
/>
|
|
{/* 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}
|
|
{/* Provider status overlays the timeline without changing its content height. */}
|
|
<div className="pointer-events-none absolute inset-x-0 top-0 z-20">
|
|
<ProviderStatusBanner
|
|
status={visibleProviderStatus}
|
|
onDismiss={() => setDismissedProviderStatusBannerKey(providerStatusBannerKey)}
|
|
/>
|
|
</div>
|
|
{/* Messages Wrapper */}
|
|
<div className="relative flex min-h-0 flex-1 flex-col">
|
|
{/* Messages — LegendList handles virtualization and scrolling internally */}
|
|
<MessagesTimeline
|
|
citationRequest={citationRequest}
|
|
citationHistoryLoading={threadDetailLoading}
|
|
onCiteAssistantText={citeAssistantText}
|
|
agentPanelModel={agentPanelModel}
|
|
onOpenAgents={addAgentsSurface}
|
|
key={activeThread.id}
|
|
isWorking={isWorking}
|
|
isPreparingWorktree={isPreparingWorktree}
|
|
activeTurnStartedAt={activeWorkStartedAt}
|
|
listRef={legendListRef}
|
|
timelineEntries={timelineEntries}
|
|
latestTurn={activeLatestTurn}
|
|
runningTurnId={activeRunningTurnId}
|
|
turnDiffSummaryByAssistantMessageId={turnDiffSummaryByAssistantMessageId}
|
|
activeThreadEnvironmentId={activeThread.environmentId}
|
|
routeThreadKey={routeThreadKey}
|
|
onOpenTurnDiff={onOpenTurnDiff}
|
|
revertTurnCountByUserMessageId={revertTurnCountByUserMessageId}
|
|
onRevertUserMessage={onRevertUserMessage}
|
|
onUseArtifactTemplate={useArtifactTemplate}
|
|
isRevertingCheckpoint={isRevertingCheckpoint}
|
|
onImageExpand={onExpandTimelineImage}
|
|
onFileOpen={openFileAttachment}
|
|
openingVideoAttachmentId={openingVideoAttachmentId}
|
|
markdownCwd={gitCwd ?? undefined}
|
|
resolvedTheme={resolvedTheme}
|
|
timestampFormat={timestampFormat}
|
|
workspaceRoot={activeWorkspaceRoot}
|
|
skills={
|
|
activeProviderStatus
|
|
? resolveProviderSkillsForCwd(activeProviderStatus, gitCwd)
|
|
: EMPTY_PROVIDER_SKILLS
|
|
}
|
|
anchorMessageId={timelineAnchorMessageId}
|
|
onAnchorReady={onTimelineAnchorReady}
|
|
contentInsetEndAdjustment={composerOverlayHeight}
|
|
liveFollowEnabled={timelineLiveFollowEnabled}
|
|
onIsAtEndChange={onIsAtEndChange}
|
|
onManualNavigation={cancelTimelineLiveFollowForUserNavigation}
|
|
hideEmptyPlaceholder={isDraftHeroState || threadDetailLoading}
|
|
topFadeEnabled={!hasTimelineTopBanner}
|
|
loadEarlier={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"
|
|
onClick={() => 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}
|
|
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 className="group/composer-stack pointer-events-auto relative z-10">
|
|
{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}
|
|
maxFileAttachmentBytes={maxFileAttachmentBytes}
|
|
routeKind={routeKind}
|
|
routeThreadRef={routeThreadRef}
|
|
draftId={draftId}
|
|
activeThreadId={activeThreadId}
|
|
activeThreadEnvironmentId={activeThread?.environmentId}
|
|
activeThread={activeThread}
|
|
isServerThread={isServerThread}
|
|
isLocalDraftThread={isLocalDraftThread}
|
|
forceExpandedOnMobile={forceExpandedMobileComposer && isDraftHeroState}
|
|
projectSelectionRequired={isLocalDraftThread && activeProject === null}
|
|
phase={phase}
|
|
isConnecting={isConnecting}
|
|
isSendBusy={isSendBusy}
|
|
sendDisabledReason={
|
|
feedbackUploading
|
|
? "Sending feedback"
|
|
: threadDetailLoading
|
|
? "Messages loading"
|
|
: null
|
|
}
|
|
isPreparingWorktree={isPreparingWorktree}
|
|
bannerItems={composerBannerItems}
|
|
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[]}
|
|
activeProjectDefaultModelSelection={
|
|
activeProject?.defaultModelSelection
|
|
}
|
|
activeThreadModelSelection={activeThread?.modelSelection}
|
|
activeContextWindow={activeContextWindow}
|
|
compactDisabled={compactDisabled}
|
|
compactDisabledReason={compactDisabledReason}
|
|
resolvedTheme={resolvedTheme}
|
|
settings={settings}
|
|
keybindings={keybindings}
|
|
terminalOpen={Boolean(terminalUiState.terminalOpen)}
|
|
gitCwd={gitCwd}
|
|
promptRef={promptRef}
|
|
composerImagesRef={composerImagesRef}
|
|
composerFilesRef={composerFilesRef}
|
|
composerTerminalContextsRef={composerTerminalContextsRef}
|
|
composerElementContextsRef={composerElementContextsRef}
|
|
onSend={onSend}
|
|
onInterrupt={onInterrupt}
|
|
onImplementPlanInNewThread={onImplementPlanInNewThread}
|
|
onRespondToApproval={onRespondToApproval}
|
|
onSelectActivePendingUserInputOption={
|
|
onSelectActivePendingUserInputOption
|
|
}
|
|
onAdvanceActivePendingUserInput={onAdvanceActivePendingUserInput}
|
|
onPreviousActivePendingUserInputQuestion={
|
|
onPreviousActivePendingUserInputQuestion
|
|
}
|
|
onChangeActivePendingUserInputCustomAnswer={
|
|
onChangeActivePendingUserInputCustomAnswer
|
|
}
|
|
onProviderModelSelect={onProviderModelSelect}
|
|
getModelDisabledReason={getModelDisabledReason}
|
|
toggleInteractionMode={toggleInteractionMode}
|
|
handleRuntimeModeChange={handleRuntimeModeChange}
|
|
handleInteractionModeChange={handleInteractionModeChange}
|
|
focusComposer={focusComposer}
|
|
scheduleComposerFocus={scheduleComposerFocus}
|
|
setThreadError={setThreadError}
|
|
onExpandImage={onExpandTimelineImage}
|
|
onFileOpen={openFileAttachment}
|
|
openingVideoAttachmentId={openingVideoAttachmentId}
|
|
/>
|
|
</div>
|
|
</ComposerSurface.Host>
|
|
<div className="min-h-0">
|
|
<div
|
|
data-terminal-open={terminalUiState.terminalOpen ? "true" : undefined}
|
|
className="relative z-0"
|
|
>
|
|
{showComposerContextStrip && (
|
|
<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 } : {})}
|
|
availableEnvironments={logicalProjectEnvironments}
|
|
/>
|
|
</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 ? (
|
|
<ThreadPreviewMiniPlayer
|
|
key={`${activeThreadKey}:${activePreviewMiniPlayer.tabId}`}
|
|
threadRef={activeThreadRef}
|
|
tabId={activePreviewMiniPlayer.tabId}
|
|
bottomInset={isDraftHeroState ? 0 : composerOverlayHeight}
|
|
/>
|
|
) : 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}
|
|
activeSurfaceId={renderedRightPanelSurface?.id ?? null}
|
|
pendingSurfaceIds={pendingFileSurfaceIds}
|
|
previewSessions={activePreviewState.sessions}
|
|
desktopByTabId={activePreviewState.desktopByTabId}
|
|
previewRuntimeTabId={resolvePreviewRuntimeTabId}
|
|
terminalLabelsById={activeTerminalLabelsById}
|
|
onActivate={activateRightPanelSurface}
|
|
onCloseSurface={closeRightPanelSurface}
|
|
onCloseOtherSurfaces={closeOtherRightPanelSurfaces}
|
|
onCloseSurfacesToRight={closeRightPanelSurfacesToRight}
|
|
onCloseAllSurfaces={closeAllRightPanelSurfaces}
|
|
onCopyFilePath={copyRightPanelFilePath}
|
|
onAddBrowser={() => createBrowserSurface()}
|
|
onAddBrowserInProfile={createBrowserSurface}
|
|
onAddTerminal={addTerminalSurface}
|
|
onAddDiff={addDiffSurface}
|
|
onAddFiles={addFilesSurface}
|
|
onAddPullRequest={addPullRequestSurface}
|
|
onAddAgents={addAgentsSurface}
|
|
browserAvailable={isPreviewSupportedInRuntime()}
|
|
terminalAvailable={activeProject !== null}
|
|
diffAvailable={isServerThread && isGitRepo}
|
|
filesAvailable={activeProject !== null}
|
|
pullRequestAvailable={pullRequestSurfaceAvailable}
|
|
agentsAvailable
|
|
pullRequestStatuses={pullRequestTabStatuses}
|
|
liveAgentCount={agentPanelModel.liveCount}
|
|
>
|
|
{rightPanelContent}
|
|
</RightPanelTabs>
|
|
) : null}
|
|
{rightPanelPresent && shouldUseRightPanelSheet && activeThreadRef ? (
|
|
<RightPanelSheet
|
|
animationDurationMs={panelAnimationsActive ? panelAnimationDurationMs : 0}
|
|
open={rightPanelOpen}
|
|
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}
|
|
activeSurfaceId={renderedRightPanelSurface?.id ?? null}
|
|
pendingSurfaceIds={pendingFileSurfaceIds}
|
|
previewSessions={activePreviewState.sessions}
|
|
desktopByTabId={activePreviewState.desktopByTabId}
|
|
previewRuntimeTabId={resolvePreviewRuntimeTabId}
|
|
terminalLabelsById={activeTerminalLabelsById}
|
|
onActivate={activateRightPanelSurface}
|
|
onCloseSurface={closeRightPanelSurface}
|
|
onCloseOtherSurfaces={closeOtherRightPanelSurfaces}
|
|
onCloseSurfacesToRight={closeRightPanelSurfacesToRight}
|
|
onCloseAllSurfaces={closeAllRightPanelSurfaces}
|
|
onCopyFilePath={copyRightPanelFilePath}
|
|
onAddBrowser={() => createBrowserSurface()}
|
|
onAddBrowserInProfile={createBrowserSurface}
|
|
onAddTerminal={addTerminalSurface}
|
|
onAddDiff={addDiffSurface}
|
|
onAddFiles={addFilesSurface}
|
|
onAddPullRequest={addPullRequestSurface}
|
|
onAddAgents={addAgentsSurface}
|
|
browserAvailable={isPreviewSupportedInRuntime()}
|
|
terminalAvailable={activeProject !== null}
|
|
diffAvailable={isServerThread && isGitRepo}
|
|
filesAvailable={activeProject !== null}
|
|
pullRequestAvailable={pullRequestSurfaceAvailable}
|
|
agentsAvailable
|
|
pullRequestStatuses={pullRequestTabStatuses}
|
|
liveAgentCount={agentPanelModel.liveCount}
|
|
>
|
|
{rightPanelContent}
|
|
</RightPanelTabs>
|
|
</RightPanelSheet>
|
|
) : null}
|
|
|
|
{expandedImage && (
|
|
<ExpandedImageDialog
|
|
key={`${expandedImage.images[expandedImage.index]?.src ?? "image"}:${expandedImage.index}`}
|
|
preview={expandedImage}
|
|
onClose={closeExpandedImage}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default function ChatView(props: ChatViewProps) {
|
|
return (
|
|
<DiffWorkerPoolProvider>
|
|
<ChatViewContent {...props} />
|
|
</DiffWorkerPoolProvider>
|
|
);
|
|
}
|