Skip to content

Commit

Permalink
Merge pull request #1465 from mito-ds/upgrade-msg-redesign
Browse files Browse the repository at this point in the history
Added alert block for nicer error messages
  • Loading branch information
aarondr77 authored Jan 3, 2025
2 parents 917460a + f21c10e commit 85b34ec
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
10 changes: 7 additions & 3 deletions mito-ai/src/Extensions/AiChat/ChatHistoryManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type PromptType = 'chat' | 'smartDebug' | 'codeExplain' | 'system'
export interface IDisplayOptimizedChatHistory {
message: OpenAI.Chat.ChatCompletionMessageParam
type: 'openai message' | 'connection error',
mitoAIConnectionErrorType?: string | null,
codeCellID: string | undefined
}

Expand Down Expand Up @@ -187,7 +188,8 @@ export class ChatHistoryManager {
addAIMessageFromResponse(
messageContent: string | null,
promptType: PromptType,
mitoAIConnectionError: boolean=false
mitoAIConnectionError: boolean=false,
mitoAIConnectionErrorType: string | null = null
): void {
if (messageContent === null) {
return
Expand All @@ -201,20 +203,22 @@ export class ChatHistoryManager {
role: 'assistant',
content: messageContent
}
this._addAIMessage(aiMessage, promptType, mitoAIConnectionError)
this._addAIMessage(aiMessage, promptType, mitoAIConnectionError, mitoAIConnectionErrorType)
}

_addAIMessage(
aiMessage: OpenAI.Chat.ChatCompletionMessageParam,
promptType: PromptType,
mitoAIConnectionError: boolean=false
mitoAIConnectionError: boolean=false,
mitoAIConnectionErrorType?: string | null
): void {
const activeCellID = getActiveCellID(this.notebookTracker)

this.history.displayOptimizedChatHistory.push(
{
message: aiMessage,
type: mitoAIConnectionError ? 'connection error' : 'openai message',
mitoAIConnectionErrorType: mitoAIConnectionErrorType,
codeCellID: activeCellID
}
);
Expand Down
35 changes: 35 additions & 0 deletions mito-ai/src/Extensions/AiChat/ChatMessage/AlertBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { useEffect, useState } from 'react';
import ErrorIcon from '../../../icons/ErrorIcon';


interface IAlertBlockProps {
content: string;
mitoAIConnectionErrorType: string | null;
}

const AlertBlock: React.FC<IAlertBlockProps> = ({ content, mitoAIConnectionErrorType }) => {
const [message, setMessage] = useState<string | JSX.Element>("");

useEffect(() => {
if (mitoAIConnectionErrorType === "mito_server_free_tier_limit_reached") {
const message = (
<p>
You've reached the free tier limit for Mito AI. <a href="https://www.trymito.io/plans" target="_blank">Upgrade to Pro for unlimited uses</a> or supply your own OpenAI API key.
</p>
);
setMessage(message);
}
else {
setMessage(content);
}
}, [content]);

return (
<div className="chat-message-alert">
<span style={{ marginRight: '4px' }}><ErrorIcon /></span>
{message}
</div>
);
};

export default AlertBlock;
17 changes: 11 additions & 6 deletions mito-ai/src/Extensions/AiChat/ChatMessage/ChatMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import OpenAI from 'openai';
import { classNames } from '../../../utils/classNames';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import CodeBlock from './CodeBlock';
import AlertBlock from './AlertBlock';
import MarkdownBlock from './MarkdownBlock';
import { INotebookTracker } from '@jupyterlab/notebook';
import { PYTHON_CODE_BLOCK_START_WITHOUT_NEW_LINE, splitStringWithCodeBlocks } from '../../../utils/strings';
import ErrorIcon from '../../../icons/ErrorIcon';
import { JupyterFrontEnd } from '@jupyterlab/application';
import { OperatingSystem } from '../../../utils/user';
import PencilIcon from '../../../icons/Pencil';
Expand All @@ -24,6 +24,7 @@ interface IChatMessageProps {
codeCellID: string | undefined
messageIndex: number
mitoAIConnectionError: boolean
mitoAIConnectionErrorType: string | null
notebookTracker: INotebookTracker
renderMimeRegistry: IRenderMimeRegistry
app: JupyterFrontEnd
Expand All @@ -41,6 +42,7 @@ const ChatMessage: React.FC<IChatMessageProps> = ({
message,
messageIndex,
mitoAIConnectionError,
mitoAIConnectionErrorType,
notebookTracker,
renderMimeRegistry,
isLastAiMessage,
Expand Down Expand Up @@ -163,11 +165,14 @@ const ChatMessage: React.FC<IChatMessageProps> = ({
}
}}
>
{mitoAIConnectionError && <span style={{ marginRight: '4px' }}><ErrorIcon /></span>}
<MarkdownBlock
markdown={messagePart}
renderMimeRegistry={renderMimeRegistry}
/>
{mitoAIConnectionError ? (
<AlertBlock content={messagePart} mitoAIConnectionErrorType={mitoAIConnectionErrorType} />
) : (
<MarkdownBlock
markdown={messagePart}
renderMimeRegistry={renderMimeRegistry}
/>
)}
</p>
{message.role === 'user' && (
<div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '4px' }}>
Expand Down
4 changes: 3 additions & 1 deletion mito-ai/src/Extensions/AiChat/ChatTaskpane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ const ChatTaskpane: React.FC<IChatTaskpaneProps> = ({
? aiResponse.error.hint
: `${aiResponse.error.error_type}: ${aiResponse.error.title}`,
promptType,
true
true,
aiResponse.error.title
);
setChatHistoryManager(newChatHistoryManager);
} else {
Expand Down Expand Up @@ -544,6 +545,7 @@ const ChatTaskpane: React.FC<IChatTaskpaneProps> = ({
message={displayOptimizedChat.message}
codeCellID={displayOptimizedChat.codeCellID}
mitoAIConnectionError={displayOptimizedChat.type === 'connection error'}
mitoAIConnectionErrorType={displayOptimizedChat.mitoAIConnectionErrorType || null}
messageIndex={index}
notebookTracker={notebookTracker}
renderMimeRegistry={renderMimeRegistry}
Expand Down
12 changes: 12 additions & 0 deletions mito-ai/style/ChatTaskpane.css
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,16 @@
.code-block-toolbar button {
height: 16px;
font-size: 12px !important;
}

.chat-message-alert {
background-color: var(--purple-300);
border-radius: 5px;
padding: 10px;
border: 1px solid var(--purple-500);
}

.chat-message-alert a {
color: var(--purple-700);
text-decoration: underline;
}

0 comments on commit 85b34ec

Please sign in to comment.