feat: surface system presence for the agent

This commit is contained in:
Peter Steinberger
2025-12-09 02:25:37 +01:00
parent 317f666d4c
commit 1969e78d54
10 changed files with 202 additions and 3 deletions

View File

@@ -10,6 +10,8 @@ import {
type HeartbeatEventPayload,
onHeartbeatEvent,
} from "./heartbeat-events.js";
import { enqueueSystemEvent } from "./system-events.js";
import { listSystemPresence, updateSystemPresence } from "./system-presence.js";
type ControlRequest = {
type: "request";
@@ -158,6 +160,19 @@ export async function startControlChannel(
respond({ ok: true });
break;
}
case "system-event": {
const text = String(parsed.params?.text ?? "").trim();
if (text) {
enqueueSystemEvent(text);
updateSystemPresence(text);
}
respond({ ok: true });
break;
}
case "system-presence": {
respond(listSystemPresence());
break;
}
default:
respond(undefined, false, `unknown method: ${parsed.method}`);
break;

View File

@@ -0,0 +1,59 @@
import { loadConfig, type WarelayConfig } from "../config/config.js";
import { normalizeE164 } from "../utils.js";
import {
getWebAuthAgeMs,
readWebSelfId,
webAuthExists,
} from "../web/session.js";
const DEFAULT_WEBCHAT_PORT = 18788;
export async function buildProviderSummary(
cfg?: WarelayConfig,
): Promise<string> {
const effective = cfg ?? loadConfig();
const parts: string[] = [];
const webLinked = await webAuthExists();
const authAgeMs = getWebAuthAgeMs();
const authAge = authAgeMs === null ? "unknown" : formatAge(authAgeMs);
const { e164 } = readWebSelfId();
parts.push(
webLinked
? `WhatsApp web linked${e164 ? ` as ${e164}` : ""} (auth ${authAge})`
: "WhatsApp web not linked",
);
const telegramToken =
process.env.TELEGRAM_BOT_TOKEN ?? effective.telegram?.botToken;
parts.push(
telegramToken ? "Telegram bot configured" : "Telegram bot not configured",
);
if (effective.webchat?.enabled === false) {
parts.push("WebChat disabled");
} else {
const port = effective.webchat?.port ?? DEFAULT_WEBCHAT_PORT;
parts.push(`WebChat enabled (port ${port})`);
}
const allowFrom = effective.inbound?.allowFrom?.length
? effective.inbound.allowFrom.map(normalizeE164).filter(Boolean)
: [];
if (allowFrom.length) {
parts.push(`AllowFrom: ${allowFrom.join(", ")}`);
}
return `System status: ${parts.join("; ")}`;
}
export function formatAge(ms: number): string {
if (ms < 0) return "unknown";
const minutes = Math.round(ms / 60_000);
if (minutes < 1) return "just now";
if (minutes < 60) return `${minutes}m ago`;
const hours = Math.round(minutes / 60);
if (hours < 48) return `${hours}h ago`;
const days = Math.round(hours / 24);
return `${days}d ago`;
}

View File

@@ -0,0 +1,33 @@
// Lightweight in-memory queue for human-readable system events that should be
// prefixed to the next main-session prompt/heartbeat. We intentionally avoid
// persistence to keep events ephemeral.
type SystemEvent = { text: string; ts: number };
const MAX_EVENTS = 20;
const queue: SystemEvent[] = [];
let lastText: string | null = null;
export function enqueueSystemEvent(text: string) {
const cleaned = text.trim();
if (!cleaned) return;
if (lastText === cleaned) return; // skip consecutive duplicates
lastText = cleaned;
queue.push({ text: cleaned, ts: Date.now() });
if (queue.length > MAX_EVENTS) queue.shift();
}
export function drainSystemEvents(): string[] {
const out = queue.map((e) => e.text);
queue.length = 0;
lastText = null;
return out;
}
export function peekSystemEvents(): string[] {
return queue.map((e) => e.text);
}
export function hasSystemEvents() {
return queue.length > 0;
}

View File

@@ -0,0 +1,48 @@
export type SystemPresence = {
host?: string;
ip?: string;
version?: string;
lastInputSeconds?: number;
mode?: string;
reason?: string;
text: string;
ts: number;
};
const entries = new Map<string, SystemPresence>();
function parsePresence(text: string): SystemPresence {
const trimmed = text.trim();
const pattern =
/Node:\s*([^ (]+)\s*\(([^)]+)\)\s*·\s*app\s*([^·]+?)\s*·\s*last input\s*([0-9]+)s ago\s*·\s*mode\s*([^·]+?)\s*·\s*reason\s*(.+)$/i;
const match = trimmed.match(pattern);
if (!match) {
return { text: trimmed, ts: Date.now() };
}
const [, host, ip, version, lastInputStr, mode, reasonRaw] = match;
const lastInputSeconds = Number.parseInt(lastInputStr, 10);
const reason = reasonRaw.trim();
return {
host: host.trim(),
ip: ip.trim(),
version: version.trim(),
lastInputSeconds: Number.isFinite(lastInputSeconds)
? lastInputSeconds
: undefined,
mode: mode.trim(),
reason,
text: trimmed,
ts: Date.now(),
};
}
export function updateSystemPresence(text: string) {
const parsed = parsePresence(text);
const key =
parsed.host?.toLowerCase() || parsed.ip || parsed.text.slice(0, 64);
entries.set(key, parsed);
}
export function listSystemPresence(): SystemPresence[] {
return [...entries.values()].sort((a, b) => b.ts - a.ts);
}