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:
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