"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.summarizeFormsInStep = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const getFormResponseText_1 = require("../../lib/getFormResponseText");
const summarizeFormWithLLM_1 = require("../../lib/summarizeFormWithLLM");
const detectLanguageWithLLM_1 = require("../../lib/detectLanguageWithLLM");
const constants_1 = require("../../lib/constants");
const awell_1 = require("../../../../src/lib/awell");
const utils_1 = require("../../../../src/utils");
const openai_1 = require("../../../../src/lib/llm/openai");
const constants_2 = require("../../../../src/lib/llm/openai/constants");
exports.summarizeFormsInStep = {
    key: 'summarizeFormsInStep',
    category: extensions_core_1.Category.WORKFLOW,
    title: 'Summarize Forms in Step',
    description: 'Summarize the responses of all forms in the step with AI.',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        // 1. Validate input fields
        const { summaryFormat, language } = config_1.FieldsValidationSchema.parse(payload.fields);
        const pathway = payload.pathway;
        // 2. Initialize OpenAI model with hideDataForTracing enabled
        const { model, metadata, callbacks } = await (0, openai_1.createOpenAIModel)({
            settings: {}, // we use built-in API key for OpenAI
            helpers,
            payload,
            modelType: constants_2.OPENAI_MODELS.GPT4o,
            hideDataForTracing: true, // Hide input and output data when tracing
        });
        // Fetch all forms in the current step
        const formsData = await (0, awell_1.getAllFormsInCurrentStep)({
            awellSdk: await helpers.awellSdk(),
            pathwayId: pathway.id,
            activityId: payload.activity.id,
        });
        // Get responses for all forms
        const { result: allFormsResponseText } = (0, getFormResponseText_1.getResponsesForAllForms)({
            formsData,
        });
        // Determine which language to use for summarization
        let summaryLanguage = language;
        // If language is set to 'Default', detect the language
        if (language === 'Default') {
            try {
                summaryLanguage = await (0, detectLanguageWithLLM_1.detectLanguageWithLLM)({
                    model,
                    text: allFormsResponseText,
                    metadata,
                    callbacks,
                });
            }
            catch (error) {
                // If language detection fails, fall back to 'Default'
                summaryLanguage = 'Default';
            }
        }
        // Summarize all forms' responses
        const summary = await (0, summarizeFormWithLLM_1.summarizeFormWithLLM)({
            model,
            formData: allFormsResponseText,
            summaryFormat,
            language: summaryLanguage,
            disclaimerMessage: constants_1.DISCLAIMER_MSG_FORM,
            metadata,
            callbacks, // Add callbacks here
        });
        // Disclaimer is now handled within summarizeFormWithLLM
        const htmlSummary = await (0, utils_1.markdownToHtml)(summary);
        await onComplete({
            data_points: {
                summary: htmlSummary,
            },
        });
    },
};
//# sourceMappingURL=summarizeFormsInStep.js.map