"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeModelParams = mergeModelParams;
exports.buildKernelDriverPolicy = buildKernelDriverPolicy;
exports.validateKernelDriverPolicyInvariants = validateKernelDriverPolicyInvariants;
exports.resolveKernelDriverPolicyViolationKind = resolveKernelDriverPolicyViolationKind;
const dialog_1 = require("../../dialog");
const fbr_1 = require("./fbr");
function isToollessFbrSelfSideDialog(dlg) {
    if (!(dlg instanceof dialog_1.SideDialog))
        return false;
    return dlg.assignmentFromAsker.callName === 'freshBootsReasoning';
}
function mergeModelParams(base, overlay) {
    if (!base && !overlay)
        return undefined;
    if (!base)
        return overlay;
    if (!overlay)
        return base;
    return {
        ...(base.json_response !== undefined ? { json_response: base.json_response } : {}),
        ...(overlay.json_response !== undefined ? { json_response: overlay.json_response } : {}),
        codex: { ...(base.codex ?? {}), ...(overlay.codex ?? {}) },
        openai: { ...(base.openai ?? {}), ...(overlay.openai ?? {}) },
        'openai-compatible': {
            ...(base['openai-compatible'] ?? {}),
            ...(overlay['openai-compatible'] ?? {}),
        },
        anthropic: { ...(base.anthropic ?? {}), ...(overlay.anthropic ?? {}) },
        'anthropic-compatible': {
            ...(base['anthropic-compatible'] ?? {}),
            ...(overlay['anthropic-compatible'] ?? {}),
        },
    };
}
function buildKernelDriverPolicy(args) {
    const { dlg, agent, systemPrompt, agentTools, language } = args;
    if (!isToollessFbrSelfSideDialog(dlg)) {
        return {
            mode: 'default',
            effectiveAgent: agent,
            effectiveSystemPrompt: systemPrompt,
            effectiveAgentTools: agentTools,
            prependedContextMessages: [],
            tellaskPolicy: 'allow_any',
            allowTellaskFunctions: true,
            allowFunctionCalls: true,
        };
    }
    const effectiveAgent = Object.assign(Object.create(agent), {
        model_params: mergeModelParams(agent.model_params, agent.fbr_model_params),
    });
    const isConclusionPhase = dlg.areFbrConclusionToolsEnabled();
    const mode = isConclusionPhase ? 'fbr_conclusion_only' : 'fbr_toolless';
    const noticePhase = isConclusionPhase ? 'finalization' : 'toolless';
    return {
        mode,
        effectiveAgent,
        effectiveSystemPrompt: (0, fbr_1.buildFbrSystemPrompt)(language, noticePhase),
        effectiveAgentTools: isConclusionPhase ? (0, fbr_1.buildFbrConclusionTools)(language) : [],
        prependedContextMessages: [
            {
                type: 'environment_msg',
                role: 'user',
                content: (0, fbr_1.buildFbrToolAvailabilityNotice)(language, noticePhase),
            },
        ],
        tellaskPolicy: 'deny_all',
        allowTellaskFunctions: false,
        allowFunctionCalls: isConclusionPhase,
    };
}
function validateKernelDriverPolicyInvariants(policy, language) {
    if (policy.mode === 'default') {
        return { ok: true };
    }
    if (policy.tellaskPolicy !== 'deny_all') {
        return { ok: false, detail: 'FBR tellaskPolicy must be deny_all.' };
    }
    if (policy.allowTellaskFunctions) {
        return { ok: false, detail: 'FBR allowTellaskFunctions must be false.' };
    }
    if (policy.prependedContextMessages.length !== 1) {
        return { ok: false, detail: 'FBR must prepend exactly one no-tools notice message.' };
    }
    const expectedNotice = (0, fbr_1.buildFbrToolAvailabilityNotice)(language, policy.mode === 'fbr_conclusion_only' ? 'finalization' : 'toolless');
    const [notice] = policy.prependedContextMessages;
    if (!notice ||
        notice.type !== 'environment_msg' ||
        notice.role !== 'user' ||
        notice.content !== expectedNotice) {
        return {
            ok: false,
            detail: 'FBR prepended notice must exactly match the expected phase notice.',
        };
    }
    if (policy.mode === 'fbr_toolless') {
        if (policy.effectiveAgentTools.length > 0) {
            return { ok: false, detail: 'Tool-less FBR effectiveAgentTools must be empty.' };
        }
        if (policy.allowFunctionCalls) {
            return { ok: false, detail: 'Tool-less FBR allowFunctionCalls must be false.' };
        }
    }
    if (policy.mode === 'fbr_conclusion_only') {
        if (policy.effectiveAgentTools.length !== 2) {
            return {
                ok: false,
                detail: 'FBR conclusion-only mode must expose exactly two conclusion tools.',
            };
        }
        if (!policy.allowFunctionCalls) {
            return { ok: false, detail: 'FBR conclusion-only mode must allow function calls.' };
        }
    }
    return { ok: true };
}
function hasTellaskPolicyViolation(policy, tellaskCallCount) {
    if (policy.tellaskPolicy === 'allow_any') {
        return false;
    }
    return tellaskCallCount > 0;
}
function resolveKernelDriverPolicyViolationKind(args) {
    const tellaskViolation = hasTellaskPolicyViolation(args.policy, args.tellaskCallCount);
    const toolViolation = !args.policy.allowFunctionCalls && args.functionCallCount > 0;
    if (tellaskViolation && toolViolation) {
        return 'tellask_and_tool';
    }
    if (tellaskViolation) {
        return 'tellask';
    }
    if (toolViolation) {
        return 'tool';
    }
    return null;
}
