fix: Prevent double using_auto_reply events when using async run
#2276
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why are these changes needed?
When running chats asynchronously (via
.a_run), theusing_auto_replyevent was being emitted twice per speaker turn.This occurred because
a_generate_replyiterates through all registered reply functions and calls both sync and async versions. Sincecheck_termination_and_human_reply(sync) anda_check_termination_and_human_reply(async) are both registered, both were being executed and each emitted theUsingAutoReplyEventand incrementing_consecutive_auto_reply_counter.The sync
generate_replymethod correctly skips async functions, but the asynca_generate_replywas not skipping sync functions that have async equivalents.This fix adds a utility method
_get_sync_funcs_to_skip_in_async_chatthat identifies sync functions with async counterparts (by detecting async functions registered withignore_async_in_sync_chat=Trueand matching them to their sync equivalents via the 'a_' naming convention). Thea_generate_replymethod now skips these sync functions, ensuring only the async version is called. This eliminates duplicate events and prevents double-incrementing of the auto-reply counter.Related issue number
N/A
Checks