stream aborter

This commit is contained in:
Dallin Romney 2024-11-18 01:09:50 -08:00
parent 51f4d1b48b
commit 356a4b67c0
5 changed files with 23 additions and 10 deletions

View file

@ -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%;

View file

@ -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;

View file

@ -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";

View file

@ -126,6 +126,7 @@ function useSetup(dispatch: Dispatch<any>) {
dispatch(setConfigError(error));
});
// TODO - remove?
useWebviewListener("submitMessage", async (data) => {
streamResponse(
data.message,

View file

@ -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<string>) => {
if (state.history.length) {
state.history[state.history.length - 1].message.content +=
@ -249,16 +255,17 @@ export const stateSlice = createSlice({
state,
{ payload }: PayloadAction<PersistedSessionInfo | undefined>,
) => {
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;