diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index ce599454d..e93cdf8d3 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, @@ -52,6 +53,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); @@ -66,23 +70,20 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { }, [active]); async function _streamNormalInput(messages: ChatMessage[]) { - const abortController = new AbortController(); - const cancelToken = abortController.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(); while (!next.done) { if (!activeRef.current) { - abortController.abort(); + dispatch(abortStream()); break; } dispatch( @@ -134,9 +135,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"); } @@ -145,7 +143,7 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { const checkActiveInterval = setInterval(() => { if (!activeRef.current) { - abortController.abort(); + dispatch(abortStream()); clearInterval(checkActiveInterval); } }, 100); @@ -162,10 +160,10 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { historyIndex, selectedCode, }, - cancelToken, + streamAborter.signal, )) { if (!activeRef.current) { - abortController.abort(); + dispatch(abortStream()); break; } if (typeof update === "string") { @@ -324,7 +322,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 f159ffea3..bb5935653 100644 --- a/gui/src/hooks/useHistory.tsx +++ b/gui/src/hooks/useHistory.tsx @@ -122,9 +122,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/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 2d5cb1267..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,9 +33,10 @@ type State = { configError: ConfigValidationError[] | undefined; checkpoints: Checkpoint[]; curCheckpointIndex: number; - isMultifileEdit: boolean; applyStates: ApplyState[]; nextCodeBlockToApplyIndex: number; + streamAborter: AbortController; + isMultifileEdit: boolean; }; const initialState: State = { @@ -70,6 +68,7 @@ const initialState: State = { curCheckpointIndex: 0, nextCodeBlockToApplyIndex: 0, applyStates: [], + streamAborter: new AbortController(), }; export const stateSlice = createSlice({ @@ -249,6 +248,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 += @@ -259,6 +262,12 @@ export const stateSlice = createSlice({ state, { payload }: PayloadAction, ) => { + state.streamAborter.abort(); + state.streamAborter = new AbortController(); + + state.active = false; + state.isGatheringContext = false; + state.isMultifileEdit = false; if (payload) { state.history = payload.history as any; state.title = payload.title; @@ -267,7 +276,6 @@ export const stateSlice = createSlice({ state.curCheckpointIndex = 0; } else { state.history = []; - state.active = false; state.title = "New Session"; state.sessionId = v4(); state.checkpoints = []; @@ -354,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++; } }, @@ -390,6 +398,7 @@ export const { setCurCheckpointIndex, resetNextCodeBlockToApplyIndex, updateApplyState, + abortStream, } = stateSlice.actions; export default stateSlice.reducer;