17
17
18
18
import java .util .ArrayList ;
19
19
import java .util .HashMap ;
20
- import java .util .LinkedHashMap ;
21
20
import java .util .List ;
22
21
import java .util .Map ;
22
+ import java .util .Optional ;
23
+ import java .util .stream .Collectors ;
23
24
24
25
import com .alibaba .cloud .ai .graph .OverAllState ;
25
26
import com .alibaba .cloud .ai .graph .action .NodeAction ;
26
27
import com .alibaba .cloud .ai .graph .node .code .entity .CodeBlock ;
27
28
import com .alibaba .cloud .ai .graph .node .code .entity .CodeExecutionConfig ;
28
29
import com .alibaba .cloud .ai .graph .node .code .entity .CodeExecutionResult ;
29
30
import com .alibaba .cloud .ai .graph .node .code .entity .CodeLanguage ;
31
+ import com .alibaba .cloud .ai .graph .node .code .entity .CodeParam ;
32
+ import com .alibaba .cloud .ai .graph .node .code .entity .CodeStyle ;
30
33
import com .alibaba .cloud .ai .graph .node .code .entity .RunnerAndPreload ;
31
34
import com .alibaba .cloud .ai .graph .node .code .javascript .NodeJsTemplateTransformer ;
32
35
import com .alibaba .cloud .ai .graph .node .code .python3 .Python3TemplateTransformer ;
@@ -47,10 +50,12 @@ public class CodeExecutorNodeAction implements NodeAction {
47
50
48
51
private final CodeExecutionConfig codeExecutionConfig ;
49
52
50
- private Map < String , Object > params ;
53
+ private final List < CodeParam > params ;
51
54
52
55
private final String outputKey ;
53
56
57
+ private final CodeStyle style ;
58
+
54
59
private static final Map <CodeLanguage , TemplateTransformer > CODE_TEMPLATE_TRANSFORMERS = Map .of (
55
60
CodeLanguage .PYTHON3 , new Python3TemplateTransformer (), CodeLanguage .PYTHON ,
56
61
new Python3TemplateTransformer (), CodeLanguage .JAVASCRIPT , new NodeJsTemplateTransformer (),
@@ -61,24 +66,25 @@ CodeLanguage.PYTHON3, new Python3TemplateTransformer(), CodeLanguage.PYTHON,
61
66
CodeLanguage .PYTHON3 .getValue (), CodeLanguage .PYTHON , CodeLanguage .PYTHON .getValue (), CodeLanguage .JAVA ,
62
67
CodeLanguage .JAVA .getValue ());
63
68
64
- public CodeExecutorNodeAction (CodeExecutor codeExecutor , String codeLanguage , String code ,
65
- CodeExecutionConfig config , Map < String , Object > params , String outputKey ) {
69
+ public CodeExecutorNodeAction (CodeExecutor codeExecutor , String codeLanguage , String code , CodeStyle style ,
70
+ CodeExecutionConfig config , List < CodeParam > params , String outputKey ) {
66
71
this .codeExecutor = codeExecutor ;
67
72
this .codeLanguage = codeLanguage ;
73
+ this .style = style ;
68
74
this .code = code ;
69
75
this .codeExecutionConfig = config ;
70
76
this .params = params ;
71
77
this .outputKey = outputKey ;
72
78
}
73
79
74
- private Map <String , Object > executeWorkflowCodeTemplate (CodeLanguage language , String code , List < Object > inputs )
75
- throws Exception {
80
+ private Map <String , Object > executeWorkflowCodeTemplate (CodeLanguage language , String code ,
81
+ Map < String , Object > inputs ) throws Exception {
76
82
TemplateTransformer templateTransformer = CODE_TEMPLATE_TRANSFORMERS .get (language );
77
83
if (templateTransformer == null ) {
78
84
throw new RuntimeException ("Unsupported language: " + language );
79
85
}
80
86
81
- RunnerAndPreload runnerAndPreload = templateTransformer .transformCaller (code , inputs );
87
+ RunnerAndPreload runnerAndPreload = templateTransformer .transformCaller (code , inputs , style );
82
88
String response = executeCode (language , runnerAndPreload .preloadScript (), runnerAndPreload .runnerScript ());
83
89
84
90
return templateTransformer .transformResponse (response );
@@ -100,12 +106,12 @@ private String executeCode(CodeLanguage language, String preloadScript, String c
100
106
101
107
@ Override
102
108
public Map <String , Object > apply (OverAllState state ) throws Exception {
103
- List < Object > inputs = new ArrayList <>( 10 );
104
- if ( params != null && ! params . isEmpty ()) {
105
- for ( String key : params . keySet ()) {
106
- inputs . add ( state . data (). get (( String ) params . get ( key )));
107
- }
108
- }
109
+ Map < String , Object > inputs = Optional . ofNullable ( params )
110
+ . orElse ( List . of ())
111
+ . stream ()
112
+ . collect ( Collectors . toUnmodifiableMap ( CodeParam :: argName , param -> Optional . ofNullable ( param . value ())
113
+ . or (() -> StringUtils . hasText ( param . stateKey ()) ? state . value ( param . stateKey ()) : Optional . empty ())
114
+ . orElseThrow (() -> new IllegalStateException ( "param has no value and legal key!" ))));
109
115
Map <String , Object > resultObjectMap = executeWorkflowCodeTemplate (CodeLanguage .fromValue (codeLanguage ), code ,
110
116
inputs );
111
117
Map <String , Object > updatedState = new HashMap <>();
@@ -127,13 +133,16 @@ public static class Builder {
127
133
128
134
private String code ;
129
135
136
+ private CodeStyle style ;
137
+
130
138
private CodeExecutionConfig config ;
131
139
132
- private Map < String , Object > params ;
140
+ private List < CodeParam > params ;
133
141
134
142
private String outputKey ;
135
143
136
144
public Builder () {
145
+ style = CodeStyle .EXPLICIT_PARAMETERS ;
137
146
}
138
147
139
148
public Builder codeExecutor (CodeExecutor codeExecutor ) {
@@ -151,13 +160,18 @@ public Builder code(String code) {
151
160
return this ;
152
161
}
153
162
163
+ public Builder codeStyle (CodeStyle style ) {
164
+ this .style = style ;
165
+ return this ;
166
+ }
167
+
154
168
public Builder config (CodeExecutionConfig config ) {
155
169
this .config = config ;
156
170
return this ;
157
171
}
158
172
159
- public Builder params (Map < String , String > params ) {
160
- this .params = new LinkedHashMap <> (params );
173
+ public Builder params (List < CodeParam > params ) {
174
+ this .params = List . copyOf (params );
161
175
return this ;
162
176
}
163
177
@@ -167,7 +181,7 @@ public Builder outputKey(String outputKey) {
167
181
}
168
182
169
183
public CodeExecutorNodeAction build () {
170
- return new CodeExecutorNodeAction (codeExecutor , codeLanguage , code , config , params , outputKey );
184
+ return new CodeExecutorNodeAction (codeExecutor , codeLanguage , code , style , config , params , outputKey );
171
185
}
172
186
173
187
}
0 commit comments