Skip to content

Commit 8ff0cc8

Browse files
committed
docs(AgentExecutor.java): added comprehensive documentation
1 parent 17386f5 commit 8ff0cc8

File tree

1 file changed

+123
-25
lines changed

1 file changed

+123
-25
lines changed

agent-executor/src/main/java/org/bsc/langgraph4j/agentexecutor/AgentExecutor.java

+123-25
Original file line numberDiff line numberDiff line change
@@ -25,88 +25,185 @@
2525
import static org.bsc.langgraph4j.action.AsyncEdgeAction.edge_async;
2626
import static org.bsc.langgraph4j.action.AsyncNodeAction.node_async;
2727

28+
/**
29+
* Interface representing an Agent Executor.
30+
*/
2831
public interface AgentExecutor {
2932

33+
/**
34+
* Represents the state of an agent.
35+
*/
3036
class State extends AgentState {
3137
static Map<String, Channel<?>> SCHEMA = Map.of(
3238
"intermediate_steps", AppenderChannel.<IntermediateStep>of(ArrayList::new)
3339
);
3440

41+
/**
42+
* Constructs a new State with the given initialization data.
43+
*
44+
* @param initData the initialization data
45+
*/
3546
public State(Map<String, Object> initData) {
3647
super(initData);
3748
}
3849

50+
/**
51+
* Retrieves the input value.
52+
*
53+
* @return an Optional containing the input value if present
54+
*/
3955
public Optional<String> input() {
4056
return value("input");
4157
}
58+
59+
/**
60+
* Retrieves the agent outcome.
61+
*
62+
* @return an Optional containing the agent outcome if present
63+
*/
4264
public Optional<AgentOutcome> agentOutcome() {
4365
return value("agent_outcome");
4466
}
67+
68+
/**
69+
* Retrieves the list of intermediate steps.
70+
*
71+
* @return a list of intermediate steps
72+
*/
4573
public List<IntermediateStep> intermediateSteps() {
4674
return this.<List<IntermediateStep>>value("intermediate_steps").orElseGet(ArrayList::new);
4775
}
4876
}
4977

78+
/**
79+
* Enum representing different serializers for the agent state.
80+
*/
5081
enum Serializers {
5182

52-
STD( new STDStateSerializer() ),
53-
JSON( new JSONStateSerializer() );
83+
STD(new STDStateSerializer()),
84+
JSON(new JSONStateSerializer());
5485

5586
private final StateSerializer<AgentExecutor.State> serializer;
5687

88+
/**
89+
* Constructs a new Serializers enum with the specified serializer.
90+
*
91+
* @param serializer the state serializer
92+
*/
5793
Serializers(StateSerializer<AgentExecutor.State> serializer) {
5894
this.serializer = serializer;
5995
}
6096

97+
/**
98+
* Retrieves the state serializer.
99+
*
100+
* @return the state serializer
101+
*/
61102
public StateSerializer<AgentExecutor.State> object() {
62103
return serializer;
63104
}
64-
65105
}
66106

107+
/**
108+
* Builder class for constructing a graph of agent execution.
109+
*/
67110
class GraphBuilder {
68111
private StreamingChatLanguageModel streamingChatLanguageModel;
69112
private ChatLanguageModel chatLanguageModel;
70113
private final ToolNode.Builder toolNodeBuilder = ToolNode.builder();
71114
private StateSerializer<State> stateSerializer;
72115

116+
/**
117+
* Sets the chat language model for the graph builder.
118+
*
119+
* @param chatLanguageModel the chat language model
120+
* @return the updated GraphBuilder instance
121+
*/
73122
public GraphBuilder chatLanguageModel(ChatLanguageModel chatLanguageModel) {
74123
this.chatLanguageModel = chatLanguageModel;
75124
return this;
76125
}
126+
127+
/**
128+
* Sets the streaming chat language model for the graph builder.
129+
*
130+
* @param streamingChatLanguageModel the streaming chat language model
131+
* @return the updated GraphBuilder instance
132+
*/
77133
public GraphBuilder chatLanguageModel(StreamingChatLanguageModel streamingChatLanguageModel) {
78134
this.streamingChatLanguageModel = streamingChatLanguageModel;
79135
return this;
80136
}
137+
138+
/**
139+
* Sets the objects with tools for the graph builder (deprecated).
140+
*
141+
* @param objectsWithTools the list of objects with tools
142+
* @return the updated GraphBuilder instance
143+
*/
81144
@Deprecated
82145
public GraphBuilder objectsWithTools(List<Object> objectsWithTools) {
83146
objectsWithTools.forEach(toolNodeBuilder::specification);
84147
return this;
85148
}
149+
150+
/**
151+
* Sets the tool specification for the graph builder.
152+
*
153+
* @param objectsWithTool the tool specification
154+
* @return the updated GraphBuilder instance
155+
*/
86156
public GraphBuilder toolSpecification(Object objectsWithTool) {
87-
toolNodeBuilder.specification( objectsWithTool );
157+
toolNodeBuilder.specification(objectsWithTool);
88158
return this;
89159
}
160+
161+
/**
162+
* Sets the tool specification with executor for the graph builder.
163+
*
164+
* @param spec the tool specification
165+
* @param executor the tool executor
166+
* @return the updated GraphBuilder instance
167+
*/
90168
public GraphBuilder toolSpecification(ToolSpecification spec, ToolExecutor executor) {
91-
toolNodeBuilder.specification( spec, executor );
169+
toolNodeBuilder.specification(spec, executor);
92170
return this;
93171
}
172+
173+
/**
174+
* Sets the tool specification for the graph builder.
175+
*
176+
* @param toolSpecifications the tool specifications
177+
* @return the updated GraphBuilder instance
178+
*/
94179
public GraphBuilder toolSpecification(ToolNode.Specification toolSpecifications) {
95-
toolNodeBuilder.specification( toolSpecifications );
180+
toolNodeBuilder.specification(toolSpecifications);
96181
return this;
97182
}
98183

184+
/**
185+
* Sets the state serializer for the graph builder.
186+
*
187+
* @param stateSerializer the state serializer
188+
* @return the updated GraphBuilder instance
189+
*/
99190
public GraphBuilder stateSerializer(StateSerializer<State> stateSerializer) {
100191
this.stateSerializer = stateSerializer;
101192
return this;
102193
}
103194

195+
/**
196+
* Builds the state graph.
197+
*
198+
* @return the constructed StateGraph
199+
* @throws GraphStateException if there is an error in the graph state
200+
*/
104201
public StateGraph<State> build() throws GraphStateException {
105202

106-
if( streamingChatLanguageModel != null && chatLanguageModel != null ) {
203+
if (streamingChatLanguageModel != null && chatLanguageModel != null) {
107204
throw new IllegalArgumentException("chatLanguageModel and streamingChatLanguageModel are mutually exclusive!");
108205
}
109-
if( streamingChatLanguageModel == null && chatLanguageModel == null ) {
206+
if (streamingChatLanguageModel == null && chatLanguageModel == null) {
110207
throw new IllegalArgumentException("a chatLanguageModel or streamingChatLanguageModel is required!");
111208
}
112209

@@ -115,38 +212,39 @@ public StateGraph<State> build() throws GraphStateException {
115212
var agent = Agent.builder()
116213
.chatLanguageModel(chatLanguageModel)
117214
.streamingChatLanguageModel(streamingChatLanguageModel)
118-
.tools( toolNode.toolSpecifications() )
215+
.tools(toolNode.toolSpecifications())
119216
.build();
120217

121-
if( stateSerializer == null ) {
218+
if (stateSerializer == null) {
122219
stateSerializer = Serializers.STD.object();
123220
}
124221

125-
final var callAgent = new CallAgent( agent );
126-
final var executeTools = new ExecuteTools( agent, toolNode );
127-
final EdgeAction<State> shouldContinue = (state ) ->
222+
final var callAgent = new CallAgent(agent);
223+
final var executeTools = new ExecuteTools(agent, toolNode);
224+
final EdgeAction<State> shouldContinue = (state) ->
128225
state.agentOutcome()
129-
.map(AgentOutcome::finish)
130-
.map( finish -> "end" )
131-
.orElse("continue");
226+
.map(AgentOutcome::finish)
227+
.map(finish -> "end")
228+
.orElse("continue");
132229

133230
return new StateGraph<>(State.SCHEMA, stateSerializer)
134-
.addNode( "agent", node_async( callAgent ) )
135-
.addNode( "action", node_async( executeTools ) )
136-
.addEdge(START,"agent")
231+
.addNode("agent", node_async(callAgent))
232+
.addNode("action", node_async(executeTools))
233+
.addEdge(START, "agent")
137234
.addConditionalEdges("agent",
138-
edge_async( shouldContinue ),
235+
edge_async(shouldContinue),
139236
Map.of("continue", "action", "end", END)
140237
)
141-
.addEdge("action", "agent")
142-
;
238+
.addEdge("action", "agent");
143239
}
144240
}
145241

242+
/**
243+
* Creates a new GraphBuilder instance.
244+
*
245+
* @return a new GraphBuilder
246+
*/
146247
static GraphBuilder graphBuilder() {
147248
return new GraphBuilder();
148249
}
149-
150250
}
151-
152-

0 commit comments

Comments
 (0)