microsoft / microsoft/agent-framework
Python: [Python] Inline HIL request and response in executor
- Dominant language
- Python
- Stars
- 13.6k
- Forks
- 2.3k
- Avg merge
- 2d 45m
- Merged PRs (30d)
- 358
Description
The HIL requesting and handling pattern is a bit cumbersome:
We first need to call ctx.request_info() then handling the response from another method marked as @response_handler. The callback ends up in another method so that it is not easy to do more things after calling request_info (this forces developers to breakdown their executor logic making complex task much more cumbersome to read and maintain). It would be great to provide another API like response = await ctx.request_info_and_wait_for_response() (just to illustrate, not a good name) so that we could fire HIL event and receive response in one line before proceeding to the next logic. This would be extremely helpful when the main executor has a complex branching and post-processing logic.
Pseudo code to illustrate:
```python
@handler
async def handle(self, input: str, ctx: WorkflowContext[str]) -> None:
...
user_feedback: ChatMessage | None = None
while True:
response = self.agent.run(messages + [user_feedback], tools=tools)
output = response.text
await ctx.yield_output(output)
request: PlanReviewRequest = PlanReviewRequest(
title=self.id,
plan=output,
)
response: PlanReviewResponse = await ctx.get_user_feedback(request)
if response.is_rejected:
return # exit
elif response.is_approved:
output = response.plan # Updated plan
break # go to next executor
elif response.is_pending_iteration:
user_feedback = ...
continue # next iteration
await ctx.set_shared_state(f"{self.id}_output", output)
await ctx.send_message(output)
```
Contributor guide
Assessment
This issue has not been assessed yet.