One or more commands to update the graph's state and send messages to nodes.
Can be used to combine routing logic with state updates in lieu of conditional edges
// Define graph state constStateAnnotation = Annotation.Root({ foo:Annotation<string>, });
// Define the nodes constnodeA = async (_state: typeofStateAnnotation.State) => { console.log("Called A"); // this is a replacement for a real conditional edge function constgoto = Math.random() > .5 ? "nodeB" : "nodeC"; // note how Command allows you to BOTH update the graph state AND route to the next node returnnewCommand({ // this is the state update update: { foo:"a", }, // this is a replacement for an edge goto, }); };
// Nodes B and C are unchanged constnodeB = async (state: typeofStateAnnotation.State) => { console.log("Called B"); return { foo:state.foo + "|b", }; }
// NOTE: there are no edges between nodes A, B and C! constgraph = newStateGraph(StateAnnotation) .addNode("nodeA", nodeA, { ends: ["nodeB", "nodeC"], }) .addNode("nodeB", nodeB) .addNode("nodeC", nodeC) .addEdge("__start__", "nodeA") .compile();
awaitgraph.invoke({ foo:"" });
// Randomly oscillates between // { foo: 'a|c' } and { foo: 'a|b' }
One or more commands to update the graph's state and send messages to nodes. Can be used to combine routing logic with state updates in lieu of conditional edges
Example