Merge #102 into develop from feature/20251219_xjn
feat(workflow_node): question classifier node optimization * feature/20251219_xjn: (9 commits) feat(tool system): The specific method for obtaining the tool and the parameters to be passed feat(tool system): add mcp testing services Merge branch 'refs/heads/develop' into feature/20251219_xjn feat(tool system): add all methods for obtaining the tool feat(tool system): add workflow tool nodes Merge branch 'refs/heads/develop' into feature/20251219_xjn feat(home page): add statistical interface Merge branch 'refs/heads/develop' into feature/20251219_xjn feat(workflow_node): question classifier node optimization Signed-off-by: 谢俊男 <accounts_6853d0ea6f8174722fb0c8f1@mail.teambition.com> Reviewed-by: zhuwenhui5566@163.com <zhuwenhui5566@163.com> Merged-by: zhuwenhui5566@163.com <zhuwenhui5566@163.com> CR-link: https://codeup.aliyun.com/redbearai/python/redbear-mem-open/change/102
This commit is contained in:
67
api/app/services/home_page_service.py
Normal file
67
api/app/services/home_page_service.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from datetime import datetime, timedelta
|
||||
from sqlalchemy.orm import Session
|
||||
from uuid import UUID
|
||||
|
||||
from app.repositories.home_page_repository import HomePageRepository
|
||||
from app.schemas.home_page_schema import HomeStatistics, WorkspaceInfo
|
||||
|
||||
class HomePageService:
|
||||
|
||||
@staticmethod
|
||||
def get_home_statistics(db: Session, tenant_id: UUID) -> HomeStatistics:
|
||||
"""获取首页统计数据"""
|
||||
# 计算时间范围
|
||||
now = datetime.now()
|
||||
month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
||||
week_start = now - timedelta(days=now.weekday())
|
||||
week_start = week_start.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
|
||||
# 获取各项统计数据
|
||||
total_models, new_models_this_month = HomePageRepository.get_model_statistics(
|
||||
db, tenant_id, month_start
|
||||
)
|
||||
|
||||
active_workspaces, new_workspaces_this_month = HomePageRepository.get_workspace_statistics(
|
||||
db, tenant_id, month_start
|
||||
)
|
||||
|
||||
total_users, new_users_this_month = HomePageRepository.get_user_statistics(
|
||||
db, tenant_id, month_start
|
||||
)
|
||||
|
||||
running_apps, new_apps_this_week = HomePageRepository.get_app_statistics(
|
||||
db, tenant_id, week_start
|
||||
)
|
||||
|
||||
return HomeStatistics(
|
||||
total_models=total_models,
|
||||
new_models_this_month=new_models_this_month,
|
||||
active_workspaces=active_workspaces,
|
||||
new_workspaces_this_month=new_workspaces_this_month,
|
||||
total_users=total_users,
|
||||
new_users_this_month=new_users_this_month,
|
||||
running_apps=running_apps,
|
||||
new_apps_this_week=new_apps_this_week
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_workspace_list(db: Session, tenant_id: UUID) -> list[WorkspaceInfo]:
|
||||
"""获取工作空间列表(优化版本)"""
|
||||
workspaces, app_count_dict, user_count_dict= HomePageRepository.get_workspaces_with_counts(
|
||||
db, tenant_id
|
||||
)
|
||||
|
||||
workspace_list = []
|
||||
for workspace in workspaces:
|
||||
workspace_info = WorkspaceInfo(
|
||||
id=str(workspace.id),
|
||||
name=workspace.name,
|
||||
icon=workspace.icon,
|
||||
description=workspace.description,
|
||||
app_count=app_count_dict.get(workspace.id, 0),
|
||||
user_count=user_count_dict.get(workspace.id, 0),
|
||||
created_at=workspace.created_at
|
||||
)
|
||||
workspace_list.append(workspace_info)
|
||||
|
||||
return workspace_list
|
||||
@@ -297,6 +297,165 @@ class ToolService:
|
||||
self.db.commit()
|
||||
logger.info(f"租户 {tenant_id} 内置工具初始化完成")
|
||||
|
||||
async def get_tool_methods(self, tool_id: str, tenant_id: uuid.UUID) -> Optional[List[Dict[str, Any]]]:
|
||||
"""获取工具的所有方法
|
||||
|
||||
Args:
|
||||
tool_id: 工具ID
|
||||
tenant_id: 租户ID
|
||||
|
||||
Returns:
|
||||
方法列表或None
|
||||
"""
|
||||
config = self._get_tool_config(tool_id, tenant_id)
|
||||
if not config:
|
||||
return None
|
||||
|
||||
try:
|
||||
if config.tool_type == ToolType.BUILTIN.value:
|
||||
return await self._get_builtin_tool_methods(config)
|
||||
elif config.tool_type == ToolType.CUSTOM.value:
|
||||
return await self._get_custom_tool_methods(config)
|
||||
elif config.tool_type == ToolType.MCP.value:
|
||||
return await self._get_mcp_tool_methods(config)
|
||||
else:
|
||||
return []
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"获取工具方法失败: {tool_id}, {e}")
|
||||
return []
|
||||
|
||||
async def _get_builtin_tool_methods(self, config: ToolConfig) -> List[Dict[str, Any]]:
|
||||
"""获取内置工具的方法"""
|
||||
builtin_config = self.builtin_repo.find_by_tool_id(self.db, config.id)
|
||||
if not builtin_config or builtin_config.tool_class not in BUILTIN_TOOLS:
|
||||
return []
|
||||
|
||||
# 获取工具实例
|
||||
tool_instance = self._get_tool_instance(str(config.id), config.tenant_id)
|
||||
if not tool_instance:
|
||||
return []
|
||||
|
||||
# 检查是否有operation参数
|
||||
operation_param = None
|
||||
for param in tool_instance.parameters:
|
||||
if param.name == "operation" and param.enum:
|
||||
operation_param = param
|
||||
break
|
||||
|
||||
if operation_param:
|
||||
# 有多个操作
|
||||
methods = []
|
||||
for operation in operation_param.enum:
|
||||
methods.append({
|
||||
"method_id": f"{config.name}_{operation}",
|
||||
"name": operation,
|
||||
"description": f"{config.description} - {operation}",
|
||||
"parameters": [p for p in tool_instance.parameters if p.name != "operation"]
|
||||
})
|
||||
return methods
|
||||
else:
|
||||
# 只有一个方法
|
||||
return [{
|
||||
"method_id": config.name,
|
||||
"name": config.name,
|
||||
"description": config.description,
|
||||
"parameters": [p for p in tool_instance.parameters if p.name != "operation"]
|
||||
}]
|
||||
|
||||
async def _get_custom_tool_methods(self, config: ToolConfig) -> List[Dict[str, Any]]:
|
||||
"""获取自定义工具的方法"""
|
||||
custom_config = self.custom_repo.find_by_tool_id(self.db, config.id)
|
||||
if not custom_config:
|
||||
return []
|
||||
|
||||
try:
|
||||
from app.core.tools.custom.schema_parser import OpenAPISchemaParser
|
||||
parser = OpenAPISchemaParser()
|
||||
|
||||
# 解析schema
|
||||
if custom_config.schema_content:
|
||||
success, schema, error = parser.parse_from_content(custom_config.schema_content, "application/json")
|
||||
elif custom_config.schema_url:
|
||||
success, schema, error = await parser.parse_from_url(custom_config.schema_url)
|
||||
else:
|
||||
return []
|
||||
|
||||
if not success:
|
||||
return []
|
||||
|
||||
# 提取操作
|
||||
tool_info = parser.extract_tool_info(schema)
|
||||
operations = tool_info.get("operations", {})
|
||||
|
||||
methods = []
|
||||
for operation_id, operation in operations.items():
|
||||
# 生成参数列表
|
||||
parameters = []
|
||||
|
||||
# 路径和查询参数
|
||||
for param_name, param_info in operation.get("parameters", {}).items():
|
||||
parameters.append({
|
||||
"name": param_name,
|
||||
"type": param_info.get("type", "string"),
|
||||
"description": param_info.get("description", ""),
|
||||
"required": param_info.get("required", False),
|
||||
"enum": param_info.get("enum"),
|
||||
"default": param_info.get("default")
|
||||
})
|
||||
|
||||
# 请求体参数
|
||||
request_body = operation.get("request_body")
|
||||
if request_body:
|
||||
schema_props = request_body.get("schema", {}).get("properties", {})
|
||||
required_props = request_body.get("schema", {}).get("required", [])
|
||||
|
||||
for prop_name, prop_schema in schema_props.items():
|
||||
parameters.append({
|
||||
"name": prop_name,
|
||||
"type": prop_schema.get("type", "string"),
|
||||
"description": prop_schema.get("description", ""),
|
||||
"required": prop_name in required_props,
|
||||
"enum": prop_schema.get("enum"),
|
||||
"default": prop_schema.get("default")
|
||||
})
|
||||
|
||||
methods.append({
|
||||
"method_id": operation_id,
|
||||
"name": operation.get("summary", operation_id),
|
||||
"description": operation.get("description", ""),
|
||||
"method": operation.get("method", "GET"),
|
||||
"path": operation.get("path", "/"),
|
||||
"parameters": parameters
|
||||
})
|
||||
|
||||
return methods
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"解析自定义工具schema失败: {e}")
|
||||
return []
|
||||
|
||||
async def _get_mcp_tool_methods(self, config: ToolConfig) -> List[Dict[str, Any]]:
|
||||
"""获取MCP工具的方法"""
|
||||
mcp_config = self.mcp_repo.find_by_tool_id(self.db, config.id)
|
||||
if not mcp_config:
|
||||
return []
|
||||
|
||||
available_tools = mcp_config.available_tools or []
|
||||
if not available_tools:
|
||||
return []
|
||||
|
||||
methods = []
|
||||
for tool_name in available_tools:
|
||||
methods.append({
|
||||
"method_id": tool_name,
|
||||
"name": tool_name,
|
||||
"description": f"MCP工具: {tool_name}",
|
||||
"parameters": [] # MCP工具参数需要动态获取
|
||||
})
|
||||
|
||||
return methods
|
||||
|
||||
def get_tool_statistics(self, tenant_id: uuid.UUID) -> Dict[str, Any]:
|
||||
"""获取工具统计信息"""
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user