|
| 1 | +import asyncio |
| 2 | +from typing import TypedDict |
| 3 | + |
| 4 | +from langgraph.graph import END, START, StateGraph |
| 5 | +from langgraph.types import interrupt |
| 6 | +from uipath.platform.common import InvokeProcess |
| 7 | +from uipath.platform.resume_triggers import assert_no_timeout |
| 8 | + |
| 9 | +CHILD_PROCESS_NAME = "timeout-child-agent" |
| 10 | +PROCESS_FOLDER_PATH = "Shared" |
| 11 | + |
| 12 | + |
| 13 | +class ParentState(TypedDict, total=False): |
| 14 | + message: str |
| 15 | + status: str |
| 16 | + child_result: str |
| 17 | + timeout: dict[str, object] |
| 18 | + |
| 19 | + |
| 20 | +class ChildState(TypedDict, total=False): |
| 21 | + message: str |
| 22 | + result: str |
| 23 | + |
| 24 | + |
| 25 | +def parent_node(state: ParentState) -> ParentState: |
| 26 | + child_result = interrupt( |
| 27 | + InvokeProcess( |
| 28 | + name=CHILD_PROCESS_NAME, |
| 29 | + process_folder_path=PROCESS_FOLDER_PATH, |
| 30 | + input_arguments={"message": state.get("message", "start child work")}, |
| 31 | + timeout=10, |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | + # Raises UiPathTimeoutError on timeout. |
| 36 | + assert_no_timeout(child_result) |
| 37 | + |
| 38 | + return { |
| 39 | + "status": "completed", |
| 40 | + "child_result": str(child_result), |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +async def child_node(state: ChildState) -> ChildState: |
| 45 | + await asyncio.sleep(300) |
| 46 | + return { |
| 47 | + "result": f"child completed: {state.get('message', '')}", |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +parent_builder = StateGraph(ParentState) |
| 52 | +parent_builder.add_node("parent", parent_node) |
| 53 | +parent_builder.add_edge(START, "parent") |
| 54 | +parent_builder.add_edge("parent", END) |
| 55 | +parent_graph = parent_builder.compile() |
| 56 | + |
| 57 | +child_builder = StateGraph(ChildState) |
| 58 | +child_builder.add_node("child", child_node) |
| 59 | +child_builder.add_edge(START, "child") |
| 60 | +child_builder.add_edge("child", END) |
| 61 | +child_graph = child_builder.compile() |
0 commit comments