"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.processPythonFile = exports.pythonPromptFunctionLegacy = exports.pythonPromptFunction = void 0;
const fs = __importStar(require("fs"));
const python_shell_1 = require("python-shell");
const logger_1 = __importDefault(require("../../logger"));
const wrapper_1 = require("../../python/wrapper");
const util_1 = require("../../util");
/**
 * Python prompt function. Runs a specific function from the python file.
 * @param promptPath - Path to the Python file.
 * @param functionName - Function name to execute.
 * @param context - Context for the prompt.
 * @returns The prompts
 */
const pythonPromptFunction = async (filePath, functionName, context) => {
    return (0, wrapper_1.runPython)(filePath, functionName, [
        {
            ...context,
            provider: {
                id: context.provider?.id,
                label: context.provider?.label,
            },
        },
    ]);
};
exports.pythonPromptFunction = pythonPromptFunction;
/**
 * Legacy Python prompt function. Runs the whole python file.
 * @param filePath - Path to the Python file.
 * @param context - Context for the prompt.
 * @returns The prompts
 */
const pythonPromptFunctionLegacy = async (filePath, context) => {
    const options = {
        mode: 'text',
        pythonPath: process.env.PROMPTFOO_PYTHON || 'python',
        args: [(0, util_1.safeJsonStringify)(context)],
    };
    logger_1.default.debug(`Executing python prompt script ${filePath}`);
    const results = (await python_shell_1.PythonShell.run(filePath, options)).join('\n');
    logger_1.default.debug(`Python prompt script ${filePath} returned: ${results}`);
    return results;
};
exports.pythonPromptFunctionLegacy = pythonPromptFunctionLegacy;
/**
 * Processes a Python file to extract or execute a function as a prompt.
 * @param filePath - Path to the Python file.
 * @param prompt - The raw prompt data.
 * @param functionName - Optional function name to execute.
 * @returns Array of prompts extracted or executed from the file.
 */
function processPythonFile(filePath, prompt, functionName) {
    const fileContent = fs.readFileSync(filePath, 'utf-8');
    return [
        {
            raw: fileContent,
            label: prompt.label
                ? prompt.label
                : functionName
                    ? `${filePath}:${functionName}`
                    : `${filePath}: ${fileContent}`,
            function: functionName
                ? (...args) => (0, exports.pythonPromptFunction)(filePath, functionName, ...args)
                : (...args) => (0, exports.pythonPromptFunctionLegacy)(filePath, ...args),
        },
    ];
}
exports.processPythonFile = processPythonFile;
//# sourceMappingURL=python.js.map