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:
80
server/internal/handler/approval_handler.go
Normal file
80
server/internal/handler/approval_handler.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"x-agents/server/internal/model"
|
||||
"x-agents/server/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ApprovalHandler struct {
|
||||
approvalService *service.ApprovalService
|
||||
}
|
||||
|
||||
func NewApprovalHandler(approvalService *service.ApprovalService) *ApprovalHandler {
|
||||
return &ApprovalHandler{approvalService: approvalService}
|
||||
}
|
||||
|
||||
// Approve 处理审批请求
|
||||
func (h *ApprovalHandler) Approve(c *gin.Context) {
|
||||
var req struct {
|
||||
RequestID string `json:"request_id" binding:"required"`
|
||||
Approved bool `json:"approved"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
userID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
var result interface{}
|
||||
var err error
|
||||
|
||||
if req.Approved {
|
||||
result, err = h.approvalService.Approve(req.RequestID, userID.(string))
|
||||
} else {
|
||||
result, err = h.approvalService.Reject(req.RequestID, userID.(string))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
// GetStatus 获取审批状态
|
||||
func (h *ApprovalHandler) GetStatus(c *gin.Context) {
|
||||
requestID := c.Param("id")
|
||||
|
||||
result, err := h.approvalService.GetApproval(requestID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "request not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
// ListPending 获取待审批列表
|
||||
func (h *ApprovalHandler) ListPending(c *gin.Context) {
|
||||
result, err := h.approvalService.GetPendingApprovals()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if result == nil {
|
||||
result = []model.ToolApprovalRequest{}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"pending": result})
|
||||
}
|
||||
80
server/internal/handler/auth_handler.go
Normal file
80
server/internal/handler/auth_handler.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"x-agents/server/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
authService *service.AuthService
|
||||
}
|
||||
|
||||
func NewAuthHandler(authService *service.AuthService) *AuthHandler {
|
||||
return &AuthHandler{authService: authService}
|
||||
}
|
||||
|
||||
type LoginRequest struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
type LoginResponse struct {
|
||||
Token string `json:"token"`
|
||||
User interface{} `json:"user"`
|
||||
}
|
||||
|
||||
// Login 处理登录
|
||||
func (h *AuthHandler) Login(c *gin.Context) {
|
||||
var req LoginRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.authService.Login(service.LoginRequest{
|
||||
Username: req.Username,
|
||||
Password: req.Password,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, LoginResponse{
|
||||
Token: resp.Token,
|
||||
User: gin.H{
|
||||
"id": resp.User.ID,
|
||||
"username": resp.User.Username,
|
||||
"email": resp.User.Email,
|
||||
"role": resp.User.RoleID,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Register 处理注册
|
||||
func (h *AuthHandler) Register(c *gin.Context) {
|
||||
var req struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.authService.Register(req.Username, req.Password, req.Email)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"id": user.ID,
|
||||
"username": user.Username,
|
||||
"email": user.Email,
|
||||
})
|
||||
}
|
||||
89
server/internal/handler/chat_handler.go
Normal file
89
server/internal/handler/chat_handler.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"x-agents/server/internal/model"
|
||||
"x-agents/server/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ChatHandler struct {
|
||||
chatService *service.ChatService
|
||||
}
|
||||
|
||||
func NewChatHandler(chatService *service.ChatService) *ChatHandler {
|
||||
return &ChatHandler{chatService: chatService}
|
||||
}
|
||||
|
||||
// Chat 处理聊天请求
|
||||
func (h *ChatHandler) Chat(c *gin.Context) {
|
||||
var req model.AgentRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 从上下文获取用户ID(由中间件设置)
|
||||
userID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.chatService.Chat(c.Request.Context(), userID.(string), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// ListAgents 获取 Agent 列表
|
||||
func (h *ChatHandler) ListAgents(c *gin.Context) {
|
||||
userID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
agents, err := h.chatService.ListAgents(userID.(string))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if agents == nil {
|
||||
agents = []model.Agent{}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"agents": agents})
|
||||
}
|
||||
|
||||
// CreateAgent 创建 Agent
|
||||
func (h *ChatHandler) CreateAgent(c *gin.Context) {
|
||||
userID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
agent, err := h.chatService.CreateAgent(userID.(string), req.Name, req.Description)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, agent)
|
||||
}
|
||||
112
server/internal/handler/database_handler.go
Normal file
112
server/internal/handler/database_handler.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"x-agents/server/internal/model"
|
||||
"x-agents/server/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type DatabaseHandler struct {
|
||||
service *service.DatabaseService
|
||||
}
|
||||
|
||||
func NewDatabaseHandler(svc *service.DatabaseService) *DatabaseHandler {
|
||||
return &DatabaseHandler{service: svc}
|
||||
}
|
||||
|
||||
// Check 检查数据库连接
|
||||
func (h *DatabaseHandler) Check(c *gin.Context) {
|
||||
var req model.CheckRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.service.Check(req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
|
||||
// Create 创建数据库信息
|
||||
func (h *DatabaseHandler) Create(c *gin.Context) {
|
||||
var req model.CreateDatabaseRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
info, err := h.service.Create(req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, info)
|
||||
}
|
||||
|
||||
// GetByID 获取详情
|
||||
func (h *DatabaseHandler) GetByID(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
info, err := h.service.GetByID(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, info)
|
||||
}
|
||||
|
||||
// List 获取列表
|
||||
func (h *DatabaseHandler) List(c *gin.Context) {
|
||||
list, err := h.service.List()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if list == nil {
|
||||
list = []model.DatabaseInfo{}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"list": list})
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
func (h *DatabaseHandler) Update(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
var req model.UpdateDatabaseRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
info, err := h.service.Update(id, req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, info)
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
func (h *DatabaseHandler) Delete(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
err := h.service.Delete(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
|
||||
}
|
||||
132
server/internal/handler/sub_table_handler.go
Normal file
132
server/internal/handler/sub_table_handler.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"x-agents/server/internal/model"
|
||||
"x-agents/server/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SubTableHandler struct {
|
||||
service *service.SubTableService
|
||||
}
|
||||
|
||||
func NewSubTableHandler(svc *service.SubTableService) *SubTableHandler {
|
||||
return &SubTableHandler{service: svc}
|
||||
}
|
||||
|
||||
// Create 创建子表信息
|
||||
func (h *SubTableHandler) Create(c *gin.Context) {
|
||||
var req model.CreateSubTableRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
info, err := h.service.Create(req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, info)
|
||||
}
|
||||
|
||||
// GetByID 获取详情
|
||||
func (h *SubTableHandler) GetByID(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
info, err := h.service.GetByID(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, info)
|
||||
}
|
||||
|
||||
// ListByDatabase 获取数据库下所有子表
|
||||
func (h *SubTableHandler) ListByDatabase(c *gin.Context) {
|
||||
databaseID := c.Param("database_id")
|
||||
|
||||
list, err := h.service.ListByDatabaseID(databaseID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if list == nil {
|
||||
list = []model.SubTableInfo{}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"list": list})
|
||||
}
|
||||
|
||||
// GetMappingFromFile 从文件获取映射
|
||||
func (h *SubTableHandler) GetMappingFromFile(c *gin.Context) {
|
||||
databaseID := c.Param("database_id")
|
||||
|
||||
mapping, err := h.service.GetMappingFromFile(databaseID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if mapping == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"mapping": nil, "message": "no mapping file found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"mapping": mapping})
|
||||
}
|
||||
|
||||
// Update 更新
|
||||
func (h *SubTableHandler) Update(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
var req model.UpdateSubTableRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
info, err := h.service.Update(id, req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, info)
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
func (h *SubTableHandler) Delete(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
err := h.service.Delete(id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
|
||||
}
|
||||
|
||||
// GetTablesDDL 获取数据库下所有表及DDL
|
||||
func (h *SubTableHandler) GetTablesDDL(c *gin.Context) {
|
||||
databaseID := c.Param("database_id")
|
||||
|
||||
tables, err := h.service.GetTableDDLFromDatabase(databaseID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if tables == nil {
|
||||
tables = []model.TableDDLInfo{}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"tables": tables})
|
||||
}
|
||||
62
server/internal/handler/system_handler.go
Normal file
62
server/internal/handler/system_handler.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"x-agents/server/internal/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SystemHandler struct{}
|
||||
|
||||
func NewSystemHandler() *SystemHandler {
|
||||
return &SystemHandler{}
|
||||
}
|
||||
|
||||
// GetSystemInfo 获取系统信息
|
||||
func (h *SystemHandler) GetSystemInfo(c *gin.Context) {
|
||||
info, err := getSystemInfo()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, info)
|
||||
}
|
||||
|
||||
// getSystemInfo 获取系统信息
|
||||
func getSystemInfo() (*model.SystemInfo, error) {
|
||||
// 获取CPU使用率
|
||||
cpuPercent, err := getCPUPercent()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取CPU核心数
|
||||
coreCount, err := getCPUCoreCount()
|
||||
if err != nil {
|
||||
coreCount = 0
|
||||
}
|
||||
|
||||
// 获取CPU型号
|
||||
modelName, err := getCPUModelName()
|
||||
if err != nil {
|
||||
modelName = "Unknown"
|
||||
}
|
||||
|
||||
// 获取内存信息
|
||||
memoryInfo, err := getMemoryInfo()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &model.SystemInfo{
|
||||
CPU: model.CPUInfo{
|
||||
Percent: cpuPercent,
|
||||
CoreCount: coreCount,
|
||||
ModelName: modelName,
|
||||
},
|
||||
Memory: *memoryInfo,
|
||||
}, nil
|
||||
}
|
||||
60
server/internal/handler/system_helper.go
Normal file
60
server/internal/handler/system_helper.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/mem"
|
||||
"x-agents/server/internal/model"
|
||||
)
|
||||
|
||||
func getCPUPercent() (float64, error) {
|
||||
percent, err := cpu.Percent(0, false)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if len(percent) > 0 {
|
||||
return percent[0], nil
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func getCPUCoreCount() (int, error) {
|
||||
count, err := cpu.Counts(false)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func getCPUModelName() (string, error) {
|
||||
info, err := cpu.Info()
|
||||
if err != nil {
|
||||
return "Unknown", err
|
||||
}
|
||||
if len(info) > 0 {
|
||||
return info[0].ModelName, nil
|
||||
}
|
||||
return "Unknown", nil
|
||||
}
|
||||
|
||||
func getMemoryInfo() (*model.MemoryInfo, error) {
|
||||
v, err := mem.VirtualMemory()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 计算使用率
|
||||
percent := 0.0
|
||||
if v.Total > 0 {
|
||||
percent = float64(v.Used) / float64(v.Total) * 100
|
||||
}
|
||||
|
||||
return &model.MemoryInfo{
|
||||
Total: v.Total,
|
||||
Used: v.Used,
|
||||
Available: v.Available,
|
||||
Percent: percent,
|
||||
TotalGB: float64(v.Total) / 1024 / 1024 / 1024,
|
||||
UsedGB: float64(v.Used) / 1024 / 1024 / 1024,
|
||||
AvailableGB: float64(v.Available) / 1024 / 1024 / 1024,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user