2026-04-21 17:22:21 +08:00
|
|
|
|
package dbtool
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
2026-04-28 22:37:10 +08:00
|
|
|
|
"mayfly-go/internal/ai/imsg"
|
2026-04-21 17:22:21 +08:00
|
|
|
|
"mayfly-go/internal/ai/tools"
|
|
|
|
|
|
"mayfly-go/internal/db/application"
|
2026-04-28 22:37:10 +08:00
|
|
|
|
"mayfly-go/pkg/i18n"
|
2026-04-21 17:22:21 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/cloudwego/eino/components/tool"
|
|
|
|
|
|
"github.com/cloudwego/eino/components/tool/utils"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type SqlExecParam struct {
|
2026-05-08 20:45:13 +08:00
|
|
|
|
DbId int64 `json:"dbId" jsonschema_description:"数据库ID。取值逻辑:1. 用户本次明确指定;2. 从前序工具的输入输出中继承已选定的数据库ID;3. 若均无,传0以触发参数补全。禁止凭空猜测。"`
|
|
|
|
|
|
DbName string `json:"dbName" jsonschema_description:"数据库名称。取值逻辑:1. 用户本次明确指定;2. 从前序工具的输入输出中继承已选定的数据库名称;3. 若均无,留空以触发参数补全。禁止凭空猜测。"`
|
2026-04-28 22:37:10 +08:00
|
|
|
|
SQL string `json:"sql" jsonschema_description:"SQL语句" jsonschema:"required" `
|
2026-04-21 17:22:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type SqlExecOutput struct {
|
2026-05-08 20:45:13 +08:00
|
|
|
|
DbId int64 `json:"dbId" jsonschema_description:"数据库ID"`
|
|
|
|
|
|
DbName string `json:"dbName" jsonschema_description:"数据库名称"`
|
|
|
|
|
|
DbType string `json:"dbType" jsonschema_description:"数据库类型,如mysql、postgresql等"`
|
|
|
|
|
|
Effected int64 `json:"effected" jsonschema_description:"影响的行数"`
|
2026-04-21 17:22:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetSqlExec() (tool.InvokableTool, error) {
|
2026-04-28 22:37:10 +08:00
|
|
|
|
return utils.InferTool("ExecSql",
|
|
|
|
|
|
i18n.T(imsg.ExecSqlToolInfo),
|
2026-04-21 17:22:21 +08:00
|
|
|
|
func(ctx context.Context, param *SqlExecParam) (*SqlExecOutput, error) {
|
2026-04-28 22:37:10 +08:00
|
|
|
|
toolDesc := i18n.TC(ctx, imsg.ExecSqlToolDesc)
|
|
|
|
|
|
// 检查必要参数,触发参数完善
|
|
|
|
|
|
if param.DbId == 0 || param.DbName == "" {
|
|
|
|
|
|
if err := tools.InterruptOrResumeParamCompletion(ctx, toolDesc, param, i18n.TC(ctx, imsg.DbInfoIncomplete), "db", []tools.CompletionParamInfo{
|
2026-05-08 20:45:13 +08:00
|
|
|
|
{Param: "dbId", Name: "数据库ID"},
|
|
|
|
|
|
{Param: "dbName", Name: "数据库名称"},
|
2026-04-28 22:37:10 +08:00
|
|
|
|
}); err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := tools.InterruptOrResumeApproval(ctx, toolDesc, param, i18n.TC(ctx, imsg.SqlExecApprovalReason)); err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
conn, err := application.GetDbApp().GetDbConn(ctx, uint64(param.DbId), param.DbName)
|
2026-04-21 17:22:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, tools.NewToolError(err, tools.RecoverRetry)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res, err := conn.ExecContext(ctx, param.SQL)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, tools.NewToolError(err, tools.RecoverRetry)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-08 20:45:13 +08:00
|
|
|
|
return &SqlExecOutput{
|
|
|
|
|
|
DbId: param.DbId,
|
|
|
|
|
|
DbName: param.DbName,
|
|
|
|
|
|
DbType: string(conn.Info.Type),
|
|
|
|
|
|
Effected: res,
|
|
|
|
|
|
}, nil
|
2026-04-21 17:22:21 +08:00
|
|
|
|
},
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|