diff --git a/gui/src/components/Layout.tsx b/gui/src/components/Layout.tsx index 42a1182e6..92dbdb4a6 100644 --- a/gui/src/components/Layout.tsx +++ b/gui/src/components/Layout.tsx @@ -21,6 +21,7 @@ import TextDialog from "./dialogs"; import Footer from "./Footer"; import { isNewUserOnboarding, useOnboardingCard } from "./OnboardingCard"; import PostHogPageView from "./PosthogPageView"; +import { setInactive } from "../redux/slices/stateSlice"; const LayoutTopDiv = styled(CustomScrollbarDiv)` height: 100%; diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index 229c03ebc..bf6836107 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -23,6 +23,7 @@ import resolveEditorContent, { import { IIdeMessenger } from "../context/IdeMessenger"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; import { + abortStream, addPromptCompletionPair, clearLastResponse, initNewActiveMessage, @@ -51,6 +52,9 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { const history = useSelector((store: RootState) => store.state.history); const active = useSelector((store: RootState) => store.state.active); + const streamAborter = useSelector( + (store: RootState) => store.state.streamAborter, + ); const activeRef = useRef(active); const { saveSession } = useHistory(dispatch); @@ -65,7 +69,7 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { }, [active]); async function _streamNormalInput(messages: ChatMessage[]) { - const abortController = new AbortController(); + const abortController = streamAborter ?? new AbortController(); const cancelToken = abortController.signal; try { @@ -133,9 +137,6 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { selectedCode: RangeInFile[], contextItems: ContextItemWithId[], ) { - const abortController = new AbortController(); - const cancelToken = abortController.signal; - if (!defaultModel) { throw new Error("Default model not defined"); } @@ -144,7 +145,7 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { const checkActiveInterval = setInterval(() => { if (!activeRef.current) { - abortController.abort(); + dispatch(abortStream()); clearInterval(checkActiveInterval); } }, 100); @@ -164,7 +165,7 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { cancelToken, )) { if (!activeRef.current) { - abortController.abort(); + dispatch(abortStream()); break; } if (typeof update === "string") { @@ -319,7 +320,9 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { } } - return { streamResponse }; + return { + streamResponse, + }; } export default useChatHandler; diff --git a/gui/src/hooks/useHistory.tsx b/gui/src/hooks/useHistory.tsx index 911a2a014..e57253924 100644 --- a/gui/src/hooks/useHistory.tsx +++ b/gui/src/hooks/useHistory.tsx @@ -7,7 +7,7 @@ import { useSelector } from "react-redux"; import { IdeMessengerContext } from "../context/IdeMessenger"; import { useLastSessionContext } from "../context/LastSessionContext"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; -import { newSession } from "../redux/slices/stateSlice"; +import { newSession, setInactive } from "../redux/slices/stateSlice"; import { RootState } from "../redux/store"; import { getLocalStorage, setLocalStorage } from "../util/localStorage"; diff --git a/gui/src/hooks/useSetup.ts b/gui/src/hooks/useSetup.ts index 2d98b1011..eae4b0acc 100644 --- a/gui/src/hooks/useSetup.ts +++ b/gui/src/hooks/useSetup.ts @@ -126,6 +126,7 @@ function useSetup(dispatch: Dispatch) { dispatch(setConfigError(error)); }); + // TODO - remove? useWebviewListener("submitMessage", async (data) => { streamResponse( data.message, diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index 4222b4693..469ef3ead 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -34,6 +34,7 @@ type State = { selectedProfileId: string; configError: ConfigValidationError[] | undefined; isInMultifileEdit: boolean; + streamAborter: AbortController; }; const initialState: State = { @@ -61,6 +62,7 @@ const initialState: State = { defaultModelTitle: "GPT-4", selectedProfileId: "local", isInMultifileEdit: false, + streamAborter: new AbortController(), }; export const stateSlice = createSlice({ @@ -239,6 +241,10 @@ export const stateSlice = createSlice({ state.isGatheringContext = false; state.active = false; }, + abortStream: (state) => { + state.streamAborter.abort(); + state.streamAborter = new AbortController(); + }, streamUpdate: (state, action: PayloadAction) => { if (state.history.length) { state.history[state.history.length - 1].message.content += @@ -249,16 +255,17 @@ export const stateSlice = createSlice({ state, { payload }: PayloadAction, ) => { + state.active = false; + state.isGatheringContext = false; + state.isInMultifileEdit = false; if (payload) { state.history = payload.history as any; state.title = payload.title; state.sessionId = payload.sessionId; } else { state.history = []; - state.active = false; state.title = "New Session"; state.sessionId = v4(); - state.isInMultifileEdit = false; } }, addHighlightedCode: ( @@ -339,6 +346,7 @@ export const { deleteMessage, setIsGatheringContext, setIsInMultifileEdit, + abortStream, } = stateSlice.actions; export default stateSlice.reducer;