diff --git a/unified-server.js b/unified-server.js index 5b691f3..80a8dcd 100644 --- a/unified-server.js +++ b/unified-server.js @@ -1344,6 +1344,54 @@ class RequestHandler { } } + // 强制联网搜索和URL上下文 (Native Google Format) + if ( + (this.serverSystem.forceWebSearch || this.serverSystem.forceUrlContext) && + req.method === "POST" && + bodyObj && + bodyObj.contents + ) { + if (!bodyObj.tools) { + bodyObj.tools = []; + } + + const toolsToAdd = []; + + // 处理 Google Search + if (this.serverSystem.forceWebSearch) { + const hasSearch = bodyObj.tools.some((t) => t.googleSearch); + if (!hasSearch) { + bodyObj.tools.push({googleSearch: {}}); + toolsToAdd.push("googleSearch"); + } else { + this.logger.info( + `[Proxy] ✅ (Google原生格式) 检测到客户端自带联网搜索,跳过强制注入。` + ); + } + } + + // 处理 URL Context + if (this.serverSystem.forceUrlContext) { + const hasUrlContext = bodyObj.tools.some((t) => t.urlContext); + if (!hasUrlContext) { + bodyObj.tools.push({urlContext: {}}); + toolsToAdd.push("urlContext"); + } else { + this.logger.info( + `[Proxy] ✅ (Google原生格式) 检测到客户端自带网址上下文,跳过强制注入。` + ); + } + } + + if (toolsToAdd.length > 0) { + this.logger.info( + `[Proxy] ⚠️ (Google原生格式) 强制功能已启用,正在注入工具: [${toolsToAdd.join( + ", " + )}]` + ); + } + } + let requestBody = ""; if (bodyObj) { requestBody = JSON.stringify(bodyObj); @@ -1912,6 +1960,43 @@ class RequestHandler { { category: "HARM_CATEGORY_DANGEROUS_CONTENT", threshold: "BLOCK_NONE" }, ]; + // 强制联网搜索和URL上下文 + if ( + (this.serverSystem.forceWebSearch || this.serverSystem.forceUrlContext) + ) { + if (!googleRequest.tools) { + googleRequest.tools = []; + } + + const toolsToAdd = []; + + // 处理 Google Search + if (this.serverSystem.forceWebSearch) { + const hasSearch = googleRequest.tools.some((t) => t.googleSearch); + if (!hasSearch) { + googleRequest.tools.push({googleSearch: {}}); + toolsToAdd.push("googleSearch"); + } + } + + // 处理 URL Context + if (this.serverSystem.forceUrlContext) { + const hasUrlContext = googleRequest.tools.some((t) => t.urlContext); + if (!hasUrlContext) { + googleRequest.tools.push({urlContext: {}}); + toolsToAdd.push("urlContext"); + } + } + + if (toolsToAdd.length > 0) { + this.logger.info( + `[Adapter] ⚠️ 强制功能已启用,正在注入工具: [${toolsToAdd.join( + ", " + )}]` + ); + } + } + this.logger.info("[Adapter] 翻译完成。"); return googleRequest; } @@ -2021,7 +2106,9 @@ class ProxyServerSystem extends EventEmitter { this._loadConfiguration(); // 这个函数会执行下面的_loadConfiguration this.streamingMode = this.config.streamingMode; - this.forceThinking = false; + this.forceThinking = process.env.FORCE_THINKING === "true"; + this.forceWebSearch = process.env.FORCE_WEB_SEARCH === "true"; + this.forceUrlContext = process.env.FORCE_URL_CONTEXT === "true"; this.authSource = new AuthSource(this.logger); this.browserManager = new BrowserManager( @@ -2485,6 +2572,12 @@ class ProxyServerSystem extends EventEmitter { 强制推理: ${ this.forceThinking ? "✅ 已启用" : "❌ 已关闭" } +强制联网: ${ + this.forceWebSearch ? "✅ 已启用" : "❌ 已关闭" + } +强制网址上下文: ${ + this.forceUrlContext ? "✅ 已启用" : "❌ 已关闭" + } 立即切换 (状态码): ${ config.immediateSwitchStatusCodes.length > 0 ? `[${config.immediateSwitchStatusCodes.join(", ")}]` @@ -2515,6 +2608,8 @@ class ProxyServerSystem extends EventEmitter { + +