Isolate portable Persona state compatibility

This commit is contained in:
Meta Vens 2026-07-16 03:00:58 +01:00
commit 3f87346dee
8 changed files with 80 additions and 191 deletions

View file

@ -0,0 +1,38 @@
import type { RepoDiscordIdentity } from "@voidbot/core";
import type { PortablePersonaStateObservation } from "./persona-portable-state-source.js";
export function projectPortablePersonaState(identity: RepoDiscordIdentity, observation: PortablePersonaStateObservation): string {
if (observation.status !== "ok") return [`${identity.displayName} is a native VoidBot Persona, not a repo Face.`, `Portable Persona state is ${observation.status}: ${observation.reason}`, "Treat this as a Body fault and keep the public turn modest."].join("\n");
const state = observation.state;
const profile = child(state, "profile") ?? child(state, "selfProfile") ?? state;
const memory = child(state, "memory") ?? child(state, "thoughtMemory");
const affect = child(state, "affect") ?? child(state, "faceAffect");
const doctrine = child(state, "doctrine") ?? child(state, "doctrineStances");
return [
`${identity.displayName} is a native VoidBot Persona, not a repo Face.`,
"Portable Persona state is an imported projection, not canonical typed runtime authority.",
identity.avatarUrl ? `Public avatar URL: ${identity.avatarUrl}` : undefined,
identity.avatarPath ? `Local avatar asset: ${identity.avatarPath}` : undefined,
text(profile, "publicDescription") ?? identity.description,
list("Private notes", array(profile, "privateNotes")),
values("Values", array(profile, "values")),
list("Activation traits", array(profile, "activationTraits")),
memories("Memories", [...array(memory, "memories"), ...array(memory, "durableMemories")]),
memories("Short-term residue", array(memory, "shortTerm")),
memories("Agency pressure", [...array(state, "pressures"), ...array(state, "agencyPressures"), ...array(child(state, "agencyPressure"), "pressures")]),
memories("Affect needs", array(affect, "needs")),
memories("Social bonds", array(affect, "socialBonds")),
memories("Doctrine stances", [...array(state, "doctrineStances"), ...array(doctrine, "stances"), ...array(doctrine, "doctrineStances")]),
].filter((line): line is string => Boolean(line?.trim())).join("\n\n");
}
export function projectNativePersonaBody(identity: RepoDiscordIdentity): string { return [`${identity.displayName} is a native VoidBot Persona.`, "Body for this turn: Persona state, avatar, allowed Discord channels, current conversation, and VoidBot's webhook mouth.", "No repo jurisdiction, Bifrost governance digest, source-repo activity, or repo proposal authority is implied by this native Persona category."].join("\n"); }
function child(value: unknown, key: string): Record<string, unknown> | undefined { return record(value) && record(value[key]) ? value[key] as Record<string, unknown> : undefined; }
function text(value: unknown, key: string): string | undefined { const field = record(value) ? value[key] : undefined; return typeof field === "string" && field.trim() ? field.trim() : undefined; }
function array(value: unknown, key: string): unknown[] { const field = record(value) ? value[key] : undefined; return Array.isArray(field) ? field : []; }
function list(title: string, entries: unknown[]): string | undefined { const rendered = entries.map((entry) => typeof entry === "string" ? entry : summarize(entry)).filter((entry): entry is string => Boolean(entry?.trim())).slice(0, 12); return rendered.length > 0 ? [`${title}:`, ...rendered.map((entry) => `- ${entry}`)].join("\n") : undefined; }
function values(title: string, entries: unknown[]): string | undefined { return list(title, entries.map((entry) => record(entry) ? [text(entry, "label") ?? text(entry, "id") ?? text(entry, "name"), text(entry, "summary") ?? text(entry, "description")].filter(Boolean).join(": ") : entry)); }
function memories(title: string, entries: unknown[]): string | undefined { return list(title, entries.map((entry) => record(entry) ? text(entry, "summary") ?? text(entry, "claim") ?? text(entry, "description") ?? text(entry, "text") ?? summarize(entry) : entry)); }
function summarize(value: unknown): string | undefined { if (!record(value)) return undefined; for (const key of ["summary", "claim", "description", "text", "label", "id", "name"]) { const field = text(value, key); if (field) return field; } return undefined; }
function record(value: unknown): value is Record<string, unknown> { return typeof value === "object" && value !== null && !Array.isArray(value); }

View file

@ -0,0 +1,23 @@
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
export type PortablePersonaStateObservation =
| { status: "ok"; statePath: string; state: Record<string, unknown>; schemaVersion: "gamecult.persona_state.v0" }
| { status: "missing" | "malformed" | "unsupported"; statePath: string; reason: string };
export async function readPortablePersonaState(path: string): Promise<PortablePersonaStateObservation> {
const statePath = resolve(path);
try {
const raw = await readFile(statePath, "utf8");
const parsed = JSON.parse(stripBom(raw)) as unknown;
if (!record(parsed)) return { status: "malformed", statePath, reason: "Portable Persona state root is not an object." };
if (parsed.schemaVersion !== "gamecult.persona_state.v0") return { status: "unsupported", statePath, reason: `Unsupported portable Persona schema ${String(parsed.schemaVersion ?? "missing")}.` };
return { status: "ok", statePath, state: parsed, schemaVersion: "gamecult.persona_state.v0" };
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return { status: "missing", statePath, reason: "Portable Persona state file does not exist." };
return { status: "malformed", statePath, reason: error instanceof Error ? error.message : String(error) };
}
}
function stripBom(value: string): string { return value.charCodeAt(0) === 0xfeff ? value.slice(1) : value; }
function record(value: unknown): value is Record<string, unknown> { return typeof value === "object" && value !== null && !Array.isArray(value); }