1
1
import { tool } from "ai" ;
2
2
import { z } from "zod/v3" ;
3
3
import { StagehandPage } from "../../StagehandPage" ;
4
-
4
+ import { buildActObservePrompt } from "../../prompt" ;
5
+ import { SupportedPlaywrightAction } from "@/types/act" ;
5
6
export const createActTool = (
6
7
stagehandPage : StagehandPage ,
7
8
executionModel ?: string ,
@@ -19,37 +20,91 @@ export const createActTool = (
19
20
} ) ,
20
21
execute : async ( { action } ) => {
21
22
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
+ } ;
30
44
}
31
- const isIframeAction = result . action === "an iframe" ;
45
+
46
+ const observeResult = observeResults [ 0 ] ;
47
+
48
+ const isIframeAction = observeResult . description === "an iframe" ;
32
49
33
50
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
+
39
76
return {
40
77
success : fallback . success ,
41
78
action : fallback . action ,
42
79
isIframe : true ,
80
+ playwrightArguments : {
81
+ description : iframeObserveResult . description ,
82
+ method : iframeObserveResult . method ,
83
+ arguments : iframeObserveResult . arguments ,
84
+ selector : iframeObserveResult . selector ,
85
+ } ,
43
86
} ;
44
87
}
45
88
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
+
46
97
return {
47
98
success : result . success ,
48
99
action : result . action ,
49
100
isIframe : false ,
101
+ playwrightArguments,
50
102
} ;
51
103
} catch ( error ) {
52
- return { success : false , error : error . message } ;
104
+ return {
105
+ success : false ,
106
+ error : error . message ,
107
+ } ;
53
108
}
54
109
} ,
55
110
} ) ;
0 commit comments