"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.summarizeCareFlow = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const config_1 = require("./config");
const constants_1 = require("../../lib/constants");
const summarizeCareFlowWithLLM_1 = require("./lib/summarizeCareFlowWithLLM");
const utils_1 = require("../../../../src/utils");
const openai_1 = require("../../../../src/lib/llm/openai");
const constants_2 = require("../../../../src/lib/llm/openai/constants");
exports.summarizeCareFlow = {
    key: 'summarizeCareFlow',
    category: extensions_core_1.Category.WORKFLOW,
    title: 'Summarize Care Flow',
    description: 'Summarize the care flow up until now',
    fields: config_1.fields,
    previewable: false,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        // 1. Validate input fields
        const { additionalInstructions, stakeholder } = config_1.FieldsValidationSchema.parse(payload.fields);
        const pathway = payload.pathway;
        // 2. Initialize OpenAI model with metadata
        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,
        });
        const awellSdk = await helpers.awellSdk();
        /**
         * Limitation: this query is paginated so we might not get all care flow activities - which is ok for now
         */
        const careflowActivitiesUntilNow = await awellSdk.orchestration.query({
            careflowActivities: {
                __args: {
                    pathway_id: pathway.id,
                    pagination: { offset: 0, count: 500 },
                    sorting: {
                        direction: 'desc',
                        field: 'date',
                    },
                },
                activities: {
                    __scalar: true,
                    subject: {
                        __scalar: true,
                    },
                    object: {
                        __scalar: true,
                    },
                    indirect_object: {
                        __scalar: true,
                    },
                    context: {
                        __scalar: true,
                    },
                    track: {
                        __scalar: true,
                    },
                    sub_activities: {
                        __scalar: true,
                    },
                },
            },
        });
        const summary = await (0, summarizeCareFlowWithLLM_1.summarizeCareFlowWithLLM)({
            model,
            careFlowActivities: JSON.stringify(careflowActivitiesUntilNow.careflowActivities.activities, null, 2),
            stakeholder,
            additionalInstructions,
            metadata,
            callbacks,
        });
        const htmlSummary = await (0, utils_1.markdownToHtml)(`${constants_1.DISCLAIMER_MSG}\n\n${summary}`);
        await onComplete({
            data_points: {
                summary: htmlSummary,
            },
        });
    },
};
//# sourceMappingURL=summarizeCareFlow.js.map