Repository navigation
feat(chatui): show failed speech and knowledge base model calls #11304
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
1d8126f
41937ad
04449e2
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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| """Show failed speech and knowledge-base model calls in ChatUI. | ||
|
|
||
| Speech-to-text, text-to-speech and knowledge-base retrieval fall back quietly | ||
| when their model fails: the voice message is dropped, the reply is sent as | ||
| text, or the answer is given without the retrieved knowledge. Other platforms | ||
| keep that behavior; ChatUI also shows a card, so the user knows which model to | ||
| fix instead of finding only a log line. | ||
| """ | ||
|
|
||
| from astrbot.core import logger | ||
| from astrbot.core.message.components import Json | ||
| from astrbot.core.message.message_event_result import MessageChain | ||
| from astrbot.core.platform.astr_message_event import AstrMessageEvent | ||
|
|
||
| AUXILIARY_MODEL_ERROR_CODE = "auxiliaryModelFailed" | ||
|
|
||
|
|
||
| async def notify_auxiliary_model_failure( | ||
| event: AstrMessageEvent, model: str, detail: str | ||
| ) -> None: | ||
| """Show a ChatUI card for a failed speech or knowledge-base model call. | ||
|
|
||
| The reply goes on without the model, so a failure to show the card is only | ||
| logged. | ||
|
|
||
| Args: | ||
| event: Message event whose reply the failure affects. | ||
| model: Which model failed, e.g. "Speech-to-text (whisper)". | ||
| detail: The provider's error, shown under the card's details. | ||
| """ | ||
| if event.get_platform_name() != "webchat": | ||
| return | ||
| # Sending marks the event as answered, and the pipeline then skips the | ||
| # model. The card is only a notice: keep the reply going. | ||
| has_sent = event._has_send_oper | ||
| try: | ||
| await event.send( | ||
| MessageChain( | ||
| type="chatui_error", | ||
| chain=[ | ||
| Json( | ||
| { | ||
| "code": AUXILIARY_MODEL_ERROR_CODE, | ||
| "message": f"{model}: {detail}", | ||
| } | ||
| ) | ||
| ], | ||
| ) | ||
| ) | ||
| except Exception as exc: | ||
|
Comment on lines
+37
to
+50
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. 🟡 Medium · Dropped cards are not logged When the WebChat queue rejects the card, for example because the request is no longer available, Make the WebChat send path report a rejected queue insertion, or have the notifier detect and log a failed-send result. Prompt for AI agentsIn `astrbot/core/auxiliary_model_errors.py` at lines 34-47:
**Dropped cards are not logged**
When the WebChat queue rejects the card, for example because the request is no longer available, `WebChatMessageEvent._send` returns `None` when the WebChat queue rejects the card, but `WebChatMessageEvent.send` ignores that result and returns normally. `notify_auxiliary_model_failure` therefore logs nothing, leaving a failed notification invisible to operators.
Make the WebChat send path report a rejected queue insertion, or have the notifier detect and log a failed-send result. |
||
| logger.warning(f"Failed to show the {model} failure in ChatUI: {exc}") | ||
| finally: | ||
| event._has_send_oper = has_sent | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -285,8 +285,25 @@ async def retrieve( | |
| kb_names: list[str], | ||
| top_k_fusion: int = 20, | ||
| top_m_final: int = 5, | ||
| errors: list[str] | None = None, | ||
| ) -> dict | None: | ||
| """从指定知识库中检索相关内容""" | ||
| """从指定知识库中检索相关内容 | ||
|
|
||
| Args: | ||
| query: Text to search for. | ||
| kb_names: Names of the knowledge bases to search. | ||
| top_k_fusion: Results kept after rank fusion. | ||
| top_m_final: Results returned. | ||
| errors: If given, collects failures that were skipped (an | ||
| unavailable knowledge base, a failed embedding or rerank call). | ||
|
|
||
| Returns: | ||
| The context text and results, an empty dict when no knowledge base | ||
| was requested, or None when nothing matched. | ||
|
|
||
| Raises: | ||
| ValueError: Every requested knowledge base is unavailable. | ||
| """ | ||
| kb_ids = [] | ||
| kb_id_helper_map = {} | ||
| unavailable_kbs = [] | ||
|
|
@@ -295,6 +312,11 @@ async def retrieve( | |
| if kb_helper.init_error: | ||
| unavailable_kbs.append((kb_name, kb_helper.init_error)) | ||
| logger.warning(f"知识库 {kb_name} 不可用: {kb_helper.init_error}") | ||
| if errors is not None: | ||
| errors.append( | ||
| f"Knowledge base {kb_name} is unavailable: " | ||
| f"{kb_helper.init_error}" | ||
| ) | ||
|
Comment on lines
+315
to
+319
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. 🟡 Medium · Missing knowledge bases go unreported When a configured knowledge-base name no longer resolves to a loaded knowledge base, Collect errors for unresolved names before early returns and pass them through the tool and injected-context retrieval paths. Also at Prompt for AI agentsIn `astrbot/core/knowledge_base/kb_mgr.py` at lines 315-319:
**Missing knowledge bases go unreported**
When a configured knowledge-base name no longer resolves to a loaded knowledge base, `KnowledgeBaseManager.retrieve` skips unresolved names, and `check_all_kb` can return before errors are collected, so the configured knowledge base is silently omitted and ChatUI shows no warning.
Collect errors for unresolved names before early returns and pass them through the tool and injected-context retrieval paths.
Also at `astrbot/core/astr_agent_runtime/tools/knowledge_base_tools.py:45`, `astrbot/core/astr_agent_runtime/tools/knowledge_base_tools.py:79`, `astrbot/core/astr_agent_runtime/tools/knowledge_base_tools.py:122`, `astrbot/core/astr_agent_runtime/tools/knowledge_base_tools.py:126`, `astrbot/core/astr_agent_runtime/main_agent/knowledge_base.py:46`. |
||
| continue | ||
| kb_ids.append(kb_helper.kb.kb_id) | ||
| kb_id_helper_map[kb_helper.kb.kb_id] = kb_helper | ||
|
|
@@ -313,6 +335,7 @@ async def retrieve( | |
| kb_id_helper_map=kb_id_helper_map, | ||
| top_k_fusion=top_k_fusion, | ||
| top_m_final=top_m_final, | ||
| errors=errors, | ||
| ) | ||
| if not results: | ||
| return None | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,7 @@ async def retrieve( | |
| kb_id_helper_map: dict[str, KBHelper], | ||
| top_k_fusion: int = 20, | ||
| top_m_final: int = 5, | ||
| errors: list[str] | None = None, | ||
| ) -> list[RetrievalResult]: | ||
| """混合检索 | ||
|
|
||
|
|
@@ -82,6 +83,8 @@ async def retrieve( | |
| kb_ids: 知识库 ID 列表 | ||
| top_m_final: 最终返回数量 | ||
| enable_rerank: 是否启用 Rerank | ||
| errors: If given, collects embedding and rerank failures that were | ||
| skipped, so the caller can report them. | ||
|
|
||
| Returns: | ||
| List[RetrievalResult]: 检索结果列表 | ||
|
|
@@ -115,6 +118,7 @@ async def retrieve( | |
| query=query, | ||
| kb_ids=kb_ids, | ||
| kb_options=kb_options, | ||
| errors=errors, | ||
| ) | ||
| time_end = time.time() | ||
| logger.debug( | ||
|
|
@@ -189,6 +193,8 @@ async def retrieve( | |
| ) | ||
| except Exception as e: | ||
| logger.warning(f"Rerank 执行失败,已跳过重排序并使用融合结果: {e}") | ||
| if errors is not None: | ||
| errors.append(f"Rerank failed: {type(e).__name__}: {e}") | ||
|
|
||
| return retrieval_results[:top_m_final] | ||
|
|
||
|
|
@@ -197,6 +203,7 @@ async def _dense_retrieve( | |
| query: str, | ||
| kb_ids: list[str], | ||
| kb_options: dict, | ||
| errors: list[str] | None = None, | ||
| ): | ||
| """稠密检索 (向量相似度) | ||
|
|
||
|
|
@@ -206,6 +213,7 @@ async def _dense_retrieve( | |
| query: 查询文本 | ||
| kb_ids: 知识库 ID 列表 | ||
| top_k: 返回结果数量 | ||
| errors: If given, collects the failure of each skipped knowledge base. | ||
|
|
||
| Returns: | ||
| List[Result]: 检索结果列表 | ||
|
|
@@ -232,6 +240,11 @@ async def _dense_retrieve( | |
| f"知识库 {kb_id} 稠密检索失败: {type(e).__name__}: {e}", | ||
| exc_info=True, | ||
| ) | ||
| if errors is not None: | ||
| errors.append( | ||
| f"Embedding search in knowledge base {kb_id} failed: " | ||
|
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. 🟡 Medium · Knowledge-base cards omit provider identity When an embedding or rerank call fails and the exception text does not itself identify the configured provider, the collected embedding and rerank errors identify a knowledge base or only say “Rerank,” not the provider that failed, so users with multiple configured models cannot tell which configuration to fix from the card. Include the relevant embedding or rerank provider ID in each collected failure detail. Also at Prompt for AI agentsIn `astrbot/core/knowledge_base/retrieval/manager.py` at line 245:
**Knowledge-base cards omit provider identity**
When an embedding or rerank call fails and the exception text does not itself identify the configured provider, the collected embedding and rerank errors identify a knowledge base or only say “Rerank,” not the provider that failed, so users with multiple configured models cannot tell which configuration to fix from the card.
Include the relevant embedding or rerank provider ID in each collected failure detail.
Also at `astrbot/core/knowledge_base/retrieval/manager.py:197`. |
||
| f"{type(e).__name__}: {e}" | ||
| ) | ||
| # skip the faulty KB and continue | ||
|
|
||
| # 按相似度排序并返回 top_k | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| from pathlib import Path | ||
|
|
||
| from astrbot.core import logger | ||
| from astrbot.core.auxiliary_model_errors import notify_auxiliary_model_failure | ||
| from astrbot.core.message.components import Image, Plain, Record, Reply | ||
| from astrbot.core.platform.astr_message_event import AstrMessageEvent | ||
| from astrbot.core.utils.astrbot_path import get_astrbot_temp_path | ||
|
|
@@ -232,10 +233,20 @@ async def process( | |
| f"Session {event.unified_msg_origin} has no speech-to-text " | ||
| "provider configured.", | ||
| ) | ||
| if any(isinstance(comp, Record) for comp in event.get_messages()): | ||
|
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. 🟡 Medium · Quoted voice is skipped without warning When speech-to-text is enabled, no provider is selected, and a voice Include records nested in Prompt for AI agentsIn `astrbot/core/pipeline/preprocess_stage/stage.py` at line 236:
**Quoted voice is skipped without warning**
When speech-to-text is enabled, no provider is selected, and a voice `Record` appears only inside a `Reply` chain, the missing-provider check scans only top-level components and returns before the reply-chain loop, so the referenced voice is dropped without a ChatUI warning.
Include records nested in `Reply` chains in the missing-provider check. |
||
| await notify_auxiliary_model_failure( | ||
| event, | ||
| "Speech-to-text", | ||
| "Speech-to-text is enabled, but no provider is selected.", | ||
| ) | ||
| return | ||
|
|
||
| # One card per message, however many recordings fail. | ||
| stt_failure_shown = False | ||
|
|
||
| async def _stt_record(record_comp: Record, is_reply: bool = False): | ||
| """对单个 Record 组件执行语音转文本,成功返回 Plain,失败返回 None。""" | ||
| nonlocal stt_failure_shown | ||
| prefix = "referenced " if is_reply else "" | ||
| try: | ||
| path = await record_comp.convert_to_file_path() | ||
|
|
@@ -263,6 +274,14 @@ async def _stt_record(record_comp: Record, is_reply: bool = False): | |
| logger.error(traceback.format_exc()) | ||
| suffix = " (referenced message)" if is_reply else "" | ||
| logger.error(f"Speech-to-text{suffix} failed: {e}") | ||
| if not stt_failure_shown: | ||
| stt_failure_shown = True | ||
| await notify_auxiliary_model_failure( | ||
| event, | ||
| "Speech-to-text " | ||
| f"({stt_provider.provider_config.get('id', 'unknown')})", | ||
| str(e), | ||
| ) | ||
| break | ||
| return None | ||
|
|
||
|
|
||
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.
🟡 Medium · Provider errors expose internal details
When a provider or knowledge-base initialization exception includes a credential or other sensitive internal detail,
notify_auxiliary_model_failureplaces the unfiltered exception or initialization-error text in the ChatUI card, so a WebChat user who triggers the failure sees any credentials, internal URLs, or filesystem details included in that text.Redact credentials and sensitive endpoint or filesystem details before displaying provider errors to ChatUI users.
Also at
astrbot/core/knowledge_base/kb_mgr.py:319.Prompt for AI agents