UNPKG

@kaia-team/n8n-nodes-kaia

Version:
448 lines 14.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.assistantsApiProperties = void 0; exports.handleAssistantsApi = handleAssistantsApi; const n8n_workflow_1 = require("n8n-workflow"); const utils_1 = require("../utils"); /** * Assistants API Properties * Properties specific to the assistants API resource */ exports.assistantsApiProperties = [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['assistants'], }, }, options: [ { name: 'Create', value: 'create', action: 'Create an assistant', }, { name: 'Delete', value: 'delete', action: 'Delete an assistant', }, { name: 'Get Available Models', value: 'getAvailableModels', action: 'Get available models for assistants', }, { name: 'List', value: 'list', action: 'List assistants', }, { name: 'Read', value: 'read', action: 'Read an assistant', }, { name: 'Update', value: 'update', action: 'Update an assistant', }, ], default: 'list', }, { displayName: 'Assistant ID', name: 'assistantId', type: 'string', default: '', displayOptions: { show: { resource: ['assistants'], operation: ['read', 'update', 'delete'], }, }, required: true, }, { displayName: 'Title', name: 'title', type: 'string', default: '', displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, }, { displayName: 'Asst UUID', name: 'asst_uuid', type: 'string', default: '', displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, }, { displayName: 'Communications Flow', name: 'communications_flow', type: 'fixedCollection', typeOptions: { multipleValues: true, }, default: {}, placeholder: 'Add Flow Item', displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, options: [ { name: 'flowItems', displayName: 'Flow Items', values: [ { displayName: 'Key', name: 'key', type: 'string', default: '', description: 'Unique key (ignored by server but required for UI)', }, { displayName: 'Enabled', name: 'enabled', type: 'boolean', default: true, }, { displayName: 'Body', name: 'body', type: 'string', default: '', }, { displayName: 'Position', name: 'position', type: 'number', default: 0, }, ], }, ], }, { displayName: 'Avatar', name: 'avatar', type: 'string', default: '', displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, description: 'Path to avatar file or base64 data', }, { displayName: 'Remove Avatar', name: 'remove_avatar', type: 'boolean', default: false, displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, }, { displayName: 'Temp', name: 'temp', type: 'number', typeOptions: { minValue: 0, maxValue: 2, numberStepSize: 0.01, }, default: 1, displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, description: 'Temperature (0.00 - 2.00)', }, { displayName: 'Model', name: 'model', type: 'string', default: '', displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, description: 'Model name (must match available_models.JSON; invalid values will raise error)', }, { displayName: 'Markdown Enabled', name: 'markdown_enabled', type: 'boolean', default: false, displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, description: 'Whether to enable markdown for the assistant. Only for reasoning models.', }, { displayName: 'Reasoning Effort', name: 'reasoning_effort', type: 'options', options: [ { name: 'High', value: 'high' }, { name: 'Low', value: 'low' }, { name: 'Medium', value: 'medium' }, ], default: 'medium', displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, }, { displayName: 'Requires Login', name: 'requires_login', type: 'boolean', default: false, displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, }, { displayName: 'Required Login Message', name: 'required_login_message', type: 'string', default: '', displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, }, { displayName: 'Vec DB IDs', name: 'vec_db_ids', type: 'fixedCollection', typeOptions: { multipleValues: true, }, default: {}, placeholder: 'Add Vec DB', displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, options: [ { name: 'vecDbItems', displayName: 'Vec DB Items', values: [ { displayName: 'ID', name: 'id', type: 'string', default: '', }, { displayName: 'Weight', name: 'weight', type: 'number', default: 1, }, ], }, ], }, { displayName: 'Function Calls', name: 'function_calls', type: 'string', default: '', displayOptions: { show: { resource: ['assistants'], operation: ['create', 'update'], }, }, description: 'Comma-separated list of function call IDs', }, ]; /** * Assistants API Handler * Handles requests to the assistants API */ async function handleAssistantsApi(executeFunctions, itemIndex, adminUrl, adminToken) { const operation = executeFunctions.getNodeParameter('operation', itemIndex); const additionalFields = executeFunctions.getNodeParameter('additionalFields', itemIndex, {}); const headers = (0, utils_1.createHeaders)(adminToken, additionalFields); if (operation === 'list') { try { const response = await executeFunctions.helpers.httpRequest({ method: 'GET', url: `${adminUrl}/assistants.json`, headers, }); return executeFunctions.helpers.returnJsonArray(response); } catch (error) { (0, utils_1.handleHttpError)(error, executeFunctions.getNode()); } } else if (operation === 'getAvailableModels') { try { const response = await executeFunctions.helpers.httpRequest({ method: 'GET', url: `${adminUrl}/assistants/available_models.json`, headers, }); return response; } catch (error) { (0, utils_1.handleHttpError)(error, executeFunctions.getNode()); } } else if (operation === 'read') { const assistantId = executeFunctions.getNodeParameter('assistantId', itemIndex); try { const response = await executeFunctions.helpers.httpRequest({ method: 'GET', url: `${adminUrl}/assistants/${assistantId}.json`, headers, }); return response; } catch (error) { (0, utils_1.handleHttpError)(error, executeFunctions.getNode()); } } else if (operation === 'create' || operation === 'update') { const payload = { assistant: {} }; // Helper to add if not empty const addIfSet = (key, param, defaultVal = '') => { const val = executeFunctions.getNodeParameter(param, itemIndex, defaultVal); if (val !== defaultVal && (typeof val !== 'string' || val.trim() !== '')) { payload.assistant[key] = val; } }; // Add simple string fields addIfSet('title', 'title'); addIfSet('asst_uuid', 'asst_uuid'); addIfSet('avatar', 'avatar'); addIfSet('model', 'model'); addIfSet('reasoning_effort', 'reasoning_effort'); addIfSet('required_login_message', 'required_login_message'); // Add boolean fields const removeAvatar = executeFunctions.getNodeParameter('remove_avatar', itemIndex, false); if (removeAvatar) { payload.assistant.remove_avatar = removeAvatar; } const markdownEnabled = executeFunctions.getNodeParameter('markdown_enabled', itemIndex, false); if (markdownEnabled) { payload.assistant.markdown_enabled = markdownEnabled; } const requiresLogin = executeFunctions.getNodeParameter('requires_login', itemIndex, false); if (requiresLogin) { payload.assistant.requires_login = requiresLogin; } // Add number fields addIfSet('temp', 'temp', 1); // Handle vec_db_ids const vecDbItems = executeFunctions.getNodeParameter('vec_db_ids.vecDbItems', itemIndex, []); if (vecDbItems.length > 0) { payload.assistant.vec_db_ids = vecDbItems.map(item => ({ id: item.id, weight: item.weight, })); } // Handle function_calls const functionCallsStr = executeFunctions.getNodeParameter('function_calls', itemIndex, ''); if (functionCallsStr.trim() !== '') { payload.assistant.function_calls = functionCallsStr .split(',') .map(s => s.trim()); } // Handle communications_flow const flowItems = executeFunctions.getNodeParameter('communications_flow.flowItems', itemIndex, []); if (flowItems.length > 0) { const communicationsFlow = {}; flowItems.forEach((item, index) => { const key = item.key || `flow${index + 1}`; communicationsFlow[key] = { enabled: item.enabled, body: item.body, position: item.position, }; }); payload.assistant.communications_flow = communicationsFlow; } const method = operation === 'create' ? 'POST' : 'PATCH'; let url; if (operation === 'create') { url = `${adminUrl}/assistants.json`; } else { const assistantId = executeFunctions.getNodeParameter('assistantId', itemIndex); url = `${adminUrl}/assistants/${assistantId}.json`; } try { const response = await executeFunctions.helpers.httpRequest({ method, url, body: payload, headers, }); return response; } catch (error) { (0, utils_1.handleHttpError)(error, executeFunctions.getNode()); } } else if (operation === 'delete') { const assistantId = executeFunctions.getNodeParameter('assistantId', itemIndex); try { const response = await executeFunctions.helpers.httpRequest({ method: 'DELETE', url: `${adminUrl}/assistants/${assistantId}.json`, headers, }); return { success: true, deletedId: assistantId }; } catch (error) { (0, utils_1.handleHttpError)(error, executeFunctions.getNode()); } } throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Unknown assistants operation: ${operation}`); } //# sourceMappingURL=assistantsApi.js.map