cherry-studio/src/main/services/ApiServerService.ts
Phantom 1a972ac0e0
fix: api server status (#10734)
* refactor(apiServer): move api server types to dedicated module

Restructure api server type definitions by moving them from index.ts to a dedicated apiServer.ts file. This improves code organization and maintainability by grouping related types together.

* feat(api-server): add api server management hooks and integration

Extract api server management logic into reusable hook and integrate with settings page

* feat(api-server): improve api server status handling and error messages

- add new error messages for api server status
- optimize initial state and loading in useApiServer hook
- centralize api server enabled check via useApiServer hook
- update components to use new api server status handling

* fix(agents): update error message key for agent server not running

* fix(i18n): update api server status messages across locales

Remove redundant 'notRunning' message in en-us locale
Add consistent 'not_running' error message in all locales
Add missing 'notEnabled' message in several locales

* refactor: update api server type imports to use @types

Move api server related type imports from renderer/src/types to @types package for better code organization and maintainability

* docs(IpcChannel): add comment about unused api-server:get-config

Add TODO comment about data inconsistency in useApiServer hook

* refactor(assistants): pass apiServerEnabled as prop instead of using hook

Move apiServerEnabled from being fetched via useApiServer hook to being passed as a prop through component hierarchy. This improves maintainability by making dependencies more explicit and reducing hook usage in child components.

* style(AssistantsTab): add consistent margin-bottom to alert components

* feat(useAgent): add api server status checks before fetching agent

Ensure api server is enabled and running before attempting to fetch agent data
2025-10-16 12:49:31 +08:00

115 lines
3.0 KiB
TypeScript

import { IpcChannel } from '@shared/IpcChannel'
import {
ApiServerConfig,
GetApiServerStatusResult,
RestartApiServerStatusResult,
StartApiServerStatusResult,
StopApiServerStatusResult
} from '@types'
import { ipcMain } from 'electron'
import { apiServer } from '../apiServer'
import { config } from '../apiServer/config'
import { loggerService } from './LoggerService'
const logger = loggerService.withContext('ApiServerService')
export class ApiServerService {
constructor() {
// Use the new clean implementation
}
async start(): Promise<void> {
try {
await apiServer.start()
logger.info('API Server started successfully')
} catch (error: any) {
logger.error('Failed to start API Server:', error)
throw error
}
}
async stop(): Promise<void> {
try {
await apiServer.stop()
logger.info('API Server stopped successfully')
} catch (error: any) {
logger.error('Failed to stop API Server:', error)
throw error
}
}
async restart(): Promise<void> {
try {
await apiServer.restart()
logger.info('API Server restarted successfully')
} catch (error: any) {
logger.error('Failed to restart API Server:', error)
throw error
}
}
isRunning(): boolean {
return apiServer.isRunning()
}
async getCurrentConfig(): Promise<ApiServerConfig> {
return config.get()
}
registerIpcHandlers(): void {
// API Server
ipcMain.handle(IpcChannel.ApiServer_Start, async (): Promise<StartApiServerStatusResult> => {
try {
await this.start()
return { success: true }
} catch (error: any) {
return { success: false, error: error instanceof Error ? error.message : 'Unknown error' }
}
})
ipcMain.handle(IpcChannel.ApiServer_Stop, async (): Promise<StopApiServerStatusResult> => {
try {
await this.stop()
return { success: true }
} catch (error: any) {
return { success: false, error: error instanceof Error ? error.message : 'Unknown error' }
}
})
ipcMain.handle(IpcChannel.ApiServer_Restart, async (): Promise<RestartApiServerStatusResult> => {
try {
await this.restart()
return { success: true }
} catch (error: any) {
return { success: false, error: error instanceof Error ? error.message : 'Unknown error' }
}
})
ipcMain.handle(IpcChannel.ApiServer_GetStatus, async (): Promise<GetApiServerStatusResult> => {
try {
const config = await this.getCurrentConfig()
return {
running: this.isRunning(),
config
}
} catch (error: any) {
return {
running: this.isRunning(),
config: null
}
}
})
ipcMain.handle(IpcChannel.ApiServer_GetConfig, async () => {
try {
return this.getCurrentConfig()
} catch (error: any) {
return null
}
})
}
}
// Export singleton instance
export const apiServerService = new ApiServerService()