11import { test as base } from '@playwright/test' ;
22import { AutoConfig } from './types' ;
33import { 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' ;
66import { createLLMModel } from './llm' ;
77import {
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 ,
1224} from './tools' ;
1325
1426// Extend base test to automatically track page
1527export const test = base . extend ( {
1628 page : async ( { page } , use ) => {
1729 sessionManager . setPage ( page ) ;
1830 await use ( page ) ;
19- }
31+ } ,
2032} ) ;
2133
2234// Initialize the LangChain agent with more detailed instructions
2335const initializeAgent = ( ) => {
2436 const model = createLLMModel ( ) ;
2537
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:
2839 - 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
2940 - For "get" or "get text" instructions, use the getText tool to retrieve content
3041 - For "click" instructions, use the click tool to interact with elements
@@ -46,32 +57,43 @@ const initializeAgent = () => {
4657 const agent = createReactAgent ( {
4758 llm : model ,
4859 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 ,
5376 ] ,
54- stateModifier : prompt
77+ stateModifier : prompt ,
5578 } ) ;
5679
5780 return { agent } ;
5881} ;
5982
6083// 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 > {
6288 console . log ( `[Auto] Processing instruction: "${ instruction } "` ) ;
6389
64- if ( config ?. page )
65- {
90+ if ( config ?. page ) {
6691 sessionManager . setPage ( config . page ) ;
6792 console . log ( `[Auto] Page set from config` ) ;
68- } else
69- {
70- try
71- {
93+ } else {
94+ try {
7295 sessionManager . getPage ( ) ;
73- } catch
74- {
96+ } catch {
7597 // In standalone mode, create a new page
7698 console . log ( `[Auto] No existing page, creating new page` ) ;
7799 await context . createPage ( ) ;
@@ -82,19 +104,17 @@ export async function auto(instruction: string, config?: AutoConfig): Promise<an
82104 console . log ( `[Auto] Creating agent for instruction` ) ;
83105 const { agent } = initializeAgent ( ) ;
84106 const result = await agent . invoke ( {
85- messages : [ new HumanMessage ( instruction ) ]
107+ messages : [ new HumanMessage ( instruction ) ] ,
86108 } ) ;
87109
88- console . log ( " Agent result:" , result ) ;
110+ console . log ( ' Agent result:' , result ) ;
89111 // Process agent result
90112 const response = result . messages ?. [ - 1 ] ?. content ;
91113 console . log ( `[Auto] Agent response:` , response ) ;
92114
93- if ( typeof response === 'string' )
94- {
115+ if ( typeof response === 'string' ) {
95116 // If it's a success message, return null to match original behavior
96- if ( response . startsWith ( 'Successfully' ) )
97- {
117+ if ( response . startsWith ( 'Successfully' ) ) {
98118 console . log ( `[Auto] Detected success message, returning null` ) ;
99119 return null ;
100120 }
0 commit comments