fix: fix code node can not get the parameters

This commit is contained in:
Michael Yang 2024-11-06 12:00:52 +08:00
parent 8d8cbd1d32
commit d989bde13c
2 changed files with 48 additions and 6 deletions

View File

@ -28,17 +28,37 @@ public class GroovyExecNode extends CodeNode {
@Override
protected Map<String, Object> executeCode(String code, Chain chain) {
Binding binding = new Binding();
Map<String, Object> all = chain.getMemory().getAll();
if (all != null) {
all.forEach(binding::setVariable);
Map<String, Object> parameters = getParameters(chain);
if (parameters != null) {
parameters.forEach(binding::setVariable);
}
Map<String, Object> result = new HashMap<>();
binding.setVariable("_result", result);
binding.setVariable("_chain", chain);
binding.setVariable("chain", chain);
GroovyShell shell = new GroovyShell(binding);
shell.evaluate(code);
return result;
}
@Override
public String toString() {
return "GroovyExecNode{" +
"inwardEdges=" + inwardEdges +
", code='" + code + '\'' +
", description='" + description + '\'' +
", inputParameters=" + inputParameters +
", outputKeys=" + outputKeys +
", id='" + id + '\'' +
", name='" + name + '\'' +
", async=" + async +
", outwardEdges=" + outwardEdges +
", condition=" + condition +
", memory=" + memory +
", nodeStatus=" + nodeStatus +
'}';
}
}

View File

@ -30,9 +30,13 @@ public class QLExpressExecNode extends CodeNode {
Map<String, Object> result = new HashMap<>();
ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<>();
context.putAll(chain.getMemory().getAll());
context.put("chain", chain);
Map<String, Object> parameters = getParameters(chain);
if (parameters != null) context.putAll(parameters);
context.put("_chain", chain);
context.put("_result", result);
try {
runner.execute(code, context, null, true, false);
} catch (Exception e) {
@ -40,4 +44,22 @@ public class QLExpressExecNode extends CodeNode {
}
return result;
}
@Override
public String toString() {
return "QLExpressExecNode{" +
"code='" + code + '\'' +
", description='" + description + '\'' +
", inputParameters=" + inputParameters +
", outputKeys=" + outputKeys +
", id='" + id + '\'' +
", name='" + name + '\'' +
", async=" + async +
", inwardEdges=" + inwardEdges +
", outwardEdges=" + outwardEdges +
", condition=" + condition +
", memory=" + memory +
", nodeStatus=" + nodeStatus +
'}';
}
}