|
| 1 | +""" |
| 2 | +Condition with User Decision HITL Example |
| 3 | +
|
| 4 | +This example demonstrates how to use HITL with a Condition component, |
| 5 | +allowing the user to decide which branch to execute at runtime. |
| 6 | +
|
| 7 | +When `requires_confirmation=True` on a Condition, the `on_reject` setting |
| 8 | +controls what happens when the user rejects: |
| 9 | +
|
| 10 | +- on_reject="else" (default): Execute `else_steps` if provided, otherwise skip |
| 11 | +- on_reject="skip": Skip the entire condition (both branches) |
| 12 | +- on_reject="cancel": Cancel the workflow |
| 13 | +
|
| 14 | +This is useful for: |
| 15 | +- User-driven decision points |
| 16 | +- Interactive branching workflows |
| 17 | +- A/B testing with human judgment |
| 18 | +""" |
| 19 | + |
| 20 | +from agno.db.sqlite import SqliteDb |
| 21 | +from agno.workflow.condition import Condition |
| 22 | +from agno.workflow.step import Step |
| 23 | +from agno.workflow.types import OnReject, StepInput, StepOutput |
| 24 | +from agno.workflow.workflow import Workflow |
| 25 | + |
| 26 | + |
| 27 | +# ============================================================ |
| 28 | +# Step functions |
| 29 | +# ============================================================ |
| 30 | +def analyze_data(step_input: StepInput) -> StepOutput: |
| 31 | + """Analyze the data.""" |
| 32 | + user_query = step_input.input or "data" |
| 33 | + return StepOutput( |
| 34 | + content=f"Analysis complete for '{user_query}':\n" |
| 35 | + "- Found potential issues that may require detailed review\n" |
| 36 | + "- Quick summary is available\n\n" |
| 37 | + "Would you like to proceed with detailed analysis?" |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def detailed_analysis(step_input: StepInput) -> StepOutput: |
| 42 | + """Perform detailed analysis (if branch).""" |
| 43 | + return StepOutput( |
| 44 | + content="Detailed Analysis Results:\n" |
| 45 | + "- Comprehensive review completed\n" |
| 46 | + "- All edge cases examined\n" |
| 47 | + "- Full report generated\n" |
| 48 | + "- Processing time: 10 minutes" |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def quick_summary(step_input: StepInput) -> StepOutput: |
| 53 | + """Provide quick summary (else branch).""" |
| 54 | + return StepOutput( |
| 55 | + content="Quick Summary:\n" |
| 56 | + "- Basic metrics computed\n" |
| 57 | + "- Key highlights identified\n" |
| 58 | + "- Processing time: 1 minute" |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def generate_report(step_input: StepInput) -> StepOutput: |
| 63 | + """Generate final report.""" |
| 64 | + previous_content = step_input.previous_step_content or "No analysis" |
| 65 | + return StepOutput( |
| 66 | + content=f"=== FINAL REPORT ===\n\n{previous_content}\n\n" |
| 67 | + "Report generated successfully." |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def run_demo(on_reject_mode: OnReject, demo_name: str): |
| 72 | + """Run a demo with the specified on_reject mode.""" |
| 73 | + print("\n" + "=" * 60) |
| 74 | + print(f"Demo: {demo_name}") |
| 75 | + print(f"on_reject = {on_reject_mode.value}") |
| 76 | + print("=" * 60) |
| 77 | + |
| 78 | + # Define the steps |
| 79 | + analyze_step = Step(name="analyze_data", executor=analyze_data) |
| 80 | + |
| 81 | + # Condition with HITL - user decides which branch to take |
| 82 | + # The evaluator is ignored when requires_confirmation=True |
| 83 | + # User confirms -> detailed_analysis (if branch) |
| 84 | + # User rejects -> behavior depends on on_reject setting |
| 85 | + analysis_condition = Condition( |
| 86 | + name="analysis_depth_decision", |
| 87 | + steps=[Step(name="detailed_analysis", executor=detailed_analysis)], |
| 88 | + else_steps=[Step(name="quick_summary", executor=quick_summary)], |
| 89 | + requires_confirmation=True, |
| 90 | + confirmation_message="Would you like to perform detailed analysis?", |
| 91 | + on_reject=on_reject_mode, |
| 92 | + ) |
| 93 | + |
| 94 | + report_step = Step(name="generate_report", executor=generate_report) |
| 95 | + |
| 96 | + # Create workflow with database for HITL persistence |
| 97 | + workflow = Workflow( |
| 98 | + name="condition_hitl_demo", |
| 99 | + steps=[analyze_step, analysis_condition, report_step], |
| 100 | + db=SqliteDb(db_file="tmp/condition_hitl.db"), |
| 101 | + ) |
| 102 | + |
| 103 | + run_output = workflow.run("Q4 sales data") |
| 104 | + |
| 105 | + # Handle HITL pauses |
| 106 | + while run_output.is_paused: |
| 107 | + # Handle Step requirements (confirmation) |
| 108 | + for requirement in run_output.steps_requiring_confirmation: |
| 109 | + print(f"\n[DECISION POINT] {requirement.step_name}") |
| 110 | + print(f"[HITL] {requirement.confirmation_message}") |
| 111 | + print(f"[INFO] on_reject mode: {requirement.on_reject}") |
| 112 | + |
| 113 | + user_choice = input("\nYour choice (yes/no): ").strip().lower() |
| 114 | + if user_choice in ("yes", "y"): |
| 115 | + requirement.confirm() |
| 116 | + print("[HITL] Confirmed - executing 'if' branch (detailed analysis)") |
| 117 | + else: |
| 118 | + requirement.reject() |
| 119 | + if on_reject_mode == OnReject.else_branch: |
| 120 | + print("[HITL] Rejected - executing 'else' branch (quick summary)") |
| 121 | + elif on_reject_mode == OnReject.skip: |
| 122 | + print("[HITL] Rejected - skipping entire condition") |
| 123 | + else: |
| 124 | + print("[HITL] Rejected - cancelling workflow") |
| 125 | + |
| 126 | + run_output = workflow.continue_run(run_output) |
| 127 | + |
| 128 | + print("\n" + "-" * 40) |
| 129 | + print(f"Status: {run_output.status}") |
| 130 | + print("-" * 40) |
| 131 | + print(run_output.content) |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + print("=" * 60) |
| 136 | + print("Condition with User Decision HITL Example") |
| 137 | + print("=" * 60) |
| 138 | + print("\nThis demo shows 3 different on_reject behaviors:") |
| 139 | + print(" 1. on_reject='else' (default) - Execute else branch on reject") |
| 140 | + print(" 2. on_reject='skip' - Skip entire condition on reject") |
| 141 | + print(" 3. on_reject='cancel' - Cancel workflow on reject") |
| 142 | + print() |
| 143 | + |
| 144 | + # Let user choose which demo to run |
| 145 | + print("Which demo would you like to run?") |
| 146 | + print(" 1. on_reject='else' (execute else branch)") |
| 147 | + print(" 2. on_reject='skip' (skip condition)") |
| 148 | + print(" 3. on_reject='cancel' (cancel workflow)") |
| 149 | + print(" 4. Run all demos") |
| 150 | + |
| 151 | + choice = input("\nEnter choice (1-4): ").strip() |
| 152 | + |
| 153 | + if choice == "1": |
| 154 | + run_demo(OnReject.else_branch, "Execute Else Branch on Reject") |
| 155 | + elif choice == "2": |
| 156 | + run_demo(OnReject.skip, "Skip Condition on Reject") |
| 157 | + elif choice == "3": |
| 158 | + run_demo(OnReject.cancel, "Cancel Workflow on Reject") |
| 159 | + elif choice == "4": |
| 160 | + # Run all demos - use a non-interactive mode for demonstration |
| 161 | + print( |
| 162 | + "\nRunning all demos with automatic 'no' response to show rejection behavior..." |
| 163 | + ) |
| 164 | + |
| 165 | + for mode, name in [ |
| 166 | + (OnReject.else_branch, "Execute Else Branch on Reject"), |
| 167 | + (OnReject.skip, "Skip Condition on Reject"), |
| 168 | + (OnReject.cancel, "Cancel Workflow on Reject"), |
| 169 | + ]: |
| 170 | + print("\n" + "=" * 60) |
| 171 | + print(f"Demo: {name}") |
| 172 | + print(f"on_reject = {mode.value}") |
| 173 | + print("=" * 60) |
| 174 | + |
| 175 | + analyze_step = Step(name="analyze_data", executor=analyze_data) |
| 176 | + analysis_condition = Condition( |
| 177 | + name="analysis_depth_decision", |
| 178 | + evaluator=True, |
| 179 | + steps=[Step(name="detailed_analysis", executor=detailed_analysis)], |
| 180 | + else_steps=[Step(name="quick_summary", executor=quick_summary)], |
| 181 | + requires_confirmation=True, |
| 182 | + confirmation_message="Would you like to perform detailed analysis?", |
| 183 | + on_reject=mode, |
| 184 | + ) |
| 185 | + report_step = Step(name="generate_report", executor=generate_report) |
| 186 | + |
| 187 | + workflow = Workflow( |
| 188 | + name="condition_hitl_demo", |
| 189 | + steps=[analyze_step, analysis_condition, report_step], |
| 190 | + db=SqliteDb(db_file="tmp/condition_hitl.db"), |
| 191 | + ) |
| 192 | + |
| 193 | + run_output = workflow.run("Q4 sales data") |
| 194 | + |
| 195 | + # Auto-reject for demonstration |
| 196 | + while run_output.is_paused: |
| 197 | + for requirement in run_output.steps_requiring_confirmation: |
| 198 | + print(f"\n[DECISION POINT] {requirement.step_name}") |
| 199 | + print(f"[HITL] {requirement.confirmation_message}") |
| 200 | + print("[AUTO] Rejecting to demonstrate on_reject behavior...") |
| 201 | + requirement.reject() |
| 202 | + |
| 203 | + run_output = workflow.continue_run(run_output) |
| 204 | + |
| 205 | + print(f"\nStatus: {run_output.status}") |
| 206 | + print(f"Content: {run_output.content}") |
| 207 | + else: |
| 208 | + print("Invalid choice. Running default demo (on_reject='else')...") |
| 209 | + run_demo(OnReject.else_branch, "Execute Else Branch on Reject") |
0 commit comments