361 lines
14 KiB
Python
361 lines
14 KiB
Python
"""
|
||
MACP API 路由模块。
|
||
|
||
为各功能模块提供 RESTful API 接口定义。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any, Optional
|
||
|
||
from fastapi import APIRouter, HTTPException
|
||
|
||
from app.models import (
|
||
Document,
|
||
FileType,
|
||
KnowledgeChunk,
|
||
KnowledgeLevel,
|
||
McpTool,
|
||
MemoryEntry,
|
||
MemoryType,
|
||
Skill,
|
||
Template,
|
||
Workflow,
|
||
)
|
||
from app.services import (
|
||
DocumentService,
|
||
GjbValidatorService,
|
||
KnowledgeBaseService,
|
||
McpToolService,
|
||
MemoryService,
|
||
ModelService,
|
||
MultimodalService,
|
||
SandboxService,
|
||
SkillService,
|
||
TemplateService,
|
||
)
|
||
|
||
# =============================================================================
|
||
# 路由实例化
|
||
# =============================================================================
|
||
|
||
router = APIRouter(prefix="/api/v1")
|
||
|
||
# 服务实例化(单例)
|
||
template_service = TemplateService()
|
||
document_service = DocumentService()
|
||
validator_service = GjbValidatorService()
|
||
skill_service = SkillService()
|
||
memory_service = MemoryService()
|
||
kb_service = KnowledgeBaseService()
|
||
model_service = ModelService()
|
||
multimodal_service = MultimodalService()
|
||
sandbox_service = SandboxService()
|
||
mcp_tool_service = McpToolService()
|
||
|
||
|
||
# =============================================================================
|
||
# 模板管理接口(SRS-MACP_F-001)
|
||
# =============================================================================
|
||
|
||
@router.get("/templates", tags=["模板管理"], summary="获取模板列表")
|
||
async def list_templates():
|
||
"""获取所有文档模板列表"""
|
||
return {"code": 200, "data": template_service.list_templates()}
|
||
|
||
|
||
@router.post("/templates", tags=["模板管理"], summary="创建模板")
|
||
async def create_template(template: Template):
|
||
"""创建新的文档模板"""
|
||
result = template_service.create_template(template)
|
||
return {"code": 200, "data": result, "message": "模板创建成功"}
|
||
|
||
|
||
@router.get("/templates/{template_id}", tags=["模板管理"], summary="获取模板详情")
|
||
async def get_template(template_id: str):
|
||
"""根据ID获取模板详情"""
|
||
result = template_service.get_template(template_id)
|
||
if not result:
|
||
raise HTTPException(status_code=404, detail="模板不存在")
|
||
return {"code": 200, "data": result}
|
||
|
||
|
||
@router.delete("/templates/{template_id}", tags=["模板管理"], summary="删除模板")
|
||
async def delete_template(template_id: str):
|
||
"""删除指定模板"""
|
||
success = template_service.delete_template(template_id)
|
||
if not success:
|
||
raise HTTPException(status_code=404, detail="模板不存在")
|
||
return {"code": 200, "message": "模板已删除"}
|
||
|
||
|
||
# =============================================================================
|
||
# 文档管理接口
|
||
# =============================================================================
|
||
|
||
@router.get("/documents", tags=["文档管理"], summary="获取文档列表")
|
||
async def list_documents():
|
||
"""获取所有文档列表"""
|
||
return {"code": 200, "data": document_service.list_documents()}
|
||
|
||
|
||
@router.post("/documents", tags=["文档管理"], summary="创建文档")
|
||
async def create_document(document: Document):
|
||
"""创建新文档"""
|
||
result = document_service.create_document(document)
|
||
return {"code": 200, "data": result, "message": "文档创建成功"}
|
||
|
||
|
||
@router.get("/documents/{doc_id}", tags=["文档管理"], summary="获取文档详情")
|
||
async def get_document(doc_id: str):
|
||
"""根据ID获取文档详情"""
|
||
result = document_service.get_document(doc_id)
|
||
if not result:
|
||
raise HTTPException(status_code=404, detail="文档不存在")
|
||
return {"code": 200, "data": result}
|
||
|
||
|
||
# =============================================================================
|
||
# GJB合规验证接口(SRS-MACP_F-002)
|
||
# =============================================================================
|
||
|
||
@router.get("/rules", tags=["GJB合规验证"], summary="获取GJB规则列表")
|
||
async def list_rules():
|
||
"""获取所有GJB规则"""
|
||
return {"code": 200, "data": validator_service.list_rules()}
|
||
|
||
|
||
@router.post("/documents/{doc_id}/validate", tags=["GJB合规验证"], summary="文档GJB合规验证")
|
||
async def validate_document(doc_id: str):
|
||
"""对指定文档执行GJB合规性验证"""
|
||
document = document_service.get_document(doc_id)
|
||
if not document:
|
||
raise HTTPException(status_code=404, detail="文档不存在")
|
||
report = validator_service.validate_document(document)
|
||
return {"code": 200, "data": report, "message": "验证完成"}
|
||
|
||
|
||
# =============================================================================
|
||
# MCP工具管理接口(SRS-MACP_F-004)
|
||
# =============================================================================
|
||
|
||
@router.get("/mcp-tools", tags=["MCP工具管理"], summary="获取MCP工具列表")
|
||
async def list_mcp_tools():
|
||
"""获取所有MCP工具"""
|
||
return {"code": 200, "data": mcp_tool_service.list_tools()}
|
||
|
||
|
||
@router.post("/mcp-tools", tags=["MCP工具管理"], summary="注册MCP工具")
|
||
async def register_mcp_tool(tool: McpTool):
|
||
"""注册新的MCP工具"""
|
||
result = mcp_tool_service.register_tool(tool)
|
||
return {"code": 200, "data": result, "message": "MCP工具注册成功"}
|
||
|
||
|
||
@router.get("/mcp-tools/{tool_id}", tags=["MCP工具管理"], summary="获取MCP工具详情")
|
||
async def get_mcp_tool(tool_id: str):
|
||
"""获取MCP工具详情"""
|
||
result = mcp_tool_service.get_tool(tool_id)
|
||
if not result:
|
||
raise HTTPException(status_code=404, detail="MCP工具不存在")
|
||
return {"code": 200, "data": result}
|
||
|
||
|
||
@router.delete("/mcp-tools/{tool_id}", tags=["MCP工具管理"], summary="删除MCP工具")
|
||
async def delete_mcp_tool(tool_id: str):
|
||
"""删除MCP工具"""
|
||
success = mcp_tool_service.delete_tool(tool_id)
|
||
if not success:
|
||
raise HTTPException(status_code=404, detail="MCP工具不存在")
|
||
return {"code": 200, "message": "MCP工具已删除"}
|
||
|
||
|
||
@router.post("/mcp-tools/{tool_id}/debug", tags=["MCP工具管理"], summary="沙箱调试MCP工具")
|
||
async def debug_mcp_tool(tool_id: str, input_data: dict[str, Any]):
|
||
"""沙箱调试MCP工具"""
|
||
result = mcp_tool_service.debug_tool(tool_id, input_data)
|
||
return {"code": 200, "data": result}
|
||
|
||
|
||
# =============================================================================
|
||
# Skills流程编排接口(SRS-MACP_F-005)
|
||
# =============================================================================
|
||
|
||
@router.get("/skills", tags=["Skills编排"], summary="获取Skills列表")
|
||
async def list_skills():
|
||
"""获取所有Skill"""
|
||
return {"code": 200, "data": skill_service.list_skills()}
|
||
|
||
|
||
@router.post("/skills", tags=["Skills编排"], summary="创建Skill")
|
||
async def create_skill(skill: Skill):
|
||
"""创建新Skill"""
|
||
result = skill_service.create_skill(skill)
|
||
return {"code": 200, "data": result, "message": "Skill创建成功"}
|
||
|
||
|
||
@router.get("/skills/{skill_id}", tags=["Skills编排"], summary="获取Skill详情")
|
||
async def get_skill(skill_id: str):
|
||
"""获取Skill详情"""
|
||
result = skill_service.get_skill(skill_id)
|
||
if not result:
|
||
raise HTTPException(status_code=404, detail="Skill不存在")
|
||
return {"code": 200, "data": result}
|
||
|
||
|
||
@router.post("/skills/execute", tags=["Skills编排"], summary="执行Skill")
|
||
async def execute_skill(skill_id: str, context: dict[str, Any] = {}):
|
||
"""执行指定的Skill"""
|
||
result = skill_service.execute_skill(skill_id, context)
|
||
return {"code": 200, "data": result}
|
||
|
||
|
||
@router.get("/workflows", tags=["Skills编排"], summary="获取工作流列表")
|
||
async def list_workflows():
|
||
"""获取所有工作流"""
|
||
return {"code": 200, "data": skill_service.list_workflows()}
|
||
|
||
|
||
@router.post("/workflows", tags=["Skills编排"], summary="创建工作流")
|
||
async def create_workflow(workflow: Workflow):
|
||
"""创建新的工作流"""
|
||
result = skill_service.create_workflow(workflow)
|
||
return {"code": 200, "data": result, "message": "工作流创建成功"}
|
||
|
||
|
||
# =============================================================================
|
||
# 智能体记忆管理接口(SRS-MACP_F-006)
|
||
# =============================================================================
|
||
|
||
@router.get("/memory", tags=["记忆管理"], summary="获取记忆列表")
|
||
async def list_memories(session_id: Optional[str] = None):
|
||
"""获取记忆列表,可按会话ID过滤"""
|
||
return {"code": 200, "data": memory_service.list_memories(session_id)}
|
||
|
||
|
||
@router.post("/memory", tags=["记忆管理"], summary="创建记忆条目")
|
||
async def create_memory(memory: MemoryEntry):
|
||
"""创建新的记忆条目"""
|
||
result = memory_service.create_memory(memory)
|
||
return {"code": 200, "data": result, "message": "记忆创建成功"}
|
||
|
||
|
||
@router.get("/memory/{memory_id}", tags=["记忆管理"], summary="获取记忆详情")
|
||
async def get_memory(memory_id: str):
|
||
"""获取记忆条目详情"""
|
||
result = memory_service.get_memory(memory_id)
|
||
if not result:
|
||
raise HTTPException(status_code=404, detail="记忆条目不存在")
|
||
return {"code": 200, "data": result}
|
||
|
||
|
||
@router.delete("/memory/{memory_id}", tags=["记忆管理"], summary="删除记忆")
|
||
async def delete_memory(memory_id: str):
|
||
"""删除记忆条目"""
|
||
success = memory_service.delete_memory(memory_id)
|
||
if not success:
|
||
raise HTTPException(status_code=404, detail="记忆条目不存在")
|
||
return {"code": 200, "message": "记忆已删除"}
|
||
|
||
|
||
@router.get("/memory/search", tags=["记忆管理"], summary="搜索记忆")
|
||
async def search_memories(query: str):
|
||
"""基于语义相似度检索记忆"""
|
||
return {"code": 200, "data": memory_service.search_memories(query)}
|
||
|
||
|
||
# =============================================================================
|
||
# 知识库交互接口(SRS-MACP_F-008)
|
||
# =============================================================================
|
||
|
||
@router.get("/knowledge", tags=["知识库"], summary="获取知识库片段")
|
||
async def list_knowledge(level: Optional[KnowledgeLevel] = None):
|
||
"""获取知识库片段列表,可按层级过滤"""
|
||
return {"code": 200, "data": kb_service.list_chunks(level)}
|
||
|
||
|
||
@router.get("/knowledge/{chunk_id}", tags=["知识库"], summary="获取知识库片段详情")
|
||
async def get_knowledge_chunk(chunk_id: str):
|
||
"""获取知识库片段详情"""
|
||
result = kb_service.get_chunk(chunk_id)
|
||
if not result:
|
||
raise HTTPException(status_code=404, detail="知识库片段不存在")
|
||
return {"code": 200, "data": result}
|
||
|
||
|
||
@router.post("/knowledge/search", tags=["知识库"], summary="搜索知识库")
|
||
async def search_knowledge(query: str, top_k: int = 5):
|
||
"""搜索知识库片段"""
|
||
return {"code": 200, "data": kb_service.search_chunks(query, top_k)}
|
||
|
||
|
||
# =============================================================================
|
||
# 大模型接入管理接口(SRS-MACP_F-009)
|
||
# =============================================================================
|
||
|
||
@router.get("/models", tags=["模型管理"], summary="获取模型配置列表")
|
||
async def list_models():
|
||
"""获取所有模型配置"""
|
||
return {"code": 200, "data": model_service.list_models()}
|
||
|
||
|
||
@router.get("/models/primary", tags=["模型管理"], summary="获取主用模型")
|
||
async def get_primary_model():
|
||
"""获取主用模型"""
|
||
model = model_service.get_primary_model()
|
||
if not model:
|
||
raise HTTPException(status_code=404, detail="无可用主用模型")
|
||
return {"code": 200, "data": model}
|
||
|
||
|
||
@router.get("/models/{model_id}/health", tags=["模型管理"], summary="模型健康检查")
|
||
async def health_check(model_id: str):
|
||
"""对指定模型执行健康检查"""
|
||
return {"code": 200, "data": model_service.health_check(model_id)}
|
||
|
||
|
||
@router.post("/models/failover", tags=["模型管理"], summary="模型故障转移")
|
||
async def failover_model(failed_model_id: str):
|
||
"""执行模型故障转移"""
|
||
model = model_service.failover(failed_model_id)
|
||
if not model:
|
||
raise HTTPException(status_code=503, detail="无可用的故障转移模型")
|
||
return {"code": 200, "data": model, "message": f"已从{failed_model_id}切换到{model.id}"}
|
||
|
||
|
||
# =============================================================================
|
||
# 多模态输入处理接口(SRS-MACP_F-010)
|
||
# =============================================================================
|
||
|
||
@router.post("/multimodal/process", tags=["多模态处理"], summary="多模态内容处理")
|
||
async def process_multimodal(file_name: str, file_type: FileType):
|
||
"""处理多模态文件(图片、音频、视频)"""
|
||
result = multimodal_service.process_file(file_name, file_type)
|
||
return {"code": 200, "data": result, "message": "多模态处理完成"}
|
||
|
||
|
||
@router.get("/multimodal/result/{result_id}", tags=["多模态处理"], summary="获取处理结果")
|
||
async def get_multimodal_result(result_id: str):
|
||
"""获取多模态处理结果"""
|
||
result = multimodal_service.get_result(result_id)
|
||
if not result:
|
||
raise HTTPException(status_code=404, detail="处理结果不存在")
|
||
return {"code": 200, "data": result}
|
||
|
||
|
||
# =============================================================================
|
||
# 安全沙箱执行接口(SRS-MACP_F-007)
|
||
# =============================================================================
|
||
|
||
@router.post("/sandbox/execute", tags=["安全沙箱"], summary="沙箱执行代码")
|
||
async def sandbox_execute(code: str, language: str = "python"):
|
||
"""在安全沙箱中执行代码"""
|
||
result = sandbox_service.execute_code(code, language)
|
||
return {"code": 200, "data": result, "message": "沙箱执行完成"}
|
||
|
||
|
||
@router.get("/sandbox/logs", tags=["安全沙箱"], summary="获取执行日志")
|
||
async def get_sandbox_logs():
|
||
"""获取沙箱执行日志"""
|
||
return {"code": 200, "data": sandbox_service.get_execution_logs()}
|