Skip to content

Commit fffce29

Browse files
committed
refactor(StateGraph): update addSubgraph method behavior using actionFactory
- Deprecate the `addSubgraph(CompiledGraph) - Add `addSubgraph(StateGraph)` work on #60
1 parent ac0e989 commit fffce29

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

core/src/main/java/org/bsc/langgraph4j/StateGraph.java

+38-14
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,7 @@ public void setFinishPoint(String finishPoint) {
201201
* @throws GraphStateException if the node identifier is invalid or the node already exists
202202
*/
203203
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) );
215205
}
216206

217207
/**
@@ -225,7 +215,7 @@ public StateGraph<State> addNode(String id, AsyncNodeActionWithConfig<State> act
225215
if (Objects.equals(id, END)) {
226216
throw Errors.invalidNodeIdentifier.exception(END);
227217
}
228-
Node<State> node = new Node<State>(id, actionWithConfig);
218+
Node<State> node = new Node<>(id, ( config ) -> actionWithConfig);
229219

230220
if (nodes.contains(node)) {
231221
throw Errors.duplicateNodeError.exception(id);
@@ -244,9 +234,43 @@ public StateGraph<State> addNode(String id, AsyncNodeActionWithConfig<State> act
244234
* @param subGraph the compiled subgraph to be added
245235
* @return this state graph instance
246236
* @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.
247240
*/
241+
@Deprecated
248242
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+
250274
}
251275

252276
/**
@@ -266,7 +290,7 @@ public StateGraph<State> addEdge(String sourceId, String targetId) throws GraphS
266290
return this;
267291
}
268292

269-
Edge<State> edge = new Edge<State>(sourceId, new EdgeValue<>(targetId, null));
293+
Edge<State> edge = new Edge<>(sourceId, new EdgeValue<>(targetId, null));
270294

271295
if (edges.contains(edge)) {
272296
throw Errors.duplicateEdgeError.exception(sourceId);

0 commit comments

Comments
 (0)