mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-06-06 03:34:40 +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 { loadAllStoredItems, runIndexing } from "../indexing/indexer";
|
||||||
//import { initVectorSearch } from "../search/vector/vectorSearch";
|
//import { initVectorSearch } from "../search/vector/vectorSearch";
|
||||||
import { VectorWorkerManager } from "../indexing/worker/vectorWorkerManager";
|
import { VectorWorkerManager } from "../indexing/worker/vectorWorkerManager";
|
||||||
import indexWorker from "./indexWorker?inlineWorker";
|
import VectorSearchWorkerManager from "../search/vector/vectorSearch";
|
||||||
|
|
||||||
const settings = defineSettings({
|
const settings = defineSettings({
|
||||||
searchHotkey: stringSetting({
|
searchHotkey: stringSetting({
|
||||||
@@ -88,18 +88,7 @@ const globalSearchPlugin: Plugin<typeof settings> = {
|
|||||||
run: async (api) => {
|
run: async (api) => {
|
||||||
let app: any;
|
let app: any;
|
||||||
|
|
||||||
console.log("=======================")
|
VectorSearchWorkerManager.getInstance();
|
||||||
/* 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("=======================")
|
|
||||||
|
|
||||||
// Run initial indexing and update dynamic items
|
// Run initial indexing and update dynamic items
|
||||||
if (api.settings.runIndexingOnLoad) {
|
if (api.settings.runIndexingOnLoad) {
|
||||||
@@ -165,9 +154,9 @@ const globalSearchPlugin: Plugin<typeof settings> = {
|
|||||||
if (searchButton) searchButton.remove();
|
if (searchButton) searchButton.remove();
|
||||||
if (searchRoot) searchRoot.remove();
|
if (searchRoot) searchRoot.remove();
|
||||||
|
|
||||||
// Clean up vector worker
|
// Clean up workers
|
||||||
VectorWorkerManager.getInstance().terminate();
|
VectorWorkerManager.getInstance().terminate();
|
||||||
|
VectorSearchWorkerManager.getInstance().terminate();
|
||||||
unmount(app);
|
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 type { VectorSearchResult } from "./vectorTypes";
|
||||||
//import vectorSearchWorker from "./vectorSearchWorker?worker";
|
import vectorSearchWorker from "./vectorSearchWorker?inlineWorker";
|
||||||
|
|
||||||
export function searchVectors(query: string, topK: number = 10): Promise<VectorSearchResult[]> {
|
export function searchVectors(query: string, topK: number = 10): Promise<VectorSearchResult[]> {
|
||||||
//return VectorSearchWorkerManager.getInstance().search(query, topK);
|
return VectorSearchWorkerManager.getInstance().search(query, topK);
|
||||||
return new Promise((resolve) => {
|
/* return new Promise((resolve) => {
|
||||||
resolve([]);
|
resolve([]);
|
||||||
});
|
}); */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* class VectorSearchWorkerManager {
|
class VectorSearchWorkerManager {
|
||||||
private static instance: VectorSearchWorkerManager;
|
private static instance: VectorSearchWorkerManager;
|
||||||
private worker: Worker | null = null;
|
private worker: Worker | null = null;
|
||||||
private pendingSearches = new Map<string, (results: VectorSearchResult[]) => void>();
|
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() {
|
constructor() {
|
||||||
this.initWorker();
|
this.initWorker();
|
||||||
@@ -19,7 +21,7 @@ export function searchVectors(query: string, topK: number = 10): Promise<VectorS
|
|||||||
|
|
||||||
private initWorker() {
|
private initWorker() {
|
||||||
try {
|
try {
|
||||||
this.worker = new vectorSearchWorker({ name: "vectorSearchWorker" });
|
this.worker = vectorSearchWorker();
|
||||||
this.worker.addEventListener('message', this.messageHandler);
|
this.worker.addEventListener('message', this.messageHandler);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Failed to initialize vector search:", 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[]> {
|
public async search(query: string, topK: number = 10): Promise<VectorSearchResult[]> {
|
||||||
console.log("Searching vectors", query, topK);
|
|
||||||
if (!this.worker) {
|
if (!this.worker) {
|
||||||
this.initWorker();
|
this.initWorker();
|
||||||
}
|
}
|
||||||
|
|
||||||
const messageId = crypto.randomUUID();
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
this.pendingSearches.set(messageId, resolve);
|
this.lastSearchParams = { query, topK, resolve };
|
||||||
this.worker?.postMessage({
|
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
||||||
type: "search",
|
this.debounceTimer = setTimeout(() => {
|
||||||
data: { query, topK },
|
const messageId = crypto.randomUUID();
|
||||||
messageId
|
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,8 +44,9 @@ self.addEventListener('message', async (e) => {
|
|||||||
switch (type) {
|
switch (type) {
|
||||||
case 'search':
|
case 'search':
|
||||||
console.log("Search request received", data);
|
console.log("Search request received", data);
|
||||||
const results = await searchVectors(data.query, data.topK);
|
searchVectors(data.query, data.topK).then((results) => {
|
||||||
self.postMessage({ type: 'searchResults', data: { messageId, results } });
|
self.postMessage({ type: 'searchResults', data: { messageId, results } });
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.warn(`Unknown message type: ${type}`);
|
console.warn(`Unknown message type: ${type}`);
|
||||||
@@ -53,7 +54,3 @@ self.addEventListener('message', async (e) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
initVectorSearch();
|
initVectorSearch();
|
||||||
|
|
||||||
export default function test() {
|
|
||||||
console.log("%cTest!!!", "background-color: #000; color: #fff;");
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user