"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.categorizeMessage = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const categorizeMessageWithLLM_1 = require("./lib/categorizeMessageWithLLM");
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: Message Categorization
 *
 * Takes a message and predefined categories as input, uses LLM to:
 * 1. Determine the most appropriate category
 * 2. Provide explanation for the categorization
 *
 * @returns category and HTML-formatted explanation
 */
exports.categorizeMessage = {
    key: 'categorizeMessage',
    category: extensions_core_1.Category.WORKFLOW,
    title: 'Categorize Message',
    description: 'Categorizes messages into predefined categories with explanation.',
    fields: config_1.fields,
    previewable: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        // 1. Validate input fields
        const { message: messageText, messageDataPoint, categories, 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',
                        },
                    },
                ],
            });
        }
        // 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.GPT4oMini,
        });
        // 3. Perform categorization
        const result = await (0, categorizeMessageWithLLM_1.categorizeMessageWithLLM)({
            model,
            message,
            categories,
            instructions,
            metadata,
            callbacks,
        });
        // 4. Format and return results
        const explanationHtml = await (0, utils_1.markdownToHtml)(result.explanation);
        await onComplete({
            data_points: { category: result.category, explanation: explanationHtml },
        });
    },
};
//# sourceMappingURL=categorizeMessage.js.map