Exclude bot prompts from default history retrieval

This commit is contained in:
Meta Vens 2026-05-04 09:54:27 +01:00
commit 0ce27b933f
23 changed files with 683 additions and 247 deletions

View file

@ -19,11 +19,13 @@ import {
ProviderRegistry,
} from "@voidbot/providers";
import {
filterPromptEchoHistoryResults,
createTextEmbedder,
createVectorStores,
FileMessageArchiveRepository,
FileSourceDocumentArchiveRepository,
RetrievalService,
searchHistoryWithArchiveFallback,
SourceDocumentIngester,
type ArchivedMessageRecord,
} from "@voidbot/rag";
@ -35,50 +37,6 @@ import {
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,
@ -214,10 +172,15 @@ async function buildProviderRegistry(systemMessagesCatalog: SystemMessageCatalog
};
},
searchHistory: async (input) => {
const rawResults = await retrievalService.searchHistory(input.query, input.limit, {
const rawResults = await searchHistoryWithArchiveFallback({
retrievalService,
archiveRepository,
query: input.query,
limit: input.limit,
guildId: input.guildId,
channelId: input.channelId,
authorId: input.authorId,
preserveOverfetch: true,
});
const results = filterPromptEchoHistoryResults(rawResults, input.query);

View file

@ -1,6 +1,7 @@
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { DEFAULT_RETRIEVAL_RESULT_LIMIT } from "@voidbot/shared";
import { searchHistoryWithArchiveFallback } from "@voidbot/rag";
import { type VoidbotMcpContext } from "./mcp-server-context";
import {
@ -87,10 +88,12 @@ export function registerVoidbotResources(
},
async (uri, variables) => {
const query = getRequiredVariable(variables, "query", "history search");
const results = await context.retrievalService.searchHistory(
const results = await searchHistoryWithArchiveFallback({
retrievalService: context.retrievalService,
archiveRepository: context.archiveRepository,
query,
DEFAULT_RETRIEVAL_RESULT_LIMIT,
);
limit: DEFAULT_RETRIEVAL_RESULT_LIMIT,
});
return jsonResource(uri, {
query,

View file

@ -2,6 +2,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { type CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { DEFAULT_RETRIEVAL_RESULT_LIMIT } from "@voidbot/shared";
import { searchHistoryWithArchiveFallback } from "@voidbot/rag";
import { type VoidbotMcpContext } from "./mcp-server-context";
import { openOwnerDmChannel, postDiscordMessage } from "./mcp-server-discord";
@ -45,7 +46,11 @@ export function registerVoidbotTools(
},
async (input: SearchHistoryArgs): Promise<CallToolResult> => {
const { query, limit, guildId, channelId, authorId } = input;
const results = await context.retrievalService.searchHistory(query, limit ?? DEFAULT_RETRIEVAL_RESULT_LIMIT, {
const results = await searchHistoryWithArchiveFallback({
retrievalService: context.retrievalService,
archiveRepository: context.archiveRepository,
query,
limit: limit ?? DEFAULT_RETRIEVAL_RESULT_LIMIT,
guildId,
channelId,
authorId,