Merge pull request #2080 from continuedev/pe/docs-sitemap
feat: use Puppeteer for docs crawling
This commit is contained in:
commit
0d85a1d3fa
18 changed files with 1087 additions and 601 deletions
2
.vscode/tasks.json
vendored
2
.vscode/tasks.json
vendored
|
|
@ -45,8 +45,6 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
// Tsc currently errors out due to testing setup issues, will be resolved in a different PR
|
||||
// This will be useful for preventing debugging if there are compile errors
|
||||
{
|
||||
"label": "vscode-extension:tsc",
|
||||
"type": "npm",
|
||||
|
|
|
|||
4
binary/package-lock.json
generated
4
binary/package-lock.json
generated
|
|
@ -49,6 +49,7 @@
|
|||
"@aws-sdk/credential-providers": "^3.620.1",
|
||||
"@continuedev/config-types": "^1.0.10",
|
||||
"@continuedev/llm-info": "^1.0.1",
|
||||
"@crawlee/playwright": "^3.11.1",
|
||||
"@mozilla/readability": "^0.5.0",
|
||||
"@octokit/rest": "^20.0.2",
|
||||
"@typescript-eslint/eslint-plugin": "^7.8.0",
|
||||
|
|
@ -57,11 +58,11 @@
|
|||
"adf-to-md": "^1.1.0",
|
||||
"async-mutex": "^0.5.0",
|
||||
"axios": "^1.6.7",
|
||||
"cheerio": "^1.0.0-rc.12",
|
||||
"commander": "^12.0.0",
|
||||
"comment-json": "^4.2.3",
|
||||
"dbinfoz": "^0.1.4",
|
||||
"dotenv": "^16.4.5",
|
||||
"extract-zip": "^2.0.1",
|
||||
"fastest-levenshtein": "^1.0.16",
|
||||
"follow-redirects": "^1.15.5",
|
||||
"handlebars": "^4.7.8",
|
||||
|
|
@ -81,6 +82,7 @@
|
|||
"openai": "^4.20.1",
|
||||
"p-limit": "^6.1.0",
|
||||
"pg": "^8.11.3",
|
||||
"playwright": "^1.46.1",
|
||||
"posthog-node": "^3.6.3",
|
||||
"quick-lru": "^7.0.0",
|
||||
"replicate": "^0.26.0",
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ import { editConfigJson } from "./util/paths";
|
|||
import { Telemetry } from "./util/posthog";
|
||||
import { TTS } from "./util/tts";
|
||||
import { streamDiffLines } from "./util/verticalEdit";
|
||||
import DocsCrawler from "./indexing/docs/DocsCrawler";
|
||||
|
||||
export class Core {
|
||||
// implements IMessenger<ToCoreProtocol, FromCoreProtocol>
|
||||
|
|
@ -168,6 +169,12 @@ export class Core {
|
|||
(..._) => Promise.resolve([]),
|
||||
);
|
||||
|
||||
try {
|
||||
DocsCrawler.verifyOrInstallChromium();
|
||||
} catch (err) {
|
||||
console.debug(`Failed to install Chromium: ${err}`);
|
||||
}
|
||||
|
||||
const on = this.messenger.on.bind(this.messenger);
|
||||
|
||||
this.messenger.onError((err) => {
|
||||
|
|
|
|||
130
core/indexing/docs/DocsCrawler.test.ts
Normal file
130
core/indexing/docs/DocsCrawler.test.ts
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
import DocsCrawler, { type PageData } from "./DocsCrawler";
|
||||
import preIndexedDocs from "./preIndexedDocs";
|
||||
|
||||
// Temporary workaround until we have better caching of Chromium
|
||||
// download between test runs
|
||||
const TIMEOUT = 1_000_000;
|
||||
|
||||
// Skipped until we have a better way to cache Chromium installs
|
||||
// between tests and in CI
|
||||
describe.skip("crawl", () => {
|
||||
describe("GitHub repositories", () => {
|
||||
const repoUrl =
|
||||
"https://github.com/Patrick-Erichsen/test-github-repo-for-crawling";
|
||||
|
||||
let crawlResults: PageData[];
|
||||
|
||||
beforeAll(async () => {
|
||||
const docsCrawler = new DocsCrawler(new URL(repoUrl));
|
||||
|
||||
crawlResults = [];
|
||||
|
||||
for await (const page of docsCrawler.crawl()) {
|
||||
crawlResults.push(page);
|
||||
}
|
||||
}, TIMEOUT);
|
||||
|
||||
test("finds expected markdown files", async () => {
|
||||
expect(crawlResults.some((page) => page.path.endsWith("README.md"))).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
expect(
|
||||
crawlResults.some((page) => page.path.endsWith("docs-depth-1.md")),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test("html includes the correct content", async () => {
|
||||
const doc = crawlResults.find((page) =>
|
||||
page.path.endsWith("docs-depth-1.md"),
|
||||
);
|
||||
|
||||
expect(doc?.content.includes("Test body")).toBe(true);
|
||||
expect(doc?.content.includes("Test H2 Header")).toBe(true);
|
||||
});
|
||||
|
||||
test("ignores non-markdown files, e.g. `test.js` at the root", async () => {
|
||||
expect(crawlResults.some((page) => page.path.endsWith("test.js"))).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe.only("Non-Github repository sites", () => {
|
||||
it(
|
||||
"succeeds in crawling a list of common sites",
|
||||
async () => {
|
||||
const TEST_SITES = [
|
||||
"https://docs.nestjs.com/",
|
||||
"https://docs.nestjs.com/",
|
||||
"https://go.dev/doc/",
|
||||
"https://clickhouse.com/docs",
|
||||
"https://www.tensorflow.org/api_docs",
|
||||
"https://www.rust-lang.org/learn",
|
||||
"https://docs.anthropic.com/en/docs",
|
||||
];
|
||||
|
||||
const NUM_PAGES_TO_CRAWL = 10;
|
||||
|
||||
for (const site of TEST_SITES) {
|
||||
const crawlResults: PageData[] = [];
|
||||
const docsCrawler = new DocsCrawler(
|
||||
new URL(site),
|
||||
NUM_PAGES_TO_CRAWL,
|
||||
);
|
||||
|
||||
for await (const page of docsCrawler.crawl()) {
|
||||
crawlResults.push(page);
|
||||
}
|
||||
|
||||
// `toBeGreaterThanOrEqual` because Crawlee doesn't guarantee
|
||||
// an exact number of pages to crawl since it runs in parallel
|
||||
expect(crawlResults.length).toBeGreaterThanOrEqual(
|
||||
NUM_PAGES_TO_CRAWL,
|
||||
);
|
||||
}
|
||||
},
|
||||
TIMEOUT,
|
||||
);
|
||||
});
|
||||
|
||||
describe("Pre-indexed Docs", () => {
|
||||
const results: { [url: string]: boolean } = {};
|
||||
let totalTests = 0;
|
||||
let passedTests = 0;
|
||||
|
||||
Object.keys(preIndexedDocs).forEach((url) => {
|
||||
test(
|
||||
`Crawl test for ${url}`,
|
||||
async () => {
|
||||
totalTests++;
|
||||
let pageFound = false;
|
||||
|
||||
try {
|
||||
const docsCrawler = new DocsCrawler(new URL(url), 1);
|
||||
|
||||
for await (const page of docsCrawler.crawl()) {
|
||||
if (page.url === url) {
|
||||
pageFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error crawling ${url}:`, error);
|
||||
}
|
||||
|
||||
results[url] = pageFound;
|
||||
if (pageFound) {
|
||||
passedTests++;
|
||||
console.log(`✅ ${url}`);
|
||||
} else {
|
||||
console.log(`❌ ${url}`);
|
||||
}
|
||||
|
||||
expect(pageFound).toBe(true);
|
||||
},
|
||||
TIMEOUT,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
248
core/indexing/docs/DocsCrawler.ts
Normal file
248
core/indexing/docs/DocsCrawler.ts
Normal file
|
|
@ -0,0 +1,248 @@
|
|||
import { Octokit } from "@octokit/rest";
|
||||
import * as fs from "fs";
|
||||
import { URL } from "node:url";
|
||||
import { Page } from "puppeteer";
|
||||
import { getChromiumPath, getContinueUtilsPath } from "../../util/paths";
|
||||
// @ts-ignore
|
||||
// @prettier-ignore
|
||||
import PCR from "puppeteer-chromium-resolver";
|
||||
|
||||
export type PageData = {
|
||||
url: string;
|
||||
path: string;
|
||||
content: string;
|
||||
};
|
||||
|
||||
export default class DocsCrawler {
|
||||
LINK_GROUP_SIZE = 2; // Controls parallelization of crawler
|
||||
GITHUB_HOST = "github.com";
|
||||
static MAX_REQUESTS_PER_CRAWL = 1000;
|
||||
markdownRegex = new RegExp(/\.(md|mdx)$/);
|
||||
octokit = new Octokit({
|
||||
auth: undefined,
|
||||
});
|
||||
|
||||
static PCR_CONFIG = {
|
||||
downloadPath: getContinueUtilsPath(),
|
||||
};
|
||||
|
||||
constructor(
|
||||
private readonly startUrl: URL,
|
||||
private readonly maxRequestsPerCrawl: number = DocsCrawler.MAX_REQUESTS_PER_CRAWL,
|
||||
) {}
|
||||
|
||||
static verifyOrInstallChromium() {
|
||||
if (!fs.existsSync(getChromiumPath())) {
|
||||
PCR(DocsCrawler.PCR_CONFIG);
|
||||
}
|
||||
}
|
||||
|
||||
async *crawl(): AsyncGenerator<PageData> {
|
||||
if (this.startUrl.host === this.GITHUB_HOST) {
|
||||
yield* this.crawlGithubRepo();
|
||||
} else {
|
||||
yield* this.crawlSite();
|
||||
}
|
||||
}
|
||||
|
||||
private async getGithubRepoDefaultBranch(
|
||||
owner: string,
|
||||
repo: string,
|
||||
): Promise<string> {
|
||||
const repoInfo = await this.octokit.repos.get({
|
||||
owner,
|
||||
repo,
|
||||
});
|
||||
|
||||
return repoInfo.data.default_branch;
|
||||
}
|
||||
|
||||
private async getGitHubRepoPaths(
|
||||
owner: string,
|
||||
repo: string,
|
||||
branch: string,
|
||||
) {
|
||||
const tree = await this.octokit.request(
|
||||
"GET /repos/{owner}/{repo}/git/trees/{tree_sha}",
|
||||
{
|
||||
owner,
|
||||
repo,
|
||||
tree_sha: branch,
|
||||
headers: {
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
recursive: "true",
|
||||
},
|
||||
);
|
||||
|
||||
const paths = tree.data.tree
|
||||
.filter(
|
||||
(file: any) =>
|
||||
file.type === "blob" && this.markdownRegex.test(file.path ?? ""),
|
||||
)
|
||||
.map((file: any) => file.path);
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
private async getGithubRepoFileContent(
|
||||
path: string,
|
||||
owner: string,
|
||||
repo: string,
|
||||
) {
|
||||
try {
|
||||
const response = await this.octokit.repos.getContent({
|
||||
owner,
|
||||
repo,
|
||||
path,
|
||||
headers: {
|
||||
Accept: "application/vnd.github.raw+json",
|
||||
},
|
||||
});
|
||||
|
||||
return response.data as unknown as string;
|
||||
} catch (error) {
|
||||
console.debug("Error fetching file contents:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private async *crawlGithubRepo() {
|
||||
console.debug(`Crawling GitHub repo: ${this.startUrl.toString()}`);
|
||||
|
||||
const urlStr = this.startUrl.toString();
|
||||
const [_, owner, repo] = this.startUrl.pathname.split("/");
|
||||
const branch = await this.getGithubRepoDefaultBranch(owner, repo);
|
||||
const paths = await this.getGitHubRepoPaths(owner, repo, branch);
|
||||
|
||||
for await (const path of paths) {
|
||||
const content = await this.getGithubRepoFileContent(path, owner, repo);
|
||||
|
||||
yield {
|
||||
path,
|
||||
url: urlStr,
|
||||
content: content ?? "",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private stripHashFromUrl(urlStr: string) {
|
||||
try {
|
||||
let url = new URL(urlStr);
|
||||
url.hash = "";
|
||||
return url;
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private isValidHostAndPath(newUrl: URL, curUrl: URL) {
|
||||
return (
|
||||
newUrl.pathname.startsWith(curUrl.pathname) && newUrl.host === curUrl.host
|
||||
);
|
||||
}
|
||||
|
||||
private async getLinksFromPage(page: Page, curUrl: URL) {
|
||||
const links = await page.$$eval("a", (links) => links.map((a) => a.href));
|
||||
|
||||
// This additional step is due to issues getting the URL module to work
|
||||
// in an $$eval command
|
||||
const cleanedLinks = links
|
||||
.map(this.stripHashFromUrl)
|
||||
.filter(
|
||||
(newUrl) =>
|
||||
newUrl !== null &&
|
||||
this.isValidHostAndPath(newUrl, curUrl) &&
|
||||
newUrl !== curUrl,
|
||||
)
|
||||
.map((newUrl) => (newUrl as URL).href);
|
||||
|
||||
const dedupedLinks = Array.from(new Set(cleanedLinks));
|
||||
|
||||
return dedupedLinks;
|
||||
}
|
||||
|
||||
private async getLinkGroupsFromPage(page: Page, curUrl: URL) {
|
||||
const links = await this.getLinksFromPage(page, curUrl);
|
||||
|
||||
const groups = links.reduce((acc, link, i) => {
|
||||
const groupIndex = Math.floor(i / this.LINK_GROUP_SIZE);
|
||||
|
||||
if (!acc[groupIndex]) {
|
||||
acc.push([]);
|
||||
}
|
||||
|
||||
acc[groupIndex].push(link);
|
||||
|
||||
return acc;
|
||||
}, [] as string[][]);
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
private async *crawlSitePages(
|
||||
page: Page,
|
||||
curUrl: URL,
|
||||
visitedLinks: Set<string> = new Set(),
|
||||
curRequestCount: number = 0,
|
||||
): AsyncGenerator<PageData> {
|
||||
if (curRequestCount >= this.maxRequestsPerCrawl) {
|
||||
console.warn("Max requests per crawl reached. Stopping crawler.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (visitedLinks.has(curUrl.toString())) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.debug(`Crawling page: ${curUrl.toString()}`);
|
||||
|
||||
await page.goto(curUrl.toString());
|
||||
|
||||
const htmlContent = await page.content();
|
||||
const linkGroups = await this.getLinkGroupsFromPage(page, curUrl);
|
||||
const requestCount = curRequestCount + 1;
|
||||
|
||||
visitedLinks.add(curUrl.toString());
|
||||
|
||||
yield {
|
||||
path: curUrl.pathname,
|
||||
url: curUrl.toString(),
|
||||
content: htmlContent,
|
||||
};
|
||||
|
||||
for (const linkGroup of linkGroups) {
|
||||
for (const link of linkGroup) {
|
||||
yield* this.crawlSitePages(
|
||||
page,
|
||||
new URL(link),
|
||||
visitedLinks,
|
||||
requestCount,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async *crawlSite(): AsyncGenerator<PageData> {
|
||||
console.debug(`Crawling site repo: ${this.startUrl}`);
|
||||
|
||||
const stats = await PCR(DocsCrawler.PCR_CONFIG);
|
||||
|
||||
const browser = await stats.puppeteer.launch({
|
||||
args: [
|
||||
"--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36",
|
||||
],
|
||||
executablePath: stats.executablePath,
|
||||
});
|
||||
|
||||
const page = await browser.newPage();
|
||||
|
||||
try {
|
||||
yield* this.crawlSitePages(page, this.startUrl);
|
||||
} catch (e) {
|
||||
console.debug("Error getting links: ", e);
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
import { open, type Database } from "sqlite";
|
||||
import sqlite3 from "sqlite3";
|
||||
import lancedb, { Connection } from "vectordb";
|
||||
import { ConfigHandler } from "../../config/ConfigHandler.js";
|
||||
import DocsContextProvider from "../../context/providers/DocsContextProvider.js";
|
||||
import { ConfigHandler } from "../../config/ConfigHandler";
|
||||
import DocsContextProvider from "../../context/providers/DocsContextProvider";
|
||||
import {
|
||||
Chunk,
|
||||
ContinueConfig,
|
||||
|
|
@ -10,27 +10,27 @@ import {
|
|||
IDE,
|
||||
IndexingProgressUpdate,
|
||||
SiteIndexingConfig,
|
||||
} from "../../index.js";
|
||||
import { FromCoreProtocol, ToCoreProtocol } from "../../protocol/index.js";
|
||||
import { GlobalContext } from "../../util/GlobalContext.js";
|
||||
import { IMessenger } from "../../util/messenger.js";
|
||||
} from "../..";
|
||||
import { FromCoreProtocol, ToCoreProtocol } from "../../protocol";
|
||||
import { GlobalContext } from "../../util/GlobalContext";
|
||||
import { IMessenger } from "../../util/messenger";
|
||||
import {
|
||||
editConfigJson,
|
||||
getDocsSqlitePath,
|
||||
getLanceDbPath,
|
||||
} from "../../util/paths.js";
|
||||
import { Telemetry } from "../../util/posthog.js";
|
||||
import TransformersJsEmbeddingsProvider from "../embeddings/TransformersJsEmbeddingsProvider.js";
|
||||
import { Article, chunkArticle, pageToArticle } from "./article.js";
|
||||
import { crawlPage } from "./crawl.js";
|
||||
import { runLanceMigrations, runSqliteMigrations } from "./migrations.js";
|
||||
} from "../../util/paths";
|
||||
import { Telemetry } from "../../util/posthog";
|
||||
import TransformersJsEmbeddingsProvider from "../embeddings/TransformersJsEmbeddingsProvider";
|
||||
import { Article, chunkArticle, pageToArticle } from "./article";
|
||||
import DocsCrawler from "./DocsCrawler";
|
||||
import { runLanceMigrations, runSqliteMigrations } from "./migrations";
|
||||
import {
|
||||
downloadFromS3,
|
||||
getS3Filename,
|
||||
S3Buckets,
|
||||
SiteIndexingResults,
|
||||
} from "./preIndexed.js";
|
||||
import preIndexedDocs from "./preIndexedDocs.js";
|
||||
} from "./preIndexed";
|
||||
import preIndexedDocs from "./preIndexedDocs";
|
||||
|
||||
// Purposefully lowercase because lancedb converts
|
||||
export interface LanceDbDocsRow {
|
||||
|
|
@ -112,8 +112,8 @@ export default class DocsService {
|
|||
}
|
||||
|
||||
/*
|
||||
* Currently, we generate and host embeddings for pre-indexed docs using transformers.js.
|
||||
* However, we don't ship transformers.js with the JetBrains extension.
|
||||
* Currently, we generate and host embeddings for pre-indexed docs using transformers.
|
||||
* However, we don't ship transformers with the JetBrains extension.
|
||||
* So, we only include pre-indexed docs in the submenu for non-JetBrains IDEs.
|
||||
*/
|
||||
async canUsePreindexedDocs() {
|
||||
|
|
@ -202,11 +202,10 @@ export default class DocsService {
|
|||
let processedPages = 0;
|
||||
let maxKnownPages = 1;
|
||||
|
||||
const docsCrawler = new DocsCrawler(new URL(startUrl));
|
||||
|
||||
// Crawl pages and retrieve info as articles
|
||||
for await (const page of crawlPage(
|
||||
new URL(startUrl),
|
||||
siteIndexingConfig.maxDepth,
|
||||
)) {
|
||||
for await (const page of docsCrawler.crawl()) {
|
||||
processedPages++;
|
||||
|
||||
const article = pageToArticle(page);
|
||||
|
|
@ -279,6 +278,8 @@ export default class DocsService {
|
|||
status: "failed",
|
||||
};
|
||||
|
||||
this.docsIndexingQueue.delete(startUrl);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -521,8 +522,9 @@ export default class DocsService {
|
|||
private async getLanceTableNameFromEmbeddingsProvider(
|
||||
isPreIndexedDoc: boolean,
|
||||
) {
|
||||
const embeddingsProvider =
|
||||
await this.getEmbeddingsProvider(isPreIndexedDoc);
|
||||
const embeddingsProvider = await this.getEmbeddingsProvider(
|
||||
isPreIndexedDoc,
|
||||
);
|
||||
const embeddingsProviderId = this.removeInvalidLanceTableNameChars(
|
||||
embeddingsProvider.id,
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
import { Readability } from "@mozilla/readability";
|
||||
import { JSDOM } from "jsdom";
|
||||
import { Chunk } from "../../index.js";
|
||||
import { cleanFragment, cleanHeader } from "../chunk/markdown.js";
|
||||
import { PageData } from "./crawl.js";
|
||||
import { Chunk } from "../../";
|
||||
import { cleanFragment, cleanHeader } from "../chunk/markdown";
|
||||
import { type PageData } from "./DocsCrawler";
|
||||
|
||||
export type ArticleComponent = {
|
||||
title: string;
|
||||
|
|
@ -148,7 +148,7 @@ export function stringToArticle(
|
|||
|
||||
export function pageToArticle(page: PageData): Article | undefined {
|
||||
try {
|
||||
return stringToArticle(page.url, page.html, page.path);
|
||||
return stringToArticle(page.url, page.content, page.path);
|
||||
} catch (err) {
|
||||
console.error("Error converting URL to article components", err);
|
||||
return undefined;
|
||||
|
|
|
|||
|
|
@ -1,105 +0,0 @@
|
|||
import { crawlPage, PageData } from "../../indexing/docs/crawl";
|
||||
|
||||
describe("crawlPage - GitHub repository", () => {
|
||||
const repoUrl = new URL(
|
||||
"https://github.com/Patrick-Erichsen/test-github-repo-for-crawling",
|
||||
);
|
||||
const maxDepth = 3;
|
||||
let crawlResults: PageData[];
|
||||
|
||||
beforeAll(async () => {
|
||||
crawlResults = [];
|
||||
for await (const page of crawlPage(repoUrl, maxDepth)) {
|
||||
crawlResults.push(page);
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
test("finds expected markdown files", async () => {
|
||||
expect(crawlResults.some((page) => page.path.endsWith("README.md"))).toBe(
|
||||
true,
|
||||
);
|
||||
|
||||
expect(
|
||||
crawlResults.some((page) => page.path.endsWith("docs-depth-1.md")),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test("html includes the correct content", async () => {
|
||||
const doc = crawlResults.find((page) =>
|
||||
page.path.endsWith("docs-depth-1.md"),
|
||||
);
|
||||
|
||||
expect(doc?.html.includes("Test body")).toBe(true);
|
||||
expect(doc?.html.includes("Test H2 Header")).toBe(true);
|
||||
});
|
||||
|
||||
// This test is currently failing, `maxDepth` does not appear to be followed right now
|
||||
test.todo(
|
||||
"ignores files past the max depth",
|
||||
// , () => {
|
||||
// expect(
|
||||
// crawlResults.some((page) => page.path.includes("docs-depth-3")),
|
||||
// ).toBe(false);
|
||||
// }
|
||||
);
|
||||
|
||||
test("ignores non-markdown files, e.g. `test.js` at the root", async () => {
|
||||
expect(crawlResults.some((page) => page.path.endsWith("test.js"))).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
async function testSite(url: string, expectedPaths: string[]) {
|
||||
test(`crawlPage - ${url}`, async () => {
|
||||
const crawlResults: PageData[] = [];
|
||||
for await (const page of crawlPage(new URL(url))) {
|
||||
crawlResults.push(page);
|
||||
}
|
||||
|
||||
console.log(crawlResults.map((page) => page.path));
|
||||
|
||||
// Check if all expected paths are found
|
||||
for (const expectedPath of expectedPaths) {
|
||||
expect(
|
||||
crawlResults.some((page) => page.path.endsWith(expectedPath)),
|
||||
).toBe(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const SITES: { [rootUrl: string]: string[] } = {
|
||||
"https://docs.continue.dev": [
|
||||
"intro",
|
||||
"quickstart",
|
||||
"features/tab-autocomplete",
|
||||
"reference/Model%20Providers/mistral",
|
||||
],
|
||||
// Failing
|
||||
// "https://docs.astro.build/en/getting-started/": ["https://docs.astro.build/en/getting-started/"],
|
||||
// Failing
|
||||
// "https://docs.nestjs.com/": ["first-steps"],
|
||||
// Taking too much time
|
||||
// "https://go.dev/doc/": ["install", "tutorial/getting-started", "comment"],
|
||||
// Taking too much time
|
||||
// "https://clickhouse.com/docs": [
|
||||
// "intro",
|
||||
// "getting-started/quick-start",
|
||||
// "migrations/bigquery/migrating-to-clickhouse-cloud",
|
||||
// ],
|
||||
// Taking too much time
|
||||
// "https://www.tensorflow.org/api_docs": ["python/tf/Variable"],
|
||||
// "https://www.rust-lang.org/learn": []
|
||||
// Taking too long
|
||||
// "https://docs.anthropic.com/en/docs": [
|
||||
// "welcome",
|
||||
// "quickstart",
|
||||
// "build-with-claude/prompt-engineering/prompt-generator",
|
||||
// ],
|
||||
};
|
||||
|
||||
describe.only("crawlPage should return expected subpages for common sites", () => {
|
||||
for (const [rootUrl, expectedPaths] of Object.entries(SITES)) {
|
||||
testSite(rootUrl, expectedPaths);
|
||||
}
|
||||
});
|
||||
|
|
@ -1,212 +0,0 @@
|
|||
import { Octokit } from "@octokit/rest";
|
||||
import cheerio from "cheerio";
|
||||
import fetch from "node-fetch";
|
||||
import { URL } from "node:url";
|
||||
|
||||
const IGNORE_PATHS_ENDING_IN = [
|
||||
"favicon.ico",
|
||||
"robots.txt",
|
||||
".rst.txt",
|
||||
"genindex",
|
||||
"py-modindex",
|
||||
"search.html",
|
||||
"search",
|
||||
"genindex.html",
|
||||
"changelog",
|
||||
"changelog.html",
|
||||
];
|
||||
|
||||
const markdownRegex = new RegExp(/\.(md|mdx)$/);
|
||||
|
||||
async function getDefaultBranch(owner: string, repo: string): Promise<string> {
|
||||
const octokit = new Octokit({ auth: undefined });
|
||||
|
||||
const repoInfo = await octokit.repos.get({
|
||||
owner,
|
||||
repo,
|
||||
});
|
||||
|
||||
return repoInfo.data.default_branch;
|
||||
}
|
||||
|
||||
async function crawlGithubRepo(baseUrl: URL) {
|
||||
const octokit = new Octokit({
|
||||
auth: undefined,
|
||||
});
|
||||
|
||||
const [_, owner, repo] = baseUrl.pathname.split("/");
|
||||
|
||||
const branch = await getDefaultBranch(owner, repo);
|
||||
console.log("Github repo detected. Crawling", branch, "branch");
|
||||
|
||||
const tree = await octokit.request(
|
||||
"GET /repos/{owner}/{repo}/git/trees/{tree_sha}",
|
||||
{
|
||||
owner,
|
||||
repo,
|
||||
tree_sha: branch,
|
||||
headers: {
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
recursive: "true",
|
||||
},
|
||||
);
|
||||
|
||||
const paths = tree.data.tree
|
||||
.filter(
|
||||
(file: any) =>
|
||||
file.type === "blob" && markdownRegex.test(file.path ?? ""),
|
||||
)
|
||||
.map((file: any) => baseUrl.pathname + "/tree/main/" + file.path);
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
async function getLinksFromUrl(url: string, path: string) {
|
||||
const baseUrl = new URL(url);
|
||||
const location = new URL(path, url);
|
||||
let response;
|
||||
|
||||
try {
|
||||
response = await fetch(location.toString());
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error && error.message.includes("maximum redirect")) {
|
||||
console.error("Maximum redirect reached for: ", location.toString());
|
||||
return {
|
||||
html: "",
|
||||
links: [],
|
||||
};
|
||||
}
|
||||
console.error(error);
|
||||
return {
|
||||
html: "",
|
||||
links: [],
|
||||
};
|
||||
}
|
||||
|
||||
const html = await response.text();
|
||||
let links: string[] = [];
|
||||
|
||||
if (url.includes("github.com")) {
|
||||
return {
|
||||
html,
|
||||
links,
|
||||
};
|
||||
}
|
||||
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
$("a").each((_, element) => {
|
||||
const href = $(element).attr("href");
|
||||
if (!href) {
|
||||
return;
|
||||
}
|
||||
|
||||
const parsedUrl = new URL(href, url);
|
||||
if (
|
||||
parsedUrl.hostname === baseUrl.hostname
|
||||
// parsedUrl.pathname.startsWith(baseUrl.pathname)
|
||||
) {
|
||||
links.push(parsedUrl.pathname);
|
||||
}
|
||||
});
|
||||
|
||||
links = [...new Set(links)].filter((link) => {
|
||||
return (
|
||||
!link.includes("#") &&
|
||||
!IGNORE_PATHS_ENDING_IN.some((ending) => link.endsWith(ending))
|
||||
);
|
||||
});
|
||||
|
||||
return {
|
||||
html,
|
||||
links,
|
||||
};
|
||||
}
|
||||
|
||||
function splitUrl(url: URL) {
|
||||
const baseUrl = `${url.protocol}//${url.hostname}${
|
||||
url.port ? ":" + url.port : ""
|
||||
}`;
|
||||
const basePath = url.pathname;
|
||||
return {
|
||||
baseUrl,
|
||||
basePath,
|
||||
};
|
||||
}
|
||||
|
||||
export type PageData = {
|
||||
url: string;
|
||||
path: string;
|
||||
html: string;
|
||||
};
|
||||
|
||||
export async function* crawlPage(
|
||||
url: URL,
|
||||
maxDepth: number = 3,
|
||||
): AsyncGenerator<PageData> {
|
||||
console.log(
|
||||
`Starting crawl from: ${url.toString()} - Max Depth: ${maxDepth}`,
|
||||
);
|
||||
|
||||
const { baseUrl, basePath } = splitUrl(url);
|
||||
let paths: { path: string; depth: number }[] = [{ path: basePath, depth: 0 }];
|
||||
|
||||
if (url.hostname === "github.com") {
|
||||
const githubLinks = await crawlGithubRepo(url);
|
||||
const githubLinkObjects = githubLinks.map((link) => ({
|
||||
path: link,
|
||||
depth: 0,
|
||||
}));
|
||||
paths = [...paths, ...githubLinkObjects];
|
||||
}
|
||||
|
||||
let index = 0;
|
||||
while (index < paths.length) {
|
||||
const batch = paths.slice(index, index + 50);
|
||||
|
||||
try {
|
||||
const promises = batch.map(({ path, depth }) =>
|
||||
getLinksFromUrl(baseUrl, path).then((links) => ({
|
||||
links,
|
||||
path,
|
||||
depth,
|
||||
})),
|
||||
); // Adjust for depth tracking
|
||||
|
||||
const results = await Promise.all(promises);
|
||||
for (const {
|
||||
links: { html, links: linksArray },
|
||||
path,
|
||||
depth,
|
||||
} of results) {
|
||||
if (html !== "" && depth <= maxDepth) {
|
||||
// Check depth
|
||||
yield {
|
||||
url: url.toString(),
|
||||
path,
|
||||
html,
|
||||
};
|
||||
}
|
||||
|
||||
// Ensure we only add links if within depth limit
|
||||
if (depth < maxDepth) {
|
||||
for (let link of linksArray) {
|
||||
if (!paths.some((p) => p.path === link)) {
|
||||
paths.push({ path: link, depth: depth + 1 }); // Increment depth for new paths
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof TypeError) {
|
||||
console.warn("Error while crawling page: ", e); // Likely an invalid url, continue with process
|
||||
} else {
|
||||
console.error("Error while crawling page: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
index += batch.length; // Proceed to next batch
|
||||
}
|
||||
console.log("Crawl completed");
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { SiteIndexingConfig } from "../../index.js";
|
||||
import { SiteIndexingConfig } from "../../";
|
||||
|
||||
const preIndexedDocs: Record<
|
||||
SiteIndexingConfig["startUrl"],
|
||||
|
|
|
|||
775
core/package-lock.json
generated
775
core/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -3,7 +3,7 @@
|
|||
"version": "1.0.13",
|
||||
"description": "The Continue Core contains functionality that can be shared across web, VS Code, or Node.js",
|
||||
"scripts": {
|
||||
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand --silent",
|
||||
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand",
|
||||
"build:npm": "tsc -p ./tsconfig.npm.json",
|
||||
"lint": "eslint . --ext ts",
|
||||
"lint:fix": "eslint . --ext ts --fix"
|
||||
|
|
@ -51,7 +51,6 @@
|
|||
"adf-to-md": "^1.1.0",
|
||||
"async-mutex": "^0.5.0",
|
||||
"axios": "^1.6.7",
|
||||
"cheerio": "^1.0.0-rc.12",
|
||||
"commander": "^12.0.0",
|
||||
"comment-json": "^4.2.3",
|
||||
"dbinfoz": "^0.1.4",
|
||||
|
|
@ -76,6 +75,8 @@
|
|||
"p-limit": "^6.1.0",
|
||||
"pg": "^8.11.3",
|
||||
"posthog-node": "^3.6.3",
|
||||
"puppeteer": "^22.4.0",
|
||||
"puppeteer-chromium-resolver": "^23.0.0",
|
||||
"quick-lru": "^7.0.0",
|
||||
"replicate": "^0.26.0",
|
||||
"request": "^2.88.2",
|
||||
|
|
@ -92,9 +93,6 @@
|
|||
"yaml": "^2.4.2",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"puppeteer": {
|
||||
"chromium_revision": "119.0.6045.105"
|
||||
},
|
||||
"engine-strict": true,
|
||||
"engines": {
|
||||
"node": ">=20.11.0"
|
||||
|
|
|
|||
|
|
@ -8,9 +8,22 @@ import Types from "../config/types.js";
|
|||
import { IdeType, SerializedContinueConfig } from "../index.js";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const CONTINUE_GLOBAL_DIR =
|
||||
process.env.CONTINUE_GLOBAL_DIR ?? path.join(os.homedir(), ".continue");
|
||||
|
||||
export function getChromiumPath(): string {
|
||||
return path.join(getContinueUtilsPath(), ".chromium-browser-snapshots");
|
||||
}
|
||||
|
||||
export function getContinueUtilsPath(): string {
|
||||
const utilsPath = path.join(getContinueGlobalPath(), ".utils");
|
||||
if (!fs.existsSync(utilsPath)) {
|
||||
fs.mkdirSync(utilsPath);
|
||||
}
|
||||
return utilsPath;
|
||||
}
|
||||
|
||||
export function getContinueGlobalPath(): string {
|
||||
// This is ~/.continue on mac/linux
|
||||
const continuePath = CONTINUE_GLOBAL_DIR;
|
||||
|
|
|
|||
3
extensions/vscode/package-lock.json
generated
3
extensions/vscode/package-lock.json
generated
|
|
@ -108,7 +108,6 @@
|
|||
"adf-to-md": "^1.1.0",
|
||||
"async-mutex": "^0.5.0",
|
||||
"axios": "^1.6.7",
|
||||
"cheerio": "^1.0.0-rc.12",
|
||||
"commander": "^12.0.0",
|
||||
"comment-json": "^4.2.3",
|
||||
"dbinfoz": "^0.1.4",
|
||||
|
|
@ -133,6 +132,8 @@
|
|||
"p-limit": "^6.1.0",
|
||||
"pg": "^8.11.3",
|
||||
"posthog-node": "^3.6.3",
|
||||
"puppeteer": "^22.4.0",
|
||||
"puppeteer-chromium-resolver": "^23.0.0",
|
||||
"quick-lru": "^7.0.0",
|
||||
"replicate": "^0.26.0",
|
||||
"request": "^2.88.2",
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
"icon": "media/icon.png",
|
||||
"author": "Continue Dev, Inc",
|
||||
"version": "0.9.199",
|
||||
"version": "0.9.198",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/continuedev/continue"
|
||||
|
|
|
|||
|
|
@ -247,6 +247,7 @@ const exe = os === "win32" ? ".exe" : "";
|
|||
"../../../core/llm/llamaTokenizer.mjs",
|
||||
"../../../core/llm/tiktokenWorkerPool.mjs",
|
||||
];
|
||||
|
||||
for (const f of filesToCopy) {
|
||||
fs.copyFileSync(
|
||||
path.join(__dirname, f),
|
||||
|
|
@ -460,6 +461,7 @@ const exe = os === "win32" ? ".exe" : "";
|
|||
"@vscode/ripgrep",
|
||||
"workerpool",
|
||||
];
|
||||
|
||||
fs.mkdirSync("out/node_modules", { recursive: true });
|
||||
|
||||
await Promise.all(
|
||||
|
|
@ -507,8 +509,8 @@ const exe = os === "win32" ? ".exe" : "";
|
|||
os === "darwin"
|
||||
? "libonnxruntime.1.14.0.dylib"
|
||||
: os === "linux"
|
||||
? "libonnxruntime.so.1.14.0"
|
||||
: "onnxruntime.dll"
|
||||
? "libonnxruntime.so.1.14.0"
|
||||
: "onnxruntime.dll"
|
||||
}`,
|
||||
"builtin-themes/dark_modern.json",
|
||||
|
||||
|
|
@ -547,8 +549,8 @@ const exe = os === "win32" ? ".exe" : "";
|
|||
target === "win32-arm64"
|
||||
? "esbuild.exe"
|
||||
: target === "win32-x64"
|
||||
? "win32-x64/esbuild.exe"
|
||||
: `${target}/bin/esbuild`
|
||||
? "win32-x64/esbuild.exe"
|
||||
: `${target}/bin/esbuild`
|
||||
}`,
|
||||
`out/node_modules/@lancedb/vectordb-${
|
||||
os === "win32"
|
||||
|
|
|
|||
65
gui/package-lock.json
generated
65
gui/package-lock.json
generated
|
|
@ -101,7 +101,6 @@
|
|||
"adf-to-md": "^1.1.0",
|
||||
"async-mutex": "^0.5.0",
|
||||
"axios": "^1.6.7",
|
||||
"cheerio": "^1.0.0-rc.12",
|
||||
"commander": "^12.0.0",
|
||||
"comment-json": "^4.2.3",
|
||||
"dbinfoz": "^0.1.4",
|
||||
|
|
@ -126,6 +125,8 @@
|
|||
"p-limit": "^6.1.0",
|
||||
"pg": "^8.11.3",
|
||||
"posthog-node": "^3.6.3",
|
||||
"puppeteer": "^22.4.0",
|
||||
"puppeteer-chromium-resolver": "^23.0.0",
|
||||
"quick-lru": "^7.0.0",
|
||||
"replicate": "^0.26.0",
|
||||
"request": "^2.88.2",
|
||||
|
|
@ -6312,40 +6313,6 @@
|
|||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"../core/node_modules/cheerio": {
|
||||
"version": "1.0.0-rc.12",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"cheerio-select": "^2.1.0",
|
||||
"dom-serializer": "^2.0.0",
|
||||
"domhandler": "^5.0.3",
|
||||
"domutils": "^3.0.1",
|
||||
"htmlparser2": "^8.0.1",
|
||||
"parse5": "^7.0.0",
|
||||
"parse5-htmlparser2-tree-adapter": "^7.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/cheeriojs/cheerio?sponsor=1"
|
||||
}
|
||||
},
|
||||
"../core/node_modules/cheerio-select": {
|
||||
"version": "2.1.0",
|
||||
"license": "BSD-2-Clause",
|
||||
"dependencies": {
|
||||
"boolbase": "^1.0.0",
|
||||
"css-select": "^5.1.0",
|
||||
"css-what": "^6.1.0",
|
||||
"domelementtype": "^2.3.0",
|
||||
"domhandler": "^5.0.3",
|
||||
"domutils": "^3.0.1"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/fb55"
|
||||
}
|
||||
},
|
||||
"../core/node_modules/chownr": {
|
||||
"version": "2.0.0",
|
||||
"license": "ISC",
|
||||
|
|
@ -8312,23 +8279,6 @@
|
|||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"../core/node_modules/htmlparser2": {
|
||||
"version": "8.0.2",
|
||||
"funding": [
|
||||
"https://github.com/fb55/htmlparser2?sponsor=1",
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/fb55"
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"domelementtype": "^2.3.0",
|
||||
"domhandler": "^5.0.3",
|
||||
"domutils": "^3.0.1",
|
||||
"entities": "^4.4.0"
|
||||
}
|
||||
},
|
||||
"../core/node_modules/http-cache-semantics": {
|
||||
"version": "4.1.1",
|
||||
"license": "BSD-2-Clause",
|
||||
|
|
@ -10574,17 +10524,6 @@
|
|||
"url": "https://github.com/inikulin/parse5?sponsor=1"
|
||||
}
|
||||
},
|
||||
"../core/node_modules/parse5-htmlparser2-tree-adapter": {
|
||||
"version": "7.0.0",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"domhandler": "^5.0.2",
|
||||
"parse5": "^7.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/inikulin/parse5?sponsor=1"
|
||||
}
|
||||
},
|
||||
"../core/node_modules/path-exists": {
|
||||
"version": "4.0.0",
|
||||
"license": "MIT",
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@ import { Button, HelperText, Input, lightGray } from "..";
|
|||
import { IdeMessengerContext } from "../../context/IdeMessenger";
|
||||
import { setShowDialog } from "../../redux/slices/uiStateSlice";
|
||||
|
||||
const DEFAULT_MAX_DEPTH = 3;
|
||||
|
||||
function AddDocsDialog() {
|
||||
const posthog = usePostHog();
|
||||
const dispatch = useDispatch();
|
||||
|
|
@ -17,9 +15,7 @@ function AddDocsDialog() {
|
|||
|
||||
const [title, setTitle] = useState("");
|
||||
const [startUrl, setStartUrl] = useState("");
|
||||
const [rootUrl, setRootUrl] = useState("");
|
||||
const [faviconUrl, setFaviconUrl] = useState("");
|
||||
const [maxDepth, setMaxDepth] = useState<number | string>("");
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
|
||||
const ideMessenger = useContext(IdeMessengerContext);
|
||||
|
|
@ -39,18 +35,14 @@ function AddDocsDialog() {
|
|||
|
||||
const siteIndexingConfig: SiteIndexingConfig = {
|
||||
startUrl,
|
||||
rootUrl,
|
||||
title,
|
||||
maxDepth: typeof maxDepth === "string" ? DEFAULT_MAX_DEPTH : maxDepth,
|
||||
faviconUrl: new URL("/favicon.ico", startUrl).toString(),
|
||||
faviconUrl,
|
||||
};
|
||||
|
||||
ideMessenger.post("context/addDocs", siteIndexingConfig);
|
||||
|
||||
setTitle("");
|
||||
setStartUrl("");
|
||||
setRootUrl("");
|
||||
setMaxDepth("");
|
||||
setFaviconUrl("");
|
||||
|
||||
dispatch(setShowDialog(false));
|
||||
|
|
@ -126,22 +118,6 @@ function AddDocsDialog() {
|
|||
|
||||
{isOpen && (
|
||||
<div className="pt-2">
|
||||
<label>
|
||||
Root URL [Optional]
|
||||
<Input
|
||||
type="url"
|
||||
placeholder="Root URL"
|
||||
value={rootUrl}
|
||||
onChange={(e) => {
|
||||
setRootUrl(e.target.value);
|
||||
}}
|
||||
/>
|
||||
<HelperText>
|
||||
Limits the crawler to pages within the same domain and path as
|
||||
the Root URL
|
||||
</HelperText>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Favicon URL [Optional]
|
||||
<Input
|
||||
|
|
@ -157,29 +133,6 @@ function AddDocsDialog() {
|
|||
`/favicon.ico` path from the Start URL
|
||||
</HelperText>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Max Depth [Optional]
|
||||
<Input
|
||||
type="text"
|
||||
inputMode="numeric"
|
||||
placeholder={`Default: ${DEFAULT_MAX_DEPTH}`}
|
||||
title="The maximum search tree depth - where your input url is the root node"
|
||||
value={maxDepth}
|
||||
onChange={(e) => {
|
||||
const value = e.target.value;
|
||||
if (value == "") {
|
||||
setMaxDepth("");
|
||||
} else if (!isNaN(+value) && Number(value) > 0) {
|
||||
setMaxDepth(Number(value));
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<HelperText>
|
||||
Limits the maximum search tree depth of the crawler - 3 by
|
||||
default
|
||||
</HelperText>
|
||||
</label>
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue