fix: emit delta-only node system events

This commit is contained in:
Peter Steinberger
2025-12-21 00:55:54 +01:00
parent 2b2f13ca79
commit 383097a03a
3 changed files with 107 additions and 15 deletions

View File

@@ -7,10 +7,30 @@ type SystemEvent = { text: string; ts: number };
const MAX_EVENTS = 20;
const queue: SystemEvent[] = [];
let lastText: string | null = null;
let lastContextKey: string | null = null;
export function enqueueSystemEvent(text: string) {
type SystemEventOptions = {
contextKey?: string | null;
};
function normalizeContextKey(key?: string | null): string | null {
if (!key) return null;
const trimmed = key.trim();
if (!trimmed) return null;
return trimmed.toLowerCase();
}
export function isSystemEventContextChanged(
contextKey?: string | null,
): boolean {
const normalized = normalizeContextKey(contextKey);
return normalized !== lastContextKey;
}
export function enqueueSystemEvent(text: string, options?: SystemEventOptions) {
const cleaned = text.trim();
if (!cleaned) return;
lastContextKey = normalizeContextKey(options?.contextKey);
if (lastText === cleaned) return; // skip consecutive duplicates
lastText = cleaned;
queue.push({ text: cleaned, ts: Date.now() });
@@ -21,6 +41,7 @@ export function drainSystemEvents(): string[] {
const out = queue.map((e) => e.text);
queue.length = 0;
lastText = null;
lastContextKey = null;
return out;
}

View File

@@ -16,6 +16,14 @@ export type SystemPresence = {
ts: number;
};
export type SystemPresenceUpdate = {
key: string;
previous?: SystemPresence;
next: SystemPresence;
changes: Partial<SystemPresence>;
changedKeys: (keyof SystemPresence)[];
};
const entries = new Map<string, SystemPresence>();
const TTL_MS = 5 * 60 * 1000; // 5 minutes
const MAX_ENTRIES = 200;
@@ -154,7 +162,9 @@ type SystemPresencePayload = {
tags?: string[];
};
export function updateSystemPresence(payload: SystemPresencePayload) {
export function updateSystemPresence(
payload: SystemPresencePayload,
): SystemPresenceUpdate {
ensureSelfPresence();
const parsed = parsePresence(payload.text);
const key =
@@ -164,6 +174,7 @@ export function updateSystemPresence(payload: SystemPresencePayload) {
parsed.ip ||
parsed.text.slice(0, 64) ||
os.hostname().toLowerCase();
const hadExisting = entries.has(key);
const existing = entries.get(key) ?? ({} as SystemPresence);
const merged: SystemPresence = {
...existing,
@@ -185,6 +196,25 @@ export function updateSystemPresence(payload: SystemPresencePayload) {
ts: Date.now(),
};
entries.set(key, merged);
const trackKeys = ["host", "ip", "version", "mode", "reason"] as const;
type TrackKey = (typeof trackKeys)[number];
const changes: Partial<Pick<SystemPresence, TrackKey>> = {};
const changedKeys: TrackKey[] = [];
for (const k of trackKeys) {
const prev = existing[k];
const next = merged[k];
if (prev !== next) {
changes[k] = next;
changedKeys.push(k);
}
}
return {
key,
previous: hadExisting ? existing : undefined,
next: merged,
changes,
changedKeys,
} satisfies SystemPresenceUpdate;
}
export function upsertPresence(key: string, presence: Partial<SystemPresence>) {