"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkflowToolService = void 0;
const tools_1 = require("@langchain/core/tools");
const isArray_1 = __importDefault(require("lodash/isArray"));
const isObject_1 = __importDefault(require("lodash/isObject"));
const manual = __importStar(require("n8n-nodes-base/dist/nodes/Set/v2/manual.mode"));
const GenericFunctions_1 = require("n8n-nodes-base/dist/utils/workflowInputsResourceMapping/GenericFunctions");
const n8n_workflow_1 = require("n8n-workflow");
const ai_utilities_1 = require("@n8n/ai-utilities");
function isNodeExecutionData(data) {
    return (0, isArray_1.default)(data) && Boolean(data.length) && (0, isObject_1.default)(data[0]) && 'json' in data[0];
}
class WorkflowToolService {
    constructor(baseContext, options) {
        this.baseContext = baseContext;
        this.returnAllItems = false;
        const subWorkflowInputs = this.baseContext.getNode().parameters
            .workflowInputs;
        this.useSchema = (subWorkflowInputs?.schema ?? []).length > 0;
        this.returnAllItems = options?.returnAllItems ?? false;
    }
    async createTool({ ctx, name, description, itemIndex, manualLogging = true, }) {
        const node = ctx.getNode();
        let runIndex = 'getNextRunIndex' in ctx ? ctx.getNextRunIndex() : 0;
        const toolHandler = async (query, runManager) => {
            let maxTries = 1;
            if (node.retryOnFail === true) {
                maxTries = Math.min(5, Math.max(2, node.maxTries ?? 3));
            }
            let waitBetweenTries = 0;
            if (node.retryOnFail === true) {
                waitBetweenTries = Math.min(5000, Math.max(0, node.waitBetweenTries ?? 1000));
            }
            let lastError;
            for (let tryIndex = 0; tryIndex < maxTries; tryIndex++) {
                const localRunIndex = runIndex++;
                let context = this.baseContext;
                if ('cloneWith' in this.baseContext) {
                    context = this.baseContext.cloneWith({
                        runIndex: localRunIndex,
                        inputData: [[{ json: { query } }]],
                    });
                }
                const abortSignal = context.getExecutionCancelSignal?.();
                if (abortSignal?.aborted) {
                    return 'There was an error: "Execution was cancelled"';
                }
                if (tryIndex !== 0) {
                    lastError = undefined;
                    if (waitBetweenTries !== 0) {
                        try {
                            await (0, n8n_workflow_1.sleepWithAbort)(waitBetweenTries, abortSignal);
                        }
                        catch (abortError) {
                            return 'There was an error: "Execution was cancelled"';
                        }
                    }
                }
                try {
                    const response = await this.runFunction(context, query, itemIndex, runManager);
                    const processedResponse = this.handleToolResponse(response);
                    let responseData;
                    if (isNodeExecutionData(response)) {
                        responseData = response;
                    }
                    else {
                        const reParsedData = (0, n8n_workflow_1.jsonParse)(processedResponse, {
                            fallbackValue: { response: processedResponse },
                        });
                        responseData = [{ json: reParsedData }];
                    }
                    let metadata;
                    if (this.subExecutionId && this.subWorkflowId) {
                        metadata = {
                            subExecution: {
                                executionId: this.subExecutionId,
                                workflowId: this.subWorkflowId,
                            },
                        };
                    }
                    if (manualLogging) {
                        void context.addOutputData(n8n_workflow_1.NodeConnectionTypes.AiTool, localRunIndex, [responseData], metadata);
                        return processedResponse;
                    }
                    if (metadata && 'setMetadata' in context) {
                        void context.setMetadata(metadata);
                    }
                    return responseData;
                }
                catch (error) {
                    if (abortSignal?.aborted) {
                        return 'There was an error: "Execution was cancelled"';
                    }
                    const executionError = error;
                    lastError = executionError;
                    const errorResponse = `There was an error: "${executionError.message}"`;
                    if (manualLogging) {
                        const metadata = (0, n8n_workflow_1.parseErrorMetadata)(error);
                        const errorData = [{ json: { error: errorResponse } }];
                        void context.addOutputData(n8n_workflow_1.NodeConnectionTypes.AiTool, localRunIndex, [errorData], metadata);
                    }
                    if (tryIndex === maxTries - 1) {
                        if (!manualLogging) {
                            throw executionError;
                        }
                        return errorResponse;
                    }
                }
            }
            return `There was an error: ${lastError?.message ?? 'Unknown error'}`;
        };
        return this.useSchema
            ? this.createStructuredTool(name, description, toolHandler)
            : new tools_1.DynamicTool({ name, description, func: toolHandler });
    }
    handleToolResponse(response) {
        if (typeof response === 'number') {
            return response.toString();
        }
        if (isNodeExecutionData(response)) {
            return JSON.stringify(response.map((item) => item.json), null, 2);
        }
        if ((0, isObject_1.default)(response)) {
            return JSON.stringify(response, null, 2);
        }
        if (typeof response !== 'string') {
            throw new n8n_workflow_1.NodeOperationError(this.baseContext.getNode(), 'Wrong output type returned', {
                description: `The response property should be a string, but it is an ${typeof response}`,
            });
        }
        return response;
    }
    async executeSubWorkflow(context, workflowInfo, items, workflowProxy, runManager) {
        let receivedData;
        try {
            receivedData = await context.executeWorkflow(workflowInfo, items, runManager?.getChild(), {
                parentExecution: {
                    executionId: workflowProxy.$execution.id,
                    workflowId: workflowProxy.$workflow.id,
                },
                returnLastRunOnly: true,
            });
            this.subExecutionId = receivedData.executionId;
        }
        catch (error) {
            throw new n8n_workflow_1.NodeOperationError(context.getNode(), error);
        }
        let response;
        if (this.returnAllItems) {
            response = receivedData?.data?.[0]?.length ? receivedData.data[0] : undefined;
        }
        else {
            response = receivedData?.data?.[0]?.[0]?.json;
        }
        if (response === undefined) {
            throw new n8n_workflow_1.NodeOperationError(context.getNode(), 'There was an error: "The workflow did not return a response"');
        }
        return { response, subExecutionId: receivedData.executionId };
    }
    async runFunction(context, query, itemIndex, runManager) {
        const source = context.getNodeParameter('source', itemIndex);
        const workflowProxy = context.getWorkflowDataProxy(0);
        const { workflowInfo } = await this.getSubWorkflowInfo(context, source, itemIndex, workflowProxy);
        const rawData = this.prepareRawData(context, query, itemIndex);
        const items = await this.prepareWorkflowItems(context, query, itemIndex, rawData);
        this.subWorkflowId = workflowInfo.id;
        const { response } = await this.executeSubWorkflow(context, workflowInfo, items, workflowProxy, runManager);
        return response;
    }
    async getSubWorkflowInfo(context, source, itemIndex, workflowProxy) {
        const workflowInfo = {};
        let subWorkflowId;
        if (source === 'database') {
            const { value } = context.getNodeParameter('workflowId', itemIndex, {});
            workflowInfo.id = value;
            subWorkflowId = workflowInfo.id;
        }
        else if (source === 'parameter') {
            const workflowJson = context.getNodeParameter('workflowJson', itemIndex);
            try {
                workflowInfo.code = JSON.parse(workflowJson);
                subWorkflowId = workflowProxy.$workflow.id;
            }
            catch (error) {
                throw new n8n_workflow_1.NodeOperationError(context.getNode(), `The provided workflow is not valid JSON: "${error.message}"`, { itemIndex });
            }
        }
        return { workflowInfo, subWorkflowId: subWorkflowId };
    }
    prepareRawData(context, query, itemIndex) {
        const rawData = { query };
        const workflowFieldsJson = context.getNodeParameter('fields.values', itemIndex, [], {
            rawExpressions: true,
        });
        for (const entry of workflowFieldsJson) {
            if (entry.type === 'objectValue' && entry.objectValue.startsWith('=')) {
                rawData[entry.name] = entry.objectValue.replace(/^=+/, '');
            }
        }
        return rawData;
    }
    async prepareWorkflowItems(context, query, itemIndex, rawData) {
        const options = { include: 'all' };
        let jsonData = typeof query === 'object' ? query : { query };
        if (this.useSchema) {
            const currentWorkflowInputs = GenericFunctions_1.getCurrentWorkflowInputData.call(context);
            jsonData = currentWorkflowInputs[itemIndex].json;
        }
        const newItem = await manual.execute.call(context, { json: jsonData }, itemIndex, options, rawData, context.getNode());
        return [newItem];
    }
    createStructuredTool(name, description, func) {
        const collectedArguments = (0, ai_utilities_1.extractFromAIParameters)(this.baseContext.getNode().parameters);
        if (collectedArguments.length === 0) {
            return new tools_1.DynamicTool({ name, description, func });
        }
        const schema = (0, ai_utilities_1.createZodSchemaFromArgs)(collectedArguments);
        return new tools_1.DynamicStructuredTool({ schema, name, description, func });
    }
}
exports.WorkflowToolService = WorkflowToolService;
//# sourceMappingURL=WorkflowToolService.js.map