import { type ApprovalRequestId, DEFAULT_MODEL_BY_PROVIDER, type ClaudeCodeEffort, type MessageId, type ModelSelection, type ProjectScript, type ProviderKind, type ProjectEntry, type ProjectId, type ProviderApprovalDecision, PROVIDER_SEND_TURN_MAX_ATTACHMENTS, PROVIDER_SEND_TURN_MAX_IMAGE_BYTES, type ServerProvider, type ThreadId, type TurnId, type KeybindingCommand, OrchestrationThreadActivity, ProviderInteractionMode, RuntimeMode, TerminalOpenInput,} from "@t3tools/contracts";import { applyClaudePromptEffortPrefix, normalizeModelSlug } from "@t3tools/shared/model";import { projectScriptCwd, projectScriptRuntimeEnv } from "@t3tools/shared/projectScripts";import { truncate } from "@t3tools/shared/String";import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";import { useQuery } from "@tanstack/react-query";import { useDebouncedValue } from "@tanstack/react-pacer";import { useNavigate, useSearch } from "@tanstack/react-router";import { useGitStatus } from "~/lib/gitStatusState";import { projectSearchEntriesQueryOptions } from "~/lib/projectReactQuery";import { isElectron } from "../env";import { parseDiffRouteSearch, stripDiffSearchParams } from "../diffRouteSearch";import { clampCollapsedComposerCursor, type ComposerTrigger, collapseExpandedComposerCursor, detectComposerTrigger, expandCollapsedComposerCursor, parseStandaloneComposerSlashCommand, replaceTextRange,} from "../composer-logic";import { deriveCompletionDividerBeforeEntryId, derivePendingApprovals, derivePendingUserInputs, derivePhase, deriveTimelineEntries, deriveActiveWorkStartedAt, deriveActivePlanState, findSidebarProposedPlan, findLatestProposedPlan, deriveWorkLogEntries, hasActionableProposedPlan, hasToolActivityForTurn, isLatestTurnSettled, formatElapsed,} from "../session-logic";import { isScrollContainerNearBottom } from "../chat-scroll";import { buildPendingUserInputAnswers, derivePendingUserInputProgress, setPendingUserInputCustomAnswer, type PendingUserInputDraftAnswer,} from "../pendingUserInput";import { useStore } from "../store";import { useProjectById, useThreadById } from "../storeSelectors";import { useUiStateStore } from "../uiStateStore";import { buildPlanImplementationThreadTitle, buildPlanImplementationPrompt, proposedPlanTitle, resolvePlanFollowUpSubmission,} from "../proposedPlan";import { DEFAULT_INTERACTION_MODE, DEFAULT_RUNTIME_MODE, DEFAULT_THREAD_TERMINAL_ID, MAX_TERMINALS_PER_GROUP, type ChatMessage, type SessionPhase, type Thread, type TurnDiffSummary,} from "../types";import { LRUCache } from "../lib/lruCache"; import { basenameOfPath } from "../vscode-icons";import { useTheme } from "../hooks/useTheme";import { useTurnDiffSummaries } from "../hooks/useTurnDiffSummaries";import BranchToolbar from "./BranchToolbar";import { resolveShortcutCommand, shortcutLabelForCommand } from "../keybindings";import PlanSidebar from "./PlanSidebar";import ThreadTerminalDrawer from "./ThreadTerminalDrawer";import { BotIcon, ChevronDownIcon, ChevronLeftIcon, ChevronRightIcon, CircleAlertIcon, ListTodoIcon, LockIcon, LockOpenIcon, XIcon,} from "lucide-react";import { Button } from "./ui/button";import { Separator } from "./ui/separator";import { cn, randomUUID } from "~/lib/utils";import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";import { toastManager } from "./ui/toast";import { decodeProjectScriptKeybindingRule } from "~/lib/projectScriptKeybindings";import { type NewProjectScriptInput } from "./ProjectScriptsControl";import { commandForProjectScript, nextProjectScriptId, projectScriptIdFromCommand,} from "~/projectScripts";import { SidebarTrigger } from "./ui/sidebar";import { newCommandId, newMessageId, newThreadId } from "~/lib/utils";import { readNativeApi } from "~/nativeApi";import { getProviderModelCapabilities, getProviderModels, resolveSelectableProvider,} from "../providerModels";import { useSettings } from "../hooks/useSettings";import { resolveAppModelSelection } from "../modelSelection";import { isTerminalFocused } from "../lib/terminalFocus";import { type ComposerImageAttachment, type DraftThreadEnvMode, type PersistedComposerImageAttachment, useComposerDraftStore, useEffectiveComposerModelState, useComposerThreadDraft,} from "../composerDraftStore";import { appendTerminalContextsToPrompt, formatTerminalContextLabel, insertInlineTerminalContextPlaceholder, removeInlineTerminalContextPlaceholder, type TerminalContextDraft, type TerminalContextSelection,} from "../lib/terminalContext";import { deriveLatestContextWindowSnapshot } from "../lib/contextWindow";import { resolveComposerFooterContentWidth, shouldForceCompactComposerFooterForFit, shouldUseCompactComposerPrimaryActions, shouldUseCompactComposerFooter,} from "./composerFooterLayout";import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore";import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor";import { PullRequestThreadDialog } from "./PullRequestThreadDialog";import { MessagesTimeline } from "./chat/MessagesTimeline";import { ChatHeader } from "./chat/ChatHeader";import { ContextWindowMeter } from "./chat/ContextWindowMeter";import { buildExpandedImagePreview, ExpandedImagePreview } from "./chat/ExpandedImagePreview";import { AVAILABLE_PROVIDER_OPTIONS, ProviderModelPicker } from "./chat/ProviderModelPicker";import { ComposerCommandItem, ComposerCommandMenu } from "./chat/ComposerCommandMenu";import { ComposerPendingApprovalActions } from "./chat/ComposerPendingApprovalActions";import { CompactComposerControlsMenu } from "./chat/CompactComposerControlsMenu";import { ComposerPrimaryActions } from "./chat/ComposerPrimaryActions";import { ComposerPendingApprovalPanel } from "./chat/ComposerPendingApprovalPanel";import { ComposerPendingUserInputPanel } from "./chat/ComposerPendingUserInputPanel";import { ComposerPlanFollowUpBanner } from "./chat/ComposerPlanFollowUpBanner";import { getComposerProviderState, renderProviderTraitsMenuContent, renderProviderTraitsPicker,} from "./chat/composerProviderRegistry";import { ProviderStatusBanner } from "./chat/ProviderStatusBanner";import { ThreadErrorBanner } from "./chat/ThreadErrorBanner";import { MAX_HIDDEN_MOUNTED_TERMINAL_THREADS, buildExpiredTerminalContextToastCopy, buildLocalDraftThread, buildTemporaryWorktreeBranchName, cloneComposerImageForRetry, collectUserMessageBlobPreviewUrls, createLocalDispatchSnapshot, deriveComposerSendState, hasServerAcknowledgedLocalDispatch, LAST_INVOKED_SCRIPT_BY_PROJECT_KEY, LastInvokedScriptByProjectSchema, type LocalDispatchSnapshot, PullRequestDialogState, readFileAsDataUrl, reconcileMountedTerminalThreadIds, revokeBlobPreviewUrl, revokeUserMessagePreviewUrls, threadHasStarted, waitForStartedServerThread,} from "./ChatView.logic";import { useLocalStorage } from "~/hooks/useLocalStorage";import { useServerAvailableEditors, useServerConfig, useServerKeybindings,} from "~/rpc/serverState";import { sanitizeThreadErrorMessage } from "~/rpc/transportError"; const ATTACHMENT_PREVIEW_HANDOFF_TTL_MS = 5000;const IMAGE_SIZE_LIMIT_LABEL = `${Math.round(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / (1024 * 1024))}MB`;const IMAGE_ONLY_BOOTSTRAP_PROMPT = "[User attached one or more images without additional text. Respond using the conversation context and the attached image(s).]";const EMPTY_ACTIVITIES: OrchestrationThreadActivity[] = [];const EMPTY_PROJECT_ENTRIES: ProjectEntry[] = [];const EMPTY_PROVIDERS: ServerProvider[] = [];const EMPTY_PENDING_USER_INPUT_ANSWERS: Record<string, PendingUserInputDraftAnswer> = {}; type ThreadPlanCatalogEntry = Pick<Thread, "id" | "proposedPlans">; const MAX_THREAD_PLAN_CATALOG_CACHE_ENTRIES = 500;const MAX_THREAD_PLAN_CATALOG_CACHE_MEMORY_BYTES = 512 * 1024;const threadPlanCatalogCache = new LRUCache<{ proposedPlans: Thread["proposedPlans"]; entry: ThreadPlanCatalogEntry;}>(MAX_THREAD_PLAN_CATALOG_CACHE_ENTRIES, MAX_THREAD_PLAN_CATALOG_CACHE_MEMORY_BYTES); function estimateThreadPlanCatalogEntrySize(thread: Thread): number { return Math.max( 64, thread.id.length + thread.proposedPlans.reduce( (total, plan) => total + plan.id.length + plan.planMarkdown.length + plan.updatedAt.length + (plan.turnId?.length ?? 0), 0, ), );} function toThreadPlanCatalogEntry(thread: Thread): ThreadPlanCatalogEntry { const cached = threadPlanCatalogCache.get(thread.id); if (cached && cached.proposedPlans === thread.proposedPlans) { return cached.entry; } const entry: ThreadPlanCatalogEntry = { id: thread.id, proposedPlans: thread.proposedPlans, }; threadPlanCatalogCache.set( thread.id, { proposedPlans: thread.proposedPlans, entry, }, estimateThreadPlanCatalogEntrySize(thread), ); return entry;} function useThreadPlanCatalog(threadIds: readonly ThreadId[]): ThreadPlanCatalogEntry[] { const selector = useMemo(() => { let previousThreads: Array<Thread | undefined> | null = null; let previousEntries: ThreadPlanCatalogEntry[] = []; return (state: { threads: Thread[] }): ThreadPlanCatalogEntry[] => { const nextThreads = threadIds.map((threadId) => state.threads.find((thread) => thread.id === threadId), ); const cachedThreads = previousThreads; if ( cachedThreads && nextThreads.length === cachedThreads.length && nextThreads.every((thread, index) => thread === cachedThreads[index]) ) { return previousEntries; } previousThreads = nextThreads; previousEntries = nextThreads.flatMap((thread) => thread ? [toThreadPlanCatalogEntry(thread)] : [], ); return previousEntries; }; }, [threadIds]); return useStore(selector);} function formatOutgoingPrompt(params: { provider: ProviderKind; model: string | null; models: ReadonlyArray<ServerProvider["models"][number]>; effort: string | null; text: string;}): string { const caps = getProviderModelCapabilities(params.models, params.model, params.provider); if (params.effort && caps.promptInjectedEffortLevels.includes(params.effort)) { return applyClaudePromptEffortPrefix(params.text, params.effort as ClaudeCodeEffort | null); } return params.text;}const COMPOSER_PATH_QUERY_DEBOUNCE_MS = 120;const SCRIPT_TERMINAL_COLS = 120;const SCRIPT_TERMINAL_ROWS = 30; const extendReplacementRangeForTrailingSpace = ( text: string, rangeEnd: number, replacement: string,): number => { if (!replacement.endsWith(" ")) { return rangeEnd; } return text[rangeEnd] === " " ? rangeEnd + 1 : rangeEnd;}; const syncTerminalContextsByIds = ( contexts: ReadonlyArray<TerminalContextDraft>, ids: ReadonlyArray<string>,): TerminalContextDraft[] => { const contextsById = new Map(contexts.map((context) => [context.id, context])); return ids.flatMap((id) => { const context = contextsById.get(id); return context ? [context] : []; });}; const terminalContextIdListsEqual = ( contexts: ReadonlyArray<TerminalContextDraft>, ids: ReadonlyArray<string>,): boolean => contexts.length === ids.length && contexts.every((context, index) => context.id === ids[index]); interface ChatViewProps { threadId: ThreadId;} 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 beginLocalDispatch = useCallback( (options?: { preparingWorktree?: boolean }) => { const preparingWorktree = Boolean(options?.preparingWorktree); setLocalDispatch((current) => { if (current) { return current.preparingWorktree === preparingWorktree ? current : { ...current, preparingWorktree }; } return createLocalDispatchSnapshot(input.activeThread, options); }); }, [input.activeThread], ); const resetLocalDispatch = useCallback(() => { setLocalDispatch(null); }, []); const serverAcknowledgedLocalDispatch = useMemo( () => hasServerAcknowledgedLocalDispatch({ localDispatch, phase: input.phase, latestTurn: input.activeLatestTurn, 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, localDispatch, ], ); useEffect(() => { if (!serverAcknowledgedLocalDispatch) { return; } resetLocalDispatch(); }, [resetLocalDispatch, serverAcknowledgedLocalDispatch]); return { beginLocalDispatch, resetLocalDispatch, localDispatchStartedAt: localDispatch?.startedAt ?? null, isPreparingWorktree: localDispatch?.preparingWorktree ?? false, isSendBusy: localDispatch !== null && !serverAcknowledgedLocalDispatch, };} interface PersistentThreadTerminalDrawerProps { threadId: ThreadId; visible: boolean; launchContext: PersistentTerminalLaunchContext | null; focusRequestId: number; splitShortcutLabel: string | undefined; newShortcutLabel: string | undefined; closeShortcutLabel: string | undefined; onAddTerminalContext: (selection: TerminalContextSelection) => void;} function PersistentThreadTerminalDrawer({ threadId, visible, launchContext, focusRequestId, splitShortcutLabel, newShortcutLabel, closeShortcutLabel, onAddTerminalContext,}: PersistentThreadTerminalDrawerProps) { const serverThread = useThreadById(threadId); const draftThread = useComposerDraftStore( (store) => store.draftThreadsByThreadId[threadId] ?? null, ); const project = useProjectById(serverThread?.projectId ?? draftThread?.projectId); const terminalState = useTerminalStateStore((state) => selectThreadTerminalState(state.terminalStateByThreadId, threadId), ); const storeSetTerminalHeight = useTerminalStateStore((state) => state.setTerminalHeight); const storeSplitTerminal = useTerminalStateStore((state) => state.splitTerminal); const storeNewTerminal = useTerminalStateStore((state) => state.newTerminal); const storeSetActiveTerminal = useTerminalStateStore((state) => state.setActiveTerminal); const storeCloseTerminal = useTerminalStateStore((state) => state.closeTerminal); 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.cwd }, worktreePath: effectiveWorktreePath, }) : null), [effectiveWorktreePath, launchContext?.cwd, project], ); const runtimeEnv = useMemo( () => project ? projectScriptRuntimeEnv({ project: { cwd: project.cwd }, worktreePath: effectiveWorktreePath, }) : {}, [effectiveWorktreePath, project], ); const bumpFocusRequestId = useCallback(() => { if (!visible) { return; } setLocalFocusRequestId((value) => value + 1); }, [visible]); const setTerminalHeight = useCallback( (height: number) => { storeSetTerminalHeight(threadId, height); }, [storeSetTerminalHeight, threadId], ); const splitTerminal = useCallback(() => { storeSplitTerminal(threadId, `terminal-${randomUUID()}`); bumpFocusRequestId(); }, [bumpFocusRequestId, storeSplitTerminal, threadId]); const createNewTerminal = useCallback(() => { storeNewTerminal(threadId, `terminal-${randomUUID()}`); bumpFocusRequestId(); }, [bumpFocusRequestId, storeNewTerminal, threadId]); const activateTerminal = useCallback( (terminalId: string) => { storeSetActiveTerminal(threadId, terminalId);Lines 1–500 of 4542. Load more
import { type ApprovalRequestId, DEFAULT_MODEL_BY_PROVIDER, type ClaudeCodeEffort, type MessageId, type ModelSelection, type ProjectScript, type ProviderKind, type ProjectEntry, type ProjectId, type ProviderApprovalDecision, PROVIDER_SEND_TURN_MAX_ATTACHMENTS, PROVIDER_SEND_TURN_MAX_IMAGE_BYTES, type ServerProvider, type ThreadId, type TurnId, type KeybindingCommand, OrchestrationThreadActivity, ProviderInteractionMode, RuntimeMode, TerminalOpenInput, } from "@t3tools/contracts"; import { applyClaudePromptEffortPrefix, normalizeModelSlug } from "@t3tools/shared/model"; import { projectScriptCwd, projectScriptRuntimeEnv } from "@t3tools/shared/projectScripts"; import { truncate } from "@t3tools/shared/String"; import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import { useQuery } from "@tanstack/react-query"; import { useDebouncedValue } from "@tanstack/react-pacer"; import { useNavigate, useSearch } from "@tanstack/react-router"; import { useGitStatus } from "~/lib/gitStatusState"; import { projectSearchEntriesQueryOptions } from "~/lib/projectReactQuery"; import { isElectron } from "../env"; import { parseDiffRouteSearch, stripDiffSearchParams } from "../diffRouteSearch"; import { clampCollapsedComposerCursor, type ComposerTrigger, collapseExpandedComposerCursor, detectComposerTrigger, expandCollapsedComposerCursor, parseStandaloneComposerSlashCommand, replaceTextRange, } from "../composer-logic"; import { deriveCompletionDividerBeforeEntryId, derivePendingApprovals, derivePendingUserInputs, derivePhase, deriveTimelineEntries, deriveActiveWorkStartedAt, deriveActivePlanState, findSidebarProposedPlan, findLatestProposedPlan, deriveWorkLogEntries, hasActionableProposedPlan, hasToolActivityForTurn, isLatestTurnSettled, formatElapsed, } from "../session-logic"; import { isScrollContainerNearBottom } from "../chat-scroll"; import { buildPendingUserInputAnswers, derivePendingUserInputProgress, setPendingUserInputCustomAnswer, type PendingUserInputDraftAnswer, } from "../pendingUserInput"; import { useStore } from "../store"; import { useProjectById, useThreadById } from "../storeSelectors"; import { useUiStateStore } from "../uiStateStore"; import { buildPlanImplementationThreadTitle, buildPlanImplementationPrompt, proposedPlanTitle, resolvePlanFollowUpSubmission, } from "../proposedPlan"; import { DEFAULT_INTERACTION_MODE, DEFAULT_RUNTIME_MODE, DEFAULT_THREAD_TERMINAL_ID, MAX_TERMINALS_PER_GROUP, type ChatMessage, type SessionPhase, type Thread, type TurnDiffSummary, } from "../types"; import { LRUCache } from "../lib/lruCache"; import { basenameOfPath } from "../vscode-icons"; import { useTheme } from "../hooks/useTheme"; import { useTurnDiffSummaries } from "../hooks/useTurnDiffSummaries"; import BranchToolbar from "./BranchToolbar"; import { resolveShortcutCommand, shortcutLabelForCommand } from "../keybindings"; import PlanSidebar from "./PlanSidebar"; import ThreadTerminalDrawer from "./ThreadTerminalDrawer"; import { BotIcon, ChevronDownIcon, ChevronLeftIcon, ChevronRightIcon, CircleAlertIcon, ListTodoIcon, LockIcon, LockOpenIcon, XIcon, } from "lucide-react"; import { Button } from "./ui/button"; import { Separator } from "./ui/separator"; import { cn, randomUUID } from "~/lib/utils"; import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; import { toastManager } from "./ui/toast"; import { decodeProjectScriptKeybindingRule } from "~/lib/projectScriptKeybindings"; import { type NewProjectScriptInput } from "./ProjectScriptsControl"; import { commandForProjectScript, nextProjectScriptId, projectScriptIdFromCommand, } from "~/projectScripts"; import { SidebarTrigger } from "./ui/sidebar"; import { newCommandId, newMessageId, newThreadId } from "~/lib/utils"; import { readNativeApi } from "~/nativeApi"; import { getProviderModelCapabilities, getProviderModels, resolveSelectableProvider, } from "../providerModels"; import { useSettings } from "../hooks/useSettings"; import { resolveAppModelSelection } from "../modelSelection"; import { isTerminalFocused } from "../lib/terminalFocus"; import { type ComposerImageAttachment, type DraftThreadEnvMode, type PersistedComposerImageAttachment, useComposerDraftStore, useEffectiveComposerModelState, useComposerThreadDraft, } from "../composerDraftStore"; import { appendTerminalContextsToPrompt, formatTerminalContextLabel, insertInlineTerminalContextPlaceholder, removeInlineTerminalContextPlaceholder, type TerminalContextDraft, type TerminalContextSelection, } from "../lib/terminalContext"; import { deriveLatestContextWindowSnapshot } from "../lib/contextWindow"; import { resolveComposerFooterContentWidth, shouldForceCompactComposerFooterForFit, shouldUseCompactComposerPrimaryActions, shouldUseCompactComposerFooter, } from "./composerFooterLayout"; import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore"; import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor"; import { PullRequestThreadDialog } from "./PullRequestThreadDialog"; import { MessagesTimeline } from "./chat/MessagesTimeline"; import { ChatHeader } from "./chat/ChatHeader"; import { ContextWindowMeter } from "./chat/ContextWindowMeter"; import { buildExpandedImagePreview, ExpandedImagePreview } from "./chat/ExpandedImagePreview"; import { AVAILABLE_PROVIDER_OPTIONS, ProviderModelPicker } from "./chat/ProviderModelPicker"; import { ComposerCommandItem, ComposerCommandMenu } from "./chat/ComposerCommandMenu"; import { ComposerPendingApprovalActions } from "./chat/ComposerPendingApprovalActions"; import { CompactComposerControlsMenu } from "./chat/CompactComposerControlsMenu"; import { ComposerPrimaryActions } from "./chat/ComposerPrimaryActions"; import { ComposerPendingApprovalPanel } from "./chat/ComposerPendingApprovalPanel"; import { ComposerPendingUserInputPanel } from "./chat/ComposerPendingUserInputPanel"; import { ComposerPlanFollowUpBanner } from "./chat/ComposerPlanFollowUpBanner"; import { getComposerProviderState, renderProviderTraitsMenuContent, renderProviderTraitsPicker, } from "./chat/composerProviderRegistry"; import { ProviderStatusBanner } from "./chat/ProviderStatusBanner"; import { ThreadErrorBanner } from "./chat/ThreadErrorBanner"; import { MAX_HIDDEN_MOUNTED_TERMINAL_THREADS, buildExpiredTerminalContextToastCopy, buildLocalDraftThread, buildTemporaryWorktreeBranchName, cloneComposerImageForRetry, collectUserMessageBlobPreviewUrls, createLocalDispatchSnapshot, deriveComposerSendState, hasServerAcknowledgedLocalDispatch, LAST_INVOKED_SCRIPT_BY_PROJECT_KEY, LastInvokedScriptByProjectSchema, type LocalDispatchSnapshot, PullRequestDialogState, readFileAsDataUrl, reconcileMountedTerminalThreadIds, revokeBlobPreviewUrl, revokeUserMessagePreviewUrls, threadHasStarted, waitForStartedServerThread, } from "./ChatView.logic"; import { useLocalStorage } from "~/hooks/useLocalStorage"; import { useServerAvailableEditors, useServerConfig, useServerKeybindings, } from "~/rpc/serverState"; import { sanitizeThreadErrorMessage } from "~/rpc/transportError"; const ATTACHMENT_PREVIEW_HANDOFF_TTL_MS = 5000; const IMAGE_SIZE_LIMIT_LABEL = `${Math.round(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / (1024 * 1024))}MB`; const IMAGE_ONLY_BOOTSTRAP_PROMPT = "[User attached one or more images without additional text. Respond using the conversation context and the attached image(s).]"; const EMPTY_ACTIVITIES: OrchestrationThreadActivity[] = []; const EMPTY_PROJECT_ENTRIES: ProjectEntry[] = []; const EMPTY_PROVIDERS: ServerProvider[] = []; const EMPTY_PENDING_USER_INPUT_ANSWERS: Record<string, PendingUserInputDraftAnswer> = {}; type ThreadPlanCatalogEntry = Pick<Thread, "id" | "proposedPlans">; const MAX_THREAD_PLAN_CATALOG_CACHE_ENTRIES = 500; const MAX_THREAD_PLAN_CATALOG_CACHE_MEMORY_BYTES = 512 * 1024; const threadPlanCatalogCache = new LRUCache<{ proposedPlans: Thread["proposedPlans"]; entry: ThreadPlanCatalogEntry; }>(MAX_THREAD_PLAN_CATALOG_CACHE_ENTRIES, MAX_THREAD_PLAN_CATALOG_CACHE_MEMORY_BYTES); function estimateThreadPlanCatalogEntrySize(thread: Thread): number { return Math.max( 64, thread.id.length + thread.proposedPlans.reduce( (total, plan) => total + plan.id.length + plan.planMarkdown.length + plan.updatedAt.length + (plan.turnId?.length ?? 0), 0, ), ); } function toThreadPlanCatalogEntry(thread: Thread): ThreadPlanCatalogEntry { const cached = threadPlanCatalogCache.get(thread.id); if (cached && cached.proposedPlans === thread.proposedPlans) { return cached.entry; } const entry: ThreadPlanCatalogEntry = { id: thread.id, proposedPlans: thread.proposedPlans, }; threadPlanCatalogCache.set( thread.id, { proposedPlans: thread.proposedPlans, entry, }, estimateThreadPlanCatalogEntrySize(thread), ); return entry; } function useThreadPlanCatalog(threadIds: readonly ThreadId[]): ThreadPlanCatalogEntry[] { const selector = useMemo(() => { let previousThreads: Array<Thread | undefined> | null = null; let previousEntries: ThreadPlanCatalogEntry[] = []; return (state: { threads: Thread[] }): ThreadPlanCatalogEntry[] => { const nextThreads = threadIds.map((threadId) => state.threads.find((thread) => thread.id === threadId), ); const cachedThreads = previousThreads; if ( cachedThreads && nextThreads.length === cachedThreads.length && nextThreads.every((thread, index) => thread === cachedThreads[index]) ) { return previousEntries; } previousThreads = nextThreads; previousEntries = nextThreads.flatMap((thread) => thread ? [toThreadPlanCatalogEntry(thread)] : [], ); return previousEntries; }; }, [threadIds]); return useStore(selector); } function formatOutgoingPrompt(params: { provider: ProviderKind; model: string | null; models: ReadonlyArray<ServerProvider["models"][number]>; effort: string | null; text: string; }): string { const caps = getProviderModelCapabilities(params.models, params.model, params.provider); if (params.effort && caps.promptInjectedEffortLevels.includes(params.effort)) { return applyClaudePromptEffortPrefix(params.text, params.effort as ClaudeCodeEffort | null); } return params.text; } const COMPOSER_PATH_QUERY_DEBOUNCE_MS = 120; const SCRIPT_TERMINAL_COLS = 120; const SCRIPT_TERMINAL_ROWS = 30; const extendReplacementRangeForTrailingSpace = ( text: string, rangeEnd: number, replacement: string, ): number => { if (!replacement.endsWith(" ")) { return rangeEnd; } return text[rangeEnd] === " " ? rangeEnd + 1 : rangeEnd; }; const syncTerminalContextsByIds = ( contexts: ReadonlyArray<TerminalContextDraft>, ids: ReadonlyArray<string>, ): TerminalContextDraft[] => { const contextsById = new Map(contexts.map((context) => [context.id, context])); return ids.flatMap((id) => { const context = contextsById.get(id); return context ? [context] : []; }); }; const terminalContextIdListsEqual = ( contexts: ReadonlyArray<TerminalContextDraft>, ids: ReadonlyArray<string>, ): boolean => contexts.length === ids.length && contexts.every((context, index) => context.id === ids[index]); interface ChatViewProps { threadId: ThreadId; } 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 beginLocalDispatch = useCallback( (options?: { preparingWorktree?: boolean }) => { const preparingWorktree = Boolean(options?.preparingWorktree); setLocalDispatch((current) => { if (current) { return current.preparingWorktree === preparingWorktree ? current : { ...current, preparingWorktree }; } return createLocalDispatchSnapshot(input.activeThread, options); }); }, [input.activeThread], ); const resetLocalDispatch = useCallback(() => { setLocalDispatch(null); }, []); const serverAcknowledgedLocalDispatch = useMemo( () => hasServerAcknowledgedLocalDispatch({ localDispatch, phase: input.phase, latestTurn: input.activeLatestTurn, 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, localDispatch, ], ); useEffect(() => { if (!serverAcknowledgedLocalDispatch) { return; } resetLocalDispatch(); }, [resetLocalDispatch, serverAcknowledgedLocalDispatch]); return { beginLocalDispatch, resetLocalDispatch, localDispatchStartedAt: localDispatch?.startedAt ?? null, isPreparingWorktree: localDispatch?.preparingWorktree ?? false, isSendBusy: localDispatch !== null && !serverAcknowledgedLocalDispatch, }; } interface PersistentThreadTerminalDrawerProps { threadId: ThreadId; visible: boolean; launchContext: PersistentTerminalLaunchContext | null; focusRequestId: number; splitShortcutLabel: string | undefined; newShortcutLabel: string | undefined; closeShortcutLabel: string | undefined; onAddTerminalContext: (selection: TerminalContextSelection) => void; } function PersistentThreadTerminalDrawer({ threadId, visible, launchContext, focusRequestId, splitShortcutLabel, newShortcutLabel, closeShortcutLabel, onAddTerminalContext, }: PersistentThreadTerminalDrawerProps) { const serverThread = useThreadById(threadId); const draftThread = useComposerDraftStore( (store) => store.draftThreadsByThreadId[threadId] ?? null, ); const project = useProjectById(serverThread?.projectId ?? draftThread?.projectId); const terminalState = useTerminalStateStore((state) => selectThreadTerminalState(state.terminalStateByThreadId, threadId), ); const storeSetTerminalHeight = useTerminalStateStore((state) => state.setTerminalHeight); const storeSplitTerminal = useTerminalStateStore((state) => state.splitTerminal); const storeNewTerminal = useTerminalStateStore((state) => state.newTerminal); const storeSetActiveTerminal = useTerminalStateStore((state) => state.setActiveTerminal); const storeCloseTerminal = useTerminalStateStore((state) => state.closeTerminal); 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.cwd }, worktreePath: effectiveWorktreePath, }) : null), [effectiveWorktreePath, launchContext?.cwd, project], ); const runtimeEnv = useMemo( () => project ? projectScriptRuntimeEnv({ project: { cwd: project.cwd }, worktreePath: effectiveWorktreePath, }) : {}, [effectiveWorktreePath, project], ); const bumpFocusRequestId = useCallback(() => { if (!visible) { return; } setLocalFocusRequestId((value) => value + 1); }, [visible]); const setTerminalHeight = useCallback( (height: number) => { storeSetTerminalHeight(threadId, height); }, [storeSetTerminalHeight, threadId], ); const splitTerminal = useCallback(() => { storeSplitTerminal(threadId, `terminal-${randomUUID()}`); bumpFocusRequestId(); }, [bumpFocusRequestId, storeSplitTerminal, threadId]); const createNewTerminal = useCallback(() => { storeNewTerminal(threadId, `terminal-${randomUUID()}`); bumpFocusRequestId(); }, [bumpFocusRequestId, storeNewTerminal, threadId]); const activateTerminal = useCallback( (terminalId: string) => { storeSetActiveTerminal(threadId, terminalId);