Guard swarm voice rendering on listeners

This commit is contained in:
Meta Vens 2026-06-13 20:43:20 +01:00
commit 2c0b0a5781
7 changed files with 110 additions and 3 deletions

View file

@ -116,6 +116,7 @@ const envSchema = z.object({
REPO_FACE_DISCORD_VOICE_CHANNEL_ID: optionalNonEmptyString,
REPO_FACE_DISCORD_VOICE_OUTBOX_PATH: z.string().min(1).default(".voidbot/status/repo-face-voice-outbox.jsonl"),
REPO_FACE_DISCORD_VOICE_PLAYED_PATH: z.string().min(1).default(".voidbot/status/repo-face-voice-played.jsonl"),
REPO_FACE_DISCORD_VOICE_PRESENCE_PATH: z.string().min(1).default(".voidbot/status/repo-face-voice-presence.json"),
REPO_FACE_DISCORD_VOICE_POLL_INTERVAL_MS: z.coerce.number().int().positive().default(5000),
METAME_OWNER_VOICE_ENABLED: booleanFromEnv.default(false),
METAME_OWNER_VOICE_SOURCE_CHANNEL_ID: optionalNonEmptyString,
@ -207,6 +208,7 @@ export interface AppConfig {
channelId?: string;
outboxPath: string;
playedPath: string;
presencePath: string;
pollIntervalMs: number;
};
metameOwnerVoice: {
@ -467,6 +469,7 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env): AppConfig {
channelId: parsed.REPO_FACE_DISCORD_VOICE_CHANNEL_ID,
outboxPath: resolve(parsed.REPO_FACE_DISCORD_VOICE_OUTBOX_PATH),
playedPath: resolve(parsed.REPO_FACE_DISCORD_VOICE_PLAYED_PATH),
presencePath: resolve(parsed.REPO_FACE_DISCORD_VOICE_PRESENCE_PATH),
pollIntervalMs: parsed.REPO_FACE_DISCORD_VOICE_POLL_INTERVAL_MS,
},
metameOwnerVoice: {

View file

@ -10,6 +10,7 @@ export * from "./repo-discord-identities";
export * from "./repo-face-heartbeat-queue";
export * from "./repo-face-initializer";
export * from "./repo-face-rest";
export * from "./repo-face-voice-presence";
export * from "./repo-face-voice-outbox";
export * from "./situational-social-read";
export * from "./state-storage";

View file

@ -0,0 +1,68 @@
import { readFile, writeFile, mkdir } from "node:fs/promises";
import { dirname } from "node:path";
export interface RepoFaceVoicePresenceSnapshot {
schemaVersion: "voidbot.repo_face_voice_presence.v1";
channelId: string;
observedAt: string;
humanListenerCount: number;
botListenerCount: number;
}
export async function writeRepoFaceVoicePresenceSnapshot(
path: string,
snapshot: RepoFaceVoicePresenceSnapshot,
): Promise<void> {
await mkdir(dirname(path), { recursive: true });
await writeFile(path, `${JSON.stringify(snapshot, null, 2)}\n`, "utf8");
}
export async function loadRepoFaceVoicePresenceSnapshot(
path: string,
): Promise<RepoFaceVoicePresenceSnapshot | undefined> {
const content = await readFile(path, "utf8").catch((error: NodeJS.ErrnoException) => {
if (error.code === "ENOENT") {
return undefined;
}
throw error;
});
if (!content) {
return undefined;
}
const parsed = JSON.parse(content) as Partial<RepoFaceVoicePresenceSnapshot>;
if (
parsed.schemaVersion !== "voidbot.repo_face_voice_presence.v1" ||
typeof parsed.channelId !== "string" ||
typeof parsed.observedAt !== "string" ||
typeof parsed.humanListenerCount !== "number"
) {
return undefined;
}
return {
schemaVersion: "voidbot.repo_face_voice_presence.v1",
channelId: parsed.channelId,
observedAt: parsed.observedAt,
humanListenerCount: parsed.humanListenerCount,
botListenerCount: typeof parsed.botListenerCount === "number" ? parsed.botListenerCount : 0,
};
}
export function hasFreshHumanRepoFaceVoiceListener(input: {
snapshot: RepoFaceVoicePresenceSnapshot | undefined;
channelId: string | undefined;
now?: Date;
maxAgeMs?: number;
}): boolean {
if (!input.snapshot || !input.channelId || input.snapshot.channelId !== input.channelId) {
return false;
}
if (input.snapshot.humanListenerCount <= 0) {
return false;
}
const observedAt = Date.parse(input.snapshot.observedAt);
if (!Number.isFinite(observedAt)) {
return false;
}
const nowMs = (input.now ?? new Date()).getTime();
return nowMs - observedAt <= (input.maxAgeMs ?? 30_000);
}