|
| 1 | +"""Activity from Node - Graph Definition. |
| 2 | +
|
| 3 | +This module defines a graph where a node runs in the workflow context |
| 4 | +and calls Temporal activities directly. |
| 5 | +""" |
| 6 | + |
| 7 | +from datetime import timedelta |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +from langgraph.graph import END, START, StateGraph |
| 11 | +from typing_extensions import TypedDict |
| 12 | + |
| 13 | + |
| 14 | +# ============================================================================= |
| 15 | +# State Definition |
| 16 | +# ============================================================================= |
| 17 | + |
| 18 | + |
| 19 | +class ProcessingState(TypedDict, total=False): |
| 20 | + """State for the processing graph.""" |
| 21 | + |
| 22 | + data: str |
| 23 | + validated: bool |
| 24 | + enriched_data: str |
| 25 | + final_result: str |
| 26 | + |
| 27 | + |
| 28 | +# ============================================================================= |
| 29 | +# Node Functions |
| 30 | +# ============================================================================= |
| 31 | + |
| 32 | + |
| 33 | +async def orchestrator_node(state: ProcessingState) -> ProcessingState: |
| 34 | + """Node that orchestrates multiple activity calls from the workflow. |
| 35 | +
|
| 36 | + This node runs directly in the workflow (run_in_workflow=True) so it can: |
| 37 | + - Call multiple Temporal activities |
| 38 | + - Use workflow features like timers, signals, queries |
| 39 | + - Implement complex orchestration logic |
| 40 | +
|
| 41 | + The node is sandboxed, ensuring deterministic code. |
| 42 | + """ |
| 43 | + from temporalio import workflow |
| 44 | + |
| 45 | + data = state.get("data", "") |
| 46 | + |
| 47 | + # Call validation activity |
| 48 | + is_valid = await workflow.execute_activity( |
| 49 | + "validate_data", |
| 50 | + data, |
| 51 | + start_to_close_timeout=timedelta(seconds=30), |
| 52 | + ) |
| 53 | + |
| 54 | + if not is_valid: |
| 55 | + return {"validated": False, "final_result": "Validation failed"} |
| 56 | + |
| 57 | + # Call enrichment activity |
| 58 | + enriched = await workflow.execute_activity( |
| 59 | + "enrich_data", |
| 60 | + data, |
| 61 | + start_to_close_timeout=timedelta(seconds=30), |
| 62 | + ) |
| 63 | + |
| 64 | + return {"validated": True, "enriched_data": enriched} |
| 65 | + |
| 66 | + |
| 67 | +def finalize_node(state: ProcessingState) -> ProcessingState: |
| 68 | + """Final processing node - runs as a regular activity. |
| 69 | +
|
| 70 | + This demonstrates mixing run_in_workflow nodes with regular activity nodes. |
| 71 | + """ |
| 72 | + if not state.get("validated"): |
| 73 | + return state |
| 74 | + |
| 75 | + enriched = state.get("enriched_data", "") |
| 76 | + return {"final_result": f"Processed: {enriched}"} |
| 77 | + |
| 78 | + |
| 79 | +# ============================================================================= |
| 80 | +# Graph Builder |
| 81 | +# ============================================================================= |
| 82 | + |
| 83 | + |
| 84 | +def build_activity_from_node_graph() -> Any: |
| 85 | + """Build a graph with a node that calls activities from the workflow. |
| 86 | +
|
| 87 | + The orchestrator node uses run_in_workflow=True to execute directly |
| 88 | + in the workflow context, allowing it to call Temporal activities. |
| 89 | + """ |
| 90 | + from temporalio.contrib.langgraph import temporal_node_metadata |
| 91 | + |
| 92 | + graph = StateGraph(ProcessingState) |
| 93 | + |
| 94 | + # Orchestrator runs in workflow to call activities |
| 95 | + graph.add_node( |
| 96 | + "orchestrator", |
| 97 | + orchestrator_node, |
| 98 | + metadata=temporal_node_metadata(run_in_workflow=True), |
| 99 | + ) |
| 100 | + |
| 101 | + # Finalize runs as a regular activity |
| 102 | + graph.add_node("finalize", finalize_node) |
| 103 | + |
| 104 | + graph.add_edge(START, "orchestrator") |
| 105 | + graph.add_edge("orchestrator", "finalize") |
| 106 | + graph.add_edge("finalize", END) |
| 107 | + |
| 108 | + return graph.compile() |
0 commit comments