Harden public reply privacy and history lookup
This commit is contained in:
parent
30e860c38e
commit
ada64a1ad1
6 changed files with 106 additions and 40 deletions
|
|
@ -34,6 +34,51 @@ import {
|
|||
} from "@voidbot/shared";
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
function filterPromptEchoHistoryResults<
|
||||
T extends {
|
||||
text: string;
|
||||
},
|
||||
>(results: T[], query: string): T[] {
|
||||
const normalizedQuery = normalizeHistoryEchoText(query);
|
||||
|
||||
if (normalizedQuery.length === 0) {
|
||||
return results;
|
||||
}
|
||||
|
||||
const filtered = results.filter((result) => {
|
||||
const normalizedResult = normalizeHistoryEchoText(result.text);
|
||||
|
||||
if (normalizedResult.length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (normalizedResult === normalizedQuery) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
normalizedQuery.length >= 48 &&
|
||||
(normalizedResult.includes(normalizedQuery) || normalizedQuery.includes(normalizedResult))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
return filtered.length > 0 ? filtered : results;
|
||||
}
|
||||
|
||||
function normalizeHistoryEchoText(value: string): string {
|
||||
return value
|
||||
.replace(/<@!?\d+>/g, " ")
|
||||
.replace(/<@&\d+>/g, " ")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
const embedder = createTextEmbedder({
|
||||
backend: config.ragEmbeddingBackend,
|
||||
hashDimensions: config.ragEmbeddingDimensions,
|
||||
|
|
@ -169,11 +214,12 @@ async function buildProviderRegistry(systemMessagesCatalog: SystemMessageCatalog
|
|||
};
|
||||
},
|
||||
searchHistory: async (input) => {
|
||||
const results = await retrievalService.searchHistory(input.query, input.limit, {
|
||||
const rawResults = await retrievalService.searchHistory(input.query, input.limit, {
|
||||
guildId: input.guildId,
|
||||
channelId: input.channelId,
|
||||
authorId: input.authorId,
|
||||
});
|
||||
const results = filterPromptEchoHistoryResults(rawResults, input.query);
|
||||
|
||||
return {
|
||||
query: input.query,
|
||||
|
|
|
|||
Loading…
Reference in a new issue