diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index 6af27337b..fb7ca32db 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -1024,6 +1024,12 @@ CONFIG_METADATA_2 = { "hint": "启用后所有函数工具将全部失效", "obvious_hint": True, }, + "gm_url_context": { + "description": "启用URL上下文功能", + "type": "bool", + "hint": "启用后所有函数工具将全部失效", + "obvious_hint": True, + }, "gm_safety_settings": { "description": "安全过滤器", "type": "object", diff --git a/astrbot/core/provider/sources/gemini_source.py b/astrbot/core/provider/sources/gemini_source.py index edd591d87..12736532a 100644 --- a/astrbot/core/provider/sources/gemini_source.py +++ b/astrbot/core/provider/sources/gemini_source.py @@ -141,30 +141,58 @@ class ProviderGoogleGenAI(Provider): logger.warning("流式输出不支持图片模态,已自动降级为文本模态") modalities = ["Text"] - tool_list = None + tool_list = [] model_name = self.get_model() native_coderunner = self.provider_config.get("gm_native_coderunner", False) native_search = self.provider_config.get("gm_native_search", False) + url_context = self.provider_config.get("gm_url_context", False) if "gemini-2.5" in model_name: if native_coderunner: - tool_list = [types.Tool(code_execution=types.ToolCodeExecution())] - if native_search: - if tool_list: + tool_list.append(types.Tool(code_execution=types.ToolCodeExecution())) + if native_search: + logger.warning("代码执行工具与搜索工具互斥,已忽略搜索工具") + if url_context: + logger.warning( + "代码执行工具与URL上下文工具互斥,已忽略URL上下文工具" + ) + else: + if native_search: tool_list.append(types.Tool(google_search=types.GoogleSearch())) - else: - tool_list = [types.Tool(google_search=types.GoogleSearch())] + + if url_context: + if hasattr(types, "UrlContext"): + tool_list.append(types.Tool(web_context=types.UrlContext())) + else: + logger.warning( + "当前 SDK 版本不支持 URL 上下文工具,已忽略该设置,请升级 google-genai 包" + ) + elif "gemini-2.0-lite" in model_name: - if native_coderunner or native_search: - logger.warning("gemini-2.0-lite 不支持代码执行和搜索工具,将忽略这些设置") - tool_list = None + if native_coderunner or native_search or url_context: + logger.warning( + "gemini-2.0-lite 不支持代码执行、搜索工具和URL上下文,将忽略这些设置" + ) + tool_list = None + else: if native_coderunner: - tool_list = [types.Tool(code_execution=types.ToolCodeExecution())] + tool_list.append(types.Tool(code_execution=types.ToolCodeExecution())) if native_search: - logger.warning("已启用代码执行工具,搜索工具将被忽略") + logger.warning("代码执行工具与搜索工具互斥,已忽略搜索工具") elif native_search: - tool_list = [types.Tool(google_search=types.GoogleSearch())] + tool_list.append(types.Tool(google_search=types.GoogleSearch())) + + if url_context and not native_coderunner: + if hasattr(types, "UrlContext"): + tool_list.append(types.Tool(web_context=types.UrlContext())) + else: + logger.warning( + "当前 SDK 版本不支持 URL 上下文工具,已忽略该设置,请升级 google-genai 包" + ) + + if not tool_list: + tool_list = None if tools and tool_list: logger.warning("已启用原生工具,函数工具将被忽略") @@ -172,6 +200,7 @@ class ProviderGoogleGenAI(Provider): tool_list = [ types.Tool(function_declarations=func_desc["function_declarations"]) ] + return types.GenerateContentConfig( system_instruction=system_instruction, temperature=temperature,