import { ConditionalEdgeRouter, END } from "@langchain/langgraph"; import { MessagesState } from "../state"; import { AIMessage } from "@langchain/core/messages"; export function createToolConditional(a: String, b: String): ConditionalEdgeRouter { // @ts-expect-error var genericToolConditional: ConditionalEdgeRouter = (state) => { const lastMessage = state.messages.at(-1); // Check if it's an AIMessage before accessing tool_calls if (!lastMessage || !AIMessage.isInstance(lastMessage)) { return b; } // If the LLM makes a tool call, then perform an action if (lastMessage.tool_calls?.length) { return a; } // Otherwise, we stop (reply to the user) return b; }; return genericToolConditional }