Skip to content

fix: recover web search citation cards from plain URLs in replies - #11099

Open
wcqqq1214 wants to merge 5 commits into
AstrBotDevs:masterfrom
wcqqq1214:fix/webchat-ref-url-fallback
Open

wcqqq1214 wants to merge 5 commits into
AstrBotDevs:masterfrom
wcqqq1214:fix/webchat-ref-url-fallback

Conversation

@wcqqq1214

@wcqqq1214 wcqqq1214 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Webchat renders citation cards by matching the private <ref>index</ref> tags the chat model is instructed to emit after sentences that rely on web search results. In practice, models often cite sources with plain or markdown URLs instead, so extract_web_search_refs matched nothing and the citation area stayed empty even though the answer listed real source links.

Modifications / 改动点

  • extract_web_search_refs (chat_service.py, live_chat_service.py): as a fallback, match URLs appearing in the reply text against URLs from web search tool results and emit cards for matches not already covered by <ref> tags. The native <ref> path is unchanged; the fallback only adds cards.

  • Collect native <ref> citations first, then append URL fallback matches, preserving first-appearance order within each group for stable card ordering.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

Verified against the real extract_web_search_refs with mocked tool results:

  • markdown / bare URL citations -> cards recovered, in text order
  • native <ref> citations -> behavior unchanged, in text order
  • mixed <ref> + URL citations -> deduplicated by result index; URL fallback selects the first result for each URL, but different indices sharing a URL may still produce separate cards
  • hallucinated URLs and dangling <ref> indices -> excluded
  • results without URL -> never matched by the fallback

Before

Screenshot 2026-11-12 at 17 30 23

After

Screenshot 2026-11-12 at 18 44 24

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。

  • 📚 I checked the affected WebUI instructions and screenshots in docs/zh and docs/en against the changed navigation, page structure, and labels, and updated them in this PR (or explained why no documentation update is needed). For renamed, moved, or merged entry points, I included an old entry → new entry mapping in the documentation and changelog.
    / 我已对照变化后的 WebUI 入口、页面结构和术语,核对并在本 PR 中更新 docs/zh 和 docs/en 的相关操作说明与截图(或说明无需更新文档的原因)。入口改名、移动或合并时,已在文档和 changelog 中补充 旧入口 → 新入口 对照。
    - No WebUI navigation, page structure, or label changes in this PR; backend-only fix.

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txt 和 pyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Restore web search citation cards from valid URLs in model replies while preserving existing native reference handling.

Bug Fixes:

  • Recover web search citation cards when replies cite search results with plain or markdown URLs instead of native <ref> tags.
  • Preserve native citation behavior while excluding hallucinated URLs, invalid references, and results without URLs.

Enhancements:

  • Maintain stable citation card ordering and deduplicate matches across native references and URL-based citations.

…replies

Models often cite sources with markdown or bare URLs instead of the
private <ref>index</ref> format, leaving the webchat citation sidebar
empty. Match URLs in the reply against search result URLs as a fallback
and order cards by first appearance in text instead of an unordered set.

Refs: webchat search citations
@codecov-commenter

codecov-commenter commented Nov 12, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 76.92308% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
astrbot/dashboard/services/chat_service.py 76.92% 3 Missing ⚠️
astrbot/dashboard/services/live_chat_service.py 76.92% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@wcqqq1214
wcqqq1214 marked this pull request as ready for review November 12, 2026 13:15

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="astrbot/dashboard/services/chat_service.py" line_range="330" />
<code_context>
+        if item.get("url") and item["url"] not in url_to_index:
+            url_to_index[item["url"]] = idx
+    for match in re.findall(
+        r"https?://[^\s<>()\[\]{}\"'\u3000-\u303f\uff00-\uffef]+", accumulated_text
+    ):
+        idx = url_to_index.get(match.rstrip(".,;:!?。))】」"))
</code_context>
<issue_to_address>
**Cited URLs lose their cards**

When a cited URL contains parentheses, is surrounded by punctuation the regex does not exclude, or legitimately ends with punctuation stripped by `rstrip`, the URL regex truncates URLs at parentheses and can include surrounding punctuation, while `rstrip` can remove characters that belong to a valid URL. The resulting string does not match `url_to_index`, so the citation card is missing.

Parse the cited URL without truncating valid parentheses, and remove only surrounding delimiters that are not part of the URL before looking it up.

