From 356a4b67c0fc4988851fa2706571c8dfc59ca988 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 18 Nov 2024 01:09:50 -0800 Subject: [PATCH 1/4] stream aborter --- gui/src/components/Layout.tsx | 1 + gui/src/hooks/useChatHandler.ts | 17 ++++++++++------- gui/src/hooks/useHistory.tsx | 2 +- gui/src/hooks/useSetup.ts | 1 + gui/src/redux/slices/stateSlice.ts | 12 ++++++++++-- 5 files changed, 23 insertions(+), 10 deletions(-) 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; From 022b7a7d7d90e58f467d057b506e79ebcbe6b0e1 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 18 Nov 2024 01:10:03 -0800 Subject: [PATCH 2/4] stream aborter p2 --- gui/src/hooks/useChatHandler.ts | 7 +++---- gui/src/hooks/useHistory.tsx | 12 ++++++++---- gui/src/redux/slices/stateSlice.ts | 3 +++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index bf6836107..75e9e428e 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -69,8 +69,7 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { }, [active]); async function _streamNormalInput(messages: ChatMessage[]) { - const abortController = streamAborter ?? new AbortController(); - const cancelToken = abortController.signal; + const cancelToken = streamAborter.signal; try { if (!defaultModel) { @@ -85,7 +84,7 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { while (!next.done) { if (!activeRef.current) { - abortController.abort(); + dispatch(abortStream()); break; } dispatch( @@ -162,7 +161,7 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { historyIndex, selectedCode, }, - cancelToken, + streamAborter.signal, )) { if (!activeRef.current) { dispatch(abortStream()); diff --git a/gui/src/hooks/useHistory.tsx b/gui/src/hooks/useHistory.tsx index e57253924..03657d74d 100644 --- a/gui/src/hooks/useHistory.tsx +++ b/gui/src/hooks/useHistory.tsx @@ -7,7 +7,11 @@ import { useSelector } from "react-redux"; import { IdeMessengerContext } from "../context/IdeMessenger"; import { useLastSessionContext } from "../context/LastSessionContext"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; -import { newSession, setInactive } from "../redux/slices/stateSlice"; +import { + abortStream, + newSession, + setInactive, +} from "../redux/slices/stateSlice"; import { RootState } from "../redux/store"; import { getLocalStorage, setLocalStorage } from "../util/localStorage"; @@ -121,9 +125,9 @@ function useHistory(dispatch: Dispatch) { if (result.status === "error") { throw new Error(result.error); } - const persistedSessionInfo = result.content; - dispatch(newSession(persistedSessionInfo)); - return persistedSessionInfo; + const sessionContent = result.content; + dispatch(newSession(sessionContent)); + return sessionContent; } async function loadLastSession(): Promise { diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index 469ef3ead..3caa424b8 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -255,6 +255,9 @@ export const stateSlice = createSlice({ state, { payload }: PayloadAction, ) => { + state.streamAborter.abort(); + state.streamAborter = new AbortController(); + state.active = false; state.isGatheringContext = false; state.isInMultifileEdit = false; From 32ee22bf700ec4d3b63a4b687418bb8595944342 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 18 Nov 2024 01:14:08 -0800 Subject: [PATCH 3/4] stream aborter cleanup --- gui/src/components/Layout.tsx | 1 - gui/src/hooks/useChatHandler.ts | 4 +--- gui/src/hooks/useHistory.tsx | 6 +----- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/gui/src/components/Layout.tsx b/gui/src/components/Layout.tsx index 92dbdb4a6..42a1182e6 100644 --- a/gui/src/components/Layout.tsx +++ b/gui/src/components/Layout.tsx @@ -21,7 +21,6 @@ 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 75e9e428e..760948134 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -69,15 +69,13 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { }, [active]); async function _streamNormalInput(messages: ChatMessage[]) { - const cancelToken = streamAborter.signal; - try { if (!defaultModel) { throw new Error("Default model not defined"); } const gen = ideMessenger.llmStreamChat( defaultModel.title, - cancelToken, + streamAborter.signal, messages, ); let next = await gen.next(); diff --git a/gui/src/hooks/useHistory.tsx b/gui/src/hooks/useHistory.tsx index 03657d74d..8b9a93531 100644 --- a/gui/src/hooks/useHistory.tsx +++ b/gui/src/hooks/useHistory.tsx @@ -7,11 +7,7 @@ import { useSelector } from "react-redux"; import { IdeMessengerContext } from "../context/IdeMessenger"; import { useLastSessionContext } from "../context/LastSessionContext"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; -import { - abortStream, - newSession, - setInactive, -} from "../redux/slices/stateSlice"; +import { newSession } from "../redux/slices/stateSlice"; import { RootState } from "../redux/store"; import { getLocalStorage, setLocalStorage } from "../util/localStorage"; From 5fc0db9f54675d30eb1242b1966f1d17cdb2c71a Mon Sep 17 00:00:00 2001 From: Test Date: Mon, 18 Nov 2024 12:45:24 -0800 Subject: [PATCH 4/4] Update stateSlice.ts --- gui/src/redux/slices/stateSlice.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index 2cdd4e72e..8c0a8e759 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -4,7 +4,6 @@ import { ChatHistoryItem, ChatMessage, Checkpoint, - ContextItemId, ContextItemWithId, PersistedSessionInfo, PromptLog, @@ -12,9 +11,7 @@ import { import { BrowserSerializedContinueConfig } from "core/config/load"; import { ConfigValidationError } from "core/config/validation"; import { stripImages } from "core/llm/images"; -import { createSelector } from "reselect"; import { v4 as uuidv4, v4 } from "uuid"; -import { RootState } from "../store"; import { ApplyState } from "core/protocol/ideWebview"; // We need this to handle reorderings (e.g. a mid-array deletion) of the messages array. @@ -36,10 +33,10 @@ type State = { configError: ConfigValidationError[] | undefined; checkpoints: Checkpoint[]; curCheckpointIndex: number; - isMultifileEdit: boolean; applyStates: ApplyState[]; nextCodeBlockToApplyIndex: number; streamAborter: AbortController; + isMultifileEdit: boolean; }; const initialState: State = { @@ -270,7 +267,7 @@ export const stateSlice = createSlice({ state.active = false; state.isGatheringContext = false; - state.isInMultifileEdit = false; + state.isMultifileEdit = false; if (payload) { state.history = payload.history as any; state.title = payload.title; @@ -365,7 +362,7 @@ export const stateSlice = createSlice({ curApplyState.numDiffs = payload.numDiffs ?? curApplyState.numDiffs; curApplyState.filepath = payload.filepath ?? curApplyState.filepath; } - if(payload.status === "done"){ + if (payload.status === "done") { state.nextCodeBlockToApplyIndex++; } },