Skip to content

Commit aaec023

Browse files
authored
feat(studio): support JudgeNode, ClassifierNode, AssignerNode and ParameterParsingNode for Studio DSL (#2405)
* refactor: prepare for BranchNodeDataConverter for Studio DSL * feat: support JudgeNode for Studio DSL * feat: enhance QuestionClassifierNode to support variableCategories * feat: support QuestionClassifierNode for Studio DSL and sorted import in generated code * refactor: add caseIdToName Map * feat: enhance AssignerNode (prepare for Studio DSL) * feat: support AssignerNode for Studio DSL * fix bugs * feat: enhance ParameterParsingNode * refactor: exact common code * feat: support ParameterParsingNode for Studio DSL * fix bugs
1 parent e0c45d4 commit aaec023

33 files changed

+1714
-1428
lines changed

spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/node/AssignerNode.java

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,25 @@ public class AssignerNode implements NodeAction {
3333

3434
public enum WriteMode {
3535

36-
OVER_WRITE, APPEND, CLEAR
36+
OVER_WRITE, APPEND, CLEAR, INPUT_CONSTANT
3737

3838
}
3939

4040
/**
4141
* description of a single assignment operation
4242
*/
43-
public static class AssignItem {
44-
45-
private final String targetKey;
46-
47-
private final String inputKey;
48-
49-
private final WriteMode writeMode;
50-
43+
public record AssignItem(String targetKey, String inputKey, WriteMode writeMode, Object inputValue) {
5144
public AssignItem(String targetKey, String inputKey, WriteMode writeMode) {
52-
this.targetKey = targetKey;
53-
this.inputKey = inputKey;
54-
this.writeMode = writeMode;
55-
}
56-
57-
public String getTargetKey() {
58-
return targetKey;
45+
this(targetKey, inputKey, writeMode, null);
5946
}
6047

61-
public String getInputKey() {
62-
return inputKey;
48+
public AssignItem(String targetKey, Object inputValue) {
49+
this(targetKey, null, WriteMode.INPUT_CONSTANT, inputValue);
6350
}
6451

65-
public WriteMode getWriteMode() {
66-
return writeMode;
52+
public AssignItem(String targetKey) {
53+
this(targetKey, null, WriteMode.OVER_WRITE);
6754
}
68-
6955
}
7056

7157
private final List<AssignItem> items;
@@ -88,15 +74,12 @@ public AssignerNode(String targetKey, String inputKey, WriteMode writeMode) {
8874
public Map<String, Object> apply(OverAllState state) {
8975
Map<String, Object> updates = new HashMap<>();
9076
for (AssignItem item : items) {
91-
Object value = state.value(item.inputKey).orElse(null);
92-
Object targetValue = state.value(item.targetKey).orElse(null);
93-
Object result = null;
94-
95-
switch (item.writeMode) {
96-
case OVER_WRITE:
97-
result = value;
98-
break;
99-
case APPEND:
77+
Object value = state.value(item.inputKey()).orElse(null);
78+
Object targetValue = state.value(item.targetKey()).orElse(null);
79+
80+
Object result = switch (item.writeMode()) {
81+
case OVER_WRITE -> value;
82+
case APPEND -> {
10083
if (targetValue instanceof List && value != null) {
10184
List<Object> newList = new ArrayList<>((List<?>) targetValue);
10285
if (value instanceof Collection<?> col) {
@@ -105,35 +88,41 @@ public Map<String, Object> apply(OverAllState state) {
10588
else {
10689
newList.add(value);
10790
}
108-
result = newList;
91+
yield newList;
10992
}
11093
else if (value != null) {
11194
if (value instanceof Collection<?> col) {
112-
result = new ArrayList<>(col);
95+
yield new ArrayList<>(col);
11396
}
11497
else {
115-
result = new ArrayList<>(List.of(value));
98+
yield new ArrayList<>(List.of(value));
11699
}
117100
}
118-
break;
119-
case CLEAR:
101+
else {
102+
throw new IllegalArgumentException(
103+
"Cannot append to non-list value for key: " + item.targetKey());
104+
}
105+
}
106+
case CLEAR -> {
120107
if (targetValue instanceof List) {
121-
result = new ArrayList<>();
108+
yield new ArrayList<>();
122109
}
123110
else if (targetValue instanceof Map) {
124-
result = new HashMap<>();
111+
yield new HashMap<>();
125112
}
126113
else if (targetValue instanceof String) {
127-
result = "";
114+
yield "";
128115
}
129116
else if (targetValue instanceof Number) {
130-
result = 0;
117+
yield 0;
131118
}
132119
else {
133-
result = null;
120+
yield null;
134121
}
135-
break;
136-
}
122+
}
123+
case INPUT_CONSTANT -> item.inputValue();
124+
default -> throw new IllegalArgumentException("Invalid write mode: " + item.writeMode());
125+
};
137126
updates.put(item.targetKey, result);
138127
}
139128
return updates;
@@ -146,13 +135,28 @@ public static Builder builder() {
146135

147136
public static class Builder {
148137

149-
private final List<AssignItem> items = new ArrayList<>();
138+
private List<AssignItem> items = new ArrayList<>();
139+
140+
public Builder setItems(List<AssignItem> items) {
141+
this.items = new ArrayList<>(items);
142+
return this;
143+
}
150144

151145
public Builder addItem(String targetKey, String inputKey, WriteMode writeMode) {
152146
items.add(new AssignItem(targetKey, inputKey, writeMode));
153147
return this;
154148
}
155149

150+
public Builder addConst(String targetKey, Object inputValue) {
151+
items.add(new AssignItem(targetKey, inputValue));
152+
return this;
153+
}
154+
155+
public Builder addClear(String targetKey) {
156+
items.add(new AssignItem(targetKey));
157+
return this;
158+
}
159+
156160
public Builder addItem(AssignItem item) {
157161
items.add(item);
158162
return this;

0 commit comments

Comments
 (0)