// Core types for the memory layer.
import { z } from "zod";
/**
 * `formatVersion` distinguishes embeddings built from different source
 * texts. Bumped to 2 when we started feeding the embedder a
 * contextualized "{name} ({type}): {content}" string per observation
 * instead of the bare observation content.
 *
 * Load-side behaviour (`MemoryManager.getEntry`): if the on-disk
 * index's `formatVersion` is missing (treated as v1) or older than
 * this constant, we discard the index in memory, log a warning so the
 * discard is visible, and start with an empty `EmbeddingManager`. The
 * graph and summary are NOT touched — only embeddings get rebuilt.
 * The next mutation (any `applyExtractionFromLLM` call) embeds the
 * affected observations in the current format and persists. Until
 * then, Tier 2 (embedding similarity) silently produces zero hits
 * and recall falls back to Tier 1 + Tier 3.
 *
 * If the source-text shape changes again, bump this constant; the
 * load guard handles the rest.
 */
export const EMBEDDING_FORMAT_VERSION = 2;
// ---- Zod schemas for persisted data ----
//
// These guard the file-backed `MemoryStore`. They validate on save so a
// shape regression in code surfaces immediately, and on load so a
// hand-edited or corrupted file fails loudly with the offending path
// rather than silently propagating bad data through the recall pipeline.
const ObservationSchema = z.object({
    id: z.string(),
    content: z.string(),
    validFrom: z.string(),
    validTo: z.string().nullable(),
});
const EntitySchema = z.object({
    id: z.string(),
    name: z.string(),
    type: z.string(),
    source: z.string(),
    createdAt: z.string(),
    observations: z.array(ObservationSchema),
});
const RelationSchema = z.object({
    id: z.string(),
    from: z.string(),
    to: z.string(),
    type: z.string(),
    source: z.string(),
    validFrom: z.string(),
    validTo: z.string().nullable(),
});
export const MemoryGraphDataSchema = z.object({
    entities: z.array(EntitySchema),
    relations: z.array(RelationSchema),
    nextId: z.number(),
});
export const EmbeddingIndexSchema = z.object({
    formatVersion: z.number().optional(),
    model: z.string(),
    entries: z.array(z.object({
        id: z.string(),
        vector: z.array(z.number()),
    })),
});
export const ConversationSummarySchema = z.object({
    summary: z.string(),
    lastCompactedAt: z.string(),
    messagesSummarized: z.number(),
});
