From 416fdbb69ff17f3ab02f6d2b7dce0ea5a7db058c Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 19 Nov 2024 09:50:10 -0800 Subject: [PATCH] unified indexing peeks --- core/indexing/docs/DocsService.ts | 138 ++++++++------- gui/src/components/dialogs/AddDocsDialog.tsx | 8 +- .../components/indexing/ChatIndexingPeeks.tsx | 161 +++++++++++------- .../components/indexing/IndexingStatuses.tsx | 1 - gui/src/components/indexing/utils.ts | 1 - gui/src/pages/gui/Chat.tsx | 1 - gui/src/redux/slices/stateSlice.ts | 65 +------ 7 files changed, 178 insertions(+), 197 deletions(-) diff --git a/core/indexing/docs/DocsService.ts b/core/indexing/docs/DocsService.ts index ffb251c64..838e8fb54 100644 --- a/core/indexing/docs/DocsService.ts +++ b/core/indexing/docs/DocsService.ts @@ -10,6 +10,7 @@ import { IndexingStatusMap, IndexingStatus, SiteIndexingConfig, + IdeInfo, } from "../.."; import { ConfigHandler } from "../../config/ConfigHandler"; import { addContextProvider } from "../../config/util"; @@ -101,12 +102,14 @@ export default class DocsService { private sqliteDb?: Database; private docsCrawler!: DocsCrawler; + private ideInfoPromise: Promise; constructor( configHandler: ConfigHandler, private readonly ide: IDE, private readonly messenger?: IMessenger, ) { + this.ideInfoPromise = this.ide.getIdeInfo(); this.isInitialized = this.init(configHandler); } @@ -135,6 +138,31 @@ export default class DocsService { readonly statuses: IndexingStatusMap = new Map(); + // Function for GUI to retrieve initial pending statuses + // And kickoff indexing where needed + async initStatuses() { + this.config?.docs?.forEach(async (doc) => { + const currentStatus = this.statuses.get(doc.startUrl); + if (currentStatus) { + this.handleStatusUpdate(currentStatus); + } else { + this.handleStatusUpdate({ + type: "docs", + id: doc.startUrl, + embeddingsProviderId: this.config.embeddingsProvider.id, + isReindexing: false, + progress: 0, + description: "Pending", + status: "pending", + title: doc.title, + debugInfo: `max depth: ${doc.maxDepth}`, + icon: doc.faviconUrl, + url: doc.startUrl, + }); + } + }); + } + handleStatusUpdate(update: IndexingStatus) { this.statuses.set(update.id, update); this.messenger?.send("indexing/statusUpdate", update); @@ -143,6 +171,7 @@ export default class DocsService { abort(startUrl: string) { const status = this.statuses.get(startUrl); if (status) { + this.docsIndexingQueue.delete(startUrl); this.handleStatusUpdate({ ...status, status: "aborted", @@ -177,7 +206,7 @@ export default class DocsService { * So, we only include pre-indexed docs in the submenu for non-JetBrains IDEs. */ async canUsePreindexedDocs() { - const ideInfo = await this.ide.getIdeInfo(); + const ideInfo = await this.ideInfoPromise; if (ideInfo.ideType === "jetbrains") { return false; } @@ -322,12 +351,15 @@ export default class DocsService { ): Promise { const { startUrl } = siteIndexingConfig; + // Queue - indexAndAdd is invoked circularly by config edits. This prevents duplicate runs if (this.docsIndexingQueue.has(startUrl)) { - console.debug("Already in queue"); return; } + const embeddingsProvider = await this.getEmbeddingsProvider(); + this.docsIndexingQueue.add(startUrl); + const indexExists = await this.hasMetadata(startUrl); const fixedStatus: Pick< @@ -366,8 +398,6 @@ export default class DocsService { } } - // Mark the site as currently being indexed - this.docsIndexingQueue.add(startUrl); try { this.handleStatusUpdate({ ...fixedStatus, @@ -378,7 +408,7 @@ export default class DocsService { const articles: Article[] = []; let processedPages = 0; - let estimatedProgress = 0; // progress heuristic = sum series 1/(2^n) from n=1 to pages + 1. Alternative e.g. sum 1/(2^n) + let estimatedProgress = 0; // Crawl pages and retrieve info as articles for await (const page of this.docsCrawler.crawl(new URL(startUrl))) { @@ -392,7 +422,10 @@ export default class DocsService { ...fixedStatus, description: `Finding subpages (${page.path})`, status: "indexing", - progress: 0.4 * estimatedProgress, // 0% -> 40%, a heuristic approach for progress calculation + progress: + 0.3 * estimatedProgress + + Math.min(0.2, (0.2 * processedPages) / 500), + // For the first 50%, 30% is sum of series 1/(2^n) and the other 20% is based on number of files/ 500 max }); const article = pageToArticle(page); @@ -425,7 +458,7 @@ export default class DocsService { ...fixedStatus, status: "indexing", description: `Creating Embeddings: ${article.subpath}`, - progress: 0.4 + 0.4 * (i / articles.length), // 40% -> 80% + progress: 0.5 + 0.3 * (i / articles.length), // 50% -> 80% }); try { @@ -466,9 +499,7 @@ export default class DocsService { }); void this.ide.showToast("info", `Failed to index ${startUrl}`); - this.docsIndexingQueue.delete(startUrl); - return; } @@ -540,28 +571,34 @@ export default class DocsService { } } - // Function for GUI to retrieve initial pending statuses - // And kickoff indexing where needed - async initStatuses() { - this.config?.docs?.forEach(async (doc) => { - const currentStatus = this.statuses.get(doc.startUrl); - if (currentStatus) { - this.handleStatusUpdate(currentStatus); - } else { - this.handleStatusUpdate({ - type: "docs", - id: doc.startUrl, - embeddingsProviderId: this.config.embeddingsProvider.id, - isReindexing: false, - progress: 0, - description: "Pending", - status: "pending", - title: doc.title, - debugInfo: `max depth: ${doc.maxDepth}`, - icon: doc.faviconUrl, - url: doc.startUrl, - }); - } + // When user requests a pre-indexed doc for the first time + // And pre-indexed embeddings are supported + // Fetch pre-indexed embeddings from S3, add to Lance, and then search those + private async fetchAndAddPreIndexedDocEmbeddings(title: string) { + const embeddingsProvider = await this.getEmbeddingsProvider(true); + + const data = await downloadFromS3( + S3Buckets.continueIndexedDocs, + getS3Filename(embeddingsProvider.id, title), + ); + + const siteEmbeddings = JSON.parse(data) as SiteIndexingResults; + const startUrl = new URL(siteEmbeddings.url).toString(); + + const faviconUrl = preIndexedDocs[startUrl].faviconUrl; + const favicon = + typeof faviconUrl === "string" + ? await getFaviconBase64(faviconUrl) + : undefined; + + await this.add({ + favicon, + siteIndexingConfig: { + startUrl, + title: siteEmbeddings.title, + }, + chunks: siteEmbeddings.chunks, + embeddings: siteEmbeddings.chunks.map((c) => c.embedding), }); } @@ -704,7 +741,7 @@ export default class DocsService { const currentlyIndexedDocs = await this.listMetadata(); const currentStartUrls = currentlyIndexedDocs.map((doc) => doc.startUrl); - // Anything found in sqlite but not in new config should be deleted + // Anything found in sqlite but not in new config should be deleted if not preindexed const deletedDocs = currentlyIndexedDocs.filter( (doc) => !preIndexedDocs[doc.startUrl] && @@ -717,12 +754,12 @@ export default class DocsService { const newDocs: SiteIndexingConfig[] = []; const changedDocs: SiteIndexingConfig[] = []; for (const doc of newConfigDocs) { - const oldConfigDoc = oldConfigDocs.find( - (d) => d.startUrl === doc.startUrl, - ); const currentIndexedDoc = currentStartUrls.includes(doc.startUrl); if (currentIndexedDoc) { + const oldConfigDoc = oldConfigDocs.find( + (d) => d.startUrl === doc.startUrl, + ); if ( oldConfigDoc && (oldConfigDoc.maxDepth !== doc.maxDepth || @@ -976,37 +1013,6 @@ export default class DocsService { this.messenger?.send("refreshSubmenuItems", undefined); } - // When user requests a pre-indexed doc for the first time - // And pre-indexed embeddings are supported - // Fetch pre-indexed embeddings from S3, add to Lance, and then search those - private async fetchAndAddPreIndexedDocEmbeddings(title: string) { - const embeddingsProvider = await this.getEmbeddingsProvider(true); - - const data = await downloadFromS3( - S3Buckets.continueIndexedDocs, - getS3Filename(embeddingsProvider.id, title), - ); - - const siteEmbeddings = JSON.parse(data) as SiteIndexingResults; - const startUrl = new URL(siteEmbeddings.url).toString(); - - const faviconUrl = preIndexedDocs[startUrl].faviconUrl; - const favicon = - typeof faviconUrl === "string" - ? await getFaviconBase64(faviconUrl) - : undefined; - - await this.add({ - favicon, - siteIndexingConfig: { - startUrl, - title: siteEmbeddings.title, - }, - chunks: siteEmbeddings.chunks, - embeddings: siteEmbeddings.chunks.map((c) => c.embedding), - }); - } - /** * Currently this deletes re-crawls + re-indexes all docs. * A more optimal solution in the future will be to create diff --git a/gui/src/components/dialogs/AddDocsDialog.tsx b/gui/src/components/dialogs/AddDocsDialog.tsx index f373f4bc0..1a8682150 100644 --- a/gui/src/components/dialogs/AddDocsDialog.tsx +++ b/gui/src/components/dialogs/AddDocsDialog.tsx @@ -9,7 +9,10 @@ import { useContext, useLayoutEffect, useRef, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import { Button, HelperText, Input, lightGray } from ".."; import { IdeMessengerContext } from "../../context/IdeMessenger"; -import { setShowDialog } from "../../redux/slices/uiStateSlice"; +import { + setDialogMessage, + setShowDialog, +} from "../../redux/slices/uiStateSlice"; import { RootState } from "../../redux/store"; import IndexingStatusViewer from "../indexing/IndexingStatus"; @@ -57,8 +60,6 @@ function AddDocsDialog() { setStartUrl(""); setFaviconUrl(""); - // dispatch(setShowDialog(false)); - posthog.capture("add_docs_gui", { url: startUrl }); } @@ -103,6 +104,7 @@ function AddDocsDialog() {