|
@@ -66,7 +66,9 @@ class ToolRegistry:
|
|
|
requires_confirmation: bool = False,
|
|
requires_confirmation: bool = False,
|
|
|
editable_params: Optional[List[str]] = None,
|
|
editable_params: Optional[List[str]] = None,
|
|
|
display: Optional[Dict[str, Dict[str, Any]]] = None,
|
|
display: Optional[Dict[str, Dict[str, Any]]] = None,
|
|
|
- url_patterns: Optional[List[str]] = None
|
|
|
|
|
|
|
+ url_patterns: Optional[List[str]] = None,
|
|
|
|
|
+ hidden_params: Optional[List[str]] = None,
|
|
|
|
|
+ inject_params: Optional[Dict[str, Any]] = None
|
|
|
):
|
|
):
|
|
|
"""
|
|
"""
|
|
|
注册工具
|
|
注册工具
|
|
@@ -78,6 +80,8 @@ class ToolRegistry:
|
|
|
editable_params: 允许用户编辑的参数列表
|
|
editable_params: 允许用户编辑的参数列表
|
|
|
display: i18n 展示信息 {"zh": {"name": "xx", "params": {...}}, "en": {...}}
|
|
display: i18n 展示信息 {"zh": {"name": "xx", "params": {...}}, "en": {...}}
|
|
|
url_patterns: URL 模式列表(如 ["*.google.com"],None = 无限制)
|
|
url_patterns: URL 模式列表(如 ["*.google.com"],None = 无限制)
|
|
|
|
|
+ hidden_params: 隐藏参数列表(不生成 schema,LLM 看不到)
|
|
|
|
|
+ inject_params: 注入参数规则 {param_name: injector_func}
|
|
|
"""
|
|
"""
|
|
|
func_name = func.__name__
|
|
func_name = func.__name__
|
|
|
|
|
|
|
@@ -85,7 +89,7 @@ class ToolRegistry:
|
|
|
if schema is None:
|
|
if schema is None:
|
|
|
try:
|
|
try:
|
|
|
from agent.tools.schema import SchemaGenerator
|
|
from agent.tools.schema import SchemaGenerator
|
|
|
- schema = SchemaGenerator.generate(func)
|
|
|
|
|
|
|
+ schema = SchemaGenerator.generate(func, hidden_params=hidden_params or [])
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.error(f"Failed to generate schema for {func_name}: {e}")
|
|
logger.error(f"Failed to generate schema for {func_name}: {e}")
|
|
|
raise
|
|
raise
|
|
@@ -94,6 +98,8 @@ class ToolRegistry:
|
|
|
"func": func,
|
|
"func": func,
|
|
|
"schema": schema,
|
|
"schema": schema,
|
|
|
"url_patterns": url_patterns,
|
|
"url_patterns": url_patterns,
|
|
|
|
|
+ "hidden_params": hidden_params or [],
|
|
|
|
|
+ "inject_params": inject_params or {},
|
|
|
"ui_metadata": {
|
|
"ui_metadata": {
|
|
|
"requires_confirmation": requires_confirmation,
|
|
"requires_confirmation": requires_confirmation,
|
|
|
"editable_params": editable_params or [],
|
|
"editable_params": editable_params or [],
|
|
@@ -204,6 +210,7 @@ class ToolRegistry:
|
|
|
|
|
|
|
|
try:
|
|
try:
|
|
|
func = self._tools[name]["func"]
|
|
func = self._tools[name]["func"]
|
|
|
|
|
+ tool_info = self._tools[name]
|
|
|
|
|
|
|
|
# 处理敏感数据占位符
|
|
# 处理敏感数据占位符
|
|
|
if sensitive_data:
|
|
if sensitive_data:
|
|
@@ -215,14 +222,34 @@ class ToolRegistry:
|
|
|
kwargs = {**arguments}
|
|
kwargs = {**arguments}
|
|
|
sig = inspect.signature(func)
|
|
sig = inspect.signature(func)
|
|
|
|
|
|
|
|
- # 注入 uid(如果函数接受)
|
|
|
|
|
- if "uid" in sig.parameters:
|
|
|
|
|
|
|
+ # 注入隐藏参数(hidden_params)
|
|
|
|
|
+ hidden_params = tool_info.get("hidden_params", [])
|
|
|
|
|
+ if "uid" in hidden_params and "uid" in sig.parameters:
|
|
|
kwargs["uid"] = uid
|
|
kwargs["uid"] = uid
|
|
|
-
|
|
|
|
|
- # 注入 context(如果函数接受)
|
|
|
|
|
- if "context" in sig.parameters:
|
|
|
|
|
|
|
+ if "context" in hidden_params and "context" in sig.parameters:
|
|
|
kwargs["context"] = context
|
|
kwargs["context"] = context
|
|
|
|
|
|
|
|
|
|
+ # 注入默认值(inject_params)
|
|
|
|
|
+ inject_params = tool_info.get("inject_params", {})
|
|
|
|
|
+ for param_name, injector in inject_params.items():
|
|
|
|
|
+ if param_name in sig.parameters:
|
|
|
|
|
+ # 如果 LLM 已提供值,不覆盖
|
|
|
|
|
+ if param_name not in kwargs or kwargs[param_name] is None:
|
|
|
|
|
+ if callable(injector):
|
|
|
|
|
+ # 检查 injector 的参数数量
|
|
|
|
|
+ injector_sig = inspect.signature(injector)
|
|
|
|
|
+ if len(injector_sig.parameters) == 1:
|
|
|
|
|
+ # lambda ctx: ...
|
|
|
|
|
+ kwargs[param_name] = injector(context)
|
|
|
|
|
+ elif len(injector_sig.parameters) == 2:
|
|
|
|
|
+ # lambda ctx, args: ...
|
|
|
|
|
+ kwargs[param_name] = injector(context, kwargs)
|
|
|
|
|
+ else:
|
|
|
|
|
+ kwargs[param_name] = injector()
|
|
|
|
|
+ else:
|
|
|
|
|
+ # 直接使用值
|
|
|
|
|
+ kwargs[param_name] = injector
|
|
|
|
|
+
|
|
|
# 执行函数
|
|
# 执行函数
|
|
|
if inspect.iscoroutinefunction(func):
|
|
if inspect.iscoroutinefunction(func):
|
|
|
result = await func(**kwargs)
|
|
result = await func(**kwargs)
|
|
@@ -415,7 +442,9 @@ def tool(
|
|
|
requires_confirmation: bool = False,
|
|
requires_confirmation: bool = False,
|
|
|
editable_params: Optional[List[str]] = None,
|
|
editable_params: Optional[List[str]] = None,
|
|
|
display: Optional[Dict[str, Dict[str, Any]]] = None,
|
|
display: Optional[Dict[str, Dict[str, Any]]] = None,
|
|
|
- url_patterns: Optional[List[str]] = None
|
|
|
|
|
|
|
+ url_patterns: Optional[List[str]] = None,
|
|
|
|
|
+ hidden_params: Optional[List[str]] = None,
|
|
|
|
|
+ inject_params: Optional[Dict[str, Any]] = None
|
|
|
):
|
|
):
|
|
|
"""
|
|
"""
|
|
|
工具装饰器 - 自动注册工具并生成 Schema
|
|
工具装饰器 - 自动注册工具并生成 Schema
|
|
@@ -427,9 +456,15 @@ def tool(
|
|
|
editable_params: 允许用户编辑的参数列表
|
|
editable_params: 允许用户编辑的参数列表
|
|
|
display: i18n 展示信息
|
|
display: i18n 展示信息
|
|
|
url_patterns: URL 模式列表(如 ["*.google.com"],None = 无限制)
|
|
url_patterns: URL 模式列表(如 ["*.google.com"],None = 无限制)
|
|
|
|
|
+ hidden_params: 隐藏参数列表(不生成 schema,LLM 看不到)
|
|
|
|
|
+ inject_params: 注入参数规则 {param_name: injector_func}
|
|
|
|
|
|
|
|
Example:
|
|
Example:
|
|
|
@tool(
|
|
@tool(
|
|
|
|
|
+ hidden_params=["context", "uid"],
|
|
|
|
|
+ inject_params={
|
|
|
|
|
+ "owner": lambda ctx: ctx.config.knowledge.get_owner(),
|
|
|
|
|
+ },
|
|
|
editable_params=["query"],
|
|
editable_params=["query"],
|
|
|
url_patterns=["*.google.com"],
|
|
url_patterns=["*.google.com"],
|
|
|
display={
|
|
display={
|
|
@@ -437,7 +472,13 @@ def tool(
|
|
|
"en": {"name": "Search Notes", "params": {"query": "Query"}}
|
|
"en": {"name": "Search Notes", "params": {"query": "Query"}}
|
|
|
}
|
|
}
|
|
|
)
|
|
)
|
|
|
- async def search_blocks(query: str, limit: int = 10, uid: str = "") -> str:
|
|
|
|
|
|
|
+ async def search_blocks(
|
|
|
|
|
+ query: str,
|
|
|
|
|
+ limit: int = 10,
|
|
|
|
|
+ owner: Optional[str] = None,
|
|
|
|
|
+ context: Optional[ToolContext] = None,
|
|
|
|
|
+ uid: str = ""
|
|
|
|
|
+ ) -> str:
|
|
|
'''搜索用户的笔记块'''
|
|
'''搜索用户的笔记块'''
|
|
|
...
|
|
...
|
|
|
"""
|
|
"""
|
|
@@ -448,7 +489,9 @@ def tool(
|
|
|
requires_confirmation=requires_confirmation,
|
|
requires_confirmation=requires_confirmation,
|
|
|
editable_params=editable_params,
|
|
editable_params=editable_params,
|
|
|
display=display,
|
|
display=display,
|
|
|
- url_patterns=url_patterns
|
|
|
|
|
|
|
+ url_patterns=url_patterns,
|
|
|
|
|
+ hidden_params=hidden_params,
|
|
|
|
|
+ inject_params=inject_params
|
|
|
)
|
|
)
|
|
|
return func
|
|
return func
|
|
|
|
|
|