Add character state projection surface

This commit is contained in:
Meta Vens 2026-05-21 12:06:52 +01:00
commit afe2479db3
11 changed files with 462 additions and 50 deletions

View file

@ -4,6 +4,7 @@ import {
type SourceMessage,
type VoidSelfStateContext,
} from "@voidbot/shared";
import { Buffer } from "node:buffer";
import {
type VoidCandidateInterventions,
@ -260,6 +261,67 @@ export function renderVoidSelfStateSummary(
].join("\n");
}
export function renderVoidSelfStateCharacterSurface(
state: VoidSelfStateTypedProjection,
options: Pick<VoidSelfStateProjectionOptions, "identity"> = {},
): string {
const profile = state.selfProfile;
const identityName = options.identity?.publicName ?? profile.publicName;
const topValues = state.selfProfile.values
.sort((left, right) => right.priority - left.priority)
.slice(0, 5)
.map((value) => value.summary
? `${projectCharacterText(value.label)}: ${projectCharacterText(value.summary)}`
: projectCharacterText(value.label));
const privateNotes = uniqueLines(profile.privateNotes).slice(0, 4).map(projectCharacterText);
const memories = state.thoughtMemory.memories
.filter((memory) => !memory.retiredAt)
.slice(-6)
.reverse()
.map((memory) => renderCharacterMemory(memory, identityName));
const shortTerm = state.thoughtMemory.shortTerm
.slice(-4)
.reverse()
.map((memory) => renderCharacterMemory(memory, identityName));
const incubation = state.thoughtMemory.incubation
.filter((thread) => thread.status !== "retired")
.sort((left, right) => right.maturation - left.maturation)
.slice(0, 4)
.map((thread) => renderCharacterIncubation(thread));
const pressures = state.agencyPressure.pressures
.filter((pressure) => ["active", "cooling", "ready_to_act"].includes(pressure.status))
.sort((left, right) => right.intensity - left.intensity)
.slice(0, 4)
.map((pressure) => renderCharacterAgencyPressure(pressure, identityName));
const affect = renderCharacterAffect(state.faceAffect, identityName);
const speakingPressure = renderCharacterSpeakingPressure(state.scheduledRuntime);
return [
topValues.length > 0
? ["What matters to you most:", ...topValues.map((value) => `- ${value}`)].join("\n")
: "What matters to you most: nothing has crystallized yet; let the room and your territory provoke a real preference.",
privateNotes.length > 0
? ["Private self-read:", ...privateNotes.map((note) => `- ${note}`)].join("\n")
: undefined,
speakingPressure,
memories.length > 0
? [`What ${identityName} keeps remembering:`, ...memories].join("\n")
: `What ${identityName} keeps remembering: not much has settled yet.`,
shortTerm.length > 0
? ["Recent residue still clinging:", ...shortTerm].join("\n")
: undefined,
incubation.length > 0
? ["Thoughts still moving under the surface:", ...incubation].join("\n")
: undefined,
pressures.length > 0
? ["Pressures looking for a future move:", ...pressures].join("\n")
: undefined,
affect,
]
.filter((line): line is string => typeof line === "string" && line.trim().length > 0)
.join("\n");
}
function renderAgencyPressure(
pressure: VoidAgencyPressure["pressures"][number],
identityName: string,
@ -279,6 +341,175 @@ function renderAgencyPressure(
return lines.join("\n");
}
function uniqueLines(lines: string[]): string[] {
const seen = new Set<string>();
const result: string[] = [];
for (const line of lines) {
const trimmed = line.trim();
const key = trimmed.toLowerCase();
if (!trimmed || seen.has(key)) {
continue;
}
seen.add(key);
result.push(trimmed);
}
return result;
}
function renderCharacterMemory(
memory: VoidThoughtMemory["memories"][number],
identityName: string,
): string {
const lines = [`- ${projectCharacterText(memory.summary)}`];
if (memory.claim) {
lines.push(` ${identityName}'s read: ${projectCharacterText(memory.claim)}`);
}
if (memory.question) {
lines.push(` Still unanswered: ${projectCharacterText(memory.question)}`);
}
if (memory.tension) {
lines.push(` What keeps it sharp: ${projectCharacterText(memory.tension)}`);
}
if (memory.actionImplication) {
lines.push(` What it should change: ${projectCharacterText(memory.actionImplication)}`);
}
return lines.join("\n");
}
function renderCharacterIncubation(thread: VoidThoughtMemory["incubation"][number]): string {
const lines = [`- ${projectCharacterText(thread.topic)}: ${projectCharacterText(thread.summary)}`];
if (thread.status === "ready_to_share" || thread.status === "crystallized") {
lines.push(" This is close to being worth saying or shaping into an artifact.");
} else if (thread.status === "cooling") {
lines.push(" Let it cool unless new evidence gives it a new edge.");
}
if (thread.saturationScore !== undefined && thread.saturationScore >= 0.75) {
lines.push(" It may be over-chewed; find a new angle before speaking.");
}
return lines.join("\n");
}
function renderCharacterAgencyPressure(
pressure: VoidAgencyPressure["pressures"][number],
identityName: string,
): string {
const lines = [`- ${projectCharacterText(pressure.summary)}`];
if (pressure.claim) {
lines.push(` ${identityName}'s claim: ${projectCharacterText(pressure.claim)}`);
}
if (pressure.question) {
lines.push(` Question still open: ${projectCharacterText(pressure.question)}`);
}
if (pressure.tension) {
lines.push(` What rubs: ${projectCharacterText(pressure.tension)}`);
}
lines.push(` What it wants next: ${projectCharacterText(pressure.actionImplication)}`);
return lines.join("\n");
}
function renderCharacterAffect(
affect: VoidFaceAffect,
identityName: string,
): string | undefined {
const needs = affect.needs
.filter((need) => ["active", "neglected"].includes(need.status))
.sort((left, right) => right.intensity - left.intensity)
.slice(0, 4);
const bonds = affect.socialBonds
.filter((bond) => bond.status === "active")
.sort((left, right) => right.intensity - left.intensity)
.slice(0, 4);
const reads = affect.statusReads
.filter((read) => !read.retiredAt)
.sort((left, right) => right.intensity - left.intensity)
.slice(0, 4);
const moods = affect.moodDimensions
.filter((dimension) => dimension.name.trim().length > 0 && dimension.value >= 0.2)
.sort((left, right) => right.value - left.value)
.slice(0, 5)
.map((dimension) => projectCharacterText(dimension.name));
if (needs.length === 0 && bonds.length === 0 && reads.length === 0 && moods.length === 0) {
return undefined;
}
return [
`What ${identityName} feels and wants:`,
...needs.map((need) => {
const target = need.target.label ?? need.target.id;
return [
`- ${need.status === "neglected" ? "Neglected" : "Active"} ${need.kind} need around ${target}: ${projectCharacterText(need.summary)}`,
` ${identityName}'s claim: ${projectCharacterText(need.claim ?? need.question ?? "(unspoken)")}`,
` What rubs: ${projectCharacterText(need.tension)}`,
` What it wants next: ${projectCharacterText(need.actionImplication)}`,
].join("\n");
}),
...bonds.map((bond) => {
const target = bond.target.label ?? bond.target.id;
return [
`- ${bond.stance} toward ${target}: ${projectCharacterText(bond.summary)}`,
` ${identityName}'s read: ${projectCharacterText(bond.claim)}`,
` How it should change behavior: ${projectCharacterText(bond.actionImplication)}`,
].join("\n");
}),
...reads.map((read) => {
const target = read.target.label ?? read.target.id;
return [
`- ${read.status} around ${target}: ${projectCharacterText(read.summary)}`,
` ${identityName}'s read: ${projectCharacterText(read.claim)}`,
` What it should do: ${projectCharacterText(read.actionImplication)}`,
].join("\n");
}),
moods.length > 0 ? `- Mood weather: ${moods.join(" | ")}` : undefined,
]
.filter((line): line is string => typeof line === "string")
.join("\n");
}
function projectCharacterText(value: string): string {
const decoded = value.includes("â")
? Buffer.from(value, "latin1").toString("utf8")
: value;
return decoded
.replace(/\u00e2\u20ac\u201d/g, "-")
.replace(/\u00e2\u20ac\u2122/g, "'")
.replace(/\u00e2\u20ac\u0153/g, "\"")
.replace(/\u00e2\u20ac\u009d/g, "\"")
.replace(/—/g, "-")
.replace(/’/g, "'")
.replace(/“/g, "\"")
.replace(/â€<C3A2>/g, "\"")
.replace(/\bheartbeat\b/gi, "earlier note")
.replace(/\bcharacter Face\b/g, "character")
.replace(/\bFaces\b/g, "characters")
.replace(/\bFace\b/g, "character")
.replace(/\binitial-face-affect-import\b/g, "old self-read")
.replace(/\s{2,}/g, " ")
.trim();
}
function renderCharacterSpeakingPressure(runtime: VoidScheduledRuntime): string {
const pressure = runtime.speakingPressure;
const parts: string[] = [];
if (pressure.needToSpeak >= 0.75) {
parts.push("There is a strong tug to speak.");
} else if (pressure.needToSpeak >= 0.4) {
parts.push("There is a moderate tug to speak.");
} else {
parts.push("There is no strong need to speak unless the room gives you a real opening.");
}
if (pressure.confessionPressure !== undefined && pressure.confessionPressure >= 0.6) {
parts.push("Something personal wants to leak through.");
}
if (pressure.noveltyPressure !== undefined && pressure.noveltyPressure >= 0.6) {
parts.push("Something feels new enough to mention.");
}
if (pressure.recentSpeechDamping !== undefined && pressure.recentSpeechDamping >= 0.5) {
parts.push("Recent speech should make you choosy.");
}
return `Current social pressure: ${parts.join(" ")}`;
}
function renderFaceAffectSummary(
affect: VoidFaceAffect,
identityName: string,