Skip to content

Commit 97880e1

Browse files
committed
rebase
1 parent 01e798f commit 97880e1

File tree

1 file changed

+78
-74
lines changed

1 file changed

+78
-74
lines changed

stagehand/src/index.ts

Lines changed: 78 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111

1212
import { Stagehand } from "@browserbasehq/stagehand";
1313

14-
import { AnyZodObject } from 'zod';
15-
import { jsonSchemaToZod } from './utils.js';
14+
import { AnyZodObject } from "zod";
15+
import { jsonSchemaToZod } from "./utils.js";
1616

1717
// Define the Stagehand tools
1818
const TOOLS: Tool[] = [
@@ -22,10 +22,10 @@ const TOOLS: Tool[] = [
2222
inputSchema: {
2323
type: "object",
2424
properties: {
25-
url: { type: "string", description: "The URL to navigate to" }
25+
url: { type: "string", description: "The URL to navigate to" },
2626
},
27-
required: ["url"]
28-
}
27+
required: ["url"],
28+
},
2929
},
3030
{
3131
name: "stagehand_act",
@@ -45,10 +45,10 @@ const TOOLS: Tool[] = [
4545
},
4646
{
4747
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:**
5252
5353
- The \`schema\` should be a valid JSON Schema object that defines the structure of the data to extract.
5454
- Use standard JSON Schema syntax.
@@ -121,18 +121,17 @@ const TOOLS: Tool[] = [
121121
- Ensure the schema is valid JSON.
122122
- Use standard JSON Schema types like \`string\`, \`number\`, \`array\`, \`object\`, etc.
123123
- You can add descriptions to help clarify the expected data.
124-
125124
`,
126-
inputSchema: {
127-
type: "object",
128125
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",
132130
},
133131
schema: {
134132
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",
136135
additionalProperties: true,
137136
},
138137
},
@@ -154,7 +153,6 @@ const TOOLS: Tool[] = [
154153
},
155154
];
156155

157-
158156
// Global state
159157
let stagehand: Stagehand | undefined;
160158
const consoleLogs: string[] = [];
@@ -169,14 +167,14 @@ function log(message: string) {
169167
// Ensure Stagehand is initialized
170168
async function ensureStagehand() {
171169
log("Ensuring Stagehand is initialized...");
172-
173170
if (!stagehand) {
174171
log("Initializing Stagehand...");
175172
stagehand = new Stagehand({
176173
env: "BROWSERBASE",
177174
headless: true,
178175
verbose: 2,
179176
debugDom: true,
177+
modelName: "claude-3-5-sonnet-20241022",
180178
});
181179
log("Running init()");
182180
await stagehand.init();
@@ -201,13 +199,13 @@ async function handleToolCall(
201199
toolResult: {
202200
content: [
203201
{
204-
type: "text",
202+
type: "text",
205203
text: `Failed to initialize Stagehand: ${errorMsg}`,
206204
},
207205
{
208206
type: "text",
209207
text: `Operation logs:\n${operationLogs.join("\n")}`,
210-
}
208+
},
211209
],
212210
isError: true,
213211
},
@@ -244,7 +242,7 @@ async function handleToolCall(
244242
{
245243
type: "text",
246244
text: `Operation logs:\n${operationLogs.join("\n")}`,
247-
}
245+
},
248246
],
249247
isError: true,
250248
},
@@ -283,65 +281,67 @@ async function handleToolCall(
283281
{
284282
type: "text",
285283
text: `Operation logs:\n${operationLogs.join("\n")}`,
286-
}
284+
},
287285
],
288286
isError: true,
289287
},
290288
};
291289
}
292290

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+
}
338336
case "stagehand_observe":
339337
try {
340338
log(`Starting observation with instruction: ${args.instruction}`);
341339
const observations = await stagehand.observe({
342340
instruction: args.instruction,
343341
});
344-
log(`Observation completed successfully: ${JSON.stringify(observations)}`);
342+
log(
343+
`Observation completed successfully: ${JSON.stringify(observations)}`
344+
);
345345
return {
346346
toolResult: {
347347
content: [
@@ -366,7 +366,7 @@ async function handleToolCall(
366366
{
367367
type: "text",
368368
text: `Operation logs:\n${operationLogs.join("\n")}`,
369-
}
369+
},
370370
],
371371
isError: true,
372372
},
@@ -385,7 +385,7 @@ async function handleToolCall(
385385
{
386386
type: "text",
387387
text: `Operation logs:\n${operationLogs.join("\n")}`,
388-
}
388+
},
389389
],
390390
isError: true,
391391
},
@@ -407,7 +407,6 @@ const server = new Server(
407407
}
408408
);
409409

410-
411410
// Setup request handlers
412411
server.setRequestHandler(ListToolsRequestSchema, async () => {
413412
log("Listing available tools");
@@ -417,7 +416,10 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
417416
server.setRequestHandler(CallToolRequestSchema, async (request) => {
418417
log(`Received tool call request for: ${request.params.name}`);
419418
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+
);
421423
log("Tool call completed");
422424
return result;
423425
});
@@ -431,6 +433,8 @@ async function runServer() {
431433
}
432434

433435
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+
);
435439
console.error(error);
436-
});
440+
});

0 commit comments

Comments
 (0)