Merge pull request #2973 from continuedev/dallin/abort-stream

Fix streaming into next session bug
This commit is contained in:
Nate Sesti 2024-11-19 22:02:47 -08:00 • committed by GitHub
commit 30325c3d6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 31 additions and 21 deletions

View file

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

View file

@ -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<PersistedSessionInfo | undefined> {

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

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