-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: deliver cron active agent fallback response #7913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |||||||||||||||||||||||||||||||||||
| from astrbot.core.cron.events import CronMessageEvent | ||||||||||||||||||||||||||||||||||||
| from astrbot.core.db import BaseDatabase | ||||||||||||||||||||||||||||||||||||
| from astrbot.core.db.po import CronJob | ||||||||||||||||||||||||||||||||||||
| from astrbot.core.message.message_event_result import MessageChain | ||||||||||||||||||||||||||||||||||||
| from astrbot.core.platform.message_session import MessageSession | ||||||||||||||||||||||||||||||||||||
| from astrbot.core.provider.entites import ProviderRequest | ||||||||||||||||||||||||||||||||||||
| from astrbot.core.utils.history_saver import persist_agent_history | ||||||||||||||||||||||||||||||||||||
|
|
@@ -386,9 +387,84 @@ async def _woke_main_agent( | |||||||||||||||||||||||||||||||||||
| req=req, | ||||||||||||||||||||||||||||||||||||
| summary_note=summary_note, | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| await self._send_active_agent_fallback_if_needed( | ||||||||||||||||||||||||||||||||||||
| session=session, | ||||||||||||||||||||||||||||||||||||
| req=req, | ||||||||||||||||||||||||||||||||||||
| llm_resp=llm_resp, | ||||||||||||||||||||||||||||||||||||
| cron_meta=cron_meta, | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| if not llm_resp: | ||||||||||||||||||||||||||||||||||||
| logger.warning("Cron job agent got no response") | ||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| async def _send_active_agent_fallback_if_needed( | ||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||||||||||||||
| session: MessageSession, | ||||||||||||||||||||||||||||||||||||
| req: ProviderRequest, | ||||||||||||||||||||||||||||||||||||
| llm_resp, | ||||||||||||||||||||||||||||||||||||
| cron_meta: dict, | ||||||||||||||||||||||||||||||||||||
| ) -> bool: | ||||||||||||||||||||||||||||||||||||
| if self._agent_sent_message_to_user(req): | ||||||||||||||||||||||||||||||||||||
| logger.info( | ||||||||||||||||||||||||||||||||||||
| "cron active agent fallback skipped agent_sent=True session=%s job_id=%s", | ||||||||||||||||||||||||||||||||||||
| session, | ||||||||||||||||||||||||||||||||||||
| cron_meta.get("id"), | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| text = str(getattr(llm_resp, "completion_text", "") or "").strip() | ||||||||||||||||||||||||||||||||||||
| if not llm_resp or getattr(llm_resp, "role", "") != "assistant" or not text: | ||||||||||||||||||||||||||||||||||||
| logger.warning( | ||||||||||||||||||||||||||||||||||||
| "cron active agent fallback skipped no assistant text session=%s job_id=%s", | ||||||||||||||||||||||||||||||||||||
| session, | ||||||||||||||||||||||||||||||||||||
| cron_meta.get("id"), | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| logger.info( | ||||||||||||||||||||||||||||||||||||
| "cron active agent fallback send start session=%s job_id=%s", | ||||||||||||||||||||||||||||||||||||
| session, | ||||||||||||||||||||||||||||||||||||
| cron_meta.get("id"), | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||
| ok = await self.ctx.send_message(session, MessageChain().message(text)) | ||||||||||||||||||||||||||||||||||||
| logger.info( | ||||||||||||||||||||||||||||||||||||
| "cron active agent fallback send done ok=%s session=%s job_id=%s", | ||||||||||||||||||||||||||||||||||||
| ok, | ||||||||||||||||||||||||||||||||||||
| session, | ||||||||||||||||||||||||||||||||||||
| cron_meta.get("id"), | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| return bool(ok) | ||||||||||||||||||||||||||||||||||||
| except Exception as e: # noqa: BLE001 | ||||||||||||||||||||||||||||||||||||
| logger.warning( | ||||||||||||||||||||||||||||||||||||
| "cron active agent fallback send exception session=%s job_id=%s err=%r", | ||||||||||||||||||||||||||||||||||||
| session, | ||||||||||||||||||||||||||||||||||||
| cron_meta.get("id"), | ||||||||||||||||||||||||||||||||||||
| e, | ||||||||||||||||||||||||||||||||||||
| exc_info=True, | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| raise | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+439
to
+447
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exception is logged here with
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||||||||||||||||||
| def _agent_sent_message_to_user(req: ProviderRequest) -> bool: | ||||||||||||||||||||||||||||||||||||
| results = getattr(req, "tool_calls_result", None) | ||||||||||||||||||||||||||||||||||||
| if not results: | ||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||
| if not isinstance(results, list): | ||||||||||||||||||||||||||||||||||||
| results = [results] | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| for result in results: | ||||||||||||||||||||||||||||||||||||
| call_results = getattr(result, "tool_calls_result", None) or [] | ||||||||||||||||||||||||||||||||||||
| for call_result in call_results: | ||||||||||||||||||||||||||||||||||||
| content = getattr(call_result, "content", "") | ||||||||||||||||||||||||||||||||||||
| if isinstance(content, list): | ||||||||||||||||||||||||||||||||||||
| content = " ".join( | ||||||||||||||||||||||||||||||||||||
| str(getattr(part, "text", part)) for part in content | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| if "Message sent to session" in str(content): | ||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| __all__ = ["CronJobManager"] | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The call to
_send_active_agent_fallback_if_neededis currently placed before the null check forllm_resp. Since the fallback mechanism requires a valid response to proceed, it should be called after ensuringllm_respis notNone. This also prevents redundant warning logs when the agent fails to provide any response, as both the fallback function and the current method would log a warning.