Skip to content

Commit 5f191e7

Browse files
committed
surface assistant errors
1 parent 21d2590 commit 5f191e7

File tree

5 files changed

+120
-36
lines changed

5 files changed

+120
-36
lines changed

package-lock.json

Lines changed: 92 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-assistant/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@mongodb-js/atlas-service": "^0.56.0",
5454
"@mongodb-js/compass-app-registry": "^9.4.20",
5555
"@mongodb-js/compass-components": "^1.49.0",
56-
"ai": "^5.0.5",
56+
"ai": "^5.0.26",
5757
"compass-preferences-model": "^2.51.0",
5858
"react": "^17.0.2",
5959
"throttleit": "^2.1.0",

packages/compass-assistant/src/@ai-sdk/react/use-chat.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type UseChatHelpers<UI_MESSAGE extends UIMessage> = {
4343
| 'addToolResult'
4444
| 'status'
4545
| 'messages'
46+
| 'clearError'
4647
>;
4748

4849
export type UseChatOptions<UI_MESSAGE extends UIMessage> =
@@ -116,6 +117,7 @@ export function useChat<UI_MESSAGE extends UIMessage = UIMessage>({
116117
setMessages,
117118
sendMessage: chatRef.current.sendMessage,
118119
regenerate: chatRef.current.regenerate,
120+
clearError: chatRef.current.clearError,
119121
stop: chatRef.current.stop,
120122
error,
121123
resumeStream: chatRef.current.resumeStream,

packages/compass-assistant/src/assistant-chat.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
LgChatInputBar,
1111
spacing,
1212
css,
13+
Banner,
1314
} from '@mongodb-js/compass-components';
1415

1516
const { ChatWindow } = LgChatChatWindow;
@@ -36,10 +37,19 @@ const chatWindowFixesStyles = css({
3637
height: '100%',
3738
});
3839

40+
function makeErrorMessage(message: string) {
41+
message = message || 'An error occurred';
42+
return `${message}. Try clearing the chat if the error persists.`;
43+
}
44+
45+
const errorBannerWrapperStyles = css({
46+
margin: spacing[400],
47+
});
48+
3949
export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
4050
chat,
4151
}) => {
42-
const { messages, sendMessage, status } = useChat({
52+
const { messages, sendMessage, status, error, clearError } = useChat({
4353
chat,
4454
});
4555

@@ -94,6 +104,13 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
94104
/>
95105
)}
96106
</MessageFeed>
107+
{error && (
108+
<div className={errorBannerWrapperStyles}>
109+
<Banner variant="danger" dismissible onClose={clearError}>
110+
{makeErrorMessage(error.message)}
111+
</Banner>
112+
</div>
113+
)}
97114
<InputBar
98115
data-testid="assistant-chat-input"
99116
onMessageSend={handleMessageSend}

packages/compass-assistant/src/compass-assistant-provider.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ export const CompassAssistantProvider = registerCompassPlugin(
106106
transport: new DocsProviderTransport({
107107
baseUrl: atlasService.assistantApiEndpoint(),
108108
}),
109+
/*
110+
onError: () => {
111+
// best we can do is assume it was the last message that caused it
112+
// because there's nothing on the error object this function receives
113+
chat.messages = chat.messages.slice(0, -1);
114+
},
115+
*/
109116
});
110117
return {
111118
store: { state: { chat } },

0 commit comments

Comments
 (0)