From 75052f198aae2edf4b5583ecc9075de9a065c9b4 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 24 Apr 2026 14:24:17 +0100 Subject: [PATCH] Track ambient mentions and specific interaction memories --- README.md | 2 +- apps/bot/src/discord-bot.ts | 85 ++++++++- packages/core/src/interaction-memory.ts | 180 ++++++++++++------ packages/providers/src/local-llm-provider.ts | 8 +- .../providers/src/owner-codex-provider.ts | 8 +- packages/shared/src/index.ts | 13 +- 6 files changed, 220 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index d66e160..ca77704 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ It can answer from archived Discord history, indexed GameCult repos, and Aetheri - answer owner questions through a Discord-safe `codex exec` lane - use real MCP tools for archived Discord history, repo search, lore lookup, and owner notifications -- keep an explicit per-speaker interaction memory for direct conversations with Void +- keep an explicit per-speaker interaction memory for direct conversations and ambient mentions of Void - index live Discord traffic for selected channels - backfill old Discord exports into the archive - index source trees and lore repos with local Ollama embeddings diff --git a/apps/bot/src/discord-bot.ts b/apps/bot/src/discord-bot.ts index c5e30f0..cf5a46b 100644 --- a/apps/bot/src/discord-bot.ts +++ b/apps/bot/src/discord-bot.ts @@ -311,6 +311,7 @@ export async function startBot(): Promise { } await ingestIfIndexed(message, config.channelIndexing, ragPipeline); + await rememberAmbientVoidReference(message, client.user?.id, interactionMemory); if (!client.user || !message.mentions.has(client.user)) { return; @@ -372,6 +373,11 @@ export async function startBot(): Promise { } await ingestIfIndexed(materializedMessage, config.channelIndexing, ragPipeline); + await rememberAmbientVoidReference( + materializedMessage, + client.user?.id, + interactionMemory, + ); }); client.on(Events.MessageDelete, async (message) => { @@ -563,13 +569,14 @@ interface PromptHandlerOptions { } async function handlePrompt(options: PromptHandlerOptions): Promise { - const interactionMemory = await options.interactionMemory.recordInteraction({ - actorId: options.actor.id, - actorName: options.actor.displayName, - guildId: options.guildContext.guildId, - channelId: options.guildContext.channelId, - channelName: options.guildContext.channelName, - command: options.command, + const interactionMemory = await options.interactionMemory.recordInteraction({ + actorId: options.actor.id, + actorName: options.actor.displayName, + sourceKind: "direct_prompt", + guildId: options.guildContext.guildId, + channelId: options.guildContext.channelId, + channelName: options.guildContext.channelName, + command: options.command, prompt: options.prompt, eventId: options.requestMessageId, }); @@ -974,6 +981,28 @@ async function ingestIfIndexed( await ragPipeline.upsertMessages([convertDiscordMessageToArchive(message)]); } +async function rememberAmbientVoidReference( + message: Message, + botUserId: string | undefined, + interactionMemory: FileInteractionMemoryBank, +): Promise { + if (!isAmbientVoidReference(message, botUserId)) { + return; + } + + await interactionMemory.recordInteraction({ + actorId: message.author.id, + actorName: message.author.displayName ?? message.author.username, + sourceKind: "ambient_mention", + guildId: message.guildId ?? undefined, + channelId: message.channelId, + channelName: "name" in message.channel ? message.channel.name ?? undefined : undefined, + prompt: message.content, + eventId: message.id, + timestamp: (message.editedAt ?? message.createdAt).toISOString(), + }); +} + function buildChannelIndexingTarget( channel: TextBasedChannel | null | undefined, ): ChannelIndexingTarget { @@ -1142,6 +1171,48 @@ function stripBotMention(content: string): string { return content.replace(/<@!?(\d+)>/g, "").trim(); } +function isAmbientVoidReference( + message: Message, + botUserId: string | undefined, +): boolean { + const content = message.content.trim(); + + if (content.length === 0) { + return false; + } + + if (botUserId && message.mentions.users.has(botUserId)) { + return false; + } + + if (/\bvoidbot\b/i.test(content)) { + return true; + } + + if (!/\bvoid\b/i.test(content)) { + return false; + } + + const normalized = ` ${content + .toLowerCase() + .replace(/[^a-z0-9]+/g, " ") + .replace(/\s+/g, " ") + .trim()} `; + + if ( + normalized.includes(" the void ") || + normalized.includes(" into the void ") || + normalized.includes(" return void ") || + normalized.includes(" void function ") || + normalized.includes(" void pointer ") || + normalized.includes(" non void ") + ) { + return false; + } + + return true; +} + function formatProviderStatuses( statuses: ReturnType, systemMessages: SystemMessageCatalog, diff --git a/packages/core/src/interaction-memory.ts b/packages/core/src/interaction-memory.ts index c00b1af..aa363a1 100644 --- a/packages/core/src/interaction-memory.ts +++ b/packages/core/src/interaction-memory.ts @@ -8,6 +8,7 @@ import { type InteractionMemoryEvent, type InteractionMemoryProfile, type InteractionMemorySentiment, + type InteractionMemorySourceKind, } from "@voidbot/shared"; interface InteractionMemoryStore { @@ -18,10 +19,11 @@ interface InteractionMemoryStore { export interface RecordInteractionInput { actorId: string; actorName: string; + sourceKind: InteractionMemorySourceKind; guildId?: string; channelId: string; channelName?: string; - command: CommandName; + command?: CommandName; prompt: string; timestamp?: string; eventId?: string; @@ -67,54 +69,34 @@ export class FileInteractionMemoryBank { const existingEvent = profile.recentEvents.find( (event) => event.id === eventId, ); + const nextEvent: InteractionMemoryEvent = { + id: eventId, + actorId: input.actorId, + actorName: input.actorName, + sourceKind: input.sourceKind, + guildId: input.guildId, + channelId: input.channelId, + channelName: input.channelName, + command: input.command, + prompt, + excerpt: buildExcerpt(prompt), + summary: buildEventSummary(input.sourceKind, analysis.sentiment, new Set(analysis.tags)), + sentiment: analysis.sentiment, + score: analysis.score, + tags: analysis.tags, + timestamp, + }; if (!existingEvent) { - const event: InteractionMemoryEvent = { - id: eventId, - actorId: input.actorId, - actorName: input.actorName, - guildId: input.guildId, - channelId: input.channelId, - channelName: input.channelName, - command: input.command, - prompt, - excerpt: buildExcerpt(prompt), - summary: analysis.summary, - sentiment: analysis.sentiment, - score: analysis.score, - tags: analysis.tags, - timestamp, - }; - - profile.totalInteractions += 1; - - if (analysis.score > 0) { - profile.positiveCount += 1; - } else if (analysis.score < 0) { - profile.negativeCount += 1; - } else { - profile.neutralCount += 1; - } - - profile.affinityScore = clamp( - profile.affinityScore + analysis.score, - -MAX_AFFINITY_SCORE, - MAX_AFFINITY_SCORE, - ); - profile.lastInteractionAt = timestamp; - profile.recentEvents.push(event); - profile.recentEvents = profile.recentEvents - .sort((left, right) => left.timestamp.localeCompare(right.timestamp)) - .slice(-MAX_RECENT_EVENTS); - } else if ( - existingEvent.actorName !== input.actorName || - existingEvent.prompt !== prompt - ) { - existingEvent.actorName = input.actorName; - existingEvent.prompt = prompt; - existingEvent.excerpt = buildExcerpt(prompt); + profile.recentEvents.push(nextEvent); + } else { + Object.assign(existingEvent, nextEvent); } + profile.recentEvents = profile.recentEvents + .sort((left, right) => left.timestamp.localeCompare(right.timestamp)) + .slice(-MAX_RECENT_EVENTS); + recalculateProfile(profile); profile.disposition = determineDisposition(profile); profile.summary = buildProfileSummary(profile); @@ -135,7 +117,9 @@ export class FileInteractionMemoryBank { private async readUnlocked(): Promise { try { const raw = await readFile(this.filePath, "utf8"); - return JSON.parse(stripLeadingBom(raw)) as InteractionMemoryStore; + return normalizeStore( + JSON.parse(stripLeadingBom(raw)) as InteractionMemoryStore, + ); } catch (error) { const code = (error as NodeJS.ErrnoException).code; @@ -170,6 +154,8 @@ function ensureProfile( disposition: "neutral", affinityScore: 0, totalInteractions: 0, + directInteractionCount: 0, + ambientMentionCount: 0, positiveCount: 0, negativeCount: 0, neutralCount: 0, @@ -182,6 +168,45 @@ function ensureProfile( return profile; } +function normalizeStore(store: InteractionMemoryStore): InteractionMemoryStore { + return { + version: 1, + profiles: (store.profiles ?? []).map((profile) => normalizeProfile(profile)), + }; +} + +function normalizeProfile( + profile: InteractionMemoryProfile, +): InteractionMemoryProfile { + const normalizedEvents = (profile.recentEvents ?? []).map((event) => ({ + ...event, + sourceKind: event.sourceKind ?? "direct_prompt", + })); + + const normalizedProfile: InteractionMemoryProfile = { + actorId: profile.actorId, + actorName: profile.actorName, + disposition: profile.disposition ?? "neutral", + affinityScore: profile.affinityScore ?? 0, + totalInteractions: profile.totalInteractions ?? 0, + directInteractionCount: profile.directInteractionCount ?? 0, + ambientMentionCount: profile.ambientMentionCount ?? 0, + positiveCount: profile.positiveCount ?? 0, + negativeCount: profile.negativeCount ?? 0, + neutralCount: profile.neutralCount ?? 0, + summary: + profile.summary ?? + "No explicit interaction memory has been recorded for this speaker yet.", + lastInteractionAt: profile.lastInteractionAt, + recentEvents: normalizedEvents, + }; + + recalculateProfile(normalizedProfile); + normalizedProfile.disposition = determineDisposition(normalizedProfile); + normalizedProfile.summary = buildProfileSummary(normalizedProfile); + return normalizedProfile; +} + function analyzeInteractionTone(prompt: string): ToneAnalysis { const normalized = ` ${prompt .toLowerCase() @@ -258,7 +283,7 @@ function analyzeInteractionTone(prompt: string): ToneAnalysis { sentiment, score: clamp(score, -4, 4), tags: [...tags], - summary: buildEventSummary(sentiment, tags), + summary: buildEventSummary("direct_prompt", sentiment, tags), }; } @@ -280,48 +305,78 @@ function matchWeightedPhrases( } function buildEventSummary( + sourceKind: InteractionMemorySourceKind, sentiment: InteractionMemorySentiment, tags: Set, ): string { + const ambient = sourceKind === "ambient_mention"; + if (tags.has("hostility")) { - return "Was openly hostile to you."; + return ambient ? "Spoke about you with open hostility." : "Was openly hostile to you."; } if (tags.has("insult")) { - return "Insulted or diminished you."; + return ambient ? "Spoke about you dismissively or with contempt." : "Insulted or diminished you."; } if (tags.has("gratitude") && tags.has("praise")) { - return "Thanked you and praised you."; + return ambient ? "Spoke very well of you." : "Thanked you and praised you."; } if (tags.has("gratitude")) { - return "Thanked you."; + return ambient ? "Spoke appreciatively about you." : "Thanked you."; } if (tags.has("praise")) { - return "Praised you."; + return ambient ? "Praised you while talking about you." : "Praised you."; } if (tags.has("apology")) { - return "Apologized to you."; + return ambient ? "Sounded apologetic while talking about you." : "Apologized to you."; } if (tags.has("courtesy")) { - return "Was notably polite."; + return ambient ? "Spoke politely about you." : "Was notably polite."; } switch (sentiment) { case "warm": - return "Was openly warm toward you."; + return ambient ? "Spoke warmly about you." : "Was openly warm toward you."; case "positive": - return "Was positive toward you."; + return ambient ? "Spoke positively about you." : "Was positive toward you."; case "negative": - return "Was curt or dismissive toward you."; + return ambient ? "Spoke curtly or dismissively about you." : "Was curt or dismissive toward you."; case "hostile": - return "Was hostile toward you."; + return ambient ? "Spoke hostilely about you." : "Was hostile toward you."; default: - return "Had a neutral interaction with you."; + return ambient ? "Mentioned you without revealing strong feelings." : "Had a neutral interaction with you."; + } +} + +function recalculateProfile(profile: InteractionMemoryProfile): void { + profile.totalInteractions = profile.recentEvents.length; + profile.directInteractionCount = profile.recentEvents.filter( + (event) => event.sourceKind === "direct_prompt", + ).length; + profile.ambientMentionCount = profile.recentEvents.filter( + (event) => event.sourceKind === "ambient_mention", + ).length; + profile.positiveCount = profile.recentEvents.filter((event) => event.score > 0).length; + profile.negativeCount = profile.recentEvents.filter((event) => event.score < 0).length; + profile.neutralCount = profile.recentEvents.filter((event) => event.score === 0).length; + profile.affinityScore = clamp( + profile.recentEvents.reduce((sum, event) => sum + event.score, 0), + -MAX_AFFINITY_SCORE, + MAX_AFFINITY_SCORE, + ); + profile.lastInteractionAt = + profile.recentEvents[profile.recentEvents.length - 1]?.timestamp; + for (const event of profile.recentEvents) { + event.summary = buildEventSummary( + event.sourceKind, + event.sentiment, + new Set(event.tags), + ); } } @@ -361,13 +416,16 @@ function determineDisposition( function buildProfileSummary(profile: InteractionMemoryProfile): string { const leading = describeDisposition(profile.disposition); - const counts = `${profile.totalInteractions} direct interaction${profile.totalInteractions === 1 ? "" : "s"} logged: ${profile.positiveCount} positive, ${profile.neutralCount} neutral, ${profile.negativeCount} negative.`; + const counts = `${profile.totalInteractions} remembered interaction${profile.totalInteractions === 1 ? "" : "s"}: ${profile.directInteractionCount} direct, ${profile.ambientMentionCount} ambient, ${profile.positiveCount} positive, ${profile.neutralCount} neutral, ${profile.negativeCount} negative.`; const recentNotable = profile.recentEvents .slice() .reverse() .filter((event) => event.score !== 0) .slice(0, SUMMARY_EVENT_LIMIT) - .map((event) => `${formatShortDate(event.timestamp)}: ${event.summary}`) + .map( + (event) => + `${formatShortDate(event.timestamp)}: ${event.summary} ("${event.excerpt}")`, + ) .join(" "); return recentNotable.length > 0 diff --git a/packages/providers/src/local-llm-provider.ts b/packages/providers/src/local-llm-provider.ts index 45bd001..e55743f 100644 --- a/packages/providers/src/local-llm-provider.ts +++ b/packages/providers/src/local-llm-provider.ts @@ -397,10 +397,10 @@ function renderInteractionMemory(context: ContextBundle): string { ? context.interactionMemory.recentEvents .slice() .reverse() - .slice(0, 4) + .slice(0, 6) .map( (event) => - `- [${event.timestamp}] ${event.sentiment} score=${event.score}: ${event.summary} Excerpt: ${event.excerpt}`, + `- [${event.timestamp}] ${event.sourceKind === "ambient_mention" ? "ambient" : "direct"} ${event.sentiment} score=${event.score}: ${event.summary} Quote: "${event.excerpt}"`, ) .join("\n") : "- No recent interaction events were retained."; @@ -409,7 +409,9 @@ function renderInteractionMemory(context: ContextBundle): string { `- Summary: ${context.interactionMemory.summary}`, `- Disposition: ${context.interactionMemory.disposition}`, `- Affinity score: ${context.interactionMemory.affinityScore}`, - "- Recent events:", + `- Direct remembered interactions: ${context.interactionMemory.directInteractionCount}`, + `- Ambient remembered mentions: ${context.interactionMemory.ambientMentionCount}`, + "- Specific remembered incidents:", recentEvents, ].join("\n"); } diff --git a/packages/providers/src/owner-codex-provider.ts b/packages/providers/src/owner-codex-provider.ts index 5223975..8308b0d 100644 --- a/packages/providers/src/owner-codex-provider.ts +++ b/packages/providers/src/owner-codex-provider.ts @@ -1114,10 +1114,10 @@ function renderInteractionMemory(context: ContextBundle): string { ? context.interactionMemory.recentEvents .slice() .reverse() - .slice(0, 4) + .slice(0, 6) .map( (event) => - `- [${event.timestamp}] ${event.sentiment} score=${event.score}: ${event.summary} Excerpt: ${event.excerpt}`, + `- [${event.timestamp}] ${event.sourceKind === "ambient_mention" ? "ambient" : "direct"} ${event.sentiment} score=${event.score}: ${event.summary} Quote: "${event.excerpt}"`, ) .join("\n") : "- No recent interaction events were retained."; @@ -1126,7 +1126,9 @@ function renderInteractionMemory(context: ContextBundle): string { `- Summary: ${context.interactionMemory.summary}`, `- Disposition: ${context.interactionMemory.disposition}`, `- Affinity score: ${context.interactionMemory.affinityScore}`, - "- Recent events:", + `- Direct remembered interactions: ${context.interactionMemory.directInteractionCount}`, + `- Ambient remembered mentions: ${context.interactionMemory.ambientMentionCount}`, + "- Specific remembered incidents:", recentEvents, ].join("\n"); } diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 98bcd5f..5383c19 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -112,6 +112,14 @@ export const interactionMemorySentiments = [ export type InteractionMemorySentiment = (typeof interactionMemorySentiments)[number]; +export const interactionMemorySourceKinds = [ + "direct_prompt", + "ambient_mention", +] as const; + +export type InteractionMemorySourceKind = + (typeof interactionMemorySourceKinds)[number]; + export const interactionMemoryDispositions = [ "warm", "friendly", @@ -128,10 +136,11 @@ export interface InteractionMemoryEvent { id: string; actorId: string; actorName: string; + sourceKind: InteractionMemorySourceKind; guildId?: string; channelId: string; channelName?: string; - command: CommandName; + command?: CommandName; prompt: string; excerpt: string; summary: string; @@ -147,6 +156,8 @@ export interface InteractionMemoryProfile { disposition: InteractionMemoryDisposition; affinityScore: number; totalInteractions: number; + directInteractionCount: number; + ambientMentionCount: number; positiveCount: number; negativeCount: number; neutralCount: number;