@@ -251,7 +251,7 @@ export class Stagehand {
251
251
}
252
252
}
253
253
254
- async observe ( observation : string , modelName ?: string ) : Promise < string | null > {
254
+ async observe ( observation : string , modelName ?: string ) : Promise < { success : boolean ; result ?: string ; error ?: string } > {
255
255
this . log ( {
256
256
category : "observation" ,
257
257
message : `starting observation: ${ observation } ` ,
@@ -278,7 +278,7 @@ export class Stagehand {
278
278
message : `no element found for ${ observation } ` ,
279
279
level : 1
280
280
} ) ;
281
- return null ;
281
+ return { success : false , error : `No element found for observation: ${ observation } ` } ;
282
282
}
283
283
284
284
this . log ( {
@@ -299,13 +299,18 @@ export class Stagehand {
299
299
// the locator string found by the LLM might resolve to multiple places in the DOM
300
300
const firstLocator = this . page . locator ( locatorString ) . first ( ) ;
301
301
302
- await expect ( firstLocator ) . toBeAttached ( ) ;
302
+ try {
303
+ await expect ( firstLocator ) . toBeAttached ( ) ;
304
+ } catch ( error ) {
305
+ return { success : false , error : `Element found but not attached: ${ error . message } ` } ;
306
+ }
307
+
303
308
const observationId = await this . recordObservation (
304
309
observation ,
305
310
locatorString
306
311
) ;
307
312
308
- return observationId ;
313
+ return { success : true , result : observationId } ;
309
314
}
310
315
async ask ( question : string , modelName ?: string ) : Promise < string | null > {
311
316
return ask ( {
@@ -344,7 +349,7 @@ export class Stagehand {
344
349
steps ?: string ;
345
350
chunksSeen ?: Array < number > ;
346
351
modelName ?: string ;
347
- } ) : Promise < void > {
352
+ } ) : Promise < { success : boolean ; error ?: string } > {
348
353
this . log ( {
349
354
category : "action" ,
350
355
message : `taking action: ${ action } ` ,
@@ -390,7 +395,7 @@ export class Stagehand {
390
395
level : 1
391
396
} ) ;
392
397
this . recordAction ( action , null ) ;
393
- return ;
398
+ return { success : false , error : "Action could not be performed after checking all chunks" } ;
394
399
}
395
400
}
396
401
@@ -416,22 +421,24 @@ export class Stagehand {
416
421
} ) ;
417
422
const locator = await this . page . locator ( `xpath=${ path } ` ) . first ( ) ;
418
423
419
- if ( method === 'scrollIntoView' ) { // this is not a native playwright function
424
+ if ( method === 'scrollIntoView' ) {
420
425
await locator . evaluate ( ( element ) => {
421
426
element . scrollIntoView ( { behavior : 'smooth' , block : 'center' } ) ;
422
427
} ) ;
423
428
} else if ( typeof locator [ method as keyof typeof locator ] === "function" ) {
424
-
425
- const isLink = await locator . evaluate ( ( element ) => {
426
- return element . tagName . toLowerCase ( ) === 'a' && element . hasAttribute ( 'href' ) ;
427
- } ) ;
428
-
429
- // Perform the action
430
- //@ts -ignore playwright's TS does not think this is valid, but we proved it with the check above
431
- await locator [ method ] ( ...args ) ;
429
+ try {
430
+ //@ts -ignore playwright's TS does not think this is valid, but we proved it with the check above
431
+ await locator [ method ] ( ...args ) ;
432
+ } catch ( error ) {
433
+ return { success : false , error : `Failed to perform ${ method } on element: ${ error . message } ` } ;
434
+ }
432
435
433
436
// Check if a new page was created, but only if the method is 'click'
434
437
if ( method === 'click' ) {
438
+ const isLink = await locator . evaluate ( ( element ) => {
439
+ return element . tagName . toLowerCase ( ) === 'a' && element . hasAttribute ( 'href' ) ;
440
+ } ) ;
441
+
435
442
if ( isLink ) {
436
443
// Create a promise that resolves when a new page is created
437
444
console . log ( "clicking link" ) ;
@@ -452,7 +459,7 @@ export class Stagehand {
452
459
}
453
460
}
454
461
} else {
455
- throw new Error ( `stagehand: chosen method ${ method } is invalid` ) ;
462
+ return { success : false , error : `Invalid method: ${ method } ` } ;
456
463
}
457
464
458
465
if ( ! response . completed ) {
@@ -468,6 +475,8 @@ export class Stagehand {
468
475
modelName,
469
476
} ) ;
470
477
}
478
+
479
+ return { success : true } ;
471
480
}
472
481
setPage ( page : Page ) {
473
482
this . page = page ;
0 commit comments