mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-25 03:10:08 +08:00
* feat: add api server
This reverts commit c76aa03566.
* update yarn.lock
* fix: correct import paths in ToolSettings component
Update import paths for PreprocessSettings and WebSearchSettings to reference correct locations in DocProcessSettings and WebSearchSettings directories.
* feat(settings): add API server settings link and route
* fix(auth): improve authorization handling and error responses
* feat(chat): enhance model validation and logging for chat completions
feat(models): improve logging for model retrieval and filtering
feat(utils): add model ID validation and support for OpenAI providers
* feat(api-server): refactor config loading and remove unused ToolSettings component
* refactor(ApiServerService): simplify config retrieval and improve error handling in ApiServerSettings
* fix(mcp): remove unnecessary await in listTools return statement
* refactor(ApiServerSettings): replace window.message with window.toast for notifications
---------
Co-authored-by: kangfenmao <kangfenmao@qq.com>
66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import { ApiServerConfig } from '@types'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
|
|
import { loggerService } from '../services/LoggerService'
|
|
import { reduxService } from '../services/ReduxService'
|
|
|
|
const logger = loggerService.withContext('ApiServerConfig')
|
|
|
|
const defaultHost = 'localhost'
|
|
const defaultPort = 23333
|
|
|
|
class ConfigManager {
|
|
private _config: ApiServerConfig | null = null
|
|
|
|
private generateApiKey(): string {
|
|
return `cs-sk-${uuidv4()}`
|
|
}
|
|
|
|
async load(): Promise<ApiServerConfig> {
|
|
try {
|
|
const settings = await reduxService.select('state.settings')
|
|
const serverSettings = settings?.apiServer
|
|
let apiKey = serverSettings?.apiKey
|
|
if (!apiKey || apiKey.trim() === '') {
|
|
apiKey = this.generateApiKey()
|
|
await reduxService.dispatch({
|
|
type: 'settings/setApiServerApiKey',
|
|
payload: apiKey
|
|
})
|
|
}
|
|
this._config = {
|
|
enabled: serverSettings?.enabled ?? false,
|
|
port: serverSettings?.port ?? defaultPort,
|
|
host: defaultHost,
|
|
apiKey: apiKey
|
|
}
|
|
return this._config
|
|
} catch (error: any) {
|
|
logger.warn('Failed to load config from Redux, using defaults:', error)
|
|
this._config = {
|
|
enabled: false,
|
|
port: defaultPort,
|
|
host: defaultHost,
|
|
apiKey: this.generateApiKey()
|
|
}
|
|
return this._config
|
|
}
|
|
}
|
|
|
|
async get(): Promise<ApiServerConfig> {
|
|
if (!this._config) {
|
|
await this.load()
|
|
}
|
|
if (!this._config) {
|
|
throw new Error('Failed to load API server configuration')
|
|
}
|
|
return this._config
|
|
}
|
|
|
|
async reload(): Promise<ApiServerConfig> {
|
|
return await this.load()
|
|
}
|
|
}
|
|
|
|
export const config = new ConfigManager()
|