Skip to content

Commit

Permalink
fix: inconsistent error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Nov 19, 2024
1 parent c501641 commit 8bd0f3d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 41 deletions.
22 changes: 7 additions & 15 deletions web/hooks/useActiveModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ export const stateModelAtom = atom<ModelState>({
model: undefined,
})

const pendingModelLoadAtom = atom<boolean>(false)

export function useActiveModel() {
const [activeModel, setActiveModel] = useAtom(activeModelAtom)
const activeThread = useAtomValue(activeThreadAtom)
const [stateModel, setStateModel] = useAtom(stateModelAtom)
const downloadedModels = useAtomValue(downloadedModelsAtom)
const setLoadModelError = useSetAtom(loadModelErrorAtom)
const [pendingModelLoad, setPendingModelLoad] = useAtom(pendingModelLoadAtom)
const pendingModelLoad = useRef(false)
const isVulkanEnabled = useAtomValue(vulkanEnabledAtom)

const downloadedModelsRef = useRef<Model[]>([])
Expand All @@ -55,7 +53,7 @@ export function useActiveModel() {
if (activeModel) {
await stopModel(activeModel)

Check warning on line 54 in web/hooks/useActiveModel.ts

View workflow job for this annotation

GitHub Actions / coverage-check

53-54 lines are not covered with tests
}
setPendingModelLoad(true)
pendingModelLoad.current = true

Check warning on line 56 in web/hooks/useActiveModel.ts

View workflow job for this annotation

GitHub Actions / coverage-check

56 line is not covered with tests

let model = downloadedModelsRef?.current.find((e) => e.id === modelId)

Check warning on line 58 in web/hooks/useActiveModel.ts

View workflow job for this annotation

GitHub Actions / coverage-check

58 line is not covered with tests

Expand Down Expand Up @@ -120,16 +118,16 @@ export function useActiveModel() {
undefined,
}))

if (!pendingModelLoad && abortable) {
if (!pendingModelLoad.current && abortable) {
return Promise.reject(new Error('aborted'))
}

toaster({
title: 'Failed!',
description: `Model ${model.id} failed to start.`,
description: `Model ${model.id} failed to start. ${error.message ?? ''}`,
type: 'error',
})
setLoadModelError(error)
setLoadModelError(error.message ?? error)
return Promise.reject(error)
})
}
Expand All @@ -147,16 +145,10 @@ export function useActiveModel() {
.then(() => {
setActiveModel(undefined)
setStateModel({ state: 'start', loading: false, model: undefined })
setPendingModelLoad(false)
pendingModelLoad.current = false
})
},
[
activeModel,
setStateModel,
setActiveModel,
setPendingModelLoad,
stateModel,
]
[activeModel, setStateModel, setActiveModel, stateModel]
)

const stopInference = useCallback(async () => {
Expand Down
8 changes: 1 addition & 7 deletions web/hooks/useSendChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { MessageRequestBuilder } from '@/utils/messageRequestBuilder'

import { ThreadMessageBuilder } from '@/utils/threadMessageBuilder'

import { loadModelErrorAtom, useActiveModel } from './useActiveModel'
import { useActiveModel } from './useActiveModel'

import { extensionManager } from '@/extension/ExtensionManager'
import {
Expand Down Expand Up @@ -60,10 +60,8 @@ export default function useSendChatMessage() {
const currentMessages = useAtomValue(getCurrentChatMessagesAtom)
const selectedModel = useAtomValue(selectedModelAtom)
const { activeModel, startModel } = useActiveModel()
const loadModelFailed = useAtomValue(loadModelErrorAtom)

const modelRef = useRef<Model | undefined>()
const loadModelFailedRef = useRef<string | undefined>()
const activeModelParams = useAtomValue(getActiveThreadModelParamsAtom)
const engineParamsUpdate = useAtomValue(engineParamsUpdateAtom)

Expand All @@ -80,10 +78,6 @@ export default function useSendChatMessage() {
modelRef.current = activeModel
}, [activeModel])

useEffect(() => {
loadModelFailedRef.current = loadModelFailed
}, [loadModelFailed])

useEffect(() => {
activeThreadRef.current = activeThread
}, [activeThread])
Expand Down
23 changes: 4 additions & 19 deletions web/screens/Thread/ThreadCenterPanel/LoadModelError/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,8 @@ const LoadModelError = () => {
const setSelectedSettingScreen = useSetAtom(selectedSettingAtom)
const activeThread = useAtomValue(activeThreadAtom)

const PORT_NOT_AVAILABLE = 'PORT_NOT_AVAILABLE'

const ErrorMessage = () => {
if (loadModelError === PORT_NOT_AVAILABLE) {
return (
<p>
Port 3928 is currently unavailable. Check for conflicting apps, or
access&nbsp;
<span
className="cursor-pointer text-[hsla(var(--app-link))]"
onClick={() => setModalTroubleShooting(true)}
>
troubleshooting assistance
</span>
</p>
)
} else if (
if (
typeof loadModelError?.includes === 'function' &&
loadModelError.includes('EXTENSION_IS_NOT_INSTALLED')
) {
Expand All @@ -63,10 +48,10 @@ const LoadModelError = () => {
)
} else {
return (
<div>
Apologies, {`Something's wrong.`}.&nbsp;
<div className="mx-6 flex flex-col items-center space-y-2 text-center font-medium text-[hsla(var(--text-secondary))]">
{loadModelError && <p>{loadModelError}</p>}
<p>
Access&nbsp;
{`Something's wrong.`}&nbsp;Access&nbsp;
<span
className="cursor-pointer text-[hsla(var(--app-link))]"
onClick={() => setModalTroubleShooting(true)}
Expand Down

0 comments on commit 8bd0f3d

Please sign in to comment.