diff --git a/core/core.ts b/core/core.ts index 332076aae..6510add64 100644 --- a/core/core.ts +++ b/core/core.ts @@ -535,33 +535,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 3f860511b..5033658c7 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) @@ -149,7 +151,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 1e8fd5234..96ef161a7 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -25,7 +25,7 @@ import { defaultModelSelector } from "../redux/selectors/modelSelectors"; import { abortStream, addPromptCompletionPair, - clearLastResponse, + clearLastEmptyResponse, initNewActiveMessage, resubmitAtIndex, setCurCheckpointIndex, @@ -71,35 +71,27 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) { }, [active]); async function _streamNormalInput(messages: ChatMessage[]) { - 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, + streamAborter.signal, + messages, + ); + let next = await gen.next(); + while (!next.done) { + if (!activeRef.current) { + dispatch(abortStream()); + break; } - const gen = ideMessenger.llmStreamChat( - defaultModel.title, - streamAborter.signal, - messages, - ); - let next = await gen.next(); + dispatch(streamUpdate(stripImages((next.value as ChatMessage).content))); + next = await gen.next(); + } - while (!next.done) { - if (!activeRef.current) { - dispatch(abortStream()); - 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])); } } @@ -149,29 +141,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, - }, - streamAborter.signal, - )) { - if (!activeRef.current) { - dispatch(abortStream()); - 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, + }, + streamAborter.signal, + )) { + if (!activeRef.current) { + dispatch(abortStream()); + clearInterval(checkActiveInterval); + break; + } + if (typeof update === "string") { + dispatch(streamUpdate(update)); + } } + } catch (e) { + throw e; + } finally { + clearInterval(checkActiveInterval); } - clearInterval(checkActiveInterval); } async function streamResponse( @@ -320,6 +318,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 359672662..a48ac244b 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"; @@ -400,12 +400,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 644aa9e58..770e9ce69 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -144,13 +144,16 @@ export const stateSlice = createSlice({ state.context.isGathering = payload.isGathering; state.context.gatheringMessage = payload.gatheringMessage; }, - 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; @@ -464,7 +467,7 @@ export const { setActive, initNewActiveMessage, setMessageAtIndex, - clearLastResponse, + clearLastEmptyResponse, consumeMainEditorContent, setSelectedProfileId, deleteMessage,