mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-05 15:20:30 +08:00
fix(agent/tools): port Crawler to ToolBase so it can load and run (#16415)
### What problem does this PR solve? Closes #16414. The **Crawler** agent tool (`agent/tools/crawler.py`) was never ported to the modern `ToolBase`/`_invoke` interface during the agent module redesign, so it was broken in three independent ways: 1. **Crashed on construction.** `CrawlerParam` extends `ToolParamBase`, whose `__init__` reads `self.meta["parameters"]`, but `CrawlerParam` defined no `meta`. Constructing it raised `AttributeError: 'CrawlerParam' object has no attribute 'meta'`. Because `agent/canvas.py` instantiates `component_class(component_name + "Param")()` while loading a canvas, **any agent containing a Crawler node failed to load.** 2. **`_invoke` missing.** It extends `ToolBase` (whose `invoke()` dispatches to `self._invoke`) but only implemented the legacy `_run`, so `_invoke` resolved to `ComponentBase._invoke` → `NotImplementedError`. 3. **`be_output` removed.** `_run` called `Crawler.be_output(...)`, which no longer exists on the base classes. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) ### Changes - Add a `ToolMeta` to `CrawlerParam` (defined before `super().__init__()`, matching every other ported tool such as `ArXivParam`/`TavilyExtractParam`) advertising a required `query` parameter — the URL to crawl, default `{sys.query}`, consistent with the `{sys.query}` convention shared by the other tools. - Replace the legacy `_run`/`be_output` with `_invoke`/`set_output`, writing the extracted page content to `formalized_content` (errors surfaced via `_ERROR`), consistent with the other tools. - Preserve the existing SSRF guard (`assert_url_is_safe` + `pin_dns_global`). - Add regression tests (`test/unit_test/agent/component/test_crawler.py`) covering param construction, validation, and the tool descriptor. Same class of defect as #16329 (DeepL). Backend-only; no frontend changes. --------- Co-authored-by: Zhichang Yu <yuzhichang@gmail.com>
This commit is contained in:
@@ -13,10 +13,13 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
import logging
|
||||
import os
|
||||
from abc import ABC
|
||||
import asyncio
|
||||
from crawl4ai import AsyncWebCrawler
|
||||
from agent.tools.base import ToolParamBase, ToolBase
|
||||
from agent.tools.base import ToolMeta, ToolParamBase, ToolBase
|
||||
from common.connection_utils import timeout
|
||||
|
||||
|
||||
class CrawlerParam(ToolParamBase):
|
||||
@@ -25,6 +28,18 @@ class CrawlerParam(ToolParamBase):
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.meta: ToolMeta = {
|
||||
"name": "web_crawler",
|
||||
"description": "This tool can be used to crawl a web page and return its content as HTML, Markdown, or the extracted main text.",
|
||||
"parameters": {
|
||||
"query": {
|
||||
"type": "string",
|
||||
"description": "The absolute URL (including the http:// or https:// scheme) of the web page to crawl.",
|
||||
"default": "{sys.query}",
|
||||
"required": True,
|
||||
}
|
||||
},
|
||||
}
|
||||
super().__init__()
|
||||
self.proxy = None
|
||||
self.extract_type = "markdown"
|
||||
@@ -32,29 +47,57 @@ class CrawlerParam(ToolParamBase):
|
||||
def check(self):
|
||||
self.check_valid_value(self.extract_type, "Type of content from the crawler", ["html", "markdown", "content"])
|
||||
|
||||
def get_input_form(self) -> dict[str, dict]:
|
||||
return {
|
||||
"query": {
|
||||
"name": "URL",
|
||||
"type": "line"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Crawler(ToolBase, ABC):
|
||||
component_name = "Crawler"
|
||||
|
||||
def _run(self, history, **kwargs):
|
||||
@timeout(int(os.environ.get("COMPONENT_EXEC_TIMEOUT", 10 * 60)))
|
||||
def _invoke(self, **kwargs):
|
||||
from common.ssrf_guard import assert_url_is_safe, pin_dns_global
|
||||
|
||||
ans = self.get_input()
|
||||
ans = " - ".join(ans["content"]) if "content" in ans else ""
|
||||
if self.check_if_canceled("Crawler processing"):
|
||||
return
|
||||
|
||||
url = kwargs.get("query")
|
||||
if not url:
|
||||
self.set_output("formalized_content", "")
|
||||
return ""
|
||||
|
||||
try:
|
||||
_ssrf_hostname, _ssrf_ip = assert_url_is_safe(ans)
|
||||
_ssrf_hostname, _ssrf_ip = assert_url_is_safe(url)
|
||||
except ValueError:
|
||||
return Crawler.be_output("URL not valid")
|
||||
msg = "URL not valid"
|
||||
self.set_output("_ERROR", msg)
|
||||
return msg
|
||||
|
||||
try:
|
||||
# pin_dns_global is used (not thread-local) because crawl4ai resolves
|
||||
# DNS in asyncio executor threads that don't share thread-local state.
|
||||
with pin_dns_global(_ssrf_hostname, _ssrf_ip):
|
||||
result = asyncio.run(self.get_web(ans))
|
||||
result = asyncio.run(self.get_web(url))
|
||||
|
||||
return Crawler.be_output(result)
|
||||
if self.check_if_canceled("Crawler processing"):
|
||||
return
|
||||
|
||||
result = result or ""
|
||||
self.set_output("formalized_content", result)
|
||||
return result
|
||||
except Exception as e:
|
||||
return Crawler.be_output(f"An unexpected error occurred: {str(e)}")
|
||||
if self.check_if_canceled("Crawler processing"):
|
||||
return
|
||||
|
||||
logging.exception(f"Crawler error: {e}")
|
||||
msg = f"An unexpected error occurred: {str(e)}"
|
||||
self.set_output("_ERROR", msg)
|
||||
return msg
|
||||
|
||||
async def get_web(self, url):
|
||||
if self.check_if_canceled("Crawler async operation"):
|
||||
|
||||
Reference in New Issue
Block a user