UNPKG

n8n-nodes-semantic-splitter-with-context

Version:

Semantic Splitter with Context for n8n with LangChain integration

84 lines (83 loc) 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.logWrapper = logWrapper; const n8n_workflow_1 = require("n8n-workflow"); const textsplitters_1 = require("@langchain/textsplitters"); async function callMethodAsync(parameters) { try { return await parameters.method.call(this, ...parameters.arguments); } catch (error) { const connectedNode = parameters.executeFunctions.getNode(); throw new n8n_workflow_1.NodeOperationError(connectedNode, error); } } function logAiEvent(executeFunctions, eventType) { try { if ('logAiEvent' in executeFunctions && typeof executeFunctions.logAiEvent === 'function') { executeFunctions.logAiEvent({ type: eventType, }); } } catch { } } function logWrapper(originalInstance, executeFunctions) { console.log('LogWrapper: Wrapping instance of type:', originalInstance.constructor.name); return new Proxy(originalInstance, { get(target, prop, receiver) { const originalValue = Reflect.get(target, prop, receiver); if (typeof originalValue === 'function' && typeof prop === 'string') { console.log('LogWrapper: Method accessed:', prop); } if (originalInstance instanceof textsplitters_1.TextSplitter) { if (prop === 'splitText' && 'splitText' in target) { return async (text) => { console.log('LogWrapper: splitText intercepted, text length:', (text === null || text === void 0 ? void 0 : text.length) || 0); const connectionType = "ai_textSplitter"; const { index } = executeFunctions.addInputData(connectionType, [ [{ json: { textSplitter: text } }], ]); const response = (await callMethodAsync.call(target, { executeFunctions, connectionType, currentNodeRunIndex: index, method: target[prop], arguments: [text], })); console.log('LogWrapper: splitText completed, chunks:', (response === null || response === void 0 ? void 0 : response.length) || 0); logAiEvent(executeFunctions, 'ai-text-split'); executeFunctions.addOutputData(connectionType, index, [ [{ json: { response } }], ]); return response; }; } if (prop === 'splitDocuments' && 'splitDocuments' in target) { return async (documents) => { console.log('LogWrapper: splitDocuments intercepted, docs:', (documents === null || documents === void 0 ? void 0 : documents.length) || 0); const connectionType = "ai_textSplitter"; const { index } = executeFunctions.addInputData(connectionType, [ [{ json: { documents: documents.length } }], ]); const response = (await callMethodAsync.call(target, { executeFunctions, connectionType, currentNodeRunIndex: index, method: target[prop], arguments: [documents], })); console.log('LogWrapper: splitDocuments completed, chunks:', (response === null || response === void 0 ? void 0 : response.length) || 0); logAiEvent(executeFunctions, 'ai-text-split'); executeFunctions.addOutputData(connectionType, index, [ [{ json: { response: response.length } }], ]); return response; }; } } return originalValue; }, }); }