11import { tool } from "ai" ;
22import { z } from "zod/v3" ;
33import { StagehandPage } from "../../StagehandPage" ;
4-
4+ import { buildActObservePrompt } from "../../prompt" ;
5+ import { SupportedPlaywrightAction } from "@/types/act" ;
56export const createActTool = (
67 stagehandPage : StagehandPage ,
78 executionModel ?: string ,
@@ -19,37 +20,91 @@ export const createActTool = (
1920 } ) ,
2021 execute : async ( { action } ) => {
2122 try {
22- let result ;
23- if ( executionModel ) {
24- result = await stagehandPage . page . act ( {
25- action,
26- modelName : executionModel ,
27- } ) ;
28- } else {
29- result = await stagehandPage . page . act ( action ) ;
23+ const builtPrompt = buildActObservePrompt (
24+ action ,
25+ Object . values ( SupportedPlaywrightAction ) ,
26+ ) ;
27+
28+ const observeOptions = executionModel
29+ ? {
30+ instruction : builtPrompt ,
31+ modelName : executionModel ,
32+ }
33+ : {
34+ instruction : builtPrompt ,
35+ } ;
36+
37+ const observeResults = await stagehandPage . page . observe ( observeOptions ) ;
38+
39+ if ( ! observeResults || observeResults . length === 0 ) {
40+ return {
41+ success : false ,
42+ error : "No observable actions found for the given instruction" ,
43+ } ;
3044 }
31- const isIframeAction = result . action === "an iframe" ;
45+
46+ const observeResult = observeResults [ 0 ] ;
47+
48+ const isIframeAction = observeResult . description === "an iframe" ;
3249
3350 if ( isIframeAction ) {
34- const fallback = await stagehandPage . page . act (
35- executionModel
36- ? { action, modelName : executionModel , iframes : true }
37- : { action, iframes : true } ,
38- ) ;
51+ const iframeObserveOptions = executionModel
52+ ? {
53+ instruction : builtPrompt ,
54+ modelName : executionModel ,
55+ iframes : true ,
56+ }
57+ : {
58+ instruction : builtPrompt ,
59+ iframes : true ,
60+ } ;
61+
62+ const iframeObserveResults =
63+ await stagehandPage . page . observe ( iframeObserveOptions ) ;
64+
65+ if ( ! iframeObserveResults || iframeObserveResults . length === 0 ) {
66+ return {
67+ success : false ,
68+ error : "No observable actions found within iframe context" ,
69+ isIframe : true ,
70+ } ;
71+ }
72+
73+ const iframeObserveResult = iframeObserveResults [ 0 ] ;
74+ const fallback = await stagehandPage . page . act ( iframeObserveResult ) ;
75+
3976 return {
4077 success : fallback . success ,
4178 action : fallback . action ,
4279 isIframe : true ,
80+ playwrightArguments : {
81+ description : iframeObserveResult . description ,
82+ method : iframeObserveResult . method ,
83+ arguments : iframeObserveResult . arguments ,
84+ selector : iframeObserveResult . selector ,
85+ } ,
4386 } ;
4487 }
4588
89+ const result = await stagehandPage . page . act ( observeResult ) ;
90+ const playwrightArguments = {
91+ description : observeResult . description ,
92+ method : observeResult . method ,
93+ arguments : observeResult . arguments ,
94+ selector : observeResult . selector ,
95+ } ;
96+
4697 return {
4798 success : result . success ,
4899 action : result . action ,
49100 isIframe : false ,
101+ playwrightArguments,
50102 } ;
51103 } catch ( error ) {
52- return { success : false , error : error . message } ;
104+ return {
105+ success : false ,
106+ error : error . message ,
107+ } ;
53108 }
54109 } ,
55110 } ) ;
0 commit comments