"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.queryPage = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const validatePayloadAndCreateSdk_1 = require("../../lib/validatePayloadAndCreateSdk");
const config_1 = require("./config");
const lodash_1 = require("lodash");
exports.queryPage = {
    key: 'queryPage',
    category: extensions_core_1.Category.DATA,
    title: 'Query web page with LLM',
    description: 'Query a web page using LLMs with Airtop’s.',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError }) => {
        const { fields, airtopSdk } = await (0, validatePayloadAndCreateSdk_1.validatePayloadAndCreateSdk)({
            fieldsSchema: config_1.FieldsValidationSchema,
            payload,
        });
        // Create a headless browser session
        const session = await airtopSdk.sessions.create();
        // Create a new window in the session and open the page
        const window = await airtopSdk.windows.create(session.data.id, {
            url: fields.pageUrl,
        });
        // query the content of the window using the prompt
        const result = await airtopSdk.windows.pageQuery(session.data.id, window.data.windowId, {
            prompt: fields.prompt,
            configuration: {
                outputSchema: fields.jsonSchema,
            },
        });
        // Close the window and terminate the session
        await airtopSdk.windows.close(session.data.id, window.data.windowId);
        await airtopSdk.sessions.terminate(session.data.id);
        const content = result.data.modelResponse;
        // Return the result
        await onComplete({
            data_points: {
                result: (0, lodash_1.isNil)(fields.jsonSchema) ? content : content,
                resultJson: (0, lodash_1.isNil)(fields.jsonSchema) ? undefined : content,
            },
        });
    },
};
//# sourceMappingURL=queryPage.js.map