handleStreamErrors

This commit is contained in:
Dallin Romney 2024-12-06 10:17:09 -08:00
parent 886b238e1f
commit 23c48080be
7 changed files with 68 additions and 70 deletions

View file

@ -1,12 +1,12 @@
{
"name": "continue",
"version": "0.9.244",
"version": "0.9.240",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "continue",
"version": "0.9.244",
"version": "0.9.240",
"license": "Apache-2.0",
"dependencies": {
"@electron/rebuild": "^3.2.10",

View file

@ -304,6 +304,7 @@ function TipTapEditor(props: TipTapEditorProps) {
const mainEditorContent = useAppSelector(
(store) => store.session.mainEditorContent,
);
console.log("MAIN EDITOR", mainEditorContent?.content);
const { prevRef, nextRef, addRef } = useInputHistory(props.historyKey);

View file

@ -1,17 +1,18 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import { setInactive } from "../slices/sessionSlice";
import { clearLastEmptyResponse, setInactive } from "../slices/sessionSlice";
import { ThunkApiType } from "../store";
export const handleErrors = createAsyncThunk<
export const handleStreamErrors = createAsyncThunk<
void,
() => Promise<void>,
ThunkApiType
>("chat/handleErrors", async (runStream, { dispatch, extra }) => {
>("chat/handleStreamErrors", async (runStream, { dispatch, extra }) => {
try {
await runStream();
} catch (e: any) {
console.debug("Error streaming response: ", e);
console.error("Error streaming response: ", e);
} finally {
dispatch(clearLastEmptyResponse());
dispatch(setInactive());
// triggerSave(!save); TODO
}

View file

@ -1,6 +1,6 @@
export { exitEditMode } from "./exitEditMode";
export { gatherContext } from "./gatherContext";
export { handleErrors } from "./handleErrors";
export { handleStreamErrors } from "./handleErrors";
export { resetStateForNewMessage } from "./resetStateForNewMessage";
export { streamNormalInput } from "./streamNormalInput";
export { streamResponseThunk } from "./streamResponse";

View file

@ -17,70 +17,65 @@ export const streamNormalInput = createAsyncThunk<
ChatMessage[],
ThunkApiType
>("chat/streamNormalInput", async (messages, { dispatch, extra, getState }) => {
try {
// Gather state
const state = getState();
const defaultModel = selectDefaultModel(state);
const toolSettings = state.ui.toolSettings;
const streamAborter = state.session.streamAborter;
const useTools = state.ui.useTools;
// Gather state
const state = getState();
const defaultModel = selectDefaultModel(state);
const toolSettings = state.ui.toolSettings;
const streamAborter = state.session.streamAborter;
const useTools = state.ui.useTools;
if (!defaultModel) {
throw new Error("Default model not defined");
if (!defaultModel) {
throw new Error("Default model not defined");
}
// Send request
const gen = extra.ideMessenger.llmStreamChat(
defaultModel.title,
streamAborter.signal,
messages,
{
tools: useTools
? Object.keys(toolSettings)
.filter((tool) => toolSettings[tool] !== "disabled")
.map((toolName) =>
state.config.config.tools.find(
(tool) => tool.function.name === toolName,
),
)
.filter(Boolean)
: undefined,
},
);
// Stream response
let next = await gen.next();
while (!next.done) {
if (!getState().session.isStreaming) {
dispatch(abortStream());
break;
}
// Send request
const gen = extra.ideMessenger.llmStreamChat(
defaultModel.title,
streamAborter.signal,
messages,
{
tools: useTools
? Object.keys(toolSettings)
.filter((tool) => toolSettings[tool] !== "disabled")
.map((toolName) =>
state.config.config.tools.find(
(tool) => tool.function.name === toolName,
),
)
.filter(Boolean)
: undefined,
},
);
const updates = next.value as ChatMessage[];
dispatch(streamUpdate(updates));
next = await gen.next();
}
// Stream response
let next = await gen.next();
while (!next.done) {
if (!getState().session.isStreaming) {
dispatch(abortStream());
break;
}
// Attach prompt log
let returnVal = next.value as PromptLog;
if (returnVal) {
dispatch(addPromptCompletionPair([returnVal]));
}
const updates = next.value as ChatMessage[];
dispatch(streamUpdate(updates));
next = await gen.next();
// If it's a tool call that is automatically accepted, we should call it
const toolCallState = selectCurrentToolCall(getState());
if (toolCallState) {
dispatch(setToolGenerated());
if (
toolSettings[toolCallState.toolCall.function.name] ===
"allowedWithoutPermission"
) {
await dispatch(callTool());
}
// Attach prompt log
let returnVal = next.value as PromptLog;
if (returnVal) {
dispatch(addPromptCompletionPair([returnVal]));
}
// If it's a tool call that is automatically accepted, we should call it
const toolCallState = selectCurrentToolCall(getState());
if (toolCallState) {
dispatch(setToolGenerated());
if (
toolSettings[toolCallState.toolCall.function.name] ===
"allowedWithoutPermission"
) {
await dispatch(callTool());
}
}
} catch (e) {
// If there's an error, we should clear the response so there aren't two input boxes
dispatch(clearLastEmptyResponse());
}
});

View file

@ -18,7 +18,7 @@ import {
} from "../slices/sessionSlice";
import { ThunkApiType } from "../store";
import { gatherContext } from "./gatherContext";
import { handleErrors } from "./handleErrors";
import { handleStreamErrors } from "./handleErrors";
import { resetStateForNewMessage } from "./resetStateForNewMessage";
import { streamNormalInput } from "./streamNormalInput";
import { streamSlashCommand } from "./streamSlashCommand";
@ -66,7 +66,8 @@ export const streamResponseThunk = createAsyncThunk<
{ dispatch, extra, getState },
) => {
await dispatch(
handleErrors(async () => {
handleStreamErrors(async () => {
throw new Error("intentional");
const state = getState();
const defaultModel = selectDefaultModel(state);
const slashCommands = state.config.config.slashCommands || [];

View file

@ -9,7 +9,7 @@ import {
streamUpdate,
} from "../slices/sessionSlice";
import { ThunkApiType } from "../store";
import { handleErrors } from "./handleErrors";
import { handleStreamErrors } from "./handleErrors";
import { resetStateForNewMessage } from "./resetStateForNewMessage";
import { streamNormalInput } from "./streamNormalInput";
@ -24,7 +24,7 @@ export const streamResponseAfterToolCall = createAsyncThunk<
"chat/streamAfterToolCall",
async ({ toolCallId, toolOutput }, { dispatch, getState }) => {
await dispatch(
handleErrors(async () => {
handleStreamErrors(async () => {
const state = getState();
const initialHistory = state.session.history;
const defaultModel = selectDefaultModel(state);