Files
linkliti 220c83341b feat(plugins): extract search_engine tool into _search_engine plugin
Move the search_engine tool, prompts, DOX, and its WebUI message handler into a _search_engine plugin so live web search ships as a standard plugin. Carried-over imports are trimmed, searxng failures render as formatted results, and malformed result items are skipped. The tool logs its message as its own `search_engine` type with an `icon://search Searching: <query>` headline rendered through the SRC badge handler with web access indigo accent styles shared with the browser badge. Regression tests cover the tool.

Validation: pytest tests/test_tool_action_contracts.py tests/test_search_engine_tool.py tests/test_webui_plugin_process_types.py
2026-10-21 19:04:33 +03:00

60 lines
1.8 KiB
Python

import uuid
from helpers.tool import Tool, Response
from helpers.errors import handle_error
from helpers.searxng import search as searxng
from helpers.strings import truncate_text
SEARCH_ENGINE_RESULTS = 10
class SearchEngine(Tool):
async def execute(self, query="", **kwargs):
searxng_result = await self.searxng_search(query)
await self.agent.handle_intervention(
searxng_result
) # wait for intervention and handle it, if paused
return Response(message=searxng_result, break_loop=False)
async def searxng_search(self, question):
try:
results = await searxng(question)
except Exception as e:
results = e
return self.format_result_searxng(results, "Search Engine")
def format_result_searxng(self, result, source):
if isinstance(result, Exception):
handle_error(result)
return f"{source} search failed: {str(result)}"
outputs = []
for item in (result or {}).get("results", []):
if not isinstance(item, dict):
continue
title = item.get("title", "")
url = item.get("url", "")
content = item.get("content", "")
outputs.append(f"{title}\n{url}\n{content}")
return "\n\n".join(outputs[:SEARCH_ENGINE_RESULTS]).strip()
def get_log_object(self):
return self.agent.context.log.log(
type="search_engine",
heading=self.get_heading(),
content="",
kvps=self.args,
id=str(uuid.uuid4()),
)
def get_heading(self):
query = str(self.args.get("query") or "").strip()
text = f"Searching: {truncate_text(query, 60)}" if query else "Searching"
return f"icon://search {text}"