refactor: enhance API server state management and remove unused initialization in useAppInit

This commit is contained in:
kangfenmao 2025-11-06 14:21:25 +08:00
parent a64b94a41f
commit 3834c5d402
3 changed files with 11 additions and 15 deletions

View File

@ -41,6 +41,7 @@ export const useAgents = () => {
// NOTE: We only use the array for now. useUpdateAgent depends on this behavior. // NOTE: We only use the array for now. useUpdateAgent depends on this behavior.
return result.data return result.data
}, [apiServerConfig.enabled, apiServerRunning, client, t]) }, [apiServerConfig.enabled, apiServerRunning, client, t])
const { data, error, isLoading, mutate } = useSWR(swrKey, fetcher) const { data, error, isLoading, mutate } = useSWR(swrKey, fetcher)
const { chat } = useRuntime() const { chat } = useRuntime()
const { activeAgentId } = chat const { activeAgentId } = chat

View File

@ -31,21 +31,24 @@ export const useApiServer = () => {
try { try {
const status = await window.api.apiServer.getStatus() const status = await window.api.apiServer.getStatus()
setApiServerRunning(status.running) setApiServerRunning(status.running)
if (status.running && !apiServerConfig.enabled) {
setApiServerEnabled(true)
}
} catch (error: any) { } catch (error: any) {
logger.error('Failed to check API server status:', error) logger.error('Failed to check API server status:', error)
} finally { } finally {
setApiServerLoading(false) setApiServerLoading(false)
} }
}, []) }, [apiServerConfig.enabled, setApiServerEnabled])
const startApiServer = useCallback(async () => { const startApiServer = useCallback(async () => {
if (apiServerLoading) return if (apiServerLoading) return
setApiServerLoading(true) setApiServerLoading(true)
try { try {
const result = await window.api.apiServer.start() const result = await window.api.apiServer.start()
if (result.success) { if (result.success) {
setApiServerRunning(true) setApiServerRunning(true)
setApiServerEnabled(true)
window.toast.success(t('apiServer.messages.startSuccess')) window.toast.success(t('apiServer.messages.startSuccess'))
} else { } else {
window.toast.error(t('apiServer.messages.startError') + result.error) window.toast.error(t('apiServer.messages.startError') + result.error)
@ -55,16 +58,16 @@ export const useApiServer = () => {
} finally { } finally {
setApiServerLoading(false) setApiServerLoading(false)
} }
}, [apiServerLoading, t]) }, [apiServerLoading, setApiServerEnabled, t])
const stopApiServer = useCallback(async () => { const stopApiServer = useCallback(async () => {
if (apiServerLoading) return if (apiServerLoading) return
setApiServerLoading(true) setApiServerLoading(true)
try { try {
const result = await window.api.apiServer.stop() const result = await window.api.apiServer.stop()
if (result.success) { if (result.success) {
setApiServerRunning(false) setApiServerRunning(false)
setApiServerEnabled(false)
window.toast.success(t('apiServer.messages.stopSuccess')) window.toast.success(t('apiServer.messages.stopSuccess'))
} else { } else {
window.toast.error(t('apiServer.messages.stopError') + result.error) window.toast.error(t('apiServer.messages.stopError') + result.error)
@ -74,14 +77,14 @@ export const useApiServer = () => {
} finally { } finally {
setApiServerLoading(false) setApiServerLoading(false)
} }
}, [apiServerLoading, t]) }, [apiServerLoading, setApiServerEnabled, t])
const restartApiServer = useCallback(async () => { const restartApiServer = useCallback(async () => {
if (apiServerLoading) return if (apiServerLoading) return
setApiServerLoading(true) setApiServerLoading(true)
try { try {
const result = await window.api.apiServer.restart() const result = await window.api.apiServer.restart()
setApiServerEnabled(result.success)
if (result.success) { if (result.success) {
await checkApiServerStatus() await checkApiServerStatus()
window.toast.success(t('apiServer.messages.restartSuccess')) window.toast.success(t('apiServer.messages.restartSuccess'))
@ -93,7 +96,7 @@ export const useApiServer = () => {
} finally { } finally {
setApiServerLoading(false) setApiServerLoading(false)
} }
}, [apiServerLoading, checkApiServerStatus, t]) }, [apiServerLoading, checkApiServerStatus, setApiServerEnabled, t])
useEffect(() => { useEffect(() => {
checkApiServerStatus() checkApiServerStatus()

View File

@ -24,7 +24,6 @@ import { useLiveQuery } from 'dexie-react-hooks'
import { useEffect } from 'react' import { useEffect } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useApiServer } from './useApiServer'
import { useDefaultModel } from './useAssistant' import { useDefaultModel } from './useAssistant'
import useFullScreenNotice from './useFullScreenNotice' import useFullScreenNotice from './useFullScreenNotice'
import { useRuntime } from './useRuntime' import { useRuntime } from './useRuntime'
@ -52,8 +51,6 @@ export function useAppInit() {
const avatar = useLiveQuery(() => db.settings.get('image://avatar')) const avatar = useLiveQuery(() => db.settings.get('image://avatar'))
const { theme } = useTheme() const { theme } = useTheme()
const memoryConfig = useAppSelector(selectMemoryConfig) const memoryConfig = useAppSelector(selectMemoryConfig)
const { apiServerConfig, startApiServer } = useApiServer()
const apiServerEnabled = apiServerConfig.enabled
useEffect(() => { useEffect(() => {
document.getElementById('spinner')?.remove() document.getElementById('spinner')?.remove()
@ -248,9 +245,4 @@ export function useAppInit() {
useEffect(() => { useEffect(() => {
checkDataLimit() checkDataLimit()
}, []) }, [])
useEffect(() => {
apiServerEnabled && startApiServer()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [apiServerEnabled])
} }