MCPcopy Create free account
hub / github.com/RioArisk/claudecode_api_RemoteControl / maybeAttachHookSession

Function maybeAttachHookSession

lib/transcript.js:148–194  ·  view source on GitHub ↗
(data, source)

Source from the content-addressed store, hash-verified

146}
147
148function maybeAttachHookSession(data, source) {
149 const target = resolveHookTranscript(data);
150 if (!target) return;
151 let hookSource = null;
152
153 if (state.currentSessionId === target.sessionId && state.transcriptPath &&
154 normalizeFsPath(state.transcriptPath) === normalizeFsPath(target.full)) {
155 return;
156 }
157
158 const targetHasContent = fileLooksLikeTranscript(target.full);
159
160 if (source === 'session-start') {
161 hookSource = data.source;
162 if (hookSource === 'clear' || hookSource === 'resume') {
163 log(`Deterministic session-start (hookSource=${hookSource}): ${target.sessionId}`);
164 } else {
165 if (state.currentSessionId && !state.expectingSwitch) {
166 const currentHasContent = state.transcriptPath && fileLooksLikeTranscript(state.transcriptPath);
167 if (!targetHasContent || currentHasContent) {
168 if (state.currentSessionId !== target.sessionId) {
169 state.pendingSwitchTarget = { ...target, seenAt: Date.now(), source };
170 log(`Queued pending session-start: ${target.sessionId} (current=${state.currentSessionId} currentHasContent=${currentHasContent} targetHasContent=${targetHasContent})`);
171 }
172 log(`Ignored session-start: ${target.sessionId} (current=${state.currentSessionId} currentHasContent=${currentHasContent} targetHasContent=${targetHasContent})`);
173 return;
174 }
175 }
176 }
177 } else if (source === 'pre-tool-use') {
178 if (state.currentSessionId && state.currentSessionId !== target.sessionId && !targetHasContent) {
179 log(`Ignored pre-tool-use: ${target.sessionId} (no conversation content)`);
180 return;
181 }
182 } else {
183 if (state.currentSessionId && state.currentSessionId !== target.sessionId && !state.expectingSwitch) {
184 log(`Ignored hook session from ${source}: ${target.sessionId} (current=${state.currentSessionId})`);
185 return;
186 }
187 }
188
189 log(`Hook session attached from ${source}: ${target.sessionId}`);
190 attachTranscript({
191 full: target.full,
192 ignoreInitialClearCommand: source === 'session-start' && hookSource === 'clear',
193 }, 0);
194}
195
196function maybeAttachPendingSwitchTarget(reason, requireReady = true) {
197 if (!state.pendingSwitchTarget) return false;

Callers 1

createHttpServerFunction · 0.85

Calls 5

resolveHookTranscriptFunction · 0.85
normalizeFsPathFunction · 0.85
fileLooksLikeTranscriptFunction · 0.85
logFunction · 0.85
attachTranscriptFunction · 0.85

Tested by

no test coverage detected