MCPcopy
hub / github.com/codeaashu/claude-code / useRemoteSession

Function useRemoteSession

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

Source from the content-addressed store, hash-verified

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