@@ -10,7 +10,7 @@ import { extractNodeText, findNearestHittableAncestor } from '../snapshot-proces
1010import { parseTimeout } from './parse-utils.ts' ;
1111import { readTextForNode } from './interaction-read.ts' ;
1212import { captureSnapshot } from './snapshot-capture.ts' ;
13- import { errorResponse } from './response.ts' ;
13+ import { errorResponse , sessionNotFoundResponse } from './response.ts' ;
1414import { getActiveAndroidSnapshotFreshness } from '../android-snapshot-freshness.ts' ;
1515
1616type FindContext = {
@@ -60,7 +60,7 @@ export async function handleFindCommands(params: {
6060 const isReadOnly =
6161 action === 'exists' || action === 'wait' || action === 'get_text' || action === 'get_attrs' ;
6262 if ( ! session && ! isReadOnly ) {
63- return errorResponse ( 'SESSION_NOT_FOUND' , 'No active session. Run open first.' ) ;
63+ return sessionNotFoundResponse ( ) ;
6464 }
6565 const device = session ?. device ?? ( await resolveTargetDevice ( req . flags ?? { } ) ) ;
6666 if ( ! session ) {
@@ -194,7 +194,7 @@ async function handleFindWait(
194194 }
195195 await new Promise ( ( resolve ) => setTimeout ( resolve , 300 ) ) ;
196196 }
197- return { ok : false , error : { code : 'COMMAND_FAILED' , message : 'find wait timed out' } } ;
197+ return errorResponse ( 'COMMAND_FAILED' , 'find wait timed out' ) ;
198198}
199199
200200async function handleFindExists ( ctx : FindContext ) : Promise < DaemonResponse > {
@@ -280,7 +280,7 @@ async function handleFindFill(
280280) : Promise < DaemonResponse > {
281281 const { req, sessionName, sessionStore, session, invoke, command } = ctx ;
282282 if ( ! value ) {
283- return { ok : false , error : { code : 'INVALID_ARGS' , message : 'find fill requires text' } } ;
283+ return errorResponse ( 'INVALID_ARGS' , 'find fill requires text' ) ;
284284 }
285285 const response = await invoke ( {
286286 token : req . token ,
@@ -305,10 +305,7 @@ async function handleFindFocus(ctx: FindContext, match: ResolvedMatch): Promise<
305305 const { req, sessionStore, session, device, command, logPath } = ctx ;
306306 const coords = match . node . rect ? centerOfRect ( match . node . rect ) : null ;
307307 if ( ! coords ) {
308- return {
309- ok : false ,
310- error : { code : 'COMMAND_FAILED' , message : 'matched element has no bounds' } ,
311- } ;
308+ return errorResponse ( 'COMMAND_FAILED' , 'matched element has no bounds' ) ;
312309 }
313310 const response = await dispatchCommand (
314311 device ,
@@ -337,14 +334,11 @@ async function handleFindType(
337334) : Promise < DaemonResponse > {
338335 const { req, sessionStore, session, device, command, logPath } = ctx ;
339336 if ( ! value ) {
340- return { ok : false , error : { code : 'INVALID_ARGS' , message : 'find type requires text' } } ;
337+ return errorResponse ( 'INVALID_ARGS' , 'find type requires text' ) ;
341338 }
342339 const coords = match . node . rect ? centerOfRect ( match . node . rect ) : null ;
343340 if ( ! coords ) {
344- return {
345- ok : false ,
346- error : { code : 'COMMAND_FAILED' , message : 'matched element has no bounds' } ,
347- } ;
341+ return errorResponse ( 'COMMAND_FAILED' , 'matched element has no bounds' ) ;
348342 }
349343 await dispatchCommand ( device , 'focus' , [ String ( coords . x ) , String ( coords . y ) ] , req . flags ?. out , {
350344 ...contextFromFlags ( logPath , req . flags , session ?. appBundleId , session ?. trace ?. outPath ) ,
@@ -375,19 +369,16 @@ function buildAmbiguousMatchError(
375369 extractNodeText ( candidate ) || candidate . label || candidate . identifier || candidate . type || '' ;
376370 return `@${ candidate . ref } ${ label ? `(${ label } )` : '' } ` ;
377371 } ) ;
378- return {
379- ok : false ,
380- error : {
381- code : 'AMBIGUOUS_MATCH' ,
382- message : `find matched ${ matches . length } elements for ${ locator } "${ query } ". Use a more specific locator or selector.` ,
383- details : {
384- locator,
385- query,
386- matches : matches . length ,
387- candidates,
388- } ,
372+ return errorResponse (
373+ 'AMBIGUOUS_MATCH' ,
374+ `find matched ${ matches . length } elements for ${ locator } "${ query } ". Use a more specific locator or selector.` ,
375+ {
376+ locator,
377+ query,
378+ matches : matches . length ,
379+ candidates,
389380 } ,
390- } ;
381+ ) ;
391382}
392383
393384type FindAction =
0 commit comments