refactor: how to add a node type in workflow (#558)
This commit is contained in:
190
backend/domain/workflow/internal/nodes/selector/callbacks.go
Normal file
190
backend/domain/workflow/internal/nodes/selector/callbacks.go
Normal file
@@ -0,0 +1,190 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package selector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
)
|
||||
|
||||
type selectorCallbackField struct {
|
||||
Key string `json:"key"`
|
||||
Type vo.DataType `json:"type"`
|
||||
Value any `json:"value"`
|
||||
}
|
||||
|
||||
type selectorCondition struct {
|
||||
Left selectorCallbackField `json:"left"`
|
||||
Operator vo.OperatorType `json:"operator"`
|
||||
Right *selectorCallbackField `json:"right,omitempty"`
|
||||
}
|
||||
|
||||
type selectorBranch struct {
|
||||
Conditions []*selectorCondition `json:"conditions"`
|
||||
Logic vo.LogicType `json:"logic"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (s *Selector) ToCallbackInput(_ context.Context, in map[string]any) (map[string]any, error) {
|
||||
count := len(s.clauses)
|
||||
|
||||
output := make([]*selectorBranch, count)
|
||||
|
||||
for _, source := range s.ns.InputSources {
|
||||
targetPath := source.Path
|
||||
if len(targetPath) == 2 {
|
||||
indexStr := targetPath[0]
|
||||
index, err := strconv.Atoi(indexStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
branch := output[index]
|
||||
if branch == nil {
|
||||
output[index] = &selectorBranch{
|
||||
Conditions: []*selectorCondition{
|
||||
{
|
||||
Operator: s.clauses[index].Single.ToCanvasOperatorType(),
|
||||
},
|
||||
},
|
||||
Logic: ClauseRelationAND.ToVOLogicType(),
|
||||
}
|
||||
}
|
||||
|
||||
if targetPath[1] == LeftKey {
|
||||
leftV, ok := nodes.TakeMapValue(in, targetPath)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to take left value of %s", targetPath)
|
||||
}
|
||||
if source.Source.Ref.VariableType != nil {
|
||||
if *source.Source.Ref.VariableType == vo.ParentIntermediate {
|
||||
parentNodeKey, ok := s.ws.Hierarchy[s.ns.Key]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to find parent node key of %s", s.ns.Key)
|
||||
}
|
||||
parentNode := s.ws.GetNode(parentNodeKey)
|
||||
output[index].Conditions[0].Left = selectorCallbackField{
|
||||
Key: parentNode.Name + "." + strings.Join(source.Source.Ref.FromPath, "."),
|
||||
Type: s.ns.InputTypes[targetPath[0]].Properties[targetPath[1]].Type,
|
||||
Value: leftV,
|
||||
}
|
||||
} else {
|
||||
output[index].Conditions[0].Left = selectorCallbackField{
|
||||
Key: "",
|
||||
Type: s.ns.InputTypes[targetPath[0]].Properties[targetPath[1]].Type,
|
||||
Value: leftV,
|
||||
}
|
||||
}
|
||||
} else {
|
||||
output[index].Conditions[0].Left = selectorCallbackField{
|
||||
Key: s.ws.GetNode(source.Source.Ref.FromNodeKey).Name + "." + strings.Join(source.Source.Ref.FromPath, "."),
|
||||
Type: s.ns.InputTypes[targetPath[0]].Properties[targetPath[1]].Type,
|
||||
Value: leftV,
|
||||
}
|
||||
}
|
||||
} else if targetPath[1] == RightKey {
|
||||
rightV, ok := nodes.TakeMapValue(in, targetPath)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to take right value of %s", targetPath)
|
||||
}
|
||||
output[index].Conditions[0].Right = &selectorCallbackField{
|
||||
Type: s.ns.InputTypes[targetPath[0]].Properties[targetPath[1]].Type,
|
||||
Value: rightV,
|
||||
}
|
||||
}
|
||||
} else if len(targetPath) == 3 {
|
||||
indexStr := targetPath[0]
|
||||
index, err := strconv.Atoi(indexStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
multi := s.clauses[index].Multi
|
||||
|
||||
branch := output[index]
|
||||
if branch == nil {
|
||||
output[index] = &selectorBranch{
|
||||
Conditions: make([]*selectorCondition, len(multi.Clauses)),
|
||||
Logic: multi.Relation.ToVOLogicType(),
|
||||
}
|
||||
}
|
||||
|
||||
clauseIndexStr := targetPath[1]
|
||||
clauseIndex, err := strconv.Atoi(clauseIndexStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clause := multi.Clauses[clauseIndex]
|
||||
|
||||
if output[index].Conditions[clauseIndex] == nil {
|
||||
output[index].Conditions[clauseIndex] = &selectorCondition{
|
||||
Operator: clause.ToCanvasOperatorType(),
|
||||
}
|
||||
}
|
||||
|
||||
if targetPath[2] == LeftKey {
|
||||
leftV, ok := nodes.TakeMapValue(in, targetPath)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to take left value of %s", targetPath)
|
||||
}
|
||||
if source.Source.Ref.VariableType != nil {
|
||||
if *source.Source.Ref.VariableType == vo.ParentIntermediate {
|
||||
parentNodeKey, ok := s.ws.Hierarchy[s.ns.Key]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to find parent node key of %s", s.ns.Key)
|
||||
}
|
||||
parentNode := s.ws.GetNode(parentNodeKey)
|
||||
output[index].Conditions[clauseIndex].Left = selectorCallbackField{
|
||||
Key: parentNode.Name + "." + strings.Join(source.Source.Ref.FromPath, "."),
|
||||
Type: s.ns.InputTypes[targetPath[0]].Properties[targetPath[1]].Properties[targetPath[2]].Type,
|
||||
Value: leftV,
|
||||
}
|
||||
} else {
|
||||
output[index].Conditions[clauseIndex].Left = selectorCallbackField{
|
||||
Key: "",
|
||||
Type: s.ns.InputTypes[targetPath[0]].Properties[targetPath[1]].Properties[targetPath[2]].Type,
|
||||
Value: leftV,
|
||||
}
|
||||
}
|
||||
} else {
|
||||
output[index].Conditions[clauseIndex].Left = selectorCallbackField{
|
||||
Key: s.ws.GetNode(source.Source.Ref.FromNodeKey).Name + "." + strings.Join(source.Source.Ref.FromPath, "."),
|
||||
Type: s.ns.InputTypes[targetPath[0]].Properties[targetPath[1]].Properties[targetPath[2]].Type,
|
||||
Value: leftV,
|
||||
}
|
||||
}
|
||||
} else if targetPath[2] == RightKey {
|
||||
rightV, ok := nodes.TakeMapValue(in, targetPath)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to take right value of %s", targetPath)
|
||||
}
|
||||
output[index].Conditions[clauseIndex].Right = &selectorCallbackField{
|
||||
Type: s.ns.InputTypes[targetPath[0]].Properties[targetPath[1]].Properties[targetPath[2]].Type,
|
||||
Value: rightV,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]any{"branches": output}, nil
|
||||
}
|
||||
@@ -180,3 +180,48 @@ func (o *Operator) ToCanvasOperatorType() vo.OperatorType {
|
||||
panic(fmt.Sprintf("unknown operator: %+v", o))
|
||||
}
|
||||
}
|
||||
|
||||
func ToSelectorOperator(o vo.OperatorType, leftType *vo.TypeInfo) (Operator, error) {
|
||||
switch o {
|
||||
case vo.Equal:
|
||||
return OperatorEqual, nil
|
||||
case vo.NotEqual:
|
||||
return OperatorNotEqual, nil
|
||||
case vo.LengthGreaterThan:
|
||||
return OperatorLengthGreater, nil
|
||||
case vo.LengthGreaterThanEqual:
|
||||
return OperatorLengthGreaterOrEqual, nil
|
||||
case vo.LengthLessThan:
|
||||
return OperatorLengthLesser, nil
|
||||
case vo.LengthLessThanEqual:
|
||||
return OperatorLengthLesserOrEqual, nil
|
||||
case vo.Contain:
|
||||
if leftType.Type == vo.DataTypeObject {
|
||||
return OperatorContainKey, nil
|
||||
}
|
||||
return OperatorContain, nil
|
||||
case vo.NotContain:
|
||||
if leftType.Type == vo.DataTypeObject {
|
||||
return OperatorNotContainKey, nil
|
||||
}
|
||||
return OperatorNotContain, nil
|
||||
case vo.Empty:
|
||||
return OperatorEmpty, nil
|
||||
case vo.NotEmpty:
|
||||
return OperatorNotEmpty, nil
|
||||
case vo.True:
|
||||
return OperatorIsTrue, nil
|
||||
case vo.False:
|
||||
return OperatorIsFalse, nil
|
||||
case vo.GreaterThan:
|
||||
return OperatorGreater, nil
|
||||
case vo.GreaterThanEqual:
|
||||
return OperatorGreaterOrEqual, nil
|
||||
case vo.LessThan:
|
||||
return OperatorLesser, nil
|
||||
case vo.LessThanEqual:
|
||||
return OperatorLesserOrEqual, nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported operator type: %d", o)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,16 @@
|
||||
package selector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
einoCompose "github.com/cloudwego/eino/compose"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/canvas/convert"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
)
|
||||
|
||||
type ClauseRelation string
|
||||
@@ -29,10 +36,6 @@ const (
|
||||
ClauseRelationOR ClauseRelation = "or"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Clauses []*OneClauseSchema `json:"clauses"`
|
||||
}
|
||||
|
||||
type OneClauseSchema struct {
|
||||
Single *Operator `json:"single,omitempty"`
|
||||
Multi *MultiClauseSchema `json:"multi,omitempty"`
|
||||
@@ -52,3 +55,140 @@ func (c ClauseRelation) ToVOLogicType() vo.LogicType {
|
||||
|
||||
panic(fmt.Sprintf("unknown clause relation: %s", c))
|
||||
}
|
||||
|
||||
func (c *Config) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema.NodeSchema, error) {
|
||||
clauses := make([]*OneClauseSchema, 0)
|
||||
ns := &schema.NodeSchema{
|
||||
Key: vo.NodeKey(n.ID),
|
||||
Name: n.Data.Meta.Title,
|
||||
Type: entity.NodeTypeSelector,
|
||||
Configs: c,
|
||||
}
|
||||
|
||||
for i, branchCond := range n.Data.Inputs.Branches {
|
||||
inputType := &vo.TypeInfo{
|
||||
Type: vo.DataTypeObject,
|
||||
Properties: map[string]*vo.TypeInfo{},
|
||||
}
|
||||
|
||||
if len(branchCond.Condition.Conditions) == 1 { // single condition
|
||||
cond := branchCond.Condition.Conditions[0]
|
||||
|
||||
left := cond.Left
|
||||
if left == nil {
|
||||
return nil, fmt.Errorf("operator left is nil")
|
||||
}
|
||||
|
||||
leftType, err := convert.CanvasBlockInputToTypeInfo(left.Input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
leftSources, err := convert.CanvasBlockInputToFieldInfo(left.Input, einoCompose.FieldPath{fmt.Sprintf("%d", i), LeftKey}, n.Parent())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
inputType.Properties[LeftKey] = leftType
|
||||
|
||||
ns.AddInputSource(leftSources...)
|
||||
|
||||
op, err := ToSelectorOperator(cond.Operator, leftType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if cond.Right != nil {
|
||||
rightType, err := convert.CanvasBlockInputToTypeInfo(cond.Right.Input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rightSources, err := convert.CanvasBlockInputToFieldInfo(cond.Right.Input, einoCompose.FieldPath{fmt.Sprintf("%d", i), RightKey}, n.Parent())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
inputType.Properties[RightKey] = rightType
|
||||
ns.AddInputSource(rightSources...)
|
||||
}
|
||||
|
||||
ns.SetInputType(fmt.Sprintf("%d", i), inputType)
|
||||
|
||||
clauses = append(clauses, &OneClauseSchema{
|
||||
Single: &op,
|
||||
})
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
var relation ClauseRelation
|
||||
logic := branchCond.Condition.Logic
|
||||
if logic == vo.OR {
|
||||
relation = ClauseRelationOR
|
||||
} else if logic == vo.AND {
|
||||
relation = ClauseRelationAND
|
||||
}
|
||||
|
||||
var ops []*Operator
|
||||
for j, cond := range branchCond.Condition.Conditions {
|
||||
left := cond.Left
|
||||
if left == nil {
|
||||
return nil, fmt.Errorf("operator left is nil")
|
||||
}
|
||||
|
||||
leftType, err := convert.CanvasBlockInputToTypeInfo(left.Input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
leftSources, err := convert.CanvasBlockInputToFieldInfo(left.Input, einoCompose.FieldPath{fmt.Sprintf("%d", i), fmt.Sprintf("%d", j), LeftKey}, n.Parent())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
inputType.Properties[fmt.Sprintf("%d", j)] = &vo.TypeInfo{
|
||||
Type: vo.DataTypeObject,
|
||||
Properties: map[string]*vo.TypeInfo{
|
||||
LeftKey: leftType,
|
||||
},
|
||||
}
|
||||
|
||||
ns.AddInputSource(leftSources...)
|
||||
|
||||
op, err := ToSelectorOperator(cond.Operator, leftType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ops = append(ops, &op)
|
||||
|
||||
if cond.Right != nil {
|
||||
rightType, err := convert.CanvasBlockInputToTypeInfo(cond.Right.Input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rightSources, err := convert.CanvasBlockInputToFieldInfo(cond.Right.Input, einoCompose.FieldPath{fmt.Sprintf("%d", i), fmt.Sprintf("%d", j), RightKey}, n.Parent())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
inputType.Properties[fmt.Sprintf("%d", j)].Properties[RightKey] = rightType
|
||||
ns.AddInputSource(rightSources...)
|
||||
}
|
||||
}
|
||||
|
||||
ns.SetInputType(fmt.Sprintf("%d", i), inputType)
|
||||
|
||||
clauses = append(clauses, &OneClauseSchema{
|
||||
Multi: &MultiClauseSchema{
|
||||
Clauses: ops,
|
||||
Relation: relation,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
c.Clauses = clauses
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
@@ -23,23 +23,32 @@ import (
|
||||
|
||||
"github.com/cloudwego/eino/compose"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
)
|
||||
|
||||
type Selector struct {
|
||||
config *Config
|
||||
clauses []*OneClauseSchema
|
||||
ns *schema.NodeSchema
|
||||
ws *schema.WorkflowSchema
|
||||
}
|
||||
|
||||
func NewSelector(_ context.Context, config *Config) (*Selector, error) {
|
||||
if config == nil {
|
||||
return nil, fmt.Errorf("config is nil")
|
||||
type Config struct {
|
||||
Clauses []*OneClauseSchema `json:"clauses"`
|
||||
}
|
||||
|
||||
func (c *Config) Build(_ context.Context, ns *schema.NodeSchema, opts ...schema.BuildOption) (any, error) {
|
||||
ws := schema.GetBuildOptions(opts...).WS
|
||||
if ws == nil {
|
||||
return nil, fmt.Errorf("workflow schema is required")
|
||||
}
|
||||
|
||||
if len(config.Clauses) == 0 {
|
||||
if len(c.Clauses) == 0 {
|
||||
return nil, fmt.Errorf("config clauses are empty")
|
||||
}
|
||||
|
||||
for _, clause := range config.Clauses {
|
||||
for _, clause := range c.Clauses {
|
||||
if clause.Single == nil && clause.Multi == nil {
|
||||
return nil, fmt.Errorf("single clause and multi clause are both nil")
|
||||
}
|
||||
@@ -60,10 +69,42 @@ func NewSelector(_ context.Context, config *Config) (*Selector, error) {
|
||||
}
|
||||
|
||||
return &Selector{
|
||||
config: config,
|
||||
clauses: c.Clauses,
|
||||
ns: ns,
|
||||
ws: ws,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Config) BuildBranch(_ context.Context) (
|
||||
func(ctx context.Context, nodeOutput map[string]any) (int64, bool, error), bool) {
|
||||
return func(ctx context.Context, nodeOutput map[string]any) (int64, bool, error) {
|
||||
choice := nodeOutput[SelectKey].(int64)
|
||||
if choice < 0 || choice > int64(len(c.Clauses)+1) {
|
||||
return -1, false, fmt.Errorf("selector choice out of range: %d", choice)
|
||||
}
|
||||
|
||||
if choice == int64(len(c.Clauses)) { // default
|
||||
return -1, true, nil
|
||||
}
|
||||
|
||||
return choice, false, nil
|
||||
}, true
|
||||
}
|
||||
|
||||
func (c *Config) ExpectPorts(_ context.Context, n *vo.Node) []string {
|
||||
expects := make([]string, len(n.Data.Inputs.Branches)+1)
|
||||
expects[0] = "false" // default branch
|
||||
if len(n.Data.Inputs.Branches) > 0 {
|
||||
expects[1] = "true" // first condition
|
||||
}
|
||||
|
||||
for i := 1; i < len(n.Data.Inputs.Branches); i++ { // other conditions
|
||||
expects[i+1] = "true_" + strconv.Itoa(i)
|
||||
}
|
||||
|
||||
return expects
|
||||
}
|
||||
|
||||
type Operants struct {
|
||||
Left any
|
||||
Right any
|
||||
@@ -76,14 +117,14 @@ const (
|
||||
SelectKey = "selected"
|
||||
)
|
||||
|
||||
func (s *Selector) Select(_ context.Context, input map[string]any) (out map[string]any, err error) {
|
||||
in, err := s.SelectorInputConverter(input)
|
||||
func (s *Selector) Invoke(_ context.Context, input map[string]any) (out map[string]any, err error) {
|
||||
in, err := s.selectorInputConverter(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
predicates := make([]Predicate, 0, len(s.config.Clauses))
|
||||
for i, oneConf := range s.config.Clauses {
|
||||
predicates := make([]Predicate, 0, len(s.clauses))
|
||||
for i, oneConf := range s.clauses {
|
||||
if oneConf.Single != nil {
|
||||
left := in[i].Left
|
||||
right := in[i].Right
|
||||
@@ -132,23 +173,15 @@ func (s *Selector) Select(_ context.Context, input map[string]any) (out map[stri
|
||||
}
|
||||
|
||||
if isTrue {
|
||||
return map[string]any{SelectKey: i}, nil
|
||||
return map[string]any{SelectKey: int64(i)}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]any{SelectKey: len(in)}, nil // default choice
|
||||
return map[string]any{SelectKey: int64(len(in))}, nil // default choice
|
||||
}
|
||||
|
||||
func (s *Selector) GetType() string {
|
||||
return "Selector"
|
||||
}
|
||||
|
||||
func (s *Selector) ConditionCount() int {
|
||||
return len(s.config.Clauses)
|
||||
}
|
||||
|
||||
func (s *Selector) SelectorInputConverter(in map[string]any) (out []Operants, err error) {
|
||||
conf := s.config.Clauses
|
||||
func (s *Selector) selectorInputConverter(in map[string]any) (out []Operants, err error) {
|
||||
conf := s.clauses
|
||||
|
||||
for i, oneConf := range conf {
|
||||
if oneConf.Single != nil {
|
||||
@@ -187,8 +220,8 @@ func (s *Selector) SelectorInputConverter(in map[string]any) (out []Operants, er
|
||||
}
|
||||
|
||||
func (s *Selector) ToCallbackOutput(_ context.Context, output map[string]any) (*nodes.StructuredCallbackOutput, error) {
|
||||
count := len(s.config.Clauses)
|
||||
out := output[SelectKey].(int)
|
||||
count := int64(len(s.clauses))
|
||||
out := output[SelectKey].(int64)
|
||||
if out == count {
|
||||
cOutput := map[string]any{"result": "pass to else branch"}
|
||||
return &nodes.StructuredCallbackOutput{
|
||||
|
||||
Reference in New Issue
Block a user