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