Skip to content

Commit

Permalink
fix: error message being sent along with conversation when inference (j…
Browse files Browse the repository at this point in the history
…anhq#2242)

Signed-off-by: James <[email protected]>
Co-authored-by: James <[email protected]>
  • Loading branch information
namchuai and James authored Mar 5, 2024
1 parent c91b212 commit 5104912
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 10 deletions.
9 changes: 9 additions & 0 deletions core/src/types/message/messageEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export type ThreadMessage = {
metadata?: Record<string, unknown>

type?: string

/** The error code which explain what error type. Used in conjunction with MessageStatus.Error */
error_code?: ErrorCode
}

/**
Expand Down Expand Up @@ -77,6 +80,12 @@ export enum MessageStatus {
Stopped = 'stopped',
}

export enum ErrorCode {
InvalidApiKey = 'invalid_api_key',

Unknown = 'unknown',
}

/**
* The content type of the message.
*/
Expand Down
10 changes: 7 additions & 3 deletions extensions/inference-openai-extension/src/helpers/sse.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ErrorCode } from '@janhq/core'
import { Observable } from 'rxjs'

/**
Expand Down Expand Up @@ -40,9 +41,12 @@ export function requestInference(
})
.then(async (response) => {
if (!response.ok) {
subscriber.next(
(await response.json()).error?.message ?? 'Error occurred.'
)
const data = await response.json()
const error = {
message: data.error?.message ?? 'Error occurred.',
code: data.error?.code ?? ErrorCode.Unknown,
}
subscriber.error(error)
subscriber.complete()
return
}
Expand Down
1 change: 1 addition & 0 deletions extensions/inference-openai-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export default class JanInferenceOpenAIExtension extends BaseExtension {
}
message.content = [messageContent]
message.status = MessageStatus.Error
message.error_code = err.code
events.emit(MessageEvent.OnMessageUpdate, message)
},
})
Expand Down
10 changes: 5 additions & 5 deletions web/screens/Chat/ChatBody/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ const ChatBody: React.FC = () => {
<ScrollToBottom className="flex h-full w-full flex-col">
{messages.map((message, index) => (
<div key={message.id}>
{((message.status !== MessageStatus.Error &&
message.status !== MessageStatus.Pending) ||
message.content.length > 0) && (
<ChatItem {...message} key={message.id} />
)}
{message.status !== MessageStatus.Error &&
message.content.length > 0 && (
<ChatItem {...message} key={message.id} />
)}

{(message.status === MessageStatus.Error ||
message.status === MessageStatus.Stopped) &&
index === messages.length - 1 && (
Expand Down
8 changes: 6 additions & 2 deletions web/screens/Chat/ErrorMessage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessageStatus, ThreadMessage } from '@janhq/core'
import { ErrorCode, MessageStatus, ThreadMessage } from '@janhq/core'
import { Button } from '@janhq/uikit'
import { useAtomValue, useSetAtom } from 'jotai'
import { RefreshCcw } from 'lucide-react'
Expand All @@ -10,6 +10,8 @@ import ModalTroubleShooting, {
import { loadModelErrorAtom } from '@/hooks/useActiveModel'
import useSendChatMessage from '@/hooks/useSendChatMessage'

import { getErrorTitle } from '@/utils/errorMessage'

import { getCurrentChatMessagesAtom } from '@/helpers/atoms/ChatMessage.atom'

const ErrorMessage = ({ message }: { message: ThreadMessage }) => {
Expand All @@ -25,6 +27,8 @@ const ErrorMessage = ({ message }: { message: ThreadMessage }) => {
resendChatMessage(message)
}

const errorTitle = getErrorTitle(message.error_code ?? ErrorCode.Unknown)

return (
<div className="mt-10">
{message.status === MessageStatus.Stopped && (
Expand Down Expand Up @@ -68,7 +72,7 @@ const ErrorMessage = ({ message }: { message: ThreadMessage }) => {
key={message.id}
className="flex flex-col items-center text-center text-sm font-medium text-gray-500"
>
<p>{`Apologies, something’s amiss!`}</p>
<p>{errorTitle}</p>
<p>
Jan’s in beta. Access&nbsp;
<span
Expand Down
11 changes: 11 additions & 0 deletions web/utils/errorMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ErrorCode } from '@janhq/core'

export const getErrorTitle = (errorCode: ErrorCode) => {
if (errorCode === ErrorCode.Unknown) {
return 'Apologies, something’s amiss!'
}

if (errorCode === ErrorCode.InvalidApiKey) {
return 'Invalid API key. Please check your API key and try again.'
}
}

0 comments on commit 5104912

Please sign in to comment.