From 1dde8b5a749ae984f16f4667cc217e869ffa80bc Mon Sep 17 00:00:00 2001 From: hex2077 Date: Fri, 14 Nov 2025 15:10:58 +0800 Subject: [PATCH] =?UTF-8?q?refactor(api):=20=E5=B0=86=E5=81=A5=E5=BA=B7?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E5=92=8C=E4=BB=A4=E7=89=8C=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E7=A7=BB=E8=87=B3=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=A4=84=E7=90=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构代码结构,将原本在api-manager.js中的健康检查端点和令牌计数请求处理 --- src/api-manager.js | 20 -------------------- src/request-handler.js | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/api-manager.js b/src/api-manager.js index a17125d..b4f849d 100644 --- a/src/api-manager.js +++ b/src/api-manager.js @@ -18,27 +18,7 @@ import { * @returns {Promise} - True if the request was handled by API */ export async function handleAPIRequests(method, path, req, res, currentConfig, apiService, providerPoolManager, promptLogFilename) { - // Health check endpoint - if (method === 'GET' && path === '/health') { - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ - status: 'healthy', - timestamp: new Date().toISOString(), - provider: currentConfig.MODEL_PROVIDER - })); - return true; - } - // Ignore count_tokens requests - if (path.includes('/count_tokens')) { - console.log(`[Server] Ignoring count_tokens request: ${path}`); - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ - tokens: 0, - message: 'Token counting is not supported' - })); - return true; - } // Route model list requests if (method === 'GET') { diff --git a/src/request-handler.js b/src/request-handler.js index 4afdff5..aea628e 100644 --- a/src/request-handler.js +++ b/src/request-handler.js @@ -80,6 +80,28 @@ export function createRequestHandler(config, providerPoolManager) { } return; } + + // Health check endpoint + if (method === 'GET' && path === '/health') { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + status: 'healthy', + timestamp: new Date().toISOString(), + provider: currentConfig.MODEL_PROVIDER + })); + return true; + } + + // Ignore count_tokens requests + if (path.includes('/count_tokens')) { + console.log(`[Server] Ignoring count_tokens request: ${path}`); + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + tokens: 0, + message: 'Token counting is not supported' + })); + return true; + } // Check authentication for API requests if (!isAuthorized(req, requestUrl, currentConfig.REQUIRED_API_KEY)) {