Documentation Index
Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
并非每个智能体操作都应该在无人监督下运行。当智能体即将发送电子邮件、删除记录、执行金融交易或执行任何不可逆操作时,你需要人工先审核并批准该操作。人机协作(HITL)模式让你的智能体暂停执行,将待处理的操作呈现给用户,并仅在获得明确批准后才恢复执行。
中断的工作原理
LangGraph 智能体支持中断,即智能体将控制权交还给客户端的显式暂停点。当智能体触发中断时:
- 智能体停止执行并发出中断载荷
useStream 钩子通过 stream.interrupt 暴露中断信息
- 你的 UI 渲染一个包含批准/拒绝/编辑选项的审核卡片
- 用户做出决定
- 你的代码调用
stream.submit() 并传入恢复命令
- 智能体从中断处继续执行
为 HITL 设置 useStream
导入你的智能体并将 typeof myAgent 作为类型参数传递给 useStream,以获得类型安全的状态值访问:
import type { myAgent } from "./agent";
import { useStream } from "@langchain/react";
const AGENT_URL = "http://localhost:2024";
export function Chat() {
const stream = useStream<typeof myAgent>({
apiUrl: AGENT_URL,
assistantId: "human_in_the_loop",
});
const interrupt = stream.interrupt;
return (
<div>
{stream.messages.map((msg) => (
<Message key={msg.id} message={msg} />
))}
{interrupt && (
<ApprovalCard
interrupt={interrupt}
onRespond={(response) =>
stream.submit(null, { command: { resume: response } })
}
/>
)}
</div>
);
}
中断载荷
当智能体暂停时,stream.interrupt 包含一个具有以下结构的 HITLRequest:
interface HITLRequest {
actionRequests: ActionRequest[];
reviewConfigs: ReviewConfig[];
}
interface ActionRequest {
action: string;
args: Record<string, unknown>;
description?: string;
}
interface ReviewConfig {
allowedDecisions: ("approve" | "reject" | "edit" | "respond")[];
}
| 属性 | 描述 |
|---|
actionRequests | 智能体想要执行的待处理操作数组 |
actionRequests[].action | 操作名称(例如 "send_email"、"delete_record") |
actionRequests[].args | 操作的结构化参数 |
actionRequests[].description | 可选的人类可读描述,说明操作的功能 |
reviewConfigs | 每个操作的配置,控制允许哪些决策类型 |
reviewConfigs[].allowedDecisions | 显示哪些按钮:"approve"、"reject"、"edit"、"respond" |
决策类型
HITL 模式支持四种决策类型:
用户确认操作应按原样执行:
const response: HITLResponse = {
decision: "approve",
};
stream.submit(null, { command: { resume: response } });
用户拒绝操作,可附带可选的原因说明:
const response: HITLResponse = {
decision: "reject",
reason: "邮件语气太激进了,请修改。",
};
stream.submit(null, { command: { resume: response } });
当操作被拒绝时,智能体会收到拒绝原因,并可以决定如何继续。它可能会重新措辞、提出澄清性问题,或完全放弃该操作。
用户在批准之前修改操作的参数:
const response: HITLResponse = {
decision: "edit",
args: {
...originalArgs,
subject: "更新后的主题行",
body: "使用更温和语气的修改后邮件正文。",
},
};
stream.submit(null, { command: { resume: response } });
用户为”询问用户”类型的工具提供直接回复。message 成为工具结果,工具本身不会被执行:
const response: HITLResponse = {
decision: "respond",
message: "蓝色。",
};
stream.submit(null, { command: { resume: response } });
当工具本身就是人工输入的占位符时使用 respond——例如,一个 ask_user 工具提示智能体向用户收集信息。
构建 ApprovalCard
以下是一个处理所有四种决策类型的完整审批卡片组件:
function ApprovalCard({
interrupt,
onRespond,
}: {
interrupt: { value: HITLRequest };
onRespond: (response: HITLResponse) => void;
}) {
const request = interrupt.value;
const [editedArgs, setEditedArgs] = useState(
request.actionRequests[0]?.args ?? {}
);
const [rejectReason, setRejectReason] = useState("");
const [respondMessage, setRespondMessage] = useState("");
const [mode, setMode] = useState<"review" | "edit" | "reject" | "respond">("review");
const action = request.actionRequests[0];
const config = request.reviewConfigs[0];
if (!action || !config) return null;
return (
<div className="rounded-lg border-2 border-amber-300 bg-amber-50 p-4">
<h3 className="font-semibold text-amber-800">需要操作审核</h3>
<p className="mt-1 text-sm text-amber-700">
{action.description ?? `智能体想要执行:${action.action}`}
</p>
<div className="mt-3 rounded bg-white p-3 font-mono text-sm">
<pre>{JSON.stringify(action.args, null, 2)}</pre>
</div>
{mode === "review" && (
<div className="mt-4 flex gap-2">
{config.allowedDecisions.includes("approve") && (
<button
className="rounded bg-green-600 px-4 py-2 text-white"
onClick={() => onRespond({ decision: "approve" })}
>
批准
</button>
)}
{config.allowedDecisions.includes("reject") && (
<button
className="rounded bg-red-600 px-4 py-2 text-white"
onClick={() => setMode("reject")}
>
拒绝
</button>
)}
{config.allowedDecisions.includes("edit") && (
<button
className="rounded bg-blue-600 px-4 py-2 text-white"
onClick={() => setMode("edit")}
>
编辑
</button>
)}
{config.allowedDecisions.includes("respond") && (
<button
className="rounded bg-purple-600 px-4 py-2 text-white"
onClick={() => setMode("respond")}
>
回复
</button>
)}
</div>
)}
{mode === "reject" && (
<div className="mt-4 space-y-2">
<textarea
className="w-full rounded border p-2"
placeholder="拒绝原因..."
value={rejectReason}
onChange={(e) => setRejectReason(e.target.value)}
/>
<button
className="rounded bg-red-600 px-4 py-2 text-white"
onClick={() =>
onRespond({ decision: "reject", reason: rejectReason })
}
>
确认拒绝
</button>
</div>
)}
{mode === "edit" && (
<div className="mt-4 space-y-2">
<textarea
className="w-full rounded border p-2 font-mono text-sm"
value={JSON.stringify(editedArgs, null, 2)}
onChange={(e) => {
try {
setEditedArgs(JSON.parse(e.target.value));
} catch {
// 编辑时允许无效的 JSON
}
}}
/>
<button
className="rounded bg-blue-600 px-4 py-2 text-white"
onClick={() =>
onRespond({ decision: "edit", args: editedArgs })
}
>
提交编辑
</button>
</div>
)}
{mode === "respond" && (
<div className="mt-4 space-y-2">
<textarea
className="w-full rounded border p-2"
placeholder="你的回复..."
value={respondMessage}
onChange={(e) => setRespondMessage(e.target.value)}
/>
<button
className="rounded bg-purple-600 px-4 py-2 text-white"
onClick={() =>
onRespond({ decision: "respond", message: respondMessage })
}
>
发送回复
</button>
</div>
)}
</div>
);
}
恢复流程
用户做出决定后,完整的流程如下:
- 调用
stream.submit(null, { command: { resume: hitlResponse } })
useStream 钩子将恢复命令发送到 LangGraph 后端
- 智能体接收
HITLResponse 并继续执行。HITL 响应可能是以下之一:
"approve":智能体继续执行下一个操作
"reject":智能体接收拒绝原因并决定下一步
"edit":智能体使用编辑后的参数运行工具
"respond":人类的消息直接作为工具结果返回,不执行工具
- 智能体恢复流式输出时,
interrupt 属性重置为 null
你可以在单次智能体运行中链接多个 HITL 检查点。例如,智能体可能先请求批准进行搜索,然后在发送包含结果的邮件前再次请求批准。每个中断都是独立处理的。
常见用例
| 用例 | 操作 | 审核配置 |
|---|
| 发送邮件 | send_email | ["approve", "reject", "edit"] |
| 数据库写入 | update_record | ["approve", "reject"] |
| 金融交易 | transfer_funds | ["approve", "reject"] |
| 文件删除 | delete_files | ["approve", "reject"] |
| 调用外部服务 API | call_api | ["approve", "reject", "edit"] |
| 收集用户输入 | ask_user | ["respond"] |
处理多个待处理操作
一个中断可以包含多个 actionRequests,表示智能体想要同时执行多个操作。为每个操作渲染一个卡片,并在恢复之前收集所有决策:
function MultiActionReview({
interrupt,
onRespond,
}: {
interrupt: { value: HITLRequest };
onRespond: (responses: HITLResponse[]) => void;
}) {
const [decisions, setDecisions] = useState<Record<number, HITLResponse>>({});
const request = interrupt.value;
const allDecided =
Object.keys(decisions).length === request.actionRequests.length;
return (
<div className="space-y-4">
{request.actionRequests.map((action, i) => (
<SingleActionCard
key={i}
action={action}
config={request.reviewConfigs[i]}
onDecide={(response) =>
setDecisions((prev) => ({ ...prev, [i]: response }))
}
/>
))}
{allDecided && (
<button
className="rounded bg-green-600 px-4 py-2 text-white"
onClick={() =>
onRespond(
request.actionRequests.map((_, i) => decisions[i])
)
}
>
提交所有决策
</button>
)}
</div>
);
}
最佳实践
实现 HITL 工作流时请牢记以下指南:
- 展示清晰的上下文。始终显示智能体想要做什么以及为什么。包括操作描述和完整参数。
- 让批准成为最简单的路径。如果操作看起来正确,批准应该只需单击一次。将多步骤流程留给拒绝/编辑。
- 验证编辑后的参数。当用户编辑操作参数时,在发送前验证 JSON 结构。对格式错误的输入显示内联错误。
- 持久化中断状态。如果用户刷新页面,中断应该仍然可见。
useStream 通过线程的检查点来处理这一点。
- 记录所有决策。为了审计追踪,记录每个批准/拒绝/编辑决策的时间戳和做出决策的用户。
- 合理设置超时。长时间运行的智能体不应无限期地阻塞在人工审核上。考虑显示智能体已等待多长时间。