Harden local history fallback for repeated prompts
This commit is contained in:
parent
7c1d7f24d4
commit
37765ce246
5 changed files with 328 additions and 10 deletions
|
|
@ -21,6 +21,7 @@ import {
|
|||
} from "@voidbot/core";
|
||||
import type { ProviderRegistry } from "@voidbot/providers";
|
||||
import {
|
||||
FileMessageArchiveRepository,
|
||||
FileSourceDocumentArchiveRepository,
|
||||
RagPipeline,
|
||||
RetrievalService,
|
||||
|
|
@ -41,11 +42,13 @@ import {
|
|||
buildChannelIndexingTarget,
|
||||
canSendMessages,
|
||||
convertDiscordMessageToArchive,
|
||||
filterPromptEchoHistoryResults,
|
||||
getRecentMessages,
|
||||
inferSourceGroundingHint,
|
||||
renderRateLimitMessage,
|
||||
renderSystemMessage,
|
||||
renderInteractionProfileDisclosure,
|
||||
searchHistoryWithArchiveFallback,
|
||||
sendDirectMessage,
|
||||
sendChunkedChannelMessage,
|
||||
splitDiscordContent,
|
||||
|
|
@ -134,6 +137,7 @@ export interface PromptHandlerOptions {
|
|||
permissionEngine: PermissionEngine;
|
||||
contextBuilder: ContextBuilder;
|
||||
retrievalService: RetrievalService;
|
||||
archiveRepository: FileMessageArchiveRepository;
|
||||
sourceArchiveRepository: FileSourceDocumentArchiveRepository;
|
||||
jobQueue: JobQueue;
|
||||
auditLog: AuditLog;
|
||||
|
|
@ -262,14 +266,22 @@ export async function handlePrompt(options: PromptHandlerOptions): Promise<void>
|
|||
await options.sourceArchiveRepository.listRepoSummaries(),
|
||||
);
|
||||
const shouldAttachInitialRetrieval =
|
||||
!provider.getCapabilities().includes("tool_driven_retrieval") &&
|
||||
(shouldIndexChannel(options.config.channelIndexing, buildChannelIndexingTarget(options.channel)) ||
|
||||
providerName !== "owner_codex");
|
||||
providerName === "local_llm" ||
|
||||
(!provider.getCapabilities().includes("tool_driven_retrieval") &&
|
||||
(shouldIndexChannel(options.config.channelIndexing, buildChannelIndexingTarget(options.channel)) ||
|
||||
providerName !== "owner_codex"));
|
||||
const initialRetrievalLimit = providerName === "local_llm" ? 3 : 5;
|
||||
const retrieval = shouldAttachInitialRetrieval
|
||||
? await options.retrievalService.search(options.prompt, 5, {
|
||||
guildId: options.guildContext.guildId,
|
||||
corpusKind: "discord_history",
|
||||
})
|
||||
? filterPromptEchoHistoryResults(
|
||||
await searchHistoryWithArchiveFallback({
|
||||
retrievalService: options.retrievalService,
|
||||
archiveRepository: options.archiveRepository,
|
||||
query: options.prompt,
|
||||
limit: initialRetrievalLimit,
|
||||
guildId: options.guildContext.guildId,
|
||||
}),
|
||||
options.prompt,
|
||||
).slice(0, initialRetrievalLimit)
|
||||
: [];
|
||||
const contextBundle = options.contextBuilder.build({
|
||||
prompt: options.prompt,
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ import {
|
|||
import type { ProviderRegistry } from "@voidbot/providers";
|
||||
import {
|
||||
RagPipeline,
|
||||
FileMessageArchiveRepository,
|
||||
RetrievalService,
|
||||
type ArchivedMessageRecord,
|
||||
} from "@voidbot/rag";
|
||||
|
|
@ -27,6 +28,7 @@ import {
|
|||
type ChannelIndexingTarget,
|
||||
type GuildContext,
|
||||
type InteractionMemoryProfile,
|
||||
type RetrievalResult,
|
||||
type SourceGroundingHint,
|
||||
type SourceMessage,
|
||||
type SituationalSocialRead,
|
||||
|
|
@ -337,6 +339,51 @@ export function renderRateLimitMessage(
|
|||
});
|
||||
}
|
||||
|
||||
export async function searchHistoryWithArchiveFallback(options: {
|
||||
retrievalService: RetrievalService;
|
||||
archiveRepository: FileMessageArchiveRepository;
|
||||
query: string;
|
||||
limit: number;
|
||||
guildId?: string;
|
||||
channelId?: string;
|
||||
authorId?: string;
|
||||
}): Promise<RetrievalResult[]> {
|
||||
const overfetchLimit = Math.max(options.limit * 3, options.limit + 4);
|
||||
const vectorResults = await options.retrievalService.searchHistory(options.query, overfetchLimit, {
|
||||
guildId: options.guildId,
|
||||
channelId: options.channelId,
|
||||
authorId: options.authorId,
|
||||
});
|
||||
|
||||
const lexicalResults = await options.archiveRepository.searchLexical(options.query, overfetchLimit, {
|
||||
guildId: options.guildId,
|
||||
channelId: options.channelId,
|
||||
authorId: options.authorId,
|
||||
});
|
||||
|
||||
if (lexicalResults.length === 0) {
|
||||
return vectorResults;
|
||||
}
|
||||
|
||||
const merged = [...vectorResults];
|
||||
const seenSourceIds = new Set(merged.map((result) => result.sourceId));
|
||||
|
||||
for (const result of lexicalResults) {
|
||||
if (seenSourceIds.has(result.sourceId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
merged.push(result);
|
||||
seenSourceIds.add(result.sourceId);
|
||||
|
||||
if (merged.length >= overfetchLimit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
export function formatHistoryResults(
|
||||
results: Awaited<ReturnType<RetrievalService["searchHistory"]>>,
|
||||
): Array<Record<string, unknown>> {
|
||||
|
|
@ -373,6 +420,96 @@ export function formatSourceResults(
|
|||
}));
|
||||
}
|
||||
|
||||
export function filterPromptEchoHistoryResults<
|
||||
T extends {
|
||||
text: string;
|
||||
},
|
||||
>(results: T[], query: string): T[] {
|
||||
const normalizedQuery = normalizeHistoryEchoText(query);
|
||||
const queryTokens = buildHistoryEchoTokens(normalizedQuery);
|
||||
|
||||
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 (queryTokens.length >= 4) {
|
||||
const resultTokens = buildHistoryEchoTokens(normalizedResult);
|
||||
const sharedTokenCount = countSharedTokens(queryTokens, resultTokens);
|
||||
const similarity = sharedTokenCount / Math.max(Math.min(queryTokens.length, resultTokens.length), 1);
|
||||
|
||||
if (similarity >= 0.7) {
|
||||
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();
|
||||
}
|
||||
|
||||
function buildHistoryEchoTokens(value: string): string[] {
|
||||
return [...new Set(
|
||||
value
|
||||
.split(/[^\p{L}\p{N}]+/u)
|
||||
.map((token) => token.trim())
|
||||
.filter((token) => token.length >= 4)
|
||||
.filter((token) => !HISTORY_ECHO_STOP_WORDS.has(token)),
|
||||
)];
|
||||
}
|
||||
|
||||
function countSharedTokens(left: string[], right: string[]): number {
|
||||
const rightSet = new Set(right);
|
||||
return left.reduce((count, token) => count + (rightSet.has(token) ? 1 : 0), 0);
|
||||
}
|
||||
|
||||
const HISTORY_ECHO_STOP_WORDS = new Set([
|
||||
"about",
|
||||
"after",
|
||||
"before",
|
||||
"could",
|
||||
"from",
|
||||
"have",
|
||||
"please",
|
||||
"pretty",
|
||||
"tell",
|
||||
"that",
|
||||
"them",
|
||||
"they",
|
||||
"this",
|
||||
"what",
|
||||
"with",
|
||||
"would",
|
||||
"your",
|
||||
]);
|
||||
|
||||
export function formatArchivedMessageContext(
|
||||
message: ArchivedMessageRecord,
|
||||
anchorMessageId: string,
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ import {
|
|||
replyToMessage,
|
||||
rememberAmbientVoidReference,
|
||||
renderSystemMessage,
|
||||
searchHistoryWithArchiveFallback,
|
||||
stripBotMention,
|
||||
} from "./discord-bot-support";
|
||||
|
||||
|
|
@ -235,12 +236,16 @@ export async function startBot(): Promise<void> {
|
|||
};
|
||||
},
|
||||
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,
|
||||
});
|
||||
const results = filterPromptEchoHistoryResults(rawResults, input.query);
|
||||
const results = filterPromptEchoHistoryResults(rawResults, input.query).slice(0, input.limit);
|
||||
|
||||
return {
|
||||
query: input.query,
|
||||
|
|
@ -362,6 +367,7 @@ export async function startBot(): Promise<void> {
|
|||
permissionEngine,
|
||||
contextBuilder,
|
||||
retrievalService,
|
||||
archiveRepository,
|
||||
sourceArchiveRepository,
|
||||
jobQueue,
|
||||
auditLog,
|
||||
|
|
@ -445,6 +451,7 @@ export async function startBot(): Promise<void> {
|
|||
permissionEngine,
|
||||
contextBuilder,
|
||||
retrievalService,
|
||||
archiveRepository,
|
||||
sourceArchiveRepository,
|
||||
jobQueue,
|
||||
auditLog,
|
||||
|
|
@ -475,6 +482,7 @@ export async function startBot(): Promise<void> {
|
|||
permissionEngine,
|
||||
contextBuilder,
|
||||
retrievalService,
|
||||
archiveRepository,
|
||||
sourceArchiveRepository,
|
||||
jobQueue,
|
||||
auditLog,
|
||||
|
|
|
|||
Loading…
Reference in a new issue