"use strict";
// import { spawn } from 'child_process';
// import path from 'path';
// import { getAndCheckProvider } from '../../matchers';
// import { getDefaultProviders } from '../../providers/defaults';
// import type { AssertionParams, GradingResult } from '../../types';
// import invariant from '../../util/invariant';
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleToolCorrectness = void 0;
// interface ToolCorrectnessResult {
//   pass: boolean;
//   score: number;
//   reason: string;
// }
// async function executeAndGradeToolCorrectness(
//   llmOutput: string,
//   modelProvider: object,
//   providerType: string,
//   context: Record<string, any>,
// ): Promise<ToolCorrectnessResult> {
//   const pythonScriptPath = path.join(__dirname, '..', 'python', 'tool_correctness.py');
//   const contextJson = JSON.stringify(context);
//   const jsonPromise = new Promise<string>((resolve, reject) => {
//     const py = spawn(
//       'python3',
//       [pythonScriptPath, llmOutput, JSON.stringify(modelProvider), providerType],
//       {
//         env: process.env,
//         stdio: ['pipe', 'pipe', 'pipe'],
//       },
//     );
//     let stdout = '',
//       stderr = '';
//     py.stdin.write(contextJson);
//     py.stdin.end();
//     py.stdout.on('data', (chunk) => {
//       if (chunk.toString().includes('__DEBUG__')) {
//         console.log(`[PYTHON STDOUT]: ${chunk.toString()}`);
//       } else {
//         stdout += chunk.toString();
//       }
//     });
//     py.stderr.on('data', (chunk) => {
//       if (chunk.toString().includes('__DEBUG__')) {
//         console.log(`[PYTHON STDERR]: ${chunk.toString()}`);
//       } else {
//         stderr += chunk.toString();
//       }
//     });
//     py.on('close', (code) => {
//       if (code === 0) {
//         resolve(stdout.trim());
//       } else {
//         reject(new Error(`Python script failed with exit code ${code}\nStderr: ${stderr}`));
//       }
//     });
//     py.on('error', (err) => {
//       reject(new Error(`Failed to spawn Python process: ${err.message}`));
//     });
//   });
//   try {
//     return JSON.parse(await jsonPromise) as ToolCorrectnessResult;
//   } catch (e) {
//     return {
//       pass: false,
//       score: 0,
//       reason: `evaluation failed: ${e instanceof Error ? e.message : 'unknown error'}`,
//     };
//   }
// }
// export const handleToolCorrectness = async ({
//   assertion,
//   outputString,
//   test,
//   context,
// }: AssertionParams): Promise<GradingResult> => {
//   invariant(typeof outputString === 'string', 'output must be string');
//   const textProvider = await getAndCheckProvider(
//     'text',
//     test.options?.provider,
//     (await getDefaultProviders()).gradingProvider,
//     'tool-correctness',
//   );
//   const providerType = textProvider.constructor.name;
//   const modelProvider = textProvider;
//   const contextForPython: any = context;
//   const grading = await executeAndGradeToolCorrectness(
//     outputString,
//     modelProvider,
//     providerType,
//     contextForPython,
//   );
//   return {
//     assertion,
//     pass: grading.pass,
//     score: grading.score,
//     reason: grading.reason,
//   };
// };
const path_1 = __importDefault(require("path"));
const pythonUtils_1 = require("../../python/pythonUtils");
const util_1 = require("./util");
const handleToolCorrectness = async ({ assertion, outputString, test, context, workspaceId, }) => {
    context.vars = { ...(context.test?.vars ?? {}), ...(context.vars ?? {}) };
    const { vars: mergedVars, outputString: resolvedOutputString } = (0, util_1.applyTraceFromOutputString)(outputString, context.vars);
    context.vars = mergedVars;
    const assertionObj = assertion;
    if (assertionObj.tools) {
        if (Array.isArray(assertionObj.tools)) {
            context.vars['expected_tools'] = assertionObj.tools;
        }
        else if (typeof assertionObj.tools === 'string') {
            context.vars['expected_tools'] = assertionObj.tools.split(',').map((s) => s.trim());
        }
    }
    // Build a clean trusys snapshot so Python always has typed, reliable data regardless
    // of how safeJsonStringify handles the broader context object.
    const trusys = {
        tool_calls: Array.isArray(mergedVars.tool_calls) ? mergedVars.tool_calls : [],
        agent_calls: Array.isArray(mergedVars.agent_calls) ? mergedVars.agent_calls : [],
        tool_definitions: Array.isArray(mergedVars.tool_definitions) ? mergedVars.tool_definitions : [],
        agent_definitions: Array.isArray(mergedVars.agent_definitions) ? mergedVars.agent_definitions : [],
        plan: mergedVars.plan ?? '',
        expected_tools: context.vars['expected_tools'] ?? [],
        prompt: mergedVars.prompt ?? context.prompt ?? '',
    };
    context.trusys = trusys;
    const pythonScriptPath = path_1.default.join(__dirname, '..', 'python', 'tool_correctness.py');
    const pythonScriptOutput = await (0, pythonUtils_1.runPython)(pythonScriptPath, 'get_assert', [
        resolvedOutputString,
        context,
    ]);
    return {
        assertion,
        ...pythonScriptOutput,
    };
};
exports.handleToolCorrectness = handleToolCorrectness;
//# sourceMappingURL=toolCorrectness.js.map