- {history.length === 0 && lastSessionId && mode !== "edit" && (
+ {history.length === 0 && lastSessionId && !isInEdit && (
{
diff --git a/gui/src/redux/slices/editModeState.ts b/gui/src/redux/slices/editState.ts
similarity index 73%
rename from gui/src/redux/slices/editModeState.ts
rename to gui/src/redux/slices/editState.ts
index 05b41a15b..74379b29f 100644
--- a/gui/src/redux/slices/editModeState.ts
+++ b/gui/src/redux/slices/editState.ts
@@ -1,14 +1,16 @@
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
-import { ApplyState, SetCodeToEditPayload, MessageModes } from "core";
+import { JSONContent } from "@tiptap/core";
+import { ApplyState, MessageModes, SetCodeToEditPayload } from "core";
import { EDIT_MODE_STREAM_ID } from "core/edit/constants";
-export interface EditModeState {
+export interface EditState {
// Array because of previous multi-file edit functionality
// Keeping array to not break persisted redux for now
codeToEdit: SetCodeToEditPayload[];
applyState: ApplyState;
returnToMode: MessageModes;
lastNonEditSessionWasEmpty: boolean;
+ previousModeEditorContent: JSONContent | undefined;
}
export const INITIAL_EDIT_APPLY_STATE: ApplyState = {
@@ -16,15 +18,16 @@ export const INITIAL_EDIT_APPLY_STATE: ApplyState = {
status: "not-started",
};
-const initialState: EditModeState = {
+const initialState: EditState = {
applyState: INITIAL_EDIT_APPLY_STATE,
codeToEdit: [],
returnToMode: "chat",
lastNonEditSessionWasEmpty: false,
+ previousModeEditorContent: undefined,
};
-export const editModeStateSlice = createSlice({
- name: "editModeState",
+export const editStateSlice = createSlice({
+ name: "editState",
initialState,
reducers: {
setReturnToModeAfterEdit: (
@@ -63,6 +66,12 @@ export const editModeStateSlice = createSlice({
) => {
state.lastNonEditSessionWasEmpty = payload;
},
+ setPreviousModeEditorContent: (
+ state,
+ { payload }: PayloadAction,
+ ) => {
+ state.previousModeEditorContent = payload;
+ },
},
selectors: {},
});
@@ -73,5 +82,6 @@ export const {
setCodeToEdit,
updateEditStateApplyState,
setLastNonEditSessionEmpty,
-} = editModeStateSlice.actions;
-export default editModeStateSlice.reducer;
+ setPreviousModeEditorContent,
+} = editStateSlice.actions;
+export default editStateSlice.reducer;
diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts
index 627829da4..d9965920b 100644
--- a/gui/src/redux/slices/sessionSlice.ts
+++ b/gui/src/redux/slices/sessionSlice.ts
@@ -47,6 +47,7 @@ type SessionState = {
mainEditorContentTrigger?: JSONContent | undefined;
symbols: FileSymbolMap;
mode: MessageModes;
+ isInEdit: boolean;
codeBlockApplyStates: {
states: ApplyState[];
curIndex: number;
@@ -63,6 +64,7 @@ const initialState: SessionState = {
streamAborter: new AbortController(),
symbols: {},
mode: "chat",
+ isInEdit: false,
codeBlockApplyStates: {
states: [],
curIndex: 0,
@@ -645,6 +647,9 @@ export const sessionSlice = createSlice({
setMode: (state, action: PayloadAction) => {
state.mode = action.payload;
},
+ setIsInEdit: (state, action: PayloadAction) => {
+ state.isInEdit = action.payload;
+ },
setNewestToolbarPreviewForInput: (
state,
{
@@ -741,6 +746,7 @@ export const {
updateSessionMetadata,
deleteSessionMetadata,
setNewestToolbarPreviewForInput,
+ setIsInEdit,
} = sessionSlice.actions;
export const { selectIsGatheringContext } = sessionSlice.selectors;
diff --git a/gui/src/redux/store.ts b/gui/src/redux/store.ts
index 983f7bb6c..1e0d098f3 100644
--- a/gui/src/redux/store.ts
+++ b/gui/src/redux/store.ts
@@ -17,7 +17,7 @@ import storage from "redux-persist/lib/storage";
import { IdeMessenger, IIdeMessenger } from "../context/IdeMessenger";
import { profilesReducer } from "./slices";
import configReducer from "./slices/configSlice";
-import editModeStateReducer from "./slices/editModeState";
+import editModeStateReducer from "./slices/editState";
import indexingReducer from "./slices/indexingSlice";
import sessionReducer from "./slices/sessionSlice";
import tabsReducer from "./slices/tabsSlice";
diff --git a/gui/src/redux/thunks/editMode.ts b/gui/src/redux/thunks/edit.ts
similarity index 61%
rename from gui/src/redux/thunks/editMode.ts
rename to gui/src/redux/thunks/edit.ts
index 8a7771a24..4471cc0b4 100644
--- a/gui/src/redux/thunks/editMode.ts
+++ b/gui/src/redux/thunks/edit.ts
@@ -1,19 +1,26 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import { JSONContent } from "@tiptap/core";
import {
- SetCodeToEditPayload,
MessageModes,
RangeInFileWithContents,
+ SetCodeToEditPayload,
} from "core";
import { stripImages } from "core/util/messageContent";
import { resolveEditorContent } from "../../components/mainInput/TipTapEditor";
import {
clearCodeToEdit,
INITIAL_EDIT_APPLY_STATE,
+ setPreviousModeEditorContent,
setReturnToModeAfterEdit,
updateEditStateApplyState,
-} from "../slices/editModeState";
-import { newSession, setActive, setMode } from "../slices/sessionSlice";
+} from "../slices/editState";
+import {
+ newSession,
+ setActive,
+ setIsInEdit,
+ setMainEditorContentTrigger,
+ setMode,
+} from "../slices/sessionSlice";
import { ThunkApiType } from "../store";
import { loadLastSession, saveCurrentSession } from "./session";
import { streamThunkWrapper } from "./streamThunkWrapper";
@@ -27,7 +34,7 @@ export const streamEditThunk = createAsyncThunk<
ThunkApiType
>(
"chat/streamResponse",
- async ({ editorState, codeToEdit }, { dispatch, extra, getState }) => {
+ async ({ editorState, codeToEdit }, { dispatch, extra }) => {
await dispatch(
streamThunkWrapper(async () => {
dispatch(setActive());
@@ -62,17 +69,20 @@ export const streamEditThunk = createAsyncThunk<
},
);
-export const exitEditMode = createAsyncThunk<
+export const exitEdit = createAsyncThunk<
void,
{ goToMode?: MessageModes; openNewSession?: boolean },
ThunkApiType
>(
- "edit/exitMode",
+ "edit/exit",
async ({ goToMode, openNewSession }, { dispatch, extra, getState }) => {
const state = getState();
const codeToEdit = state.editModeState.codeToEdit;
+ const isInEdit = state.session.isInEdit;
+ const previousModeEditorContent =
+ state.editModeState.previousModeEditorContent;
- if (state.session.mode !== "edit") {
+ if (!isInEdit) {
return;
}
@@ -86,6 +96,13 @@ export const exitEditMode = createAsyncThunk<
dispatch(clearCodeToEdit());
dispatch(updateEditStateApplyState(INITIAL_EDIT_APPLY_STATE));
+ dispatch(setIsInEdit(false));
+
+ // Restore the previous editor content if available
+ if (previousModeEditorContent) {
+ dispatch(setMainEditorContentTrigger(previousModeEditorContent));
+ dispatch(setPreviousModeEditorContent(undefined));
+ }
if (openNewSession || state.editModeState.lastNonEditSessionWasEmpty) {
dispatch(newSession());
@@ -101,30 +118,38 @@ export const exitEditMode = createAsyncThunk<
},
);
-export const enterEditMode = createAsyncThunk<
+export const enterEdit = createAsyncThunk<
void,
- { returnToMode?: MessageModes },
+ { returnToMode?: MessageModes; editorContent?: JSONContent },
ThunkApiType
->("edit/enterMode", async ({ returnToMode }, { dispatch, extra, getState }) => {
- const state = getState();
+>(
+ "edit/enter",
+ async ({ returnToMode, editorContent }, { dispatch, extra, getState }) => {
+ const state = getState();
+ const isInEdit = state.session.isInEdit;
- if (state.session.mode === "edit") {
- return;
- }
+ if (isInEdit) {
+ return;
+ }
- dispatch(setReturnToModeAfterEdit(returnToMode ?? state.session.mode));
+ dispatch(setMainEditorContentTrigger({}));
+ dispatch(setPreviousModeEditorContent(editorContent));
- await dispatch(
- saveCurrentSession({
- openNewSession: true,
- // Because this causes a lag before Edit mode is focused. TODO just have that happen in background
- generateTitle: false,
- }),
- );
- dispatch(updateEditStateApplyState(INITIAL_EDIT_APPLY_STATE));
- dispatch(setMode("edit"));
+ dispatch(setReturnToModeAfterEdit(returnToMode ?? state.session.mode));
+ dispatch(updateEditStateApplyState(INITIAL_EDIT_APPLY_STATE));
- if (!state.editModeState.codeToEdit[0]) {
- extra.ideMessenger.post("edit/addCurrentSelection", undefined);
- }
-});
+ await dispatch(
+ saveCurrentSession({
+ openNewSession: true,
+ // Because this causes a lag before Edit is focused. TODO just have that happen in background
+ generateTitle: false,
+ }),
+ );
+
+ dispatch(setIsInEdit(true));
+
+ if (!state.editModeState.codeToEdit[0]) {
+ extra.ideMessenger.post("edit/addCurrentSelection", undefined);
+ }
+ },
+);
diff --git a/gui/src/redux/thunks/index.ts b/gui/src/redux/thunks/index.ts
index 7fe098e70..81c3e937c 100644
--- a/gui/src/redux/thunks/index.ts
+++ b/gui/src/redux/thunks/index.ts
@@ -1,7 +1,11 @@
-export { gatherContext } from "./gatherContext";
-export { resetStateForNewMessage } from "./resetStateForNewMessage";
-export { streamNormalInput } from "./streamNormalInput";
-export { streamResponseThunk } from "./streamResponse";
-export { streamResponseAfterToolCall } from "./streamResponseAfterToolCall";
-export { streamThunkWrapper } from "./streamThunkWrapper";
-export { updateSelectedModelByRole } from "./updateSelectedModelByRole";
+export * from "./callCurrentTool";
+export * from "./cancelCurrentToolCall";
+export * from "./cancelStream";
+export * from "./edit";
+export * from "./gatherContext";
+export * from "./resetStateForNewMessage";
+export * from "./streamNormalInput";
+export * from "./streamResponse";
+export * from "./streamResponseAfterToolCall";
+export * from "./streamThunkWrapper";
+export * from "./updateSelectedModelByRole";