Const
import { MessagesAnnotation, StateGraph } from "@langchain/langgraph";
const graph = new StateGraph(MessagesAnnotation)
.addNode(...)
...
等同于像这样手动初始化您的状态
import { BaseMessage } from "@langchain/core/messages";
import { Annotation, StateGraph, messagesStateReducer } from "@langchain/langgraph";
export const StateAnnotation = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: messagesStateReducer,
default: () => [],
}),
});
const graph = new StateGraph(StateAnnotation)
.addNode(...)
...
预构建的状态注解,用于组合返回的消息。可以处理标准消息和类似 RemoveMessage 实例的特殊修饰符。
具体来说,像这样导入和使用预构建的 MessagesAnnotation