Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

7.7 KB
Raw Blame
"""Fetch a historical URL through the existing page-fetch pipeline."""

from typing import Any

from langchain_core.tools import tool
from loguru import logger

from ...security import redact_url_for_log, sanitize_error_for_agent
from ...security.egress.policy import PolicyDeniedError, evaluate_engine
from ...utilities.type_utils import resolve_boolean_setting
from ...web_search_engines.engine_availability import failure_from_exception
from ...web_search_engines.wayback_client import (
    AVAILABLE_API,
    lookup_archive_snapshot,
    normalize_archive_date,
    validate_capture_fetch_url,
    validate_archive_url,
)
from .fetch import (
    FETCH_MODES,
    _check_and_record_fetch,
    _enforce_url_policy,
    _read_content_max_length_setting,
    _read_max_fetches_setting,
    _read_summary_input_max_length_setting,
    _summary_invocation_budget,
    build_fetch_tool,
)


def _enforce_archive_engine(egress_context, settings_snapshot):
    if egress_context is not None:
        decision = evaluate_engine(
            "wayback", egress_context, settings_snapshot=settings_snapshot or {}
        )
        if not decision.allowed:
            raise PolicyDeniedError(decision, target=AVAILABLE_API)


def build_archive_fetch_tool(
    mode: str,
    collector: Any,
    *,
    model=None,
    overall_query: str = "",
    settings_snapshot: dict | None = None,
    egress_context=None,
    max_fetches: int | None = None,
    subagent_id: str | None = None,
    availability=None,
):
    """Build fetch_archive, following the run's fetch mode and shared quota."""
    if mode not in FETCH_MODES:
        raise ValueError(f"Unknown fetch mode {mode!r}")
    if mode == "disabled":
        return None
    if not resolve_boolean_setting(
        settings_snapshot, "search.engine.web.wayback.agent_enabled", True
    ):
        return None
    if (
        egress_context is not None
        and not evaluate_engine(
            "wayback", egress_context, settings_snapshot=settings_snapshot or {}
        ).allowed
    ):
        return None
    if mode != "full" and model is None:
        raise ValueError(f"{mode} fetch mode requires a model")
    limit = (
        max_fetches
        if max_fetches is not None
        else _read_max_fetches_setting(settings_snapshot)
    )
    description = (
        "Read an archived version of one absolute HTTP(S) URL. Discover URLs "
        "with a configured search provider first, or use a URL supplied by the user. "
        "Optionally pass date as YYYY-MM-DD to select the closest available capture; "
        "omit date for the most recent available capture. The actual capture date "
        "can differ from the requested date. Never substitutes the live page. "
        "Shares the fetch_content budget and fetch mode."
    )
    if mode != "full":
        description += " Pass focus as the specific question to extract from the archived page."

    def fetch(url: str, date: str | None, focus: str | None = None) -> str:
        try:
            url = validate_archive_url(url)
            timestamp = normalize_archive_date(date)
            if availability is not None:
                state = availability.state("wayback")
                if state is not None:
                    return state.describe()
            _enforce_archive_engine(egress_context, settings_snapshot)
            # Check the target before sending it to an external archive, and
            # gate the API before reserving the shared quota slot.
            _enforce_url_policy(url, egress_context)
            _enforce_url_policy(AVAILABLE_API, egress_context)
            # Refuse an invalid configured budget before archive lookup or
            # reservation of a shared fetch slot. The delegated page tool
            # applies the same budget to the selected capture.
            if mode == "full":
                _read_content_max_length_setting(settings_snapshot)
            else:
                _read_summary_input_max_length_setting(settings_snapshot)
                _summary_invocation_budget(model, settings_snapshot)
            error, count = _check_and_record_fetch(
                collector, limit, subagent_id=subagent_id
            )
            if error is not None:
                return error
            snapshot = lookup_archive_snapshot(url, timestamp)
            if snapshot is None:
                if availability is not None:
                    availability.record_success("wayback")
                return f"No accessible archive capture was found for {url}. Try another date or URL."
            replay = snapshot["url"]
            embedded_target = replay.split(f"/web/{snapshot['timestamp']}/", 1)[
                1
            ]
            _enforce_url_policy(embedded_target, egress_context)
            _enforce_url_policy(replay, egress_context)
            metadata = {
                "source_type": "archive",
                "original_url": snapshot["original_url"],
                "requested_url": snapshot["requested_url"],
                "timestamp": snapshot["timestamp"],
                "formatted_date": snapshot["formatted_date"],
            }

            def capture_guard(candidate):
                validate_capture_fetch_url(
                    candidate, snapshot["timestamp"], snapshot["original_url"]
                )

            page_fetch = build_fetch_tool(
                mode,
                collector,
                model=model,
                overall_query=overall_query,
                settings_snapshot=settings_snapshot,
                egress_context=egress_context,
                max_fetches=limit,
                subagent_id=subagent_id,
                record_fetch=False,
                source_metadata=metadata,
                url_guard=capture_guard,
                availability=availability,
            )
            arguments: dict[str, Any] = {"url": replay}
            if mode != "full":
                arguments["focus"] = focus
            message = page_fetch.invoke(arguments)
            # Shared fetching displays provenance on successful reads,
            # including ordinary rereads of the resulting citation.
            if date is not None:
                message += (
                    f"\nRequested date: {date} (closest available capture)"
                )
            if limit > 0 and count is not None:
                message += f"\n\n({count} of {limit} page fetches used)"
            return message
        except PolicyDeniedError as exc:
            return sanitize_error_for_agent(
                f"Cannot fetch archive: blocked by egress policy ({exc.decision.reason}). "
                "Use an allowed source or choose an appropriate search provider."
            )
        except Exception as exc:
            logger.opt(exception=False).warning(
                "Archive fetch failed for {} ({})",
                redact_url_for_log(url),
                type(exc).__name__,
            )
            failure = failure_from_exception(exc)
            if availability is not None and failure is not None:
                return availability.disable("wayback", failure).describe()
            return sanitize_error_for_agent(f"Cannot fetch archive: {exc}")

    if mode == "full":

        @tool("fetch_archive", description=description)
        def fetch_archive(url: str, date: str | None = None) -> str:
            """Read the archived page at URL, optionally near a calendar date."""
            return fetch(url, date)

        return fetch_archive

    @tool("fetch_archive", description=description)
    def fetch_archive_summary(
        url: str, focus: str, date: str | None = None
    ) -> str:
        """Extract facts relevant to focus from an archived page."""
        return fetch(url, date, focus)

    return fetch_archive_summary