mirror of
https://github.com/foxhui/WebAI2API.git
synced 2026-06-16 21:03:59 +08:00
feat: 聚合模式增加闲时监控,支持获取指定域名的Cookie
This commit is contained in:
@@ -58,8 +58,9 @@ export function createRouter(context) {
|
||||
* 处理 GET /v1/cookies
|
||||
* @param {import('http').ServerResponse} res - HTTP 响应
|
||||
* @param {string} requestId - 请求 ID
|
||||
* @param {string} [domain] - 可选,指定获取某个域名的 Cookies
|
||||
*/
|
||||
async function handleCookies(res, requestId) {
|
||||
async function handleCookies(res, requestId, domain) {
|
||||
const browserContext = queueManager.getBrowserContext();
|
||||
|
||||
if (!browserContext?.page) {
|
||||
@@ -69,7 +70,16 @@ export function createRouter(context) {
|
||||
|
||||
try {
|
||||
const context = browserContext.page.context();
|
||||
const cookies = await context.cookies();
|
||||
let cookies;
|
||||
|
||||
if (domain) {
|
||||
// 指定域名时,只获取该域名的 Cookies
|
||||
cookies = await context.cookies(domain.startsWith('http') ? domain : `https://${domain}`);
|
||||
} else {
|
||||
// 默认获取所有 Cookies
|
||||
cookies = await context.cookies();
|
||||
}
|
||||
|
||||
sendJson(res, 200, { cookies });
|
||||
} catch (err) {
|
||||
logger.error('服务器', '获取 Cookies 失败', { id: requestId, error: err.message });
|
||||
@@ -179,11 +189,15 @@ export function createRouter(context) {
|
||||
}
|
||||
|
||||
// 路由分发
|
||||
if (req.method === 'GET' && req.url === '/v1/models') {
|
||||
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
|
||||
const pathname = parsedUrl.pathname;
|
||||
|
||||
if (req.method === 'GET' && pathname === '/v1/models') {
|
||||
handleModels(res);
|
||||
} else if (req.method === 'GET' && req.url === '/v1/cookies') {
|
||||
await handleCookies(res, requestId);
|
||||
} else if (req.method === 'POST' && req.url?.startsWith('/v1/chat/completions')) {
|
||||
} else if (req.method === 'GET' && pathname === '/v1/cookies') {
|
||||
const domain = parsedUrl.searchParams.get('domain');
|
||||
await handleCookies(res, requestId, domain);
|
||||
} else if (req.method === 'POST' && pathname.startsWith('/v1/chat/completions')) {
|
||||
await handleChatCompletions(req, res, requestId);
|
||||
} else {
|
||||
res.writeHead(404);
|
||||
|
||||
+12
-2
@@ -51,7 +51,7 @@ import {
|
||||
*/
|
||||
export function createQueueManager(queueConfig, callbacks) {
|
||||
const { maxConcurrent, maxQueueSize, keepaliveMode } = queueConfig;
|
||||
const { initBrowser, generateImage, config } = callbacks;
|
||||
const { initBrowser, generateImage, config, navigateToMonitor } = callbacks;
|
||||
|
||||
/** @type {TaskContext[]} */
|
||||
const queue = [];
|
||||
@@ -149,7 +149,13 @@ export function createQueueManager(queueConfig, callbacks) {
|
||||
*/
|
||||
async function processQueue() {
|
||||
// 如果正在处理的任务已满,或队列为空,则停止
|
||||
if (processingCount >= maxConcurrent || queue.length === 0) return;
|
||||
if (processingCount >= maxConcurrent || queue.length === 0) {
|
||||
// 队列空闲时,触发监控跳转
|
||||
if (processingCount === 0 && queue.length === 0 && navigateToMonitor) {
|
||||
navigateToMonitor().catch(() => { });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 取出下一个任务
|
||||
const task = queue.shift();
|
||||
@@ -201,6 +207,10 @@ export function createQueueManager(queueConfig, callbacks) {
|
||||
*/
|
||||
async function initializeBrowser() {
|
||||
browserContext = await initBrowser(config);
|
||||
// 初始化完成后,触发首次监控跳转
|
||||
if (navigateToMonitor) {
|
||||
navigateToMonitor().catch(() => { });
|
||||
}
|
||||
return browserContext;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user