Skip to content

Commit

Permalink
add docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Dec 10, 2024
1 parent 949db54 commit 2fc5b3d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions libs/langgraph/src/interrupt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,45 @@ import {
} from "./constants.js";
import { PregelScratchpad } from "./pregel/types.js";

/**
* Interrupts the execution of a graph node.
* This function can be used to pause execution of a node, and return the value of the `resume`
* input when the graph is re-invoked using `Command`.
* Multiple interrupts can be called within a single node, and each will be handled sequentially.
*
* When an interrupt is called:
* 1. If there's a `resume` value available (from a previous `Command`), it returns that value.
* 2. Otherwise, it throws a `GraphInterrupt` with the provided value
* 3. The graph can be resumed by passing a `Command` with a `resume` value
*
* @param value - The value to include in the interrupt. This will be available in task.interrupts[].value
* @returns The `resume` value provided when the graph is re-invoked with a Command
*
* @example
* ```typescript
* // 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" }
* ```
*
* @throws {Error} If called outside the context of a graph
* @throws {GraphInterrupt} When no resume value is available
*/
export function interrupt<I = unknown, R = unknown>(value: I): R {
const config: RunnableConfig | undefined =
AsyncLocalStorageProviderSingleton.getRunnableConfig();
Expand Down

0 comments on commit 2fc5b3d

Please sign in to comment.