Add Persona semantic memory indexing

This commit is contained in:
Meta Vens 2026-06-05 22:19:27 +01:00
commit 95764c9a63
14 changed files with 3978 additions and 811 deletions

View file

@ -16,7 +16,7 @@ interface StoredVectorChunk extends EmbeddingChunk {
interface LegacyStoredVectorChunk extends Omit<StoredVectorChunk, "sourceId" | "sourceKind"> {
sourceId?: string;
sourceMessageId?: string;
sourceKind?: "discord_message" | "source_document";
sourceKind?: "discord_message" | "source_document" | "persona_memory";
}
interface VectorIndexStore {
@ -354,6 +354,12 @@ function normalizeStoredChunk(chunk: LegacyStoredVectorChunk): StoredVectorChunk
};
}
function inferCorpusKind(chunk: LegacyStoredVectorChunk): "discord_history" | "repository_source" {
return chunk.sourceKind === "source_document" ? "repository_source" : "discord_history";
function inferCorpusKind(chunk: LegacyStoredVectorChunk): "discord_history" | "repository_source" | "persona_memory" {
if (chunk.sourceKind === "source_document") {
return "repository_source";
}
if (chunk.sourceKind === "persona_memory") {
return "persona_memory";
}
return "discord_history";
}

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;
@ -346,8 +346,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 +413,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 +441,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 +492,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 +517,19 @@ function buildPayloadIndexDefinitions(
];
}
if (corpusKind === "persona_memory") {
return [
...shared,
{ field_name: "personaId", field_schema: "keyword" as const },
{ field_name: "memoryId", 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 },
{ field_name: "repoName", field_schema: "keyword" as const },
{ field_name: "contentHash", field_schema: "keyword" as const },
];
}
return [
...shared,
{ field_name: "repoName", field_schema: "keyword" as const },

View file

@ -15,14 +15,17 @@ export interface VectorStoreFactoryOptions {
timeoutMs: number;
historyCollection: string;
sourceCollection: string;
personaCollection: string;
};
historyEmbedder: TextEmbedder;
sourceEmbedder: TextEmbedder;
personaEmbedder?: TextEmbedder;
}
export interface BuiltVectorStores {
history: VectorStore;
source: VectorStore;
persona: VectorStore;
}
export function createVectorStores(options: VectorStoreFactoryOptions): BuiltVectorStores {
@ -44,11 +47,20 @@ export function createVectorStores(options: VectorStoreFactoryOptions): BuiltVec
corpusKind: "repository_source",
embedder: options.sourceEmbedder,
}),
persona: new QdrantVectorStore({
url: options.qdrant.url,
apiKey: options.qdrant.apiKey,
timeoutMs: options.qdrant.timeoutMs,
collectionName: options.qdrant.personaCollection,
corpusKind: "persona_memory",
embedder: options.personaEmbedder ?? options.historyEmbedder,
}),
};
}
return {
history: new FileVectorStore(options.historyPath, options.historyEmbedder),
source: new ShardedSourceVectorStore(options.sourceRoot, options.sourceEmbedder),
persona: new FileVectorStore(options.historyPath.replace(/\.json$/i, "-persona-memory.json"), options.personaEmbedder ?? options.historyEmbedder),
};
}