From ada10398f748aa1cd94d44ba42aeee741b3b7d31 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Fri, 20 Dec 2024 15:26:19 -0800 Subject: [PATCH] embeddingsprovider docs column --- core/context/providers/DocsContextProvider.ts | 4 +- core/indexing/docs/DocsService.ts | 25 +++++------ core/indexing/docs/migrations.ts | 43 +++++++++++-------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/core/context/providers/DocsContextProvider.ts b/core/context/providers/DocsContextProvider.ts index 47542bc1c..9ee50c256 100644 --- a/core/context/providers/DocsContextProvider.ts +++ b/core/context/providers/DocsContextProvider.ts @@ -94,7 +94,7 @@ class DocsContextProvider extends BaseContextProvider { query, this.options?.nRetrieve ?? DocsContextProvider.nRetrieve, ); - if(!chunks?.length) { + if (!chunks?.length) { return []; } @@ -160,7 +160,7 @@ class DocsContextProvider extends BaseContextProvider { const docs = (await docsService.listMetadata()) ?? []; for (const { startUrl, title, favicon } of docs) { submenuItemsMap.set(startUrl, { - title,\ + title, id: startUrl, description: new URL(startUrl).hostname, icon: favicon, diff --git a/core/indexing/docs/DocsService.ts b/core/indexing/docs/DocsService.ts index 0af6aa28f..a65c65879 100644 --- a/core/indexing/docs/DocsService.ts +++ b/core/indexing/docs/DocsService.ts @@ -17,7 +17,6 @@ import DocsContextProvider from "../../context/providers/DocsContextProvider"; import TransformersJsEmbeddingsProvider from "../../llm/llms/TransformersJsEmbeddingsProvider"; import { FromCoreProtocol, ToCoreProtocol } from "../../protocol"; import { fetchFavicon, getFaviconBase64 } from "../../util/fetchFavicon"; -import { GlobalContext } from "../../util/GlobalContext"; import { IMessenger } from "../../protocol/messenger"; import { editConfigJson, @@ -87,7 +86,6 @@ export default class DocsService { public isSyncing: boolean = false; private docsIndexingQueue = new Set(); - private globalContext = new GlobalContext(); private lanceTableNamesSet = new Set(); private config!: ContinueConfig; @@ -144,7 +142,7 @@ export default class DocsService { this.config.docs?.forEach((doc) => { if (!doc.startUrl) { - console.error("Invalid config docs entry, no start"); + console.error("Invalid config docs entry, no start url", doc.title); return; } @@ -396,11 +394,12 @@ export default class DocsService { return; } } - this.docsIndexingQueue.add(startUrl); - - this.addToConfig(siteIndexingConfig); try { + this.docsIndexingQueue.add(startUrl); + + this.addToConfig(siteIndexingConfig); + this.handleStatusUpdate({ ...fixedStatus, status: "indexing", @@ -710,16 +709,11 @@ export default class DocsService { await db.exec(`CREATE TABLE IF NOT EXISTS ${DocsService.sqlitebTableName} ( id INTEGER PRIMARY KEY AUTOINCREMENT, title STRING NOT NULL, - startUrl STRING NOT NULL UNIQUE, - favicon STRING + startUrl STRING NOT NULL, + favicon STRING, embeddingsProviderId STRING )`); - // embeddingsProviderId was added later - await db.exec(`ALTER TABLE ${DocsService.sqlitebTableName} - ADD COLUMN IF NOT EXISTS embeddingsProviderId STRING; - `); - this.sqliteDb = db; } @@ -756,6 +750,7 @@ export default class DocsService { const newConfigDocs = newConfig.docs || []; const newConfigStartUrls = newConfigDocs.map((doc) => doc.startUrl); + // NOTE since listMetadata filters by embeddings provider id embedding model changes are accounted for here const currentlyIndexedDocs = await this.listMetadata(); const currentStartUrls = currentlyIndexedDocs.map((doc) => doc.startUrl); @@ -955,7 +950,7 @@ export default class DocsService { private addToConfig(siteIndexingConfig: SiteIndexingConfig) { // Handles the case where a user has manually added the doc to config.json // so it already exists in the file - const doesDocExist = this.config.docs?.some( + const doesEquivalentDocExist = this.config.docs?.some( (doc) => doc.startUrl === siteIndexingConfig.startUrl && doc.faviconUrl === siteIndexingConfig.faviconUrl && @@ -964,7 +959,7 @@ export default class DocsService { // doc.rootUrl === siteIndexingConfig.rootUrl, ); - if (!doesDocExist) { + if (!doesEquivalentDocExist) { editConfigJson((config) => ({ ...config, docs: [ diff --git a/core/indexing/docs/migrations.ts b/core/indexing/docs/migrations.ts index e3483d507..b84b0b842 100644 --- a/core/indexing/docs/migrations.ts +++ b/core/indexing/docs/migrations.ts @@ -27,41 +27,50 @@ export async function runLanceMigrations(table: Table) { export async function runSqliteMigrations(db: Database) { await new Promise((resolve) => { - migrate( + void migrate( "sqlite_modify_docs_columns_and_copy_to_config", async () => { try { const pragma = await db.all( - `PRAGMA table_info(${DocsService.sqlitebTableName})`, + `PRAGMA table_info(${DocsService.sqlitebTableName});`, ); const hasFaviconCol = pragma.some( (pragma) => pragma.name === "favicon", ); + if (!hasFaviconCol) { + await db.exec( + `ALTER TABLE ${DocsService.sqlitebTableName} ADD COLUMN favicon BLOB;`, + ); + } + const hasBaseUrlCol = pragma.some( (pragma) => pragma.name === "baseUrl", ); - - if (!hasFaviconCol) { - await db.exec( - `ALTER TABLE ${DocsService.sqlitebTableName} ADD COLUMN favicon BLOB`, - ); - } - if (hasBaseUrlCol) { await db.exec( - `ALTER TABLE ${DocsService.sqlitebTableName} RENAME COLUMN baseUrl TO startUrl`, + `ALTER TABLE ${DocsService.sqlitebTableName} RENAME COLUMN baseUrl TO startUrl;`, ); } - const sqliteDocs = await db.all< - Array> - >(`SELECT title, startUrl FROM ${DocsService.sqlitebTableName}`); + const hasEmbeddingsProviderColumn = pragma.some( + (pragma) => pragma.name === "embeddingsProviderId", + ); + if (!hasEmbeddingsProviderColumn) { + // gotta just delete in this case since old docs will be unusable anyway + await db.exec(`DROP TABLE ${DocsService.sqlitebTableName};`); + } - editConfigJson((config) => ({ - ...config, - docs: [...(config.docs || []), ...sqliteDocs], - })); + const needsToUpdateConfig = !hasFaviconCol || hasBaseUrlCol; + if (needsToUpdateConfig) { + const sqliteDocs = await db.all< + Array> + >(`SELECT title, startUrl FROM ${DocsService.sqlitebTableName}`); + editConfigJson((config) => ({ + ...config, + docs: [...(config.docs || []), ...sqliteDocs], + })); + } } finally { resolve(undefined); }