-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathdelete-knowledge-base-document.ts
More file actions
285 lines (259 loc) · 9.69 KB
/
delete-knowledge-base-document.ts
File metadata and controls
285 lines (259 loc) · 9.69 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { logger, tags, task } from '@trigger.dev/sdk';
import { findEmbeddingsForSource } from '@/vector-store/lib/core/find-existing-embeddings';
import { vectorIndex } from '@/vector-store/lib/core/client';
import { db } from '@db';
/**
* Task to delete all embeddings for a Knowledge Base document from vector database
*/
export const deleteKnowledgeBaseDocumentTask = task({
id: 'delete-knowledge-base-document-from-vector',
retry: {
maxAttempts: 3,
},
run: async (payload: { documentId: string; organizationId: string }) => {
await tags.add([`org:${payload.organizationId}`]);
logger.info('Deleting Knowledge Base document from vector DB', {
documentId: payload.documentId,
organizationId: payload.organizationId,
});
try {
// Fetch document info to use document name in query (helps find all chunks)
let documentName: string | undefined;
try {
const document = await db.knowledgeBaseDocument.findUnique({
where: {
id: payload.documentId,
organizationId: payload.organizationId,
},
select: {
name: true,
},
});
documentName = document?.name;
} catch (dbError) {
logger.warn('Could not fetch document name, proceeding without it', {
documentId: payload.documentId,
error: dbError instanceof Error ? dbError.message : 'Unknown error',
});
}
// Find all embeddings for this document
// Pass documentName to help find all chunks (used in query strategies)
const existingEmbeddings = await findEmbeddingsForSource(
payload.documentId,
'knowledge_base_document',
payload.organizationId,
documentName, // Optional: helps find chunks semantically similar to document name
);
if (existingEmbeddings.length === 0) {
logger.info('No embeddings found for document', {
documentId: payload.documentId,
});
return {
success: true,
documentId: payload.documentId,
deletedCount: 0,
};
}
// Delete all embeddings
if (!vectorIndex) {
logger.error('Vector index not configured');
return {
success: false,
documentId: payload.documentId,
error: 'Vector index not configured',
};
}
const idsToDelete = existingEmbeddings.map((e) => e.id);
if (idsToDelete.length === 0) {
logger.info('No embeddings to delete for document', {
documentId: payload.documentId,
});
return {
success: true,
documentId: payload.documentId,
deletedCount: 0,
};
}
// Delete all embeddings in batches (Upstash Vector supports batch delete)
const batchSize = 100;
let deletedCount = 0;
for (let i = 0; i < idsToDelete.length; i += batchSize) {
const batch = idsToDelete.slice(i, i + batchSize);
try {
await vectorIndex.delete(batch);
deletedCount += batch.length;
logger.info('Deleted batch of embeddings', {
documentId: payload.documentId,
batchSize: batch.length,
totalDeleted: deletedCount,
totalToDelete: idsToDelete.length,
});
} catch (batchError) {
logger.error('Error deleting batch of embeddings', {
documentId: payload.documentId,
batchSize: batch.length,
error:
batchError instanceof Error
? batchError.message
: 'Unknown error',
});
// Continue with next batch even if one fails
}
}
// Verify deletion with retry logic (with delays to allow propagation)
// This helps catch cases where some chunks might have been missed or not found initially
// Use the enhanced findEmbeddingsForSource which now includes chunk content queries
let remainingEmbeddings = await findEmbeddingsForSource(
payload.documentId,
'knowledge_base_document',
payload.organizationId,
documentName, // Use document name in verification queries too
);
logger.info('Initial verification after deletion', {
documentId: payload.documentId,
remainingCount: remainingEmbeddings.length,
remainingIds: remainingEmbeddings.map((e) => e.id),
});
// Retry deletion up to 3 times if chunks remain
let retryAttempt = 0;
const maxRetries = 3;
while (remainingEmbeddings.length > 0 && retryAttempt < maxRetries) {
retryAttempt++;
logger.warn(
'Some embeddings were not deleted, attempting retry deletion',
{
documentId: payload.documentId,
remainingCount: remainingEmbeddings.length,
remainingIds: remainingEmbeddings.map((e) => e.id),
retryAttempt,
maxRetries,
},
);
// Wait before retry to allow propagation
await new Promise((resolve) =>
setTimeout(resolve, 2000 * retryAttempt),
); // Increasing delay
// Try deleting remaining chunks
const remainingIds = remainingEmbeddings.map((e) => e.id);
try {
// Delete in batches
const batchSize = 100;
for (let i = 0; i < remainingIds.length; i += batchSize) {
const batch = remainingIds.slice(i, i + batchSize);
await vectorIndex.delete(batch);
deletedCount += batch.length;
}
logger.info('Deleted remaining embeddings in retry attempt', {
documentId: payload.documentId,
deletedCount: remainingIds.length,
retryAttempt,
});
} catch (retryError) {
logger.error('Error deleting remaining embeddings in retry attempt', {
documentId: payload.documentId,
retryAttempt,
error:
retryError instanceof Error
? retryError.message
: 'Unknown error',
});
}
// Query again to check if deletion was successful
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for propagation
remainingEmbeddings = await findEmbeddingsForSource(
payload.documentId,
'knowledge_base_document',
payload.organizationId,
documentName, // Use document name in retry queries too
);
}
// Final verification - if chunks still remain, try one more aggressive search
if (remainingEmbeddings.length > 0) {
logger.warn(
'Chunks still remain after retries, attempting final aggressive search',
{
documentId: payload.documentId,
remainingCount: remainingEmbeddings.length,
remainingIds: remainingEmbeddings.map((e) => e.id),
},
);
// Wait a bit longer for final attempt
await new Promise((resolve) => setTimeout(resolve, 3000));
// Try one more time with enhanced search (now includes chunk content queries)
const finalRemainingEmbeddings = await findEmbeddingsForSource(
payload.documentId,
'knowledge_base_document',
payload.organizationId,
documentName,
);
if (finalRemainingEmbeddings.length > 0) {
// Try deleting these final chunks
const finalIds = finalRemainingEmbeddings.map((e) => e.id);
try {
await vectorIndex.delete(finalIds);
deletedCount += finalIds.length;
logger.info('Deleted chunks in final aggressive attempt', {
documentId: payload.documentId,
deletedCount: finalIds.length,
});
} catch (finalError) {
logger.error('Error in final deletion attempt', {
documentId: payload.documentId,
error:
finalError instanceof Error
? finalError.message
: 'Unknown error',
});
}
// Final check
await new Promise((resolve) => setTimeout(resolve, 2000));
const trulyRemaining = await findEmbeddingsForSource(
payload.documentId,
'knowledge_base_document',
payload.organizationId,
documentName,
);
if (trulyRemaining.length > 0) {
logger.error(
'CRITICAL: Some embeddings still remain after all deletion attempts',
{
documentId: payload.documentId,
remainingCount: trulyRemaining.length,
remainingIds: trulyRemaining.map((e) => e.id),
remainingChunks: trulyRemaining.map((e) => ({
id: e.id,
sourceId: e.sourceId,
updatedAt: e.updatedAt,
})),
note: 'These chunks may need manual deletion or there may be a synchronization issue with Upstash Vector',
},
);
}
}
}
logger.info(
'Successfully deleted Knowledge Base document embeddings from vector DB',
{
documentId: payload.documentId,
deletedCount,
totalFound: idsToDelete.length,
},
);
return {
success: true,
documentId: payload.documentId,
deletedCount,
};
} catch (error) {
logger.error('Error deleting Knowledge Base document from vector DB', {
documentId: payload.documentId,
error: error instanceof Error ? error.message : 'Unknown error',
});
return {
success: false,
documentId: payload.documentId,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
},
});