1
- import { Tool } from ' ../../core/types.js' ;
2
- import { z } from ' zod' ;
3
- import { zodToJsonSchema } from ' zod-to-json-schema' ;
4
- import { v4 as uuidv4 } from ' uuid' ;
5
- import { chromium } from ' @playwright/test' ;
6
- import { browserSessions , type BrowserError , BrowserErrorCode } from ' ./types.js' ;
1
+ import { Tool } from " ../../core/types.js" ;
2
+ import { z } from " zod" ;
3
+ import { zodToJsonSchema } from " zod-to-json-schema" ;
4
+ import { v4 as uuidv4 } from " uuid" ;
5
+ import { chromium } from " @playwright/test" ;
6
+ import { browserSessions } from " ./types.js" ;
7
7
8
8
const parameterSchema = z . object ( {
9
- url : z . string ( ) . url ( ) . optional ( ) . describe ( 'Initial URL to navigate to' ) ,
10
- headless : z . boolean ( ) . optional ( ) . describe ( 'Run browser in headless mode (default: true)' ) ,
11
- timeout : z . number ( ) . optional ( ) . describe ( 'Default timeout in milliseconds (default: 30000)' ) ,
12
- description : z . string ( ) . max ( 80 ) . describe ( 'The reason for starting this browser session (max 80 chars)' ) ,
9
+ url : z . string ( ) . url ( ) . optional ( ) . describe ( "Initial URL to navigate to" ) ,
10
+ headless : z
11
+ . boolean ( )
12
+ . optional ( )
13
+ . describe ( "Run browser in headless mode (default: true)" ) ,
14
+ timeout : z
15
+ . number ( )
16
+ . optional ( )
17
+ . describe ( "Default timeout in milliseconds (default: 30000)" ) ,
18
+ description : z
19
+ . string ( )
20
+ . max ( 80 )
21
+ . describe ( "The reason for starting this browser session (max 80 chars)" ) ,
13
22
} ) ;
14
23
15
24
const returnSchema = z . object ( {
@@ -23,32 +32,36 @@ type Parameters = z.infer<typeof parameterSchema>;
23
32
type ReturnType = z . infer < typeof returnSchema > ;
24
33
25
34
export const browseStartTool : Tool < Parameters , ReturnType > = {
26
- name : ' browseStart' ,
27
- description : ' Starts a new browser session with optional initial URL' ,
35
+ name : " browseStart" ,
36
+ description : " Starts a new browser session with optional initial URL" ,
28
37
parameters : zodToJsonSchema ( parameterSchema ) ,
29
38
returns : zodToJsonSchema ( returnSchema ) ,
30
39
31
- execute : async ( { url, headless = true , timeout = 30000 } , { logger } ) : Promise < ReturnType > => {
32
- logger . verbose ( `Starting browser session${ url ? ` at ${ url } ` : '' } ` ) ;
40
+ execute : async (
41
+ { url, headless = true , timeout = 30000 } ,
42
+ { logger }
43
+ ) : Promise < ReturnType > => {
44
+ logger . verbose ( `Starting browser session${ url ? ` at ${ url } ` : "" } ` ) ;
33
45
34
46
try {
35
47
const instanceId = uuidv4 ( ) ;
36
-
48
+
37
49
// Launch browser
38
50
const browser = await chromium . launch ( {
39
- headless
51
+ headless,
40
52
} ) ;
41
53
42
54
// Create new context with default settings
43
55
const context = await browser . newContext ( {
44
56
viewport : null ,
45
- userAgent : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
57
+ userAgent :
58
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" ,
46
59
} ) ;
47
60
48
61
// Create new page
49
62
const page = await context . newPage ( ) ;
50
63
page . setDefaultTimeout ( timeout ) ;
51
-
64
+
52
65
// Initialize browser session
53
66
const session = {
54
67
browser,
@@ -59,41 +72,40 @@ export const browseStartTool: Tool<Parameters, ReturnType> = {
59
72
browserSessions . set ( instanceId , session ) ;
60
73
61
74
// Setup cleanup handlers
62
- browser . on ( ' disconnected' , ( ) => {
75
+ browser . on ( " disconnected" , ( ) => {
63
76
browserSessions . delete ( instanceId ) ;
64
77
} ) ;
65
78
66
79
// Navigate to URL if provided
67
- let content = '' ;
80
+ let content = "" ;
68
81
if ( url ) {
69
- await page . goto ( url , { waitUntil : ' networkidle' } ) ;
82
+ await page . goto ( url , { waitUntil : " networkidle" } ) ;
70
83
content = await page . content ( ) ;
71
84
}
72
85
73
- logger . verbose ( ' Browser session started successfully' ) ;
74
-
86
+ logger . verbose ( " Browser session started successfully" ) ;
87
+
75
88
return {
76
89
instanceId,
77
- status : ' initialized' ,
90
+ status : " initialized" ,
78
91
content : content || undefined ,
79
92
} ;
80
-
81
93
} catch ( error ) {
82
94
logger . error ( `Failed to start browser: ${ error } ` ) ;
83
95
return {
84
- instanceId : '' ,
85
- status : ' error' ,
96
+ instanceId : "" ,
97
+ status : " error" ,
86
98
error : error instanceof Error ? error . message : String ( error ) ,
87
99
} ;
88
100
}
89
101
} ,
90
102
91
103
logParameters : ( { url, description } , { logger } ) => {
92
104
logger . info (
93
- `Starting browser session${ url ? ` at ${ url } ` : '' } , ${ description } ` ,
105
+ `Starting browser session${ url ? ` at ${ url } ` : "" } , ${ description } `
94
106
) ;
95
107
} ,
96
-
108
+
97
109
logReturns : ( output , { logger } ) => {
98
110
if ( output . error ) {
99
111
logger . error ( `Browser start failed: ${ output . error } ` ) ;
0 commit comments