1
1
import { test as base } from '@playwright/test' ;
2
2
import { AutoConfig } from './types' ;
3
3
import { sessionManager , context } from './browser' ;
4
- import { createReactAgent } from " @langchain/langgraph/prebuilt" ;
5
- import { HumanMessage } from " @langchain/core/messages" ;
4
+ import { createReactAgent } from ' @langchain/langgraph/prebuilt' ;
5
+ import { HumanMessage } from ' @langchain/core/messages' ;
6
6
import { createLLMModel } from './llm' ;
7
7
import {
8
- browser_click , browser_type , browser_get_text , browser_navigate , browser_snapshot ,
9
- browser_hover , browser_drag , browser_select_option , browser_take_screenshot ,
10
- browser_go_back , browser_wait , browser_press_key , browser_save_pdf , browser_choose_file ,
11
- browser_go_forward , browser_assert
8
+ browser_click ,
9
+ browser_type ,
10
+ browser_get_text ,
11
+ browser_navigate ,
12
+ browser_snapshot ,
13
+ browser_hover ,
14
+ browser_drag ,
15
+ browser_select_option ,
16
+ browser_take_screenshot ,
17
+ browser_go_back ,
18
+ browser_wait ,
19
+ browser_press_key ,
20
+ browser_save_pdf ,
21
+ browser_choose_file ,
22
+ browser_go_forward ,
23
+ browser_assert ,
12
24
} from './tools' ;
13
25
14
26
// Extend base test to automatically track page
15
27
export const test = base . extend ( {
16
28
page : async ( { page } , use ) => {
17
29
sessionManager . setPage ( page ) ;
18
30
await use ( page ) ;
19
- }
31
+ } ,
20
32
} ) ;
21
33
22
34
// Initialize the LangChain agent with more detailed instructions
23
35
const initializeAgent = ( ) => {
24
36
const model = createLLMModel ( ) ;
25
37
26
- const prompt =
27
- `You are a web automation assistant. When given a natural language instruction:
38
+ const prompt = `You are a web automation assistant. When given a natural language instruction:
28
39
- Always call the snapshot tool first to analyze the page structure and elements, so you can understand the context ad the elements available on the page to perform the requested action
29
40
- For "get" or "get text" instructions, use the getText tool to retrieve content
30
41
- For "click" instructions, use the click tool to interact with elements
@@ -46,32 +57,43 @@ const initializeAgent = () => {
46
57
const agent = createReactAgent ( {
47
58
llm : model ,
48
59
tools : [
49
- browser_click , browser_type , browser_get_text , browser_navigate , browser_snapshot ,
50
- browser_hover , browser_drag , browser_select_option , browser_take_screenshot ,
51
- browser_go_back , browser_wait , browser_press_key , browser_save_pdf , browser_choose_file , browser_assert ,
52
- browser_go_forward
60
+ browser_click ,
61
+ browser_type ,
62
+ browser_get_text ,
63
+ browser_navigate ,
64
+ browser_snapshot ,
65
+ browser_hover ,
66
+ browser_drag ,
67
+ browser_select_option ,
68
+ browser_take_screenshot ,
69
+ browser_go_back ,
70
+ browser_wait ,
71
+ browser_press_key ,
72
+ browser_save_pdf ,
73
+ browser_choose_file ,
74
+ browser_assert ,
75
+ browser_go_forward ,
53
76
] ,
54
- stateModifier : prompt
77
+ stateModifier : prompt ,
55
78
} ) ;
56
79
57
80
return { agent } ;
58
81
} ;
59
82
60
83
// Main auto function that processes instructions
61
- export async function auto ( instruction : string , config ?: AutoConfig ) : Promise < any > {
84
+ export async function auto (
85
+ instruction : string ,
86
+ config ?: AutoConfig ,
87
+ ) : Promise < any > {
62
88
console . log ( `[Auto] Processing instruction: "${ instruction } "` ) ;
63
89
64
- if ( config ?. page )
65
- {
90
+ if ( config ?. page ) {
66
91
sessionManager . setPage ( config . page ) ;
67
92
console . log ( `[Auto] Page set from config` ) ;
68
- } else
69
- {
70
- try
71
- {
93
+ } else {
94
+ try {
72
95
sessionManager . getPage ( ) ;
73
- } catch
74
- {
96
+ } catch {
75
97
// In standalone mode, create a new page
76
98
console . log ( `[Auto] No existing page, creating new page` ) ;
77
99
await context . createPage ( ) ;
@@ -82,19 +104,17 @@ export async function auto(instruction: string, config?: AutoConfig): Promise<an
82
104
console . log ( `[Auto] Creating agent for instruction` ) ;
83
105
const { agent } = initializeAgent ( ) ;
84
106
const result = await agent . invoke ( {
85
- messages : [ new HumanMessage ( instruction ) ]
107
+ messages : [ new HumanMessage ( instruction ) ] ,
86
108
} ) ;
87
109
88
- console . log ( " Agent result:" , result ) ;
110
+ console . log ( ' Agent result:' , result ) ;
89
111
// Process agent result
90
112
const response = result . messages ?. [ - 1 ] ?. content ;
91
113
console . log ( `[Auto] Agent response:` , response ) ;
92
114
93
- if ( typeof response === 'string' )
94
- {
115
+ if ( typeof response === 'string' ) {
95
116
// If it's a success message, return null to match original behavior
96
- if ( response . startsWith ( 'Successfully' ) )
97
- {
117
+ if ( response . startsWith ( 'Successfully' ) ) {
98
118
console . log ( `[Auto] Detected success message, returning null` ) ;
99
119
return null ;
100
120
}
0 commit comments