Add Persona semantic memory indexing
This commit is contained in:
parent
10a0c71aa2
commit
95764c9a63
14 changed files with 3978 additions and 811 deletions
|
|
@ -54,6 +54,7 @@ const envSchema = z.object({
|
|||
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_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"),
|
||||
|
|
@ -261,6 +262,7 @@ export interface AppConfig {
|
|||
timeoutMs: number;
|
||||
historyCollection: string;
|
||||
sourceCollection: string;
|
||||
personaCollection: string;
|
||||
};
|
||||
rateLimits: {
|
||||
defaultCooldownSeconds: number;
|
||||
|
|
@ -512,6 +514,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,
|
||||
personaCollection: parsed.QDRANT_PERSONA_COLLECTION,
|
||||
},
|
||||
rateLimits: {
|
||||
defaultCooldownSeconds: parsed.VOID_USAGE_COOLDOWN_SECONDS,
|
||||
|
|
|
|||
|
|
@ -31,6 +31,17 @@ const evidenceRefSchema = z.object({
|
|||
}).strict();
|
||||
const anchorRefSchema = evidenceRefSchema;
|
||||
|
||||
const semanticMemoryIndexSchema = z.object({
|
||||
corpusKind: z.literal("persona_memory"),
|
||||
vectorStore: z.enum(["qdrant", "local_json"]),
|
||||
collectionName: nonEmptyStringSchema,
|
||||
embedderId: nonEmptyStringSchema,
|
||||
contentHash: nonEmptyStringSchema,
|
||||
chunkIds: z.array(nonEmptyStringSchema).default([]),
|
||||
indexedAt: timestampSchema,
|
||||
stale: z.boolean().default(false),
|
||||
}).strict();
|
||||
|
||||
const activationVectorSchema = z.object({
|
||||
mean: z.number().min(0).max(1),
|
||||
plasticity: z.number().min(0).max(1),
|
||||
|
|
@ -148,6 +159,7 @@ const distilledMemorySchema = z.object({
|
|||
actionImplication: z.string().trim().min(1).max(2000).optional(),
|
||||
anchorRefs: z.array(anchorRefSchema).default([]),
|
||||
evidenceRefs: z.array(evidenceRefSchema).default([]),
|
||||
semanticIndex: semanticMemoryIndexSchema.optional(),
|
||||
createdAt: timestampSchema,
|
||||
updatedAt: timestampSchema,
|
||||
retiredAt: timestampSchema.optional(),
|
||||
|
|
|
|||
|
|
@ -51,6 +51,25 @@ export async function loadVoidSelfStateTypedDocuments(
|
|||
return readTypedStateOrEmpty(cache, options.identity);
|
||||
}
|
||||
|
||||
export async function writeVoidSelfStateTypedDocuments(
|
||||
options: VoidSelfStateServiceOptions,
|
||||
typedState: VoidSelfStateTypedProjection,
|
||||
): Promise<VoidSelfStateOperationResult> {
|
||||
const canonicalPath = resolve(options.canonicalPath);
|
||||
const cache = createVoidSelfStateCache(canonicalPath);
|
||||
await cache.pullAllBackingStores();
|
||||
if (options.identity) {
|
||||
repairSelfProfileIdentity(typedState, options.identity);
|
||||
}
|
||||
await writeTypedState(cache, typedState);
|
||||
|
||||
return {
|
||||
operation: "repair_typed_state",
|
||||
canonicalPath,
|
||||
typedDocumentsWritten: 8,
|
||||
};
|
||||
}
|
||||
|
||||
export async function applyVoidSelfStateOperation(
|
||||
options: VoidSelfStateServiceOptions,
|
||||
rawOperation: unknown,
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 },
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -108,12 +108,12 @@ 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;
|
||||
|
|
@ -592,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>;
|
||||
|
|
|
|||
Loading…
Reference in a new issue