UNPKG

n8n-nodes-smartgent

Version:

SmartGent custom nodes for n8n - AI-powered automation and intelligent workflow integrations including LiteLLM chat completions, SharePoint file monitoring, and enterprise search

174 lines 7.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SmartGentEnterpriseSearchTool = void 0; const n8n_workflow_1 = require("n8n-workflow"); class SmartGentEnterpriseSearchTool { constructor() { this.description = { displayName: 'SmartGent Enterprise Search Tool', name: 'smartGentEnterpriseSearchTool', icon: 'file:smartgent.svg', group: ['transform'], version: 1, description: 'SmartGent Enterprise Search tool for AI Agent nodes', defaults: { name: 'SmartGent Enterprise Search Tool', }, inputs: ['main'], outputs: ['main'], usableAsTool: true, credentials: [ { name: 'smartGentApi', required: true, }, ], properties: [ { displayName: 'Chatbot Name or ID', name: 'chatbotGuid', type: 'options', typeOptions: { loadOptionsMethod: 'getChatbots', }, default: '', required: true, description: 'Select the chatbot to use for search. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.', }, { displayName: 'Search Query', name: 'query', type: 'string', default: '', required: true, placeholder: 'Enter your search query...', description: 'The search query to execute', }, { displayName: 'Additional Options', name: 'additionalFields', type: 'collection', placeholder: 'Add Field', default: {}, options: [ { displayName: 'Max Results', name: 'maxResults', type: 'number', default: 10, description: 'Maximum number of results to return', }, ], }, ], }; this.methods = { loadOptions: { async getChatbots() { try { const credentials = await this.getCredentials('smartGentApi'); const options = { method: 'GET', url: `${credentials.baseUrl}/api/v1/rag/enterprise/group/chatbot`, headers: { 'smartgen-api-key': credentials.apiKey, Accept: 'application/json', }, json: true, }; const response = await this.helpers.httpRequest(options); if (response.success && Array.isArray(response.result)) { return response.result.map((chatbot) => ({ name: chatbot.name, value: chatbot.guid, })); } return []; } catch (error) { return []; } }, }, }; } async execute() { const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const chatbotGuid = this.getNodeParameter('chatbotGuid', i); const query = this.getNodeParameter('query', i); const additionalFields = this.getNodeParameter('additionalFields', i); const formData = { question: query, chatbot_guid: chatbotGuid, }; if (additionalFields.maxResults) { formData.limit = additionalFields.maxResults.toString(); } const formDataString = Object.keys(formData) .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(formData[key])}`) .join('&'); const credentials = await this.getCredentials('smartGentApi'); const options = { method: 'POST', url: `${credentials.baseUrl}/api/v1/rag/enterprise/groupsearch`, headers: { 'smartgen-api-key': credentials.apiKey, Accept: 'application/json', 'Content-Type': 'application/x-www-form-urlencoded', }, body: formDataString, json: true, }; const response = await this.helpers.httpRequest(options); let formattedResponse = ''; if (response.success && response.docs && Array.isArray(response.docs)) { const results = response.docs .map((doc, index) => { return `Result ${index + 1}:\n${doc.content}\nSource: ${doc.org_filename || 'Unknown'} (Page ${doc.page_number || 'Unknown'})`; }) .join('\n\n---\n\n'); formattedResponse = `Found ${response.docs.length} results for "${query}":\n\n${results}`; } else { formattedResponse = `No results found for "${query}".`; } const executionData = { json: { query, chatbotGuid, response: formattedResponse, rawResults: response, timestamp: new Date().toISOString(), resultCount: Array.isArray(response.docs) ? response.docs.length : 0, }, pairedItem: { item: i }, }; returnData.push(executionData); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message, query: this.getNodeParameter('query', i), timestamp: new Date().toISOString(), }, pairedItem: { item: i }, }); } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex: i, description: `Failed to execute SmartGent Enterprise Search Tool: ${error.message}`, }); } } } return [returnData]; } } exports.SmartGentEnterpriseSearchTool = SmartGentEnterpriseSearchTool; //# sourceMappingURL=SmartGentEnterpriseSearchTool.node.js.map