Skip to content

Commit

Permalink
fix: complete puppeteer server implementation with error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gitw21 committed Nov 29, 2024
1 parent 8be0113 commit 2ea5630
Showing 1 changed file with 2 additions and 206 deletions.
208 changes: 2 additions & 206 deletions src/puppeteer/index.ts
Original file line number Diff line number Diff line change
@@ -1,207 +1,3 @@
isError: true,
},
};
}

case "puppeteer_screenshot": {
const width = args.width ?? config.screenshotOptions.defaultWidth;
const height = args.height ?? config.screenshotOptions.defaultHeight;

try {
await page.setViewport({ width, height });

// Wait for any animations to complete
await page.waitForTimeout(500);

let screenshot;
if (args.selector) {
const element = await withTimeout(
page.waitForSelector(args.selector, { timeout: 5000 })
);
if (!element) {
throw new Error(`Element not found: ${args.selector}`);
}
screenshot = await element.screenshot({
encoding: "base64",
quality: config.screenshotOptions.quality
});
} else {
screenshot = await page.screenshot({
encoding: "base64",
fullPage: false,
captureBeyondViewport: false,
quality: config.screenshotOptions.quality
});
}

if (!screenshot) {
throw new Error('Screenshot capture failed');
}

screenshots.set(args.name, screenshot as string);
server.notification({
method: "notifications/resources/list_changed",
});

return {
toolResult: {
content: [
{
type: "text",
text: `Screenshot '${args.name}' taken at ${width}x${height}`,
} as TextContent,
{
type: "image",
data: screenshot,
mimeType: "image/png",
} as ImageContent,
],
isError: false,
},
};
} catch (error) {
return {
toolResult: {
content: [{
type: "text",
text: `Screenshot failed: ${(error as Error).message}`,
}],
isError: true,
},
};
}
}

case "puppeteer_click":
try {
const element = await withTimeout(
page.waitForSelector(args.selector, { timeout: 5000 })
);
if (!element) {
throw new Error(`Element not found: ${args.selector}`);
}
await element.click();
return {
toolResult: {
content: [{
type: "text",
text: `Clicked: ${args.selector}`,
}],
isError: false,
},
};
} catch (error) {
return {
toolResult: {
content: [{
type: "text",
text: `Failed to click ${args.selector}: ${(error as Error).message}`,
}],
isError: true,
},
};
}

case "puppeteer_fill":
try {
const element = await withTimeout(
page.waitForSelector(args.selector, { timeout: 5000 })
);
if (!element) {
throw new Error(`Element not found: ${args.selector}`);
}
await element.type(args.value);
return {
toolResult: {
content: [{
type: "text",
text: `Filled ${args.selector} with: ${args.value}`,
}],
isError: false,
},
};
} catch (error) {
return {
toolResult: {
content: [{
type: "text",
text: `Failed to fill ${args.selector}: ${(error as Error).message}`,
}],
isError: true,
},
};
}

case "puppeteer_select":
try {
const element = await withTimeout(
page.waitForSelector(args.selector, { timeout: 5000 })
);
if (!element) {
throw new Error(`Element not found: ${args.selector}`);
}
await page.select(args.selector, args.value);
return {
toolResult: {
content: [{
type: "text",
text: `Selected ${args.selector} with: ${args.value}`,
}],
isError: false,
},
};
} catch (error) {
return {
toolResult: {
content: [{
type: "text",
text: `Failed to select ${args.selector}: ${(error as Error).message}`,
}],
isError: true,
},
};
}

case "puppeteer_hover":
try {
const element = await withTimeout(
page.waitForSelector(args.selector, { timeout: 5000 })
);
if (!element) {
throw new Error(`Element not found: ${args.selector}`);
}
await element.hover();
return {
toolResult: {
content: [{
type: "text",
text: `Hovered ${args.selector}`,
}],
isError: false,
},
};
} catch (error) {
return {
toolResult: {
content: [{
type: "text",
text: `Failed to hover ${args.selector}: ${(error as Error).message}`,
}],
isError: true,
},
};
}

case "puppeteer_evaluate":
try {
const result = await withTimeout(
page.evaluate((script) => {
const logs: string[] = [];
const originalConsole = { ...console };

['log', 'info', 'warn', 'error'].forEach(method => {
(console as any)[method] = (...args: any[]) => {
logs.push(`[${method}] ${args.join(' ')}`);
(originalConsole as any)[method](...args);
};
});
Expand Down Expand Up @@ -310,7 +106,7 @@ server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
}

throw new Error(`Resource not found: ${uri}`);
}));
});

server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: TOOLS,
Expand All @@ -334,4 +130,4 @@ async function runServer() {
await server.connect(transport);
}

runServer().catch(console.error);
runServer().catch(console.error);

0 comments on commit 2ea5630

Please sign in to comment.