feat: parallelize lookup (Wiki+SearXNG) and include Wikipedia thumbnails

Runs the Wikipedia summary and SearXNG search concurrently and returns
both when available, so current-event questions aren't masked by a
generic role article from Wikipedia. When the Wikipedia summary includes
a thumbnail, it is cached through the existing image pipeline and
surfaced as an embeddable markdown snippet alongside the extract.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-04-17 22:44:42 -04:00
parent 8db6b4d230
commit 9a252c8dde
4 changed files with 172 additions and 49 deletions
+60 -10
View File
@@ -1,5 +1,7 @@
import pytest
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
from fabledassistant.services.tools.web import lookup_tool
@pytest.mark.asyncio
@@ -8,16 +10,19 @@ async def test_lookup_wikipedia_hit():
"title": "QUIC",
"extract": "QUIC is a transport layer protocol.",
"url": "https://en.wikipedia.org/wiki/QUIC",
"thumbnail_url": "",
}
with patch("fabledassistant.services.wikipedia.wiki_summary", new_callable=AsyncMock, return_value=wiki_data):
from fabledassistant.services.tools.web import lookup_tool
with patch("fabledassistant.services.wikipedia.wiki_summary", new_callable=AsyncMock, return_value=wiki_data), \
patch("fabledassistant.config.Config") as mock_config:
mock_config.searxng_enabled.return_value = False
result = await lookup_tool(user_id=1, arguments={"query": "QUIC"})
assert result["success"] is True
assert result["type"] == "lookup"
assert result["source"] == "wikipedia"
assert result["data"]["title"] == "QUIC"
assert result["data"]["wikipedia"]["title"] == "QUIC"
assert result["data"]["web"] == []
assert "image" not in result["data"]["wikipedia"]
@pytest.mark.asyncio
@@ -31,12 +36,58 @@ async def test_lookup_wikipedia_miss_searxng_fallback():
patch("fabledassistant.services.research._search_searxng", new_callable=AsyncMock, return_value=searxng_results), \
patch("fabledassistant.services.rss._fetch_full_article", new_callable=AsyncMock, return_value="Full article about QUIC..."):
mock_config.searxng_enabled.return_value = True
from fabledassistant.services.tools.web import lookup_tool
result = await lookup_tool(user_id=1, arguments={"query": "QUIC"})
assert result["success"] is True
assert result["source"] == "web"
assert result["data"]["results"]
assert result["data"]["wikipedia"] is None
assert len(result["data"]["web"]) == 1
assert result["data"]["web"][0]["url"] == "https://example.com/quic"
@pytest.mark.asyncio
async def test_lookup_parallel_both_sources():
wiki_data = {
"title": "President of the United States",
"extract": "The president is the head of state.",
"url": "https://en.wikipedia.org/wiki/President_of_the_United_States",
"thumbnail_url": "",
}
searxng_results = [
{"url": "https://whitehouse.gov", "title": "The White House", "snippet": "Current admin..."},
]
with patch("fabledassistant.services.wikipedia.wiki_summary", new_callable=AsyncMock, return_value=wiki_data), \
patch("fabledassistant.config.Config") as mock_config, \
patch("fabledassistant.services.research._search_searxng", new_callable=AsyncMock, return_value=searxng_results), \
patch("fabledassistant.services.rss._fetch_full_article", new_callable=AsyncMock, return_value="Current president is..."):
mock_config.searxng_enabled.return_value = True
result = await lookup_tool(user_id=1, arguments={"query": "president of the united states"})
assert result["success"] is True
assert result["data"]["wikipedia"]["title"] == "President of the United States"
assert len(result["data"]["web"]) == 1
@pytest.mark.asyncio
async def test_lookup_wikipedia_thumbnail_cached():
wiki_data = {
"title": "Hedgehog",
"extract": "Hedgehogs are spiny mammals.",
"url": "https://en.wikipedia.org/wiki/Hedgehog",
"thumbnail_url": "https://upload.wikimedia.org/.../Hedgehog.jpg",
}
mock_record = MagicMock()
mock_record.id = 42
with patch("fabledassistant.services.wikipedia.wiki_summary", new_callable=AsyncMock, return_value=wiki_data), \
patch("fabledassistant.config.Config") as mock_config, \
patch("fabledassistant.services.images.fetch_and_store_image", new_callable=AsyncMock, return_value=mock_record):
mock_config.searxng_enabled.return_value = False
result = await lookup_tool(user_id=1, arguments={"query": "hedgehog"})
assert result["data"]["wikipedia"]["image"]["embed"] == "![Hedgehog](/api/images/42)"
assert "Wikipedia" in result["data"]["wikipedia"]["image"]["citation"]
assert "image_instructions" in result["data"]
@pytest.mark.asyncio
@@ -44,11 +95,10 @@ async def test_lookup_wikipedia_miss_no_searxng():
with patch("fabledassistant.services.wikipedia.wiki_summary", new_callable=AsyncMock, return_value=None), \
patch("fabledassistant.config.Config") as mock_config:
mock_config.searxng_enabled.return_value = False
from fabledassistant.services.tools.web import lookup_tool
result = await lookup_tool(user_id=1, arguments={"query": "xyznonexistent"})
assert result["success"] is True
assert result["source"] == "none"
assert "message" in result["data"]
@pytest.mark.asyncio
+27
View File
@@ -37,6 +37,33 @@ async def test_wiki_summary_returns_extract():
assert result["title"] == "Python (programming language)"
assert "Python" in result["extract"]
assert result["url"].startswith("https://")
assert result["thumbnail_url"] == ""
@pytest.mark.asyncio
async def test_wiki_summary_includes_thumbnail():
"""Thumbnail URL is extracted when the article has an image."""
payload = {
"type": "standard",
"title": "Hedgehog",
"extract": "Hedgehogs are spiny mammals.",
"content_urls": {"desktop": {"page": "https://en.wikipedia.org/wiki/Hedgehog"}},
"thumbnail": {"source": "https://upload.wikimedia.org/foo-320px.jpg"},
"originalimage": {"source": "https://upload.wikimedia.org/foo.jpg"},
}
mock_resp = _make_mock_response(payload)
with patch("fabledassistant.services.wikipedia.httpx.AsyncClient") as mock_client_cls:
mock_client = AsyncMock()
mock_client.__aenter__ = AsyncMock(return_value=mock_client)
mock_client.__aexit__ = AsyncMock(return_value=False)
mock_client.get = AsyncMock(return_value=mock_resp)
mock_client_cls.return_value = mock_client
result = await wiki_summary("Hedgehog")
assert result is not None
assert result["thumbnail_url"] == "https://upload.wikimedia.org/foo-320px.jpg"
@pytest.mark.asyncio