|
24 | 24 | import java.util.stream.Stream;
|
25 | 25 |
|
26 | 26 | import com.alibaba.cloud.ai.studio.admin.generator.model.Variable;
|
| 27 | +import com.alibaba.cloud.ai.studio.admin.generator.model.VariableSelector; |
27 | 28 | import com.alibaba.cloud.ai.studio.admin.generator.model.VariableType;
|
28 | 29 | import com.alibaba.cloud.ai.studio.admin.generator.model.workflow.NodeType;
|
29 | 30 | import com.alibaba.cloud.ai.studio.admin.generator.model.workflow.nodedata.CodeNodeData;
|
@@ -138,7 +139,82 @@ public Boolean supportDialect(DSLDialectType dialectType) {
|
138 | 139 |
|
139 | 140 | @Override
|
140 | 141 | public CodeNodeData parse(Map<String, Object> data) throws JsonProcessingException {
|
141 |
| - return null; |
| 142 | + CodeNodeData nodeData = new CodeNodeData(); |
| 143 | + |
| 144 | + // 获取基本信息 |
| 145 | + String code = MapReadUtil.getMapDeepValue(data, String.class, "config", "node_param", "script_content"); |
| 146 | + String lang = MapReadUtil.getMapDeepValue(data, String.class, "config", "node_param", "script_type"); |
| 147 | + Boolean isRetry = Optional |
| 148 | + .ofNullable(MapReadUtil.getMapDeepValue(data, Boolean.class, "config", "node_param", "retry_config", |
| 149 | + "retry_enabled")) |
| 150 | + .orElse(false); |
| 151 | + int maxRetryCount = isRetry ? Optional |
| 152 | + .ofNullable(MapReadUtil.getMapDeepValue(data, Integer.class, "config", "node_param", "retry_config", |
| 153 | + "max_retries")) |
| 154 | + .orElse(1) : 1; |
| 155 | + int retryIntervalMs = isRetry ? Optional |
| 156 | + .ofNullable(MapReadUtil.getMapDeepValue(data, Integer.class, "config", "node_param", "retry_config", |
| 157 | + "retry_interval")) |
| 158 | + .orElse(1000) : 1000; |
| 159 | + |
| 160 | + List<Variable> outputParams = Optional |
| 161 | + .ofNullable(MapReadUtil.safeCastToListWithMap( |
| 162 | + MapReadUtil.getMapDeepValue(data, List.class, "config", "output_params"))) |
| 163 | + .orElse(List.of()) |
| 164 | + .stream() |
| 165 | + .filter(map -> map.containsKey("key")) |
| 166 | + .map(map -> new Variable(map.get("key").toString(), VariableType.OBJECT)) |
| 167 | + .toList(); |
| 168 | + List<CodeNodeData.CodeParam> inputParams = Optional |
| 169 | + .ofNullable(MapReadUtil |
| 170 | + .safeCastToListWithMap(MapReadUtil.getMapDeepValue(data, List.class, "config", "input_params"))) |
| 171 | + .orElse(List.of()) |
| 172 | + .stream() |
| 173 | + .filter(map -> map.containsKey("key") && map.containsKey("value") && map.containsKey("value_from")) |
| 174 | + .map(map -> { |
| 175 | + String key = map.get("key").toString(); |
| 176 | + Object value = map.get("value"); |
| 177 | + String valueFrom = map.get("value_from").toString(); |
| 178 | + if ("input".equalsIgnoreCase(valueFrom)) { |
| 179 | + return CodeNodeData.CodeParam.withValue(key, value); |
| 180 | + } |
| 181 | + else { |
| 182 | + // 先以Value的形式存储selector,在post阶段转换为正确的stateKey |
| 183 | + VariableSelector selector = this.varTemplateToSelector(DSLDialectType.STUDIO, |
| 184 | + value.toString()); |
| 185 | + List<String> list = List.of(selector.getNamespace(), selector.getName()); |
| 186 | + return new CodeNodeData.CodeParam(key, list, value.toString()); |
| 187 | + } |
| 188 | + }) |
| 189 | + .toList(); |
| 190 | + |
| 191 | + // 设置基本信息 |
| 192 | + nodeData.setCodeStyle(CodeNodeData.CodeStyle.GLOBAL_DICTIONARY); |
| 193 | + nodeData.setCode(code); |
| 194 | + nodeData.setCodeLanguage(lang); |
| 195 | + nodeData.setMaxRetryCount(maxRetryCount); |
| 196 | + nodeData.setRetryIntervalMs(retryIntervalMs); |
| 197 | + nodeData.setInputParams(inputParams); |
| 198 | + nodeData.setOutputs(outputParams); |
| 199 | + |
| 200 | + // 设置错误策略 |
| 201 | + String errorStrategy = MapReadUtil.getMapDeepValue(data, String.class, "config", "node_param", |
| 202 | + "try_catch_config", "strategy"); |
| 203 | + if (errorStrategy != null) { |
| 204 | + // 暂仅支持默认值 |
| 205 | + List<Map<String, Object>> defaultValueList = MapReadUtil |
| 206 | + .safeCastToListWithMap(MapReadUtil.getMapDeepValue(data, List.class, "config", "node_param", |
| 207 | + "try_catch_config", "default_values")); |
| 208 | + if (defaultValueList != null) { |
| 209 | + Map<String, Object> defaultValue = defaultValueList.stream() |
| 210 | + .filter(map -> map.containsKey("key") && map.containsKey("value")) |
| 211 | + .collect(Collectors.toUnmodifiableMap(map -> map.get("key").toString(), |
| 212 | + map -> map.get("value"), (a, b) -> b)); |
| 213 | + nodeData.setDefaultValue(defaultValue); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return nodeData; |
142 | 218 | }
|
143 | 219 |
|
144 | 220 | @Override
|
@@ -180,7 +256,7 @@ public BiConsumer<CodeNodeData, Map<String, String>> postProcessConsumer(DSLDial
|
180 | 256 | @SuppressWarnings("unchecked")
|
181 | 257 | List<String> selector = (List<String>) param.value();
|
182 | 258 | return CodeNodeData.CodeParam.withKey(param.argName(),
|
183 |
| - idToVarName.get(selector.get(0)) + "_" + selector.get(1)); |
| 259 | + idToVarName.getOrDefault(selector.get(0), selector.get(0)) + "_" + selector.get(1)); |
184 | 260 | }).toList());
|
185 | 261 | }).andThen(super.postProcessConsumer(dialectType));
|
186 | 262 | default -> super.postProcessConsumer(dialectType);
|
|
0 commit comments