Is Command on JS/TS langgraph?

Hello!! I am running into the most silly of the problems: I am not making Command work at all

// index.ts
import { Annotation, Command, END, START, StateGraph } from "@langchain/langgraph";

export const main = async () => {
	const agent = new StateGraph(Annotation.Root({}))
		.addNode("node1", (): Command => {
			console.log('Node 1: Starting execution');			
			// Send command to jump to end
			return new Command({ goto: END });
		}, { ends: [END] })
		.addNode("node2", async () => {
			console.log('Node 2: Starting execution');
		})	
		.addNode("node3", async () => {
			console.log('Node 3: Starting execution');
		})
		.addNode("node4", async () => {
			console.log('Node 4: Starting execution');
		})
		
		// Start with node1
		.addEdge(START, "node1")
		
		// Normal flow: node1 -> node2 -> node3 -> node4 -> END
		.addEdge("node1", "node2")
		.addEdge("node2", "node3")
		.addEdge("node3", "node4")
		.addEdge("node4", END)

		
		.compile();
		
	return agent.invoke({});
};

main();

my package.json

{
  "name": "playground",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@langchain/langgraph": "^1.0.1"
  }
}

also tried with "@langchain/langgraph": "^0.4.5" to no success

Anyone else seeing this??

Hi @juansanuy

This is expected: static edges still run. Command({ goto: END }) does not cancel them. It yields no branch, so the graph only finishes if no further tasks are scheduled.

Imho that is an unreal case.

I could certainly see this somewhere in the real world (when node1 holds the power to end the party):

import { Annotation, Command, END, START, StateGraph } from "@langchain/langgraph";

export const main = async () => {
	const agent = new StateGraph(Annotation.Root({}))
		.addNode("node1", () => {
			console.log('Node 1: Starting execution');
			// Send command to jump to end
			return new Command({ goto: Math.random() > 0.5 ? END : 'node2' });
		}, { ends: [END, 'node2'] })
		.addNode("node2", async () => {
			console.log('Node 2: Starting execution');
		})
		.addNode("node3", async () => {
			console.log('Node 3: Starting execution');
		})
		.addNode("node4", async () => {
			console.log('Node 4: Starting execution');
		})

		// Start with node1
		.addEdge(START, "node1")

		// Normal flow: node1 -> node2 -> node3 -> node4 -> END
		// .addEdge("node1", "node2")
		.addEdge("node2", "node3")
		.addEdge("node3", "node4")
		.addEdge("node4", END)


		.compile();

	return agent.invoke({});
};

main();

Hey @pawel-twardziak Yes I made that example up to showcase the problem I was having!!
Tho in my back and forth probably forgot to do add multiple ends or something!!
Needless to say that you made my day man!! :heart_hands:t2:

1 Like