Also at `astrbot/dashboard/services/live_chat_service.py:338-340`, `astrbot/dashboard/services/chat_service.py:329`, `astrbot/dashboard/services/chat_service.py:331-332`.
</issue_to_address>

### Comment 2
<location path="astrbot/dashboard/services/chat_service.py" line_range="325" />
<code_context>
+            matched_indices.setdefault(idx)
+    # Fallback: models may cite sources with plain or markdown URLs instead
+    # of <ref> tags; recover cards by matching URLs from the search results.
+    url_to_index = {}
+    for idx, item in web_search_results.items():
+        if item.get("url") and item["url"] not in url_to_index:
</code_context>
<issue_to_address>
**Some search providers still lack cards**

When the configured search provider is Exa or AnySearch and the reply cites a result by URL, `web_search_results` remains empty because the extraction allowlist excludes `web_search_exa` and `web_search_anysearch`, so the early return prevents the URL fallback from producing cards for those providers.

Build the extraction allowlist from `WEB_SEARCH_CITATION_TOOL_NAMES` or add the omitted supported tool names to both functions.

Also at `astrbot/dashboard/services/live_chat_service.py:333`.
</issue_to_address>

Sourcery assessment

Approval pending. 2 findings to address first.

Blocking findings: astrbot/dashboard/services/chat_service.py:330, astrbot/dashboard/services/chat_service.py:325


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

if item.get("url") and item["url"] not in url_to_index:
url_to_index[item["url"]] = idx
for match in re.findall(
r"https?://[^\s<>()\[\]{}\"'\u3000-\u303f\uff00-\uffef]+", accumulated_text

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Cited URLs lose their cards

When a cited URL contains parentheses, is surrounded by punctuation the regex does not exclude, or legitimately ends with punctuation stripped by rstrip, the URL regex truncates URLs at parentheses and can include surrounding punctuation, while rstrip can remove characters that belong to a valid URL. The resulting string does not match url_to_index, so the citation card is missing.

Parse the cited URL without truncating valid parentheses, and remove only surrounding delimiters that are not part of the URL before looking it up.

Also at astrbot/dashboard/services/live_chat_service.py:338-340, astrbot/dashboard/services/chat_service.py:329, astrbot/dashboard/services/chat_service.py:331-332.

Prompt for AI agents
In `astrbot/dashboard/services/chat_service.py` at line 330:

**Cited URLs lose their cards**

When a cited URL contains parentheses, is surrounded by punctuation the regex does not exclude, or legitimately ends with punctuation stripped by `rstrip`, the URL regex truncates URLs at parentheses and can include surrounding punctuation, while `rstrip` can remove characters that belong to a valid URL. The resulting string does not match `url_to_index`, so the citation card is missing.

Parse the cited URL without truncating valid parentheses, and remove only surrounding delimiters that are not part of the URL before looking it up.

Also at `astrbot/dashboard/services/live_chat_service.py:338-340`, `astrbot/dashboard/services/chat_service.py:329`, `astrbot/dashboard/services/chat_service.py:331-332`.

matched_indices.setdefault(idx)
# Fallback: models may cite sources with plain or markdown URLs instead
# of <ref> tags; recover cards by matching URLs from the search results.
url_to_index = {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Some search providers still lack cards

When the configured search provider is Exa or AnySearch and the reply cites a result by URL, web_search_results remains empty because the extraction allowlist excludes web_search_exa and web_search_anysearch, so the early return prevents the URL fallback from producing cards for those providers.

Build the extraction allowlist from WEB_SEARCH_CITATION_TOOL_NAMES or add the omitted supported tool names to both functions.

Also at astrbot/dashboard/services/live_chat_service.py:333.

Prompt for AI agents
In `astrbot/dashboard/services/chat_service.py` at line 325:

**Some search providers still lack cards**

When the configured search provider is Exa or AnySearch and the reply cites a result by URL, `web_search_results` remains empty because the extraction allowlist excludes `web_search_exa` and `web_search_anysearch`, so the early return prevents the URL fallback from producing cards for those providers.

Build the extraction allowlist from `WEB_SEARCH_CITATION_TOOL_NAMES` or add the omitted supported tool names to both functions.

Also at `astrbot/dashboard/services/live_chat_service.py:333`.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants