diff --git a/core/indexing/CodeSnippetsIndex.ts b/core/indexing/CodeSnippetsIndex.ts index a5ec68f8b..8fc1ccb95 100644 --- a/core/indexing/CodeSnippetsIndex.ts +++ b/core/indexing/CodeSnippetsIndex.ts @@ -7,6 +7,7 @@ import type { IndexingProgressUpdate, } from "../index.js"; import { getBasename, getLastNPathParts } from "../util/index.js"; +import { migrate } from "../util/paths.js"; import { TSQueryType, getParserForFile, @@ -43,6 +44,55 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { snippetId INTEGER NOT NULL, FOREIGN KEY (snippetId) REFERENCES code_snippets (id) )`); + + migrate("delete_duplicate_code_snippets", async () => { + // Delete duplicate entries in code_snippets + await db.exec(` + DELETE FROM code_snippets + WHERE id NOT IN ( + SELECT MIN(id) + FROM code_snippets + GROUP BY path, cacheKey, content, title, startLine, endLine + ) + `); + + // Add unique constraint if it doesn't exist + await db.exec(` + CREATE UNIQUE INDEX IF NOT EXISTS idx_code_snippets_unique + ON code_snippets (path, cacheKey, content, title, startLine, endLine) + `); + + // Delete code_snippets associated with duplicate code_snippets_tags entries + await db.exec(` + DELETE FROM code_snippets + WHERE id IN ( + SELECT snippetId + FROM code_snippets_tags + WHERE (snippetId, tag) IN ( + SELECT snippetId, tag + FROM code_snippets_tags + GROUP BY snippetId, tag + HAVING COUNT(*) > 1 + ) + ) + `); + + // Delete duplicate entries + await db.exec(` + DELETE FROM code_snippets_tags + WHERE id NOT IN ( + SELECT MIN(id) + FROM code_snippets_tags + GROUP BY snippetId, tag + ) + `); + + // Add unique constraint if it doesn't exist + await db.exec(` + CREATE UNIQUE INDEX IF NOT EXISTS idx_snippetId_tag + ON code_snippets_tags (snippetId, tag) + `); + }); } async getSnippetsInFile( @@ -93,12 +143,13 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { ); } catch (e) { // If can't parse, assume malformatted code + console.error(`Error parsing ${compute.path}:`, e); } // Add snippets to sqlite for (const snippet of snippets) { const { lastID } = await db.run( - "INSERT INTO code_snippets (path, cacheKey, content, title, startLine, endLine) VALUES (?, ?, ?, ?, ?, ?)", + "REPLACE INTO code_snippets (path, cacheKey, content, title, startLine, endLine) VALUES (?, ?, ?, ?, ?, ?)", [ compute.path, compute.cacheKey, @@ -110,7 +161,7 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { ); await db.run( - "INSERT INTO code_snippets_tags (snippetId, tag) VALUES (?, ?)", + "REPLACE INTO code_snippets_tags (snippetId, tag) VALUES (?, ?)", [lastID, tagString], ); } @@ -136,15 +187,33 @@ export class CodeSnippetsCodebaseIndex implements CodebaseIndex { } for (let i = 0; i < results.addTag.length; i++) { - const snippetsWithPath = await db.all( - "SELECT * FROM code_snippets WHERE cacheKey = ?", - [results.addTag[i].cacheKey], - ); + const addTag = results.addTag[i]; + let snippets: (ChunkWithoutID & { title: string })[] = []; + try { + snippets = await this.getSnippetsInFile( + addTag.path, + await this.ide.readFile(addTag.path), + ); + } catch (e) { + // If can't parse, assume malformatted code + console.error(`Error parsing ${addTag.path}:`, e); + } - for (const snippet of snippetsWithPath) { + for (const snippet of snippets) { + const { lastID } = await db.run( + "INSERT INTO code_snippets (path, cacheKey, content, title, startLine, endLine) VALUES (?, ?, ?, ?, ?, ?)", + [ + addTag.path, + addTag.cacheKey, + snippet.content, + snippet.title, + snippet.startLine, + snippet.endLine, + ], + ); await db.run( "INSERT INTO code_snippets_tags (snippetId, tag) VALUES (?, ?)", - [snippet.id, tagString], + [lastID, tagString], ); } diff --git a/gui/package-lock.json b/gui/package-lock.json index d2cf7916d..68ffbcc48 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -29,7 +29,7 @@ "dompurify": "^3.0.6", "downshift": "^7.6.0", "lodash": "^4.17.21", - "minisearch": "^6.3.0", + "minisearch": "^7.0.2", "onigasm": "^2.2.5", "posthog-js": "^1.130.1", "prismjs": "^1.29.0", @@ -17252,8 +17252,9 @@ } }, "node_modules/minisearch": { - "version": "6.3.0", - "license": "MIT" + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/minisearch/-/minisearch-7.0.2.tgz", + "integrity": "sha512-Pf0sFXaCgRpOBDr4G8wTbVAEH9o9rvJzCMwj0TMe3L/NfUuG188xabfx6Vm3vD/Dv5L500n7JeiMB9Mq3sWMfQ==" }, "node_modules/monaco-editor": { "version": "0.49.0", diff --git a/gui/package.json b/gui/package.json index 0d4ab0fd5..2b6bff3d1 100644 --- a/gui/package.json +++ b/gui/package.json @@ -32,7 +32,7 @@ "dompurify": "^3.0.6", "downshift": "^7.6.0", "lodash": "^4.17.21", - "minisearch": "^6.3.0", + "minisearch": "^7.0.2", "onigasm": "^2.2.5", "posthog-js": "^1.130.1", "prismjs": "^1.29.0", diff --git a/gui/src/hooks/useSubmenuContextProviders.tsx b/gui/src/hooks/useSubmenuContextProviders.tsx index 262b45eee..b41526ce9 100644 --- a/gui/src/hooks/useSubmenuContextProviders.tsx +++ b/gui/src/hooks/useSubmenuContextProviders.tsx @@ -197,33 +197,51 @@ function useSubmenuContextProviders() { return; } setLoaded(true); - contextProviderDescriptions.forEach(async (description) => { - const minisearch = new MiniSearch({ - fields: ["title", "description"], - storeFields: ["id", "title", "description"], - }); - const items = await ideMessenger.request("context/loadSubmenuItems", { - title: description.title, - }); - minisearch.addAll(items); - setMinisearches((prev) => ({ ...prev, [description.title]: minisearch })); - if (description.title === "file") { - const openFiles = await getOpenFileItems(); - setFallbackResults((prev) => ({ + const loadSubmenuItems = async () => { + for (const description of contextProviderDescriptions) { + const minisearch = new MiniSearch({ + fields: ["title", "description"], + storeFields: ["id", "title", "description"], + }); + const items = await ideMessenger.request("context/loadSubmenuItems", { + title: description.title, + }); + + try { + minisearch.addAll(items); + } catch (itemError) { + console.error( + "Error adding item to minisearch:", + itemError.message, + itemError.stack, + ); + } + + setMinisearches((prev) => ({ ...prev, - file: [ - ...openFiles, - ...items.slice(0, MAX_LENGTH - openFiles.length), - ], - })); - } else { - setFallbackResults((prev) => ({ - ...prev, - [description.title]: items.slice(0, MAX_LENGTH), + [description.title]: minisearch, })); + + if (description.title === "file") { + const openFiles = await getOpenFileItems(); + setFallbackResults((prev) => ({ + ...prev, + file: [ + ...openFiles, + ...items.slice(0, MAX_LENGTH - openFiles.length), + ], + })); + } else { + setFallbackResults((prev) => ({ + ...prev, + [description.title]: items.slice(0, MAX_LENGTH), + })); + } } - }); + }; + + loadSubmenuItems(); }, [contextProviderDescriptions, loaded]); useWebviewListener("configUpdate", async () => {