@@ -11,8 +11,8 @@ import {
11
11
12
12
import { Stagehand } from "@browserbasehq/stagehand" ;
13
13
14
- import { AnyZodObject } from ' zod' ;
15
- import { jsonSchemaToZod } from ' ./utils.js' ;
14
+ import { AnyZodObject } from " zod" ;
15
+ import { jsonSchemaToZod } from " ./utils.js" ;
16
16
17
17
// Define the Stagehand tools
18
18
const TOOLS : Tool [ ] = [
@@ -22,10 +22,10 @@ const TOOLS: Tool[] = [
22
22
inputSchema : {
23
23
type : "object" ,
24
24
properties : {
25
- url : { type : "string" , description : "The URL to navigate to" }
25
+ url : { type : "string" , description : "The URL to navigate to" } ,
26
26
} ,
27
- required : [ "url" ]
28
- }
27
+ required : [ "url" ] ,
28
+ } ,
29
29
} ,
30
30
{
31
31
name : "stagehand_act" ,
@@ -45,10 +45,10 @@ const TOOLS: Tool[] = [
45
45
} ,
46
46
{
47
47
name : "stagehand_extract" ,
48
- description : `
49
- Extracts structured data from the web page based on an instruction and a JSON schema.
50
-
51
- **Instructions for providing the schema:**
48
+ description : `Extracts structured data from the web page based on an instruction and a JSON schema.` ,
49
+ inputSchema : {
50
+ type : "object" ,
51
+ description : ` **Instructions for providing the schema:**
52
52
53
53
- The \`schema\` should be a valid JSON Schema object that defines the structure of the data to extract.
54
54
- Use standard JSON Schema syntax.
@@ -121,18 +121,17 @@ const TOOLS: Tool[] = [
121
121
- Ensure the schema is valid JSON.
122
122
- Use standard JSON Schema types like \`string\`, \`number\`, \`array\`, \`object\`, etc.
123
123
- You can add descriptions to help clarify the expected data.
124
-
125
124
` ,
126
- inputSchema : {
127
- type : "object" ,
128
125
properties : {
129
- instruction : {
130
- type : "string" ,
131
- description : "Clear instruction for what data to extract from the page"
126
+ instruction : {
127
+ type : "string" ,
128
+ description :
129
+ "Clear instruction for what data to extract from the page" ,
132
130
} ,
133
131
schema : {
134
132
type : "object" ,
135
- description : "A JSON Schema object defining the structure of data to extract" ,
133
+ description :
134
+ "A JSON Schema object defining the structure of data to extract" ,
136
135
additionalProperties : true ,
137
136
} ,
138
137
} ,
@@ -154,7 +153,6 @@ const TOOLS: Tool[] = [
154
153
} ,
155
154
] ;
156
155
157
-
158
156
// Global state
159
157
let stagehand : Stagehand | undefined ;
160
158
const consoleLogs : string [ ] = [ ] ;
@@ -169,14 +167,14 @@ function log(message: string) {
169
167
// Ensure Stagehand is initialized
170
168
async function ensureStagehand ( ) {
171
169
log ( "Ensuring Stagehand is initialized..." ) ;
172
-
173
170
if ( ! stagehand ) {
174
171
log ( "Initializing Stagehand..." ) ;
175
172
stagehand = new Stagehand ( {
176
173
env : "BROWSERBASE" ,
177
174
headless : true ,
178
175
verbose : 2 ,
179
176
debugDom : true ,
177
+ modelName : "claude-3-5-sonnet-20241022" ,
180
178
} ) ;
181
179
log ( "Running init()" ) ;
182
180
await stagehand . init ( ) ;
@@ -201,13 +199,13 @@ async function handleToolCall(
201
199
toolResult : {
202
200
content : [
203
201
{
204
- type : "text" ,
202
+ type : "text" ,
205
203
text : `Failed to initialize Stagehand: ${ errorMsg } ` ,
206
204
} ,
207
205
{
208
206
type : "text" ,
209
207
text : `Operation logs:\n${ operationLogs . join ( "\n" ) } ` ,
210
- }
208
+ } ,
211
209
] ,
212
210
isError : true ,
213
211
} ,
@@ -244,7 +242,7 @@ async function handleToolCall(
244
242
{
245
243
type : "text" ,
246
244
text : `Operation logs:\n${ operationLogs . join ( "\n" ) } ` ,
247
- }
245
+ } ,
248
246
] ,
249
247
isError : true ,
250
248
} ,
@@ -283,65 +281,67 @@ async function handleToolCall(
283
281
{
284
282
type : "text" ,
285
283
text : `Operation logs:\n${ operationLogs . join ( "\n" ) } ` ,
286
- }
284
+ } ,
287
285
] ,
288
286
isError : true ,
289
287
} ,
290
288
} ;
291
289
}
292
290
293
- case "stagehand_extract" :
294
- try {
295
- log ( `Extracting data with instruction: ${ args . instruction } ` ) ;
296
- log ( `Schema: ${ JSON . stringify ( args . schema ) } ` ) ;
297
- // Convert the JSON schema from args.schema to a zod schema
298
- const zodSchema = jsonSchemaToZod ( args . schema ) as AnyZodObject ;
299
- const data = await stagehand . extract ( {
300
- instruction : args . instruction ,
301
- schema : zodSchema ,
302
- } ) ;
303
- log ( `Data extracted successfully: ${ JSON . stringify ( data ) } ` ) ;
304
- return {
305
- toolResult : {
306
- content : [
307
- {
308
- type : "text" ,
309
- text : `Extraction result: ${ JSON . stringify ( data ) } ` ,
310
- } ,
311
- {
312
- type : "text" ,
313
- text : `Operation logs:\n${ operationLogs . join ( "\n" ) } ` ,
314
- }
315
- ] ,
316
- isError : false ,
317
- } ,
318
- } ;
319
- } catch ( error ) {
320
- const errorMsg = error instanceof Error ? error . message : String ( error ) ;
321
- log ( `Extraction failed: ${ errorMsg } ` ) ;
322
- return {
323
- toolResult : {
324
- content : [
325
- {
326
- type : "text" ,
327
- text : `Failed to extract: ${ errorMsg } ` ,
328
- } ,
329
- {
330
- type : "text" ,
331
- text : `Operation logs:\n${ operationLogs . join ( "\n" ) } ` ,
332
- }
333
- ] ,
334
- isError : true ,
335
- } ,
336
- } ;
337
- }
291
+ case "stagehand_extract" :
292
+ try {
293
+ log ( `Extracting data with instruction: ${ args . instruction } ` ) ;
294
+ log ( `Schema: ${ JSON . stringify ( args . schema ) } ` ) ;
295
+ // Convert the JSON schema from args.schema to a zod schema
296
+ const zodSchema = jsonSchemaToZod ( args . schema ) as AnyZodObject ;
297
+ const data = await stagehand . extract ( {
298
+ instruction : args . instruction ,
299
+ schema : zodSchema ,
300
+ } ) ;
301
+ log ( `Data extracted successfully: ${ JSON . stringify ( data ) } ` ) ;
302
+ return {
303
+ toolResult : {
304
+ content : [
305
+ {
306
+ type : "text" ,
307
+ text : `Extraction result: ${ JSON . stringify ( data ) } ` ,
308
+ } ,
309
+ {
310
+ type : "text" ,
311
+ text : `Operation logs:\n${ operationLogs . join ( "\n" ) } ` ,
312
+ } ,
313
+ ] ,
314
+ isError : false ,
315
+ } ,
316
+ } ;
317
+ } catch ( error ) {
318
+ const errorMsg = error instanceof Error ? error . message : String ( error ) ;
319
+ log ( `Extraction failed: ${ errorMsg } ` ) ;
320
+ return {
321
+ toolResult : {
322
+ content : [
323
+ {
324
+ type : "text" ,
325
+ text : `Failed to extract: ${ errorMsg } ` ,
326
+ } ,
327
+ {
328
+ type : "text" ,
329
+ text : `Operation logs:\n${ operationLogs . join ( "\n" ) } ` ,
330
+ } ,
331
+ ] ,
332
+ isError : true ,
333
+ } ,
334
+ } ;
335
+ }
338
336
case "stagehand_observe" :
339
337
try {
340
338
log ( `Starting observation with instruction: ${ args . instruction } ` ) ;
341
339
const observations = await stagehand . observe ( {
342
340
instruction : args . instruction ,
343
341
} ) ;
344
- log ( `Observation completed successfully: ${ JSON . stringify ( observations ) } ` ) ;
342
+ log (
343
+ `Observation completed successfully: ${ JSON . stringify ( observations ) } `
344
+ ) ;
345
345
return {
346
346
toolResult : {
347
347
content : [
@@ -366,7 +366,7 @@ async function handleToolCall(
366
366
{
367
367
type : "text" ,
368
368
text : `Operation logs:\n${ operationLogs . join ( "\n" ) } ` ,
369
- }
369
+ } ,
370
370
] ,
371
371
isError : true ,
372
372
} ,
@@ -385,7 +385,7 @@ async function handleToolCall(
385
385
{
386
386
type : "text" ,
387
387
text : `Operation logs:\n${ operationLogs . join ( "\n" ) } ` ,
388
- }
388
+ } ,
389
389
] ,
390
390
isError : true ,
391
391
} ,
@@ -407,7 +407,6 @@ const server = new Server(
407
407
}
408
408
) ;
409
409
410
-
411
410
// Setup request handlers
412
411
server . setRequestHandler ( ListToolsRequestSchema , async ( ) => {
413
412
log ( "Listing available tools" ) ;
@@ -417,7 +416,10 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
417
416
server . setRequestHandler ( CallToolRequestSchema , async ( request ) => {
418
417
log ( `Received tool call request for: ${ request . params . name } ` ) ;
419
418
operationLogs . length = 0 ; // Clear logs for new operation
420
- const result = await handleToolCall ( request . params . name , request . params . arguments ?? { } ) ;
419
+ const result = await handleToolCall (
420
+ request . params . name ,
421
+ request . params . arguments ?? { }
422
+ ) ;
421
423
log ( "Tool call completed" ) ;
422
424
return result ;
423
425
} ) ;
@@ -431,6 +433,8 @@ async function runServer() {
431
433
}
432
434
433
435
runServer ( ) . catch ( ( error ) => {
434
- log ( `Server error: ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
436
+ log (
437
+ `Server error: ${ error instanceof Error ? error . message : String ( error ) } `
438
+ ) ;
435
439
console . error ( error ) ;
436
- } ) ;
440
+ } ) ;
0 commit comments