You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.7 KiB
102 lines
2.7 KiB
package mcp
|
|
|
|
import (
|
|
"context"
|
|
"yunyan/comm"
|
|
"yunyan/lego/core"
|
|
"yunyan/lego/core/cbase"
|
|
"yunyan/lego/sys/log"
|
|
"yunyan/lego/sys/mysql"
|
|
"yunyan/pb"
|
|
"yunyan/utils"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
)
|
|
|
|
// 取消用户任务工具
|
|
type tool_cancel_user_task struct {
|
|
cbase.ModuleCompBase
|
|
module *Mcp
|
|
}
|
|
|
|
func (this *tool_cancel_user_task) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
|
this.ModuleCompBase.Init(service, module, comp, opt)
|
|
this.module = module.(*Mcp)
|
|
return
|
|
}
|
|
|
|
func (this *tool_cancel_user_task) Start() (err error) {
|
|
err = this.ModuleCompBase.Start()
|
|
this.module.AddTool(ToolGroup_GLOBAL, this.Tool(), this.Handl)
|
|
return
|
|
}
|
|
|
|
func (this *tool_cancel_user_task) Tool() mcp.Tool {
|
|
return mcp.NewTool("cancel_user_task",
|
|
mcp.WithDescription("取消用户的提醒任务"),
|
|
mcp.WithString("uid",
|
|
mcp.Description("用户ID"),
|
|
mcp.Required(),
|
|
),
|
|
mcp.WithNumber("task_id",
|
|
mcp.Description("要取消的任务ID"),
|
|
mcp.Required(),
|
|
),
|
|
)
|
|
}
|
|
|
|
func (this *tool_cancel_user_task) Handl(ctx context.Context, request mcp.CallToolRequest) (result *mcp.CallToolResult, err error) {
|
|
this.module.Debug("tool_cancel_user_task",
|
|
log.Field{Key: "request.Params.Arguments", Value: request.GetRawArguments()},
|
|
)
|
|
|
|
uid, err := request.RequireString("uid")
|
|
if err != nil {
|
|
return mcp.NewToolResultError("uid is required"), nil
|
|
}
|
|
taskId := request.GetInt("task_id", 0)
|
|
if taskId <= 0 {
|
|
return mcp.NewToolResultError("task_id is required"), nil
|
|
}
|
|
|
|
// 查找任务并验证归属
|
|
task := &pb.DBTask{}
|
|
if findErr := mysql.FindOne(comm.TableAllhelpTask, task, "id = ?", taskId); findErr != nil {
|
|
return mcp.NewToolResultError("任务不存在"), nil
|
|
}
|
|
if task.Uid != uid {
|
|
return mcp.NewToolResultError("无权操作该任务"), nil
|
|
}
|
|
if task.Status != pb.TaskStatus_TaskStatus_Pending {
|
|
return mcp.NewToolResultError(fmt.Sprintf("任务当前状态(%d)无法取消", task.Status)), nil
|
|
}
|
|
|
|
now := time.Now().Unix()
|
|
updateErr := mysql.Table(comm.TableAllhelpTask).Where("id = ?", taskId).Updates(map[string]interface{}{
|
|
"status": int32(pb.TaskStatus_TaskStatus_Cancelled),
|
|
"update_time": now,
|
|
"finish_time": now,
|
|
}).Error
|
|
if updateErr != nil {
|
|
this.module.Errorln("cancel_user_task error:", updateErr)
|
|
return mcp.NewToolResultError("取消任务失败"), nil
|
|
}
|
|
|
|
text := utils.ToString(map[string]interface{}{
|
|
"result": "success",
|
|
"task_id": task.Id,
|
|
"message": fmt.Sprintf("已成功取消任务「%s」", task.TaskName),
|
|
})
|
|
|
|
result = &mcp.CallToolResult{
|
|
Content: []mcp.Content{
|
|
mcp.TextContent{
|
|
Type: "text",
|
|
Text: text,
|
|
},
|
|
},
|
|
}
|
|
return
|
|
}
|
|
|