MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / useRemoteSession

Function useRemoteSession

source code/hooks/useRemoteSession.ts:78–607  ·  view source on GitHub ↗
({
  config,
  setMessages,
  setIsLoading,
  onInit,
  setToolUseConfirmQueue,
  tools,
  setStreamingToolUses,
  setStreamMode,
  setInProgressToolUseIDs,
}: UseRemoteSessionProps)

Source from the content-addressed store, hash-verified

76 * - Permission request/response flow via existing ToolUseConfirm queue
77 */
78export function useRemoteSession({
79 config,
80 setMessages,
81 setIsLoading,
82 onInit,
83 setToolUseConfirmQueue,
84 tools,
85 setStreamingToolUses,
86 setStreamMode,
87 setInProgressToolUseIDs,
88}: UseRemoteSessionProps): UseRemoteSessionResult {
89 const isRemoteMode = !!config
90
91 const setAppState = useSetAppState()
92 const setConnStatus = useCallback(
93 (s: AppState['remoteConnectionStatus']) =>
94 setAppState(prev =>
95 prev.remoteConnectionStatus === s
96 ? prev
97 : { ...prev, remoteConnectionStatus: s },
98 ),
99 [setAppState],
100 )
101
102 // Event-sourced count of subagents running inside the remote daemon child.
103 // The viewer's own AppState.tasks is empty — tasks live in a different
104 // process. task_started/task_notification reach us via the bridge WS.
105 const runningTaskIdsRef = useRef(new Set<string>())
106 const writeTaskCount = useCallback(() => {
107 const n = runningTaskIdsRef.current.size
108 setAppState(prev =>
109 prev.remoteBackgroundTaskCount === n
110 ? prev
111 : { ...prev, remoteBackgroundTaskCount: n },
112 )
113 }, [setAppState])
114
115 // Timer for detecting stuck sessions
116 const responseTimeoutRef = useRef<NodeJS.Timeout | null>(null)
117
118 // Track whether the remote session is compacting. During compaction the
119 // CLI worker is busy with an API call and won't emit messages for a while;
120 // use a longer timeout and suppress spurious "unresponsive" warnings.
121 const isCompactingRef = useRef(false)
122
123 const managerRef = useRef<RemoteSessionManager | null>(null)
124
125 // Track whether we've already updated the session title (for no-initial-prompt sessions)
126 const hasUpdatedTitleRef = useRef(false)
127
128 // UUIDs of user messages we POSTed locally — the WS echoes them back and
129 // we must filter them out when convertUserTextMessages is on, or the viewer
130 // sees every typed message twice (once from local createUserMessage, once
131 // from the echo). A single POST can echo MULTIPLE times with the same uuid:
132 // the server may broadcast the POST directly to /subscribe, AND the worker
133 // (cowork desktop / CLI daemon) echoes it again on its write path. A
134 // delete-on-first-match Set would let the second echo through — use a
135 // bounded ring instead. Cap is generous: users don't type 50 messages

Callers 1

REPLFunction · 0.85

Calls 15

connectMethod · 0.95
disconnectMethod · 0.95
sendMessageMethod · 0.95
reconnectMethod · 0.95
useSetAppStateFunction · 0.85
logForDebuggingFunction · 0.85
onInitFunction · 0.85
isSessionEndMessageFunction · 0.85
convertSDKMessageFunction · 0.85
handleMessageFromStreamFunction · 0.85
findToolByNameFunction · 0.85
createToolStubFunction · 0.85

Tested by

no test coverage detected