2026-05-05 13:42:41 +01:00
|
|
|
import {
|
|
|
|
|
type GuildContext,
|
|
|
|
|
type SourceMessage,
|
|
|
|
|
type VoidSelfStateContext,
|
|
|
|
|
} from "@voidbot/shared";
|
2026-05-04 20:45:26 +01:00
|
|
|
|
2026-05-16 12:01:43 +01:00
|
|
|
import {
|
2026-05-16 18:43:53 +01:00
|
|
|
type VoidScheduledRuntime,
|
|
|
|
|
type VoidThoughtMemory,
|
|
|
|
|
} from "./void-self-state-domain";
|
|
|
|
|
import { type VoidSelfStateTypedProjection } from "./void-self-state-projection";
|
|
|
|
|
import { loadVoidSelfStateTypedDocuments } from "./void-self-state-service";
|
2026-05-04 20:45:26 +01:00
|
|
|
|
2026-05-05 13:42:41 +01:00
|
|
|
export interface LoadVoidSelfStateOptions {
|
|
|
|
|
recentMessages?: SourceMessage[];
|
|
|
|
|
guildContext?: GuildContext;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-04 20:45:26 +01:00
|
|
|
export async function loadVoidSelfState(
|
|
|
|
|
statePath: string,
|
2026-05-05 13:42:41 +01:00
|
|
|
options: LoadVoidSelfStateOptions = {},
|
2026-05-04 20:45:26 +01:00
|
|
|
): Promise<VoidSelfStateContext | undefined> {
|
2026-05-16 18:43:53 +01:00
|
|
|
let typedProjection: VoidSelfStateTypedProjection;
|
2026-05-04 20:45:26 +01:00
|
|
|
try {
|
2026-05-16 18:43:53 +01:00
|
|
|
typedProjection = await loadVoidSelfStateTypedDocuments({ canonicalPath: statePath });
|
2026-05-04 20:45:26 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
const code = (error as NodeJS.ErrnoException).code;
|
|
|
|
|
|
|
|
|
|
if (code === "ENOENT") {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-16 12:01:43 +01:00
|
|
|
const projection = buildVoidSelfStateProjection(typedProjection);
|
2026-05-04 20:45:26 +01:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
sourcePath: statePath,
|
|
|
|
|
loadedAt: new Date().toISOString(),
|
2026-05-16 18:43:53 +01:00
|
|
|
summary: renderTypedVoidSelfStateSummary(typedProjection, options),
|
2026-05-05 03:10:37 +01:00
|
|
|
projection,
|
2026-05-04 20:45:26 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-16 18:43:53 +01:00
|
|
|
function renderTypedVoidSelfStateSummary(
|
|
|
|
|
state: VoidSelfStateTypedProjection,
|
2026-05-05 13:42:41 +01:00
|
|
|
options: LoadVoidSelfStateOptions,
|
|
|
|
|
): string {
|
2026-05-16 18:43:53 +01:00
|
|
|
const profile = state.selfProfile;
|
|
|
|
|
const topValues = state.selfProfile.values
|
|
|
|
|
.sort((left, right) => right.priority - left.priority)
|
2026-05-04 20:45:26 +01:00
|
|
|
.slice(0, 4)
|
2026-05-16 18:43:53 +01:00
|
|
|
.map((value) => value.label);
|
|
|
|
|
const memories = state.thoughtMemory.memories
|
2026-05-04 20:45:26 +01:00
|
|
|
.slice(-6)
|
2026-05-16 18:43:53 +01:00
|
|
|
.reverse()
|
|
|
|
|
.map((memory) => renderTypedMemory(memory));
|
|
|
|
|
const incubation = state.thoughtMemory.incubation
|
|
|
|
|
.filter((thread) => thread.status !== "retired")
|
|
|
|
|
.sort((left, right) => right.maturation - left.maturation)
|
|
|
|
|
.slice(0, 4)
|
|
|
|
|
.map((thread) => renderTypedIncubationThread(thread));
|
|
|
|
|
const openCases = state.moderationCursor.openCases
|
|
|
|
|
.filter((entry) => !["answered", "resolved", "closed", "retired", "dropped"].includes(entry.status))
|
|
|
|
|
.slice(-4)
|
|
|
|
|
.reverse()
|
|
|
|
|
.map((entry) => `- ${entry.summary}${entry.authorName ? ` [from ${entry.authorName}]` : ""}`);
|
|
|
|
|
const interventions = state.candidateInterventions.interventions
|
|
|
|
|
.filter((entry) => entry.status === "queued" || entry.status === "deferred")
|
2026-05-04 20:45:26 +01:00
|
|
|
.slice(-3)
|
2026-05-16 18:43:53 +01:00
|
|
|
.reverse()
|
|
|
|
|
.map((entry) => `- ${entry.summary}: ${entry.draft}`);
|
2026-05-04 20:45:26 +01:00
|
|
|
|
|
|
|
|
return [
|
2026-05-16 18:43:53 +01:00
|
|
|
`- Identity: ${profile.publicName}${profile.publicDescription ? ` - ${profile.publicDescription}` : ""}`,
|
|
|
|
|
profile.privateNotes.length > 0
|
|
|
|
|
? `- Private notes: ${profile.privateNotes.slice(0, 4).join(" | ")}`
|
2026-05-04 20:45:26 +01:00
|
|
|
: "- Private notes: none recorded.",
|
|
|
|
|
topValues.length > 0
|
|
|
|
|
? `- Highest-priority values: ${topValues.join(" | ")}`
|
|
|
|
|
: "- Highest-priority values: none recorded.",
|
2026-05-16 18:43:53 +01:00
|
|
|
renderTypedSleepCycleSummary(state.scheduledRuntime, state.thoughtMemory),
|
|
|
|
|
renderTypedSpeakingPressureSummary(state.scheduledRuntime),
|
2026-05-07 23:40:00 +01:00
|
|
|
openCases.length > 0
|
|
|
|
|
? ["- Outstanding room obligations:", ...openCases].join("\n")
|
|
|
|
|
: "- Outstanding room obligations: none recorded.",
|
2026-05-16 18:43:53 +01:00
|
|
|
memories.length > 0
|
|
|
|
|
? ["- Typed memory:", ...memories].join("\n")
|
|
|
|
|
: "- Typed memory: none recorded.",
|
|
|
|
|
incubation.length > 0
|
|
|
|
|
? ["- Incubating threads:", ...incubation].join("\n")
|
|
|
|
|
: "- Incubating threads: none recorded.",
|
|
|
|
|
interventions.length > 0
|
|
|
|
|
? ["- Candidate interventions:", ...interventions].join("\n")
|
|
|
|
|
: "- Candidate interventions: none recorded.",
|
|
|
|
|
renderTransientRoomLog(options),
|
2026-05-05 13:42:41 +01:00
|
|
|
].join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-16 18:43:53 +01:00
|
|
|
function renderTypedMemory(memory: VoidThoughtMemory["memories"][number]): string {
|
|
|
|
|
const target = memory.target.label ?? memory.target.id;
|
|
|
|
|
const meaning = [
|
|
|
|
|
memory.claim ? `claim=${memory.claim}` : undefined,
|
|
|
|
|
memory.question ? `question=${memory.question}` : undefined,
|
|
|
|
|
memory.tension ? `tension=${memory.tension}` : undefined,
|
|
|
|
|
memory.actionImplication ? `next=${memory.actionImplication}` : undefined,
|
|
|
|
|
].filter((value): value is string => Boolean(value));
|
|
|
|
|
return `- ${target} [${memory.kind}]: ${memory.summary}${meaning.length > 0 ? ` (${meaning.join("; ")})` : ""}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderTypedIncubationThread(thread: VoidThoughtMemory["incubation"][number]): string {
|
|
|
|
|
const target = thread.target.label ?? thread.target.id;
|
|
|
|
|
const metrics = [
|
|
|
|
|
`status=${thread.status}`,
|
|
|
|
|
`maturation=${thread.maturation.toFixed(2)}`,
|
|
|
|
|
thread.desireToSpeak !== undefined ? `speak=${thread.desireToSpeak.toFixed(2)}` : undefined,
|
|
|
|
|
thread.noveltyToSelf !== undefined ? `self=${thread.noveltyToSelf.toFixed(2)}` : undefined,
|
|
|
|
|
thread.noveltyToRoom !== undefined ? `room=${thread.noveltyToRoom.toFixed(2)}` : undefined,
|
|
|
|
|
].filter((value): value is string => Boolean(value));
|
|
|
|
|
return `- ${thread.topic} -> ${target} [${metrics.join("; ")}]: ${thread.summary}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderTypedSleepCycleSummary(
|
|
|
|
|
runtime: VoidScheduledRuntime,
|
|
|
|
|
thoughtMemory: VoidThoughtMemory,
|
|
|
|
|
): string {
|
|
|
|
|
const sleepCycle = runtime.sleepCycle;
|
|
|
|
|
const dreams = thoughtMemory.memories
|
|
|
|
|
.filter((memory) => memory.kind === "dream_residue")
|
|
|
|
|
.slice(-2)
|
|
|
|
|
.reverse()
|
|
|
|
|
.map((memory) => `${memory.target.label ?? memory.target.id}: ${memory.summary}`);
|
|
|
|
|
const headline = sleepCycle.isNapping
|
|
|
|
|
? `- Sleep cycle: napping${sleepCycle.currentNapEndsAt ? ` until ${sleepCycle.currentNapEndsAt}` : ""}.`
|
|
|
|
|
: `- Sleep cycle: awake${sleepCycle.nextNapStartsAt ? `; next nap ${sleepCycle.nextNapStartsAt}` : ""}.`;
|
|
|
|
|
const themeLine =
|
|
|
|
|
sleepCycle.activeDreamThemes.length > 0
|
|
|
|
|
? `- Active dream themes: ${sleepCycle.activeDreamThemes.join(" | ")}`
|
|
|
|
|
: "- Active dream themes: none recorded.";
|
|
|
|
|
const dreamLine =
|
|
|
|
|
dreams.length > 0
|
|
|
|
|
? `- Recent dream residue: ${dreams.join(" | ")}`
|
|
|
|
|
: "- Recent dream residue: none recorded.";
|
|
|
|
|
|
|
|
|
|
return [headline, themeLine, dreamLine].join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function renderTypedSpeakingPressureSummary(runtime: VoidScheduledRuntime): string {
|
|
|
|
|
const pressure = runtime.speakingPressure;
|
|
|
|
|
const parts = [
|
|
|
|
|
`needToSpeak=${pressure.needToSpeak.toFixed(2)}`,
|
|
|
|
|
pressure.confessionPressure !== undefined ? `confession=${pressure.confessionPressure.toFixed(2)}` : undefined,
|
|
|
|
|
pressure.noveltyPressure !== undefined ? `novelty=${pressure.noveltyPressure.toFixed(2)}` : undefined,
|
|
|
|
|
pressure.recentSpeechDamping !== undefined ? `speechDamping=${pressure.recentSpeechDamping.toFixed(2)}` : undefined,
|
|
|
|
|
].filter((value): value is string => Boolean(value));
|
|
|
|
|
|
|
|
|
|
return `- Speaking pressure: ${parts.join(", ")}`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-05 13:42:41 +01:00
|
|
|
function renderTransientRoomLog(options: LoadVoidSelfStateOptions): string {
|
|
|
|
|
const recentMessages = (options.recentMessages ?? []).slice(-10);
|
|
|
|
|
const guildName = options.guildContext?.guildName ?? options.guildContext?.guildId ?? "(direct/unknown)";
|
|
|
|
|
const channelName =
|
|
|
|
|
options.guildContext?.channelName ?? options.guildContext?.channelId ?? "(unknown channel)";
|
|
|
|
|
|
|
|
|
|
if (recentMessages.length === 0) {
|
|
|
|
|
return [
|
2026-05-06 01:46:04 +01:00
|
|
|
"- Current room snapshot:",
|
2026-05-05 13:42:41 +01:00
|
|
|
`- Current room: guild=${guildName}; channel=${channelName}`,
|
2026-05-06 01:46:04 +01:00
|
|
|
"- No recent chat messages were captured in this snapshot.",
|
2026-05-05 13:42:41 +01:00
|
|
|
].join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const lines = recentMessages.map(
|
|
|
|
|
(message) =>
|
|
|
|
|
`- [${message.timestamp}] ${message.authorName}: ${collapseWhitespace(message.content || "(no text content)")}`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return [
|
2026-05-06 01:46:04 +01:00
|
|
|
"- Current room snapshot:",
|
2026-05-05 13:42:41 +01:00
|
|
|
`- Current room: guild=${guildName}; channel=${channelName}`,
|
|
|
|
|
...lines,
|
2026-05-04 20:45:26 +01:00
|
|
|
].join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-16 12:01:43 +01:00
|
|
|
function buildVoidSelfStateProjection(
|
|
|
|
|
typedState: VoidSelfStateTypedProjection,
|
|
|
|
|
): VoidSelfStateContext["projection"] {
|
|
|
|
|
const sleepCycle = typedState.scheduledRuntime.sleepCycle;
|
|
|
|
|
const dreamSummaries = typedState.thoughtMemory.memories
|
|
|
|
|
.filter((memory) => memory.kind === "dream_residue")
|
2026-05-05 03:10:37 +01:00
|
|
|
.slice(-3)
|
2026-05-16 12:01:43 +01:00
|
|
|
.reverse()
|
|
|
|
|
.map((memory) => memory.summary);
|
|
|
|
|
const isNapping = sleepCycle.isNapping === true;
|
|
|
|
|
const napStartedAt = sleepCycle.currentNapStartedAt;
|
|
|
|
|
const napEndsAt = sleepCycle.currentNapEndsAt;
|
|
|
|
|
const nextNapAt = sleepCycle.nextNapStartsAt;
|
2026-05-05 03:10:37 +01:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
mode: isNapping ? "napping" : "awake",
|
|
|
|
|
effortCeiling: isNapping ? "minimal" : "normal",
|
|
|
|
|
napStartedAt: isNapping ? napStartedAt : undefined,
|
|
|
|
|
napEndsAt: isNapping ? napEndsAt : undefined,
|
|
|
|
|
nextNapAt,
|
2026-05-16 12:01:43 +01:00
|
|
|
activeDreamThemes: sleepCycle.activeDreamThemes,
|
2026-05-05 03:10:37 +01:00
|
|
|
recentDreamSummaries: dreamSummaries,
|
|
|
|
|
replyDirective: isNapping
|
|
|
|
|
? "You are in a scheduled nap. Reply in brief, low-effort, half-dreaming mutters instead of doing full attentive service-work."
|
|
|
|
|
: undefined,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-05 13:42:41 +01:00
|
|
|
function collapseWhitespace(value: string): string {
|
|
|
|
|
return value.replace(/\s+/g, " ").trim();
|
|
|
|
|
}
|