"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasGatedToolNodeName = hasGatedToolNodeName;
exports.extractHitlMetadata = extractHitlMetadata;
exports.createEngineRequests = createEngineRequests;
const isObject_1 = __importDefault(require("lodash/isObject"));
const omit_1 = __importDefault(require("lodash/omit"));
const n8n_workflow_1 = require("n8n-workflow");
const types_1 = require("./types");
function hasGatedToolNodeName(metadata) {
    return ((0, isObject_1.default)(metadata) &&
        typeof metadata.gatedToolNodeName === 'string');
}
function extractHitlMetadata(metadata, toolName, toolInput) {
    if (!hasGatedToolNodeName(metadata))
        return undefined;
    return {
        gatedToolNodeName: metadata.gatedToolNodeName,
        toolName,
        originalInput: toolInput.toolParameters,
    };
}
function extractThinkingMetadata(toolCall, sharedMessageLog, sharedAdditionalKwargs) {
    const result = {};
    const effectiveAdditionalKwargs = toolCall.additionalKwargs ?? sharedAdditionalKwargs;
    const effectiveMessageLog = toolCall.messageLog && toolCall.messageLog.length > 0 ? toolCall.messageLog : sharedMessageLog;
    let thoughtSignature;
    if (effectiveAdditionalKwargs) {
        const geminiSignatures = effectiveAdditionalKwargs['__gemini_function_call_thought_signatures__'];
        if (geminiSignatures && typeof geminiSignatures === 'object') {
            thoughtSignature =
                geminiSignatures[toolCall.toolCallId] || Object.values(geminiSignatures)[0];
        }
        if (!thoughtSignature) {
            const signatures = effectiveAdditionalKwargs.signatures;
            if (signatures && Array.isArray(signatures) && signatures.length > 0) {
                thoughtSignature = signatures.find((s) => s && s.length > 0);
            }
        }
    }
    let thinkingContent;
    let thinkingType;
    let thinkingSignature;
    if (effectiveMessageLog && Array.isArray(effectiveMessageLog)) {
        for (const message of effectiveMessageLog) {
            if (message && typeof message === 'object' && 'content' in message) {
                const content = message.content;
                if (Array.isArray(content)) {
                    for (const block of content) {
                        if (!thoughtSignature && (0, types_1.isGeminiThoughtSignatureBlock)(block)) {
                            thoughtSignature = block.thoughtSignature;
                        }
                        if ((0, types_1.isThinkingBlock)(block)) {
                            thinkingContent = block.thinking;
                            thinkingType = 'thinking';
                            thinkingSignature = block.signature;
                        }
                        else if ((0, types_1.isRedactedThinkingBlock)(block)) {
                            thinkingContent = block.data;
                            thinkingType = 'redacted_thinking';
                        }
                    }
                }
                if (!thoughtSignature && 'additional_kwargs' in message) {
                    const msgAdditionalKwargs = message.additional_kwargs;
                    if (msgAdditionalKwargs) {
                        const geminiSignatures = msgAdditionalKwargs['__gemini_function_call_thought_signatures__'];
                        if (geminiSignatures && typeof geminiSignatures === 'object') {
                            thoughtSignature =
                                geminiSignatures[toolCall.toolCallId] || Object.values(geminiSignatures)[0];
                        }
                        if (!thoughtSignature) {
                            const signatures = msgAdditionalKwargs.signatures;
                            const msgToolCalls = 'tool_calls' in message
                                ? message.tool_calls
                                : undefined;
                            if (signatures && Array.isArray(signatures)) {
                                if (msgToolCalls && Array.isArray(msgToolCalls)) {
                                    const toolCallIndex = msgToolCalls.findIndex((tc) => tc.id === toolCall.toolCallId);
                                    if (toolCallIndex > 0 && toolCallIndex < signatures.length) {
                                        thoughtSignature = signatures[toolCallIndex];
                                    }
                                }
                                thoughtSignature ??= signatures.find((s) => s && s.length > 0);
                            }
                        }
                    }
                }
                if (thoughtSignature || thinkingContent)
                    break;
            }
        }
    }
    if (thoughtSignature) {
        result.google = { thoughtSignature };
    }
    if (thinkingContent && thinkingType) {
        result.anthropic = {
            thinkingContent,
            thinkingType,
            ...(thinkingSignature ? { thinkingSignature } : {}),
        };
    }
    return result;
}
function createEngineRequests(toolCalls, itemIndex, tools) {
    const sharedMessageLog = toolCalls.find((tc) => tc.messageLog && tc.messageLog.length > 0)?.messageLog;
    const sharedAdditionalKwargs = toolCalls.find((tc) => tc.additionalKwargs)?.additionalKwargs;
    return toolCalls
        .map((toolCall) => {
        const foundTool = tools.find((tool) => tool.name === toolCall.tool);
        if (!foundTool)
            return undefined;
        const nodeName = foundTool.metadata?.sourceNodeName;
        if (typeof nodeName !== 'string')
            return undefined;
        const metadata = (foundTool.metadata ?? {});
        const toolInput = toolCall.toolInput;
        const hitlMetadata = extractHitlMetadata(metadata, toolCall.tool, toolInput);
        let input = toolInput;
        if (metadata.isFromToolkit) {
            input = { ...input, tool: toolCall.tool };
        }
        if (hitlMetadata) {
            input = {
                ...(0, omit_1.default)(input, 'hitlParameters'),
                ...input.hitlParameters,
                toolParameters: input.toolParameters,
            };
        }
        return {
            actionType: 'ExecutionNodeAction',
            nodeName,
            input,
            type: n8n_workflow_1.NodeConnectionTypes.AiTool,
            id: toolCall.toolCallId,
            metadata: {
                itemIndex,
                hitl: hitlMetadata,
                toolName: toolCall.tool,
                ...extractThinkingMetadata(toolCall, sharedMessageLog, sharedAdditionalKwargs),
            },
        };
    })
        .filter((item) => item !== undefined);
}
//# sourceMappingURL=createEngineRequests.js.map