@@ -26,6 +26,7 @@ import {
26
26
lte ,
27
27
or ,
28
28
sql ,
29
+ not ,
29
30
} from 'drizzle-orm' ;
30
31
import { v4 } from 'uuid' ;
31
32
import { DIMENSION_MAP , type EmbeddingDimensionColumn } from './schema/embedding' ;
@@ -291,11 +292,86 @@ export abstract class BaseDrizzleAdapter<
291
292
* @returns {Promise<boolean> } - A boolean indicating if the deletion was successful.
292
293
*/
293
294
async deleteAgent ( agentId : UUID ) : Promise < boolean > {
294
- // casacade delete all related for the agent
295
295
return this . withDatabase ( async ( ) => {
296
296
await this . db . transaction ( async ( tx ) => {
297
+ const entities = await this . db
298
+ . select ( { entityId : entityTable . id } )
299
+ . from ( entityTable )
300
+ . where ( eq ( entityTable . agentId , agentId ) ) ;
301
+
302
+ const entityIds = entities . map ( ( e ) => e . entityId ) ;
303
+
304
+ let memoryIds : UUID [ ] = [ ] ;
305
+
306
+ if ( entityIds . length > 0 ) {
307
+ const entityMemories = await this . db
308
+ . select ( { memoryId : memoryTable . id } )
309
+ . from ( memoryTable )
310
+ . where ( inArray ( memoryTable . entityId , entityIds ) ) ;
311
+
312
+ memoryIds = entityMemories . map ( ( m ) => m . memoryId ) ;
313
+ }
314
+
315
+ const agentMemories = await this . db
316
+ . select ( { memoryId : memoryTable . id } )
317
+ . from ( memoryTable )
318
+ . where ( eq ( memoryTable . agentId , agentId ) ) ;
319
+
320
+ memoryIds . push ( ...agentMemories . map ( ( m ) => m . memoryId ) ) ;
321
+
322
+ if ( memoryIds . length > 0 ) {
323
+ await tx . delete ( embeddingTable ) . where ( inArray ( embeddingTable . memoryId , memoryIds ) ) ;
324
+
325
+ await tx . delete ( memoryTable ) . where ( inArray ( memoryTable . id , memoryIds ) ) ;
326
+ }
327
+
328
+ const rooms = await this . db
329
+ . select ( { roomId : roomTable . id } )
330
+ . from ( roomTable )
331
+ . where ( eq ( roomTable . agentId , agentId ) ) ;
332
+
333
+ const roomIds = rooms . map ( ( r ) => r . roomId ) ;
334
+
335
+ if ( entityIds . length > 0 ) {
336
+ await tx . delete ( logTable ) . where ( inArray ( logTable . entityId , entityIds ) ) ;
337
+ await tx . delete ( participantTable ) . where ( inArray ( participantTable . entityId , entityIds ) ) ;
338
+ }
339
+
340
+ if ( roomIds . length > 0 ) {
341
+ await tx . delete ( logTable ) . where ( inArray ( logTable . roomId , roomIds ) ) ;
342
+ await tx . delete ( participantTable ) . where ( inArray ( participantTable . roomId , roomIds ) ) ;
343
+ }
344
+
345
+ await tx . delete ( participantTable ) . where ( eq ( participantTable . agentId , agentId ) ) ;
346
+
347
+ if ( roomIds . length > 0 ) {
348
+ await tx . delete ( roomTable ) . where ( inArray ( roomTable . id , roomIds ) ) ;
349
+ }
350
+
351
+ await tx . delete ( cacheTable ) . where ( eq ( cacheTable . agentId , agentId ) ) ;
352
+
353
+ await tx . delete ( relationshipTable ) . where ( eq ( relationshipTable . agentId , agentId ) ) ;
354
+
355
+ await tx . delete ( entityTable ) . where ( eq ( entityTable . agentId , agentId ) ) ;
356
+
357
+ const newAgent = await this . db
358
+ . select ( { newAgentId : agentTable . id } )
359
+ . from ( agentTable )
360
+ . where ( not ( eq ( agentTable . id , agentId ) ) )
361
+ . limit ( 1 ) ;
362
+
363
+ if ( newAgent . length > 0 ) {
364
+ await tx
365
+ . update ( worldTable )
366
+ . set ( { agentId : newAgent [ 0 ] . newAgentId } )
367
+ . where ( eq ( worldTable . agentId , agentId ) ) ;
368
+ } else {
369
+ await tx . delete ( worldTable ) . where ( eq ( worldTable . agentId , agentId ) ) ;
370
+ }
371
+
297
372
await tx . delete ( agentTable ) . where ( eq ( agentTable . id , agentId ) ) ;
298
373
} ) ;
374
+
299
375
return true ;
300
376
} ) ;
301
377
}
0 commit comments