"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseStructuredDataWithLLM = void 0;
const parser_1 = require("./parser");
const prompt_1 = require("./prompt");
/**
 * Uses LLM to extract structured data from a message according to a provided schema.
 * The function follows these steps:
 * 1. Formats prompt with schema and message
 * 2. Runs LLM chain with structured output parsing
 * 3. Validates and processes the result
 *
 * @example
 * const result = await parseStructuredDataWithLLM({
 *   model,
 *   message: "Patient John Doe, age 45, scheduled for tomorrow at 2pm",
 *   schema: { name: "string", age: "number", appointmentTime: "string" },
 *   metadata: { ... }
 * })
 * // Returns: {
 * //   data: { name: "John Doe", age: 45, appointmentTime: "2pm tomorrow" },
 * //   confidenceLevel: 85,
 * //   explanation: "..."
 * // }
 */
const parseStructuredDataWithLLM = async ({ model, message, schema, instructions, metadata, callbacks, }) => {
    var _a, _b, _c;
    const prompt = await prompt_1.systemPrompt.format({
        schema: JSON.stringify(schema, null, 2),
        input: message,
        instructions: instructions !== null && instructions !== void 0 ? instructions : 'No additional instructions have been provided',
    });
    const chain = model.pipe(parser_1.parser);
    let result;
    try {
        result = await chain.invoke(prompt, {
            metadata,
            runName: 'ShellyParseStructuredData',
            callbacks,
        });
    }
    catch (error) {
        throw new Error('Failed to parse structured data from the message due to an internal error.');
    }
    return {
        data: (_a = result.extracted_data) !== null && _a !== void 0 ? _a : {},
        confidenceLevel: (_b = result.confidence_level) !== null && _b !== void 0 ? _b : 0,
        explanation: (_c = result.extraction_explanation) !== null && _c !== void 0 ? _c : '',
    };
};
exports.parseStructuredDataWithLLM = parseStructuredDataWithLLM;
//# sourceMappingURL=parseStructuredDataWithLLM.js.map