Guard swarm voice rendering on listeners
This commit is contained in:
parent
f338eb4854
commit
2c0b0a5781
7 changed files with 110 additions and 3 deletions
|
|
@ -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";
|
||||
|
|
|
|||
68
packages/core/src/repo-face-voice-presence.ts
Normal file
68
packages/core/src/repo-face-voice-presence.ts
Normal 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);
|
||||
}
|
||||
Loading…
Reference in a new issue