From b619f9a6cff76067217e50f96c3e9fe98bfbb36f Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sun, 21 Jan 2024 20:45:39 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20small=20improvements=20to=20GitH?= =?UTF-8?q?ub=20docs=20indexing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/context/providers/DocsContextProvider.ts | 20 ++++-- core/indexing/chunk/markdown.ts | 4 +- core/indexing/docs/crawl.ts | 63 ++++++++++++++++++- core/indexing/docs/index.ts | 2 +- core/indexing/docs/urlToMarkdown.ts | 37 ++++++++++- gui/src/components/FileIcon.tsx | 2 +- .../components/mainInput/ContextItemsPeek.tsx | 8 ++- gui/src/components/mainInput/MentionList.tsx | 6 ++ 8 files changed, 127 insertions(+), 15 deletions(-) diff --git a/core/context/providers/DocsContextProvider.ts b/core/context/providers/DocsContextProvider.ts index 7422e1579..f7eba59d3 100644 --- a/core/context/providers/DocsContextProvider.ts +++ b/core/context/providers/DocsContextProvider.ts @@ -28,17 +28,25 @@ class DocsContextProvider extends BaseContextProvider { const chunks = await retrieveDocs( query, vector, - this.options?.nRetrieve || 15 + this.options?.nRetrieve || 8 ); console.log(chunks); return [ - ...chunks.map((chunk) => ({ - name: chunk.otherMetadata?.title || chunk.filepath, - description: new URL(chunk.filepath, query).toString(), - content: chunk.content, - })), + ...chunks + .map((chunk) => ({ + name: chunk.filepath + .split("/") + .slice(1) + .join("/") + .split("/tree/main/") + .slice(1) + .join("/"), // chunk.otherMetadata?.title || chunk.filepath, + description: new URL(chunk.filepath, query).toString(), + content: chunk.content, + })) + .reverse(), { name: "Instructions", description: "Instructions", diff --git a/core/indexing/chunk/markdown.ts b/core/indexing/chunk/markdown.ts index 9a7becd3d..0ca92158c 100644 --- a/core/indexing/chunk/markdown.ts +++ b/core/indexing/chunk/markdown.ts @@ -10,8 +10,8 @@ function cleanFragment(fragment: string | undefined): string | undefined { // Remove leading and trailing whitespaces fragment = fragment.trim(); - // If there's a (, remove everything after it - const parenIndex = fragment.indexOf("("); + // If there's a ](, which would mean a link, remove everything after it + const parenIndex = fragment.indexOf("]("); if (parenIndex !== -1) { fragment = fragment.slice(0, parenIndex); } diff --git a/core/indexing/docs/crawl.ts b/core/indexing/docs/crawl.ts index 9570b86d0..13b86c807 100644 --- a/core/indexing/docs/crawl.ts +++ b/core/indexing/docs/crawl.ts @@ -1,3 +1,4 @@ +import { Octokit } from "@octokit/rest"; import cheerio from "cheerio"; import fetch from "node-fetch"; // const HCCrawler = require("headless-chrome-crawler"); @@ -17,7 +18,9 @@ const IGNORE_PATHS_ENDING_IN = [ "changelog.html", ]; -function shouldFilterPath(pathname: string): boolean { +const GITHUB_PATHS_TO_TRAVERSE = ["/blob/", "/tree/"]; + +function shouldFilterPath(pathname: string, baseUrl: URL): boolean { if (pathname.includes("#")) { pathname = pathname.slice(0, pathname.indexOf("#")); } @@ -25,13 +28,23 @@ function shouldFilterPath(pathname: string): boolean { if (pathname.endsWith("/")) { pathname = pathname.slice(0, -1); } + + if (baseUrl.hostname === "github.com") { + if ( + pathname.split("/").length > 3 && + !GITHUB_PATHS_TO_TRAVERSE.some((path) => pathname.includes(path)) + ) + return true; + return false; + } + if (IGNORE_PATHS_ENDING_IN.some((path) => pathname.endsWith(path))) return true; return false; } async function crawlLinks(path: string, baseUrl: URL, visited: Set) { - if (visited.has(path) || shouldFilterPath(path)) { + if (visited.has(path) || shouldFilterPath(path, baseUrl)) { return; } visited.add(path); @@ -62,7 +75,53 @@ async function crawlLinks(path: string, baseUrl: URL, visited: Set) { ); } +async function crawlGithubRepo(baseUrl: URL) { + const octokit = new Octokit({ + auth: undefined, + }); + + const [_, owner, repo] = baseUrl.pathname.split("/"); + + let dirContentsConfig = { + owner: owner, + repo: repo, + }; + + const tree = await octokit.request( + "GET /repos/{owner}/{repo}/git/trees/{tree_sha}", + { + owner, + repo, + tree_sha: "main", + headers: { + "X-GitHub-Api-Version": "2022-11-28", + }, + recursive: "true", + } + ); + + const paths = tree.data.tree + .filter( + (file) => file.type === "blob" && file.path?.endsWith(".md") + // || + // file.path?.endsWith(".rst") || + // file.path?.split("/").includes("documentation") || + // file.path?.split("/").includes("docs") || + // file.path?.split("/").includes("doc") || + // file.path?.split("/").includes("examples") || + // file.path?.split("/").includes("example") + ) + .map((file) => baseUrl.pathname + "/tree/main/" + file.path); + + return paths; +} + export async function crawlSubpages(baseUrl: URL) { + // Special case for GitHub repos + if (baseUrl.hostname === "github.com") { + return crawlGithubRepo(baseUrl); + } + // First, check if the parent of the path redirects to the same page const parentUrl = new URL(baseUrl); parentUrl.pathname = parentUrl.pathname.split("/").slice(0, -1).join("/"); diff --git a/core/indexing/docs/index.ts b/core/indexing/docs/index.ts index 7b147dcdd..c803177ca 100644 --- a/core/indexing/docs/index.ts +++ b/core/indexing/docs/index.ts @@ -53,7 +53,7 @@ export async function* indexDocs( for (let i = 0; i < subpaths.length; i++) { const subpath = subpaths[i]; yield { - progress: 1 / (subpaths.length + 1), + progress: 100 / (subpaths.length + 1), desc: `${subpath}`, }; diff --git a/core/indexing/docs/urlToMarkdown.ts b/core/indexing/docs/urlToMarkdown.ts index ca98bd873..ec0a678da 100644 --- a/core/indexing/docs/urlToMarkdown.ts +++ b/core/indexing/docs/urlToMarkdown.ts @@ -1,3 +1,4 @@ +import { Octokit } from "@octokit/rest"; import { NodeHtmlMarkdown, PostProcessResult } from "node-html-markdown"; const nhm = new NodeHtmlMarkdown( @@ -21,10 +22,42 @@ const nhm = new NodeHtmlMarkdown( const STRIP_BEFORE = ["\n# "]; const STRIP_AFTER_AND_INCLUDING = ["Was this page helpful?"]; +const octokit = new Octokit({ + auth: undefined, +}); +async function retrieveGitHubBlob(url: URL): Promise { + const [_, owner, repo, _tree, _main, ...path] = url.pathname.split("/"); + const response = await octokit.repos.getContent({ + owner, + repo, + path: path.join("/"), + }); + + if (response.status !== 200) { + return undefined; + } + + const content = Buffer.from( + (response.data as any).content || "", + "base64" + ).toString(); + + const fileExtension = url.pathname.split(".").slice(-1)[0]; + if (fileExtension === "md") { + return content; + } + + return `\`\`\`${fileExtension} title=${path.join("/")}\n${content}\n\`\`\``; +} + export async function convertURLToMarkdown( url: URL ): Promise { try { + if (url.hostname === "github.com") { + return await retrieveGitHubBlob(url); + } + const response = await fetch(url); if (!response.ok) { @@ -55,7 +88,7 @@ export async function convertURLToMarkdown( return markdown; } catch (err) { - console.error(err); - throw new Error("Error converting URL to markdown"); + console.error("Error converting URL to markdown", err); + return undefined; } } diff --git a/gui/src/components/FileIcon.tsx b/gui/src/components/FileIcon.tsx index 42609bdf3..8ea591827 100644 --- a/gui/src/components/FileIcon.tsx +++ b/gui/src/components/FileIcon.tsx @@ -26,7 +26,7 @@ const FileIcon = ({ filename, height, width }) => { return (
); }; diff --git a/gui/src/components/mainInput/ContextItemsPeek.tsx b/gui/src/components/mainInput/ContextItemsPeek.tsx index e52640d52..c70fa40ff 100644 --- a/gui/src/components/mainInput/ContextItemsPeek.tsx +++ b/gui/src/components/mainInput/ContextItemsPeek.tsx @@ -109,7 +109,13 @@ const ContextItemsPeek = (props: ContextItemsPeekProps) => { }} > diff --git a/gui/src/components/mainInput/MentionList.tsx b/gui/src/components/mainInput/MentionList.tsx index 9abfd2d6c..c639320f9 100644 --- a/gui/src/components/mainInput/MentionList.tsx +++ b/gui/src/components/mainInput/MentionList.tsx @@ -74,6 +74,12 @@ function DropdownIcon(props: { className?: string; type: ComboBoxItemType; }) { + if (props.type === "action") { + return ( + + ); + } + const Icon = ICONS_FOR_DROPDOWN[props.provider]; const iconClass = `${props.className} flex-shrink-0`; if (!Icon) {