2024-10-21 11:38:41 +08:00
|
|
|
#
|
|
|
|
|
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
|
|
|
|
from abc import ABC
|
|
|
|
|
import asyncio
|
|
|
|
|
from crawl4ai import AsyncWebCrawler
|
2025-07-30 19:41:09 +08:00
|
|
|
from agent.tools.base import ToolParamBase, ToolBase
|
2025-08-28 18:40:32 +08:00
|
|
|
|
2024-10-21 11:38:41 +08:00
|
|
|
|
2025-07-30 19:41:09 +08:00
|
|
|
class CrawlerParam(ToolParamBase):
|
2024-10-21 11:38:41 +08:00
|
|
|
"""
|
|
|
|
|
Define the Crawler component parameters.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
2024-10-22 14:16:44 +08:00
|
|
|
self.proxy = None
|
|
|
|
|
self.extract_type = "markdown"
|
2025-11-11 17:36:48 +08:00
|
|
|
|
2024-10-21 11:38:41 +08:00
|
|
|
def check(self):
|
2026-04-25 15:30:15 +09:00
|
|
|
self.check_valid_value(self.extract_type, "Type of content from the crawler", ["html", "markdown", "content"])
|
2024-10-21 11:38:41 +08:00
|
|
|
|
|
|
|
|
|
2025-07-30 19:41:09 +08:00
|
|
|
class Crawler(ToolBase, ABC):
|
2024-10-21 11:38:41 +08:00
|
|
|
component_name = "Crawler"
|
|
|
|
|
|
|
|
|
|
def _run(self, history, **kwargs):
|
2026-04-25 15:30:15 +09:00
|
|
|
from common.ssrf_guard import assert_url_is_safe, pin_dns_global
|
|
|
|
|
|
2024-10-21 11:38:41 +08:00
|
|
|
ans = self.get_input()
|
|
|
|
|
ans = " - ".join(ans["content"]) if "content" in ans else ""
|
2026-04-25 15:30:15 +09:00
|
|
|
try:
|
|
|
|
|
_ssrf_hostname, _ssrf_ip = assert_url_is_safe(ans)
|
|
|
|
|
except ValueError:
|
2025-01-02 18:45:45 +08:00
|
|
|
return Crawler.be_output("URL not valid")
|
2024-10-21 11:38:41 +08:00
|
|
|
try:
|
2026-04-25 15:30:15 +09:00
|
|
|
# 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))
|
2024-10-21 11:38:41 +08:00
|
|
|
|
|
|
|
|
return Crawler.be_output(result)
|
2025-11-11 17:36:48 +08:00
|
|
|
|
2024-10-21 11:38:41 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
return Crawler.be_output(f"An unexpected error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
async def get_web(self, url):
|
2025-11-11 17:36:48 +08:00
|
|
|
if self.check_if_canceled("Crawler async operation"):
|
|
|
|
|
return
|
|
|
|
|
|
2024-10-21 11:38:41 +08:00
|
|
|
proxy = self._param.proxy if self._param.proxy else None
|
|
|
|
|
async with AsyncWebCrawler(verbose=True, proxy=proxy) as crawler:
|
2026-04-25 15:30:15 +09:00
|
|
|
result = await crawler.arun(url=url, bypass_cache=True)
|
2025-11-11 17:36:48 +08:00
|
|
|
|
|
|
|
|
if self.check_if_canceled("Crawler async operation"):
|
|
|
|
|
return
|
|
|
|
|
|
2026-04-25 15:30:15 +09:00
|
|
|
if self._param.extract_type == "html":
|
2024-10-22 14:16:44 +08:00
|
|
|
return result.cleaned_html
|
2026-04-25 15:30:15 +09:00
|
|
|
elif self._param.extract_type == "markdown":
|
2024-10-22 14:16:44 +08:00
|
|
|
return result.markdown
|
2026-04-25 15:30:15 +09:00
|
|
|
elif self._param.extract_type == "content":
|
2025-09-05 12:31:44 +08:00
|
|
|
return result.extracted_content
|
2024-10-22 14:16:44 +08:00
|
|
|
return result.markdown
|