UNPKG

n8n-nodes-google-gemini-embeddings-extended

Version:

n8n community sub-node for Google Gemini Embeddings with extended features like output dimensions support

94 lines (93 loc) 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.logWrapper = logWrapper; const n8n_workflow_1 = require("n8n-workflow"); 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 (error) { // Silently fail if logAiEvent is not available } } function logWrapper(originalInstance, executeFunctions) { console.log('EmbeddingsLogWrapper: Wrapping instance of type:', originalInstance.constructor.name); return new Proxy(originalInstance, { get(target, prop, receiver) { const originalValue = Reflect.get(target, prop, receiver); // Log all method calls for debugging if (typeof originalValue === 'function' && typeof prop === 'string') { console.log('EmbeddingsLogWrapper: Method accessed:', prop); } // Handle Embeddings - check for embedDocuments/embedQuery methods instead of instanceof if ('embedDocuments' in target || 'embedQuery' in target) { if (prop === 'embedDocuments' && 'embedDocuments' in target) { return async (documents) => { console.log('EmbeddingsLogWrapper: embedDocuments intercepted, docs:', documents?.length || 0); const connectionType = "ai_embedding" /* NodeConnectionType.AiEmbedding */; // Log input data const { index } = executeFunctions.addInputData(connectionType, [ [{ json: { documents } }], ]); // Call the original method with proper error handling const response = (await callMethodAsync.call(target, { executeFunctions, connectionType, currentNodeRunIndex: index, method: target[prop], arguments: [documents], })); console.log('EmbeddingsLogWrapper: embedDocuments completed, embeddings:', response?.length || 0); // Log AI event logAiEvent(executeFunctions, 'ai-document-embedded'); // Log output data executeFunctions.addOutputData(connectionType, index, [ [{ json: { response } }], ]); return response; }; } if (prop === 'embedQuery' && 'embedQuery' in target) { return async (query) => { console.log('EmbeddingsLogWrapper: embedQuery intercepted, query length:', query?.length || 0); const connectionType = "ai_embedding" /* NodeConnectionType.AiEmbedding */; // Log input data const { index } = executeFunctions.addInputData(connectionType, [ [{ json: { query } }], ]); // Call the original method with proper error handling const response = (await callMethodAsync.call(target, { executeFunctions, connectionType, currentNodeRunIndex: index, method: target[prop], arguments: [query], })); console.log('EmbeddingsLogWrapper: embedQuery completed, embedding dimension:', response?.length || 0); // Log AI event logAiEvent(executeFunctions, 'ai-query-embedded'); // Log output data executeFunctions.addOutputData(connectionType, index, [ [{ json: { response } }], ]); return response; }; } } return originalValue; }, }); }