export const OBSERVATION_CUSTOM_TYPE = "om.observation";
export const OBSERVATIONAL_MEMORY_SNAPSHOT_CUSTOM_TYPE = "om.snapshot";
export const OBSERVATIONAL_MEMORY_CONTEXT_CUSTOM_TYPE = "om.snapshot.context";
export const RELEVANCE_VALUES = ["low", "medium", "high", "critical"];
export const MEMORY_ID_PATTERN = /^[a-f0-9]{12}$/;
function isRelevance(v) {
    return typeof v === "string" && RELEVANCE_VALUES.includes(v);
}
function isObservationRecord(v) {
    if (!v || typeof v !== "object")
        return false;
    const o = v;
    if (typeof o.id !== "string" ||
        typeof o.content !== "string" ||
        typeof o.timestamp !== "string" ||
        !isRelevance(o.relevance)) {
        return false;
    }
    if (o.sourceEntryIds === undefined)
        return true;
    return isNonEmptyStringArray(o.sourceEntryIds);
}
function isNonEmptyStringArray(v) {
    return Array.isArray(v) && v.length > 0 && v.every((id) => typeof id === "string" && id.length > 0);
}
function isEmptyStringArray(v) {
    return Array.isArray(v) && v.length === 0;
}
export function isReflectionRecord(v) {
    if (!v || typeof v !== "object")
        return false;
    const o = v;
    if (typeof o.id !== "string" ||
        !MEMORY_ID_PATTERN.test(o.id) ||
        typeof o.content !== "string" ||
        o.content.trim().length === 0 ||
        /[\r\n]/.test(o.content)) {
        return false;
    }
    if (o.legacy !== undefined && typeof o.legacy !== "boolean")
        return false;
    if (o.legacy === true)
        return isEmptyStringArray(o.supportingObservationIds);
    return isNonEmptyStringArray(o.supportingObservationIds);
}
export function isMemoryReflection(v) {
    return typeof v === "string" || isReflectionRecord(v);
}
export function reflectionContent(reflection) {
    return typeof reflection === "string" ? reflection : reflection.content;
}
export function reflectionId(reflection) {
    return typeof reflection === "string" ? undefined : reflection.id;
}
export function reflectionToPromptLine(reflection) {
    return typeof reflection === "string" ? reflection : `[${reflection.id}] ${reflection.content}`;
}
export function isMemoryDetailsV3(d) {
    if (!d || typeof d !== "object")
        return false;
    const o = d;
    if (o.type !== "observational-memory" || o.version !== 3)
        return false;
    if (!Array.isArray(o.observations) || !Array.isArray(o.reflections))
        return false;
    if (!o.observations.every(isObservationRecord))
        return false;
    return o.reflections.every((r) => typeof r === "string");
}
export function isMemoryDetailsV4(d) {
    if (!d || typeof d !== "object")
        return false;
    const o = d;
    if (o.type !== "observational-memory" || o.version !== 4)
        return false;
    if (!Array.isArray(o.observations) || !Array.isArray(o.reflections))
        return false;
    if (!o.observations.every(isObservationRecord))
        return false;
    return o.reflections.every(isMemoryReflection);
}
export function isSupportedMemoryDetails(d) {
    return isMemoryDetailsV3(d) || isMemoryDetailsV4(d);
}
export function isMemoryDetails(d) {
    return isMemoryDetailsV3(d);
}
export function isObservationEntryData(d) {
    if (!d || typeof d !== "object")
        return false;
    const o = d;
    return (Array.isArray(o.records) &&
        o.records.every(isObservationRecord) &&
        typeof o.coversFromId === "string" &&
        typeof o.coversUpToId === "string" &&
        typeof o.tokenCount === "number");
}
export function isRestoredMemorySnapshot(d) {
    if (!d || typeof d !== "object")
        return false;
    const o = d;
    return (o.type === "observational-memory-snapshot" &&
        o.version === 1 &&
        typeof o.coveredSourceCount === "number" &&
        Number.isInteger(o.coveredSourceCount) &&
        o.coveredSourceCount >= 0 &&
        typeof o.summary === "string" &&
        isSupportedMemoryDetails(o.details));
}
