MCPcopy Create free account
hub / github.com/Roy3838/Observer / useRemoteControl

Function useRemoteControl

app/src/mcp/useRemoteControl.ts:70–246  ·  view source on GitHub ↗
(
  mcp: Pick<UseMCPReturn, 'send' | 'isRunning' | 'subscribeTurn'>,
  getToken: TokenProvider,
)

Source from the content-addressed store, hash-verified

68}
69
70export function useRemoteControl(
71 mcp: Pick<UseMCPReturn, 'send' | 'isRunning' | 'subscribeTurn'>,
72 getToken: TokenProvider,
73): RemoteControlState {
74 const getTokenRef = useRef(getToken);
75 getTokenRef.current = getToken;
76
77 const queue = useRef<RemoteMessage[]>([]);
78 const draining = useRef(false);
79 // The inbox loop that owns the `listening` flag.
80 const listenerRef = useRef<AbortController | null>(null);
81 // Bumped to re-run the drain effect when a message arrives or a remote run finishes.
82 const [wake, setWake] = useState(0);
83
84 const [mirror, setMirrorState] = useState<Record<RemoteChannel, boolean>>(() => ({
85 whatsapp: SensorSettings.getMirror('whatsapp') === 'on',
86 telegram: SensorSettings.getMirror('telegram') === 'on',
87 }));
88 const [status, setStatus] = useState<Record<RemoteChannel, RemoteStatus | null>>({ whatsapp: null, telegram: null });
89 const [linked, setLinked] = useState<RemoteSessions>({});
90 // The one gate: linked and mirrored. As JSON so the listener only restarts on a real change.
91 const sessionKey = JSON.stringify(Object.fromEntries(
92 CHANNELS.filter(c => linked[c] && mirror[c]).map(c => [c, linked[c]]),
93 ));
94 // The last answer per channel, and the code it was for: a failed status fetch falls back to it.
95 const lastStatus = useRef<Partial<Record<RemoteChannel, { code: string; status: RemoteStatus }>>>({});
96 const [listening, setListening] = useState(false);
97 const [lastMessageAt, setLastMessageAt] = useState<number | null>(null);
98
99 const setMirror = useCallback((channel: RemoteChannel, on: boolean) => {
100 // Off: its polls are about to stop for good, so turnOffMirror disarms first, or the
101 // silence reads as an alarm. It fires MIRROR_CHANGED_EVENT, picked up below.
102 if (!on) {
103 turnOffMirror(channel, () => getTokenRef.current());
104 return;
105 }
106 SensorSettings.setMirror(channel, on);
107 setMirrorState(prev => ({ ...prev, [channel]: on }));
108 }, []);
109
110 // A mirror turned off elsewhere (the MCP's ask_user_info mirror=false), already disarmed.
111 useEffect(() => {
112 const sync = () => setMirrorState({
113 whatsapp: SensorSettings.getMirror('whatsapp') === 'on',
114 telegram: SensorSettings.getMirror('telegram') === 'on',
115 });
116 window.addEventListener(MIRROR_CHANGED_EVENT, sync);
117 return () => window.removeEventListener(MIRROR_CHANGED_EVENT, sync);
118 }, []);
119
120 // Only mirrored phones/chats are polled, all in one request: most tabs have none and so hold
121 // no connection open at all.
122 useEffect(() => {
123 const sessions = JSON.parse(sessionKey) as RemoteSessions;
124 if (Object.keys(sessions).length === 0) {
125 setListening(false);
126 return;
127 }

Callers 1

MCPProviderFunction · 0.90

Calls 13

turnOffMirrorFunction · 0.90
listenInboxFunction · 0.90
remotePromptFunction · 0.90
forwardFunction · 0.85
userEchoFunction · 0.85
getMirrorMethod · 0.80
filterMethod · 0.80
setMirrorMethod · 0.80
infoMethod · 0.80
pushMethod · 0.80
errorMethod · 0.80
checkFunction · 0.70

Tested by

no test coverage detected