|
| 1 | +import { validateNodeEventParams } from "../../../packages/gateway-protocol/src/index.js"; |
| 2 | +import { |
| 3 | + captureNodePairingGeneration, |
| 4 | + isNodePairingGenerationCurrent, |
| 5 | +} from "../../infra/node-pairing-state.js"; |
| 6 | +import type { NodeEventContext } from "../server-node-events-types.js"; |
| 7 | +import { respondInvalidParams, respondUnavailableOnThrow } from "./nodes.helpers.js"; |
| 8 | +import { resolveDispatchableNodeSession, respondPairingChanged } from "./nodes.shared.js"; |
| 9 | +import type { GatewayRequestHandlers } from "./types.js"; |
| 10 | + |
| 11 | +export const nodeEventHandlers: GatewayRequestHandlers = { |
| 12 | + "node.event": async ({ params, respond, context, client }) => { |
| 13 | + if (!validateNodeEventParams(params)) { |
| 14 | + respondInvalidParams({ |
| 15 | + respond, |
| 16 | + method: "node.event", |
| 17 | + validator: validateNodeEventParams, |
| 18 | + }); |
| 19 | + return; |
| 20 | + } |
| 21 | + const p = params as { event: string; payload?: unknown; payloadJSON?: string | null }; |
| 22 | + const payloadJSON = |
| 23 | + typeof p.payloadJSON === "string" |
| 24 | + ? p.payloadJSON |
| 25 | + : p.payload !== undefined |
| 26 | + ? JSON.stringify(p.payload) |
| 27 | + : null; |
| 28 | + await respondUnavailableOnThrow(respond, async () => { |
| 29 | + const nodeId = client?.connect?.device?.id ?? client?.connect?.client?.id ?? "node"; |
| 30 | + const nodeSession = context.nodeRegistry.get(nodeId); |
| 31 | + const eventConnId = client?.connId; |
| 32 | + const eventPairingGeneration = nodeSession?.pairingGeneration; |
| 33 | + const isEventConnectionCurrent = async (): Promise<boolean> => { |
| 34 | + if (!eventConnId || !eventPairingGeneration) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + const before = resolveDispatchableNodeSession( |
| 38 | + context.nodeRegistry.getForPairingGeneration(nodeId, eventPairingGeneration), |
| 39 | + ); |
| 40 | + if (!before || before.connId !== eventConnId) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + if (!(await context.nodeRegistry.isConnectionCurrentPairingState(eventConnId))) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + const after = resolveDispatchableNodeSession( |
| 47 | + context.nodeRegistry.getForPairingGeneration(nodeId, eventPairingGeneration), |
| 48 | + ); |
| 49 | + return after?.connId === eventConnId; |
| 50 | + }; |
| 51 | + const { handleNodeEvent } = await import("../server-node-events.js"); |
| 52 | + const apnsGeneration = |
| 53 | + p.event === "push.apns.register" ? await captureNodePairingGeneration(nodeId) : null; |
| 54 | + const presenceAllowed = |
| 55 | + nodeSession !== undefined && |
| 56 | + nodeSession.connId === client?.connId && |
| 57 | + nodeSession.permissions?.accessibility === true; |
| 58 | + const nodeContext: NodeEventContext = { |
| 59 | + deps: context.deps, |
| 60 | + broadcast: context.broadcast, |
| 61 | + nodeSendToSession: context.nodeSendToSession, |
| 62 | + nodeSubscribe: async (subscriptionNodeId, sessionKey, subscriptionConnId) => { |
| 63 | + if ( |
| 64 | + subscriptionNodeId !== nodeId || |
| 65 | + !subscriptionConnId || |
| 66 | + subscriptionConnId !== client?.connId || |
| 67 | + !(await isEventConnectionCurrent()) |
| 68 | + ) { |
| 69 | + return; |
| 70 | + } |
| 71 | + context.nodeSubscribe(subscriptionNodeId, sessionKey, subscriptionConnId); |
| 72 | + }, |
| 73 | + nodeUnsubscribe: async (subscriptionNodeId, sessionKey, subscriptionConnId) => { |
| 74 | + if ( |
| 75 | + subscriptionNodeId !== nodeId || |
| 76 | + !subscriptionConnId || |
| 77 | + subscriptionConnId !== client?.connId || |
| 78 | + !(await isEventConnectionCurrent()) |
| 79 | + ) { |
| 80 | + return; |
| 81 | + } |
| 82 | + context.nodeUnsubscribe(subscriptionNodeId, sessionKey, subscriptionConnId); |
| 83 | + }, |
| 84 | + broadcastVoiceWakeChanged: context.broadcastVoiceWakeChanged, |
| 85 | + addChatRun: context.addChatRun, |
| 86 | + removeChatRun: context.removeChatRun, |
| 87 | + chatAbortControllers: context.chatAbortControllers, |
| 88 | + dedupe: context.dedupe, |
| 89 | + agentRunSeq: context.agentRunSeq, |
| 90 | + getHealthCache: context.getHealthCache, |
| 91 | + refreshHealthSnapshot: context.refreshHealthSnapshot, |
| 92 | + loadGatewayModelCatalog: context.loadGatewayModelCatalog, |
| 93 | + authorizeNodeSystemRunEvent: (eventParams) => |
| 94 | + context.nodeRegistry.authorizeSystemRunEvent({ |
| 95 | + nodeId: eventParams.nodeId, |
| 96 | + connId: eventParams.connId, |
| 97 | + runId: eventParams.runId, |
| 98 | + sessionKey: eventParams.sessionKey, |
| 99 | + terminal: eventParams.terminal, |
| 100 | + }), |
| 101 | + updateNodePresenceActivity: (activity) => { |
| 102 | + const updated = context.nodeRegistry.updatePresenceActivity(activity); |
| 103 | + return updated?.lastActiveAtMs !== undefined && updated.presenceUpdatedAtMs !== undefined |
| 104 | + ? { |
| 105 | + lastActiveAtMs: updated.lastActiveAtMs, |
| 106 | + presenceUpdatedAtMs: updated.presenceUpdatedAtMs, |
| 107 | + } |
| 108 | + : null; |
| 109 | + }, |
| 110 | + clearNodePresenceActivity: (activity) => |
| 111 | + context.nodeRegistry.clearPresenceActivity(activity), |
| 112 | + logGateway: { warn: context.logGateway.warn }, |
| 113 | + }; |
| 114 | + const result = await handleNodeEvent( |
| 115 | + nodeContext, |
| 116 | + nodeId, |
| 117 | + { |
| 118 | + event: p.event, |
| 119 | + payloadJSON, |
| 120 | + }, |
| 121 | + { |
| 122 | + connId: client?.connId, |
| 123 | + deviceId: client?.connect?.device?.id, |
| 124 | + pairingGeneration: eventPairingGeneration |
| 125 | + ? { nodeId, key: eventPairingGeneration } |
| 126 | + : undefined, |
| 127 | + presenceAllowed, |
| 128 | + isConnectionCurrent: isEventConnectionCurrent, |
| 129 | + resolveApnsRegistrationGeneration: async () => { |
| 130 | + if (!apnsGeneration || !client?.connId) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + const before = resolveDispatchableNodeSession( |
| 134 | + context.nodeRegistry.getForPairingGeneration(nodeId, apnsGeneration.key), |
| 135 | + ); |
| 136 | + if (!before || before.connId !== client.connId) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + if (!(await isNodePairingGenerationCurrent(apnsGeneration))) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + const after = resolveDispatchableNodeSession( |
| 143 | + context.nodeRegistry.getForPairingGeneration(nodeId, apnsGeneration.key), |
| 144 | + ); |
| 145 | + return after?.connId === client.connId ? apnsGeneration.key : null; |
| 146 | + }, |
| 147 | + }, |
| 148 | + ); |
| 149 | + if (result?.reason === "pairing_changed") { |
| 150 | + respondPairingChanged(respond); |
| 151 | + return; |
| 152 | + } |
| 153 | + respond(true, result ?? { ok: true }, undefined); |
| 154 | + }); |
| 155 | + }, |
| 156 | +}; |
0 commit comments