From c06c31edb31f6c1837ef9758f6e7161e75b34961 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 19 Nov 2024 15:00:40 -0800 Subject: [PATCH] dont clear non-empty messages on error --- core/core.ts | 56 +++++++++-------- core/llm/llms/Anthropic.ts | 8 ++- gui/src/hooks/useChatHandler.ts | 99 +++++++++++++++--------------- gui/src/pages/gui/Chat.tsx | 9 +-- gui/src/redux/slices/stateSlice.ts | 15 +++-- 5 files changed, 96 insertions(+), 91 deletions(-) diff --git a/core/core.ts b/core/core.ts index 853666174..14db3517d 100644 --- a/core/core.ts +++ b/core/core.ts @@ -540,33 +540,39 @@ export class Core { } }, 100); - for await (const content of slashCommand.run({ - input, - history, - llm, - contextItems, - params, - ide, - addContextItem: (item) => { - void messenger.request("addContextItem", { - item, - historyIndex, - }); - }, - selectedCode, - config, - fetch: (url, init) => - fetchwithRequestOptions(url, init, config.requestOptions), - })) { - if (abortedMessageIds.has(msg.messageId)) { - abortedMessageIds.delete(msg.messageId); - break; - } - if (content) { - yield { content }; + try { + for await (const content of slashCommand.run({ + input, + history, + llm, + contextItems, + params, + ide, + addContextItem: (item) => { + void messenger.request("addContextItem", { + item, + historyIndex, + }); + }, + selectedCode, + config, + fetch: (url, init) => + fetchwithRequestOptions(url, init, config.requestOptions), + })) { + if (abortedMessageIds.has(msg.messageId)) { + abortedMessageIds.delete(msg.messageId); + clearInterval(checkActiveInterval); + break; + } + if (content) { + yield { content }; + } } + } catch (e) { + throw e; + } finally { + clearInterval(checkActiveInterval); } - clearInterval(checkActiveInterval); yield { done: true, content: "" }; } on("command/run", (msg) => diff --git a/core/llm/llms/Anthropic.ts b/core/llm/llms/Anthropic.ts index 8db42eefa..dfb3065cd 100644 --- a/core/llm/llms/Anthropic.ts +++ b/core/llm/llms/Anthropic.ts @@ -37,7 +37,9 @@ class Anthropic extends BaseLLM { public convertMessages(msgs: ChatMessage[]): any[] { // should be public for use within VertexAI - const filteredmessages = msgs.filter((m) => m.role !== "system"); + const filteredmessages = msgs.filter( + (m) => m.role !== "system" && !!m.content, + ); const lastTwoUserMsgIndices = filteredmessages .map((msg, index) => (msg.role === "user" ? index : -1)) .filter((index) => index !== -1) @@ -146,7 +148,9 @@ class Anthropic extends BaseLLM { } for await (const value of streamSse(response)) { - if (value.type == "message_start") {console.log(value);} + if (value.type == "message_start") { + console.log(value); + } if (value.delta?.text) { yield { role: "assistant", content: value.delta.text }; } diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index ce599454d..02f44e1be 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -24,7 +24,7 @@ import { IIdeMessenger } from "../context/IdeMessenger"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; import { addPromptCompletionPair, - clearLastResponse, + clearLastEmptyResponse, initNewActiveMessage, resubmitAtIndex, setCurCheckpointIndex, @@ -69,35 +69,27 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { const abortController = new AbortController(); const cancelToken = abortController.signal; - try { - if (!defaultModel) { - throw new Error("Default model not defined"); + if (!defaultModel) { + throw new Error("Default model not defined"); + } + const gen = ideMessenger.llmStreamChat( + defaultModel.title, + cancelToken, + messages, + ); + let next = await gen.next(); + while (!next.done) { + if (!activeRef.current) { + abortController.abort(); + break; } - const gen = ideMessenger.llmStreamChat( - defaultModel.title, - cancelToken, - messages, - ); - let next = await gen.next(); + dispatch(streamUpdate(stripImages((next.value as ChatMessage).content))); + next = await gen.next(); + } - while (!next.done) { - if (!activeRef.current) { - abortController.abort(); - break; - } - dispatch( - streamUpdate(stripImages((next.value as ChatMessage).content)), - ); - next = await gen.next(); - } - - let returnVal = next.value as PromptLog; - if (returnVal) { - dispatch(addPromptCompletionPair([returnVal])); - } - } catch (e) { - // If there's an error, we should clear the response so there aren't two input boxes - dispatch(clearLastResponse()); + let returnVal = next.value as PromptLog; + if (returnVal) { + dispatch(addPromptCompletionPair([returnVal])); } } @@ -150,29 +142,35 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { } }, 100); - for await (const update of ideMessenger.streamRequest( - "command/run", - { - input, - history: messages, - modelTitle, - slashCommandName: slashCommand.name, - contextItems, - params: slashCommand.params, - historyIndex, - selectedCode, - }, - cancelToken, - )) { - if (!activeRef.current) { - abortController.abort(); - break; - } - if (typeof update === "string") { - dispatch(streamUpdate(update)); + try { + for await (const update of ideMessenger.streamRequest( + "command/run", + { + input, + history: messages, + modelTitle, + slashCommandName: slashCommand.name, + contextItems, + params: slashCommand.params, + historyIndex, + selectedCode, + }, + cancelToken, + )) { + if (!activeRef.current) { + abortController.abort(); + clearInterval(checkActiveInterval); + break; + } + if (typeof update === "string") { + dispatch(streamUpdate(update)); + } } + } catch (e) { + throw e; + } finally { + clearInterval(checkActiveInterval); } - clearInterval(checkActiveInterval); } async function streamResponse( @@ -252,8 +250,6 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { } } - // dispatch(addContextItems(contextItems)); - const message: ChatMessage = { role: "user", content, @@ -313,6 +309,7 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { ); } } catch (e: any) { + dispatch(clearLastEmptyResponse()); console.debug("Error streaming response: ", e); ideMessenger.post("showToast", [ "error", diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 345fe6f37..aee6d9416 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -41,7 +41,7 @@ import { useTutorialCard } from "../../hooks/useTutorialCard"; import { useWebviewListener } from "../../hooks/useWebviewListener"; import { defaultModelSelector } from "../../redux/selectors/modelSelectors"; import { - clearLastResponse, + clearLastEmptyResponse, newSession, setInactive, } from "../../redux/slices/stateSlice"; @@ -399,12 +399,7 @@ export function Chat() { { dispatch(setInactive()); - if ( - state.history[state.history.length - 1]?.message.content - .length === 0 - ) { - dispatch(clearLastResponse()); - } + dispatch(clearLastEmptyResponse()); }} > {getMetaKeyLabel()} ⌫ Cancel diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index 2d5cb1267..9c3292c1a 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -116,13 +116,16 @@ export const stateSlice = createSlice({ setIsGatheringContext: (state, { payload }: PayloadAction) => { state.isGatheringContext = payload; }, - clearLastResponse: (state) => { + clearLastEmptyResponse: (state) => { if (state.history.length < 2) { return; } - state.mainEditorContent = - state.history[state.history.length - 2].editorState; - state.history = state.history.slice(0, -2); + // Only clear in the case of an empty message + if (!state.history[state.history.length - 1]?.message.content.length) { + state.mainEditorContent = + state.history[state.history.length - 2].editorState; + state.history = state.history.slice(0, -2); + } }, consumeMainEditorContent: (state) => { state.mainEditorContent = undefined; @@ -354,7 +357,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++; } }, @@ -380,7 +383,7 @@ export const { setActive, initNewActiveMessage, setMessageAtIndex, - clearLastResponse, + clearLastEmptyResponse, consumeMainEditorContent, setSelectedProfileId, deleteMessage,