@auto-browse/auto-browse
Version:
AI-powered browser automation
48 lines (47 loc) • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.browser_take_screenshot = void 0;
const tools_1 = require("@langchain/core/tools");
const zod_1 = require("zod");
const context_1 = require("../browser/context");
/**
* Schema for taking screenshots with descriptions for the AI model
*/
const screenshotSchema = zod_1.z.object({
raw: zod_1.z
.boolean()
.optional()
.describe("Whether to return without compression (in PNG format). Default is false, which returns a JPEG image."),
});
exports.browser_take_screenshot = (0, tools_1.tool)(async ({ raw }) => {
try {
console.log(`[Screenshot Tool] Starting operation:`, { raw });
const page = context_1.context.existingPage();
const options = raw
? { type: "png", scale: "css" }
: { type: "jpeg", quality: 50, scale: "css" };
console.log(`[Screenshot Tool] Taking screenshot with options:`, options);
const screenshot = await page.screenshot(options);
console.log(`[Screenshot Tool] Screenshot captured successfully`);
const result = {
content: [
{
type: "image",
data: screenshot.toString("base64"),
mimeType: raw ? "image/png" : "image/jpeg",
},
],
};
console.log(`[Screenshot Tool] Operation completed successfully`);
return result;
}
catch (error) {
const errorMessage = `Failed to take screenshot: ${error instanceof Error ? error.message : "Unknown error"}`;
console.error(`[Screenshot Tool] Error:`, errorMessage);
return errorMessage;
}
}, {
name: "screenshot",
description: "Take a screenshot of the current page. Note: Use browser_snapshot for actions, not this tool.",
schema: screenshotSchema,
});