diff --git a/internal/biz/router.go b/internal/biz/router.go index 5888c76..0cdc93d 100644 --- a/internal/biz/router.go +++ b/internal/biz/router.go @@ -223,13 +223,31 @@ func (r *AiRouterService) handleMatch(c *websocket.Conn, matchJson *entitys.Matc switch pointTask.Type { case constant.TaskTypeApi: res, err = r.handleApiTask(c, matchJson, pointTask) + case constant.TaskTypeFunc: + ctx := context.TODO() + res, err = r.handleTask(ctx, matchJson, pointTask) default: return r.handleOtherTask(c, matchJson) } - + fmt.Println(res) return } +func (r *AiRouterService) handleTask(c context.Context, matchJson *entitys.Match, task *model.AiTask) (res []byte, err error) { + + var configData entitys.ConfigDataTool + err = json.Unmarshal([]byte(task.Config), &configData) + if err != nil { + return + } + resInterface, err := r.toolManager.ExecuteTool(c, configData.Tool, []byte(matchJson.Parameters)) + if err != nil { + return nil, err + } + + return json.Marshal(resInterface) +} + func (r *AiRouterService) handleOtherTask(c *websocket.Conn, matchJson *entitys.Match) (err error) { c.WriteMessage(1, []byte(matchJson.Reasoning)) @@ -251,12 +269,12 @@ func (r *AiRouterService) handleApiTask(c *websocket.Conn, matchJson *entitys.Ma for k, v := range requestParam { task.Config = strings.ReplaceAll(task.Config, "${"+k+"}", fmt.Sprintf("%v", v)) } - var configData entitys.ConfigData + var configData entitys.ConfigDataHttp err = json.Unmarshal([]byte(task.Config), &configData) if err != nil { return } - err = mapstructure.Decode(configData.Do, &request) + err = mapstructure.Decode(configData.Request, &request) if err != nil { return } diff --git a/internal/data/constant/const.go b/internal/data/constant/const.go index 19ae774..13ef745 100644 --- a/internal/data/constant/const.go +++ b/internal/data/constant/const.go @@ -13,4 +13,5 @@ type TaskType int32 const ( TaskTypeApi = 1 TaskTypeKnowle = 2 + TaskTypeFunc = 3 ) diff --git a/internal/entitys/types.go b/internal/entitys/types.go index 0a3373f..fea6177 100644 --- a/internal/entitys/types.go +++ b/internal/entitys/types.go @@ -69,9 +69,14 @@ type Tool interface { Execute(ctx context.Context, args json.RawMessage) (interface{}, error) } -type ConfigData struct { +type ConfigDataHttp struct { + Param map[string]interface{} `json:"param"` + Request map[string]interface{} `json:"request"` +} + +type ConfigDataTool struct { Param map[string]interface{} `json:"param"` - Do map[string]interface{} `json:"do"` + Tool string `json:"tool"` } // Message 消息