feat: 重构前后端架构,添加Go后端和Python Agent服务

- 新增 Go 语言后端服务(server/),包含用户认证、Agent管理、数据库连接等API
- 新增 Python Agent 服务(agent/),实现Agent核心逻辑和工具集
- 前端从原生HTML迁移到Vue.js框架(web/src/)
- 添加 Docker Compose 支持(docker-compose.yml)
- 添加项目架构文档(docs/ARCHITECTURE.md)
- 添加环境变量示例(.env.example)和本地启动脚本(start-local.ps1)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 16:39:42 +08:00
parent 6fe3c412f4
commit b2bc9988a9
90 changed files with 9317 additions and 469 deletions

View File

@@ -0,0 +1,104 @@
"""
审批服务 - 处理工具执行的审批流程
"""
import uuid
from datetime import datetime
from typing import Any, Dict, Optional
from enum import Enum
class ApprovalStatus(Enum):
"""审批状态"""
PENDING = "pending"
APPROVED = "approved"
REJECTED = "rejected"
class ApprovalService:
"""审批服务"""
def __init__(self):
# 待审批队列
self.pending: Dict[str, dict] = {}
# 审批结果
self.results: Dict[str, ApprovalStatus] = {}
async def request_approval(
self,
tool_name: str,
params: dict,
user_id: str,
agent_id: str,
reason: str
) -> str:
"""
请求审批
Returns:
request_id: 审批请求ID
"""
request_id = str(uuid.uuid4())
request = {
"request_id": request_id,
"tool_name": tool_name,
"params": params,
"user_id": user_id,
"agent_id": agent_id,
"reason": reason,
"status": ApprovalStatus.PENDING,
"created_at": datetime.now().isoformat()
}
self.pending[request_id] = request
self.results[request_id] = ApprovalStatus.PENDING
# TODO: 通知 Go 后端有新审批
return request_id
async def check_approval(self, request_id: str, timeout: int = 300) -> bool:
"""
检查审批状态
Args:
request_id: 审批请求ID
timeout: 超时时间(秒)
Returns:
是否已批准
"""
import asyncio
start = datetime.now()
while (datetime.now() - start).seconds < timeout:
status = self.results.get(request_id)
if status == ApprovalStatus.APPROVED:
return True
elif status == ApprovalStatus.REJECTED:
return False
await asyncio.sleep(1)
raise TimeoutError("Approval request timeout")
async def approve(self, request_id: str):
"""批准请求"""
if request_id in self.pending:
self.pending[request_id]["status"] = ApprovalStatus.APPROVED
self.results[request_id] = ApprovalStatus.APPROVED
async def reject(self, request_id: str):
"""拒绝请求"""
if request_id in self.pending:
self.pending[request_id]["status"] = ApprovalStatus.REJECTED
self.results[request_id] = ApprovalStatus.REJECTED
def get_pending(self) -> list[dict]:
"""获取待审批列表"""
return [
req for req in self.pending.values()
if req["status"] == ApprovalStatus.PENDING
]

View File

@@ -0,0 +1,81 @@
"""
审计日志 - 记录所有 Agent 操作
"""
import json
from datetime import datetime
from typing import Any, Dict, Optional
from pathlib import Path
class AuditLogger:
"""审计日志记录器"""
def __init__(self, log_file: str = "audit.log"):
self.log_file = log_file
def log(
self,
action: str,
agent_id: str = "",
session_id: str = "",
user_id: str = "",
details: Dict[str, Any] = None,
result: str = "success"
):
"""记录审计日志"""
entry = {
"timestamp": datetime.now().isoformat(),
"action": action,
"agent_id": agent_id,
"session_id": session_id,
"user_id": user_id,
"details": details or {},
"result": result
}
# 写入文件
self._write_log(entry)
# TODO: 发送到 Go 后端
def log_tool_execution(
self,
tool_name: str,
params: Dict[str, Any],
user_id: str,
agent_id: str,
approved: bool,
result: Any
):
"""记录工具执行"""
self.log(
action="tool_execution",
agent_id=agent_id,
user_id=user_id,
details={
"tool_name": tool_name,
"params": params,
"approved": approved,
"result_preview": str(result)[:200] if result else None
},
result="approved" if approved else "pending_approval"
)
def log_error(self, action: str, error: str, **kwargs):
"""记录错误"""
self.log(
action=action,
details={"error": error, **kwargs},
result="error"
)
def _write_log(self, entry: dict):
"""写入日志文件"""
try:
log_path = Path(self.log_file)
log_path.parent.mkdir(parents=True, exist_ok=True)
with open(log_path, "a", encoding="utf-8") as f:
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
except Exception as e:
print(f"Failed to write audit log: {e}")