Skip to content

Commit 7b13d77

Browse files
authored
Merge pull request #2351 from augchan42/feature/rag-knowledge-improvements-v2
feat: (ragKnowledge) Enhance RAG knowledge handling
2 parents 6dd1679 + 8ef2dc7 commit 7b13d77

File tree

7 files changed

+661
-106
lines changed

7 files changed

+661
-106
lines changed

packages/adapter-sqlite/src/index.ts

+101-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
type Relationship,
1717
type UUID,
1818
RAGKnowledgeItem,
19+
type ChunkRow,
1920
} from "@elizaos/core";
2021
import { Database } from "better-sqlite3";
2122
import { v4 } from "uuid";
@@ -966,8 +967,106 @@ export class SqliteDatabaseAdapter
966967
}
967968

968969
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+
}
9711070
}
9721071

9731072
async clearKnowledge(agentId: UUID, shared?: boolean): Promise<void> {

packages/core/src/environment.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,23 @@ export const CharacterSchema = z.object({
7777
postExamples: z.array(z.string()),
7878
topics: z.array(z.string()),
7979
adjectives: z.array(z.string()),
80-
knowledge: z.array(
81-
z.union([
82-
z.string(),
83-
z.object({
84-
path: z.string(),
85-
shared: z.boolean().optional()
86-
})
87-
])
88-
).optional(),
80+
knowledge: z
81+
.array(
82+
z.union([
83+
z.string(), // Direct knowledge strings
84+
z.object({
85+
// Individual file config
86+
path: z.string(),
87+
shared: z.boolean().optional(),
88+
}),
89+
z.object({
90+
// Directory config
91+
directory: z.string(),
92+
shared: z.boolean().optional(),
93+
}),
94+
])
95+
)
96+
.optional(),
8997
clients: z.array(z.nativeEnum(Clients)),
9098
plugins: z.union([z.array(z.string()), z.array(PluginSchema)]),
9199
settings: z

0 commit comments

Comments
 (0)