|
| 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, get_timeout, is_timeout |
| 8 | + |
| 9 | +CHILD_PROCESS_NAME = "timeout-child-agent" |
| 10 | + |
| 11 | + |
| 12 | +class ParentState(TypedDict, total=False): |
| 13 | + message: str |
| 14 | + status: str |
| 15 | + child_result: str |
| 16 | + timeout: dict[str, object] |
| 17 | + |
| 18 | + |
| 19 | +class ChildState(TypedDict, total=False): |
| 20 | + message: str |
| 21 | + result: str |
| 22 | + |
| 23 | + |
| 24 | +def parent_node(state: ParentState) -> ParentState: |
| 25 | + child_result = interrupt( |
| 26 | + InvokeProcess( |
| 27 | + name=CHILD_PROCESS_NAME, |
| 28 | + input_arguments={"message": state.get("message", "start child work")}, |
| 29 | + timeout=10, |
| 30 | + ) |
| 31 | + ) |
| 32 | + |
| 33 | + if is_timeout(child_result): |
| 34 | + timeout = get_timeout(child_result) |
| 35 | + return { |
| 36 | + "status": "timed out", |
| 37 | + "timeout": timeout or {}, |
| 38 | + } |
| 39 | + |
| 40 | + child_result = assert_no_timeout(child_result) |
| 41 | + return { |
| 42 | + "status": "completed", |
| 43 | + "child_result": str(child_result), |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +async def child_node(state: ChildState) -> ChildState: |
| 48 | + await asyncio.sleep(300) |
| 49 | + return { |
| 50 | + "result": f"child completed: {state.get('message', '')}", |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | +parent_builder = StateGraph(ParentState) |
| 55 | +parent_builder.add_node("parent", parent_node) |
| 56 | +parent_builder.add_edge(START, "parent") |
| 57 | +parent_builder.add_edge("parent", END) |
| 58 | +parent_graph = parent_builder.compile() |
| 59 | + |
| 60 | +child_builder = StateGraph(ChildState) |
| 61 | +child_builder.add_node("child", child_node) |
| 62 | +child_builder.add_edge(START, "child") |
| 63 | +child_builder.add_edge("child", END) |
| 64 | +child_graph = child_builder.compile() |
0 commit comments