src/middleware/
├── auth.ts # Bearer Token 认证中间件
├── cors.ts # CORS 处理中间件
└── request-preprocessor.ts # 模型别名、角色重写、参数过滤
src/handler/
└── chatHandler.ts # Chat Completions 请求处理
src/routes/
├── index.ts # 路由注册与健康检查
└── chat.ts # /chat/completions 路由
请求 → Express 服务器
│
├── corsMiddleware # OPTIONS 预检 → 204
├── authMiddleware # Bearer Token → 401
├── express.json() # 请求体解析
│
├── POST /chat/completions
│ └── chatCompletionsHandler
│ ├── 读取请求体
│ ├── 检测流式标记
│ ├── 应用模型别名 (preprocessRequest)
│ ├── 匹配后端前缀
│ ├── 移除前缀
│ ├── 应用角色重写 (preprocessRequest)
│ ├── 移除不支持参数 (preprocessRequest)
│ └── 代理转发
│
├── GET /health
│ └── healthCheckHandler → {name, version, description}
│
└── * (其他路径)
└── defaultProxyHandler → 默认后端代理
function authMiddleware(config: Config): RequestHandler {
return (req: Request, res: Response, next: NextFunction) => {
// OPTIONS 请求跳过认证
if (req.method === 'OPTIONS') {
return next();
}
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({
error: { message: 'Unauthorized', type: 'authentication_error', code: 'invalid_api_key' }
});
}
const token = authHeader.slice(7);
if (token !== config.llmrouterApiKey) {
return res.status(401).json({
error: { message: 'Unauthorized', type: 'authentication_error', code: 'invalid_api_key' }
});
}
next();
};
}
function matchBackend(model: string, proxyMap: Map<string, RequestHandler>, config: Config):
{ proxy: RequestHandler; backend: BackendConfig; targetModel: string } | null {
// 1. 遍历后端配置,匹配前缀
for (const backend of config.backends) {
if (model.startsWith(backend.prefix)) {
const targetModel = model.slice(backend.prefix.length);
const proxy = proxyMap.get(backend.prefix);
if (proxy) {
return { proxy, backend, targetModel };
}
}
}
// 2. 无前缀匹配,使用默认后端
const defaultBackend = config.backends.find(b => b.default);
if (defaultBackend) {
const proxy = proxyMap.get(defaultBackend.prefix);
if (proxy) {
return { proxy, backend: defaultBackend, targetModel: model };
}
}
// 3. 无匹配
return null;
}