Skip to content

Commit

Permalink
feat: handle network/parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
skoob13 committed Oct 29, 2024
1 parent 71a914d commit 0657e1a
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions frontend/src/scenes/max/maxLogic.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { captureException } from '@sentry/react'
import { shuffle } from 'd3'
import { createParser } from 'eventsource-parser'
import { actions, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
import { isHumanMessage } from 'scenes/max/utils'
import { isHumanMessage, isVisualizationMessage } from 'scenes/max/utils'

import {
AssistantEventType,
AssistantGenerationStatusEvent,
AssistantGenerationStatusType,
AssistantMessageType,
FailureMessage,
NodeKind,
RootAssistantMessage,
SuggestedQuestionsQuery,
Expand All @@ -27,6 +29,11 @@ export type ThreadMessage = RootAssistantMessage & {
status?: MessageStatus
}

const failureMessage: FailureMessage = {
type: AssistantMessageType.Failure,
content: 'Oops! It looks like I’m having trouble generating this trends insight. Could you please try again?',
}

export const maxLogic = kea<maxLogicType>([
path(['scenes', 'max', 'maxLogic']),
props({} as MaxLogicProps),
Expand Down Expand Up @@ -180,12 +187,31 @@ export const maxLogic = kea<maxLogicType>([
parser.feed(decoder.decode(value))

if (done) {
actions.setMessageStatus(newIndex, 'completed')
const generatedMessage = values.thread[newIndex]
if (generatedMessage && isVisualizationMessage(generatedMessage) && generatedMessage.plan) {
actions.setMessageStatus(newIndex, 'completed')
} else if (generatedMessage) {
actions.replaceMessage(newIndex, failureMessage)
} else {
actions.addMessage({
...failureMessage,
status: 'completed',
})
}
break
}
}
} catch {
actions.setMessageStatus(values.thread.length - 1 === newIndex ? newIndex : newIndex - 1, 'error')
} catch (e) {
captureException(e)

if (values.thread[newIndex]) {
actions.replaceMessage(newIndex, failureMessage)
} else {
actions.addMessage({
...failureMessage,
status: 'completed',
})
}
}

actions.setThreadLoaded()
Expand Down

0 comments on commit 0657e1a

Please sign in to comment.