From e36bef8a16de0cb6ec5edbf42ab2ef69b3104007 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 29 Apr 2025 15:24:49 -0700 Subject: [PATCH] edit mode state updates --- gui/src/components/AcceptRejectAllButtons.tsx | 23 +----- gui/src/components/Layout.tsx | 17 ++-- .../OnboardingLocalTab.tsx | 4 +- .../platform/PlatformOnboardingCard.tsx | 2 +- .../components/OnboardingCard/tabs/index.ts | 2 +- .../StepContainer/ResponseActions.tsx | 5 +- .../StepContainerPreToolbar/index.tsx | 10 +-- .../dialogs/FreeTrialOverDialog.tsx | 9 +-- .../components/mainInput/ContinueInputBox.tsx | 52 +++++++------ gui/src/components/mainInput/InputToolbar.tsx | 15 ++-- .../mainInput/TipTapEditor/TipTapEditor.tsx | 11 ++- .../useMainEditorWebviewListeners.ts | 6 +- .../TipTapEditor/utils/editorConfig.ts | 21 ++--- .../components/modelSelection/ModeSelect.tsx | 8 +- .../components/modelSelection/ModelSelect.tsx | 23 +++--- gui/src/pages/gui/Chat.tsx | 67 ++++++---------- gui/src/pages/gui/EmptyChatBody.tsx | 9 +-- gui/src/pages/gui/StreamError.tsx | 21 ++--- gui/src/redux/selectors/index.ts | 3 - gui/src/redux/slices/editModeState.ts | 72 ++++++++++++++++- gui/src/redux/slices/sessionSlice.ts | 77 +------------------ gui/src/redux/store.ts | 3 +- gui/src/redux/thunks/exitEditMode.ts | 21 ++--- gui/src/util/getMultifileEditPrompt.ts | 62 --------------- 24 files changed, 194 insertions(+), 349 deletions(-) rename gui/src/components/OnboardingCard/{tabs => components}/OnboardingLocalTab.tsx (97%) delete mode 100644 gui/src/util/getMultifileEditPrompt.ts diff --git a/gui/src/components/AcceptRejectAllButtons.tsx b/gui/src/components/AcceptRejectAllButtons.tsx index 7a9c28062..5ffbabbf1 100644 --- a/gui/src/components/AcceptRejectAllButtons.tsx +++ b/gui/src/components/AcceptRejectAllButtons.tsx @@ -2,8 +2,6 @@ import { CheckIcon, XMarkIcon } from "@heroicons/react/24/outline"; import { ApplyState } from "core"; import { useContext } from "react"; import { IdeMessengerContext } from "../context/IdeMessenger"; -import { useAppSelector } from "../redux/hooks"; -import { selectIsSingleRangeEditOrInsertion } from "../redux/slices/sessionSlice"; import { getMetaKeyLabel } from "../util"; export interface AcceptRejectAllButtonsProps { @@ -18,7 +16,6 @@ export default function AcceptRejectAllButtons({ onAcceptOrReject, }: AcceptRejectAllButtonsProps) { const ideMessenger = useContext(IdeMessengerContext); - const isSingleRangeEdit = useAppSelector(selectIsSingleRangeEditOrInsertion); async function handleAcceptOrReject(status: AcceptOrRejectOutcome) { for (const { filepath = "", streamId } of pendingApplyStates) { @@ -41,15 +38,7 @@ export default function AcceptRejectAllButtons({ data-testid="edit-reject-button" > - {isSingleRangeEdit ? ( - Reject ({getMetaKeyLabel()}⇧⌫) - ) : ( - <> - Reject - Reject all - Reject all changes - - )} + Reject ({getMetaKeyLabel()}⇧⌫) ); diff --git a/gui/src/components/Layout.tsx b/gui/src/components/Layout.tsx index 0c5acda83..7844a3e04 100644 --- a/gui/src/components/Layout.tsx +++ b/gui/src/components/Layout.tsx @@ -6,13 +6,12 @@ import { AuthProvider } from "../context/Auth"; import { LocalStorageProvider } from "../context/LocalStorage"; import { useWebviewListener } from "../hooks/useWebviewListener"; import { useAppDispatch, useAppSelector } from "../redux/hooks"; -import { focusEdit, setEditStatus } from "../redux/slices/editModeState"; import { addCodeToEdit, - newSession, - selectIsInEditMode, - setMode, -} from "../redux/slices/sessionSlice"; + focusEdit, + setEditStatus, +} from "../redux/slices/editModeState"; +import { newSession, setMode } from "../redux/slices/sessionSlice"; import { setShowDialog } from "../redux/slices/uiSlice"; import { exitEditMode } from "../redux/thunks"; import { loadLastSession, saveCurrentSession } from "../redux/thunks/session"; @@ -172,7 +171,7 @@ const Layout = () => { async (payload) => { dispatch(addCodeToEdit(payload)); }, - [navigate], + [], ); useWebviewListener( @@ -183,11 +182,11 @@ const Layout = () => { [], ); - const isInEditMode = useAppSelector(selectIsInEditMode); + const mode = useAppSelector((store) => store.session.mode); useWebviewListener( "exitEditMode", async () => { - if (!isInEditMode) { + if (mode !== "edit") { return; } dispatch( @@ -197,7 +196,7 @@ const Layout = () => { ); dispatch(exitEditMode()); }, - [isInEditMode], + [mode], ); useEffect(() => { diff --git a/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx b/gui/src/components/OnboardingCard/components/OnboardingLocalTab.tsx similarity index 97% rename from gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx rename to gui/src/components/OnboardingCard/components/OnboardingLocalTab.tsx index 7e99f430d..b0cc6f179 100644 --- a/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx +++ b/gui/src/components/OnboardingCard/components/OnboardingLocalTab.tsx @@ -12,9 +12,9 @@ import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { useAppDispatch } from "../../../redux/hooks"; import { setDialogMessage, setShowDialog } from "../../../redux/slices/uiSlice"; import { updateSelectedModelByRole } from "../../../redux/thunks"; -import OllamaModelDownload from "../components/OllamaModelDownload"; -import { OllamaStatus } from "../components/OllamaStatus"; import { useSubmitOnboarding } from "../hooks"; +import OllamaModelDownload from "./OllamaModelDownload"; +import { OllamaStatus } from "./OllamaStatus"; const OLLAMA_CHECK_INTERVAL_MS = 3000; diff --git a/gui/src/components/OnboardingCard/platform/PlatformOnboardingCard.tsx b/gui/src/components/OnboardingCard/platform/PlatformOnboardingCard.tsx index 69eed3ffb..883ea66d1 100644 --- a/gui/src/components/OnboardingCard/platform/PlatformOnboardingCard.tsx +++ b/gui/src/components/OnboardingCard/platform/PlatformOnboardingCard.tsx @@ -5,8 +5,8 @@ import { getLocalStorage, setLocalStorage } from "../../../util/localStorage"; import Alert from "../../gui/Alert"; import { ReusableCard } from "../../ReusableCard"; import { TabTitle } from "../components/OnboardingCardTabs"; +import OnboardingLocalTab from "../components/OnboardingLocalTab"; import { useOnboardingCard } from "../hooks"; -import OnboardingLocalTab from "../tabs/OnboardingLocalTab"; import MainTab from "./tabs/main"; export interface OnboardingCardState { diff --git a/gui/src/components/OnboardingCard/tabs/index.ts b/gui/src/components/OnboardingCard/tabs/index.ts index 7e975a8ec..1e6aae37d 100644 --- a/gui/src/components/OnboardingCard/tabs/index.ts +++ b/gui/src/components/OnboardingCard/tabs/index.ts @@ -1,3 +1,3 @@ +export { default as Local } from "../components/OnboardingLocalTab"; export { default as Best } from "./OnboardingBestTab"; -export { default as Local } from "./OnboardingLocalTab"; export { default as Quickstart } from "./OnboardingQuickstartTab"; diff --git a/gui/src/components/StepContainer/ResponseActions.tsx b/gui/src/components/StepContainer/ResponseActions.tsx index dce39ea6f..c49daccda 100644 --- a/gui/src/components/StepContainer/ResponseActions.tsx +++ b/gui/src/components/StepContainer/ResponseActions.tsx @@ -2,7 +2,6 @@ import { BarsArrowDownIcon, TrashIcon } from "@heroicons/react/24/outline"; import { ChatHistoryItem } from "core"; import { renderChatMessage } from "core/util/messageContent"; import { useAppSelector } from "../../redux/hooks"; -import { selectIsInEditMode } from "../../redux/slices/sessionSlice"; import FeedbackButtons from "../FeedbackButtons"; import { CopyIconButton } from "../gui/CopyIconButton"; import HeaderButtonWithToolTip from "../gui/HeaderButtonWithToolTip"; @@ -23,9 +22,9 @@ export default function ResponseActions({ isTruncated, onDelete, }: ResponseActionsProps) { - const isInEditMode = useAppSelector(selectIsInEditMode); + const mode = useAppSelector((store) => store.session.mode); - if (isInEditMode) { + if (mode === "edit") { return ; } diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx index 6b299db18..44977514f 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx @@ -11,10 +11,7 @@ import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { useIdeMessengerRequest } from "../../../hooks"; import { useWebviewListener } from "../../../hooks/useWebviewListener"; import { useAppSelector } from "../../../redux/hooks"; -import { - selectApplyStateByStreamId, - selectIsInEditMode, -} from "../../../redux/slices/sessionSlice"; +import { selectApplyStateByStreamId } from "../../../redux/slices/sessionSlice"; import { getFontSize } from "../../../util"; import { isTerminalCodeBlock } from "../utils"; import { ApplyActions } from "./ApplyActions"; @@ -74,10 +71,7 @@ export function StepContainerPreToolbar({ disableManualApply, }: StepContainerPreToolbarProps) { const ideMessenger = useContext(IdeMessengerContext); - const isInEditMode = useAppSelector(selectIsInEditMode); - const [isExpanded, setIsExpanded] = useState( - expanded ?? (isInEditMode ? false : true), - ); + const [isExpanded, setIsExpanded] = useState(expanded ?? true); const [relativeFilepathUri, setRelativeFilepathUri] = useState( null, diff --git a/gui/src/components/dialogs/FreeTrialOverDialog.tsx b/gui/src/components/dialogs/FreeTrialOverDialog.tsx index 107e351ce..a200b2f15 100644 --- a/gui/src/components/dialogs/FreeTrialOverDialog.tsx +++ b/gui/src/components/dialogs/FreeTrialOverDialog.tsx @@ -1,15 +1,12 @@ import { useEffect } from "react"; import { useDispatch } from "react-redux"; import { useAppSelector } from "../../redux/hooks"; -import { selectUseHub } from "../../redux/selectors"; import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; -import { OnboardingCard } from "../OnboardingCard"; import { PlatformOnboardingCard } from "../OnboardingCard/platform/PlatformOnboardingCard"; function FreeTrialOverDialog() { const dispatch = useDispatch(); const history = useAppSelector((store) => store.session.history); - const usePlatform = useAppSelector(selectUseHub); useEffect(() => { if (history.length === 0) { @@ -20,11 +17,7 @@ function FreeTrialOverDialog() { return (
- {usePlatform ? ( - - ) : ( - - )} +
); } diff --git a/gui/src/components/mainInput/ContinueInputBox.tsx b/gui/src/components/mainInput/ContinueInputBox.tsx index 14d4816f4..7540dae8d 100644 --- a/gui/src/components/mainInput/ContinueInputBox.tsx +++ b/gui/src/components/mainInput/ContinueInputBox.tsx @@ -11,7 +11,6 @@ import { Lump } from "./Lump"; import { TipTapEditor } from "./TipTapEditor"; interface ContinueInputBoxProps { - isEditMode?: boolean; isLastUserInput: boolean; isMainInput?: boolean; onEnter: ( @@ -82,36 +81,41 @@ function ContinueInputBox(props: ContinueInputBoxProps) { const availableContextProviders = useAppSelector( (state) => state.config.config.contextProviders, ); + const mode = useAppSelector((store) => store.session.mode); const editModeState = useAppSelector((state) => state.editModeState); - const filteredSlashCommands = props.isEditMode ? [] : availableSlashCommands; + const filteredSlashCommands = useMemo(() => { + return mode === "edit" ? [] : availableSlashCommands; + }, [mode, availableSlashCommands]); + const filteredContextProviders = useMemo(() => { - if (!props.isEditMode) { - return availableContextProviders ?? []; + if (mode === "edit") { + return ( + availableContextProviders?.filter( + (provider) => + !EDIT_DISALLOWED_CONTEXT_PROVIDERS.includes(provider.title), + ) ?? [] + ); } - return ( - availableContextProviders?.filter( - (provider) => - !EDIT_DISALLOWED_CONTEXT_PROVIDERS.includes(provider.title), - ) ?? [] - ); - }, [availableContextProviders]); + return availableContextProviders ?? []; + }, [availableContextProviders, mode]); - const historyKey = props.isEditMode ? "edit" : "chat"; - const placeholder = props.isEditMode - ? "Describe changes, '#' to add files" - : undefined; + const historyKey = mode === "edit" ? "edit" : "chat"; + const placeholder = + mode === "edit" ? "Describe changes, '#' to add files" : undefined; - const toolbarOptions: ToolbarOptions = props.isEditMode - ? { - hideAddContext: false, - hideImageUpload: false, - hideUseCodebase: true, - hideSelectModel: false, - enterText: editModeState.editStatus === "accepting" ? "Retry" : "Edit", - } - : {}; + const toolbarOptions: ToolbarOptions = + mode === "edit" + ? { + hideAddContext: false, + hideImageUpload: false, + hideUseCodebase: true, + hideSelectModel: false, + enterText: + editModeState.editStatus === "accepting" ? "Retry" : "Edit", + } + : {}; return (
(null); const defaultModel = useAppSelector(selectSelectedChatModel); const useActiveFile = useAppSelector(selectUseActiveFile); - const isInEditMode = useAppSelector(selectIsInEditMode); - const hasCodeToEdit = useAppSelector(selectHasCodeToEdit); + const mode = useAppSelector((store) => store.session.mode); + const codeToEdit = useAppSelector((store) => store.editModeState.codeToEdit); const toolCallState = useAppSelector(selectCurrentToolCall); - const isEditModeAndNoCodeToEdit = isInEditMode && !hasCodeToEdit; const currentToolCallApplyState = useAppSelector( selectCurrentToolCallApplyState, ); const isEnterDisabled = props.disabled || - isEditModeAndNoCodeToEdit || + (mode === "edit" && codeToEdit.length === 0) || toolCallState?.status === "generated" || (currentToolCallApplyState && currentToolCallApplyState.status !== "closed"); @@ -158,7 +153,7 @@ function InputToolbar(props: InputToolbarProps) { fontSize: tinyFont, }} > - {!props.toolbarOptions?.hideUseCodebase && !isInEditMode && ( + {!props.toolbarOptions?.hideUseCodebase && mode !== "edit" && ( )} - {isInEditMode && ( + {mode === "edit" && ( { diff --git a/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx index f5d172dc2..b9f341032 100644 --- a/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor/TipTapEditor.tsx @@ -7,7 +7,6 @@ import useIsOSREnabled from "../../../hooks/useIsOSREnabled"; import useUpdatingRef from "../../../hooks/useUpdatingRef"; import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; import { selectSelectedChatModel } from "../../../redux/slices/configSlice"; -import { selectIsInEditMode } from "../../../redux/slices/sessionSlice"; import InputToolbar, { ToolbarOptions } from "../InputToolbar"; import { ComboBoxItem } from "../types"; import { DragOverlay, InputBoxDiv } from "./components"; @@ -48,9 +47,9 @@ export function TipTapEditor(props: TipTapEditorProps) { const defaultModel = useAppSelector(selectSelectedChatModel); const isStreaming = useAppSelector((state) => state.session.isStreaming); - const isInEditMode = useAppSelector(selectIsInEditMode); const historyLength = useAppSelector((store) => store.session.history.length); + const mode = useAppSelector((store) => store.session.mode); const { editor, onEnterRef } = createEditorConfig({ props, ideMessenger, @@ -82,13 +81,13 @@ export function TipTapEditor(props: TipTapEditorProps) { }, [editor, props.placeholder, historyLength]); useEffect(() => { - if (isInEditMode) { + if (mode === "edit") { setShouldHideToolbar(false); } if (props.isMainInput) { editor?.commands.clearContent(true); } - }, [editor, isInEditMode, props.isMainInput]); + }, [editor, mode, props.isMainInput]); const editorFocusedRef = useUpdatingRef(editor?.isFocused, [editor]); @@ -148,7 +147,7 @@ export function TipTapEditor(props: TipTapEditorProps) { const handleBlur = useCallback( (e: React.FocusEvent) => { - if (isInEditMode) { + if (mode === "edit") { return; } // Check if the new focus target is within our InputBoxDiv @@ -163,7 +162,7 @@ export function TipTapEditor(props: TipTapEditorProps) { setShouldHideToolbar(true); }, 100); }, - [isInEditMode, blurTimeout], + [mode, blurTimeout], ); const handleFocus = useCallback(() => { diff --git a/gui/src/components/mainInput/TipTapEditor/useMainEditorWebviewListeners.ts b/gui/src/components/mainInput/TipTapEditor/useMainEditorWebviewListeners.ts index 400f08019..15fd50a18 100644 --- a/gui/src/components/mainInput/TipTapEditor/useMainEditorWebviewListeners.ts +++ b/gui/src/components/mainInput/TipTapEditor/useMainEditorWebviewListeners.ts @@ -3,10 +3,8 @@ import { InputModifiers } from "core"; import { rifWithContentsToContextItem } from "core/commands/util"; import { MutableRefObject } from "react"; import { useWebviewListener } from "../../../hooks/useWebviewListener"; -import { - clearCodeToEdit, - setNewestToolbarPreviewForInput, -} from "../../../redux/slices/sessionSlice"; +import { clearCodeToEdit } from "../../../redux/slices/editModeState"; +import { setNewestToolbarPreviewForInput } from "../../../redux/slices/sessionSlice"; import { AppDispatch } from "../../../redux/store"; import { loadSession, saveCurrentSession } from "../../../redux/thunks/session"; import { CodeBlock, PromptBlock } from "./extensions"; diff --git a/gui/src/components/mainInput/TipTapEditor/utils/editorConfig.ts b/gui/src/components/mainInput/TipTapEditor/utils/editorConfig.ts index c4a70bb51..340190683 100644 --- a/gui/src/components/mainInput/TipTapEditor/utils/editorConfig.ts +++ b/gui/src/components/mainInput/TipTapEditor/utils/editorConfig.ts @@ -22,11 +22,7 @@ import useUpdatingRef from "../../../../hooks/useUpdatingRef"; import { useAppSelector } from "../../../../redux/hooks"; import { selectUseActiveFile } from "../../../../redux/selectors"; import { selectSelectedChatModel } from "../../../../redux/slices/configSlice"; -import { - addCodeToEdit, - selectHasCodeToEdit, - selectIsInEditMode, -} from "../../../../redux/slices/sessionSlice"; +import { addCodeToEdit } from "../../../../redux/slices/editModeState"; import { AppDispatch } from "../../../../redux/store"; import { exitEditMode } from "../../../../redux/thunks"; import { loadLastSession } from "../../../../redux/thunks/session"; @@ -87,15 +83,14 @@ export function createEditorConfig(options: { const isStreaming = useAppSelector((state) => state.session.isStreaming); const useActiveFile = useAppSelector(selectUseActiveFile); const historyLength = useAppSelector((store) => store.session.history.length); - const isInEditMode = useAppSelector(selectIsInEditMode); - const hasCodeToEdit = useAppSelector(selectHasCodeToEdit); - const isEditModeAndNoCodeToEdit = isInEditMode && !hasCodeToEdit; + const mode = useAppSelector((store) => store.session.mode); + const modeRef = useUpdatingRef(mode); + const codeToEdit = useAppSelector((store) => store.editModeState.codeToEdit); const inSubmenuRef = useRef(undefined); const inDropdownRef = useRef(false); const defaultModelRef = useUpdatingRef(defaultModel); const isStreamingRef = useUpdatingRef(isStreaming); - const isInEditModeRef = useUpdatingRef(isInEditMode); const getSubmenuContextItemsRef = useUpdatingRef(getSubmenuContextItems); const availableContextProvidersRef = useUpdatingRef( props.availableContextProviders, @@ -273,7 +268,7 @@ export function createEditorConfig(options: { return true; } - if (inDropdownRef.current || !isInEditModeRef.current) { + if (inDropdownRef.current || modeRef.current !== "edit") { ideMessenger.post("focusEditor", undefined); return true; } @@ -336,7 +331,7 @@ export function createEditorConfig(options: { inSubmenuRef, ideMessenger, ), - allow: () => isInEditModeRef.current, + allow: () => modeRef.current !== "edit", command: async ({ editor, range, props }) => { editor.chain().focus().insertContentAt(range, "").run(); const filepath = props.id; @@ -389,7 +384,7 @@ export function createEditorConfig(options: { if (!editor) { return; } - if (isStreaming || isEditModeAndNoCodeToEdit) { + if (isStreaming || (codeToEdit.length === 0 && mode === "edit")) { return; } @@ -406,7 +401,7 @@ export function createEditorConfig(options: { props.onEnter(json, modifiers, editor); }, - [props.onEnter, editor, props.isMainInput], + [props.onEnter, editor, props.isMainInput, codeToEdit, mode], ); return { editor, onEnterRef }; diff --git a/gui/src/components/modelSelection/ModeSelect.tsx b/gui/src/components/modelSelection/ModeSelect.tsx index b4cb94545..0ab3225f9 100644 --- a/gui/src/components/modelSelection/ModeSelect.tsx +++ b/gui/src/components/modelSelection/ModeSelect.tsx @@ -14,11 +14,7 @@ import { lightGray } from ".."; import { useWebviewListener } from "../../hooks/useWebviewListener"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; import { selectSelectedChatModel } from "../../redux/slices/configSlice"; -import { - cycleMode, - selectCurrentMode, - setMode, -} from "../../redux/slices/sessionSlice"; +import { cycleMode, setMode } from "../../redux/slices/sessionSlice"; import { exitEditMode } from "../../redux/thunks"; import { loadLastSession, @@ -40,7 +36,7 @@ const ShortcutText = styled.span` function ModeSelect() { const dispatch = useAppDispatch(); - const mode = useAppSelector(selectCurrentMode); + const mode = useAppSelector((store) => store.session.mode); const selectedModel = useAppSelector(selectSelectedChatModel); const currentSession = useAppSelector((state) => state.session); const [newSessionInitialized, setNewSessionInitialized] = useState(false); diff --git a/gui/src/components/modelSelection/ModelSelect.tsx b/gui/src/components/modelSelection/ModelSelect.tsx index 88e1e6829..3523a7d9f 100644 --- a/gui/src/components/modelSelection/ModelSelect.tsx +++ b/gui/src/components/modelSelection/ModelSelect.tsx @@ -12,9 +12,6 @@ import { useAuth } from "../../context/Auth"; import { IdeMessengerContext } from "../../context/IdeMessenger"; import AddModelForm from "../../forms/AddModelForm"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; -import { - selectIsInEditMode, -} from "../../redux/slices/sessionSlice"; import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; import { updateSelectedModelByRole } from "../../redux/thunks"; import { getMetaKeyLabel, isMetaEquivalentKeyPressed } from "../../util"; @@ -123,12 +120,12 @@ function ModelOption({ function ModelSelect() { const dispatch = useAppDispatch(); - const isInEditMode = useAppSelector(selectIsInEditMode); + const mode = useAppSelector((store) => store.session.mode); const config = useAppSelector((state) => state.config.config); let selectedModel = null; let allModels = null; - if (isInEditMode) { + if (mode === "edit") { allModels = config.modelsByRole.edit; selectedModel = config.selectedModelByRole.edit; } @@ -158,14 +155,14 @@ function ModelSelect() { useEffect(() => { setOptions( allModels.map((model) => { - return { - value: model.title, - title: modelSelectTitle(model), - apiKey: model.apiKey, - }; - }), + return { + value: model.title, + title: modelSelectTitle(model), + apiKey: model.apiKey, + }; + }), ); - }, [isInEditMode, config]); + }, [config]); useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { @@ -225,7 +222,7 @@ function ModelSelect() { dispatch( updateSelectedModelByRole({ selectedProfile, - role: isInEditMode ? "edit" : "chat", + role: mode === "edit" ? "edit" : "chat", modelTitle: val, }), ); diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 4350ff56d..268e22e7a 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -29,7 +29,6 @@ import { TabBar } from "../../components/TabBar/TabBar"; import { IdeMessengerContext } from "../../context/IdeMessenger"; import { useWebviewListener } from "../../hooks/useWebviewListener"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; -import { selectUseHub } from "../../redux/selectors"; import { selectCurrentToolCall, selectCurrentToolCallApplyState, @@ -38,8 +37,6 @@ import { selectSelectedChatModel } from "../../redux/slices/configSlice"; import { submitEdit } from "../../redux/slices/editModeState"; import { newSession, - selectIsInEditMode, - selectIsSingleRangeEditOrInsertion, updateToolCallOutput, } from "../../redux/slices/sessionSlice"; import { @@ -56,7 +53,7 @@ import { FREE_TRIAL_LIMIT_REQUESTS, incrementFreeTrialCount, } from "../../util/freeTrial"; -import getMultifileEditPrompt from "../../util/getMultifileEditPrompt"; + import { getLocalStorage, setLocalStorage } from "../../util/localStorage"; import { EmptyChatBody } from "./EmptyChatBody"; import { ExploreDialogWatcher } from "./ExploreDialogWatcher"; @@ -115,7 +112,7 @@ export function Chat() { const showChatScrollbar = useAppSelector( (state) => state.config.config.ui?.showChatScrollbar, ); - const codeToEdit = useAppSelector((state) => state.session.codeToEdit); + const codeToEdit = useAppSelector((state) => state.editModeState.codeToEdit); const toolCallState = useAppSelector(selectCurrentToolCall); const applyStates = useAppSelector( (state) => state.session.codeBlockApplyStates.states, @@ -124,12 +121,8 @@ export function Chat() { (state) => state.status === "done", ); const hasPendingApplies = pendingApplyStates.length > 0; - const isInEditMode = useAppSelector(selectIsInEditMode); - const isSingleRangeEditOrInsertion = useAppSelector( - selectIsSingleRangeEditOrInsertion, - ); + const mode = useAppSelector((store) => store.session.mode); const lastSessionId = useAppSelector((state) => state.session.lastSessionId); - const useHub = useAppSelector(selectUseHub); const hasDismissedExploreDialog = useAppSelector( (state) => state.ui.hasDismissedExploreDialog, ); @@ -206,18 +199,12 @@ export function Chat() { } } - if (isSingleRangeEditOrInsertion) { + if (mode === "edit") { handleSingleRangeEditOrInsertion(editorState); return; } - const promptPreamble = isInEditMode - ? getMultifileEditPrompt(codeToEdit) - : undefined; - - dispatch( - streamResponseThunk({ editorState, modifiers, promptPreamble, index }), - ); + dispatch(streamResponseThunk({ editorState, modifiers, index })); if (editorToClearOnSend) { editorToClearOnSend.commands.clearContent(); @@ -240,7 +227,7 @@ export function Chat() { history, selectedChatModel, streamResponse, - isSingleRangeEditOrInsertion, + mode, codeToEdit, toolCallState, ], @@ -314,8 +301,6 @@ export function Chat() { useAutoScroll(stepsDivRef, history); - const showPageHeader = isInEditMode || useHub; - return ( <> {widget} @@ -324,7 +309,7 @@ export function Chat() { 0 ? "flex-1" : ""}`} + className={`mt-3 overflow-y-scroll pt-[8px] ${showScrollbar ? "thin-scrollbar" : "no-scrollbar"} ${history.length > 0 ? "flex-1" : ""}`} > {highlights} {history.map((item, index: number) => ( @@ -342,14 +327,13 @@ export function Chat() { > {item.message.role === "user" ? ( <> - {isInEditMode && index === 0 && } + {mode === "edit" && } sendInput(editorState, modifiers, index) } isLastUserInput={isLastUserInput(index)} - isMainInput={false} + isMainInput={mode === "edit"} editorState={item.editorState} contextItems={item.contextItems} inputId={item.message.id} @@ -421,18 +405,18 @@ export function Chat() { ))}
- {isInEditMode && history.length === 0 && } - - {isInEditMode && history.length > 0 ? null : ( - - sendInput(editorState, modifiers, undefined, editor) - } - inputId={MAIN_EDITOR_INPUT_ID} - /> + {mode === "edit" && history.length > 0 ? null : ( + <> + {mode === "edit" && } + + sendInput(editorState, modifiers, undefined, editor) + } + inputId={MAIN_EDITOR_INPUT_ID} + /> + )}
- {history.length === 0 && lastSessionId && !isInEditMode && ( + {history.length === 0 && lastSessionId && mode !== "edit" && (
{ @@ -462,7 +446,7 @@ export function Chat() {
- {hasPendingApplies && isSingleRangeEditOrInsertion && ( + {hasPendingApplies && ( { @@ -481,10 +465,7 @@ export function Chat() { {!hasDismissedExploreDialog && } {history.length === 0 && ( - + )}
diff --git a/gui/src/pages/gui/EmptyChatBody.tsx b/gui/src/pages/gui/EmptyChatBody.tsx index dce70cdd5..ac8ba7631 100644 --- a/gui/src/pages/gui/EmptyChatBody.tsx +++ b/gui/src/pages/gui/EmptyChatBody.tsx @@ -4,18 +4,15 @@ import { OnboardingCard } from "../../components/OnboardingCard"; import { PlatformOnboardingCard } from "../../components/OnboardingCard/platform/PlatformOnboardingCard"; export interface EmptyChatBodyProps { - useHub: boolean; showOnboardingCard?: boolean; } -export function EmptyChatBody({ - useHub, - showOnboardingCard, -}: EmptyChatBodyProps) { +export function EmptyChatBody({ showOnboardingCard }: EmptyChatBodyProps) { if (showOnboardingCard) { return (
- {useHub ? ( + {true ? ( + // For now we are excluding local onboarding options other than ollama ) : ( diff --git a/gui/src/pages/gui/StreamError.tsx b/gui/src/pages/gui/StreamError.tsx index d098d2bc4..3d89a6245 100644 --- a/gui/src/pages/gui/StreamError.tsx +++ b/gui/src/pages/gui/StreamError.tsx @@ -11,7 +11,6 @@ import { useAuth } from "../../context/Auth"; import { IdeMessengerContext } from "../../context/IdeMessenger"; import { selectSelectedProfile } from "../../redux/"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; -import { selectUseHub } from "../../redux/selectors"; import { selectSelectedChatModel } from "../../redux/slices/configSlice"; import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; import { isLocalProfile } from "../../util"; @@ -36,7 +35,6 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { const dispatch = useAppDispatch(); const ideMessenger = useContext(IdeMessengerContext); const selectedModel = useAppSelector(selectSelectedChatModel); - const hubEnabled = useAppSelector(selectUseHub); const selectedProfile = useAppSelector(selectSelectedProfile); const { session, refreshProfiles } = useAuth(); @@ -180,17 +178,14 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { if (statusCode === 401) { errorContent = (
- {hubEnabled && - session && - selectedProfile && - !isLocalProfile(selectedProfile) && ( -
- {`If your hub secret values may have changed, refresh your assistants`} - - Refresh assistant secrets - -
- )} + {session && selectedProfile && !isLocalProfile(selectedProfile) && ( +
+ {`If your hub secret values may have changed, refresh your assistants`} + + Refresh assistant secrets + +
+ )} {`It's possible that your API key is invalid.`}
{checkKeysButton} diff --git a/gui/src/redux/selectors/index.ts b/gui/src/redux/selectors/index.ts index bbfea5364..aa24f87b3 100644 --- a/gui/src/redux/selectors/index.ts +++ b/gui/src/redux/selectors/index.ts @@ -46,6 +46,3 @@ export const selectUseActiveFile = createSelector( [(state: RootState) => state.config.config.experimental?.defaultContext], (defaultContext) => defaultContext?.includes("activeFile" as any), ); - -export const selectUseHub = (state: RootState) => - state.config.config.usePlatform; diff --git a/gui/src/redux/slices/editModeState.ts b/gui/src/redux/slices/editModeState.ts index b0283a0ab..391da9200 100644 --- a/gui/src/redux/slices/editModeState.ts +++ b/gui/src/redux/slices/editModeState.ts @@ -1,15 +1,19 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; -import { EditStatus, MessageContent } from "core"; +import { CodeToEdit, EditStatus, MessageContent } from "core"; export interface EditModeState { + codeToEdit: CodeToEdit[]; editStatus: EditStatus; previousInputs: MessageContent[]; fileAfterEdit?: string; + enteredEditModeFromEditor: boolean; } const initialState: EditModeState = { editStatus: "not-started", previousInputs: [], + codeToEdit: [], + enteredEditModeFromEditor: false, }; export const editModeStateSlice = createSlice({ @@ -69,6 +73,36 @@ export const editModeStateSlice = createSlice({ state.editStatus = "done"; state.previousInputs = []; }, + addCodeToEdit: ( + state, + { payload }: PayloadAction, + ) => { + const entries = Array.isArray(payload) ? payload : [payload]; + + const newEntries = entries.filter( + (entry) => + !state.codeToEdit.some((existingEntry) => + isCodeToEditEqual(existingEntry, entry), + ), + ); + + if (newEntries.length > 0) { + state.codeToEdit.push(...newEntries); + } + }, + removeCodeToEdit: (state, { payload }: PayloadAction) => { + state.codeToEdit = state.codeToEdit.filter( + (entry) => !isCodeToEditEqual(entry, payload), + ); + }, + clearCodeToEdit: (state) => { + state.codeToEdit = []; + }, + }, + selectors: { + selectHasCodeToEdit: (state) => { + return state.codeToEdit.length > 0; + }, }, }); @@ -78,5 +112,41 @@ export const { setEditDone, submitEdit, focusEdit, + clearCodeToEdit, + addCodeToEdit, + removeCodeToEdit, } = editModeStateSlice.actions; export default editModeStateSlice.reducer; + +function isCodeToEditEqual(a: CodeToEdit, b: CodeToEdit) { + if (a.filepath !== b.filepath || a.contents !== b.contents) { + return false; + } + + if ("range" in a && "range" in b) { + const rangeA = a.range; + const rangeB = b.range; + + return ( + rangeA.start.line === rangeB.start.line && + rangeA.end.line === rangeB.end.line + ); + } + + // If neither has a range, they are considered equal in this context + return !("range" in a) && !("range" in b); +} + +export const { selectHasCodeToEdit } = editModeStateSlice.selectors; + +// selectIsSingleRangeEditOrInsertion: (state) => { +// if (state.mode !== "edit") { +// return false; +// } + +// const isInsertion = state.codeToEdit.length === 0; +// const selectIsSingleRangeEdit = +// state.codeToEdit.length === 1 && "range" in state.codeToEdit[0]; + +// return selectIsSingleRangeEdit || isInsertion; +// }, diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index 5f74e0c50..5ef58dfff 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -10,7 +10,6 @@ import { ApplyState, ChatHistoryItem, ChatMessage, - CodeToEdit, ContextItem, ContextItemWithId, FileSymbolMap, @@ -44,7 +43,6 @@ type SessionState = { title: string; id: string; streamAborter: AbortController; - codeToEdit: CodeToEdit[]; curCheckpointIndex: number; mainEditorContentTrigger?: JSONContent | undefined; symbols: FileSymbolMap; @@ -56,25 +54,6 @@ type SessionState = { newestToolbarPreviewForInput: Record; }; -function isCodeToEditEqual(a: CodeToEdit, b: CodeToEdit) { - if (a.filepath !== b.filepath || a.contents !== b.contents) { - return false; - } - - if ("range" in a && "range" in b) { - const rangeA = a.range; - const rangeB = b.range; - - return ( - rangeA.start.line === rangeB.start.line && - rangeA.end.line === rangeB.end.line - ); - } - - // If neither has a range, they are considered equal in this context - return !("range" in a) && !("range" in b); -} - const initialState: SessionState = { allSessionMetadata: [], history: [], @@ -83,7 +62,6 @@ const initialState: SessionState = { id: uuidv4(), curCheckpointIndex: 0, streamAborter: new AbortController(), - codeToEdit: [], symbols: {}, mode: "chat", codeBlockApplyStates: { @@ -584,31 +562,7 @@ export const sessionSlice = createSlice({ resetNextCodeBlockToApplyIndex: (state) => { state.codeBlockApplyStates.curIndex = 0; }, - addCodeToEdit: ( - state, - { payload }: PayloadAction, - ) => { - const entries = Array.isArray(payload) ? payload : [payload]; - const newEntries = entries.filter( - (entry) => - !state.codeToEdit.some((existingEntry) => - isCodeToEditEqual(existingEntry, entry), - ), - ); - - if (newEntries.length > 0) { - state.codeToEdit.push(...newEntries); - } - }, - removeCodeToEdit: (state, { payload }: PayloadAction) => { - state.codeToEdit = state.codeToEdit.filter( - (entry) => !isCodeToEditEqual(entry, payload), - ); - }, - clearCodeToEdit: (state) => { - state.codeToEdit = []; - }, // TOOL CALL STATE setToolGenerated: ( state, @@ -724,26 +678,6 @@ export const sessionSlice = createSlice({ const curHistoryItem = state.history.at(-1); return curHistoryItem?.isGatheringContext || false; }, - selectIsInEditMode: (state) => { - return state.mode === "edit"; - }, - selectCurrentMode: (state) => { - return state.mode; - }, - selectIsSingleRangeEditOrInsertion: (state) => { - if (state.mode !== "edit") { - return false; - } - - const isInsertion = state.codeToEdit.length === 0; - const selectIsSingleRangeEdit = - state.codeToEdit.length === 1 && "range" in state.codeToEdit[0]; - - return selectIsSingleRangeEdit || isInsertion; - }, - selectHasCodeToEdit: (state) => { - return state.codeToEdit.length > 0; - }, }, extraReducers: (builder) => { addPassthroughCases(builder, [streamResponseThunk]); @@ -811,9 +745,6 @@ export const { resetNextCodeBlockToApplyIndex, updateApplyState, abortStream, - clearCodeToEdit, - addCodeToEdit, - removeCodeToEdit, setToolCallCalling, cancelToolCall, errorToolCall, @@ -829,12 +760,6 @@ export const { cycleMode, } = sessionSlice.actions; -export const { - selectIsGatheringContext, - selectIsInEditMode, - selectCurrentMode, - selectIsSingleRangeEditOrInsertion, - selectHasCodeToEdit, -} = sessionSlice.selectors; +export const { selectIsGatheringContext } = sessionSlice.selectors; export default sessionSlice.reducer; diff --git a/gui/src/redux/store.ts b/gui/src/redux/store.ts index aa7c2bb5c..6a15431e0 100644 --- a/gui/src/redux/store.ts +++ b/gui/src/redux/store.ts @@ -47,7 +47,6 @@ const saveSubsetFilters = [ // Persist edit mode in case closes in middle "mode", - "codeToEdit", // higher risk to persist // codeBlockApplyStates @@ -55,7 +54,7 @@ const saveSubsetFilters = [ // curCheckpointIndex ]), // Don't persist any of the edit state for now - createFilter("editModeState", []), + createFilter("editModeState", ["codeToEdit"]), createFilter("config", ["defaultModelTitle"]), createFilter("ui", ["toolSettings", "toolGroupSettings"]), createFilter("indexing", []), diff --git a/gui/src/redux/thunks/exitEditMode.ts b/gui/src/redux/thunks/exitEditMode.ts index 438783499..9e5452e68 100644 --- a/gui/src/redux/thunks/exitEditMode.ts +++ b/gui/src/redux/thunks/exitEditMode.ts @@ -1,24 +1,17 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; -import { - clearCodeToEdit, - selectIsInEditMode, - selectIsSingleRangeEditOrInsertion, - setMainEditorContentTrigger, - setMode, -} from "../slices/sessionSlice"; +import { clearCodeToEdit, setEditDone } from "../slices/editModeState"; +import { setMainEditorContentTrigger, setMode } from "../slices/sessionSlice"; import { ThunkApiType } from "../store"; -import { setEditDone } from "../slices/editModeState"; export const exitEditMode = createAsyncThunk( "edit/complete", async (_, { dispatch, extra, getState }) => { const state = getState(); - const isInEditMode = selectIsInEditMode(state); - const codeToEdit = state.session.codeToEdit; - const isSingleRangeEditOrInsertion = - selectIsSingleRangeEditOrInsertion(state); + const codeToEdit = state.editModeState.codeToEdit; + const enteredEditModeFromEditor = + state.editModeState.enteredEditModeFromEditor; - if (!isInEditMode) { + if (state.session.mode !== "edit") { return; } @@ -35,7 +28,7 @@ export const exitEditMode = createAsyncThunk( dispatch(setMainEditorContentTrigger(undefined)); extra.ideMessenger.post("edit/exit", { - shouldFocusEditor: isSingleRangeEditOrInsertion, + shouldFocusEditor: enteredEditModeFromEditor, }); }, ); diff --git a/gui/src/util/getMultifileEditPrompt.ts b/gui/src/util/getMultifileEditPrompt.ts deleted file mode 100644 index 13ee8ac78..000000000 --- a/gui/src/util/getMultifileEditPrompt.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { CodeToEdit, IDE } from "core"; -import { findUriInDirs } from "core/util/uri"; - -export default function getMultifileEditPrompt( - codeToEdit: CodeToEdit[], - dirs?: string[], -): string { - const codeToEditStr = codeToEdit - .map((code) => { - const { relativePathOrBasename } = findUriInDirs( - code.filepath, - dirs ?? window.workspacePaths ?? [], - ); - return ` -\`\`\` ${relativePathOrBasename} -${code.contents} -\`\`\` - `; - }) - .join("\n"); - - return ` -You are an AI assistant designed to help software engineers make multi-file edits in their codebase. Your task is to generate the necessary code changes for multiple files based on the engineer's request. Follow these guidelines: - -1. Start with a brief introduction of the task you're about to perform. Do not start with any other preamble, such as "Certainly!" -2. When providing instructions, if a user needs to interact with a CLI, walk them through each step -2. Perform file edits in a logical, sequential ordering -3. For each file edit, provide a brief explanation of the changes -4. If the user submits a code block that contains a filename in the language specifier, always include the filename in any code block you generate based on that file. The filename should be on the same line as the language specifier in your code block. - a. When creating new files, also inlcude the filname in the language specifier of the code block. -6. After providing all file changes, include a brief, sequential overview of the most important 3-5 edits - -Remember to be concise and focus on the code changes and their impact. - -Here's an example of how your response should be structured: - - -I'll make the following changes to implement feature X: - -1. First, I'll modify file1.js to add a new function: -\`\`\`javascript /path/to/file1.js -// Entire content of file1.js or changes to be made -\`\`\` - -2. Next, let's create a new test file: -\`\`\`javascript /path/to/file1.test.js -# Entire content of file2.py or changes to be made -\`\`\` - -Summary: -- [Sequential overview of the most important 3-5 edits] - - -Here are the files to base your edits on: - - -${codeToEditStr} - - -Please provide the multi-file edit details based on the engineer's request below: - `; -}