"use strict";
/**
 * Argus UI contexts for functional / security **test runs** (list + result pages).
 * Bare UUIDs in the user message are test_run_id values, not trace/session ids.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TEST_RUN_EVAL_CONTEXT_SOURCES = void 0;
exports.isTestRunEvalContextSource = isTestRunEvalContextSource;
exports.extractUuidsFromText = extractUuidsFromText;
exports.applyTestRunIdFromEvalContext = applyTestRunIdFromEvalContext;
exports.buildTestRunScopedContextHint = buildTestRunScopedContextHint;
exports.TEST_RUN_EVAL_CONTEXT_SOURCES = new Set([
    'functional_eval_list',
    'security_eval_list',
    'functional_eval',
    'security_eval',
]);
const UUID_IN_TEXT_RE = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/gi;
function isTestRunEvalContextSource(contextSource) {
    if (!contextSource) {
        return false;
    }
    return exports.TEST_RUN_EVAL_CONTEXT_SOURCES.has(String(contextSource).trim());
}
function extractUuidsFromText(text) {
    const matches = String(text ?? '').match(UUID_IN_TEXT_RE) ?? [];
    return [...new Set(matches.map((m) => m.toLowerCase()))];
}
function pickTestRunIdFromMessageUuids(uuids, filters) {
    if (uuids.length === 0) {
        return undefined;
    }
    const appId = typeof filters.applicationId === 'string' ? filters.applicationId.trim().toLowerCase() : '';
    const withoutApp = uuids.filter((u) => u !== appId);
    if (withoutApp.length === 1) {
        return withoutApp[0];
    }
    if (withoutApp.length > 1) {
        return withoutApp[withoutApp.length - 1];
    }
    return uuids.length === 1 ? uuids[0] : uuids[uuids.length - 1];
}
/**
 * When Argus is opened from a test-run UI, map UUIDs in the user query to testRunId filters
 * and avoid sticky trace/session/eval ids that share the same value.
 */
function applyTestRunIdFromEvalContext(query, contextSource, filters) {
    if (!isTestRunEvalContextSource(contextSource)) {
        return filters;
    }
    const next = { ...(filters || {}) };
    const existing = (typeof next.testRunId === 'string' && next.testRunId.trim()) ||
        (typeof next.test_run_id === 'string' && next.test_run_id.trim()) ||
        '';
    let testRunId = existing;
    if (!testRunId) {
        testRunId = pickTestRunIdFromMessageUuids(extractUuidsFromText(query), next) ?? '';
    }
    if (!testRunId) {
        return next;
    }
    next.testRunId = testRunId;
    next.test_run_id = testRunId;
    const normalized = testRunId.toLowerCase();
    for (const key of ['traceId', 'sessionId', 'evaluationId', 'evalId', 'spanId']) {
        const v = next[key];
        if (typeof v === 'string' && v.trim().toLowerCase() === normalized) {
            delete next[key];
        }
    }
    return next;
}
function buildTestRunScopedContextHint(contextSource) {
    if (!isTestRunEvalContextSource(contextSource)) {
        return '';
    }
    const fromList = contextSource === 'functional_eval_list' || contextSource === 'security_eval_list';
    return ('\n\nUI context (test runs): This Argus session is from the **functional or security test run** product area—not trace/session telemetry.' +
        ' Any UUID the user mentions ("this id", "summarise …", run id, eval id, security test run id) is **test_run_id** (one executed batch in eval_results), **not** traceId, TraceId, trace_id, or sessionId.' +
        ' Use clickhouse_text_to_sql on **eval_results** with `WHERE test_run_id = \'<id>\'` (and is_redteam when appropriate).' +
        ' Do not query otel_traces or filter eval_results.trace_id unless the user explicitly asks about traces or spans.' +
        (fromList
            ? ' From the **list** page the user may reference any run in the project; still treat bare UUIDs as test_run_id.'
            : ''));
}
//# sourceMappingURL=argusEvalContext.js.map