用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/tomevault-io/skills-registry --skill langgraph-state命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
| name | langgraph-state |
| description | 在 LangGraph 中管理状态:模式、reducer、通道和消息传递,用于协调 Agent 执行 Use when this capability is needed. |
| metadata | {"author":"evanfang0054"} |
状态是 LangGraph 中的核心数据结构,在整个图执行期间持久存在。正确的状态管理对于构建可靠的 Agent 至关重要。
核心概念:
| 需求 | 解决方案 | 使用场景 |
|---|---|---|
| 覆盖值 | 普通 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); // { input: 'hello', processed: 'HELLO', count: 1 }
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..);
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.({
: { : },
: ,
});
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.);
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) => {
// 只更新 field1,其他不变
return { field1: "updated" };
};
const updateField2 = async (state: typeof State.State) => {
// 只更新 field2
return { field2: "also updated" };
};
const graph = new StateGraph(State)
.addNode("node1", updateField1)
.addNode("node2", updateField2)
.addEdge(, )
.(, )
.(, )
.();
result = graph.({
: ,
: ,
: ,
});
✅ 使用 Zod 定义自定义状态模式 ✅ 通过 ReducedValue 添加 reducer ✅ 创建自定义 reducer 函数 ✅ 使用内置通道 ✅ 使用 MessagesValue 进行聊天 ✅ 部分状态更新 ✅ 嵌套状态结构
❌ 编译后更改状态模式 ❌ 在节点函数外访问状态 ❌ 直接修改状态(必须返回更新) ❌ 在不同图之间共享状态
// ❌ 错误 - 数组将被覆盖
const State = new StateSchema({
items: z.array(z.string()), // 没有 reducer!
});
// 节点 1 返回: { items: ["A"] }
// 节点 2 返回: { items: ["B"] }
// 最终状态: { items: ["B"] } // A 丢失!
// ✅ 正确
import { ReducedValue } from "@langchain/langgraph";
const State = new StateSchema({
items: new ReducedValue(
z.array(z.string()).default(() => []),
{ reducer: (current, update) => current.concat(update) }
),
});
// 最终状态: { items: ["A", "B"] }
// ❌ 错误 - 返回整个状态对象
const myNode = async (state: typeof State.State) => {
state.field = "updated";
return state; // 不要这样做!
};
// ✅ 正确 - 返回部分更新
const myNode = async (state: typeof State.State) => {
return { field: "updated" };
};
// ❌ 有风险 - 没有默认处理
const State = new StateSchema({
count: z.number(), // 如果是 undefined 怎么办?
});
const increment = async (state: typeof State.State) => {
return { count: state.count + 1 }; // 如果 count undefined 可能出错
};
// ✅ 更好 - 在模式中使用默认值
const State = new StateSchema({
count: z.number().default(0),
});
// ❌ 错误 - 忘记 await
const result = graph.invoke({ input: "test" });
console.log(result.output); // undefined (Promise!)
// ✅ 正确
const result = await graph.invoke({ input: "test" });
console.log(result.output); // 可以工作!
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
基于 SOC 职业分类