openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
88 lines (87 loc) • 3.63 kB
JavaScript
import { o as upsertPresence } from "./system-presence-Dj1W_mp4.mjs";
import { n as presenceUserKey } from "./presence-user-Bwx09R-f.mjs";
import { t as buildAuthenticatedPresenceUser } from "./authenticated-presence-user-cWd9lfpl.mjs";
//#region src/gateway/server/client-presence.ts
const ACTIVITY_BROADCAST_INTERVAL_MS = 3e4;
const activityPublications = /* @__PURE__ */ new WeakMap();
function isLiveClient(e) {
return !e.invalidated && e.socket.readyState === 1;
}
function presenceIdentity(e) {
const t = e.authenticatedUserProfile?.profileId;
return t ? presenceUserKey({
id: t,
identity: {
type: "profile",
id: t
}
}) : e.authenticatedUserId && !e.authenticatedGitHubIdentitySync ? presenceUserKey({ id: e.authenticatedUserId }) : void 0;
}
/** Current transport facts are complete even when the legacy beacon cache evicts a row. */
function snapshotClientPresence(e, t = Date.now()) {
return [...e].filter((e) => e.presenceKey && isLiveClient(e)).map((e) => {
const n = {
connectionId: e.connId,
deviceId: e.connect.device?.id,
instanceId: e.connect.client.instanceId,
host: e.connect.client.displayName ?? e.connect.client.id,
clientId: e.connect.client.id,
platform: e.connect.client.platform,
deviceFamily: e.connect.client.deviceFamily,
timeZone: e.connect.client.timeZone,
ip: e.internal?.isLocalClient ? void 0 : e.clientIp,
roles: [e.connect.role ?? "operator"],
user: presenceIdentity(e) ? buildAuthenticatedPresenceUser(e) : void 0,
connectionLastActivityAt: e.connectionLastActivityAt,
ts: t
};
if (e.personPresence) {
n.onlineSince = e.personPresence.onlineSince;
if (e.personPresence.lastActivityAt !== void 0) n.lastActivityAt = e.personPresence.lastActivityAt;
}
return n;
});
}
/** Reconciles live identity/timing and returns whether a presence snapshot is needed. */
function refreshClientPresence(e, t, n) {
if (!e.has(t) || !isLiveClient(t) || !t.presenceKey) return false;
const r = presenceIdentity(t);
if (!r) return false;
const i = [...e].filter((e) => isLiveClient(e) && e.presenceKey && presenceIdentity(e) === r && (e === t || t.personPresence && e.personPresence));
const a = t.personPresence ? { ...t.personPresence } : void 0;
for (const e of i) if (a && e.personPresence) {
a.onlineSince = Math.min(a.onlineSince, e.personPresence.onlineSince);
const t = e.personPresence.lastActivityAt;
if (t !== void 0) a.lastActivityAt = Math.max(a.lastActivityAt ?? t, t);
}
const o = activityPublications.get(t);
const s = n === void 0 || a?.lastActivityAt === void 0 || o?.identity !== r || n - o.at >= ACTIVITY_BROADCAST_INTERVAL_MS;
if (a && n !== void 0) {
a.lastActivityAt = n;
t.connectionLastActivityAt = n;
}
const c = s ? {
identity: r,
at: n ?? a?.lastActivityAt ?? Date.now()
} : o;
for (const e of i) {
if (a && e.personPresence) e.personPresence = { ...a };
if (c) activityPublications.set(e, c);
upsertPresence(e.presenceKey, {
connectionId: e.connId,
clientId: e.connect.client.id,
mode: e.connect.client.mode,
user: buildAuthenticatedPresenceUser(e),
...e.personPresence,
connectionLastActivityAt: e.connectionLastActivityAt
});
}
return s;
}
/** Records accepted human activity; copies and clients closed during admission cannot write. */
function recordClientPresenceActivity(e, t) {
const n = t?.connId ? e.getByConnectionId(t.connId) : void 0;
return Boolean(n && n === t && n.personPresence && refreshClientPresence(e, n, Date.now()));
}
//#endregion
export { refreshClientPresence as n, snapshotClientPresence as r, recordClientPresenceActivity as t };