"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateFunctionCall = void 0;
const ajv_1 = __importDefault(require("ajv"));
const util_1 = require("../util");
const ajv = new ajv_1.default();
function validateFunctionCall(functionCall, functions, vars) {
    // Parse function call and validate it against schema
    const interpolatedFunctions = (0, util_1.renderVarsInObject)(functions, vars);
    const functionArgs = JSON.parse(functionCall.arguments);
    const functionName = functionCall.name;
    const functionSchema = interpolatedFunctions?.find((f) => f.name === functionName)?.parameters;
    if (!functionSchema) {
        throw new Error(`Called "${functionName}", but there is no function with that name`);
    }
    const validate = ajv.compile(functionSchema);
    if (!validate(functionArgs)) {
        throw new Error(`Call to "${functionName}" does not match schema: ${JSON.stringify(validate.errors)}`);
    }
}
exports.validateFunctionCall = validateFunctionCall;
//# sourceMappingURL=openaiUtil.js.map