Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

6.1 KB
Raw Blame
import type { CrawlImplType } from './crawImpl';
import { crawlImpls } from './crawImpl';
import type { CrawlErrorResult, CrawlUniformResult, CrawlUrlRule } from './type';
import { crawUrlRules } from './urlRules';
import { applyUrlRules } from './utils/appUrlRules';
import {
  InvalidUrlError,
  isRetryableCrawlError,
  PageNotFoundError,
  UnsupportedContentError,
} from './utils/errorType';
import { getNonDocumentExtension, normalizeCrawlUrl } from './utils/urlPreflight';

/**
 * Built-in crawler impl order used when no impls are provided. Re-exported from
 * the package entry (`@lobechat/web-crawler`) so callers resolving a
 * user-preferred order can intersect against the same default set the Crawler
 * falls back to.
 */
export const DEFAULT_CRAWL_IMPLS = [
  'jina',
  'naive',
  'search1api',
  'browserless',
] as CrawlImplType[];

/** Pseudo crawler name for failures raised before any provider was contacted. */
const PREFLIGHT_CRAWLER = 'preflight';

interface CrawlOptions {
  impls?: string[];
  /**
   * Impls that URL rules may pick from. Defaults to `impls`.
   *
   * Lets a caller narrow the general fallback order (e.g. to a user's preferred
   * channels) while site-specific rules (PDF, YouTube, ...) can still use any
   * server-enabled impl, so their crawl quality does not regress.
   */
  urlRuleImpls?: string[];
}

/**
 * Build the error payload handed back to the model. Authoritative failures get an
 * explicit "this is final, do not retry" message so the model changes strategy
 * instead of re-issuing the same (billed) request.
 */
const buildErrorData = (error: Error | undefined): CrawlErrorResult => {
  const errorType = error?.name || 'UnknownError';
  const errorMessage = error?.message;
  const retryable = isRetryableCrawlError(error);

  let content: string;

  if (error instanceof PageNotFoundError) {
    content = `Dead link: the server confirmed this page does not exist (HTTP ${error.status}). This is a final answer β€” do not retry this URL; find a different source instead.`;
  } else if (error instanceof InvalidUrlError) {
    content = `Invalid URL: ${errorMessage}. Nothing was fetched β€” pass an absolute http(s) URL.`;
  } else if (error instanceof UnsupportedContentError) {
    content = `Unsupported content: ${errorMessage}. Nothing to read here β€” only crawl HTML/text document pages.`;
  } else {
    content = `Fail to crawl the page. Error type: ${errorType}, error message: ${errorMessage}`;
  }

  return {
    content,
    errorMessage,
    errorType,
    ...(retryable ? {} : { retryable: false }),
  };
};

const toKnownImpls = (impls: string[] | undefined): CrawlImplType[] =>
  impls?.length
    ? (impls.filter((impl) => Object.keys(crawlImpls).includes(impl)) as CrawlImplType[])
    : DEFAULT_CRAWL_IMPLS;

export class Crawler {
  impls: CrawlImplType[];
  urlRuleImpls: CrawlImplType[];

  constructor(options: CrawlOptions = {}) {
    this.impls = toKnownImpls(options.impls);
    this.urlRuleImpls = options.urlRuleImpls ? toKnownImpls(options.urlRuleImpls) : this.impls;
  }

  /**
   * Crawl webpage content
   * @param options Crawl options
   */
  async crawl({
    url,
    impls: userImpls,
    filterOptions: userFilterOptions,
  }: {
    filterOptions?: CrawlUrlRule['filterOptions'];
    impls?: CrawlImplType[];
    url: string;
  }): Promise<CrawlUniformResult> {
    // Preflight: repair or reject the URL before any provider (and its quota) is touched.
    let normalizedUrl = url;

    try {
      normalizedUrl = normalizeCrawlUrl(url);

      const resourceExtension = getNonDocumentExtension(normalizedUrl);
      if (resourceExtension) {
        throw new UnsupportedContentError(
          `URL points to a .${resourceExtension} resource file, which has no readable page content`,
        );
      }
    } catch (error) {
      return {
        crawler: PREFLIGHT_CRAWLER,
        data: buildErrorData(error as Error),
        originalUrl: url,
        transformedUrl: normalizedUrl !== url ? normalizedUrl : undefined,
      };
    }

    // Apply URL rules
    const {
      transformedUrl,
      filterOptions: ruleFilterOptions,
      impls: ruleImpls,
    } = applyUrlRules(normalizedUrl, crawUrlRules);

    // Merge user-provided filter options and rule filter options, user options take priority
    const mergedFilterOptions = {
      ...ruleFilterOptions,
      ...userFilterOptions,
    };

    let finalCrawler: string | undefined;
    let finalError: Error | undefined;

    const filteredRuleImpls = ruleImpls
      ? (ruleImpls.filter((impl) =>
          this.urlRuleImpls.includes(impl as CrawlImplType),
        ) as CrawlImplType[])
      : undefined;
    const systemImpls = (
      filteredRuleImpls?.length ? filteredRuleImpls : this.impls
    ) as CrawlImplType[];

    const finalImpls = userImpls
      ? (userImpls.filter((impl) => Object.keys(crawlImpls).includes(impl)) as CrawlImplType[])
      : systemImpls;

    // Try each implementation in the built-in order
    for (const impl of finalImpls) {
      try {
        const res = await crawlImpls[impl](transformedUrl, { filterOptions: mergedFilterOptions });

        if (res && res.content && res.content.length > 100) {
          return {
            crawler: impl,
            data: res,
            originalUrl: url,
            transformedUrl: transformedUrl !== url ? transformedUrl : undefined,
          };
        }

        console.error(
          `[${impl}] returned empty or short content (length: ${res?.content?.length ?? 0})`,
        );
        finalError = new Error(`${impl} returned empty or short content`);
        finalError.name = 'EmptyCrawlResultError';
        finalCrawler = impl;
      } catch (error) {
        console.error(`[${impl}]`, error);
        finalError = error as Error;
        finalCrawler = impl;

        // A confirmed 404/410 is the page's final answer; asking the next provider
        // only buys the same "not found" again.
        if (error instanceof PageNotFoundError) break;
      }
    }

    return {
      crawler: finalCrawler || finalImpls.at(-1) || 'unknown',
      data: buildErrorData(finalError),
      originalUrl: url,
      transformedUrl: transformedUrl !== url ? transformedUrl : undefined,
    };
  }
}