Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle inference error gracefully #44

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/commands/chat/state/actions.ts
Original file line number Diff line number Diff line change
@@ -38,6 +38,19 @@ export function addProgramMessage(text: string, level: MessageLevel = 'info') {
});
}

export function removeUnansweredUserContextMessage() {
useChatState.setState((state) => {
const lastMessage = state.contextMessages.at(-1);
if (lastMessage?.role !== 'user') {
return state;
}

return {
contextMessages: state.contextMessages.slice(0, state.contextMessages.length - 1),
};
});
}

export function forgetContextMessages() {
useChatState.setState((state) => {
return {
22 changes: 11 additions & 11 deletions src/commands/chat/ui/ChatUi.tsx
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import {
addProgramMessage,
addUserMessage,
hideActiveView,
removeUnansweredUserContextMessage,
} from '../state/actions.js';
import { useChatState } from '../state/state.js';
import { texts } from '../texts.js';
@@ -38,19 +39,15 @@ export function ChatUi() {
}, []);

const handleSubmit = (message: string) => {
try {
hideActiveView();

const isCommand = processChatCommand(message);
if (isCommand) {
return;
}
hideActiveView();

addUserMessage(message);
void fetchAiResponse();
} catch (error) {
addProgramMessage(`Error: ${extractErrorMessage(error)}`, 'error');
const isCommand = processChatCommand(message);
if (isCommand) {
return;
}

addUserMessage(message);
void fetchAiResponse();
};

const showInput = !isLoading && !shouldExit;
@@ -97,6 +94,9 @@ function useAiResponse() {
addAiResponse(response);
}
} catch (error) {
// We cannot leave unanswered user message in context, as there is no AI response for it.
// Inference APIs require that user and assistant messages should be alternating.
removeUnansweredUserContextMessage();
addProgramMessage(`Error: ${extractErrorMessage(error)}`, 'error');
} finally {
setLoading(false);