"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkPatientTags = void 0;
const extensions_core_1 = require("@awell-health/extensions-core");
const addEventLog_1 = require("../../../../src/lib/awell/addEventLog");
const client_1 = require("../../client");
const config_1 = require("./config");
const checkTagsWithLLM_1 = require("./lib/checkTagsWithLLM/checkTagsWithLLM");
const createOpenAIModel_1 = require("../../../../src/lib/llm/openai/createOpenAIModel");
const constants_1 = require("../../../../src/lib/llm/openai/constants");
/**
 * Awell Action: Check Patient Tags
 *
 * Takes existing tags and instructions, uses LLM to:
 * 1. Verify if tags match the given instruction
 * 2. Provide explanation for the verification result
 * 3. Return boolean result indicating if tags exist as specified
 */
exports.checkPatientTags = {
    key: 'checkPatientTags',
    category: extensions_core_1.Category.EHR_INTEGRATIONS,
    title: '✨ Check Patient Tags',
    description: 'Verify if patient tags in Elation match the given instruction.',
    fields: config_1.fields,
    previewable: false,
    supports_automated_retries: true,
    dataPoints: config_1.dataPoints,
    onEvent: async ({ payload, onComplete, onError, helpers }) => {
        // 1. Validate input and initialize API client
        const { instructions, patientId } = config_1.FieldsValidationSchema.parse(payload.fields);
        const api = (0, client_1.makeAPIClient)(payload.settings);
        // 2. Get existing tags
        const { tags } = await api.getPatient(patientId);
        const existingTags = tags !== null && tags !== void 0 ? tags : [];
        // 3. Initialize OpenAI model with metadata and callbacks
        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.GPT4o,
        });
        // 4. Check tags against instruction
        const { tagsFound, explanation } = await (0, checkTagsWithLLM_1.checkTagsWithLLM)({
            model,
            existingTags,
            instructions,
            metadata,
            callbacks,
        });
        // 5. Complete action with results
        await onComplete({
            data_points: {
                tagsFound: tagsFound ? 'true' : 'false',
                explanation,
            },
            events: [
                (0, addEventLog_1.addActivityEventLog)({
                    message: `Patient tags: ${(existingTags === null || existingTags === void 0 ? void 0 : existingTags.length) > 0 ? existingTags === null || existingTags === void 0 ? void 0 : existingTags.join(', ') : 'No tags'}\nInstruction: ${instructions}\nResult: ${tagsFound ? 'true' : 'false'}\nExplanation: ${explanation}`,
                }),
            ],
        });
    },
};
//# sourceMappingURL=checkPatientTags.js.map