packagecom.yuliang.aicodemother.langgraph4j.demo;importorg.bsc.langgraph4j.action.NodeAction;importjava.util.Collections;importjava.util.List;importjava.util.Map;// Node that adds a greetingclassGreeterNodeimplementsNodeAction<SimpleState>{@OverridepublicMap<String,Object>apply(SimpleState state){System.out.println("GreeterNode executing. Current messages: "+ state.messages());returnMap.of(SimpleState.MESSAGES_KEY,"Hello from GreeterNode!");}}// Node that adds a responseclassResponderNodeimplementsNodeAction<SimpleState>{@OverridepublicMap<String,Object>apply(SimpleState state){System.out.println("ResponderNode executing. Current messages: "+ state.messages());List<String> currentMessages = state.messages();if(currentMessages.contains("Hello from GreeterNode!")){returnMap.of(SimpleState.MESSAGES_KEY,"Acknowledged greeting!");}returnMap.of(SimpleState.MESSAGES_KEY,"No greeting found.");}}packagecom.yuliang.aicodemother.langgraph4j.demo;importorg.bsc.langgraph4j.state.AgentState;importorg.bsc.langgraph4j.state.Channels;importorg.bsc.langgraph4j.state.Channel;importjava.util.*;// Define the state for our graphclassSimpleStateextendsAgentState{publicstaticfinalString MESSAGES_KEY ="messages";// Define the schema for the state.// MESSAGES_KEY will hold a list of strings, and new messages will be appended.publicstaticfinalMap<String,Channel<?>> SCHEMA =Map.of(
MESSAGES_KEY,Channels.appender(ArrayList::new));publicSimpleState(Map<String,Object> initData){super(initData);}publicList<String>messages(){returnthis.<List<String>>value("messages").orElse(List.of());}}packagecom.yuliang.aicodemother.langgraph4j.demo;importorg.bsc.langgraph4j.GraphRepresentation;importorg.bsc.langgraph4j.StateGraph;importorg.bsc.langgraph4j.GraphStateException;importstaticorg.bsc.langgraph4j.action.AsyncNodeAction.node_async;importstaticorg.bsc.langgraph4j.StateGraph.START;importstaticorg.bsc.langgraph4j.StateGraph.END;importjava.util.List;importjava.util.Map;publicclassSimpleGraphApp{publicstaticvoidmain(String[] args)throwsGraphStateException{// Initialize nodesGreeterNode greeterNode =newGreeterNode();ResponderNode responderNode =newResponderNode();// Define the graph structurevar stateGraph =newStateGraph<>(SimpleState.SCHEMA, initData ->newSimpleState(initData)).addNode("greeter",node_async(greeterNode)).addNode("responder",node_async(responderNode))// Define edges.addEdge(START,"greeter")// Start with the greeter node.addEdge("greeter","responder").addEdge("responder", END)// End after the responder node;// Compile the graphvar compiledGraph = stateGraph.compile();GraphRepresentation demo = stateGraph.getGraph(GraphRepresentation.Type.MERMAID,"demo");System.out.println(demo.toString());// Run the graph// The `stream` method returns an AsyncGenerator.// For simplicity, we'll collect results. In a real app, you might process them as they arrive.// Here, the final state after execution is the item of interest.for(var item : compiledGraph.stream(Map.of(SimpleState.MESSAGES_KEY,"Let's, begin!"))){System.out.println( item );}}}