Task example with callbacks / 带回调的任务示例
(lang: str = "cn")
| 42 | |
| 43 | |
| 44 | def example_with_callbacks(lang: str = "cn"): |
| 45 | """Task example with callbacks / 带回调的任务示例""" |
| 46 | msgs = get_messages(lang) |
| 47 | |
| 48 | def my_confirmation(message: str) -> bool: |
| 49 | """Sensitive operation confirmation callback / 敏感操作确认回调""" |
| 50 | print(f"\n[{msgs['confirmation_required']}] {message}") |
| 51 | response = input(f"{msgs['continue_prompt']}: ") |
| 52 | return response.lower() in ("yes", "y", "是") |
| 53 | |
| 54 | def my_takeover(message: str) -> None: |
| 55 | """Manual takeover callback / 人工接管回调""" |
| 56 | print(f"\n[{msgs['manual_operation_required']}] {message}") |
| 57 | print(msgs["manual_operation_hint"]) |
| 58 | input(f"{msgs['press_enter_when_done']}: ") |
| 59 | |
| 60 | # Create Agent with custom callbacks |
| 61 | agent_config = AgentConfig(lang=lang) |
| 62 | agent = PhoneAgent( |
| 63 | agent_config=agent_config, |
| 64 | confirmation_callback=my_confirmation, |
| 65 | takeover_callback=my_takeover, |
| 66 | ) |
| 67 | |
| 68 | # Execute task that may require confirmation |
| 69 | result = agent.run("打开淘宝搜索无线耳机并加入购物车") |
| 70 | print(f"{msgs['task_result']}: {result}") |
| 71 | |
| 72 | |
| 73 | def example_step_by_step(lang: str = "cn"): |
nothing calls this directly
no test coverage detected