| 1 | import { URL } from "node:url"; |
| 2 | |
| 3 | import { ContinueConfig, IDE } from "../../.."; |
| 4 | |
| 5 | import CheerioCrawler from "./CheerioCrawler"; |
| 6 | import { ChromiumCrawler, ChromiumInstaller } from "./ChromiumCrawler"; |
| 7 | import { DefaultCrawler } from "./DefaultCrawler"; |
| 8 | import GitHubCrawler from "./GitHubCrawler"; |
| 9 | |
| 10 | export type PageData = { |
| 11 | url: string; |
| 12 | path: string; |
| 13 | content: string; |
| 14 | }; |
| 15 | |
| 16 | export type DocsCrawlerType = "default" | "cheerio" | "chromium" | "github"; |
| 17 | |
| 18 | class DocsCrawler { |
| 19 | private readonly GITHUB_HOST = "github.com"; |
| 20 | private readonly chromiumInstaller: ChromiumInstaller; |
| 21 | |
| 22 | constructor( |
| 23 | private readonly ide: IDE, |
| 24 | private readonly config: ContinueConfig, |
| 25 | private readonly maxDepth: number = 4, |
| 26 | private readonly maxRequestsPerCrawl: number = 1000, |
| 27 | private readonly useLocalCrawling: boolean = false, |
| 28 | private readonly githubToken: string | undefined = undefined, |
| 29 | ) { |
| 30 | this.chromiumInstaller = new ChromiumInstaller(this.ide, this.config); |
| 31 | } |
| 32 | |
| 33 | private shouldUseChromium() { |
| 34 | return ( |
| 35 | this.config.experimental?.useChromiumForDocsCrawling && |
| 36 | this.chromiumInstaller.isInstalled() |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | Returns the type of crawler used in the end |
| 42 | */ |
| 43 | async *crawl( |
| 44 | startUrl: URL, |
| 45 | ): AsyncGenerator<PageData, DocsCrawlerType, undefined> { |
| 46 | if (startUrl.host === this.GITHUB_HOST) { |
| 47 | yield* new GitHubCrawler(startUrl, this.githubToken).crawl(); |
| 48 | return "github"; |
| 49 | } |
| 50 | |
| 51 | if (!this.useLocalCrawling) { |
| 52 | try { |
| 53 | const pageData = await new DefaultCrawler( |
| 54 | startUrl, |
| 55 | this.maxRequestsPerCrawl, |
| 56 | this.maxDepth, |
| 57 | ).crawl(); |
| 58 | if (pageData.length > 0) { |
| 59 | yield* pageData; |
| 60 | return "default"; |
| 61 | } |
| 62 | } catch (e) { |
| 63 | console.error("Default crawler failed, trying backup: ", e); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (this.shouldUseChromium()) { |
| 68 | yield* new ChromiumCrawler( |
| 69 | startUrl, |
| 70 | this.maxRequestsPerCrawl, |
| 71 | this.maxDepth, |
| 72 | ).crawl(); |
| 73 | return "chromium"; |
| 74 | } else { |
| 75 | let didCrawlSinglePage = false; |
| 76 | |
| 77 | for await (const pageData of new CheerioCrawler( |
| 78 | startUrl, |
| 79 | this.maxRequestsPerCrawl, |
| 80 | this.maxDepth, |
| 81 | ).crawl()) { |
| 82 | yield pageData; |
| 83 | didCrawlSinglePage = true; |
| 84 | } |
| 85 | |
| 86 | // We assume that if we failed to crawl a single page, |
| 87 | // it was due to an error that using Chromium can resolve |
| 88 | const shouldProposeUseChromium = |
| 89 | !didCrawlSinglePage && |
| 90 | this.chromiumInstaller.shouldProposeUseChromiumOnCrawlFailure(); |
| 91 | |
| 92 | if (shouldProposeUseChromium) { |
| 93 | const didInstall = |
| 94 | await this.chromiumInstaller.proposeAndAttemptInstall( |
| 95 | startUrl.toString(), |
| 96 | ); |
| 97 | |
| 98 | if (didInstall) { |
| 99 | void this.ide.showToast( |
| 100 | "info", |
| 101 | `Successfully installed Chromium! Retrying crawl of: ${startUrl.toString()}`, |
| 102 | ); |
| 103 | |
| 104 | yield* new ChromiumCrawler( |
| 105 | startUrl, |
| 106 | this.maxRequestsPerCrawl, |
| 107 | this.maxDepth, |
| 108 | ).crawl(); |
| 109 | return "chromium"; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return "cheerio"; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | export default DocsCrawler; |
| 119 | export { CheerioCrawler, ChromiumCrawler, ChromiumInstaller, GitHubCrawler }; |