Practical TypeScript graph with an approval pause
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn Practical TypeScript graph with an approval pause
- This is a minimal orchestration demonstration, not an insurance backend.
- Dependencies are
@langchain/langgraphandzod. - It uses the documented
StateSchemastyle; older tutorials may useAnnotation.Root. - Select compatible package versions before adopting either API.
import {
StateGraph, StateSchema, START, END,
MemorySaver, interrupt, Command,
} from "@langchain/langgraph";
import { z } from "zod";
const State = new StateSchema({
claimId: z.string(),
draftSummary: z.string(),
approved: z.boolean().default(false),
outcome: z.string().default("pending"),
});
const graph = new StateGraph(State)
.addNode("prepare", (state) => ({
draftSummary: `Review draft for claim ${state.claimId}`,
}))
.addNode("review", (state) => {
// No external side effects before this pause.
const choice = interrupt({
action: "review_claim_draft",
summary: state.draftSummary,
});
const decision = z.object({ approved: z.boolean() }).parse(choice);
return { approved: decision.approved };
})
.addNode("accepted", () => ({ outcome: "ready_for_business_validation" }))
.addNode("rejected", () => ({ outcome: "cancelled" }))
.addEdge(START, "prepare")
.addEdge("prepare", "review")
.addConditionalEdges("review", (state) =>
state.approved ? "accepted" : "rejected"
)
.addEdge("accepted", END)
.addEdge("rejected", END)
.compile({ checkpointer: new MemorySaver() });
// Demo only: production derives the thread through authorized server-side lookup.
const config = { configurable: { thread_id: "demo-claim-review-42" } };
const paused = await graph.invoke({
claimId: "C42",
draftSummary: "",
}, config);
console.log(paused.__interrupt__); // Display the review proposal safely.
// Simulates a person's decision. A real resume endpoint must authenticate it.
const resumed = await graph.invoke(
new Command({ resume: { approved: true } }), config,
);
console.log(resumed.outcome); // ready_for_business_validationStateSchema declares data. Nodes return partial updates. interrupt pauses at review; Command({resume: ...}) supplies the answer for that pause using the same thread. MemorySaver is process memory for learning, so restart loses progress. The accepted branch deliberately reports readiness; actual submission belongs in an authorized business service.
- The demo's
approvedflag illustrates routing. - In production, a forged flag must never authorize an operation: the commit service verifies the persisted approval record, reviewer identity, exact draft hash, expiry, and current permissions.
- LangGraph interrupts.
Adapted from 13_AI.MD · “Practical TypeScript graph with an approval pause”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.