Add Persona semantic memory recall

This commit is contained in:
Meta Vens 2026-06-09 00:36:42 +01:00
commit c779f4915f
16 changed files with 396 additions and 12 deletions

View file

@ -48,12 +48,14 @@ const envSchema = z.object({
DATABASE_DSN: z.string().min(1).default("postgres://voidbot:voidbot@localhost:5432/voidbot"),
VECTOR_STORE_KIND: z.enum(["local_json", "qdrant"]).default("local_json"),
VECTOR_STORE_PATH: z.string().min(1).default(".voidbot/history-vector-store.json"),
PERSONA_MEMORY_VECTOR_STORE_PATH: z.string().min(1).default(".voidbot/persona-memory-vector-store.json"),
SOURCE_VECTOR_STORE_ROOT: z.string().min(1).default(".voidbot/source-vectors"),
QDRANT_URL: z.string().url().default("http://127.0.0.1:6333"),
QDRANT_API_KEY: optionalNonEmptyString,
QDRANT_TIMEOUT_MS: z.coerce.number().int().positive().default(30000),
QDRANT_HISTORY_COLLECTION: z.string().min(1).default("voidbot_discord_history_chunks"),
QDRANT_SOURCE_COLLECTION: z.string().min(1).default("voidbot_repository_source_chunks"),
QDRANT_PERSONA_MEMORY_COLLECTION: z.string().min(1).default("voidbot_persona_memory_chunks"),
RAG_ARCHIVE_PATH: z.string().min(1).default(".voidbot/rag/messages.json"),
RAG_SOURCE_ARCHIVE_PATH: z.string().min(1).default(".voidbot/rag/source-documents.json"),
RAG_IMPORT_STATE_PATH: z.string().min(1).default(".voidbot/rag/import-state.json"),
@ -253,6 +255,7 @@ export interface AppConfig {
vectorStore: {
kind: "local_json" | "qdrant";
path: string;
personaMemoryPath: string;
};
sourceVectorStoreRoot: string;
qdrant: {
@ -261,6 +264,7 @@ export interface AppConfig {
timeoutMs: number;
historyCollection: string;
sourceCollection: string;
personaMemoryCollection: string;
};
rateLimits: {
defaultCooldownSeconds: number;
@ -504,6 +508,7 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): AppConfig {
vectorStore: {
kind: parsed.VECTOR_STORE_KIND,
path: resolve(parsed.VECTOR_STORE_PATH),
personaMemoryPath: resolve(parsed.PERSONA_MEMORY_VECTOR_STORE_PATH),
},
sourceVectorStoreRoot: resolve(parsed.SOURCE_VECTOR_STORE_ROOT),
qdrant: {
@ -512,6 +517,7 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): AppConfig {
timeoutMs: parsed.QDRANT_TIMEOUT_MS,
historyCollection: parsed.QDRANT_HISTORY_COLLECTION,
sourceCollection: parsed.QDRANT_SOURCE_COLLECTION,
personaMemoryCollection: parsed.QDRANT_PERSONA_MEMORY_COLLECTION,
},
rateLimits: {
defaultCooldownSeconds: parsed.VOID_USAGE_COOLDOWN_SECONDS,

View file

@ -111,6 +111,10 @@ function matchesFilters(
return false;
}
if (filters.identityId && metadata.identityId !== filters.identityId) {
return false;
}
if (filters.corpusKind && metadata.corpusKind !== filters.corpusKind) {
return false;
}

View file

@ -27,7 +27,7 @@ export interface StoredQdrantChunk extends EmbeddingChunk {
export interface QdrantVectorStoreOptions {
url: string;
collectionName: string;
corpusKind: "discord_history" | "repository_source";
corpusKind: "discord_history" | "repository_source" | "persona_memory";
apiKey?: string;
timeoutMs?: number;
embedder?: TextEmbedder;
@ -296,6 +296,10 @@ function buildPayload(chunk: StoredQdrantChunk): QdrantPayload {
copyOptionalMetadataField(payload, chunk.metadata, "guildId");
copyOptionalMetadataField(payload, chunk.metadata, "channelId");
copyOptionalMetadataField(payload, chunk.metadata, "authorId");
copyOptionalMetadataField(payload, chunk.metadata, "identityId");
copyOptionalMetadataField(payload, chunk.metadata, "memoryKind");
copyOptionalMetadataField(payload, chunk.metadata, "targetKind");
copyOptionalMetadataField(payload, chunk.metadata, "targetId");
copyOptionalMetadataField(payload, chunk.metadata, "threadId");
copyOptionalMetadataField(payload, chunk.metadata, "repoName");
copyOptionalMetadataField(payload, chunk.metadata, "language");
@ -346,8 +350,14 @@ function buildPathPrefixes(path: string): string[] {
function inferCorpusKind(
sourceKind: StoredQdrantChunk["sourceKind"],
): "discord_history" | "repository_source" {
return sourceKind === "source_document" ? "repository_source" : "discord_history";
): "discord_history" | "repository_source" | "persona_memory" {
if (sourceKind === "source_document") {
return "repository_source";
}
if (sourceKind === "persona_memory") {
return "persona_memory";
}
return "discord_history";
}
function fromQdrantPoint(point: QdrantScoredPoint): RetrievalResult | undefined {
@ -407,8 +417,10 @@ function readString(value: unknown): string | undefined {
return typeof value === "string" && value.length > 0 ? value : undefined;
}
function readSourceKind(value: unknown): "discord_message" | "source_document" | undefined {
return value === "discord_message" || value === "source_document" ? value : undefined;
function readSourceKind(value: unknown): "discord_message" | "source_document" | "persona_memory" | undefined {
return value === "discord_message" || value === "source_document" || value === "persona_memory"
? value
: undefined;
}
function validateEmbeddingBatch(vectors: number[][], expectedCount: number): number {
@ -433,7 +445,7 @@ function validateCollectionCompatibility(
collection: Schemas["CollectionInfo"],
expected: {
collectionName: string;
corpusKind: "discord_history" | "repository_source";
corpusKind: "discord_history" | "repository_source" | "persona_memory";
embedderId: string;
vectorLength: number;
},
@ -484,7 +496,7 @@ function readCollectionVectorLength(
}
function buildPayloadIndexDefinitions(
corpusKind: "discord_history" | "repository_source",
corpusKind: "discord_history" | "repository_source" | "persona_memory",
): Array<{
field_name: string;
field_schema:
@ -509,6 +521,16 @@ function buildPayloadIndexDefinitions(
];
}
if (corpusKind === "persona_memory") {
return [
...shared,
{ field_name: "identityId", field_schema: "keyword" as const },
{ field_name: "memoryKind", field_schema: "keyword" as const },
{ field_name: "targetKind", field_schema: "keyword" as const },
{ field_name: "targetId", field_schema: "keyword" as const },
];
}
return [
...shared,
{ field_name: "repoName", field_schema: "keyword" as const },
@ -528,6 +550,7 @@ function toQdrantFilter(filters?: RetrievalFilters): QdrantFilter | undefined {
appendExactMatch(must, "guildId", filters.guildId);
appendExactMatch(must, "channelId", filters.channelId);
appendExactMatch(must, "authorId", filters.authorId);
appendExactMatch(must, "identityId", filters.identityId);
appendExactMatch(must, "repoName", filters.repoName);
appendExactMatch(must, "language", filters.language);
appendExactMatch(must, "sourceId", filters.sourceId);

View file

@ -8,6 +8,7 @@ export class RetrievalService {
public constructor(
private readonly historyVectorStore: VectorStore,
private readonly sourceVectorStore: VectorStore = historyVectorStore,
private readonly personaMemoryVectorStore: VectorStore = historyVectorStore,
) {}
public async search(
@ -39,4 +40,15 @@ export class RetrievalService {
corpusKind: "repository_source",
});
}
public async searchPersonaMemory(
query: string,
limit = 5,
filters?: Omit<RetrievalFilters, "corpusKind" | "guildId" | "channelId" | "authorId" | "repoName" | "pathPrefix" | "language">,
): Promise<RetrievalResult[]> {
return this.personaMemoryVectorStore.query(query, limit, {
...filters,
corpusKind: "persona_memory",
});
}
}

View file

@ -8,6 +8,7 @@ import { ShardedSourceVectorStore } from "./source-vector-store";
export interface VectorStoreFactoryOptions {
kind: "local_json" | "qdrant";
historyPath: string;
personaMemoryPath: string;
sourceRoot: string;
qdrant: {
url: string;
@ -15,14 +16,17 @@ export interface VectorStoreFactoryOptions {
timeoutMs: number;
historyCollection: string;
sourceCollection: string;
personaMemoryCollection: string;
};
historyEmbedder: TextEmbedder;
sourceEmbedder: TextEmbedder;
personaMemoryEmbedder: TextEmbedder;
}
export interface BuiltVectorStores {
history: VectorStore;
source: VectorStore;
personaMemory: VectorStore;
}
export function createVectorStores(options: VectorStoreFactoryOptions): BuiltVectorStores {
@ -44,11 +48,20 @@ export function createVectorStores(options: VectorStoreFactoryOptions): BuiltVec
corpusKind: "repository_source",
embedder: options.sourceEmbedder,
}),
personaMemory: new QdrantVectorStore({
url: options.qdrant.url,
apiKey: options.qdrant.apiKey,
timeoutMs: options.qdrant.timeoutMs,
collectionName: options.qdrant.personaMemoryCollection,
corpusKind: "persona_memory",
embedder: options.personaMemoryEmbedder,
}),
};
}
return {
history: new FileVectorStore(options.historyPath, options.historyEmbedder),
source: new ShardedSourceVectorStore(options.sourceRoot, options.sourceEmbedder),
personaMemory: new FileVectorStore(options.personaMemoryPath, options.personaMemoryEmbedder),
};
}

View file

@ -107,15 +107,16 @@ export interface RetrievalResult {
score: number;
text: string;
sourceId: string;
sourceKind: "discord_message" | "source_document";
sourceKind: "discord_message" | "source_document" | "persona_memory";
metadata: Record<string, string>;
}
export interface RetrievalFilters {
corpusKind?: "discord_history" | "repository_source";
corpusKind?: "discord_history" | "repository_source" | "persona_memory";
guildId?: string;
channelId?: string;
authorId?: string;
identityId?: string;
repoName?: string;
pathPrefix?: string;
language?: string;
@ -591,7 +592,7 @@ export interface ArchivedMessage {
export interface EmbeddingChunk {
id: string;
sourceId: string;
sourceKind: "discord_message" | "source_document";
sourceKind: "discord_message" | "source_document" | "persona_memory";
text: string;
normalizedText: string;
metadata: Record<string, string>;