|
| 1 | +"""Calculator agent demonstrating suspend/resume with RPA process invocation for calculations.""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from langgraph.constants import END, START |
| 6 | +from langgraph.graph import StateGraph |
| 7 | +from langgraph.types import interrupt |
| 8 | +from pydantic import BaseModel |
| 9 | +from uipath.platform.common import InvokeProcess |
| 10 | + |
| 11 | +# Configure logging |
| 12 | +logging.basicConfig( |
| 13 | + level=logging.INFO, |
| 14 | + format="%(asctime)s [%(levelname)s] %(message)s", |
| 15 | + datefmt="%Y-%m-%d %H:%M:%S", |
| 16 | +) |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +class Input(BaseModel): |
| 21 | + """Input for the calculator agent.""" |
| 22 | + |
| 23 | + a: int |
| 24 | + b: int |
| 25 | + operator: str |
| 26 | + |
| 27 | + |
| 28 | +class Output(BaseModel): |
| 29 | + """Output from the calculator agent.""" |
| 30 | + |
| 31 | + result: str |
| 32 | + calculation: str |
| 33 | + |
| 34 | + |
| 35 | +class State(BaseModel): |
| 36 | + """Internal state for the calculator agent.""" |
| 37 | + |
| 38 | + a: int |
| 39 | + b: int |
| 40 | + operator: str |
| 41 | + result: str = "" |
| 42 | + calculation: str = "" |
| 43 | + |
| 44 | + |
| 45 | +async def calculator_process_node(state: State) -> State: |
| 46 | + """Node that invokes an RPA calculator process and suspends execution.""" |
| 47 | + logger.info("=" * 80) |
| 48 | + logger.info("CALCULATOR NODE: Starting calculator_process_node") |
| 49 | + logger.info(f"CALCULATOR NODE: Operand A: {state.a}") |
| 50 | + logger.info(f"CALCULATOR NODE: Operand B: {state.b}") |
| 51 | + logger.info(f"CALCULATOR NODE: Operator: {state.operator}") |
| 52 | + |
| 53 | + # Create an InvokeProcess request for calculator |
| 54 | + invoke_request = InvokeProcess( |
| 55 | + name="Calculator", |
| 56 | + input_arguments={ |
| 57 | + "a": state.a, |
| 58 | + "b": state.b, |
| 59 | + "operator": state.operator, |
| 60 | + }, |
| 61 | + process_folder_path="Shared", |
| 62 | + ) |
| 63 | + |
| 64 | + logger.info( |
| 65 | + f"CALCULATOR NODE: Created InvokeProcess request: {invoke_request.model_dump()}" |
| 66 | + ) |
| 67 | + logger.info("🔴 CALCULATOR NODE: About to call interrupt() - SUSPENDING EXECUTION") |
| 68 | + logger.info("=" * 80) |
| 69 | + |
| 70 | + # Interrupt execution and capture the process output |
| 71 | + # The runtime will detect this and return SUSPENDED status |
| 72 | + # When resumed, the return value will contain the process output |
| 73 | + process_output = interrupt(invoke_request) |
| 74 | + |
| 75 | + # This code won't execute until the process completes and execution resumes |
| 76 | + logger.info("=" * 80) |
| 77 | + logger.info("🟢 CALCULATOR NODE: Execution RESUMED after interrupt()") |
| 78 | + logger.info("CALCULATOR NODE: RPA calculator process has completed") |
| 79 | + logger.info(f"CALCULATOR NODE: Process output: {process_output}") |
| 80 | + logger.info("=" * 80) |
| 81 | + |
| 82 | + # Extract result from process output |
| 83 | + if isinstance(process_output, dict): |
| 84 | + result = process_output.get("result", "") |
| 85 | + calculation = f"{state.a} {state.operator} {state.b} = {result}" |
| 86 | + else: |
| 87 | + result = str(process_output) |
| 88 | + calculation = f"{state.a} {state.operator} {state.b} = {result}" |
| 89 | + |
| 90 | + logger.info(f"CALCULATOR NODE: Calculation: {calculation}") |
| 91 | + |
| 92 | + return State( |
| 93 | + a=state.a, |
| 94 | + b=state.b, |
| 95 | + operator=state.operator, |
| 96 | + result=str(result), |
| 97 | + calculation=calculation, |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +# Build the graph |
| 102 | +builder = StateGraph(state_schema=State, input=Input, output=Output) |
| 103 | + |
| 104 | +# Add single node that invokes the calculator process |
| 105 | +builder.add_node("calculator_process", calculator_process_node) |
| 106 | + |
| 107 | +# Connect: START -> calculator_process -> END |
| 108 | +builder.add_edge(START, "calculator_process") |
| 109 | +builder.add_edge("calculator_process", END) |
| 110 | + |
| 111 | +# Compile with checkpointer (required for interrupts to work) |
| 112 | +from langgraph.checkpoint.memory import MemorySaver |
| 113 | + |
| 114 | +checkpointer = MemorySaver() |
| 115 | +graph = builder.compile(checkpointer=checkpointer) |
0 commit comments