@@ -201,17 +201,7 @@ public void setFinishPoint(String finishPoint) {
201
201
* @throws GraphStateException if the node identifier is invalid or the node already exists
202
202
*/
203
203
public StateGraph <State > addNode (String id , AsyncNodeAction <State > action ) throws GraphStateException {
204
- if (Objects .equals (id , END )) {
205
- throw Errors .invalidNodeIdentifier .exception (END );
206
- }
207
- Node <State > node = new Node <State >(id , action );
208
-
209
- if (nodes .contains (node )) {
210
- throw Errors .duplicateNodeError .exception (id );
211
- }
212
-
213
- nodes .add (node );
214
- return this ;
204
+ return addNode ( id , AsyncNodeActionWithConfig .of (action ) );
215
205
}
216
206
217
207
/**
@@ -225,7 +215,7 @@ public StateGraph<State> addNode(String id, AsyncNodeActionWithConfig<State> act
225
215
if (Objects .equals (id , END )) {
226
216
throw Errors .invalidNodeIdentifier .exception (END );
227
217
}
228
- Node <State > node = new Node <State >(id , actionWithConfig );
218
+ Node <State > node = new Node <>(id , ( config ) -> actionWithConfig );
229
219
230
220
if (nodes .contains (node )) {
231
221
throw Errors .duplicateNodeError .exception (id );
@@ -244,9 +234,43 @@ public StateGraph<State> addNode(String id, AsyncNodeActionWithConfig<State> act
244
234
* @param subGraph the compiled subgraph to be added
245
235
* @return this state graph instance
246
236
* @throws GraphStateException if the node identifier is invalid or the node already exists
237
+ * @Deprecated This method is deprecated because since the subgraph's state with the parent graph, the compilation
238
+ * must be done with the same compile config of the parent to avoid unintended side effects.
239
+ * Use {@link #addSubgraph(String, StateGraph)} instead.
247
240
*/
241
+ @ Deprecated
248
242
public StateGraph <State > addSubgraph (String id , CompiledGraph <State > subGraph ) throws GraphStateException {
249
- return addNode (id , new SubgraphNodeAction <State >(subGraph ) );
243
+ return this .addSubgraph ( id , subGraph .stateGraph );
244
+ }
245
+
246
+ /**
247
+ * Adds a subgraph to the state graph by creating a node with the specified identifier.
248
+ * This implies that Subgraph share the same state with parent graph
249
+ *
250
+ * @param id the identifier of the node representing the subgraph
251
+ * @param subGraph the subgraph to be added. it will be compiled on compilation of the parent
252
+ * @return this state graph instance
253
+ * @throws GraphStateException if the node identifier is invalid or the node already exists
254
+ */
255
+ public StateGraph <State > addSubgraph (String id , StateGraph <State > subGraph ) throws GraphStateException {
256
+ if (Objects .equals (id , END )) {
257
+ throw Errors .invalidNodeIdentifier .exception (END );
258
+ }
259
+ var node = new Node <>(id , ( config ) -> {
260
+ try {
261
+ return new SubgraphNodeAction <>(subGraph , config );
262
+ } catch (GraphStateException e ) {
263
+ throw new RuntimeException (e );
264
+ }
265
+ });
266
+
267
+ if (nodes .contains (node )) {
268
+ throw Errors .duplicateNodeError .exception (id );
269
+ }
270
+
271
+ nodes .add (node );
272
+ return this ;
273
+
250
274
}
251
275
252
276
/**
@@ -266,7 +290,7 @@ public StateGraph<State> addEdge(String sourceId, String targetId) throws GraphS
266
290
return this ;
267
291
}
268
292
269
- Edge <State > edge = new Edge <State >(sourceId , new EdgeValue <>(targetId , null ));
293
+ Edge <State > edge = new Edge <>(sourceId , new EdgeValue <>(targetId , null ));
270
294
271
295
if (edges .contains (edge )) {
272
296
throw Errors .duplicateEdgeError .exception (sourceId );
0 commit comments