admin
2025-06-06 9cd825aea53fa5ba0cda1485464af027e27f0ce4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package tech.aiflowy.ai.entity;
 
import com.agentsflex.core.chain.Chain;
import com.agentsflex.core.chain.Parameter;
import com.agentsflex.core.llm.functions.BaseFunction;
import dev.tinyflow.core.Tinyflow;
import tech.aiflowy.ai.service.AiWorkflowService;
import tech.aiflowy.ai.utils.TinyFlowConfigService;
import tech.aiflowy.common.util.SpringContextUtil;
 
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
 
public class AiWorkflowFunction extends BaseFunction {
 
    private BigInteger workflowId;
 
    public AiWorkflowFunction() {
    }
 
    public AiWorkflowFunction(AiWorkflow aiWorkflow) {
        this.workflowId = aiWorkflow.getId();
        this.name = aiWorkflow.getTitle();
        this.description = aiWorkflow.getDescription();
        this.parameters = toParameters(aiWorkflow);
    }
 
 
    static com.agentsflex.core.llm.functions.Parameter[] toParameters(AiWorkflow aiWorkflow) {
        List<Parameter> parameterDefs = aiWorkflow.toTinyflow().toChain().getParameters();
        if (parameterDefs == null || parameterDefs.isEmpty()) {
            return new com.agentsflex.core.llm.functions.Parameter[0];
        }
 
        com.agentsflex.core.llm.functions.Parameter[] parameters = new com.agentsflex.core.llm.functions.Parameter[parameterDefs.size()];
        for (int i = 0; i < parameterDefs.size(); i++) {
            Parameter parameterDef = parameterDefs.get(i);
            com.agentsflex.core.llm.functions.Parameter parameter = new com.agentsflex.core.llm.functions.Parameter();
            parameter.setName(parameterDef.getName());
            parameter.setDescription(parameterDef.getDescription());
            parameter.setType(parameterDef.getDataType().toString());
            parameter.setRequired(parameterDef.isRequired());
            parameters[i] = parameter;
        }
        return parameters;
    }
 
    public BigInteger getWorkflowId() {
        return workflowId;
    }
 
    public void setWorkflowId(BigInteger workflowId) {
        this.workflowId = workflowId;
    }
 
    @Override
    public Object invoke(Map<String, Object> argsMap) {
        AiWorkflowService service = SpringContextUtil.getBean(AiWorkflowService.class);
        AiWorkflow workflow = service.getById(this.workflowId);
        if (workflow != null) {
            Tinyflow tinyflow = workflow.toTinyflow();
            Chain chain = tinyflow.toChain();
            return chain.executeForResult(argsMap);
        } else {
            throw new RuntimeException("can not find the workflow by id: " + this.workflowId);
        }
    }
 
 
    @Override
    public String toString() {
        return "AiWorkflowFunction{" +
                "workflowId=" + workflowId +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", parameters=" + Arrays.toString(parameters) +
                '}';
    }
}