add main.py

This commit is contained in:
2026-05-25 22:22:01 +00:00
parent 53acea0bb8
commit 9907fead15
+26 -26
View File
@@ -21,41 +21,41 @@ def interrupt_node(state: GraphState) -> GraphState:
# Call interrupt; execution pauses until resumed # Call interrupt; execution pauses until resumed
interrupt(payload) interrupt(payload)
# After resume, payload will contain 'answer' # After resume, payload will contain 'answer'
# We expect the resumed payload to be the same dict with added 'answer'
# The node receives the resumed payload as its return value # The node receives the resumed payload as its return value
# So we just return the state with human_value set # We store the answer in state
return {"human_value": payload.get("answer", None), "foo": state.get("foo", "")} state["human_value"] = payload.get("answer")
return state
# Build graph # Build graph
builder = StateGraph(GraphState) builder = StateGraph(GraphState)
builder.add_node("interrupt_node", interrupt_node) builder.add_node("interrupt_node", interrupt_node)
builder.set_entry_point("interrupt_node")
builder.add_edge(START, "interrupt_node") builder.add_edge(START, "interrupt_node")
# No further nodes; graph ends after interrupt node builder.add_edge("interrupt_node", END)
# Compile with checkpoint
graph = builder.compile(checkpointer=InMemorySaver()) graph = builder.compile(checkpointer=InMemorySaver())
# Run graph with interrupt handling # Run graph with stream and handle interrupt
if __name__ == "__main__": config = {"configurable": {"thread_id": "demo"}}
config = {"configurable": {"thread_id": "hitl-demo"}}
# Initial state # First run: will pause at interrupt
state = {"human_value": None, "foo": "initial"} stream = graph.stream({}, config)
# Stream execution for chunk in stream:
for chunk in graph.stream(state, config):
if "__interrupt__" in chunk: if "__interrupt__" in chunk:
# Extract interrupt payload # Extract payload
interrupt_payload = chunk["__interrupt__"][0].value payload = chunk["__interrupt__"][0].value
# Show question to user # Show question and get answer
answer = questionary.select( answer = questionary.select(
interrupt_payload["question"], payload["question"],
choices=interrupt_payload["allow_responds"], choices=payload["allow_responds"],
).ask() ).ask()
# Add answer to payload and resume # Add answer to payload
interrupt_payload["answer"] = answer payload["answer"] = answer
# Resume graph # Resume graph
for resume_chunk in graph.stream(Command(resume=interrupt_payload), config): stream = graph.stream(Command(resume=payload), config)
# Print final state when graph finishes continue
if "node" in resume_chunk: # Print normal output
print("Final state:", resume_chunk["node"]) print(chunk)
else:
# Normal output (none expected here) # After stream ends, print final state
pass print("Final state:", stream.final_state)