# now we handle events coming back from the workflow
async for event in handler.stream_events():
logging.info(f"Got event: {event}")
# if we get an InputRequiredEvent, that means the workflow needs human input
# so we send an event to the frontend that will be handled specially
if isinstance(event, ToolRequestEvent):
response = "Yes"
# which we send back to the workflow as a HumanResponseEvent
handler.ctx.send_event(
ToolApprovedEvent(
tool_id=event.tool_id,
tool_name=event.tool_name,
tool_kwargs=event.tool_kwargs,
approved=True
)
)
elif isinstance(event, InputRequiredEvent):
logging.info("InputRequiredEvent")
# we expect the next thing from the socket to be human input, always "Yes" for now
response = "Yes"
# which we send back to the workflow as a HumanResponseEvent
handler.ctx.send_event(
HumanResponseEvent(response=response)
)
elif isinstance(event, ProgressEvent):
# the workflow also emits progress events which we send to the frontend
logging.info("ProgressEvent")
elif isinstance(event, StopEvent):
# the workflow also emits progress events which we send to the frontend
logging.info("StopEvent")
else:
logging.info("Unknown Event")
# once we've handled all the events, we await the final result
logging.info("Awaiting final result")
final_result = await handler
except Exception as e:
return JSONResponse(status_code=500, content={"message": str(e)})
return final_result