forked from lioensky/VCPToolBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorizationWorker.js
More file actions
62 lines (54 loc) · 2.68 KB
/
Copy pathvectorizationWorker.js
File metadata and controls
62 lines (54 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// vectorizationWorker.js
const { parentPort, workerData } = require('worker_threads');
// 强制清除TextChunker模块缓存,确保使用最新版本
const textChunkerPath = require.resolve('./TextChunker.js');
if (require.cache[textChunkerPath]) {
delete require.cache[textChunkerPath];
}
// We only import the processing function, not the whole class.
const { processSingleDiaryBookInWorker } = require('./VectorDBManager.js');
/**
* This worker is responsible for processing a single diary book in the background
* to avoid blocking the main server's event loop. It's a stateless worker
* that receives all necessary data and configuration from the main thread.
*/
async function run() {
if (!parentPort) return;
const { task, diaryName, config, chunksToAdd } = workerData;
try {
switch (task) {
case 'fullRebuild':
if (!diaryName || !config) throw new Error('Worker (fullRebuild) started without required diaryName or config.');
console.log(`[VectorDB][Worker] Starting full rebuild for: ${diaryName}`);
const newManifestEntry = await processSingleDiaryBookInWorker(diaryName, config);
if (newManifestEntry) {
parentPort.postMessage({ status: 'success', task: 'fullRebuild', diaryName, newManifestEntry });
} else {
throw new Error('Full rebuild processing returned null or undefined manifest entry.');
}
break;
case 'incrementalUpdate':
if (!diaryName || !config || !chunksToAdd) throw new Error('Worker (incrementalUpdate) started without required data.');
console.log(`[VectorDB][Worker] Starting incremental update for ${diaryName}: ${chunksToAdd.length} chunks to add.`);
const newVectorsData = await processIncrementalUpdateInWorker(chunksToAdd, config);
if (newVectorsData) {
parentPort.postMessage({ status: 'success', task: 'incrementalUpdate', diaryName, newVectorsData });
} else {
throw new Error('Incremental update processing returned null data.');
}
break;
default:
throw new Error(`Unknown task type received: ${task}`);
}
} catch (error) {
console.error(`[VectorDB][Worker] Error during task "${task}" for "${diaryName}":`, error);
parentPort.postMessage({
status: 'error',
task: task,
diaryName: diaryName,
error: error.message,
stack: error.stack
});
}
}
run();