diff --git a/core/context/providers/DocsContextProvider.ts b/core/context/providers/DocsContextProvider.ts index b9e98c171..83329a930 100644 --- a/core/context/providers/DocsContextProvider.ts +++ b/core/context/providers/DocsContextProvider.ts @@ -5,6 +5,7 @@ import { ContextProviderExtras, ContextSubmenuItem, LoadSubmenuItemsArgs, + SiteIndexingConfig, } from "../../index.js"; import { DocsService } from "../../indexing/docs/DocsService.js"; import configs from "../../indexing/docs/preIndexedDocs.js"; @@ -108,40 +109,44 @@ class DocsContextProvider extends BaseContextProvider { ]; } - async loadSubmenuItems( - args: LoadSubmenuItemsArgs, - ): Promise { - const docs = await this.docsService.list(); - const submenuItems: ContextSubmenuItem[] = docs.map((doc) => ({ + // Get combined site configs from preIndexedDocs and options.sites. + private _getDocsSitesConfig(): SiteIndexingConfig[] { + return [...configs, ...(this.options?.sites || [])]; + } + + // Get indexed docs as ContextSubmenuItems from database. + private async _getIndexedDocsContextSubmenuItems(): Promise { + return (await this.docsService.list()).map((doc) => ({ title: doc.title, description: new URL(doc.baseUrl).hostname, id: doc.baseUrl, - metadata: { - preIndexed: !!configs.find((config) => config.title === doc.title), - }, })); + } - submenuItems.push( - ...configs - // After it's actually downloaded, we don't want to show twice - .filter( - (config) => !submenuItems.some((item) => item.id === config.startUrl), - ) - .map((config) => ({ - title: config.title, - description: new URL(config.startUrl).hostname, - id: config.startUrl, - metadata: { - preIndexed: true, - }, - // iconUrl: config.faviconUrl, - })), - ); + async loadSubmenuItems( + args: LoadSubmenuItemsArgs, + ): Promise { + const submenuItemsMap = new Map(); + + for (const item of await this._getIndexedDocsContextSubmenuItems()) { + submenuItemsMap.set(item.id, item); + } + + for (const config of this._getDocsSitesConfig()) { + submenuItemsMap.set(config.startUrl, { + id: config.startUrl, + title: config.title, + description: new URL(config.startUrl).hostname, + metadata: { preIndexed: !!configs.find((cnf) => cnf.title === config.title), }, + }); + } + + const submenuItems = Array.from(submenuItemsMap.values()); // Sort submenuItems such that the objects with titles which don't occur in configs occur first, and alphabetized submenuItems.sort((a, b) => { - const aTitleInConfigs = a.metadata?.preIndexed; - const bTitleInConfigs = b.metadata?.preIndexed; + const aTitleInConfigs = a.metadata?.preIndexed ?? false; + const bTitleInConfigs = b.metadata?.preIndexed ?? false; // Primary criterion: Items not in configs come first if (!aTitleInConfigs && bTitleInConfigs) { diff --git a/core/core.ts b/core/core.ts index abb47f3e5..25e44a849 100644 --- a/core/core.ts +++ b/core/core.ts @@ -242,6 +242,19 @@ export class Core { await this.docsService.delete(baseUrl); this.messenger.send("refreshSubmenuItems", undefined); }); + on("context/indexDocs", async (msg) => { + const config = await this.config(); + const provider: any = config.contextProviders?.find( + (provider) => provider.description.title === "docs", + ); + + const siteIndexingOptions: SiteIndexingConfig[] = provider ? + (mProvider => mProvider?.options?.sites || [])({ ...provider }) : + []; + + await this.indexDocs(siteIndexingOptions, msg.data.reIndex); + this.ide.infoPopup("Docs indexing completed"); + }); on("context/loadSubmenuItems", async (msg) => { const config = await this.config(); const items = config.contextProviders @@ -623,4 +636,16 @@ export class Core { this.indexingState = update; } } + + private async indexDocs(sites: SiteIndexingConfig[], reIndex: boolean): Promise { + for (const site of sites) { + for await (const update of this.docsService.indexAndAdd(site, new TransformersJsEmbeddingsProvider(), reIndex)) { + // Temporary disabled posting progress updates to the UI due to + // possible collision with code indexing progress updates. + + // this.messenger.request("indexProgress", update); + // this.indexingState = update; + } + } + } } diff --git a/core/indexing/docs/DocsService.ts b/core/indexing/docs/DocsService.ts index 443ecf124..9cd8feb5f 100644 --- a/core/indexing/docs/DocsService.ts +++ b/core/indexing/docs/DocsService.ts @@ -30,6 +30,7 @@ export class DocsService { private static instance: DocsService; private static DOCS_TABLE_NAME = "docs"; private _sqliteTable: Database | undefined; + private docsIndexingQueue: Set = new Set(); public static getInstance(): DocsService { if (!DocsService.instance) { @@ -204,10 +205,16 @@ export class DocsService { async *indexAndAdd( siteIndexingConfig: SiteIndexingConfig, embeddingsProvider: EmbeddingsProvider, + reIndex: boolean = false, ): AsyncGenerator { - const startUrl = new URL(siteIndexingConfig.startUrl); + const startUrl = new URL(siteIndexingConfig.startUrl.toString()); - if (await this.has(siteIndexingConfig.startUrl.toString())) { + if (this.docsIndexingQueue.has(startUrl.toString())) { + console.log("Already in queue"); + return; + } + + if (!reIndex && await this.has(startUrl.toString())) { yield { progress: 1, desc: "Already indexed", @@ -216,6 +223,9 @@ export class DocsService { return; } + // Mark the site as currently being indexed + this.docsIndexingQueue.add(startUrl.toString()); + yield { progress: 0, desc: "Finding subpages", @@ -223,20 +233,31 @@ export class DocsService { }; const articles: Article[] = []; + let processedPages = 0; + let maxKnownPages = 1; // Crawl pages and retrieve info as articles for await (const page of crawlPage(startUrl, siteIndexingConfig.maxDepth)) { + processedPages++; const article = pageToArticle(page); if (!article) { continue; } articles.push(article); + // Use a heuristic approach for progress calculation + const progress = Math.min(processedPages / maxKnownPages, 1); + yield { - progress: 0, + progress, // Yield the heuristic progress desc: `Finding subpages (${page.path})`, status: "indexing", }; + + // Increase maxKnownPages to delay progress reaching 100% too soon + if (processedPages === maxKnownPages) { + maxKnownPages *= 2; + } } const chunks: Chunk[] = []; @@ -244,10 +265,11 @@ export class DocsService { // Create embeddings of retrieved articles console.log("Creating Embeddings for ", articles.length, " articles"); - for (const article of articles) { + for (let i = 0; i < articles.length; i++) { + const article = articles[i]; yield { - progress: Math.max(1, Math.floor(100 / (articles.length + 1))), - desc: `${article.subpath}`, + progress: i / articles.length, + desc: `Creating Embeddings: ${article.subpath}`, status: "indexing", }; @@ -268,7 +290,14 @@ export class DocsService { // Add docs to databases console.log("Adding ", embeddings.length, " embeddings to db"); + yield { + progress: 0.5, + desc: `Adding ${embeddings.length} embeddings to db`, + status: "indexing", + }; + await this.add(siteIndexingConfig.title, startUrl, chunks, embeddings); + this.docsIndexingQueue.delete(startUrl.toString()); yield { progress: 1, diff --git a/core/protocol/core.ts b/core/protocol/core.ts index ab15fa6c3..3ccc7ed16 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -69,6 +69,7 @@ export type ToCoreFromIdeOrWebviewProtocol = { "autocomplete/complete": [AutocompleteInput, string[]]; "context/addDocs": [SiteIndexingConfig, void]; "context/removeDocs": [{ baseUrl: string }, void]; + "context/indexDocs": [{ reIndex: boolean }, void]; "autocomplete/cancel": [undefined, void]; "autocomplete/accept": [{ completionId: string }, void]; "command/run": [ diff --git a/core/protocol/passThrough.ts b/core/protocol/passThrough.ts index a228194e3..5fe14573a 100644 --- a/core/protocol/passThrough.ts +++ b/core/protocol/passThrough.ts @@ -25,6 +25,7 @@ export const WEBVIEW_TO_CORE_PASS_THROUGH: (keyof ToCoreFromWebviewProtocol)[] = "context/loadSubmenuItems", "context/addDocs", "context/removeDocs", + "context/indexDocs", "autocomplete/complete", "autocomplete/cancel", "autocomplete/accept", diff --git a/docs/docs/customization/context-providers.md b/docs/docs/customization/context-providers.md index 66aacea53..82d6aa133 100644 --- a/docs/docs/customization/context-providers.md +++ b/docs/docs/customization/context-providers.md @@ -58,6 +58,28 @@ Type `@docs` to index and retrieve snippets from any documentation site. You can Continue also pre-indexes a number of common sites, listed [here](https://github.com/continuedev/continue/blob/main/core/indexing/docs/preIndexedDocs.ts). The embeddings for these sites are hosted by us, but downloaded for local use after the first time. All other indexing occurs entirely locally. +#### Adding a Documentation Site via Configuration + +To add a documentation site via configuration, update the `config.json` file as follows: + +```json +{ + "name": "docs", + "params": { + "sites": [ + { + "title": "ExampleDocs", + "startUrl": "https://exampledocs.com/docs", + "rootUrl": "https://exampledocs.com", + "maxDepth": 3 // Default + } + ] + } +} +``` + +The docs are indexed when you modify the configuration file, unless indexing is disabled. If you want to manually trigger the indexing, you can use the command `Continue: Docs Index`. For force indexing, you can use the command `Continue: Docs Force Re-Index`. Note that these commands will work even if automatic indexing is disabled. + ### Open Files Type '@open' to reference the contents of all of your open files. Set `onlyPinned` to `true` to only reference pinned files. diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index 796c9641c..0152eb02a 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -229,6 +229,18 @@ "category": "Continue", "title": "Fix Grammar / Spelling", "group": "Continue" + }, + { + "command": "continue.docsIndex", + "category": "Continue", + "title": "Docs Index", + "group": "Continue" + }, + { + "command": "continue.docsReIndex", + "category": "Continue", + "title": "Docs Force Re-Index", + "group": "Continue" } ], "keybindings": [ diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index 3e0a83b77..7b1c9e623 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -25,6 +25,7 @@ import { VerticalPerLineDiffManager } from "./diff/verticalPerLine/manager"; import { QuickEdit, QuickEditShowParams } from "./quickEdit/QuickEditQuickPick"; import { Battery } from "./util/battery"; import type { VsCodeWebviewProtocol } from "./webviewProtocol"; +import { Core } from "core/core"; let fullScreenPanel: vscode.WebviewPanel | undefined; @@ -170,6 +171,7 @@ const commandsMap: ( continueServerClientPromise: Promise, battery: Battery, quickEdit: QuickEdit, + core: Core, ) => { [command: string]: (...args: any) => any } = ( ide, extensionContext, @@ -180,6 +182,7 @@ const commandsMap: ( continueServerClientPromise, battery, quickEdit, + core ) => { /** * Streams an inline edit to the vertical diff manager. @@ -285,6 +288,12 @@ const commandsMap: ( "continue.toggleAuxiliaryBar": () => { vscode.commands.executeCommand("workbench.action.toggleAuxiliaryBar"); }, + "continue.docsIndex": async () => { + core.invoke("context/indexDocs", {reIndex: false}); + }, + "continue.docsReIndex": async () => { + core.invoke("context/indexDocs", {reIndex: true}); + }, "continue.focusContinueInput": async () => { const fullScreenTab = getFullScreenTab(); if (!fullScreenTab) { @@ -669,6 +678,7 @@ export function registerAllCommands( continueServerClientPromise: Promise, battery: Battery, quickEdit: QuickEdit, + core: Core, ) { for (const [command, callback] of Object.entries( commandsMap( @@ -681,6 +691,7 @@ export function registerAllCommands( continueServerClientPromise, battery, quickEdit, + core ), )) { context.subscriptions.push( diff --git a/extensions/vscode/src/extension/VsCodeExtension.ts b/extensions/vscode/src/extension/VsCodeExtension.ts index 251ba02fd..b0f5950fe 100644 --- a/extensions/vscode/src/extension/VsCodeExtension.ts +++ b/extensions/vscode/src/extension/VsCodeExtension.ts @@ -196,6 +196,7 @@ export class VsCodeExtension { this.core.continueServerClientPromise, this.battery, quickEdit, + this.core ); registerDebugTracker(this.sidebar.webviewProtocol, this.ide); diff --git a/gui/package-lock.json b/gui/package-lock.json index 0d43acf64..92f1a4d1f 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -64,6 +64,7 @@ "devDependencies": { "@swc/cli": "^0.3.12", "@swc/core": "^1.5.25", + "@types/lodash": "^4.17.6", "@types/node": "^20.5.6", "@types/node-fetch": "^2.6.4", "@types/react": "^18.0.27", @@ -1999,6 +2000,12 @@ "@types/node": "*" } }, + "node_modules/@types/lodash": { + "version": "4.17.6", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.6.tgz", + "integrity": "sha512-OpXEVoCKSS3lQqjx9GGGOapBeuW5eUboYHRlHP9urXPX25IKZ6AnP5ZRxtVf63iieUbsHxLn8NQ5Nlftc6yzAA==", + "dev": true + }, "node_modules/@types/mdast": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-4.0.4.tgz", diff --git a/gui/package.json b/gui/package.json index b2a4426e8..f0e4611e6 100644 --- a/gui/package.json +++ b/gui/package.json @@ -66,6 +66,7 @@ "devDependencies": { "@swc/cli": "^0.3.12", "@swc/core": "^1.5.25", + "@types/lodash": "^4.17.6", "@types/node": "^20.5.6", "@types/node-fetch": "^2.6.4", "@types/react": "^18.0.27", diff --git a/gui/src/hooks/useSetup.ts b/gui/src/hooks/useSetup.ts index f43fc7902..28cd4019b 100644 --- a/gui/src/hooks/useSetup.ts +++ b/gui/src/hooks/useSetup.ts @@ -13,9 +13,10 @@ import { import { RootState } from "../redux/store"; import { isJetBrains } from "../util"; -import { setLocalStorage } from "../util/localStorage"; +import { getLocalStorage, setLocalStorage } from "../util/localStorage"; import useChatHandler from "./useChatHandler"; import { useWebviewListener } from "./useWebviewListener"; +import { debounce } from "lodash"; function useSetup(dispatch: Dispatch) { const [configLoaded, setConfigLoaded] = useState(false); @@ -30,6 +31,7 @@ function useSetup(dispatch: Dispatch) { dispatch(setConfig(config)); dispatch(setSelectedProfileId(profileId)); setConfigLoaded(true); + setLocalStorage("disableIndexing", config.disableIndexing || false); // Perform any actions needed with the config if (config.ui?.fontSize) { @@ -86,8 +88,16 @@ function useSetup(dispatch: Dispatch) { }); }); + const debouncedIndexDocs = debounce(() => { + ideMessenger.request("context/indexDocs", { reIndex: false }); + }, 1000); + useWebviewListener("configUpdate", async () => { - loadConfig(); + await loadConfig(); + + if (!isJetBrains && !getLocalStorage("disableIndexing")) { + debouncedIndexDocs(); + } }); useWebviewListener("submitMessage", async (data) => { diff --git a/gui/src/util/localStorage.ts b/gui/src/util/localStorage.ts index 21ae4499f..1eecda363 100644 --- a/gui/src/util/localStorage.ts +++ b/gui/src/util/localStorage.ts @@ -16,6 +16,7 @@ type LocalStorageTypes = { isOnboardingInProgress: boolean; showTutorialCard: boolean; shownProfilesIntroduction: boolean; + disableIndexing: boolean; }; export function getLocalStorage(