@@ -16,6 +16,7 @@ import {
16
16
type Relationship ,
17
17
type UUID ,
18
18
RAGKnowledgeItem ,
19
+ type ChunkRow ,
19
20
} from "@elizaos/core" ;
20
21
import { Database } from "better-sqlite3" ;
21
22
import { v4 } from "uuid" ;
@@ -966,8 +967,106 @@ export class SqliteDatabaseAdapter
966
967
}
967
968
968
969
async removeKnowledge ( id : UUID ) : Promise < void > {
969
- const sql = `DELETE FROM knowledge WHERE id = ?` ;
970
- this . db . prepare ( sql ) . run ( id ) ;
970
+ if ( typeof id !== "string" ) {
971
+ throw new Error ( "Knowledge ID must be a string" ) ;
972
+ }
973
+
974
+ try {
975
+ // Execute the transaction and ensure it's called with ()
976
+ await this . db . transaction ( ( ) => {
977
+ if ( id . includes ( "*" ) ) {
978
+ const pattern = id . replace ( "*" , "%" ) ;
979
+ const sql = "DELETE FROM knowledge WHERE id LIKE ?" ;
980
+ elizaLogger . debug (
981
+ `[Knowledge Remove] Executing SQL: ${ sql } with pattern: ${ pattern } `
982
+ ) ;
983
+ const stmt = this . db . prepare ( sql ) ;
984
+ const result = stmt . run ( pattern ) ;
985
+ elizaLogger . debug (
986
+ `[Knowledge Remove] Pattern deletion affected ${ result . changes } rows`
987
+ ) ;
988
+ return result . changes ; // Return changes for logging
989
+ } else {
990
+ // Log queries before execution
991
+ const selectSql = "SELECT id FROM knowledge WHERE id = ?" ;
992
+ const chunkSql =
993
+ "SELECT id FROM knowledge WHERE json_extract(content, '$.metadata.originalId') = ?" ;
994
+ elizaLogger . debug ( `[Knowledge Remove] Checking existence with:
995
+ Main: ${ selectSql } [${ id } ]
996
+ Chunks: ${ chunkSql } [${ id } ]` ) ;
997
+
998
+ const mainEntry = this . db . prepare ( selectSql ) . get ( id ) as
999
+ | ChunkRow
1000
+ | undefined ;
1001
+ const chunks = this . db
1002
+ . prepare ( chunkSql )
1003
+ . all ( id ) as ChunkRow [ ] ;
1004
+
1005
+ elizaLogger . debug ( `[Knowledge Remove] Found:` , {
1006
+ mainEntryExists : ! ! mainEntry ?. id ,
1007
+ chunkCount : chunks . length ,
1008
+ chunkIds : chunks . map ( ( c ) => c . id ) ,
1009
+ } ) ;
1010
+
1011
+ // Execute and log chunk deletion
1012
+ const chunkDeleteSql =
1013
+ "DELETE FROM knowledge WHERE json_extract(content, '$.metadata.originalId') = ?" ;
1014
+ elizaLogger . debug (
1015
+ `[Knowledge Remove] Executing chunk deletion: ${ chunkDeleteSql } [${ id } ]`
1016
+ ) ;
1017
+ const chunkResult = this . db . prepare ( chunkDeleteSql ) . run ( id ) ;
1018
+ elizaLogger . debug (
1019
+ `[Knowledge Remove] Chunk deletion affected ${ chunkResult . changes } rows`
1020
+ ) ;
1021
+
1022
+ // Execute and log main entry deletion
1023
+ const mainDeleteSql = "DELETE FROM knowledge WHERE id = ?" ;
1024
+ elizaLogger . debug (
1025
+ `[Knowledge Remove] Executing main deletion: ${ mainDeleteSql } [${ id } ]`
1026
+ ) ;
1027
+ const mainResult = this . db . prepare ( mainDeleteSql ) . run ( id ) ;
1028
+ elizaLogger . debug (
1029
+ `[Knowledge Remove] Main deletion affected ${ mainResult . changes } rows`
1030
+ ) ;
1031
+
1032
+ const totalChanges =
1033
+ chunkResult . changes + mainResult . changes ;
1034
+ elizaLogger . debug (
1035
+ `[Knowledge Remove] Total rows affected: ${ totalChanges } `
1036
+ ) ;
1037
+
1038
+ // Verify deletion
1039
+ const verifyMain = this . db . prepare ( selectSql ) . get ( id ) ;
1040
+ const verifyChunks = this . db . prepare ( chunkSql ) . all ( id ) ;
1041
+ elizaLogger . debug (
1042
+ `[Knowledge Remove] Post-deletion check:` ,
1043
+ {
1044
+ mainStillExists : ! ! verifyMain ,
1045
+ remainingChunks : verifyChunks . length ,
1046
+ }
1047
+ ) ;
1048
+
1049
+ return totalChanges ; // Return changes for logging
1050
+ }
1051
+ } ) ( ) ; // Important: Call the transaction function
1052
+
1053
+ elizaLogger . debug (
1054
+ `[Knowledge Remove] Transaction completed for id: ${ id } `
1055
+ ) ;
1056
+ } catch ( error ) {
1057
+ elizaLogger . error ( "[Knowledge Remove] Error:" , {
1058
+ id,
1059
+ error :
1060
+ error instanceof Error
1061
+ ? {
1062
+ message : error . message ,
1063
+ stack : error . stack ,
1064
+ name : error . name ,
1065
+ }
1066
+ : error ,
1067
+ } ) ;
1068
+ throw error ;
1069
+ }
971
1070
}
972
1071
973
1072
async clearKnowledge ( agentId : UUID , shared ?: boolean ) : Promise < void > {
0 commit comments