|
| 1 | +import { asNullableRecord } from "@openclaw/normalization-core/record-coerce"; |
| 2 | +import type { RouteId } from "../app-route-paths.ts"; |
| 3 | +import type { ApplicationContext } from "../app/context.ts"; |
| 4 | +import { readPresenceEntries, type PresencePayload } from "../app/user-profile.ts"; |
| 5 | +import { createSessionEventRefreshCoordinator } from "../lib/sessions/event-refresh-coordinator.ts"; |
| 6 | +import { readSessionChangedEvent } from "../lib/sessions/reconcile.ts"; |
| 7 | +import { normalizeAgentId, parseAgentSessionKey } from "../lib/sessions/session-key.ts"; |
| 8 | +import type { SidebarSessionStatusFilter } from "./app-sidebar-session-types.ts"; |
| 9 | +import type { SessionDataControllerHost } from "./session-data-controller-catalog.ts"; |
| 10 | + |
| 11 | +type SessionGatewayEventOwner = { |
| 12 | + presencePayload: PresencePayload | undefined; |
| 13 | + readonly sessionScopeGeneration: number; |
| 14 | + handleSessionCatalogHostEvent(payload: unknown): void; |
| 15 | + handleSessionCatalogPresence(payload: unknown): void; |
| 16 | + refreshSidebarSessions(agentId?: string): Promise<void>; |
| 17 | + requestSessionDataUpdate(): void; |
| 18 | +}; |
| 19 | + |
| 20 | +type FilteredSessionRefreshScope = { |
| 21 | + agentId: string; |
| 22 | + archivedFilter: SidebarSessionStatusFilter; |
| 23 | + client: ApplicationContext<RouteId>["gateway"]["snapshot"]["client"]; |
| 24 | + generation: number; |
| 25 | +}; |
| 26 | + |
| 27 | +export function subscribeSessionDataGatewayEvents( |
| 28 | + gateway: ApplicationContext<RouteId>["gateway"], |
| 29 | + owner: SessionGatewayEventOwner, |
| 30 | + host: Pick< |
| 31 | + SessionDataControllerHost, |
| 32 | + "expandedAgentId" | "isConnected" | "sidebarSessionStatusFilter" |
| 33 | + >, |
| 34 | +): () => void { |
| 35 | + let subscribed = true; |
| 36 | + let refreshScope: FilteredSessionRefreshScope | null = null; |
| 37 | + const scopeIsCurrent = () => |
| 38 | + refreshScope !== null && |
| 39 | + subscribed && |
| 40 | + host.isConnected && |
| 41 | + gateway.snapshot.phase === "connected" && |
| 42 | + gateway.snapshot.client === refreshScope.client && |
| 43 | + owner.sessionScopeGeneration === refreshScope.generation && |
| 44 | + host.sidebarSessionStatusFilter() === refreshScope.archivedFilter && |
| 45 | + normalizeAgentId(host.expandedAgentId()) === refreshScope.agentId; |
| 46 | + const refreshCoordinator = createSessionEventRefreshCoordinator({ |
| 47 | + canRefresh: scopeIsCurrent, |
| 48 | + refresh: () => |
| 49 | + refreshScope ? owner.refreshSidebarSessions(refreshScope.agentId) : Promise.resolve(), |
| 50 | + }); |
| 51 | + const unsubscribe = gateway.subscribeEvents((event) => { |
| 52 | + if (event.event === "sessions.catalog.host") { |
| 53 | + owner.handleSessionCatalogHostEvent(event.payload); |
| 54 | + return; |
| 55 | + } |
| 56 | + if (event.event === "sessions.changed") { |
| 57 | + const archivedFilter = host.sidebarSessionStatusFilter(); |
| 58 | + if (archivedFilter === "active") { |
| 59 | + return; |
| 60 | + } |
| 61 | + const agentId = normalizeAgentId(host.expandedAgentId()); |
| 62 | + const sessionEvent = readSessionChangedEvent(event.payload); |
| 63 | + const payloadAgentId = asNullableRecord(event.payload)?.agentId; |
| 64 | + const eventAgentId = |
| 65 | + sessionEvent?.agentId ?? |
| 66 | + parseAgentSessionKey(sessionEvent?.key)?.agentId ?? |
| 67 | + (typeof payloadAgentId === "string" ? payloadAgentId : undefined); |
| 68 | + if (eventAgentId && normalizeAgentId(eventAgentId) !== agentId) { |
| 69 | + return; |
| 70 | + } |
| 71 | + const nextScope: FilteredSessionRefreshScope = { |
| 72 | + agentId, |
| 73 | + archivedFilter, |
| 74 | + client: gateway.snapshot.client, |
| 75 | + generation: owner.sessionScopeGeneration, |
| 76 | + }; |
| 77 | + if ( |
| 78 | + refreshScope && |
| 79 | + (refreshScope.agentId !== nextScope.agentId || |
| 80 | + refreshScope.archivedFilter !== nextScope.archivedFilter || |
| 81 | + refreshScope.client !== nextScope.client || |
| 82 | + refreshScope.generation !== nextScope.generation) |
| 83 | + ) { |
| 84 | + refreshCoordinator.reset(); |
| 85 | + } |
| 86 | + refreshScope = nextScope; |
| 87 | + // Canonical debounce/max-wait and single-flight behavior belong to one |
| 88 | + // coordinator; scope checks retire stale agents, filters, and clients. |
| 89 | + refreshCoordinator.schedule(); |
| 90 | + return; |
| 91 | + } |
| 92 | + if (event.event === "presence") { |
| 93 | + const presence = readPresenceEntries(event.payload); |
| 94 | + owner.presencePayload = presence ? { presence } : undefined; |
| 95 | + owner.requestSessionDataUpdate(); |
| 96 | + owner.handleSessionCatalogPresence(event.payload); |
| 97 | + } |
| 98 | + }); |
| 99 | + return () => { |
| 100 | + subscribed = false; |
| 101 | + refreshCoordinator.dispose(); |
| 102 | + unsubscribe(); |
| 103 | + }; |
| 104 | +} |
0 commit comments