| name | langgraph-state |
| description | 在 LangGraph 中管理状态:模式、reducer、通道和消息传递,用于协调 Agent 执行 |
| language | js |
langgraph-state (JavaScript/TypeScript)
name: langgraph-state
description: 在 LangGraph 中管理状态 - 模式、reducer、通道和消息传递,用于协调 Agent 执行
概述
状态是 LangGraph 中的核心数据结构,在整个图执行期间持久存在。正确的状态管理对于构建可靠的 Agent 至关重要。
核心概念:
- StateSchema:定义状态结构和类型
- Reducers:控制如何应用状态更新(ReducedValue)
- 通道(Channels):低级状态管理原语
- 消息传递:节点如何通过状态更新进行通信
决策表:状态更新策略
| 需求 | 解决方案 | 使用场景 |
|---|
| 覆盖值 | 普通 Zod 模式 | 简单字段如字符串 |
| 追加到列表 | ReducedValue 与 concat | 日志、累积数据 |
| 自定义逻辑 | 自定义 reducer 函数 | 复杂合并、验证 |
| 消息 | MessagesValue | 聊天应用程序 |
代码示例
基本状态管理
import { StateGraph, StateSchema, START, END } from "@langchain/langgraph";
import { z } from "zod";
const State = new StateSchema({
input: z.string(),
processed: z.string(),
count: z.number(),
});
const process = async (state: typeof State.State) => {
return {
processed: state.input.toUpperCase(),
count: state.count + 1,
};
};
const graph = new StateGraph(State)
.addNode("process", process)
.addEdge(START, "process")
.addEdge("process", END)
.compile();
const result = await graph.invoke({ input: "hello", count: 0 });
console.log(result);
带Reducer 的消息
import { StateSchema, MessagesValue, StateGraph, START, END } from "@langchain/langgraph";
import { HumanMessage, AIMessage } from "@langchain/core/messages";
const MessagesState = new StateSchema({
messages: MessagesValue,
});
const addResponse = async (state: typeof MessagesState.State) => {
const lastMessage = state.messages.at(-1);
const userMsg = lastMessage?.content || "";
return {
messages: [new AIMessage({ content: `Response to: ${userMsg}` })],
};
};
const graph = new StateGraph(MessagesState)
.addNode("respond", addResponse)
.addEdge(START, "respond")
.addEdge(, )
.();
result = graph.({
: [ ({ : })],
});
.(result..);
使用 ReducedValue 的自定义 Reducer
import { StateSchema, ReducedValue, START, END, StateGraph } from "@langchain/langgraph";
import { z } from "zod";
const State = new StateSchema({
metadata: new ReducedValue(
z.record(z.string(), z.any()).default(() => ({})),
{
inputSchema: z.record(z.string(), z.any()),
reducer: (current, update) => ({ ...current, ...update }),
}
),
data: z.string(),
});
const updateMetadata = async (state: typeof State.State) => {
return { metadata: { timestamp: "2024-01-01" } };
};
const graph = new StateGraph(State)
.addNode("update", updateMetadata)
.addEdge(, )
.(, )
.();
result = graph.({
: { : },
: ,
});
使用 ReducedValue 的列表累积
import { StateSchema, ReducedValue } from "@langchain/langgraph";
import { z } from "zod";
const State = new StateSchema({
items: new ReducedValue(
z.array(z.string()).default(() => []),
{
inputSchema: z.array(z.string()),
reducer: (current, update) => current.concat(update),
}
),
});
const addItems = async (state: typeof State.State) => {
return { items: ["new_item"] };
};
const graph = new StateGraph(State)
.addNode("add", addItems)
.addEdge(START, "add")
.addEdge("add", END)
.compile();
const result = await graph.({ : [, ] });
.(result.);
使用通道 API
import { StateGraph, LastValue, BinaryOperatorAggregate } from "@langchain/langgraph";
interface State {
counter: number;
logs: string[];
}
const graph = new StateGraph<State>({
channels: {
counter: new BinaryOperatorAggregate<number>(
(x, y) => x + y,
() => 0
),
logs: new BinaryOperatorAggregate<string[]>(
(x, y) => x.concat(y),
() => []
),
},
});
部分状态更新
import { StateSchema, StateGraph, START, END } from "@langchain/langgraph";
import { z } from "zod";
const State = new StateSchema({
field1: z.string(),
field2: z.string(),
field3: z.string(),
});
const updateField1 = async (state: typeof State.State) => {
return { field1: "updated" };
};
const updateField2 = async (state: typeof State.State) => {
return { field2: "also updated" };
};
const graph = new StateGraph(State)
.addNode("node1", updateField1)
.addNode("node2", updateField2)
.addEdge(, )
.(, )
.(, )
.();
result = graph.({
: ,
: ,
: ,
});
边界
您能够配置的
✅ 使用 Zod 定义自定义状态模式
✅ 通过 ReducedValue 添加 reducer
✅ 创建自定义 reducer 函数
✅ 使用内置通道
✅ 使用 MessagesValue 进行聊天
✅ 部分状态更新
✅ 嵌套状态结构
您不能配置的
❌ 编译后更改状态模式
❌ 在节点函数外访问状态
❌ 直接修改状态(必须返回更新)
❌ 在不同图之间共享状态
注意事项
1. 忘记为 Arrays 添加 Reducer
const State = new StateSchema({
items: z.array(z.string()),
});
import { ReducedValue } from "@langchain/langgraph";
const State = new StateSchema({
items: new ReducedValue(
z.array(z.string()).default(() => []),
{ reducer: (current, update) => current.concat(update) }
),
});
2. 状态更新必须返回 Partial
const myNode = async (state: typeof State.State) => {
state.field = "updated";
return state;
};
const myNode = async (state: typeof State.State) => {
return { field: "updated" };
};
3. 默认值
const State = new StateSchema({
count: z.number(),
});
const increment = async (state: typeof State.State) => {
return { count: state.count + 1 };
};
const State = new StateSchema({
count: z.number().default(0),
});
4. 始终 Await 节点
const result = graph.invoke({ input: "test" });
console.log(result.output);
const result = await graph.invoke({ input: "test" });
console.log(result.output);
相关链接