diff --git a/packages/shared/config/constant.ts b/packages/shared/config/constant.ts index c05fde902..1e02ce770 100644 --- a/packages/shared/config/constant.ts +++ b/packages/shared/config/constant.ts @@ -7,6 +7,11 @@ export const documentExts = ['.pdf', '.doc', '.docx', '.pptx', '.xlsx', '.odt', export const thirdPartyApplicationExts = ['.draftsExport'] export const bookExts = ['.epub'] +export const API_SERVER_DEFAULTS = { + HOST: '127.0.0.1', + PORT: 23333 +} + /** * A flat array of all file extensions known by the linguist database. * This is the primary source for identifying code files. diff --git a/src/main/apiServer/config.ts b/src/main/apiServer/config.ts index 60b1986be..0966827a7 100644 --- a/src/main/apiServer/config.ts +++ b/src/main/apiServer/config.ts @@ -1,3 +1,4 @@ +import { API_SERVER_DEFAULTS } from '@shared/config/constant' import type { ApiServerConfig } from '@types' import { v4 as uuidv4 } from 'uuid' @@ -6,9 +7,6 @@ import { reduxService } from '../services/ReduxService' const logger = loggerService.withContext('ApiServerConfig') -const defaultHost = 'localhost' -const defaultPort = 23333 - class ConfigManager { private _config: ApiServerConfig | null = null @@ -30,8 +28,8 @@ class ConfigManager { } this._config = { enabled: serverSettings?.enabled ?? false, - port: serverSettings?.port ?? defaultPort, - host: defaultHost, + port: serverSettings?.port ?? API_SERVER_DEFAULTS.PORT, + host: serverSettings?.host ?? API_SERVER_DEFAULTS.HOST, apiKey: apiKey } return this._config @@ -39,8 +37,8 @@ class ConfigManager { logger.warn('Failed to load config from Redux, using defaults', { error }) this._config = { enabled: false, - port: defaultPort, - host: defaultHost, + port: API_SERVER_DEFAULTS.PORT, + host: API_SERVER_DEFAULTS.HOST, apiKey: this.generateApiKey() } return this._config diff --git a/src/main/apiServer/middleware/openapi.ts b/src/main/apiServer/middleware/openapi.ts index ff01005bd..6b374901c 100644 --- a/src/main/apiServer/middleware/openapi.ts +++ b/src/main/apiServer/middleware/openapi.ts @@ -20,8 +20,8 @@ const swaggerOptions: swaggerJSDoc.Options = { }, servers: [ { - url: 'http://localhost:23333', - description: 'Local development server' + url: '/', + description: 'Current server' } ], components: { diff --git a/src/renderer/src/pages/settings/ToolSettings/ApiServerSettings/ApiServerSettings.tsx b/src/renderer/src/pages/settings/ToolSettings/ApiServerSettings/ApiServerSettings.tsx index 0205ec676..58a0ab4b1 100644 --- a/src/renderer/src/pages/settings/ToolSettings/ApiServerSettings/ApiServerSettings.tsx +++ b/src/renderer/src/pages/settings/ToolSettings/ApiServerSettings/ApiServerSettings.tsx @@ -4,6 +4,7 @@ import type { RootState } from '@renderer/store' import { useAppDispatch } from '@renderer/store' import { setApiServerApiKey, setApiServerPort } from '@renderer/store/settings' import { formatErrorMessage } from '@renderer/utils/error' +import { API_SERVER_DEFAULTS } from '@shared/config/constant' import { Alert, Button, Input, InputNumber, Tooltip, Typography } from 'antd' import { Copy, ExternalLink, Play, RotateCcw, Square } from 'lucide-react' import type { FC } from 'react' @@ -56,7 +57,7 @@ const ApiServerSettings: FC = () => { } const handlePortChange = (value: string) => { - const port = parseInt(value) || 23333 + const port = parseInt(value) || API_SERVER_DEFAULTS.PORT if (port >= 1000 && port <= 65535) { dispatch(setApiServerPort(port)) } @@ -64,7 +65,9 @@ const ApiServerSettings: FC = () => { const openApiDocs = () => { if (apiServerRunning) { - window.open(`http://localhost:${apiServerConfig.port}/api-docs`, '_blank') + const host = apiServerConfig.host || API_SERVER_DEFAULTS.HOST + const port = apiServerConfig.port || API_SERVER_DEFAULTS.PORT + window.open(`http://${host}:${port}/api-docs`, '_blank') } } @@ -98,7 +101,9 @@ const ApiServerSettings: FC = () => { {apiServerRunning ? t('apiServer.status.running') : t('apiServer.status.stopped')} - {apiServerRunning ? `http://localhost:${apiServerConfig.port}` : t('apiServer.fields.port.description')} + {apiServerRunning + ? `http://${apiServerConfig.host || API_SERVER_DEFAULTS.HOST}:${apiServerConfig.port || API_SERVER_DEFAULTS.PORT}` + : t('apiServer.fields.port.description')} @@ -119,11 +124,11 @@ const ApiServerSettings: FC = () => { {!apiServerRunning && ( handlePortChange(String(value || 23333))} + onChange={(value) => handlePortChange(String(value || API_SERVER_DEFAULTS.PORT))} min={1000} max={65535} disabled={apiServerRunning} - placeholder="23333" + placeholder={String(API_SERVER_DEFAULTS.PORT)} size="middle" /> )} diff --git a/src/renderer/src/store/migrate.ts b/src/renderer/src/store/migrate.ts index 6f05c2b34..0e8922790 100644 --- a/src/renderer/src/store/migrate.ts +++ b/src/renderer/src/store/migrate.ts @@ -32,6 +32,7 @@ import { isSupportDeveloperRoleProvider, isSupportStreamOptionsProvider } from '@renderer/utils/provider' +import { API_SERVER_DEFAULTS } from '@shared/config/constant' import { defaultByPassRules, UpgradeChannel } from '@shared/config/constant' import { isEmpty } from 'lodash' import { createMigrate } from 'redux-persist' @@ -2032,8 +2033,8 @@ const migrateConfig = { if (!state.settings.apiServer) { state.settings.apiServer = { enabled: false, - host: 'localhost', - port: 23333, + host: API_SERVER_DEFAULTS.HOST, + port: API_SERVER_DEFAULTS.PORT, apiKey: `cs-sk-${uuid()}` } } @@ -2909,6 +2910,9 @@ const migrateConfig = { }, '180': (state: RootState) => { try { + if (state.settings.apiServer) { + state.settings.apiServer.host = API_SERVER_DEFAULTS.HOST + } // @ts-expect-error if (state.settings.openAI.summaryText === 'undefined') { state.settings.openAI.summaryText = undefined diff --git a/src/renderer/src/store/settings.ts b/src/renderer/src/store/settings.ts index d6c685606..36a478853 100644 --- a/src/renderer/src/store/settings.ts +++ b/src/renderer/src/store/settings.ts @@ -18,7 +18,7 @@ import type { import { ThemeMode } from '@renderer/types' import type { OpenAISummaryText, OpenAIVerbosity } from '@renderer/types/aiCoreTypes' import { uuid } from '@renderer/utils' -import { UpgradeChannel } from '@shared/config/constant' +import { API_SERVER_DEFAULTS, UpgradeChannel } from '@shared/config/constant' import type { RemoteSyncState } from './backup' @@ -410,8 +410,8 @@ export const initialState: SettingsState = { // API Server apiServer: { enabled: false, - host: 'localhost', - port: 23333, + host: API_SERVER_DEFAULTS.HOST, + port: API_SERVER_DEFAULTS.PORT, apiKey: `cs-sk-${uuid()}` }, showMessageOutline: false diff --git a/tests/apis/agents/agents.http b/tests/apis/agents/agents.http index ce21217ff..3717fbfe3 100644 --- a/tests/apis/agents/agents.http +++ b/tests/apis/agents/agents.http @@ -1,4 +1,4 @@ -@host=http://localhost:23333 +@host=http://127.0.0.1:23333 @token=cs-sk-af798ed4-7cf5-4fd7-ae4b-df203b164194 @agent_id=agent_1758092281575_tn9dxio9k @@ -56,4 +56,3 @@ Content-Type: application/json "max_turns": 5 } } - diff --git a/tests/apis/agents/sessions.http b/tests/apis/agents/sessions.http index f7e941c93..b236e214d 100644 --- a/tests/apis/agents/sessions.http +++ b/tests/apis/agents/sessions.http @@ -1,5 +1,5 @@ -@host=http://localhost:23333 +@host=http://127.0.0.1:23333 @token=cs-sk-af798ed4-7cf5-4fd7-ae4b-df203b164194 @agent_id=agent_1758092281575_tn9dxio9k @session_id=session_1758278828236_mqj91e7c0 diff --git a/tests/apis/chat.http b/tests/apis/chat.http index eefa86dee..ab556ccb0 100644 --- a/tests/apis/chat.http +++ b/tests/apis/chat.http @@ -1,4 +1,4 @@ -@host=http://localhost:23333 +@host=http://127.0.0.1:23333 @token=cs-sk-af798ed4-7cf5-4fd7-ae4b-df203b164194 @agent_id=agent_1758092281575_tn9dxio9k