"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAudioLanguage = exports.handleLanguage = void 0;
const dedent_1 = __importDefault(require("dedent"));
const invariant_1 = __importDefault(require("../../util/invariant"));
const defaults_1 = require("../../providers/defaults");
const handleLanguage = async ({ assertion, renderedValue, prompt, output, test, evaluateResult, }) => {
    (0, invariant_1.default)(typeof renderedValue === 'string' || renderedValue === undefined, 'Language assertion type must have a string value');
    if (evaluateResult) {
        const result = output.map(str => str.toLowerCase());
        const rendered = typeof renderedValue === 'string' ? renderedValue.toLowerCase() : renderedValue;
        const pass = rendered ? result.includes(rendered) : true;
        return {
            pass,
            score: pass ? 1 : 0,
            reason: result.join(', '),
            assertion,
        };
    }
    return {
        pass: true,
        score: 1,
        reason: '',
        metadata: {
            prompt: `Determine the languages spoken in the call and output them as an array (e.g. ["English"]). ` +
                `Provide the result in a metric called 'language'.`,
            metric: '"language": "English",',
        },
    };
};
exports.handleLanguage = handleLanguage;
const getAudioLanguage = async (outputAudio, workspaceId) => {
    const systemPrompt = (0, dedent_1.default) `
    You are an expert at identifying languages from audio recordings. Given an audio recording, identify the primary language spoken in the recording.

    Your entire reply must be exactly one token: either the two-letter ISO 639-1 language code in lowercase (e.g. en, es, hi) or the word none.

    Do not use JSON, objects, key-value pairs, quotes, labels, markdown, code fences, or any explanation—only those two letters or none, with nothing before or after.
    If the language is unclear or you cannot process the audio, reply with exactly: none
    `;
    const llmPrompt = [
        {
            role: 'system',
            content: [{ type: 'text', text: systemPrompt }],
        },
        {
            role: 'user',
            content: [
                {
                    type: 'text',
                    text: 'Identify the language spoken in the following audio. Output only the two-letter ISO 639-1 code or none—no JSON, no other text.',
                },
                {
                    type: 'input_audio',
                    input_audio: { data: outputAudio?.data, format: outputAudio?.format },
                },
            ],
        },
    ];
    const textProvider = (await (0, defaults_1.getDefaultProviders)(undefined, workspaceId)).audioProvider; //TODO: Hardcoded for now
    const promptText = JSON.stringify(llmPrompt);
    const resp = await textProvider.callApi(promptText);
    const result = (resp.output || '').toString().trim();
    return {
        language: result,
        tokenUsage: resp.tokenUsage,
    };
};
exports.getAudioLanguage = getAudioLanguage;
//# sourceMappingURL=language.js.map