@@ -18,7 +18,8 @@ import {
1818} from '../../core/interaction-targeting.ts' ;
1919import { isSnapshotNodeInteractionBlocked } from '../../utils/snapshot-occlusion.ts' ;
2020import { readTextForNode } from './interaction-read.ts' ;
21- import { errorResponse } from './response.ts' ;
21+ import { errorResponse , noActiveSessionError } from './response.ts' ;
22+ import { recordSessionAction } from './handler-utils.ts' ;
2223import { stripInternalInteractionFlags } from '../interaction-outcome-policy.ts' ;
2324import { dispatchFindReadOnlyViaRuntime } from '../selector-runtime.ts' ;
2425import { createSelectorCaptureRuntime } from '../selector-capture-runtime.ts' ;
@@ -88,7 +89,7 @@ export async function handleFindCommands(params: {
8889 const session = sessionStore . get ( sessionName ) ;
8990 const isReadOnly = isReadOnlyFindAction ( action ) ;
9091 if ( ! session && ! isReadOnly ) {
91- return errorResponse ( 'SESSION_NOT_FOUND' , 'No active session. Run open first.' ) ;
92+ return noActiveSessionError ( ) ;
9293 }
9394 const device = session ?. device ?? ( await resolveTargetDevice ( req . flags ?? { } ) ) ;
9495 if ( ! session ) {
@@ -428,14 +429,14 @@ async function handleFindWait(
428429 const match = findBestMatchesByLocator ( nodes , locator , query , { requireRect : false } )
429430 . matches [ 0 ] ;
430431 if ( match ) {
431- if ( session ) {
432- sessionStore . recordAction ( session , {
433- command ,
434- positionals : req . positionals ?? [ ] ,
435- flags : publicFlags ,
436- result : { found : true , waitedMs : Date . now ( ) - start } ,
437- } ) ;
438- }
432+ recordSessionAction (
433+ sessionStore ,
434+ session ,
435+ req ,
436+ command ,
437+ { found : true , waitedMs : Date . now ( ) - start } ,
438+ { flags : publicFlags } ,
439+ ) ;
439440 return { ok : true , data : { found : true , waitedMs : Date . now ( ) - start } } ;
440441 }
441442 await sleep ( 300 ) ;
@@ -446,14 +447,7 @@ async function handleFindWait(
446447
447448async function handleFindExists ( ctx : FindContext ) : Promise < DaemonResponse > {
448449 const { req, sessionStore, session, command, publicFlags } = ctx ;
449- if ( session ) {
450- sessionStore . recordAction ( session , {
451- command,
452- positionals : req . positionals ?? [ ] ,
453- flags : publicFlags ,
454- result : { found : true } ,
455- } ) ;
456- }
450+ recordSessionAction ( sessionStore , session , req , command , { found : true } , { flags : publicFlags } ) ;
457451 return { ok : true , data : { found : true } } ;
458452}
459453
@@ -469,27 +463,27 @@ async function handleFindGetText(ctx: FindContext, match: ResolvedMatch): Promis
469463 contextFromFlags : ( flags , appBundleId , traceLogPath ) =>
470464 contextFromFlags ( logPath , flags , appBundleId , traceLogPath ) ,
471465 } ) ;
472- if ( session ) {
473- sessionStore . recordAction ( session , {
474- command ,
475- positionals : req . positionals ?? [ ] ,
476- flags : publicFlags ,
477- result : { ref : match . ref , action : 'get text' , text } ,
478- } ) ;
479- }
466+ recordSessionAction (
467+ sessionStore ,
468+ session ,
469+ req ,
470+ command ,
471+ { ref : match . ref , action : 'get text' , text } ,
472+ { flags : publicFlags } ,
473+ ) ;
480474 return { ok : true , data : { ref : match . ref , text, node : match . node } } ;
481475}
482476
483477async function handleFindGetAttrs ( ctx : FindContext , match : ResolvedMatch ) : Promise < DaemonResponse > {
484478 const { req, sessionStore, session, command, publicFlags } = ctx ;
485- if ( session ) {
486- sessionStore . recordAction ( session , {
487- command ,
488- positionals : req . positionals ?? [ ] ,
489- flags : publicFlags ,
490- result : { ref : match . ref , action : 'get attrs' } ,
491- } ) ;
492- }
479+ recordSessionAction (
480+ sessionStore ,
481+ session ,
482+ req ,
483+ command ,
484+ { ref : match . ref , action : 'get attrs' } ,
485+ { flags : publicFlags } ,
486+ ) ;
493487 return { ok : true , data : { ref : match . ref , node : match . node } } ;
494488}
495489
@@ -514,14 +508,14 @@ async function handleFindClick(ctx: FindContext, match: ResolvedMatch): Promise<
514508 matchData . x = matchCoords . x ;
515509 matchData . y = matchCoords . y ;
516510 }
517- if ( session ) {
518- sessionStore . recordAction ( session , {
519- command ,
520- positionals : req . positionals ?? [ ] ,
521- flags : publicFlags ,
522- result : { ref : match . ref , action : 'click' , locator, query } ,
523- } ) ;
524- }
511+ recordSessionAction (
512+ sessionStore ,
513+ session ,
514+ req ,
515+ command ,
516+ { ref : match . ref , action : 'click' , locator, query } ,
517+ { flags : publicFlags } ,
518+ ) ;
525519 return { ok : true , data : matchData } ;
526520}
527521
@@ -542,14 +536,14 @@ async function handleFindFill(
542536 flags : match . actionFlags ,
543537 } ) ;
544538 if ( ! response . ok ) return response ;
545- if ( session ) {
546- sessionStore . recordAction ( session , {
547- command ,
548- positionals : req . positionals ?? [ ] ,
549- flags : publicFlags ,
550- result : { ref : match . ref , action : 'fill' } ,
551- } ) ;
552- }
539+ recordSessionAction (
540+ sessionStore ,
541+ session ,
542+ req ,
543+ command ,
544+ { ref : match . ref , action : 'fill' } ,
545+ { flags : publicFlags } ,
546+ ) ;
553547 return response ;
554548}
555549
@@ -617,14 +611,14 @@ function rejectCoveredFindMatch(match: ResolvedMatch, interaction: string): Daem
617611
618612function recordFindAction ( ctx : FindContext , match : ResolvedMatch , action : string ) : void {
619613 const { req, sessionStore, session, command, publicFlags } = ctx ;
620- if ( session ) {
621- sessionStore . recordAction ( session , {
622- command ,
623- positionals : req . positionals ?? [ ] ,
624- flags : publicFlags ,
625- result : { ref : match . ref , action } ,
626- } ) ;
627- }
614+ recordSessionAction (
615+ sessionStore ,
616+ session ,
617+ req ,
618+ command ,
619+ { ref : match . ref , action } ,
620+ { flags : publicFlags } ,
621+ ) ;
628622}
629623
630624// --- Helpers ---
0 commit comments