feat: 后端认证和工具模块更新
- main.go: 添加 Swagger 文档、初始化默认管理员 - 认证模块: 完善用户角色管理 - 新增工具模块: tool_handler, tool_repo, tool_service, tool model - 更新 go.mod 依赖 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"x-agents/server/internal/model"
|
||||
@@ -123,10 +128,11 @@ func (s *AuthService) Register(username, password, email string) (*model.User, e
|
||||
role, err := s.userRepo.FindRoleByID(user.RoleID)
|
||||
if err != nil {
|
||||
// 创建默认角色
|
||||
perms, _ := json.Marshal([]int{int(model.PermissionRead), int(model.PermissionWrite)})
|
||||
role = &model.Role{
|
||||
ID: "user",
|
||||
Name: "user",
|
||||
Permissions: []model.PermissionLevel{model.PermissionRead, model.PermissionWrite},
|
||||
Permissions: string(perms),
|
||||
}
|
||||
s.userRepo.CreateRole(role)
|
||||
user.Role = role
|
||||
@@ -136,10 +142,74 @@ func (s *AuthService) Register(username, password, email string) (*model.User, e
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 创建用户工作空间目录
|
||||
if err := s.createUserWorkspace(username); err != nil {
|
||||
// 工作空间创建失败不影响注册成功,仅记录日志
|
||||
println("Warning: failed to create user workspace:", err.Error())
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// createUserWorkspace 创建用户工作空间目录
|
||||
func (s *AuthService) createUserWorkspace(username string) error {
|
||||
// 获取当前可执行文件所在目录
|
||||
execPath, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 尝试多种方式获取项目根目录
|
||||
projectRoot := execPath
|
||||
|
||||
// 方式1: 如果当前目录名为 server,向上找一级
|
||||
baseName := filepath.Base(execPath)
|
||||
// 处理 Windows 和 Unix 风格路径
|
||||
if baseName == "server" || strings.HasSuffix(execPath, "/server") || strings.HasSuffix(execPath, "\\server") {
|
||||
projectRoot = filepath.Dir(execPath)
|
||||
}
|
||||
|
||||
// 方式2: 尝试向上查找包含 .git 或 package.json 的目录
|
||||
if _, err := os.Stat(filepath.Join(projectRoot, ".git")); os.IsNotExist(err) {
|
||||
// 继续向上查找
|
||||
for i := 0; i < 3; i++ {
|
||||
parent := filepath.Dir(projectRoot)
|
||||
if parent == projectRoot {
|
||||
break
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(parent, ".git")); err == nil {
|
||||
projectRoot = parent
|
||||
break
|
||||
}
|
||||
projectRoot = parent
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[Workspace] Creating workspace at: %s", filepath.Join(projectRoot, "account", username))
|
||||
|
||||
// 创建 account 目录和用户目录
|
||||
workspacePath := filepath.Join(projectRoot, "account", username)
|
||||
if err := os.MkdirAll(workspacePath, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 创建子目录
|
||||
subDirs := []string{"projects", "files", "temp"}
|
||||
for _, dir := range subDirs {
|
||||
if err := os.MkdirAll(filepath.Join(workspacePath, dir), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserByID 根据ID获取用户
|
||||
func (s *AuthService) GetUserByID(id string) (*model.User, error) {
|
||||
return s.userRepo.FindByID(id)
|
||||
}
|
||||
|
||||
// GetAllUsers 获取所有用户
|
||||
func (s *AuthService) GetAllUsers() ([]model.User, error) {
|
||||
return s.userRepo.FindAll()
|
||||
}
|
||||
|
||||
219
server/internal/service/tool_service.go
Normal file
219
server/internal/service/tool_service.go
Normal file
@@ -0,0 +1,219 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"log"
|
||||
"x-agents/server/internal/model"
|
||||
"x-agents/server/internal/repository"
|
||||
)
|
||||
|
||||
type ToolService struct {
|
||||
toolRepo *repository.ToolRepository
|
||||
}
|
||||
|
||||
func NewToolService(toolRepo *repository.ToolRepository) *ToolService {
|
||||
return &ToolService{toolRepo: toolRepo}
|
||||
}
|
||||
|
||||
func (s *ToolService) GetAllTools() ([]model.Tool, error) {
|
||||
return s.toolRepo.FindAll()
|
||||
}
|
||||
|
||||
func (s *ToolService) GetToolsByCategory(category string) ([]model.Tool, error) {
|
||||
return s.toolRepo.FindByCategory(category)
|
||||
}
|
||||
|
||||
func (s *ToolService) GetToolByID(id string) (*model.Tool, error) {
|
||||
return s.toolRepo.FindByID(id)
|
||||
}
|
||||
|
||||
// GetTools 根据条件获取工具列表
|
||||
func (s *ToolService) GetTools(category string, status string) ([]model.Tool, error) {
|
||||
var tools []model.Tool
|
||||
query := s.toolRepo.DB()
|
||||
|
||||
if category != "" {
|
||||
query = query.Where("category = ?", category)
|
||||
}
|
||||
if status != "" {
|
||||
query = query.Where("status = ?", status)
|
||||
}
|
||||
|
||||
err := query.Find(&tools).Error
|
||||
return tools, err
|
||||
}
|
||||
|
||||
func (s *ToolService) CreateTool(tool *model.Tool) error {
|
||||
return s.toolRepo.Create(tool)
|
||||
}
|
||||
|
||||
func (s *ToolService) UpdateTool(tool *model.Tool) error {
|
||||
return s.toolRepo.Update(tool)
|
||||
}
|
||||
|
||||
func (s *ToolService) DeleteTool(id string) error {
|
||||
return s.toolRepo.Delete(id)
|
||||
}
|
||||
|
||||
// InitDefaultTools 初始化默认工具到数据库
|
||||
func (s *ToolService) InitDefaultTools() error {
|
||||
log.Println("[ToolService] Starting init default tools...")
|
||||
|
||||
// 获取默认工具
|
||||
tools := s.getDefaultTools()
|
||||
|
||||
// 删除现有的系统工具,重新插入
|
||||
s.toolRepo.DB().Where("provider = ?", "system").Delete(&model.Tool{})
|
||||
log.Printf("[ToolService] Deleted existing system tools, inserting %d default tools...", len(tools))
|
||||
|
||||
for _, tool := range tools {
|
||||
if err := s.toolRepo.Create(&tool); err != nil {
|
||||
log.Printf("[ToolService] Create tool error: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[ToolService] Default tools initialized successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
// getDefaultTools 获取默认工具列表
|
||||
func (s *ToolService) getDefaultTools() []model.Tool {
|
||||
return []model.Tool{
|
||||
// 文件操作
|
||||
{
|
||||
Name: "read_file",
|
||||
Description: "Read the contents of a file from the filesystem.",
|
||||
Category: "file",
|
||||
SecurityLevel: "safe",
|
||||
RequireApproval: false,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"file_path":{"type":"string","description":"The path to the file to read"},"encoding":{"type":"string","description":"File encoding (default: utf-8)","default":"utf-8"}},"required":["file_path"]}`,
|
||||
},
|
||||
{
|
||||
Name: "write_file",
|
||||
Description: "Write content to a file. Creates the file if it doesn't exist, overwrites if it does.",
|
||||
Category: "file",
|
||||
SecurityLevel: "review",
|
||||
RequireApproval: true,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"file_path":{"type":"string","description":"The path to the file to write"},"content":{"type":"string","description":"The content to write to the file"}},"required":["file_path","content"]}`,
|
||||
},
|
||||
{
|
||||
Name: "list_dir",
|
||||
Description: "List the contents of a directory.",
|
||||
Category: "file",
|
||||
SecurityLevel: "safe",
|
||||
RequireApproval: false,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"dir_path":{"type":"string","description":"The path to the directory to list","default":"."}}}`,
|
||||
},
|
||||
{
|
||||
Name: "delete_file",
|
||||
Description: "Delete a file or directory.",
|
||||
Category: "file",
|
||||
SecurityLevel: "danger",
|
||||
RequireApproval: true,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"file_path":{"type":"string","description":"The path to the file or directory to delete"}},"required":["file_path"]}`,
|
||||
},
|
||||
{
|
||||
Name: "search_files",
|
||||
Description: "Search for files by name pattern or content.",
|
||||
Category: "file",
|
||||
SecurityLevel: "safe",
|
||||
RequireApproval: false,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"directory":{"type":"string","description":"The directory to search in"},"pattern":{"type":"string","description":"Glob pattern for file names","default":"*"},"content_pattern":{"type":"string","description":"Search for files containing this text"}},"required":["directory"]}`,
|
||||
},
|
||||
// 代码执行
|
||||
{
|
||||
Name: "execute_python",
|
||||
Description: "Execute Python code in a sandboxed environment.",
|
||||
Category: "executor",
|
||||
SecurityLevel: "review",
|
||||
RequireApproval: true,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"code":{"type":"string","description":"The Python code to execute"},"timeout":{"type":"integer","description":"Execution timeout in seconds","default":30}},"required":["code"]}`,
|
||||
},
|
||||
{
|
||||
Name: "execute_javascript",
|
||||
Description: "Execute JavaScript code in a sandboxed environment.",
|
||||
Category: "executor",
|
||||
SecurityLevel: "review",
|
||||
RequireApproval: true,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"code":{"type":"string","description":"The JavaScript code to execute"},"timeout":{"type":"integer","description":"Execution timeout in seconds","default":30}},"required":["code"]}`,
|
||||
},
|
||||
{
|
||||
Name: "execute_bash",
|
||||
Description: "Execute a bash command in a sandboxed environment.",
|
||||
Category: "executor",
|
||||
SecurityLevel: "danger",
|
||||
RequireApproval: true,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"command":{"type":"string","description":"The bash command to execute"},"timeout":{"type":"integer","description":"Execution timeout in seconds","default":30}},"required":["command"]}`,
|
||||
},
|
||||
// 网页
|
||||
{
|
||||
Name: "web_fetch",
|
||||
Description: "Fetch content from a web URL.",
|
||||
Category: "web",
|
||||
SecurityLevel: "safe",
|
||||
RequireApproval: false,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"url":{"type":"string","description":"The URL to fetch"},"method":{"type":"string","description":"HTTP method","default":"GET"},"timeout":{"type":"integer","description":"Request timeout in seconds","default":30}},"required":["url"]}`,
|
||||
},
|
||||
{
|
||||
Name: "web_search",
|
||||
Description: "Search the web for information.",
|
||||
Category: "web",
|
||||
SecurityLevel: "safe",
|
||||
RequireApproval: false,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"query":{"type":"string","description":"The search query"},"max_results":{"type":"integer","description":"Maximum number of results","default":5}},"required":["query"]}`,
|
||||
},
|
||||
// HTTP
|
||||
{
|
||||
Name: "http_request",
|
||||
Description: "Make HTTP requests to APIs.",
|
||||
Category: "http",
|
||||
SecurityLevel: "safe",
|
||||
RequireApproval: false,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"url":{"type":"string","description":"The URL to request"},"method":{"type":"string","description":"HTTP method","default":"GET"},"params":{"type":"object","description":"Query parameters"},"json_data":{"type":"object","description":"JSON body"},"timeout":{"type":"integer","description":"Request timeout","default":30}},"required":["url"]}`,
|
||||
},
|
||||
// 通知
|
||||
{
|
||||
Name: "send_notification",
|
||||
Description: "Send notifications via email, webhook, dingtalk, or slack.",
|
||||
Category: "notification",
|
||||
SecurityLevel: "safe",
|
||||
RequireApproval: false,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"type":{"type":"string","description":"Notification type: email, webhook, dingtalk, slack"},"message":{"type":"string","description":"The notification message"}},"required":["type","message"]}`,
|
||||
},
|
||||
// 系统
|
||||
{
|
||||
Name: "get_current_time",
|
||||
Description: "Get the current date and time.",
|
||||
Category: "system",
|
||||
SecurityLevel: "safe",
|
||||
RequireApproval: false,
|
||||
Provider: "system",
|
||||
Status: "active",
|
||||
Parameters: `{"type":"object","properties":{"timezone":{"type":"string","description":"Optional timezone (e.g., UTC, Asia/Shanghai)"}}}`,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user