add main.py

This commit is contained in:
2026-05-25 22:22:01 +00:00
parent 53acea0bb8
commit 9907fead15
+30 -30
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 payload
# Extract interrupt payload payload = chunk["__interrupt__"][0].value
interrupt_payload = chunk["__interrupt__"][0].value # Show question and get answer
# Show question to user answer = questionary.select(
answer = questionary.select( payload["question"],
interrupt_payload["question"], choices=payload["allow_responds"],
choices=interrupt_payload["allow_responds"], ).ask()
).ask() # Add answer to payload
# Add answer to payload and resume payload["answer"] = answer
interrupt_payload["answer"] = answer # Resume graph
# Resume graph stream = graph.stream(Command(resume=payload), config)
for resume_chunk in graph.stream(Command(resume=interrupt_payload), config): continue
# Print final state when graph finishes # Print normal output
if "node" in resume_chunk: print(chunk)
print("Final state:", resume_chunk["node"])
else: # After stream ends, print final state
# Normal output (none expected here) print("Final state:", stream.final_state)
pass