fix: inconsistent error handling

This commit is contained in:
Louis 2024-11-19 23:18:52 +07:00
parent c501641c91
commit 8bd0f3da21
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
3 changed files with 12 additions and 41 deletions

View File

@ -26,15 +26,13 @@ export const stateModelAtom = atom<ModelState>({
model: undefined, model: undefined,
}) })
const pendingModelLoadAtom = atom<boolean>(false)
export function useActiveModel() { export function useActiveModel() {
const [activeModel, setActiveModel] = useAtom(activeModelAtom) const [activeModel, setActiveModel] = useAtom(activeModelAtom)
const activeThread = useAtomValue(activeThreadAtom) const activeThread = useAtomValue(activeThreadAtom)
const [stateModel, setStateModel] = useAtom(stateModelAtom) const [stateModel, setStateModel] = useAtom(stateModelAtom)
const downloadedModels = useAtomValue(downloadedModelsAtom) const downloadedModels = useAtomValue(downloadedModelsAtom)
const setLoadModelError = useSetAtom(loadModelErrorAtom) const setLoadModelError = useSetAtom(loadModelErrorAtom)
const [pendingModelLoad, setPendingModelLoad] = useAtom(pendingModelLoadAtom) const pendingModelLoad = useRef(false)
const isVulkanEnabled = useAtomValue(vulkanEnabledAtom) const isVulkanEnabled = useAtomValue(vulkanEnabledAtom)
const downloadedModelsRef = useRef<Model[]>([]) const downloadedModelsRef = useRef<Model[]>([])
@ -55,7 +53,7 @@ export function useActiveModel() {
if (activeModel) { if (activeModel) {
await stopModel(activeModel) await stopModel(activeModel)
} }
setPendingModelLoad(true) pendingModelLoad.current = true
let model = downloadedModelsRef?.current.find((e) => e.id === modelId) let model = downloadedModelsRef?.current.find((e) => e.id === modelId)
@ -120,16 +118,16 @@ export function useActiveModel() {
undefined, undefined,
})) }))
if (!pendingModelLoad && abortable) { if (!pendingModelLoad.current && abortable) {
return Promise.reject(new Error('aborted')) return Promise.reject(new Error('aborted'))
} }
toaster({ toaster({
title: 'Failed!', title: 'Failed!',
description: `Model ${model.id} failed to start.`, description: `Model ${model.id} failed to start. ${error.message ?? ''}`,
type: 'error', type: 'error',
}) })
setLoadModelError(error) setLoadModelError(error.message ?? error)
return Promise.reject(error) return Promise.reject(error)
}) })
} }
@ -147,16 +145,10 @@ export function useActiveModel() {
.then(() => { .then(() => {
setActiveModel(undefined) setActiveModel(undefined)
setStateModel({ state: 'start', loading: false, model: undefined }) setStateModel({ state: 'start', loading: false, model: undefined })
setPendingModelLoad(false) pendingModelLoad.current = false
}) })
}, },
[ [activeModel, setStateModel, setActiveModel, stateModel]
activeModel,
setStateModel,
setActiveModel,
setPendingModelLoad,
stateModel,
]
) )
const stopInference = useCallback(async () => { const stopInference = useCallback(async () => {

View File

@ -27,7 +27,7 @@ import { MessageRequestBuilder } from '@/utils/messageRequestBuilder'
import { ThreadMessageBuilder } from '@/utils/threadMessageBuilder' import { ThreadMessageBuilder } from '@/utils/threadMessageBuilder'
import { loadModelErrorAtom, useActiveModel } from './useActiveModel' import { useActiveModel } from './useActiveModel'
import { extensionManager } from '@/extension/ExtensionManager' import { extensionManager } from '@/extension/ExtensionManager'
import { import {
@ -60,10 +60,8 @@ export default function useSendChatMessage() {
const currentMessages = useAtomValue(getCurrentChatMessagesAtom) const currentMessages = useAtomValue(getCurrentChatMessagesAtom)
const selectedModel = useAtomValue(selectedModelAtom) const selectedModel = useAtomValue(selectedModelAtom)
const { activeModel, startModel } = useActiveModel() const { activeModel, startModel } = useActiveModel()
const loadModelFailed = useAtomValue(loadModelErrorAtom)
const modelRef = useRef<Model | undefined>() const modelRef = useRef<Model | undefined>()
const loadModelFailedRef = useRef<string | undefined>()
const activeModelParams = useAtomValue(getActiveThreadModelParamsAtom) const activeModelParams = useAtomValue(getActiveThreadModelParamsAtom)
const engineParamsUpdate = useAtomValue(engineParamsUpdateAtom) const engineParamsUpdate = useAtomValue(engineParamsUpdateAtom)
@ -80,10 +78,6 @@ export default function useSendChatMessage() {
modelRef.current = activeModel modelRef.current = activeModel
}, [activeModel]) }, [activeModel])
useEffect(() => {
loadModelFailedRef.current = loadModelFailed
}, [loadModelFailed])
useEffect(() => { useEffect(() => {
activeThreadRef.current = activeThread activeThreadRef.current = activeThread
}, [activeThread]) }, [activeThread])

View File

@ -20,23 +20,8 @@ const LoadModelError = () => {
const setSelectedSettingScreen = useSetAtom(selectedSettingAtom) const setSelectedSettingScreen = useSetAtom(selectedSettingAtom)
const activeThread = useAtomValue(activeThreadAtom) const activeThread = useAtomValue(activeThreadAtom)
const PORT_NOT_AVAILABLE = 'PORT_NOT_AVAILABLE'
const ErrorMessage = () => { const ErrorMessage = () => {
if (loadModelError === PORT_NOT_AVAILABLE) { if (
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 (
typeof loadModelError?.includes === 'function' && typeof loadModelError?.includes === 'function' &&
loadModelError.includes('EXTENSION_IS_NOT_INSTALLED') loadModelError.includes('EXTENSION_IS_NOT_INSTALLED')
) { ) {
@ -63,10 +48,10 @@ const LoadModelError = () => {
) )
} else { } else {
return ( return (
<div> <div className="mx-6 flex flex-col items-center space-y-2 text-center font-medium text-[hsla(var(--text-secondary))]">
Apologies, {`Something's wrong.`}.&nbsp; {loadModelError && <p>{loadModelError}</p>}
<p> <p>
Access&nbsp; {`Something's wrong.`}&nbsp;Access&nbsp;
<span <span
className="cursor-pointer text-[hsla(var(--app-link))]" className="cursor-pointer text-[hsla(var(--app-link))]"
onClick={() => setModalTroubleShooting(true)} onClick={() => setModalTroubleShooting(true)}