mirror of
https://github.com/flipped-aurora/gin-vue-admin.git
synced 2026-10-08 10:39:56 +00:00
* refactor: 移除冗余的工具名称和描述,简化代码生成器的文档 * Add Content-Type to S3 upload input Add Content-Type to S3 upload input * style: 优化仪表盘组件样式,调整各个子组件的布局和样式,提升视觉效果 * fix: 更新插件链接 * 优化角色配置-首页配置相关内容 * feat: 默认首页只允许选择已选中的菜单 * feat: 更新样式和优化组件,调整背景色及文本颜色 * feat: 清理无用的右侧边线 * feat: 增加了导入导出模板的自定义sql能力,方便灵活化扩展导入导出 * feat: 优化日志输出,增加插件字典定义 * feat: 增加插件字典定义前端相关 * feat: 增加插件自定义字典方法 * feat: 增加文件名和路径合法性检查 * feat: 修复暗黑模式和亮色模式logo路径定义 * feat: 移除未使用的defineEmits导入 * feat: 更新授权链接至新地址 * feat: 移除菜单组件背景色 * feat: 更新导出和导入SQL语句的示例占位符 * feat: 错误日志必须有数据库链接才会存储 * feat: 移除仪表板中公告卡片的高度限制 * feat: 添加插件列表接口,更新插件表格组件,移除不必要的注释 * feat: 更新版本号至v2.8.8,调整插件表格组件的请求参数 --------- Co-authored-by: piexlMax(奇淼 <qimiaojiangjizhao@gmail.com> Co-authored-by: Yexk_M <yexk@yexk.cn> Co-authored-by: Azir-11 <2075125282@qq.com>
230 lines
6.6 KiB
Go
230 lines
6.6 KiB
Go
package mcpTool
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
|
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
|
"github.com/flipped-aurora/gin-vue-admin/server/service"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func init() {
|
|
RegisterTool(&DictionaryOptionsGenerator{})
|
|
}
|
|
|
|
// DictionaryOptionsGenerator 字典选项生成器
|
|
type DictionaryOptionsGenerator struct{}
|
|
|
|
// DictionaryOption 字典选项结构
|
|
type DictionaryOption struct {
|
|
Label string `json:"label"`
|
|
Value string `json:"value"`
|
|
Sort int `json:"sort"`
|
|
}
|
|
|
|
// DictionaryGenerateRequest 字典生成请求
|
|
type DictionaryGenerateRequest struct {
|
|
DictType string `json:"dictType"` // 字典类型
|
|
FieldDesc string `json:"fieldDesc"` // 字段描述
|
|
Options []DictionaryOption `json:"options"` // AI生成的字典选项
|
|
DictName string `json:"dictName"` // 字典名称(可选)
|
|
Description string `json:"description"` // 字典描述(可选)
|
|
}
|
|
|
|
// DictionaryGenerateResponse 字典生成响应
|
|
type DictionaryGenerateResponse struct {
|
|
Success bool `json:"success"`
|
|
Message string `json:"message"`
|
|
DictType string `json:"dictType"`
|
|
OptionsCount int `json:"optionsCount"`
|
|
}
|
|
|
|
// New 返回工具注册信息
|
|
func (d *DictionaryOptionsGenerator) New() mcp.Tool {
|
|
return mcp.NewTool("generate_dictionary_options",
|
|
mcp.WithDescription("智能生成字典选项并自动创建字典和字典详情"),
|
|
mcp.WithString("dictType",
|
|
mcp.Required(),
|
|
mcp.Description("字典类型,用于标识字典的唯一性"),
|
|
),
|
|
mcp.WithString("fieldDesc",
|
|
mcp.Required(),
|
|
mcp.Description("字段描述,用于AI理解字段含义"),
|
|
),
|
|
mcp.WithString("options",
|
|
mcp.Required(),
|
|
mcp.Description("字典选项JSON字符串,格式:[{\"label\":\"显示名\",\"value\":\"值\",\"sort\":1}]"),
|
|
),
|
|
mcp.WithString("dictName",
|
|
mcp.Description("字典名称,如果不提供将自动生成"),
|
|
),
|
|
mcp.WithString("description",
|
|
mcp.Description("字典描述"),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Handle 处理工具调用
|
|
func (d *DictionaryOptionsGenerator) Handle(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
// 解析请求参数
|
|
args := request.GetArguments()
|
|
|
|
dictType, ok := args["dictType"].(string)
|
|
if !ok || dictType == "" {
|
|
return nil, errors.New("dictType 参数是必需的")
|
|
}
|
|
|
|
fieldDesc, ok := args["fieldDesc"].(string)
|
|
if !ok || fieldDesc == "" {
|
|
return nil, errors.New("fieldDesc 参数是必需的")
|
|
}
|
|
|
|
optionsStr, ok := args["options"].(string)
|
|
if !ok || optionsStr == "" {
|
|
return nil, errors.New("options 参数是必需的")
|
|
}
|
|
|
|
// 解析options JSON字符串
|
|
var options []DictionaryOption
|
|
if err := json.Unmarshal([]byte(optionsStr), &options); err != nil {
|
|
return nil, fmt.Errorf("options 参数格式错误: %v", err)
|
|
}
|
|
|
|
if len(options) == 0 {
|
|
return nil, errors.New("options 不能为空")
|
|
}
|
|
|
|
dictName, _ := args["dictName"].(string)
|
|
description, _ := args["description"].(string)
|
|
|
|
// 构建请求对象
|
|
req := &DictionaryGenerateRequest{
|
|
DictType: dictType,
|
|
FieldDesc: fieldDesc,
|
|
Options: options,
|
|
DictName: dictName,
|
|
Description: description,
|
|
}
|
|
|
|
// 创建字典
|
|
response, err := d.createDictionaryWithOptions(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 构建响应
|
|
resultJSON, err := json.MarshalIndent(response, "", " ")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("序列化结果失败: %v", err)
|
|
}
|
|
|
|
return &mcp.CallToolResult{
|
|
Content: []mcp.Content{
|
|
mcp.TextContent{
|
|
Type: "text",
|
|
Text: fmt.Sprintf("字典选项生成结果:\n\n%s", string(resultJSON)),
|
|
},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// createDictionaryWithOptions 创建字典和字典选项
|
|
func (d *DictionaryOptionsGenerator) createDictionaryWithOptions(ctx context.Context, req *DictionaryGenerateRequest) (*DictionaryGenerateResponse, error) {
|
|
// 检查字典是否已存在
|
|
exists, err := d.checkDictionaryExists(req.DictType)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("检查字典是否存在失败: %v", err)
|
|
}
|
|
|
|
if exists {
|
|
return &DictionaryGenerateResponse{
|
|
Success: false,
|
|
Message: fmt.Sprintf("字典 %s 已存在,跳过创建", req.DictType),
|
|
DictType: req.DictType,
|
|
OptionsCount: 0,
|
|
}, nil
|
|
}
|
|
|
|
// 生成字典名称
|
|
dictName := req.DictName
|
|
if dictName == "" {
|
|
dictName = d.generateDictionaryName(req.DictType, req.FieldDesc)
|
|
}
|
|
|
|
// 创建字典
|
|
dictionaryService := service.ServiceGroupApp.SystemServiceGroup.DictionaryService
|
|
dictionary := system.SysDictionary{
|
|
Name: dictName,
|
|
Type: req.DictType,
|
|
Status: &[]bool{true}[0], // 默认启用
|
|
Desc: req.Description,
|
|
}
|
|
|
|
err = dictionaryService.CreateSysDictionary(dictionary)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("创建字典失败: %v", err)
|
|
}
|
|
|
|
// 获取刚创建的字典ID
|
|
var createdDict system.SysDictionary
|
|
err = global.GVA_DB.Where("type = ?", req.DictType).First(&createdDict).Error
|
|
if err != nil {
|
|
return nil, fmt.Errorf("获取创建的字典失败: %v", err)
|
|
}
|
|
|
|
// 创建字典详情项
|
|
dictionaryDetailService := service.ServiceGroupApp.SystemServiceGroup.DictionaryDetailService
|
|
successCount := 0
|
|
|
|
for _, option := range req.Options {
|
|
dictionaryDetail := system.SysDictionaryDetail{
|
|
Label: option.Label,
|
|
Value: option.Value,
|
|
Status: &[]bool{true}[0], // 默认启用
|
|
Sort: option.Sort,
|
|
SysDictionaryID: int(createdDict.ID),
|
|
}
|
|
|
|
err = dictionaryDetailService.CreateSysDictionaryDetail(dictionaryDetail)
|
|
if err != nil {
|
|
global.GVA_LOG.Warn("创建字典详情项失败", zap.Error(err))
|
|
} else {
|
|
successCount++
|
|
}
|
|
}
|
|
|
|
return &DictionaryGenerateResponse{
|
|
Success: true,
|
|
Message: fmt.Sprintf("成功创建字典 %s,包含 %d 个选项", req.DictType, successCount),
|
|
DictType: req.DictType,
|
|
OptionsCount: successCount,
|
|
}, nil
|
|
}
|
|
|
|
// checkDictionaryExists 检查字典是否存在
|
|
func (d *DictionaryOptionsGenerator) checkDictionaryExists(dictType string) (bool, error) {
|
|
var dictionary system.SysDictionary
|
|
err := global.GVA_DB.Where("type = ?", dictType).First(&dictionary).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return false, nil // 字典不存在
|
|
}
|
|
return false, err // 其他错误
|
|
}
|
|
return true, nil // 字典存在
|
|
}
|
|
|
|
// generateDictionaryName 生成字典名称
|
|
func (d *DictionaryOptionsGenerator) generateDictionaryName(dictType, fieldDesc string) string {
|
|
if fieldDesc != "" {
|
|
return fmt.Sprintf("%s字典", fieldDesc)
|
|
}
|
|
return fmt.Sprintf("%s字典", dictType)
|
|
}
|