Skip to content

Commit

Permalink
fix: handle inference error gracefully (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski authored Apr 2, 2024
1 parent 5fdf95b commit 5a9acd5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
13 changes: 13 additions & 0 deletions src/commands/chat/state/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions src/commands/chat/ui/ChatUi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
addProgramMessage,
addUserMessage,
hideActiveView,
removeUnansweredUserContextMessage,
} from '../state/actions.js';
import { useChatState } from '../state/state.js';
import { texts } from '../texts.js';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5a9acd5

Please sign in to comment.