@@ -68,7 +68,6 @@ export interface RequestHelpDeps {
6868interface ActiveHelpRequest {
6969 ctx : SessionContext ;
7070 requestId : string ;
71- subjectTabId : number ;
7271 primaryTabId : number ;
7372 overlayTabIds : Set < number > ;
7473 prompt : string ;
@@ -126,8 +125,8 @@ function sleep(ms: number): Promise<void> {
126125 return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
127126}
128127
129- function helpRequestMessage ( help : ActiveHelpRequest ) : HelpRequestMessage {
130- const isPrimaryTab = help . subjectTabId === help . primaryTabId ;
128+ function helpRequestMessage ( help : ActiveHelpRequest , tabId : number ) : HelpRequestMessage {
129+ const isPrimaryTab = tabId === help . primaryTabId ;
131130 return {
132131 type : HELP_REQUEST ,
133132 requestId : help . requestId ,
@@ -277,10 +276,10 @@ async function resolveHelpTargets(
277276}
278277
279278async function refreshHelpTargets ( help : ActiveHelpRequest ) : Promise < void > {
280- await clearRefTags ( help . deps . cdp , help . subjectTabId ) ;
279+ await clearRefTags ( help . deps . cdp , help . primaryTabId ) ;
281280 const { selectors, resolvedTargets } = await resolveHelpTargets (
282281 help . ctx ,
283- help . subjectTabId ,
282+ help . primaryTabId ,
284283 help . targets ,
285284 help . deps ,
286285 ) ;
@@ -289,14 +288,18 @@ async function refreshHelpTargets(help: ActiveHelpRequest): Promise<void> {
289288}
290289
291290async function cleanupHelp ( help : ActiveHelpRequest ) : Promise < void > {
292- await clearRefTags ( help . deps . cdp , help . subjectTabId ) ;
293291 if ( help . deps . notifications ) {
294292 await help . deps . notifications . clear ( help . notificationId ) . catch ( ( ) => { } ) ;
295293 }
296- const tabsToCancel = new Set ( [ help . subjectTabId , ...help . overlayTabIds ] ) ;
294+ const tabsToCancel = new Set ( [ help . primaryTabId , ...help . overlayTabIds ] ) ;
297295 await Promise . all (
298296 [ ...tabsToCancel ] . map ( ( tabId ) =>
299- help . deps . sendToTab ( tabId , { type : HELP_CANCEL , requestId : help . requestId } ) . catch ( ( ) => { } ) ,
297+ Promise . all ( [
298+ clearRefTags ( help . deps . cdp , tabId ) ,
299+ help . deps
300+ . sendToTab ( tabId , { type : HELP_CANCEL , requestId : help . requestId } )
301+ . catch ( ( ) => { } ) ,
302+ ] ) ,
300303 ) ,
301304 ) ;
302305}
@@ -312,15 +315,15 @@ function finishHelp(
312315 clearTimeout ( help . timeoutTimer ) ;
313316 if ( help . completionTimer ) clearInterval ( help . completionTimer ) ;
314317 if ( help . abortHandler ) help . deps . signal ?. removeEventListener ( "abort" , help . abortHandler ) ;
315- clearRearmTimer ( help . subjectTabId ) ;
318+ clearRearmTimer ( help . primaryTabId ) ;
316319 for ( const tabId of help . overlayTabIds ) clearRearmTimer ( tabId ) ;
317320 if ( notifyContent ) void cleanupHelp ( help ) ;
318321 help . resolve ( value ) ;
319322}
320323
321- function findHelpBySubjectTabId ( tabId : number ) : ActiveHelpRequest | null {
324+ function findHelpByKnownTabId ( tabId : number ) : ActiveHelpRequest | null {
322325 for ( const help of activeHelpRequests . values ( ) ) {
323- if ( ! help . settled && ( help . subjectTabId === tabId || help . overlayTabIds . has ( tabId ) ) ) {
326+ if ( ! help . settled && ( help . primaryTabId === tabId || help . overlayTabIds . has ( tabId ) ) ) {
324327 return help ;
325328 }
326329 }
@@ -331,7 +334,7 @@ async function findHelpForTab(
331334 tabId : number ,
332335 deps : RequestHelpDeps ,
333336) : Promise < ActiveHelpRequest | null > {
334- const direct = findHelpBySubjectTabId ( tabId ) ;
337+ const direct = findHelpByKnownTabId ( tabId ) ;
335338 if ( direct ) return direct ;
336339
337340 try {
@@ -356,30 +359,37 @@ function clearRearmTimer(tabId: number): void {
356359
357360async function sendCurrentHelpOverlay ( help : ActiveHelpRequest ) : Promise < HelpFinishMessage | null > {
358361 const legacyFinish = await sendHelpRequestWithAck (
359- help . subjectTabId ,
360- helpRequestMessage ( help ) ,
362+ help . primaryTabId ,
363+ helpRequestMessage ( help , help . primaryTabId ) ,
361364 help . deps ,
362365 ) ;
363- help . overlayTabIds . add ( help . subjectTabId ) ;
366+ help . overlayTabIds . add ( help . primaryTabId ) ;
364367 return legacyFinish ;
365368}
366369
367370async function refreshAndSendHelpOverlay (
368371 help : ActiveHelpRequest ,
372+ tabId : number ,
369373) : Promise < HelpFinishMessage | null > {
370- await refreshHelpTargets ( help ) ;
371- return sendCurrentHelpOverlay ( help ) ;
374+ if ( tabId === help . primaryTabId ) await refreshHelpTargets ( help ) ;
375+ const legacyFinish = await sendHelpRequestWithAck (
376+ tabId ,
377+ helpRequestMessage ( help , tabId ) ,
378+ help . deps ,
379+ ) ;
380+ help . overlayTabIds . add ( tabId ) ;
381+ return legacyFinish ;
372382}
373383
374- async function rearmHelp ( help : ActiveHelpRequest ) : Promise < boolean > {
384+ async function rearmHelp ( help : ActiveHelpRequest , tabId : number ) : Promise < boolean > {
375385 for ( let attempt = 0 ; attempt < HELP_REARM_MAX_ATTEMPTS ; attempt += 1 ) {
376386 try {
377- const legacyFinish = await refreshAndSendHelpOverlay ( help ) ;
387+ const legacyFinish = await refreshAndSendHelpOverlay ( help , tabId ) ;
378388 if ( legacyFinish && ! help . settled ) {
379389 finishHelp ( help , {
380390 outcome : legacyFinish . outcome ,
381391 ...( legacyFinish . note ? { note : legacyFinish . note } : { } ) ,
382- tab_id : help . subjectTabId ,
392+ tab_id : help . primaryTabId ,
383393 resolved_targets : help . resolvedTargets ,
384394 } ) ;
385395 }
@@ -401,8 +411,7 @@ function scheduleRearmForTab(tabId: number, deps: RequestHelpDeps): void {
401411 void ( async ( ) => {
402412 const help = await findHelpForTab ( tabId , deps ) ;
403413 if ( ! help ) return ;
404- help . subjectTabId = tabId ;
405- await rearmHelp ( help ) ;
414+ await rearmHelp ( help , tabId ) ;
406415 } ) ( ) ;
407416 } , HELP_REARM_DEBOUNCE_MS ) ,
408417 ) ;
@@ -457,8 +466,7 @@ function attachHelpRuntimeListener(deps: RequestHelpDeps): () => void {
457466 sendResponse ( { active : false } ) ;
458467 return ;
459468 }
460- help . subjectTabId = tabId ;
461- await refreshHelpTargets ( help ) ;
469+ if ( tabId === help . primaryTabId ) await refreshHelpTargets ( help ) ;
462470 help . overlayTabIds . add ( tabId ) ;
463471 sendResponse ( {
464472 active : true ,
@@ -492,14 +500,13 @@ async function finishHelpFromContent(
492500 if ( tabId !== undefined ) {
493501 const match = await findHelpForTab ( tabId , deps ) ;
494502 if ( match !== help ) return ;
495- help . subjectTabId = tabId ;
496503 }
497504 finishHelp (
498505 help ,
499506 {
500507 outcome : message . outcome ,
501508 ...( message . note ? { note : message . note } : { } ) ,
502- tab_id : help . subjectTabId ,
509+ tab_id : help . primaryTabId ,
503510 resolved_targets : help . resolvedTargets ,
504511 } ,
505512 false ,
@@ -551,7 +558,7 @@ async function evaluateCompletionCondition(
551558 if ( condition . url_contains || condition . url_matches ) {
552559 let url = "" ;
553560 try {
554- const tab = await help . deps . tabsApi . get ( help . subjectTabId ) ;
561+ const tab = await help . deps . tabsApi . get ( help . primaryTabId ) ;
555562 url = tab . url ?? "" ;
556563 } catch {
557564 return false ;
@@ -587,7 +594,7 @@ async function evaluateCompletionCondition(
587594 })()` ;
588595 try {
589596 const result = await help . deps . cdp . send < { result ?: { value ?: boolean } } > (
590- help . subjectTabId ,
597+ help . primaryTabId ,
591598 "Runtime.evaluate" ,
592599 { expression, returnByValue : true } ,
593600 ) ;
@@ -639,7 +646,7 @@ function startCompletionPolling(help: ActiveHelpRequest): void {
639646 finishHelp ( help , {
640647 outcome : "completed" ,
641648 completed_by : "system" ,
642- tab_id : help . subjectTabId ,
649+ tab_id : help . primaryTabId ,
643650 resolved_targets : help . resolvedTargets ,
644651 } ) ;
645652 } ;
@@ -697,15 +704,14 @@ export async function handleRequestHelp(
697704 const timeoutTimer = setTimeout ( ( ) => {
698705 finishHelp ( help , {
699706 outcome : "timed_out" ,
700- tab_id : help . subjectTabId ,
707+ tab_id : help . primaryTabId ,
701708 resolved_targets : help . resolvedTargets ,
702709 } ) ;
703710 } , timeoutMs ) ;
704711
705712 const help : ActiveHelpRequest = {
706713 ctx,
707714 requestId,
708- subjectTabId : tabId ,
709715 primaryTabId : tabId ,
710716 overlayTabIds : new Set ( ) ,
711717 prompt : params . prompt ,
@@ -741,7 +747,7 @@ export async function handleRequestHelp(
741747 finishHelp ( help , {
742748 outcome : legacyFinish . outcome ,
743749 ...( legacyFinish . note ? { note : legacyFinish . note } : { } ) ,
744- tab_id : help . subjectTabId ,
750+ tab_id : help . primaryTabId ,
745751 resolved_targets : help . resolvedTargets ,
746752 } ) ;
747753 } )
0 commit comments