执行中断 #
本文档中引用的文件
目录 #
中断配置语义 #
langgraphgo 提供了三种中断配置项:InterruptBefore、InterruptAfter 和 ResumeFrom,它们定义了图执行过程中的暂停与恢复行为。
-
InterruptBefore: 当配置了
InterruptBefore时,图在执行到指定节点之前会立即中断。此时,该节点尚未执行,中断时的状态是进入该节点前的状态。此配置适用于需要在关键操作前进行人工审批的场景。 -
InterruptAfter: 当配置了
InterruptAfter时,图在执行完指定节点之后会中断。此时,该节点已经完成执行,中断时的状态是该节点执行后的状态。此配置适用于需要在某个处理步骤完成后检查结果的场景。 -
ResumeFrom: 用于指定从中断点恢复执行的起始节点。当图因中断而停止后,可以通过设置
ResumeFrom来指定从哪个节点继续执行。通常,ResumeFrom的值是中断时的节点名或其后续节点列表。
Section sources
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L238-247)
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L439-452)
- [resume_test.go](https://github.com/smallnest/langgraphgo/blob/main/graph/resume_test.go#L49-51)
- [resume_test.go](https://github.com/smallnest/langgraphgo/blob/main/graph/resume_test.go#L73-75)
中断检查执行时机 #
在 InvokeWithConfig 方法中,中断检查的执行时机遵循严格的逻辑路径。整个执行循环分为以下几个阶段:
- 初始化: 设置初始状态和当前节点列表,处理
ResumeFrom配置。 - 中断前检查 (InterruptBefore): 在执行任何节点之前,检查当前节点是否在
InterruptBefore列表中。如果是,则立即返回GraphInterrupt错误。 - 并行执行节点: 执行当前节点列表中的所有节点,每个节点在一个独立的 goroutine 中运行。
- 错误处理: 检查节点执行过程中是否有错误。如果遇到
NodeInterrupt错误,则将其转换为GraphInterrupt错误并返回。 - 状态更新: 根据节点执行结果更新图的状态。
- 确定下一节点: 根据静态边或条件边确定下一组要执行的节点。
- 中断后检查 (InterruptAfter): 在确定下一节点后,检查刚刚执行的节点是否在
InterruptAfter列表中。如果是,则返回GraphInterrupt错误,并附带下一节点列表。 - 继续循环: 更新当前节点列表,进入下一轮循环。
这种设计确保了中断检查的精确性和可预测性,使得开发者可以准确地控制图的执行流程。
Section sources
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L238-247)
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L439-452)
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L320-333)
GraphInterrupt 错误类型 #
GraphInterrupt 是一个特殊的错误类型,用于表示图执行被中断。它包含了以下字段:
- Node: 触发中断的节点名称。
- State: 中断发生时的图状态。
- NextNodes: 如果中断发生在
InterruptAfter,则包含接下来要执行的节点列表。 - InterruptValue: 如果是动态中断,则包含由
Interrupt函数提供的值。
当图执行被中断时,InvokeWithConfig 方法会返回 GraphInterrupt 错误。应用程序可以捕获此错误,检查中断原因,并根据需要更新状态或提供恢复值。恢复执行时,可以通过设置 ResumeFrom 和 ResumeValue 来继续图的执行。
Section sources
- [errors.go](https://github.com/smallnest/langgraphgo/blob/main/graph/errors.go#L24-34)
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L326-331)
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L444-449)
动态中断与人机交互 #
动态中断允许节点在运行时主动请求中断,等待外部输入。这是通过 graph.Interrupt(ctx, value) 函数实现的。当节点调用此函数时:
- 如果是首次调用,函数会返回
NodeInterrupt错误,导致图执行中断。 - 如果是在恢复执行时调用(即上下文中存在
ResumeValue),函数会直接返回ResumeValue,并继续执行。
这种机制非常适合实现人机交互(human-in-the-loop)工作流。例如,在一个审批流程中,某个节点可以调用 Interrupt 函数来等待人工审批。应用程序捕获中断后,可以将请求展示给用户,获取输入后,再通过 ResumeValue 将输入传递回图中,从而完成审批流程。
Section sources
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L43-50)
- [context.go](https://github.com/smallnest/langgraphgo/blob/main/graph/context.go#L5-16)
- [dynamic_interrupt\main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/dynamic_interrupt/main.go#L23-26)
- [human_in_the_loop\main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/human_in_the_loop/main.go#L31-41)
中断状态序列化与持久化 #
为了支持长时间运行的图和跨会话恢复,中断状态需要被序列化和持久化。最佳实践包括:
- 状态序列化: 将
GraphInterrupt中的State字段序列化为 JSON 或其他格式,并存储在数据库或文件系统中。 - 配置持久化: 同时保存中断时的配置信息,如
InterruptBefore、InterruptAfter和NextNodes。 - 恢复时重建: 在恢复执行时,从持久化存储中读取状态和配置,重建
Config对象,并设置ResumeFrom和ResumeValue。 - 使用检查点: 结合
checkpoint模块(如postgres、redis或sqlite)来自动管理状态的保存和恢复。
通过这种方式,可以实现图的持久化执行,即使在应用程序重启后也能从中断点继续执行。
Section sources
- [schema.go](https://github.com/smallnest/langgraphgo/blob/main/graph/schema.go#L62-99)
- [checkpointing.go](https://github.com/smallnest/langgraphgo/blob/main/graph/checkpointing.go)
- [postgres.go](https://github.com/smallnest/langgraphgo/blob/main/checkpoint/postgres/postgres.go)
- [redis.go](https://github.com/smallnest/langgraphgo/blob/main/checkpoint/redis/redis.go)
- [sqlite.go](https://github.com/smallnest/langgraphgo/blob/main/checkpoint/sqlite/sqlite.go)
中断生命周期时序图 #
sequenceDiagram
participant Application as 应用程序
participant Graph as 图执行引擎
participant User as 用户
Application->>Graph : InvokeWithConfig(初始状态, config)
Graph->>Graph : 执行节点直到中断点
Graph-->>Application : 返回 GraphInterrupt 错误
Application->>User : 展示中断状态和查询
User->>Application : 提供输入
Application->>Graph : InvokeWithConfig(更新状态, resumeConfig)
Graph->>Graph : 从 ResumeFrom 节点继续执行
Graph-->>Application : 返回最终结果
Diagram sources
- [graph.go](https://github.com/smallnest/langgraphgo/blob/main/graph/graph.go#L182-492)
- [resume_test.go](https://github.com/smallnest/langgraphgo/blob/main/graph/resume_test.go#L31-81)
- [dynamic_interrupt\main.go](https://github.com/smallnest/langgraphgo/blob/main/examples/dynamic_interrupt/main.go#L40-70)