"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.categorizeMessageWithLLM = void 0;
const parser_1 = require("./parser");
const prompt_1 = require("./prompt");
/**
 * Uses LLM to categorize a message into predefined categories.
 * The function follows these steps:
 * 1. Formats prompt with available categories
 * 2. Runs LLM chain with structured output parsing
 * 3. Validates and processes the result
 *
 * @example
 * const result = await categorizeMessageWithLLM({
 *   model,
 *   message: "I need to schedule an appointment",
 *   categories: ["Scheduling", "Medical Question"],
 *   metadata: { ... }
 * })
 * // Returns: { category: "Scheduling", explanation: "..." }
 */
const categorizeMessageWithLLM = async ({ model, message, categories, metadata, callbacks, }) => {
    var _a, _b;
    const prompt = await prompt_1.systemPrompt.format({
        categories: categories.concat('None').join(', '),
        input: message,
    });
    const chain = model.pipe(parser_1.parser);
    let result;
    try {
        result = await chain.invoke(prompt, { metadata, runName: 'ShellyCategorizeMessage', callbacks });
    }
    catch (error) {
        throw new Error('Failed to categorize the message due to an internal error.');
    }
    const matchedCategory = (_a = result.matched_category) !== null && _a !== void 0 ? _a : 'None';
    let category;
    let explanation = (_b = result.match_explanation) !== null && _b !== void 0 ? _b : '';
    // Check if the matched category is valid
    if (categories.includes(matchedCategory)) {
        category = matchedCategory;
    }
    else {
        category = 'None';
        explanation =
            'Categorization was ambiguous; we could not find a proper category.';
    }
    return { category, explanation };
};
exports.categorizeMessageWithLLM = categorizeMessageWithLLM;
//# sourceMappingURL=categorizeMessageWithLLM.js.map