Organize tool scheme
This commit is contained in:
@@ -15,17 +15,6 @@ router = APIRouter(prefix="/tools", tags=["Tools & Autotest"])
|
||||
|
||||
# ============ Available Tools ============
|
||||
TOOL_REGISTRY = {
|
||||
"search": {
|
||||
"name": "网络搜索",
|
||||
"description": "搜索互联网获取最新信息",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": {"type": "string", "description": "搜索关键词"}
|
||||
},
|
||||
"required": ["query"]
|
||||
}
|
||||
},
|
||||
"calculator": {
|
||||
"name": "计算器",
|
||||
"description": "执行数学计算",
|
||||
@@ -37,50 +26,6 @@ TOOL_REGISTRY = {
|
||||
"required": ["expression"]
|
||||
}
|
||||
},
|
||||
"weather": {
|
||||
"name": "天气查询",
|
||||
"description": "查询指定城市的天气",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {"type": "string", "description": "城市名称"}
|
||||
},
|
||||
"required": ["city"]
|
||||
}
|
||||
},
|
||||
"translate": {
|
||||
"name": "翻译",
|
||||
"description": "翻译文本到指定语言",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"text": {"type": "string", "description": "要翻译的文本"},
|
||||
"target_lang": {"type": "string", "description": "目标语言,如: en, ja, ko"}
|
||||
},
|
||||
"required": ["text", "target_lang"]
|
||||
}
|
||||
},
|
||||
"knowledge": {
|
||||
"name": "知识库查询",
|
||||
"description": "从知识库中检索相关信息",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": {"type": "string", "description": "查询内容"},
|
||||
"kb_id": {"type": "string", "description": "知识库ID"}
|
||||
},
|
||||
"required": ["query"]
|
||||
}
|
||||
},
|
||||
"current_time": {
|
||||
"name": "当前时间",
|
||||
"description": "获取当前本地时间",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": []
|
||||
}
|
||||
},
|
||||
"code_interpreter": {
|
||||
"name": "代码执行",
|
||||
"description": "安全地执行Python代码",
|
||||
@@ -92,9 +37,27 @@ TOOL_REGISTRY = {
|
||||
"required": ["code"]
|
||||
}
|
||||
},
|
||||
"take_phone": {
|
||||
"name": "接听电话",
|
||||
"description": "执行接听电话命令",
|
||||
"current_time": {
|
||||
"name": "当前时间",
|
||||
"description": "获取当前本地时间",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": []
|
||||
}
|
||||
},
|
||||
"turn_on_camera": {
|
||||
"name": "打开摄像头",
|
||||
"description": "执行打开摄像头命令",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": []
|
||||
}
|
||||
},
|
||||
"turn_off_camera": {
|
||||
"name": "关闭摄像头",
|
||||
"description": "执行关闭摄像头命令",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
@@ -126,68 +89,48 @@ TOOL_REGISTRY = {
|
||||
}
|
||||
|
||||
TOOL_CATEGORY_MAP = {
|
||||
"search": "query",
|
||||
"weather": "query",
|
||||
"translate": "query",
|
||||
"knowledge": "query",
|
||||
"calculator": "query",
|
||||
"current_time": "query",
|
||||
"code_interpreter": "query",
|
||||
"take_phone": "system",
|
||||
"turn_on_camera": "system",
|
||||
"turn_off_camera": "system",
|
||||
"increase_volume": "system",
|
||||
"decrease_volume": "system",
|
||||
}
|
||||
|
||||
TOOL_ICON_MAP = {
|
||||
"search": "Globe",
|
||||
"weather": "CloudSun",
|
||||
"translate": "Globe",
|
||||
"knowledge": "Box",
|
||||
"current_time": "Calendar",
|
||||
"calculator": "Terminal",
|
||||
"current_time": "Calendar",
|
||||
"code_interpreter": "Terminal",
|
||||
"take_phone": "Phone",
|
||||
"turn_on_camera": "Camera",
|
||||
"turn_off_camera": "CameraOff",
|
||||
"increase_volume": "Volume2",
|
||||
"decrease_volume": "Volume2",
|
||||
}
|
||||
|
||||
def _sync_default_tools(db: Session) -> None:
|
||||
"""Ensure built-in tools exist and keep system tool metadata aligned."""
|
||||
changed = False
|
||||
def _seed_default_tools_if_empty(db: Session) -> None:
|
||||
"""Seed built-in tools only when tool_resources is empty."""
|
||||
if db.query(ToolResource).count() > 0:
|
||||
return
|
||||
for tool_id, payload in TOOL_REGISTRY.items():
|
||||
row = db.query(ToolResource).filter(ToolResource.id == tool_id).first()
|
||||
category = TOOL_CATEGORY_MAP.get(tool_id, "system")
|
||||
icon = TOOL_ICON_MAP.get(tool_id, "Wrench")
|
||||
if not row:
|
||||
db.add(ToolResource(
|
||||
id=tool_id,
|
||||
user_id=1,
|
||||
name=payload.get("name", tool_id),
|
||||
description=payload.get("description", ""),
|
||||
category=category,
|
||||
icon=icon,
|
||||
enabled=True,
|
||||
is_system=True,
|
||||
))
|
||||
changed = True
|
||||
continue
|
||||
if row.is_system:
|
||||
new_name = payload.get("name", row.name)
|
||||
new_description = payload.get("description", row.description)
|
||||
if row.name != new_name:
|
||||
row.name = new_name
|
||||
changed = True
|
||||
if row.description != new_description:
|
||||
row.description = new_description
|
||||
changed = True
|
||||
if row.category != category:
|
||||
row.category = category
|
||||
changed = True
|
||||
if row.icon != icon:
|
||||
row.icon = icon
|
||||
changed = True
|
||||
if changed:
|
||||
db.commit()
|
||||
db.add(ToolResource(
|
||||
id=tool_id,
|
||||
user_id=1,
|
||||
name=payload.get("name", tool_id),
|
||||
description=payload.get("description", ""),
|
||||
category=TOOL_CATEGORY_MAP.get(tool_id, "system"),
|
||||
icon=TOOL_ICON_MAP.get(tool_id, "Wrench"),
|
||||
enabled=True,
|
||||
is_system=True,
|
||||
))
|
||||
db.commit()
|
||||
|
||||
|
||||
def recreate_tool_resources(db: Session) -> None:
|
||||
"""Recreate tool resources table content with current built-in defaults."""
|
||||
db.query(ToolResource).delete()
|
||||
db.commit()
|
||||
_seed_default_tools_if_empty(db)
|
||||
|
||||
|
||||
@router.get("/list")
|
||||
@@ -215,7 +158,7 @@ def list_tool_resources(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""获取工具资源列表。system/query 仅表示工具执行类型,不代表权限。"""
|
||||
_sync_default_tools(db)
|
||||
_seed_default_tools_if_empty(db)
|
||||
query = db.query(ToolResource)
|
||||
if not include_system:
|
||||
query = query.filter(ToolResource.is_system == False)
|
||||
@@ -231,7 +174,7 @@ def list_tool_resources(
|
||||
@router.get("/resources/{id}", response_model=ToolResourceOut)
|
||||
def get_tool_resource(id: str, db: Session = Depends(get_db)):
|
||||
"""获取单个工具资源详情。"""
|
||||
_sync_default_tools(db)
|
||||
_seed_default_tools_if_empty(db)
|
||||
item = db.query(ToolResource).filter(ToolResource.id == id).first()
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Tool resource not found")
|
||||
@@ -241,7 +184,7 @@ def get_tool_resource(id: str, db: Session = Depends(get_db)):
|
||||
@router.post("/resources", response_model=ToolResourceOut)
|
||||
def create_tool_resource(data: ToolResourceCreate, db: Session = Depends(get_db)):
|
||||
"""创建自定义工具资源。"""
|
||||
_sync_default_tools(db)
|
||||
_seed_default_tools_if_empty(db)
|
||||
candidate_id = (data.id or "").strip()
|
||||
if candidate_id and db.query(ToolResource).filter(ToolResource.id == candidate_id).first():
|
||||
raise HTTPException(status_code=400, detail="Tool ID already exists")
|
||||
@@ -265,7 +208,7 @@ def create_tool_resource(data: ToolResourceCreate, db: Session = Depends(get_db)
|
||||
@router.put("/resources/{id}", response_model=ToolResourceOut)
|
||||
def update_tool_resource(id: str, data: ToolResourceUpdate, db: Session = Depends(get_db)):
|
||||
"""更新工具资源。"""
|
||||
_sync_default_tools(db)
|
||||
_seed_default_tools_if_empty(db)
|
||||
item = db.query(ToolResource).filter(ToolResource.id == id).first()
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Tool resource not found")
|
||||
@@ -283,7 +226,7 @@ def update_tool_resource(id: str, data: ToolResourceUpdate, db: Session = Depend
|
||||
@router.delete("/resources/{id}")
|
||||
def delete_tool_resource(id: str, db: Session = Depends(get_db)):
|
||||
"""删除工具资源。"""
|
||||
_sync_default_tools(db)
|
||||
_seed_default_tools_if_empty(db)
|
||||
item = db.query(ToolResource).filter(ToolResource.id == id).first()
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Tool resource not found")
|
||||
|
||||
Reference in New Issue
Block a user