"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.customProviderEvaluationService = customProviderEvaluationService;
const drizzle_orm_1 = require("drizzle-orm");
const uuid_1 = require("uuid");
const database_1 = require("../../database");
const tables_1 = require("../../database/tables");
const logger_1 = require("../../logger");
const providers_1 = require("../../providers");
const assertCustomAIExecutionPolicy_1 = require("../../util/customAIApplication/assertCustomAIExecutionPolicy");
async function customProviderEvaluationService({ applicationPath, library, testRunId, projectId, ...rest }) {
    const db = await (0, database_1.getDb)();
    const { prompts, tests } = rest;
    const paths = Array.isArray(applicationPath) ? applicationPath : [applicationPath];
    for (const providerPath of paths) {
        (0, assertCustomAIExecutionPolicy_1.assertServerAllowsLocalCustomProviderPath)(String(providerPath ?? ''));
    }
    const applications = [];
    let usedTestRunId = testRunId;
    const results = [];
    try {
        // Create or update test run
        const thisTestRunId = usedTestRunId ?? (0, uuid_1.v4)();
        if (!usedTestRunId) {
            usedTestRunId = thisTestRunId;
        }
        // Insert or update test run
        const existing = await db
            .select({ id: tables_1.testRunsTable.id })
            .from(tables_1.testRunsTable)
            .where((0, drizzle_orm_1.eq)(tables_1.testRunsTable.id, thisTestRunId));
        if (existing.length > 0) {
            await db
                .update(tables_1.testRunsTable)
                .set({ status: 'pending' })
                .where((0, drizzle_orm_1.eq)(tables_1.testRunsTable.id, thisTestRunId));
        }
        else {
            await db.insert(tables_1.testRunsTable).values({
                id: thisTestRunId,
                status: 'pending',
                projectId: projectId ?? null,
                applicationIds: paths,
                libraryId: library,
            });
        }
        const test = tests[0];
        const prompt = prompts[0];
        const providers = await (0, providers_1.loadApiProviders)(paths);
        const response = await providers[0].callApi(prompt, {
            vars: test.vars,
            prompt,
            filters: undefined,
            test,
            logger: logger_1.logger,
        }, {});
        logger_1.logger.debug(`Provider response properties: ${Object.keys(response).join(', ')}`);
        logger_1.logger.debug(`Provider response cached property explicitly: ${response.cached}`);
        console.log(providers);
        // TODO: Replace with real evaluation logic
        // Simulate evaluation and mark as complete
        await db
            .update(tables_1.testRunsTable)
            .set({ status: 'completed' })
            .where((0, drizzle_orm_1.eq)(tables_1.testRunsTable.id, thisTestRunId));
        results.push({
            application: 'custom',
            testRunId: thisTestRunId,
            status: 'completed',
        });
    }
    catch (e) {
        const errMsg = e instanceof Error ? e.message : String(e);
        results.push({ error: errMsg });
    }
    return {
        message: 'Custom provider evaluation completed',
        applicationsEvaluated: applications.length,
        libraryId: library,
        testRunIds: results?.map((r) => r.testRunId).filter(Boolean),
        results,
    };
}
//# sourceMappingURL=customProviderEvaluationService.js.map