@thaleslaray/n8n-nodes-elevenlabs
Version:
Nó n8n para integração com a API da ElevenLabs incluindo Speech-to-Text, Text-to-Speech e Conversational AI
303 lines (302 loc) • 13.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ElevenLabsAgents = void 0;
const utils_1 = require("../utils");
class ElevenLabsAgents {
constructor() {
this.description = {
displayName: 'ElevenLabs Agents',
name: 'elevenLabsAgents',
icon: 'file:../elevenlabs.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"]}}',
description: 'Use agentes de conversação da ElevenLabs',
defaults: {
name: 'ElevenLabs Agents',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'elevenLabsApi',
required: true,
},
],
properties: [
{
displayName: 'Operação',
name: 'operation',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Criar Agente',
value: 'createAgent',
description: 'Criar um novo agente',
action: 'Create a new agent',
},
{
name: 'Obter Agente',
value: 'getAgent',
description: 'Obter detalhes de um agente existente',
action: 'Get agent details',
},
{
name: 'Listar Agentes',
value: 'listAgents',
description: 'Listar todos os agentes',
action: 'List all agents',
},
{
name: 'Atualizar Agente',
value: 'updateAgent',
description: 'Atualizar um agente existente',
action: 'Update an existing agent',
},
{
name: 'Excluir Agente',
value: 'deleteAgent',
description: 'Excluir um agente existente',
action: 'Delete an existing agent',
},
{
name: 'Iniciar Sessão',
value: 'startSession',
description: 'Iniciar uma nova sessão de conversação',
action: 'Start a new conversation session',
},
{
name: 'Enviar Mensagem',
value: 'sendMessage',
description: 'Enviar uma mensagem para um agente',
action: 'Send a message to an agent',
},
],
default: 'listAgents',
},
{
displayName: 'Nome do Agente',
name: 'agentName',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
operation: ['createAgent', 'updateAgent'],
},
},
description: 'Nome do agente',
},
{
displayName: 'ID da Voz',
name: 'voiceId',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
operation: ['createAgent', 'updateAgent'],
},
},
description: 'ID da voz que o agente usará',
placeholder: 'ex: 21m00Tcm4TlvDq8ikWAM',
},
{
displayName: 'Instruções do Sistema',
name: 'systemInstruction',
type: 'string',
typeOptions: {
rows: 4,
},
required: true,
default: '',
displayOptions: {
show: {
operation: ['createAgent', 'updateAgent'],
},
},
description: 'Instruções sobre como o agente deve se comportar',
},
{
displayName: 'ID do Agente',
name: 'agentId',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
operation: ['getAgent', 'updateAgent', 'deleteAgent', 'startSession'],
},
},
description: 'ID do agente',
},
{
displayName: 'ID da Sessão',
name: 'sessionId',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
operation: ['sendMessage'],
},
},
description: 'ID da sessão de conversação',
},
{
displayName: 'Mensagem',
name: 'message',
type: 'string',
typeOptions: {
rows: 4,
},
required: true,
default: '',
displayOptions: {
show: {
operation: ['sendMessage'],
},
},
description: 'Mensagem a ser enviada ao agente',
},
{
displayName: 'Opções Avançadas',
name: 'advancedOptions',
type: 'collection',
placeholder: 'Adicionar Opção',
default: {},
options: [
{
displayName: 'Conhecimentos',
name: 'knowledgeBaseIds',
type: 'string',
default: '',
description: 'IDs das bases de conhecimento separados por vírgula',
},
{
displayName: 'Forçar Resposta de Áudio',
name: 'forceAudioResponse',
type: 'boolean',
default: false,
description: 'Sempre gerar resposta em áudio, mesmo que normalmente não seria gerada',
},
{
displayName: 'Limite de Pesquisa',
name: 'searchLimit',
type: 'number',
default: 10,
description: 'Número máximo de resultados para pesquisar na base de conhecimento',
},
{
displayName: 'Idioma',
name: 'language',
type: 'string',
default: 'pt-BR',
description: 'Código de idioma para a sessão (ex: pt-BR, en-US)',
},
],
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
try {
const operation = this.getNodeParameter('operation', i);
let response;
if (operation === 'listAgents') {
const options = await utils_1.createBaseOptions.call(this, 'conversational/agents', 'GET');
response = await this.helpers.request(options);
}
else if (operation === 'getAgent') {
const agentId = this.getNodeParameter('agentId', i);
const options = await utils_1.createBaseOptions.call(this, `conversational/agents/${agentId}`, 'GET');
response = await this.helpers.request(options);
}
else if (operation === 'createAgent') {
const agentName = this.getNodeParameter('agentName', i);
const voiceId = this.getNodeParameter('voiceId', i);
const systemInstruction = this.getNodeParameter('systemInstruction', i);
const advancedOptions = this.getNodeParameter('advancedOptions', i);
const body = {
name: agentName,
voice_id: voiceId,
system_instruction: systemInstruction,
};
if (advancedOptions.knowledgeBaseIds) {
body.knowledge_base_ids = advancedOptions.knowledgeBaseIds
.split(',')
.map(id => id.trim());
}
const options = await utils_1.createBaseOptions.call(this, 'conversational/agents', 'POST', body);
response = await this.helpers.request(options);
}
else if (operation === 'updateAgent') {
const agentId = this.getNodeParameter('agentId', i);
const agentName = this.getNodeParameter('agentName', i);
const voiceId = this.getNodeParameter('voiceId', i);
const systemInstruction = this.getNodeParameter('systemInstruction', i);
const advancedOptions = this.getNodeParameter('advancedOptions', i);
const body = {
name: agentName,
voice_id: voiceId,
system_instruction: systemInstruction,
};
if (advancedOptions.knowledgeBaseIds) {
body.knowledge_base_ids = advancedOptions.knowledgeBaseIds
.split(',')
.map(id => id.trim());
}
const options = await utils_1.createBaseOptions.call(this, `conversational/agents/${agentId}`, 'PUT', body);
response = await this.helpers.request(options);
}
else if (operation === 'deleteAgent') {
const agentId = this.getNodeParameter('agentId', i);
const options = await utils_1.createBaseOptions.call(this, `conversational/agents/${agentId}`, 'DELETE');
response = await this.helpers.request(options);
response = response || { success: true, message: 'Agente excluído com sucesso' };
}
else if (operation === 'startSession') {
const agentId = this.getNodeParameter('agentId', i);
const advancedOptions = this.getNodeParameter('advancedOptions', i);
const body = {};
if (advancedOptions.language) {
body.language = advancedOptions.language;
}
const options = await utils_1.createBaseOptions.call(this, `conversational/agents/${agentId}/sessions`, 'POST', body);
response = await this.helpers.request(options);
}
else if (operation === 'sendMessage') {
const sessionId = this.getNodeParameter('sessionId', i);
const message = this.getNodeParameter('message', i);
const advancedOptions = this.getNodeParameter('advancedOptions', i);
const body = {
message,
};
if (advancedOptions.forceAudioResponse !== undefined) {
body.force_audio_response = advancedOptions.forceAudioResponse;
}
if (advancedOptions.searchLimit !== undefined) {
body.search_limit = advancedOptions.searchLimit;
}
const options = await utils_1.createBaseOptions.call(this, `conversational/sessions/${sessionId}/interaction`, 'POST', body);
response = await this.helpers.request(options);
}
returnData.push((0, utils_1.formatReturnData)(response, i));
}
catch (error) {
const errorResult = utils_1.handleApiError.call(this, error, i);
if (errorResult) {
returnData.push(errorResult);
continue;
}
throw error;
}
}
return [returnData];
}
}
exports.ElevenLabsAgents = ElevenLabsAgents;