mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-06-05 19:24:39 +00:00
feat: vector search worker improvements
This commit is contained in:
@@ -15,7 +15,7 @@ import { waitForElm } from "@/seqta/utils/waitForElm";
|
||||
import { loadAllStoredItems, runIndexing } from "../indexing/indexer";
|
||||
//import { initVectorSearch } from "../search/vector/vectorSearch";
|
||||
import { VectorWorkerManager } from "../indexing/worker/vectorWorkerManager";
|
||||
import indexWorker from "./indexWorker?inlineWorker";
|
||||
import VectorSearchWorkerManager from "../search/vector/vectorSearch";
|
||||
|
||||
const settings = defineSettings({
|
||||
searchHotkey: stringSetting({
|
||||
@@ -88,18 +88,7 @@ const globalSearchPlugin: Plugin<typeof settings> = {
|
||||
run: async (api) => {
|
||||
let app: any;
|
||||
|
||||
console.log("=======================")
|
||||
/* const worker = new Worker(new URL('./indexWorker.ts', import.meta.url), {
|
||||
type: 'module',
|
||||
}); */
|
||||
//const blob = new Blob([indexWorker], { type: 'application/javascript' });
|
||||
const worker = indexWorker();
|
||||
worker.addEventListener("message", (e) => {
|
||||
console.log(e);
|
||||
});
|
||||
|
||||
worker.postMessage({ type: "ready" });
|
||||
console.log("=======================")
|
||||
VectorSearchWorkerManager.getInstance();
|
||||
|
||||
// Run initial indexing and update dynamic items
|
||||
if (api.settings.runIndexingOnLoad) {
|
||||
@@ -165,9 +154,9 @@ const globalSearchPlugin: Plugin<typeof settings> = {
|
||||
if (searchButton) searchButton.remove();
|
||||
if (searchRoot) searchRoot.remove();
|
||||
|
||||
// Clean up vector worker
|
||||
// Clean up workers
|
||||
VectorWorkerManager.getInstance().terminate();
|
||||
|
||||
VectorSearchWorkerManager.getInstance().terminate();
|
||||
unmount(app);
|
||||
};
|
||||
},
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
self.addEventListener('message', (e: Event) => {
|
||||
console.log(e);
|
||||
});
|
||||
|
||||
self.postMessage({ type: 'ready' });
|
||||
@@ -1,17 +1,19 @@
|
||||
import type { VectorSearchResult } from "./vectorTypes";
|
||||
//import vectorSearchWorker from "./vectorSearchWorker?worker";
|
||||
import vectorSearchWorker from "./vectorSearchWorker?inlineWorker";
|
||||
|
||||
export function searchVectors(query: string, topK: number = 10): Promise<VectorSearchResult[]> {
|
||||
//return VectorSearchWorkerManager.getInstance().search(query, topK);
|
||||
return new Promise((resolve) => {
|
||||
return VectorSearchWorkerManager.getInstance().search(query, topK);
|
||||
/* return new Promise((resolve) => {
|
||||
resolve([]);
|
||||
});
|
||||
}); */
|
||||
}
|
||||
|
||||
/* class VectorSearchWorkerManager {
|
||||
class VectorSearchWorkerManager {
|
||||
private static instance: VectorSearchWorkerManager;
|
||||
private worker: Worker | null = null;
|
||||
private pendingSearches = new Map<string, (results: VectorSearchResult[]) => void>();
|
||||
private debounceTimer: NodeJS.Timeout | null = null;
|
||||
private lastSearchParams: { query: string; topK: number; resolve: (results: VectorSearchResult[]) => void } | null = null;
|
||||
|
||||
constructor() {
|
||||
this.initWorker();
|
||||
@@ -19,7 +21,7 @@ export function searchVectors(query: string, topK: number = 10): Promise<VectorS
|
||||
|
||||
private initWorker() {
|
||||
try {
|
||||
this.worker = new vectorSearchWorker({ name: "vectorSearchWorker" });
|
||||
this.worker = vectorSearchWorker();
|
||||
this.worker.addEventListener('message', this.messageHandler);
|
||||
} catch (e) {
|
||||
console.error("Failed to initialize vector search:", e);
|
||||
@@ -46,21 +48,40 @@ export function searchVectors(query: string, topK: number = 10): Promise<VectorS
|
||||
}
|
||||
|
||||
public async search(query: string, topK: number = 10): Promise<VectorSearchResult[]> {
|
||||
console.log("Searching vectors", query, topK);
|
||||
if (!this.worker) {
|
||||
this.initWorker();
|
||||
}
|
||||
|
||||
const messageId = crypto.randomUUID();
|
||||
|
||||
return new Promise((resolve) => {
|
||||
this.pendingSearches.set(messageId, resolve);
|
||||
this.worker?.postMessage({
|
||||
type: "search",
|
||||
data: { query, topK },
|
||||
messageId
|
||||
});
|
||||
this.lastSearchParams = { query, topK, resolve };
|
||||
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
||||
this.debounceTimer = setTimeout(() => {
|
||||
const messageId = crypto.randomUUID();
|
||||
if (this.lastSearchParams) {
|
||||
this.pendingSearches.set(messageId, this.lastSearchParams.resolve);
|
||||
this.worker?.postMessage({
|
||||
type: "search",
|
||||
data: { query: this.lastSearchParams.query, topK: this.lastSearchParams.topK },
|
||||
messageId
|
||||
});
|
||||
this.lastSearchParams = null;
|
||||
}
|
||||
this.debounceTimer = null;
|
||||
}, query !== '' ? 300 : 0);
|
||||
});
|
||||
}
|
||||
|
||||
public terminate() {
|
||||
if (this.worker) {
|
||||
for (const [messageId, resolve] of this.pendingSearches.entries()) {
|
||||
resolve([]);
|
||||
this.pendingSearches.delete(messageId);
|
||||
}
|
||||
|
||||
this.worker.terminate();
|
||||
this.worker = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default VectorSearchWorkerManager; */
|
||||
export default VectorSearchWorkerManager;
|
||||
@@ -44,16 +44,13 @@ self.addEventListener('message', async (e) => {
|
||||
switch (type) {
|
||||
case 'search':
|
||||
console.log("Search request received", data);
|
||||
const results = await searchVectors(data.query, data.topK);
|
||||
self.postMessage({ type: 'searchResults', data: { messageId, results } });
|
||||
searchVectors(data.query, data.topK).then((results) => {
|
||||
self.postMessage({ type: 'searchResults', data: { messageId, results } });
|
||||
});
|
||||
break;
|
||||
default:
|
||||
console.warn(`Unknown message type: ${type}`);
|
||||
}
|
||||
});
|
||||
|
||||
initVectorSearch();
|
||||
|
||||
export default function test() {
|
||||
console.log("%cTest!!!", "background-color: #000; color: #fff;");
|
||||
}
|
||||
initVectorSearch();
|
||||
Reference in New Issue
Block a user