"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseStructuredData = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const parseStructuredDataWithLLM_1 = require("./lib/parseStructuredDataWithLLM");
const createOpenAIModel_1 = require("../../../../src/lib/llm/openai/createOpenAIModel");
const constants_1 = require("../../../../src/lib/llm/openai/constants");
const config_1 = require("./config");
const utils_1 = require("../../../../src/utils");
const lodash_1 = require("lodash");
/**
 * Awell Action: Parse Structured Data
 *
 * Takes a message and a JSON schema as input, uses LLM to:
 * 1. Extract structured data matching the schema
 * 2. Provide a confidence level (0-100) for the extraction
 * 3. Provide explanation for the extraction process
 *
 * @returns parsed data (JSON), confidence level (number), and HTML-formatted explanation
 */
exports.parseStructuredData = {
    key: 'parseStructuredData',
    category: extensions_core_1.Category.WORKFLOW,
    title: 'Parse Structured Data',
    description: 'Extracts structured data from unstructured text messages using a provided JSON schema.',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        // 1. Validate input fields
        const { message: messageText, messageDataPoint, schema, instructions, } = config_1.FieldsValidationSchema.parse(payload.fields);
        const message = !(0, lodash_1.isNil)(messageDataPoint) && (messageDataPoint === null || messageDataPoint === void 0 ? void 0 : messageDataPoint.length) > 0
            ? messageDataPoint
            : (messageText !== null && messageText !== void 0 ? messageText : '');
        // ensure message is not empty
        if (message.length === 0) {
            await onError({
                events: [
                    {
                        date: new Date().toISOString(),
                        text: { en: 'Either message or messageDataPoint is required' },
                        error: {
                            category: 'BAD_REQUEST',
                            message: 'Either message or messageDataPoint is required',
                        },
                    },
                ],
            });
            return;
        }
        // 2. Initialize OpenAI model with metadata
        const { model, metadata, callbacks } = await (0, createOpenAIModel_1.createOpenAIModel)({
            settings: {}, // we use built-in API key for OpenAI
            helpers,
            payload,
            modelType: constants_1.OPENAI_MODELS.GPT5Mini,
        });
        // 3. Perform structured data extraction
        const result = await (0, parseStructuredDataWithLLM_1.parseStructuredDataWithLLM)({
            model,
            message,
            schema,
            instructions,
            metadata,
            callbacks,
        });
        // 4. Format and return results
        const explanationHtml = await (0, utils_1.markdownToHtml)(result.explanation);
        await onComplete({
            data_points: {
                parsedData: JSON.stringify(result.data),
                confidenceLevel: String(result.confidenceLevel),
                explanation: explanationHtml,
            },
        });
    },
};
//# sourceMappingURL=parseStructuredData.js.map