• 中断图节点(graph node)的执行。此函数可用于暂停节点执行,并在图使用 Command 重新调用时返回 resume 输入的值。单个节点中可以调用多个中断,每个中断将按顺序处理。

    当调用中断时

    1. 如果有可用的 resume 值(来自之前的 Command),它将返回该值。
    2. 否则,它将抛出一个带有提供值的 GraphInterrupt
    3. 通过传递一个带有 resume 值的 Command,可以恢复图的执行。

    由于 interrupt 函数通过抛出特殊的 GraphInterrupt 错误进行传播,因此您应该避免在 interrupt 函数周围使用 try/catch 块,如果使用了,请确保在您的 catch 块中再次抛出 GraphInterrupt 错误。

    类型参数

    • I = unknown
    • R = any

    参数

    • value: I

      要包含在中断中的值。这将在 task.interrupts[].value 中可用。

    返回 R

    当图使用 Command 重新调用时提供的 resume

    示例

    // Define a node that uses multiple interrupts
    const nodeWithInterrupts = () => {
    // First interrupt - will pause execution and include {value: 1} in task values
    const answer1 = interrupt({ value: 1 });

    // Second interrupt - only called after first interrupt is resumed
    const answer2 = interrupt({ value: 2 });

    // Use the resume values
    return { myKey: answer1 + " " + answer2 };
    };

    // Resume the graph after first interrupt
    await graph.stream(new Command({ resume: "answer 1" }));

    // Resume the graph after second interrupt
    await graph.stream(new Command({ resume: "answer 2" }));
    // Final result: { myKey: "answer 1 answer 2" }

    抛出

    如果在图的上下文之外调用

    抛出

    当没有可用的 resume 值时